9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { CommandExecutionError, TimeoutError } from '@jackwener/opencli/errors';
|
|
import {
|
|
authRequired,
|
|
ensureOnGrok,
|
|
getMessageBubbles,
|
|
isLoggedIn,
|
|
normalizeBooleanFlag,
|
|
sendMessage,
|
|
startNewChat,
|
|
waitForAnswer,
|
|
} from './utils.js';
|
|
|
|
const SESSION_HINT = 'Likely login/auth/challenge/session issue in the existing grok.com browser session.';
|
|
|
|
async function getBaselineLastAssistantId(page) {
|
|
const bubbles = await getMessageBubbles(page);
|
|
for (let i = bubbles.length - 1; i >= 0; i -= 1) {
|
|
if (bubbles[i].role === 'Assistant') return bubbles[i].id;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export const askCommand = cli({
|
|
site: 'grok',
|
|
name: 'ask',
|
|
access: 'write',
|
|
description: 'Send a message to Grok and get response',
|
|
domain: 'grok.com',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
siteSession: 'persistent',
|
|
args: [
|
|
{ name: 'prompt', positional: true, type: 'string', required: true, help: 'Prompt to send to Grok' },
|
|
{ name: 'timeout', type: 'int', default: 120, help: 'Max seconds to wait for response (default: 120)' },
|
|
{ name: 'new', type: 'boolean', default: false, help: 'Start a new chat before sending (default: false)' },
|
|
],
|
|
columns: ['response'],
|
|
func: async (page, kwargs) => {
|
|
const prompt = kwargs.prompt;
|
|
const timeoutSeconds = kwargs.timeout || 120;
|
|
const newChat = normalizeBooleanFlag(kwargs.new);
|
|
|
|
if (newChat) {
|
|
await startNewChat(page);
|
|
} else {
|
|
await ensureOnGrok(page);
|
|
}
|
|
|
|
if (!(await isLoggedIn(page))) {
|
|
throw authRequired();
|
|
}
|
|
|
|
const baselineLastAssistantId = await getBaselineLastAssistantId(page);
|
|
const sendResult = await sendMessage(page, prompt);
|
|
if (!sendResult || !sendResult.ok) {
|
|
const reason = sendResult?.reason || 'Unable to send the prompt to Grok.';
|
|
const detail = sendResult?.detail ? ` ${sendResult.detail}` : '';
|
|
throw new CommandExecutionError(`${reason}${detail}`, SESSION_HINT);
|
|
}
|
|
|
|
const result = await waitForAnswer(page, prompt, timeoutSeconds, baselineLastAssistantId);
|
|
if (result.status === 'ok') {
|
|
return [{ response: result.assistant.text }];
|
|
}
|
|
// Partial: streaming was seen but did not stabilize; keep the best-effort
|
|
// text rather than throwing — the caller asked us to wait, not to discard.
|
|
if (result.status === 'partial' && result.assistant) {
|
|
return [{ response: result.assistant.text }];
|
|
}
|
|
throw new TimeoutError('grok ask response', timeoutSeconds);
|
|
},
|
|
});
|
|
|
|
export const __test__ = {
|
|
getBaselineLastAssistantId,
|
|
};
|