ai 3.0.21 → 3.0.23

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 (57) hide show
  1. package/dist/index.d.mts +42 -1
  2. package/dist/index.d.ts +42 -1
  3. package/dist/index.js +104 -177
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +65 -138
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +4 -33
  8. package/react/dist/index.d.mts +6 -2
  9. package/react/dist/index.d.ts +6 -2
  10. package/react/dist/index.js +107 -24
  11. package/react/dist/index.js.map +1 -1
  12. package/react/dist/index.mjs +107 -24
  13. package/react/dist/index.mjs.map +1 -1
  14. package/rsc/dist/rsc-server.mjs +3 -3
  15. package/rsc/dist/rsc-server.mjs.map +1 -1
  16. package/solid/dist/index.d.mts +6 -2
  17. package/solid/dist/index.d.ts +6 -2
  18. package/solid/dist/index.js +105 -23
  19. package/solid/dist/index.js.map +1 -1
  20. package/solid/dist/index.mjs +105 -23
  21. package/solid/dist/index.mjs.map +1 -1
  22. package/svelte/dist/index.d.mts +6 -2
  23. package/svelte/dist/index.d.ts +6 -2
  24. package/svelte/dist/index.js +107 -24
  25. package/svelte/dist/index.js.map +1 -1
  26. package/svelte/dist/index.mjs +107 -24
  27. package/svelte/dist/index.mjs.map +1 -1
  28. package/vue/dist/index.d.mts +6 -2
  29. package/vue/dist/index.d.ts +6 -2
  30. package/vue/dist/index.js +105 -23
  31. package/vue/dist/index.js.map +1 -1
  32. package/vue/dist/index.mjs +105 -23
  33. package/vue/dist/index.mjs.map +1 -1
  34. package/anthropic/dist/index.d.mts +0 -51
  35. package/anthropic/dist/index.d.ts +0 -51
  36. package/anthropic/dist/index.js +0 -792
  37. package/anthropic/dist/index.js.map +0 -1
  38. package/anthropic/dist/index.mjs +0 -760
  39. package/anthropic/dist/index.mjs.map +0 -1
  40. package/google/dist/index.d.mts +0 -47
  41. package/google/dist/index.d.ts +0 -47
  42. package/google/dist/index.js +0 -796
  43. package/google/dist/index.js.map +0 -1
  44. package/google/dist/index.mjs +0 -764
  45. package/google/dist/index.mjs.map +0 -1
  46. package/mistral/dist/index.d.mts +0 -52
  47. package/mistral/dist/index.d.ts +0 -52
  48. package/mistral/dist/index.js +0 -763
  49. package/mistral/dist/index.js.map +0 -1
  50. package/mistral/dist/index.mjs +0 -731
  51. package/mistral/dist/index.mjs.map +0 -1
  52. package/openai/dist/index.d.mts +0 -116
  53. package/openai/dist/index.d.ts +0 -116
  54. package/openai/dist/index.js +0 -1143
  55. package/openai/dist/index.js.map +0 -1
  56. package/openai/dist/index.mjs +0 -1115
  57. package/openai/dist/index.mjs.map +0 -1
