dsh-duet 0.1.3 → 0.1.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 +1 -1
- package/package.json +2 -2
- package/src/client/audio.mjs +1 -1
- package/src/client/browser.mjs +42 -20
- package/src/client/composer-bridge.mjs +201 -0
- package/src/client/composer.mjs +23 -0
- package/src/client/connection-hint.mjs +8 -3
- package/src/client/entry.js +1 -408
- package/src/client/rpc.mjs +6 -2
- package/src/client/settings.html +3 -3
- package/src/client/tutorial-adapter.mjs +17 -12
- package/src/client/tutorial.mjs +1 -1
- package/src/host/assets.mjs +2 -0
- package/src/host/endpoint-discovery.mjs +185 -0
- package/src/host/feedback-files.mjs +5 -5
- package/src/host/feedback-store.mjs +3 -3
- package/src/host/notices.mjs +1 -1
- package/src/host/plugin.mjs +43 -16
- package/src/host/proxy.mjs +54 -17
- package/src/host/service-defaults.mjs +12 -4
- package/src/host/service-info.mjs +2 -1
- package/src/host/session-metadata.mjs +17 -0
- package/src/host/settings.mjs +19 -7
- package/src/host/storage-paths.mjs +28 -0
- package/src/host/trace.mjs +2 -2
- package/src/shared/connection-guidance.mjs +9 -0
- package/src/shared/state.mjs +1 -1
package/src/client/entry.js
CHANGED
|
@@ -8,30 +8,10 @@ window.__ModuleLoader__.load({
|
|
|
8
8
|
const inject = ["sessions", "conversation", "uiSession", "workspaces"]
|
|
9
9
|
|
|
10
10
|
function apply(ctx) {
|
|
11
|
-
const focusedSession = () => {
|
|
12
|
-
const snapshot = ctx.sessions.list.getSnapshot()
|
|
13
|
-
if ('current' in snapshot) return snapshot.current || null
|
|
14
|
-
const selected = Object.entries(snapshot.byId || {}).filter(([, row]) => row.retainedBy?.mainView > 0)
|
|
15
|
-
return selected.length === 1 ? selected[0][0] : null
|
|
16
|
-
}
|
|
17
|
-
const browserClientId = crypto.randomUUID()
|
|
18
|
-
// A hung fetch must not permanently hold heartbeatRunning/pollRunning.
|
|
19
|
-
const bridgeFetch = async (url, options = {}, timeoutMs = 8000) => {
|
|
20
|
-
const request = new AbortController()
|
|
21
|
-
const abort = () => request.abort()
|
|
22
|
-
options.signal?.addEventListener('abort', abort, { once: true })
|
|
23
|
-
if (options.signal?.aborted) abort()
|
|
24
|
-
const timer = setTimeout(abort, timeoutMs)
|
|
25
|
-
try { return await fetch(url, { ...options, signal: request.signal }) }
|
|
26
|
-
finally {
|
|
27
|
-
clearTimeout(timer)
|
|
28
|
-
options.signal?.removeEventListener('abort', abort)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
11
|
ctx.effect(() => {
|
|
32
12
|
let active = true
|
|
33
13
|
let dispose
|
|
34
|
-
import('/duet/assets/client/browser.mjs?v=0.1.
|
|
14
|
+
import('/duet/assets/client/browser.mjs?v=0.1.4').then(module => {
|
|
35
15
|
if (active) dispose = module.mountDuet(ctx)
|
|
36
16
|
}).catch(error => console.warn('duet controls could not load:', error))
|
|
37
17
|
return () => { active = false; dispose?.() }
|
|
@@ -70,393 +50,6 @@ window.__ModuleLoader__.load({
|
|
|
70
50
|
}
|
|
71
51
|
}, "duet-client: mount main page entry")
|
|
72
52
|
|
|
73
|
-
ctx.effect(() => {
|
|
74
|
-
if (typeof BroadcastChannel === "undefined") return () => {}
|
|
75
|
-
const channel = new BroadcastChannel("dsh-duplex-control-v1")
|
|
76
|
-
|
|
77
|
-
const currentComposer = () => {
|
|
78
|
-
const sessionId = focusedSession()
|
|
79
|
-
if (typeof sessionId !== "string") throw new Error("Harness currently has no selected session")
|
|
80
|
-
const sessionContext = ctx.sessions.scope(sessionId)
|
|
81
|
-
if (sessionContext === undefined) throw new Error(`Harness session ${sessionId} is not ready`)
|
|
82
|
-
return {
|
|
83
|
-
sessionId,
|
|
84
|
-
input: ctx.conversation.input.for(sessionContext),
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const composerState = (sessionId, input) => {
|
|
89
|
-
const state = input.state.getSnapshot()
|
|
90
|
-
return {
|
|
91
|
-
session_id: sessionId,
|
|
92
|
-
draft: state.draft,
|
|
93
|
-
phase: state.phase,
|
|
94
|
-
draft_revision: state.draftRev,
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
channel.onmessage = (event) => {
|
|
99
|
-
const message = event.data
|
|
100
|
-
if (
|
|
101
|
-
message === null
|
|
102
|
-
|| typeof message !== "object"
|
|
103
|
-
|| message.source !== "duplex-control-page"
|
|
104
|
-
|| typeof message.request_id !== "string"
|
|
105
|
-
) return
|
|
106
|
-
if (!["composer/get", "composer/set", "composer/clear"].includes(message.type)) return
|
|
107
|
-
|
|
108
|
-
const respond = (payload) => channel.postMessage({
|
|
109
|
-
source: "harness-main-page",
|
|
110
|
-
type: "composer/result",
|
|
111
|
-
request_id: message.request_id,
|
|
112
|
-
...payload,
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const { sessionId, input } = currentComposer()
|
|
117
|
-
if (message.type === "composer/set") {
|
|
118
|
-
const text = String(message.text ?? "")
|
|
119
|
-
if (text.length > 100_000) throw new Error("Draft is too large")
|
|
120
|
-
const before = input.state.getSnapshot()
|
|
121
|
-
if (before.phase !== "plain") {
|
|
122
|
-
throw new Error(`Harness input is busy (${before.phase})`)
|
|
123
|
-
}
|
|
124
|
-
input.setDraft(message.mode === "append" ? `${before.draft}${text}` : text)
|
|
125
|
-
} else if (message.type === "composer/clear") {
|
|
126
|
-
const before = input.state.getSnapshot()
|
|
127
|
-
if (before.phase !== "plain") {
|
|
128
|
-
throw new Error(`Harness input is busy (${before.phase})`)
|
|
129
|
-
}
|
|
130
|
-
input.setDraft("")
|
|
131
|
-
}
|
|
132
|
-
respond({ ok: true, composer: composerState(sessionId, input) })
|
|
133
|
-
} catch (error) {
|
|
134
|
-
respond({ ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return () => channel.close()
|
|
139
|
-
}, "duet-client: bridge current composer")
|
|
140
|
-
|
|
141
|
-
ctx.effect(() => {
|
|
142
|
-
const controller = new AbortController()
|
|
143
|
-
const clientId = browserClientId
|
|
144
|
-
let pollRunning = false
|
|
145
|
-
let retryTimer
|
|
146
|
-
|
|
147
|
-
const digest = async (text) => {
|
|
148
|
-
const bytes = new TextEncoder().encode(text)
|
|
149
|
-
const hash = await crypto.subtle.digest("SHA-256", bytes)
|
|
150
|
-
return [...new Uint8Array(hash)].map(value => value.toString(16).padStart(2, "0")).join("")
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const composerForSession = async (sessionId) => {
|
|
154
|
-
await ctx.sessions.refresh()
|
|
155
|
-
const sessionContext = ctx.sessions.scope(sessionId)
|
|
156
|
-
if (sessionContext === undefined) {
|
|
157
|
-
const error = new Error(`Harness session ${sessionId} is not ready`)
|
|
158
|
-
error.code = "session_not_ready"
|
|
159
|
-
throw error
|
|
160
|
-
}
|
|
161
|
-
return ctx.conversation.input.for(sessionContext)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const composerState = async (sessionId, input) => {
|
|
165
|
-
const state = input.state.getSnapshot()
|
|
166
|
-
return {
|
|
167
|
-
session_id: sessionId,
|
|
168
|
-
text: state.draft,
|
|
169
|
-
phase: state.phase,
|
|
170
|
-
revision: state.draftRev,
|
|
171
|
-
hash: await digest(state.draft),
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const execute = async (request) => {
|
|
176
|
-
const input = await composerForSession(request.session_id)
|
|
177
|
-
const before = await composerState(request.session_id, input)
|
|
178
|
-
const checkFocus = () => {
|
|
179
|
-
if (request.require_focus && focusedSession() !== request.session_id) {
|
|
180
|
-
const error = new Error("Focused session changed; read input_status again")
|
|
181
|
-
error.code = "focus_conflict"
|
|
182
|
-
throw error
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
checkFocus()
|
|
186
|
-
if (["set", "clear", "consume"].includes(request.type)) {
|
|
187
|
-
const overwrite = request.type === "set" && request.overwrite === true
|
|
188
|
-
if (before.phase !== "plain") {
|
|
189
|
-
const error = new Error(`Harness input is busy (${before.phase})`)
|
|
190
|
-
error.code = "composer_busy"
|
|
191
|
-
throw error
|
|
192
|
-
}
|
|
193
|
-
if (!overwrite && (
|
|
194
|
-
before.revision !== request.expected_revision
|
|
195
|
-
|| before.hash !== request.expected_hash
|
|
196
|
-
)) {
|
|
197
|
-
const error = new Error("Harness input changed before the requested edit")
|
|
198
|
-
error.code = "draft_conflict"
|
|
199
|
-
throw error
|
|
200
|
-
}
|
|
201
|
-
// Hashing yields to the browser. Recheck the live draft immediately before
|
|
202
|
-
// the synchronous write so a keystroke during hashing cannot be overwritten.
|
|
203
|
-
const live = input.state.getSnapshot()
|
|
204
|
-
if (!overwrite && (live.draftRev !== before.revision || live.draft !== before.text || live.phase !== before.phase)) {
|
|
205
|
-
const error = new Error("Harness input changed during snapshot")
|
|
206
|
-
error.code = "draft_conflict"
|
|
207
|
-
throw error
|
|
208
|
-
}
|
|
209
|
-
if (live.phase !== "plain") {
|
|
210
|
-
const error = new Error("Harness input is busy")
|
|
211
|
-
error.code = "composer_busy"
|
|
212
|
-
throw error
|
|
213
|
-
}
|
|
214
|
-
checkFocus()
|
|
215
|
-
// DSH 0.1.5 adds generic attachments. The host submit RPC below is
|
|
216
|
-
// text-only: do not clear/send just the text and silently omit files.
|
|
217
|
-
// Older DSH exposes the same browser-owned items as imageIds.
|
|
218
|
-
if (request.type === "consume" && (live.attachmentIds ?? live.imageIds ?? []).length > 0) {
|
|
219
|
-
const error = new Error("输入框含有附件,请在 Harness 页面发送;语音发送目前只支持纯文本")
|
|
220
|
-
error.code = "composer_attachments_unsupported"
|
|
221
|
-
throw error
|
|
222
|
-
}
|
|
223
|
-
const consumed = request.type === "consume" ? before : null
|
|
224
|
-
input.setDraft(["clear", "consume"].includes(request.type) ? "" : String(request.text ?? ""))
|
|
225
|
-
await new Promise(resolve => setTimeout(resolve, 0))
|
|
226
|
-
const after = await composerState(request.session_id, input)
|
|
227
|
-
if (consumed !== null) {
|
|
228
|
-
return {
|
|
229
|
-
...after,
|
|
230
|
-
consumed_text: consumed.text,
|
|
231
|
-
consumed_revision: consumed.revision,
|
|
232
|
-
consumed_hash: consumed.hash,
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return after
|
|
236
|
-
} else if (request.type !== "get") {
|
|
237
|
-
const error = new Error(`Unsupported composer request: ${request.type}`)
|
|
238
|
-
error.code = "unsupported_composer_operation"
|
|
239
|
-
throw error
|
|
240
|
-
}
|
|
241
|
-
return before
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const complete = async (requestId, payload) => {
|
|
245
|
-
await bridgeFetch(`/duplex-control/api/composer/requests/${encodeURIComponent(requestId)}/complete`, {
|
|
246
|
-
method: "POST",
|
|
247
|
-
headers: { "Content-Type": "application/json" },
|
|
248
|
-
body: JSON.stringify({ client_id: clientId, ...payload }),
|
|
249
|
-
cache: "no-store",
|
|
250
|
-
signal: controller.signal,
|
|
251
|
-
})
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const processRequest = async (candidate) => {
|
|
255
|
-
const claimResponse = await bridgeFetch(
|
|
256
|
-
`/duplex-control/api/composer/requests/${encodeURIComponent(candidate.id)}/claim`,
|
|
257
|
-
{
|
|
258
|
-
method: "POST",
|
|
259
|
-
headers: { "Content-Type": "application/json" },
|
|
260
|
-
body: JSON.stringify({ client_id: clientId }),
|
|
261
|
-
cache: "no-store",
|
|
262
|
-
signal: controller.signal,
|
|
263
|
-
},
|
|
264
|
-
)
|
|
265
|
-
if (claimResponse.status === 404 || claimResponse.status === 409) return
|
|
266
|
-
if (!claimResponse.ok) throw new Error(`Composer claim failed: HTTP ${claimResponse.status}`)
|
|
267
|
-
const request = (await claimResponse.json()).request
|
|
268
|
-
try {
|
|
269
|
-
const composer = await execute(request)
|
|
270
|
-
await complete(request.id, { ok: true, composer })
|
|
271
|
-
} catch (error) {
|
|
272
|
-
await complete(request.id, {
|
|
273
|
-
ok: false,
|
|
274
|
-
error_code: error?.code || "composer_operation_failed",
|
|
275
|
-
error: error instanceof Error ? error.message : String(error),
|
|
276
|
-
status: error?.code === "session_not_ready" ? 503 : 409,
|
|
277
|
-
})
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const poll = async () => {
|
|
282
|
-
if (pollRunning || controller.signal.aborted) return
|
|
283
|
-
clearTimeout(retryTimer)
|
|
284
|
-
pollRunning = true
|
|
285
|
-
let succeeded = false
|
|
286
|
-
try {
|
|
287
|
-
const sessionId = focusedSession() || ''
|
|
288
|
-
const visibility = typeof document === 'undefined' ? 'unknown' : document.visibilityState
|
|
289
|
-
const query = `wait=1&client_id=${encodeURIComponent(clientId)}&session_id=${encodeURIComponent(sessionId)}&visibility=${encodeURIComponent(visibility)}`
|
|
290
|
-
const response = await bridgeFetch(`/duplex-control/api/composer/requests?${query}`, {
|
|
291
|
-
cache: "no-store",
|
|
292
|
-
signal: controller.signal,
|
|
293
|
-
}, 30_000)
|
|
294
|
-
if (!response.ok) return
|
|
295
|
-
const payload = await response.json()
|
|
296
|
-
await Promise.all((payload.requests || []).map(processRequest))
|
|
297
|
-
// Old hosts return an immediate empty list: retain a retry delay
|
|
298
|
-
// rather than spinning if a browser/host upgrade is staggered.
|
|
299
|
-
succeeded = payload.long_poll_supported === true
|
|
300
|
-
} catch (error) {
|
|
301
|
-
if (!controller.signal.aborted) console.warn("duet composer RPC failed:", error)
|
|
302
|
-
} finally {
|
|
303
|
-
pollRunning = false
|
|
304
|
-
if (!controller.signal.aborted) {
|
|
305
|
-
// Successful long polls re-arm without a background-tab timer.
|
|
306
|
-
if (succeeded) void poll()
|
|
307
|
-
else retryTimer = setTimeout(() => { void poll() }, 1000)
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
void poll()
|
|
313
|
-
const resume = () => { void poll() }
|
|
314
|
-
window.addEventListener?.('online', resume)
|
|
315
|
-
window.addEventListener?.('pageshow', resume)
|
|
316
|
-
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', resume)
|
|
317
|
-
return () => {
|
|
318
|
-
controller.abort()
|
|
319
|
-
clearTimeout(retryTimer)
|
|
320
|
-
window.removeEventListener?.('online', resume)
|
|
321
|
-
window.removeEventListener?.('pageshow', resume)
|
|
322
|
-
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', resume)
|
|
323
|
-
}
|
|
324
|
-
}, "duet-client: bridge server composer RPC")
|
|
325
|
-
|
|
326
|
-
ctx.effect(() => {
|
|
327
|
-
const controller = new AbortController()
|
|
328
|
-
const focusClientId = browserClientId
|
|
329
|
-
let lastEpoch
|
|
330
|
-
let lastRevision = -1
|
|
331
|
-
let lastPublishedSession
|
|
332
|
-
let pollRunning = false
|
|
333
|
-
let heartbeatRunning = false
|
|
334
|
-
let publishRunning = false
|
|
335
|
-
let publishPending = false
|
|
336
|
-
let publishReady = false
|
|
337
|
-
|
|
338
|
-
const selectedSession = focusedSession
|
|
339
|
-
|
|
340
|
-
const observeEpoch = (epoch) => {
|
|
341
|
-
if (typeof epoch !== "string" || epoch === lastEpoch) return
|
|
342
|
-
lastEpoch = epoch
|
|
343
|
-
lastRevision = -1
|
|
344
|
-
lastPublishedSession = undefined
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const heartbeat = async () => {
|
|
348
|
-
if (heartbeatRunning) return
|
|
349
|
-
heartbeatRunning = true
|
|
350
|
-
try {
|
|
351
|
-
const response = await bridgeFetch("/duplex-control/api/browser-clients/heartbeat", {
|
|
352
|
-
method: "POST",
|
|
353
|
-
headers: { "Content-Type": "application/json" },
|
|
354
|
-
body: JSON.stringify({
|
|
355
|
-
client_id: focusClientId,
|
|
356
|
-
session_id: selectedSession(),
|
|
357
|
-
visibility: document.visibilityState,
|
|
358
|
-
}),
|
|
359
|
-
cache: "no-store",
|
|
360
|
-
signal: controller.signal,
|
|
361
|
-
})
|
|
362
|
-
if (response.ok) observeEpoch((await response.json()).focus_epoch)
|
|
363
|
-
} catch (error) {
|
|
364
|
-
if (!controller.signal.aborted) console.warn("duet heartbeat failed:", error)
|
|
365
|
-
} finally {
|
|
366
|
-
heartbeatRunning = false
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const publishSelection = async () => {
|
|
371
|
-
if (!publishReady) return
|
|
372
|
-
if (publishRunning) {
|
|
373
|
-
publishPending = true
|
|
374
|
-
return
|
|
375
|
-
}
|
|
376
|
-
publishRunning = true
|
|
377
|
-
try {
|
|
378
|
-
do {
|
|
379
|
-
publishPending = false
|
|
380
|
-
const sessionId = selectedSession()
|
|
381
|
-
if (sessionId === lastPublishedSession) continue
|
|
382
|
-
const response = await bridgeFetch("/duplex-control/api/focus", {
|
|
383
|
-
method: "POST",
|
|
384
|
-
headers: { "Content-Type": "application/json" },
|
|
385
|
-
body: JSON.stringify({ session_id: sessionId }),
|
|
386
|
-
cache: "no-store",
|
|
387
|
-
signal: controller.signal,
|
|
388
|
-
})
|
|
389
|
-
if (!response.ok) {
|
|
390
|
-
const payload = await response.json().catch(() => ({}))
|
|
391
|
-
throw new Error(payload.detail || `HTTP ${response.status}`)
|
|
392
|
-
}
|
|
393
|
-
lastPublishedSession = sessionId
|
|
394
|
-
} while (publishPending)
|
|
395
|
-
} catch (error) {
|
|
396
|
-
if (!controller.signal.aborted) console.warn("duet selection publish failed:", error)
|
|
397
|
-
} finally {
|
|
398
|
-
publishRunning = false
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
const poll = async () => {
|
|
403
|
-
if (pollRunning) return
|
|
404
|
-
pollRunning = true
|
|
405
|
-
try {
|
|
406
|
-
const response = await bridgeFetch("/duplex-control/api/focus", {
|
|
407
|
-
cache: "no-store",
|
|
408
|
-
signal: controller.signal,
|
|
409
|
-
})
|
|
410
|
-
if (!response.ok) return
|
|
411
|
-
const focus = await response.json()
|
|
412
|
-
observeEpoch(focus.focus_epoch)
|
|
413
|
-
if (!Number.isInteger(focus.revision) || focus.revision <= lastRevision) return
|
|
414
|
-
if (focus.revision === 0 || typeof focus.session_id !== "string") {
|
|
415
|
-
lastRevision = focus.revision
|
|
416
|
-
return
|
|
417
|
-
}
|
|
418
|
-
await ctx.sessions.refresh()
|
|
419
|
-
if (ctx.sessions.list.getSnapshot().byId[focus.session_id] === undefined) return
|
|
420
|
-
lastPublishedSession = focus.session_id
|
|
421
|
-
// New DSH separates controller selection from the visible panel.
|
|
422
|
-
// Use the UI navigation API so a selected session is not hidden
|
|
423
|
-
// behind the empty-workspace/settings panel while RPC edits it.
|
|
424
|
-
const navigation = typeof ctx.get === "function" ? ctx.get("uiWorkspace") : ctx.uiWorkspace
|
|
425
|
-
if (navigation?.openSession) navigation.openSession(focus.session_id)
|
|
426
|
-
else ctx.sessions.open(focus.session_id)
|
|
427
|
-
lastRevision = focus.revision
|
|
428
|
-
} catch (error) {
|
|
429
|
-
if (!controller.signal.aborted) console.warn("duet focus sync failed:", error)
|
|
430
|
-
} finally {
|
|
431
|
-
pollRunning = false
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const unsubscribe = ctx.sessions.list.subscribe(() => { void publishSelection() })
|
|
436
|
-
void poll().finally(() => {
|
|
437
|
-
publishReady = true
|
|
438
|
-
void publishSelection()
|
|
439
|
-
})
|
|
440
|
-
void heartbeat()
|
|
441
|
-
const timer = setInterval(() => {
|
|
442
|
-
void poll()
|
|
443
|
-
void publishSelection()
|
|
444
|
-
}, 500)
|
|
445
|
-
const heartbeatTimer = setInterval(() => { void heartbeat() }, 1000)
|
|
446
|
-
const onVisibilityChange = () => { void heartbeat(); void poll(); void publishSelection() }
|
|
447
|
-
document.addEventListener("visibilitychange", onVisibilityChange)
|
|
448
|
-
window.addEventListener?.('online', onVisibilityChange)
|
|
449
|
-
window.addEventListener?.('pageshow', onVisibilityChange)
|
|
450
|
-
return () => {
|
|
451
|
-
controller.abort()
|
|
452
|
-
clearInterval(timer)
|
|
453
|
-
clearInterval(heartbeatTimer)
|
|
454
|
-
document.removeEventListener("visibilitychange", onVisibilityChange)
|
|
455
|
-
window.removeEventListener?.('online', onVisibilityChange)
|
|
456
|
-
window.removeEventListener?.('pageshow', onVisibilityChange)
|
|
457
|
-
unsubscribe()
|
|
458
|
-
}
|
|
459
|
-
}, "duet-client: synchronize host and browser focus")
|
|
460
53
|
}
|
|
461
54
|
|
|
462
55
|
exports.apply = apply
|
package/src/client/rpc.mjs
CHANGED
|
@@ -20,13 +20,14 @@ export function validateRPC(request) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export class HarnessRPCExecutor {
|
|
23
|
-
constructor(send, fetchImpl = fetch, diagnostic = () => {}, localComposer = null, catalogOrder = rows => orderSessionCatalog(rows, readWorkspaceView())) {
|
|
23
|
+
constructor(send, fetchImpl = fetch, diagnostic = () => {}, localComposer = null, catalogOrder = rows => orderSessionCatalog(rows, readWorkspaceView()), binding = {}) {
|
|
24
24
|
// Native Window.fetch rejects an RPCExecutor receiver (Illegal invocation).
|
|
25
25
|
// Call the captured function plainly, not as an instance method.
|
|
26
26
|
this.send = send; this.fetch = (...args) => fetchImpl(...args); this.closed = false
|
|
27
27
|
this.diagnostic = diagnostic
|
|
28
28
|
this.localComposer = localComposer
|
|
29
29
|
this.catalogOrder = catalogOrder
|
|
30
|
+
this.binding = binding
|
|
30
31
|
this.connectionId = null; this.seen = new Map(); this.retired = new Set(); this.controllers = new Set()
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -75,13 +76,16 @@ export class HarnessRPCExecutor {
|
|
|
75
76
|
method: request.method, credentials: 'same-origin', redirect: 'error',
|
|
76
77
|
cache: 'no-store', signal: controller.signal,
|
|
77
78
|
headers: { 'Content-Type': 'application/json', 'X-Duplex-Control-Client': 'realtime-client-rpc-v1',
|
|
78
|
-
'X-Duet-Native-Interactions': '1'
|
|
79
|
+
'X-Duet-Native-Interactions': '1',
|
|
80
|
+
...(this.binding.id ? { 'X-Duet-Browser-Client': this.binding.id } : {}) },
|
|
79
81
|
...(request.method !== 'GET' ? { body: JSON.stringify(request.payload ?? {}) } : {}),
|
|
80
82
|
})
|
|
81
83
|
const text = await response.text()
|
|
82
84
|
if (new TextEncoder().encode(text).length > MAX_BYTES - 512) throw new Error('harness_rpc_response_too_large')
|
|
83
85
|
const body = JSON.parse(text)
|
|
84
86
|
if (!body || typeof body !== 'object' || Array.isArray(body)) throw new Error('invalid_harness_rpc_body')
|
|
87
|
+
controller.signal.throwIfAborted()
|
|
88
|
+
if (response.ok && this.binding.completed) await this.binding.completed(request, body, controller.signal)
|
|
85
89
|
if (response.status === 200 && request.path === '/api/sessions' && Array.isArray(body.sessions)) {
|
|
86
90
|
// Both startup prompt and nth_N resolution read this same browser-local
|
|
87
91
|
// order, including manual drag order; archived rows cannot consume ranks.
|
package/src/client/settings.html
CHANGED
|
@@ -366,7 +366,7 @@
|
|
|
366
366
|
}
|
|
367
367
|
$('openTutorial').onclick = async () => {
|
|
368
368
|
try {
|
|
369
|
-
const {requestTutorial} = await import('/duet/assets/client/tutorial-request.mjs?v=0.1.
|
|
369
|
+
const {requestTutorial} = await import('/duet/assets/client/tutorial-request.mjs?v=0.1.4');
|
|
370
370
|
requestTutorial(localStorage);
|
|
371
371
|
$('tutorialStatus').textContent = '请回到 DSH 页面继续教学。';
|
|
372
372
|
} catch { $('tutorialStatus').textContent = '暂时无法打开教学,请允许浏览器存储后重试。'; }
|
|
@@ -375,11 +375,11 @@
|
|
|
375
375
|
setInterval(() => { loadDuetConfig().catch(() => { $('duetConnection').textContent = '语音连接信息不可用'; }); }, 2000);
|
|
376
376
|
void loadNotices();
|
|
377
377
|
setInterval(loadNotices, 60000);
|
|
378
|
-
import('/duet/assets/client/shortcuts.mjs?v=0.1.
|
|
378
|
+
import('/duet/assets/client/shortcuts.mjs?v=0.1.4').then(({mountShortcutSettings}) => {
|
|
379
379
|
const dispose = mountShortcutSettings({speaker:$('speakerShortcut'),mic:$('micShortcut'),clearSpeaker:$('clearSpeakerShortcut'),clearMic:$('clearMicShortcut'),status:$('shortcutStatus')});
|
|
380
380
|
window.addEventListener('pagehide', e => {if(!e.persisted)dispose()}, {once:true});
|
|
381
381
|
}).catch(() => { $('shortcutStatus').textContent = '暂时无法加载快捷键设置,请刷新重试。'; });
|
|
382
|
-
import('/duet/assets/client/microphone.mjs?v=0.1.
|
|
382
|
+
import('/duet/assets/client/microphone.mjs?v=0.1.4').then(({mountMicrophoneSettings}) => {
|
|
383
383
|
const dispose = mountMicrophoneSettings({select:$('microphoneDevice'),refresh:$('refreshMicrophones'),status:$('microphoneStatus')});
|
|
384
384
|
window.addEventListener('pagehide', dispose, {once:true});
|
|
385
385
|
}).catch(() => { $('microphoneStatus').textContent = '暂时无法加载麦克风设置,请刷新重试。'; });
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/** DSH browser services plus existing voice connection; never opens a microphone. */
|
|
2
|
-
import { PLUGIN_VERSION } from '../shared/state.mjs?v=0.1.
|
|
3
|
-
import { focusedSession } from './composer.mjs?v=0.1.
|
|
4
|
-
import { requireWorkspace, workspaceIssue } from './workspaces.mjs?v=0.1.
|
|
5
|
-
|
|
2
|
+
import { PLUGIN_VERSION } from '../shared/state.mjs?v=0.1.4'
|
|
3
|
+
import { focusedSession } from './composer.mjs?v=0.1.4'
|
|
4
|
+
import { requireWorkspace, workspaceIssue } from './workspaces.mjs?v=0.1.4'
|
|
5
|
+
import { startComposerBridge } from './composer-bridge.mjs?v=0.1.4'
|
|
6
|
+
export function tutorialAdapter(ctx, state, audio, pollReady, startBridge = startComposerBridge) {
|
|
6
7
|
let narration
|
|
7
8
|
const completed = new Map()
|
|
8
9
|
const check = signal => signal.throwIfAborted()
|
|
@@ -17,10 +18,10 @@ export function tutorialAdapter(ctx, state, audio, pollReady) {
|
|
|
17
18
|
while(Date.now()<deadline){check(signal);if(await fn())return;await wait(150,signal)}
|
|
18
19
|
throw Error('等待超时,教学已停止;不会自动重新发送')
|
|
19
20
|
}
|
|
20
|
-
const api = async (path, signal, method='GET', body) => {
|
|
21
|
+
const api = async (path, signal, method='GET', body, owner) => {
|
|
21
22
|
check(signal)
|
|
22
23
|
const r=await fetch('/duplex-control/api/'+path,{method,signal,cache:'no-store',credentials:'same-origin',
|
|
23
|
-
headers:{'Content-Type':'application/json'},...(body?{body:JSON.stringify(body)}:{})})
|
|
24
|
+
headers:{'Content-Type':'application/json', 'X-Duet-Browser-Client': owner || 'tutorial-local'},...(body?{body:JSON.stringify(body)}:{})})
|
|
24
25
|
const d=await r.json();check(signal)
|
|
25
26
|
if(!r.ok)throw Error(d.detail||d.error_code||'DSH 请求失败')
|
|
26
27
|
return d
|
|
@@ -57,12 +58,16 @@ export function tutorialAdapter(ctx, state, audio, pollReady) {
|
|
|
57
58
|
async write(id,text,expected,signal){check(signal);safeSnapshot(id,expected);input(id).setDraft(text)},
|
|
58
59
|
async submit(id,expected,signal){
|
|
59
60
|
safeSnapshot(id,expected)
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
const owner = crypto.randomUUID()
|
|
62
|
+
const stop = startBridge(ctx, owner)
|
|
63
|
+
try {
|
|
64
|
+
const {composer}=await api(`sessions/${encodeURIComponent(id)}/composer?require_focus=true`,signal,'GET',undefined,owner)
|
|
65
|
+
safeSnapshot(id,expected)
|
|
66
|
+
if(composer.text!==expected)throw Error('教学草稿不一致,未发送')
|
|
67
|
+
const {job}=await api(`sessions/${encodeURIComponent(id)}/composer/submit`,signal,'POST',{
|
|
68
|
+
expected_revision:composer.revision,expected_hash:composer.hash,
|
|
69
|
+
},owner);return job
|
|
70
|
+
} finally { stop() }
|
|
66
71
|
},
|
|
67
72
|
async enableBroadcast(signal){
|
|
68
73
|
await pollReady(signal);check(signal)
|
package/src/client/tutorial.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Opt-in walkthrough. All writes are limited to sessions created by this run. */
|
|
2
|
-
import {consumeTutorialRequest} from './tutorial-request.mjs?v=0.1.
|
|
2
|
+
import {consumeTutorialRequest} from './tutorial-request.mjs?v=0.1.4'
|
|
3
3
|
export const TUTORIAL_KEY = 'dsh-duplex-tutorial-v1'
|
|
4
4
|
export const QUESTION = '什么是语音交互?请用一句话简单解释,不使用工具。'
|
|
5
5
|
export const INPUT = '用一句话介绍语音助手。'
|
package/src/host/assets.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/** Explicit public asset inventory. Host code and configuration are never served. */
|
|
2
2
|
const modules = {
|
|
3
3
|
state: 'shared/state', catalog: 'shared/catalog',
|
|
4
|
+
connection_guidance: 'shared/connection-guidance',
|
|
4
5
|
audio: 'client/audio', microphone: 'client/microphone', shortcuts: 'client/shortcuts',
|
|
5
6
|
browser: 'client/browser', rpc: 'client/rpc', composer: 'client/composer',
|
|
7
|
+
composer_bridge: 'client/composer-bridge',
|
|
6
8
|
interactions: 'client/interactions', tutorial: 'client/tutorial',
|
|
7
9
|
tutorial_adapter: 'client/tutorial-adapter', tutorial_request: 'client/tutorial-request',
|
|
8
10
|
feedback: 'client/feedback', consent: 'client/consent',
|