Files
jackwener__opencli/clis/codex/ask.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

72 lines
2.7 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { SelectorError } from '@jackwener/opencli/errors';
export const askCommand = cli({
site: 'codex',
name: 'ask',
description: 'Send a prompt and wait for the AI response (send + wait + read)',
domain: 'localhost',
strategy: Strategy.UI,
browser: true,
args: [
{ name: 'text', required: true, positional: true, help: 'Prompt to send' },
{ name: 'timeout', required: false, help: 'Max seconds to wait for response (default: 60)', default: '60' },
],
columns: ['Role', 'Text'],
func: async (page, kwargs) => {
const text = kwargs.text;
const timeout = parseInt(kwargs.timeout, 10) || 60;
// Snapshot the current content length before sending
const beforeLen = await page.evaluate(`
(function() {
const turns = document.querySelectorAll('[data-content-search-turn-key]');
return turns.length;
})()
`);
// Inject and send
const injected = await page.evaluate(`
(function(text) {
const editables = Array.from(document.querySelectorAll('[contenteditable="true"]'));
const composer = editables.length > 0 ? editables[editables.length - 1] : document.querySelector('textarea');
if (!composer) return false;
composer.focus();
document.execCommand('insertText', false, text);
return true;
})(${JSON.stringify(text)})
`);
if (!injected)
throw new SelectorError('Codex input element');
await page.wait(0.5);
await page.pressKey('Enter');
// Poll for new content
const pollInterval = 3;
const maxPolls = Math.ceil(timeout / pollInterval);
let response = '';
for (let i = 0; i < maxPolls; i++) {
await page.wait(pollInterval);
const result = await page.evaluate(`
(function(prevLen) {
const turns = document.querySelectorAll('[data-content-search-turn-key]');
if (turns.length <= prevLen) return null;
const lastTurn = turns[turns.length - 1];
const text = lastTurn.innerText || lastTurn.textContent;
return text ? text.trim() : null;
})(${beforeLen})
`);
if (result) {
response = result;
break;
}
}
if (!response) {
return [
{ Role: 'User', Text: text },
{ Role: 'System', Text: `No response within ${timeout}s. The agent may still be working.` },
];
}
return [
{ Role: 'User', Text: text },
{ Role: 'Assistant', Text: response },
];
},
});