Skip to content

Commit 100e9a2

Browse files
committed
add job.remote()
1 parent 249bda3 commit 100e9a2

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const job = load({
8181
To serialize a job to a JSON object, use the `dump` method:
8282

8383
```ts
84-
const obj = await job.dump();
84+
const payload = job.dump();
8585
```
8686

8787
This allows you to save the job's state for later use, such as storing it in a queue or database.
@@ -90,7 +90,7 @@ To recreate and execute a job from the JSON object, use the `load` function:
9090
```ts
9191
import { load } from "fluent-ai";
9292

93-
const job = load(obj);
93+
const job = load(payload);
9494
await job.run();
9595
```
9696

examples/openai-chat-remote.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { openai, userPrompt } from "../src";
2+
3+
const job = openai()
4+
.chat("gpt-4o-mini")
5+
.messages([{ role: "user", content: "generate a 50 words text" }])
6+
.stream();
7+
8+
const stream = await job.remote();

src/jobs/job.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22
import zodToJsonSchema from "zod-to-json-schema";
33
import { version } from "../../package.json";
4+
import { runRemoteJob, type JobRemoteOptions } from "./remote";
45

56
export const providerSchema = z.enum([
67
"anthropic",
@@ -282,6 +283,11 @@ export class Job {
282283
return await this.handleResponse!(response);
283284
}
284285

286+
async remote(options?: JobRemoteOptions) {
287+
const payload = this.dump() as AIJob;
288+
return runRemoteJob(payload, options);
289+
}
290+
285291
dump() {
286292
return {
287293
version: version,

src/jobs/remote.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { AIJob } from "./job";
2+
import { version } from "../../package.json";
3+
4+
export interface JobRemoteOptions {
5+
apiKey?: string;
6+
baseURL?: string;
7+
}
8+
9+
export async function runRemoteJob(payload: AIJob, options?: JobRemoteOptions) {
10+
options = options || {};
11+
options.apiKey = options.apiKey || process.env.FLUENT_API_KEY;
12+
const baseURL = options.baseURL || "http://localhost:5173/api";
13+
14+
// TODO: add more api types
15+
const job: { id: string; stream: boolean } = await fetch(`${baseURL}/jobs`, {
16+
method: "POST",
17+
headers: {
18+
"Content-Type": "application/json",
19+
"User-Agent": `fluent-ai/${version}`,
20+
Authorization: `Bearer ${options?.apiKey}`,
21+
},
22+
body: JSON.stringify(payload),
23+
}).then((r) => r.json());
24+
25+
console.log("created", job);
26+
27+
const stream = await fetch(`${baseURL}/jobs/${job.id}/stream`);
28+
}

0 commit comments

Comments
 (0)