@jackwener/opencli 0.1.1 → 0.1.2

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 (103) hide show
  1. package/README.md +9 -2
  2. package/README.zh-CN.md +9 -1
  3. package/SKILL.md +24 -0
  4. package/dist/bilibili.d.ts +6 -5
  5. package/dist/browser.d.ts +2 -1
  6. package/dist/browser.js +9 -1
  7. package/dist/cascade.d.ts +3 -2
  8. package/dist/clis/bbc/news.js +42 -0
  9. package/dist/clis/boss/search.d.ts +1 -0
  10. package/dist/clis/boss/search.js +47 -0
  11. package/dist/clis/ctrip/search.d.ts +1 -0
  12. package/dist/clis/ctrip/search.js +62 -0
  13. package/dist/clis/index.d.ts +8 -0
  14. package/dist/clis/index.js +16 -0
  15. package/dist/clis/reuters/search.d.ts +1 -0
  16. package/dist/clis/reuters/search.js +52 -0
  17. package/dist/clis/smzdm/search.d.ts +1 -0
  18. package/dist/clis/smzdm/search.js +66 -0
  19. package/dist/clis/weibo/hot.d.ts +1 -0
  20. package/dist/clis/weibo/hot.js +41 -0
  21. package/dist/clis/yahoo-finance/quote.d.ts +1 -0
  22. package/dist/clis/yahoo-finance/quote.js +74 -0
  23. package/dist/clis/youtube/search.d.ts +1 -0
  24. package/dist/clis/youtube/search.js +60 -0
  25. package/dist/engine.d.ts +2 -1
  26. package/dist/explore.js +1 -1
  27. package/dist/generate.js +2 -1
  28. package/dist/main.js +6 -4
  29. package/dist/pipeline/executor.d.ts +9 -0
  30. package/dist/pipeline/executor.js +88 -0
  31. package/dist/pipeline/index.d.ts +5 -0
  32. package/dist/pipeline/index.js +5 -0
  33. package/dist/pipeline/steps/browser.d.ts +12 -0
  34. package/dist/pipeline/steps/browser.js +68 -0
  35. package/dist/pipeline/steps/fetch.d.ts +5 -0
  36. package/dist/pipeline/steps/fetch.js +50 -0
  37. package/dist/pipeline/steps/intercept.d.ts +5 -0
  38. package/dist/pipeline/steps/intercept.js +75 -0
  39. package/dist/pipeline/steps/tap.d.ts +12 -0
  40. package/dist/pipeline/steps/tap.js +130 -0
  41. package/dist/pipeline/steps/transform.d.ts +8 -0
  42. package/dist/pipeline/steps/transform.js +53 -0
  43. package/dist/pipeline/template.d.ts +16 -0
  44. package/dist/pipeline/template.js +115 -0
  45. package/dist/pipeline/template.test.d.ts +4 -0
  46. package/dist/pipeline/template.test.js +102 -0
  47. package/dist/pipeline/transform.test.d.ts +4 -0
  48. package/dist/pipeline/transform.test.js +90 -0
  49. package/dist/pipeline.d.ts +5 -7
  50. package/dist/pipeline.js +5 -549
  51. package/dist/registry.d.ts +3 -2
  52. package/dist/runtime.d.ts +2 -1
  53. package/dist/types.d.ts +27 -0
  54. package/dist/types.js +7 -0
  55. package/package.json +6 -3
  56. package/src/bilibili.ts +9 -7
  57. package/src/browser.ts +8 -2
  58. package/src/cascade.ts +3 -2
  59. package/src/clis/bbc/news.ts +42 -0
  60. package/src/clis/boss/search.ts +47 -0
  61. package/src/clis/ctrip/search.ts +62 -0
  62. package/src/clis/index.ts +24 -0
  63. package/src/clis/reuters/search.ts +52 -0
  64. package/src/clis/smzdm/search.ts +66 -0
  65. package/src/clis/weibo/hot.ts +41 -0
  66. package/src/clis/yahoo-finance/quote.ts +74 -0
  67. package/src/clis/youtube/search.ts +60 -0
  68. package/src/engine.ts +2 -1
  69. package/src/explore.ts +1 -1
  70. package/src/generate.ts +3 -1
  71. package/src/main.ts +7 -5
  72. package/src/pipeline/executor.ts +98 -0
  73. package/src/pipeline/index.ts +6 -0
  74. package/src/pipeline/steps/browser.ts +67 -0
  75. package/src/pipeline/steps/fetch.ts +60 -0
  76. package/src/pipeline/steps/intercept.ts +78 -0
  77. package/src/pipeline/steps/tap.ts +137 -0
  78. package/src/pipeline/steps/transform.ts +50 -0
  79. package/src/pipeline/template.test.ts +107 -0
  80. package/src/pipeline/template.ts +101 -0
  81. package/src/pipeline/transform.test.ts +107 -0
  82. package/src/pipeline.ts +5 -529
  83. package/src/registry.ts +4 -2
  84. package/src/runtime.ts +3 -1
  85. package/src/types.ts +23 -0
  86. package/vitest.config.ts +7 -0
  87. package/dist/clis/github/search.js +0 -20
  88. package/dist/clis/github/trending.yaml +0 -58
  89. package/dist/promote.d.ts +0 -1
  90. package/dist/promote.js +0 -3
  91. package/dist/register.d.ts +0 -2
  92. package/dist/register.js +0 -2
  93. package/dist/scaffold.d.ts +0 -2
  94. package/dist/scaffold.js +0 -2
  95. package/dist/smoke.d.ts +0 -2
  96. package/dist/smoke.js +0 -2
  97. package/src/clis/github/search.ts +0 -21
  98. package/src/clis/github/trending.yaml +0 -58
  99. package/src/promote.ts +0 -3
  100. package/src/register.ts +0 -2
  101. package/src/scaffold.ts +0 -2
  102. package/src/smoke.ts +0 -2
  103. /package/dist/clis/{github/search.d.ts → bbc/news.d.ts} +0 -0
