wecool-chat 0.9.0 → 0.10.0

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.ja.md +78 -20
  3. package/README.md +145 -23
  4. package/dist/CopyIndicator-RR3hSsBA.js +51 -0
  5. package/dist/MarkdownBody-EIz0jnQy.js +14910 -0
  6. package/dist/VoiceControls-A7N4uPTo.js +146 -0
  7. package/dist/VoiceRuntime-DrYjNkZK.js +459 -0
  8. package/dist/blank-line-CUDmZ-mX.js +143 -0
  9. package/dist/{browser-D1aZQhQL.js → browser-M7mS-hAN.js} +5 -5
  10. package/dist/clsx-Cf23UI1w.js +16 -0
  11. package/dist/core/client.d.ts +11 -3
  12. package/dist/core/client.d.ts.map +1 -1
  13. package/dist/core/error-reporter.d.ts +1 -1
  14. package/dist/core/error-reporter.d.ts.map +1 -1
  15. package/dist/core/i18n/messages.d.ts +77 -1
  16. package/dist/core/i18n/messages.d.ts.map +1 -1
  17. package/dist/core/tool-runner.d.ts.map +1 -1
  18. package/dist/core/types.d.ts +23 -1
  19. package/dist/core/types.d.ts.map +1 -1
  20. package/dist/createLucideIcon-C7wGkK-Q.js +43 -0
  21. package/dist/default-sNAxTmdF.js +8 -0
  22. package/dist/index.css +2 -2
  23. package/dist/index.js +5181 -4871
  24. package/dist/jsx-runtime-Cyy0mp0f.js +328 -0
  25. package/dist/lib-DG_DjiCr.js +15 -0
  26. package/dist/{messages-CveZLPSJ.js → messages-FvtWi3lI.js} +99 -27
  27. package/dist/plugins/qrcode.js +2 -2
  28. package/dist/remark-gfm-Bzy1u34W.js +1962 -0
  29. package/dist/remark-parse-BnwSmQr-.js +2486 -0
  30. package/dist/stable-prefix-BHsGj4tL.js +21 -0
  31. package/dist/ui/components/ChatFooter.d.ts +16 -2
  32. package/dist/ui/components/ChatFooter.d.ts.map +1 -1
  33. package/dist/ui/components/ChatHeader.d.ts +9 -2
  34. package/dist/ui/components/ChatHeader.d.ts.map +1 -1
  35. package/dist/ui/components/ChatHeaderMenu.d.ts +23 -2
  36. package/dist/ui/components/ChatHeaderMenu.d.ts.map +1 -1
  37. package/dist/ui/components/ChatPanel.d.ts +13 -3
  38. package/dist/ui/components/ChatPanel.d.ts.map +1 -1
  39. package/dist/ui/components/ChatWidget.d.ts.map +1 -1
  40. package/dist/ui/components/CommandConfirmationCard.d.ts +7 -1
  41. package/dist/ui/components/CommandConfirmationCard.d.ts.map +1 -1
  42. package/dist/ui/components/CommandOverlays.d.ts +13 -1
  43. package/dist/ui/components/CommandOverlays.d.ts.map +1 -1
  44. package/dist/ui/components/FooterActions.d.ts +18 -0
  45. package/dist/ui/components/FooterActions.d.ts.map +1 -0
  46. package/dist/ui/components/MessageList.d.ts +7 -1
  47. package/dist/ui/components/MessageList.d.ts.map +1 -1
  48. package/dist/ui/components/MessageRow.d.ts +7 -1
  49. package/dist/ui/components/MessageRow.d.ts.map +1 -1
  50. package/dist/ui/components/MessageRowActions.d.ts +9 -3
  51. package/dist/ui/components/MessageRowActions.d.ts.map +1 -1
  52. package/dist/ui/components/MicButton.d.ts +11 -0
  53. package/dist/ui/components/MicButton.d.ts.map +1 -0
  54. package/dist/ui/components/VoiceControls.d.ts +22 -0
  55. package/dist/ui/components/VoiceControls.d.ts.map +1 -0
  56. package/dist/ui/components/VoiceLoopStatusBar.d.ts +17 -0
  57. package/dist/ui/components/VoiceLoopStatusBar.d.ts.map +1 -0
  58. package/dist/ui/components/VoiceLoopToggle.d.ts +15 -0
  59. package/dist/ui/components/VoiceLoopToggle.d.ts.map +1 -0
  60. package/dist/ui/components/VoiceRuntime.d.ts +16 -0
  61. package/dist/ui/components/VoiceRuntime.d.ts.map +1 -0
  62. package/dist/ui/components/lazy-voice.d.ts +19 -0
  63. package/dist/ui/components/lazy-voice.d.ts.map +1 -0
  64. package/dist/ui/hooks/use-command-execution.d.ts +23 -1
  65. package/dist/ui/hooks/use-command-execution.d.ts.map +1 -1
  66. package/dist/ui/hooks/use-message-history.d.ts +14 -0
  67. package/dist/ui/hooks/use-message-history.d.ts.map +1 -1
  68. package/dist/ui/hooks/use-mic-button.d.ts +29 -0
  69. package/dist/ui/hooks/use-mic-button.d.ts.map +1 -0
  70. package/dist/ui/hooks/use-send-message.d.ts.map +1 -1
  71. package/dist/ui/hooks/use-speak-message.d.ts +26 -0
  72. package/dist/ui/hooks/use-speak-message.d.ts.map +1 -0
  73. package/dist/ui/hooks/use-visitor-id.d.ts +15 -3
  74. package/dist/ui/hooks/use-visitor-id.d.ts.map +1 -1
  75. package/dist/ui/hooks/use-voice-input.d.ts +38 -0
  76. package/dist/ui/hooks/use-voice-input.d.ts.map +1 -0
  77. package/dist/ui/hooks/use-voice-loop-speech.d.ts +38 -0
  78. package/dist/ui/hooks/use-voice-loop-speech.d.ts.map +1 -0
  79. package/dist/ui/hooks/use-voice-loop.d.ts +12 -0
  80. package/dist/ui/hooks/use-voice-loop.d.ts.map +1 -0
  81. package/dist/ui/hooks/use-voice-runtime-snapshot.d.ts +19 -0
  82. package/dist/ui/hooks/use-voice-runtime-snapshot.d.ts.map +1 -0
  83. package/dist/ui/hooks/use-voice-runtime.d.ts +29 -0
  84. package/dist/ui/hooks/use-voice-runtime.d.ts.map +1 -0
  85. package/dist/ui/hooks/useChatStream.d.ts +5 -0
  86. package/dist/ui/hooks/useChatStream.d.ts.map +1 -1
  87. package/dist/ui/hooks/useConversationList.d.ts +7 -0
  88. package/dist/ui/hooks/useConversationList.d.ts.map +1 -1
  89. package/dist/ui/utils/speech-text.d.ts +29 -0
  90. package/dist/ui/utils/speech-text.d.ts.map +1 -0
  91. package/dist/ui/utils/voice-locale.d.ts +3 -0
  92. package/dist/ui/utils/voice-locale.d.ts.map +1 -0
  93. package/dist/ui/utils/voice-loop-cue.d.ts +23 -0
  94. package/dist/ui/utils/voice-loop-cue.d.ts.map +1 -0
  95. package/dist/ui/utils/voice-loop-status.d.ts +17 -0
  96. package/dist/ui/utils/voice-loop-status.d.ts.map +1 -0
  97. package/dist/ui/utils/voice-loop-types.d.ts +40 -0
  98. package/dist/ui/utils/voice-loop-types.d.ts.map +1 -0
  99. package/dist/ui/utils/voice-runtime.d.ts +38 -0
  100. package/dist/ui/utils/voice-runtime.d.ts.map +1 -0
  101. package/dist/ui/utils/voice-support.d.ts +9 -0
  102. package/dist/ui/utils/voice-support.d.ts.map +1 -0
  103. package/dist/unified-DctkAd9P.js +518 -0
  104. package/dist/use-voice-input-LtJJz-zQ.js +102 -0
  105. package/dist/version.d.ts +17 -0
  106. package/dist/version.d.ts.map +1 -0
  107. package/dist/voice-runtime-F5NxN_0m.js +11 -0
  108. package/dist/voice-support-BeOiOzcn.js +9 -0
  109. package/package.json +27 -25
  110. package/dist/CopyIndicator-D50ixqNB.js +0 -407
  111. package/dist/MarkdownBody-eopKrDot.js +0 -19983
