ai 0.0.0-85f9a635-20240518005312

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.
Files changed (56) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +37 -0
  3. package/dist/index.d.mts +2287 -0
  4. package/dist/index.d.ts +2287 -0
  5. package/dist/index.js +3531 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +3454 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +178 -0
  10. package/prompts/dist/index.d.mts +324 -0
  11. package/prompts/dist/index.d.ts +324 -0
  12. package/prompts/dist/index.js +178 -0
  13. package/prompts/dist/index.js.map +1 -0
  14. package/prompts/dist/index.mjs +146 -0
  15. package/prompts/dist/index.mjs.map +1 -0
  16. package/react/dist/index.d.mts +565 -0
  17. package/react/dist/index.d.ts +582 -0
  18. package/react/dist/index.js +1430 -0
  19. package/react/dist/index.js.map +1 -0
  20. package/react/dist/index.mjs +1391 -0
  21. package/react/dist/index.mjs.map +1 -0
  22. package/react/dist/index.server.d.mts +17 -0
  23. package/react/dist/index.server.d.ts +17 -0
  24. package/react/dist/index.server.js +50 -0
  25. package/react/dist/index.server.js.map +1 -0
  26. package/react/dist/index.server.mjs +23 -0
  27. package/react/dist/index.server.mjs.map +1 -0
  28. package/rsc/dist/index.d.ts +580 -0
  29. package/rsc/dist/index.mjs +18 -0
  30. package/rsc/dist/rsc-client.d.mts +1 -0
  31. package/rsc/dist/rsc-client.mjs +18 -0
  32. package/rsc/dist/rsc-client.mjs.map +1 -0
  33. package/rsc/dist/rsc-server.d.mts +516 -0
  34. package/rsc/dist/rsc-server.mjs +1900 -0
  35. package/rsc/dist/rsc-server.mjs.map +1 -0
  36. package/rsc/dist/rsc-shared.d.mts +94 -0
  37. package/rsc/dist/rsc-shared.mjs +346 -0
  38. package/rsc/dist/rsc-shared.mjs.map +1 -0
  39. package/solid/dist/index.d.mts +408 -0
  40. package/solid/dist/index.d.ts +408 -0
  41. package/solid/dist/index.js +1072 -0
  42. package/solid/dist/index.js.map +1 -0
  43. package/solid/dist/index.mjs +1044 -0
  44. package/solid/dist/index.mjs.map +1 -0
  45. package/svelte/dist/index.d.mts +484 -0
  46. package/svelte/dist/index.d.ts +484 -0
  47. package/svelte/dist/index.js +1778 -0
  48. package/svelte/dist/index.js.map +1 -0
  49. package/svelte/dist/index.mjs +1749 -0
  50. package/svelte/dist/index.mjs.map +1 -0
  51. package/vue/dist/index.d.mts +402 -0
  52. package/vue/dist/index.d.ts +402 -0
  53. package/vue/dist/index.js +1072 -0
  54. package/vue/dist/index.js.map +1 -0
  55. package/vue/dist/index.mjs +1034 -0
  56. package/vue/dist/index.mjs.map +1 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../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":["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,SAAqC,oBAAoB;AACzD,SAAS,mBAAmB;AAC5B,SAAS,sBAAsB;;;ACF/B,SAAS,sBAAsB;AAKxB,IAAM,aAAa;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,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,EAAE,mBAAmB,UACrB,OAAO,MAAM,kBAAkB,YAC/B,MAAM,iBAAiB,QACvB,EAAE,UAA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B;AAAA,MACA;AAAA,MACA;AAAA,MACA,SAAS;AAAA,QACP,GAAG;AAAA,QACH,GAAG,mCAAS;AAAA,MACd;AAAA,MACA,MAAM;AAAA,QACJ,GAAG;AAAA,QACH,GAAG,mCAAS;AAAA,MACd;AAAA,MACA;AAAA,MACA,eAAe;AAAA,MACf,YAAY;AAAA,MACZ;AAAA,MACA,oBAAoB,gBAAc;AAChC,0BAAkB;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,QAAQ,CAAAD,UAAQ;AACd,sBAAc,CAAC,GAAG,cAAc,GAAIA,SAAA,OAAAA,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,CAACE,gBAAuB;AAC5C,WAAOA,WAAU;AAAA,EACnB;AAEA,QAAM,CAAC,OAAO,QAAQ,IAAID,cAAa,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","createSignal","useSWRStore","createSWRStore","uniqueId","store","createSWRStore","useSWRStore","data","createSignal","completion"]}
