Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit deacf87

Browse files
committed
fix some types and buffering
1 parent 9f9d98c commit deacf87

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

.changeset/fuzzy-pots-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"workers-ai-provider": patch
3+
---
4+
5+
fix some types and buffering

examples/ai-chat/src/server/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ export default {
2121
model: workersai("@cf/meta/llama-3-8b-instruct"),
2222
messages: convertToCoreMessages(messages),
2323
});
24-
return result.toDataStreamResponse();
24+
return result.toDataStreamResponse({
25+
headers: {
26+
"Content-Type": "text/x-workers-ai",
27+
"content-encoding": "identity",
28+
"transfer-encoding": "chunked",
29+
},
30+
});
2531
}
2632

2733
return new Response("Not Found!!", { status: 404 });

packages/ai-provider/src/convert-to-workersai-chat-messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function convertToWorkersAIChatMessages(
6464
break;
6565
}
6666
default: {
67-
const exhaustiveCheck: never = part;
67+
const exhaustiveCheck = part satisfies never;
6868
throw new Error(`Unsupported part: ${exhaustiveCheck}`);
6969
}
7070
}
@@ -96,8 +96,8 @@ export function convertToWorkersAIChatMessages(
9696
break;
9797
}
9898
default: {
99-
const _exhaustiveCheck: never = role;
100-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
99+
const exhaustiveCheck = role satisfies never;
100+
throw new Error(`Unsupported role: ${exhaustiveCheck}`);
101101
}
102102
}
103103
}

packages/ai-provider/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ export function createWorkersAI(options: WorkersAISettings): WorkersAI {
5858

5959
provider.chat = createChatModel;
6060

61-
return provider as WorkersAI;
61+
return provider;
6262
}

packages/ai-provider/src/workersai-chat-language-model.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ export class WorkersAIChatLanguageModel implements LanguageModelV1 {
130130
}
131131

132132
default: {
133-
const _exhaustiveCheck: never = type;
134-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
133+
const exhaustiveCheck = type satisfies never;
134+
throw new Error(`Unsupported type: ${exhaustiveCheck}`);
135135
}
136136
}
137137
}
@@ -141,18 +141,31 @@ export class WorkersAIChatLanguageModel implements LanguageModelV1 {
141141
): Promise<Awaited<ReturnType<LanguageModelV1["doGenerate"]>>> {
142142
const { args, warnings } = this.getArgs(options);
143143

144-
const response = (await this.config.binding.run(args.model, {
144+
const response = await this.config.binding.run(args.model, {
145145
messages: args.messages,
146-
})) as { response: string };
146+
});
147+
148+
if (response instanceof ReadableStream) {
149+
throw new Error("This shouldn't happen");
150+
}
147151

148152
return {
149153
text: response.response,
150-
finishReason: "stop",
154+
// TODO: tool calls
155+
// toolCalls: response.tool_calls?.map((toolCall) => ({
156+
// toolCallType: "function",
157+
// toolCallId: toolCall.name, // TODO: what can the id be?
158+
// toolName: toolCall.name,
159+
// args: JSON.stringify(toolCall.arguments || {}),
160+
// })),
161+
finishReason: "stop", // TODO: mapWorkersAIFinishReason(response.finish_reason),
151162
rawCall: { rawPrompt: args.messages, rawSettings: args },
152163
usage: {
164+
// TODO: mapWorkersAIUsage(response.usage),
153165
promptTokens: 0,
154166
completionTokens: 0,
155167
},
168+
warnings,
156169
};
157170
}
158171

@@ -163,10 +176,14 @@ export class WorkersAIChatLanguageModel implements LanguageModelV1 {
163176

164177
const decoder = new TextDecoder();
165178

166-
const response = (await this.config.binding.run(args.model, {
179+
const response = await this.config.binding.run(args.model, {
167180
messages: args.messages,
168181
stream: true,
169-
})) as ReadableStream;
182+
});
183+
184+
if (!(response instanceof ReadableStream)) {
185+
throw new Error("This shouldn't happen");
186+
}
170187

171188
return {
172189
stream: response.pipeThrough(
@@ -272,8 +289,8 @@ function prepareToolsAndToolChoice(
272289
tool_choice: "any",
273290
};
274291
default: {
275-
const _exhaustiveCheck: never = type;
276-
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
292+
const exhaustiveCheck = type satisfies never;
293+
throw new Error(`Unsupported tool choice type: ${exhaustiveCheck}`);
277294
}
278295
}
279296
}

0 commit comments

Comments
 (0)