From 4fcb40e5d241107c77f0359121e323f5c8f292b7 Mon Sep 17 00:00:00 2001 From: adolphnov Date: Sat, 28 Dec 2024 23:18:02 +0800 Subject: [PATCH] chore: additional handling for TypeValidationError --- package.json | 8 ++++---- scripts/plugins/docker/index.ts | 2 +- src/agent/request.ts | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 782560fc..ad5ecfb6 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@ai-sdk/mistral": "^1.0.6", "@ai-sdk/openai": "^1.0.11", "@ai-sdk/provider": "^1.0.3", - "@ai-sdk/xai": "^1.0.12", + "@ai-sdk/xai": "^1.0.14", "@ffmpeg.wasm/core-st": "^0.13.2", "@ffmpeg.wasm/main": "^0.13.1", "ai": "^4.0.22", @@ -63,8 +63,8 @@ "@ai-sdk/google-vertex": "^2.0.12", "@ai-sdk/mistral": "^1.0.6", "@ai-sdk/openai": "^1.0.11", - "@antfu/eslint-config": "^3.12.0", - "@cloudflare/workers-types": "^4.20241218.0", + "@antfu/eslint-config": "^3.12.1", + "@cloudflare/workers-types": "^4.20241224.0", "@rollup/plugin-node-resolve": "^16.0.0", "@types/node": "^22.10.2", "@types/node-cron": "^3.0.11", @@ -81,7 +81,7 @@ "toml": "^3.0.0", "tsx": "^4.19.2", "typescript": "^5.7.2", - "vite": "^6.0.5", + "vite": "^6.0.6", "vite-plugin-checker": "^0.8.0", "vite-plugin-dts": "^4.4.0", "vitest": "^2.1.8", diff --git a/scripts/plugins/docker/index.ts b/scripts/plugins/docker/index.ts index b44ea0bf..e69757dc 100644 --- a/scripts/plugins/docker/index.ts +++ b/scripts/plugins/docker/index.ts @@ -33,7 +33,7 @@ const packageJson = ` "@ai-sdk/mistral": "^1.0.6", "@ai-sdk/openai": "^1.0.11", "@ai-sdk/provider": "^1.0.3", - "@ai-sdk/xai": "^1.0.12", + "@ai-sdk/xai": "^1.0.14", "@ffmpeg.wasm/core-st": "^0.13.2", "@ffmpeg.wasm/main": "^0.13.1", "ai": "^4.0.22", diff --git a/src/agent/request.ts b/src/agent/request.ts index 5e334722..1a16ab56 100644 --- a/src/agent/request.ts +++ b/src/agent/request.ts @@ -1,7 +1,7 @@ import type { CoreMessage, LanguageModelV1, StepResult, ToolCallPart, ToolResultPart } from 'ai'; import type { AgentUserConfig } from '../config/env'; import type { ChatStreamTextHandler, OpenAIFuncCallData, ResponseMessage } from './types'; -import { generateText, streamText, experimental_wrapLanguageModel as wrapLanguageModel } from 'ai'; +import { generateText, streamText, TypeValidationError, experimental_wrapLanguageModel as wrapLanguageModel } from 'ai'; import { createLlmModel, type ToolChoice } from '.'; import { ENV } from '../config/env'; import { log } from '../log/logger'; @@ -176,7 +176,11 @@ export async function streamHandler(stream: AsyncIterable, contentExtractor throw e; } console.error((e as Error).message, (e as Error).stack); - contentFull += `\n\n\`\`\`Error\n${(e as Error).message}\n\`\`\``; + let content: string | undefined; + if (e instanceof TypeValidationError) { + content = (e.value as any)?.choices?.[0]?.delta?.content; + } + contentFull += (content ?? `\n\n\`\`\`Error\n${(e as Error).message}\n\`\`\``); errorReferencer[0] = true; } @@ -229,7 +233,8 @@ export async function requestChatCompletionsV2(params: { model: LanguageModelV1; } try { // when last message is tool, avoid ai message not sent complete - if (contentFull.trim() !== '' && (messages.at(-1)?.content as (ToolCallPart | ToolResultPart)[])?.some(c => c.type === 'tool-call') && onStream) { + const lastMessageContent = messages.at(-1)?.content; + if (contentFull.trim() !== '' && Array.isArray(lastMessageContent) && (lastMessageContent as (ToolCallPart | ToolResultPart)[]).some(c => c.type === 'tool-call') && onStream) { await onStream.end?.(contentFull); } await manualRequestTool(messages, params.context);