@jackwener/opencli 1.7.6 → 1.7.7
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.
- package/README.md +17 -8
- package/README.zh-CN.md +14 -8
- package/cli-manifest.json +325 -11
- package/clis/51job/company.js +125 -0
- package/clis/51job/detail.js +108 -0
- package/clis/51job/hot.js +55 -0
- package/clis/51job/search.js +79 -0
- package/clis/51job/utils.js +302 -0
- package/clis/51job/utils.test.js +69 -0
- package/clis/bilibili/video.js +11 -4
- package/clis/bilibili/video.test.js +51 -0
- package/clis/chatgpt/image.js +1 -1
- package/clis/deepseek/ask.js +19 -13
- package/clis/deepseek/ask.test.js +93 -1
- package/clis/deepseek/utils.js +108 -23
- package/clis/deepseek/utils.test.js +109 -1
- package/clis/gemini/image.js +1 -1
- package/clis/instagram/download.js +1 -1
- package/clis/twitter/likes.js +3 -2
- package/clis/twitter/search.js +4 -2
- package/clis/twitter/search.test.js +4 -0
- package/clis/twitter/shared.js +28 -0
- package/clis/twitter/shared.test.js +96 -0
- package/clis/twitter/thread.js +3 -1
- package/clis/twitter/timeline.js +3 -2
- package/clis/twitter/tweets.js +3 -2
- package/clis/twitter/tweets.test.js +1 -1
- package/clis/web/read.js +25 -5
- package/clis/web/read.test.js +76 -0
- package/clis/weread/ai-outline.js +170 -0
- package/clis/weread/ai-outline.test.js +83 -0
- package/clis/weread/book.js +57 -44
- package/clis/weread/commands.test.js +24 -0
- package/clis/xiaoyuzhou/podcast-episodes.js +2 -2
- package/clis/xiaoyuzhou/podcast-episodes.test.js +78 -0
- package/dist/src/browser/analyze.d.ts +103 -0
- package/dist/src/browser/analyze.js +230 -0
- package/dist/src/browser/analyze.test.d.ts +1 -0
- package/dist/src/browser/analyze.test.js +164 -0
- package/dist/src/browser/article-extract.d.ts +57 -0
- package/dist/src/browser/article-extract.e2e.test.d.ts +1 -0
- package/dist/src/browser/article-extract.e2e.test.js +105 -0
- package/dist/src/browser/article-extract.js +169 -0
- package/dist/src/browser/article-extract.test.d.ts +1 -0
- package/dist/src/browser/article-extract.test.js +94 -0
- package/dist/src/browser/cdp.js +11 -2
- package/dist/src/browser/verify-fixture.d.ts +59 -0
- package/dist/src/browser/verify-fixture.js +213 -0
- package/dist/src/browser/verify-fixture.test.d.ts +1 -0
- package/dist/src/browser/verify-fixture.test.js +161 -0
- package/dist/src/cli.d.ts +32 -0
- package/dist/src/cli.js +333 -43
- package/dist/src/cli.test.js +257 -1
- package/dist/src/daemon.d.ts +3 -2
- package/dist/src/daemon.js +16 -4
- package/dist/src/daemon.test.d.ts +1 -0
- package/dist/src/daemon.test.js +19 -0
- package/dist/src/download/article-download.d.ts +12 -0
- package/dist/src/download/article-download.js +141 -17
- package/dist/src/download/article-download.test.js +196 -0
- package/dist/src/download/index.js +73 -86
- package/dist/src/errors.js +4 -2
- package/dist/src/errors.test.js +13 -0
- package/dist/src/launcher.d.ts +1 -1
- package/dist/src/launcher.js +3 -3
- package/dist/src/output.js +1 -1
- package/dist/src/output.test.js +6 -0
- package/package.json +5 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { CliError } from '@jackwener/opencli/errors';
|
|
3
|
+
import { parseCompanyJobCard, pageFetchJson, resolveCity } from './utils.js';
|
|
4
|
+
|
|
5
|
+
describe('51job resolveCity', () => {
|
|
6
|
+
it('maps known city names and explicit national scope', () => {
|
|
7
|
+
expect(resolveCity('杭州')).toBe('080200');
|
|
8
|
+
expect(resolveCity('all')).toBe('000000');
|
|
9
|
+
expect(resolveCity('000000')).toBe('000000');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('rejects unknown non-empty inputs instead of silently widening to 全国', () => {
|
|
13
|
+
expect(() => resolveCity('杭州z')).toThrowError(CliError);
|
|
14
|
+
expect(() => resolveCity('杭州z')).toThrow(/Unknown city\/area/);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('51job pageFetchJson', () => {
|
|
19
|
+
it('detects WAF challenge HTML and throws ANTI_BOT', async () => {
|
|
20
|
+
const page = {
|
|
21
|
+
evaluate: vi.fn().mockResolvedValue({
|
|
22
|
+
ok: true,
|
|
23
|
+
status: 200,
|
|
24
|
+
text: '<html><title>slider</title></html>',
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
await expect(pageFetchJson(page, 'https://we.51job.com/api/job/search-pc')).rejects.toMatchObject({
|
|
29
|
+
code: 'ANTI_BOT',
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('51job parseCompanyJobCard', () => {
|
|
35
|
+
it('parses sensorsdata JSON into a stable row fragment', () => {
|
|
36
|
+
const row = parseCompanyJobCard({
|
|
37
|
+
href: 'https://jobs.51job.com/shanghai/123456789.html',
|
|
38
|
+
sensorsdata: JSON.stringify({
|
|
39
|
+
jobId: '123456789',
|
|
40
|
+
jobTitle: 'Senior Engineer',
|
|
41
|
+
jobSalary: '20-30K',
|
|
42
|
+
jobArea: '上海',
|
|
43
|
+
jobYear: '3-5年',
|
|
44
|
+
jobDegree: '本科',
|
|
45
|
+
funcType: '后端开发',
|
|
46
|
+
jobTime: '04-22',
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(row).toEqual({
|
|
51
|
+
jobId: '123456789',
|
|
52
|
+
title: 'Senior Engineer',
|
|
53
|
+
salary: '20-30K',
|
|
54
|
+
city: '上海',
|
|
55
|
+
workYear: '3-5年',
|
|
56
|
+
degree: '本科',
|
|
57
|
+
funcType: '后端开发',
|
|
58
|
+
issueDate: '04-22',
|
|
59
|
+
url: 'https://jobs.51job.com/shanghai/123456789.html',
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('returns null on malformed sensorsdata', () => {
|
|
64
|
+
expect(parseCompanyJobCard({
|
|
65
|
+
href: 'https://jobs.51job.com/shanghai/123456789.html',
|
|
66
|
+
sensorsdata: '{bad json}',
|
|
67
|
+
})).toBeNull();
|
|
68
|
+
});
|
|
69
|
+
});
|
package/clis/bilibili/video.js
CHANGED
|
@@ -15,7 +15,16 @@ cli({
|
|
|
15
15
|
if (!page) {
|
|
16
16
|
throw new CommandExecutionError('Browser session required for bilibili video');
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
// Resolve BV ID from three advertised input forms:
|
|
20
|
+
// 1. Bare "BV..." id
|
|
21
|
+
// 2. Full bilibili.com/video/<BV>... URL (with or without query string / www / m.)
|
|
22
|
+
// 3. b23.tv short link (delegated to resolveBvid)
|
|
23
|
+
// resolveBvid() alone handles (1) and (3) but not (2), so we pre-extract
|
|
24
|
+
// from bilibili URLs before falling through.
|
|
25
|
+
const input = String(kwargs.bvid ?? '').trim();
|
|
26
|
+
const bilibiliUrlMatch = input.match(/bilibili\.com\/(?:video|bangumi\/play)\/(BV[A-Za-z0-9]+)/i);
|
|
27
|
+
const bvid = bilibiliUrlMatch ? bilibiliUrlMatch[1] : await resolveBvid(input);
|
|
19
28
|
|
|
20
29
|
// Navigate to video page first so subsequent api call shares a primed session.
|
|
21
30
|
await page.goto(`https://www.bilibili.com/video/${bvid}/`);
|
|
@@ -35,8 +44,6 @@ cli({
|
|
|
35
44
|
const dur = d.duration || 0;
|
|
36
45
|
const mm = Math.floor(dur / 60);
|
|
37
46
|
const ss = dur % 60;
|
|
38
|
-
const desc = (d.desc || '').replace(/\s+/g, ' ').trim();
|
|
39
|
-
const descTrunc = desc.length > 200 ? desc.slice(0, 200) + '…' : desc;
|
|
40
47
|
|
|
41
48
|
return [
|
|
42
49
|
{ field: 'bvid', value: d.bvid ?? '' },
|
|
@@ -55,7 +62,7 @@ cli({
|
|
|
55
62
|
{ field: 'share', value: String(stat.share ?? '') },
|
|
56
63
|
{ field: 'parts', value: String(d.videos ?? 1) },
|
|
57
64
|
{ field: 'thumbnail', value: d.pic ?? '' },
|
|
58
|
-
{ field: 'description', value:
|
|
65
|
+
{ field: 'description', value: d.desc ?? '' },
|
|
59
66
|
];
|
|
60
67
|
},
|
|
61
68
|
});
|
|
@@ -78,4 +78,55 @@ describe('bilibili video', () => {
|
|
|
78
78
|
(err) => err instanceof CommandExecutionError && /啥都木有|-404/.test(err.message),
|
|
79
79
|
);
|
|
80
80
|
});
|
|
81
|
+
|
|
82
|
+
it('extracts BV ID from full bilibili.com URL input', async () => {
|
|
83
|
+
mockApiGet.mockResolvedValueOnce({
|
|
84
|
+
code: 0,
|
|
85
|
+
data: { bvid: 'BV1xx411c7mD', stat: {}, owner: {}, desc: '' },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
await command.func(page, { bvid: 'https://www.bilibili.com/video/BV1xx411c7mD/' });
|
|
89
|
+
|
|
90
|
+
expect(page.goto).toHaveBeenCalledWith('https://www.bilibili.com/video/BV1xx411c7mD/');
|
|
91
|
+
expect(mockApiGet).toHaveBeenCalledWith(page, '/x/web-interface/view', { params: { bvid: 'BV1xx411c7mD' } });
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('extracts BV ID from bilibili URL with trailing query string', async () => {
|
|
95
|
+
mockApiGet.mockResolvedValueOnce({
|
|
96
|
+
code: 0,
|
|
97
|
+
data: { bvid: 'BV1Je9EBnEha', stat: {}, owner: {}, desc: '' },
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
await command.func(page, {
|
|
101
|
+
bvid: 'https://www.bilibili.com/video/BV1Je9EBnEha/?spm_id_from=333.1007&vd_source=abc',
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(mockApiGet).toHaveBeenCalledWith(page, '/x/web-interface/view', { params: { bvid: 'BV1Je9EBnEha' } });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('extracts BV ID from m.bilibili.com mobile URL', async () => {
|
|
108
|
+
mockApiGet.mockResolvedValueOnce({
|
|
109
|
+
code: 0,
|
|
110
|
+
data: { bvid: 'BV1xx411c7mD', stat: {}, owner: {}, desc: '' },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
await command.func(page, { bvid: 'https://m.bilibili.com/video/BV1xx411c7mD' });
|
|
114
|
+
|
|
115
|
+
expect(mockApiGet).toHaveBeenCalledWith(page, '/x/web-interface/view', { params: { bvid: 'BV1xx411c7mD' } });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('returns full description without truncation or whitespace collapse', async () => {
|
|
119
|
+
const longDesc = '第一行描述\n\n第二段,有多个空格 和换行\n\n' + 'x'.repeat(500);
|
|
120
|
+
mockApiGet.mockResolvedValueOnce({
|
|
121
|
+
code: 0,
|
|
122
|
+
data: { bvid: 'BV1xx411c7mD', stat: {}, owner: {}, desc: longDesc },
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const rows = await command.func(page, { bvid: 'BV1xx411c7mD' });
|
|
126
|
+
const byField = Object.fromEntries(rows.map((r) => [r.field, r.value]));
|
|
127
|
+
// JSON/YAML consumers must receive the complete description verbatim,
|
|
128
|
+
// including original whitespace and length > 200 chars.
|
|
129
|
+
expect(byField.description).toBe(longDesc);
|
|
130
|
+
expect(byField.description.length).toBeGreaterThan(200);
|
|
131
|
+
});
|
|
81
132
|
});
|
package/clis/chatgpt/image.js
CHANGED
|
@@ -41,7 +41,7 @@ export const imageCommand = cli({
|
|
|
41
41
|
timeoutSeconds: 240,
|
|
42
42
|
args: [
|
|
43
43
|
{ name: 'prompt', positional: true, required: true, help: 'Image prompt to send to ChatGPT' },
|
|
44
|
-
{ name: 'op', default:
|
|
44
|
+
{ name: 'op', default: '~/Pictures/chatgpt', help: 'Output directory' },
|
|
45
45
|
{ name: 'sd', type: 'boolean', default: false, help: 'Skip download shorthand; only show ChatGPT link' },
|
|
46
46
|
],
|
|
47
47
|
columns: ['status', 'file', 'link'],
|
package/clis/deepseek/ask.js
CHANGED
|
@@ -23,7 +23,7 @@ export const askCommand = cli({
|
|
|
23
23
|
{ name: 'search', type: 'boolean', default: false, help: 'Enable web search' },
|
|
24
24
|
{ name: 'file', help: 'Attach a file (PDF, image, text) with the prompt' },
|
|
25
25
|
],
|
|
26
|
-
columns:
|
|
26
|
+
// columns omitted: derived from row keys so non-think output shows only 'response'
|
|
27
27
|
|
|
28
28
|
func: async (page, kwargs) => {
|
|
29
29
|
const prompt = kwargs.prompt;
|
|
@@ -45,19 +45,19 @@ export const askCommand = cli({
|
|
|
45
45
|
if (!modelResult?.ok) {
|
|
46
46
|
throw new CommandExecutionError(`Could not switch to ${wantModel} model`);
|
|
47
47
|
}
|
|
48
|
-
if (modelResult
|
|
48
|
+
if (modelResult?.toggled) await page.wait(0.5);
|
|
49
49
|
|
|
50
50
|
const thinkResult = await withRetry(() => setFeature(page, 'DeepThink', wantThink));
|
|
51
|
-
if (!thinkResult?.ok) {
|
|
52
|
-
throw new CommandExecutionError('Could not
|
|
51
|
+
if (!thinkResult?.ok && wantThink) {
|
|
52
|
+
throw new CommandExecutionError('Could not enable DeepThink');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
const searchResult = await withRetry(() => setFeature(page, 'Search', wantSearch));
|
|
56
|
-
if (!searchResult?.ok) {
|
|
57
|
-
throw new CommandExecutionError('Could not
|
|
56
|
+
if (!searchResult?.ok && wantSearch) {
|
|
57
|
+
throw new CommandExecutionError('Could not enable Search');
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
if (thinkResult
|
|
60
|
+
if (thinkResult?.toggled || searchResult?.toggled) await page.wait(0.5);
|
|
61
61
|
|
|
62
62
|
if (kwargs.file) {
|
|
63
63
|
const baseline = await withRetry(() => getBubbleCount(page));
|
|
@@ -71,11 +71,14 @@ export const askCommand = cli({
|
|
|
71
71
|
if (!String(err?.message || err).includes('Promise was collected')) throw err;
|
|
72
72
|
}
|
|
73
73
|
await page.wait(3);
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
74
|
+
const result = await waitForResponse(page, baseline, prompt, timeoutMs, wantThink);
|
|
75
|
+
if (!result) {
|
|
76
76
|
return [{ response: `[NO RESPONSE] No reply within ${kwargs.timeout}s.` }];
|
|
77
77
|
}
|
|
78
|
-
|
|
78
|
+
if (wantThink && typeof result === 'object' && result.response !== undefined) {
|
|
79
|
+
return [result];
|
|
80
|
+
}
|
|
81
|
+
return [{ response: result }];
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
const baseline = await withRetry(() => getBubbleCount(page));
|
|
@@ -84,11 +87,14 @@ export const askCommand = cli({
|
|
|
84
87
|
throw new CommandExecutionError(sendResult?.reason || 'Failed to send message');
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
90
|
+
const result = await waitForResponse(page, baseline, prompt, timeoutMs, wantThink);
|
|
91
|
+
if (!result) {
|
|
89
92
|
return [{ response: `[NO RESPONSE] No reply within ${kwargs.timeout}s.` }];
|
|
90
93
|
}
|
|
91
94
|
|
|
92
|
-
|
|
95
|
+
if (wantThink && typeof result === 'object' && result.response !== undefined) {
|
|
96
|
+
return [result];
|
|
97
|
+
}
|
|
98
|
+
return [{ response: result }];
|
|
93
99
|
},
|
|
94
100
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
2
3
|
|
|
3
4
|
const {
|
|
4
5
|
mockEnsureOnDeepSeek,
|
|
@@ -68,6 +69,97 @@ describe('deepseek ask --file', () => {
|
|
|
68
69
|
expect(rows).toEqual([{ response: 'new reply' }]);
|
|
69
70
|
expect(mockGetBubbleCount).toHaveBeenCalledTimes(1);
|
|
70
71
|
expect(mockSendWithFile).toHaveBeenCalledWith(page, './report.pdf', 'summarize this');
|
|
71
|
-
expect(mockWaitForResponse).toHaveBeenCalledWith(page, 7, 'summarize this', 120000);
|
|
72
|
+
expect(mockWaitForResponse).toHaveBeenCalledWith(page, 7, 'summarize this', 120000, false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('still fails when explicit instant model selection cannot be verified', async () => {
|
|
76
|
+
mockSelectModel.mockResolvedValue({ ok: false });
|
|
77
|
+
|
|
78
|
+
await expect(askCommand.func(page, {
|
|
79
|
+
prompt: 'summarize this',
|
|
80
|
+
timeout: 120,
|
|
81
|
+
new: false,
|
|
82
|
+
model: 'instant',
|
|
83
|
+
think: false,
|
|
84
|
+
search: false,
|
|
85
|
+
})).rejects.toThrow(new CommandExecutionError('Could not switch to instant model'));
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('deepseek ask --think', () => {
|
|
90
|
+
const page = {
|
|
91
|
+
wait: vi.fn().mockResolvedValue(undefined),
|
|
92
|
+
goto: vi.fn().mockResolvedValue(undefined),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
vi.clearAllMocks();
|
|
97
|
+
mockEnsureOnDeepSeek.mockResolvedValue(undefined);
|
|
98
|
+
mockSelectModel.mockResolvedValue({ ok: true, toggled: false });
|
|
99
|
+
mockSetFeature.mockResolvedValue({ ok: true, toggled: false });
|
|
100
|
+
mockSendMessage.mockResolvedValue({ ok: true });
|
|
101
|
+
mockGetBubbleCount.mockResolvedValue(5);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('returns separate thinking and response fields when --think is enabled', async () => {
|
|
105
|
+
mockWaitForResponse.mockResolvedValue({
|
|
106
|
+
response: 'The answer is 42.',
|
|
107
|
+
thinking: 'Let me analyze this...',
|
|
108
|
+
thinking_time: '2.5',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const rows = await askCommand.func(page, {
|
|
112
|
+
prompt: 'what is the answer?',
|
|
113
|
+
timeout: 120,
|
|
114
|
+
new: false,
|
|
115
|
+
model: 'instant',
|
|
116
|
+
think: true,
|
|
117
|
+
search: false,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
expect(rows).toEqual([{
|
|
121
|
+
response: 'The answer is 42.',
|
|
122
|
+
thinking: 'Let me analyze this...',
|
|
123
|
+
thinking_time: '2.5',
|
|
124
|
+
}]);
|
|
125
|
+
expect(mockWaitForResponse).toHaveBeenCalledWith(page, 5, 'what is the answer?', 120000, true);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('returns plain response when --think is disabled', async () => {
|
|
129
|
+
mockWaitForResponse.mockResolvedValue('The answer is 42.');
|
|
130
|
+
|
|
131
|
+
const rows = await askCommand.func(page, {
|
|
132
|
+
prompt: 'what is the answer?',
|
|
133
|
+
timeout: 120,
|
|
134
|
+
new: false,
|
|
135
|
+
model: 'instant',
|
|
136
|
+
think: false,
|
|
137
|
+
search: false,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(rows).toEqual([{ response: 'The answer is 42.' }]);
|
|
141
|
+
expect(mockWaitForResponse).toHaveBeenCalledWith(page, 5, 'what is the answer?', 120000, false);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('does not declare static columns (derived from row keys)', () => {
|
|
145
|
+
// columns should be undefined so the renderer infers from row keys,
|
|
146
|
+
// avoiding empty trailing columns on non-think output.
|
|
147
|
+
expect(askCommand.columns).toBeUndefined();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('non-think rows only contain response key', async () => {
|
|
151
|
+
mockWaitForResponse.mockResolvedValue('Plain answer.');
|
|
152
|
+
|
|
153
|
+
const rows = await askCommand.func(page, {
|
|
154
|
+
prompt: 'hello',
|
|
155
|
+
timeout: 120,
|
|
156
|
+
new: false,
|
|
157
|
+
model: 'instant',
|
|
158
|
+
think: false,
|
|
159
|
+
search: false,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Row keys drive rendered columns; no thinking/thinking_time present.
|
|
163
|
+
expect(Object.keys(rows[0])).toEqual(['response']);
|
|
72
164
|
});
|
|
73
165
|
});
|
package/clis/deepseek/utils.js
CHANGED
|
@@ -38,31 +38,27 @@ export async function getPageState(page) {
|
|
|
38
38
|
|
|
39
39
|
export async function selectModel(page, modelName) {
|
|
40
40
|
return page.evaluate(`(() => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
return { ok: false };
|
|
41
|
+
var radios = document.querySelectorAll('div[role="radio"]');
|
|
42
|
+
if (radios.length === 0) return { ok: false };
|
|
43
|
+
var isFirst = '${modelName}'.toLowerCase() === 'instant';
|
|
44
|
+
if (!isFirst && radios.length < 2) return { ok: false };
|
|
45
|
+
var target = isFirst ? radios[0] : radios[radios.length - 1];
|
|
46
|
+
var alreadySelected = target.getAttribute('aria-checked') === 'true';
|
|
47
|
+
if (!alreadySelected) target.click();
|
|
48
|
+
return { ok: true, toggled: !alreadySelected };
|
|
51
49
|
})()`);
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
export async function setFeature(page, featureName, enabled) {
|
|
53
|
+
// Match by position: DeepThink is the first toggle, Search is the second
|
|
54
|
+
var index = featureName === 'DeepThink' ? 0 : 1;
|
|
55
55
|
return page.evaluate(`(() => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return { ok: true, toggled: ${enabled} !== isActive };
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return { ok: false };
|
|
56
|
+
var toggles = Array.from(document.querySelectorAll('.ds-toggle-button'));
|
|
57
|
+
var btn = toggles[${index}];
|
|
58
|
+
if (!btn) return { ok: false };
|
|
59
|
+
var isActive = btn.classList.contains('ds-toggle-button--selected');
|
|
60
|
+
if (${enabled} !== isActive) btn.click();
|
|
61
|
+
return { ok: true, toggled: ${enabled} !== isActive };
|
|
66
62
|
})()`);
|
|
67
63
|
}
|
|
68
64
|
|
|
@@ -101,7 +97,35 @@ export async function getBubbleCount(page) {
|
|
|
101
97
|
return count || 0;
|
|
102
98
|
}
|
|
103
99
|
|
|
104
|
-
|
|
100
|
+
// Parse thinking response using text as a fallback when DOM-level extraction
|
|
101
|
+
// is not available. Does NOT split on \n\n — that heuristic silently corrupts
|
|
102
|
+
// multi-paragraph thinking or multi-paragraph answers. Instead, everything
|
|
103
|
+
// after the header is treated as thinking content, and `response` stays empty
|
|
104
|
+
// until the caller provides a DOM-separated answer.
|
|
105
|
+
export function parseThinkingResponse(rawText) {
|
|
106
|
+
if (!rawText) return null;
|
|
107
|
+
|
|
108
|
+
// Match thinking header patterns: "Thought for X seconds" or "已思考(用时 X 秒)"
|
|
109
|
+
const thinkHeaderMatch = rawText.match(/^(Thought for ([\d.]+) seconds?|已思考(用时 ([\d.]+) 秒))\s*/);
|
|
110
|
+
|
|
111
|
+
if (!thinkHeaderMatch) {
|
|
112
|
+
// No thinking section found, return plain response
|
|
113
|
+
return { response: rawText, thinking: null, thinking_time: null };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const thinkingTime = thinkHeaderMatch[2] || thinkHeaderMatch[3];
|
|
117
|
+
const afterHeader = rawText.slice(thinkHeaderMatch[0].length);
|
|
118
|
+
|
|
119
|
+
// Treat everything after the header as thinking. The response will be
|
|
120
|
+
// populated by the DOM-level extraction in waitForResponse().
|
|
121
|
+
return {
|
|
122
|
+
response: '',
|
|
123
|
+
thinking: afterHeader.trim(),
|
|
124
|
+
thinking_time: thinkingTime,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function waitForResponse(page, baselineCount, prompt, timeoutMs, parseThinking = false) {
|
|
105
129
|
const startTime = Date.now();
|
|
106
130
|
let lastText = '';
|
|
107
131
|
let stableCount = 0;
|
|
@@ -114,7 +138,51 @@ export async function waitForResponse(page, baselineCount, prompt, timeoutMs) {
|
|
|
114
138
|
result = await page.evaluate(`(() => {
|
|
115
139
|
const bubbles = document.querySelectorAll('${MESSAGE_SELECTOR}');
|
|
116
140
|
const texts = Array.from(bubbles).map(b => (b.innerText || '').trim()).filter(Boolean);
|
|
117
|
-
|
|
141
|
+
var last = texts[texts.length - 1] || '';
|
|
142
|
+
|
|
143
|
+
// DOM-level thinking/response separation.
|
|
144
|
+
// DeepSeek renders thinking in a collapsible container with a
|
|
145
|
+
// distinct class (e.g. .ds-markdown--think or similar) and the
|
|
146
|
+
// final answer in the main .ds-markdown region. By querying
|
|
147
|
+
// these separately we avoid any text-heuristic split.
|
|
148
|
+
var thinkEl = null, answerEl = null, thinkTime = null;
|
|
149
|
+
if (${parseThinking} && bubbles.length > 0) {
|
|
150
|
+
var lastBubble = bubbles[bubbles.length - 1];
|
|
151
|
+
// Thinking container — DeepSeek uses various class names;
|
|
152
|
+
// try common selectors.
|
|
153
|
+
thinkEl = lastBubble.querySelector('.ds-markdown--think')
|
|
154
|
+
|| lastBubble.querySelector('[class*="think"]');
|
|
155
|
+
// Final answer container — the main markdown block that is
|
|
156
|
+
// NOT the thinking section.
|
|
157
|
+
var markdownEls = lastBubble.querySelectorAll('.ds-markdown');
|
|
158
|
+
for (var i = 0; i < markdownEls.length; i++) {
|
|
159
|
+
if (markdownEls[i] !== thinkEl
|
|
160
|
+
&& !(thinkEl && thinkEl.contains(markdownEls[i]))
|
|
161
|
+
&& !markdownEls[i].classList.contains('ds-markdown--think')) {
|
|
162
|
+
answerEl = markdownEls[i];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Thinking time from the toggle/header element
|
|
166
|
+
var timeEl = lastBubble.querySelector('[class*="think"] ~ *')
|
|
167
|
+
|| lastBubble.querySelector('.ds-thinking-header');
|
|
168
|
+
if (!timeEl) {
|
|
169
|
+
// Fallback: parse from raw text header
|
|
170
|
+
var m = last.match(/^(?:Thought for ([\\d.]+) seconds?|已思考(用时 ([\\d.]+) 秒))/);
|
|
171
|
+
if (m) thinkTime = m[1] || m[2];
|
|
172
|
+
} else {
|
|
173
|
+
var tm = (timeEl.textContent || '').match(/([\\d.]+)/);
|
|
174
|
+
if (tm) thinkTime = tm[1];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
count: texts.length,
|
|
180
|
+
last: last,
|
|
181
|
+
// DOM-separated fields (null when not available)
|
|
182
|
+
thinkText: thinkEl ? (thinkEl.innerText || '').trim() : null,
|
|
183
|
+
answerText: answerEl ? (answerEl.innerText || '').trim() : null,
|
|
184
|
+
thinkTime: thinkTime,
|
|
185
|
+
};
|
|
118
186
|
})()`);
|
|
119
187
|
} catch {
|
|
120
188
|
continue;
|
|
@@ -126,7 +194,21 @@ export async function waitForResponse(page, baselineCount, prompt, timeoutMs) {
|
|
|
126
194
|
if (candidate && result.count > baselineCount && candidate !== prompt.trim()) {
|
|
127
195
|
if (candidate === lastText) {
|
|
128
196
|
stableCount++;
|
|
129
|
-
if (stableCount >= 3)
|
|
197
|
+
if (stableCount >= 3) {
|
|
198
|
+
if (parseThinking) {
|
|
199
|
+
// Prefer DOM-level separation
|
|
200
|
+
if (result.thinkText != null || result.answerText != null) {
|
|
201
|
+
return {
|
|
202
|
+
thinking: result.thinkText || '',
|
|
203
|
+
response: result.answerText || '',
|
|
204
|
+
thinking_time: result.thinkTime || null,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
// Fallback to text-header parsing (no \n\n split)
|
|
208
|
+
return parseThinkingResponse(candidate);
|
|
209
|
+
}
|
|
210
|
+
return candidate;
|
|
211
|
+
}
|
|
130
212
|
} else {
|
|
131
213
|
stableCount = 0;
|
|
132
214
|
}
|
|
@@ -134,6 +216,9 @@ export async function waitForResponse(page, baselineCount, prompt, timeoutMs) {
|
|
|
134
216
|
}
|
|
135
217
|
}
|
|
136
218
|
|
|
219
|
+
if (parseThinking && lastText) {
|
|
220
|
+
return parseThinkingResponse(lastText);
|
|
221
|
+
}
|
|
137
222
|
return lastText || null;
|
|
138
223
|
}
|
|
139
224
|
|
|
@@ -2,7 +2,90 @@ import fs from 'node:fs';
|
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
-
import { sendWithFile } from './utils.js';
|
|
5
|
+
import { selectModel, sendWithFile, parseThinkingResponse } from './utils.js';
|
|
6
|
+
|
|
7
|
+
describe('deepseek parseThinkingResponse', () => {
|
|
8
|
+
it('returns plain response when no thinking header is present', () => {
|
|
9
|
+
const rawText = 'This is a regular response without thinking.';
|
|
10
|
+
const result = parseThinkingResponse(rawText);
|
|
11
|
+
|
|
12
|
+
expect(result).toEqual({
|
|
13
|
+
response: rawText,
|
|
14
|
+
thinking: null,
|
|
15
|
+
thinking_time: null,
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('parses English thinking header — all content after header is thinking', () => {
|
|
20
|
+
const rawText = 'Thought for 3.5 seconds\n\nLet me analyze this problem...\nFirst, I need to consider X.\nThen, Y.\n\nThe answer is 42.';
|
|
21
|
+
const result = parseThinkingResponse(rawText);
|
|
22
|
+
|
|
23
|
+
// Text-level parser no longer splits on \n\n; everything after header is thinking.
|
|
24
|
+
// DOM-level extraction in waitForResponse() handles the actual separation.
|
|
25
|
+
expect(result).toEqual({
|
|
26
|
+
response: '',
|
|
27
|
+
thinking: 'Let me analyze this problem...\nFirst, I need to consider X.\nThen, Y.\n\nThe answer is 42.',
|
|
28
|
+
thinking_time: '3.5',
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('parses Chinese thinking header — all content after header is thinking', () => {
|
|
33
|
+
const rawText = '已思考(用时 2.3 秒)\n\n让我分析这个问题...\n首先需要考虑X。\n然后是Y。\n\n答案是42。';
|
|
34
|
+
const result = parseThinkingResponse(rawText);
|
|
35
|
+
|
|
36
|
+
expect(result).toEqual({
|
|
37
|
+
response: '',
|
|
38
|
+
thinking: '让我分析这个问题...\n首先需要考虑X。\n然后是Y。\n\n答案是42。',
|
|
39
|
+
thinking_time: '2.3',
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('multi-paragraph thinking without final answer is not corrupted', () => {
|
|
44
|
+
const rawText = 'Thought for 1.2 seconds\n\nFirst paragraph.\n\nSecond paragraph.';
|
|
45
|
+
const result = parseThinkingResponse(rawText);
|
|
46
|
+
|
|
47
|
+
// Both paragraphs must stay in thinking; response is empty.
|
|
48
|
+
expect(result).toEqual({
|
|
49
|
+
response: '',
|
|
50
|
+
thinking: 'First paragraph.\n\nSecond paragraph.',
|
|
51
|
+
thinking_time: '1.2',
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('multi-paragraph final answer is not split by text parser', () => {
|
|
56
|
+
const rawText = 'Thought for 3 seconds\n\nreasoning\n\nAnswer para 1.\n\nAnswer para 2.';
|
|
57
|
+
const result = parseThinkingResponse(rawText);
|
|
58
|
+
|
|
59
|
+
// Text parser treats everything as thinking; DOM handles separation.
|
|
60
|
+
expect(result).toEqual({
|
|
61
|
+
response: '',
|
|
62
|
+
thinking: 'reasoning\n\nAnswer para 1.\n\nAnswer para 2.',
|
|
63
|
+
thinking_time: '3',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('handles thinking without final response', () => {
|
|
68
|
+
const rawText = 'Thought for 1.2 seconds\n\nThinking process here...';
|
|
69
|
+
const result = parseThinkingResponse(rawText);
|
|
70
|
+
|
|
71
|
+
expect(result).toEqual({
|
|
72
|
+
response: '',
|
|
73
|
+
thinking: 'Thinking process here...',
|
|
74
|
+
thinking_time: '1.2',
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('returns null for empty input', () => {
|
|
79
|
+
const result = parseThinkingResponse('');
|
|
80
|
+
expect(result).toBeNull();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('returns null for null input', () => {
|
|
84
|
+
const result = parseThinkingResponse(null);
|
|
85
|
+
expect(result).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
6
89
|
|
|
7
90
|
describe('deepseek sendWithFile', () => {
|
|
8
91
|
const tempDirs = [];
|
|
@@ -35,3 +118,28 @@ describe('deepseek sendWithFile', () => {
|
|
|
35
118
|
expect(page.setFileInput).toHaveBeenCalledWith([filePath], 'input[type="file"]');
|
|
36
119
|
});
|
|
37
120
|
});
|
|
121
|
+
|
|
122
|
+
describe('deepseek selectModel', () => {
|
|
123
|
+
afterEach(() => {
|
|
124
|
+
vi.restoreAllMocks();
|
|
125
|
+
delete global.document;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('fails expert selection when only one radio is present', async () => {
|
|
129
|
+
const instantRadio = {
|
|
130
|
+
getAttribute: vi.fn(() => 'true'),
|
|
131
|
+
click: vi.fn(),
|
|
132
|
+
};
|
|
133
|
+
global.document = {
|
|
134
|
+
querySelectorAll: vi.fn(() => [instantRadio]),
|
|
135
|
+
};
|
|
136
|
+
const page = {
|
|
137
|
+
evaluate: vi.fn(async (script) => eval(script)),
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const result = await selectModel(page, 'expert');
|
|
141
|
+
|
|
142
|
+
expect(result).toEqual({ ok: false });
|
|
143
|
+
expect(instantRadio.click).not.toHaveBeenCalled();
|
|
144
|
+
});
|
|
145
|
+
});
|
package/clis/gemini/image.js
CHANGED
|
@@ -57,7 +57,7 @@ export const imageCommand = cli({
|
|
|
57
57
|
{ name: 'prompt', positional: true, required: true, help: 'Image prompt to send to Gemini' },
|
|
58
58
|
{ name: 'rt', default: '1:1', help: 'Ratio shorthand for aspect ratio (1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3)' },
|
|
59
59
|
{ name: 'st', default: '', help: 'Style shorthand, e.g. anime, icon, watercolor' },
|
|
60
|
-
{ name: 'op', default:
|
|
60
|
+
{ name: 'op', default: '~/tmp/gemini-images', help: 'Output directory shorthand' },
|
|
61
61
|
{ name: 'sd', type: 'boolean', default: false, help: 'Skip download shorthand; only show Gemini page link' },
|
|
62
62
|
],
|
|
63
63
|
columns: ['status', 'file', 'link'],
|
|
@@ -202,7 +202,7 @@ cli({
|
|
|
202
202
|
navigateBefore: false,
|
|
203
203
|
args: [
|
|
204
204
|
{ name: 'url', positional: true, required: true, help: 'Instagram post / reel / tv URL' },
|
|
205
|
-
{ name: 'path', default:
|
|
205
|
+
{ name: 'path', default: '~/Downloads/Instagram', help: 'Download directory' },
|
|
206
206
|
],
|
|
207
207
|
func: async (page, kwargs) => {
|
|
208
208
|
const browserPage = ensurePage(page);
|