package/dist/pipeline.js CHANGED
@@ -1,551 +1,7 @@
1
1
  /**
2
- * YAML pipeline executor.
3
- * Steps: fetch, navigate, evaluate, map, filter, sort, limit, select, snapshot, click, type, wait, press, intercept.
2
+ * YAML pipeline executor — re-exports from modular pipeline system.
3
+ *
4
+ * This file exists for backward compatibility. All logic has been
5
+ * refactored into src/pipeline/ with modular step handlers.
4
6
  */
5
- import chalk from 'chalk';
6
- export async function executePipeline(page, pipeline, ctx = {}) {
7
- const args = ctx.args ?? {};
8
- const debug = ctx.debug ?? false;
9
- let data = null;
10
- const total = pipeline.length;
11
- for (let i = 0; i < pipeline.length; i++) {
12
- const step = pipeline[i];
13
- if (!step || typeof step !== 'object')
14
- continue;
15
- for (const [op, params] of Object.entries(step)) {
16
- if (debug)
17
- debugStepStart(i + 1, total, op, params);
18
- data = await executeStep(page, op, params, data, args);
19
- // Detect error objects returned by steps (e.g. tap store not found)
20
- if (data && typeof data === 'object' && !Array.isArray(data) && data.error) {
21
- process.stderr.write(` ${chalk.yellow('⚠')} ${chalk.yellow(op)}: ${data.error}\n`);
22
- if (data.hint)
23
- process.stderr.write(` ${chalk.dim('💡')} ${chalk.dim(data.hint)}\n`);
24
- }
25
- if (debug)
26
- debugStepResult(op, data);
27
- }
28
- }
29
- return data;
30
- }
31
- function normalizeEvaluateSource(source) {
32
- const stripped = source.trim();
33
- if (!stripped)
34
- return '() => undefined';
35
- if (stripped.startsWith('(') && stripped.endsWith(')()'))
36
- return `() => (${stripped})`;
37
- if (/^(async\s+)?\([^)]*\)\s*=>/.test(stripped))
38
- return stripped;
39
- if (/^(async\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=>/.test(stripped))
40
- return stripped;
41
- if (stripped.startsWith('function ') || stripped.startsWith('async function '))
42
- return stripped;
43
- return `() => (${stripped})`;
44
- }
45
- function debugStepStart(stepNum, total, op, params) {
46
- let preview = '';
47
- if (typeof params === 'string') {
48
- preview = params.length <= 80 ? ` → ${params}` : ` → ${params.slice(0, 77)}...`;
49
- }
50
- else if (params && typeof params === 'object' && !Array.isArray(params)) {
51
- preview = ` (${Object.keys(params).join(', ')})`;
52
- }
53
- process.stderr.write(` ${chalk.dim(`[${stepNum}/${total}]`)} ${chalk.bold.cyan(op)}${preview}\n`);
54
- }
55
- function debugStepResult(op, data) {
56
- if (data === null || data === undefined) {
57
- process.stderr.write(` ${chalk.dim('→ (no data)')}\n`);
58
- }
59
- else if (Array.isArray(data)) {
60
- process.stderr.write(` ${chalk.dim(`→ ${data.length} items`)}\n`);
61
- }
62
- else if (typeof data === 'object') {
63
- const keys = Object.keys(data).slice(0, 5);
64
- process.stderr.write(` ${chalk.dim(`→ dict (${keys.join(', ')}${Object.keys(data).length > 5 ? '...' : ''})`)}\n`);
65
- }
66
- else if (typeof data === 'string') {
67
- const p = data.slice(0, 60).replace(/\n/g, '\\n');
68
- process.stderr.write(` ${chalk.dim(`→ "${p}${data.length > 60 ? '...' : ''}"`)}\n`);
69
- }
70
- else {
71
- process.stderr.write(` ${chalk.dim(`→ ${typeof data}`)}\n`);
72
- }
73
- }
74
- // Single URL fetch helper
75
- async function fetchSingle(page, url, method, queryParams, headers, args, data) {
76
- const renderedParams = {};
77
- for (const [k, v] of Object.entries(queryParams))
78
- renderedParams[k] = String(render(v, { args, data }));
79
- const renderedHeaders = {};
80
- for (const [k, v] of Object.entries(headers))
81
- renderedHeaders[k] = String(render(v, { args, data }));
82
- let finalUrl = url;
83
- if (Object.keys(renderedParams).length > 0) {
84
- const qs = new URLSearchParams(renderedParams).toString();
85
- finalUrl = `${finalUrl}${finalUrl.includes('?') ? '&' : '?'}${qs}`;
86
- }
87
- if (page === null) {
88
- const resp = await fetch(finalUrl, { method: method.toUpperCase(), headers: renderedHeaders });
89
- return resp.json();
90
- }
91
- const headersJs = JSON.stringify(renderedHeaders);
92
- const escapedUrl = finalUrl.replace(/"/g, '\\"');
93
- return page.evaluate(`
94
- async () => {
95
- const resp = await fetch("${escapedUrl}", {
96
- method: "${method}", headers: ${headersJs}, credentials: "include"
97
- });
98
- return await resp.json();
99
- }
100
- `);
101
- }
102
- async function executeStep(page, op, params, data, args) {
103
- switch (op) {
104
- case 'navigate': {
105
- const url = render(params, { args, data });
106
- await page.goto(String(url));
107
- return data;
108
- }
109
- case 'fetch': {
110
- const urlOrObj = typeof params === 'string' ? params : (params?.url ?? '');
111
- const method = params?.method ?? 'GET';
112
- const queryParams = params?.params ?? {};
113
- const headers = params?.headers ?? {};
114
- const urlTemplate = String(urlOrObj);
115
- // Per-item fetch when data is array and URL references item
116
- if (Array.isArray(data) && urlTemplate.includes('item')) {
117
- const results = [];
118
- for (let i = 0; i < data.length; i++) {
119
- const itemUrl = String(render(urlTemplate, { args, data, item: data[i], index: i }));
120
- results.push(await fetchSingle(page, itemUrl, method, queryParams, headers, args, data));
121
- }
122
- return results;
123
- }
124
- const url = render(urlOrObj, { args, data });
125
- return fetchSingle(page, String(url), method, queryParams, headers, args, data);
126
- }
127
- case 'select': {
128
- const pathStr = String(render(params, { args, data }));
129
- if (data && typeof data === 'object') {
130
- let current = data;
131
- for (const part of pathStr.split('.')) {
132
- if (current && typeof current === 'object' && !Array.isArray(current))
133
- current = current[part];
134
- else if (Array.isArray(current) && /^\d+$/.test(part))
135
- current = current[parseInt(part, 10)];
136
- else
137
- return null;
138
- }
139
- return current;
140
- }
141
- return data;
142
- }
143
- case 'evaluate': {
144
- const js = String(render(params, { args, data }));
145
- let result = await page.evaluate(normalizeEvaluateSource(js));
146
- // MCP may return JSON as a string — auto-parse it
147
- if (typeof result === 'string') {
148
- const trimmed = result.trim();
149
- if ((trimmed.startsWith('[') && trimmed.endsWith(']')) || (trimmed.startsWith('{') && trimmed.endsWith('}'))) {
150
- try {
151
- result = JSON.parse(trimmed);
152
- }
153
- catch { }
154
- }
155
- }
156
- return result;
157
- }
158
- case 'snapshot': {
159
- const opts = (typeof params === 'object' && params) ? params : {};
160
- return page.snapshot({ interactive: opts.interactive ?? false, compact: opts.compact ?? false, maxDepth: opts.max_depth, raw: opts.raw ?? false });
161
- }
162
- case 'click': {
163
- await page.click(String(render(params, { args, data })).replace(/^@/, ''));
164
- return data;
165
- }
166
- case 'type': {
167
- if (typeof params === 'object' && params) {
168
- const ref = String(render(params.ref ?? '', { args, data })).replace(/^@/, '');
169
- const text = String(render(params.text ?? '', { args, data }));
170
- await page.typeText(ref, text);
171
- if (params.submit)
172
- await page.pressKey('Enter');
173
- }
174
- return data;
175
- }
176
- case 'wait': {
177
- if (typeof params === 'number')
178
- await page.wait(params);
179
- else if (typeof params === 'object' && params) {
180
- if ('text' in params) {
181
- const timeout = params.timeout ?? 10;
182
- const start = Date.now();
183
- while ((Date.now() - start) / 1000 < timeout) {
184
- const snap = await page.snapshot({ raw: true });
185
- if (typeof snap === 'string' && snap.includes(params.text))
186
- break;
187
- await page.wait(0.5);
188
- }
189
- }
190
- else if ('time' in params)
191
- await page.wait(Number(params.time));
192
- }
193
- else if (typeof params === 'string')
194
- await page.wait(Number(render(params, { args, data })));
195
- return data;
196
- }
197
- case 'press': {
198
- await page.pressKey(String(render(params, { args, data })));
199
- return data;
200
- }
201
- case 'map': {
202
- if (!data || typeof data !== 'object')
203
- return data;
204
- let items = Array.isArray(data) ? data : [data];
205
- if (!Array.isArray(data) && typeof data === 'object' && 'data' in data)
206
- items = data.data;
207
- const result = [];
208
- for (let i = 0; i < items.length; i++) {
209
- const item = items[i];
210
- const row = {};
211
- for (const [key, template] of Object.entries(params))
212
- row[key] = render(template, { args, data, item, index: i });
213
- result.push(row);
214
- }
215
- return result;
216
- }
217
- case 'filter': {
218
- if (!Array.isArray(data))
219
- return data;
220
- return data.filter((item, i) => evalExpr(String(params), { args, item, index: i }));
221
- }
222
- case 'sort': {
223
- if (!Array.isArray(data))
224
- return data;
225
- const key = typeof params === 'object' ? (params.by ?? '') : String(params);
226
- const reverse = typeof params === 'object' ? params.order === 'desc' : false;
227
- return [...data].sort((a, b) => { const va = a[key] ?? ''; const vb = b[key] ?? ''; const cmp = va < vb ? -1 : va > vb ? 1 : 0; return reverse ? -cmp : cmp; });
228
- }
229
- case 'limit': {
230
- if (!Array.isArray(data))
231
- return data;
232
- return data.slice(0, Number(render(params, { args, data })));
233
- }
234
- case 'intercept': {
235
- // Declarative XHR interception step
236
- // Usage:
237
- // intercept:
238
- // trigger: "navigate:https://..." | "evaluate:store.note.fetch()" | "click:ref"
239
- // capture: "api/pattern" # URL substring to match
240
- // timeout: 5 # seconds to wait for matching request
241
- // select: "data.items" # optional: extract sub-path from response
242
- const cfg = typeof params === 'object' ? params : {};
243
- const trigger = cfg.trigger ?? '';
244
- const capturePattern = cfg.capture ?? '';
245
- const timeout = cfg.timeout ?? 8;
246
- const selectPath = cfg.select ?? null;
247
- if (!capturePattern)
248
- return data;
249
- // Step 1: Execute the trigger action
250
- if (trigger.startsWith('navigate:')) {
251
- const url = render(trigger.slice('navigate:'.length), { args, data });
252
- await page.goto(String(url));
253
- }
254
- else if (trigger.startsWith('evaluate:')) {
255
- const js = trigger.slice('evaluate:'.length);
256
- await page.evaluate(normalizeEvaluateSource(render(js, { args, data })));
257
- }
258
- else if (trigger.startsWith('click:')) {
259
- const ref = render(trigger.slice('click:'.length), { args, data });
260
- await page.click(String(ref).replace(/^@/, ''));
261
- }
262
- else if (trigger === 'scroll') {
263
- await page.scroll('down');
264
- }
265
- // Step 2: Wait a bit for network requests to fire
266
- await page.wait(Math.min(timeout, 3));
267
- // Step 3: Get network requests and find matching ones
268
- const rawNetwork = await page.networkRequests(false);
269
- const matchingResponses = [];
270
- if (typeof rawNetwork === 'string') {
271
- // Parse the network output to find matching URLs
272
- const lines = rawNetwork.split('\n');
273
- for (const line of lines) {
274
- const match = line.match(/\[?(GET|POST)\]?\s+(\S+)\s*(?:=>|→)\s*\[?(\d+)\]?/i);
275
- if (match) {
276
- const [, method, url, status] = match;
277
- if (url.includes(capturePattern) && status === '200') {
278
- // Re-fetch the matching URL to get the response body
279
- try {
280
- const body = await page.evaluate(`
281
- async () => {
282
- try {
283
- const resp = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
284
- if (!resp.ok) return null;
285
- return await resp.json();
286
- } catch { return null; }
287
- }
288
- `);
289
- if (body)
290
- matchingResponses.push(body);
291
- }
292
- catch { }
293
- }
294
- }
295
- }
296
- }
297
- // Step 4: Select from response if specified
298
- let result = matchingResponses.length === 1 ? matchingResponses[0] :
299
- matchingResponses.length > 1 ? matchingResponses : data;
300
- if (selectPath && result) {
301
- let current = result;
302
- for (const part of String(selectPath).split('.')) {
303
- if (current && typeof current === 'object' && !Array.isArray(current)) {
304
- current = current[part];
305
- }
306
- else
307
- break;
308
- }
309
- result = current ?? result;
310
- }
311
- return result;
312
- }
313
- case 'tap': {
314
- // ── Declarative Store Action Bridge ──────────────────────────────────
315
- // Usage:
316
- // tap:
317
- // store: feed # Pinia/Vuex store name
318
- // action: fetchFeeds # Store action to call
319
- // args: [] # Optional args to pass to action
320
- // capture: homefeed # URL pattern to capture response
321
- // timeout: 5 # Seconds to wait for network (default: 5)
322
- // select: data.items # Optional: extract sub-path from response
323
- // framework: pinia # Optional: pinia | vuex (auto-detected if omitted)
324
- //
325
- // Generates a self-contained IIFE that:
326
- // 1. Injects fetch + XHR dual interception proxy
327
- // 2. Finds the Pinia/Vuex store and calls the action
328
- // 3. Captures the response matching the URL pattern
329
- // 4. Auto-cleans up interception in finally block
330
- // 5. Returns the captured data (optionally sub-selected)
331
- const cfg = typeof params === 'object' ? params : {};
332
- const storeName = String(render(cfg.store ?? '', { args, data }));
333
- const actionName = String(render(cfg.action ?? '', { args, data }));
334
- const capturePattern = String(render(cfg.capture ?? '', { args, data }));
335
- const timeout = cfg.timeout ?? 5;
336
- const selectPath = cfg.select ? String(render(cfg.select, { args, data })) : null;
337
- const framework = cfg.framework ?? null; // auto-detect if null
338
- const actionArgs = cfg.args ?? [];
339
- if (!storeName || !actionName)
340
- throw new Error('tap: store and action are required');
341
- // Build select chain for the captured response
342
- const selectChain = selectPath
343
- ? selectPath.split('.').map((p) => `?.[${JSON.stringify(p)}]`).join('')
344
- : '';
345
- // Serialize action arguments
346
- const actionArgsRendered = actionArgs.map((a) => {
347
- const rendered = render(a, { args, data });
348
- return JSON.stringify(rendered);
349
- });
350
- const actionCall = actionArgsRendered.length
351
- ? `store[${JSON.stringify(actionName)}](${actionArgsRendered.join(', ')})`
352
- : `store[${JSON.stringify(actionName)}]()`;
353
- const js = `
354
- async () => {
355
- // ── 1. Setup capture proxy (fetch + XHR dual interception) ──
356
- let captured = null;
357
- const capturePattern = ${JSON.stringify(capturePattern)};
358
-
359
- // Intercept fetch API
360
- const origFetch = window.fetch;
361
- window.fetch = async function(...fetchArgs) {
362
- const resp = await origFetch.apply(this, fetchArgs);
363
- try {
364
- const url = typeof fetchArgs[0] === 'string' ? fetchArgs[0]
365
- : fetchArgs[0] instanceof Request ? fetchArgs[0].url : String(fetchArgs[0]);
366
- if (capturePattern && url.includes(capturePattern) && !captured) {
367
- try { captured = await resp.clone().json(); } catch {}
368
- }
369
- } catch {}
370
- return resp;
371
- };
372
-
373
- // Intercept XMLHttpRequest
374
- const origXhrOpen = XMLHttpRequest.prototype.open;
375
- const origXhrSend = XMLHttpRequest.prototype.send;
376
- XMLHttpRequest.prototype.open = function(method, url) {
377
- this.__tapUrl = String(url);
378
- return origXhrOpen.apply(this, arguments);
379
- };
380
- XMLHttpRequest.prototype.send = function(body) {
381
- if (capturePattern && this.__tapUrl?.includes(capturePattern)) {
382
- const xhr = this;
383
- const origHandler = xhr.onreadystatechange;
384
- xhr.onreadystatechange = function() {
385
- if (xhr.readyState === 4 && !captured) {
386
- try { captured = JSON.parse(xhr.responseText); } catch {}
387
- }
388
- if (origHandler) origHandler.apply(this, arguments);
389
- };
390
- // Also handle onload
391
- const origOnload = xhr.onload;
392
- xhr.onload = function() {
393
- if (!captured) { try { captured = JSON.parse(xhr.responseText); } catch {} }
394
- if (origOnload) origOnload.apply(this, arguments);
395
- };
396
- }
397
- return origXhrSend.apply(this, arguments);
398
- };
399
-
400
- try {
401
- // ── 2. Find store ──
402
- let store = null;
403
- const storeName = ${JSON.stringify(storeName)};
404
- const fw = ${JSON.stringify(framework)};
405
-
406
- // Auto-detect framework if not specified
407
- const app = document.querySelector('#app');
408
- if (!fw || fw === 'pinia') {
409
- // Try Pinia (Vue 3)
410
- try {
411
- const pinia = app?.__vue_app__?.config?.globalProperties?.$pinia;
412
- if (pinia?._s) store = pinia._s.get(storeName);
413
- } catch {}
414
- }
415
- if (!store && (!fw || fw === 'vuex')) {
416
- // Try Vuex (Vue 2/3)
417
- try {
418
- const vuexStore = app?.__vue_app__?.config?.globalProperties?.$store
419
- ?? app?.__vue__?.$store;
420
- if (vuexStore) {
421
- // Vuex doesn't have named stores like Pinia, dispatch action
422
- store = { [${JSON.stringify(actionName)}]: (...a) => vuexStore.dispatch(storeName + '/' + ${JSON.stringify(actionName)}, ...a) };
423
- }
424
- } catch {}
425
- }
426
-
427
- if (!store) return { error: 'Store not found: ' + storeName, hint: 'Page may not be fully loaded or store name may be incorrect' };
428
- if (typeof store[${JSON.stringify(actionName)}] !== 'function') {
429
- return { error: 'Action not found: ' + ${JSON.stringify(actionName)} + ' on store ' + storeName,
430
- hint: 'Available: ' + Object.keys(store).filter(k => typeof store[k] === 'function' && !k.startsWith('$') && !k.startsWith('_')).join(', ') };
431
- }
432
-
433
- // ── 3. Call store action ──
434
- await ${actionCall};
435
-
436
- // ── 4. Wait for network response ──
437
- const deadline = Date.now() + ${timeout} * 1000;
438
- while (!captured && Date.now() < deadline) {
439
- await new Promise(r => setTimeout(r, 200));
440
- }
441
- } finally {
442
- // ── 5. Always restore originals ──
443
- window.fetch = origFetch;
444
- XMLHttpRequest.prototype.open = origXhrOpen;
445
- XMLHttpRequest.prototype.send = origXhrSend;
446
- }
447
-
448
- if (!captured) return { error: 'No matching response captured for pattern: ' + capturePattern };
449
- return captured${selectChain} ?? captured;
450
- }
451
- `;
452
- return page.evaluate(js);
453
- }
454
- default: return data;
455
- }
456
- }
457
- function render(template, ctx) {
458
- if (typeof template !== 'string')
459
- return template;
460
- const fullMatch = template.match(/^\$\{\{\s*(.*?)\s*\}\}$/);
461
- if (fullMatch)
462
- return evalExpr(fullMatch[1].trim(), ctx);
463
- return template.replace(/\$\{\{\s*(.*?)\s*\}\}/g, (_m, expr) => String(evalExpr(expr.trim(), ctx)));
464
- }
465
- function evalExpr(expr, ctx) {
466
- const args = ctx.args ?? {};
467
- const item = ctx.item ?? {};
468
- const data = ctx.data;
469
- const index = ctx.index ?? 0;
470
- // Default filter: args.limit | default(20)
471
- if (expr.includes('|') && expr.includes('default(')) {
472
- const [mainExpr, rest] = expr.split('|', 2);
473
- const defaultMatch = rest.match(/default\((.+?)\)/);
474
- const defaultVal = defaultMatch ? defaultMatch[1] : null;
475
- const result = resolvePath(mainExpr.trim(), { args, item, data, index });
476
- if (result === null || result === undefined) {
477
- if (defaultVal !== null) {
478
- const intVal = parseInt(defaultVal, 10);
479
- if (!isNaN(intVal) && String(intVal) === defaultVal.trim())
480
- return intVal;
481
- return defaultVal.replace(/^['"]|['"]$/g, '');
482
- }
483
- }
484
- return result;
485
- }
486
- // Arithmetic: index + 1
487
- const arithMatch = expr.match(/^([\w][\w.]*)\s*([+\-*/])\s*(\d+)$/);
488
- if (arithMatch) {
489
- const [, varName, op, numStr] = arithMatch;
490
- const val = resolvePath(varName, { args, item, data, index });
491
- if (val !== null && val !== undefined) {
492
- const numVal = Number(val);
493
- const num = Number(numStr);
494
- if (!isNaN(numVal)) {
495
- switch (op) {
496
- case '+': return numVal + num;
497
- case '-': return numVal - num;
498
- case '*': return numVal * num;
499
- case '/': return num !== 0 ? numVal / num : 0;
500
- }
501
- }
502
- }
503
- }
504
- // JS-like fallback expression: item.tweetCount || 'N/A'
505
- const orMatch = expr.match(/^(.+?)\s*\|\|\s*(.+)$/);
506
- if (orMatch) {
507
- const left = evalExpr(orMatch[1].trim(), ctx);
508
- if (left)
509
- return left;
510
- const right = orMatch[2].trim();
511
- return right.replace(/^['"]|['"]$/g, '');
512
- }
513
- return resolvePath(expr, { args, item, data, index });
514
- }
515
- function resolvePath(pathStr, ctx) {
516
- const args = ctx.args ?? {};
517
- const item = ctx.item ?? {};
518
- const data = ctx.data;
519
- const index = ctx.index ?? 0;
520
- const parts = pathStr.split('.');
521
- const rootName = parts[0];
522
- let obj;
523
- let rest;
524
- if (rootName === 'args') {
525
- obj = args;
526
- rest = parts.slice(1);
527
- }
528
- else if (rootName === 'item') {
529
- obj = item;
530
- rest = parts.slice(1);
531
- }
532
- else if (rootName === 'data') {
533
- obj = data;
534
- rest = parts.slice(1);
535
- }
536
- else if (rootName === 'index')
537
- return index;
538
- else {
539
- obj = item;
540
- rest = parts;
541
- }
542
- for (const part of rest) {
543
- if (obj && typeof obj === 'object' && !Array.isArray(obj))
544
- obj = obj[part];
545
- else if (Array.isArray(obj) && /^\d+$/.test(part))
546
- obj = obj[parseInt(part, 10)];
547
- else
548
- return null;
549
- }
550
- return obj;
551
- }
7
+ export { executePipeline } from './pipeline/index.js';
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Core registry: Strategy enum, Arg/CliCommand interfaces, cli() registration.
3
3
  */
4
+ import type { IPage } from './types.js';
4
5
  export declare enum Strategy {
5
6
  PUBLIC = "public",
6
7
  COOKIE = "cookie",
@@ -25,7 +26,7 @@ export interface CliCommand {
25
26
  browser?: boolean;
26
27
  args: Arg[];
27
28
  columns?: string[];
28
- func?: (page: any, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
29
+ func?: (page: IPage | null, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
29
30
  pipeline?: any[];
30
31
  timeoutSeconds?: number;
31
32
  source?: string;
@@ -39,7 +40,7 @@ export interface CliOptions {
39
40
  browser?: boolean;
40
41
  args?: Arg[];
41
42
  columns?: string[];
42
- func?: (page: any, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
43
+ func?: (page: IPage | null, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
43
44
  pipeline?: any[];
44
45
  timeoutSeconds?: number;
45
46
  }
package/dist/runtime.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Runtime utilities: timeouts and browser session management.
3
3
  */
4
+ import type { IPage } from './types.js';
4
5
  export declare const DEFAULT_BROWSER_CONNECT_TIMEOUT: number;
5
6
  export declare const DEFAULT_BROWSER_COMMAND_TIMEOUT: number;
6
7
  export declare const DEFAULT_BROWSER_EXPLORE_TIMEOUT: number;
@@ -9,4 +10,4 @@ export declare function runWithTimeout<T>(promise: Promise<T>, opts: {
9
10
  timeout: number;
10
11
  label?: string;
11
12
  }): Promise<T>;
12
- export declare function browserSession<T>(BrowserFactory: new () => any, fn: (page: any) => Promise<T>): Promise<T>;
13
+ export declare function browserSession<T>(BrowserFactory: new () => any, fn: (page: IPage) => Promise<T>): Promise<T>;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Page interface: type-safe abstraction over Playwright MCP browser page.
3
+ *
4
+ * All pipeline steps and CLI adapters should use this interface
5
+ * instead of `any` for browser interactions.
6
+ */
7
+ export interface IPage {
8
+ goto(url: string): Promise<void>;
9
+ evaluate(js: string): Promise<any>;
10
+ snapshot(opts?: {
11
+ interactive?: boolean;
12
+ compact?: boolean;
13
+ maxDepth?: number;
14
+ raw?: boolean;
15
+ }): Promise<any>;
16
+ click(ref: string): Promise<void>;
17
+ typeText(ref: string, text: string): Promise<void>;
18
+ pressKey(key: string): Promise<void>;
19
+ wait(seconds: number): Promise<void>;
20
+ tabs(): Promise<any>;
21
+ closeTab(index?: number): Promise<void>;
22
+ newTab(): Promise<void>;
23
+ selectTab(index: number): Promise<void>;
24
+ networkRequests(includeStatic?: boolean): Promise<any>;
25
+ consoleMessages(level?: string): Promise<any>;
26
+ scroll(direction?: string, amount?: number): Promise<void>;
27
+ }
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Page interface: type-safe abstraction over Playwright MCP browser page.
3
+ *
4
+ * All pipeline steps and CLI adapters should use this interface
5
+ * instead of `any` for browser interactions.
6
+ */
7
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jackwener/opencli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -18,7 +18,9 @@
18
18
  "start": "node dist/main.js",
19
19
  "typecheck": "tsc --noEmit",
20
20
  "lint": "tsc --noEmit",
21
- "prepublishOnly": "npm run build"
21
+ "prepublishOnly": "npm run build",
22
+ "test": "vitest run",
23
+ "test:watch": "vitest"
22
24
  },
23
25
  "keywords": [
24
26
  "cli",
@@ -44,6 +46,7 @@
44
46
  "@types/js-yaml": "^4.0.9",
45
47
  "@types/node": "^22.13.10",
46
48
  "tsx": "^4.19.3",
47
- "typescript": "^5.8.2"
49
+ "typescript": "^5.8.2",
50
+ "vitest": "^4.1.0"
48
51
  }
49
52
  }