fix(websearch): support Gemini positional prompt mode

This commit is contained in:
Tam Nhu Tran committed 2026-02-26 22:12:16 +07:00
1 parent bf198e4af0
commit 0d09604bb9
2 files changed
+86 -60

No files matched your search

+83 -57
View File
@@ -282,6 +282,61 @@ async function processHook() {
/**
* Execute search via Gemini CLI
*/
function shouldRetryGeminiWithLegacyPrompt(errorMessage) {
const lowerError = (errorMessage || '').toLowerCase();
return (
lowerError.includes('unknown option') ||
lowerError.includes('unknown argument') ||
lowerError.includes('unrecognized option') ||
lowerError.includes('usage: gemini') ||
lowerError.includes('use --prompt') ||
lowerError.includes('using the --prompt option')
);
}
function runGeminiCommand(args, timeoutMs) {
const spawnResult = spawnSync('gemini', args, {
encoding: 'utf8',
timeout: timeoutMs,
maxBuffer: 1024 * 1024 * 2,
stdio: ['pipe', 'pipe', 'pipe'],
shell: isWindows,
});
if (spawnResult.error) {
if (spawnResult.error.code === 'ENOENT') {
return { success: false, error: 'Gemini CLI not installed' };
}
throw spawnResult.error;
}
if (spawnResult.status !== 0) {
const stderr = (spawnResult.stderr || '').trim();
return {
success: false,
error: stderr || `Gemini CLI exited with code ${spawnResult.status}`,
};
}
const result = (spawnResult.stdout || '').trim();
if (!result || result.length < MIN_VALID_RESPONSE_LENGTH) {
return { success: false, error: 'Empty or too short response from Gemini' };
}
const lowerResult = result.toLowerCase();
if (
lowerResult.includes('error:') ||
lowerResult.includes('failed to') ||
lowerResult.includes('authentication required')
) {
return { success: false, error: `Gemini returned error: ${result.substring(0, 100)}` };
}
return { success: true, content: result };
}
function tryGeminiSearch(query, timeoutSec = DEFAULT_TIMEOUT_SEC) {
try {
const timeoutMs = timeoutSec * 1000;
@@ -290,54 +345,31 @@ function tryGeminiSearch(query, timeoutSec = DEFAULT_TIMEOUT_SEC) {
// Allow model override via env var
const model = process.env.CCS_WEBSEARCH_GEMINI_MODEL || config.model;
const baseArgs = ['--model', model, '--yolo'];
const positionalArgs = [...baseArgs, prompt];
if (process.env.CCS_DEBUG) {
console.error(`[CCS Hook] Executing: gemini --model ${model} --yolo "..."`);
}
// Current Gemini CLI prefers positional prompts and deprecates -p/--prompt.
// Retry once with -p for legacy CLIs that still require it.
const positionalResult = runGeminiCommand(positionalArgs, timeoutMs);
if (positionalResult.success) {
return positionalResult;
}
if (!shouldRetryGeminiWithLegacyPrompt(positionalResult.error)) {
return positionalResult;
}
if (process.env.CCS_DEBUG) {
console.error('[CCS Hook] Positional Gemini prompt failed; retrying with -p for legacy CLI');
console.error(`[CCS Hook] Executing: gemini --model ${model} --yolo -p "..."`);
}
const spawnResult = spawnSync(
'gemini',
['--model', model, '--yolo', '-p', prompt],
{
encoding: 'utf8',
timeout: timeoutMs,
maxBuffer: 1024 * 1024 * 2,
stdio: ['pipe', 'pipe', 'pipe'],
shell: isWindows,
}
);
if (spawnResult.error) {
if (spawnResult.error.code === 'ENOENT') {
return { success: false, error: 'Gemini CLI not installed' };
}
throw spawnResult.error;
}
if (spawnResult.status !== 0) {
const stderr = (spawnResult.stderr || '').trim();
return {
success: false,
error: stderr || `Gemini CLI exited with code ${spawnResult.status}`,
};
}
const result = (spawnResult.stdout || '').trim();
if (!result || result.length < MIN_VALID_RESPONSE_LENGTH) {
return { success: false, error: 'Empty or too short response from Gemini' };
}
const lowerResult = result.toLowerCase();
if (
lowerResult.includes('error:') ||
lowerResult.includes('failed to') ||
lowerResult.includes('authentication required')
) {
return { success: false, error: `Gemini returned error: ${result.substring(0, 100)}` };
}
return { success: true, content: result };
const legacyPromptArgs = [...baseArgs, '-p', prompt];
return runGeminiCommand(legacyPromptArgs, timeoutMs);
} catch (err) {
if (err.killed) {
return { success: false, error: 'Gemini CLI timed out' };
@@ -362,17 +394,13 @@ function tryOpenCodeSearch(query, timeoutSec = DEFAULT_TIMEOUT_SEC) {
console.error(`[CCS Hook] Executing: opencode run --model ${model} "..."`);
}
const spawnResult = spawnSync(
'opencode',
['run', prompt, '--model', model],
{
encoding: 'utf8',
timeout: timeoutMs,
maxBuffer: 1024 * 1024 * 2,
stdio: ['pipe', 'pipe', 'pipe'],
shell: isWindows,
}
);
const spawnResult = spawnSync('opencode', ['run', prompt, '--model', model], {
encoding: 'utf8',
timeout: timeoutMs,
maxBuffer: 1024 * 1024 * 2,
stdio: ['pipe', 'pipe', 'pipe'],
shell: isWindows,
});
if (spawnResult.error) {
if (spawnResult.error.code === 'ENOENT') {
@@ -599,9 +627,7 @@ function outputNoToolsMessage(query) {
* Output all providers failed message
*/
function outputAllFailedMessage(query, errors) {
const errorDetails = errors
.map((e) => ` - ${e.provider}: ${e.error}`)
.join('\n');
const errorDetails = errors.map((e) => ` - ${e.provider}: ${e.error}`).join('\n');
const message = [
'[WebSearch - All Providers Failed]',