@@ -102,9 +102,9 @@ var toolCallStreamPart = {
102
102
  code: "7",
103
103
  name: "tool_calls",
104
104
  parse: (value) => {
105
- if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some((tc) => {
106
- tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string";
107
- })) {
105
+ if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
106
+ (tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
107
+ )) {
108
108
  throw new Error(
109
109
  '"tool_calls" parts expect an object with a ToolCallPayload.'
110
110
  );
@@ -335,11 +335,28 @@ async function parseComplexResponse({
335
335
  };
336
336
  }
337
337
 
338
+ // shared/utils.ts
339
+ function createChunkDecoder(complex) {
340
+ const decoder = new TextDecoder();
341
+ if (!complex) {
342
+ return function(chunk) {
343
+ if (!chunk)
344
+ return "";
345
+ return decoder.decode(chunk, { stream: true });
346
+ };
347
+ }
348
+ return function(chunk) {
349
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
350
+ return decoded.map(parseStreamPart).filter(Boolean);
351
+ };
352
+ }
353
+
338
354
  // shared/call-chat-api.ts
339
355
  async function callChatApi({
340
356
  api,
341
357
  messages,
342
358
  body,
359
+ streamMode = "stream-data",
343
360
  credentials,
344
361
  headers,
345
362
  abortController,
@@ -383,17 +400,52 @@ async function callChatApi({
383
400
  throw new Error("The response body is empty.");
384
401
  }
385
402
  const reader = response.body.getReader();
386
- return await parseComplexResponse({
387
- reader,
388
- abortControllerRef: abortController != null ? { current: abortController() } : void 0,
389
- update: onUpdate,
390
- onFinish(prefixMap) {
391
- if (onFinish && prefixMap.text != null) {
392
- onFinish(prefixMap.text);
403
+ switch (streamMode) {
404
+ case "text": {
405
+ const decoder = createChunkDecoder();
406
+ const resultMessage = {
407
+ id: generateId2(),
408
+ createdAt: /* @__PURE__ */ new Date(),
409
+ role: "assistant",
410
+ content: ""
411
+ };
412
+ while (true) {
413
+ const { done, value } = await reader.read();
414
+ if (done) {
415
+ break;
416
+ }
417
+ resultMessage.content += decoder(value);
418
+ resultMessage.id = generateId2();
419
+ onUpdate([{ ...resultMessage }], []);
420
+ if ((abortController == null ? void 0 : abortController()) === null) {
421
+ reader.cancel();
422
+ break;
423
+ }
393
424
  }
394
- },
395
- generateId: generateId2
396
- });
425
+ onFinish == null ? void 0 : onFinish(resultMessage);
426
+ return {
427
+ messages: [resultMessage],
428
+ data: []
429
+ };
430
+ }
431
+ case "stream-data": {
432
+ return await parseComplexResponse({
433
+ reader,
434
+ abortControllerRef: abortController != null ? { current: abortController() } : void 0,
435
+ update: onUpdate,
436
+ onFinish(prefixMap) {
437
+ if (onFinish && prefixMap.text != null) {
438
+ onFinish(prefixMap.text);
439
+ }
440
+ },
441
+ generateId: generateId2
442
+ });
443
+ }
444
+ default: {
445
+ const exhaustiveCheck = streamMode;
446
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
447
+ }
448
+ }
397
449
  }
398
450
 
399
451
  // shared/process-chat-stream.ts
@@ -523,6 +575,7 @@ function useChat({
523
575
  initialInput = "",
524
576
  sendExtraMessageFields,
525
577
  experimental_onFunctionCall,
578
+ streamMode,
526
579
  onResponse,
527
580
  onFinish,
528
581
  onError,
@@ -586,6 +639,7 @@ function useChat({
586
639
  // Use unref to unwrap the ref value
587
640
  ...options == null ? void 0 : options.body
588
641
  },
642
+ streamMode,
589
643
  headers: {
590
644
  ...headers,
591
645
  ...options == null ? void 0 : options.headers
@@ -692,6 +746,7 @@ async function callCompletionApi({
692
746
  credentials,
693
747
  headers,
694
748
  body,
749
+ streamMode = "stream-data",
695
750
  setCompletion,
696
751
  setLoading,
697
752
  setError,
@@ -739,19 +794,44 @@ async function callCompletionApi({
739
794
  }
740
795
  let result = "";
741
796
  const reader = res.body.getReader();
742
- for await (const { type, value } of readDataStream(reader, {
743
- isAborted: () => abortController === null
744
- })) {
745
- switch (type) {
746
- case "text": {
747
- result += value;
797
+ switch (streamMode) {
798
+ case "text": {
799
+ const decoder = createChunkDecoder();
800
+ while (true) {
801
+ const { done, value } = await reader.read();
802
+ if (done) {
803
+ break;
804
+ }
805
+ result += decoder(value);
748
806
  setCompletion(result);
749
- break;
807
+ if (abortController === null) {
808
+ reader.cancel();
809
+ break;
810
+ }
750
811
  }
751
- case "data": {
752
- onData == null ? void 0 : onData(value);
753
- break;
812
+ break;
813
+ }
814
+ case "stream-data": {
815
+ for await (const { type, value } of readDataStream(reader, {
816
+ isAborted: () => abortController === null
817
+ })) {
818
+ switch (type) {
819
+ case "text": {
820
+ result += value;
821
+ setCompletion(result);
822
+ break;
823
+ }
824
+ case "data": {
825
+ onData == null ? void 0 : onData(value);
826
+ break;
827
+ }
828
+ }
754
829
  }
830
+ break;
831
+ }
832
+ default: {
833
+ const exhaustiveCheck = streamMode;
834
+ throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
755
835
  }
756
836
  }
757
837
  if (onFinish) {
@@ -787,6 +867,7 @@ function useCompletion({
787
867
  credentials,
788
868
  headers,
789
869
  body,
870
+ streamMode,
790
871
  onResponse,
791
872
  onFinish,
792
873
  onError
@@ -827,6 +908,7 @@ function useCompletion({
827
908
  ...unref2(body),
828
909
  ...options == null ? void 0 : options.body
829
910
  },
911
+ streamMode,
830
912
  setCompletion: mutate,
831
913
  setLoading: (loading) => mutateLoading(() => loading),
832
914
  setError: (err) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../use-chat.ts","../../shared/stream-parts.ts","../../shared/read-data-stream.ts","../../shared/generate-id.ts","../../shared/parse-complex-response.ts","../../shared/call-chat-api.ts","../../shared/process-chat-stream.ts","../use-completion.ts","../../shared/call-completion-api.ts"],"sourcesContent":["import swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\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: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: Ref<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: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\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 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 const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesData.value;\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.value ?? []) as JSONValue[];\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: function_call,\n }),\n }),\n ),\n body: {\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\n ...options?.body,\n },\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 streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n mutate(previousMessages);\n },\n generateId,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\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 error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (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 = ref(initialInput);\n\n const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {\n e.preventDefault();\n const inputValue = input.value;\n if (!inputValue) return;\n append(\n {\n content: inputValue,\n role: 'user',\n },\n options,\n );\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\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(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/**\n * Parses 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/**\n * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,\n * and appends a new line.\n *\n * It 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\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 { parseComplexResponse } from './parse-complex-response';\nimport { IdGenerator, JSONValue, Message } from './types';\n\nexport async function callChatApi({\n api,\n messages,\n body,\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 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 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","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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\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: Ref<string>;\n /** The error object of the API request */\n error: Ref<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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (e: any) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\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, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\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 = ref(initialInput);\n\n const handleSubmit = (e: any) => {\n e.preventDefault();\n const inputValue = input.value;\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 handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { readDataStream } from './read-data-stream';\nimport { JSONValue } from './types';\n\nexport async function callCompletionApi({\n api,\n prompt,\n credentials,\n headers,\n body,\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 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 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\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,OAAO,UAAU;AAEjB,SAAS,KAAK,aAAa;;;ACa3B,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,UAAU,MAAM,kBAClB,EAAE,eAAe,MAAM,kBACvB,OAAO,MAAM,cAAc,SAAS,YACpC,OAAO,MAAM,cAAc,cAAc,UACzC;AACA,YAAM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;AAAA,MACA;AAAA,MACA,SAAS;AAAA,QACP,GAAG;AAAA,QACH,GAAG,mCAAS;AAAA,MACd;AAAA,MACA,MAAM;AAAA,QACJ,GAAGC,OAAM,IAAI;AAAA,QACb,GAAG,mCAAS;AAAA,MACd;AAAA,MACA,eAAe;AAAA,MACf,YAAY,aAAW,cAAc,MAAM,OAAO;AAAA,MAClD,UAAU,SAAO;AACf,cAAM,QAAQ;AAAA,MAChB;AAAA,MACA,oBAAoB,gBAAc;AAChC,0BAAkB;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,QAAQ,CAAAH,UAAQ;AACd,yBAAiB,MAAM,CAAC,GAAG,cAAc,GAAIA,SAAA,OAAAA,QAAQ,CAAC,CAAE,CAAC;AAAA,MAC3D;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,WAA6C,OACjD,QACA,YACG;AACH,WAAO,eAAe,QAAQ,OAAO;AAAA,EACvC;AAEA,QAAM,OAAO,MAAM;AACjB,QAAI,iBAAiB;AACnB,sBAAgB,MAAM;AACtB,wBAAkB;AAAA,IACpB;AAAA,EACF;AAEA,QAAM,gBAAgB,CAACI,gBAAuB;AAC5C,WAAOA,WAAU;AAAA,EACnB;AAEA,QAAM,QAAQH,KAAI,YAAY;AAE9B,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,MAAM;AAAA,EACR;AACF;","names":["streamParts","generateId","generateId","fixFunctionCallArguments","generateId","_a","data","messages","swrv","ref","unref","uniqueId","useSWRV","swrv","store","data","ref","_a","unref","completion"]}
1
+ {"version":3,"sources":["../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":["import swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\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: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: Ref<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: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\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 const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesData.value;\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.value ?? []) as JSONValue[];\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: function_call,\n }),\n }),\n ),\n body: {\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\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 streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n mutate(previousMessages);\n },\n generateId,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\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 error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (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 = ref(initialInput);\n\n const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {\n e.preventDefault();\n const inputValue = input.value;\n if (!inputValue) return;\n append(\n {\n content: inputValue,\n role: 'user',\n },\n options,\n );\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\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: Ref<string>;\n /** The error object of the API request */\n error: Ref<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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (e: any) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\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, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\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 = ref(initialInput);\n\n const handleSubmit = (e: any) => {\n e.preventDefault();\n const inputValue = input.value;\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 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,OAAO,UAAU;AAEjB,SAAS,KAAK,aAAa;;;ACa3B,IAAM,iBAAkD;AAAA,EACtD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO,CAAC,UAAqB;AAC3B,QAAI,OAAO,UAAU,UAAU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ACA;AAAA,MACA,QAAQ,CAAAH,UAAQ;AACd,yBAAiB,MAAM,CAAC,GAAG,cAAc,GAAIA,SAAA,OAAAA,QAAQ,CAAC,CAAE,CAAC;AAAA,MAC3D;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,WAA6C,OACjD,QACA,YACG;AACH,WAAO,eAAe,QAAQ,OAAO;AAAA,EACvC;AAEA,QAAM,OAAO,MAAM;AACjB,QAAI,iBAAiB;AACnB,sBAAgB,MAAM;AACtB,wBAAkB;AAAA,IACpB;AAAA,EACF;AAEA,QAAM,gBAAgB,CAACI,gBAAuB;AAC5C,WAAOA,WAAU;AAAA,EACnB;AAEA,QAAM,QAAQH,KAAI,YAAY;AAE9B,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,MAAM;AAAA,EACR;AACF;","names":["streamParts","generateId","generateId","fixFunctionCallArguments","generateId","_a","data","messages","swrv","ref","unref","uniqueId","useSWRV","swrv","store","data","ref","_a","unref","completion"]}
@@ -1,51 +0,0 @@
1
- import { LanguageModelV1 } from '@ai-sdk/provider';
2
-
3
- type AnthropicMessagesModelId = 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307' | (string & {});
4
- interface AnthropicMessagesSettings {
5
- /**
6
- Only sample from the top K options for each subsequent token.
7
-
8
- Used to remove "long tail" low probability responses.
9
- Recommended for advanced use cases only. You usually only need to use temperature.
10
- */
11
- topK?: number;
12
- }
13
-
14
- type AnthropicMessagesConfig = {
15
- provider: string;
16
- baseUrl: string;
17
- headers: () => Record<string, string | undefined>;
18
- };
19
- declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
20
- readonly specificationVersion = "v1";
21
- readonly defaultObjectGenerationMode = "tool";
22
- readonly modelId: AnthropicMessagesModelId;
23
- readonly settings: AnthropicMessagesSettings;
24
- private readonly config;
25
- constructor(modelId: AnthropicMessagesModelId, settings: AnthropicMessagesSettings, config: AnthropicMessagesConfig);
26
- get provider(): string;
27
- private getArgs;
28
- doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
29
- doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
30
- }
31
-
32
- /**
33
- * Anthropic provider.
34
- */
35
- declare class Anthropic {
36
- readonly baseUrl?: string;
37
- readonly apiKey?: string;
38
- constructor(options?: {
39
- baseUrl?: string;
40
- apiKey?: string;
41
- generateId?: () => string;
42
- });
43
- private get baseConfig();
44
- messages(modelId: AnthropicMessagesModelId, settings?: AnthropicMessagesSettings): AnthropicMessagesLanguageModel;
45
- }
46
- /**
47
- * Default Anthropic provider instance.
48
- */
49
- declare const anthropic: Anthropic;
50
-
51
- export { Anthropic, anthropic };
@@ -1,51 +0,0 @@
1
- import { LanguageModelV1 } from '@ai-sdk/provider';
2
-
3
- type AnthropicMessagesModelId = 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307' | (string & {});
4
- interface AnthropicMessagesSettings {
5
- /**
6
- Only sample from the top K options for each subsequent token.
7
-
8
- Used to remove "long tail" low probability responses.
9
- Recommended for advanced use cases only. You usually only need to use temperature.
10
- */
11
- topK?: number;
12
- }
13
-
14
- type AnthropicMessagesConfig = {
15
- provider: string;
16
- baseUrl: string;
17
- headers: () => Record<string, string | undefined>;
18
- };
19
- declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
20
- readonly specificationVersion = "v1";
21
- readonly defaultObjectGenerationMode = "tool";
22
- readonly modelId: AnthropicMessagesModelId;
23
- readonly settings: AnthropicMessagesSettings;
24
- private readonly config;
25
- constructor(modelId: AnthropicMessagesModelId, settings: AnthropicMessagesSettings, config: AnthropicMessagesConfig);
26
- get provider(): string;
27
- private getArgs;
28
- doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
29
- doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
30
- }
31
-
32
- /**
33
- * Anthropic provider.
34
- */
35
- declare class Anthropic {
36
- readonly baseUrl?: string;
37
- readonly apiKey?: string;
38
- constructor(options?: {
39
- baseUrl?: string;
40
- apiKey?: string;
41
- generateId?: () => string;
42
- });
43
- private get baseConfig();
44
- messages(modelId: AnthropicMessagesModelId, settings?: AnthropicMessagesSettings): AnthropicMessagesLanguageModel;
45
- }
46
- /**
47
- * Default Anthropic provider instance.
48
- */
49
- declare const anthropic: Anthropic;
50
-
51
- export { Anthropic, anthropic };