|
| 1 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 2 | +import { CommandExecutionError } from '@jackwener/opencli/errors'; |
| 3 | +import { |
| 4 | + DEEPSEEK_DOMAIN, DEEPSEEK_URL, ensureOnDeepSeek, selectModel, setFeature, |
| 5 | + sendMessage, getBubbleCount, waitForResponse, parseBoolFlag, withRetry, |
| 6 | +} from './utils.js'; |
| 7 | + |
| 8 | +export const askCommand = cli({ |
| 9 | + site: 'deepseek', |
| 10 | + name: 'ask', |
| 11 | + description: 'Send a prompt to DeepSeek and get the response', |
| 12 | + domain: DEEPSEEK_DOMAIN, |
| 13 | + strategy: Strategy.COOKIE, |
| 14 | + browser: true, |
| 15 | + navigateBefore: false, |
| 16 | + timeoutSeconds: 180, |
| 17 | + args: [ |
| 18 | + { name: 'prompt', positional: true, required: true, help: 'Prompt to send' }, |
| 19 | + { name: 'timeout', type: 'int', default: 120, help: 'Max seconds to wait for response' }, |
| 20 | + { name: 'new', type: 'boolean', default: false, help: 'Start a new chat before sending' }, |
| 21 | + { name: 'model', default: 'instant', choices: ['instant', 'expert'], help: 'Model to use: instant or expert' }, |
| 22 | + { name: 'think', type: 'boolean', default: false, help: 'Enable DeepThink mode' }, |
| 23 | + { name: 'search', type: 'boolean', default: false, help: 'Enable web search' }, |
| 24 | + ], |
| 25 | + columns: ['response'], |
| 26 | + |
| 27 | + func: async (page, kwargs) => { |
| 28 | + const prompt = kwargs.prompt; |
| 29 | + const timeoutMs = (kwargs.timeout || 120) * 1000; |
| 30 | + const wantThink = parseBoolFlag(kwargs.think); |
| 31 | + const wantSearch = parseBoolFlag(kwargs.search); |
| 32 | + |
| 33 | + if (parseBoolFlag(kwargs.new)) { |
| 34 | + await page.goto(DEEPSEEK_URL); |
| 35 | + await page.wait(3); |
| 36 | + } else { |
| 37 | + await ensureOnDeepSeek(page); |
| 38 | + } |
| 39 | + |
| 40 | + await page.wait(2); |
| 41 | + |
| 42 | + const wantModel = kwargs.model || 'instant'; |
| 43 | + const modelResult = await withRetry(() => selectModel(page, wantModel)); |
| 44 | + if (!modelResult?.ok) { |
| 45 | + throw new CommandExecutionError(`Could not switch to ${wantModel} model`); |
| 46 | + } |
| 47 | + if (modelResult.toggled) await page.wait(0.5); |
| 48 | + |
| 49 | + const thinkResult = await withRetry(() => setFeature(page, 'DeepThink', wantThink)); |
| 50 | + if (!thinkResult?.ok) { |
| 51 | + throw new CommandExecutionError('Could not toggle DeepThink'); |
| 52 | + } |
| 53 | + |
| 54 | + const searchResult = await withRetry(() => setFeature(page, 'Search', wantSearch)); |
| 55 | + if (!searchResult?.ok) { |
| 56 | + throw new CommandExecutionError('Could not toggle Search'); |
| 57 | + } |
| 58 | + |
| 59 | + if (thinkResult.toggled || searchResult.toggled) await page.wait(0.5); |
| 60 | + |
| 61 | + const baseline = await withRetry(() => getBubbleCount(page)); |
| 62 | + const sendResult = await withRetry(() => sendMessage(page, prompt)); |
| 63 | + if (!sendResult?.ok) { |
| 64 | + throw new CommandExecutionError(sendResult?.reason || 'Failed to send message'); |
| 65 | + } |
| 66 | + |
| 67 | + const response = await waitForResponse(page, baseline, prompt, timeoutMs); |
| 68 | + if (!response) { |
| 69 | + return [{ response: `[NO RESPONSE] No reply within ${kwargs.timeout}s.` }]; |
| 70 | + } |
| 71 | + |
| 72 | + return [{ response }]; |
| 73 | + }, |
| 74 | +}); |
0 commit comments