@ai-sdk/ui-utils
Advanced tools
Comparing version 0.0.39 to 0.0.40
@@ -42,6 +42,6 @@ "use strict"; | ||
jsonSchema: () => jsonSchema, | ||
parseComplexResponse: () => parseComplexResponse, | ||
parsePartialJson: () => parsePartialJson, | ||
parseStreamPart: () => parseStreamPart, | ||
processChatStream: () => processChatStream, | ||
processDataProtocolResponse: () => processDataProtocolResponse, | ||
readDataStream: () => readDataStream, | ||
@@ -53,3 +53,3 @@ zodSchema: () => zodSchema | ||
// src/parse-complex-response.ts | ||
// src/process-data-protocol-response.ts | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
@@ -604,2 +604,23 @@ | ||
}; | ||
var finishRoundtripStreamPart = { | ||
code: "e", | ||
name: "finish_roundtrip", | ||
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) || !("completionTokens" in value.usage)) { | ||
throw new Error( | ||
'"finish_roundtrip" parts expect an object with a "finishReason" and "usage" property.' | ||
); | ||
} | ||
if (typeof value.usage.promptTokens !== "number") { | ||
value.usage.promptTokens = Number.NaN; | ||
} | ||
if (typeof value.usage.completionTokens !== "number") { | ||
value.usage.completionTokens = Number.NaN; | ||
} | ||
return { | ||
type: "finish_roundtrip", | ||
value | ||
}; | ||
} | ||
}; | ||
var streamParts = [ | ||
@@ -619,3 +640,4 @@ textStreamPart, | ||
toolCallDeltaStreamPart, | ||
finishMessageStreamPart | ||
finishMessageStreamPart, | ||
finishRoundtripStreamPart | ||
]; | ||
@@ -636,3 +658,4 @@ var streamPartsByCode = { | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart, | ||
[finishMessageStreamPart.code]: finishMessageStreamPart | ||
[finishMessageStreamPart.code]: finishMessageStreamPart, | ||
[finishRoundtripStreamPart.code]: finishRoundtripStreamPart | ||
}; | ||
@@ -653,3 +676,4 @@ var StreamStringPrefixes = { | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code, | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code, | ||
[finishRoundtripStreamPart.name]: finishRoundtripStreamPart.code | ||
}; | ||
@@ -723,3 +747,3 @@ var validCodes = streamParts.map((part) => part.code); | ||
// src/parse-complex-response.ts | ||
// src/process-data-protocol-response.ts | ||
function assignAnnotationsToMessage(message, annotations) { | ||
@@ -730,3 +754,3 @@ if (!message || !annotations || !annotations.length) | ||
} | ||
async function parseComplexResponse({ | ||
async function processDataProtocolResponse({ | ||
reader, | ||
@@ -742,5 +766,6 @@ abortControllerRef, | ||
const createdAt = getCurrentDate(); | ||
const prefixMap = { | ||
data: [] | ||
}; | ||
let prefixMap = {}; | ||
let nextPrefixMap = void 0; | ||
const previousMessages = []; | ||
const data = []; | ||
let message_annotations = void 0; | ||
@@ -760,2 +785,29 @@ const partialToolCalls = {}; | ||
} | ||
if (type === "finish_roundtrip") { | ||
nextPrefixMap = {}; | ||
continue; | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
continue; | ||
} | ||
if (nextPrefixMap) { | ||
if (prefixMap.text) { | ||
previousMessages.push(prefixMap.text); | ||
} | ||
if (prefixMap.function_call) { | ||
previousMessages.push(prefixMap.function_call); | ||
} | ||
if (prefixMap.tool_calls) { | ||
previousMessages.push(prefixMap.tool_calls); | ||
} | ||
prefixMap = nextPrefixMap; | ||
nextPrefixMap = void 0; | ||
} | ||
if (type === "text") { | ||
@@ -776,11 +828,2 @@ if (prefixMap["text"]) { | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
} | ||
if (type === "tool_call_streaming_start") { | ||
@@ -890,3 +933,3 @@ if (prefixMap.text == null) { | ||
if (type === "data") { | ||
prefixMap["data"].push(...value); | ||
data.push(...value); | ||
} | ||
@@ -914,12 +957,11 @@ let responseMessage = prefixMap["text"]; | ||
if (message_annotations == null ? void 0 : message_annotations.length) { | ||
const messagePrefixKeys = [ | ||
"text", | ||
"function_call", | ||
"tool_calls" | ||
]; | ||
messagePrefixKeys.forEach((key) => { | ||
if (prefixMap[key]) { | ||
prefixMap[key].annotations = [...message_annotations]; | ||
} | ||
}); | ||
if (prefixMap.text) { | ||
prefixMap.text.annotations = [...message_annotations]; | ||
} | ||
if (prefixMap.function_call) { | ||
prefixMap.function_call.annotations = [...message_annotations]; | ||
} | ||
if (prefixMap.tool_calls) { | ||
prefixMap.tool_calls.annotations = [...message_annotations]; | ||
} | ||
} | ||
@@ -929,5 +971,5 @@ const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({ | ||
})); | ||
update(merged, [...prefixMap["data"]]); | ||
update([...previousMessages, ...merged], [...data]); | ||
} | ||
onFinish == null ? void 0 : onFinish({ prefixMap, finishReason, usage }); | ||
onFinish == null ? void 0 : onFinish({ message: prefixMap.text, finishReason, usage }); | ||
return { | ||
@@ -939,3 +981,3 @@ messages: [ | ||
].filter(Boolean), | ||
data: prefixMap.data | ||
data | ||
}; | ||
@@ -1023,3 +1065,3 @@ } | ||
case "data": { | ||
return await parseComplexResponse({ | ||
return await processDataProtocolResponse({ | ||
reader, | ||
@@ -1029,5 +1071,5 @@ abortControllerRef: abortController != null ? { current: abortController() } : void 0, | ||
onToolCall, | ||
onFinish({ prefixMap, finishReason, usage }) { | ||
if (onFinish && prefixMap.text != null) { | ||
onFinish(prefixMap.text, { usage, finishReason }); | ||
onFinish({ message, finishReason, usage }) { | ||
if (onFinish && message != null) { | ||
onFinish(message, { usage, finishReason }); | ||
} | ||
@@ -1389,6 +1431,6 @@ }, | ||
jsonSchema, | ||
parseComplexResponse, | ||
parsePartialJson, | ||
parseStreamPart, | ||
processChatStream, | ||
processDataProtocolResponse, | ||
readDataStream, | ||
@@ -1395,0 +1437,0 @@ zodSchema |
@@ -624,3 +624,3 @@ import { LanguageModelV1FinishReason, JSONValue as JSONValue$1 } from '@ai-sdk/provider'; | ||
onResponse: ((response: Response) => void | Promise<void>) | undefined; | ||
onUpdate: (merged: Message[], data: JSONValue[] | undefined) => void; | ||
onUpdate: (newMessages: Message[], data: JSONValue[] | undefined) => void; | ||
onFinish: UseChatOptions['onFinish']; | ||
@@ -704,3 +704,10 @@ onToolCall: UseChatOptions['onToolCall']; | ||
}>; | ||
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; | ||
declare const finishRoundtripStreamPart: StreamPart<'e', 'finish_roundtrip', { | ||
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 | typeof finishRoundtripStreamPart; | ||
/** | ||
@@ -712,3 +719,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> | ReturnType<typeof finishMessageStreamPart.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> | ReturnType<typeof finishRoundtripStreamPart.parse>; | ||
/** | ||
@@ -751,2 +758,3 @@ * The map of prefixes for data in the stream | ||
readonly finish_message: "d"; | ||
readonly finish_roundtrip: "e"; | ||
}; | ||
@@ -805,15 +813,3 @@ /** | ||
type PrefixMap = { | ||
text?: Message; | ||
function_call?: Message & { | ||
role: 'assistant'; | ||
function_call: FunctionCall; | ||
}; | ||
tool_calls?: Message & { | ||
role: 'assistant'; | ||
tool_calls: ToolCall[]; | ||
}; | ||
data: JSONValue[]; | ||
}; | ||
declare function parseComplexResponse({ reader, abortControllerRef, update, onToolCall, onFinish, generateId, getCurrentDate, }: { | ||
declare function processDataProtocolResponse({ reader, abortControllerRef, update, onToolCall, onFinish, generateId, getCurrentDate, }: { | ||
reader: ReadableStreamDefaultReader<Uint8Array>; | ||
@@ -823,6 +819,6 @@ abortControllerRef?: { | ||
}; | ||
update: (merged: Message[], data: JSONValue[] | undefined) => void; | ||
update: (newMessages: Message[], data: JSONValue[] | undefined) => void; | ||
onToolCall?: UseChatOptions['onToolCall']; | ||
onFinish?: (options: { | ||
prefixMap: PrefixMap; | ||
message: Message | undefined; | ||
finishReason: LanguageModelV1FinishReason; | ||
@@ -909,2 +905,2 @@ usage: { | ||
export { type AssistantMessage, type AssistantStatus, type Attachment, type ChatRequest, type ChatRequestOptions, type CreateMessage, type DataMessage, type DeepPartial, type Function, type FunctionCall, type FunctionCallHandler, type IdGenerator, type JSONValue, type Message, type RequestOptions, type Schema, type StreamPart, type StreamString, type Tool, type ToolCall, type ToolCallHandler, type ToolChoice, type ToolInvocation, type UseAssistantOptions, type UseChatOptions, type UseCompletionOptions, asSchema, callChatApi, callCompletionApi, createChunkDecoder, formatStreamPart, getTextFromDataUrl, isDeepEqualData, jsonSchema, parseComplexResponse, parsePartialJson, parseStreamPart, processChatStream, readDataStream, zodSchema }; | ||
export { type AssistantMessage, type AssistantStatus, type Attachment, type ChatRequest, type ChatRequestOptions, type CreateMessage, type DataMessage, type DeepPartial, type Function, type FunctionCall, type FunctionCallHandler, type IdGenerator, type JSONValue, type Message, type RequestOptions, type Schema, type StreamPart, type StreamString, type Tool, type ToolCall, type ToolCallHandler, type ToolChoice, type ToolInvocation, type UseAssistantOptions, type UseChatOptions, type UseCompletionOptions, asSchema, callChatApi, callCompletionApi, createChunkDecoder, formatStreamPart, getTextFromDataUrl, isDeepEqualData, jsonSchema, parsePartialJson, parseStreamPart, processChatStream, processDataProtocolResponse, readDataStream, zodSchema }; |
@@ -42,6 +42,6 @@ "use strict"; | ||
jsonSchema: () => jsonSchema, | ||
parseComplexResponse: () => parseComplexResponse, | ||
parsePartialJson: () => parsePartialJson, | ||
parseStreamPart: () => parseStreamPart, | ||
processChatStream: () => processChatStream, | ||
processDataProtocolResponse: () => processDataProtocolResponse, | ||
readDataStream: () => readDataStream, | ||
@@ -53,3 +53,3 @@ zodSchema: () => zodSchema | ||
// src/parse-complex-response.ts | ||
// src/process-data-protocol-response.ts | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
@@ -604,2 +604,23 @@ | ||
}; | ||
var finishRoundtripStreamPart = { | ||
code: "e", | ||
name: "finish_roundtrip", | ||
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) || !("completionTokens" in value.usage)) { | ||
throw new Error( | ||
'"finish_roundtrip" parts expect an object with a "finishReason" and "usage" property.' | ||
); | ||
} | ||
if (typeof value.usage.promptTokens !== "number") { | ||
value.usage.promptTokens = Number.NaN; | ||
} | ||
if (typeof value.usage.completionTokens !== "number") { | ||
value.usage.completionTokens = Number.NaN; | ||
} | ||
return { | ||
type: "finish_roundtrip", | ||
value | ||
}; | ||
} | ||
}; | ||
var streamParts = [ | ||
@@ -619,3 +640,4 @@ textStreamPart, | ||
toolCallDeltaStreamPart, | ||
finishMessageStreamPart | ||
finishMessageStreamPart, | ||
finishRoundtripStreamPart | ||
]; | ||
@@ -636,3 +658,4 @@ var streamPartsByCode = { | ||
[toolCallDeltaStreamPart.code]: toolCallDeltaStreamPart, | ||
[finishMessageStreamPart.code]: finishMessageStreamPart | ||
[finishMessageStreamPart.code]: finishMessageStreamPart, | ||
[finishRoundtripStreamPart.code]: finishRoundtripStreamPart | ||
}; | ||
@@ -653,3 +676,4 @@ var StreamStringPrefixes = { | ||
[toolCallDeltaStreamPart.name]: toolCallDeltaStreamPart.code, | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code | ||
[finishMessageStreamPart.name]: finishMessageStreamPart.code, | ||
[finishRoundtripStreamPart.name]: finishRoundtripStreamPart.code | ||
}; | ||
@@ -723,3 +747,3 @@ var validCodes = streamParts.map((part) => part.code); | ||
// src/parse-complex-response.ts | ||
// src/process-data-protocol-response.ts | ||
function assignAnnotationsToMessage(message, annotations) { | ||
@@ -730,3 +754,3 @@ if (!message || !annotations || !annotations.length) | ||
} | ||
async function parseComplexResponse({ | ||
async function processDataProtocolResponse({ | ||
reader, | ||
@@ -742,5 +766,6 @@ abortControllerRef, | ||
const createdAt = getCurrentDate(); | ||
const prefixMap = { | ||
data: [] | ||
}; | ||
let prefixMap = {}; | ||
let nextPrefixMap = void 0; | ||
const previousMessages = []; | ||
const data = []; | ||
let message_annotations = void 0; | ||
@@ -760,2 +785,29 @@ const partialToolCalls = {}; | ||
} | ||
if (type === "finish_roundtrip") { | ||
nextPrefixMap = {}; | ||
continue; | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
continue; | ||
} | ||
if (nextPrefixMap) { | ||
if (prefixMap.text) { | ||
previousMessages.push(prefixMap.text); | ||
} | ||
if (prefixMap.function_call) { | ||
previousMessages.push(prefixMap.function_call); | ||
} | ||
if (prefixMap.tool_calls) { | ||
previousMessages.push(prefixMap.tool_calls); | ||
} | ||
prefixMap = nextPrefixMap; | ||
nextPrefixMap = void 0; | ||
} | ||
if (type === "text") { | ||
@@ -776,11 +828,2 @@ if (prefixMap["text"]) { | ||
} | ||
if (type === "finish_message") { | ||
const { completionTokens, promptTokens } = value.usage; | ||
finishReason = value.finishReason; | ||
usage = { | ||
completionTokens, | ||
promptTokens, | ||
totalTokens: completionTokens + promptTokens | ||
}; | ||
} | ||
if (type === "tool_call_streaming_start") { | ||
@@ -890,3 +933,3 @@ if (prefixMap.text == null) { | ||
if (type === "data") { | ||
prefixMap["data"].push(...value); | ||
data.push(...value); | ||
} | ||
@@ -914,12 +957,11 @@ let responseMessage = prefixMap["text"]; | ||
if (message_annotations == null ? void 0 : message_annotations.length) { | ||
const messagePrefixKeys = [ | ||
"text", | ||
"function_call", | ||
"tool_calls" | ||
]; | ||
messagePrefixKeys.forEach((key) => { | ||
if (prefixMap[key]) { | ||
prefixMap[key].annotations = [...message_annotations]; | ||
} | ||
}); | ||
if (prefixMap.text) { | ||
prefixMap.text.annotations = [...message_annotations]; | ||
} | ||
if (prefixMap.function_call) { | ||
prefixMap.function_call.annotations = [...message_annotations]; | ||
} | ||
if (prefixMap.tool_calls) { | ||
prefixMap.tool_calls.annotations = [...message_annotations]; | ||
} | ||
} | ||
@@ -929,5 +971,5 @@ const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({ | ||
})); | ||
update(merged, [...prefixMap["data"]]); | ||
update([...previousMessages, ...merged], [...data]); | ||
} | ||
onFinish == null ? void 0 : onFinish({ prefixMap, finishReason, usage }); | ||
onFinish == null ? void 0 : onFinish({ message: prefixMap.text, finishReason, usage }); | ||
return { | ||
@@ -939,3 +981,3 @@ messages: [ | ||
].filter(Boolean), | ||
data: prefixMap.data | ||
data | ||
}; | ||
@@ -1023,3 +1065,3 @@ } | ||
case "data": { | ||
return await parseComplexResponse({ | ||
return await processDataProtocolResponse({ | ||
reader, | ||
@@ -1029,5 +1071,5 @@ abortControllerRef: abortController != null ? { current: abortController() } : void 0, | ||
onToolCall, | ||
onFinish({ prefixMap, finishReason, usage }) { | ||
if (onFinish && prefixMap.text != null) { | ||
onFinish(prefixMap.text, { usage, finishReason }); | ||
onFinish({ message, finishReason, usage }) { | ||
if (onFinish && message != null) { | ||
onFinish(message, { usage, finishReason }); | ||
} | ||
@@ -1389,6 +1431,6 @@ }, | ||
jsonSchema, | ||
parseComplexResponse, | ||
parsePartialJson, | ||
parseStreamPart, | ||
processChatStream, | ||
processDataProtocolResponse, | ||
readDataStream, | ||
@@ -1395,0 +1437,0 @@ zodSchema |
{ | ||
"name": "@ai-sdk/ui-utils", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"license": "Apache-2.0", | ||
@@ -11,3 +11,4 @@ "sideEffects": false, | ||
"dist/**/*", | ||
"test/dist/**/*" | ||
"test/dist/**/*", | ||
"CHANGELOG.md" | ||
], | ||
@@ -14,0 +15,0 @@ "exports": { |
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
4070383
21
37722