chore(deps): updated patch

This commit is contained in:
Neko Ayaka 2026-08-09 03:35:50 +08:00
parent 16f7dfaf4d
commit 38a008500d
No known key found for this signature in database
12 changed files with 628 additions and 766 deletions

2
.gitignore vendored
View file

@ -144,6 +144,8 @@ apps/stage-tamagotchi/electron.vite.config.*.mjs
docs/ai/context/verifications/
.playwright-mcp/
.vishot/
.alint/
.alintcache
# Generated from packages/i18n/glossary/terms.yaml at upload time. Never committed, so it
# cannot drift from its source.

View file

@ -29,7 +29,7 @@ const provider = {
function createMockStreamResult(
steps: Promise<unknown[]> = Promise.resolve([]),
totalUsage: Promise<{ prompt_tokens: number, completion_tokens: number, total_tokens: number } | undefined> = Promise.resolve(undefined),
totalUsage: Promise<{ inputTokens: number, outputTokens: number, totalTokens: number } | undefined> = Promise.resolve(undefined),
) {
return {
steps,
@ -44,7 +44,7 @@ describe('streamFrom tool error capture', () => {
const onUsage = vi.fn()
streamTextMock.mockReturnValueOnce(createMockStreamResult(
Promise.resolve([]),
Promise.resolve({ prompt_tokens: 12, completion_tokens: 8, total_tokens: 20 }),
Promise.resolve({ inputTokens: 12, outputTokens: 8, totalTokens: 20 }),
))
await streamFrom({
@ -84,7 +84,7 @@ describe('streamFrom tool error capture', () => {
const onUsage = vi.fn()
streamTextMock.mockReturnValueOnce(createMockStreamResult(
Promise.resolve([]),
Promise.resolve({} as { prompt_tokens: number, completion_tokens: number, total_tokens: number }),
Promise.resolve({} as { inputTokens: number, outputTokens: number, totalTokens: number }),
))
await streamFrom({

View file

@ -114,14 +114,14 @@ function createCapturedToolErrorResult(toolName: string, error: unknown): string
}
function normalizeUsage(usage: Usage | undefined) {
if (!usage || (usage.prompt_tokens == null && usage.completion_tokens == null && usage.total_tokens == null)) {
if (usage?.inputTokens == null || usage.outputTokens == null || usage.totalTokens == null) {
return { source: 'unavailable' as const }
}
return {
inputTokens: usage.prompt_tokens,
outputTokens: usage.completion_tokens,
totalTokens: usage.total_tokens,
inputTokens: usage.inputTokens,
outputTokens: usage.outputTokens,
totalTokens: usage.totalTokens,
source: 'reported' as const,
}
}
@ -241,6 +241,7 @@ export async function streamFrom({
// chat body, so unknown runtime-only fields can be rejected upstream.
// AIRI captures tool failures by wrapping local tool executors instead.
tools: streamTools,
toolChoice: options?.toolChoice,
onEvent,
})

View file

@ -1,5 +1,5 @@
import type { ChatProvider } from '@xsai-ext/providers/utils'
import type { CommonContentPart, CompletionToolCall, CompletionToolResult, Message, Tool } from '@xsai/shared-chat'
import type { CommonContentPart, CompletionToolCall, CompletionToolResult, Message, Tool, ToolChoice } from '@xsai/shared-chat'
/** Describes whether generation usage came from the provider or a local fallback. */
export type LlmUsageSource = 'reported' | 'estimated' | 'unavailable'
@ -36,6 +36,8 @@ export interface StreamOptions {
supportsTools?: boolean
waitForTools?: boolean
captureToolErrors?: boolean
/** Provider tool-selection directive for one request. */
toolChoice?: ToolChoice
tools?: Tool[] | (() => Promise<Tool[] | undefined>)
/**
* Per-model runtime cache of whether the provider accepts content-part arrays

View file

@ -1,7 +1,7 @@
<script setup lang="ts">
import { all } from '@proj-airi/i18n'
import { useAnalytics } from '@proj-airi/stage-ui/composables/use-analytics'
import { isAnalyticsAvailableInBuild } from '@proj-airi/stage-ui/stores/analytics'
import { isAnalyticsAvailableInBuild } from '@proj-airi/stage-ui/libs/analytics'
import { useSettings } from '@proj-airi/stage-ui/stores/settings'
import { FieldCheckbox, FieldCombobox, useTheme } from '@proj-airi/ui'
import { computed } from 'vue'

View file

@ -1,33 +1,38 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index bfa8434ddad32d08af9d9aedaf6a1f1caa21cac8..db248a93025a37d2c0d11d7a6bb31aa9ba6ee63b 100644
index c9cc51d8b715b6f220d67574b8c674247d032926..9b4c2e6b096a0a139792d3fd4ce89c2f15bfc51e 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,9 +1,13 @@
@@ -1,11 +1,15 @@
import { WithUnknown } from '@xsai/shared';
-import { ChatOptions, CompletionStep, PrepareStep, StopCondition, FinishReason, AssistantMessage, Usage, Message, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
+import { ChatOptions, CompletionStep, OnToolCallFinishCallback, OnToolCallStartCallback, PrepareStep, RepairToolCallFunction, StopCondition, FinishReason, AssistantMessage, Usage, Message, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
-import { ChatOptions, CompletionStep, PostToolCall, PrepareStep, PreToolCall, StopCondition, Message, Usage, FinishReason, AssistantMessage, ChatCompletionUsage, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
+import { ChatOptions, CompletionStep, OnToolCallFinishCallback, OnToolCallStartCallback, PostToolCall, PrepareStep, PreToolCall, RepairToolCallFunction, StopCondition, Message, Usage, FinishReason, AssistantMessage, ChatCompletionUsage, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
interface GenerateTextOptions extends ChatOptions {
onStepFinish?: (step: CompletionStep<true>) => Promise<unknown> | unknown;
+ captureToolErrors?: boolean;
+ onToolCallFinish?: OnToolCallFinishCallback;
+ onToolCallStart?: OnToolCallStartCallback;
postToolCall?: PostToolCall;
prepareStep?: PrepareStep;
preToolCall?: PreToolCall;
+ repairToolCall?: RepairToolCallFunction;
/** @internal */
steps?: CompletionStep<true>[];
/** @default `stepCountAtLeast(1)` */
diff --git a/dist/index.js b/dist/index.js
index 11940a69300f795caa7b780deba1f61a4d3127f6..2cefa4f4806493b21aae75df2cb1ae4e6ae5f160 100644
index d4fdab8b13f83fc2eb55eca0d15919133347393e..334428415f242fc6b11c874e988bc98187c3eadc 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -41,6 +41,10 @@ const rawGenerateText = async (options) => {
@@ -55,9 +55,13 @@ const rawGenerateText = async (options) => {
const results = await Promise.all(
msgToolCalls.map(async (toolCall) => executeTool({
abortSignal: options.abortSignal,
+ captureToolErrors: options.captureToolErrors,
messages,
+ onToolCallFinish: options.onToolCallFinish,
+ onToolCallStart: options.onToolCallStart,
postToolCall: options.postToolCall,
preToolCall: options.preToolCall,
+ repairToolCall: options.repairToolCall,
toolCall,
tools: options.tools

View file

@ -1,264 +0,0 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index 1b8df0289811d82777e853bc4290e35b16b0bd36..4682f7bbf62b1bc3d65abde34d7366c64f9b5eb0 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -96,6 +96,8 @@ interface CompletionToolCall {
}
interface CompletionToolResult {
args: Record<string, unknown>;
+ error?: Error;
+ isError?: boolean;
result: ToolMessage['content'];
toolCallId: string;
toolName: string;
@@ -222,9 +224,32 @@ interface DetermineStepTypeOptions {
/** @internal */
declare const determineStepType: ({ finishReason, stepsLength, toolCallsLength, willContinue }: DetermineStepTypeOptions) => CompletionStepType;
+type OnToolCallFinishCallback = (context: {
+ durationMs: number;
+ error?: unknown;
+ output?: unknown;
+ toolCallId: string;
+ toolName: string;
+}) => Promise<void> | void;
+type OnToolCallStartCallback = (context: {
+ input: unknown;
+ toolCallId: string;
+ toolName: string;
+}) => Promise<void> | void;
+type RepairToolCallFunction = (context: {
+ error: Error;
+ messages: Message[];
+ toolCall: ToolCall;
+ tools?: Tool[];
+}) => Promise<ToolCall | null> | ToolCall | null;
+
interface ExecuteToolOptions {
abortSignal?: AbortSignal;
+ captureToolErrors?: boolean;
messages: Message[];
+ onToolCallFinish?: OnToolCallFinishCallback;
+ onToolCallStart?: OnToolCallStartCallback;
+ repairToolCall?: RepairToolCallFunction;
toolCall: ToolCall;
tools?: Tool[];
}
@@ -233,7 +257,7 @@ interface ExecuteToolResult {
completionToolResult: CompletionToolResult;
message: ToolMessage;
}
-declare const executeTool: ({ abortSignal, messages, toolCall, tools }: ExecuteToolOptions) => Promise<ExecuteToolResult>;
+declare const executeTool: (options: ExecuteToolOptions) => Promise<ExecuteToolResult>;
interface ResolvedStepOptions {
messages: Message[];
@@ -256,4 +280,4 @@ declare const hasToolCall: (name?: string) => StopCondition;
declare const shouldStop: (stopWhen: StopCondition, context: StopContext) => boolean;
export { and, chat, determineStepType, executeTool, hasToolCall, not, or, resolveStepOptions, shouldStop, stepCountAtLeast };
-export type { AssistantMessage, AudioContentPart, ChatOptions, CommonContentPart, CompletionStep, CompletionStepType, CompletionToolCall, CompletionToolResult, DetermineStepTypeOptions, DeveloperMessage, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, PrepareStep, PrepareStepOptions, PrepareStepResult, RefusalContentPart, ResolveStepOptionsOptions, ResolvedStepOptions, StopCondition, StopContext, StopStep, SystemMessage, TextContentPart, Tool, ToolCall, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, Usage, UserMessage };
+export type { AssistantMessage, AudioContentPart, ChatOptions, CommonContentPart, CompletionStep, CompletionStepType, CompletionToolCall, CompletionToolResult, DetermineStepTypeOptions, DeveloperMessage, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, OnToolCallFinishCallback, OnToolCallStartCallback, PrepareStep, PrepareStepOptions, PrepareStepResult, RefusalContentPart, RepairToolCallFunction, ResolveStepOptionsOptions, ResolvedStepOptions, StopCondition, StopContext, StopStep, SystemMessage, TextContentPart, Tool, ToolCall, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, Usage, UserMessage };
diff --git a/dist/index.js b/dist/index.js
index 34e87341ac400dd8c61457c188485b82a1383300..2e7e1fa99fdbccb7a0be0af92504ed903d95c482 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -66,57 +66,150 @@ const runTool = async (tool, options) => {
});
}
};
-const executeTool = async ({ abortSignal, messages, toolCall, tools }) => {
- const toolName = toolCall.function.name;
- const toolArguments = toolCall.function.arguments;
- if (toolName == null) {
- throw new InvalidToolCallError(`Missing toolCall.function.name: ${JSON.stringify(toolCall)}`, {
- reason: "missing_name",
- toolCall
- });
- }
- if (toolArguments == null) {
- throw new InvalidToolCallError(`Missing toolCall.function.arguments: ${JSON.stringify(toolCall)}`, {
- reason: "missing_arguments",
- toolCall
- });
- }
- const tool = tools?.find((tool2) => tool2.function.name === toolName);
- if (!tool) {
- const availableTools = tools?.map((tool2) => tool2.function.name);
- const availableToolsErrorMsg = availableTools == null || availableTools.length === 0 ? "No tools are available" : `Available tools: ${availableTools.join(", ")}`;
- throw new InvalidToolCallError(`Model tried to call unavailable tool "${toolName}", ${availableToolsErrorMsg}.`, {
- availableTools,
- reason: "unknown_tool",
- toolCall,
- toolName
- });
- }
- const parsedArgs = parseToolInput(toolName, toolArguments);
- const result = await runTool(tool, { abortSignal, messages, parsedArgs, toolCall });
- const completionToolCall = {
- args: toolArguments,
- toolCallId: toolCall.id,
- toolCallType: toolCall.type,
- toolName
- };
- const completionToolResult = {
- args: parsedArgs,
- result,
- toolCallId: toolCall.id,
- toolName
- };
- const message = {
- content: result,
- role: "tool",
- tool_call_id: toolCall.id
- };
+const buildErrorReturn = (toolCall, toolName, toolCallId, error) => {
+ const errorMessage = error instanceof Error ? error.message : String(error);
+ const errorContent = `Tool call error for "${toolName}": ${errorMessage}`;
return {
- completionToolCall,
- completionToolResult,
- message
+ completionToolCall: {
+ args: toolCall.function?.arguments ?? "{}",
+ toolCallId,
+ toolCallType: toolCall.type ?? "function",
+ toolName
+ },
+ completionToolResult: {
+ args: {},
+ error,
+ isError: true,
+ result: errorContent,
+ toolCallId,
+ toolName
+ },
+ message: {
+ content: errorContent,
+ role: "tool",
+ tool_call_id: toolCallId
+ }
};
};
+const executeTool = async ({
+ abortSignal,
+ captureToolErrors,
+ messages,
+ onToolCallFinish,
+ onToolCallStart,
+ repairToolCall,
+ toolCall,
+ tools
+}) => {
+ const resolvedToolName = toolCall.function?.name ?? "unknown";
+ const toolCallId = toolCall.id;
+ const startTime = Date.now();
+ try {
+ const toolName = toolCall.function.name;
+ const toolArguments = toolCall.function.arguments;
+ if (toolName == null) {
+ throw new InvalidToolCallError(`Missing toolCall.function.name: ${JSON.stringify(toolCall)}`, {
+ reason: "missing_name",
+ toolCall
+ });
+ }
+ if (toolArguments == null) {
+ throw new InvalidToolCallError(`Missing toolCall.function.arguments: ${JSON.stringify(toolCall)}`, {
+ reason: "missing_arguments",
+ toolCall
+ });
+ }
+ const tool = tools?.find((tool2) => tool2.function.name === toolName);
+ if (!tool) {
+ const availableTools = tools?.map((tool2) => tool2.function.name);
+ const availableToolsErrorMsg = availableTools == null || availableTools.length === 0 ? "No tools are available" : `Available tools: ${availableTools.join(", ")}`;
+ throw new InvalidToolCallError(`Model tried to call unavailable tool "${toolName}", ${availableToolsErrorMsg}.`, {
+ availableTools,
+ reason: "unknown_tool",
+ toolCall,
+ toolName
+ });
+ }
+ const parsedArgs = parseToolInput(toolName, toolArguments);
+ if (onToolCallStart) {
+ try {
+ await onToolCallStart({ input: parsedArgs, toolCallId, toolName });
+ } catch {
+ }
+ }
+ const result = await runTool(tool, { abortSignal, messages, parsedArgs, toolCall });
+ if (onToolCallFinish) {
+ try {
+ await onToolCallFinish({
+ durationMs: Date.now() - startTime,
+ error: void 0,
+ output: result,
+ toolCallId,
+ toolName
+ });
+ } catch {
+ }
+ }
+ const completionToolCall = {
+ args: toolArguments,
+ toolCallId: toolCall.id,
+ toolCallType: toolCall.type,
+ toolName
+ };
+ const completionToolResult = {
+ args: parsedArgs,
+ result,
+ toolCallId: toolCall.id,
+ toolName
+ };
+ const message = {
+ content: result,
+ role: "tool",
+ tool_call_id: toolCall.id
+ };
+ return {
+ completionToolCall,
+ completionToolResult,
+ message
+ };
+ } catch (error) {
+ if (isAbortError(error, abortSignal))
+ throw error;
+ if (repairToolCall && (InvalidToolCallError.isInstance(error) || InvalidToolInputError.isInstance(error))) {
+ try {
+ const repaired = await repairToolCall({ error, messages, toolCall, tools });
+ if (repaired != null) {
+ return executeTool({
+ abortSignal,
+ captureToolErrors,
+ messages,
+ onToolCallFinish,
+ onToolCallStart,
+ repairToolCall: void 0,
+ toolCall: repaired,
+ tools
+ });
+ }
+ } catch {
+ }
+ }
+ if (onToolCallFinish) {
+ try {
+ await onToolCallFinish({
+ durationMs: Date.now() - startTime,
+ error,
+ output: void 0,
+ toolCallId,
+ toolName: resolvedToolName
+ });
+ } catch {
+ }
+ }
+ if (!captureToolErrors)
+ throw error;
+ return buildErrorReturn(toolCall, resolvedToolName, toolCallId, error);
+ }
+};
const resolveStepOptions = async ({ messages, model, prepareStep, stepNumber, steps, toolChoice }) => {
const prepared = prepareStep == null ? void 0 : await prepareStep({

View file

@ -0,0 +1,211 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index b327ec1c9dda3a8c4ddf39beb6a7b4ffc6afb860..8f740ffc45080cf6b80155e62fa87b090aa8efff 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -94,6 +94,7 @@ interface CompletionToolCall {
}
interface CompletionToolResult {
args: unknown;
+ error?: unknown;
isError?: boolean;
result: ToolExecuteResult;
toolCallId: string;
@@ -101,6 +102,24 @@ interface CompletionToolResult {
}
type PostToolCall = (toolResult: CompletionToolResult, options: ToolExecuteOptions) => CompletionToolResult | Promise<CompletionToolResult | void> | void;
type PreToolCall = (toolCall: CompletionToolCall, options: ToolExecuteOptions) => CompletionToolCall | CompletionToolResult | Promise<CompletionToolCall | CompletionToolResult | void> | void;
+type OnToolCallFinishCallback = (context: {
+ durationMs: number;
+ error?: unknown;
+ output?: unknown;
+ toolCallId: string;
+ toolName: string;
+}) => Promise<void> | void;
+type OnToolCallStartCallback = (context: {
+ input: unknown;
+ toolCallId: string;
+ toolName: string;
+}) => Promise<void> | void;
+type RepairToolCallFunction = (context: {
+ error: Error;
+ messages: Message[];
+ toolCall: ToolCall;
+ tools?: Tool[];
+}) => Promise<ToolCall | null> | ToolCall | null;
interface Tool {
execute: (input: unknown, options: ToolExecuteOptions) => Promise<ToolExecuteResult> | ToolExecuteResult;
function: {
@@ -275,9 +294,13 @@ declare const chat: <T extends WithUnknown<ChatOptions>>(options: T) => Promise<
interface ExecuteToolOptions<T = ToolMessage['content']> {
abortSignal?: AbortSignal;
+ captureToolErrors?: boolean;
messages: Message[];
+ onToolCallFinish?: OnToolCallFinishCallback;
+ onToolCallStart?: OnToolCallStartCallback;
postToolCall?: PostToolCall;
preToolCall?: PreToolCall;
+ repairToolCall?: RepairToolCallFunction;
toolCall: ToolCall;
tools?: Tool[];
wrapResult?: (result: ToolExecuteResult) => T;
@@ -285,10 +308,11 @@ interface ExecuteToolOptions<T = ToolMessage['content']> {
interface ExecuteToolResult<T = ToolMessage['content']> {
completionToolCall: CompletionToolCall;
completionToolResult: CompletionToolResult;
+ message: ToolMessage;
result: T;
}
declare const toCompletionToolCall: (toolCall: ToolCall) => CompletionToolCall;
-declare const executeTool: <T = ToolMessage["content"]>({ abortSignal, messages, postToolCall, preToolCall, toolCall, tools, wrapResult }: ExecuteToolOptions<T>) => Promise<ExecuteToolResult<T>>;
+declare const executeTool: <T = ToolMessage["content"]>(options: ExecuteToolOptions<T>) => Promise<ExecuteToolResult<T>>;
interface ResolvePrepareStepOptions<TInput = Message[], TToolChoice = ToolChoice> {
input: TInput;
@@ -317,4 +341,4 @@ declare const computeTotalUsage: (totalUsage: undefined | Usage, usage: Usage) =
declare const normalizeChatCompletionUsage: (usage: ChatCompletionUsage) => Usage;
export { and, chat, computeTotalUsage, executeTool, hasToolCall, normalizeChatCompletionUsage, not, or, resolvePrepareStep, shouldStop, stepCountAtLeast, toCompletionToolCall };
-export type { AssistantMessage, AudioContentPart, ChatCompletionUsage, ChatOptions, CommonContentPart, CompletionStep, CompletionToolCall, CompletionToolResult, DeveloperMessage, ErrorEvent, Event, EventType, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, PostToolCall, PreToolCall, PrepareStep, PrepareStepOptions, PrepareStepResult, ReasoningDeltaEvent, ReasoningDoneEvent, ReasoningStartEvent, RefusalContentPart, ResolvePrepareStepOptions, ResolvePrepareStepResult, StepDoneEvent, StepStartEvent, StopCondition, StopContext, SystemMessage, TextContentPart, TextDeltaEvent, TextDoneEvent, TextStartEvent, Tool, ToolCall, ToolCallDeltaEvent, ToolCallDoneEvent, ToolCallStartEvent, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, ToolResultDoneEvent, ToolValidateFailure, ToolValidateResult, ToolValidateSuccess, Usage, UserMessage };
+export type { AssistantMessage, AudioContentPart, ChatCompletionUsage, ChatOptions, CommonContentPart, CompletionStep, CompletionToolCall, CompletionToolResult, DeveloperMessage, ErrorEvent, Event, EventType, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, OnToolCallFinishCallback, OnToolCallStartCallback, PostToolCall, PreToolCall, PrepareStep, PrepareStepOptions, PrepareStepResult, RepairToolCallFunction, ReasoningDeltaEvent, ReasoningDoneEvent, ReasoningStartEvent, RefusalContentPart, ResolvePrepareStepOptions, ResolvePrepareStepResult, StepDoneEvent, StepStartEvent, StopCondition, StopContext, SystemMessage, TextContentPart, TextDeltaEvent, TextDoneEvent, TextStartEvent, Tool, ToolCall, ToolCallDeltaEvent, ToolCallDoneEvent, ToolCallStartEvent, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, ToolResultDoneEvent, ToolValidateFailure, ToolValidateResult, ToolValidateSuccess, Usage, UserMessage };
diff --git a/dist/index.js b/dist/index.js
index d8fa061c8fe9ba9b76aafea74f40e27a957b0a17..45373a31d56a3127377789177e28e609c99fca64 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1,4 +1,4 @@
-import { postJSON, InvalidToolCallError, InvalidToolInputError } from '@xsai/shared';
+import { postJSON, InvalidToolCallError, InvalidToolInputError, ToolExecutionError } from '@xsai/shared';
const chat = async (options) => postJSON("chat/completions", {
...options,
@@ -56,15 +56,19 @@ const parseToolInput = async (tool, input) => {
};
const createErrorToolResult = (toolCall, args, cause, abortSignal) => ({
args,
+ error: cause,
isError: true,
result: `Tool "${toolCall.toolName}" execution failed: ${abortSignal?.aborted === true ? "This operation was aborted" : cause instanceof Error ? cause.message : String(cause)}`,
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName
});
-const catchToolError = async (toolCall, abortSignal, callback) => {
+const isAbortError = (error, abortSignal) => abortSignal?.aborted === true || error instanceof Error && error.name === "AbortError";
+const catchToolError = async (toolCall, abortSignal, captureToolErrors, callback) => {
try {
return await callback(toolCall);
} catch (cause) {
+ if (isAbortError(cause, abortSignal) || !captureToolErrors)
+ throw cause;
return createErrorToolResult(toolCall, InvalidToolInputError.isInstance(cause) ? cause.toolInput : toolCall.args, cause, abortSignal);
}
};
@@ -90,7 +94,7 @@ const findTool = (tools, toolName, toolCall) => {
}
return tool;
};
-const executeTool = async ({ abortSignal, messages, postToolCall, preToolCall, toolCall, tools, wrapResult }) => {
+const executeToolBase = async ({ abortSignal, captureToolErrors, messages, postToolCall, preToolCall, toolCall, tools, wrapResult }) => {
const wrap = wrapResult ?? toToolMessageContent;
const toolName = toolCall.function.name;
const toolArguments = toolCall.function.arguments;
@@ -120,7 +124,7 @@ const executeTool = async ({ abortSignal, messages, postToolCall, preToolCall, t
let completionToolResult;
let parsedArgs;
let shouldPostToolCall = false;
- const preToolCallResult = await catchToolError(completionToolCall, abortSignal, async (toolCall2) => preToolCall?.(toolCall2, toolExecuteOptions));
+ const preToolCallResult = await catchToolError(completionToolCall, abortSignal, captureToolErrors, async (toolCall2) => preToolCall?.(toolCall2, toolExecuteOptions));
if (preToolCallResult) {
assertSameToolCallId(completionToolCall.toolCallId, preToolCallResult, "preToolCallResult");
if ("result" in preToolCallResult)
@@ -128,13 +132,25 @@ const executeTool = async ({ abortSignal, messages, postToolCall, preToolCall, t
else
completionToolCall = preToolCallResult;
}
- completionToolResult ??= await catchToolError(completionToolCall, abortSignal, async () => {
+ completionToolResult ??= await catchToolError(completionToolCall, abortSignal, captureToolErrors, async () => {
const tool = findTool(tools, completionToolCall.toolName, completionToolCall);
parsedArgs = await parseToolInput(tool, completionToolCall.args);
if (abortSignal?.aborted === true)
return createErrorToolResult(completionToolCall, parsedArgs, abortSignal.reason, abortSignal);
shouldPostToolCall = true;
- const result = await tool.execute(parsedArgs, toolExecuteOptions);
+ let result;
+ try {
+ result = await tool.execute(parsedArgs, toolExecuteOptions);
+ } catch (cause) {
+ if (isAbortError(cause, abortSignal))
+ throw cause;
+ throw new ToolExecutionError(`Tool "${completionToolCall.toolName}" execution failed.`, {
+ cause,
+ toolCallId: completionToolCall.toolCallId,
+ toolInput: parsedArgs,
+ toolName: completionToolCall.toolName
+ });
+ }
return {
args: parsedArgs,
result,
@@ -144,7 +160,7 @@ const executeTool = async ({ abortSignal, messages, postToolCall, preToolCall, t
});
if (shouldPostToolCall) {
completionToolResult.args = parsedArgs;
- const postToolCallResult = await catchToolError(completionToolResult, abortSignal, async (toolResult) => postToolCall?.(toolResult, toolExecuteOptions));
+ const postToolCallResult = await catchToolError(completionToolResult, abortSignal, captureToolErrors, async (toolResult) => postToolCall?.(toolResult, toolExecuteOptions));
if (postToolCallResult) {
assertSameToolCallId(completionToolResult.toolCallId, postToolCallResult, "postToolCallResult");
completionToolResult = postToolCallResult;
@@ -156,6 +172,54 @@ const executeTool = async ({ abortSignal, messages, postToolCall, preToolCall, t
result: wrap(completionToolResult.result)
};
};
+const callToolLifecycle = async (callback, context) => {
+ try {
+ await callback?.(context);
+ } catch {
+ }
+};
+const executeTool = async (options) => {
+ const { abortSignal, captureToolErrors, messages, onToolCallFinish, onToolCallStart, repairToolCall, toolCall, tools } = options;
+ const toolCallId = toolCall.id;
+ const toolName = toolCall.function?.name ?? "unknown";
+ const startTime = Date.now();
+ try {
+ const execution = await executeToolBase(options);
+ const failure = execution.completionToolResult.error;
+ if (failure instanceof Error && repairToolCall && (InvalidToolCallError.isInstance(failure) || InvalidToolInputError.isInstance(failure))) {
+ const repaired = await repairToolCall({ error: failure, messages, toolCall, tools });
+ if (repaired != null)
+ return executeTool({ ...options, repairToolCall: void 0, toolCall: repaired });
+ }
+ if (!execution.completionToolResult.isError)
+ await callToolLifecycle(onToolCallStart, { input: execution.completionToolResult.args, toolCallId, toolName });
+ await callToolLifecycle(onToolCallFinish, {
+ durationMs: Date.now() - startTime,
+ error: execution.completionToolResult.error,
+ output: execution.completionToolResult.isError ? void 0 : execution.completionToolResult.result,
+ toolCallId,
+ toolName
+ });
+ return {
+ ...execution,
+ message: {
+ content: execution.result,
+ role: "tool",
+ tool_call_id: execution.completionToolCall.toolCallId
+ }
+ };
+ } catch (error) {
+ if (isAbortError(error, abortSignal))
+ throw error;
+ if (error instanceof Error && repairToolCall && (InvalidToolCallError.isInstance(error) || InvalidToolInputError.isInstance(error))) {
+ const repaired = await repairToolCall({ error, messages, toolCall, tools });
+ if (repaired != null)
+ return executeTool({ ...options, repairToolCall: void 0, toolCall: repaired });
+ }
+ await callToolLifecycle(onToolCallFinish, { durationMs: Date.now() - startTime, error, toolCallId, toolName });
+ throw error;
+ }
+};
const resolvePrepareStep = async ({ input, model, prepareStep, stepNumber, steps, toolChoice }) => {
const prepared = prepareStep == null ? void 0 : await prepareStep({

View file

@ -1,55 +0,0 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index e184d851f8f0dec7456bab129ed90eaad8944125..e363d3d15261fa57831cf4d6fee384644bd45d00 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,8 +1,10 @@
import { WithUnknown } from '@xsai/shared';
-import { CompletionToolCall, CompletionToolResult, FinishReason, Usage, ChatOptions, CompletionStep, PrepareStep, StopCondition, Message } from '@xsai/shared-chat';
+import { CompletionToolCall, CompletionToolResult, FinishReason, OnToolCallFinishCallback, OnToolCallStartCallback, RepairToolCallFunction, Usage, ChatOptions, CompletionStep, PrepareStep, StopCondition, Message } from '@xsai/shared-chat';
type StreamTextEvent = (CompletionToolCall & {
type: 'tool-call';
+}) | (CompletionToolResult & {
+ type: 'tool-error';
}) | (CompletionToolResult & {
type: 'tool-result';
}) | {
@@ -33,7 +35,11 @@ interface StreamTextOptions extends ChatOptions {
onEvent?: (event: StreamTextEvent) => Promise<unknown> | unknown;
onFinish?: (step?: CompletionStep) => Promise<unknown> | unknown;
onStepFinish?: (step: CompletionStep) => Promise<unknown> | unknown;
+ captureToolErrors?: boolean;
+ onToolCallFinish?: OnToolCallFinishCallback;
+ onToolCallStart?: OnToolCallStartCallback;
prepareStep?: PrepareStep;
+ repairToolCall?: RepairToolCallFunction;
/** @default `stepCountAtLeast(1)` */
stopWhen?: StopCondition;
/**
diff --git a/dist/index.js b/dist/index.js
index 6353da4d45ad65340104530d83b5337bba8d9d77..34e6e4fda7f390be6334fe3eadcbc5199957e998 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -139,6 +139,10 @@ const streamText = (options) => {
validToolCalls.map(async (toolCall) => executeTool({
abortSignal: options.abortSignal,
+ captureToolErrors: options.captureToolErrors,
messages,
+ onToolCallFinish: options.onToolCallFinish,
+ onToolCallStart: options.onToolCallStart,
+ repairToolCall: options.repairToolCall,
toolCall,
tools: options.tools
}))
@@ -148,7 +151,10 @@ const streamText = (options) => {
toolResults.push(completionToolResult);
messages.push(message);
pushEvent({ ...completionToolCall, type: "tool-call" });
- pushEvent({ ...completionToolResult, type: "tool-result" });
+ pushEvent({
+ ...completionToolResult,
+ type: completionToolResult.isError ? "tool-error" : "tool-result"
+ });
}
} else {
pushEvent({

View file

@ -0,0 +1,200 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index a596849dec1316dd84e4261650aa6b439327a4f5..b238d42480e4654cdb89d0619540275b7796e80d 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,5 +1,35 @@
import { WithUnknown } from '@xsai/shared';
-import { ToolCall, FinishReason, ChatCompletionUsage, ChatOptions, Event, CompletionStep, PostToolCall, PrepareStep, PreToolCall, StopCondition, Message, Usage } from '@xsai/shared-chat';
+import { CompletionToolCall, CompletionToolResult, ToolCall, FinishReason, ChatCompletionUsage, ChatOptions, CompletionStep, OnToolCallFinishCallback, OnToolCallStartCallback, PostToolCall, PrepareStep, PreToolCall, RepairToolCallFunction, StopCondition, Message, Usage } from '@xsai/shared-chat';
+
+type StreamTextEvent = (CompletionToolCall & {
+ type: 'tool-call';
+}) | (CompletionToolResult & {
+ type: 'tool-error';
+}) | (CompletionToolResult & {
+ type: 'tool-result';
+}) | {
+ argsTextDelta: string;
+ toolCallId: string;
+ toolName: string;
+ type: 'tool-call-delta';
+} | {
+ error: unknown;
+ type: 'error';
+} | {
+ finishReason: FinishReason;
+ type: 'finish';
+ usage?: Usage;
+} | {
+ text: string;
+ type: 'reasoning-delta';
+} | {
+ text: string;
+ type: 'text-delta';
+} | {
+ toolCallId: string;
+ toolName: string;
+ type: 'tool-call-streaming-start';
+};
interface StreamTextChunkResult {
choices: {
@@ -27,12 +57,16 @@ interface StreamTextChunkResult {
}
interface StreamTextOptions extends ChatOptions {
- onEvent?: (event: Event) => Promise<unknown> | unknown;
+ onEvent?: (event: StreamTextEvent) => Promise<unknown> | unknown;
onFinish?: (step?: CompletionStep) => Promise<unknown> | unknown;
onStepFinish?: (step: CompletionStep) => Promise<unknown> | unknown;
+ captureToolErrors?: boolean;
+ onToolCallFinish?: OnToolCallFinishCallback;
+ onToolCallStart?: OnToolCallStartCallback;
postToolCall?: PostToolCall;
prepareStep?: PrepareStep;
preToolCall?: PreToolCall;
+ repairToolCall?: RepairToolCallFunction;
/** @default `stepCountAtLeast(1)` */
stopWhen?: StopCondition<Message>;
/**
@@ -48,8 +82,8 @@ interface StreamTextOptions extends ChatOptions {
};
}
interface StreamTextResult {
- eventStream: ReadableStream<Event>;
- fullStream: ReadableStream<StreamTextChunkResult>;
+ eventStream: ReadableStream<StreamTextEvent>;
+ fullStream: ReadableStream<StreamTextEvent>;
messages: Promise<Message[]>;
reasoningTextStream: ReadableStream<string>;
steps: Promise<CompletionStep[]>;
@@ -60,4 +94,4 @@ interface StreamTextResult {
declare const streamText: (options: WithUnknown<StreamTextOptions>) => StreamTextResult;
export { streamText };
-export type { StreamTextChunkResult, StreamTextOptions, StreamTextResult };
+export type { StreamTextChunkResult, StreamTextEvent, StreamTextOptions, StreamTextResult };
diff --git a/dist/index.js b/dist/index.js
index ec6e01b0defd49370f75ed50d975aa6da8c03faf..4a956cc07f62049de4fe2ea10ba8c3fba2875ecb 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -63,7 +63,6 @@ const streamText = (options) => {
let finishReason = "other";
let reasoningStarted = false;
let textStarted = false;
- pushEvent({ type: "step.start" });
await stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(new JsonMessageTransformStream()).pipeTo(new WritableStream({
abort: (reason) => {
errorControllers(reason, eventCtrl, fullCtrl, textCtrl, reasoningTextCtrl);
@@ -82,18 +81,16 @@ const streamText = (options) => {
reasoningField = "reasoning";
if (!reasoningStarted) {
reasoningStarted = true;
- pushEvent({ type: "reasoning.start" });
}
- pushEvent({ delta: choice.delta.reasoning, type: "reasoning.delta" });
+ pushEvent({ text: choice.delta.reasoning, type: "reasoning-delta" });
pushReasoningText(choice.delta.reasoning);
} else if (choice.delta.reasoning_content != null) {
if (reasoningField !== "reasoning_content")
reasoningField = "reasoning_content";
if (!reasoningStarted) {
reasoningStarted = true;
- pushEvent({ type: "reasoning.start" });
}
- pushEvent({ delta: choice.delta.reasoning_content, type: "reasoning.delta" });
+ pushEvent({ text: choice.delta.reasoning_content, type: "reasoning-delta" });
pushReasoningText(choice.delta.reasoning_content);
}
if (choice.finish_reason != null)
@@ -102,16 +99,14 @@ const streamText = (options) => {
if (choice.delta.content != null) {
if (!textStarted) {
textStarted = true;
- pushEvent({ type: "text.start" });
}
- pushEvent({ delta: choice.delta.content, type: "text.delta" });
+ pushEvent({ text: choice.delta.content, type: "text-delta" });
pushText(choice.delta.content);
} else if (choice.delta.refusal != null) {
if (!textStarted) {
textStarted = true;
- pushEvent({ type: "text.start" });
}
- pushEvent({ delta: choice.delta.refusal, type: "text.delta" });
+ pushEvent({ text: choice.delta.refusal, type: "text-delta" });
pushText(choice.delta.refusal);
}
} else {
@@ -125,21 +120,17 @@ const streamText = (options) => {
arguments: toolCall.function.arguments ?? ""
}
};
- pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call.start" });
+ pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
if (toolCall.function.arguments != null && toolCall.function.arguments.length > 0)
- pushEvent({ delta: toolCall.function.arguments, type: "tool-call.delta" });
+ pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-delta" });
} else {
tool_calls[index].function.arguments += toolCall.function.arguments;
- pushEvent({ delta: toolCall.function.arguments, type: "tool-call.delta" });
+ pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.function.name ?? tool_calls[index].function.name, type: "tool-call-delta" });
}
}
}
}
}));
- if (reasoningStarted)
- pushEvent({ content: reasoningText ?? "", type: "reasoning.done" });
- if (textStarted)
- pushEvent({ content: text, type: "text.done" });
messages.push({
...reasoningField != null ? { [reasoningField]: reasoningText } : {},
content: text,
@@ -152,7 +143,7 @@ const streamText = (options) => {
if (options.abortSignal?.aborted === true)
throw options.abortSignal.reason ?? new Error("This operation was aborted");
for (const toolCall of toolCalls)
- pushEvent({ ...toolCall, type: "tool-call.done" });
+ pushEvent({ ...toolCall, type: "tool-call" });
const step = {
finishReason,
text,
@@ -169,9 +160,13 @@ const streamText = (options) => {
const results = await Promise.all(
validToolCalls.map(async (toolCall) => executeTool({
abortSignal: options.abortSignal,
+ captureToolErrors: options.captureToolErrors,
messages,
+ onToolCallFinish: options.onToolCallFinish,
+ onToolCallStart: options.onToolCallStart,
postToolCall: options.postToolCall,
preToolCall: options.preToolCall,
+ repairToolCall: options.repairToolCall,
toolCall,
tools: options.tools
}))
@@ -185,12 +180,12 @@ const streamText = (options) => {
role: "tool",
tool_call_id: completionToolCall.toolCallId
});
- pushEvent({ ...completionToolResult, type: "tool-result.done" });
+ pushEvent({ ...completionToolResult, type: completionToolResult.isError ? "tool-error" : "tool-result" });
}
}
const willContinue = validToolCalls.length > 0 && !stop && !options.abortSignal?.aborted;
pushStep(step);
- pushEvent({ type: "step.done", usage });
+ pushEvent({ finishReason, type: "finish", usage });
if (willContinue)
return async () => doStream();
};
@@ -222,7 +217,7 @@ const streamText = (options) => {
})();
return {
eventStream,
- fullStream,
+ fullStream: eventStream,
messages: resultMessages.promise,
reasoningTextStream,
steps: resultSteps.promise,

594
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -31,9 +31,9 @@ overrides:
side-channel: npm:@nolyfill/side-channel@^1.0.44
string.prototype.matchall: npm:@nolyfill/string.prototype.matchall@^1.0.44
patchedDependencies:
'@xsai/generate-text@0.5.0-beta.2': patches/@xsai__generate-text@0.5.0-beta.2.patch
'@xsai/shared-chat@0.5.0-beta.2': patches/@xsai__shared-chat@0.5.0-beta.2.patch
'@xsai/stream-text@0.5.0-beta.2': patches/@xsai__stream-text@0.5.0-beta.2.patch
'@xsai/generate-text@0.5.0-beta.8': patches/@xsai__generate-text@0.5.0-beta.8.patch
'@xsai/shared-chat@0.5.0-beta.8': patches/@xsai__shared-chat@0.5.0-beta.8.patch
'@xsai/stream-text@0.5.0-beta.8': patches/@xsai__stream-text@0.5.0-beta.8.patch
mineflayer-pathfinder: patches/mineflayer-pathfinder.patch
pixi-live2d-display: patches/pixi-live2d-display.patch
sponsorkit@17.1.0: patches/sponsorkit@17.1.0.patch
@ -220,21 +220,21 @@ catalog:
'@vueuse/shared': ^14.2.1
'@webgpu/types': ^0.1.69
'@wxt-dev/module-vue': ^1.0.3
'@xsai-ext/providers': 0.5.0-beta.2
'@xsai-ext/providers': 0.5.0-beta.8
'@xsai-transformers/embed': ^0.1.0
'@xsai-transformers/shared': ^0.1.0
'@xsai-transformers/transcription': ^0.1.0
'@xsai/embed': 0.5.0-beta.2
'@xsai/generate-speech': 0.5.0-beta.2
'@xsai/generate-text': 0.5.0-beta.2
'@xsai/generate-transcription': 0.5.0-beta.2
'@xsai/model': 0.5.0-beta.2
'@xsai/shared': 0.5.0-beta.2
'@xsai/shared-chat': 0.5.0-beta.2
'@xsai/stream-text': 0.5.0-beta.2
'@xsai/stream-transcription': 0.5.0-beta.2
'@xsai/tool': 0.5.0-beta.2
'@xsai/utils-chat': 0.5.0-beta.2
'@xsai/embed': 0.5.0-beta.8
'@xsai/generate-speech': 0.5.0-beta.8
'@xsai/generate-text': 0.5.0-beta.8
'@xsai/generate-transcription': 0.5.0-beta.8
'@xsai/model': 0.5.0-beta.8
'@xsai/shared': 0.5.0-beta.8
'@xsai/shared-chat': 0.5.0-beta.8
'@xsai/stream-text': 0.5.0-beta.8
'@xsai/stream-transcription': 0.5.0-beta.8
'@xsai/tool': 0.5.0-beta.8
'@xsai/utils-chat': 0.5.0-beta.8
alien-signals: ^3.1.2
animejs: ^4.3.6
async-mutex: 0.5.0