🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🌐 chore: translate non-English comments to English in model-runtime/core (#12244)
LobeHub Bot9cdfff1aa808
diff --git a/packages/model-runtime/src/core/contextBuilders/anthropic.ts b/packages/model-runtime/src/core/contextBuilders/anthropic.tsindex 3e1a4a4aa3c..67674243a35 100644--- a/packages/model-runtime/src/core/contextBuilders/anthropic.ts+++ b/packages/model-runtime/src/core/contextBuilders/anthropic.ts@@ -166,7 +166,7 @@ export const buildAnthropicMessages = async (const messages: Anthropic.Messages.MessageParam[] = [];let pendingToolResults: Anthropic.ToolResultBlockParam[] = [];- // 首先收集所有 assistant 消息中的 tool_call_id 以便后续查找+ // First collect all tool_call_id from assistant messages for subsequent lookupconst validToolCallIds = new Set<string>();for (const message of oaiMessages) {if (message.role === 'assistant' && message.tool_calls?.length) {@@ -190,7 +190,7 @@ export const buildAnthropicMessages = async (? [{ text: '<empty_content>', type: 'text' as const }]: [{ text: message.content, type: 'text' as const }];- // 检查这个工具消息是否有对应的 assistant 工具调用+ // Check if this tool message has a corresponding assistant tool callif (message.tool_call_id && validToolCallIds.has(message.tool_call_id)) {pendingToolResults.push({content: toolResultContent as Anthropic.ToolResultBlockParam['content'],@@ -198,7 +198,7 @@ export const buildAnthropicMessages = async (type: 'tool_result',});- // 如果这是最后一个消息或者下一个消息不是 'tool',则添加累积的工具结果作为一个 'user' 消息+ // If this is the last message or the next message is not 'tool', add accumulated tool results as a 'user' messageif (index === oaiMessages.length - 1 || oaiMessages[index + 1].role !== 'tool') {messages.push({content: pendingToolResults,@@ -207,7 +207,7 @@ export const buildAnthropicMessages = async (pendingToolResults = [];}} else {- // 如果工具消息没有对应的 assistant 工具调用,则作为普通文本处理+ // If tool message has no corresponding assistant tool call, treat as plain textconst fallbackContent = Array.isArray(message.content)? JSON.stringify(message.content): message.content || '<empty_content>';diff --git a/packages/model-runtime/src/core/contextBuilders/sensenova.ts b/packages/model-runtime/src/core/contextBuilders/sensenova.tsindex 1346fecac2f..09887732783 100644--- a/packages/model-runtime/src/core/contextBuilders/sensenova.ts+++ b/packages/model-runtime/src/core/contextBuilders/sensenova.ts@@ -1,31 +1,31 @@export const convertSenseNovaMessage = (content: any) => {- // 如果为单条 string 类 content,则格式转换为 text 类+ // If it's a single string content, convert format to text typeif (typeof content === 'string') {return [{ text: content, type: 'text' }];}- // 如果内容为空或不是数组,返回空数组避免后续错误+ // If content is empty or not an array, return empty array to avoid subsequent errorsif (!Array.isArray(content)) {return [];}- // 如果内容包含图片内容,则需要对 array 类 content,进行格式转换+ // If content contains images, need to convert array content formatreturn content.map((item: any) => {- // 如果项为空,跳过处理+ // If item is empty, skip processingif (!item) return null;- // 如果为 content,则格式转换为 text 类+ // If it's content, convert format to text typeif (item.type === 'text') return item;- // 如果为 image_url,则格式转换为 image_url 类+ // If it's image_url, convert format to image_url typeif (item.type === 'image_url' && item.image_url) {const url = item.image_url.url;- // 确保 URL 存在且为字符串+ // Ensure URL exists and is a stringif (!url || typeof url !== 'string') return null;- // 如果 image_url 为 base64 格式,则返回 image_base64 类,否则返回 image_url 类+ // If image_url is in base64 format, return image_base64 type, otherwise return image_url typereturn url.startsWith('data:image/jpeg;base64') || url.startsWith('data:image/png;base64')? {image_base64: url.split(',')[1],diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.tsindex 1d5cd470f1a..d23c90bf052 100644--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts@@ -334,7 +334,7 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = anprocessedPayload = { ...payload, apiMode: 'responses' } as any;}- // 再进行工厂级处理+ // Then perform factory-level processingconst postPayload = chatCompletion?.handlePayload? chatCompletion.handlePayload(processedPayload, this._options): ({diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/nonStreamToStream.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/nonStreamToStream.tsindex 033b9d13026..ed794c32518 100644--- a/packages/model-runtime/src/core/openaiCompatibleFactory/nonStreamToStream.ts+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/nonStreamToStream.ts@@ -8,7 +8,7 @@ export const transformResponseToStream = (data: OpenAI.ChatCompletion) =>start(controller) {const choices = data.choices || [];const first = choices[0];- // 兼容:非流式里 DeepSeek 等会把“深度思考”放在 message.reasoning_content+ // Compatibility: Non-streaming responses from DeepSeek etc. put "deep thinking" in message.reasoning_contentconst message: any = first?.message ?? {};const reasoningText =typeof message.reasoning_content === 'string' && message.reasoning_content.length > 0diff --git a/packages/model-runtime/src/core/streams/openai/openai.ts b/packages/model-runtime/src/core/streams/openai/openai.tsindex 541e9d5137e..c6d8592b7ff 100644--- a/packages/model-runtime/src/core/streams/openai/openai.ts+++ b/packages/model-runtime/src/core/streams/openai/openai.ts@@ -256,12 +256,12 @@ const transformOpenAIStream = (.filter(Boolean) as StreamProtocolChunk[];}- // 给定结束原因+ // Handle finish reasonif (item.finish_reason) {- // one-api 的流式接口,会出现既有 finish_reason ,也有 content 的情况+ // one-api's streaming interface can have both finish_reason and content// {"id":"demo","model":"deepl-en","choices":[{"index":0,"delta":{"role":"assistant","content":"Introduce yourself."},"finish_reason":"stop"}]}if (typeof item.delta?.content === 'string' && !!item.delta.content) {- // MiniMax 内建搜索功能会在第一个 tools 流中 content 返回引用源,需要忽略+ // MiniMax built-in search returns citation sources in the first tool stream content, needs to be ignored// {"id":"0483748a25071c611e2f48d2982fbe96","choices":[{"finish_reason":"stop","index":0,"delta":{"content":"[{\"no\":1,\"url\":\"https://www.xiaohongshu.com/discovery/item/66d8de3c000000001f01e752\",\"title\":\"郑钦文为国而战,没有理由不坚持🏅\",\"content\":\"·2024年08月03日\\n中国队选手郑钦文夺得巴黎奥运会网球女单比赛金牌(巴黎奥运第16金)\\n#巴黎奥运会[话题]# #郑钦文[话题]# #人物素材积累[话题]# #作文素材积累[话题]# #申论素材[话题]#\",\"web_icon\":\"https://www.xiaohongshu.com/favicon.ico\"}]","role":"tool","tool_call_id":"call_function_6696730535"}}],"created":1748255114,"model":"abab6.5s-chat","object":"chat.completion.chunk","usage":{"total_tokens":0,"total_characters":0},"input_sensitive":false,"output_sensitive":false,"input_sensitive_type":0,"output_sensitive_type":0,"output_sensitive_int":0}if (typeof item.delta?.role === 'string' && item.delta.role === 'tool') {return { data: null, id: chunk.id, type: 'text' };@@ -285,7 +285,7 @@ const transformOpenAIStream = (return { data: text, id: chunk.id, type: 'text' };}- // OpenAI Search Preview 模型返回引用源+ // OpenAI Search Preview model returns citation sources// {"id":"chatcmpl-18037d13-243c-4941-8b05-9530b352cf17","object":"chat.completion.chunk","created":1748351805,"model":"gpt-4o-mini-search-preview-2025-03-11","choices":[{"index":0,"delta":{"annotations":[{"type":"url_citation","url_citation":{"url":"https://zh.wikipedia.org/wiki/%E4%B8%8A%E6%B5%B7%E4%B9%90%E9%AB%98%E4%B9%90%E5%9B%AD?utm_source=openai","title":"上海乐高乐园","start_index":75,"end_index":199}}]},"finish_reason":"stop"}],"service_tier":"default"}if ((item as any).delta?.annotations && (item as any).delta.annotations.length > 0) {const citations = (item as any).delta.annotations;@@ -307,7 +307,7 @@ const transformOpenAIStream = (];}- // MiniMax 内建搜索功能会在最后一个流中的 message 数组中返回 4 个 Object,其中最后一个为 annotations+ // MiniMax built-in search returns 4 objects in the message array of the last stream, with the last one being annotations// {"id":"0483bf14ba55225a66de2342a21b4003","choices":[{"finish_reason":"tool_calls","index":0,"messages":[{"content":"","role":"user","reasoning_content":""},{"content":"","role":"assistant","tool_calls":[{"id":"call_function_0872338692","type":"web_search","function":{"name":"get_search_result","arguments":"{\"query_tag\":[\"天气\"],\"query_list\":[\"上海 2025年5月26日 天气\"]}"}}],"reasoning_content":""},{"content":"","role":"tool","tool_call_id":"call_function_0872338692","reasoning_content":""},{"content":"","role":"assistant","name":"海螺AI","annotations":[{"text":"【5†source】","url":"https://mtianqi.eastday.com/tianqi/shanghai/20250526.html","quote":"上海天气预报提供上海2025年05月26日天气"}],"audio_content":"","reasoning_content":""}]}],"created":1748274196,"model":"MiniMax-Text-01","object":"chat.completion","usage":{"total_tokens":13110,"total_characters":0,"prompt_tokens":12938,"completion_tokens":172},"base_resp":{"status_code":0,"status_msg":"Invalid parameters detected, json: unknown field \"user\""}}if ((item as any).messages && (item as any).messages.length > 0) {const citations = (item as any).messages.at(-1).annotations;@@ -334,7 +334,7 @@ const transformOpenAIStream = (return { data: convertOpenAIUsage(usage, payload), id: chunk.id, type: 'usage' };}- // xAI Live Search 功能返回引用源+ // xAI Live Search feature returns citation sources// {"id":"8721eebb-6465-4c47-ba2e-8e2ec0f97055","object":"chat.completion.chunk","created":1747809109,"model":"grok-3","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":"stop"}],"system_fingerprint":"fp_1affcf9872","citations":["https://world.huanqiu.com/"]}if ((chunk as any).citations) {const citations = (chunk as any).citations;@@ -416,21 +416,21 @@ const transformOpenAIStream = (}if (typeof content === 'string') {- // 如果 content 是空字符串但 chunk 带有 usage,则优先返回 usage(例如 Gemini image-preview 最终会在单独的 chunk 中返回 usage)+ // If content is an empty string but chunk has usage, prioritize returning usage (e.g., Gemini image-preview eventually returns usage in a separate chunk)if (content === '' && chunk.usage) {const usage = chunk.usage;return { data: convertOpenAIUsage(usage, payload), id: chunk.id, type: 'usage' };}- // 处理包含 </think> 标签的特殊情况:需要分割内容+ // Handle special case with </think> tag: need to split contentif (content.includes('</think>')) {const parts = content.split('</think>');- const beforeThink = parts[0].replaceAll('<think>', ''); // 移除可能的 <think> 标签- const afterThink = parts.slice(1).join('</think>'); // 处理可能有多个 </think> 的情况+ const beforeThink = parts[0].replaceAll('<think>', ''); // Remove possible <think> tag+ const afterThink = parts.slice(1).join('</think>'); // Handle case with multiple </think> tagsconst results: StreamProtocolChunk[] = [];- // </think> 之前的内容(如果有)作为 reasoning+ // Content before </think> (if any) as reasoningif (beforeThink) {results.push({data: beforeThink,@@ -439,10 +439,10 @@ const transformOpenAIStream = (});}- // 更新状态:已经结束思考模式+ // Update state: thinking mode has endedstreamContext.thinkingInContent = false;- // </think> 之后的内容(如果有)作为 text+ // Content after </think> (if any) as textif (afterThink) {results.push({data: afterThink,@@ -454,15 +454,15 @@ const transformOpenAIStream = (return results.length > 0 ? results : { data: '', id: chunk.id, type: 'text' };}- // 清除 <think> 标签(不需要分割,因为 <think> 标签后续内容都是 reasoning)+ // Remove <think> tag (no need to split, as content after <think> tag is all reasoning)const thinkingContent = content.replaceAll(/<\/?think>/g, '');- // 判断是否有 <think> 标签,更新 thinkingInContent 状态+ // Check if there's a <think> tag, update thinkingInContent stateif (content.includes('<think>')) {streamContext.thinkingInContent = true;}- // 判断是否有 citations 内容,更新 returnedCitation 状态+ // Check if there's citation content, update returnedCitation stateif (!streamContext?.returnedCitation) {const citations =// in Perplexity api, the citation is in every chunk, but we only need to return it once@@ -485,7 +485,7 @@ const transformOpenAIStream = (title: typeof item === 'string' ? item : item.title,url: typeof item === 'string' ? item : item.url || item.link,}))- .filter((c) => c.title && c.url), // Zhipu 内建搜索工具有时会返回空 link 引发程序崩溃+ .filter((c) => c.title && c.url), // Zhipu built-in search tool sometimes returns empty link causing crashes},id: chunk.id,type: 'grounding',@@ -500,7 +500,7 @@ const transformOpenAIStream = (}}- // 非思考模式下,额外解析 markdown 中的 base64 图片,按顺序输出 text -> base64_image+ // In non-thinking mode, additionally parse base64 images in markdown, output in order: text -> base64_imageif (!streamContext?.thinkingInContent) {const { urls, cleanedText: cleaned } = processMarkdownBase64Images(thinkingContent);if (urls.length > 0) {@@ -517,7 +517,7 @@ const transformOpenAIStream = (}}- // 根据当前思考模式确定返回类型+ // Determine return type based on current thinking modereturn {data: thinkingContent,id: chunk.id,@@ -526,18 +526,18 @@ const transformOpenAIStream = (}}- // 无内容情况+ // No content caseif (item.delta && item.delta.content === null) {return { data: item.delta, id: chunk.id, type: 'data' };}- // litellm 的返回结果中,存在 delta 为空,但是有 usage 的情况+ // In litellm responses, there are cases where delta is empty but usage existsif (chunk.usage) {const usage = chunk.usage;return { data: convertOpenAIUsage(usage, payload), id: chunk.id, type: 'usage' };}- // 其余情况下,返回 delta 和 index+ // In other cases, return delta and indexreturn {data: { delta: item.delta, id: chunk.id, index: item.index },id: chunk.id,@@ -571,7 +571,7 @@ export interface OpenAIStreamOptions {name: string;}) => ILobeAgentRuntimeErrorType | undefined;callbacks?: ChatStreamCallbacks;- enableStreaming?: boolean; // 选择 TPS 计算方式(非流式时传 false)+ enableStreaming?: boolean; // Choose TPS calculation method (pass false for non-streaming)inputStartAt?: number;payload?: ChatPayloadForTransformStream;}diff --git a/packages/model-runtime/src/core/usageConverters/openai.ts b/packages/model-runtime/src/core/usageConverters/openai.tsindex 19590575fca..d68cd9e00a5 100644--- a/packages/model-runtime/src/core/usageConverters/openai.ts+++ b/packages/model-runtime/src/core/usageConverters/openai.ts@@ -12,7 +12,7 @@ export const convertOpenAIUsage = (usage: OpenAI.Completions.CompletionUsage,payload?: ChatPayloadForTransformStream,): ModelUsage => {- // 目前只有 pplx 才有 citation_tokens+ // Currently only pplx has citation_tokensconst inputTextTokens = usage.prompt_tokens || 0;const inputCitationTokens = (usage as any).citation_tokens || 0;const totalInputTokens = inputCitationTokens + inputTextTokens;@@ -28,7 +28,7 @@ export const convertOpenAIUsage = (const outputAudioTokens = usage.completion_tokens_details?.audio_tokens || 0;const outputImageTokens = (usage.completion_tokens_details as any)?.image_tokens || 0;- // XAI 的 completion_tokens 不包含 reasoning_tokens,需要特殊处理+ // XAI's completion_tokens does not include reasoning_tokens, requires special handlingconst outputTextTokens =payload?.provider === 'xai'? totalOutputTokens - outputAudioTokens