@ai-sdk/vue
Advanced tools
Comparing version 0.0.49 to 0.0.50
@@ -63,5 +63,9 @@ "use strict"; | ||
generateId: generateId2 = import_ui_utils.generateId, | ||
onToolCall, | ||
fetch: fetch2, | ||
keepLastMessageOnError = false | ||
} = {}) { | ||
keepLastMessageOnError = false, | ||
maxSteps | ||
} = { | ||
maxSteps: 1 | ||
}) { | ||
var _a, _b; | ||
@@ -92,2 +96,3 @@ if (streamMode) { | ||
async function triggerRequest(messagesSnapshot, { options, data, headers, body } = {}) { | ||
const messageCount = messages.value.length; | ||
try { | ||
@@ -121,2 +126,3 @@ error.value = void 0; | ||
annotations, | ||
toolInvocations, | ||
function_call | ||
@@ -129,2 +135,3 @@ }) => ({ | ||
...annotations !== void 0 && { annotations }, | ||
...toolInvocations !== void 0 && { toolInvocations }, | ||
// outdated function/tool call handling (TODO deprecate): | ||
@@ -165,4 +172,3 @@ ...function_call !== void 0 && { function_call } | ||
generateId: generateId2, | ||
onToolCall: void 0, | ||
// not implemented yet | ||
onToolCall, | ||
fetch: fetch2 | ||
@@ -190,2 +196,13 @@ }); | ||
} | ||
const lastMessage = messages.value[messages.value.length - 1]; | ||
if ( | ||
// ensure we actually have new messages (to prevent infinite loops in case of errors): | ||
messages.value.length > messageCount && // ensure there is a last message: | ||
lastMessage != null && // check if the feature is enabled: | ||
maxSteps && maxSteps > 0 && // check that next step is possible: | ||
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps: | ||
countTrailingAssistantMessages(messages.value) <= maxSteps | ||
) { | ||
await triggerRequest(messages.value); | ||
} | ||
} | ||
@@ -238,2 +255,23 @@ const append = async (message, options) => { | ||
}; | ||
const addToolResult = ({ | ||
toolCallId, | ||
result | ||
}) => { | ||
const updatedMessages = messages.value.map( | ||
(message, index, arr) => ( | ||
// update the tool calls in the last assistant message: | ||
index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? { | ||
...message, | ||
toolInvocations: message.toolInvocations.map( | ||
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation | ||
) | ||
} : message | ||
) | ||
); | ||
mutate(updatedMessages); | ||
const lastMessage = updatedMessages[updatedMessages.length - 1]; | ||
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) { | ||
triggerRequest(updatedMessages); | ||
} | ||
}; | ||
return { | ||
@@ -249,5 +287,20 @@ messages, | ||
isLoading, | ||
data: streamData | ||
data: streamData, | ||
addToolResult | ||
}; | ||
} | ||
function isAssistantMessageWithCompletedToolCalls(message) { | ||
return message.role === "assistant" && message.toolInvocations && message.toolInvocations.length > 0 && message.toolInvocations.every((toolInvocation) => "result" in toolInvocation); | ||
} | ||
function countTrailingAssistantMessages(messages) { | ||
let count = 0; | ||
for (let i = messages.length - 1; i >= 0; i--) { | ||
if (messages[i].role === "assistant") { | ||
count++; | ||
} else { | ||
break; | ||
} | ||
} | ||
return count; | ||
} | ||
@@ -254,0 +307,0 @@ // src/use-completion.ts |
# @ai-sdk/vue | ||
## 0.0.50 | ||
### Patch Changes | ||
- 692e265: feat (ui/vue): add multi-step support to useChat | ||
## 0.0.49 | ||
@@ -4,0 +10,0 @@ |
@@ -41,4 +41,15 @@ import { Message, CreateMessage, ChatRequestOptions, JSONValue, UseChatOptions, RequestOptions, UseCompletionOptions, AssistantStatus, UseAssistantOptions } from '@ai-sdk/ui-utils'; | ||
data: Ref<JSONValue[] | undefined>; | ||
addToolResult: ({ toolCallId, result, }: { | ||
toolCallId: string; | ||
result: any; | ||
}) => void; | ||
}; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers: metadataHeaders, body: metadataBody, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions): UseChatHelpers; | ||
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers: metadataHeaders, body: metadataBody, generateId, onToolCall, fetch, keepLastMessageOnError, maxSteps, }?: UseChatOptions & { | ||
/** | ||
* Maximum number of sequential LLM calls (steps), e.g. when you use tool calls. Must be at least 1. | ||
* A maximum number is required to prevent infinite loops in the case of misconfigured tools. | ||
* By default, it's set to 1, which means that only a single LLM call is made. | ||
*/ | ||
maxSteps?: number; | ||
}): UseChatHelpers; | ||
@@ -45,0 +56,0 @@ type UseCompletionHelpers = { |
@@ -63,5 +63,9 @@ "use strict"; | ||
generateId: generateId2 = import_ui_utils.generateId, | ||
onToolCall, | ||
fetch: fetch2, | ||
keepLastMessageOnError = false | ||
} = {}) { | ||
keepLastMessageOnError = false, | ||
maxSteps | ||
} = { | ||
maxSteps: 1 | ||
}) { | ||
var _a, _b; | ||
@@ -92,2 +96,3 @@ if (streamMode) { | ||
async function triggerRequest(messagesSnapshot, { options, data, headers, body } = {}) { | ||
const messageCount = messages.value.length; | ||
try { | ||
@@ -121,2 +126,3 @@ error.value = void 0; | ||
annotations, | ||
toolInvocations, | ||
function_call | ||
@@ -129,2 +135,3 @@ }) => ({ | ||
...annotations !== void 0 && { annotations }, | ||
...toolInvocations !== void 0 && { toolInvocations }, | ||
// outdated function/tool call handling (TODO deprecate): | ||
@@ -165,4 +172,3 @@ ...function_call !== void 0 && { function_call } | ||
generateId: generateId2, | ||
onToolCall: void 0, | ||
// not implemented yet | ||
onToolCall, | ||
fetch: fetch2 | ||
@@ -190,2 +196,13 @@ }); | ||
} | ||
const lastMessage = messages.value[messages.value.length - 1]; | ||
if ( | ||
// ensure we actually have new messages (to prevent infinite loops in case of errors): | ||
messages.value.length > messageCount && // ensure there is a last message: | ||
lastMessage != null && // check if the feature is enabled: | ||
maxSteps && maxSteps > 0 && // check that next step is possible: | ||
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps: | ||
countTrailingAssistantMessages(messages.value) <= maxSteps | ||
) { | ||
await triggerRequest(messages.value); | ||
} | ||
} | ||
@@ -238,2 +255,23 @@ const append = async (message, options) => { | ||
}; | ||
const addToolResult = ({ | ||
toolCallId, | ||
result | ||
}) => { | ||
const updatedMessages = messages.value.map( | ||
(message, index, arr) => ( | ||
// update the tool calls in the last assistant message: | ||
index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? { | ||
...message, | ||
toolInvocations: message.toolInvocations.map( | ||
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation | ||
) | ||
} : message | ||
) | ||
); | ||
mutate(updatedMessages); | ||
const lastMessage = updatedMessages[updatedMessages.length - 1]; | ||
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) { | ||
triggerRequest(updatedMessages); | ||
} | ||
}; | ||
return { | ||
@@ -249,5 +287,20 @@ messages, | ||
isLoading, | ||
data: streamData | ||
data: streamData, | ||
addToolResult | ||
}; | ||
} | ||
function isAssistantMessageWithCompletedToolCalls(message) { | ||
return message.role === "assistant" && message.toolInvocations && message.toolInvocations.length > 0 && message.toolInvocations.every((toolInvocation) => "result" in toolInvocation); | ||
} | ||
function countTrailingAssistantMessages(messages) { | ||
let count = 0; | ||
for (let i = messages.length - 1; i >= 0; i--) { | ||
if (messages[i].role === "assistant") { | ||
count++; | ||
} else { | ||
break; | ||
} | ||
} | ||
return count; | ||
} | ||
@@ -254,0 +307,0 @@ // src/use-completion.ts |
{ | ||
"name": "@ai-sdk/vue", | ||
"version": "0.0.49", | ||
"version": "0.0.50", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
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
147081
1879