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`, {