@ai-sdk/ui-utils
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -573,2 +573,17 @@ "use strict"; | ||
}; | ||
var finishMessageStreamPart = { | ||
code: "d", | ||
name: "finish_message", | ||
parse: (value) => { | ||
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string" || !("usage" in value) || value.usage == null || typeof value.usage !== "object" || !("promptTokens" in value.usage) || typeof value.usage.promptTokens !== "number" || !("completionTokens" in value.usage) || typeof value.usage.completionTokens !== "number") { | ||
throw new Error( | ||
'"finish_message" parts expect an object with a "finishReason" and "usage" property.' | ||
); | ||
} | ||
return { | ||
type: "finish_message", | ||
value | ||
}; | ||
} | ||
}; | ||
var streamParts = [ | ||
@@ -587,3 +602,4 @@ textStreamPart, | ||
toolCallStreamingStartStreamPart, | ||
toolCallDeltaStreamPart | ||
toolCallDeltaStreamPart, | ||
finishMessageStreamPart | ||
]; | ||
@@ -603,3 +619,4 @@ var streamPartsByCode = { | ||
[toolCallStreamingStartStreamPart.code]: toolCallStreamingStartStreamPart, | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart, | ||
[finishMessageStreamPart.code]: finishMessageStreamPart | ||
}; | ||
@@ -619,3 +636,4 @@ var StreamStringPrefixes = { | ||
[toolCallStreamingStartStreamPart.name]: toolCallStreamingStartStreamPart.code, | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code, | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code | ||
}; | ||
@@ -710,2 +728,8 @@ var validCodes = streamParts.map((part) => part.code); | ||
const partialToolCalls = {}; | ||
let usage = { | ||
completionTokens: NaN, | ||
promptTokens: NaN, | ||
totalTokens: NaN | ||
}; | ||
let finishReason = "unknown"; | ||
for await (const { type, value } of readDataStream(reader, { | ||
@@ -729,2 +753,11 @@ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
} | ||
if (type === "tool_call_streaming_start") { | ||
@@ -875,3 +908,3 @@ if (prefixMap.text == null) { | ||
} | ||
onFinish == null ? void 0 : onFinish(prefixMap); | ||
onFinish == null ? void 0 : onFinish({ prefixMap, finishReason, usage }); | ||
return { | ||
@@ -892,3 +925,3 @@ messages: [ | ||
body, | ||
streamMode = "stream-data", | ||
streamProtocol = "data", | ||
credentials, | ||
@@ -936,3 +969,3 @@ headers, | ||
const reader = response.body.getReader(); | ||
switch (streamMode) { | ||
switch (streamProtocol) { | ||
case "text": { | ||
@@ -959,3 +992,6 @@ const decoder = createChunkDecoder(); | ||
} | ||
onFinish == null ? void 0 : onFinish(resultMessage); | ||
onFinish == null ? void 0 : onFinish(resultMessage, { | ||
usage: { completionTokens: NaN, promptTokens: NaN, totalTokens: NaN }, | ||
finishReason: "unknown" | ||
}); | ||
return { | ||
@@ -966,3 +1002,3 @@ messages: [resultMessage], | ||
} | ||
case "stream-data": { | ||
case "data": { | ||
return await parseComplexResponse({ | ||
@@ -973,5 +1009,5 @@ reader, | ||
onToolCall, | ||
onFinish(prefixMap) { | ||
onFinish({ prefixMap, finishReason, usage }) { | ||
if (onFinish && prefixMap.text != null) { | ||
onFinish(prefixMap.text); | ||
onFinish(prefixMap.text, { usage, finishReason }); | ||
} | ||
@@ -983,4 +1019,4 @@ }, | ||
default: { | ||
const exhaustiveCheck = streamMode; | ||
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`); | ||
const exhaustiveCheck = streamProtocol; | ||
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`); | ||
} | ||
@@ -998,3 +1034,3 @@ } | ||
body, | ||
streamMode = "stream-data", | ||
streamProtocol = "data", | ||
setCompletion, | ||
@@ -1048,3 +1084,3 @@ setLoading, | ||
const reader = res.body.getReader(); | ||
switch (streamMode) { | ||
switch (streamProtocol) { | ||
case "text": { | ||
@@ -1066,3 +1102,3 @@ const decoder = createChunkDecoder(); | ||
} | ||
case "stream-data": { | ||
case "data": { | ||
for await (const { type, value } of readDataStream(reader, { | ||
@@ -1086,4 +1122,4 @@ isAborted: () => abortController === null | ||
default: { | ||
const exhaustiveCheck = streamMode; | ||
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`); | ||
const exhaustiveCheck = streamProtocol; | ||
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`); | ||
} | ||
@@ -1090,0 +1126,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { LanguageModelV1FinishReason } from '@ai-sdk/provider'; | ||
export { generateId } from '@ai-sdk/provider-utils'; | ||
@@ -46,2 +47,20 @@ import { z } from 'zod'; | ||
/** | ||
Represents the number of tokens used in a prompt and completion. | ||
*/ | ||
type CompletionTokenUsage = { | ||
/** | ||
The number of tokens used in the prompt. | ||
*/ | ||
promptTokens: number; | ||
/** | ||
The number of tokens used in the completion. | ||
*/ | ||
completionTokens: number; | ||
/** | ||
The total number of tokens used (promptTokens + completionTokens). | ||
*/ | ||
totalTokens: number; | ||
}; | ||
type AssistantStatus = 'in_progress' | 'awaiting_message'; | ||
@@ -436,5 +455,12 @@ type UseAssistantOptions = { | ||
/** | ||
* Callback function to be called when the chat is finished streaming. | ||
* Optional callback function that is called when the assistant message is finished streaming. | ||
* | ||
* @param message The message that was streamed. | ||
* @param options.usage The token usage of the message. | ||
* @param options.finishReason The finish reason of the message. | ||
*/ | ||
onFinish?: (message: Message) => void; | ||
onFinish?: (message: Message, options: { | ||
usage: CompletionTokenUsage; | ||
finishReason: LanguageModelV1FinishReason; | ||
}) => void; | ||
/** | ||
@@ -478,5 +504,13 @@ * Callback function to be called when an error is encountered. | ||
sendExtraMessageFields?: boolean; | ||
/** Stream mode (default to "stream-data") */ | ||
/** | ||
* Stream mode (default to "stream-data") | ||
* | ||
* @deprecated Use `streamProtocol` instead. | ||
*/ | ||
streamMode?: 'stream-data' | 'text'; | ||
/** | ||
Streaming protocol that is used. Defaults to `data`. | ||
*/ | ||
streamProtocol?: 'data' | 'text'; | ||
/** | ||
Custom fetch implementation. You can use it as a middleware to intercept requests, | ||
@@ -542,5 +576,13 @@ or to provide a custom fetch implementation for e.g. testing. | ||
body?: object; | ||
/** Stream mode (default to "stream-data") */ | ||
/** | ||
* Stream mode (default to "stream-data") | ||
* | ||
* @deprecated Use `streamProtocol` instead. | ||
*/ | ||
streamMode?: 'stream-data' | 'text'; | ||
/** | ||
Streaming protocol that is used. Defaults to `data`. | ||
*/ | ||
streamProtocol?: 'data' | 'text'; | ||
/** | ||
Custom fetch implementation. You can use it as a middleware to intercept requests, | ||
@@ -579,6 +621,6 @@ or to provide a custom fetch implementation for e.g. testing. | ||
declare const getOriginalFetch$1: () => typeof fetch; | ||
declare function callChatApi({ api, body, streamMode, credentials, headers, abortController, restoreMessagesOnFailure, onResponse, onUpdate, onFinish, onToolCall, generateId, fetch, }: { | ||
declare function callChatApi({ api, body, streamProtocol, credentials, headers, abortController, restoreMessagesOnFailure, onResponse, onUpdate, onFinish, onToolCall, generateId, fetch, }: { | ||
api: string; | ||
body: Record<string, any>; | ||
streamMode: 'stream-data' | 'text' | undefined; | ||
streamProtocol: 'data' | 'text' | undefined; | ||
credentials: RequestCredentials | undefined; | ||
@@ -590,4 +632,4 @@ headers: HeadersInit | undefined; | ||
onUpdate: (merged: Message[], data: JSONValue[] | undefined) => void; | ||
onFinish: ((message: Message) => void) | undefined; | ||
onToolCall: UseChatOptions['onToolCall'] | undefined; | ||
onFinish: UseChatOptions['onFinish']; | ||
onToolCall: UseChatOptions['onToolCall']; | ||
generateId: IdGenerator; | ||
@@ -609,3 +651,3 @@ fetch: ReturnType<typeof getOriginalFetch$1> | undefined; | ||
declare const getOriginalFetch: () => typeof fetch; | ||
declare function callCompletionApi({ api, prompt, credentials, headers, body, streamMode, setCompletion, setLoading, setError, setAbortController, onResponse, onFinish, onError, onData, fetch, }: { | ||
declare function callCompletionApi({ api, prompt, credentials, headers, body, streamProtocol, setCompletion, setLoading, setError, setAbortController, onResponse, onFinish, onError, onData, fetch, }: { | ||
api: string; | ||
@@ -616,3 +658,3 @@ prompt: string; | ||
body: Record<string, any>; | ||
streamMode: 'stream-data' | 'text' | undefined; | ||
streamProtocol: 'data' | 'text' | undefined; | ||
setCompletion: (completion: string) => void; | ||
@@ -664,3 +706,10 @@ setLoading: (loading: boolean) => void; | ||
}>; | ||
type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallsStreamPart | typeof messageAnnotationsStreamPart | typeof toolCallStreamPart | typeof toolResultStreamPart | typeof toolCallStreamingStartStreamPart | typeof toolCallDeltaStreamPart; | ||
declare const finishMessageStreamPart: StreamPart<'d', 'finish_message', { | ||
finishReason: LanguageModelV1FinishReason; | ||
usage: { | ||
promptTokens: number; | ||
completionTokens: number; | ||
}; | ||
}>; | ||
type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallsStreamPart | typeof messageAnnotationsStreamPart | typeof toolCallStreamPart | typeof toolResultStreamPart | typeof toolCallStreamingStartStreamPart | typeof toolCallDeltaStreamPart | typeof finishMessageStreamPart; | ||
/** | ||
@@ -672,3 +721,3 @@ * Maps the type of a stream part to its value type. | ||
}; | ||
type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse> | ReturnType<typeof toolCallsStreamPart.parse> | ReturnType<typeof messageAnnotationsStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse> | ReturnType<typeof toolResultStreamPart.parse> | ReturnType<typeof toolCallStreamingStartStreamPart.parse> | ReturnType<typeof toolCallDeltaStreamPart.parse>; | ||
type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse> | ReturnType<typeof toolCallsStreamPart.parse> | ReturnType<typeof messageAnnotationsStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse> | ReturnType<typeof toolResultStreamPart.parse> | ReturnType<typeof toolCallStreamingStartStreamPart.parse> | ReturnType<typeof toolCallDeltaStreamPart.parse> | ReturnType<typeof finishMessageStreamPart.parse>; | ||
/** | ||
@@ -710,2 +759,3 @@ * The map of prefixes for data in the stream | ||
readonly tool_call_delta: "c"; | ||
readonly finish_message: "d"; | ||
}; | ||
@@ -777,3 +827,11 @@ /** | ||
onToolCall?: UseChatOptions['onToolCall']; | ||
onFinish?: (prefixMap: PrefixMap) => void; | ||
onFinish?: (options: { | ||
prefixMap: PrefixMap; | ||
finishReason: LanguageModelV1FinishReason; | ||
usage: { | ||
completionTokens: number; | ||
promptTokens: number; | ||
totalTokens: number; | ||
}; | ||
}) => void; | ||
generateId?: () => string; | ||
@@ -780,0 +838,0 @@ getCurrentDate?: () => Date; |
@@ -573,2 +573,17 @@ "use strict"; | ||
}; | ||
var finishMessageStreamPart = { | ||
code: "d", | ||
name: "finish_message", | ||
parse: (value) => { | ||
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string" || !("usage" in value) || value.usage == null || typeof value.usage !== "object" || !("promptTokens" in value.usage) || typeof value.usage.promptTokens !== "number" || !("completionTokens" in value.usage) || typeof value.usage.completionTokens !== "number") { | ||
throw new Error( | ||
'"finish_message" parts expect an object with a "finishReason" and "usage" property.' | ||
); | ||
} | ||
return { | ||
type: "finish_message", | ||
value | ||
}; | ||
} | ||
}; | ||
var streamParts = [ | ||
@@ -587,3 +602,4 @@ textStreamPart, | ||
toolCallStreamingStartStreamPart, | ||
toolCallDeltaStreamPart | ||
toolCallDeltaStreamPart, | ||
finishMessageStreamPart | ||
]; | ||
@@ -603,3 +619,4 @@ var streamPartsByCode = { | ||
[toolCallStreamingStartStreamPart.code]: toolCallStreamingStartStreamPart, | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart, | ||
[finishMessageStreamPart.code]: finishMessageStreamPart | ||
}; | ||
@@ -619,3 +636,4 @@ var StreamStringPrefixes = { | ||
[toolCallStreamingStartStreamPart.name]: toolCallStreamingStartStreamPart.code, | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code, | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code | ||
}; | ||
@@ -710,2 +728,8 @@ var validCodes = streamParts.map((part) => part.code); | ||
const partialToolCalls = {}; | ||
let usage = { | ||
completionTokens: NaN, | ||
promptTokens: NaN, | ||
totalTokens: NaN | ||
}; | ||
let finishReason = "unknown"; | ||
for await (const { type, value } of readDataStream(reader, { | ||
@@ -729,2 +753,11 @@ isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
} | ||
if (type === "tool_call_streaming_start") { | ||
@@ -875,3 +908,3 @@ if (prefixMap.text == null) { | ||
} | ||
onFinish == null ? void 0 : onFinish(prefixMap); | ||
onFinish == null ? void 0 : onFinish({ prefixMap, finishReason, usage }); | ||
return { | ||
@@ -892,3 +925,3 @@ messages: [ | ||
body, | ||
streamMode = "stream-data", | ||
streamProtocol = "data", | ||
credentials, | ||
@@ -936,3 +969,3 @@ headers, | ||
const reader = response.body.getReader(); | ||
switch (streamMode) { | ||
switch (streamProtocol) { | ||
case "text": { | ||
@@ -959,3 +992,6 @@ const decoder = createChunkDecoder(); | ||
} | ||
onFinish == null ? void 0 : onFinish(resultMessage); | ||
onFinish == null ? void 0 : onFinish(resultMessage, { | ||
usage: { completionTokens: NaN, promptTokens: NaN, totalTokens: NaN }, | ||
finishReason: "unknown" | ||
}); | ||
return { | ||
@@ -966,3 +1002,3 @@ messages: [resultMessage], | ||
} | ||
case "stream-data": { | ||
case "data": { | ||
return await parseComplexResponse({ | ||
@@ -973,5 +1009,5 @@ reader, | ||
onToolCall, | ||
onFinish(prefixMap) { | ||
onFinish({ prefixMap, finishReason, usage }) { | ||
if (onFinish && prefixMap.text != null) { | ||
onFinish(prefixMap.text); | ||
onFinish(prefixMap.text, { usage, finishReason }); | ||
} | ||
@@ -983,4 +1019,4 @@ }, | ||
default: { | ||
const exhaustiveCheck = streamMode; | ||
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`); | ||
const exhaustiveCheck = streamProtocol; | ||
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`); | ||
} | ||
@@ -998,3 +1034,3 @@ } | ||
body, | ||
streamMode = "stream-data", | ||
streamProtocol = "data", | ||
setCompletion, | ||
@@ -1048,3 +1084,3 @@ setLoading, | ||
const reader = res.body.getReader(); | ||
switch (streamMode) { | ||
switch (streamProtocol) { | ||
case "text": { | ||
@@ -1066,3 +1102,3 @@ const decoder = createChunkDecoder(); | ||
} | ||
case "stream-data": { | ||
case "data": { | ||
for await (const { type, value } of readDataStream(reader, { | ||
@@ -1086,4 +1122,4 @@ isAborted: () => abortController === null | ||
default: { | ||
const exhaustiveCheck = streamMode; | ||
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`); | ||
const exhaustiveCheck = streamProtocol; | ||
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`); | ||
} | ||
@@ -1090,0 +1126,0 @@ } |
{ | ||
"name": "@ai-sdk/ui-utils", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"license": "Apache-2.0", | ||
@@ -28,2 +28,3 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.14", | ||
"@ai-sdk/provider-utils": "1.0.5", | ||
@@ -30,0 +31,0 @@ "secure-json-parse": "2.7.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4026151
37299
4
+ Added@ai-sdk/provider@0.0.14