workers-ai-provider 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,9 @@
1
1
  import {
2
- type LanguageModelV1,
3
- type LanguageModelV1CallWarning,
4
- type LanguageModelV1StreamPart,
5
- UnsupportedFunctionalityError,
2
+ type LanguageModelV1,
3
+ type LanguageModelV1CallWarning,
4
+ type LanguageModelV1StreamPart,
5
+ UnsupportedFunctionalityError,
6
6
  } from "@ai-sdk/provider";
7
- import { z } from "zod";
8
7
  import { convertToWorkersAIChatMessages } from "./convert-to-workersai-chat-messages";
9
8
  import type { WorkersAIChatSettings } from "./workersai-chat-settings";
10
9
  import type { TextGenerationModels } from "./workersai-models";
@@ -14,334 +13,323 @@ import { mapWorkersAIUsage } from "./map-workersai-usage";
14
13
  import type { WorkersAIChatPrompt } from "./workersai-chat-prompt";
15
14
 
16
15
  type WorkersAIChatConfig = {
17
- provider: string;
18
- binding: Ai;
19
- gateway?: GatewayOptions;
16
+ provider: string;
17
+ binding: Ai;
18
+ gateway?: GatewayOptions;
20
19
  };
21
20
 
22
21
  export class WorkersAIChatLanguageModel implements LanguageModelV1 {
23
- readonly specificationVersion = "v1";
24
- readonly defaultObjectGenerationMode = "json";
25
-
26
- readonly modelId: TextGenerationModels;
27
- readonly settings: WorkersAIChatSettings;
28
-
29
- private readonly config: WorkersAIChatConfig;
30
-
31
- constructor(
32
- modelId: TextGenerationModels,
33
- settings: WorkersAIChatSettings,
34
- config: WorkersAIChatConfig
35
- ) {
36
- this.modelId = modelId;
37
- this.settings = settings;
38
- this.config = config;
39
- }
40
-
41
- get provider(): string {
42
- return this.config.provider;
43
- }
44
-
45
- private getArgs({
46
- mode,
47
- prompt,
48
- maxTokens,
49
- temperature,
50
- topP,
51
- frequencyPenalty,
52
- presencePenalty,
53
- seed,
54
- }: Parameters<LanguageModelV1["doGenerate"]>[0]) {
55
- const type = mode.type;
56
-
57
- const warnings: LanguageModelV1CallWarning[] = [];
58
-
59
- if (frequencyPenalty != null) {
60
- warnings.push({
61
- type: "unsupported-setting",
62
- setting: "frequencyPenalty",
63
- });
64
- }
65
-
66
- if (presencePenalty != null) {
67
- warnings.push({
68
- type: "unsupported-setting",
69
- setting: "presencePenalty",
70
- });
71
- }
72
-
73
- const baseArgs = {
74
- // model id:
75
- model: this.modelId,
76
-
77
- // model specific settings:
78
- safe_prompt: this.settings.safePrompt,
79
-
80
- // standardized settings:
81
- max_tokens: maxTokens,
82
- temperature,
83
- top_p: topP,
84
- random_seed: seed,
85
-
86
- // messages:
87
- messages: convertToWorkersAIChatMessages(prompt),
88
- };
89
-
90
- switch (type) {
91
- case "regular": {
92
- return {
93
- args: { ...baseArgs, ...prepareToolsAndToolChoice(mode) },
94
- warnings,
95
- };
96
- }
97
-
98
- case "object-json": {
99
- return {
100
- args: {
101
- ...baseArgs,
102
- response_format: {
103
- type: "json_schema",
104
- json_schema: mode.schema,
105
- },
106
- tools: undefined,
107
- },
108
- warnings,
109
- };
110
- }
111
-
112
- case "object-tool": {
113
- return {
114
- args: {
115
- ...baseArgs,
116
- tool_choice: "any",
117
- tools: [{ type: "function", function: mode.tool }],
118
- },
119
- warnings,
120
- };
121
- }
122
-
123
- // @ts-expect-error - this is unreachable code
124
- // TODO: fixme
125
- case "object-grammar": {
126
- throw new UnsupportedFunctionalityError({
127
- functionality: "object-grammar mode",
128
- });
129
- }
130
-
131
- default: {
132
- const exhaustiveCheck = type satisfies never;
133
- throw new Error(`Unsupported type: ${exhaustiveCheck}`);
134
- }
135
- }
136
- }
137
-
138
- async doGenerate(
139
- options: Parameters<LanguageModelV1["doGenerate"]>[0]
140
- ): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>> {
141
- const { args, warnings } = this.getArgs(options);
142
-
143
- const output = await this.config.binding.run(
144
- args.model,
145
- {
146
- messages: args.messages,
147
- max_tokens: args.max_tokens,
148
- temperature: args.temperature,
149
- tools: args.tools,
150
- top_p: args.top_p,
151
- // @ts-expect-error response_format not yet added to types
152
- response_format: args.response_format,
153
- },
154
- { gateway: this.config.gateway ?? this.settings.gateway }
155
- );
156
-
157
- if (output instanceof ReadableStream) {
158
- throw new Error("This shouldn't happen");
159
- }
160
-
161
- return {
162
- text:
163
- typeof output.response === "object" && output.response !== null
164
- ? JSON.stringify(output.response) // ai-sdk expects a string here
165
- : output.response,
166
- toolCalls: output.tool_calls?.map((toolCall) => ({
167
- toolCallType: "function",
168
- toolCallId: toolCall.name,
169
- toolName: toolCall.name,
170
- args: JSON.stringify(toolCall.arguments || {}),
171
- })),
172
- finishReason: "stop", // TODO: mapWorkersAIFinishReason(response.finish_reason),
173
- rawCall: { rawPrompt: args.messages, rawSettings: args },
174
- usage: mapWorkersAIUsage(output),
175
- warnings,
176
- };
177
- }
178
-
179
- async doStream(
180
- options: Parameters<LanguageModelV1["doStream"]>[0]
181
- ): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>> {
182
- const { args, warnings } = this.getArgs(options);
183
-
184
- // [1] When the latest message is not a tool response, we use the regular generate function
185
- // and simulate it as a streamed response in order to satisfy the AI SDK's interface for
186
- // doStream...
187
- if (args.tools?.length && lastMessageWasUser(args.messages)) {
188
- const response = await this.doGenerate(options);
189
-
190
- if ((response instanceof ReadableStream)) {
191
- throw new Error("This shouldn't happen");
192
- }
193
-
194
- return {
195
- stream: new ReadableStream<LanguageModelV1StreamPart>({
196
- async start(controller) {
197
- if (response.text) {
198
- controller.enqueue({
199
- type: "text-delta",
200
- textDelta: response.text,
201
- })
202
- }
203
- if (response.toolCalls) {
204
- for (const toolCall of response.toolCalls) {
205
- controller.enqueue({
206
- type: "tool-call",
207
- ...toolCall,
208
- })
209
- }
210
- }
211
- controller.enqueue({
212
- type: "finish",
213
- finishReason: "stop",
214
- usage: response.usage,
215
- });
216
- controller.close();
217
- },
218
- }),
219
- rawCall: { rawPrompt: args.messages, rawSettings: args },
220
- warnings,
221
- };
222
- }
223
-
224
- // [2] ...otherwise, we just proceed as normal and stream the response directly from the remote model.
225
- const response = await this.config.binding.run(
226
- args.model,
227
- {
228
- messages: args.messages,
229
- max_tokens: args.max_tokens,
230
- stream: true,
231
- temperature: args.temperature,
232
- tools: args.tools,
233
- top_p: args.top_p,
234
- // @ts-expect-error response_format not yet added to types
235
- response_format: args.response_format,
236
- },
237
- { gateway: this.config.gateway ?? this.settings.gateway }
238
- );
239
-
240
- if (!(response instanceof ReadableStream)) {
241
- throw new Error("This shouldn't happen");
242
- }
243
-
244
- const chunkEvent = events(new Response(response));
245
- let usage = { promptTokens: 0, completionTokens: 0 };
246
-
247
- return {
248
- stream: new ReadableStream<LanguageModelV1StreamPart>({
249
- async start(controller) {
250
- for await (const event of chunkEvent) {
251
- if (!event.data) {
252
- continue;
253
- }
254
- if (event.data === "[DONE]") {
255
- break;
256
- }
257
- const chunk = JSON.parse(event.data);
258
- if (chunk.usage) {
259
- usage = mapWorkersAIUsage(chunk);
260
- }
261
- chunk.response.length &&
262
- controller.enqueue({
263
- type: "text-delta",
264
- textDelta: chunk.response,
265
- });
266
- }
267
- controller.enqueue({
268
- type: "finish",
269
- finishReason: "stop",
270
- usage: usage,
271
- });
272
- controller.close();
273
- },
274
- }),
275
- rawCall: { rawPrompt: args.messages, rawSettings: args },
276
- warnings,
277
- };
278
- }
22
+ readonly specificationVersion = "v1";
23
+ readonly defaultObjectGenerationMode = "json";
24
+
25
+ readonly modelId: TextGenerationModels;
26
+ readonly settings: WorkersAIChatSettings;
27
+
28
+ private readonly config: WorkersAIChatConfig;
29
+
30
+ constructor(
31
+ modelId: TextGenerationModels,
32
+ settings: WorkersAIChatSettings,
33
+ config: WorkersAIChatConfig,
34
+ ) {
35
+ this.modelId = modelId;
36
+ this.settings = settings;
37
+ this.config = config;
38
+ }
39
+
40
+ get provider(): string {
41
+ return this.config.provider;
42
+ }
43
+
44
+ private getArgs({
45
+ mode,
46
+ prompt,
47
+ maxTokens,
48
+ temperature,
49
+ topP,
50
+ frequencyPenalty,
51
+ presencePenalty,
52
+ seed,
53
+ }: Parameters<LanguageModelV1["doGenerate"]>[0]) {
54
+ const type = mode.type;
55
+
56
+ const warnings: LanguageModelV1CallWarning[] = [];
57
+
58
+ if (frequencyPenalty != null) {
59
+ warnings.push({
60
+ type: "unsupported-setting",
61
+ setting: "frequencyPenalty",
62
+ });
63
+ }
64
+
65
+ if (presencePenalty != null) {
66
+ warnings.push({
67
+ type: "unsupported-setting",
68
+ setting: "presencePenalty",
69
+ });
70
+ }
71
+
72
+ const baseArgs = {
73
+ // model id:
74
+ model: this.modelId,
75
+
76
+ // model specific settings:
77
+ safe_prompt: this.settings.safePrompt,
78
+
79
+ // standardized settings:
80
+ max_tokens: maxTokens,
81
+ temperature,
82
+ top_p: topP,
83
+ random_seed: seed,
84
+
85
+ // messages:
86
+ messages: convertToWorkersAIChatMessages(prompt),
87
+ };
88
+
89
+ switch (type) {
90
+ case "regular": {
91
+ return {
92
+ args: { ...baseArgs, ...prepareToolsAndToolChoice(mode) },
93
+ warnings,
94
+ };
95
+ }
96
+
97
+ case "object-json": {
98
+ return {
99
+ args: {
100
+ ...baseArgs,
101
+ response_format: {
102
+ type: "json_schema",
103
+ json_schema: mode.schema,
104
+ },
105
+ tools: undefined,
106
+ },
107
+ warnings,
108
+ };
109
+ }
110
+
111
+ case "object-tool": {
112
+ return {
113
+ args: {
114
+ ...baseArgs,
115
+ tool_choice: "any",
116
+ tools: [{ type: "function", function: mode.tool }],
117
+ },
118
+ warnings,
119
+ };
120
+ }
121
+
122
+ // @ts-expect-error - this is unreachable code
123
+ // TODO: fixme
124
+ case "object-grammar": {
125
+ throw new UnsupportedFunctionalityError({
126
+ functionality: "object-grammar mode",
127
+ });
128
+ }
129
+
130
+ default: {
131
+ const exhaustiveCheck = type satisfies never;
132
+ throw new Error(`Unsupported type: ${exhaustiveCheck}`);
133
+ }
134
+ }
135
+ }
136
+
137
+ async doGenerate(
138
+ options: Parameters<LanguageModelV1["doGenerate"]>[0],
139
+ ): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>> {
140
+ const { args, warnings } = this.getArgs(options);
141
+
142
+ const output = await this.config.binding.run(
143
+ args.model,
144
+ {
145
+ messages: args.messages,
146
+ max_tokens: args.max_tokens,
147
+ temperature: args.temperature,
148
+ tools: args.tools,
149
+ top_p: args.top_p,
150
+ // @ts-expect-error response_format not yet added to types
151
+ response_format: args.response_format,
152
+ },
153
+ { gateway: this.config.gateway ?? this.settings.gateway },
154
+ );
155
+
156
+ if (output instanceof ReadableStream) {
157
+ throw new Error("This shouldn't happen");
158
+ }
159
+
160
+ return {
161
+ text:
162
+ typeof output.response === "object" && output.response !== null
163
+ ? JSON.stringify(output.response) // ai-sdk expects a string here
164
+ : output.response,
165
+ toolCalls: output.tool_calls?.map((toolCall) => ({
166
+ toolCallType: "function",
167
+ toolCallId: toolCall.name,
168
+ toolName: toolCall.name,
169
+ args: JSON.stringify(toolCall.arguments || {}),
170
+ })),
171
+ finishReason: "stop", // TODO: mapWorkersAIFinishReason(response.finish_reason),
172
+ rawCall: { rawPrompt: args.messages, rawSettings: args },
173
+ usage: mapWorkersAIUsage(output),
174
+ warnings,
175
+ };
176
+ }
177
+
178
+ async doStream(
179
+ options: Parameters<LanguageModelV1["doStream"]>[0],
180
+ ): Promise<Awaited<ReturnType<LanguageModelV1["doStream"]>>> {
181
+ const { args, warnings } = this.getArgs(options);
182
+
183
+ // [1] When the latest message is not a tool response, we use the regular generate function
184
+ // and simulate it as a streamed response in order to satisfy the AI SDK's interface for
185
+ // doStream...
186
+ if (args.tools?.length && lastMessageWasUser(args.messages)) {
187
+ const response = await this.doGenerate(options);
188
+
189
+ if (response instanceof ReadableStream) {
190
+ throw new Error("This shouldn't happen");
191
+ }
192
+
193
+ return {
194
+ stream: new ReadableStream<LanguageModelV1StreamPart>({
195
+ async start(controller) {
196
+ if (response.text) {
197
+ controller.enqueue({
198
+ type: "text-delta",
199
+ textDelta: response.text,
200
+ });
201
+ }
202
+ if (response.toolCalls) {
203
+ for (const toolCall of response.toolCalls) {
204
+ controller.enqueue({
205
+ type: "tool-call",
206
+ ...toolCall,
207
+ });
208
+ }
209
+ }
210
+ controller.enqueue({
211
+ type: "finish",
212
+ finishReason: "stop",
213
+ usage: response.usage,
214
+ });
215
+ controller.close();
216
+ },
217
+ }),
218
+ rawCall: { rawPrompt: args.messages, rawSettings: args },
219
+ warnings,
220
+ };
221
+ }
222
+
223
+ // [2] ...otherwise, we just proceed as normal and stream the response directly from the remote model.
224
+ const response = await this.config.binding.run(
225
+ args.model,
226
+ {
227
+ messages: args.messages,
228
+ max_tokens: args.max_tokens,
229
+ stream: true,
230
+ temperature: args.temperature,
231
+ tools: args.tools,
232
+ top_p: args.top_p,
233
+ // @ts-expect-error response_format not yet added to types
234
+ response_format: args.response_format,
235
+ },
236
+ { gateway: this.config.gateway ?? this.settings.gateway },
237
+ );
238
+
239
+ if (!(response instanceof ReadableStream)) {
240
+ throw new Error("This shouldn't happen");
241
+ }
242
+
243
+ const chunkEvent = events(new Response(response));
244
+ let usage = { promptTokens: 0, completionTokens: 0 };
245
+
246
+ return {
247
+ stream: new ReadableStream<LanguageModelV1StreamPart>({
248
+ async start(controller) {
249
+ for await (const event of chunkEvent) {
250
+ if (!event.data) {
251
+ continue;
252
+ }
253
+ if (event.data === "[DONE]") {
254
+ break;
255
+ }
256
+ const chunk = JSON.parse(event.data);
257
+ if (chunk.usage) {
258
+ usage = mapWorkersAIUsage(chunk);
259
+ }
260
+ chunk.response.length &&
261
+ controller.enqueue({
262
+ type: "text-delta",
263
+ textDelta: chunk.response,
264
+ });
265
+ }
266
+ controller.enqueue({
267
+ type: "finish",
268
+ finishReason: "stop",
269
+ usage: usage,
270
+ });
271
+ controller.close();
272
+ },
273
+ }),
274
+ rawCall: { rawPrompt: args.messages, rawSettings: args },
275
+ warnings,
276
+ };
277
+ }
279
278
  }
