@jackwener/opencli 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.github/workflows/build-extension.yml +62 -0
  2. package/.github/workflows/ci.yml +6 -6
  3. package/.github/workflows/e2e-headed.yml +2 -2
  4. package/.github/workflows/pkg-pr-new.yml +2 -2
  5. package/.github/workflows/release.yml +2 -5
  6. package/.github/workflows/security.yml +2 -2
  7. package/CDP.md +1 -1
  8. package/CDP.zh-CN.md +1 -1
  9. package/README.md +15 -7
  10. package/README.zh-CN.md +15 -7
  11. package/SKILL.md +3 -5
  12. package/dist/browser/cdp.d.ts +27 -0
  13. package/dist/browser/cdp.js +295 -0
  14. package/dist/browser/index.d.ts +3 -0
  15. package/dist/browser/index.js +4 -0
  16. package/dist/browser/page.js +2 -23
  17. package/dist/browser/utils.d.ts +10 -0
  18. package/dist/browser/utils.js +27 -0
  19. package/dist/browser.test.js +42 -1
  20. package/dist/chaoxing.d.ts +58 -0
  21. package/dist/chaoxing.js +225 -0
  22. package/dist/chaoxing.test.d.ts +1 -0
  23. package/dist/chaoxing.test.js +38 -0
  24. package/dist/cli-manifest.json +203 -0
  25. package/dist/cli.d.ts +1 -0
  26. package/dist/cli.js +197 -0
  27. package/dist/clis/boss/chatlist.d.ts +1 -0
  28. package/dist/clis/boss/chatlist.js +50 -0
  29. package/dist/clis/boss/chatmsg.d.ts +1 -0
  30. package/dist/clis/boss/chatmsg.js +73 -0
  31. package/dist/clis/boss/send.d.ts +1 -0
  32. package/dist/clis/boss/send.js +176 -0
  33. package/dist/clis/chaoxing/assignments.d.ts +1 -0
  34. package/dist/clis/chaoxing/assignments.js +74 -0
  35. package/dist/clis/chaoxing/exams.d.ts +1 -0
  36. package/dist/clis/chaoxing/exams.js +74 -0
  37. package/dist/clis/chatgpt/ask.js +15 -14
  38. package/dist/clis/chatgpt/ax.d.ts +1 -0
  39. package/dist/clis/chatgpt/ax.js +78 -0
  40. package/dist/clis/chatgpt/read.js +5 -6
  41. package/dist/clis/twitter/post.js +9 -2
  42. package/dist/clis/twitter/search.js +14 -33
  43. package/dist/clis/xiaohongshu/download.d.ts +1 -1
  44. package/dist/clis/xiaohongshu/download.js +1 -1
  45. package/dist/engine.js +24 -13
  46. package/dist/explore.js +46 -101
  47. package/dist/main.js +4 -193
  48. package/dist/output.d.ts +1 -1
  49. package/dist/registry.d.ts +3 -3
  50. package/dist/scripts/framework.d.ts +4 -0
  51. package/dist/scripts/framework.js +21 -0
  52. package/dist/scripts/interact.d.ts +4 -0
  53. package/dist/scripts/interact.js +20 -0
  54. package/dist/scripts/store.d.ts +9 -0
  55. package/dist/scripts/store.js +44 -0
  56. package/dist/synthesize.js +1 -1
  57. package/extension/dist/background.js +338 -430
  58. package/extension/manifest.json +2 -2
  59. package/extension/src/background.ts +2 -2
  60. package/package.json +1 -1
  61. package/src/browser/cdp.ts +295 -0
  62. package/src/browser/index.ts +4 -0
  63. package/src/browser/page.ts +2 -24
  64. package/src/browser/utils.ts +27 -0
  65. package/src/browser.test.ts +46 -0
  66. package/src/chaoxing.test.ts +45 -0
  67. package/src/chaoxing.ts +268 -0
  68. package/src/cli.ts +185 -0
  69. package/src/clis/antigravity/SKILL.md +5 -0
  70. package/src/clis/boss/chatlist.ts +50 -0
  71. package/src/clis/boss/chatmsg.ts +70 -0
  72. package/src/clis/boss/send.ts +193 -0
  73. package/src/clis/chaoxing/README.md +36 -0
  74. package/src/clis/chaoxing/README.zh-CN.md +35 -0
  75. package/src/clis/chaoxing/assignments.ts +88 -0
  76. package/src/clis/chaoxing/exams.ts +88 -0
  77. package/src/clis/chatgpt/ask.ts +14 -15
  78. package/src/clis/chatgpt/ax.ts +81 -0
  79. package/src/clis/chatgpt/read.ts +5 -7
  80. package/src/clis/twitter/post.ts +9 -2
  81. package/src/clis/twitter/search.ts +15 -33
  82. package/src/clis/xiaohongshu/download.ts +1 -1
  83. package/src/engine.ts +20 -13
  84. package/src/explore.ts +51 -100
  85. package/src/main.ts +4 -180
  86. package/src/output.ts +12 -12
  87. package/src/registry.ts +3 -3
  88. package/src/scripts/framework.ts +20 -0
  89. package/src/scripts/interact.ts +22 -0
  90. package/src/scripts/store.ts +40 -0
  91. package/src/synthesize.ts +1 -1
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Chaoxing (学习通) shared helpers.
3
+ *
4
+ * Flow: initSession → getCourses → enterCourse → getTabIframeUrl → navigate → parse DOM
5
+ * Chaoxing has no flat "list all assignments" API; data is behind session-gated
6
+ * course pages loaded as iframes.
7
+ */
8
+
9
+ import type { IPage } from './types.js';
10
+
11
+ // ── Utilities ────────────────────────────────────────────────────────
12
+
13
+ /** Sleep for given milliseconds (anti-scraping delay). */
14
+ export function sleep(ms: number): Promise<void> {
15
+ return new Promise(resolve => setTimeout(resolve, ms));
16
+ }
17
+
18
+ /** Execute a credentialed fetch in the browser context, returning JSON or text. */
19
+ export async function fetchChaoxing(page: IPage, url: string): Promise<any> {
20
+ const urlJs = JSON.stringify(url);
21
+ return page.evaluate(`
22
+ async () => {
23
+ const res = await fetch(${urlJs}, { credentials: "include" });
24
+ const text = await res.text();
25
+ try { return JSON.parse(text); } catch {}
26
+ return text;
27
+ }
28
+ `);
29
+ }
30
+
31
+ /** Format a timestamp (seconds or milliseconds or date string) to YYYY-MM-DD HH:mm. */
32
+ export function formatTimestamp(ts: unknown): string {
33
+ if (ts == null || ts === '' || ts === 0) return '';
34
+ if (typeof ts === 'string' && !/^\d+$/.test(ts.trim())) return ts.trim();
35
+ const num = Number(ts);
36
+ if (Number.isNaN(num) || num <= 0) return String(ts);
37
+ const millis = num > 1e12 ? num : num * 1000;
38
+ const d = new Date(millis);
39
+ if (Number.isNaN(d.getTime())) return String(ts);
40
+ const yyyy = d.getFullYear();
41
+ const mm = String(d.getMonth() + 1).padStart(2, '0');
42
+ const dd = String(d.getDate()).padStart(2, '0');
43
+ const hh = String(d.getHours()).padStart(2, '0');
44
+ const mi = String(d.getMinutes()).padStart(2, '0');
45
+ return `${yyyy}-${mm}-${dd} ${hh}:${mi}`;
46
+ }
47
+
48
+ /** Map numeric work status to Chinese label. */
49
+ export function workStatusLabel(status: unknown): string {
50
+ if (status == null || status === '') return '未知';
51
+ const s = Number(status);
52
+ if (s === 0) return '未交';
53
+ if (s === 1) return '已交';
54
+ if (s === 2) return '已批阅';
55
+ const str = String(status).trim();
56
+ return str || '未知';
57
+ }
58
+
59
+ // ── Course list ──────────────────────────────────────────────────────
60
+
61
+ export interface ChaoxingCourse {
62
+ courseId: string;
63
+ classId: string;
64
+ cpi: string;
65
+ title: string;
66
+ }
67
+
68
+ /** Fetch enrolled course list via backclazzdata JSON API. */
69
+ export async function getCourses(page: IPage): Promise<ChaoxingCourse[]> {
70
+ const resp = await fetchChaoxing(
71
+ page,
72
+ 'https://mooc1-api.chaoxing.com/mycourse/backclazzdata?view=json&rss=1',
73
+ );
74
+ if (!resp || typeof resp !== 'object') return [];
75
+ const channelList: any[] = resp.channelList ?? [];
76
+ const courses: ChaoxingCourse[] = [];
77
+ for (const channel of channelList) {
78
+ const content = channel?.content;
79
+ if (!content) continue;
80
+ const courseData = content.course?.data;
81
+ if (!Array.isArray(courseData)) continue;
82
+ for (const c of courseData) {
83
+ courses.push({
84
+ courseId: String(c.id ?? ''),
85
+ classId: String(content.id ?? ''),
86
+ cpi: String(channel.cpi ?? ''),
87
+ title: String(c.name ?? ''),
88
+ });
89
+ }
90
+ }
91
+ return courses;
92
+ }
93
+
94
+ // ── Session & course entry ───────────────────────────────────────────
95
+
96
+ /** Navigate to the interaction page to establish a Chaoxing session. */
97
+ export async function initSession(page: IPage): Promise<void> {
98
+ await page.goto('https://mooc2-ans.chaoxing.com/mooc2-ans/visit/interaction');
99
+ await page.wait(3);
100
+ }
101
+
102
+ /**
103
+ * Enter a course via stucoursemiddle redirect (establishes course session + enc).
104
+ * After this call the browser is on the course page.
105
+ */
106
+ export async function enterCourse(page: IPage, course: ChaoxingCourse): Promise<void> {
107
+ const url =
108
+ `https://mooc1.chaoxing.com/visit/stucoursemiddle` +
109
+ `?courseid=${course.courseId}&clazzid=${course.classId}&cpi=${course.cpi}&ismooc2=1&v=2`;
110
+ await page.goto(url);
111
+ await page.wait(3);
112
+ }
113
+
114
+ /**
115
+ * On the course page, click a tab (作业 / 考试) and return the iframe src
116
+ * that gets loaded. Returns empty string if the tab is not found.
117
+ */
118
+ export async function getTabIframeUrl(page: IPage, tabName: string): Promise<string> {
119
+ const nameJs = JSON.stringify(tabName);
120
+ const result: any = await page.evaluate(`
121
+ async () => {
122
+ const tabs = document.querySelectorAll('a[data-url]');
123
+ let target = null;
124
+ for (const tab of tabs) {
125
+ if ((tab.innerText || '').trim() === ${nameJs}) { target = tab; break; }
126
+ }
127
+ if (!target) return '';
128
+ target.click();
129
+ await new Promise(r => setTimeout(r, 2000));
130
+ const iframe = document.getElementById('frame_content-hd') || document.querySelector('iframe');
131
+ return iframe?.src || '';
132
+ }
133
+ `);
134
+ return typeof result === 'string' ? result : '';
135
+ }
136
+
137
+ // ── Assignment parsing ───────────────────────────────────────────────
138
+
139
+ export interface AssignmentRow {
140
+ course: string;
141
+ title: string;
142
+ deadline: string;
143
+ status: string;
144
+ score: string;
145
+ }
146
+
147
+ /**
148
+ * Parse assignments from the current page DOM (the 作业列表 page).
149
+ * The page uses `.ulDiv li` items with status/deadline/score info.
150
+ */
151
+ export async function parseAssignmentsFromDom(page: IPage, courseName: string): Promise<AssignmentRow[]> {
152
+ const raw: any[] = await page.evaluate(`
153
+ (() => {
154
+ const items = [];
155
+ // Each assignment is a li or div block; try multiple selectors
156
+ const blocks = document.querySelectorAll('.ulDiv li, .work-list-item, .listContent > div, ul > li');
157
+ for (const block of blocks) {
158
+ const text = (block.innerText || '').trim();
159
+ if (!text || text.length < 3) continue;
160
+ // Skip filter buttons and headers
161
+ if (/^(全部|已完成|未完成|筛选)$/.test(text)) continue;
162
+ items.push(text);
163
+ }
164
+ // Fallback: split body text by common patterns
165
+ if (items.length === 0) {
166
+ const body = (document.body?.innerText || '').trim();
167
+ return [body];
168
+ }
169
+ return items;
170
+ })()
171
+ `) ?? [];
172
+
173
+ const rows: AssignmentRow[] = [];
174
+ for (const text of raw) {
175
+ if (typeof text !== 'string' || text.length < 3) continue;
176
+ // Skip noise
177
+ if (/^(全部|已完成|未完成|筛选|暂无|提交的作业将经过)/.test(text)) continue;
178
+
179
+ const lines = text.split('\n').map((l: string) => l.trim()).filter(Boolean);
180
+ if (!lines.length) continue;
181
+
182
+ // First meaningful line is the title
183
+ const title = lines[0].replace(/\s+/g, ' ').trim();
184
+ if (!title || /^(全部|已完成|未完成|筛选)$/.test(title)) continue;
185
+
186
+ // Extract status: 未交 / 待批阅 / 已完成 / 已批阅
187
+ const statusMatch = text.match(/(未交|待批阅|已完成|已批阅)/);
188
+ const status = statusMatch?.[1] ?? '';
189
+
190
+ // Extract deadline: "剩余XXX" or date pattern
191
+ const remainMatch = text.match(/(剩余[\d天小时分钟秒]+)/);
192
+ const dateMatch = text.match(/(\d{4}[-/.]\d{1,2}[-/.]\d{1,2}(?:\s+\d{1,2}:\d{2})?)/);
193
+ const deadline = remainMatch?.[1] ?? dateMatch?.[1] ?? '';
194
+
195
+ // Extract score (exclude "分钟")
196
+ const scoreMatch = text.match(/(\d+(?:\.\d+)?)\s*分(?!钟)/);
197
+ const score = scoreMatch?.[1] ?? '';
198
+
199
+ rows.push({ course: courseName, title, deadline, status, score });
200
+ }
201
+ return rows;
202
+ }
203
+
204
+ // ── Exam parsing ─────────────────────────────────────────────────────
205
+
206
+ export interface ExamRow {
207
+ course: string;
208
+ title: string;
209
+ start: string;
210
+ end: string;
211
+ status: string;
212
+ score: string;
213
+ }
214
+
215
+ /** Parse exams from the current page DOM (the 考试列表 page). */
216
+ export async function parseExamsFromDom(page: IPage, courseName: string): Promise<ExamRow[]> {
217
+ const raw: any[] = await page.evaluate(`
218
+ (() => {
219
+ const items = [];
220
+ const blocks = document.querySelectorAll('.ulDiv li, .exam-list-item, .listContent > div, ul > li');
221
+ for (const block of blocks) {
222
+ const text = (block.innerText || '').trim();
223
+ if (!text || text.length < 3) continue;
224
+ if (/^(全部|已完成|未完成|筛选|暂无)$/.test(text)) continue;
225
+ items.push(text);
226
+ }
227
+ if (items.length === 0) {
228
+ const body = (document.body?.innerText || '').trim();
229
+ return [body];
230
+ }
231
+ return items;
232
+ })()
233
+ `) ?? [];
234
+
235
+ // Check for "暂无考试"
236
+ if (raw.length === 1 && typeof raw[0] === 'string' && raw[0].includes('暂无考试')) {
237
+ return [];
238
+ }
239
+
240
+ const rows: ExamRow[] = [];
241
+ for (const text of raw) {
242
+ if (typeof text !== 'string' || text.length < 3) continue;
243
+ if (/^(全部|已完成|未完成|筛选|暂无)/.test(text)) continue;
244
+
245
+ const lines = text.split('\n').map((l: string) => l.trim()).filter(Boolean);
246
+ if (!lines.length) continue;
247
+
248
+ const title = lines[0].replace(/\s+/g, ' ').trim();
249
+ if (!title || /^(全部|已完成|未完成|筛选)$/.test(title)) continue;
250
+
251
+ // Extract dates
252
+ const dates = text.match(/\d{4}[-/.]\d{1,2}[-/.]\d{1,2}\s+\d{1,2}:\d{2}/g) ?? [];
253
+ const start = dates[0] ?? '';
254
+ const end = dates[1] ?? '';
255
+
256
+ // Status
257
+ const statusMatch = text.match(/(未开始|进行中|已结束|已完成|未交|待批阅)/);
258
+ let status = statusMatch?.[1] ?? '';
259
+ if (!status && text.includes('剩余')) status = '进行中';
260
+
261
+ // Score (exclude "分钟")
262
+ const scoreMatch = text.match(/(\d+(?:\.\d+)?)\s*分(?!钟)/);
263
+ const score = scoreMatch?.[1] ?? '';
264
+
265
+ rows.push({ course: courseName, title, start, end, status, score });
266
+ }
267
+ return rows;
268
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,185 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import { executeCommand } from './engine.js';
4
+ import { type CliCommand, fullName, getRegistry, strategyLabel } from './registry.js';
5
+ import { render as renderOutput } from './output.js';
6
+ import { BrowserBridge, CDPBridge } from './browser/index.js';
7
+ import { browserSession, DEFAULT_BROWSER_COMMAND_TIMEOUT, runWithTimeout } from './runtime.js';
8
+ import { PKG_VERSION } from './version.js';
9
+ import { printCompletionScript } from './completion.js';
10
+ import { CliError } from './errors.js';
11
+
12
+ export function runCli(BUILTIN_CLIS: string, USER_CLIS: string): void {
13
+ const program = new Command();
14
+ program.name('opencli').description('Make any website your CLI. Zero setup. AI-powered.').version(PKG_VERSION);
15
+
16
+ // ── Built-in commands ──────────────────────────────────────────────────────
17
+
18
+ program.command('list').description('List all available CLI commands').option('-f, --format <fmt>', 'Output format: table, json, yaml, md, csv', 'table').option('--json', 'JSON output (deprecated)')
19
+ .action((opts) => {
20
+ const registry = getRegistry();
21
+ const commands = [...registry.values()].sort((a, b) => fullName(a).localeCompare(fullName(b)));
22
+ const rows = commands.map(c => ({
23
+ command: fullName(c),
24
+ site: c.site,
25
+ name: c.name,
26
+ description: c.description,
27
+ strategy: strategyLabel(c),
28
+ browser: c.browser,
29
+ args: c.args.map(a => a.name).join(', '),
30
+ }));
31
+ const fmt = opts.json && opts.format === 'table' ? 'json' : opts.format;
32
+ if (fmt !== 'table') {
33
+ renderOutput(rows, {
34
+ fmt,
35
+ columns: ['command', 'site', 'name', 'description', 'strategy', 'browser', 'args'],
36
+ title: 'opencli/list',
37
+ source: 'opencli list',
38
+ });
39
+ return;
40
+ }
41
+ const sites = new Map<string, CliCommand[]>();
42
+ for (const cmd of commands) { const g = sites.get(cmd.site) ?? []; g.push(cmd); sites.set(cmd.site, g); }
43
+ console.log(); console.log(chalk.bold(' opencli') + chalk.dim(' — available commands')); console.log();
44
+ for (const [site, cmds] of sites) {
45
+ console.log(chalk.bold.cyan(` ${site}`));
46
+ for (const cmd of cmds) { const tag = strategyLabel(cmd) === 'public' ? chalk.green('[public]') : chalk.yellow(`[${strategyLabel(cmd)}]`); console.log(` ${cmd.name} ${tag}${cmd.description ? chalk.dim(` — ${cmd.description}`) : ''}`); }
47
+ console.log();
48
+ }
49
+ console.log(chalk.dim(` ${commands.length} commands across ${sites.size} sites`)); console.log();
50
+ });
51
+
52
+ program.command('validate').description('Validate CLI definitions').argument('[target]', 'site or site/name')
53
+ .action(async (target) => {
54
+ const { validateClisWithTarget, renderValidationReport } = await import('./validate.js');
55
+ console.log(renderValidationReport(validateClisWithTarget([BUILTIN_CLIS, USER_CLIS], target)));
56
+ });
57
+
58
+ program.command('verify').description('Validate + smoke test').argument('[target]').option('--smoke', 'Run smoke tests', false)
59
+ .action(async (target, opts) => {
60
+ const { verifyClis, renderVerifyReport } = await import('./verify.js');
61
+ const r = await verifyClis({ builtinClis: BUILTIN_CLIS, userClis: USER_CLIS, target, smoke: opts.smoke });
62
+ console.log(renderVerifyReport(r));
63
+ process.exitCode = r.ok ? 0 : 1;
64
+ });
65
+
66
+ program.command('explore').alias('probe').description('Explore a website: discover APIs, stores, and recommend strategies').argument('<url>').option('--site <name>').option('--goal <text>').option('--wait <s>', '', '3').option('--auto', 'Enable interactive fuzzing (simulate clicks to trigger lazy APIs)').option('--click <labels>', 'Comma-separated labels to click before fuzzing (e.g. "字幕,CC,评论")')
67
+ .action(async (url, opts) => { const { exploreUrl, renderExploreSummary } = await import('./explore.js'); const clickLabels = opts.click ? opts.click.split(',').map((s: string) => s.trim()) : undefined; const BrowserFactory = process.env.OPENCLI_CDP_ENDPOINT ? CDPBridge : BrowserBridge; console.log(renderExploreSummary(await exploreUrl(url, { BrowserFactory: BrowserFactory as any, site: opts.site, goal: opts.goal, waitSeconds: parseFloat(opts.wait), auto: opts.auto, clickLabels }))); });
68
+
69
+ program.command('synthesize').description('Synthesize CLIs from explore').argument('<target>').option('--top <n>', '', '3')
70
+ .action(async (target, opts) => { const { synthesizeFromExplore, renderSynthesizeSummary } = await import('./synthesize.js'); console.log(renderSynthesizeSummary(synthesizeFromExplore(target, { top: parseInt(opts.top) }))); });
71
+
72
+ program.command('generate').description('One-shot: explore → synthesize → register').argument('<url>').option('--goal <text>').option('--site <name>')
73
+ .action(async (url, opts) => { const { generateCliFromUrl, renderGenerateSummary } = await import('./generate.js'); const BrowserFactory = process.env.OPENCLI_CDP_ENDPOINT ? CDPBridge : BrowserBridge; const r = await generateCliFromUrl({ url, BrowserFactory: BrowserFactory as any, builtinClis: BUILTIN_CLIS, userClis: USER_CLIS, goal: opts.goal, site: opts.site }); console.log(renderGenerateSummary(r)); process.exitCode = r.ok ? 0 : 1; });
74
+
75
+ program.command('cascade').description('Strategy cascade: find simplest working strategy').argument('<url>').option('--site <name>')
76
+ .action(async (url, opts) => {
77
+ const { cascadeProbe, renderCascadeResult } = await import('./cascade.js');
78
+ const BrowserFactory = process.env.OPENCLI_CDP_ENDPOINT ? CDPBridge : BrowserBridge;
79
+ const result = await browserSession(BrowserFactory as any, async (page) => {
80
+ // Navigate to the site first for cookie context
81
+ try { const siteUrl = new URL(url); await page.goto(`${siteUrl.protocol}//${siteUrl.host}`); await page.wait(2); } catch {}
82
+ return cascadeProbe(page, url);
83
+ });
84
+ console.log(renderCascadeResult(result));
85
+ });
86
+
87
+ program.command('doctor')
88
+ .description('Diagnose opencli browser bridge connectivity')
89
+ .option('--live', 'Test browser connectivity (requires Chrome running)', false)
90
+ .action(async (opts) => {
91
+ const { runBrowserDoctor, renderBrowserDoctorReport } = await import('./doctor.js');
92
+ const report = await runBrowserDoctor({ live: opts.live, cliVersion: PKG_VERSION });
93
+ console.log(renderBrowserDoctorReport(report));
94
+ });
95
+
96
+ program.command('setup')
97
+ .description('Interactive setup: verify browser bridge connectivity')
98
+ .action(async () => {
99
+ const { runSetup } = await import('./setup.js');
100
+ await runSetup({ cliVersion: PKG_VERSION });
101
+ });
102
+
103
+ program.command('completion')
104
+ .description('Output shell completion script')
105
+ .argument('<shell>', 'Shell type: bash, zsh, or fish')
106
+ .action((shell) => {
107
+ printCompletionScript(shell);
108
+ });
109
+
110
+ // ── Dynamic site commands ──────────────────────────────────────────────────
111
+
112
+ const registry = getRegistry();
113
+ const siteGroups = new Map<string, Command>();
114
+
115
+ for (const [, cmd] of registry) {
116
+ let siteCmd = siteGroups.get(cmd.site);
117
+ if (!siteCmd) { siteCmd = program.command(cmd.site).description(`${cmd.site} commands`); siteGroups.set(cmd.site, siteCmd); }
118
+ const subCmd = siteCmd.command(cmd.name).description(cmd.description);
119
+
120
+ // Register positional args first, then named options
121
+ const positionalArgs: typeof cmd.args = [];
122
+ for (const arg of cmd.args) {
123
+ if (arg.positional) {
124
+ const bracket = arg.required ? `<${arg.name}>` : `[${arg.name}]`;
125
+ subCmd.argument(bracket, arg.help ?? '');
126
+ positionalArgs.push(arg);
127
+ } else {
128
+ const flag = arg.required ? `--${arg.name} <value>` : `--${arg.name} [value]`;
129
+ if (arg.required) subCmd.requiredOption(flag, arg.help ?? '');
130
+ else if (arg.default != null) subCmd.option(flag, arg.help ?? '', String(arg.default));
131
+ else subCmd.option(flag, arg.help ?? '');
132
+ }
133
+ }
134
+ subCmd.option('-f, --format <fmt>', 'Output format: table, json, yaml, md, csv', 'table').option('-v, --verbose', 'Debug output', false);
135
+
136
+ subCmd.action(async (...actionArgs: any[]) => {
137
+ // Commander passes positional args first, then options object, then the Command
138
+ const actionOpts = actionArgs[positionalArgs.length] ?? {};
139
+ const startTime = Date.now();
140
+ const kwargs: Record<string, any> = {};
141
+
142
+ // Collect positional args
143
+ for (let i = 0; i < positionalArgs.length; i++) {
144
+ const arg = positionalArgs[i];
145
+ const v = actionArgs[i];
146
+ if (v !== undefined) kwargs[arg.name] = v;
147
+ }
148
+
149
+ // Collect named options
150
+ for (const arg of cmd.args) {
151
+ if (arg.positional) continue;
152
+ const camelName = arg.name.replace(/-([a-z])/g, (_m, ch: string) => ch.toUpperCase());
153
+ const v = actionOpts[arg.name] ?? actionOpts[camelName];
154
+ if (v !== undefined) kwargs[arg.name] = v;
155
+ }
156
+
157
+ try {
158
+ if (actionOpts.verbose) process.env.OPENCLI_VERBOSE = '1';
159
+ let result: any;
160
+ if (cmd.browser) {
161
+ const BrowserFactory = process.env.OPENCLI_CDP_ENDPOINT ? CDPBridge : BrowserBridge;
162
+ result = await browserSession(BrowserFactory as any, async (page) => {
163
+ return runWithTimeout(executeCommand(cmd, page, kwargs, actionOpts.verbose), { timeout: cmd.timeoutSeconds ?? DEFAULT_BROWSER_COMMAND_TIMEOUT, label: fullName(cmd) });
164
+ });
165
+ } else { result = await executeCommand(cmd, null, kwargs, actionOpts.verbose); }
166
+ if (actionOpts.verbose && (!result || (Array.isArray(result) && result.length === 0))) {
167
+ console.error(chalk.yellow(`[Verbose] Warning: Command returned an empty result. If the website structural API changed or requires authentication, check the network or update the adapter.`));
168
+ }
169
+ renderOutput(result, { fmt: actionOpts.format, columns: cmd.columns, title: `${cmd.site}/${cmd.name}`, elapsed: (Date.now() - startTime) / 1000, source: fullName(cmd) });
170
+ } catch (err: any) {
171
+ if (err instanceof CliError) {
172
+ console.error(chalk.red(`Error [${err.code}]: ${err.message}`));
173
+ if (err.hint) console.error(chalk.yellow(`Hint: ${err.hint}`));
174
+ } else if (actionOpts.verbose && err.stack) {
175
+ console.error(chalk.red(err.stack));
176
+ } else {
177
+ console.error(chalk.red(`Error: ${err.message ?? err}`));
178
+ }
179
+ process.exitCode = 1;
180
+ }
181
+ });
182
+ }
183
+
184
+ program.parse();
185
+ }
@@ -17,6 +17,11 @@ The agent must configure the endpoint environment variable locally before invoki
17
17
  export OPENCLI_CDP_ENDPOINT="http://127.0.0.1:9224"
18
18
  \`\`\`
19
19
 
20
+ If the endpoint exposes multiple inspectable targets, also set:
21
+ \`\`\`bash
22
+ export OPENCLI_CDP_TARGET="antigravity"
23
+ \`\`\`
24
+
20
25
  ## High-Level Capabilities
21
26
  1. **Send Messages (`opencli antigravity send <message>`)**: Type and send a message directly into the chat UI.
22
27
  2. **Read History (`opencli antigravity read`)**: Scrape the raw chat transcript from the main UI container.
@@ -0,0 +1,50 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import type { IPage } from '../../types.js';
3
+
4
+ cli({
5
+ site: 'boss',
6
+ name: 'chatlist',
7
+ description: 'BOSS直聘查看聊天列表(招聘端)',
8
+ domain: 'www.zhipin.com',
9
+ strategy: Strategy.COOKIE,
10
+ browser: true,
11
+ args: [
12
+ { name: 'page', type: 'int', default: 1, help: 'Page number' },
13
+ { name: 'limit', type: 'int', default: 20, help: 'Number of results' },
14
+ { name: 'job_id', default: '0', help: 'Filter by job ID (0=all)' },
15
+ ],
16
+ columns: ['name', 'job', 'last_msg', 'last_time', 'uid', 'security_id'],
17
+ func: async (page: IPage | null, kwargs) => {
18
+ if (!page) throw new Error('Browser page required');
19
+ await page.goto('https://www.zhipin.com/web/chat/index');
20
+ await page.wait({ time: 2 });
21
+ const jobId = kwargs.job_id || '0';
22
+ const pageNum = kwargs.page || 1;
23
+ const limit = kwargs.limit || 20;
24
+ const targetUrl = `https://www.zhipin.com/wapi/zprelation/friend/getBossFriendListV2.json?page=${pageNum}&status=0&jobId=${jobId}`;
25
+ const data: any = await page.evaluate(`
26
+ async () => {
27
+ return new Promise((resolve, reject) => {
28
+ const xhr = new XMLHttpRequest();
29
+ xhr.open('GET', '${targetUrl}', true);
30
+ xhr.withCredentials = true;
31
+ xhr.timeout = 15000;
32
+ xhr.setRequestHeader('Accept', 'application/json');
33
+ xhr.onload = () => { try { resolve(JSON.parse(xhr.responseText)); } catch(e) { reject(new Error('JSON parse failed')); } };
34
+ xhr.onerror = () => reject(new Error('Network Error'));
35
+ xhr.send();
36
+ });
37
+ }
38
+ `);
39
+ if (data.code !== 0) throw new Error(`API error: ${data.message} (code=${data.code})`);
40
+ const friends = (data.zpData?.friendList || []).slice(0, limit);
41
+ return friends.map((f: any) => ({
42
+ name: f.name || '',
43
+ job: f.jobName || '',
44
+ last_msg: f.lastMessageInfo?.text || '',
45
+ last_time: f.lastTime || '',
46
+ uid: f.encryptUid || '',
47
+ security_id: f.securityId || '',
48
+ }));
49
+ },
50
+ });
@@ -0,0 +1,70 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import type { IPage } from '../../types.js';
3
+
4
+ cli({
5
+ site: 'boss',
6
+ name: 'chatmsg',
7
+ description: 'BOSS直聘查看与候选人的聊天消息',
8
+ domain: 'www.zhipin.com',
9
+ strategy: Strategy.COOKIE,
10
+ browser: true,
11
+ args: [
12
+ { name: 'uid', required: true, help: 'Encrypted UID (from chatlist)' },
13
+ { name: 'page', type: 'int', default: 1, help: 'Page number' },
14
+ ],
15
+ columns: ['from', 'type', 'text', 'time'],
16
+ func: async (page: IPage | null, kwargs) => {
17
+ if (!page) throw new Error('Browser page required');
18
+ await page.goto('https://www.zhipin.com/web/chat/index');
19
+ await page.wait({ time: 2 });
20
+ const uid = kwargs.uid;
21
+ const friendData: any = await page.evaluate(`
22
+ async () => {
23
+ return new Promise((resolve, reject) => {
24
+ const xhr = new XMLHttpRequest();
25
+ xhr.open('GET', 'https://www.zhipin.com/wapi/zprelation/friend/getBossFriendListV2.json?page=1&status=0&jobId=0', true);
26
+ xhr.withCredentials = true;
27
+ xhr.timeout = 15000;
28
+ xhr.setRequestHeader('Accept', 'application/json');
29
+ xhr.onload = () => { try { resolve(JSON.parse(xhr.responseText)); } catch(e) { reject(e); } };
30
+ xhr.onerror = () => reject(new Error('Network Error'));
31
+ xhr.send();
32
+ });
33
+ }
34
+ `);
35
+ if (friendData.code !== 0) throw new Error('获取好友列表失败');
36
+ const friend = (friendData.zpData?.friendList || []).find((f: any) => f.encryptUid === uid);
37
+ if (!friend) throw new Error('未找到该候选人');
38
+ const gid = friend.uid;
39
+ const securityId = encodeURIComponent(friend.securityId);
40
+ const msgUrl = `https://www.zhipin.com/wapi/zpchat/boss/historyMsg?gid=${gid}&securityId=${securityId}&page=${kwargs.page}&c=20&src=0`;
41
+ const msgData: any = await page.evaluate(`
42
+ async () => {
43
+ return new Promise((resolve, reject) => {
44
+ const xhr = new XMLHttpRequest();
45
+ xhr.open('GET', '${msgUrl}', true);
46
+ xhr.withCredentials = true;
47
+ xhr.timeout = 15000;
48
+ xhr.setRequestHeader('Accept', 'application/json');
49
+ xhr.onload = () => { try { resolve(JSON.parse(xhr.responseText)); } catch(e) { resolve({raw: xhr.responseText.substring(0,500)}); } };
50
+ xhr.onerror = () => reject(new Error('Network Error'));
51
+ xhr.send();
52
+ });
53
+ }
54
+ `);
55
+ if (msgData.raw) throw new Error('Non-JSON: ' + msgData.raw);
56
+ if (msgData.code !== 0) throw new Error('API error: ' + (msgData.message || msgData.code));
57
+ const TYPE_MAP: Record<number, string> = {1: '文本', 2: '图片', 3: '招呼', 4: '简历', 5: '系统', 6: '名片', 7: '语音', 8: '视频', 9: '表情'};
58
+ const messages = msgData.zpData?.messages || msgData.zpData?.historyMsgList || [];
59
+ return messages.map((m: any) => {
60
+ const fromObj = m.from || {};
61
+ const isSelf = typeof fromObj === 'object' ? fromObj.uid !== friend.uid : false;
62
+ return {
63
+ from: isSelf ? '我' : (typeof fromObj === 'object' ? fromObj.name : friend.name),
64
+ type: TYPE_MAP[m.type] || '其他(' + m.type + ')',
65
+ text: m.text || m.body?.text || '',
66
+ time: m.time ? new Date(m.time).toLocaleString('zh-CN') : '',
67
+ };
68
+ });
69
+ },
70
+ });