@@ -0,0 +1,484 @@
1
+ import { Readable, Writable } from 'svelte/store';
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
+
45
+ type AssistantStatus = 'in_progress' | 'awaiting_message';
46
+ type UseAssistantOptions = {
47
+ /**
48
+ * The API endpoint that accepts a `{ threadId: string | null; message: string; }` object and returns an `AssistantResponse` stream.
49
+ * The threadId refers to an existing thread with messages (or is `null` to create a new thread).
50
+ * The message is the next message that should be appended to the thread and sent to the assistant.
51
+ */
52
+ api: string;
53
+ /**
54
+ * An optional string that represents the ID of an existing thread.
55
+ * If not provided, a new thread will be created.
56
+ */
57
+ threadId?: string;
58
+ /**
59
+ * An optional literal that sets the mode of credentials to be used on the request.
60
+ * Defaults to "same-origin".
61
+ */
62
+ credentials?: RequestCredentials;
63
+ /**
64
+ * An optional object of headers to be passed to the API endpoint.
65
+ */
66
+ headers?: Record<string, string> | Headers;
67
+ /**
68
+ * An optional, additional body object to be passed to the API endpoint.
69
+ */
70
+ body?: object;
71
+ /**
72
+ * An optional callback that will be called when the assistant encounters an error.
73
+ */
74
+ onError?: (error: Error) => void;
75
+ };
76
+
77
+ interface FunctionCall {
78
+ /**
79
+ * The arguments to call the function with, as generated by the model in JSON
80
+ * format. Note that the model does not always generate valid JSON, and may
81
+ * hallucinate parameters not defined by your function schema. Validate the
82
+ * arguments in your code before calling your function.
83
+ */
84
+ arguments?: string;
85
+ /**
86
+ * The name of the function to call.
87
+ */
88
+ name?: string;
89
+ }
90
+ /**
91
+ * The tool calls generated by the model, such as function calls.
92
+ */
93
+ interface ToolCall {
94
+ id: string;
95
+ type: string;
96
+ function: {
97
+ name: string;
98
+ arguments: string;
99
+ };
100
+ }
101
+ /**
102
+ * Controls which (if any) function is called by the model.
103
+ * - none means the model will not call a function and instead generates a message.
104
+ * - auto means the model can pick between generating a message or calling a function.
105
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
106
+ * none is the default when no functions are present. auto is the default if functions are present.
107
+ */
108
+ type ToolChoice = 'none' | 'auto' | {
109
+ type: 'function';
110
+ function: {
111
+ name: string;
112
+ };
113
+ };
114
+ /**
115
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
116
+ * Use this to provide a list of functions the model may generate JSON inputs for.
117
+ */
118
+ interface Tool {
119
+ type: 'function';
120
+ function: Function;
121
+ }
122
+ interface Function {
123
+ /**
124
+ * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
125
+ * underscores and dashes, with a maximum length of 64.
126
+ */
127
+ name: string;
128
+ /**
129
+ * The parameters the functions accepts, described as a JSON Schema object. See the
130
+ * [guide](/docs/guides/gpt/function-calling) for examples, and the
131
+ * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for
132
+ * documentation about the format.
133
+ *
134
+ * To describe a function that accepts no parameters, provide the value
135
+ * `{"type": "object", "properties": {}}`.
136
+ */
137
+ parameters: Record<string, unknown>;
138
+ /**
139
+ * A description of what the function does, used by the model to choose when and
140
+ * how to call the function.
141
+ */
142
+ description?: string;
143
+ }
144
+ type IdGenerator = () => string;
145
+ /**
146
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
147
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
148
+ Once the call is complete, the invocation is a tool result.
149
+ */
150
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
151
+ /**
152
+ * Shared types between the API and UI packages.
153
+ */
154
+ interface Message {
155
+ id: string;
156
+ tool_call_id?: string;
157
+ createdAt?: Date;
158
+ content: string;
159
+ /**
160
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
161
+ */
162
+ ui?: string | JSX.Element | JSX.Element[] | null | undefined;
163
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
164
+ /**
165
+ *
166
+ * If the message has a role of `function`, the `name` field is the name of the function.
167
+ * Otherwise, the name field should not be set.
168
+ */
169
+ name?: string;
170
+ /**
171
+ * If the assistant role makes a function call, the `function_call` field
172
+ * contains the function call name and arguments. Otherwise, the field should
173
+ * not be set. (Deprecated and replaced by tool_calls.)
174
+ */
175
+ function_call?: string | FunctionCall;
176
+ data?: JSONValue;
177
+ /**
178
+ * If the assistant role makes a tool call, the `tool_calls` field contains
179
+ * the tool call name and arguments. Otherwise, the field should not be set.
180
+ */
181
+ tool_calls?: string | ToolCall[];
182
+ /**
183
+ * Additional message-specific information added on the server via StreamData
184
+ */
185
+ annotations?: JSONValue[] | undefined;
186
+ /**
187
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
188
+ that the assistant made as part of this message.
189
+ */
190
+ toolInvocations?: Array<ToolInvocation>;
191
+ }
192
+ type CreateMessage = Omit<Message, 'id'> & {
193
+ id?: Message['id'];
194
+ };
195
+ type ChatRequest = {
196
+ messages: Message[];
197
+ options?: RequestOptions;
198
+ functions?: Array<Function>;
199
+ function_call?: FunctionCall;
200
+ data?: Record<string, string>;
201
+ tools?: Array<Tool>;
202
+ tool_choice?: ToolChoice;
203
+ };
204
+ type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
205
+ type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
206
+ type RequestOptions = {
207
+ headers?: Record<string, string> | Headers;
208
+ body?: object;
209
+ };
210
+ type ChatRequestOptions = {
211
+ options?: RequestOptions;
212
+ functions?: Array<Function>;
213
+ function_call?: FunctionCall;
214
+ tools?: Array<Tool>;
215
+ tool_choice?: ToolChoice;
216
+ data?: Record<string, string>;
217
+ };
218
+ type UseChatOptions = {
219
+ /**
220
+ * The API endpoint that accepts a `{ messages: Message[] }` object and returns
221
+ * a stream of tokens of the AI chat response. Defaults to `/api/chat`.
222
+ */
223
+ api?: string;
224
+ /**
225
+ * A unique identifier for the chat. If not provided, a random one will be
226
+ * generated. When provided, the `useChat` hook with the same `id` will
227
+ * have shared states across components.
228
+ */
229
+ id?: string;
230
+ /**
231
+ * Initial messages of the chat. Useful to load an existing chat history.
232
+ */
233
+ initialMessages?: Message[];
234
+ /**
235
+ * Initial input of the chat.
236
+ */
237
+ initialInput?: string;
238
+ /**
239
+ * Callback function to be called when a function call is received.
240
+ * If the function returns a `ChatRequest` object, the request will be sent
241
+ * automatically to the API and will be used to update the chat.
242
+ */
243
+ experimental_onFunctionCall?: FunctionCallHandler;
244
+ /**
245
+ * Callback function to be called when a tool call is received.
246
+ * If the function returns a `ChatRequest` object, the request will be sent
247
+ * automatically to the API and will be used to update the chat.
248
+ */
249
+ experimental_onToolCall?: ToolCallHandler;
250
+ /**
251
+ * Callback function to be called when the API response is received.
252
+ */
253
+ onResponse?: (response: Response) => void | Promise<void>;
254
+ /**
255
+ * Callback function to be called when the chat is finished streaming.
256
+ */
257
+ onFinish?: (message: Message) => void;
258
+ /**
259
+ * Callback function to be called when an error is encountered.
260
+ */
261
+ onError?: (error: Error) => void;
262
+ /**
263
+ * A way to provide a function that is going to be used for ids for messages.
264
+ * If not provided nanoid is used by default.
265
+ */
266
+ generateId?: IdGenerator;
267
+ /**
268
+ * The credentials mode to be used for the fetch request.
269
+ * Possible values are: 'omit', 'same-origin', 'include'.
270
+ * Defaults to 'same-origin'.
271
+ */
272
+ credentials?: RequestCredentials;
273
+ /**
274
+ * HTTP headers to be sent with the API request.
275
+ */
276
+ headers?: Record<string, string> | Headers;
277
+ /**
278
+ * Extra body object to be sent with the API request.
279
+ * @example
280
+ * Send a `sessionId` to the API along with the messages.
281
+ * ```js
282
+ * useChat({
283
+ * body: {
284
+ * sessionId: '123',
285
+ * }
286
+ * })
287
+ * ```
288
+ */
289
+ body?: object;
290
+ /**
291
+ * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API.
292
+ * Defaults to `false`. When set to `true`, the API endpoint might need to
293
+ * handle the extra fields before forwarding the request to the AI service.
294
+ */
295
+ sendExtraMessageFields?: boolean;
296
+ /** Stream mode (default to "stream-data") */
297
+ streamMode?: 'stream-data' | 'text';
298
+ };
299
+ type UseCompletionOptions = {
300
+ /**
301
+ * The API endpoint that accepts a `{ prompt: string }` object and returns
302
+ * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
303
+ */
304
+ api?: string;
305
+ /**
306
+ * An unique identifier for the chat. If not provided, a random one will be
307
+ * generated. When provided, the `useChat` hook with the same `id` will
308
+ * have shared states across components.
309
+ */
310
+ id?: string;
311
+ /**
312
+ * Initial prompt input of the completion.
313
+ */
314
+ initialInput?: string;
315
+ /**
316
+ * Initial completion result. Useful to load an existing history.
317
+ */
318
+ initialCompletion?: string;
319
+ /**
320
+ * Callback function to be called when the API response is received.
321
+ */
322
+ onResponse?: (response: Response) => void | Promise<void>;
323
+ /**
324
+ * Callback function to be called when the completion is finished streaming.
325
+ */
326
+ onFinish?: (prompt: string, completion: string) => void;
327
+ /**
328
+ * Callback function to be called when an error is encountered.
329
+ */
330
+ onError?: (error: Error) => void;
331
+ /**
332
+ * The credentials mode to be used for the fetch request.
333
+ * Possible values are: 'omit', 'same-origin', 'include'.
334
+ * Defaults to 'same-origin'.
335
+ */
336
+ credentials?: RequestCredentials;
337
+ /**
338
+ * HTTP headers to be sent with the API request.
339
+ */
340
+ headers?: Record<string, string> | Headers;
341
+ /**
342
+ * Extra body object to be sent with the API request.
343
+ * @example
344
+ * Send a `sessionId` to the API along with the prompt.
345
+ * ```js
346
+ * useChat({
347
+ * body: {
348
+ * sessionId: '123',
349
+ * }
350
+ * })
351
+ * ```
352
+ */
353
+ body?: object;
354
+ /** Stream mode (default to "stream-data") */
355
+ streamMode?: 'stream-data' | 'text';
356
+ };
357
+ type JSONValue = null | string | number | boolean | {
358
+ [x: string]: JSONValue;
359
+ } | Array<JSONValue>;
360
+
361
+ type UseChatHelpers = {
362
+ /** Current messages in the chat */
363
+ messages: Readable<Message[]>;
364
+ /** The error object of the API request */
365
+ error: Readable<undefined | Error>;
366
+ /**
367
+ * Append a user message to the chat list. This triggers the API call to fetch
368
+ * the assistant's response.
369
+ * @param message The message to append
370
+ * @param chatRequestOptions Additional options to pass to the API call
371
+ */
372
+ append: (message: Message | CreateMessage, chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
373
+ /**
374
+ * Reload the last AI chat response for the given chat history. If the last
375
+ * message isn't from the assistant, it will request the API to generate a
376
+ * new response.
377
+ */
378
+ reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;
379
+ /**
380
+ * Abort the current request immediately, keep the generated tokens if any.
381
+ */
382
+ stop: () => void;
383
+ /**
384
+ * Update the `messages` state locally. This is useful when you want to
385
+ * edit the messages on the client, and then trigger the `reload` method
386
+ * manually to regenerate the AI response.
387
+ */
388
+ setMessages: (messages: Message[]) => void;
389
+ /** The current value of the input */
390
+ input: Writable<string>;
391
+ /** Form submission handler to automatically reset input and append a user message */
392
+ handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
393
+ metadata?: Object;
394
+ /** Whether the API request is in progress */
395
+ isLoading: Readable<boolean | undefined>;
396
+ /** Additional data added on the server via StreamData */
397
+ data: Readable<JSONValue[] | undefined>;
398
+ };
399
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: UseChatOptions): UseChatHelpers;
400
+
401
+ type UseCompletionHelpers = {
402
+ /** The current completion result */
403
+ completion: Readable<string>;
404
+ /** The error object of the API request */
405
+ error: Readable<undefined | Error>;
406
+ /**
407
+ * Send a new prompt to the API endpoint and update the completion state.
408
+ */
409
+ complete: (prompt: string, options?: RequestOptions) => Promise<string | null | undefined>;
410
+ /**
411
+ * Abort the current API request but keep the generated tokens.
412
+ */
413
+ stop: () => void;
414
+ /**
415
+ * Update the `completion` state locally.
416
+ */
417
+ setCompletion: (completion: string) => void;
418
+ /** The current value of the input */
419
+ input: Writable<string>;
420
+ /**
421
+ * Form submission handler to automatically reset input and append a user message
422
+ * @example
423
+ * ```jsx
424
+ * <form onSubmit={handleSubmit}>
425
+ * <input onChange={handleInputChange} value={input} />
426
+ * </form>
427
+ * ```
428
+ */
429
+ handleSubmit: (e: any) => void;
430
+ /** Whether the API request is in progress */
431
+ isLoading: Readable<boolean | undefined>;
432
+ /** Additional data added on the server via StreamData */
433
+ data: Readable<JSONValue[] | undefined>;
434
+ };
435
+ declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
436
+
437
+ type UseAssistantHelpers = {
438
+ /**
439
+ * The current array of chat messages.
440
+ */
441
+ messages: Readable<Message[]>;
442
+ /**
443
+ * Update the message store with a new array of messages.
444
+ */
445
+ setMessages: (messages: Message[]) => void;
446
+ /**
447
+ * The current thread ID.
448
+ */
449
+ threadId: Readable<string | undefined>;
450
+ /**
451
+ * The current value of the input field.
452
+ */
453
+ input: Writable<string>;
454
+ /**
455
+ * Append a user message to the chat list. This triggers the API call to fetch
456
+ * the assistant's response.
457
+ * @param message The message to append
458
+ * @param requestOptions Additional options to pass to the API call
459
+ */
460
+ append: (message: Message | CreateMessage, requestOptions?: {
461
+ data?: Record<string, string>;
462
+ }) => Promise<void>;
463
+ /**
464
+ Abort the current request immediately, keep the generated tokens if any.
465
+ */
466
+ stop: () => void;
467
+ /**
468
+ * Form submission handler that automatically resets the input field and appends a user message.
469
+ */
470
+ submitMessage: (e: any, requestOptions?: {
471
+ data?: Record<string, string>;
472
+ }) => Promise<void>;
473
+ /**
474
+ * The current status of the assistant. This can be used to show a loading indicator.
475
+ */
476
+ status: Readable<AssistantStatus>;
477
+ /**
478
+ * The error thrown during the assistant message processing, if any.
479
+ */
480
+ error: Readable<undefined | Error>;
481
+ };
482
+ declare function useAssistant({ api, threadId: threadIdParam, credentials, headers, body, onError, }: UseAssistantOptions): UseAssistantHelpers;
483
+
484
+ export { CreateMessage, Message, UseAssistantHelpers, UseChatHelpers, UseChatOptions, UseCompletionHelpers, UseCompletionOptions, useAssistant, useChat, useCompletion };