@@ -0,0 +1,146 @@
1
+ import { a as f } from "./messages-FvtWi3lI.js";
2
+ import { i as g, r as b, t as x } from "./jsx-runtime-Cyy0mp0f.js";
3
+ import { t as d } from "./clsx-Cf23UI1w.js";
4
+ import { n as M, t as k } from "./voice-support-BeOiOzcn.js";
5
+ import { t as m } from "./createLucideIcon-C7wGkK-Q.js";
6
+ import { t as L } from "./use-voice-input-LtJJz-zQ.js";
7
+ var w = [
8
+ ["path", {
9
+ d: "M2 10v3",
10
+ key: "1fnikh"
11
+ }],
12
+ ["path", {
13
+ d: "M6 6v11",
14
+ key: "11sgs0"
15
+ }],
16
+ ["path", {
17
+ d: "M10 3v18",
18
+ key: "yhl04a"
19
+ }],
20
+ ["path", {
21
+ d: "M14 8v7",
22
+ key: "3a1oy3"
23
+ }],
24
+ ["path", {
25
+ d: "M18 5v13",
26
+ key: "123xd1"
27
+ }],
28
+ ["path", {
29
+ d: "M22 10v3",
30
+ key: "154ddg"
31
+ }]
32
+ ], y = m("audio-lines", w), E = [
33
+ ["path", {
34
+ d: "M12 19v3",
35
+ key: "npa21l"
36
+ }],
37
+ ["path", {
38
+ d: "M19 10v2a7 7 0 0 1-14 0v-2",
39
+ key: "1vc78b"
40
+ }],
41
+ ["rect", {
42
+ x: "9",
43
+ y: "2",
44
+ width: "6",
45
+ height: "13",
46
+ rx: "3",
47
+ key: "s6n7sd"
48
+ }]
49
+ ], I = m("mic", E), l = /* @__PURE__ */ f(g(), 1), j = 4e3, S = ({ voice: n, onBeforeListen: c, onTranscript: o, messages: e, announce: a }) => {
50
+ const t = L(), [s, i] = (0, l.useState)(null), u = (0, l.useRef)(null), _ = n && k();
51
+ (0, l.useEffect)(() => {
52
+ if (!t.error) return;
53
+ const p = {
54
+ unsupported: e.errorGeneric,
55
+ permission_denied: e.voiceInputErrorPermissionDenied,
56
+ timeout: e.voiceInputErrorTimeout,
57
+ no_match: e.voiceInputErrorNoMatch,
58
+ network_error: e.voiceInputErrorNetwork,
59
+ device_unavailable: e.voiceInputErrorDeviceUnavailable,
60
+ language_unsupported: e.voiceInputErrorLanguageUnsupported
61
+ }[t.error];
62
+ i(p), a(p), u.current && clearTimeout(u.current), u.current = setTimeout(() => i(null), j);
63
+ }, [
64
+ t.error,
65
+ e,
66
+ a
67
+ ]), (0, l.useEffect)(() => () => {
68
+ u.current && clearTimeout(u.current);
69
+ }, []);
70
+ const v = (0, l.useCallback)(() => {
71
+ if (t.isListening) {
72
+ t.stop();
73
+ return;
74
+ }
75
+ c?.(), i(null), a(e.voiceInputListeningAnnouncement), t.start(o);
76
+ }, [
77
+ t,
78
+ c,
79
+ e.voiceInputListeningAnnouncement,
80
+ a,
81
+ o
82
+ ]), h = t.isListening ? e.voiceInputListeningAria : e.voiceInputButtonAria;
83
+ return {
84
+ showsMic: _,
85
+ isListening: t.isListening,
86
+ micErrorText: s,
87
+ ariaLabel: h,
88
+ handleMicClick: v
89
+ };
90
+ }, r = x();
91
+ function R({ isListening: n, disabled: c, errorText: o, ariaLabel: e, onClick: a }) {
92
+ return /* @__PURE__ */ (0, r.jsxs)(r.Fragment, { children: [/* @__PURE__ */ (0, r.jsx)("button", {
93
+ type: "button",
94
+ onClick: a,
95
+ disabled: c,
96
+ className: d("wecool-footer__button", "wecool-footer__button--mic", n && "wecool-footer__button--mic-listening"),
97
+ "aria-label": e,
98
+ children: /* @__PURE__ */ (0, r.jsx)(I, {
99
+ className: "wecool-icon--md",
100
+ "aria-hidden": "true"
101
+ })
102
+ }), o && /* @__PURE__ */ (0, r.jsx)("span", {
103
+ className: "wecool-footer__mic-error",
104
+ children: o
105
+ })] });
106
+ }
107
+ function T({ isRunning: n, disabled: c, ariaLabel: o, onClick: e }) {
108
+ return /* @__PURE__ */ (0, r.jsx)("button", {
109
+ type: "button",
110
+ onClick: e,
111
+ disabled: c,
112
+ "aria-pressed": n,
113
+ "aria-label": o,
114
+ className: d("wecool-footer__button", "wecool-footer__button--voice-loop", n && "wecool-footer__button--voice-loop-on"),
115
+ children: /* @__PURE__ */ (0, r.jsx)(y, {
116
+ className: "wecool-icon--md",
117
+ "aria-hidden": "true"
118
+ })
119
+ });
120
+ }
121
+ function B({ voice: n, isLoading: c, onBeforeListen: o, onTranscript: e, announce: a, voiceLoop: t }) {
122
+ const s = b(), i = S({
123
+ voice: n,
124
+ onBeforeListen: o,
125
+ onTranscript: e,
126
+ messages: s,
127
+ announce: a
128
+ });
129
+ if (!i.showsMic) return null;
130
+ const u = t !== void 0 && M();
131
+ return /* @__PURE__ */ (0, r.jsxs)(r.Fragment, { children: [/* @__PURE__ */ (0, r.jsx)(R, {
132
+ isListening: i.isListening,
133
+ disabled: c || (t?.isRunning ?? !1),
134
+ errorText: i.micErrorText,
135
+ ariaLabel: i.ariaLabel,
136
+ onClick: i.handleMicClick
137
+ }), u && /* @__PURE__ */ (0, r.jsx)(T, {
138
+ isRunning: t.isRunning,
139
+ disabled: i.isListening && !t.isRunning,
140
+ ariaLabel: t.isRunning ? s.voiceLoopToggleStopAria : s.voiceLoopToggleStartAria,
141
+ onClick: t.toggle
142
+ })] });
143
+ }
144
+ export {
145
+ B as VoiceControls
146
+ };
@@ -0,0 +1,459 @@
1
+ import { a as re } from "./messages-FvtWi3lI.js";
2
+ import { i as oe, r as O, t as ce } from "./jsx-runtime-Cyy0mp0f.js";
3
+ import { i as X, n as V } from "./voice-support-BeOiOzcn.js";
4
+ import { t as se } from "./voice-runtime-F5NxN_0m.js";
5
+ import { n as ie, t as ue } from "./use-voice-input-LtJJz-zQ.js";
6
+ import { t as ae } from "./stable-prefix-BHsGj4tL.js";
7
+ var t = /* @__PURE__ */ re(oe(), 1), le = 2e3, fe = new RegExp("\\p{Extended_Pictographic}|[\\u{1F3FB}-\\u{1F3FF}]|\\u{FE0F}|\\u{200D}|\\u{20E3}", "gu"), pe = /(?:https?:\/\/|www\.)\S+/gi, de = /^(?:https?:\/\/|www\.|mailto:)/i, G = null, he = () => (G ??= (async () => {
8
+ const [{ unified: e }, { default: r }, { default: o }] = await Promise.all([
9
+ import("./unified-DctkAd9P.js").then((c) => c.t),
10
+ import("./remark-parse-BnwSmQr-.js").then((c) => c.t),
11
+ import("./remark-gfm-Bzy1u34W.js")
12
+ ]), n = e().use(r).use(o, { singleTilde: !1 });
13
+ return (c) => n.parse(c);
14
+ })(), G), D = (e) => (e.children ?? []).map(me).join(""), me = (e) => {
15
+ switch (e.type) {
16
+ case "text":
17
+ return e.value ?? "";
18
+ case "inlineCode":
19
+ return e.value ?? "";
20
+ case "link":
21
+ case "linkReference": {
22
+ const r = D(e);
23
+ return de.test(r) ? "" : r;
24
+ }
25
+ case "image":
26
+ case "imageReference":
27
+ case "html":
28
+ case "footnoteReference":
29
+ return "";
30
+ case "break":
31
+ return " ";
32
+ default:
33
+ return D(e);
34
+ }
35
+ }, $ = (e, r) => {
36
+ switch (e.type) {
37
+ case "code":
38
+ return [r.codeBlock];
39
+ case "table":
40
+ return [r.table];
41
+ case "thematicBreak":
42
+ case "html":
43
+ case "definition":
44
+ case "footnoteDefinition":
45
+ case "yaml":
46
+ return [];
47
+ case "list":
48
+ case "listItem":
49
+ case "blockquote":
50
+ case "footnoteDefinitionContent":
51
+ return (e.children ?? []).flatMap((o) => $(o, r));
52
+ default:
53
+ return [D(e)];
54
+ }
55
+ }, ve = (e) => e.replace(fe, "").replace(pe, "").replace(/\s+/g, " ").trim(), z = async (e, r) => {
56
+ if (e.trim().length === 0) return "";
57
+ const o = ((await he())(e).children ?? []).flatMap((n) => $(n, r)).map(ve).filter((n) => n.length > 0).join(`
58
+ `);
59
+ return o.length <= 2e3 ? o : `${o.slice(0, le).trimEnd()}
60
+ ${r.truncatedNotice}`;
61
+ }, we = 3e3, ge = () => {
62
+ const e = X(), r = O(), [o, n] = (0, t.useState)(null), [c, a] = (0, t.useState)(0), s = (0, t.useRef)(null), d = (0, t.useRef)(/* @__PURE__ */ new Set()), l = (0, t.useCallback)(() => {
63
+ d.current.forEach((f) => f()), d.current.clear(), V() && window.speechSynthesis.cancel(), s.current = null, n(null), a(0);
64
+ }, []), h = (0, t.useCallback)(() => a((f) => Math.max(f - 1, 0)), []), k = (0, t.useCallback)((f, i) => {
65
+ const u = new SpeechSynthesisUtterance(f);
66
+ u.lang = ie[e];
67
+ let m = !1, R = 0;
68
+ const E = () => {
69
+ R = window.setTimeout(() => {
70
+ if (!m) {
71
+ if (window.speechSynthesis.speaking) {
72
+ E();
73
+ return;
74
+ }
75
+ m = !0, d.current.delete(v), window.speechSynthesis.cancel(), i();
76
+ }
77
+ }, we);
78
+ };
79
+ E();
80
+ const v = () => {
81
+ m = !0, window.clearTimeout(R);
82
+ };
83
+ d.current.add(v);
84
+ const L = () => {
85
+ d.current.delete(v), !m && (m = !0, window.clearTimeout(R), i());
86
+ };
87
+ u.onstart = () => window.clearTimeout(R), u.onend = L, u.onerror = L, window.speechSynthesis.speak(u);
88
+ }, [e]);
89
+ (0, t.useEffect)(() => l, [l]);
90
+ const S = (0, t.useCallback)((f, i) => {
91
+ if (s.current === f) {
92
+ if (i.length === 0) {
93
+ s.current = null, n(null), h();
94
+ return;
95
+ }
96
+ k(i, () => {
97
+ h(), s.current === f && (s.current = null, n(null));
98
+ });
99
+ }
100
+ }, [k, h]), w = (0, t.useCallback)((f, i) => {
101
+ if (V()) {
102
+ if (s.current === f) {
103
+ l();
104
+ return;
105
+ }
106
+ window.speechSynthesis.cancel(), window.speechSynthesis.speak(new SpeechSynthesisUtterance(" ")), s.current = f, n(f), a((u) => u + 1), z(i, {
107
+ codeBlock: r.speechCodeBlockLabel,
108
+ table: r.speechTableLabel,
109
+ truncatedNotice: r.speechTruncatedNotice
110
+ }).catch(() => i).then((u) => S(f, u));
111
+ }
112
+ }, [
113
+ r,
114
+ S,
115
+ l
116
+ ]), b = (0, t.useCallback)((f) => {
117
+ V() && (a((i) => i + 1), k(f, h));
118
+ }, [k, h]);
119
+ return {
120
+ speakingMessageId: o,
121
+ isSpeaking: c > 0,
122
+ toggleSpeak: w,
123
+ enqueue: b,
124
+ stop: l
125
+ };
126
+ }, Re = (e, r, o) => {
127
+ if (typeof e == "string") return e;
128
+ if (e) {
129
+ const n = e[r];
130
+ if (typeof n == "string") return n;
131
+ }
132
+ return o;
133
+ }, Ee = (e) => e.trim().length > 0 ? e : " ", Le = ({ isResponding: e, isLoading: r, assistantMessages: o, speak: n, isEnded: c }) => {
134
+ const a = O(), s = (0, t.useRef)(/* @__PURE__ */ new Map()), d = (0, t.useRef)(/* @__PURE__ */ new Map()), l = (0, t.useRef)(/* @__PURE__ */ new Set()), h = (0, t.useRef)(Promise.resolve()), [k, S] = (0, t.useState)(0), w = (0, t.useRef)(0), b = (0, t.useRef)(o);
135
+ b.current = o;
136
+ const f = (0, t.useCallback)((u, m) => {
137
+ w.current += 1, S(w.current), h.current = h.current.then(async () => {
138
+ if (c()) return;
139
+ const R = await z(m, {
140
+ codeBlock: a.speechCodeBlockLabel,
141
+ table: a.speechTableLabel,
142
+ truncatedNotice: a.speechTruncatedNotice
143
+ }), E = s.current.get(u) ?? 0;
144
+ if (R.length <= E) return;
145
+ s.current.set(u, R.length);
146
+ const v = R.slice(E).trim();
147
+ v.length > 0 && !c() && n(v);
148
+ }).catch(() => {
149
+ }).finally(() => {
150
+ w.current = Math.max(w.current - 1, 0), S(w.current);
151
+ });
152
+ }, [
153
+ a,
154
+ n,
155
+ c
156
+ ]);
157
+ (0, t.useEffect)(() => {
158
+ if (e)
159
+ for (const u of o) {
160
+ if (l.current.has(u.id)) continue;
161
+ const m = r ? ae(u.content).prefix : u.content;
162
+ m.trim().length !== 0 && d.current.get(u.id) !== m && (d.current.set(u.id, m), f(u.id, m));
163
+ }
164
+ }, [
165
+ e,
166
+ o,
167
+ r,
168
+ f
169
+ ]);
170
+ const i = (0, t.useCallback)(() => {
171
+ l.current = new Set(b.current.map((u) => u.id));
172
+ }, []);
173
+ return {
174
+ pendingSpeechCount: k,
175
+ hasPendingSpeech: (0, t.useCallback)(() => w.current > 0, []),
176
+ markTurnStart: i,
177
+ reset: (0, t.useCallback)(() => {
178
+ s.current.clear(), d.current.clear(), h.current = Promise.resolve();
179
+ }, [])
180
+ };
181
+ }, Se = {
182
+ permission_denied: "permission",
183
+ device_unavailable: "device",
184
+ language_unsupported: "language",
185
+ unsupported: "device"
186
+ }, ke = (e, r) => e === "idle" ? "idle" : e === "listening" ? "listening" : e === "cue" || r ? "speaking" : "thinking", K = (e) => ({
187
+ silence: e.voiceLoopEndedSilence,
188
+ network: e.voiceLoopEndedNetwork,
189
+ permission: e.voiceLoopEndedPermission,
190
+ device: e.voiceLoopEndedDevice,
191
+ language: e.voiceLoopEndedLanguage
192
+ }), P = 300, _e = 12e4, W = 1500, Te = 6e3, be = ({ isSupported: e, isLoading: r, hasTurnFailed: o, hasPendingConfirmation: n, isSpeaking: c, assistantMessages: a, onSubmit: s, speak: d, stopSpeaking: l, startCue: h }) => {
193
+ const k = X(), S = O(), { error: w, start: b, stop: f } = ue(), [i, u] = (0, t.useState)("idle"), [m, R] = (0, t.useState)(null), E = (0, t.useRef)("idle"), v = (0, t.useRef)(null), L = (0, t.useRef)(null), M = (0, t.useRef)(0), I = (0, t.useRef)(""), y = (0, t.useRef)(!1), x = (0, t.useRef)(!1), B = (0, t.useRef)(() => {
194
+ }), j = (0, t.useRef)(d);
195
+ j.current = d;
196
+ const F = (0, t.useRef)(K(S));
197
+ F.current = K(S);
198
+ const J = (0, t.useCallback)(() => E.current === "idle", []), { pendingSpeechCount: Q, hasPendingSpeech: U, markTurnStart: q, reset: Y } = Le({
199
+ isResponding: i === "responding",
200
+ isLoading: r,
201
+ assistantMessages: a,
202
+ speak: d,
203
+ isEnded: J
204
+ }), _ = (0, t.useCallback)((p) => {
205
+ E.current = p, u(p);
206
+ }, []), N = (0, t.useCallback)(() => {
207
+ v.current !== null && window.clearTimeout(v.current), v.current = null;
208
+ }, []), T = (0, t.useCallback)((p) => {
209
+ N(), _("listening"), v.current = window.setTimeout(() => B.current(), p);
210
+ }, [N, _]), g = (0, t.useCallback)((p) => {
211
+ N(), L.current !== null && window.clearTimeout(L.current), L.current = null, f(), l(), Y(), _("idle"), R(p), p && j.current(F.current[p]);
212
+ }, [
213
+ N,
214
+ f,
215
+ l,
216
+ Y,
217
+ _
218
+ ]), H = (0, t.useCallback)(() => {
219
+ _("listening"), b((p) => {
220
+ y.current = !1, x.current = !1, q(), M.current = Date.now(), I.current = p, _("responding"), s(p);
221
+ }, {
222
+ isContinuous: !0,
223
+ onNoResult: () => {
224
+ E.current === "listening" && T(P);
225
+ }
226
+ });
227
+ }, [
228
+ _,
229
+ b,
230
+ s,
231
+ T,
232
+ q
233
+ ]);
234
+ (0, t.useEffect)(() => {
235
+ B.current = H;
236
+ }, [H]), (0, t.useEffect)(() => {
237
+ if (i === "idle") return;
238
+ if (n) {
239
+ const ne = Date.now();
240
+ return () => {
241
+ M.current += Date.now() - ne;
242
+ };
243
+ }
244
+ const p = Date.now() - M.current, te = Math.max(_e - p, 0);
245
+ return L.current = window.setTimeout(() => g("silence"), te), () => {
246
+ L.current !== null && window.clearTimeout(L.current), L.current = null;
247
+ };
248
+ }, [
249
+ i,
250
+ n,
251
+ g
252
+ ]), (0, t.useEffect)(() => {
253
+ if (E.current === "idle" || !w) return;
254
+ const p = Se[w];
255
+ if (p) {
256
+ g(p);
257
+ return;
258
+ }
259
+ if (w !== "network_error") {
260
+ T(P);
261
+ return;
262
+ }
263
+ if (y.current) {
264
+ g("network");
265
+ return;
266
+ }
267
+ y.current = !0, T(W);
268
+ }, [
269
+ w,
270
+ g,
271
+ T
272
+ ]), (0, t.useEffect)(() => {
273
+ if (!(i !== "responding" || n)) {
274
+ if (r) {
275
+ x.current = !0;
276
+ return;
277
+ }
278
+ if (!(!x.current || c || U())) {
279
+ if (!o) {
280
+ T(P);
281
+ return;
282
+ }
283
+ if (y.current) {
284
+ g("network");
285
+ return;
286
+ }
287
+ y.current = !0, x.current = !1, N(), v.current = window.setTimeout(() => s(I.current), W);
288
+ }
289
+ }
290
+ }, [
291
+ i,
292
+ r,
293
+ c,
294
+ Q,
295
+ U,
296
+ o,
297
+ n,
298
+ g,
299
+ N,
300
+ s,
301
+ T
302
+ ]), (0, t.useEffect)(() => {
303
+ i !== "cue" || c || T(P);
304
+ }, [
305
+ i,
306
+ c,
307
+ T
308
+ ]), (0, t.useEffect)(() => {
309
+ if (i === "idle") return;
310
+ const p = () => {
311
+ document.visibilityState === "hidden" && g(null);
312
+ };
313
+ return document.addEventListener("visibilitychange", p), () => document.removeEventListener("visibilitychange", p);
314
+ }, [i, g]), (0, t.useEffect)(() => {
315
+ if (!m) return;
316
+ const p = window.setTimeout(() => R(null), Te);
317
+ return () => window.clearTimeout(p);
318
+ }, [m]), (0, t.useEffect)(() => () => {
319
+ v.current !== null && window.clearTimeout(v.current), L.current !== null && window.clearTimeout(L.current), l();
320
+ }, [l]);
321
+ const Z = (0, t.useCallback)(() => {
322
+ if (E.current !== "idle") {
323
+ g(null);
324
+ return;
325
+ }
326
+ e && (y.current = !1, x.current = !1, M.current = Date.now(), R(null), d(Ee(Re(h, k, S.voiceLoopStartCue))), _("cue"));
327
+ }, [
328
+ e,
329
+ g,
330
+ d,
331
+ h,
332
+ k,
333
+ S.voiceLoopStartCue,
334
+ _
335
+ ]), ee = (0, t.useCallback)(() => {
336
+ E.current !== "idle" && g(null);
337
+ }, [g]), A = ke(i, c);
338
+ return {
339
+ isRunning: i !== "idle",
340
+ status: A,
341
+ statusKey: A === "idle" ? null : n ? "awaiting-confirmation" : A,
342
+ endReason: m,
343
+ toggle: Z,
344
+ stop: ee
345
+ };
346
+ }, Ce = ({ speak: e, voiceLoop: r, isVoiceLoopSupported: o }) => {
347
+ const { speakingMessageId: n, toggleSpeak: c, stop: a } = e, { isRunning: s, toggle: d, stop: l } = r, h = (0, t.useCallback)(() => {
348
+ a(), l();
349
+ }, [a, l]);
350
+ return (0, t.useMemo)(() => ({
351
+ speakingMessageId: n,
352
+ toggleSpeak: c,
353
+ stopSpeaking: a,
354
+ stopVoice: h,
355
+ voiceLoop: o ? {
356
+ isRunning: s,
357
+ toggle: d
358
+ } : void 0
359
+ }), [
360
+ n,
361
+ c,
362
+ a,
363
+ h,
364
+ o,
365
+ s,
366
+ d
367
+ ]);
368
+ }, ye = (e) => {
369
+ for (let r = e.length - 1; r >= 0; r -= 1) {
370
+ const o = e[r];
371
+ if (o.role === "assistant") return o.isError === !0;
372
+ }
373
+ return !1;
374
+ }, Ne = ({ messages: e, isLoading: r, pendingConfirmationCount: o, onSendMessage: n, isVoiceLoopSupported: c, voiceLoopStartCue: a }) => {
375
+ const s = ge(), d = (0, t.useMemo)(() => e.filter((h) => h.role === "assistant").map((h) => ({
376
+ id: h.id,
377
+ content: h.content
378
+ })), [e]), l = be({
379
+ isSupported: c,
380
+ isLoading: r,
381
+ hasTurnFailed: ye(e),
382
+ hasPendingConfirmation: o > 0,
383
+ isSpeaking: s.isSpeaking,
384
+ assistantMessages: d,
385
+ onSubmit: n,
386
+ speak: s.enqueue,
387
+ stopSpeaking: s.stop,
388
+ startCue: a
389
+ });
390
+ return {
391
+ snapshot: Ce({
392
+ speak: s,
393
+ voiceLoop: l,
394
+ isVoiceLoopSupported: c
395
+ }),
396
+ statusKey: l.statusKey,
397
+ endReason: l.endReason,
398
+ stopLoop: l.stop
399
+ };
400
+ }, C = ce();
401
+ function xe({ statusKey: e, endReason: r, onStop: o }) {
402
+ const n = O(), c = {
403
+ listening: n.voiceLoopStatusListening,
404
+ thinking: n.voiceLoopStatusThinking,
405
+ speaking: n.voiceLoopStatusSpeaking,
406
+ "awaiting-confirmation": n.voiceLoopStatusAwaitingConfirmation
407
+ }, a = {
408
+ silence: n.voiceLoopEndedSilence,
409
+ network: n.voiceLoopEndedNetwork,
410
+ permission: n.voiceLoopEndedPermission,
411
+ device: n.voiceLoopEndedDevice,
412
+ language: n.voiceLoopEndedLanguage
413
+ };
414
+ return r ? /* @__PURE__ */ (0, C.jsx)("div", {
415
+ className: "wecool-voice-loop-bar wecool-voice-loop-bar--ended",
416
+ role: "alert",
417
+ children: /* @__PURE__ */ (0, C.jsx)("span", {
418
+ className: "wecool-voice-loop-bar__text",
419
+ children: a[r]
420
+ })
421
+ }) : e ? /* @__PURE__ */ (0, C.jsxs)("div", {
422
+ className: "wecool-voice-loop-bar",
423
+ role: "status",
424
+ "aria-live": "polite",
425
+ children: [
426
+ /* @__PURE__ */ (0, C.jsx)("span", {
427
+ className: "wecool-voice-loop-bar__indicator",
428
+ "data-wecool-voice-loop-status": e,
429
+ "aria-hidden": "true"
430
+ }),
431
+ /* @__PURE__ */ (0, C.jsx)("span", {
432
+ className: "wecool-voice-loop-bar__text",
433
+ children: c[e]
434
+ }),
435
+ /* @__PURE__ */ (0, C.jsx)("button", {
436
+ type: "button",
437
+ onClick: o,
438
+ className: "wecool-voice-loop-bar__stop",
439
+ "aria-label": n.voiceLoopToggleStopAria,
440
+ children: n.voiceLoopStopButton
441
+ })
442
+ ]
443
+ }) : null;
444
+ }
445
+ function Ie({ onSnapshotChange: e, ...r }) {
446
+ const { snapshot: o, statusKey: n, endReason: c, stopLoop: a } = Ne(r), { stopVoice: s } = o;
447
+ return (0, t.useLayoutEffect)(() => {
448
+ e(o);
449
+ }, [e, o]), (0, t.useLayoutEffect)(() => () => {
450
+ s(), e(se);
451
+ }, [e, s]), /* @__PURE__ */ (0, C.jsx)(xe, {
452
+ statusKey: n,
453
+ endReason: c,
454
+ onStop: a
455
+ });
456
+ }
457
+ export {
458
+ Ie as VoiceRuntime
459
+ };