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/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**DSH 语音操控和语音播报**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
https://github.com/user-attachments/assets/83152138-c292-4057-a820-efc2bc96f33a
|
|
6
6
|
|
|
7
7
|
duet,意为「二重奏」。这里的 duet 使用全双工语音模型,可以边听边说,
|
|
8
8
|
让你与 DSH 的交流不必一人一句地等待。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-duet",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "duet(二重奏):只用语音就能操作 DSH,边听边说、随时插话的全双工语音助手。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh-plugin",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "node scripts/test.mjs",
|
|
24
24
|
"check": "node scripts/check-package.mjs",
|
|
25
|
-
"test:browser": "node tests/browser/settings.mjs && node tests/browser/tutorial.mjs && node tests/browser/feedback.mjs && node tests/browser/connection-hint.mjs && node tests/browser/resilience.mjs",
|
|
25
|
+
"test:browser": "node tests/browser/settings.mjs && node tests/browser/tutorial.mjs && node tests/browser/feedback.mjs && node tests/browser/connection-hint.mjs && node tests/browser/resilience.mjs && node tests/browser/page-binding.mjs",
|
|
26
26
|
"test:host": "node tests/host/isolated-host.mjs",
|
|
27
27
|
"prepack": "npm run check"
|
|
28
28
|
},
|
package/src/client/audio.mjs
CHANGED
package/src/client/browser.mjs
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import { DuetState, PLUGIN_VERSION } from '../shared/state.mjs?v=0.1.
|
|
2
|
-
import { DuetAudio } from './audio.mjs?v=0.1.
|
|
3
|
-
import { HarnessRPCExecutor } from './rpc.mjs?v=0.1.
|
|
4
|
-
import { composerAccess, observeComposer, focusedSession } from './composer.mjs?v=0.1.
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
1
|
+
import { DuetState, PLUGIN_VERSION } from '../shared/state.mjs?v=0.1.4'
|
|
2
|
+
import { DuetAudio } from './audio.mjs?v=0.1.4'
|
|
3
|
+
import { HarnessRPCExecutor } from './rpc.mjs?v=0.1.4'
|
|
4
|
+
import { composerAccess, observeComposer, focusedSession, openLocalSession } from './composer.mjs?v=0.1.4'
|
|
5
|
+
import { startComposerBridge } from './composer-bridge.mjs?v=0.1.4'
|
|
6
|
+
import { mountTutorial } from './tutorial.mjs?v=0.1.4'
|
|
7
|
+
import { tutorialAdapter } from './tutorial-adapter.mjs?v=0.1.4'
|
|
8
|
+
import { mountRating } from './feedback.mjs?v=0.1.4'
|
|
9
|
+
import { createAudioUploadConsent } from './consent.mjs?v=0.1.4'
|
|
10
|
+
import { bindDuetShortcuts } from './shortcuts.mjs?v=0.1.4'
|
|
11
|
+
import { nativeInteractions } from './interactions.mjs?v=0.1.4'
|
|
12
|
+
import { mountConnectionHint } from './connection-hint.mjs?v=0.1.4'
|
|
13
|
+
import { mountControlsLayout } from './controls-layout.mjs?v=0.1.4'
|
|
14
|
+
import { mountNotice } from './notice.mjs?v=0.1.4'
|
|
15
|
+
import { workspaceIssue, requireWorkspace, browserSessionCatalog } from './workspaces.mjs?v=0.1.4'
|
|
15
16
|
|
|
16
17
|
export function mountDuet(ctx) {
|
|
18
|
+
const pageId = crypto.randomUUID()
|
|
17
19
|
const audio = new DuetAudio()
|
|
18
20
|
const audioConsent = createAudioUploadConsent()
|
|
19
21
|
let disposed = false, controls, pollRunning = false, cursor = null, resultEpoch = null
|
|
@@ -21,11 +23,12 @@ export function mountDuet(ctx) {
|
|
|
21
23
|
const notices = mountNotice(() => controls)
|
|
22
24
|
const labels = new Map()
|
|
23
25
|
let clientTraceId = null, diagnosticCount = 0
|
|
26
|
+
let connectionGuidance = null, reportHintShown = null
|
|
24
27
|
let tutorial, teaching, ratingPanel, connectionHint, stopControlsLayout
|
|
25
28
|
const diagnostic = data => {
|
|
26
29
|
if (++diagnosticCount > 100) return
|
|
27
30
|
const bounded = Object.fromEntries(Object.entries(data).map(([k, v]) => [k, typeof v === 'string' ? v.slice(0, 4000) : v]))
|
|
28
|
-
const body = JSON.stringify({ client_trace_id: clientTraceId, plugin_version: PLUGIN_VERSION,
|
|
31
|
+
const body = JSON.stringify({ client_trace_id: clientTraceId, plugin_version: PLUGIN_VERSION, page_id: pageId,
|
|
29
32
|
utc: new Date().toISOString(), visibility: document.visibilityState, ...bounded })
|
|
30
33
|
if (body.length > 24000) return
|
|
31
34
|
void fetch('/duplex-control/api/voice/diagnostic', { method: 'POST', credentials: 'same-origin',
|
|
@@ -62,15 +65,16 @@ export function mountDuet(ctx) {
|
|
|
62
65
|
const mark = controls.querySelector('[data-duet-mark]')
|
|
63
66
|
mark.style.color = state.ready ? '#7fd6a6' : '#ed9b35'
|
|
64
67
|
mark.title = state.ready ? '语音已连接' : '语音未连接'
|
|
65
|
-
connectionHint?.update({ mode: state.mode, ready: state.ready, teaching: Boolean(tutorial?.active) })
|
|
68
|
+
connectionHint?.update({ mode: state.mode, ready: state.ready, teaching: Boolean(tutorial?.active), guidance:connectionGuidance })
|
|
66
69
|
}
|
|
67
70
|
const connect = (mode, receive) => {
|
|
68
71
|
requireWorkspace(ctx)
|
|
69
72
|
if (!audioConsent.granted()) throw Error('data_upload_consent_required')
|
|
70
73
|
clientTraceId = crypto.randomUUID(); diagnosticCount = 0
|
|
74
|
+
connectionGuidance = null; reportHintShown = null
|
|
71
75
|
const connectionTraceId = clientTraceId
|
|
72
76
|
const report = data => diagnostic({ ...data, client_trace_id: connectionTraceId, mode })
|
|
73
|
-
let ws, closed = false, release, ready = false, stopComposer
|
|
77
|
+
let ws, closed = false, release, ready = false, stopComposer, stopBridge
|
|
74
78
|
const opening = new AbortController()
|
|
75
79
|
const startupTimer = setTimeout(() => {
|
|
76
80
|
if (!closed) receive({ type: 'error', code: 'voice_start_timeout' })
|
|
@@ -83,7 +87,15 @@ export function mountDuet(ctx) {
|
|
|
83
87
|
const composer = composerAccess(ctx, id => labels.get(id) || id)
|
|
84
88
|
const interactions = nativeInteractions(ctx)
|
|
85
89
|
const rpc = new HarnessRPCExecutor(send, fetch, report, async (request, signal) =>
|
|
86
|
-
(await interactions(request, signal)) ?? composer.execute(request, signal), rows =>
|
|
90
|
+
(await interactions(request, signal)) ?? composer.execute(request, signal), rows =>
|
|
91
|
+
browserSessionCatalog(ctx, rows).map(row => ({ ...row, active: row.session_id === focusedSession(ctx) })), {
|
|
92
|
+
id: connectionTraceId,
|
|
93
|
+
completed: async (request, body, signal) => {
|
|
94
|
+
const id = request.path.endsWith('/activate') ? body.active_session_id
|
|
95
|
+
: request.path.endsWith('/fork') ? body.session?.session_id : null
|
|
96
|
+
if (id) await openLocalSession(ctx, id, signal)
|
|
97
|
+
},
|
|
98
|
+
})
|
|
87
99
|
const connection = {
|
|
88
100
|
feedbackTicket: null,
|
|
89
101
|
send,
|
|
@@ -95,6 +107,7 @@ export function mountDuet(ctx) {
|
|
|
95
107
|
clearTimeout(startupTimer)
|
|
96
108
|
opening.abort()
|
|
97
109
|
stopComposer?.()
|
|
110
|
+
stopBridge?.(); stopBridge = null
|
|
98
111
|
rpc.close()
|
|
99
112
|
audio.stop()
|
|
100
113
|
if (ws && ws.readyState !== WebSocket.CLOSED) {
|
|
@@ -117,8 +130,10 @@ export function mountDuet(ctx) {
|
|
|
117
130
|
if(!identity.ok)throw Error('voice_config_unavailable')
|
|
118
131
|
requireWorkspace(ctx)
|
|
119
132
|
if(closed)return
|
|
133
|
+
if (mode === 'online') stopBridge = startComposerBridge(ctx, connectionTraceId)
|
|
134
|
+
report({ event: 'browser.connection_start', page_binding: 'connection_owner_v1' })
|
|
120
135
|
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
|
121
|
-
ws = new WebSocket(`${protocol}//${location.host}/duplex-control/voice/ws?mode=${mode}&plugin_version=${PLUGIN_VERSION}&client_trace_id=${connectionTraceId}`)
|
|
136
|
+
ws = new WebSocket(`${protocol}//${location.host}/duplex-control/voice/ws?mode=${mode}&plugin_version=${PLUGIN_VERSION}&client_trace_id=${connectionTraceId}&page_id=${pageId}`)
|
|
122
137
|
ws.onopen = () => {
|
|
123
138
|
send({ type: 'session.update' })
|
|
124
139
|
if (mode === 'online') stopComposer = observeComposer(ctx, composer, send)
|
|
@@ -140,6 +155,12 @@ export function mountDuet(ctx) {
|
|
|
140
155
|
compatibility: message.error?.compatibility, policy: message.error?.policy }); return
|
|
141
156
|
}
|
|
142
157
|
if (message.type === 'session.created' && !ready) {
|
|
158
|
+
connectionGuidance=message.connection_route?.connection_hint
|
|
159
|
+
reportHintShown=hint=>{
|
|
160
|
+
report({event:'browser.connection_hint_shown',connection_hint:hint})
|
|
161
|
+
if(message.connection_hint_reporting===true)send({type:'client.connection_hint',connection_hint:hint})
|
|
162
|
+
}
|
|
163
|
+
report({event:'browser.endpoint_selected', connection_route:message.connection_route})
|
|
143
164
|
if (message.compatibility) receive({ type: 'compatibility', compatibility: message.compatibility })
|
|
144
165
|
connection.feedbackTicket = message.feedback_session_ticket || null
|
|
145
166
|
ready = true
|
|
@@ -175,6 +196,7 @@ export function mountDuet(ctx) {
|
|
|
175
196
|
}
|
|
176
197
|
ws.onclose = event => {
|
|
177
198
|
stopComposer?.()
|
|
199
|
+
stopBridge?.(); stopBridge = null
|
|
178
200
|
report({ event: 'browser.ws_closed', code: event.code, reason: event.reason, was_clean: event.wasClean })
|
|
179
201
|
rpc.close()
|
|
180
202
|
release?.()
|
|
@@ -268,7 +290,7 @@ export function mountDuet(ctx) {
|
|
|
268
290
|
stopControlsLayout?.()
|
|
269
291
|
stopControlsLayout = mountControlsLayout(controls)
|
|
270
292
|
connectionHint?.dispose()
|
|
271
|
-
connectionHint = mountConnectionHint(controls)
|
|
293
|
+
connectionHint = mountConnectionHint(controls,{onShown:hint=>reportHintShown?.(hint)})
|
|
272
294
|
draw(state)
|
|
273
295
|
}
|
|
274
296
|
mount()
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/** Composer requests belong to one explicit voice/tutorial connection, never all tabs. */
|
|
2
|
+
import { focusedSession as currentSession } from './composer.mjs?v=0.1.4'
|
|
3
|
+
|
|
4
|
+
export function startComposerBridge(ctx, clientId, fetchImpl = fetch) {
|
|
5
|
+
const focusedSession = () => currentSession(ctx)
|
|
6
|
+
const bridgeFetch = async (url, options = {}, timeoutMs = 8000) => {
|
|
7
|
+
const request = new AbortController()
|
|
8
|
+
const abort = () => request.abort()
|
|
9
|
+
options.signal?.addEventListener('abort', abort, { once: true })
|
|
10
|
+
if (options.signal?.aborted) abort()
|
|
11
|
+
const timer = setTimeout(abort, timeoutMs)
|
|
12
|
+
try { return await fetchImpl(url, { ...options, signal: request.signal }) }
|
|
13
|
+
finally { clearTimeout(timer); options.signal?.removeEventListener('abort', abort) }
|
|
14
|
+
}
|
|
15
|
+
const controller = new AbortController()
|
|
16
|
+
let pollRunning = false
|
|
17
|
+
let retryTimer
|
|
18
|
+
|
|
19
|
+
const digest = async (text) => {
|
|
20
|
+
const bytes = new TextEncoder().encode(text)
|
|
21
|
+
const hash = await crypto.subtle.digest("SHA-256", bytes)
|
|
22
|
+
return [...new Uint8Array(hash)].map(value => value.toString(16).padStart(2, "0")).join("")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const composerForSession = async (sessionId) => {
|
|
26
|
+
await ctx.sessions.refresh()
|
|
27
|
+
const sessionContext = ctx.sessions.scope(sessionId)
|
|
28
|
+
if (sessionContext === undefined) {
|
|
29
|
+
const error = new Error(`Harness session ${sessionId} is not ready`)
|
|
30
|
+
error.code = "session_not_ready"
|
|
31
|
+
throw error
|
|
32
|
+
}
|
|
33
|
+
return ctx.conversation.input.for(sessionContext)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const composerState = async (sessionId, input) => {
|
|
37
|
+
const state = input.state.getSnapshot()
|
|
38
|
+
return {
|
|
39
|
+
session_id: sessionId,
|
|
40
|
+
text: state.draft,
|
|
41
|
+
phase: state.phase,
|
|
42
|
+
revision: state.draftRev,
|
|
43
|
+
hash: await digest(state.draft),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const execute = async (request) => {
|
|
48
|
+
controller.signal.throwIfAborted()
|
|
49
|
+
const input = await composerForSession(request.session_id)
|
|
50
|
+
const before = await composerState(request.session_id, input)
|
|
51
|
+
controller.signal.throwIfAborted()
|
|
52
|
+
const checkFocus = () => {
|
|
53
|
+
if (request.require_focus && focusedSession() !== request.session_id) {
|
|
54
|
+
const error = new Error("Focused session changed; read input_status again")
|
|
55
|
+
error.code = "focus_conflict"
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
checkFocus()
|
|
60
|
+
if (["set", "clear", "consume"].includes(request.type)) {
|
|
61
|
+
const overwrite = request.type === "set" && request.overwrite === true
|
|
62
|
+
if (before.phase !== "plain") {
|
|
63
|
+
const error = new Error(`Harness input is busy (${before.phase})`)
|
|
64
|
+
error.code = "composer_busy"
|
|
65
|
+
throw error
|
|
66
|
+
}
|
|
67
|
+
if (!overwrite && (
|
|
68
|
+
before.revision !== request.expected_revision
|
|
69
|
+
|| before.hash !== request.expected_hash
|
|
70
|
+
)) {
|
|
71
|
+
const error = new Error("Harness input changed before the requested edit")
|
|
72
|
+
error.code = "draft_conflict"
|
|
73
|
+
throw error
|
|
74
|
+
}
|
|
75
|
+
// Hashing yields to the browser. Recheck the live draft immediately before
|
|
76
|
+
// the synchronous write so a keystroke during hashing cannot be overwritten.
|
|
77
|
+
const live = input.state.getSnapshot()
|
|
78
|
+
if (!overwrite && (live.draftRev !== before.revision || live.draft !== before.text || live.phase !== before.phase)) {
|
|
79
|
+
const error = new Error("Harness input changed during snapshot")
|
|
80
|
+
error.code = "draft_conflict"
|
|
81
|
+
throw error
|
|
82
|
+
}
|
|
83
|
+
if (live.phase !== "plain") {
|
|
84
|
+
const error = new Error("Harness input is busy")
|
|
85
|
+
error.code = "composer_busy"
|
|
86
|
+
throw error
|
|
87
|
+
}
|
|
88
|
+
checkFocus()
|
|
89
|
+
// DSH 0.1.5 adds generic attachments. The host submit RPC below is
|
|
90
|
+
// text-only: do not clear/send just the text and silently omit files.
|
|
91
|
+
// Older DSH exposes the same browser-owned items as imageIds.
|
|
92
|
+
if (request.type === "consume" && (live.attachmentIds ?? live.imageIds ?? []).length > 0) {
|
|
93
|
+
const error = new Error("输入框含有附件,请在 Harness 页面发送;语音发送目前只支持纯文本")
|
|
94
|
+
error.code = "composer_attachments_unsupported"
|
|
95
|
+
throw error
|
|
96
|
+
}
|
|
97
|
+
const consumed = request.type === "consume" ? before : null
|
|
98
|
+
input.setDraft(["clear", "consume"].includes(request.type) ? "" : String(request.text ?? ""))
|
|
99
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
100
|
+
const after = await composerState(request.session_id, input)
|
|
101
|
+
if (consumed !== null) {
|
|
102
|
+
return {
|
|
103
|
+
...after,
|
|
104
|
+
consumed_text: consumed.text,
|
|
105
|
+
consumed_revision: consumed.revision,
|
|
106
|
+
consumed_hash: consumed.hash,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return after
|
|
110
|
+
} else if (request.type !== "get") {
|
|
111
|
+
const error = new Error(`Unsupported composer request: ${request.type}`)
|
|
112
|
+
error.code = "unsupported_composer_operation"
|
|
113
|
+
throw error
|
|
114
|
+
}
|
|
115
|
+
return before
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const complete = async (requestId, payload) => {
|
|
119
|
+
await bridgeFetch(`/duplex-control/api/composer/requests/${encodeURIComponent(requestId)}/complete`, {
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers: { "Content-Type": "application/json" },
|
|
122
|
+
body: JSON.stringify({ client_id: clientId, ...payload }),
|
|
123
|
+
cache: "no-store",
|
|
124
|
+
signal: controller.signal,
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const processRequest = async (candidate) => {
|
|
129
|
+
if (controller.signal.aborted || candidate.target_client_id !== clientId) return
|
|
130
|
+
const claimResponse = await bridgeFetch(
|
|
131
|
+
`/duplex-control/api/composer/requests/${encodeURIComponent(candidate.id)}/claim`,
|
|
132
|
+
{
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers: { "Content-Type": "application/json" },
|
|
135
|
+
body: JSON.stringify({ client_id: clientId }),
|
|
136
|
+
cache: "no-store",
|
|
137
|
+
signal: controller.signal,
|
|
138
|
+
},
|
|
139
|
+
)
|
|
140
|
+
if (claimResponse.status === 404 || claimResponse.status === 409) return
|
|
141
|
+
if (!claimResponse.ok) throw new Error(`Composer claim failed: HTTP ${claimResponse.status}`)
|
|
142
|
+
const request = (await claimResponse.json()).request
|
|
143
|
+
if (controller.signal.aborted || request.target_client_id !== clientId) return
|
|
144
|
+
try {
|
|
145
|
+
const composer = await execute(request)
|
|
146
|
+
await complete(request.id, { ok: true, composer })
|
|
147
|
+
} catch (error) {
|
|
148
|
+
await complete(request.id, {
|
|
149
|
+
ok: false,
|
|
150
|
+
error_code: error?.code || "composer_operation_failed",
|
|
151
|
+
error: error instanceof Error ? error.message : String(error),
|
|
152
|
+
status: error?.code === "session_not_ready" ? 503 : 409,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const poll = async () => {
|
|
158
|
+
if (pollRunning || controller.signal.aborted) return
|
|
159
|
+
clearTimeout(retryTimer)
|
|
160
|
+
pollRunning = true
|
|
161
|
+
let succeeded = false
|
|
162
|
+
try {
|
|
163
|
+
const sessionId = focusedSession() || ''
|
|
164
|
+
const visibility = typeof document === 'undefined' ? 'unknown' : document.visibilityState
|
|
165
|
+
const query = `wait=1&client_id=${encodeURIComponent(clientId)}&session_id=${encodeURIComponent(sessionId)}&visibility=${encodeURIComponent(visibility)}`
|
|
166
|
+
const response = await bridgeFetch(`/duplex-control/api/composer/requests?${query}`, {
|
|
167
|
+
cache: "no-store",
|
|
168
|
+
signal: controller.signal,
|
|
169
|
+
}, 30_000)
|
|
170
|
+
if (!response.ok) return
|
|
171
|
+
const payload = await response.json()
|
|
172
|
+
await Promise.all((payload.requests || []).map(processRequest))
|
|
173
|
+
// Old hosts return an immediate empty list: retain a retry delay
|
|
174
|
+
// rather than spinning if a browser/host upgrade is staggered.
|
|
175
|
+
succeeded = payload.long_poll_supported === true
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (!controller.signal.aborted) console.warn("duet composer RPC failed:", error)
|
|
178
|
+
} finally {
|
|
179
|
+
pollRunning = false
|
|
180
|
+
if (!controller.signal.aborted) {
|
|
181
|
+
// Successful long polls re-arm without a background-tab timer.
|
|
182
|
+
if (succeeded) void poll()
|
|
183
|
+
else retryTimer = setTimeout(() => { void poll() }, 1000)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
void poll()
|
|
189
|
+
const resume = () => { void poll() }
|
|
190
|
+
window.addEventListener?.('online', resume)
|
|
191
|
+
window.addEventListener?.('pageshow', resume)
|
|
192
|
+
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', resume)
|
|
193
|
+
return () => {
|
|
194
|
+
controller.abort()
|
|
195
|
+
clearTimeout(retryTimer)
|
|
196
|
+
window.removeEventListener?.('online', resume)
|
|
197
|
+
window.removeEventListener?.('pageshow', resume)
|
|
198
|
+
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', resume)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
}
|
package/src/client/composer.mjs
CHANGED
|
@@ -10,6 +10,22 @@ export function focusedSession(ctx) {
|
|
|
10
10
|
return selected.length === 1 ? selected[0][0] : null
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** Navigation is local to the page owning this connection, never a host broadcast. */
|
|
14
|
+
export async function openLocalSession(ctx, id, signal) {
|
|
15
|
+
signal?.throwIfAborted()
|
|
16
|
+
await ctx.sessions.refresh()
|
|
17
|
+
signal?.throwIfAborted()
|
|
18
|
+
const navigation = typeof ctx.get === 'function' ? ctx.get('uiWorkspace') : ctx.uiWorkspace
|
|
19
|
+
if (navigation?.openSession) navigation.openSession(id)
|
|
20
|
+
else ctx.sessions.open(id)
|
|
21
|
+
const deadline = Date.now() + 3000
|
|
22
|
+
while (focusedSession(ctx) !== id) {
|
|
23
|
+
signal?.throwIfAborted()
|
|
24
|
+
if (Date.now() > deadline) fail('focus_not_ready')
|
|
25
|
+
await new Promise(resolve => setTimeout(resolve, 25))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
13
29
|
export function composerAccess(ctx, label = id => id) {
|
|
14
30
|
const focused = () => focusedSession(ctx)
|
|
15
31
|
const inputFor = id => {
|
|
@@ -42,6 +58,13 @@ export function composerAccess(ctx, label = id => id) {
|
|
|
42
58
|
return { composer: value }
|
|
43
59
|
}
|
|
44
60
|
const match = request.path.match(/^\/api\/sessions\/([A-Za-z0-9_-]+)\/composer$/)
|
|
61
|
+
if (request.method === 'GET' && match) {
|
|
62
|
+
const id = match[1]
|
|
63
|
+
const value = await snapshot(id, inputFor(id))
|
|
64
|
+
signal?.throwIfAborted()
|
|
65
|
+
if (request.params?.require_focus === 'true' && focused() !== id) fail('focus_conflict')
|
|
66
|
+
return { composer: value }
|
|
67
|
+
}
|
|
45
68
|
if (request.method !== 'PUT' || !match) return undefined
|
|
46
69
|
const id = match[1], payload = request.payload || {}, input = inputFor(id)
|
|
47
70
|
const overwrite = payload.overwrite === true
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/** A brief, non-modal invitation after the microphone is actually ready. */
|
|
2
|
-
|
|
2
|
+
import {normalizeGuidance} from '../shared/connection-guidance.mjs?v=0.1.4'
|
|
3
|
+
export function mountConnectionHint(controls, { duration = 8000, onShown = ()=>{} } = {}) {
|
|
3
4
|
const hint = document.createElement('div')
|
|
4
5
|
hint.dataset.duetConnectionHint = ''
|
|
5
6
|
hint.setAttribute('role', 'status')
|
|
6
7
|
hint.setAttribute('aria-live', 'polite')
|
|
7
8
|
hint.hidden = true
|
|
8
9
|
hint.style.cssText = 'position:fixed;z-index:100020;box-sizing:border-box;width:268px;max-width:calc(100vw - 32px);padding:12px 15px;border:1px solid #a5d8f4;border-radius:12px;background:#eff9ff;color:#163c56;font:13px/1.65 system-ui,sans-serif;box-shadow:0 8px 28px #153e6524;pointer-events:none'
|
|
9
|
-
|
|
10
|
+
const text=document.createTextNode(normalizeGuidance(null).text)
|
|
11
|
+
hint.append(text)
|
|
10
12
|
const arrow = document.createElement('span')
|
|
11
13
|
arrow.setAttribute('aria-hidden', 'true')
|
|
12
14
|
arrow.style.cssText = 'position:absolute;bottom:-6px;width:10px;height:10px;transform:rotate(45deg);background:#eff9ff;border-right:1px solid #a5d8f4;border-bottom:1px solid #a5d8f4'
|
|
@@ -38,15 +40,18 @@ export function mountConnectionHint(controls, { duration = 8000 } = {}) {
|
|
|
38
40
|
observer.observe(controls)
|
|
39
41
|
return {
|
|
40
42
|
dismiss: hide,
|
|
41
|
-
update({ mode, ready, teaching = false }) {
|
|
43
|
+
update({ mode, ready, teaching = false, guidance }) {
|
|
42
44
|
if (disposed) return
|
|
43
45
|
const active = mode === 'online' && ready
|
|
44
46
|
if (!active || teaching) hide()
|
|
45
47
|
if (active && !wasReady && !teaching) {
|
|
48
|
+
const actual=normalizeGuidance(guidance)
|
|
49
|
+
text.textContent=actual.text
|
|
46
50
|
hint.hidden = false
|
|
47
51
|
position()
|
|
48
52
|
clearTimeout(timer)
|
|
49
53
|
timer = setTimeout(hide, duration)
|
|
54
|
+
try{onShown(actual)}catch{ /* Diagnostics must never break the tooltip. */ }
|
|
50
55
|
}
|
|
51
56
|
wasReady = active
|
|
52
57
|
},
|