From 7a5050d2a2ae54dd2c274fc62647010508a63278 Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 29 Aug 2025 22:25:07 +0800 Subject: [PATCH] =?UTF-8?q?feat(aiCore):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E5=A2=9E=E9=87=8F=E7=B4=AF=E7=A7=AF=E7=9A=84?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据模型是否支持文本增量来决定是否累积文本内容,新增accumulate参数控制行为 --- src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts | 11 +++++++++-- src/renderer/src/aiCore/index_new.ts | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts index 0430c750d2..d441f228c1 100644 --- a/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts +++ b/src/renderer/src/aiCore/chunk/AiSdkToChunkAdapter.ts @@ -28,11 +28,14 @@ export interface CherryStudioChunk { */ export class AiSdkToChunkAdapter { toolCallHandler: ToolCallChunkHandler + private accumulate: boolean | undefined constructor( private onChunk: (chunk: Chunk) => void, - mcpTools: MCPTool[] = [] + mcpTools: MCPTool[] = [], + accumulate?: boolean ) { this.toolCallHandler = new ToolCallChunkHandler(onChunk, mcpTools) + this.accumulate = accumulate } /** @@ -95,7 +98,11 @@ export class AiSdkToChunkAdapter { }) break case 'text-delta': - final.text += chunk.text || '' + if (this.accumulate) { + final.text += chunk.text || '' + } else { + final.text = chunk.text || '' + } this.onChunk({ type: ChunkType.TEXT_DELTA, text: final.text || '' diff --git a/src/renderer/src/aiCore/index_new.ts b/src/renderer/src/aiCore/index_new.ts index 553010c5ae..9d94fdb544 100644 --- a/src/renderer/src/aiCore/index_new.ts +++ b/src/renderer/src/aiCore/index_new.ts @@ -43,10 +43,12 @@ export default class ModernAiProvider { private legacyProvider: LegacyAiProvider private config: ReturnType private actualProvider: Provider + private model: Model constructor(model: Model, provider?: Provider) { this.actualProvider = provider || getActualProvider(model) this.legacyProvider = new LegacyAiProvider(this.actualProvider) + this.model = model // 只保存配置,不预先创建executor this.config = providerToAiSdkConfig(this.actualProvider, model) @@ -237,7 +239,8 @@ export default class ModernAiProvider { topicId: config.topicId }) - const adapter = new AiSdkToChunkAdapter(config.onChunk, config.mcpTools) + const accumulate = this.model.supported_text_delta !== false // true and undefined + const adapter = new AiSdkToChunkAdapter(config.onChunk, config.mcpTools, accumulate) logger.debug('Final params before streamText', { modelId,