Skip to content

Commit b7ba29d

Browse files
committed
refactor: reorganize chat job types and introduce ChatTool class
1 parent c2ebd78 commit b7ba29d

File tree

3 files changed

+47
-79
lines changed

3 files changed

+47
-79
lines changed

src/jobs/chat/job.ts

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { z, ZodSchema } from "zod";
2-
import zodToJsonSchema from "zod-to-json-schema";
32
import { Job } from "../job";
43
import type {
54
ChatJobSchemaType,
65
ChatJobParams,
76
ChatResultSchema,
7+
ChatStreamOptions,
8+
ResponseFormat,
89
} from "./schema";
10+
import { ChatTool } from "./tool";
911

1012
export function systemPrompt(content: string) {
1113
return { role: "system", content };
@@ -64,81 +66,6 @@ export function tool(name: string) {
6466
return new ChatTool(name);
6567
}
6668

67-
export class ChatTool {
68-
public params: {
69-
name: string;
70-
description?: string;
71-
parameters?: ZodSchema;
72-
};
73-
74-
constructor(name: string) {
75-
this.params = { name };
76-
}
77-
78-
description(description: string) {
79-
this.params.description = description;
80-
return this;
81-
}
82-
83-
parameters(parameters: ZodSchema) {
84-
this.params.parameters = parameters;
85-
return this;
86-
}
87-
88-
toJSON() {
89-
return {
90-
type: "function",
91-
function: {
92-
name: this.params.name,
93-
description: this.params.description,
94-
parameters: zodToJsonSchema(this.params.parameters!),
95-
},
96-
};
97-
}
98-
}
99-
100-
export interface ChatUsage {
101-
input_tokens: number;
102-
output_tokens: number;
103-
}
104-
105-
export interface ChatTextResponse {
106-
usage?: ChatUsage;
107-
text?: string;
108-
}
109-
110-
export interface ChatObjectResponse {
111-
usage?: ChatUsage;
112-
object?: any;
113-
}
114-
115-
export interface StreamOptions {
116-
includeUsage?: boolean;
117-
}
118-
119-
export interface ResponseFormat {
120-
type: "json_object" | "json_schema";
121-
json_schema?: ZodSchema;
122-
}
123-
124-
export type MessageContent =
125-
| string
126-
| { text: string; type: "text" }[]
127-
| {
128-
type: "image";
129-
image_url?: string;
130-
source?: {
131-
type: "base64";
132-
data: string;
133-
media_type: "image/jpeg" | "image/png" | "image/webp" | "image/gif";
134-
};
135-
}[];
136-
137-
export interface Message {
138-
role: "system" | "user" | "assistant";
139-
content: MessageContent;
140-
}
141-
14269
export class ChatJob<T extends ChatJobSchemaType> extends Job<T> {
14370
model: string;
14471
params: ChatJobParams;
@@ -218,7 +145,7 @@ export class ChatJob<T extends ChatJobSchemaType> extends Job<T> {
218145
return this;
219146
}
220147

221-
stream(streamOptions?: StreamOptions) {
148+
stream(streamOptions?: ChatStreamOptions) {
222149
this.params.stream = true;
223150
if (streamOptions) {
224151
this.params.streamOptions = streamOptions;

src/jobs/chat/schema.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,21 @@ export const MessageSchema = z.object({
3434
content: MessageContentSchema,
3535
});
3636

37-
export const StreamOptionsSchema = z.object({
37+
export type Message = z.infer<typeof MessageContentSchema>;
38+
39+
export const ChatStreamOptionsSchema = z.object({
3840
includeUsage: z.boolean().optional(),
3941
});
4042

43+
export type ChatStreamOptions = z.infer<typeof ChatStreamOptionsSchema>;
44+
4145
export const ResponseFormatSchema = z.object({
4246
type: z.enum(["json_object", "json_schema"]),
4347
json_schema: z.any().optional(),
4448
});
4549

50+
export type ResponseFormat = z.infer<typeof ResponseFormatSchema>;
51+
4652
export const ChatToolSchema = z.object({
4753
params: z.object({
4854
name: z.string(),
@@ -61,7 +67,7 @@ export const JsonSchemaDefSchema = z.object({
6167
export const ChatJobParamsSchema = z.object({
6268
temperature: z.number().optional(),
6369
stream: z.boolean().optional(),
64-
streamOptions: StreamOptionsSchema.optional(),
70+
streamOptions: ChatStreamOptionsSchema.optional(),
6571
maxTokens: z.number().optional(),
6672
messages: z.array(MessageSchema),
6773
tools: z.array(ChatToolSchema).optional(),

src/jobs/chat/tool.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ZodSchema } from "zod";
2+
import zodToJsonSchema from "zod-to-json-schema";
3+
4+
export class ChatTool {
5+
public params: {
6+
name: string;
7+
description?: string;
8+
parameters?: ZodSchema;
9+
};
10+
11+
constructor(name: string) {
12+
this.params = { name };
13+
}
14+
15+
description(description: string) {
16+
this.params.description = description;
17+
return this;
18+
}
19+
20+
parameters(parameters: ZodSchema) {
21+
this.params.parameters = parameters;
22+
return this;
23+
}
24+
25+
toJSON() {
26+
return {
27+
type: "function",
28+
function: {
29+
name: this.params.name,
30+
description: this.params.description,
31+
parameters: zodToJsonSchema(this.params.parameters!),
32+
},
33+
};
34+
}
35+
}

0 commit comments

Comments
 (0)