280
- // limited version of the schema, focussed on what is needed for the implementation
281
- // this approach limits breakages when the API changes and increases efficiency
282
- const workersAIChatResponseSchema = z.object({
283
- response: z.string(),
284
- });
285
-
286
- // limited version of the schema, focussed on what is needed for the implementation
287
- // this approach limits breakages when the API changes and increases efficiency
288
- const workersAIChatChunkSchema = z.instanceof(Uint8Array);
289
279
 
290
280
  function prepareToolsAndToolChoice(
291
- mode: Parameters<LanguageModelV1["doGenerate"]>[0]["mode"] & {
292
- type: "regular";
293
- }
281
+ mode: Parameters<LanguageModelV1["doGenerate"]>[0]["mode"] & {
282
+ type: "regular";
283
+ },
294
284
  ) {
295
- // when the tools array is empty, change it to undefined to prevent errors:
296
- const tools = mode.tools?.length ? mode.tools : undefined;
297
-
298
- if (tools == null) {
299
- return { tools: undefined, tool_choice: undefined };
300
- }
301
-
302
- const mappedTools = tools.map((tool) => ({
303
- type: "function",
304
- function: {
305
- name: tool.name,
306
- // @ts-expect-error - description is not a property of tool
307
- description: tool.description,
308
- // @ts-expect-error - parameters is not a property of tool
309
- parameters: tool.parameters,
310
- },
311
- }));
312
-
313
- const toolChoice = mode.toolChoice;
314
-
315
- if (toolChoice == null) {
316
- return { tools: mappedTools, tool_choice: undefined };
317
- }
318
-
319
- const type = toolChoice.type;
320
-
321
- switch (type) {
322
- case "auto":
323
- return { tools: mappedTools, tool_choice: type };
324
- case "none":
325
- return { tools: mappedTools, tool_choice: type };
326
- case "required":
327
- return { tools: mappedTools, tool_choice: "any" };
328
-
329
- // workersAI does not support tool mode directly,
330
- // so we filter the tools and force the tool choice through 'any'
331
- case "tool":
332
- return {
333
- tools: mappedTools.filter(
334
- (tool) => tool.function.name === toolChoice.toolName
335
- ),
336
- tool_choice: "any",
337
- };
338
- default: {
339
- const exhaustiveCheck = type satisfies never;
340
- throw new Error(`Unsupported tool choice type: ${exhaustiveCheck}`);
341
- }
342
- }
285
+ // when the tools array is empty, change it to undefined to prevent errors:
286
+ const tools = mode.tools?.length ? mode.tools : undefined;
287
+
288
+ if (tools == null) {
289
+ return { tools: undefined, tool_choice: undefined };
290
+ }
291
+
292
+ const mappedTools = tools.map((tool) => ({
293
+ type: "function",
294
+ function: {
295
+ name: tool.name,
296
+ // @ts-expect-error - description is not a property of tool
297
+ description: tool.description,
298
+ // @ts-expect-error - parameters is not a property of tool
299
+ parameters: tool.parameters,
300
+ },
301
+ }));
302
+
303
+ const toolChoice = mode.toolChoice;
304
+
305
+ if (toolChoice == null) {
306
+ return { tools: mappedTools, tool_choice: undefined };
307
+ }
308
+
309
+ const type = toolChoice.type;
310
+
311
+ switch (type) {
312
+ case "auto":
313
+ return { tools: mappedTools, tool_choice: type };
314
+ case "none":
315
+ return { tools: mappedTools, tool_choice: type };
316
+ case "required":
317
+ return { tools: mappedTools, tool_choice: "any" };
318
+
319
+ // workersAI does not support tool mode directly,
320
+ // so we filter the tools and force the tool choice through 'any'
321
+ case "tool":
322
+ return {
323
+ tools: mappedTools.filter((tool) => tool.function.name === toolChoice.toolName),
324
+ tool_choice: "any",
325
+ };
326
+ default: {
327
+ const exhaustiveCheck = type satisfies never;
328
+ throw new Error(`Unsupported tool choice type: ${exhaustiveCheck}`);
329
+ }
330
+ }
343
331
  }
344
332
 
345
333
  function lastMessageWasUser(messages: WorkersAIChatPrompt) {
346
- return messages.length > 0 && messages[messages.length - 1].role === "user";
334
+ return messages.length > 0 && messages[messages.length - 1].role === "user";
347
335
  }