@jskit-ai/assistant-voice 0.2.9 → 0.2.11
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/examples/conversation/main.js +1 -1
- package/examples/conversation/package.json +8 -8
- package/package.json +3 -3
- package/src/client/ConversationDialog.vue +2 -1
- package/src/client/VoiceConversation.vue +12 -6
- package/src/client/VoiceConversationControls.vue +6 -6
- package/src/client/VoiceConversationHost.vue +2 -1
- package/src/client/VoiceConversationSettings.vue +2 -1
- package/src/client/VoiceSpeakingVoiceSelect.vue +1 -1
- package/src/client/audioCapture.js +57 -36
- package/src/client/browser.js +89 -0
- package/src/client/conversationNarration.js +4 -1
- package/src/client/conversationVoiceState.js +6 -2
- package/src/client/i18n.js +7 -5
- package/src/client/types.js +104 -0
- package/src/client/voiceAvatar.js +13 -5
- package/src/client/voiceController.js +31 -7
- package/src/client/voiceConversation.js +66 -355
- package/src/client/voiceLauncher.js +18 -5
- package/src/client/voiceReplies.js +389 -0
- package/src/client/voiceSpeech.js +520 -0
- package/src/client/voiceTransport.js +55 -464
- package/src/server/voiceAccessToken.js +4 -1
- package/src/server/voiceDaemon.js +105 -47
- package/src/server/voiceProxy.js +3 -1
- package/src/server/voiceProxyConfig.js +5 -1
- package/src/shared/protocol.js +27 -5
|
@@ -42,7 +42,7 @@ const router = createRouter({ history: createWebHistory(), routes: [{ path: "/:p
|
|
|
42
42
|
app.use(pinia).use(VueQueryPlugin, { queryClient }).use(router)
|
|
43
43
|
.use(createVuetify({ components, directives, icons: { defaultSet: "mdi", aliases, sets: { mdi } } }));
|
|
44
44
|
const modules = await bootClientModules({
|
|
45
|
-
app, pinia, queryClient, router, env: import.meta.env, surfaceMode: "home",
|
|
45
|
+
app, pinia, queryClient, router, env: import.meta.env, surfaceMode: "home", logger: console,
|
|
46
46
|
surfaceRuntime: createSurfaceRuntime({ defaultSurfaceId: "home", surfaces: appConfig.surfaceDefinitions }),
|
|
47
47
|
clientModules: [
|
|
48
48
|
{ packageId: "@jskit-ai/shell-web", module: { ShellWebClientProvider },
|
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@fastify/websocket": "^11.2.0",
|
|
12
|
-
"@jskit-ai/assistant-core": "0.2.
|
|
13
|
-
"@jskit-ai/assistant-runtime": "0.2.
|
|
14
|
-
"@jskit-ai/assistant-voice": "0.2.
|
|
15
|
-
"@jskit-ai/connectors-catalog": "0.2.
|
|
16
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
17
|
-
"@jskit-ai/kernel": "0.2.
|
|
18
|
-
"@jskit-ai/realtime": "0.2.
|
|
19
|
-
"@jskit-ai/shell-web": "0.2.
|
|
12
|
+
"@jskit-ai/assistant-core": "0.2.9",
|
|
13
|
+
"@jskit-ai/assistant-runtime": "0.2.11",
|
|
14
|
+
"@jskit-ai/assistant-voice": "0.2.11",
|
|
15
|
+
"@jskit-ai/connectors-catalog": "0.2.5",
|
|
16
|
+
"@jskit-ai/http-runtime": "0.2.4",
|
|
17
|
+
"@jskit-ai/kernel": "0.2.4",
|
|
18
|
+
"@jskit-ai/realtime": "0.2.7",
|
|
19
|
+
"@jskit-ai/shell-web": "0.2.4",
|
|
20
20
|
"@mdi/js": "^7.4.47",
|
|
21
21
|
"@tanstack/vue-query": "^5.90.5",
|
|
22
22
|
"fastify": "^5.8.0",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/assistant-voice",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Duplex voice conversations with application-owned targets, authorization and speech services.",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"examples"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@jskit-ai/assistant-core": "0.2.
|
|
23
|
+
"@jskit-ai/assistant-core": "0.2.9",
|
|
24
24
|
"@mdi/js": "^7.4.47",
|
|
25
25
|
"ws": "^8.20.1"
|
|
26
26
|
},
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"@jskit-ai/kernel": "0.2.
|
|
66
|
+
"@jskit-ai/kernel": "0.2.4",
|
|
67
67
|
"vue": "^3.5.13",
|
|
68
68
|
"vuetify": "^4.0.0"
|
|
69
69
|
},
|
|
@@ -10,7 +10,8 @@ const { t } = useJskitText();
|
|
|
10
10
|
|
|
11
11
|
const props = defineProps({
|
|
12
12
|
modelValue: Boolean,
|
|
13
|
-
|
|
13
|
+
// readable-check: allow collaborator-prop - optional operation: a dialog opened without a launcher has no activator, so a press on the scrim always closes it
|
|
14
|
+
activator: { type: /** @type {import("vue").PropType<HTMLElement | null>} */ (Object), default: null },
|
|
14
15
|
title: { type: String, default: "" },
|
|
15
16
|
mode: { type: String, default: "talk" },
|
|
16
17
|
showModes: Boolean,
|
|
@@ -7,8 +7,13 @@ import { mdiMicrophone, mdiVolumeHigh } from "@mdi/js";
|
|
|
7
7
|
import { useJskitText } from "./i18n.js";
|
|
8
8
|
|
|
9
9
|
const props = defineProps({
|
|
10
|
-
session: { type:
|
|
11
|
-
|
|
10
|
+
session: { type: /** @type {import("vue").PropType<import("./types.js").VoiceSession>} */ (Object), required: true },
|
|
11
|
+
disabled: { type: Boolean, default: false },
|
|
12
|
+
// readable-check: allow collaborator-prop - optional operation: a conversation without a retained presentation passes no adapter, and the voice call is shown instead
|
|
13
|
+
adapter: {
|
|
14
|
+
type: /** @type {import("vue").PropType<import("@jskit-ai/assistant-core/client/types").AssistantConversationAdapter | null>} */ (Object),
|
|
15
|
+
default: null
|
|
16
|
+
},
|
|
12
17
|
showAvatar: { type: Boolean, default: true },
|
|
13
18
|
avatarSize: { type: String, default: "compact" }
|
|
14
19
|
});
|
|
@@ -31,11 +36,12 @@ const youLabel = computed(() => {
|
|
|
31
36
|
return t("jskit.assistant.voice.conversation.you");
|
|
32
37
|
});
|
|
33
38
|
function isAtBottom(element) { return element.scrollHeight - element.scrollTop - element.clientHeight < 24; }
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}, { flush: "post" });
|
|
39
|
+
/** Keeps a caption on its newest words unless the reader has scrolled away from the bottom. */
|
|
40
|
+
function followCaption(target, following) {
|
|
41
|
+
if (target.value && following.value) target.value.scrollTop = target.value.scrollHeight;
|
|
38
42
|
}
|
|
43
|
+
watch(voiceWords, () => followCaption(voiceWordsTarget, followWords), { flush: "post" });
|
|
44
|
+
watch(voiceAnswer, () => followCaption(voiceAnswerTarget, followAnswer), { flush: "post" });
|
|
39
45
|
</script>
|
|
40
46
|
|
|
41
47
|
<template>
|
|
@@ -5,14 +5,14 @@ import { useVoiceLauncher } from "./voiceLauncher.js";
|
|
|
5
5
|
import { useJskitText, voiceErrorText } from "./i18n.js";
|
|
6
6
|
|
|
7
7
|
const props = defineProps({
|
|
8
|
-
session: { type: Object, required: true },
|
|
8
|
+
session: { type: /** @type {import("vue").PropType<import("./types.js").VoiceSession>} */ (Object), required: true },
|
|
9
9
|
disabled: { type: Boolean, default: false },
|
|
10
10
|
speakerDisabled: { type: Boolean, default: false },
|
|
11
11
|
compact: { type: Boolean, default: false },
|
|
12
12
|
iconOnly: { type: Boolean, default: false },
|
|
13
13
|
reviewInTranscript: { type: Boolean, default: false },
|
|
14
|
-
toolsTarget: { type: Object, default: null },
|
|
15
|
-
feedbackTarget: { type: Object, default: null }
|
|
14
|
+
toolsTarget: { type: /** @type {import("vue").PropType<HTMLElement | null>} */ (Object), default: null },
|
|
15
|
+
feedbackTarget: { type: /** @type {import("vue").PropType<HTMLElement | null>} */ (Object), default: null }
|
|
16
16
|
});
|
|
17
17
|
const {
|
|
18
18
|
targetLabel, voice, error, microphoneMuted, live, starting, callMode,
|
|
@@ -60,8 +60,8 @@ const gesture = useVoiceLauncher({
|
|
|
60
60
|
>
|
|
61
61
|
{{ t("jskit.assistant.voice.controls.notSent", { words: heldTranscript }) }}
|
|
62
62
|
</p>
|
|
63
|
-
<p v-if="readAloud && voice.playbackBlocked
|
|
64
|
-
<v-btn v-if="readAloud && voice.playbackBlocked
|
|
63
|
+
<p v-if="readAloud && voice.playbackBlocked.value" class="assistant-voice-controls__status" role="status">{{ t("jskit.assistant.voice.controls.soundBlocked") }}</p>
|
|
64
|
+
<v-btn v-if="readAloud && voice.playbackBlocked.value" min-height="48" @click="enableSound">{{ t("jskit.assistant.voice.controls.enableSound") }}</v-btn>
|
|
65
65
|
<section
|
|
66
66
|
v-if="!reviewInTranscript && pendingTranscript && (pendingTranscript.reviewBeforeSend || !sending)" class="assistant-voice__review"
|
|
67
67
|
:aria-label="t('jskit.assistant.voice.controls.review')"
|
|
@@ -112,7 +112,7 @@ const gesture = useVoiceLauncher({
|
|
|
112
112
|
? t('jskit.assistant.voice.controls.readAloudOff', { target: targetLabel })
|
|
113
113
|
: t('jskit.assistant.voice.controls.readAloudOn', { target: targetLabel })"
|
|
114
114
|
:title="readAloud ? t('jskit.assistant.voice.controls.spokenRepliesOn') : t('jskit.assistant.voice.controls.spokenRepliesOff')"
|
|
115
|
-
:disabled="speakerDisabled || session.readAloudChangePending
|
|
115
|
+
:disabled="speakerDisabled || session.readAloudChangePending.value" @click="toggleReadAloud"
|
|
116
116
|
>
|
|
117
117
|
<template v-if="iconOnly" #default>
|
|
118
118
|
<span class="assistant-voice-controls__icon-disc"><v-icon size="20" :icon="readAloud ? mdiVolumeHigh : mdiVolumeOff" /></span>
|
|
@@ -6,7 +6,8 @@ import VoiceConversationSettings from "./VoiceConversationSettings.vue";
|
|
|
6
6
|
import { useJskitText, voiceErrorText } from "./i18n.js";
|
|
7
7
|
|
|
8
8
|
const props = defineProps({
|
|
9
|
-
controller: { type:
|
|
9
|
+
controller: { type: /** @type {import("vue").PropType<import("./types.js").VoiceConversationController>} */ (Object), required: true },
|
|
10
|
+
activator: { type: /** @type {import("vue").PropType<HTMLElement | null>} */ (Object), default: null },
|
|
10
11
|
avatarSize: { type: String, default: "hidden" },
|
|
11
12
|
presentation: { type: String, default: "dialog", validator: value => ["dialog", "inline"].includes(value) }
|
|
12
13
|
});
|
|
@@ -5,7 +5,8 @@ import VoiceSpeakingVoiceSelect from "./VoiceSpeakingVoiceSelect.vue";
|
|
|
5
5
|
import { useJskitText } from "./i18n.js";
|
|
6
6
|
|
|
7
7
|
const props = defineProps({
|
|
8
|
-
|
|
8
|
+
// readable-check: allow collaborator-prop - optional operation: a conversation without voice passes no voice session, and the menu has no speaking voice to choose
|
|
9
|
+
voice: { type: /** @type {import("vue").PropType<import("./types.js").VoiceTransport | null>} */ (Object), default: null },
|
|
9
10
|
compact: Boolean
|
|
10
11
|
});
|
|
11
12
|
const emit = defineEmits(["error"]);
|
|
@@ -5,7 +5,7 @@ import { useJskitText } from "./i18n.js";
|
|
|
5
5
|
|
|
6
6
|
const { t } = useJskitText();
|
|
7
7
|
const props = defineProps({
|
|
8
|
-
voice: { type: Object, required: true }
|
|
8
|
+
voice: { type: /** @type {import("vue").PropType<import("./types.js").VoiceTransport>} */ (Object), required: true }
|
|
9
9
|
});
|
|
10
10
|
function selectVoice(value) {
|
|
11
11
|
const selectedVoice = props.voice.selectedVoice;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { audioContextConstructor } from "./browser.js";
|
|
2
|
+
|
|
1
3
|
const CAPTURE_WORKLET_NAME = "jskit-voice-capture";
|
|
2
4
|
const CAPTURE_FLUSH_TIMEOUT_MS = 500;
|
|
3
5
|
const CAPTURE_FRAME_SAMPLES = 1_024;
|
|
@@ -61,8 +63,25 @@ function float32ToPcm16ArrayBuffer(samples) {
|
|
|
61
63
|
return output;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
/** Ignores the input level when a caller does not draw it. */
|
|
67
|
+
function ignoreLevel() {}
|
|
68
|
+
|
|
69
|
+
/** Ignores the audio when a caller only needs the level. */
|
|
70
|
+
function ignorePcm() {}
|
|
71
|
+
|
|
72
|
+
/** Nothing to finish while no flush is waiting for the worklet. */
|
|
73
|
+
function noFlushPending() {}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Opens the microphone and streams it as 16-bit PCM at the target sample rate until closed.
|
|
77
|
+
* @param {{
|
|
78
|
+
* onLevel?: (level: number) => void,
|
|
79
|
+
* onPcm?: (pcm: ArrayBuffer) => void,
|
|
80
|
+
* targetSampleRate?: number
|
|
81
|
+
* }} [options]
|
|
82
|
+
*/
|
|
83
|
+
async function createMicrophoneCapture({ onLevel = ignoreLevel, onPcm = ignorePcm, targetSampleRate = 16_000 } = {}) {
|
|
84
|
+
const AudioContextCtor = audioContextConstructor();
|
|
66
85
|
if (!AudioContextCtor) {
|
|
67
86
|
throw new Error("This browser does not support microphone audio processing.");
|
|
68
87
|
}
|
|
@@ -108,12 +127,12 @@ async function createMicrophoneCapture({ onLevel, onPcm, targetSampleRate = 16_0
|
|
|
108
127
|
});
|
|
109
128
|
const silence = context.createGain();
|
|
110
129
|
silence.gain.value = 0;
|
|
111
|
-
let finishFlush =
|
|
130
|
+
let finishFlush = noFlushPending;
|
|
112
131
|
let muted = false;
|
|
113
132
|
let preserveBuffered = false;
|
|
114
133
|
capture.port.onmessage = (event) => {
|
|
115
134
|
if (event.data?.type === "flushed") {
|
|
116
|
-
finishFlush
|
|
135
|
+
finishFlush();
|
|
117
136
|
return;
|
|
118
137
|
}
|
|
119
138
|
if (muted && !preserveBuffered) return;
|
|
@@ -124,10 +143,10 @@ async function createMicrophoneCapture({ onLevel, onPcm, targetSampleRate = 16_0
|
|
|
124
143
|
for (const sample of samples) {
|
|
125
144
|
squaredTotal += sample * sample;
|
|
126
145
|
}
|
|
127
|
-
onLevel
|
|
146
|
+
onLevel(samples.length
|
|
128
147
|
? Math.min(1, Math.sqrt(squaredTotal / samples.length) * 7)
|
|
129
148
|
: 0);
|
|
130
|
-
onPcm
|
|
149
|
+
onPcm(float32ToPcm16ArrayBuffer(samples));
|
|
131
150
|
};
|
|
132
151
|
source.connect(capture);
|
|
133
152
|
capture.connect(silence);
|
|
@@ -139,7 +158,7 @@ async function createMicrophoneCapture({ onLevel, onPcm, targetSampleRate = 16_0
|
|
|
139
158
|
const timeout = globalThis.setTimeout(() => {
|
|
140
159
|
if (!finished) {
|
|
141
160
|
finished = true;
|
|
142
|
-
finishFlush =
|
|
161
|
+
finishFlush = noFlushPending;
|
|
143
162
|
resolve();
|
|
144
163
|
}
|
|
145
164
|
}, CAPTURE_FLUSH_TIMEOUT_MS);
|
|
@@ -147,7 +166,7 @@ async function createMicrophoneCapture({ onLevel, onPcm, targetSampleRate = 16_0
|
|
|
147
166
|
if (finished) return;
|
|
148
167
|
finished = true;
|
|
149
168
|
globalThis.clearTimeout(timeout);
|
|
150
|
-
finishFlush =
|
|
169
|
+
finishFlush = noFlushPending;
|
|
151
170
|
resolve();
|
|
152
171
|
};
|
|
153
172
|
try {
|
|
@@ -156,38 +175,40 @@ async function createMicrophoneCapture({ onLevel, onPcm, targetSampleRate = 16_0
|
|
|
156
175
|
finishFlush();
|
|
157
176
|
}
|
|
158
177
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
178
|
+
function setMuted(value, { preserveBuffered: retainTail = false } = {}) {
|
|
179
|
+
const nextPreserveBuffered = Boolean(value) && retainTail;
|
|
180
|
+
if (muted === Boolean(value) && preserveBuffered === nextPreserveBuffered) return;
|
|
181
|
+
const keepBuffered = nextPreserveBuffered || (!value && preserveBuffered);
|
|
182
|
+
muted = Boolean(value);
|
|
183
|
+
preserveBuffered = nextPreserveBuffered;
|
|
184
|
+
for (const track of stream.getTracks()) track.enabled = !muted;
|
|
185
|
+
capture.port.postMessage({ type: "mute", muted, ...(keepBuffered ? { preserveBuffered: true } : {}) });
|
|
186
|
+
if (muted) onLevel(0);
|
|
187
|
+
}
|
|
188
|
+
function close() {
|
|
189
|
+
if (!closePromise) {
|
|
190
|
+
closePromise = (async () => {
|
|
191
|
+
try {
|
|
192
|
+
await flush();
|
|
193
|
+
} finally {
|
|
194
|
+
onLevel(0);
|
|
195
|
+
capture.port.onmessage = null;
|
|
173
196
|
try {
|
|
174
|
-
|
|
197
|
+
source.disconnect();
|
|
198
|
+
capture.disconnect();
|
|
199
|
+
silence.disconnect();
|
|
175
200
|
} finally {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
source.disconnect();
|
|
180
|
-
capture.disconnect();
|
|
181
|
-
silence.disconnect();
|
|
182
|
-
} finally {
|
|
183
|
-
stopTracks();
|
|
184
|
-
await context.close();
|
|
185
|
-
}
|
|
201
|
+
stopTracks();
|
|
202
|
+
await context.close();
|
|
186
203
|
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return closePromise;
|
|
204
|
+
}
|
|
205
|
+
})();
|
|
190
206
|
}
|
|
207
|
+
return closePromise;
|
|
208
|
+
}
|
|
209
|
+
return Object.freeze({
|
|
210
|
+
setMuted,
|
|
211
|
+
close
|
|
191
212
|
});
|
|
192
213
|
} catch (error) {
|
|
193
214
|
stopTracks();
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the voice client reads from the browser, each probe written once. Outside a browser, as in a server render or a
|
|
3
|
+
* test, there is no page, document, audio or crypto to reach.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The page's AudioContext constructor under either of its names; undefined where the browser has none.
|
|
8
|
+
* @returns {typeof AudioContext | undefined}
|
|
9
|
+
*/
|
|
10
|
+
function audioContextConstructor() {
|
|
11
|
+
return window.AudioContext || window.webkitAudioContext;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Listens to an event of the page until the returned function is called.
|
|
16
|
+
* @param {string} type
|
|
17
|
+
* @param {EventListener} listener
|
|
18
|
+
* @returns {() => void} Stops listening.
|
|
19
|
+
*/
|
|
20
|
+
function listenToPage(type, listener) {
|
|
21
|
+
const page = globalThis.window;
|
|
22
|
+
// readable-check: allow optional-call - platform object: outside a browser there is no window to listen to
|
|
23
|
+
page?.addEventListener?.(type, listener);
|
|
24
|
+
return () => {
|
|
25
|
+
// readable-check: allow optional-call - platform object: outside a browser there is no window to listen to
|
|
26
|
+
page?.removeEventListener?.(type, listener);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Listens to an event of the document until the returned function is called.
|
|
32
|
+
* @param {string} type
|
|
33
|
+
* @param {EventListener} listener
|
|
34
|
+
* @returns {() => void} Stops listening.
|
|
35
|
+
*/
|
|
36
|
+
function listenToDocument(type, listener) {
|
|
37
|
+
// readable-check: allow optional-call - platform object: outside a browser there is no document to listen to
|
|
38
|
+
globalThis.document?.addEventListener?.(type, listener);
|
|
39
|
+
return () => {
|
|
40
|
+
// readable-check: allow optional-call - platform object: outside a browser there is no document to listen to
|
|
41
|
+
globalThis.document?.removeEventListener?.(type, listener);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reads a media query and follows its changes until `stop()` is called.
|
|
47
|
+
* @param {string} query
|
|
48
|
+
* @param {(event: MediaQueryListEvent) => void} listener
|
|
49
|
+
* @returns {{ matches: boolean, stop: () => void }}
|
|
50
|
+
*/
|
|
51
|
+
function followMediaQuery(query, listener) {
|
|
52
|
+
const list = window.matchMedia(query);
|
|
53
|
+
const matches = list.matches;
|
|
54
|
+
// readable-check: allow optional-call - platform object: an older MediaQueryList has no addEventListener, so its first reading stays
|
|
55
|
+
list.addEventListener?.("change", listener);
|
|
56
|
+
return {
|
|
57
|
+
matches,
|
|
58
|
+
stop() {
|
|
59
|
+
// readable-check: allow optional-call - platform object: an older MediaQueryList has no addEventListener, so its first reading stays
|
|
60
|
+
list.removeEventListener?.("change", listener);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Whether the page is visible; outside a browser it counts as visible.
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
function pageVisible() {
|
|
70
|
+
return globalThis.document?.visibilityState !== "hidden";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A unique id for one listening or speaking turn. Voice runs only in a secure context, which always has crypto.randomUUID.
|
|
75
|
+
* @param {"listen" | "speak"} prefix
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
78
|
+
function createVoiceTurnId(prefix) {
|
|
79
|
+
return `${prefix}-${crypto.randomUUID()}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export {
|
|
83
|
+
audioContextConstructor,
|
|
84
|
+
createVoiceTurnId,
|
|
85
|
+
followMediaQuery,
|
|
86
|
+
listenToDocument,
|
|
87
|
+
listenToPage,
|
|
88
|
+
pageVisible
|
|
89
|
+
};
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Canonical history is authoritative; only user and assistant output is spoken.
|
|
3
|
+
* @param {{ turns?: object[], status?: string, interimReply?: object | null }} [conversation]
|
|
4
|
+
* @returns {import("./types.js").VoiceConversationState}
|
|
5
|
+
*/
|
|
2
6
|
export function projectConversationVoiceState({ turns = [], status = "ready", interimReply = null } = {}) {
|
|
3
7
|
const messages = [];
|
|
4
8
|
let streamingReply = null;
|
|
@@ -7,7 +11,7 @@ export function projectConversationVoiceState({ turns = [], status = "ready", in
|
|
|
7
11
|
if (turn.assistant?.text) {
|
|
8
12
|
const reply = { ...turn.assistant, role: "assistant", id: turn.assistant.outputId || `${turn.turnId}:assistant` };
|
|
9
13
|
if (turn.pending || ["starting", "inProgress"].includes(reply.status)) {
|
|
10
|
-
//
|
|
14
|
+
// An application's live notification keeps its own id until its saved reply arrives.
|
|
11
15
|
if (reply.origin === "application") Object.assign(reply, { id: `${reply.id}:stream`, autonomous: true });
|
|
12
16
|
streamingReply = reply;
|
|
13
17
|
}
|
package/src/client/i18n.js
CHANGED
|
@@ -6,8 +6,10 @@ registerJskitMessages(en);
|
|
|
6
6
|
|
|
7
7
|
const { errorMessage, t } = useJskitText();
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A failure made in the browser keeps its English message for callers and logs, and names its sentence so a screen
|
|
11
|
+
* can show it in the active language.
|
|
12
|
+
*/
|
|
11
13
|
function voiceError(key, params = {}) {
|
|
12
14
|
return Object.assign(new Error(formatMessage(catalogMessage(en, key), params)), {
|
|
13
15
|
messageCode: key.slice("jskit.".length),
|
|
@@ -15,7 +17,7 @@ function voiceError(key, params = {}) {
|
|
|
15
17
|
});
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
/** An error event from the voice service, kept with its code and translation fields. */
|
|
19
21
|
function voiceEventError(event, fallbackKey) {
|
|
20
22
|
if (!event?.message) return voiceError(fallbackKey);
|
|
21
23
|
const { code, params, messageCode, messageParams } = event;
|
|
@@ -23,12 +25,12 @@ function voiceEventError(event, fallbackKey) {
|
|
|
23
25
|
Object.fromEntries(Object.entries({ code, params, messageCode, messageParams }).filter(([, value]) => value !== undefined)));
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
/** What a screen shows for a caught failure; a thrown string is shown as written. */
|
|
27
29
|
function voiceErrorText(cause) {
|
|
28
30
|
return errorMessage(cause, typeof cause === "string" ? cause : "");
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
/** What speech says in place of content it leaves on screen, in the active language. */
|
|
32
34
|
function voiceSpeechWording() {
|
|
33
35
|
return {
|
|
34
36
|
code: t("jskit.assistant.voice.speech.codeOnScreen"),
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// The values the voice client hands out and receives across files: what its composables and factories return, and
|
|
2
|
+
// the conversation binding an application implements.
|
|
3
|
+
|
|
4
|
+
/** @typedef {ReturnType<typeof import("./voiceController.js").createVoiceConversationController>} VoiceConversationController */
|
|
5
|
+
/** @typedef {ReturnType<typeof import("./voiceConversation.js").useVoiceConversation>} VoiceSession */
|
|
6
|
+
/** @typedef {ReturnType<typeof import("./voiceTransport.js").useVoiceTransport>} VoiceTransport */
|
|
7
|
+
/** @typedef {ReturnType<typeof import("./voiceSpeech.js").useVoiceSpeech>} VoiceSpeech */
|
|
8
|
+
/** @typedef {Awaited<ReturnType<typeof import("./audioCapture.js").createMicrophoneCapture>>} MicrophoneCapture */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One transport's playback receipt for a speaking turn.
|
|
12
|
+
* @typedef {object} VoicePlaybackEvent
|
|
13
|
+
* @property {string} turnId
|
|
14
|
+
* @property {"started" | "completed" | "interrupted" | "failed"} phase
|
|
15
|
+
* @property {string} [reason]
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The playback receipt an application observes for one canonical assistant output.
|
|
20
|
+
* @typedef {object} VoiceOutputPlayback
|
|
21
|
+
* @property {string} conversationId
|
|
22
|
+
* @property {string} outputId
|
|
23
|
+
* @property {"started" | "completed" | "interrupted" | "failed"} phase
|
|
24
|
+
* @property {string} [reason]
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A message as voice reads it from a conversation's state.
|
|
29
|
+
* @typedef {object} VoiceConversationMessage
|
|
30
|
+
* @property {string} id
|
|
31
|
+
* @property {string} role
|
|
32
|
+
* @property {string} [text]
|
|
33
|
+
* @property {string} [outputId]
|
|
34
|
+
* @property {string} [streamId]
|
|
35
|
+
* @property {string} [status]
|
|
36
|
+
* @property {boolean} [autonomous]
|
|
37
|
+
* @property {boolean} [receipt] False while the application has not admitted a local user message.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The conversation state voice speaks from.
|
|
42
|
+
* @typedef {object} VoiceConversationState
|
|
43
|
+
* @property {VoiceConversationMessage[]} [messages]
|
|
44
|
+
* @property {VoiceConversationMessage | null} [streamingReply]
|
|
45
|
+
* @property {string} [status] "working" while the agent is running.
|
|
46
|
+
* @property {boolean} [available]
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Optional activity speech: what the agent is doing while it works.
|
|
51
|
+
* @typedef {object} VoiceNarration
|
|
52
|
+
* @property {object[]} [turns]
|
|
53
|
+
* @property {boolean} [loading]
|
|
54
|
+
* @property {boolean} [eligible]
|
|
55
|
+
* @property {boolean} [working]
|
|
56
|
+
* @property {boolean} [vocalizeThinking]
|
|
57
|
+
* @property {boolean} [vocalizeInterimTurns]
|
|
58
|
+
* @property {boolean} [thinkingSounds]
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The words captured but not yet sent, as an application previews them.
|
|
63
|
+
* @typedef {{ id: string, text: string } | null} VoiceTranscript
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What an application supplies for one conversation voice can hold. Its members are read where they are used, so a
|
|
68
|
+
* getter may change them while voice holds the binding; each optional part is marked.
|
|
69
|
+
* @typedef {object} VoiceConversationBinding
|
|
70
|
+
* @property {string} id Compared to reveal or switch targets.
|
|
71
|
+
* @property {VoiceConversationState} state
|
|
72
|
+
* @property {(text: string, options: { messageId: string, context: object }) => unknown} submitText
|
|
73
|
+
* Resolves when the application's normal server operation accepts the message.
|
|
74
|
+
* @property {string} [label]
|
|
75
|
+
* @property {string} [conversationId] The logical conversation id, when `id` is scoped.
|
|
76
|
+
* @property {boolean} [available] False when access to the conversation is lost.
|
|
77
|
+
* @property {boolean} [showAvatar]
|
|
78
|
+
* @property {unknown} [avatar]
|
|
79
|
+
* @property {import("@jskit-ai/assistant-core/client/types").AssistantConversationAdapter | null} [adapter]
|
|
80
|
+
* The retained conversation presentation, when the application has one.
|
|
81
|
+
* @property {{ mode?: string, reviewBeforeSend?: boolean, voiceId?: string, readAloud?: boolean }} [defaults]
|
|
82
|
+
* @property {VoiceNarration | null} [narration]
|
|
83
|
+
* @property {boolean} [readAloudChangePending]
|
|
84
|
+
* @property {() => object} [captureContext] The destination a new recording keeps.
|
|
85
|
+
* @property {() => unknown} [openText] Opens the application's text conversation.
|
|
86
|
+
* @property {() => unknown} [cancelWork] Stops the agent's work.
|
|
87
|
+
* @property {() => void} [retain] Keeps the conversation reader alive while voice holds it.
|
|
88
|
+
* @property {() => unknown} [release] Lets go of what `retain` kept.
|
|
89
|
+
* @property {(event: VoiceOutputPlayback) => unknown} [onPlayback]
|
|
90
|
+
* @property {(readAloud: boolean) => unknown} [onReadAloudChange]
|
|
91
|
+
* @property {(transcript: VoiceTranscript, metadata?: { canTake: boolean }) => unknown} [onTranscript]
|
|
92
|
+
* @property {(visual: object | null) => unknown} [onVisual]
|
|
93
|
+
* @property {(message: string) => unknown} [onError]
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* What a launcher's `open()` hands back for a held gesture: the shared session's held recording.
|
|
98
|
+
* @typedef {object} VoiceHeldRecording
|
|
99
|
+
* @property {() => unknown} startHeldRecording
|
|
100
|
+
* @property {() => unknown} finishHeldRecording
|
|
101
|
+
* @property {() => unknown} discardHeldRecording
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
|
2
|
+
import { followMediaQuery } from "./browser.js";
|
|
2
3
|
|
|
3
4
|
const IDLE_GESTURES = Object.freeze([
|
|
4
5
|
"glance-left",
|
|
@@ -67,6 +68,13 @@ const ACTIVE_EXPRESSION_SEQUENCES = Object.freeze({
|
|
|
67
68
|
thinking: THINKING_EXPRESSIONS
|
|
68
69
|
});
|
|
69
70
|
|
|
71
|
+
/** Nothing to stop before the avatar is mounted or after it is unmounted. */
|
|
72
|
+
function notFollowing() {}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The avatar's blinking, idle gestures and expressions for a voice state, and the mouth pose to draw.
|
|
76
|
+
* @param {{ state?: string, mouthPose?: string, mouthLevel: number }} props
|
|
77
|
+
*/
|
|
70
78
|
export function useVoiceAvatar(props) {
|
|
71
79
|
const blinking = ref(false);
|
|
72
80
|
const activeExpression = ref("");
|
|
@@ -82,7 +90,7 @@ export function useVoiceAvatar(props) {
|
|
|
82
90
|
let idleGestureIndex = Math.floor(Math.random() * IDLE_GESTURES.length);
|
|
83
91
|
let idleGestureTimer = 0;
|
|
84
92
|
let reopenTimer = 0;
|
|
85
|
-
let
|
|
93
|
+
let stopFollowingReducedMotion = notFollowing;
|
|
86
94
|
|
|
87
95
|
const expression = computed(() => String(props.state || "idle"));
|
|
88
96
|
const visibleMouthPose = computed(() => {
|
|
@@ -211,9 +219,9 @@ export function useVoiceAvatar(props) {
|
|
|
211
219
|
});
|
|
212
220
|
|
|
213
221
|
onMounted(() => {
|
|
214
|
-
reducedMotionQuery =
|
|
222
|
+
const reducedMotionQuery = followMediaQuery("(prefers-reduced-motion: reduce)", updateReducedMotion);
|
|
215
223
|
reducedMotion.value = reducedMotionQuery.matches;
|
|
216
|
-
reducedMotionQuery.
|
|
224
|
+
stopFollowingReducedMotion = reducedMotionQuery.stop;
|
|
217
225
|
if (!reducedMotion.value) {
|
|
218
226
|
scheduleBlink(1_800 + (Math.random() * 1_800));
|
|
219
227
|
scheduleIdleGesture(900 + (Math.random() * 1_500));
|
|
@@ -227,8 +235,8 @@ export function useVoiceAvatar(props) {
|
|
|
227
235
|
clearBlinkTimers();
|
|
228
236
|
clearActiveExpression();
|
|
229
237
|
clearIdleGesture();
|
|
230
|
-
|
|
231
|
-
|
|
238
|
+
stopFollowingReducedMotion();
|
|
239
|
+
stopFollowingReducedMotion = notFollowing;
|
|
232
240
|
});
|
|
233
241
|
return { blinking, activeExpression, idleGesture, expression, visibleMouthPose, reducedMotion };
|
|
234
242
|
}
|