From ef616e1c3b64e6a08d8e77f87be2b287dd31c892 Mon Sep 17 00:00:00 2001 From: MyPrototypeWhat Date: Fri, 27 Jun 2025 19:24:23 +0800 Subject: [PATCH] fix: update reasoningTimePlugin and smoothReasoningPlugin for improved performance tracking - Changed the invocation of `reasoningTimePlugin` to a direct reference in `ModernAiProvider`. - Initialized `thinkingStartTime` with `performance.now()` in `reasoningTimePlugin` for accurate timing. - Removed `thinking_millsec` from the enqueued chunks in `smoothReasoningPlugin` to streamline data handling. - Added console logging for performance tracking in `reasoningTimePlugin` to aid in debugging. --- src/renderer/src/aiCore/index_new.ts | 2 +- src/renderer/src/aiCore/plugins/reasoningTimePlugin.ts | 9 +++++---- src/renderer/src/aiCore/plugins/smoothReasoningPlugin.ts | 4 ++-- src/renderer/src/services/ApiService.ts | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/aiCore/index_new.ts b/src/renderer/src/aiCore/index_new.ts index 8fc6a80de5..01a81d2cd4 100644 --- a/src/renderer/src/aiCore/index_new.ts +++ b/src/renderer/src/aiCore/index_new.ts @@ -133,7 +133,7 @@ export default class ModernAiProvider { delayInMs: 80, chunkingRegex: /([\u4E00-\u9FFF]{3})|\S+\s+/ }), - reasoningTimePlugin() + reasoningTimePlugin ) } diff --git a/src/renderer/src/aiCore/plugins/reasoningTimePlugin.ts b/src/renderer/src/aiCore/plugins/reasoningTimePlugin.ts index 516be1d3ce..60fd61c512 100644 --- a/src/renderer/src/aiCore/plugins/reasoningTimePlugin.ts +++ b/src/renderer/src/aiCore/plugins/reasoningTimePlugin.ts @@ -1,9 +1,9 @@ import { definePlugin } from '@cherrystudio/ai-core' -export default definePlugin(() => ({ +export default definePlugin({ name: 'reasoningTimePlugin', transformStream: () => () => { - let thinkingStartTime = 0 + let thinkingStartTime = performance.now() let hasStartedThinking = false let accumulatedThinkingContent = '' return new TransformStream({ @@ -11,9 +11,10 @@ export default definePlugin(() => ({ if (chunk.type === 'reasoning') { if (!hasStartedThinking) { hasStartedThinking = true - thinkingStartTime = performance.now() + // thinkingStartTime = performance.now() } accumulatedThinkingContent += chunk.textDelta + console.log('performance.now() - thinkingStartTime', performance.now() - thinkingStartTime) controller.enqueue({ ...chunk, thinking_millsec: performance.now() - thinkingStartTime @@ -34,4 +35,4 @@ export default definePlugin(() => ({ } }) } -})) +}) diff --git a/src/renderer/src/aiCore/plugins/smoothReasoningPlugin.ts b/src/renderer/src/aiCore/plugins/smoothReasoningPlugin.ts index 8876d7fbb2..be86a455f1 100644 --- a/src/renderer/src/aiCore/plugins/smoothReasoningPlugin.ts +++ b/src/renderer/src/aiCore/plugins/smoothReasoningPlugin.ts @@ -19,7 +19,7 @@ export default definePlugin(({ delayInMs, chunkingRegex }: { delayInMs: number; async transform(chunk, controller) { if (chunk.type !== 'reasoning') { if (buffer.length > 0) { - controller.enqueue({ type: 'reasoning', textDelta: buffer, thinking_millsec: chunk.thinking_millsec }) + controller.enqueue({ type: 'reasoning', textDelta: buffer }) buffer = '' } @@ -31,7 +31,7 @@ export default definePlugin(({ delayInMs, chunkingRegex }: { delayInMs: number; let match while ((match = detectChunk(buffer)) != null) { - controller.enqueue({ type: 'reasoning', textDelta: match, thinking_millsec: chunk.thinking_millsec }) + controller.enqueue({ type: 'reasoning', textDelta: match }) buffer = buffer.slice(match.length) await delay(delayInMs) diff --git a/src/renderer/src/services/ApiService.ts b/src/renderer/src/services/ApiService.ts index da73ac4c23..00a7bc6d96 100644 --- a/src/renderer/src/services/ApiService.ts +++ b/src/renderer/src/services/ApiService.ts @@ -306,12 +306,13 @@ export async function fetchChatCompletion({ enableTools: isEnabledToolUse(assistant), requestOptions: options }) - + console.log('assistant.settings?.reasoning_effort', assistant.settings?.reasoning_effort) const middlewareConfig: AiSdkMiddlewareConfig = { streamOutput: assistant.settings?.streamOutput ?? true, onChunk: onChunkReceived, model: assistant.model, provider: provider, + // FIXME: 这里需要根据模型来判断是否启用推理 enableReasoning: assistant.settings?.reasoning_effort !== undefined, enableTool: assistant.settings?.toolUseMode === 'prompt', mcpTools