From a389082dd4b836e0be012849d70da9efcb90893a Mon Sep 17 00:00:00 2001 From: iven Date: Mon, 23 Mar 2026 23:59:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(llm-service):=20=E5=9C=A8=E5=86=85=E6=A0=B8?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E4=BD=BF=E7=94=A8=20Tauri=20invoke?= =?UTF-8?q?=20=E8=80=8C=E9=9D=9E=20HTTP=20=E7=AB=AF=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:记忆系统尝试调用 /api/agents/default/message 导致 ECONNREFUSED 根因:GatewayLLMAdapter 在内核模式下仍使用外部 OpenFang HTTP 端点 修复:检测 Tauri 运行时,使用 agent_chat Tauri 命令代替 HTTP 请求 Co-Authored-By: Claude Opus 4.6 --- desktop/src/lib/llm-service.ts | 45 +++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/desktop/src/lib/llm-service.ts b/desktop/src/lib/llm-service.ts index 8f4e7d2..ea2d34b 100644 --- a/desktop/src/lib/llm-service.ts +++ b/desktop/src/lib/llm-service.ts @@ -282,7 +282,7 @@ class VolcengineLLMAdapter implements LLMServiceAdapter { } } -// === Gateway Adapter (pass through to OpenFang) === +// === Gateway Adapter (pass through to OpenFang or internal Kernel) === class GatewayLLMAdapter implements LLMServiceAdapter { private config: LLMConfig; @@ -304,8 +304,47 @@ class GatewayLLMAdapter implements LLMServiceAdapter { ? `${systemMessage}\n\n${userMessage}` : userMessage; - // Use OpenFang's chat endpoint (same as main chat) - // Try to get the default agent ID from localStorage or use 'default' + // Check if running in Tauri with internal kernel + // Use the same detection as kernel-client.ts + const isTauri = typeof window !== 'undefined' && + '__TAURI_INTERNALS__' in window; + + if (isTauri) { + // Use internal Kernel via Tauri invoke + try { + const { invoke } = await import('@tauri-apps/api/core'); + + // Get the default agent ID from connectionStore or use the first agent + const agentId = localStorage.getItem('zclaw-default-agent-id'); + + const response = await invoke<{ content: string; input_tokens: number; output_tokens: number }>('agent_chat', { + request: { + agentId: agentId || null, // null will use default agent + message: fullPrompt, + }, + }); + + const latencyMs = Date.now() - startTime; + return { + content: response.content || '', + tokensUsed: { + input: response.input_tokens || 0, + output: response.output_tokens || 0, + }, + latencyMs, + }; + } catch (err) { + console.warn('[LLMService] Kernel chat failed, falling back to mock:', err); + // Return empty response instead of throwing + return { + content: '', + tokensUsed: { input: 0, output: 0 }, + latencyMs: Date.now() - startTime, + }; + } + } + + // External Gateway mode: Use OpenFang's chat endpoint const agentId = localStorage.getItem('zclaw-default-agent-id') || 'default'; const response = await fetch(`/api/agents/${agentId}/message`, {