@olares/lares-mobile 0.15.36 → 0.15.37

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.
@@ -0,0 +1,431 @@
1
+ <template>
2
+ <section class="lares-voice-page" :data-device="layoutDevice">
3
+ <h2 v-if="heading">{{ tVoice("settings.title") }}</h2>
4
+ <div v-if="isDesktop" class="lares-voice-page__list">
5
+ <label class="lares-voice-page__field">
6
+ <span>{{ tVoice("settings.stt.model") }}</span>
7
+ <LaresSelect
8
+ :value="voiceModelId"
9
+ :sections="voiceModelSections"
10
+ :disabled="busy || !voice"
11
+ @select="chooseVoiceModel"
12
+ />
13
+ </label>
14
+ <label class="lares-voice-page__field">
15
+ <span>{{ tVoice("settings.language.title") }}</span>
16
+ <LaresSelect
17
+ :value="voiceLangId"
18
+ :sections="voiceLanguageSections"
19
+ :disabled="busy || !voice"
20
+ @select="chooseVoiceLanguage"
21
+ />
22
+ </label>
23
+ </div>
24
+ <VoicePreferences
25
+ :key="hostKey"
26
+ ref="voicePreferences"
27
+ :settings="settings"
28
+ :tts-fetch="ttsFetch"
29
+ :snapshot="voice"
30
+ :device="device"
31
+ :t="tVoice"
32
+ :bind-before-leave="bindBeforeLeave"
33
+ @change="voice = $event"
34
+ >
35
+ <template v-if="!isDesktop" #lead>
36
+ <LaresSettingRow
37
+ :label="tVoice('settings.stt.model')"
38
+ :value="voiceModelLabel"
39
+ chevron
40
+ :disabled="Boolean(pending)"
41
+ @click="openSheet('voiceModel')"
42
+ />
43
+ <LaresSettingRow
44
+ :label="tVoice('settings.language.title')"
45
+ :value="voiceLangLabel"
46
+ chevron
47
+ :disabled="Boolean(pending)"
48
+ @click="openSheet('voiceLang')"
49
+ />
50
+ </template>
51
+ </VoicePreferences>
52
+ <h3>{{ tVoice("settings.conversation") }}</h3>
53
+ <div v-if="isDesktop" class="lares-voice-page__list">
54
+ <label class="lares-voice-page__field">
55
+ <span>{{ tVoice("settings.mobileMode") }}</span>
56
+ <LaresSelect
57
+ :value="voice?.config?.mobileMode || 'continuous'"
58
+ :sections="mobileModeSections"
59
+ :disabled="busy || !voice"
60
+ @select="(item) => patchVoice('mobileMode', item.id)"
61
+ />
62
+ </label>
63
+ <label class="lares-voice-page__field">
64
+ <span>{{ tVoice("settings.device") }}</span>
65
+ <LaresSelect
66
+ :value="voice?.config?.device || 'auto'"
67
+ :sections="deviceSections"
68
+ :disabled="busy || !voice"
69
+ @select="(item) => patchVoice('device', item.id)"
70
+ />
71
+ </label>
72
+ <label class="lares-voice-page__field">
73
+ <span>{{ tVoice("settings.mode.title") }}</span>
74
+ <LaresSelect
75
+ :value="voice?.config?.streaming ? 'streaming' : 'turns'"
76
+ :sections="readingSections"
77
+ :disabled="busy || !voice || !reading"
78
+ @select="(item) => chooseReading(item.id)"
79
+ />
80
+ </label>
81
+ </div>
82
+ <div v-else class="lares-set-block">
83
+ <LaresSettingRow
84
+ :label="tVoice('settings.mobileMode')"
85
+ :value="voiceChoiceLabel('mobileMode')"
86
+ chevron
87
+ :disabled="Boolean(pending) || !voice"
88
+ @click="openSheet('voiceMobileMode')"
89
+ />
90
+ <LaresSettingRow
91
+ :label="tVoice('settings.device')"
92
+ :value="voiceChoiceLabel('device')"
93
+ chevron
94
+ :disabled="Boolean(pending) || !voice"
95
+ @click="openSheet('voiceDevice')"
96
+ />
97
+ <LaresSettingRow
98
+ :label="tVoice('settings.mode.title')"
99
+ :value="voiceChoiceLabel('reading')"
100
+ :hint="readingHint"
101
+ chevron
102
+ :disabled="Boolean(pending) || !voice || !reading"
103
+ @click="openSheet('voiceReading')"
104
+ />
105
+ </div>
106
+ <p v-if="isDesktop" class="lares-voice-page__hint">{{ readingHint }}</p>
107
+ <p v-if="voiceError" class="lares-voice-page__hint" data-status="error">{{ voiceError }}</p>
108
+ <VoiceKeys :key="`keys-${hostKey}`" :client="voiceKeys" :t="tVoice" />
109
+ <LaresSheet v-if="!isDesktop" :open="Boolean(panel)" :title="sheetTitle" @close="panel = ''">
110
+ <template v-for="section in sheetSections" :key="section.heading || 'main'">
111
+ <p v-if="section.heading" class="lares-voice-page__heading">{{ section.heading }}</p>
112
+ <LaresSettingRow
113
+ v-for="item in section.items"
114
+ :key="item.id"
115
+ :label="item.label"
116
+ :checked="item.id === sheetCurrent"
117
+ :disabled="busy || item.disabled"
118
+ @click="pick(item)"
119
+ />
120
+ </template>
121
+ </LaresSheet>
122
+ </section>
123
+ </template>
124
+
125
+ <script>
126
+ import { t as translate } from "@olares/lares-core/i18n/t";
127
+ import { EN as VOICE_EN, ZH as VOICE_ZH } from "@olares/lares-core/i18n/voice";
128
+ import {
129
+ voiceLanguageItems,
130
+ voiceMenuValue,
131
+ voiceModelItems,
132
+ voiceValueFromMenu,
133
+ } from "@olares/lares-core/voice/languages";
134
+ import { rememberedSettings } from "@olares/lares-core/larepass/settings";
135
+ import { hostKey as hostSessionKey } from "@olares/lares-core/larepass/host";
136
+ import { createHostClient } from "../host.js";
137
+ import { chatDevice, DEVICE_DESKTOP } from "../layout.js";
138
+ import VoicePreferences from "./VoicePreferences.vue";
139
+ import VoiceKeys from "./VoiceKeys.vue";
140
+ import LaresSettingRow from "./SettingRow.vue";
141
+ import LaresSheet from "./Sheet.vue";
142
+ import LaresSelect from "./Select.vue";
143
+
144
+ const catalogs = { voice: { zh: VOICE_ZH, en: VOICE_EN } };
145
+
146
+ function itemLabel(items, id) {
147
+ return items.find((item) => item.id === id)?.label || "—";
148
+ }
149
+
150
+ export default {
151
+ name: "LaresVoiceSettings",
152
+ components: { VoicePreferences, VoiceKeys, LaresSettingRow, LaresSheet, LaresSelect },
153
+ expose: ["reload", "beforeLeave"],
154
+ props: {
155
+ locale: { type: String, default: "en" },
156
+ device: { type: String, default: "mobile" },
157
+ heading: { type: Boolean, default: true },
158
+ baseUrl: { type: String, default: undefined },
159
+ proxyPrefix: { type: String, default: undefined },
160
+ request: { type: Function, default: undefined },
161
+ env: { type: Object, default: undefined },
162
+ missing: { type: Boolean, default: false },
163
+ bindBeforeLeave: { type: Function, default: undefined },
164
+ },
165
+ data() {
166
+ const remembered = rememberedSettings();
167
+ return {
168
+ pending: "",
169
+ panel: "",
170
+ voice: remembered.voice,
171
+ voiceError: "",
172
+ reading: null,
173
+ };
174
+ },
175
+ computed: {
176
+ layoutDevice() {
177
+ return chatDevice(this.device);
178
+ },
179
+ isDesktop() {
180
+ return this.layoutDevice === DEVICE_DESKTOP;
181
+ },
182
+ ports() {
183
+ return {
184
+ baseUrl: this.baseUrl,
185
+ proxyPrefix: this.proxyPrefix,
186
+ request: this.request,
187
+ env: this.env,
188
+ missing: this.missing,
189
+ };
190
+ },
191
+ hostKey() {
192
+ return hostSessionKey(this.ports);
193
+ },
194
+ ttsFetch() { return createHostClient(this.ports).ttsFetch; },
195
+ voiceKeys() { return createHostClient(this.ports).voiceKeys; },
196
+ settings() { return createHostClient(this.ports).settings; },
197
+ busy() { return Boolean(this.pending); },
198
+ voiceModels() {
199
+ return voiceModelItems(this.voice?.sttModels ?? [], this.tVoice("settings.model.auto"));
200
+ },
201
+ voiceLanguages() {
202
+ return voiceLanguageItems(this.tVoice("lang.auto"));
203
+ },
204
+ voiceModelId() { return voiceMenuValue(this.voice?.config?.model); },
205
+ voiceLangId() { return voiceMenuValue(this.voice?.config?.language); },
206
+ voiceModelSections() { return [{ items: this.voiceModels }]; },
207
+ voiceLanguageSections() { return [{ items: this.voiceLanguages }]; },
208
+ voiceModelLabel() {
209
+ if (!this.voice) return "—";
210
+ return itemLabel(this.voiceModels, this.voiceModelId);
211
+ },
212
+ voiceLangLabel() {
213
+ if (!this.voice) return "—";
214
+ return itemLabel(this.voiceLanguages, this.voiceLangId);
215
+ },
216
+ readingHint() {
217
+ if (this.reading && !this.reading.allowed) return this.tVoice("settings.mode.hint");
218
+ const type = this.reading?.gpu?.type;
219
+ if (type) return this.tVoice("settings.mode.gpu", { type: this.tVoice(`settings.gpu.${type}`) });
220
+ return this.tVoice("settings.mode.hint");
221
+ },
222
+ mobileModeSections() {
223
+ return [{ items: ["single", "continuous"].map((id) => ({ id, label: this.tVoice(`settings.mode.${id}`) })) }];
224
+ },
225
+ deviceSections() {
226
+ return [{ items: ["auto", "device", "headset"].map((id) => ({ id, label: this.tVoice(`settings.device.${id}`) })) }];
227
+ },
228
+ readingSections() {
229
+ return [{
230
+ items: [
231
+ { id: "turns", label: this.tVoice("settings.mode.turns") },
232
+ { id: "streaming", label: this.tVoice("settings.mode.streaming"), disabled: !this.reading?.allowed },
233
+ ],
234
+ }];
235
+ },
236
+ sheetTitle() {
237
+ if (this.panel === "voiceModel") return this.tVoice("settings.stt.model");
238
+ if (this.panel === "voiceLang") return this.tVoice("settings.language.title");
239
+ if (this.panel === "voiceMobileMode") return this.tVoice("settings.mobileMode");
240
+ if (this.panel === "voiceDevice") return this.tVoice("settings.device");
241
+ if (this.panel === "voiceReading") return this.tVoice("settings.mode.title");
242
+ return "";
243
+ },
244
+ sheetCurrent() { return this.sheetCurrentFor(this.panel); },
245
+ sheetSections() {
246
+ if (this.panel === "voiceModel") return [{ items: this.voiceModels.map((item) => ({ id: item.id, label: item.label })) }];
247
+ if (this.panel === "voiceLang") return [{ items: this.voiceLanguages.map((item) => ({ id: item.id, label: item.label })) }];
248
+ if (this.panel === "voiceMobileMode") return this.mobileModeSections;
249
+ if (this.panel === "voiceDevice") return this.deviceSections;
250
+ if (this.panel === "voiceReading") return this.readingSections;
251
+ return [];
252
+ },
253
+ },
254
+ watch: {
255
+ hostKey() {
256
+ this.panel = "";
257
+ this.voice = null;
258
+ this.voiceError = "";
259
+ this.load(true);
260
+ },
261
+ },
262
+ mounted() {
263
+ this.load(false);
264
+ },
265
+ methods: {
266
+ tVoice(key, params) {
267
+ return translate(catalogs.voice, this.locale, key, params);
268
+ },
269
+ openSheet(panel) {
270
+ if (panel !== "voiceReading" && !this.voice) return;
271
+ if (panel === "voiceReading" && !this.reading) return;
272
+ this.panel = panel;
273
+ },
274
+ chooseVoiceModel(item) { return this.patchVoice("model", voiceValueFromMenu(item.id)); },
275
+ chooseVoiceLanguage(item) { return this.patchVoice("language", voiceValueFromMenu(item.id)); },
276
+ voiceChoiceLabel(kind) {
277
+ if (!this.voice) return "—";
278
+ if (kind === "mobileMode") return this.tVoice(`settings.mode.${this.voice.config?.mobileMode || "continuous"}`);
279
+ if (kind === "device") return this.tVoice(`settings.device.${this.voice.config?.device || "auto"}`);
280
+ return this.tVoice(`settings.mode.${this.voice.config?.streaming ? "streaming" : "turns"}`);
281
+ },
282
+ async pick(item) {
283
+ if (item.disabled) return;
284
+ const panel = this.panel;
285
+ if (item.id === this.sheetCurrentFor(panel)) {
286
+ this.panel = "";
287
+ return;
288
+ }
289
+ this.panel = "";
290
+ if (panel === "voiceModel") await this.patchVoice("model", voiceValueFromMenu(item.id));
291
+ if (panel === "voiceLang") await this.patchVoice("language", voiceValueFromMenu(item.id));
292
+ if (panel === "voiceMobileMode") await this.patchVoice("mobileMode", item.id);
293
+ if (panel === "voiceDevice") await this.patchVoice("device", item.id);
294
+ if (panel === "voiceReading") this.chooseReading(item.id);
295
+ },
296
+ sheetCurrentFor(panel) {
297
+ if (panel === "voiceModel") return voiceMenuValue(this.voice?.config?.model);
298
+ if (panel === "voiceLang") return voiceMenuValue(this.voice?.config?.language);
299
+ if (panel === "voiceMobileMode") return this.voice?.config?.mobileMode || "continuous";
300
+ if (panel === "voiceDevice") return this.voice?.config?.device || "auto";
301
+ if (panel === "voiceReading") return this.voice?.config?.streaming ? "streaming" : "turns";
302
+ return "";
303
+ },
304
+ beforeLeave() { return this.$refs.voicePreferences?.beforeLeave() ?? Promise.resolve(true); },
305
+ reload() {
306
+ void this.$refs.voicePreferences?.reload().catch(() => {});
307
+ return this.load(true);
308
+ },
309
+ async load(force) {
310
+ if (!force && this.voice) {
311
+ void this.loadReading();
312
+ return;
313
+ }
314
+ this.panel = "";
315
+ this.voiceError = "";
316
+ try {
317
+ this.voice = await this.settings.voice(force, { diagnostics: false });
318
+ } catch {
319
+ this.voiceError = this.tVoice("settings.loadFailed");
320
+ }
321
+ void this.loadReading();
322
+ },
323
+ async loadReading() {
324
+ try {
325
+ this.reading = await this.settings.readingMode();
326
+ } catch {
327
+ this.reading = { streaming: false, allowed: false, gpu: null };
328
+ }
329
+ },
330
+ chooseReading(value) {
331
+ if (value === "streaming" && !this.reading?.allowed) {
332
+ this.voiceError = this.tVoice("error.voice_streaming_time_slice");
333
+ return;
334
+ }
335
+ void this.patchVoice("streaming", value === "streaming");
336
+ },
337
+ async patchVoice(key, value) {
338
+ if (!this.voice || this.busy) return;
339
+ this.pending = `voice:${key}`;
340
+ this.voiceError = "";
341
+ try {
342
+ this.voice = await this.$refs.voicePreferences.patch({ [key]: value });
343
+ } catch (err) {
344
+ const code = err?.code;
345
+ const text = code ? this.tVoice(`error.${code}`) : "";
346
+ this.voiceError = text && text !== `error.${code}` ? text : this.tVoice("settings.saveFailed");
347
+ } finally {
348
+ this.pending = "";
349
+ }
350
+ },
351
+ },
352
+ };
353
+ </script>
354
+
355
+ <style scoped>
356
+ .lares-voice-page {
357
+ display: flex;
358
+ flex-direction: column;
359
+ gap: 12px;
360
+ min-width: 0;
361
+ }
362
+
363
+ .lares-voice-page h2,
364
+ .lares-voice-page h3 {
365
+ margin: 0;
366
+ font-weight: 500;
367
+ }
368
+
369
+ .lares-voice-page h2 {
370
+ color: var(--q-ink-1);
371
+ font-size: 18px;
372
+ line-height: 24px;
373
+ }
374
+
375
+ .lares-voice-page[data-device="desktop"] h2 {
376
+ font-size: 16px;
377
+ }
378
+
379
+ .lares-voice-page h3 {
380
+ margin-top: 8px;
381
+ color: var(--q-ink-3);
382
+ font-size: 14px;
383
+ line-height: 18px;
384
+ }
385
+
386
+ .lares-voice-page__list {
387
+ display: flex;
388
+ flex-direction: column;
389
+ gap: 12px;
390
+ }
391
+
392
+ .lares-voice-page__field {
393
+ display: flex;
394
+ flex-direction: column;
395
+ gap: 8px;
396
+ min-width: 0;
397
+ color: var(--q-ink-1);
398
+ font-size: 15px;
399
+ line-height: 22px;
400
+ }
401
+
402
+ .lares-set-block {
403
+ border: 1px solid var(--q-separator);
404
+ border-radius: 20px;
405
+ overflow: hidden;
406
+ }
407
+
408
+ .lares-set-block :deep(.lares-set-row) {
409
+ min-height: 64px;
410
+ }
411
+
412
+ .lares-voice-page__hint {
413
+ margin: 0;
414
+ font-size: 15px;
415
+ line-height: 22px;
416
+ color: var(--q-ink-3);
417
+ }
418
+
419
+ .lares-voice-page__hint[data-status="error"] {
420
+ color: var(--q-orange-default);
421
+ }
422
+
423
+ .lares-voice-page__heading {
424
+ margin: 0;
425
+ padding: 12px 16px 4px;
426
+ color: var(--q-ink-3);
427
+ font-size: 14px;
428
+ font-weight: 500;
429
+ line-height: 18px;
430
+ }
431
+ </style>