ask.js
1 import { cli, Strategy } from '@jackwener/opencli/registry'; 2 import { GEMINI_DOMAIN, readGeminiSnapshot, sendGeminiMessage, startNewGeminiChat, waitForGeminiResponse, waitForGeminiSubmission } from './utils.js'; 3 function normalizeBooleanFlag(value) { 4 if (typeof value === 'boolean') 5 return value; 6 const normalized = String(value ?? '').trim().toLowerCase(); 7 return normalized === 'true' || normalized === '1' || normalized === 'yes' || normalized === 'on'; 8 } 9 const NO_RESPONSE_PREFIX = '[NO RESPONSE]'; 10 export const askCommand = cli({ 11 site: 'gemini', 12 name: 'ask', 13 description: 'Send a prompt to Gemini and return only the assistant response', 14 domain: GEMINI_DOMAIN, 15 strategy: Strategy.COOKIE, 16 browser: true, 17 navigateBefore: false, 18 defaultFormat: 'plain', 19 timeoutSeconds: 180, 20 args: [ 21 { name: 'prompt', required: true, positional: true, help: 'Prompt to send' }, 22 { name: 'timeout', required: false, help: 'Max seconds to wait (default: 60)', default: '60' }, 23 { name: 'new', required: false, help: 'Start a new chat first (true/false, default: false)', default: 'false' }, 24 ], 25 columns: ['response'], 26 func: async (page, kwargs) => { 27 const prompt = kwargs.prompt; 28 const timeout = parseInt(kwargs.timeout, 10) || 60; 29 const startFresh = normalizeBooleanFlag(kwargs.new); 30 if (startFresh) 31 await startNewGeminiChat(page); 32 const before = await readGeminiSnapshot(page); 33 await sendGeminiMessage(page, prompt); 34 const submissionStartedAt = Date.now(); 35 const submitted = await waitForGeminiSubmission(page, before, timeout); 36 if (!submitted) { 37 return [{ response: `💬 ${NO_RESPONSE_PREFIX} No Gemini response within ${timeout}s.` }]; 38 } 39 const remainingTimeoutSeconds = Math.max(0, timeout - Math.ceil((Date.now() - submissionStartedAt) / 1000)); 40 const response = await waitForGeminiResponse(page, submitted, prompt, remainingTimeoutSeconds); 41 if (!response) { 42 return [{ response: `💬 ${NO_RESPONSE_PREFIX} No Gemini response within ${timeout}s.` }]; 43 } 44 return [{ response: `💬 ${response}` }]; 45 }, 46 });