ai 3.1.6 → 3.1.7
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.
- package/dist/index.d.mts +71 -6
- package/dist/index.d.ts +71 -6
- package/dist/index.js +184 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +183 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/prompts/dist/index.d.mts +53 -0
- package/prompts/dist/index.d.ts +53 -0
- package/react/dist/index.d.mts +59 -1
- package/react/dist/index.d.ts +59 -1
- package/react/dist/index.js +119 -20
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +119 -20
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +40 -4
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +53 -0
- package/solid/dist/index.d.ts +53 -0
- package/solid/dist/index.js +81 -11
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +81 -11
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +53 -0
- package/svelte/dist/index.d.ts +53 -0
- package/svelte/dist/index.js +81 -11
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +81 -11
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +53 -0
- package/vue/dist/index.d.ts +53 -0
- package/vue/dist/index.js +81 -11
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +81 -11
- package/vue/dist/index.mjs.map +1 -1
package/solid/dist/index.d.mts
CHANGED
@@ -1,5 +1,47 @@
|
|
1
1
|
import { Resource, Accessor, Setter } from 'solid-js';
|
2
2
|
|
3
|
+
/**
|
4
|
+
Typed tool call that is returned by generateText and streamText.
|
5
|
+
It contains the tool call ID, the tool name, and the tool arguments.
|
6
|
+
*/
|
7
|
+
interface ToolCall$1<NAME extends string, ARGS> {
|
8
|
+
/**
|
9
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
10
|
+
*/
|
11
|
+
toolCallId: string;
|
12
|
+
/**
|
13
|
+
Name of the tool that is being called.
|
14
|
+
*/
|
15
|
+
toolName: NAME;
|
16
|
+
/**
|
17
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
18
|
+
*/
|
19
|
+
args: ARGS;
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
Typed tool result that is returned by generateText and streamText.
|
24
|
+
It contains the tool call ID, the tool name, the tool arguments, and the tool result.
|
25
|
+
*/
|
26
|
+
interface ToolResult<NAME extends string, ARGS, RESULT> {
|
27
|
+
/**
|
28
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
29
|
+
*/
|
30
|
+
toolCallId: string;
|
31
|
+
/**
|
32
|
+
Name of the tool that was called.
|
33
|
+
*/
|
34
|
+
toolName: NAME;
|
35
|
+
/**
|
36
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
37
|
+
*/
|
38
|
+
args: ARGS;
|
39
|
+
/**
|
40
|
+
Result of the tool call. This is the result of the tool's execution.
|
41
|
+
*/
|
42
|
+
result: RESULT;
|
43
|
+
}
|
44
|
+
|
3
45
|
interface FunctionCall {
|
4
46
|
/**
|
5
47
|
* The arguments to call the function with, as generated by the model in JSON
|
@@ -68,6 +110,12 @@ interface Function {
|
|
68
110
|
description?: string;
|
69
111
|
}
|
70
112
|
type IdGenerator = () => string;
|
113
|
+
/**
|
114
|
+
Tool invocations are either tool calls or tool results. For each assistant tool call,
|
115
|
+
there is one tool invocation. While the call is in progress, the invocation is a tool call.
|
116
|
+
Once the call is complete, the invocation is a tool result.
|
117
|
+
*/
|
118
|
+
type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
|
71
119
|
/**
|
72
120
|
* Shared types between the API and UI packages.
|
73
121
|
*/
|
@@ -103,6 +151,11 @@ interface Message {
|
|
103
151
|
* Additional message-specific information added on the server via StreamData
|
104
152
|
*/
|
105
153
|
annotations?: JSONValue[] | undefined;
|
154
|
+
/**
|
155
|
+
Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
|
156
|
+
that the assistant made as part of this message.
|
157
|
+
*/
|
158
|
+
toolInvocations?: Array<ToolInvocation>;
|
106
159
|
}
|
107
160
|
type CreateMessage = Omit<Message, 'id'> & {
|
108
161
|
id?: Message['id'];
|
package/solid/dist/index.d.ts
CHANGED
@@ -1,5 +1,47 @@
|
|
1
1
|
import { Resource, Accessor, Setter } from 'solid-js';
|
2
2
|
|
3
|
+
/**
|
4
|
+
Typed tool call that is returned by generateText and streamText.
|
5
|
+
It contains the tool call ID, the tool name, and the tool arguments.
|
6
|
+
*/
|
7
|
+
interface ToolCall$1<NAME extends string, ARGS> {
|
8
|
+
/**
|
9
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
10
|
+
*/
|
11
|
+
toolCallId: string;
|
12
|
+
/**
|
13
|
+
Name of the tool that is being called.
|
14
|
+
*/
|
15
|
+
toolName: NAME;
|
16
|
+
/**
|
17
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
18
|
+
*/
|
19
|
+
args: ARGS;
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
Typed tool result that is returned by generateText and streamText.
|
24
|
+
It contains the tool call ID, the tool name, the tool arguments, and the tool result.
|
25
|
+
*/
|
26
|
+
interface ToolResult<NAME extends string, ARGS, RESULT> {
|
27
|
+
/**
|
28
|
+
ID of the tool call. This ID is used to match the tool call with the tool result.
|
29
|
+
*/
|
30
|
+
toolCallId: string;
|
31
|
+
/**
|
32
|
+
Name of the tool that was called.
|
33
|
+
*/
|
34
|
+
toolName: NAME;
|
35
|
+
/**
|
36
|
+
Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
|
37
|
+
*/
|
38
|
+
args: ARGS;
|
39
|
+
/**
|
40
|
+
Result of the tool call. This is the result of the tool's execution.
|
41
|
+
*/
|
42
|
+
result: RESULT;
|
43
|
+
}
|
44
|
+
|
3
45
|
interface FunctionCall {
|
4
46
|
/**
|
5
47
|
* The arguments to call the function with, as generated by the model in JSON
|
@@ -68,6 +110,12 @@ interface Function {
|
|
68
110
|
description?: string;
|
69
111
|
}
|
70
112
|
type IdGenerator = () => string;
|
113
|
+
/**
|
114
|
+
Tool invocations are either tool calls or tool results. For each assistant tool call,
|
115
|
+
there is one tool invocation. While the call is in progress, the invocation is a tool call.
|
116
|
+
Once the call is complete, the invocation is a tool result.
|
117
|
+
*/
|
118
|
+
type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
|
71
119
|
/**
|
72
120
|
* Shared types between the API and UI packages.
|
73
121
|
*/
|
@@ -103,6 +151,11 @@ interface Message {
|
|
103
151
|
* Additional message-specific information added on the server via StreamData
|
104
152
|
*/
|
105
153
|
annotations?: JSONValue[] | undefined;
|
154
|
+
/**
|
155
|
+
Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
|
156
|
+
that the assistant made as part of this message.
|
157
|
+
*/
|
158
|
+
toolInvocations?: Array<ToolInvocation>;
|
106
159
|
}
|
107
160
|
type CreateMessage = Omit<Message, 'id'> & {
|
108
161
|
id?: Message['id'];
|
package/solid/dist/index.js
CHANGED
@@ -30,6 +30,13 @@ var import_solid_js = require("solid-js");
|
|
30
30
|
var import_solid_swr_store = require("solid-swr-store");
|
31
31
|
var import_swr_store = require("swr-store");
|
32
32
|
|
33
|
+
// shared/generate-id.ts
|
34
|
+
var import_non_secure = require("nanoid/non-secure");
|
35
|
+
var generateId = (0, import_non_secure.customAlphabet)(
|
36
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
37
|
+
7
|
38
|
+
);
|
39
|
+
|
33
40
|
// shared/stream-parts.ts
|
34
41
|
var textStreamPart = {
|
35
42
|
code: "0",
|
@@ -126,7 +133,7 @@ var dataMessageStreamPart = {
|
|
126
133
|
};
|
127
134
|
}
|
128
135
|
};
|
129
|
-
var
|
136
|
+
var toolCallsStreamPart = {
|
130
137
|
code: "7",
|
131
138
|
name: "tool_calls",
|
132
139
|
parse: (value) => {
|
@@ -153,6 +160,36 @@ var messageAnnotationsStreamPart = {
|
|
153
160
|
return { type: "message_annotations", value };
|
154
161
|
}
|
155
162
|
};
|
163
|
+
var toolCallStreamPart = {
|
164
|
+
code: "9",
|
165
|
+
name: "tool_call",
|
166
|
+
parse: (value) => {
|
167
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
168
|
+
throw new Error(
|
169
|
+
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
170
|
+
);
|
171
|
+
}
|
172
|
+
return {
|
173
|
+
type: "tool_call",
|
174
|
+
value
|
175
|
+
};
|
176
|
+
}
|
177
|
+
};
|
178
|
+
var toolResultStreamPart = {
|
179
|
+
code: "a",
|
180
|
+
name: "tool_result",
|
181
|
+
parse: (value) => {
|
182
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
|
183
|
+
throw new Error(
|
184
|
+
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
185
|
+
);
|
186
|
+
}
|
187
|
+
return {
|
188
|
+
type: "tool_result",
|
189
|
+
value
|
190
|
+
};
|
191
|
+
}
|
192
|
+
};
|
156
193
|
var streamParts = [
|
157
194
|
textStreamPart,
|
158
195
|
functionCallStreamPart,
|
@@ -161,8 +198,10 @@ var streamParts = [
|
|
161
198
|
assistantMessageStreamPart,
|
162
199
|
assistantControlDataStreamPart,
|
163
200
|
dataMessageStreamPart,
|
201
|
+
toolCallsStreamPart,
|
202
|
+
messageAnnotationsStreamPart,
|
164
203
|
toolCallStreamPart,
|
165
|
-
|
204
|
+
toolResultStreamPart
|
166
205
|
];
|
167
206
|
var streamPartsByCode = {
|
168
207
|
[textStreamPart.code]: textStreamPart,
|
@@ -172,8 +211,10 @@ var streamPartsByCode = {
|
|
172
211
|
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
173
212
|
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
174
213
|
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
214
|
+
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
215
|
+
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
175
216
|
[toolCallStreamPart.code]: toolCallStreamPart,
|
176
|
-
[
|
217
|
+
[toolResultStreamPart.code]: toolResultStreamPart
|
177
218
|
};
|
178
219
|
var StreamStringPrefixes = {
|
179
220
|
[textStreamPart.name]: textStreamPart.code,
|
@@ -183,8 +224,10 @@ var StreamStringPrefixes = {
|
|
183
224
|
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
184
225
|
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
185
226
|
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
227
|
+
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
228
|
+
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
186
229
|
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
187
|
-
[
|
230
|
+
[toolResultStreamPart.name]: toolResultStreamPart.code
|
188
231
|
};
|
189
232
|
var validCodes = streamParts.map((part) => part.code);
|
190
233
|
var parseStreamPart = (line) => {
|
@@ -245,13 +288,6 @@ async function* readDataStream(reader, {
|
|
245
288
|
}
|
246
289
|
}
|
247
290
|
|
248
|
-
// shared/generate-id.ts
|
249
|
-
var import_non_secure = require("nanoid/non-secure");
|
250
|
-
var generateId = (0, import_non_secure.customAlphabet)(
|
251
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
252
|
-
7
|
253
|
-
);
|
254
|
-
|
255
291
|
// shared/parse-complex-response.ts
|
256
292
|
function assignAnnotationsToMessage(message, annotations) {
|
257
293
|
if (!message || !annotations || !annotations.length)
|
@@ -289,6 +325,40 @@ async function parseComplexResponse({
|
|
289
325
|
};
|
290
326
|
}
|
291
327
|
}
|
328
|
+
if (type === "tool_call") {
|
329
|
+
if (prefixMap.text == null) {
|
330
|
+
prefixMap.text = {
|
331
|
+
id: generateId2(),
|
332
|
+
role: "assistant",
|
333
|
+
content: "",
|
334
|
+
createdAt
|
335
|
+
};
|
336
|
+
}
|
337
|
+
if (prefixMap.text.toolInvocations == null) {
|
338
|
+
prefixMap.text.toolInvocations = [];
|
339
|
+
}
|
340
|
+
prefixMap.text.toolInvocations.push(value);
|
341
|
+
} else if (type === "tool_result") {
|
342
|
+
if (prefixMap.text == null) {
|
343
|
+
prefixMap.text = {
|
344
|
+
id: generateId2(),
|
345
|
+
role: "assistant",
|
346
|
+
content: "",
|
347
|
+
createdAt
|
348
|
+
};
|
349
|
+
}
|
350
|
+
if (prefixMap.text.toolInvocations == null) {
|
351
|
+
prefixMap.text.toolInvocations = [];
|
352
|
+
}
|
353
|
+
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
354
|
+
(invocation) => invocation.toolCallId === value.toolCallId
|
355
|
+
);
|
356
|
+
if (toolInvocationIndex !== -1) {
|
357
|
+
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
358
|
+
} else {
|
359
|
+
prefixMap.text.toolInvocations.push(value);
|
360
|
+
}
|
361
|
+
}
|
292
362
|
let functionCallMessage = null;
|
293
363
|
if (type === "function_call") {
|
294
364
|
prefixMap["function_call"] = {
|
package/solid/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../index.ts","../use-chat.ts","../../shared/stream-parts.ts","../../shared/read-data-stream.ts","../../shared/generate-id.ts","../../shared/parse-complex-response.ts","../../shared/utils.ts","../../shared/call-chat-api.ts","../../shared/process-chat-stream.ts","../use-completion.ts","../../shared/call-completion-api.ts"],"sourcesContent":["export * from './use-chat';\nexport * from './use-completion';\n","import { Accessor, Resource, Setter, createSignal } from 'solid-js';\nimport { useSWRStore } from 'solid-swr-store';\nimport { createSWRStore } from 'swr-store';\nimport { callChatApi } from '../shared/call-chat-api';\nimport { generateId as generateIdFunc } from '../shared/generate-id';\nimport { processChatStream } from '../shared/process-chat-stream';\nimport type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\n JSONValue,\n Message,\n UseChatOptions,\n} from '../shared/types';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Resource<Message[]>;\n /** The error object of the API request */\n error: Accessor<undefined | Error>;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (messages: Message[]) => void;\n /** The current value of the input */\n input: Accessor<string>;\n /** Signal setter to update the input value */\n setInput: Setter<string>;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;\n /** Whether the API request is in progress */\n isLoading: Accessor<boolean>;\n /** Additional data added on the server via StreamData */\n data: Accessor<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\nconst store: Record<string, Message[] | undefined> = {};\nconst chatApiStore = createSWRStore<Message[], string[]>({\n get: async (key: string) => {\n return store[key] ?? [];\n },\n});\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n streamMode,\n generateId = generateIdFunc,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n\n // Because of the `initialData` option, the `data` will never be `undefined`:\n const messages = useSWRStore(chatApiStore, () => [key], {\n initialData: initialMessages,\n }) as Resource<Message[]>;\n\n const mutate = (data: Message[]) => {\n store[key] = data;\n return chatApiStore.mutate([key], {\n status: 'success',\n data,\n });\n };\n\n const [error, setError] = createSignal<undefined | Error>(undefined);\n const [streamData, setStreamData] = createSignal<JSONValue[] | undefined>(\n undefined,\n );\n const [isLoading, setIsLoading] = createSignal(false);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data }: ChatRequestOptions = {},\n ) {\n try {\n setError(undefined);\n setIsLoading(true);\n\n abortController = new AbortController();\n\n const getCurrentMessages = () =>\n chatApiStore.get([key], {\n shouldRevalidate: false,\n });\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = getCurrentMessages();\n mutate(messagesSnapshot);\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = streamData() ?? [];\n\n return await callChatApi({\n api,\n messages: sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({ role, content, name, function_call }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(function_call !== undefined && {\n function_call,\n }),\n }),\n ),\n body: {\n data: chatRequest.data,\n ...body,\n ...options?.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...options?.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n setStreamData([...existingData, ...(data ?? [])]);\n },\n onFinish,\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (previousMessages.status === 'success') {\n mutate(previousMessages.data);\n }\n },\n generateId,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => getCurrentMessages().data,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n setIsLoading(false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n return triggerRequest(\n (messages() ?? []).concat(message as Message),\n options,\n );\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages();\n if (!messagesSnapshot || messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (messages: Message[]) => {\n mutate(messages);\n };\n\n const [input, setInput] = createSignal(initialInput);\n\n const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {\n e.preventDefault();\n const inputValue = input();\n if (!inputValue) return;\n\n append(\n {\n content: inputValue,\n role: 'user',\n createdAt: new Date(),\n },\n options,\n );\n\n setInput('');\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import {\n AssistantMessage,\n DataMessage,\n FunctionCall,\n JSONValue,\n ToolCall,\n} from './types';\nimport { StreamString } from './utils';\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<'0', 'text', string> = {\n code: '0',\n name: 'text',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: 'text', value };\n },\n};\n\nconst functionCallStreamPart: StreamPart<\n '1',\n 'function_call',\n { function_call: FunctionCall }\n> = {\n code: '1',\n name: 'function_call',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('function_call' in value) ||\n typeof value.function_call !== 'object' ||\n value.function_call == null ||\n !('name' in value.function_call) ||\n !('arguments' in value.function_call) ||\n typeof value.function_call.name !== 'string' ||\n typeof value.function_call.arguments !== 'string'\n ) {\n throw new Error(\n '\"function_call\" parts expect an object with a \"function_call\" property.',\n );\n }\n\n return {\n type: 'function_call',\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>> = {\n code: '2',\n name: 'data',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: 'data', value };\n },\n};\n\nconst errorStreamPart: StreamPart<'3', 'error', string> = {\n code: '3',\n name: 'error',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: 'error', value };\n },\n};\n\nconst assistantMessageStreamPart: StreamPart<\n '4',\n 'assistant_message',\n AssistantMessage\n> = {\n code: '4',\n name: 'assistant_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('id' in value) ||\n !('role' in value) ||\n !('content' in value) ||\n typeof value.id !== 'string' ||\n typeof value.role !== 'string' ||\n value.role !== 'assistant' ||\n !Array.isArray(value.content) ||\n !value.content.every(\n item =>\n item != null &&\n typeof item === 'object' &&\n 'type' in item &&\n item.type === 'text' &&\n 'text' in item &&\n item.text != null &&\n typeof item.text === 'object' &&\n 'value' in item.text &&\n typeof item.text.value === 'string',\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: 'assistant_message',\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlDataStreamPart: StreamPart<\n '5',\n 'assistant_control_data',\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: '5',\n name: 'assistant_control_data',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('threadId' in value) ||\n !('messageId' in value) ||\n typeof value.threadId !== 'string' ||\n typeof value.messageId !== 'string'\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: 'assistant_control_data',\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage> = {\n code: '6',\n name: 'data_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('role' in value) ||\n !('data' in value) ||\n typeof value.role !== 'string' ||\n value.role !== 'data'\n ) {\n throw new Error(\n '\"data_message\" parts expect an object with a \"role\" and \"data\" property.',\n );\n }\n\n return {\n type: 'data_message',\n value: value as DataMessage,\n };\n },\n};\n\nconst toolCallStreamPart: StreamPart<\n '7',\n 'tool_calls',\n { tool_calls: ToolCall[] }\n> = {\n code: '7',\n name: 'tool_calls',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('tool_calls' in value) ||\n typeof value.tool_calls !== 'object' ||\n value.tool_calls == null ||\n !Array.isArray(value.tool_calls) ||\n value.tool_calls.some(\n tc =>\n tc == null ||\n typeof tc !== 'object' ||\n !('id' in tc) ||\n typeof tc.id !== 'string' ||\n !('type' in tc) ||\n typeof tc.type !== 'string' ||\n !('function' in tc) ||\n tc.function == null ||\n typeof tc.function !== 'object' ||\n !('arguments' in tc.function) ||\n typeof tc.function.name !== 'string' ||\n typeof tc.function.arguments !== 'string',\n )\n ) {\n throw new Error(\n '\"tool_calls\" parts expect an object with a ToolCallPayload.',\n );\n }\n\n return {\n type: 'tool_calls',\n value: value as unknown as { tool_calls: ToolCall[] },\n };\n },\n};\n\nconst messageAnnotationsStreamPart: StreamPart<\n '8',\n 'message_annotations',\n Array<JSONValue>\n> = {\n code: '8',\n name: 'message_annotations',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"message_annotations\" parts expect an array value.');\n }\n\n return { type: 'message_annotations', value };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessageStreamPart,\n assistantControlDataStreamPart,\n dataMessageStreamPart,\n toolCallStreamPart,\n messageAnnotationsStreamPart,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessageStreamPart\n | typeof assistantControlDataStreamPart\n | typeof dataMessageStreamPart\n | typeof toolCallStreamPart\n | typeof messageAnnotationsStreamPart;\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessageStreamPart.parse>\n | ReturnType<typeof assistantControlDataStreamPart.parse>\n | ReturnType<typeof dataMessageStreamPart.parse>\n | ReturnType<typeof toolCallStreamPart.parse>\n | ReturnType<typeof messageAnnotationsStreamPart.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessageStreamPart.code]: assistantMessageStreamPart,\n [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,\n [dataMessageStreamPart.code]: dataMessageStreamPart,\n [toolCallStreamPart.code]: toolCallStreamPart,\n [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n * - 6: (OpenAI) tool_call responses\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n * 6: {\"tool_call\": {\"id\": \"tool_0\", \"type\": \"function\", \"function\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,\n [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,\n [dataMessageStreamPart.name]: dataMessageStreamPart.code,\n [toolCallStreamPart.name]: toolCallStreamPart.code,\n [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,\n} as const;\n\nexport const validCodes = streamParts.map(part => part.code);\n\n/**\nParses a stream part from a string.\n\n@param line The string to parse.\n@returns The parsed stream part.\n@throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(':');\n\n if (firstSeparatorIndex === -1) {\n throw new Error('Failed to parse stream string. No separator found.');\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\nPrepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\nand appends a new line.\n\nIt ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find(part => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n","import { StreamPartType, parseStreamPart } from './stream-parts';\n\nconst NEWLINE = '\\n'.charCodeAt(0);\n\n// concatenates all the chunks into a single Uint8Array\nfunction concatChunks(chunks: Uint8Array[], totalLength: number) {\n const concatenatedChunks = new Uint8Array(totalLength);\n\n let offset = 0;\n for (const chunk of chunks) {\n concatenatedChunks.set(chunk, offset);\n offset += chunk.length;\n }\n chunks.length = 0;\n\n return concatenatedChunks;\n}\n\n/**\nConverts a ReadableStreamDefaultReader into an async generator that yields\nStreamPart objects.\n\n@param reader \n Reader for the stream to read from.\n@param isAborted\n Optional function that returns true if the request has been aborted.\n If the function returns true, the generator will stop reading the stream.\n If the function is not provided, the generator will not stop reading the stream.\n */\nexport async function* readDataStream(\n reader: ReadableStreamDefaultReader<Uint8Array>,\n {\n isAborted,\n }: {\n isAborted?: () => boolean;\n } = {},\n): AsyncGenerator<StreamPartType> {\n // implementation note: this slightly more complex algorithm is required\n // to pass the tests in the edge environment.\n\n const decoder = new TextDecoder();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { value } = await reader.read();\n\n if (value) {\n chunks.push(value);\n totalLength += value.length;\n if (value[value.length - 1] !== NEWLINE) {\n // if the last character is not a newline, we have not read the whole JSON value\n continue;\n }\n }\n\n if (chunks.length === 0) {\n break; // we have reached the end of the stream\n }\n\n const concatenatedChunks = concatChunks(chunks, totalLength);\n totalLength = 0;\n\n const streamParts = decoder\n .decode(concatenatedChunks, { stream: true })\n .split('\\n')\n .filter(line => line !== '') // splitting leaves an empty string at the end\n .map(parseStreamPart);\n\n for (const streamPart of streamParts) {\n yield streamPart;\n }\n\n // The request has been aborted, stop reading the stream.\n if (isAborted?.()) {\n reader.cancel();\n break;\n }\n }\n}\n","import { customAlphabet } from 'nanoid/non-secure';\n\n/**\n * Generates a 7-character random string to use for IDs. Not secure.\n */\nexport const generateId = customAlphabet(\n '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',\n 7,\n);\n","import { readDataStream } from './read-data-stream';\nimport type { FunctionCall, JSONValue, Message, ToolCall } from './types';\nimport { generateId as generateIdFunction } from './generate-id';\n\ntype PrefixMap = {\n text?: Message;\n function_call?: Message & {\n role: 'assistant';\n function_call: FunctionCall;\n };\n tool_calls?: Message & {\n role: 'assistant';\n tool_calls: ToolCall[];\n };\n data: JSONValue[];\n};\n\nfunction assignAnnotationsToMessage<T extends Message | null | undefined>(\n message: T,\n annotations: JSONValue[] | undefined,\n): T {\n if (!message || !annotations || !annotations.length) return message;\n return { ...message, annotations: [...annotations] } as T;\n}\n\nexport async function parseComplexResponse({\n reader,\n abortControllerRef,\n update,\n onFinish,\n generateId = generateIdFunction,\n getCurrentDate = () => new Date(),\n}: {\n reader: ReadableStreamDefaultReader<Uint8Array>;\n abortControllerRef?: {\n current: AbortController | null;\n };\n update: (merged: Message[], data: JSONValue[] | undefined) => void;\n onFinish?: (prefixMap: PrefixMap) => void;\n generateId?: () => string;\n getCurrentDate?: () => Date;\n}) {\n const createdAt = getCurrentDate();\n const prefixMap: PrefixMap = {\n data: [],\n };\n\n // keep list of current message annotations for message\n let message_annotations: JSONValue[] | undefined = undefined;\n\n // we create a map of each prefix, and for each prefixed message we push to the map\n for await (const { type, value } of readDataStream(reader, {\n isAborted: () => abortControllerRef?.current === null,\n })) {\n if (type === 'text') {\n if (prefixMap['text']) {\n prefixMap['text'] = {\n ...prefixMap['text'],\n content: (prefixMap['text'].content || '') + value,\n };\n } else {\n prefixMap['text'] = {\n id: generateId(),\n role: 'assistant',\n content: value,\n createdAt,\n };\n }\n }\n\n let functionCallMessage: Message | null | undefined = null;\n\n if (type === 'function_call') {\n prefixMap['function_call'] = {\n id: generateId(),\n role: 'assistant',\n content: '',\n function_call: value.function_call,\n name: value.function_call.name,\n createdAt,\n };\n\n functionCallMessage = prefixMap['function_call'];\n }\n\n let toolCallMessage: Message | null | undefined = null;\n\n if (type === 'tool_calls') {\n prefixMap['tool_calls'] = {\n id: generateId(),\n role: 'assistant',\n content: '',\n tool_calls: value.tool_calls,\n createdAt,\n };\n\n toolCallMessage = prefixMap['tool_calls'];\n }\n\n if (type === 'data') {\n prefixMap['data'].push(...value);\n }\n\n let responseMessage = prefixMap['text'];\n\n if (type === 'message_annotations') {\n if (!message_annotations) {\n message_annotations = [...value];\n } else {\n message_annotations.push(...value);\n }\n\n // Update any existing message with the latest annotations\n functionCallMessage = assignAnnotationsToMessage(\n prefixMap['function_call'],\n message_annotations,\n );\n toolCallMessage = assignAnnotationsToMessage(\n prefixMap['tool_calls'],\n message_annotations,\n );\n responseMessage = assignAnnotationsToMessage(\n prefixMap['text'],\n message_annotations,\n );\n }\n\n // keeps the prefixMap up to date with the latest annotations, even if annotations preceded the message\n if (message_annotations?.length) {\n const messagePrefixKeys: (keyof PrefixMap)[] = [\n 'text',\n 'function_call',\n 'tool_calls',\n ];\n messagePrefixKeys.forEach(key => {\n if (prefixMap[key]) {\n (prefixMap[key] as Message).annotations = [...message_annotations!];\n }\n });\n }\n\n // We add function & tool calls and response messages to the messages[], but data is its own thing\n const merged = [functionCallMessage, toolCallMessage, responseMessage]\n .filter(Boolean)\n .map(message => ({\n ...assignAnnotationsToMessage(message, message_annotations),\n })) as Message[];\n\n update(merged, [...prefixMap['data']]); // make a copy of the data array\n }\n\n onFinish?.(prefixMap);\n\n return {\n messages: [\n prefixMap.text,\n prefixMap.function_call,\n prefixMap.tool_calls,\n ].filter(Boolean) as Message[],\n data: prefixMap.data,\n };\n}\n","import {\n StreamPartType,\n StreamStringPrefixes,\n parseStreamPart,\n} from './stream-parts';\n\nexport * from './generate-id';\n\n// TODO remove (breaking change)\nexport { generateId as nanoid } from './generate-id';\n\n// Export stream data utilities for custom stream implementations,\n// both on the client and server side.\nexport type { StreamPart } from './stream-parts';\nexport { formatStreamPart, parseStreamPart } from './stream-parts';\nexport { readDataStream } from './read-data-stream';\n\n// simple decoder signatures:\nfunction createChunkDecoder(): (chunk: Uint8Array | undefined) => string;\nfunction createChunkDecoder(\n complex: false,\n): (chunk: Uint8Array | undefined) => string;\n// complex decoder signature:\nfunction createChunkDecoder(\n complex: true,\n): (chunk: Uint8Array | undefined) => StreamPartType[];\n// combined signature for when the client calls this function with a boolean:\nfunction createChunkDecoder(\n complex?: boolean,\n): (chunk: Uint8Array | undefined) => StreamPartType[] | string;\nfunction createChunkDecoder(complex?: boolean) {\n const decoder = new TextDecoder();\n\n if (!complex) {\n return function (chunk: Uint8Array | undefined): string {\n if (!chunk) return '';\n return decoder.decode(chunk, { stream: true });\n };\n }\n\n return function (chunk: Uint8Array | undefined) {\n const decoded = decoder\n .decode(chunk, { stream: true })\n .split('\\n')\n .filter(line => line !== ''); // splitting leaves an empty string at the end\n\n return decoded.map(parseStreamPart).filter(Boolean);\n };\n}\n\nexport { createChunkDecoder };\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith('\\n');\n\nexport type StreamString =\n `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n","import { parseComplexResponse } from './parse-complex-response';\nimport { IdGenerator, JSONValue, Message } from './types';\nimport { createChunkDecoder } from './utils';\n\nexport async function callChatApi({\n api,\n messages,\n body,\n streamMode = 'stream-data',\n credentials,\n headers,\n abortController,\n restoreMessagesOnFailure,\n onResponse,\n onUpdate,\n onFinish,\n generateId,\n}: {\n api: string;\n messages: Omit<Message, 'id'>[];\n body: Record<string, any>;\n streamMode?: 'stream-data' | 'text';\n credentials?: RequestCredentials;\n headers?: HeadersInit;\n abortController?: () => AbortController | null;\n restoreMessagesOnFailure: () => void;\n onResponse?: (response: Response) => void | Promise<void>;\n onUpdate: (merged: Message[], data: JSONValue[] | undefined) => void;\n onFinish?: (message: Message) => void;\n generateId: IdGenerator;\n}) {\n const response = await fetch(api, {\n method: 'POST',\n body: JSON.stringify({\n messages,\n ...body,\n }),\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n signal: abortController?.()?.signal,\n credentials,\n }).catch(err => {\n restoreMessagesOnFailure();\n throw err;\n });\n\n if (onResponse) {\n try {\n await onResponse(response);\n } catch (err) {\n throw err;\n }\n }\n\n if (!response.ok) {\n restoreMessagesOnFailure();\n throw new Error(\n (await response.text()) || 'Failed to fetch the chat response.',\n );\n }\n\n if (!response.body) {\n throw new Error('The response body is empty.');\n }\n\n const reader = response.body.getReader();\n\n switch (streamMode) {\n case 'text': {\n const decoder = createChunkDecoder();\n\n const resultMessage = {\n id: generateId(),\n createdAt: new Date(),\n role: 'assistant' as const,\n content: '',\n };\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n resultMessage.content += decoder(value);\n resultMessage.id = generateId();\n\n // note: creating a new message object is required for Solid.js streaming\n onUpdate([{ ...resultMessage }], []);\n\n // The request has been aborted, stop reading the stream.\n if (abortController?.() === null) {\n reader.cancel();\n break;\n }\n }\n\n onFinish?.(resultMessage);\n\n return {\n messages: [resultMessage],\n data: [],\n };\n }\n\n case 'stream-data': {\n return await parseComplexResponse({\n reader,\n abortControllerRef:\n abortController != null ? { current: abortController() } : undefined,\n update: onUpdate,\n onFinish(prefixMap) {\n if (onFinish && prefixMap.text != null) {\n onFinish(prefixMap.text);\n }\n },\n generateId,\n });\n }\n\n default: {\n const exhaustiveCheck: never = streamMode;\n throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);\n }\n }\n}\n","import {\n ChatRequest,\n FunctionCall,\n JSONValue,\n Message,\n ToolCall,\n} from './types';\n\nexport async function processChatStream({\n getStreamedResponse,\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest,\n getCurrentMessages,\n}: {\n getStreamedResponse: () => Promise<\n Message | { messages: Message[]; data: JSONValue[] }\n >;\n experimental_onFunctionCall?: (\n chatMessages: Message[],\n functionCall: FunctionCall,\n ) => Promise<void | ChatRequest>;\n experimental_onToolCall?: (\n chatMessages: Message[],\n toolCalls: ToolCall[],\n ) => Promise<void | ChatRequest>;\n updateChatRequest: (chatRequest: ChatRequest) => void;\n getCurrentMessages: () => Message[];\n}) {\n while (true) {\n // TODO-STREAMDATA: This should be { const { messages: streamedResponseMessages, data } =\n // await getStreamedResponse(} once Stream Data is not experimental\n const messagesAndDataOrJustMessage = await getStreamedResponse();\n\n // Using experimental stream data\n if ('messages' in messagesAndDataOrJustMessage) {\n let hasFollowingResponse = false;\n\n for (const message of messagesAndDataOrJustMessage.messages) {\n // See if the message has a complete function call or tool call\n if (\n (message.function_call === undefined ||\n typeof message.function_call === 'string') &&\n (message.tool_calls === undefined ||\n typeof message.tool_calls === 'string')\n ) {\n continue;\n }\n\n hasFollowingResponse = true;\n // Try to handle function call\n if (experimental_onFunctionCall) {\n const functionCall = message.function_call;\n // Make sure functionCall is an object\n // If not, we got tool calls instead of function calls\n if (typeof functionCall !== 'object') {\n console.warn(\n 'experimental_onFunctionCall should not be defined when using tools',\n );\n continue;\n }\n\n // User handles the function call in their own functionCallHandler.\n // The \"arguments\" key of the function call object will still be a string which will have to be parsed in the function handler.\n // If the \"arguments\" JSON is malformed due to model error the user will have to handle that themselves.\n\n const functionCallResponse: ChatRequest | void =\n await experimental_onFunctionCall(\n getCurrentMessages(),\n functionCall,\n );\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (functionCallResponse === undefined) {\n hasFollowingResponse = false;\n break;\n }\n\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n updateChatRequest(functionCallResponse);\n }\n // Try to handle tool call\n if (experimental_onToolCall) {\n const toolCalls = message.tool_calls;\n // Make sure toolCalls is an array of objects\n // If not, we got function calls instead of tool calls\n if (\n !Array.isArray(toolCalls) ||\n toolCalls.some(toolCall => typeof toolCall !== 'object')\n ) {\n console.warn(\n 'experimental_onToolCall should not be defined when using tools',\n );\n continue;\n }\n\n // User handles the function call in their own functionCallHandler.\n // The \"arguments\" key of the function call object will still be a string which will have to be parsed in the function handler.\n // If the \"arguments\" JSON is malformed due to model error the user will have to handle that themselves.\n const toolCallResponse: ChatRequest | void =\n await experimental_onToolCall(getCurrentMessages(), toolCalls);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (toolCallResponse === undefined) {\n hasFollowingResponse = false;\n break;\n }\n\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n updateChatRequest(toolCallResponse);\n }\n }\n if (!hasFollowingResponse) {\n break;\n }\n } else {\n const streamedResponseMessage = messagesAndDataOrJustMessage;\n\n // TODO-STREAMDATA: Remove this once Stream Data is not experimental\n if (\n (streamedResponseMessage.function_call === undefined ||\n typeof streamedResponseMessage.function_call === 'string') &&\n (streamedResponseMessage.tool_calls === undefined ||\n typeof streamedResponseMessage.tool_calls === 'string')\n ) {\n break;\n }\n\n // If we get here and are expecting a function call, the message should have one, if not warn and continue\n if (experimental_onFunctionCall) {\n const functionCall = streamedResponseMessage.function_call;\n if (!(typeof functionCall === 'object')) {\n console.warn(\n 'experimental_onFunctionCall should not be defined when using tools',\n );\n continue;\n }\n const functionCallResponse: ChatRequest | void =\n await experimental_onFunctionCall(getCurrentMessages(), functionCall);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (functionCallResponse === undefined) break;\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n fixFunctionCallArguments(functionCallResponse);\n updateChatRequest(functionCallResponse);\n }\n // If we get here and are expecting a tool call, the message should have one, if not warn and continue\n if (experimental_onToolCall) {\n const toolCalls = streamedResponseMessage.tool_calls;\n if (!(typeof toolCalls === 'object')) {\n console.warn(\n 'experimental_onToolCall should not be defined when using functions',\n );\n continue;\n }\n const toolCallResponse: ChatRequest | void =\n await experimental_onToolCall(getCurrentMessages(), toolCalls);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (toolCallResponse === undefined) break;\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n fixFunctionCallArguments(toolCallResponse);\n updateChatRequest(toolCallResponse);\n }\n\n // Make sure function call arguments are sent back to the API as a string\n function fixFunctionCallArguments(response: ChatRequest) {\n for (const message of response.messages) {\n if (message.tool_calls !== undefined) {\n for (const toolCall of message.tool_calls) {\n if (typeof toolCall === 'object') {\n if (\n toolCall.function.arguments &&\n typeof toolCall.function.arguments !== 'string'\n ) {\n toolCall.function.arguments = JSON.stringify(\n toolCall.function.arguments,\n );\n }\n }\n }\n }\n if (message.function_call !== undefined) {\n if (typeof message.function_call === 'object') {\n if (\n message.function_call.arguments &&\n typeof message.function_call.arguments !== 'string'\n ) {\n message.function_call.arguments = JSON.stringify(\n message.function_call.arguments,\n );\n }\n }\n }\n }\n }\n }\n }\n}\n","import { Accessor, Resource, Setter, createSignal } from 'solid-js';\nimport { useSWRStore } from 'solid-swr-store';\nimport { createSWRStore } from 'swr-store';\nimport { callCompletionApi } from '../shared/call-completion-api';\nimport type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '../shared/types';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Resource<string>;\n /** The error object of the API request */\n error: Accessor<undefined | Error>;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: Accessor<string>;\n /** Signal Setter to update the input value */\n setInput: Setter<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input value={input()} />\n * </form>\n * ```\n */\n handleSubmit: (e: any) => void;\n /** Whether the API request is in progress */\n isLoading: Accessor<boolean>;\n /** Additional data added on the server via StreamData */\n data: Accessor<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\nconst store: Record<string, any> = {};\nconst completionApiStore = createSWRStore<any, string[]>({\n get: async (key: string) => {\n return store[key] ?? [];\n },\n});\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const data = useSWRStore(completionApiStore, () => [key], {\n initialData: initialCompletion,\n });\n\n const mutate = (data: string) => {\n store[key] = data;\n return completionApiStore.mutate([key], {\n data,\n status: 'success',\n });\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Resource<string>;\n\n const [error, setError] = createSignal<undefined | Error>(undefined);\n const [streamData, setStreamData] = createSignal<JSONValue[] | undefined>(\n undefined,\n );\n const [isLoading, setIsLoading] = createSignal(false);\n\n let abortController: AbortController | null = null;\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt: string,\n options?: RequestOptions,\n ) => {\n const existingData = streamData() ?? [];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...body,\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: setIsLoading,\n setError,\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n setStreamData([...existingData, ...(data ?? [])]);\n },\n });\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const [input, setInput] = createSignal(initialInput);\n\n const handleSubmit = (e: any) => {\n e.preventDefault();\n const inputValue = input();\n if (!inputValue) return;\n return complete(inputValue);\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n setInput,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { readDataStream } from './read-data-stream';\nimport { JSONValue } from './types';\nimport { createChunkDecoder } from './utils';\n\nexport async function callCompletionApi({\n api,\n prompt,\n credentials,\n headers,\n body,\n streamMode = 'stream-data',\n setCompletion,\n setLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData,\n}: {\n api: string;\n prompt: string;\n credentials?: RequestCredentials;\n headers?: HeadersInit;\n body: Record<string, any>;\n streamMode?: 'stream-data' | 'text';\n setCompletion: (completion: string) => void;\n setLoading: (loading: boolean) => void;\n setError: (error: Error | undefined) => void;\n setAbortController: (abortController: AbortController | null) => void;\n onResponse?: (response: Response) => void | Promise<void>;\n onFinish?: (prompt: string, completion: string) => void;\n onError?: (error: Error) => void;\n onData?: (data: JSONValue[]) => void;\n}) {\n try {\n setLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n setAbortController(abortController);\n\n // Empty the completion immediately.\n setCompletion('');\n\n const res = await fetch(api, {\n method: 'POST',\n body: JSON.stringify({\n prompt,\n ...body,\n }),\n credentials,\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n signal: abortController.signal,\n }).catch(err => {\n throw err;\n });\n\n if (onResponse) {\n try {\n await onResponse(res);\n } catch (err) {\n throw err;\n }\n }\n\n if (!res.ok) {\n throw new Error(\n (await res.text()) || 'Failed to fetch the chat response.',\n );\n }\n\n if (!res.body) {\n throw new Error('The response body is empty.');\n }\n\n let result = '';\n const reader = res.body.getReader();\n\n switch (streamMode) {\n case 'text': {\n const decoder = createChunkDecoder();\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n // Update the completion state with the new message tokens.\n result += decoder(value);\n setCompletion(result);\n\n // The request has been aborted, stop reading the stream.\n if (abortController === null) {\n reader.cancel();\n break;\n }\n }\n\n break;\n }\n\n case 'stream-data': {\n for await (const { type, value } of readDataStream(reader, {\n isAborted: () => abortController === null,\n })) {\n switch (type) {\n case 'text': {\n result += value;\n setCompletion(result);\n break;\n }\n case 'data': {\n onData?.(value);\n break;\n }\n }\n }\n break;\n }\n\n default: {\n const exhaustiveCheck: never = streamMode;\n throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);\n }\n }\n\n if (onFinish) {\n onFinish(prompt, result);\n }\n\n setAbortController(null);\n return result;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n setAbortController(null);\n return null;\n }\n\n if (err instanceof Error) {\n if (onError) {\n onError(err);\n }\n }\n\n setError(err as Error);\n } finally {\n setLoading(false);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAyD;AACzD,6BAA4B;AAC5B,uBAA+B;;;ACa/B,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,MAAM,QAAQ,MAAM;AAAA,EAC/B;AACF;AAEA,IAAM,yBAIF;AAAA,EACF,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QACE,SAAS,QACT,OAAO,UAAU,YACjB,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A,QAAQ,CAAC,CAAE,CAAC;AAAA,MAClD;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,OAAO,MAAM;AACjB,QAAI,iBAAiB;AACnB,sBAAgB,MAAM;AACtB,wBAAkB;AAAA,IACpB;AAAA,EACF;AAEA,QAAM,gBAAgB,CAACC,gBAAuB;AAC5C,WAAOA,WAAU;AAAA,EACnB;AAEA,QAAM,CAAC,OAAO,QAAQ,QAAI,+BAAa,YAAY;AAEnD,QAAM,eAAe,CAAC,MAAW;AAC/B,MAAE,eAAe;AACjB,UAAM,aAAa,MAAM;AACzB,QAAI,CAAC;AAAY;AACjB,WAAO,SAAS,UAAU;AAAA,EAC5B;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM;AAAA,EACR;AACF;","names":["streamParts","generateId","generateId","fixFunctionCallArguments","generateId","data","messages","import_solid_js","import_solid_swr_store","import_swr_store","uniqueId","store","data","completion"]}
|
1
|
+
{"version":3,"sources":["../index.ts","../use-chat.ts","../../shared/generate-id.ts","../../shared/stream-parts.ts","../../shared/read-data-stream.ts","../../shared/parse-complex-response.ts","../../shared/utils.ts","../../shared/call-chat-api.ts","../../shared/process-chat-stream.ts","../use-completion.ts","../../shared/call-completion-api.ts"],"sourcesContent":["export * from './use-chat';\nexport * from './use-completion';\n","import { Accessor, Resource, Setter, createSignal } from 'solid-js';\nimport { useSWRStore } from 'solid-swr-store';\nimport { createSWRStore } from 'swr-store';\nimport { callChatApi } from '../shared/call-chat-api';\nimport { generateId as generateIdFunc } from '../shared/generate-id';\nimport { processChatStream } from '../shared/process-chat-stream';\nimport type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\n JSONValue,\n Message,\n UseChatOptions,\n} from '../shared/types';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Resource<Message[]>;\n /** The error object of the API request */\n error: Accessor<undefined | Error>;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (messages: Message[]) => void;\n /** The current value of the input */\n input: Accessor<string>;\n /** Signal setter to update the input value */\n setInput: Setter<string>;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;\n /** Whether the API request is in progress */\n isLoading: Accessor<boolean>;\n /** Additional data added on the server via StreamData */\n data: Accessor<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\nconst store: Record<string, Message[] | undefined> = {};\nconst chatApiStore = createSWRStore<Message[], string[]>({\n get: async (key: string) => {\n return store[key] ?? [];\n },\n});\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n streamMode,\n generateId = generateIdFunc,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n\n // Because of the `initialData` option, the `data` will never be `undefined`:\n const messages = useSWRStore(chatApiStore, () => [key], {\n initialData: initialMessages,\n }) as Resource<Message[]>;\n\n const mutate = (data: Message[]) => {\n store[key] = data;\n return chatApiStore.mutate([key], {\n status: 'success',\n data,\n });\n };\n\n const [error, setError] = createSignal<undefined | Error>(undefined);\n const [streamData, setStreamData] = createSignal<JSONValue[] | undefined>(\n undefined,\n );\n const [isLoading, setIsLoading] = createSignal(false);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data }: ChatRequestOptions = {},\n ) {\n try {\n setError(undefined);\n setIsLoading(true);\n\n abortController = new AbortController();\n\n const getCurrentMessages = () =>\n chatApiStore.get([key], {\n shouldRevalidate: false,\n });\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = getCurrentMessages();\n mutate(messagesSnapshot);\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = streamData() ?? [];\n\n return await callChatApi({\n api,\n messages: sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({ role, content, name, function_call }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(function_call !== undefined && {\n function_call,\n }),\n }),\n ),\n body: {\n data: chatRequest.data,\n ...body,\n ...options?.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...options?.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n setStreamData([...existingData, ...(data ?? [])]);\n },\n onFinish,\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (previousMessages.status === 'success') {\n mutate(previousMessages.data);\n }\n },\n generateId,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => getCurrentMessages().data,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n setIsLoading(false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n return triggerRequest(\n (messages() ?? []).concat(message as Message),\n options,\n );\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages();\n if (!messagesSnapshot || messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (messages: Message[]) => {\n mutate(messages);\n };\n\n const [input, setInput] = createSignal(initialInput);\n\n const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {\n e.preventDefault();\n const inputValue = input();\n if (!inputValue) return;\n\n append(\n {\n content: inputValue,\n role: 'user',\n createdAt: new Date(),\n },\n options,\n );\n\n setInput('');\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { customAlphabet } from 'nanoid/non-secure';\n\n/**\n * Generates a 7-character random string to use for IDs. Not secure.\n */\nexport const generateId = customAlphabet(\n '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',\n 7,\n);\n","import { ToolCall as CoreToolCall } from '../core/generate-text/tool-call';\nimport { ToolResult as CoreToolResult } from '../core/generate-text/tool-result';\nimport {\n AssistantMessage,\n DataMessage,\n FunctionCall,\n JSONValue,\n ToolCall,\n} from './types';\nimport { StreamString } from './utils';\n\nexport interface StreamPart<CODE extends string, NAME extends string, TYPE> {\n code: CODE;\n name: NAME;\n parse: (value: JSONValue) => { type: NAME; value: TYPE };\n}\n\nconst textStreamPart: StreamPart<'0', 'text', string> = {\n code: '0',\n name: 'text',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"text\" parts expect a string value.');\n }\n return { type: 'text', value };\n },\n};\n\nconst functionCallStreamPart: StreamPart<\n '1',\n 'function_call',\n { function_call: FunctionCall }\n> = {\n code: '1',\n name: 'function_call',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('function_call' in value) ||\n typeof value.function_call !== 'object' ||\n value.function_call == null ||\n !('name' in value.function_call) ||\n !('arguments' in value.function_call) ||\n typeof value.function_call.name !== 'string' ||\n typeof value.function_call.arguments !== 'string'\n ) {\n throw new Error(\n '\"function_call\" parts expect an object with a \"function_call\" property.',\n );\n }\n\n return {\n type: 'function_call',\n value: value as unknown as { function_call: FunctionCall },\n };\n },\n};\n\nconst dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>> = {\n code: '2',\n name: 'data',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"data\" parts expect an array value.');\n }\n\n return { type: 'data', value };\n },\n};\n\nconst errorStreamPart: StreamPart<'3', 'error', string> = {\n code: '3',\n name: 'error',\n parse: (value: JSONValue) => {\n if (typeof value !== 'string') {\n throw new Error('\"error\" parts expect a string value.');\n }\n return { type: 'error', value };\n },\n};\n\nconst assistantMessageStreamPart: StreamPart<\n '4',\n 'assistant_message',\n AssistantMessage\n> = {\n code: '4',\n name: 'assistant_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('id' in value) ||\n !('role' in value) ||\n !('content' in value) ||\n typeof value.id !== 'string' ||\n typeof value.role !== 'string' ||\n value.role !== 'assistant' ||\n !Array.isArray(value.content) ||\n !value.content.every(\n item =>\n item != null &&\n typeof item === 'object' &&\n 'type' in item &&\n item.type === 'text' &&\n 'text' in item &&\n item.text != null &&\n typeof item.text === 'object' &&\n 'value' in item.text &&\n typeof item.text.value === 'string',\n )\n ) {\n throw new Error(\n '\"assistant_message\" parts expect an object with an \"id\", \"role\", and \"content\" property.',\n );\n }\n\n return {\n type: 'assistant_message',\n value: value as AssistantMessage,\n };\n },\n};\n\nconst assistantControlDataStreamPart: StreamPart<\n '5',\n 'assistant_control_data',\n {\n threadId: string;\n messageId: string;\n }\n> = {\n code: '5',\n name: 'assistant_control_data',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('threadId' in value) ||\n !('messageId' in value) ||\n typeof value.threadId !== 'string' ||\n typeof value.messageId !== 'string'\n ) {\n throw new Error(\n '\"assistant_control_data\" parts expect an object with a \"threadId\" and \"messageId\" property.',\n );\n }\n\n return {\n type: 'assistant_control_data',\n value: {\n threadId: value.threadId,\n messageId: value.messageId,\n },\n };\n },\n};\n\nconst dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage> = {\n code: '6',\n name: 'data_message',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('role' in value) ||\n !('data' in value) ||\n typeof value.role !== 'string' ||\n value.role !== 'data'\n ) {\n throw new Error(\n '\"data_message\" parts expect an object with a \"role\" and \"data\" property.',\n );\n }\n\n return {\n type: 'data_message',\n value: value as DataMessage,\n };\n },\n};\n\nconst toolCallsStreamPart: StreamPart<\n '7',\n 'tool_calls',\n { tool_calls: ToolCall[] }\n> = {\n code: '7',\n name: 'tool_calls',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('tool_calls' in value) ||\n typeof value.tool_calls !== 'object' ||\n value.tool_calls == null ||\n !Array.isArray(value.tool_calls) ||\n value.tool_calls.some(\n tc =>\n tc == null ||\n typeof tc !== 'object' ||\n !('id' in tc) ||\n typeof tc.id !== 'string' ||\n !('type' in tc) ||\n typeof tc.type !== 'string' ||\n !('function' in tc) ||\n tc.function == null ||\n typeof tc.function !== 'object' ||\n !('arguments' in tc.function) ||\n typeof tc.function.name !== 'string' ||\n typeof tc.function.arguments !== 'string',\n )\n ) {\n throw new Error(\n '\"tool_calls\" parts expect an object with a ToolCallPayload.',\n );\n }\n\n return {\n type: 'tool_calls',\n value: value as unknown as { tool_calls: ToolCall[] },\n };\n },\n};\n\nconst messageAnnotationsStreamPart: StreamPart<\n '8',\n 'message_annotations',\n Array<JSONValue>\n> = {\n code: '8',\n name: 'message_annotations',\n parse: (value: JSONValue) => {\n if (!Array.isArray(value)) {\n throw new Error('\"message_annotations\" parts expect an array value.');\n }\n\n return { type: 'message_annotations', value };\n },\n};\n\nconst toolCallStreamPart: StreamPart<\n '9',\n 'tool_call',\n CoreToolCall<string, any>\n> = {\n code: '9',\n name: 'tool_call',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('toolName' in value) ||\n typeof value.toolName !== 'string' ||\n !('args' in value) ||\n typeof value.args !== 'object'\n ) {\n throw new Error(\n '\"tool_call\" parts expect an object with a \"toolCallId\", \"toolName\", and \"args\" property.',\n );\n }\n\n return {\n type: 'tool_call',\n value: value as unknown as CoreToolCall<string, any>,\n };\n },\n};\n\nconst toolResultStreamPart: StreamPart<\n 'a',\n 'tool_result',\n CoreToolResult<string, any, any>\n> = {\n code: 'a',\n name: 'tool_result',\n parse: (value: JSONValue) => {\n if (\n value == null ||\n typeof value !== 'object' ||\n !('toolCallId' in value) ||\n typeof value.toolCallId !== 'string' ||\n !('toolName' in value) ||\n typeof value.toolName !== 'string' ||\n !('args' in value) ||\n typeof value.args !== 'object' ||\n !('result' in value)\n ) {\n throw new Error(\n '\"tool_result\" parts expect an object with a \"toolCallId\", \"toolName\", \"args\", and \"result\" property.',\n );\n }\n\n return {\n type: 'tool_result',\n value: value as unknown as CoreToolResult<string, any, any>,\n };\n },\n};\n\nconst streamParts = [\n textStreamPart,\n functionCallStreamPart,\n dataStreamPart,\n errorStreamPart,\n assistantMessageStreamPart,\n assistantControlDataStreamPart,\n dataMessageStreamPart,\n toolCallsStreamPart,\n messageAnnotationsStreamPart,\n toolCallStreamPart,\n toolResultStreamPart,\n] as const;\n\n// union type of all stream parts\ntype StreamParts =\n | typeof textStreamPart\n | typeof functionCallStreamPart\n | typeof dataStreamPart\n | typeof errorStreamPart\n | typeof assistantMessageStreamPart\n | typeof assistantControlDataStreamPart\n | typeof dataMessageStreamPart\n | typeof toolCallsStreamPart\n | typeof messageAnnotationsStreamPart\n | typeof toolCallStreamPart\n | typeof toolResultStreamPart;\n\n/**\n * Maps the type of a stream part to its value type.\n */\ntype StreamPartValueType = {\n [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];\n};\n\nexport type StreamPartType =\n | ReturnType<typeof textStreamPart.parse>\n | ReturnType<typeof functionCallStreamPart.parse>\n | ReturnType<typeof dataStreamPart.parse>\n | ReturnType<typeof errorStreamPart.parse>\n | ReturnType<typeof assistantMessageStreamPart.parse>\n | ReturnType<typeof assistantControlDataStreamPart.parse>\n | ReturnType<typeof dataMessageStreamPart.parse>\n | ReturnType<typeof toolCallsStreamPart.parse>\n | ReturnType<typeof messageAnnotationsStreamPart.parse>\n | ReturnType<typeof toolCallStreamPart.parse>\n | ReturnType<typeof toolResultStreamPart.parse>;\n\nexport const streamPartsByCode = {\n [textStreamPart.code]: textStreamPart,\n [functionCallStreamPart.code]: functionCallStreamPart,\n [dataStreamPart.code]: dataStreamPart,\n [errorStreamPart.code]: errorStreamPart,\n [assistantMessageStreamPart.code]: assistantMessageStreamPart,\n [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,\n [dataMessageStreamPart.code]: dataMessageStreamPart,\n [toolCallsStreamPart.code]: toolCallsStreamPart,\n [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,\n [toolCallStreamPart.code]: toolCallStreamPart,\n [toolResultStreamPart.code]: toolResultStreamPart,\n} as const;\n\n/**\n * The map of prefixes for data in the stream\n *\n * - 0: Text from the LLM response\n * - 1: (OpenAI) function_call responses\n * - 2: custom JSON added by the user using `Data`\n * - 6: (OpenAI) tool_call responses\n *\n * Example:\n * ```\n * 0:Vercel\n * 0:'s\n * 0: AI\n * 0: AI\n * 0: SDK\n * 0: is great\n * 0:!\n * 2: { \"someJson\": \"value\" }\n * 1: {\"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}\n * 6: {\"tool_call\": {\"id\": \"tool_0\", \"type\": \"function\", \"function\": {\"name\": \"get_current_weather\", \"arguments\": \"{\\\\n\\\\\"location\\\\\": \\\\\"Charlottesville, Virginia\\\\\",\\\\n\\\\\"format\\\\\": \\\\\"celsius\\\\\"\\\\n}\"}}}\n *```\n */\nexport const StreamStringPrefixes = {\n [textStreamPart.name]: textStreamPart.code,\n [functionCallStreamPart.name]: functionCallStreamPart.code,\n [dataStreamPart.name]: dataStreamPart.code,\n [errorStreamPart.name]: errorStreamPart.code,\n [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,\n [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,\n [dataMessageStreamPart.name]: dataMessageStreamPart.code,\n [toolCallsStreamPart.name]: toolCallsStreamPart.code,\n [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,\n [toolCallStreamPart.name]: toolCallStreamPart.code,\n [toolResultStreamPart.name]: toolResultStreamPart.code,\n} as const;\n\nexport const validCodes = streamParts.map(part => part.code);\n\n/**\nParses a stream part from a string.\n\n@param line The string to parse.\n@returns The parsed stream part.\n@throws An error if the string cannot be parsed.\n */\nexport const parseStreamPart = (line: string): StreamPartType => {\n const firstSeparatorIndex = line.indexOf(':');\n\n if (firstSeparatorIndex === -1) {\n throw new Error('Failed to parse stream string. No separator found.');\n }\n\n const prefix = line.slice(0, firstSeparatorIndex);\n\n if (!validCodes.includes(prefix as keyof typeof streamPartsByCode)) {\n throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);\n }\n\n const code = prefix as keyof typeof streamPartsByCode;\n\n const textValue = line.slice(firstSeparatorIndex + 1);\n const jsonValue: JSONValue = JSON.parse(textValue);\n\n return streamPartsByCode[code].parse(jsonValue);\n};\n\n/**\nPrepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\nand appends a new line.\n\nIt ensures type-safety for the part type and value.\n */\nexport function formatStreamPart<T extends keyof StreamPartValueType>(\n type: T,\n value: StreamPartValueType[T],\n): StreamString {\n const streamPart = streamParts.find(part => part.name === type);\n\n if (!streamPart) {\n throw new Error(`Invalid stream part type: ${type}`);\n }\n\n return `${streamPart.code}:${JSON.stringify(value)}\\n`;\n}\n","import { StreamPartType, parseStreamPart } from './stream-parts';\n\nconst NEWLINE = '\\n'.charCodeAt(0);\n\n// concatenates all the chunks into a single Uint8Array\nfunction concatChunks(chunks: Uint8Array[], totalLength: number) {\n const concatenatedChunks = new Uint8Array(totalLength);\n\n let offset = 0;\n for (const chunk of chunks) {\n concatenatedChunks.set(chunk, offset);\n offset += chunk.length;\n }\n chunks.length = 0;\n\n return concatenatedChunks;\n}\n\n/**\nConverts a ReadableStreamDefaultReader into an async generator that yields\nStreamPart objects.\n\n@param reader \n Reader for the stream to read from.\n@param isAborted\n Optional function that returns true if the request has been aborted.\n If the function returns true, the generator will stop reading the stream.\n If the function is not provided, the generator will not stop reading the stream.\n */\nexport async function* readDataStream(\n reader: ReadableStreamDefaultReader<Uint8Array>,\n {\n isAborted,\n }: {\n isAborted?: () => boolean;\n } = {},\n): AsyncGenerator<StreamPartType> {\n // implementation note: this slightly more complex algorithm is required\n // to pass the tests in the edge environment.\n\n const decoder = new TextDecoder();\n const chunks: Uint8Array[] = [];\n let totalLength = 0;\n\n while (true) {\n const { value } = await reader.read();\n\n if (value) {\n chunks.push(value);\n totalLength += value.length;\n if (value[value.length - 1] !== NEWLINE) {\n // if the last character is not a newline, we have not read the whole JSON value\n continue;\n }\n }\n\n if (chunks.length === 0) {\n break; // we have reached the end of the stream\n }\n\n const concatenatedChunks = concatChunks(chunks, totalLength);\n totalLength = 0;\n\n const streamParts = decoder\n .decode(concatenatedChunks, { stream: true })\n .split('\\n')\n .filter(line => line !== '') // splitting leaves an empty string at the end\n .map(parseStreamPart);\n\n for (const streamPart of streamParts) {\n yield streamPart;\n }\n\n // The request has been aborted, stop reading the stream.\n if (isAborted?.()) {\n reader.cancel();\n break;\n }\n }\n}\n","import { generateId as generateIdFunction } from './generate-id';\nimport { readDataStream } from './read-data-stream';\nimport type { FunctionCall, JSONValue, Message, ToolCall } from './types';\n\ntype PrefixMap = {\n text?: Message;\n function_call?: Message & {\n role: 'assistant';\n function_call: FunctionCall;\n };\n tool_calls?: Message & {\n role: 'assistant';\n tool_calls: ToolCall[];\n };\n data: JSONValue[];\n};\n\nfunction assignAnnotationsToMessage<T extends Message | null | undefined>(\n message: T,\n annotations: JSONValue[] | undefined,\n): T {\n if (!message || !annotations || !annotations.length) return message;\n return { ...message, annotations: [...annotations] } as T;\n}\n\nexport async function parseComplexResponse({\n reader,\n abortControllerRef,\n update,\n onFinish,\n generateId = generateIdFunction,\n getCurrentDate = () => new Date(),\n}: {\n reader: ReadableStreamDefaultReader<Uint8Array>;\n abortControllerRef?: {\n current: AbortController | null;\n };\n update: (merged: Message[], data: JSONValue[] | undefined) => void;\n onFinish?: (prefixMap: PrefixMap) => void;\n generateId?: () => string;\n getCurrentDate?: () => Date;\n}) {\n const createdAt = getCurrentDate();\n const prefixMap: PrefixMap = {\n data: [],\n };\n\n // keep list of current message annotations for message\n let message_annotations: JSONValue[] | undefined = undefined;\n\n // we create a map of each prefix, and for each prefixed message we push to the map\n for await (const { type, value } of readDataStream(reader, {\n isAborted: () => abortControllerRef?.current === null,\n })) {\n if (type === 'text') {\n if (prefixMap['text']) {\n prefixMap['text'] = {\n ...prefixMap['text'],\n content: (prefixMap['text'].content || '') + value,\n };\n } else {\n prefixMap['text'] = {\n id: generateId(),\n role: 'assistant',\n content: value,\n createdAt,\n };\n }\n }\n\n // Tool invocations are part of an assistant message\n if (type === 'tool_call') {\n // create message if it doesn't exist\n if (prefixMap.text == null) {\n prefixMap.text = {\n id: generateId(),\n role: 'assistant',\n content: '',\n createdAt,\n };\n }\n\n if (prefixMap.text.toolInvocations == null) {\n prefixMap.text.toolInvocations = [];\n }\n\n prefixMap.text.toolInvocations.push(value);\n } else if (type === 'tool_result') {\n // create message if it doesn't exist\n if (prefixMap.text == null) {\n prefixMap.text = {\n id: generateId(),\n role: 'assistant',\n content: '',\n createdAt,\n };\n }\n\n if (prefixMap.text.toolInvocations == null) {\n prefixMap.text.toolInvocations = [];\n }\n\n // find if there is any tool invocation with the same toolCallId\n // and replace it with the result\n const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(\n invocation => invocation.toolCallId === value.toolCallId,\n );\n\n if (toolInvocationIndex !== -1) {\n prefixMap.text.toolInvocations[toolInvocationIndex] = value;\n } else {\n prefixMap.text.toolInvocations.push(value);\n }\n }\n\n let functionCallMessage: Message | null | undefined = null;\n\n if (type === 'function_call') {\n prefixMap['function_call'] = {\n id: generateId(),\n role: 'assistant',\n content: '',\n function_call: value.function_call,\n name: value.function_call.name,\n createdAt,\n };\n\n functionCallMessage = prefixMap['function_call'];\n }\n\n let toolCallMessage: Message | null | undefined = null;\n\n if (type === 'tool_calls') {\n prefixMap['tool_calls'] = {\n id: generateId(),\n role: 'assistant',\n content: '',\n tool_calls: value.tool_calls,\n createdAt,\n };\n\n toolCallMessage = prefixMap['tool_calls'];\n }\n\n if (type === 'data') {\n prefixMap['data'].push(...value);\n }\n\n let responseMessage = prefixMap['text'];\n\n if (type === 'message_annotations') {\n if (!message_annotations) {\n message_annotations = [...value];\n } else {\n message_annotations.push(...value);\n }\n\n // Update any existing message with the latest annotations\n functionCallMessage = assignAnnotationsToMessage(\n prefixMap['function_call'],\n message_annotations,\n );\n toolCallMessage = assignAnnotationsToMessage(\n prefixMap['tool_calls'],\n message_annotations,\n );\n responseMessage = assignAnnotationsToMessage(\n prefixMap['text'],\n message_annotations,\n );\n }\n\n // keeps the prefixMap up to date with the latest annotations, even if annotations preceded the message\n if (message_annotations?.length) {\n const messagePrefixKeys: (keyof PrefixMap)[] = [\n 'text',\n 'function_call',\n 'tool_calls',\n ];\n messagePrefixKeys.forEach(key => {\n if (prefixMap[key]) {\n (prefixMap[key] as Message).annotations = [...message_annotations!];\n }\n });\n }\n\n // We add function & tool calls and response messages to the messages[], but data is its own thing\n const merged = [functionCallMessage, toolCallMessage, responseMessage]\n .filter(Boolean)\n .map(message => ({\n ...assignAnnotationsToMessage(message, message_annotations),\n })) as Message[];\n\n update(merged, [...prefixMap['data']]); // make a copy of the data array\n }\n\n onFinish?.(prefixMap);\n\n return {\n messages: [\n prefixMap.text,\n prefixMap.function_call,\n prefixMap.tool_calls,\n ].filter(Boolean) as Message[],\n data: prefixMap.data,\n };\n}\n","import {\n StreamPartType,\n StreamStringPrefixes,\n parseStreamPart,\n} from './stream-parts';\n\nexport * from './generate-id';\n\n// TODO remove (breaking change)\nexport { generateId as nanoid } from './generate-id';\n\n// Export stream data utilities for custom stream implementations,\n// both on the client and server side.\nexport type { StreamPart } from './stream-parts';\nexport { formatStreamPart, parseStreamPart } from './stream-parts';\nexport { readDataStream } from './read-data-stream';\n\n// simple decoder signatures:\nfunction createChunkDecoder(): (chunk: Uint8Array | undefined) => string;\nfunction createChunkDecoder(\n complex: false,\n): (chunk: Uint8Array | undefined) => string;\n// complex decoder signature:\nfunction createChunkDecoder(\n complex: true,\n): (chunk: Uint8Array | undefined) => StreamPartType[];\n// combined signature for when the client calls this function with a boolean:\nfunction createChunkDecoder(\n complex?: boolean,\n): (chunk: Uint8Array | undefined) => StreamPartType[] | string;\nfunction createChunkDecoder(complex?: boolean) {\n const decoder = new TextDecoder();\n\n if (!complex) {\n return function (chunk: Uint8Array | undefined): string {\n if (!chunk) return '';\n return decoder.decode(chunk, { stream: true });\n };\n }\n\n return function (chunk: Uint8Array | undefined) {\n const decoded = decoder\n .decode(chunk, { stream: true })\n .split('\\n')\n .filter(line => line !== ''); // splitting leaves an empty string at the end\n\n return decoded.map(parseStreamPart).filter(Boolean);\n };\n}\n\nexport { createChunkDecoder };\n\nexport const isStreamStringEqualToType = (\n type: keyof typeof StreamStringPrefixes,\n value: string,\n): value is StreamString =>\n value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith('\\n');\n\nexport type StreamString =\n `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\\n`;\n","import { parseComplexResponse } from './parse-complex-response';\nimport { IdGenerator, JSONValue, Message } from './types';\nimport { createChunkDecoder } from './utils';\n\nexport async function callChatApi({\n api,\n messages,\n body,\n streamMode = 'stream-data',\n credentials,\n headers,\n abortController,\n restoreMessagesOnFailure,\n onResponse,\n onUpdate,\n onFinish,\n generateId,\n}: {\n api: string;\n messages: Omit<Message, 'id'>[];\n body: Record<string, any>;\n streamMode?: 'stream-data' | 'text';\n credentials?: RequestCredentials;\n headers?: HeadersInit;\n abortController?: () => AbortController | null;\n restoreMessagesOnFailure: () => void;\n onResponse?: (response: Response) => void | Promise<void>;\n onUpdate: (merged: Message[], data: JSONValue[] | undefined) => void;\n onFinish?: (message: Message) => void;\n generateId: IdGenerator;\n}) {\n const response = await fetch(api, {\n method: 'POST',\n body: JSON.stringify({\n messages,\n ...body,\n }),\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n signal: abortController?.()?.signal,\n credentials,\n }).catch(err => {\n restoreMessagesOnFailure();\n throw err;\n });\n\n if (onResponse) {\n try {\n await onResponse(response);\n } catch (err) {\n throw err;\n }\n }\n\n if (!response.ok) {\n restoreMessagesOnFailure();\n throw new Error(\n (await response.text()) || 'Failed to fetch the chat response.',\n );\n }\n\n if (!response.body) {\n throw new Error('The response body is empty.');\n }\n\n const reader = response.body.getReader();\n\n switch (streamMode) {\n case 'text': {\n const decoder = createChunkDecoder();\n\n const resultMessage = {\n id: generateId(),\n createdAt: new Date(),\n role: 'assistant' as const,\n content: '',\n };\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n resultMessage.content += decoder(value);\n resultMessage.id = generateId();\n\n // note: creating a new message object is required for Solid.js streaming\n onUpdate([{ ...resultMessage }], []);\n\n // The request has been aborted, stop reading the stream.\n if (abortController?.() === null) {\n reader.cancel();\n break;\n }\n }\n\n onFinish?.(resultMessage);\n\n return {\n messages: [resultMessage],\n data: [],\n };\n }\n\n case 'stream-data': {\n return await parseComplexResponse({\n reader,\n abortControllerRef:\n abortController != null ? { current: abortController() } : undefined,\n update: onUpdate,\n onFinish(prefixMap) {\n if (onFinish && prefixMap.text != null) {\n onFinish(prefixMap.text);\n }\n },\n generateId,\n });\n }\n\n default: {\n const exhaustiveCheck: never = streamMode;\n throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);\n }\n }\n}\n","import {\n ChatRequest,\n FunctionCall,\n JSONValue,\n Message,\n ToolCall,\n} from './types';\n\nexport async function processChatStream({\n getStreamedResponse,\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest,\n getCurrentMessages,\n}: {\n getStreamedResponse: () => Promise<\n Message | { messages: Message[]; data: JSONValue[] }\n >;\n experimental_onFunctionCall?: (\n chatMessages: Message[],\n functionCall: FunctionCall,\n ) => Promise<void | ChatRequest>;\n experimental_onToolCall?: (\n chatMessages: Message[],\n toolCalls: ToolCall[],\n ) => Promise<void | ChatRequest>;\n updateChatRequest: (chatRequest: ChatRequest) => void;\n getCurrentMessages: () => Message[];\n}) {\n while (true) {\n // TODO-STREAMDATA: This should be { const { messages: streamedResponseMessages, data } =\n // await getStreamedResponse(} once Stream Data is not experimental\n const messagesAndDataOrJustMessage = await getStreamedResponse();\n\n // Using experimental stream data\n if ('messages' in messagesAndDataOrJustMessage) {\n let hasFollowingResponse = false;\n\n for (const message of messagesAndDataOrJustMessage.messages) {\n // See if the message has a complete function call or tool call\n if (\n (message.function_call === undefined ||\n typeof message.function_call === 'string') &&\n (message.tool_calls === undefined ||\n typeof message.tool_calls === 'string')\n ) {\n continue;\n }\n\n hasFollowingResponse = true;\n // Try to handle function call\n if (experimental_onFunctionCall) {\n const functionCall = message.function_call;\n // Make sure functionCall is an object\n // If not, we got tool calls instead of function calls\n if (typeof functionCall !== 'object') {\n console.warn(\n 'experimental_onFunctionCall should not be defined when using tools',\n );\n continue;\n }\n\n // User handles the function call in their own functionCallHandler.\n // The \"arguments\" key of the function call object will still be a string which will have to be parsed in the function handler.\n // If the \"arguments\" JSON is malformed due to model error the user will have to handle that themselves.\n\n const functionCallResponse: ChatRequest | void =\n await experimental_onFunctionCall(\n getCurrentMessages(),\n functionCall,\n );\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (functionCallResponse === undefined) {\n hasFollowingResponse = false;\n break;\n }\n\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n updateChatRequest(functionCallResponse);\n }\n // Try to handle tool call\n if (experimental_onToolCall) {\n const toolCalls = message.tool_calls;\n // Make sure toolCalls is an array of objects\n // If not, we got function calls instead of tool calls\n if (\n !Array.isArray(toolCalls) ||\n toolCalls.some(toolCall => typeof toolCall !== 'object')\n ) {\n console.warn(\n 'experimental_onToolCall should not be defined when using tools',\n );\n continue;\n }\n\n // User handles the function call in their own functionCallHandler.\n // The \"arguments\" key of the function call object will still be a string which will have to be parsed in the function handler.\n // If the \"arguments\" JSON is malformed due to model error the user will have to handle that themselves.\n const toolCallResponse: ChatRequest | void =\n await experimental_onToolCall(getCurrentMessages(), toolCalls);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (toolCallResponse === undefined) {\n hasFollowingResponse = false;\n break;\n }\n\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n updateChatRequest(toolCallResponse);\n }\n }\n if (!hasFollowingResponse) {\n break;\n }\n } else {\n const streamedResponseMessage = messagesAndDataOrJustMessage;\n\n // TODO-STREAMDATA: Remove this once Stream Data is not experimental\n if (\n (streamedResponseMessage.function_call === undefined ||\n typeof streamedResponseMessage.function_call === 'string') &&\n (streamedResponseMessage.tool_calls === undefined ||\n typeof streamedResponseMessage.tool_calls === 'string')\n ) {\n break;\n }\n\n // If we get here and are expecting a function call, the message should have one, if not warn and continue\n if (experimental_onFunctionCall) {\n const functionCall = streamedResponseMessage.function_call;\n if (!(typeof functionCall === 'object')) {\n console.warn(\n 'experimental_onFunctionCall should not be defined when using tools',\n );\n continue;\n }\n const functionCallResponse: ChatRequest | void =\n await experimental_onFunctionCall(getCurrentMessages(), functionCall);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (functionCallResponse === undefined) break;\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n fixFunctionCallArguments(functionCallResponse);\n updateChatRequest(functionCallResponse);\n }\n // If we get here and are expecting a tool call, the message should have one, if not warn and continue\n if (experimental_onToolCall) {\n const toolCalls = streamedResponseMessage.tool_calls;\n if (!(typeof toolCalls === 'object')) {\n console.warn(\n 'experimental_onToolCall should not be defined when using functions',\n );\n continue;\n }\n const toolCallResponse: ChatRequest | void =\n await experimental_onToolCall(getCurrentMessages(), toolCalls);\n\n // If the user does not return anything as a result of the function call, the loop will break.\n if (toolCallResponse === undefined) break;\n // A function call response was returned.\n // The updated chat with function call response will be sent to the API in the next iteration of the loop.\n fixFunctionCallArguments(toolCallResponse);\n updateChatRequest(toolCallResponse);\n }\n\n // Make sure function call arguments are sent back to the API as a string\n function fixFunctionCallArguments(response: ChatRequest) {\n for (const message of response.messages) {\n if (message.tool_calls !== undefined) {\n for (const toolCall of message.tool_calls) {\n if (typeof toolCall === 'object') {\n if (\n toolCall.function.arguments &&\n typeof toolCall.function.arguments !== 'string'\n ) {\n toolCall.function.arguments = JSON.stringify(\n toolCall.function.arguments,\n );\n }\n }\n }\n }\n if (message.function_call !== undefined) {\n if (typeof message.function_call === 'object') {\n if (\n message.function_call.arguments &&\n typeof message.function_call.arguments !== 'string'\n ) {\n message.function_call.arguments = JSON.stringify(\n message.function_call.arguments,\n );\n }\n }\n }\n }\n }\n }\n }\n}\n","import { Accessor, Resource, Setter, createSignal } from 'solid-js';\nimport { useSWRStore } from 'solid-swr-store';\nimport { createSWRStore } from 'swr-store';\nimport { callCompletionApi } from '../shared/call-completion-api';\nimport type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '../shared/types';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Resource<string>;\n /** The error object of the API request */\n error: Accessor<undefined | Error>;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: Accessor<string>;\n /** Signal Setter to update the input value */\n setInput: Setter<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input value={input()} />\n * </form>\n * ```\n */\n handleSubmit: (e: any) => void;\n /** Whether the API request is in progress */\n isLoading: Accessor<boolean>;\n /** Additional data added on the server via StreamData */\n data: Accessor<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\nconst store: Record<string, any> = {};\nconst completionApiStore = createSWRStore<any, string[]>({\n get: async (key: string) => {\n return store[key] ?? [];\n },\n});\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const data = useSWRStore(completionApiStore, () => [key], {\n initialData: initialCompletion,\n });\n\n const mutate = (data: string) => {\n store[key] = data;\n return completionApiStore.mutate([key], {\n data,\n status: 'success',\n });\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Resource<string>;\n\n const [error, setError] = createSignal<undefined | Error>(undefined);\n const [streamData, setStreamData] = createSignal<JSONValue[] | undefined>(\n undefined,\n );\n const [isLoading, setIsLoading] = createSignal(false);\n\n let abortController: AbortController | null = null;\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt: string,\n options?: RequestOptions,\n ) => {\n const existingData = streamData() ?? [];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...body,\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: setIsLoading,\n setError,\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n setStreamData([...existingData, ...(data ?? [])]);\n },\n });\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const [input, setInput] = createSignal(initialInput);\n\n const handleSubmit = (e: any) => {\n e.preventDefault();\n const inputValue = input();\n if (!inputValue) return;\n return complete(inputValue);\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n setInput,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { readDataStream } from './read-data-stream';\nimport { JSONValue } from './types';\nimport { createChunkDecoder } from './utils';\n\nexport async function callCompletionApi({\n api,\n prompt,\n credentials,\n headers,\n body,\n streamMode = 'stream-data',\n setCompletion,\n setLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData,\n}: {\n api: string;\n prompt: string;\n credentials?: RequestCredentials;\n headers?: HeadersInit;\n body: Record<string, any>;\n streamMode?: 'stream-data' | 'text';\n setCompletion: (completion: string) => void;\n setLoading: (loading: boolean) => void;\n setError: (error: Error | undefined) => void;\n setAbortController: (abortController: AbortController | null) => void;\n onResponse?: (response: Response) => void | Promise<void>;\n onFinish?: (prompt: string, completion: string) => void;\n onError?: (error: Error) => void;\n onData?: (data: JSONValue[]) => void;\n}) {\n try {\n setLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n setAbortController(abortController);\n\n // Empty the completion immediately.\n setCompletion('');\n\n const res = await fetch(api, {\n method: 'POST',\n body: JSON.stringify({\n prompt,\n ...body,\n }),\n credentials,\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n signal: abortController.signal,\n }).catch(err => {\n throw err;\n });\n\n if (onResponse) {\n try {\n await onResponse(res);\n } catch (err) {\n throw err;\n }\n }\n\n if (!res.ok) {\n throw new Error(\n (await res.text()) || 'Failed to fetch the chat response.',\n );\n }\n\n if (!res.body) {\n throw new Error('The response body is empty.');\n }\n\n let result = '';\n const reader = res.body.getReader();\n\n switch (streamMode) {\n case 'text': {\n const decoder = createChunkDecoder();\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n\n // Update the completion state with the new message tokens.\n result += decoder(value);\n setCompletion(result);\n\n // The request has been aborted, stop reading the stream.\n if (abortController === null) {\n reader.cancel();\n break;\n }\n }\n\n break;\n }\n\n case 'stream-data': {\n for await (const { type, value } of readDataStream(reader, {\n isAborted: () => abortController === null,\n })) {\n switch (type) {\n case 'text': {\n result += value;\n setCompletion(result);\n break;\n }\n case 'data': {\n onData?.(value);\n break;\n }\n }\n }\n break;\n }\n\n default: {\n const exhaustiveCheck: never = streamMode;\n throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);\n }\n }\n\n if (onFinish) {\n onFinish(prompt, result);\n }\n\n setAbortController(null);\n return result;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n setAbortController(null);\n return null;\n }\n\n if (err instanceof Error) {\n if (onError) {\n onError(err);\n }\n }\n\n setError(err as Error);\n } finally {\n setLoading(false);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAyD;AACzD,6BAA4B;AAC5B,uBAA+B;;;ACF/B,wBAA+B;AAKxB,IAAM,iBAAa;AAAA,EACxB;AAAA,EACA;AACF;;;ACSA,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,IAAI,MAAM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fixFunctionCallArguments","generateId","data","messages","import_solid_js","import_solid_swr_store","import_swr_store","uniqueId","store","data","completion"]}
|