helixlife-v5-cli 1.2.5 → 1.2.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.
@@ -1,262 +1,262 @@
1
- /**
2
- * 综述工作台(jova)· 初稿校对页 · 章节校对 / AI 改写
3
- *
4
- * Agent 调用约定(严格遵守):
5
- *
6
- * 1) 用户只说「校对 <章节>」、未指明操作时:
7
- * action = null → 仅滚动定位 → 悬停 → 点「点击本段进入编辑校对」或「重新编辑」
8
- * → 返回 needsUserChoice,由 Agent 询问用户选「确认校对」或「AI 改写」
9
- * → 禁止自动点「确认校对」
10
- *
11
- * 2) 用户明确说「确认校对」:action = 'confirm'
12
- *
13
- * 3) 用户明确说「AI 改写」:
14
- * action = 'ai_rewrite' → 点「AI 改写」→(有额外需求则填写)→「一键重写」→ 等待生成
15
- * → 返回 needsReplaceChoice,提醒用户是否替换原文,禁止自动点「替换原文」
16
- *
17
- * 4) 用户明确同意替换原文:action = 'replace'
18
- *
19
- * 用法:
20
- * helixlife-v5-cli run-code --filename=scripts/workbench-jova-proofread-section.js
21
- */
22
- async page => {
23
- const intent = '校对 1. 乳腺癌分子分型体系及其分子特征';
24
- const action = null; // null | 'confirm' | 'ai_rewrite' | 'replace'
25
- const extraRequirement = ''; // 仅 ai_rewrite 时可选
26
-
27
- const main = page.locator('main');
28
-
29
- function parseIntent(raw) {
30
- const m = raw.match(/^校对\s*(.+)$/);
31
- return { sectionTitle: (m ? m[1] : raw).trim() };
32
- }
33
-
34
- const { sectionTitle } = parseIntent(intent);
35
-
36
- if (!page.url().includes('/workbench/jova/proofread')) {
37
- return {
38
- ok: false,
39
- reason: '当前不在综述工作台初稿校对页 /workbench/jova/proofread',
40
- intent,
41
- sectionTitle,
42
- };
43
- }
44
-
45
- async function dismissPopconfirmOnly() {
46
- const cancel = main.getByRole('button', { name: '取 消' });
47
- if ((await cancel.count()) > 0 && (await cancel.first().isVisible())) {
48
- await cancel.first().click();
49
- await page.waitForTimeout(300);
50
- }
51
- }
52
-
53
- function sectionHeading(title) {
54
- return main.getByRole('heading', { name: title, exact: true }).first();
55
- }
56
-
57
- function sectionBlock(title) {
58
- return sectionHeading(title).locator('xpath=ancestor::div[contains(@class,"group")][1]');
59
- }
60
-
61
- async function navigateToSection(title) {
62
- const toc = main.locator('[class*="cursor-pointer"]').getByText(title, { exact: true });
63
- if ((await toc.count()) > 0) {
64
- await toc.first().click({ timeout: 3000 }).catch(() => {});
65
- await page.waitForTimeout(500);
66
- }
67
- const heading = sectionHeading(title);
68
- if ((await heading.count()) === 0) {
69
- return { ok: false, reason: `未找到章节「${title}」` };
70
- }
71
- await heading.scrollIntoViewIfNeeded();
72
- await page.waitForTimeout(400);
73
- return { ok: true };
74
- }
75
-
76
- async function detectProofreadStatus(block) {
77
- const pending = block.getByText('请校对本段内容', { exact: true });
78
- const done = block.getByText('本段完成校对', { exact: true });
79
- if ((await pending.count()) > 0) return 'pending';
80
- if ((await done.count()) > 0) return 'done';
81
- return 'completed_or_editing';
82
- }
83
-
84
- async function clickEnterEdit(block) {
85
- const enterLabels = [
86
- '点击本段进入编辑校对',
87
- '点击本段进行校对',
88
- '点击本段进行编辑校对',
89
- ];
90
- for (const label of enterLabels) {
91
- const btn = block.getByText(label, { exact: true });
92
- if ((await btn.count()) > 0 && (await btn.first().isVisible())) {
93
- await btn.first().click();
94
- return true;
95
- }
96
- }
97
- return false;
98
- }
99
-
100
- async function openSectionEdit(title) {
101
- const nav = await navigateToSection(title);
102
- if (!nav.ok) return nav;
103
-
104
- const block = sectionBlock(title);
105
-
106
- if ((await block.getByRole('button', { name: '确认校对' }).count()) > 0) {
107
- return { ok: true, proofreadStatus: 'already_editing' };
108
- }
109
-
110
- const proofreadStatus = await detectProofreadStatus(block);
111
-
112
- if (proofreadStatus === 'pending') {
113
- await block.getByText('请校对本段内容', { exact: true }).first().hover();
114
- await page.waitForTimeout(600);
115
- if (!(await clickEnterEdit(block))) {
116
- return { ok: false, reason: '未找到「点击本段进入编辑校对」入口', proofreadStatus };
117
- }
118
- } else if (proofreadStatus === 'done') {
119
- await block.getByText('本段完成校对', { exact: true }).first().hover();
120
- await page.waitForTimeout(600);
121
- const reEdit = block.getByText('重新编辑', { exact: true });
122
- if ((await reEdit.count()) === 0 || !(await reEdit.first().isVisible())) {
123
- return { ok: false, reason: '未找到「重新编辑」入口', proofreadStatus };
124
- }
125
- await reEdit.first().click();
126
- } else {
127
- await block.hover();
128
- await page.waitForTimeout(600);
129
- const reEdit = block.getByText('重新编辑', { exact: true });
130
- if ((await reEdit.count()) > 0 && (await reEdit.first().isVisible())) {
131
- await reEdit.first().click();
132
- } else if (await clickEnterEdit(block)) {
133
- // ok
134
- } else {
135
- return { ok: false, reason: '无法进入编辑模式(未找到入口)', proofreadStatus };
136
- }
137
- }
138
-
139
- await block.getByRole('button', { name: '确认校对' }).waitFor({ state: 'visible', timeout: 10000 });
140
- await page.waitForTimeout(400);
141
- return { ok: true, proofreadStatus };
142
- }
143
-
144
- async function confirmProofread(title) {
145
- const block = sectionBlock(title);
146
- await block.getByRole('button', { name: '确认校对' }).click();
147
- await page.waitForTimeout(1000);
148
- return { ok: true };
149
- }
150
-
151
- async function waitReplaceReady(timeoutMs = 120000) {
152
- const replaceBtn = main.getByRole('button', { name: '替换原文' });
153
- await replaceBtn.waitFor({ state: 'visible', timeout: timeoutMs });
154
- const deadline = Date.now() + timeoutMs;
155
- while (Date.now() < deadline) {
156
- if (await replaceBtn.isEnabled()) return replaceBtn;
157
- await page.waitForTimeout(1000);
158
- }
159
- throw new Error('等待「替换原文」可点击超时');
160
- }
161
-
162
- async function aiRewriteGenerate(title, extra) {
163
- const block = sectionBlock(title);
164
- await block.getByRole('button', { name: /AI\s*改写/ }).click();
165
- await main.getByText('原文内容', { exact: true }).waitFor({ state: 'visible', timeout: 10000 });
166
-
167
- if (extra) {
168
- const extraBox = main.getByRole('textbox', { name: '额外需求' });
169
- if ((await extraBox.count()) > 0) await extraBox.fill(extra);
170
- }
171
-
172
- await main.getByRole('button', { name: '一键重写' }).click();
173
- await waitReplaceReady();
174
-
175
- return { ok: true, generated: true, replaceReady: true };
176
- }
177
-
178
- async function aiRewriteReplace() {
179
- const replaceBtn = main.getByRole('button', { name: '替换原文' });
180
- await replaceBtn.click();
181
- await main.getByText('是否将本次生成结果替换原文?', { exact: true }).waitFor({ state: 'visible', timeout: 10000 });
182
- await main.getByRole('button', { name: '确 定' }).click();
183
- await page.waitForTimeout(1000);
184
- return { ok: true, replaced: true };
185
- }
186
-
187
- await dismissPopconfirmOnly();
188
-
189
- const needOpenEdit = action === null || action === '' || action === undefined || action === 'confirm' || action === 'ai_rewrite';
190
- let opened = { ok: true, proofreadStatus: 'already_editing' };
191
-
192
- if (needOpenEdit && action !== 'replace') {
193
- opened = await openSectionEdit(sectionTitle);
194
- if (!opened.ok) return { ok: false, intent, sectionTitle, ...opened };
195
- }
196
-
197
- if (action === null || action === undefined || action === '') {
198
- return {
199
- ok: true,
200
- needsUserChoice: true,
201
- intent,
202
- sectionTitle,
203
- proofreadStatus: opened.proofreadStatus,
204
- availableActions: ['confirm', 'ai_rewrite'],
205
- message: '已进入该段编辑模式。请选择:① 确认校对(直接完成本段校对)② AI 改写(AI 重新生成内容)',
206
- url: page.url(),
207
- };
208
- }
209
-
210
- if (action === 'confirm') {
211
- await confirmProofread(sectionTitle);
212
- const block = sectionBlock(sectionTitle);
213
- const afterStatus = await detectProofreadStatus(block);
214
- return {
215
- ok: true,
216
- intent,
217
- sectionTitle,
218
- action,
219
- proofreadStatusBefore: opened.proofreadStatus,
220
- proofreadStatusAfter: afterStatus,
221
- message: '本段已确认校对完成。',
222
- url: page.url(),
223
- };
224
- }
225
-
226
- if (action === 'ai_rewrite') {
227
- const generated = await aiRewriteGenerate(sectionTitle, extraRequirement);
228
- return {
229
- ok: generated.ok,
230
- intent,
231
- sectionTitle,
232
- action,
233
- extraRequirement: extraRequirement || null,
234
- generated: true,
235
- needsReplaceChoice: true,
236
- availableActions: ['replace'],
237
- message:
238
- 'AI 改写结果已生成。请在右侧查看「生成结果」,确认无误后告知我「替换原文」以应用;若不满意可直接在页面调整或重新生成。',
239
- url: page.url(),
240
- };
241
- }
242
-
243
- if (action === 'replace') {
244
- const replaced = await aiRewriteReplace();
245
- return {
246
- ok: replaced.ok,
247
- intent,
248
- sectionTitle,
249
- action,
250
- replaced: true,
251
- message: '已替换原文。如需完成本段校对,请明确告知「确认校对」。',
252
- url: page.url(),
253
- };
254
- }
255
-
256
- return {
257
- ok: false,
258
- reason: `未知 action「${action}」,应为 null | confirm | ai_rewrite | replace`,
259
- intent,
260
- sectionTitle,
261
- };
262
- }
1
+ /**
2
+ * 综述工作台(jova)· 初稿校对页 · 章节校对 / AI 改写
3
+ *
4
+ * Agent 调用约定(严格遵守):
5
+ *
6
+ * 1) 用户只说「校对 <章节>」、未指明操作时:
7
+ * action = null → 仅滚动定位 → 悬停 → 点「点击本段进入编辑校对」或「重新编辑」
8
+ * → 返回 needsUserChoice,由 Agent 询问用户选「确认校对」或「AI 改写」
9
+ * → 禁止自动点「确认校对」
10
+ *
11
+ * 2) 用户明确说「确认校对」:action = 'confirm'
12
+ *
13
+ * 3) 用户明确说「AI 改写」:
14
+ * action = 'ai_rewrite' → 点「AI 改写」→(有额外需求则填写)→「一键重写」→ 等待生成
15
+ * → 返回 needsReplaceChoice,提醒用户是否替换原文,禁止自动点「替换原文」
16
+ *
17
+ * 4) 用户明确同意替换原文:action = 'replace'
18
+ *
19
+ * 用法:
20
+ * helixlife-v5-cli run-code --filename=scripts/workbench-jova-proofread-section.js
21
+ */
22
+ async page => {
23
+ const intent = '校对 1. 乳腺癌分子分型体系及其分子特征';
24
+ const action = null; // null | 'confirm' | 'ai_rewrite' | 'replace'
25
+ const extraRequirement = ''; // 仅 ai_rewrite 时可选
26
+
27
+ const main = page.locator('main');
28
+
29
+ function parseIntent(raw) {
30
+ const m = raw.match(/^校对\s*(.+)$/);
31
+ return { sectionTitle: (m ? m[1] : raw).trim() };
32
+ }
33
+
34
+ const { sectionTitle } = parseIntent(intent);
35
+
36
+ if (!page.url().includes('/workbench/jova/proofread')) {
37
+ return {
38
+ ok: false,
39
+ reason: '当前不在综述工作台初稿校对页 /workbench/jova/proofread',
40
+ intent,
41
+ sectionTitle,
42
+ };
43
+ }
44
+
45
+ async function dismissPopconfirmOnly() {
46
+ const cancel = main.getByRole('button', { name: '取 消' });
47
+ if ((await cancel.count()) > 0 && (await cancel.first().isVisible())) {
48
+ await cancel.first().click();
49
+ await page.waitForTimeout(300);
50
+ }
51
+ }
52
+
53
+ function sectionHeading(title) {
54
+ return main.getByRole('heading', { name: title, exact: true }).first();
55
+ }
56
+
57
+ function sectionBlock(title) {
58
+ return sectionHeading(title).locator('xpath=ancestor::div[contains(@class,"group")][1]');
59
+ }
60
+
61
+ async function navigateToSection(title) {
62
+ const toc = main.locator('[class*="cursor-pointer"]').getByText(title, { exact: true });
63
+ if ((await toc.count()) > 0) {
64
+ await toc.first().click({ timeout: 3000 }).catch(() => {});
65
+ await page.waitForTimeout(500);
66
+ }
67
+ const heading = sectionHeading(title);
68
+ if ((await heading.count()) === 0) {
69
+ return { ok: false, reason: `未找到章节「${title}」` };
70
+ }
71
+ await heading.scrollIntoViewIfNeeded();
72
+ await page.waitForTimeout(400);
73
+ return { ok: true };
74
+ }
75
+
76
+ async function detectProofreadStatus(block) {
77
+ const pending = block.getByText('请校对本段内容', { exact: true });
78
+ const done = block.getByText('本段完成校对', { exact: true });
79
+ if ((await pending.count()) > 0) return 'pending';
80
+ if ((await done.count()) > 0) return 'done';
81
+ return 'completed_or_editing';
82
+ }
83
+
84
+ async function clickEnterEdit(block) {
85
+ const enterLabels = [
86
+ '点击本段进入编辑校对',
87
+ '点击本段进行校对',
88
+ '点击本段进行编辑校对',
89
+ ];
90
+ for (const label of enterLabels) {
91
+ const btn = block.getByText(label, { exact: true });
92
+ if ((await btn.count()) > 0 && (await btn.first().isVisible())) {
93
+ await btn.first().click();
94
+ return true;
95
+ }
96
+ }
97
+ return false;
98
+ }
99
+
100
+ async function openSectionEdit(title) {
101
+ const nav = await navigateToSection(title);
102
+ if (!nav.ok) return nav;
103
+
104
+ const block = sectionBlock(title);
105
+
106
+ if ((await block.getByRole('button', { name: '确认校对' }).count()) > 0) {
107
+ return { ok: true, proofreadStatus: 'already_editing' };
108
+ }
109
+
110
+ const proofreadStatus = await detectProofreadStatus(block);
111
+
112
+ if (proofreadStatus === 'pending') {
113
+ await block.getByText('请校对本段内容', { exact: true }).first().hover();
114
+ await page.waitForTimeout(600);
115
+ if (!(await clickEnterEdit(block))) {
116
+ return { ok: false, reason: '未找到「点击本段进入编辑校对」入口', proofreadStatus };
117
+ }
118
+ } else if (proofreadStatus === 'done') {
119
+ await block.getByText('本段完成校对', { exact: true }).first().hover();
120
+ await page.waitForTimeout(600);
121
+ const reEdit = block.getByText('重新编辑', { exact: true });
122
+ if ((await reEdit.count()) === 0 || !(await reEdit.first().isVisible())) {
123
+ return { ok: false, reason: '未找到「重新编辑」入口', proofreadStatus };
124
+ }
125
+ await reEdit.first().click();
126
+ } else {
127
+ await block.hover();
128
+ await page.waitForTimeout(600);
129
+ const reEdit = block.getByText('重新编辑', { exact: true });
130
+ if ((await reEdit.count()) > 0 && (await reEdit.first().isVisible())) {
131
+ await reEdit.first().click();
132
+ } else if (await clickEnterEdit(block)) {
133
+ // ok
134
+ } else {
135
+ return { ok: false, reason: '无法进入编辑模式(未找到入口)', proofreadStatus };
136
+ }
137
+ }
138
+
139
+ await block.getByRole('button', { name: '确认校对' }).waitFor({ state: 'visible', timeout: 10000 });
140
+ await page.waitForTimeout(400);
141
+ return { ok: true, proofreadStatus };
142
+ }
143
+
144
+ async function confirmProofread(title) {
145
+ const block = sectionBlock(title);
146
+ await block.getByRole('button', { name: '确认校对' }).click();
147
+ await page.waitForTimeout(1000);
148
+ return { ok: true };
149
+ }
150
+
151
+ async function waitReplaceReady(timeoutMs = 120000) {
152
+ const replaceBtn = main.getByRole('button', { name: '替换原文' });
153
+ await replaceBtn.waitFor({ state: 'visible', timeout: timeoutMs });
154
+ const deadline = Date.now() + timeoutMs;
155
+ while (Date.now() < deadline) {
156
+ if (await replaceBtn.isEnabled()) return replaceBtn;
157
+ await page.waitForTimeout(1000);
158
+ }
159
+ throw new Error('等待「替换原文」可点击超时');
160
+ }
161
+
162
+ async function aiRewriteGenerate(title, extra) {
163
+ const block = sectionBlock(title);
164
+ await block.getByRole('button', { name: /AI\s*改写/ }).click();
165
+ await main.getByText('原文内容', { exact: true }).waitFor({ state: 'visible', timeout: 10000 });
166
+
167
+ if (extra) {
168
+ const extraBox = main.getByRole('textbox', { name: '额外需求' });
169
+ if ((await extraBox.count()) > 0) await extraBox.fill(extra);
170
+ }
171
+
172
+ await main.getByRole('button', { name: '一键重写' }).click();
173
+ await waitReplaceReady();
174
+
175
+ return { ok: true, generated: true, replaceReady: true };
176
+ }
177
+
178
+ async function aiRewriteReplace() {
179
+ const replaceBtn = main.getByRole('button', { name: '替换原文' });
180
+ await replaceBtn.click();
181
+ await main.getByText('是否将本次生成结果替换原文?', { exact: true }).waitFor({ state: 'visible', timeout: 10000 });
182
+ await main.getByRole('button', { name: '确 定' }).click();
183
+ await page.waitForTimeout(1000);
184
+ return { ok: true, replaced: true };
185
+ }
186
+
187
+ await dismissPopconfirmOnly();
188
+
189
+ const needOpenEdit = action === null || action === '' || action === undefined || action === 'confirm' || action === 'ai_rewrite';
190
+ let opened = { ok: true, proofreadStatus: 'already_editing' };
191
+
192
+ if (needOpenEdit && action !== 'replace') {
193
+ opened = await openSectionEdit(sectionTitle);
194
+ if (!opened.ok) return { ok: false, intent, sectionTitle, ...opened };
195
+ }
196
+
197
+ if (action === null || action === undefined || action === '') {
198
+ return {
199
+ ok: true,
200
+ needsUserChoice: true,
201
+ intent,
202
+ sectionTitle,
203
+ proofreadStatus: opened.proofreadStatus,
204
+ availableActions: ['confirm', 'ai_rewrite'],
205
+ message: '已进入该段编辑模式。请选择:① 确认校对(直接完成本段校对)② AI 改写(AI 重新生成内容)',
206
+ url: page.url(),
207
+ };
208
+ }
209
+
210
+ if (action === 'confirm') {
211
+ await confirmProofread(sectionTitle);
212
+ const block = sectionBlock(sectionTitle);
213
+ const afterStatus = await detectProofreadStatus(block);
214
+ return {
215
+ ok: true,
216
+ intent,
217
+ sectionTitle,
218
+ action,
219
+ proofreadStatusBefore: opened.proofreadStatus,
220
+ proofreadStatusAfter: afterStatus,
221
+ message: '本段已确认校对完成。',
222
+ url: page.url(),
223
+ };
224
+ }
225
+
226
+ if (action === 'ai_rewrite') {
227
+ const generated = await aiRewriteGenerate(sectionTitle, extraRequirement);
228
+ return {
229
+ ok: generated.ok,
230
+ intent,
231
+ sectionTitle,
232
+ action,
233
+ extraRequirement: extraRequirement || null,
234
+ generated: true,
235
+ needsReplaceChoice: true,
236
+ availableActions: ['replace'],
237
+ message:
238
+ 'AI 改写结果已生成。请在右侧查看「生成结果」,确认无误后告知我「替换原文」以应用;若不满意可直接在页面调整或重新生成。',
239
+ url: page.url(),
240
+ };
241
+ }
242
+
243
+ if (action === 'replace') {
244
+ const replaced = await aiRewriteReplace();
245
+ return {
246
+ ok: replaced.ok,
247
+ intent,
248
+ sectionTitle,
249
+ action,
250
+ replaced: true,
251
+ message: '已替换原文。如需完成本段校对,请明确告知「确认校对」。',
252
+ url: page.url(),
253
+ };
254
+ }
255
+
256
+ return {
257
+ ok: false,
258
+ reason: `未知 action「${action}」,应为 null | confirm | ai_rewrite | replace`,
259
+ intent,
260
+ sectionTitle,
261
+ };
262
+ }