mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-28 23:15:33 +08:00
* feat: add opencode support
* fix: stabilize opencode session startup
* fix: /models
* fix: improveUI for commands
* fix: format commands.js
* feat: load models through provider adapters
Provider model selection had outgrown a single hardcoded service.
The old service mixed shared caching with provider catalogs and CLI lookup details.
That made stale model lists more likely as providers changed on separate schedules.
Move model discovery behind each provider so lookup lives next to the integration.
The shared service now focuses on provider resolution, caching, persistence, and dedupe.
Return cache metadata and add bypassCache because model availability changes outside the app.
The UI and /models command can show freshness and let users force a provider refresh.
Surface model descriptions while keeping fallback catalogs for unavailable CLIs or SDKs.
* feat(models): resolve active session models through provider adapters
The model inventory command was showing a mix of catalog defaults and
composer-local state instead of the model that is actually active for a
real provider session. That made /models, /cost, and /status
misleading once a session had already started, especially for providers
whose effective runtime model can differ from the optimistic model value
held in the UI.
Introduce an explicit getCurrentActiveModel() contract on
IProviderModels so model resolution lives next to each provider's
catalog logic and uses the provider-native source of truth:
- Claude reads the init event from a resumed stream-json run
- Codex reads model from ~/.codex/config.toml
- Cursor reads lastUsedModel from the chat store.db
- OpenCode reads the persisted session model from opencode.db
- Gemini intentionally returns its default because the CLI does not
provide a reliable active-session lookup
Keep the returned shape intentionally minimal ({ model }). The goal is
to expose only what downstream command consumers need and avoid leaking
provider-specific metadata into a shared transport shape that would
create extra UI coupling and future cleanup cost.
Also make command behavior session-aware: when there is no concrete
session id, do not spawn provider processes or inspect provider session
storage just to answer /models, /cost, or /status. In a new-session
view the correct answer is simply the provider default, and doing more
work there adds latency and unnecessary side effects for no user value.
As part of this, centralize two supporting concerns:
- add a shared helper for building the default current-model result from
a provider catalog so fallbacks stay aligned with DEFAULT
- move leaf-directory validation into shared utils so Cursor session
readers and model lookup code enforce the same path-safety rule
Tests were expanded to cover both the new service delegation path and
the sessionless command behavior, while keeping cache-sensitive tests
isolated from persisted host cache state.
Why this change:
- command output should reflect the model actually driving a session
- new-session views should stay fast and side-effect free
- provider-specific active-model lookup should not be scattered across
routes or UI code
- fallback behavior should be explicit, consistent, and limited to the
provider default when no true active model can be resolved
* feat: support session-scoped model overrides
Model selection was acting like a provider-level preference.
That made resumed sessions drift back to a default or request-time model.
Users expect /models changes made inside a conversation to affect that session.
Store explicit session choices in app-owned ~/.cloudcli state.
This avoids editing provider transcripts or native provider config.
Resolve the effective model before launching each provider runtime.
Claude, Cursor, Codex, Gemini, and OpenCode now honor stored resume choices.
Expose a backend active-model change endpoint for existing sessions.
The models modal can now distinguish default changes from session overrides.
It also shows when a selected model will apply on the next response.
For Claude, stop probing active model state by resuming with a dummy prompt.
Read the indexed JSONL transcript from the end instead.
This preserves provider history while honoring /model stdout or model fields.
Add service tests for adapter delegation and resume-model precedence.
The tests keep cache state, override state, and requested fallback separate.
* feat: make command modal more compact
* fix: preserve opencode session creation events
OpenCode emits the real session id asynchronously on its first JSON output. The runner
registered that id from a helper that could not see the spawned process because
the process reference was scoped inside the model-resolution callback. That
ReferenceError was swallowed by the generic JSON parse fallback, so the client
never received session_created. Without that event, a new OpenCode chat stayed
on / and the assistant stream was not attached to the new session view.
Keep the process reference in the outer spawn scope so registration can update
the active-process map and websocket writer as soon as OpenCode announces the
session id. Split JSON parsing from event processing so malformed non-JSON
output can still stream as raw text, while registration or adapter failures are
surfaced as real errors instead of being hidden as assistant content.
Add a fake opencode executable regression test to lock in the expected lifecycle
ordering: session_created must be sent before live assistant messages, and the
same session id must carry through stream_end and complete.
* fix: clarify model refresh and onboarding providers
OpenCode is now a supported chat provider, but first-run onboarding still only offered
Claude, Cursor, Codex, and Gemini. That made OpenCode harder to discover and
forced users to finish setup before finding the provider in settings or chat.
Adding it to onboarding keeps first-run setup aligned with the providers the
application already supports elsewhere.
The model refresh control was also doing too much visual work. In the new chat
model picker, the previous Hard Refresh label looked like the dialog heading,
which made the primary task unclear. Users open that dialog to choose a model;
refreshing catalogs is only a secondary maintenance action for stale cached
provider model lists.
Rename and reposition the refresh affordance so the model picker reads as a
model picker first. The copy now explains why catalogs are cached, when a refresh
is useful, and that the refresh checks every provider. The /models modal gets the
same clarification so both model-selection surfaces describe the cache behavior
consistently.
* fix: format opencode model catalog labels
OpenCode returns provider-prefixed ids directly from the CLI. Passing those ids through as
labels made the model picker hard to scan: users saw values like
anthropic/claude-3-5-sonnet-20241022 or lowercased, hyphen-split text instead
of readable model names.
Keep the exact OpenCode id as the option value because that is what the CLI
expects, but derive a presentation label for the frontend. The formatter is
intentionally generic rather than a catalog of known providers. It handles common
identifier structure such as provider/model, hyphen-delimited words, v-prefixed
versions, adjacent numeric version tokens, and 8-digit date suffixes.
This keeps OpenCode usable as its model list expands across many upstream
providers without requiring code changes for every new provider or model family.
The description keeps the raw provider-prefixed id visible so users can still
confirm the precise model being selected.
* feat: add more fallback models for cursor
* docs: move model catalog out of shared
The model catalog is no longer a frontend/backend runtime contract.
Keeping it under shared made ownership misleading. It implied the catalog was
application code shared by runtime consumers, even though it now only supports
README links and public API documentation.
Move the catalog into public so it lives beside the docs surfaces that need it.
This gives the API docs a stable, served module and gives README readers a
linkable source without suggesting frontend or backend runtime dependency.
Render the API docs model list from the exported provider registry instead of a
hardcoded Claude/Cursor/Codex subset. That keeps Gemini and OpenCode visible and
makes future provider documentation changes flow through one docs-specific file.
Update README links, provider maintenance notes, and package files so published
artifacts include the standalone docs page and model catalog without relying on
the old shared path.
* fix: simplify empty-state model selector
Keep the provider empty state focused on the setup action users need there:
choosing a model.
The refresh control, cache timestamp, and refresh explanation made the dialog feel
like a cache-management surface.
That extra action is out of place in the empty state, where the goal is to start
a chat with the selected provider and model.
Remove the refresh-specific UI from ProviderSelectionEmptyState and drop the
now-unused refresh/cache props from the ChatMessagesPane pass-through.
Refresh behavior remains available in the dedicated command result flow.
624 lines
25 KiB
JavaScript
624 lines
25 KiB
JavaScript
import { spawn } from 'child_process';
|
|
import { promises as fs } from 'fs';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
|
|
import crossSpawn from 'cross-spawn';
|
|
|
|
import sessionManager from './sessionManager.js';
|
|
import GeminiResponseHandler from './gemini-response-handler.js';
|
|
import { notifyRunFailed, notifyRunStopped } from './services/notification-orchestrator.js';
|
|
import { providerAuthService } from './modules/providers/services/provider-auth.service.js';
|
|
import { providerModelsService } from './modules/providers/services/provider-models.service.js';
|
|
import { createNormalizedMessage } from './shared/utils.js';
|
|
|
|
// Use cross-spawn on Windows for correct .cmd resolution (same pattern as cursor-cli.js)
|
|
const spawnFunction = process.platform === 'win32' ? crossSpawn : spawn;
|
|
|
|
let activeGeminiProcesses = new Map(); // Track active processes by session ID
|
|
|
|
function mapGeminiExitCodeToMessage(exitCode) {
|
|
switch (exitCode) {
|
|
case 42:
|
|
return 'Gemini rejected the request input (exit code 42).';
|
|
case 44:
|
|
return 'Gemini sandbox error (exit code 44). Check local sandbox/container settings.';
|
|
case 52:
|
|
return 'Gemini configuration error (exit code 52). Check your Gemini settings files for invalid JSON/config.';
|
|
case 53:
|
|
return 'Gemini conversation turn limit reached (exit code 53). Start a new Gemini session.';
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const GEMINI_AUTH_ENV_KEYS = [
|
|
'GEMINI_API_KEY',
|
|
'GOOGLE_API_KEY',
|
|
'GOOGLE_CLOUD_PROJECT',
|
|
'GOOGLE_CLOUD_PROJECT_ID',
|
|
'GOOGLE_CLOUD_LOCATION',
|
|
'GOOGLE_APPLICATION_CREDENTIALS'
|
|
];
|
|
|
|
function parseEnvFileContent(content) {
|
|
const parsed = {};
|
|
|
|
for (const rawLine of content.split(/\r?\n/)) {
|
|
const line = rawLine.trim();
|
|
if (!line || line.startsWith('#')) {
|
|
continue;
|
|
}
|
|
|
|
const exportPrefix = 'export ';
|
|
const normalizedLine = line.startsWith(exportPrefix) ? line.slice(exportPrefix.length).trim() : line;
|
|
const separatorIndex = normalizedLine.indexOf('=');
|
|
|
|
if (separatorIndex <= 0) {
|
|
continue;
|
|
}
|
|
|
|
const key = normalizedLine.slice(0, separatorIndex).trim();
|
|
if (!key) {
|
|
continue;
|
|
}
|
|
|
|
let value = normalizedLine.slice(separatorIndex + 1).trim();
|
|
const hasDoubleQuotes = value.startsWith('"') && value.endsWith('"');
|
|
const hasSingleQuotes = value.startsWith('\'') && value.endsWith('\'');
|
|
|
|
if (hasDoubleQuotes || hasSingleQuotes) {
|
|
value = value.slice(1, -1);
|
|
} else {
|
|
// Support inline comments in unquoted values: KEY=value # comment
|
|
value = value.replace(/\s+#.*$/, '').trim();
|
|
}
|
|
|
|
parsed[key] = value;
|
|
}
|
|
|
|
return parsed;
|
|
}
|
|
|
|
async function loadGeminiUserLevelEnv() {
|
|
const geminiCliHome = (process.env.GEMINI_CLI_HOME || '').trim() || os.homedir();
|
|
const envCandidates = [
|
|
path.join(geminiCliHome, '.gemini', '.env'),
|
|
path.join(geminiCliHome, '.env')
|
|
];
|
|
|
|
for (const envPath of envCandidates) {
|
|
try {
|
|
await fs.access(envPath);
|
|
const content = await fs.readFile(envPath, 'utf8');
|
|
return parseEnvFileContent(content);
|
|
} catch {
|
|
// Keep scanning for the next candidate.
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
async function buildGeminiProcessEnv() {
|
|
const processEnv = { ...process.env };
|
|
if (processEnv.GEMINI_API_KEY || processEnv.GOOGLE_API_KEY || processEnv.GOOGLE_APPLICATION_CREDENTIALS) {
|
|
return processEnv;
|
|
}
|
|
|
|
// Gemini CLI docs recommend ~/.gemini/.env for persistent headless auth settings.
|
|
// When the server process was launched without shell profile variables, we still
|
|
// want the spawned CLI process to inherit those user-level credentials.
|
|
const userEnv = await loadGeminiUserLevelEnv();
|
|
for (const key of GEMINI_AUTH_ENV_KEYS) {
|
|
if (!processEnv[key] && userEnv[key]) {
|
|
processEnv[key] = userEnv[key];
|
|
}
|
|
}
|
|
|
|
return processEnv;
|
|
}
|
|
|
|
async function spawnGemini(command, options = {}, ws) {
|
|
const { sessionId, projectPath, cwd, toolsSettings, permissionMode, images, sessionSummary } = options;
|
|
const resolvedModel = await providerModelsService.resolveResumeModel(
|
|
'gemini',
|
|
sessionId,
|
|
options.model
|
|
);
|
|
let capturedSessionId = sessionId; // Track session ID throughout the process
|
|
let sessionCreatedSent = false; // Track if we've already sent session-created event
|
|
let assistantBlocks = []; // Accumulate the full response blocks including tools
|
|
|
|
// Use tools settings passed from frontend, or defaults
|
|
const settings = toolsSettings || {
|
|
allowedTools: [],
|
|
disallowedTools: [],
|
|
skipPermissions: false
|
|
};
|
|
|
|
// Build Gemini CLI command - start with print/resume flags first
|
|
const args = [];
|
|
|
|
// Add prompt flag with command if we have a command
|
|
if (command && command.trim()) {
|
|
args.push('--prompt', command);
|
|
}
|
|
|
|
// If we have a sessionId, we want to resume
|
|
if (sessionId) {
|
|
const session = sessionManager.getSession(sessionId);
|
|
if (session && session.cliSessionId) {
|
|
args.push('--resume', session.cliSessionId);
|
|
}
|
|
}
|
|
|
|
// Use cwd (actual project directory) instead of projectPath (Gemini's metadata directory)
|
|
// Clean the path by removing any non-printable characters
|
|
const cleanPath = (cwd || projectPath || process.cwd()).replace(/[^\x20-\x7E]/g, '').trim();
|
|
const workingDir = cleanPath;
|
|
|
|
// Handle images by saving them to temporary files and passing paths to Gemini
|
|
const tempImagePaths = [];
|
|
let tempDir = null;
|
|
if (images && images.length > 0) {
|
|
try {
|
|
// Create temp directory in the project directory so Gemini can access it
|
|
tempDir = path.join(workingDir, '.tmp', 'images', Date.now().toString());
|
|
await fs.mkdir(tempDir, { recursive: true });
|
|
|
|
// Save each image to a temp file
|
|
for (const [index, image] of images.entries()) {
|
|
// Extract base64 data and mime type
|
|
const matches = image.data.match(/^data:([^;]+);base64,(.+)$/);
|
|
if (!matches) {
|
|
continue;
|
|
}
|
|
|
|
const [, mimeType, base64Data] = matches;
|
|
const extension = mimeType.split('/')[1] || 'png';
|
|
const filename = `image_${index}.${extension}`;
|
|
const filepath = path.join(tempDir, filename);
|
|
|
|
// Write base64 data to file
|
|
await fs.writeFile(filepath, Buffer.from(base64Data, 'base64'));
|
|
tempImagePaths.push(filepath);
|
|
}
|
|
|
|
// Include the full image paths in the prompt for Gemini to reference
|
|
// Gemini CLI can read images from file paths in the prompt
|
|
if (tempImagePaths.length > 0 && command && command.trim()) {
|
|
const imageNote = `\n\n[Images given: ${tempImagePaths.length} images are located at the following paths:]\n${tempImagePaths.map((p, i) => `${i + 1}. ${p}`).join('\n')}`;
|
|
const modifiedCommand = command + imageNote;
|
|
|
|
// Update the command in args
|
|
const promptIndex = args.indexOf('--prompt');
|
|
if (promptIndex !== -1 && args[promptIndex + 1] === command) {
|
|
args[promptIndex + 1] = modifiedCommand;
|
|
} else if (promptIndex !== -1) {
|
|
// If we're using context, update the full prompt
|
|
args[promptIndex + 1] = args[promptIndex + 1] + imageNote;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error processing images for Gemini:', error);
|
|
}
|
|
}
|
|
|
|
// Add basic flags for Gemini
|
|
if (options.debug) {
|
|
args.push('--debug');
|
|
}
|
|
|
|
// This integration runs Gemini in headless mode and cannot answer trust prompts.
|
|
// Skip folder-trust interactivity so authenticated runs don't fail with
|
|
// FatalUntrustedWorkspaceError in previously unseen directories.
|
|
args.push('--skip-trust');
|
|
|
|
// Add MCP config flag only if MCP servers are configured
|
|
try {
|
|
const geminiConfigPath = path.join(os.homedir(), '.gemini.json');
|
|
let hasMcpServers = false;
|
|
|
|
try {
|
|
await fs.access(geminiConfigPath);
|
|
const geminiConfigRaw = await fs.readFile(geminiConfigPath, 'utf8');
|
|
const geminiConfig = JSON.parse(geminiConfigRaw);
|
|
|
|
// Check global MCP servers
|
|
if (geminiConfig.mcpServers && Object.keys(geminiConfig.mcpServers).length > 0) {
|
|
hasMcpServers = true;
|
|
}
|
|
|
|
// Check project-specific MCP servers
|
|
if (!hasMcpServers && geminiConfig.geminiProjects) {
|
|
const currentProjectPath = process.cwd();
|
|
const projectConfig = geminiConfig.geminiProjects[currentProjectPath];
|
|
if (projectConfig && projectConfig.mcpServers && Object.keys(projectConfig.mcpServers).length > 0) {
|
|
hasMcpServers = true;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// Ignore if file doesn't exist or isn't parsable
|
|
}
|
|
|
|
if (hasMcpServers) {
|
|
args.push('--mcp-config', geminiConfigPath);
|
|
}
|
|
} catch (error) {
|
|
// Ignore outer errors
|
|
}
|
|
|
|
// Add model for all sessions (both new and resumed)
|
|
let modelToUse = resolvedModel || 'gemini-2.5-flash';
|
|
args.push('--model', modelToUse);
|
|
args.push('--output-format', 'stream-json');
|
|
|
|
// Handle approval modes and allowed tools
|
|
if (settings.skipPermissions || options.skipPermissions || permissionMode === 'yolo') {
|
|
args.push('--yolo');
|
|
} else if (permissionMode === 'auto_edit') {
|
|
args.push('--approval-mode', 'auto_edit');
|
|
} else if (permissionMode === 'plan') {
|
|
args.push('--approval-mode', 'plan');
|
|
}
|
|
|
|
if (settings.allowedTools && settings.allowedTools.length > 0) {
|
|
args.push('--allowed-tools', settings.allowedTools.join(','));
|
|
}
|
|
|
|
// Try to find gemini in PATH first, then fall back to environment variable
|
|
const geminiPath = process.env.GEMINI_PATH || 'gemini';
|
|
let spawnCmd = geminiPath;
|
|
let spawnArgs = args;
|
|
|
|
// On non-Windows platforms, wrap the execution in a shell to avoid ENOEXEC
|
|
// which happens when the target is a script lacking a shebang.
|
|
if (os.platform() !== 'win32') {
|
|
spawnCmd = 'sh';
|
|
// Use exec to replace the shell process, ensuring signals hit gemini directly
|
|
spawnArgs = ['-c', 'exec "$0" "$@"', geminiPath, ...args];
|
|
}
|
|
|
|
const spawnEnv = await buildGeminiProcessEnv();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const geminiProcess = spawnFunction(spawnCmd, spawnArgs, {
|
|
cwd: workingDir,
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
env: spawnEnv
|
|
});
|
|
let terminalNotificationSent = false;
|
|
let terminalFailureReason = null;
|
|
|
|
const notifyTerminalState = ({ code = null, error = null } = {}) => {
|
|
if (terminalNotificationSent) {
|
|
return;
|
|
}
|
|
|
|
terminalNotificationSent = true;
|
|
|
|
const finalSessionId = capturedSessionId || sessionId || processKey;
|
|
if (code === 0 && !error) {
|
|
notifyRunStopped({
|
|
userId: ws?.userId || null,
|
|
provider: 'gemini',
|
|
sessionId: finalSessionId,
|
|
sessionName: sessionSummary,
|
|
stopReason: 'completed'
|
|
});
|
|
return;
|
|
}
|
|
|
|
notifyRunFailed({
|
|
userId: ws?.userId || null,
|
|
provider: 'gemini',
|
|
sessionId: finalSessionId,
|
|
sessionName: sessionSummary,
|
|
error: error || terminalFailureReason || `Gemini CLI exited with code ${code}`
|
|
});
|
|
};
|
|
|
|
// Attach temp file info to process for cleanup later
|
|
geminiProcess.tempImagePaths = tempImagePaths;
|
|
geminiProcess.tempDir = tempDir;
|
|
|
|
// Store process reference for potential abort
|
|
const processKey = capturedSessionId || sessionId || Date.now().toString();
|
|
activeGeminiProcesses.set(processKey, geminiProcess);
|
|
|
|
// Store sessionId on the process object for debugging
|
|
geminiProcess.sessionId = processKey;
|
|
|
|
// Close stdin to signal we're done sending input
|
|
geminiProcess.stdin.end();
|
|
|
|
// Add timeout handler
|
|
const timeoutMs = 120000; // 120 seconds for slower models
|
|
let timeout;
|
|
|
|
const startTimeout = () => {
|
|
if (timeout) clearTimeout(timeout);
|
|
timeout = setTimeout(() => {
|
|
const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId || processKey);
|
|
terminalFailureReason = `Gemini CLI timeout - no response received for ${timeoutMs / 1000} seconds`;
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'gemini' }));
|
|
try {
|
|
geminiProcess.kill('SIGTERM');
|
|
} catch (e) { }
|
|
}, timeoutMs);
|
|
};
|
|
|
|
startTimeout();
|
|
|
|
// Save user message to session when starting
|
|
if (command && capturedSessionId) {
|
|
sessionManager.addMessage(capturedSessionId, 'user', command);
|
|
}
|
|
|
|
// Create response handler for NDJSON buffering
|
|
let responseHandler;
|
|
if (ws) {
|
|
responseHandler = new GeminiResponseHandler(ws, {
|
|
onContentFragment: (content) => {
|
|
if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
|
|
assistantBlocks[assistantBlocks.length - 1].text += content;
|
|
} else {
|
|
assistantBlocks.push({ type: 'text', text: content });
|
|
}
|
|
},
|
|
onToolUse: (event) => {
|
|
assistantBlocks.push({
|
|
type: 'tool_use',
|
|
id: event.tool_id,
|
|
name: event.tool_name,
|
|
input: event.parameters
|
|
});
|
|
},
|
|
onToolResult: (event) => {
|
|
if (capturedSessionId) {
|
|
if (assistantBlocks.length > 0) {
|
|
sessionManager.addMessage(capturedSessionId, 'assistant', [...assistantBlocks]);
|
|
assistantBlocks = [];
|
|
}
|
|
sessionManager.addMessage(capturedSessionId, 'user', [{
|
|
type: 'tool_result',
|
|
tool_use_id: event.tool_id,
|
|
content: event.output === undefined ? null : event.output,
|
|
is_error: event.status === 'error'
|
|
}]);
|
|
}
|
|
},
|
|
onInit: (event) => {
|
|
const discoveredSessionId = event?.session_id;
|
|
if (!discoveredSessionId) {
|
|
return;
|
|
}
|
|
|
|
// New Gemini sessions announce their canonical ID asynchronously via the
|
|
// initial `init` stream event. Avoid synthetic IDs and only register
|
|
// the session once that real ID is known (same model used by Claude/Codex).
|
|
if (!capturedSessionId) {
|
|
capturedSessionId = discoveredSessionId;
|
|
|
|
sessionManager.createSession(capturedSessionId, cwd || process.cwd());
|
|
if (command) {
|
|
sessionManager.addMessage(capturedSessionId, 'user', command);
|
|
}
|
|
|
|
if (processKey !== capturedSessionId) {
|
|
activeGeminiProcesses.delete(processKey);
|
|
activeGeminiProcesses.set(capturedSessionId, geminiProcess);
|
|
}
|
|
|
|
geminiProcess.sessionId = capturedSessionId;
|
|
|
|
if (ws.setSessionId && typeof ws.setSessionId === 'function') {
|
|
ws.setSessionId(capturedSessionId);
|
|
}
|
|
|
|
if (!sessionId && !sessionCreatedSent) {
|
|
sessionCreatedSent = true;
|
|
ws.send(createNormalizedMessage({ kind: 'session_created', newSessionId: capturedSessionId, sessionId: capturedSessionId, provider: 'gemini' }));
|
|
}
|
|
}
|
|
|
|
const sess = sessionManager.getSession(capturedSessionId);
|
|
if (sess && !sess.cliSessionId) {
|
|
sess.cliSessionId = discoveredSessionId;
|
|
sessionManager.saveSession(capturedSessionId);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// Handle stdout
|
|
geminiProcess.stdout.on('data', (data) => {
|
|
const rawOutput = data.toString();
|
|
startTimeout(); // Re-arm the timeout
|
|
|
|
if (responseHandler) {
|
|
responseHandler.processData(rawOutput);
|
|
} else if (rawOutput) {
|
|
// Fallback to direct sending for raw CLI mode without WS
|
|
if (assistantBlocks.length > 0 && assistantBlocks[assistantBlocks.length - 1].type === 'text') {
|
|
assistantBlocks[assistantBlocks.length - 1].text += rawOutput;
|
|
} else {
|
|
assistantBlocks.push({ type: 'text', text: rawOutput });
|
|
}
|
|
const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
|
|
ws.send(createNormalizedMessage({ kind: 'stream_delta', content: rawOutput, sessionId: socketSessionId, provider: 'gemini' }));
|
|
}
|
|
});
|
|
|
|
// Handle stderr
|
|
geminiProcess.stderr.on('data', (data) => {
|
|
const errorMsg = data.toString();
|
|
|
|
// Filter out deprecation warnings and "Loaded cached credentials" message
|
|
if (errorMsg.includes('[DEP0040]') ||
|
|
errorMsg.includes('DeprecationWarning') ||
|
|
errorMsg.includes('--trace-deprecation') ||
|
|
errorMsg.includes('Loaded cached credentials')) {
|
|
return;
|
|
}
|
|
|
|
const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : (capturedSessionId || sessionId);
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: errorMsg, sessionId: socketSessionId, provider: 'gemini' }));
|
|
});
|
|
|
|
// Handle process completion
|
|
geminiProcess.on('close', async (code) => {
|
|
clearTimeout(timeout);
|
|
|
|
// Flush any remaining buffered content
|
|
if (responseHandler) {
|
|
responseHandler.forceFlush();
|
|
responseHandler.destroy();
|
|
}
|
|
|
|
// Clean up process reference
|
|
const finalSessionId = capturedSessionId || sessionId || processKey;
|
|
activeGeminiProcesses.delete(finalSessionId);
|
|
|
|
// Save assistant response to session if we have one
|
|
if (finalSessionId && assistantBlocks.length > 0) {
|
|
sessionManager.addMessage(finalSessionId, 'assistant', assistantBlocks);
|
|
}
|
|
|
|
ws.send(createNormalizedMessage({ kind: 'complete', exitCode: code, isNewSession: !sessionId && !!command, sessionId: finalSessionId, provider: 'gemini' }));
|
|
|
|
// Clean up temporary image files if any
|
|
if (geminiProcess.tempImagePaths && geminiProcess.tempImagePaths.length > 0) {
|
|
for (const imagePath of geminiProcess.tempImagePaths) {
|
|
await fs.unlink(imagePath).catch(err => { });
|
|
}
|
|
if (geminiProcess.tempDir) {
|
|
await fs.rm(geminiProcess.tempDir, { recursive: true, force: true }).catch(err => { });
|
|
}
|
|
}
|
|
|
|
if (code === 0) {
|
|
notifyTerminalState({ code });
|
|
resolve();
|
|
} else {
|
|
const socketSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
|
|
|
|
// code 127 = shell "command not found" - check installation
|
|
if (code === 127) {
|
|
const installed = await providerAuthService.isProviderInstalled('gemini');
|
|
if (!installed) {
|
|
terminalFailureReason = 'Gemini CLI is not installed. Please install it first: https://github.com/google-gemini/gemini-cli';
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'gemini' }));
|
|
}
|
|
} else if (code === 41) {
|
|
// Gemini CLI documents exit code 41 as FatalAuthenticationError.
|
|
// Surface an actionable auth error instead of a generic exit-code message.
|
|
let authErrorSuffix = '';
|
|
try {
|
|
const authStatus = await providerAuthService.getProviderAuthStatus('gemini');
|
|
if (!authStatus?.authenticated && authStatus?.error) {
|
|
authErrorSuffix = ` Details: ${authStatus.error}`;
|
|
}
|
|
} catch {
|
|
// Keep base remediation text when auth status lookup fails.
|
|
}
|
|
|
|
terminalFailureReason =
|
|
'Gemini authentication failed (exit code 41). '
|
|
+ 'Run `gemini` in a terminal to choose an auth method, or configure a valid `GEMINI_API_KEY`.'
|
|
+ authErrorSuffix;
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'gemini' }));
|
|
} else {
|
|
const mappedError = mapGeminiExitCodeToMessage(code);
|
|
if (mappedError) {
|
|
terminalFailureReason = mappedError;
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: terminalFailureReason, sessionId: socketSessionId, provider: 'gemini' }));
|
|
}
|
|
}
|
|
|
|
notifyTerminalState({
|
|
code,
|
|
error: code === null ? 'Gemini CLI process was terminated or timed out' : null
|
|
});
|
|
reject(
|
|
new Error(
|
|
terminalFailureReason
|
|
|| (code === null
|
|
? 'Gemini CLI process was terminated or timed out'
|
|
: `Gemini CLI exited with code ${code}`)
|
|
)
|
|
);
|
|
}
|
|
});
|
|
|
|
// Handle process errors
|
|
geminiProcess.on('error', async (error) => {
|
|
// Clean up process reference on error
|
|
const finalSessionId = capturedSessionId || sessionId || processKey;
|
|
activeGeminiProcesses.delete(finalSessionId);
|
|
|
|
// Check if Gemini CLI is installed for a clearer error message
|
|
const installed = await providerAuthService.isProviderInstalled('gemini');
|
|
const errorContent = !installed
|
|
? 'Gemini CLI is not installed. Please install it first: https://github.com/google-gemini/gemini-cli'
|
|
: error.message;
|
|
|
|
const errorSessionId = typeof ws.getSessionId === 'function' ? ws.getSessionId() : finalSessionId;
|
|
ws.send(createNormalizedMessage({ kind: 'error', content: errorContent, sessionId: errorSessionId, provider: 'gemini' }));
|
|
notifyTerminalState({ error });
|
|
|
|
reject(error);
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
function abortGeminiSession(sessionId) {
|
|
let geminiProc = activeGeminiProcesses.get(sessionId);
|
|
let processKey = sessionId;
|
|
|
|
if (!geminiProc) {
|
|
for (const [key, proc] of activeGeminiProcesses.entries()) {
|
|
if (proc.sessionId === sessionId) {
|
|
geminiProc = proc;
|
|
processKey = key;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (geminiProc) {
|
|
try {
|
|
geminiProc.kill('SIGTERM');
|
|
setTimeout(() => {
|
|
if (activeGeminiProcesses.has(processKey)) {
|
|
try {
|
|
geminiProc.kill('SIGKILL');
|
|
} catch (e) { }
|
|
}
|
|
}, 2000); // Wait 2 seconds before force kill
|
|
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isGeminiSessionActive(sessionId) {
|
|
return activeGeminiProcesses.has(sessionId);
|
|
}
|
|
|
|
function getActiveGeminiSessions() {
|
|
return Array.from(activeGeminiProcesses.keys());
|
|
}
|
|
|
|
export {
|
|
spawnGemini,
|
|
abortGeminiSession,
|
|
isGeminiSessionActive,
|
|
getActiveGeminiSessions
|
|
};
|