Skip to content

Commit a15df41

Browse files
committed
add createClient
1 parent fddfb99 commit a15df41

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

src/client.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { Job } from "./jobs/load";
2+
3+
export interface ClientOptions {
4+
url: string;
5+
apiKey: string;
6+
}
7+
8+
export class Client {
9+
url: string;
10+
apiKey: string;
11+
12+
constructor(options: ClientOptions) {
13+
this.url = options.url;
14+
this.apiKey = options.apiKey;
15+
}
16+
17+
async createJob(job: Job) {
18+
// TODO: reuse fetch error handling
19+
const response = await fetch(`${this.url}/api/jobs`, {
20+
method: "POST",
21+
headers: {
22+
"Content-Type": "application/json",
23+
Authorization: `Bearer ${this.apiKey}`,
24+
},
25+
body: JSON.stringify(job),
26+
});
27+
28+
const data = await response.json();
29+
return data;
30+
}
31+
32+
async streamJob(jobId: string) {
33+
const response = await fetch(`${this.url}/api/jobs/${jobId}/stream`, {
34+
headers: {
35+
Authorization: `Bearer ${this.apiKey}`,
36+
},
37+
});
38+
39+
const reader = response.body!.getReader();
40+
const decoder = new TextDecoder();
41+
42+
async function* streamGenerator() {
43+
while (true) {
44+
const { done, value } = await reader.read();
45+
if (done) break;
46+
47+
const chunk = decoder.decode(value, { stream: true });
48+
const lines = chunk.split("\n").filter((line) => line.trim());
49+
50+
for (const line of lines) {
51+
if (line.startsWith("data: ")) {
52+
const jsonStr = line.slice(6);
53+
try {
54+
const data = JSON.parse(jsonStr);
55+
yield data;
56+
} catch (e) {
57+
console.error("Error parsing SSE data:", e);
58+
}
59+
}
60+
}
61+
}
62+
}
63+
64+
return streamGenerator();
65+
}
66+
}
67+
68+
export function createClient(options: ClientOptions): Client {
69+
return new Client(options);
70+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ export * from "./providers/together";
1818
export * from "./providers/voyage";
1919

2020
export * from "./workflow";
21+
22+
export * from "./client";

src/jobs/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export const JobCostSchema = z.object({
3333

3434
export const JobPerformance = z.object({});
3535

36+
export const RemoteJobSchema = z.object({
37+
id: z.string().optional(),
38+
status: z.enum(["pending", "running", "completed", "failed"]),
39+
createdAt: z.date().optional(),
40+
});
41+
3642
export const BaseJobSchema = z.object({
3743
version: z.string().optional(),
3844
provider: JobProviderSchema,

src/providers/mistral/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function mistral(options?: JobOptions) {
2+
options = options || {};
3+
options.apiKey = options.apiKey || process.env.MISTRAL_API_KEY;
4+
5+
return {
6+
chat(model: string) {},
7+
};
8+
}

0 commit comments

Comments
 (0)