dsh-annotator 0.5.5 → 0.6.0
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 +4 -3
- package/lib/client.js +1697 -296
- package/lib/index.js +146 -32
- package/lib/overlay.js +321 -46
- package/lib/session.js +549 -47
- package/lib/shared/annotation-markdown.js +174 -0
- package/lib/shared/binding-action.js +3 -0
- package/lib/shared/page-roles.js +365 -0
- package/lib/shared/tool-schema.js +21 -0
- package/package.json +7 -3
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One markdown for every consumer: the agent tool output, the panel's copy button,
|
|
3
|
+
* and the HUD's 「复制提示词」. Three code paths writing their own version drift —
|
|
4
|
+
* the panel used to omit the per-change 「参照:」 line the tool printed, so the
|
|
5
|
+
* prompt pasted into another agent lost the whole 改动↔参照 mapping.
|
|
6
|
+
*
|
|
7
|
+
* The shape is a contract (docs/reference-mode.md §6):
|
|
8
|
+
* 「页面元素标注(N 处:X 处改动 / Y 处参照)」
|
|
9
|
+
* 「任务:…」/「执行要求」1..4
|
|
10
|
+
* 「## 参照 R1 — 页面标题(只读参考…)」
|
|
11
|
+
* 「R1-1. 选择器 — tag:文本」 …「 参照:R1-1 的配色、圆角(只改这一处)」
|
|
12
|
+
*
|
|
13
|
+
* 页面没有角色,只有标注有:参照完全可能和改动取自同一页(同一页的卡片 A 照着
|
|
14
|
+
* 同一页的卡片 B 改)。所以这里按「页面」分组只用于分节阅读,绝不暗示两页对立;
|
|
15
|
+
* 反过来,改动分散在多个标签页时也按页各分一节,不再假装只有一个「目标页」。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { normalizeSpec, pageKeyOf, pairIdsOf, pickRoleOf, refLabelsOf } from './page-roles.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 任务的唯一说法:面板「插入」到输入框的草稿开头也用这两句(lib/client.js 里同名
|
|
22
|
+
* 字面量,test/panel-target-visual.test.mjs 逐字比对)。一份提示词两个开头,用户
|
|
23
|
+
* 拿到的就是两套指令。
|
|
24
|
+
*/
|
|
25
|
+
export const TASK_REFERENCE =
|
|
26
|
+
'任务:按「改动」标注修改对应元素;「参照」只提供样式与结构,别改它指向的元素,也别改它的代码。';
|
|
27
|
+
export const TASK_PLAIN = '任务:按下列标注修改对应元素。';
|
|
28
|
+
|
|
29
|
+
/** One pick as markdown lines; the caller owns the numbering. */
|
|
30
|
+
function pushPick(lines, pick, label, { commentLabel = '评论' } = {}) {
|
|
31
|
+
lines.push('');
|
|
32
|
+
lines.push(
|
|
33
|
+
label +
|
|
34
|
+
' `' +
|
|
35
|
+
(pick.selector || pick.tag) +
|
|
36
|
+
'` — ' +
|
|
37
|
+
pick.tag +
|
|
38
|
+
(pick.text ? ':' + String(pick.text).replace(/\s+/g, ' ').slice(0, 80) : ''),
|
|
39
|
+
);
|
|
40
|
+
if (pick.url) lines.push(' 页面:' + pick.url);
|
|
41
|
+
if (pick.pageRect)
|
|
42
|
+
lines.push(
|
|
43
|
+
' 位置:x=' +
|
|
44
|
+
Math.round(pick.pageRect.x) +
|
|
45
|
+
' y=' +
|
|
46
|
+
Math.round(pick.pageRect.y) +
|
|
47
|
+
' ' +
|
|
48
|
+
Math.round(pick.pageRect.width) +
|
|
49
|
+
'×' +
|
|
50
|
+
Math.round(pick.pageRect.height),
|
|
51
|
+
);
|
|
52
|
+
const spec = normalizeSpec(pick.spec);
|
|
53
|
+
if (spec.length) lines.push(' 参考维度:' + spec.join('、'));
|
|
54
|
+
if (pick.comment) lines.push(' ' + commentLabel + ':' + pick.comment);
|
|
55
|
+
if (pick.html) lines.push(' HTML: `' + String(pick.html).replace(/\s+/g, ' ').slice(0, 300) + '`');
|
|
56
|
+
if (pick.shotPath) lines.push(' 截图:' + pick.shotPath);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {Array<object>} picks
|
|
61
|
+
* @param {{ empty?: string, source?: string }} [options]
|
|
62
|
+
* empty: no-pick message; source: 「,来源 chrome:url」 for the legacy single-block header.
|
|
63
|
+
*/
|
|
64
|
+
export function annotationMarkdown(picks, options = {}) {
|
|
65
|
+
const list = Array.isArray(picks) ? picks : [];
|
|
66
|
+
if (!list.length) return options.empty || '(当前还没有页面元素标注)';
|
|
67
|
+
// 角色一律经 pickRoleOf 归一,和 session / 工具输出走同一个判断:直接读 pick.role
|
|
68
|
+
// 的话,将来 role 多出一种值或缺失时,只有这一条路径会把标注分错边。
|
|
69
|
+
const references = list.filter((pick) => pickRoleOf(pick) === 'reference');
|
|
70
|
+
const targets = list.filter((pick) => pickRoleOf(pick) !== 'reference');
|
|
71
|
+
|
|
72
|
+
// 没有参照时保持单块编号(老提示词与既有测试都依赖这一行),但也给任务与要求:
|
|
73
|
+
// 一份没有「要怎么改」的标注列表,读到它的 agent 只能自己猜。
|
|
74
|
+
if (!references.length) {
|
|
75
|
+
const lines = ['页面元素标注(' + list.length + ' 处' + (options.source ? ',来源 ' + options.source : '') + ')'];
|
|
76
|
+
lines.push('');
|
|
77
|
+
lines.push(TASK_PLAIN);
|
|
78
|
+
lines.push('');
|
|
79
|
+
lines.push('执行要求');
|
|
80
|
+
lines.push('1. 一条标注对应一处元素,按该条的「评论」改。');
|
|
81
|
+
lines.push('2. 只改这些元素,别顺手重构同一页的其它部分。');
|
|
82
|
+
lines.push('3. 改完逐条自检,确认每条评论都落实到了对应元素上。');
|
|
83
|
+
list.forEach((pick, index) => pushPick(lines, pick, String(index + 1) + '.'));
|
|
84
|
+
return lines.join('\n');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const labels = refLabelsOf(list);
|
|
88
|
+
const labelOf = (pick) => labels.get(pick.pageKey || pageKeyOf(pick.url)) || pick.refLabel || 'R';
|
|
89
|
+
// 参照标注的编号(R1-1、R1-2…):目标行靠它说清「照哪一条改」。
|
|
90
|
+
const code = new Map();
|
|
91
|
+
const perPage = new Map();
|
|
92
|
+
for (const pick of references) {
|
|
93
|
+
const key = pick.pageKey || pageKeyOf(pick.url);
|
|
94
|
+
const next = (perPage.get(key) || 0) + 1;
|
|
95
|
+
perPage.set(key, next);
|
|
96
|
+
code.set(String(pick.id), labelOf(pick) + '-' + next);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const keyOf = (pick) => pick.pageKey || pageKeyOf(pick.url);
|
|
100
|
+
const titleOf = (items) => {
|
|
101
|
+
const pick = items[0];
|
|
102
|
+
return String(pick?.pageTitle || '').trim() || String(pick?.url || '').trim() || '页面';
|
|
103
|
+
};
|
|
104
|
+
const keysOf = (items) => {
|
|
105
|
+
const keys = [];
|
|
106
|
+
for (const pick of items) {
|
|
107
|
+
const key = keyOf(pick);
|
|
108
|
+
if (!keys.includes(key)) keys.push(key);
|
|
109
|
+
}
|
|
110
|
+
return keys;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const lines = [
|
|
114
|
+
'页面元素标注(' + list.length + ' 处:' + targets.length + ' 处改动 / ' + references.length + ' 处参照)',
|
|
115
|
+
];
|
|
116
|
+
// 执行意图写在最前面:任务一句话说清「改什么、别改什么」,编号要求逐条对齐,
|
|
117
|
+
// 最后一条要求自检——读到提示词的 agent 不用回头猜规矩。
|
|
118
|
+
lines.push('');
|
|
119
|
+
lines.push(TASK_REFERENCE);
|
|
120
|
+
lines.push('');
|
|
121
|
+
lines.push('执行要求');
|
|
122
|
+
lines.push(
|
|
123
|
+
'1. 只改带 T 编号的「改动」元素;带 R 编号的是只读参考,只取它的样式与结构,不改那处元素,也不改它所在的代码。',
|
|
124
|
+
);
|
|
125
|
+
lines.push('2. 改动与参照可以来自同一页:同一页里带 R 编号的那几处依然只读,只改 T 编号那几处。');
|
|
126
|
+
lines.push('3. 每条改动下的「参照:」行写明照哪几条、照哪些维度;没写维度就整体对齐,多条说法冲突时以先列者为准。');
|
|
127
|
+
lines.push('4. 改完逐条自检:T 编号每一处都改到位,「参照:」写到的维度都对齐,R 编号那几处一处没动。');
|
|
128
|
+
|
|
129
|
+
for (const key of keysOf(references)) {
|
|
130
|
+
const items = references.filter((pick) => keyOf(pick) === key);
|
|
131
|
+
const sharesPage = targets.some((pick) => keyOf(pick) === key);
|
|
132
|
+
lines.push('');
|
|
133
|
+
lines.push(
|
|
134
|
+
'## 参照 ' +
|
|
135
|
+
labelOf(items[0]) +
|
|
136
|
+
' — ' +
|
|
137
|
+
titleOf(items) +
|
|
138
|
+
(sharesPage ? '(本页同时有改动:只改 T 编号那几处,这里只读)' : '(只读参考:不改这处元素,也不改它的代码)'),
|
|
139
|
+
);
|
|
140
|
+
for (const item of items) pushPick(lines, item, code.get(String(item.id)) + '.');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let nth = 0;
|
|
144
|
+
for (const key of keysOf(targets)) {
|
|
145
|
+
const items = targets.filter((pick) => keyOf(pick) === key);
|
|
146
|
+
const sharesPage = references.some((pick) => keyOf(pick) === key);
|
|
147
|
+
lines.push('');
|
|
148
|
+
lines.push('## 改动 — ' + titleOf(items) + (sharesPage ? '(待修改;本页参照只读)' : '(待修改)'));
|
|
149
|
+
for (const pick of items) {
|
|
150
|
+
pushPick(lines, pick, 'T' + ++nth + '.', { commentLabel: '改法' });
|
|
151
|
+
const pairs = pairIdsOf(pick)
|
|
152
|
+
.map((id) => list.find((item) => String(item.id) === id))
|
|
153
|
+
.filter(Boolean);
|
|
154
|
+
if (!pairs.length) {
|
|
155
|
+
lines.push(' 参照:未指定参照标注' + (references.length ? '(可按上面 R 编号的样式对齐)' : ''));
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const pairLabels = [];
|
|
159
|
+
const dims = [];
|
|
160
|
+
for (const pair of pairs) {
|
|
161
|
+
const pairCode = code.get(String(pair.id));
|
|
162
|
+
if (pairCode && !pairLabels.includes(pairCode)) pairLabels.push(pairCode);
|
|
163
|
+
for (const dim of normalizeSpec(pair.spec)) if (!dims.includes(dim)) dims.push(dim);
|
|
164
|
+
}
|
|
165
|
+
lines.push(
|
|
166
|
+
' 参照:' +
|
|
167
|
+
pairLabels.join('、') +
|
|
168
|
+
(dims.length ? ' 的' + dims.join('、') : '') +
|
|
169
|
+
'(只改这一处,参照处只读)',
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return lines.join('\n');
|
|
174
|
+
}
|
|
@@ -16,6 +16,9 @@ export function classifyBindingAction(payload) {
|
|
|
16
16
|
return 'pick';
|
|
17
17
|
case 'mode':
|
|
18
18
|
return payload.mode === 'pick' || payload.mode === 'off' ? 'mode' : null;
|
|
19
|
+
// HUD 上的「改动 / 参照」开关:只认这两个落点,别的一律当噪声。
|
|
20
|
+
case 'landing':
|
|
21
|
+
return payload.slot === 'target' || payload.slot === 'reference' ? 'landing' : null;
|
|
19
22
|
case 'undo':
|
|
20
23
|
return 'undo';
|
|
21
24
|
case 'consume':
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Roles of picks inside one annotation task.
|
|
3
|
+
*
|
|
4
|
+
* A pick is either a **target** ("change this") or a **reference** ("make it look
|
|
5
|
+
* like this"). The role belongs to the pick, not to the page: the same page can
|
|
6
|
+
* hold both kinds, and a reference does not have to come from another tab. Pages
|
|
7
|
+
* are only provenance — where a pick came from, so the agent knows which page it
|
|
8
|
+
* may edit and which one it must leave alone.
|
|
9
|
+
*
|
|
10
|
+
* Everything here is pure: session.js owns the mutable copy, the panel and the
|
|
11
|
+
* agent tool read the same shapes, and all of it is unit-testable without a browser.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Reference dimensions a pick can carry (chips in the panel and the overlay). */
|
|
15
|
+
export const SPEC_DIMENSIONS = ['布局', '间距', '配色', '字体', '字号', '圆角', '交互', '文案'];
|
|
16
|
+
|
|
17
|
+
export const PICK_ROLES = ['target', 'reference'];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Stable key for "the same page". The hash is dropped (it is in-page navigation,
|
|
21
|
+
* not a different page) and a trailing slash is ignored so /docs and /docs/ are
|
|
22
|
+
* one reference page. Anything unparseable falls back to the trimmed URL.
|
|
23
|
+
*/
|
|
24
|
+
export function pageKeyOf(url) {
|
|
25
|
+
const raw = String(url ?? '').trim();
|
|
26
|
+
if (!raw || raw === 'about:blank') return '';
|
|
27
|
+
try {
|
|
28
|
+
const parsed = new URL(raw);
|
|
29
|
+
parsed.hash = '';
|
|
30
|
+
return (parsed.origin + parsed.pathname + parsed.search).replace(/\/$/, '') || raw;
|
|
31
|
+
} catch {
|
|
32
|
+
return raw.replace(/#.*$/, '').replace(/\/$/, '');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Keep only known dimensions, de-duplicated, in the canonical order. */
|
|
37
|
+
export function normalizeSpec(spec) {
|
|
38
|
+
const wanted = new Set((Array.isArray(spec) ? spec : []).map((item) => String(item)));
|
|
39
|
+
return SPEC_DIMENSIONS.filter((dimension) => wanted.has(dimension));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One page record, no matter what the caller stored. Role is deliberately absent. */
|
|
43
|
+
function normalizePage(page) {
|
|
44
|
+
return {
|
|
45
|
+
key: String(page?.key || pageKeyOf(page?.url) || ''),
|
|
46
|
+
url: String(page?.url ?? ''),
|
|
47
|
+
title: String(page?.title ?? ''),
|
|
48
|
+
fullShotPath: page?.fullShotPath ?? null,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function normalizePages(pages) {
|
|
53
|
+
const list = (Array.isArray(pages) ? pages : []).map(normalizePage).filter((page) => page.key);
|
|
54
|
+
// One key, one record: duplicates would render twice in the page strip.
|
|
55
|
+
const seen = new Set();
|
|
56
|
+
return list.filter((page) => (seen.has(page.key) ? false : (seen.add(page.key), true)));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function findPage(pages, key) {
|
|
60
|
+
const wanted = String(key ?? '');
|
|
61
|
+
if (!wanted) return null;
|
|
62
|
+
return (Array.isArray(pages) ? pages : []).find((page) => String(page?.key ?? '') === wanted) ?? null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Display name of a page: title first, host second, never empty. */
|
|
66
|
+
export function pageLabel(page) {
|
|
67
|
+
const title = String(page?.title ?? '').trim();
|
|
68
|
+
if (title) return title;
|
|
69
|
+
try {
|
|
70
|
+
return new URL(page?.url ?? '').host || String(page?.url ?? '');
|
|
71
|
+
} catch {
|
|
72
|
+
return String(page?.url ?? '');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Role of one pick: the only authority. Unknown values mean "change this". */
|
|
77
|
+
export function pickRoleOf(pick) {
|
|
78
|
+
return PICK_ROLES.includes(pick?.role) ? pick.role : 'target';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* R1..Rn, one label per page that carries at least one reference pick.
|
|
83
|
+
*
|
|
84
|
+
* Numbering follows the order those pages first appear in the pick list (i.e. the
|
|
85
|
+
* order the user annotated them), so it is stable while they work and never
|
|
86
|
+
* depends on a page-role table that could disagree with the picks themselves.
|
|
87
|
+
*/
|
|
88
|
+
export function refLabelsOf(picks) {
|
|
89
|
+
const labels = new Map();
|
|
90
|
+
for (const pick of Array.isArray(picks) ? picks : []) {
|
|
91
|
+
if (pickRoleOf(pick) !== 'reference') continue;
|
|
92
|
+
const key = pick?.pageKey || pageKeyOf(pick?.url);
|
|
93
|
+
if (!key || labels.has(key)) continue;
|
|
94
|
+
labels.set(key, 'R' + (labels.size + 1));
|
|
95
|
+
}
|
|
96
|
+
return labels;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** R-number of one page; '' when that page holds no reference pick. */
|
|
100
|
+
export function refLabelOf(picks, key) {
|
|
101
|
+
const wanted = String(key ?? '');
|
|
102
|
+
if (!wanted) return '';
|
|
103
|
+
return refLabelsOf(picks).get(wanted) ?? '';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* How many picks one page holds, split by role.
|
|
108
|
+
*
|
|
109
|
+
* `included` counts the picks that would go into the next insert (`included !== false`),
|
|
110
|
+
* so this is the one place both the HUD ("本页标注 N 处") and the panel's tab strip get
|
|
111
|
+
* their numbers: two hand-rolled tallies used to drift apart.
|
|
112
|
+
*/
|
|
113
|
+
export function pagePickCounts(picks, key) {
|
|
114
|
+
const wanted = String(key ?? '');
|
|
115
|
+
const counts = { target: 0, reference: 0, total: 0, included: 0 };
|
|
116
|
+
for (const pick of Array.isArray(picks) ? picks : []) {
|
|
117
|
+
if (!wanted || (pick?.pageKey || pageKeyOf(pick?.url)) !== wanted) continue;
|
|
118
|
+
counts[pickRoleOf(pick)] += 1;
|
|
119
|
+
counts.total += 1;
|
|
120
|
+
if (pick.included !== false) counts.included += 1;
|
|
121
|
+
}
|
|
122
|
+
return counts;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* HUD payload for one tab: mode, this page's counts, and what the next pick will be.
|
|
127
|
+
*
|
|
128
|
+
* The role is the **landing** (the column the user last armed), not the page's
|
|
129
|
+
* majority: the HUD must describe what happens if they click now. Without a
|
|
130
|
+
* landing the next pick is a target, so that is what it says.
|
|
131
|
+
*
|
|
132
|
+
* The URL must be the tab's own, never the active page's: a background tab is
|
|
133
|
+
* exactly the case this feature lives on, and pushing the active page's numbers
|
|
134
|
+
* would show one tab's count in another tab's HUD.
|
|
135
|
+
*
|
|
136
|
+
* `prompt` is the same markdown the panel copies and the agent tool returns, so the
|
|
137
|
+
* HUD's 「复制提示词」 hands out an identical prompt instead of a third paraphrase.
|
|
138
|
+
*/
|
|
139
|
+
export function pageHudInfo({
|
|
140
|
+
url = '',
|
|
141
|
+
picks = [],
|
|
142
|
+
landing = null,
|
|
143
|
+
mode = 'off',
|
|
144
|
+
specs = SPEC_DIMENSIONS,
|
|
145
|
+
prompt = '',
|
|
146
|
+
} = {}) {
|
|
147
|
+
const key = pageKeyOf(url);
|
|
148
|
+
const list = Array.isArray(picks) ? picks : [];
|
|
149
|
+
const counts = pagePickCounts(list, key);
|
|
150
|
+
const role = PICK_ROLES.includes(landing) ? landing : 'target';
|
|
151
|
+
const labels = refLabelsOf(list);
|
|
152
|
+
let refLabel = '';
|
|
153
|
+
if (role === 'reference') refLabel = (key && labels.get(key)) || 'R' + (labels.size + 1);
|
|
154
|
+
return {
|
|
155
|
+
mode: mode ?? 'off',
|
|
156
|
+
count: counts.total,
|
|
157
|
+
targetCount: counts.target,
|
|
158
|
+
referenceCount: counts.reference,
|
|
159
|
+
role,
|
|
160
|
+
landing: PICK_ROLES.includes(landing) ? landing : '',
|
|
161
|
+
refLabel,
|
|
162
|
+
specs: Array.isArray(specs) ? specs : SPEC_DIMENSIONS,
|
|
163
|
+
prompt: String(prompt ?? ''),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A reference pick that was inserted before and is checked again: this is a resend. */
|
|
168
|
+
export function isResend(pick) {
|
|
169
|
+
return pickRoleOf(pick) === 'reference' && pick?.included === true && Boolean(pick?.referencedAt);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Split picks the way the panel renders them. Order inside each group is preserved. */
|
|
173
|
+
export function groupPicks(picks) {
|
|
174
|
+
const targets = [];
|
|
175
|
+
const references = [];
|
|
176
|
+
for (const pick of Array.isArray(picks) ? picks : []) {
|
|
177
|
+
if (pickRoleOf(pick) === 'reference') references.push(pick);
|
|
178
|
+
else targets.push(pick);
|
|
179
|
+
}
|
|
180
|
+
return { targets, references };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Included picks of one role, in list order — what an insert actually sends. */
|
|
184
|
+
export function includedPicks(picks, role) {
|
|
185
|
+
return (Array.isArray(picks) ? picks : []).filter((pick) => pick?.included !== false && pickRoleOf(pick) === role);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Title of the page the targets came from: first target pick wins, else the active page. */
|
|
189
|
+
export function targetTitleOf(picks, activeTitle = '') {
|
|
190
|
+
const { targets } = groupPicks(picks);
|
|
191
|
+
const first = targets[0];
|
|
192
|
+
const own = String(first?.pageTitle ?? '').trim();
|
|
193
|
+
return own || String(activeTitle ?? '').trim();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 一条目标标注配了哪些参考标注。
|
|
198
|
+
*
|
|
199
|
+
* pairIds 是权威列表(改一处可以同时参照多处:左栏模式取自 A、圆角取自 B),
|
|
200
|
+
* pairId 始终是它的第一条——agent 工具契约和旧数据只认这一个字段,两个字段
|
|
201
|
+
* 不能各说各话,所以读写都走这里。
|
|
202
|
+
*/
|
|
203
|
+
export function pairIdsOf(pick) {
|
|
204
|
+
const listed = Array.isArray(pick?.pairIds) ? pick.pairIds : [];
|
|
205
|
+
const ids = [];
|
|
206
|
+
for (const raw of listed) {
|
|
207
|
+
const id = String(raw ?? '');
|
|
208
|
+
if (id && !ids.includes(id)) ids.push(id);
|
|
209
|
+
}
|
|
210
|
+
const first = String(pick?.pairId ?? '');
|
|
211
|
+
if (first && !ids.includes(first)) ids.unshift(first);
|
|
212
|
+
return ids;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** 原地写回:pairIds 与 pairId 一起更新,空列表时 pairId 归 null。 */
|
|
216
|
+
export function setPairIds(pick, ids) {
|
|
217
|
+
const next = [];
|
|
218
|
+
for (const raw of Array.isArray(ids) ? ids : []) {
|
|
219
|
+
const id = String(raw ?? '');
|
|
220
|
+
if (id && !next.includes(id)) next.push(id);
|
|
221
|
+
}
|
|
222
|
+
pick.pairIds = next;
|
|
223
|
+
pick.pairId = next[0] ?? null;
|
|
224
|
+
return pick;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 参考对照的一行:左边要改的标注 + 右边配给它的参考标注(可以有多条)。
|
|
229
|
+
* 顺序按目标标注的创建顺序;没有任何目标认领的参考标注单独成行(左边为空),
|
|
230
|
+
* 用来表达「这些参考还没说清照着改哪儿」。
|
|
231
|
+
*/
|
|
232
|
+
export function pairRows(picks) {
|
|
233
|
+
const list = Array.isArray(picks) ? picks : [];
|
|
234
|
+
const { targets, references } = groupPicks(list);
|
|
235
|
+
const refById = new Map(references.map((pick) => [String(pick.id), pick]));
|
|
236
|
+
const paired = new Set();
|
|
237
|
+
const rows = targets.map((target) => {
|
|
238
|
+
const refs = pairIdsOf(target)
|
|
239
|
+
.map((id) => refById.get(id))
|
|
240
|
+
.filter(Boolean);
|
|
241
|
+
for (const ref of refs) paired.add(String(ref.id));
|
|
242
|
+
return { target, refs, ref: refs[0] ?? null };
|
|
243
|
+
});
|
|
244
|
+
for (const ref of references) if (!paired.has(String(ref.id))) rows.push({ target: null, refs: [], ref });
|
|
245
|
+
return rows;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 新参考标注默认配给第一条还没配的目标标注:边标边配,不用再点一次。 */
|
|
249
|
+
export function firstUnpairedTarget(picks) {
|
|
250
|
+
const { targets } = groupPicks(picks);
|
|
251
|
+
return targets.find((pick) => pairIdsOf(pick).length === 0) ?? null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** 把一条参考挂到一条目标上;已经挂着就不重复加。返回是否真的变了。 */
|
|
255
|
+
export function pairTo(picks, targetId, refId) {
|
|
256
|
+
const wanted = String(refId ?? '');
|
|
257
|
+
const target = (Array.isArray(picks) ? picks : []).find((pick) => String(pick?.id ?? '') === String(targetId ?? ''));
|
|
258
|
+
if (!target || !wanted) return false;
|
|
259
|
+
const ids = pairIdsOf(target);
|
|
260
|
+
if (ids.includes(wanted)) return false;
|
|
261
|
+
setPairIds(target, [...ids, wanted]);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 解开一条目标上的某一条参考(右侧小 ×);返回是否真的变了。 */
|
|
266
|
+
export function unpair(picks, targetId, refId) {
|
|
267
|
+
const wanted = String(refId ?? '');
|
|
268
|
+
const target = (Array.isArray(picks) ? picks : []).find((pick) => String(pick?.id ?? '') === String(targetId ?? ''));
|
|
269
|
+
if (!target) return false;
|
|
270
|
+
const ids = pairIdsOf(target);
|
|
271
|
+
if (!ids.includes(wanted)) return false;
|
|
272
|
+
setPairIds(
|
|
273
|
+
target,
|
|
274
|
+
ids.filter((id) => id !== wanted),
|
|
275
|
+
);
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** 一条目标上的参考全部解开。 */
|
|
280
|
+
export function unpairAll(picks, targetId) {
|
|
281
|
+
const target = (Array.isArray(picks) ? picks : []).find((pick) => String(pick?.id ?? '') === String(targetId ?? ''));
|
|
282
|
+
if (!target || !pairIdsOf(target).length) return false;
|
|
283
|
+
setPairIds(target, []);
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** 哪些目标标注在参照这一条参考(工具输出与 markdown 都要按这个反查)。 */
|
|
288
|
+
export function pairedTargetsOf(picks, refId) {
|
|
289
|
+
const wanted = String(refId ?? '');
|
|
290
|
+
if (!wanted) return [];
|
|
291
|
+
return (Array.isArray(picks) ? picks : [])
|
|
292
|
+
.filter((pick) => pairIdsOf(pick).includes(wanted))
|
|
293
|
+
.map((pick) => String(pick.id));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 参考对照插入时真正发出去的行:有目标的行总是发;孤立参考行只在本次勾着时发
|
|
298
|
+
* (插入过一次的参考会标成已引用,下一轮默认不再塞进对话)。
|
|
299
|
+
*/
|
|
300
|
+
export function insertableRows(rows) {
|
|
301
|
+
return (Array.isArray(rows) ? rows : []).filter(
|
|
302
|
+
(row) => row && (row.target || (row.ref && row.ref.included !== false)),
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 删除一条标注时,把指向它的配对一并解开。留一个指向已不存在标注的 pairId,
|
|
308
|
+
* 面板渲染时查不到就当没配对,看不出问题;但 agent 会读到「配给 #7」而 #7 早已不在列表里。
|
|
309
|
+
*/
|
|
310
|
+
export function clearPairTo(picks, id) {
|
|
311
|
+
const wanted = String(id ?? '');
|
|
312
|
+
const list = Array.isArray(picks) ? picks : [];
|
|
313
|
+
if (!wanted) return list;
|
|
314
|
+
for (const pick of list) {
|
|
315
|
+
if (!pick) continue;
|
|
316
|
+
const ids = pairIdsOf(pick);
|
|
317
|
+
if (ids.includes(wanted))
|
|
318
|
+
setPairIds(
|
|
319
|
+
pick,
|
|
320
|
+
ids.filter((each) => each !== wanted),
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return list;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** 把一条参考套用到所有还没配对的目标:返回真正改动到的目标 id。 */
|
|
327
|
+
export function pairAllUnpaired(picks, refId) {
|
|
328
|
+
const wanted = String(refId ?? '');
|
|
329
|
+
const touched = [];
|
|
330
|
+
if (!wanted) return touched;
|
|
331
|
+
const { targets } = groupPicks(picks);
|
|
332
|
+
for (const target of targets) {
|
|
333
|
+
if (!target) continue;
|
|
334
|
+
if (pairIdsOf(target).length) continue;
|
|
335
|
+
setPairIds(target, [wanted]);
|
|
336
|
+
touched.push(String(target.id));
|
|
337
|
+
}
|
|
338
|
+
return touched;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Deterministic delegation summary: no model call, no surprise. The panel shows
|
|
343
|
+
* it before insert and the user may edit it; regenerating always reproduces this.
|
|
344
|
+
*/
|
|
345
|
+
export function delegationSummary({ picks, activeTitle } = {}) {
|
|
346
|
+
const list = Array.isArray(picks) ? picks : [];
|
|
347
|
+
const refs = includedPicks(list, 'reference');
|
|
348
|
+
const targets = includedPicks(list, 'target');
|
|
349
|
+
if (!refs.length) return '';
|
|
350
|
+
const labels = [];
|
|
351
|
+
for (const pick of refs) {
|
|
352
|
+
const label = refLabelsOf(list).get(pick.pageKey || pageKeyOf(pick.url)) || 'R';
|
|
353
|
+
if (!labels.includes(label)) labels.push(label);
|
|
354
|
+
}
|
|
355
|
+
const dims = [];
|
|
356
|
+
for (const pick of refs) for (const dim of normalizeSpec(pick.spec)) if (!dims.includes(dim)) dims.push(dim);
|
|
357
|
+
const listText = labels.join('、');
|
|
358
|
+
const dimText = dims.length ? dims.join('、') : '写法';
|
|
359
|
+
const title = targetTitleOf(list, activeTitle);
|
|
360
|
+
if (!targets.length) return '按 ' + listText + ' 的' + dimText + '新建。';
|
|
361
|
+
// 页面只是出处:有改动就点名那几处要改,不再假装有一个「目标页」这个角色。
|
|
362
|
+
return (
|
|
363
|
+
'参照 ' + listText + ' 的' + dimText + (title ? ',改「' + title + '」上的改动标注。' : ',改下面列出的改动标注。')
|
|
364
|
+
);
|
|
365
|
+
}
|
|
@@ -29,6 +29,11 @@ export const TOOL_PARAMETERS = {
|
|
|
29
29
|
type: 'integer',
|
|
30
30
|
description: '等待新标注秒数 0-60(默认 0):无新 pick 时轮询等待,出现新 pick 或超时返回。',
|
|
31
31
|
},
|
|
32
|
+
roles: {
|
|
33
|
+
oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
|
|
34
|
+
description:
|
|
35
|
+
"按角色过滤:'target' 只要改动对象、'reference' 只要只读参照规范;也可给数组。缺省返回全部(参照在前,目标在后)。",
|
|
36
|
+
},
|
|
32
37
|
},
|
|
33
38
|
};
|
|
34
39
|
|
|
@@ -55,6 +60,22 @@ export const TOOL_OUTPUT_SCHEMA = {
|
|
|
55
60
|
shotPath: nullable('string'),
|
|
56
61
|
url: { type: 'string' },
|
|
57
62
|
pageRect: nullable('object'),
|
|
63
|
+
// 参考模式:role=reference 的是只读规范,role=target 才是改动对象。
|
|
64
|
+
role: { type: 'string', description: "'target' 或 'reference'" },
|
|
65
|
+
roleSource: { type: 'string' },
|
|
66
|
+
pageKey: { type: 'string' },
|
|
67
|
+
pageTitle: { type: 'string' },
|
|
68
|
+
refLabel: { type: 'string', description: "该参照标注所在页的 R 编号,如 'R1';改动标注为 ''" },
|
|
69
|
+
spec: { type: 'array', items: { type: 'string' } },
|
|
70
|
+
included: { type: 'boolean' },
|
|
71
|
+
referenced: { type: 'boolean' },
|
|
72
|
+
resend: { type: 'boolean' },
|
|
73
|
+
pageClosed: { type: 'boolean' },
|
|
74
|
+
// 配对:目标标注 pairIds 是它参照的全部参考标注(pairId 是第一条);
|
|
75
|
+
// 参考标注回列引用它的目标。
|
|
76
|
+
pairIds: { type: 'array', items: { type: 'string' } },
|
|
77
|
+
pairId: nullable('string'),
|
|
78
|
+
pairedTargets: { type: 'array', items: { type: 'string' } },
|
|
58
79
|
},
|
|
59
80
|
},
|
|
60
81
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-annotator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Visual element picker for DSH: highlight, annotate, comment and screenshot elements of a real Chrome/Edge page, then hand the selector + comment + shot to the agent.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -38,9 +38,13 @@
|
|
|
38
38
|
"lint:fix": "eslint . --fix",
|
|
39
39
|
"format": "prettier --write .",
|
|
40
40
|
"format:check": "prettier --check .",
|
|
41
|
-
"check": "node --check lib/index.js && node --check lib/session.js && node --check lib/cdp.js && node --check lib/client.js && node --check lib/overlay.js && node --check lib/shared/gui-page.js && node --check lib/shared/binding-action.js && node --check lib/shared/pick-actions.js && node --check lib/shared/auto-pick.js && node --check lib/shared/scratch-dir.js && node --check lib/shared/tool-schema.js && node --check lib/shared/cdp-endpoint.js && node --check lib/shared/browsers.js && node --check lib/shared/spaces.js && node --check lib/shared/ego.js && node --check lib/shared/tabs.js && node --check lib/shared/url.js && node --check lib/shared/host-rev.js && node --check lib/shared/embedded-runtime.js && node --check scripts/check-build-sync.mjs && node --check scripts/check-embedded-runtime.mjs && node --check scripts/canary-gates.mjs && node --check scripts/assess-ai-native.mjs && node --check scripts/mutation-test.mjs && node --check hooks/verify-before-finish.mjs",
|
|
42
|
-
"gates": "node scripts/check-build-sync.mjs && node scripts/check-embedded-runtime.mjs",
|
|
41
|
+
"check": "node --check lib/index.js && node --check lib/session.js && node --check lib/cdp.js && node --check lib/client.js && node --check lib/overlay.js && node --check lib/shared/gui-page.js && node --check lib/shared/binding-action.js && node --check lib/shared/pick-actions.js && node --check lib/shared/auto-pick.js && node --check lib/shared/scratch-dir.js && node --check lib/shared/tool-schema.js && node --check lib/shared/cdp-endpoint.js && node --check lib/shared/browsers.js && node --check lib/shared/spaces.js && node --check lib/shared/ego.js && node --check lib/shared/tabs.js && node --check lib/shared/url.js && node --check lib/shared/host-rev.js && node --check lib/shared/embedded-runtime.js && node --check scripts/check-build-sync.mjs && node --check scripts/check-embedded-runtime.mjs && node --check scripts/canary-gates.mjs && node --check scripts/assess-ai-native.mjs && node --check scripts/mutation-test.mjs && node --check scripts/gate-doctor.mjs && node --check hooks/verify-before-finish.mjs",
|
|
42
|
+
"gates": "node scripts/check-build-sync.mjs && node scripts/check-embedded-runtime.mjs && node scripts/gate-doctor.mjs",
|
|
43
|
+
"gate:doctor": "node scripts/gate-doctor.mjs",
|
|
44
|
+
"gate:live": "node scripts/gate-doctor.mjs --live",
|
|
45
|
+
"gate:apply": "node scripts/gate-doctor.mjs --apply",
|
|
43
46
|
"canary": "node scripts/canary-gates.mjs",
|
|
47
|
+
"setup:hooks": "git config core.hooksPath .githooks",
|
|
44
48
|
"assess": "node scripts/assess-ai-native.mjs",
|
|
45
49
|
"assess:gate": "node scripts/assess-ai-native.mjs --min-score 80",
|
|
46
50
|
"mutate": "node scripts/mutation-test.mjs",
|