Skip to content

Commit

Permalink
chore: additional handling for TypeValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
adolphnov committed Dec 28, 2024
1 parent b4d825a commit 4fcb40e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion scripts/plugins/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions src/agent/request.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -176,7 +176,11 @@ export async function streamHandler(stream: AsyncIterable<any>, 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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4fcb40e

Please sign in to comment.