dsh-marker 0.0.0-stage → 0.1.1
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 +44 -2
- package/client.js +609 -0
- package/cordis.patch.yml +5 -0
- package/docs/DETAIL.md +45 -0
- package/icon.svg +8 -0
- package/index.js +204 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +59 -4
- package/src/bridge.js +494 -0
- package/src/discovery.js +428 -0
- package/src/endpoint.js +166 -0
- package/src/focus.js +204 -0
- package/src/locator.js +317 -0
- package/src/mentions.js +120 -0
- package/src/paths.js +52 -0
- package/src/presentation.js +255 -0
- package/src/protocol.js +610 -0
- package/src/queue.js +278 -0
- package/src/sessions.js +240 -0
- package/src/store.js +267 -0
- package/src/tools.js +392 -0
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-marker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Marks made in your everyday browser, as first-class context for
|
|
4
|
+
[DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (DSH).
|
|
5
|
+
|
|
6
|
+
Point at a paragraph, an element, or a region in Firefox or Zen, add one line of
|
|
7
|
+
intent, and the mark lands in the running DSH Session with a cropped screenshot,
|
|
8
|
+
DOM selectors, and page context. The model can also ask you to point at
|
|
9
|
+
something (`annotate_pick`) when a request is ambiguous.
|
|
10
|
+
|
|
11
|
+
This package is the **host half**: the DSH bundle that serves the bridge, stores
|
|
12
|
+
marks, and registers the `annotate_*` tools. The browser half is the `dsh-marker`
|
|
13
|
+
extension in the same repository — without it the tools have nothing to read.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
dsh plugin add dsh-marker
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Add `--profile <name>` to install into a profile other than the one `dsh` boots.
|
|
22
|
+
The bundle's one runtime dependency (`ws`) is installed with it, so the machine
|
|
23
|
+
needs the npm registry reachable once. If the plugin row is inactive, enable
|
|
24
|
+
`dsh-marker` on the **Plugins** page of the DSH Web UI.
|
|
25
|
+
|
|
26
|
+
Requirements: DSH 0.2 or newer (declared as `engines.dsh`, checked before
|
|
27
|
+
install) and Node.js 22+. The bridge binds to loopback only; there is no extra
|
|
28
|
+
daemon and no extra port beyond the DSH webserver it attaches to.
|
|
29
|
+
|
|
30
|
+
## The browser half
|
|
31
|
+
|
|
32
|
+
The extension has no build step and is loaded from `packages/extension` in the
|
|
33
|
+
repository, then paired with the host through the bridge token in
|
|
34
|
+
`$DSH_HOME/ext-bridge-token`. Signed distribution is tracked as a milestone; see
|
|
35
|
+
the repository for the current state.
|
|
36
|
+
|
|
37
|
+
## Links
|
|
38
|
+
|
|
39
|
+
- Source, docs, and the extension: <https://github.com/modenicheng/dsh-annotate>
|
|
40
|
+
- Design decisions, including why the wire keeps its `anno` vocabulary:
|
|
41
|
+
[docs/DECISIONS.md](https://github.com/modenicheng/dsh-annotate/blob/main/docs/DECISIONS.md)
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
MIT
|
package/client.js
ADDED
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-marker — Web Client half.
|
|
3
|
+
*
|
|
4
|
+
* Turns the canonical `@[批注 · 那段话](dsh-anno:an-20261118-ab12cd)` mention into
|
|
5
|
+
* one atomic chip inside a user message, the same way `@session` renders. The
|
|
6
|
+
* chip is display only: it never invents navigation, and the annotation body
|
|
7
|
+
* stays out of the transcript — the Host attaches that at `agent/pre-step`.
|
|
8
|
+
* Hovering the chip opens the product's own `HoverCard` with what the user
|
|
9
|
+
* marked and the crop the browser took, read back from the Host on demand.
|
|
10
|
+
*
|
|
11
|
+
* It also answers the reverse question: which Session is this page showing. The
|
|
12
|
+
* Host needs that to default an annotation's target to the conversation the user
|
|
13
|
+
* is actually looking at.
|
|
14
|
+
*
|
|
15
|
+
* This file is a classic script, not a module: it registers a lazy factory with
|
|
16
|
+
* the page's module table, which is the only supported shape for a Client half.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
window.__ModuleLoader__.load({
|
|
20
|
+
id: 'dsh-marker',
|
|
21
|
+
factory(require) {
|
|
22
|
+
const React = require('react')
|
|
23
|
+
const h = React.createElement
|
|
24
|
+
|
|
25
|
+
/** Sticky matcher for one canonical mention; shared shape with the Host parser. */
|
|
26
|
+
const WIRE = /@\[((?:\\.|[^\\\]\n])*)\]\(dsh-anno:([A-Za-z0-9_-]+)\)/uy
|
|
27
|
+
|
|
28
|
+
/** How often the page checks for mentions waiting on an open Session. */
|
|
29
|
+
const QUEUE_POLL_MS = 4_000
|
|
30
|
+
|
|
31
|
+
/** Queue, focus, and annotation reads the Host serves to the page it serves. */
|
|
32
|
+
const QUEUE_PATH = '/ext/anno/queue'
|
|
33
|
+
const QUEUE_ACK_PATH = '/ext/anno/queue/ack'
|
|
34
|
+
const FOCUS_PATH = '/ext/anno/focus'
|
|
35
|
+
const ITEM_PATH = '/ext/anno/item'
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The reference chip's own declarations, copied from the product's
|
|
39
|
+
* `ReferenceChip` stylesheet (`refChip` / `refIcon`): an inline run in the
|
|
40
|
+
* business colour with a one-em glyph. Kept as literal declarations rather
|
|
41
|
+
* than `var()` aliases because those class names are hashed per build.
|
|
42
|
+
*
|
|
43
|
+
* The card's frame belongs to the product's `HoverCard`; only what goes
|
|
44
|
+
* inside it is styled here.
|
|
45
|
+
*/
|
|
46
|
+
const CSS = `
|
|
47
|
+
.dsh-anno-chip {
|
|
48
|
+
display: inline;
|
|
49
|
+
max-width: 100%;
|
|
50
|
+
margin: 0 2px;
|
|
51
|
+
color: var(--dsw-alias-state-business-primary);
|
|
52
|
+
font-weight: 500;
|
|
53
|
+
white-space: inherit;
|
|
54
|
+
overflow-wrap: anywhere;
|
|
55
|
+
text-align: inherit;
|
|
56
|
+
}
|
|
57
|
+
.dsh-anno-chip-glyph {
|
|
58
|
+
width: 1em;
|
|
59
|
+
height: 1em;
|
|
60
|
+
margin-right: 4px;
|
|
61
|
+
vertical-align: -0.125em;
|
|
62
|
+
}
|
|
63
|
+
.dsh-anno-card {
|
|
64
|
+
display: flex;
|
|
65
|
+
flex-direction: column;
|
|
66
|
+
gap: 6px;
|
|
67
|
+
font-size: 12px;
|
|
68
|
+
line-height: 1.5;
|
|
69
|
+
text-align: left;
|
|
70
|
+
white-space: normal;
|
|
71
|
+
}
|
|
72
|
+
.dsh-anno-card-shot {
|
|
73
|
+
display: block;
|
|
74
|
+
width: 100%;
|
|
75
|
+
max-height: 132px;
|
|
76
|
+
object-fit: cover;
|
|
77
|
+
object-position: top left;
|
|
78
|
+
border-radius: 6px;
|
|
79
|
+
border: 1px solid color-mix(in srgb, currentColor 14%, transparent);
|
|
80
|
+
}
|
|
81
|
+
.dsh-anno-card-page {
|
|
82
|
+
display: flex;
|
|
83
|
+
flex-direction: column;
|
|
84
|
+
gap: 1px;
|
|
85
|
+
min-width: 0;
|
|
86
|
+
}
|
|
87
|
+
.dsh-anno-card-title {
|
|
88
|
+
font-weight: 600;
|
|
89
|
+
color: var(--dsw-alias-label-primary);
|
|
90
|
+
overflow: hidden;
|
|
91
|
+
text-overflow: ellipsis;
|
|
92
|
+
white-space: nowrap;
|
|
93
|
+
}
|
|
94
|
+
.dsh-anno-card-url {
|
|
95
|
+
color: var(--dsw-alias-label-secondary);
|
|
96
|
+
overflow: hidden;
|
|
97
|
+
text-overflow: ellipsis;
|
|
98
|
+
white-space: nowrap;
|
|
99
|
+
}
|
|
100
|
+
.dsh-anno-card-mark {
|
|
101
|
+
margin: 0;
|
|
102
|
+
padding-left: 8px;
|
|
103
|
+
border-left: 2px solid color-mix(in srgb, currentColor 22%, transparent);
|
|
104
|
+
display: -webkit-box;
|
|
105
|
+
-webkit-box-orient: vertical;
|
|
106
|
+
-webkit-line-clamp: 3;
|
|
107
|
+
overflow: hidden;
|
|
108
|
+
overflow-wrap: anywhere;
|
|
109
|
+
}
|
|
110
|
+
.dsh-anno-card-mark[data-kind="element"] {
|
|
111
|
+
font-family: var(--dsw-font-markdown-code-block, ui-monospace, monospace);
|
|
112
|
+
font-size: 11px;
|
|
113
|
+
}
|
|
114
|
+
.dsh-anno-card-comment {
|
|
115
|
+
color: var(--dsw-alias-label-primary);
|
|
116
|
+
display: -webkit-box;
|
|
117
|
+
-webkit-box-orient: vertical;
|
|
118
|
+
-webkit-line-clamp: 4;
|
|
119
|
+
overflow: hidden;
|
|
120
|
+
overflow-wrap: anywhere;
|
|
121
|
+
}
|
|
122
|
+
.dsh-anno-card-meta {
|
|
123
|
+
display: flex;
|
|
124
|
+
flex-wrap: wrap;
|
|
125
|
+
gap: 6px;
|
|
126
|
+
align-items: center;
|
|
127
|
+
color: var(--dsw-alias-label-secondary);
|
|
128
|
+
font-size: 11px;
|
|
129
|
+
}
|
|
130
|
+
.dsh-anno-card-tag {
|
|
131
|
+
padding: 0 5px;
|
|
132
|
+
border-radius: 4px;
|
|
133
|
+
background: color-mix(in srgb, currentColor 12%, transparent);
|
|
134
|
+
}
|
|
135
|
+
`
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The product's primitives, when this build exposes them.
|
|
139
|
+
*
|
|
140
|
+
* `HoverCard` is the exact interaction an official reference gets — open
|
|
141
|
+
* delay, hover bridge, Escape, body portal, viewport-aware placement — so the
|
|
142
|
+
* chip uses it rather than a lookalike. A build that renamed the module
|
|
143
|
+
* leaves the chip working, with its mention as the fallback tooltip.
|
|
144
|
+
*/
|
|
145
|
+
const primitives = (() => {
|
|
146
|
+
try {
|
|
147
|
+
return require('@deepseek-ai/dsh-client-ui-primitives')
|
|
148
|
+
} catch {
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
})()
|
|
152
|
+
const HoverCard = typeof primitives?.HoverCard === 'function' ? primitives.HoverCard : undefined
|
|
153
|
+
|
|
154
|
+
/** @param {string} label */
|
|
155
|
+
function unescapeLabel(label) {
|
|
156
|
+
return label.replace(/\\(.)/gu, '$1')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Record one line of state on the document element, for support and tests. */
|
|
160
|
+
function mark(attribute, value) {
|
|
161
|
+
try {
|
|
162
|
+
document.documentElement.setAttribute(attribute, value)
|
|
163
|
+
} catch {
|
|
164
|
+
// A document without a root element is not a page we can serve.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Record the last delivery decision on the document element.
|
|
170
|
+
*
|
|
171
|
+
* This module's realm has no console of its own (its output never reaches
|
|
172
|
+
* the page), so the only place a diagnostic can land where a human — or an
|
|
173
|
+
* automation — can read it is the page's own DOM.
|
|
174
|
+
* @param {string} reason
|
|
175
|
+
*/
|
|
176
|
+
function trace(reason) {
|
|
177
|
+
mark('data-dsh-anno-trace', reason)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Find the nearest annotation mention at or after `from`. Mentions carry
|
|
182
|
+
* their own label, so no Host read is needed to draw one.
|
|
183
|
+
*/
|
|
184
|
+
const annoRule = {
|
|
185
|
+
id: 'anno',
|
|
186
|
+
order: 20,
|
|
187
|
+
match(text, from) {
|
|
188
|
+
for (let start = text.indexOf('@', from); start >= 0; start = text.indexOf('@', start + 1)) {
|
|
189
|
+
WIRE.lastIndex = start
|
|
190
|
+
const found = WIRE.exec(text)
|
|
191
|
+
if (found === null) continue
|
|
192
|
+
return {
|
|
193
|
+
id: 'anno',
|
|
194
|
+
start,
|
|
195
|
+
end: start + found[0].length,
|
|
196
|
+
data: { id: found[2], label: unescapeLabel(found[1]) },
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return undefined
|
|
200
|
+
},
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The region glyph: a selection frame with corner handles and a centred
|
|
205
|
+
* bar, drawn at the product's regular one-pixel stroke once scaled to 1em.
|
|
206
|
+
* Decorative, so it inherits the chip's colour and stays out of the tree.
|
|
207
|
+
*/
|
|
208
|
+
function RegionGlyph() {
|
|
209
|
+
return h(
|
|
210
|
+
'svg',
|
|
211
|
+
{
|
|
212
|
+
class: 'dsh-anno-chip-glyph',
|
|
213
|
+
width: 16,
|
|
214
|
+
height: 16,
|
|
215
|
+
viewBox: '0 0 24 24',
|
|
216
|
+
fill: 'none',
|
|
217
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
218
|
+
'aria-hidden': 'true',
|
|
219
|
+
},
|
|
220
|
+
h(
|
|
221
|
+
'g',
|
|
222
|
+
{
|
|
223
|
+
fill: 'none',
|
|
224
|
+
stroke: 'currentColor',
|
|
225
|
+
strokeLinecap: 'round',
|
|
226
|
+
strokeLinejoin: 'round',
|
|
227
|
+
strokeWidth: 1.5,
|
|
228
|
+
},
|
|
229
|
+
h('path', { d: 'M7.5 4.5h9m-9 15h9m-12-12v9m15-9v9' }),
|
|
230
|
+
h('rect', { width: 3.5, height: 3.5, x: 2.75, y: 2.75, rx: 1 }),
|
|
231
|
+
h('rect', { width: 3.5, height: 3.5, x: 17.75, y: 2.75, rx: 1 }),
|
|
232
|
+
h('rect', { width: 3.5, height: 3.5, x: 2.75, y: 17.75, rx: 1 }),
|
|
233
|
+
h('rect', { width: 3.5, height: 3.5, x: 17.75, y: 17.75, rx: 1 }),
|
|
234
|
+
h('rect', { width: 7.5, height: 3.5, x: 8.25, y: 10.25, rx: 1.25 }),
|
|
235
|
+
),
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Annotation reads, cached per page: a card is asked for once and then
|
|
241
|
+
* answered from memory, because the record never changes underneath a
|
|
242
|
+
* transcript.
|
|
243
|
+
*
|
|
244
|
+
* @type {Map<string, { status: 'loading' | 'ready' | 'failed', item?: any, error?: string }>}
|
|
245
|
+
*/
|
|
246
|
+
const ITEMS = new Map()
|
|
247
|
+
/** In-flight reads, so a hover and a prefetch share one request. @type {Map<string, Promise<any>>} */
|
|
248
|
+
const READING = new Map()
|
|
249
|
+
|
|
250
|
+
/** @param {string} id */
|
|
251
|
+
function readAnnotation(id) {
|
|
252
|
+
const known = ITEMS.get(id)
|
|
253
|
+
if (known !== undefined && known.status !== 'loading') return Promise.resolve(known)
|
|
254
|
+
const pending = READING.get(id)
|
|
255
|
+
if (pending !== undefined) return pending
|
|
256
|
+
const request = fetch(`${window.location.origin}${ITEM_PATH}?id=${encodeURIComponent(id)}`, {
|
|
257
|
+
credentials: 'same-origin',
|
|
258
|
+
cache: 'no-store',
|
|
259
|
+
})
|
|
260
|
+
.then(async (response) => {
|
|
261
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
262
|
+
const body = await response.json()
|
|
263
|
+
if (body?.ok !== true || body.item === undefined) throw new Error('unreadable')
|
|
264
|
+
const entry = { status: 'ready', item: body.item }
|
|
265
|
+
ITEMS.set(id, entry)
|
|
266
|
+
return entry
|
|
267
|
+
})
|
|
268
|
+
.catch((error) => {
|
|
269
|
+
const entry = { status: 'failed', error: String(error).slice(0, 80) }
|
|
270
|
+
ITEMS.set(id, entry)
|
|
271
|
+
return entry
|
|
272
|
+
})
|
|
273
|
+
.finally(() => READING.delete(id))
|
|
274
|
+
ITEMS.set(id, { status: 'loading' })
|
|
275
|
+
READING.set(id, request)
|
|
276
|
+
return request
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** @param {number} bytes */
|
|
280
|
+
function readableBytes(bytes) {
|
|
281
|
+
if (!Number.isFinite(bytes) || bytes <= 0) return ''
|
|
282
|
+
if (bytes < 1_024) return `${bytes} B`
|
|
283
|
+
if (bytes < 1_024 * 1_024) return `${Math.round(bytes / 1_024)} KB`
|
|
284
|
+
return `${(bytes / (1_024 * 1_024)).toFixed(1)} MB`
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** @param {string} iso */
|
|
288
|
+
function clockOf(iso) {
|
|
289
|
+
const at = new Date(iso)
|
|
290
|
+
if (Number.isNaN(at.getTime())) return ''
|
|
291
|
+
return at.toLocaleString(undefined, {
|
|
292
|
+
month: 'numeric',
|
|
293
|
+
day: 'numeric',
|
|
294
|
+
hour: '2-digit',
|
|
295
|
+
minute: '2-digit',
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** @param {string} url */
|
|
300
|
+
function pageLabel(url) {
|
|
301
|
+
try {
|
|
302
|
+
const parsed = new URL(url)
|
|
303
|
+
return `${parsed.host}${parsed.pathname}`
|
|
304
|
+
} catch {
|
|
305
|
+
return url
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The line that says what was marked: the quote, the element, or the box.
|
|
311
|
+
* @param {any} item
|
|
312
|
+
*/
|
|
313
|
+
function markOf(item) {
|
|
314
|
+
const anchor = item.anchor ?? {}
|
|
315
|
+
if (anchor.kind === 'selection') return { kind: 'selection', text: `“${anchor.quote ?? ''}”` }
|
|
316
|
+
if (anchor.kind === 'element') {
|
|
317
|
+
const head = `<${anchor.tag ?? 'element'}>`
|
|
318
|
+
const body = anchor.name ?? anchor.text ?? anchor.selector ?? ''
|
|
319
|
+
return { kind: 'element', text: body === '' ? head : `${head} ${body}` }
|
|
320
|
+
}
|
|
321
|
+
const rect = anchor.rect ?? {}
|
|
322
|
+
return { kind: 'region', text: `区域 ${Math.round(rect.w ?? 0)}×${Math.round(rect.h ?? 0)}` }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* One annotation, as the chip's hover card: the crop the browser took, the
|
|
327
|
+
* page it came from, what was marked, and what the user said about it.
|
|
328
|
+
* @param {{ id: string }} props
|
|
329
|
+
*/
|
|
330
|
+
function AnnotationCard({ id }) {
|
|
331
|
+
const [entry, setEntry] = React.useState(() => ITEMS.get(id) ?? { status: 'loading' })
|
|
332
|
+
React.useEffect(() => {
|
|
333
|
+
if (entry.status !== 'loading') return undefined
|
|
334
|
+
let live = true
|
|
335
|
+
void readAnnotation(id).then((next) => {
|
|
336
|
+
if (live) setEntry(next)
|
|
337
|
+
})
|
|
338
|
+
return () => {
|
|
339
|
+
live = false
|
|
340
|
+
}
|
|
341
|
+
}, [id, entry.status])
|
|
342
|
+
|
|
343
|
+
if (entry.status === 'loading') return h('div', { class: 'dsh-anno-card' }, '读取批注…')
|
|
344
|
+
if (entry.status === 'failed') return h('div', { class: 'dsh-anno-card' }, `这条批注读不出来了(${entry.error})`)
|
|
345
|
+
|
|
346
|
+
const item = entry.item
|
|
347
|
+
const mark = markOf(item)
|
|
348
|
+
const bytes = readableBytes(item.image?.bytes ?? 0)
|
|
349
|
+
return h(
|
|
350
|
+
'div',
|
|
351
|
+
{ class: 'dsh-anno-card' },
|
|
352
|
+
item.image === undefined
|
|
353
|
+
? null
|
|
354
|
+
: h('img', {
|
|
355
|
+
class: 'dsh-anno-card-shot',
|
|
356
|
+
src: `${window.location.origin}${item.image.href}`,
|
|
357
|
+
alt: `批注 ${id} 的截图`,
|
|
358
|
+
decoding: 'async',
|
|
359
|
+
}),
|
|
360
|
+
h(
|
|
361
|
+
'div',
|
|
362
|
+
{ class: 'dsh-anno-card-page' },
|
|
363
|
+
h('div', { class: 'dsh-anno-card-title' }, item.title === '' ? pageLabel(item.url) : item.title),
|
|
364
|
+
h('div', { class: 'dsh-anno-card-url' }, pageLabel(item.url)),
|
|
365
|
+
),
|
|
366
|
+
h('p', { class: 'dsh-anno-card-mark', 'data-kind': mark.kind }, mark.text),
|
|
367
|
+
item.comment === '' ? null : h('div', { class: 'dsh-anno-card-comment' }, item.comment),
|
|
368
|
+
h(
|
|
369
|
+
'div',
|
|
370
|
+
{ class: 'dsh-anno-card-meta' },
|
|
371
|
+
item.intent === undefined ? null : h('span', { class: 'dsh-anno-card-tag' }, item.intent),
|
|
372
|
+
h('span', null, item.state),
|
|
373
|
+
h('span', null, clockOf(item.snapshot?.at ?? item.createdAt)),
|
|
374
|
+
item.image === undefined ? null : h('span', null, `截图 ${bytes}`),
|
|
375
|
+
),
|
|
376
|
+
)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* One annotation citation, marked up exactly like the product's
|
|
381
|
+
* `ReferenceChip` in its non-interactive form: an inline `span` whose
|
|
382
|
+
* `title` is the whole mention, a leading decorative glyph, then the label.
|
|
383
|
+
* With `HoverCard` available it becomes the same hover target an official
|
|
384
|
+
* reference is, showing what the user marked instead of just its name.
|
|
385
|
+
*/
|
|
386
|
+
function AnnoReference({ node, raw }) {
|
|
387
|
+
const { id, label } = node.data
|
|
388
|
+
const chip = h(
|
|
389
|
+
'span',
|
|
390
|
+
{
|
|
391
|
+
class: 'dsh-anno-chip',
|
|
392
|
+
'data-ref-chip': 'anno',
|
|
393
|
+
'data-anno-id': id,
|
|
394
|
+
...(HoverCard === undefined ? { title: raw } : {}),
|
|
395
|
+
},
|
|
396
|
+
h(RegionGlyph, null),
|
|
397
|
+
label === '' ? id : label,
|
|
398
|
+
)
|
|
399
|
+
if (HoverCard === undefined) return chip
|
|
400
|
+
return h(HoverCard, {
|
|
401
|
+
anchor: chip,
|
|
402
|
+
content: h(AnnotationCard, { id }),
|
|
403
|
+
inline: true,
|
|
404
|
+
grouped: true,
|
|
405
|
+
openDelayMs: 200,
|
|
406
|
+
})
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The `@` source that owns our chip.
|
|
411
|
+
*
|
|
412
|
+
* A chip is only sendable while some source claims it: submit serializes
|
|
413
|
+
* every occurrence through its owner's codec and refuses the message when
|
|
414
|
+
* that owner is missing. This is the product's own reference source, and its
|
|
415
|
+
* codec is the identity, so the sent text is exactly the mention — which is
|
|
416
|
+
* what the transcript renders and what the Host parses at `agent/pre-step`.
|
|
417
|
+
*/
|
|
418
|
+
const REFERENCE_SOURCE = 'reference'
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Fold queued mentions into the draft the composer already holds.
|
|
422
|
+
*
|
|
423
|
+
* Everything stays in clipboard coordinates: `draft` is the whole document
|
|
424
|
+
* with chips expanded, and each reference is a span inside it. Mentions go
|
|
425
|
+
* after the last line, so a question the user already drafted survives, and
|
|
426
|
+
* a mention the draft already carries is never added twice.
|
|
427
|
+
*
|
|
428
|
+
* @param {any} state live input snapshot (`draft`, `occurrences`, `phase`, `claim`)
|
|
429
|
+
* @param {readonly any[]} items queue entries for one Session
|
|
430
|
+
* @returns {{ text?: string, references?: readonly any[], ack: readonly string[] } | undefined}
|
|
431
|
+
* the document to write and the entries it accounts for, or undefined
|
|
432
|
+
* when the composer cannot take an edit right now
|
|
433
|
+
*/
|
|
434
|
+
function composeDraft(state, items) {
|
|
435
|
+
// A busy composer owns the draft: adjudication and submission (and a
|
|
436
|
+
// pending command claim, whose chip is not part of `references`) would be
|
|
437
|
+
// lost by a wholesale write. The next poll retries.
|
|
438
|
+
if (state === undefined || state === null || state.phase !== 'plain' || state.claim !== undefined) {
|
|
439
|
+
return undefined
|
|
440
|
+
}
|
|
441
|
+
const known = new Set(state.occurrences.map((occurrence) => occurrence.ref))
|
|
442
|
+
const ack = []
|
|
443
|
+
const fresh = []
|
|
444
|
+
for (const item of items) {
|
|
445
|
+
if (typeof item?.id !== 'string' || typeof item?.mention !== 'string' || item.mention === '') continue
|
|
446
|
+
// Accounted for either way: an entry whose mention is already in the
|
|
447
|
+
// draft describes a delivery that happened, so acknowledging is the
|
|
448
|
+
// whole repair.
|
|
449
|
+
ack.push(item.id)
|
|
450
|
+
if (known.has(item.mention)) continue
|
|
451
|
+
known.add(item.mention)
|
|
452
|
+
fresh.push(item)
|
|
453
|
+
}
|
|
454
|
+
if (fresh.length === 0) return { ack }
|
|
455
|
+
let text = state.draft
|
|
456
|
+
const references = [...state.occurrences]
|
|
457
|
+
for (const item of fresh) {
|
|
458
|
+
if (text !== '' && !text.endsWith('\n')) text += '\n'
|
|
459
|
+
references.push({
|
|
460
|
+
source: REFERENCE_SOURCE,
|
|
461
|
+
ref: item.mention,
|
|
462
|
+
offset: text.length,
|
|
463
|
+
length: item.mention.length,
|
|
464
|
+
label: typeof item.label === 'string' && item.label !== '' ? item.label : item.mention,
|
|
465
|
+
clipboardText: item.mention,
|
|
466
|
+
})
|
|
467
|
+
text += item.mention
|
|
468
|
+
}
|
|
469
|
+
return { text, references, ack }
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Tell the Host that these entries are in a composer now. @param {readonly string[]} ids */
|
|
473
|
+
async function acknowledge(ids) {
|
|
474
|
+
await fetch(`${window.location.origin}${QUEUE_ACK_PATH}`, {
|
|
475
|
+
method: 'POST',
|
|
476
|
+
credentials: 'same-origin',
|
|
477
|
+
cache: 'no-store',
|
|
478
|
+
headers: { 'content-type': 'application/json' },
|
|
479
|
+
body: JSON.stringify({ ids }),
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Tell the Host which Session this page is showing.
|
|
485
|
+
*
|
|
486
|
+
* The overlay's default target is the conversation the user is looking at,
|
|
487
|
+
* and the Host cannot see browser focus — this is the only half that can.
|
|
488
|
+
* @param {string} sessionId
|
|
489
|
+
*/
|
|
490
|
+
async function reportFocus(sessionId) {
|
|
491
|
+
const response = await fetch(`${window.location.origin}${FOCUS_PATH}`, {
|
|
492
|
+
method: 'POST',
|
|
493
|
+
credentials: 'same-origin',
|
|
494
|
+
cache: 'no-store',
|
|
495
|
+
headers: { 'content-type': 'application/json' },
|
|
496
|
+
body: JSON.stringify({ sessionId }),
|
|
497
|
+
})
|
|
498
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* The dock seat that delivers queued mentions into its own Session.
|
|
503
|
+
*
|
|
504
|
+
* It is the only place holding both halves of the problem: the Session id
|
|
505
|
+
* and that Session's live input actions. Being mounted is what "the user is
|
|
506
|
+
* looking at this Session" means, so that is exactly when a delivery
|
|
507
|
+
* happens — a chip appears in the composer, visible and removable before it
|
|
508
|
+
* counts, and nothing is sent. A Session the user is not looking at is never
|
|
509
|
+
* touched, and its entries simply wait in the queue.
|
|
510
|
+
*
|
|
511
|
+
* Being mounted is also what the Host needs to know, so the same effect
|
|
512
|
+
* reports the focus.
|
|
513
|
+
*/
|
|
514
|
+
function QueuedMentions({ sessionId, input, inputActions }) {
|
|
515
|
+
// The pull is asynchronous but must not restart on every keystroke, so it
|
|
516
|
+
// reads the newest state through a ref instead of a dependency.
|
|
517
|
+
const latest = React.useRef({ input, inputActions })
|
|
518
|
+
React.useEffect(() => {
|
|
519
|
+
latest.current = { input, inputActions }
|
|
520
|
+
})
|
|
521
|
+
|
|
522
|
+
React.useEffect(() => {
|
|
523
|
+
if (typeof sessionId !== 'string' || sessionId === '') return undefined
|
|
524
|
+
let cancelled = false
|
|
525
|
+
const pull = async () => {
|
|
526
|
+
// A background tab must not take a delivery meant for the one the
|
|
527
|
+
// user is looking at: two tabs of the same Session each hold their own
|
|
528
|
+
// draft, and the queue is a single hand-off.
|
|
529
|
+
if (document.visibilityState === 'hidden') return trace('hidden')
|
|
530
|
+
try {
|
|
531
|
+
const url = `${window.location.origin}${QUEUE_PATH}?session=${encodeURIComponent(sessionId)}`
|
|
532
|
+
const response = await fetch(url, { credentials: 'same-origin', cache: 'no-store' })
|
|
533
|
+
if (!response.ok) return trace(`http-${response.status}`)
|
|
534
|
+
const body = await response.json()
|
|
535
|
+
const items = Array.isArray(body?.items) ? body.items : []
|
|
536
|
+
trace(`queue-${items.length}`)
|
|
537
|
+
if (cancelled || items.length === 0) return
|
|
538
|
+
const plan = composeDraft(latest.current.input, items)
|
|
539
|
+
if (plan === undefined) return trace('busy')
|
|
540
|
+
if (plan.text !== undefined) {
|
|
541
|
+
// A structured document, not text: this is what makes the editor
|
|
542
|
+
// build a chip node instead of a run of mention characters.
|
|
543
|
+
latest.current.inputActions.setDraft({ text: plan.text, references: plan.references })
|
|
544
|
+
trace(`appended-${plan.ack.length}`)
|
|
545
|
+
}
|
|
546
|
+
if (plan.ack.length > 0) await acknowledge(plan.ack)
|
|
547
|
+
} catch (error) {
|
|
548
|
+
trace(`throw-${String(error).slice(0, 48)}`)
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
const announce = () => {
|
|
552
|
+
if (document.visibilityState === 'hidden') return
|
|
553
|
+
void reportFocus(sessionId).catch((error) => trace(`focus-${String(error).slice(0, 40)}`))
|
|
554
|
+
}
|
|
555
|
+
announce()
|
|
556
|
+
void pull()
|
|
557
|
+
const timer = setInterval(() => void pull(), QUEUE_POLL_MS)
|
|
558
|
+
// A hidden tab has its timers frozen, so the moment the user comes back
|
|
559
|
+
// is also the moment to look.
|
|
560
|
+
const onVisible = () => {
|
|
561
|
+
if (document.visibilityState !== 'visible') return
|
|
562
|
+
announce()
|
|
563
|
+
void pull()
|
|
564
|
+
}
|
|
565
|
+
document.addEventListener('visibilitychange', onVisible)
|
|
566
|
+
window.addEventListener('focus', onVisible)
|
|
567
|
+
return () => {
|
|
568
|
+
cancelled = true
|
|
569
|
+
clearInterval(timer)
|
|
570
|
+
document.removeEventListener('visibilitychange', onVisible)
|
|
571
|
+
window.removeEventListener('focus', onVisible)
|
|
572
|
+
}
|
|
573
|
+
}, [sessionId])
|
|
574
|
+
|
|
575
|
+
return null
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return {
|
|
579
|
+
inject: ['slots', 'uiConversation'],
|
|
580
|
+
/** The draft composition this plugin owns; exported for its unit test. */
|
|
581
|
+
composeDraft,
|
|
582
|
+
apply(ctx) {
|
|
583
|
+
mark('data-dsh-anno-hovercard', HoverCard === undefined ? 'unavailable' : 'primitives')
|
|
584
|
+
// One stylesheet for the whole page, removed when the plugin disposes.
|
|
585
|
+
ctx.effect(() => {
|
|
586
|
+
const style = document.createElement('style')
|
|
587
|
+
style.dataset.dshAnno = 'chip'
|
|
588
|
+
style.textContent = CSS
|
|
589
|
+
document.head.appendChild(style)
|
|
590
|
+
return () => {
|
|
591
|
+
style.remove()
|
|
592
|
+
}
|
|
593
|
+
}, 'dsh-marker: chip styles')
|
|
594
|
+
|
|
595
|
+
ctx.effect(() => ctx.uiConversation.userText.register(annoRule), 'dsh-marker: user-text rule')
|
|
596
|
+
ctx.slots.inject('conversation.user.text.node', function* () {
|
|
597
|
+
yield ctx.slots.register({ name: 'conversation.user.text.node', key: 'anno', priority: 100 }, AnnoReference)
|
|
598
|
+
})
|
|
599
|
+
|
|
600
|
+
// The composer zone, not the composer dock: it is rendered for the
|
|
601
|
+
// session body's hero and composer alike, and it carries the live input
|
|
602
|
+
// state and actions the delivery needs.
|
|
603
|
+
ctx.slots.inject('conversation.input.dock', function* () {
|
|
604
|
+
yield ctx.slots.register({ name: 'conversation.input.dock', id: 'dsh-anno-queue', order: 90 }, QueuedMentions)
|
|
605
|
+
})
|
|
606
|
+
},
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
})
|