dsh-annotator 0.0.0-stage → 0.5.4
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 +159 -2
- package/cordis.patch.yml +10 -0
- package/lib/cdp.js +218 -0
- package/lib/client.js +1911 -0
- package/lib/index.js +447 -0
- package/lib/overlay.js +862 -0
- package/lib/session.js +1745 -0
- package/lib/shared/binding-action.js +26 -0
- package/lib/shared/browsers.js +126 -0
- package/lib/shared/cdp-endpoint.js +70 -0
- package/lib/shared/ego.js +68 -0
- package/lib/shared/embedded-runtime.js +53 -0
- package/lib/shared/gui-page.js +14 -0
- package/lib/shared/host-rev.js +120 -0
- package/lib/shared/pick-actions.js +21 -0
- package/lib/shared/pick-dedupe.js +26 -0
- package/lib/shared/png.js +173 -0
- package/lib/shared/scratch-dir.js +57 -0
- package/lib/shared/spaces.js +54 -0
- package/lib/shared/tabs.js +45 -0
- package/lib/shared/tool-schema.js +72 -0
- package/lib/shared/url.js +33 -0
- package/package.json +52 -5
package/lib/index.js
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-annotator — host half.
|
|
3
|
+
*
|
|
4
|
+
* Registers the HTTP surface the Web panel talks to (same-origin fetch on the
|
|
5
|
+
* DSH web server) plus one agent-facing tool, and owns the visible Chrome/Edge
|
|
6
|
+
* window driven over the CDP pipe.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
|
|
11
|
+
import { VisualPicker, availableBrowsers } from './session.js';
|
|
12
|
+
import { TOOL_OUTPUT_SCHEMA, TOOL_PARAMETERS } from './shared/tool-schema.js';
|
|
13
|
+
|
|
14
|
+
export const name = 'dsh-annotator';
|
|
15
|
+
export const inject = ['webServer'];
|
|
16
|
+
|
|
17
|
+
const ROUTE_PREFIX = '/api/visual-annotator';
|
|
18
|
+
/** Proves which build of the host half is live in the running DSH. */
|
|
19
|
+
const HOST_REV = 'v29-' + Date.now();
|
|
20
|
+
/** Populated when ctx.tools.register rejects the definition; surfaced by /state. */
|
|
21
|
+
let toolError = null;
|
|
22
|
+
/** True once visual_annotator_read is really in the live registry (not just declared). */
|
|
23
|
+
let toolReady = false;
|
|
24
|
+
|
|
25
|
+
async function readJson(req) {
|
|
26
|
+
// The DSH web server may already have parsed the body; `for await` covers the
|
|
27
|
+
// plain IncomingMessage case. Both are tried so a POST never silently loses
|
|
28
|
+
// its payload (a lost body used to launch about:blank instead of the URL).
|
|
29
|
+
const direct = req?.body;
|
|
30
|
+
if (direct && typeof direct === 'object' && !Buffer.isBuffer(direct)) return direct;
|
|
31
|
+
if (typeof direct === 'string' && direct) {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(direct);
|
|
34
|
+
} catch {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (Buffer.isBuffer(direct) && direct.length) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(direct.toString('utf8'));
|
|
41
|
+
} catch {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (req?.readableEnded) return {};
|
|
46
|
+
const chunks = [];
|
|
47
|
+
try {
|
|
48
|
+
for await (const chunk of req) {
|
|
49
|
+
chunks.push(Buffer.from(chunk));
|
|
50
|
+
if (chunks.reduce((total, item) => total + item.length, 0) > 4_000_000) break;
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
const raw = Buffer.concat(chunks).toString('utf8');
|
|
56
|
+
if (!raw) return {};
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(raw);
|
|
59
|
+
} catch {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function sendJson(res, status, value) {
|
|
65
|
+
const body = JSON.stringify(value);
|
|
66
|
+
res.statusCode = status;
|
|
67
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
68
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
69
|
+
res.end(body);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Ego-browser authoritative fallback: a present Origin must match Host. */
|
|
73
|
+
function sameOriginRejection(req) {
|
|
74
|
+
const origin = req?.headers?.origin;
|
|
75
|
+
if (!origin) return undefined;
|
|
76
|
+
let originHost;
|
|
77
|
+
try {
|
|
78
|
+
originHost = new URL(origin).host;
|
|
79
|
+
} catch {
|
|
80
|
+
return 403;
|
|
81
|
+
}
|
|
82
|
+
if (!req.headers.host || originHost !== req.headers.host) return 403;
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Admission gate mirroring DSH's own web auth (dsh-client-connection
|
|
88
|
+
* HostConnectionService.requestRejection): Host/Origin fence (403) plus the
|
|
89
|
+
* signed browser-cookie check (401). Falls back to the ego-style same-origin
|
|
90
|
+
* fence when the connection service is absent (headless profiles).
|
|
91
|
+
* Returns the HTTP status to reject with, or undefined to admit.
|
|
92
|
+
*/
|
|
93
|
+
function requestRejection(ctx, req) {
|
|
94
|
+
const connection = ctx.get?.('connection');
|
|
95
|
+
if (connection && typeof connection.requestRejection === 'function') {
|
|
96
|
+
try {
|
|
97
|
+
return connection.requestRejection(req);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
ctx.logger?.warn?.(`dsh-annotator: connection admission failed: ${String(error?.message ?? error)}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return sameOriginRejection(req);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
106
|
+
|
|
107
|
+
/** Normalizes the tool's shots argument: 'none' | 'all' | positive integer (default latest 3). */
|
|
108
|
+
export function normalizeShots(value) {
|
|
109
|
+
if (value === 'none' || value === 'all') return value;
|
|
110
|
+
const n = Number(value);
|
|
111
|
+
if (Number.isInteger(n) && n > 0) return n;
|
|
112
|
+
return 3;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Markdown for an id-filtered pick subset (same shape as VisualPicker.toMarkdown). */
|
|
116
|
+
export function filteredPicksMarkdown(picks) {
|
|
117
|
+
if (!picks.length) return '(没有匹配 id 的页面元素标注)';
|
|
118
|
+
const lines = [`页面元素标注(${picks.length} 处)`];
|
|
119
|
+
picks.forEach((pick, index) => {
|
|
120
|
+
lines.push(
|
|
121
|
+
'',
|
|
122
|
+
`${index + 1}. \`${pick.selector || pick.tag}\` — ${pick.tag}${pick.text ? `:${pick.text.replace(/\s+/g, ' ').slice(0, 80)}` : ''}`,
|
|
123
|
+
);
|
|
124
|
+
if (pick.url) lines.push(` 页面:${pick.url}`);
|
|
125
|
+
if (pick.pageRect)
|
|
126
|
+
lines.push(
|
|
127
|
+
` 位置:x=${Math.round(pick.pageRect.x)} y=${Math.round(pick.pageRect.y)} ${Math.round(pick.pageRect.width)}×${Math.round(pick.pageRect.height)}`,
|
|
128
|
+
);
|
|
129
|
+
if (pick.comment) lines.push(` 评论:${pick.comment}`);
|
|
130
|
+
if (pick.shotPath) lines.push(` 截图:${pick.shotPath}`);
|
|
131
|
+
});
|
|
132
|
+
return lines.join('\n');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Persists the chosen pick screenshots as durable attachments for the tool render. */
|
|
136
|
+
async function saveShotAttachments(ctx, picks, shots) {
|
|
137
|
+
if (shots === 'none') return [];
|
|
138
|
+
const store = ctx.get?.('attachments');
|
|
139
|
+
if (!store || typeof store.saveImages !== 'function') return [];
|
|
140
|
+
const withShots = picks.filter((pick) => pick.shotPath && fs.existsSync(pick.shotPath));
|
|
141
|
+
const chosen = shots === 'all' ? withShots : withShots.slice(-shots);
|
|
142
|
+
const attachments = [];
|
|
143
|
+
for (const pick of chosen) {
|
|
144
|
+
try {
|
|
145
|
+
const data = fs.readFileSync(pick.shotPath).toString('base64');
|
|
146
|
+
const refs = await store.saveImages([{ mediaType: 'image/png', data, name: `visual-annotator-${pick.id}.png` }]);
|
|
147
|
+
if (refs?.[0]) attachments.push({ pickId: pick.id, ref: refs[0] });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
ctx.logger?.warn?.(`dsh-annotator: shot attachment failed for ${pick.id}: ${String(error?.message ?? error)}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return attachments;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function apply(ctx) {
|
|
156
|
+
// 传 logger 让启动降级(--no-sandbox 回退)这类「不该静默」的事进 host 日志。
|
|
157
|
+
const picker = new VisualPicker({ log: (level, message) => ctx.logger?.[level]?.(message) });
|
|
158
|
+
const disposers = [];
|
|
159
|
+
let closed = false;
|
|
160
|
+
/** Pick count at the tool's previous read; wait_seconds waits for growth past it. */
|
|
161
|
+
let lastReadCount = -1;
|
|
162
|
+
|
|
163
|
+
const teardown = () => {
|
|
164
|
+
if (closed) return;
|
|
165
|
+
closed = true;
|
|
166
|
+
for (const dispose of disposers.splice(0)) {
|
|
167
|
+
try {
|
|
168
|
+
dispose?.();
|
|
169
|
+
} catch {
|
|
170
|
+
/* the web server may already be gone */
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// host 卸载时窗口不该继续飘在桌面上;profile 留在临时目录,重开接着用。
|
|
174
|
+
void picker.close({ terminate: true });
|
|
175
|
+
};
|
|
176
|
+
ctx.effect?.(() => teardown);
|
|
177
|
+
|
|
178
|
+
const mount = (rawServer) => {
|
|
179
|
+
const server = rawServer?.register ? rawServer : ctx.get?.('webServer');
|
|
180
|
+
if (!server || typeof server.register !== 'function') {
|
|
181
|
+
ctx.logger?.warn?.('dsh-annotator: webServer service unavailable, panel routes not mounted');
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const route = (path, handler) => {
|
|
185
|
+
const guarded = async (req, res) => {
|
|
186
|
+
const rejection = requestRejection(ctx, req);
|
|
187
|
+
if (rejection !== undefined) {
|
|
188
|
+
sendJson(res, rejection, { ok: false, error: 'unauthorized' });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
await handler(req, res);
|
|
192
|
+
};
|
|
193
|
+
const dispose = server.register({ kind: 'exact', path: ROUTE_PREFIX + path, handler: guarded });
|
|
194
|
+
disposers.push(dispose);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
route('/state', async (_req, res) => {
|
|
198
|
+
// 面板每秒轮询一次:顺手用 Target.getTargets 对一遍标签页(标题/URL),
|
|
199
|
+
// 不依赖 targetInfoChanged 一定能送达——某些页面标题是框架异步写的,
|
|
200
|
+
// 或者在 attach 之前就改完,只靠事件时面板会一直显示 URL。
|
|
201
|
+
await picker.refreshTabs().catch(() => {});
|
|
202
|
+
const snapshot = picker.snapshot();
|
|
203
|
+
sendJson(res, 200, {
|
|
204
|
+
ok: true,
|
|
205
|
+
...snapshot,
|
|
206
|
+
rev: HOST_REV,
|
|
207
|
+
file: import.meta.url,
|
|
208
|
+
// Registration used to fail silently; /state now distinguishes
|
|
209
|
+
// "registered fine" from "register threw" so a missing tool is diagnosable.
|
|
210
|
+
tool: { registered: toolReady, error: toolError },
|
|
211
|
+
toolError,
|
|
212
|
+
markdown: picker.toMarkdown(),
|
|
213
|
+
browsers: availableBrowsers(),
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
route('/launch', async (req, res) => {
|
|
218
|
+
const body = await readJson(req);
|
|
219
|
+
const snapshot = await picker.launch({
|
|
220
|
+
url: body.url,
|
|
221
|
+
browser: body.browser,
|
|
222
|
+
cwd: body.cwd,
|
|
223
|
+
cleanProfile: body.cleanProfile === undefined ? undefined : body.cleanProfile === true,
|
|
224
|
+
});
|
|
225
|
+
sendJson(res, 200, {
|
|
226
|
+
ok: snapshot.status === 'ready',
|
|
227
|
+
...snapshot,
|
|
228
|
+
rev: HOST_REV,
|
|
229
|
+
received: { url: body.url ?? null, browser: body.browser ?? null, cwd: body.cwd ?? null },
|
|
230
|
+
markdown: picker.toMarkdown(),
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
route('/space', async (req, res) => {
|
|
235
|
+
// 调试空间 = Chromium BrowserContext(与 ego lite 的 task space 同一原语)。
|
|
236
|
+
// action: create(新建并切过去)/ use(切换)/ close(销毁并回默认)。
|
|
237
|
+
const body = await readJson(req);
|
|
238
|
+
const action = String(body.action ?? 'use');
|
|
239
|
+
try {
|
|
240
|
+
let snapshot;
|
|
241
|
+
if (action === 'create') snapshot = await picker.createSpace({ name: body.name, url: body.url });
|
|
242
|
+
else if (action === 'close') snapshot = await picker.closeSpace(body.id);
|
|
243
|
+
else snapshot = await picker.useSpace(body.id, body.url);
|
|
244
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
245
|
+
} catch (error) {
|
|
246
|
+
sendJson(res, 200, {
|
|
247
|
+
ok: false,
|
|
248
|
+
error: String(error?.message ?? error),
|
|
249
|
+
error_message: String(error?.message ?? error),
|
|
250
|
+
...picker.snapshot(),
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
route('/probe', async (_req, res) => {
|
|
256
|
+
// Health check: proves the page-side overlay is actually alive on the
|
|
257
|
+
// active tab. Deliberately evaluates one fixed expression only.
|
|
258
|
+
const picker0 = picker;
|
|
259
|
+
let value = null;
|
|
260
|
+
let error = null;
|
|
261
|
+
try {
|
|
262
|
+
const snapshot = picker0.snapshot();
|
|
263
|
+
if (snapshot.status !== 'ready') throw new Error(`browser ${snapshot.status}`);
|
|
264
|
+
const result = await picker0.probeOverlay();
|
|
265
|
+
value = result;
|
|
266
|
+
} catch (caught) {
|
|
267
|
+
error = String(caught?.message ?? caught);
|
|
268
|
+
}
|
|
269
|
+
sendJson(res, 200, {
|
|
270
|
+
ok: error === null,
|
|
271
|
+
rev: HOST_REV,
|
|
272
|
+
overlay: value,
|
|
273
|
+
error,
|
|
274
|
+
...picker0.snapshot(),
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
route('/mode', async (req, res) => {
|
|
279
|
+
const body = await readJson(req);
|
|
280
|
+
const snapshot = await picker.setMode(body.mode);
|
|
281
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
route('/navigate', async (req, res) => {
|
|
285
|
+
const body = await readJson(req);
|
|
286
|
+
const snapshot = await picker.navigate(String(body.url ?? ''));
|
|
287
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
route('/tab', async (req, res) => {
|
|
291
|
+
const body = await readJson(req);
|
|
292
|
+
const snapshot = await picker.activateTab(String(body.targetId ?? ''));
|
|
293
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
route('/comment', async (req, res) => {
|
|
297
|
+
const body = await readJson(req);
|
|
298
|
+
const snapshot = picker.setComment(String(body.id ?? ''), body.comment);
|
|
299
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
route('/remove', async (req, res) => {
|
|
303
|
+
const body = await readJson(req);
|
|
304
|
+
const snapshot = await picker.removePick(String(body.id ?? ''));
|
|
305
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// The panel inserted these picks into the composer: drop them from the list so the
|
|
309
|
+
// next annotation round starts clean. /restore still brings the whole batch back.
|
|
310
|
+
route('/consume', async (req, res) => {
|
|
311
|
+
const body = await readJson(req);
|
|
312
|
+
const snapshot = await picker.consumePicks(Array.isArray(body.ids) ? body.ids : []);
|
|
313
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// Chip expansion resolves one id at submit time. The pick may already have left
|
|
317
|
+
// the list (it was inserted in an earlier round), so answer from the archive too.
|
|
318
|
+
route('/pick', async (req, res) => {
|
|
319
|
+
const body = await readJson(req);
|
|
320
|
+
const pick = picker.findPick(String(body.id ?? ''));
|
|
321
|
+
sendJson(res, 200, { ok: Boolean(pick), pick, archived: picker.retired.length });
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
route('/focus', async (req, res) => {
|
|
325
|
+
const body = await readJson(req);
|
|
326
|
+
const snapshot = await picker.focusPick(String(body.id ?? ''));
|
|
327
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
route('/restore', async (_req, res) => {
|
|
331
|
+
const snapshot = await picker.restorePick();
|
|
332
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
route('/clear', async (_req, res) => {
|
|
336
|
+
const snapshot = await picker.clear();
|
|
337
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
route('/close', async (req, res) => {
|
|
341
|
+
const body = await readJson(req);
|
|
342
|
+
// terminate=true 才真的关浏览器;缺省只断开,窗口与 profile 留给下次重连。
|
|
343
|
+
const snapshot = await picker.close({ terminate: body.terminate === true });
|
|
344
|
+
sendJson(res, 200, { ok: true, ...snapshot, markdown: picker.toMarkdown() });
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
route('/shot', async (req, res) => {
|
|
348
|
+
const id = new URL(req.url ?? '/', 'http://127.0.0.1').searchParams.get('id') ?? '';
|
|
349
|
+
const file = picker.shotFile(id);
|
|
350
|
+
if (!file) {
|
|
351
|
+
sendJson(res, 404, { ok: false, error: 'shot not found' });
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
const bytes = fs.readFileSync(file);
|
|
356
|
+
res.statusCode = 200;
|
|
357
|
+
res.setHeader('Content-Type', 'image/png');
|
|
358
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
359
|
+
res.end(bytes);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
sendJson(res, 500, { ok: false, error: String(error?.message ?? error) });
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const webServer = ctx.get?.('webServer');
|
|
367
|
+
if (webServer) mount(webServer);
|
|
368
|
+
else ctx.inject?.(['webServer'], (wctx) => mount(wctx.get?.('webServer')));
|
|
369
|
+
|
|
370
|
+
const registerTool = (tools) => {
|
|
371
|
+
// A second attempt would hit the duplicate-name error and look like a failure.
|
|
372
|
+
if (!tools?.register || toolReady) return;
|
|
373
|
+
try {
|
|
374
|
+
const dispose = tools.register({
|
|
375
|
+
name: 'visual_annotator_read',
|
|
376
|
+
description:
|
|
377
|
+
"读取用户在 Visual Picker 面板里对网页元素的标注(CSS 选择器、元素文本、评论、截图路径)。用户说“看我的标注/元素标注/截图”时先调用。当用户消息中出现「[标注 #pN]」引用标记时必须调用本工具,并用 ids 参数只取对应标注。返回 markdown、picks 列表,并按 shots 参数把元素截图作为图片附件内联。参数 shots:'none' 不带截图;'all' 内联全部;正整数 n=只内联最近 n 张;缺省内联最近 3 张。参数 wait_seconds(0-60,默认 0):浏览器运行中且自上次读取后没有新标注时轮询等待,出现新标注或超时立即返回,可替代轮询实现“用户标注完成即推送”。",
|
|
378
|
+
parameters: TOOL_PARAMETERS,
|
|
379
|
+
timeoutMs: 90000,
|
|
380
|
+
output: {
|
|
381
|
+
schema: TOOL_OUTPUT_SCHEMA,
|
|
382
|
+
render: (_args, value) => {
|
|
383
|
+
const blocks = [{ type: 'text', text: String(value?.markdown ?? JSON.stringify(value, null, 2)) }];
|
|
384
|
+
for (const item of value?.attachments ?? []) {
|
|
385
|
+
if (item?.ref) blocks.push({ type: 'image', attachment: item.ref });
|
|
386
|
+
}
|
|
387
|
+
return blocks;
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
execute: async (args) => {
|
|
391
|
+
const shots = normalizeShots(args?.shots);
|
|
392
|
+
const waitSeconds = Math.max(0, Math.min(60, Number(args?.wait_seconds) || 0));
|
|
393
|
+
const deadline = waitSeconds > 0 ? Date.now() + waitSeconds * 1000 : 0;
|
|
394
|
+
// Push-style substitute: poll until a pick arrives past the previous
|
|
395
|
+
// read instead of returning unchanged snapshots. Skipped entirely when
|
|
396
|
+
// the browser is not running — nothing can arrive then.
|
|
397
|
+
while (
|
|
398
|
+
deadline > 0 &&
|
|
399
|
+
picker.state.status === 'ready' &&
|
|
400
|
+
picker.state.picks.length <= lastReadCount &&
|
|
401
|
+
Date.now() < deadline
|
|
402
|
+
) {
|
|
403
|
+
await sleep(400);
|
|
404
|
+
}
|
|
405
|
+
// Includes picks already inserted: a chip keeps its id, and the agent may
|
|
406
|
+
// be asked about that id long after the panel list moved on.
|
|
407
|
+
const all = picker.readablePicks().map((pick) => ({
|
|
408
|
+
id: pick.id,
|
|
409
|
+
selector: pick.selector,
|
|
410
|
+
tag: pick.tag,
|
|
411
|
+
text: pick.text,
|
|
412
|
+
comment: pick.comment,
|
|
413
|
+
shotPath: pick.shotPath ?? null,
|
|
414
|
+
url: pick.url,
|
|
415
|
+
pageRect: pick.pageRect ?? null,
|
|
416
|
+
}));
|
|
417
|
+
lastReadCount = all.length;
|
|
418
|
+
// Reference mode: the composer only carries [标注 #pN] markers, so the
|
|
419
|
+
// tool must be able to fetch exactly the referenced subset.
|
|
420
|
+
const wanted = Array.isArray(args?.ids) ? args.ids.map((id) => String(id)) : null;
|
|
421
|
+
const picks = wanted && wanted.length ? all.filter((pick) => wanted.includes(pick.id)) : all;
|
|
422
|
+
const attachments = await saveShotAttachments(ctx, picks, shots);
|
|
423
|
+
return {
|
|
424
|
+
ok: true,
|
|
425
|
+
count: picks.length,
|
|
426
|
+
rev: HOST_REV,
|
|
427
|
+
markdown: wanted && wanted.length ? filteredPicksMarkdown(picks) : picker.toMarkdown(),
|
|
428
|
+
picks,
|
|
429
|
+
attachments,
|
|
430
|
+
};
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
disposers.push(dispose);
|
|
434
|
+
toolReady = true;
|
|
435
|
+
} catch (error) {
|
|
436
|
+
toolError = String(error?.stack ?? error?.message ?? error);
|
|
437
|
+
ctx.logger?.warn?.(`dsh-annotator: tool registration failed: ${toolError}`);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
// The tools service can be provided after this plugin applies. Probing once
|
|
441
|
+
// used to drop the tool silently, so wait for the service instead.
|
|
442
|
+
const tools = ctx.get?.('tools');
|
|
443
|
+
if (tools?.register) registerTool(tools);
|
|
444
|
+
else ctx.inject?.(['tools'], (toolsCtx) => registerTool(toolsCtx.get?.('tools')));
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export default { name, inject, apply };
|