agent-yes 1.303.1 → 1.304.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 (184) hide show
  1. package/README.md +54 -0
  2. package/default.config.yaml +5 -5
  3. package/dist/{JsonlStore-BpYZZTap.js → JsonlStore-CB_0sfdT.js} +5 -4
  4. package/dist/{SUPPORTED_CLIS-Dq9Lq9WJ.js → SUPPORTED_CLIS-C_1IiOj3.js} +2 -2
  5. package/dist/SUPPORTED_CLIS-Do3GHAY1.js +12 -0
  6. package/dist/{agentShare-suB6yPgO.js → agentShare-BvpEm-Fr.js} +10 -9
  7. package/dist/{askCli-BU3CNCLU.js → askCli-KmMf-3wA.js} +6 -6
  8. package/dist/{browser-BmxN1Ur9.js → browser-BAFPmEvN.js} +2 -2
  9. package/dist/{callback-Ctj3_Wxp.js → callback-BiSUhjw7.js} +6 -6
  10. package/dist/callback-SqLD2mMX.js +17 -0
  11. package/dist/{callbackCore-DSJWhqAe.js → callbackCore-BZ_10zyc.js} +1 -1
  12. package/dist/{callbackCore-BesbQ6-Z.js → callbackCore-Deve5pv3.js} +1 -1
  13. package/dist/{channels-j5nWuzlQ.js → channels-Y36S0ccW.js} +2 -2
  14. package/dist/cli.js +118 -13
  15. package/dist/{cmdDsh-2LP0grim.js → cmdDsh-DZ8bLsH7.js} +1 -1
  16. package/dist/cmdKill-CzSBfWmx.js +116 -0
  17. package/dist/{cmdPs-wc1j85fi.js → cmdPs-DBt23Qz0.js} +11 -10
  18. package/dist/{cwdConflictWarn-skQSMwEh.js → cwdConflictWarn-y-epQyOj.js} +21 -5
  19. package/dist/{cwdPassthroughHint-BUbWMoz0.js → cwdPassthroughHint-CzWh0hrw.js} +1 -1
  20. package/dist/{expose-Dawq1LxC.js → expose-Bhsn5yUz.js} +2 -2
  21. package/dist/forceKill-BDqxx4uQ.js +44 -0
  22. package/dist/{forkNested-CavHyWzA.js → forkNested-3heLs5XC.js} +45 -5
  23. package/dist/{globalPidIndex-CRB5KtyL.js → globalPidIndex-BsjlId-h.js} +6 -5
  24. package/dist/{globalPidIndex-BftWlg2V.js → globalPidIndex-uoz8Esqz.js} +2 -2
  25. package/dist/{hist-Cs0KFSUW.js → hist-DY16wm7g.js} +1 -1
  26. package/dist/{identity-DA7YI9km.js → identity-C0MxfwI2.js} +1 -1
  27. package/dist/{identity-Bk-aG_xl.js → identity-Ckav3-Up.js} +1 -1
  28. package/dist/index.js +7 -7
  29. package/dist/{initMsg-CcTqFtsp.js → initMsg-BEVDWYnv.js} +1 -1
  30. package/dist/{initMsg-D9KJnfgC.js → initMsg-GPU52N-j.js} +1 -1
  31. package/dist/liagent/box-BSIrRRc_.js +2320 -0
  32. package/dist/liagent/box-DloywR57.d.ts +409 -0
  33. package/dist/liagent/box.d.ts +3 -0
  34. package/dist/liagent/box.js +3 -0
  35. package/dist/liagent/cli.d.ts +1 -0
  36. package/dist/liagent/cli.js +908 -0
  37. package/dist/liagent/client.d.ts +28 -0
  38. package/dist/liagent/client.js +240 -0
  39. package/dist/liagent/echo-BvzPSI0x.js +86 -0
  40. package/dist/liagent/echo-W2RcMEny.d.ts +38 -0
  41. package/dist/liagent/hub.d.ts +405 -0
  42. package/dist/liagent/hub.js +1831 -0
  43. package/dist/liagent/index.d.ts +14 -0
  44. package/dist/liagent/index.js +10 -0
  45. package/dist/liagent/liagent.js +339 -0
  46. package/dist/liagent/mic-audio-C3Shud_F.js +754 -0
  47. package/dist/liagent/mic-audio-DMCWtEI-.d.ts +317 -0
  48. package/dist/liagent/mic-button-CtZHFBSG.js +651 -0
  49. package/dist/liagent/mic-button-Y0ioPetQ.d.ts +248 -0
  50. package/dist/liagent/mic.d.ts +3 -0
  51. package/dist/liagent/mic.js +4 -0
  52. package/dist/liagent/progress-CJs08bXb.js +473 -0
  53. package/dist/liagent/progress-CfE-meHr.d.ts +205 -0
  54. package/dist/liagent/progress.d.ts +2 -0
  55. package/dist/liagent/progress.js +3 -0
  56. package/dist/liagent/protocol.d.ts +324 -0
  57. package/dist/liagent/protocol.js +504 -0
  58. package/dist/liagent/speech-rate-DXeJjRTc.js +42 -0
  59. package/dist/liagent/state-tree-BZCmAknI.js +353 -0
  60. package/dist/liagent/tree-BrR7NwMf.js +710 -0
  61. package/dist/liagent/tree-HB9RhX_H.d.ts +295 -0
  62. package/dist/liagent/tree.d.ts +2 -0
  63. package/dist/liagent/tree.js +4 -0
  64. package/dist/liagent/ui-strings-B03le3fh.d.ts +27 -0
  65. package/dist/liagent/ui-strings-CJIwW5Gf.js +794 -0
  66. package/dist/liagent/ui.d.ts +394 -0
  67. package/dist/liagent/ui.js +5 -0
  68. package/dist/liagent/voice-D7yeS9VM.js +802 -0
  69. package/dist/liagent/voice-Qo7SCgi8.d.ts +308 -0
  70. package/dist/liagent/voice.d.ts +3 -0
  71. package/dist/liagent/voice.js +4 -0
  72. package/dist/{notifyDaemon-C7GlfJ1E.js → notifyDaemon-DkUB7GV2.js} +10 -9
  73. package/dist/{openBrowser-1cIo4dJL.js → openBrowser-CQAOIrD6.js} +1 -1
  74. package/dist/oxmgrService-DlYHmqyI.js +37 -0
  75. package/dist/oxmgrService-o9L3I6FX.js +3 -0
  76. package/dist/{parentLink-DoEWVGld.js → parentLink-BcAYcO9L.js} +2 -2
  77. package/dist/{parentLink-B58V1RVd.js → parentLink-MBUD3oY2.js} +2 -2
  78. package/dist/{parentPingLoop-C73bbmCE.js → parentPingLoop-DDO592Wh.js} +5 -5
  79. package/dist/{pidStore-433kvh9D.js → pidStore-C50VPk3E.js} +3 -3
  80. package/dist/pidStore-COkPjpW8.js +6 -0
  81. package/dist/{procStats-BvmrBrOs.js → procStats-DwTxj3rs.js} +1 -1
  82. package/dist/{remotes-MfdV9M8u.js → remotes-BqLoB5GR.js} +2 -2
  83. package/dist/{remotes-DevcBeLl.js → remotes-BsMGvAay.js} +3 -3
  84. package/dist/{roomReplyAlias-DFqg_6IU.js → roomReplyAlias-B-v76TA8.js} +3 -3
  85. package/dist/roomReplyAlias-BzWUBkQy.js +6 -0
  86. package/dist/{rustBinary-7VK7oLlW.js → rustBinary-BrJ2p8wy.js} +2 -2
  87. package/dist/{rustBinary-BV4LxMMn.js → rustBinary-vpfpWj1q.js} +2 -2
  88. package/dist/{schedule-Dp0qkNO1.js → schedule-Bzjhl6-h.js} +15 -44
  89. package/dist/schedulesCli-CLqieyQZ.js +784 -0
  90. package/dist/senderAncestry-CVHeKk1Y.js +173 -0
  91. package/dist/senderAncestry-KhAUPCQs.js +3 -0
  92. package/dist/{serve-BpOIIOVc.js → serve-XwFYE0cE.js} +67 -69
  93. package/dist/{serveRust-CbBrcUrg.js → serveRust-BfWkeL6T.js} +31 -4
  94. package/dist/{setup-DdIBk6Iv.js → setup-D4-s2VZw.js} +5 -5
  95. package/dist/{share-w5oRbvlj.js → share-CbFetL5H.js} +1 -1
  96. package/dist/{share-DrS3LYqf.js → share-_TPYAGWC.js} +1 -1
  97. package/dist/{shareCmd-DDENkBW2.js → shareCmd-DUBsVI3e.js} +10 -10
  98. package/dist/spawnGate-Dfj9V76w.js +5 -0
  99. package/dist/{spawnGate-lr3tvA6U.js → spawnGate-Dj_WaS29.js} +3 -3
  100. package/dist/{subcommands-DxgvDmZX.js → subcommands-B7YZJL8n.js} +471 -307
  101. package/dist/subcommands-BZPxFgkT.js +15 -0
  102. package/dist/{systemPathLink-SjUzgSiq.js → systemPathLink-Y3iC7bYV.js} +1 -1
  103. package/dist/{terminal-C2h7tpe0.js → terminal-13cGj3h6.js} +2 -2
  104. package/dist/terminal.js +1 -1
  105. package/dist/{todoAutomation-TTp_Qz9G.js → todoAutomation-CKlrxuZv.js} +2 -2
  106. package/dist/{todoCli-BWm5nZE0.js → todoCli-CU9vSJSJ.js} +6 -6
  107. package/dist/{todoIdentity-BvXZKWJI.js → todoIdentity-CzS1e7VL.js} +2 -2
  108. package/dist/{todoStore-C9RTz0Vs.js → todoStore-msRCEUi1.js} +2 -2
  109. package/dist/{tray-BeRSozJz.js → tray-CLktEN3G.js} +1 -1
  110. package/dist/{trayApp-QQjqhDv1.js → trayApp-5_OlvQcz.js} +2 -2
  111. package/dist/trayApp-DkAPo0m6.js +5 -0
  112. package/dist/{ts-6LF7f4fx.js → ts-njuddXQz.js} +126 -57
  113. package/dist/{versionChecker-Bc62HNf6.js → versionChecker-BJycAanp.js} +2 -2
  114. package/dist/{webrtcLink-va_gAR77.js → webrtcLink-DcvacEci.js} +1 -1
  115. package/dist/{webrtcLink-SsInrVHU.js → webrtcLink-WxS1nTIs.js} +1 -1
  116. package/dist/{webrtcRemote-BlGyNZDC.js → webrtcRemote-C3xQyf3u.js} +2 -2
  117. package/dist/{widget-Crb5zeHL.js → widget-DmRwOlco.js} +3 -3
  118. package/dist/widgets.js +1 -1
  119. package/dist/{workspaceConfig-BLWB5Wu9.js → workspaceConfig-DBgsu81B.js} +1 -1
  120. package/dist/{ws-DIbwNz8Z.js → ws-ByO6JO6m.js} +3 -3
  121. package/dist/{ws-BRhPG1Vy.js → ws-D0-Jg88z.js} +9 -8
  122. package/lab/ui/index.html +1 -1
  123. package/lab/ui/lab/posts/2026-08-11-slack-term-safe-writes.md +1 -1
  124. package/package.json +73 -8
  125. package/ts/JsonlStore.ts +10 -3
  126. package/ts/cli-idle.bun.spec.ts +1 -1
  127. package/ts/cli-idle.spec.ts +1 -1
  128. package/ts/cli.ts +35 -0
  129. package/ts/cmdKill.bun.spec.ts +155 -0
  130. package/ts/cmdKill.spec.ts +155 -0
  131. package/ts/cmdKill.ts +183 -0
  132. package/ts/composerGuard.edges.spec.ts +91 -0
  133. package/ts/composerGuard.spec.ts +53 -6
  134. package/ts/composerGuard.ts +42 -13
  135. package/ts/conptyModeFilter.spec.ts +30 -0
  136. package/ts/conptyModeFilter.ts +53 -0
  137. package/ts/copyLiagent.ts +42 -0
  138. package/ts/cwdConflictWarn.io.spec.ts +50 -0
  139. package/ts/cwdConflictWarn.spec.ts +34 -1
  140. package/ts/cwdConflictWarn.ts +43 -5
  141. package/ts/cwdGuard.spec.ts +110 -0
  142. package/ts/cwdGuard.ts +118 -0
  143. package/ts/forceKill.ts +52 -0
  144. package/ts/forkNested.bun.spec.ts +41 -0
  145. package/ts/forkNested.spec.ts +41 -0
  146. package/ts/forkNested.ts +48 -1
  147. package/ts/globalPidIndex.edges.spec.ts +125 -0
  148. package/ts/globalPidIndex.ts +6 -2
  149. package/ts/index.ts +13 -1
  150. package/ts/interruptSend.bun.spec.ts +99 -0
  151. package/ts/interruptSend.spec.ts +99 -0
  152. package/ts/interruptSend.ts +51 -0
  153. package/ts/lockRegistry.spec.ts +49 -0
  154. package/ts/lsRenderCache.spec.ts +58 -0
  155. package/ts/needsInput.edges.spec.ts +23 -0
  156. package/ts/parseCliArgs.ts +7 -0
  157. package/ts/pushGatePlan.spec.ts +147 -0
  158. package/ts/roomReplyAlias.io.spec.ts +81 -0
  159. package/ts/schedule.ts +5 -1
  160. package/ts/schedules.bun.spec.ts +397 -0
  161. package/ts/schedules.spec.ts +397 -0
  162. package/ts/schedules.ts +517 -0
  163. package/ts/schedulesCli.ts +386 -0
  164. package/ts/sendControl.bun.spec.ts +12 -0
  165. package/ts/senderAncestry.ts +1 -3
  166. package/ts/serve.ts +22 -33
  167. package/ts/serveRust.spec.ts +29 -2
  168. package/ts/serveRust.ts +27 -2
  169. package/ts/sizeNego.bun.spec.ts +9 -2
  170. package/ts/sizeNego.spec.ts +9 -2
  171. package/ts/sizeNego.ts +10 -5
  172. package/ts/subcommands.spec.ts +348 -9
  173. package/ts/subcommands.ts +513 -93
  174. package/ts/suggestAgents.spec.ts +33 -0
  175. package/ts/terminal/browser.ts +1 -1
  176. package/ts/todoParse.spec.ts +22 -1
  177. package/ts/todoParse.ts +19 -0
  178. package/dist/SUPPORTED_CLIS-ByO5twge.js +0 -12
  179. package/dist/callback-D7qfh_7p.js +0 -16
  180. package/dist/pidStore-BRlKRHXY.js +0 -6
  181. package/dist/roomReplyAlias-DuEkSSDI.js +0 -6
  182. package/dist/spawnGate-DeQBTX-o.js +0 -5
  183. package/dist/subcommands-DmiswRy4.js +0 -14
  184. package/dist/trayApp-C4TAvRvU.js +0 -5
@@ -0,0 +1,651 @@
1
+ import { B as scheduleTick, D as createMic, E as appendDictation, F as micStatus, I as postStt, P as micErrorNote, a as canCapture, d as sileroVad, f as vadFromScripts, g as MIC_ERROR_STRINGS_EN, i as audioCtx, l as micStream, o as energyVad, s as existingCtx } from "./mic-audio-C3Shud_F.js";
2
+
3
+ //#region src/mic-session.ts
4
+ /**
5
+ * The mic as one page-wide session, outside any element: the stream, the VAD
6
+ * (createMic, src/mic.ts), the transcripts on their way, and the hooks of a
7
+ * voice output (MicVoiceLink). It lives on globalThis (MIC_SESSION_KEY), so
8
+ * a remounted `<liagent-mic>`, a framework re-render, a client-side route
9
+ * change or a second copy of the module finds it still listening and
10
+ * re-attaches; disconnecting never stops it — only an explicit "mic off"
11
+ * does. One getUserMedia per capture: a live stream is reused.
12
+ *
13
+ * On a real reload the session is new. If this tab had the mic on
14
+ * (sessionStorage MIC_ON_KEY, per tab, surviving a refresh), it tries to
15
+ * resume at once: with the microphone permission granted (or the query not
16
+ * supported, as on iOS) and the AudioContext running, it turns capture back
17
+ * on without a gesture ({path: "auto", ok: true}). Otherwise the button
18
+ * waits in a "tap to resume" state ({path: "tap", reason}). Either outcome
19
+ * goes to `onResume` and to the view's `resumed`.
20
+ *
21
+ * Everything that touches the browser is injected (MicDeps), so this is
22
+ * tested with fakes (test/mic-session.test.ts).
23
+ */
24
+ /** globalThis key of the session. */
25
+ const MIC_SESSION_KEY = "__liagentMic";
26
+ /** Bumped when the session's shape changes; an older one is migrated, its stream kept. */
27
+ const MIC_SESSION_V = 1;
28
+ /** sessionStorage: "1" while this tab has the mic on (cleared by an explicit off). */
29
+ const MIC_ON_KEY = "liagent:mic";
30
+ const live = (s) => !!s && s.getTracks().length > 0 && s.getTracks().every((t) => t.readyState === "live");
31
+ function createMicSession(deps, from) {
32
+ const key = deps.storageKey ?? MIC_ON_KEY;
33
+ let stream = from?.stream ?? null;
34
+ let view = {
35
+ state: "off",
36
+ pending: 0,
37
+ interim: "",
38
+ error: null,
39
+ resume: false,
40
+ speaking: false
41
+ };
42
+ let cfg;
43
+ let voice = null;
44
+ let unvoice = null;
45
+ const listeners = /* @__PURE__ */ new Set();
46
+ let sink = null;
47
+ /** Transcripts that landed while nothing was attached (mid-remount): handed over on attach. */
48
+ let held = [];
49
+ let gumCalls = 0;
50
+ const emit = (patch) => {
51
+ view = {
52
+ ...view,
53
+ ...patch
54
+ };
55
+ for (const f of listeners) f(view);
56
+ };
57
+ const store = {
58
+ on: () => {
59
+ try {
60
+ deps.storage?.set(key, "1");
61
+ } catch {}
62
+ },
63
+ clear: () => {
64
+ try {
65
+ deps.storage?.remove(key);
66
+ } catch {}
67
+ },
68
+ get: () => {
69
+ try {
70
+ return deps.storage?.get(key) === "1";
71
+ } catch {
72
+ return false;
73
+ }
74
+ }
75
+ };
76
+ /** The live stream, or a new one (the only getUserMedia). */
77
+ const ensureStream = async () => {
78
+ if (stream && live(stream)) return stream;
79
+ gumCalls++;
80
+ stream = await deps.getStream(cfg);
81
+ return stream;
82
+ };
83
+ const stopStream = () => {
84
+ for (const t of stream?.getTracks() ?? []) t.stop();
85
+ stream = null;
86
+ };
87
+ const fallback = deps.fallback;
88
+ let prev = "off";
89
+ const mic = createMic({
90
+ load: async (ev) => deps.load(ev, await ensureStream(), cfg),
91
+ fallback: fallback ? async (ev) => fallback(ev, await ensureStream(), cfg) : void 0,
92
+ upload: async (wav, info) => {
93
+ const text = await deps.upload(wav, info, cfg);
94
+ const echo = text && voice?.echoCheck ? voice.echoCheck(text, info.durationMs) : void 0;
95
+ if (!info.partial) deps.onUtterance?.({
96
+ ...info,
97
+ wav,
98
+ text,
99
+ echo
100
+ }, cfg);
101
+ if (echo?.drop) {
102
+ if (!info.partial) voice?.echoDropped?.();
103
+ return "";
104
+ }
105
+ return text;
106
+ },
107
+ onState: (s) => {
108
+ const was = prev;
109
+ prev = s;
110
+ emit({
111
+ state: s,
112
+ ...s !== "off" ? { resume: false } : {}
113
+ });
114
+ if (voice) {
115
+ voice.setMic(s !== "off");
116
+ if (s === "capturing") voice.userSpeech();
117
+ else if (was === "capturing") voice.userIdle();
118
+ }
119
+ deps.onState?.(s, was);
120
+ },
121
+ onText: (t) => sink ? sink.onText(t) : void held.push(t),
122
+ onPending: (n, interim) => {
123
+ emit({
124
+ pending: n,
125
+ interim
126
+ });
127
+ sink?.onPending(n, interim);
128
+ },
129
+ onError: (e) => emit({ error: e }),
130
+ onProb: (p) => voice?.userFrame?.(p)
131
+ });
132
+ const session = {
133
+ v: MIC_SESSION_V,
134
+ view: () => view,
135
+ stream: () => stream,
136
+ gumCalls: () => gumCalls,
137
+ subscribe(f) {
138
+ listeners.add(f);
139
+ f(view);
140
+ return () => void listeners.delete(f);
141
+ },
142
+ attach(s) {
143
+ sink = s;
144
+ const t = held;
145
+ held = [];
146
+ for (const x of t) s.onText(x);
147
+ s.onPending(view.pending, view.interim);
148
+ return () => {
149
+ if (sink === s) sink = null;
150
+ };
151
+ },
152
+ configure(c) {
153
+ cfg = c;
154
+ },
155
+ config: () => cfg,
156
+ setVoice(link) {
157
+ if (link === voice) return;
158
+ unvoice?.();
159
+ unvoice = null;
160
+ voice = link;
161
+ if (!link) {
162
+ mic.hold(false);
163
+ emit({ speaking: false });
164
+ return;
165
+ }
166
+ const follow = (s) => {
167
+ mic.hold(s.hold);
168
+ if (s.speaking !== view.speaking) emit({ speaking: s.speaking });
169
+ };
170
+ follow(link.state());
171
+ unvoice = link.subscribe(follow);
172
+ if (mic.state() !== "off") link.setMic(true);
173
+ },
174
+ voice: () => voice,
175
+ async toggle() {
176
+ emit({ error: null });
177
+ if (mic.state() === "off") {
178
+ store.on();
179
+ await mic.toggle();
180
+ } else await session.off();
181
+ },
182
+ async off() {
183
+ store.clear();
184
+ emit({ resume: false });
185
+ await mic.off();
186
+ stopStream();
187
+ },
188
+ hold: (on) => mic.hold(on),
189
+ async retire() {
190
+ unvoice?.();
191
+ unvoice = null;
192
+ voice = null;
193
+ listeners.clear();
194
+ sink = null;
195
+ const s = stream;
196
+ stream = null;
197
+ await mic.off();
198
+ return s;
199
+ },
200
+ async autoResume() {
201
+ if (!store.get()) return;
202
+ const done = (r) => {
203
+ emit({
204
+ resumed: r,
205
+ ...r.path === "tap" ? { resume: true } : {}
206
+ });
207
+ deps.onResume?.(r);
208
+ };
209
+ const tap = (reason) => done({
210
+ path: "tap",
211
+ reason
212
+ });
213
+ let perm = "unknown";
214
+ try {
215
+ perm = await deps.permission?.() ?? "unknown";
216
+ } catch {}
217
+ if (perm !== "granted" && perm !== "unknown") return tap(`permission ${perm}`);
218
+ let audio = true;
219
+ try {
220
+ audio = await deps.resumeAudio?.() ?? true;
221
+ } catch {
222
+ audio = false;
223
+ }
224
+ if (!audio) return tap("audio context suspended");
225
+ await mic.toggle();
226
+ if (mic.state() === "off") {
227
+ const e = view.error;
228
+ return tap(e?.name || e?.message || "capture did not start");
229
+ }
230
+ done({
231
+ path: "auto",
232
+ ok: true,
233
+ permission: perm
234
+ });
235
+ }
236
+ };
237
+ if (from?.on) mic.toggle();
238
+ return session;
239
+ }
240
+ /**
241
+ * The page's mic session, made once: an element that connects later (a
242
+ * remount, another copy of the module) gets the same one. An older version
243
+ * is retired and its stream handed to the new session, which resumes
244
+ * listening if it was.
245
+ */
246
+ function micSession(deps) {
247
+ const g = globalThis;
248
+ const have = g[MIC_SESSION_KEY];
249
+ if (have && have.v === MIC_SESSION_V) return have;
250
+ if (have) {
251
+ const on = (have.view?.().state ?? "off") !== "off";
252
+ const kept = have.stream?.() ?? null;
253
+ have.retire?.();
254
+ const next = createMicSession(deps, {
255
+ stream: kept,
256
+ on
257
+ });
258
+ g[MIC_SESSION_KEY] = next;
259
+ return next;
260
+ }
261
+ const s = createMicSession(deps);
262
+ g[MIC_SESSION_KEY] = s;
263
+ queueMicrotask(() => void s.autoResume());
264
+ return s;
265
+ }
266
+
267
+ //#endregion
268
+ //#region src/mic-button.ts
269
+ /** Is `x` something the mic can type into? */
270
+ const isDictationTarget = (x) => !!x && typeof x.setDraft === "function" && typeof x.setPartial === "function" && typeof x.submit === "function";
271
+ /**
272
+ * One final transcript into the target: appended to its draft, punctuated,
273
+ * and sent at once when it ends a sentence with words in it. True if it went
274
+ * out.
275
+ */
276
+ function dictate(box, text) {
277
+ const r = appendDictation(box.draft ?? "", text);
278
+ if (r.draft === box.draft) return false;
279
+ box.setDraft(r.draft, { source: "stt" });
280
+ return r.send && box.submit() !== false;
281
+ }
282
+ const MIC_STRINGS_EN = {
283
+ ...MIC_ERROR_STRINGS_EN,
284
+ dictate: "Dictate: listen and type what you say",
285
+ resume: "Tap to resume listening",
286
+ speaking: "Agent speaking; tap to interrupt and talk",
287
+ recording: "Recording; tap to stop listening",
288
+ listening: "Listening; tap to stop",
289
+ rec: "REC",
290
+ noStt: "No STT endpoint: set stt-url"
291
+ };
292
+ /** The button's label for a view. */
293
+ function micLabel(v, status, t = MIC_STRINGS_EN) {
294
+ if (v.state === "off") return v.resume ? t.resume : t.dictate;
295
+ if (status === "speaking") return t.speaking;
296
+ return v.state === "capturing" ? t.recording : t.listening;
297
+ }
298
+ var NoStt = class extends Error {
299
+ constructor() {
300
+ super("no STT endpoint");
301
+ this.name = "NoStt";
302
+ }
303
+ };
304
+ /** The browser side of the page's session, made by the first element to connect. */
305
+ function browserDeps(storageKey) {
306
+ const ss = () => globalThis.sessionStorage;
307
+ return {
308
+ getStream: (c) => micStream(c?.aec ?? true),
309
+ load: (ev, s, c) => c?.vad ? sileroVad(ev, s, c.vad) : Promise.reject(/* @__PURE__ */ new Error("no VAD source")),
310
+ fallback: (ev, s) => energyVad(ev, s),
311
+ upload: async (wav, info, c) => {
312
+ if (!c?.stt) throw new NoStt();
313
+ const r = await c.stt(wav, info);
314
+ return typeof r === "string" ? r : r.text ?? "";
315
+ },
316
+ onState: (s) => {
317
+ const speaking = globalThis[MIC_SESSION_KEY]?.view().speaking ?? false;
318
+ if (s === "off" && !speaking) existingCtx()?.suspend().catch(() => {});
319
+ },
320
+ onUtterance: (u, c) => c?.onUtterance?.(u),
321
+ storage: {
322
+ get: (k) => ss().getItem(k),
323
+ set: (k, x) => ss().setItem(k, x),
324
+ remove: (k) => ss().removeItem(k)
325
+ },
326
+ storageKey,
327
+ permission: async () => {
328
+ return (await navigator.permissions?.query({ name: "microphone" }))?.state ?? "unknown";
329
+ },
330
+ resumeAudio: async () => {
331
+ const c = audioCtx();
332
+ if (!c) return false;
333
+ if (c.state === "running") return true;
334
+ await Promise.race([c.resume().catch(() => {}), new Promise((r) => setTimeout(r, 800))]);
335
+ return c.state === "running";
336
+ }
337
+ };
338
+ }
339
+ /** Phosphor "microphone" (MIT, (c) 2023 Phosphor Icons), regular and fill. */
340
+ const MIC_PATH = {
341
+ regular: "M128,176a48.05,48.05,0,0,0,48-48V64a48,48,0,0,0-96,0v64A48.05,48.05,0,0,0,128,176ZM96,64a32,32,0,0,1,64,0v64a32,32,0,0,1-64,0Zm40,143.6V240a8,8,0,0,1-16,0V207.6A80.11,80.11,0,0,1,48,128a8,8,0,0,1,16,0,64,64,0,0,0,128,0,8,8,0,0,1,16,0A80.11,80.11,0,0,1,136,207.6Z",
342
+ fill: "M80,128V64a48,48,0,0,1,96,0v64a48,48,0,0,1-96,0Zm128,0a8,8,0,0,0-16,0,64,64,0,0,1-128,0,8,8,0,0,0-16,0,80.11,80.11,0,0,0,72,79.6V240a8,8,0,0,0,16,0V207.6A80.11,80.11,0,0,0,208,128Z"
343
+ };
344
+ const CSS = `
345
+ :host {
346
+ position: relative; display: inline-flex; flex-shrink: 0;
347
+ color: var(--liagent-fg, CanvasText);
348
+ --rec: var(--liagent-mic-rec, oklch(0.6 0.22 25));
349
+ --talk: var(--liagent-mic-talk, oklch(0.62 0.14 250));
350
+ }
351
+ :host([hidden]) { display: none; }
352
+ button {
353
+ all: unset; box-sizing: border-box; position: relative; cursor: pointer;
354
+ display: flex; flex-direction: column; align-items: center; justify-content: center;
355
+ width: 40px; height: 32px; border-radius: 999px; font-size: 16px; line-height: 1;
356
+ color: color-mix(in oklch, currentColor 55%, transparent); touch-action: none;
357
+ -webkit-tap-highlight-color: transparent;
358
+ }
359
+ button::after { content: ""; position: absolute; inset: -4px 0; }
360
+ button::before {
361
+ content: ""; position: absolute; inset: 2px 6px; border-radius: 999px; pointer-events: none;
362
+ }
363
+ button:focus-visible { outline: 2px solid currentColor; outline-offset: 1px; }
364
+ button[data-state="loading"] { opacity: 0.5; }
365
+ button[data-state="listening"], button[data-state="capturing"] { color: var(--rec); }
366
+ button[data-state="listening"]::before { box-shadow: 0 0 0 2px color-mix(in oklch, var(--rec) 35%, transparent); }
367
+ button[data-state="capturing"]::before { box-shadow: 0 0 0 2px var(--rec); animation: pulse 1.2s ease-in-out infinite; }
368
+ button[data-status="speaking"] { color: var(--talk); }
369
+ button[data-status="speaking"]::before { box-shadow: 0 0 0 2px var(--talk); animation: pulse 2.4s ease-in-out infinite; }
370
+ button[data-resume]::before { border: 2px dashed var(--rec); }
371
+ svg { width: 1em; height: 1em; fill: currentColor; }
372
+ .rec { font-size: 8px; font-weight: 700; color: var(--rec); }
373
+ .rec[hidden] { display: none; }
374
+ .note {
375
+ position: absolute; inset-inline-end: 0; bottom: 100%; margin-bottom: 8px;
376
+ white-space: nowrap; font-size: 10px; pointer-events: none; color: oklch(0.6 0.17 45);
377
+ }
378
+ .note:empty { display: none; }
379
+ @keyframes pulse { 50% { opacity: 0.35; } }
380
+ @media (prefers-reduced-motion: reduce) { button::before { animation: none !important; } }
381
+ `;
382
+ const Base = typeof HTMLElement === "undefined" ? class {} : HTMLElement;
383
+ const SVG = "http://www.w3.org/2000/svg";
384
+ /** The mic button. See the header for its attributes, properties and events. */
385
+ var MicElement = class extends Base {
386
+ static observedAttributes = [
387
+ "stt-url",
388
+ "lang",
389
+ "vad-src",
390
+ "ort-src",
391
+ "aec"
392
+ ];
393
+ /** The host's own STT call (else a POST to stt-url). */
394
+ stt;
395
+ sttHeaders;
396
+ sttCredentials;
397
+ /** Silero VAD's source (else vad-src/ort-src; else the energy gate). */
398
+ vad;
399
+ strings = {};
400
+ _box = null;
401
+ _voice;
402
+ _session;
403
+ shadow;
404
+ btn;
405
+ path;
406
+ recEl;
407
+ noteEl;
408
+ off = [];
409
+ last = {};
410
+ lastResume;
411
+ get sttUrl() {
412
+ return this.getAttribute("stt-url") ?? "";
413
+ }
414
+ set sttUrl(v) {
415
+ if (v) this.setAttribute("stt-url", v);
416
+ else this.removeAttribute("stt-url");
417
+ }
418
+ get lang() {
419
+ return this.getAttribute("lang") ?? "";
420
+ }
421
+ set lang(v) {
422
+ if (v) this.setAttribute("lang", v);
423
+ else this.removeAttribute("lang");
424
+ }
425
+ get storageKey() {
426
+ return this.getAttribute("storage-key") || MIC_ON_KEY;
427
+ }
428
+ set storageKey(v) {
429
+ this.setAttribute("storage-key", v);
430
+ }
431
+ /** Echo-cancelled barge-in (default: on unless localStorage `liagent:voice-aec` is "0"; `aec="off"` turns it off). */
432
+ get aec() {
433
+ const a = this.getAttribute("aec");
434
+ if (a === null) try {
435
+ return globalThis.localStorage?.getItem("liagent:voice-aec") !== "0";
436
+ } catch {
437
+ return true;
438
+ }
439
+ return a !== "off" && a !== "false" && a !== "0";
440
+ }
441
+ set aec(v) {
442
+ this.setAttribute("aec", v ? "on" : "off");
443
+ }
444
+ /** A soft tick when a spoken sentence is sent. */
445
+ get tick() {
446
+ return this.hasAttribute("tick");
447
+ }
448
+ set tick(v) {
449
+ this.toggleAttribute("tick", v);
450
+ }
451
+ /** The target; null: the closest <liagent-chat>. */
452
+ get box() {
453
+ if (this._box) return this._box;
454
+ const near = this.closest?.("liagent-chat");
455
+ if (isDictationTarget(near)) return near;
456
+ const host = (this.getRootNode?.())?.host;
457
+ return isDictationTarget(host) ? host : null;
458
+ }
459
+ set box(v) {
460
+ this._box = v;
461
+ this.syncActive(true);
462
+ }
463
+ /** The voice output for hands-free replies (undefined: leave the session's as it is). */
464
+ get voice() {
465
+ return this._session?.voice() ?? this._voice ?? null;
466
+ }
467
+ set voice(v) {
468
+ this._voice = v;
469
+ this._session?.setVoice(v);
470
+ }
471
+ /** The page's mic session (undefined until connected where the browser can capture). */
472
+ get session() {
473
+ return this._session;
474
+ }
475
+ get t() {
476
+ return {
477
+ ...MIC_STRINGS_EN,
478
+ ...this.strings
479
+ };
480
+ }
481
+ config() {
482
+ const url = this.sttUrl;
483
+ const vadSrc = this.getAttribute("vad-src");
484
+ const ortSrc = this.getAttribute("ort-src");
485
+ return {
486
+ stt: this.stt ?? (url ? (wav) => postStt(wav, {
487
+ url,
488
+ lang: this.lang || void 0,
489
+ headers: this.sttHeaders,
490
+ credentials: this.sttCredentials
491
+ }) : void 0),
492
+ vad: this.vad ?? (vadSrc && ortSrc ? vadFromScripts(vadSrc, ortSrc) : void 0),
493
+ aec: this.aec,
494
+ onUtterance: (u) => this.fire("mic-utterance", u)
495
+ };
496
+ }
497
+ attributeChangedCallback() {
498
+ this._session?.configure(this.config());
499
+ }
500
+ connectedCallback() {
501
+ if (!canCapture()) {
502
+ this.hidden = true;
503
+ return;
504
+ }
505
+ this.build();
506
+ const s = this._session = micSession(browserDeps(this.storageKey));
507
+ s.configure(this.config());
508
+ if (this._voice !== void 0) s.setVoice(this._voice);
509
+ this.off.push(s.subscribe((v) => this.render(v)), s.attach({
510
+ onText: (text) => this.onText(text),
511
+ onPending: (n, interim) => this.box?.setPartial(n > 0 && interim ? interim : null)
512
+ }));
513
+ const esc = (e) => {
514
+ if (e.key === "Escape" && s.view().state !== "off") s.off();
515
+ };
516
+ document.addEventListener("keydown", esc);
517
+ this.off.push(() => document.removeEventListener("keydown", esc));
518
+ const box = this.box;
519
+ if (box && typeof box.addEventListener === "function") {
520
+ const out = () => {
521
+ if (s.view().state !== "off") s.voice()?.resume?.();
522
+ };
523
+ box.addEventListener("focusout", out);
524
+ this.off.push(() => box.removeEventListener("focusout", out));
525
+ }
526
+ }
527
+ disconnectedCallback() {
528
+ for (const f of this.off.splice(0)) f();
529
+ }
530
+ /** Turn listening on or off, as a tap does (call it in a gesture). */
531
+ toggle() {
532
+ const s = this._session;
533
+ if (!s) return;
534
+ if (s.view().state === "off") {
535
+ audioCtx()?.resume().catch(() => {});
536
+ s.voice()?.unlock?.();
537
+ }
538
+ s.toggle();
539
+ }
540
+ fire(type, detail, cancelable = false) {
541
+ return this.dispatchEvent(new CustomEvent(type, {
542
+ detail,
543
+ bubbles: true,
544
+ composed: true,
545
+ cancelable
546
+ }));
547
+ }
548
+ onText(text) {
549
+ if (!this.fire("mic-text", { text }, true)) return;
550
+ const box = this.box;
551
+ if (!box || !dictate(box, text)) return;
552
+ this.fire("mic-sent", { text });
553
+ this._session?.voice()?.resume?.();
554
+ this.playTick();
555
+ }
556
+ /** The soft tick, on the mic's AudioContext; never while the agent speaks. */
557
+ playTick() {
558
+ const c = existingCtx();
559
+ if (!this.tick || !c || c.state === "closed" || this._session?.view().speaking) return;
560
+ c.resume().then(() => scheduleTick(c), () => {});
561
+ }
562
+ syncActive(force = false) {
563
+ const on = (this._session?.view().state ?? "off") !== "off";
564
+ if (!force && on === this.last.active) return;
565
+ this.last.active = on;
566
+ const box = this.box;
567
+ if (box) box.micActive = on;
568
+ }
569
+ build() {
570
+ if (this.shadow) return;
571
+ const root = this.shadow = this.attachShadow({ mode: "open" });
572
+ try {
573
+ const sheet = new CSSStyleSheet();
574
+ sheet.replaceSync(CSS);
575
+ root.adoptedStyleSheets = [sheet];
576
+ } catch {
577
+ const st = document.createElement("style");
578
+ st.textContent = CSS;
579
+ root.append(st);
580
+ }
581
+ const note = this.noteEl = document.createElement("span");
582
+ note.className = "note";
583
+ note.setAttribute("role", "alert");
584
+ note.setAttribute("part", "note");
585
+ const btn = this.btn = document.createElement("button");
586
+ btn.type = "button";
587
+ btn.setAttribute("part", "button");
588
+ const svg = document.createElementNS(SVG, "svg");
589
+ svg.setAttribute("viewBox", "0 0 256 256");
590
+ svg.setAttribute("aria-hidden", "true");
591
+ const path = this.path = document.createElementNS(SVG, "path");
592
+ svg.append(path);
593
+ const rec = this.recEl = document.createElement("span");
594
+ rec.className = "rec";
595
+ rec.setAttribute("aria-hidden", "true");
596
+ btn.append(svg, rec);
597
+ btn.addEventListener("pointerdown", (e) => e.preventDefault());
598
+ btn.addEventListener("click", () => {
599
+ const s = this._session;
600
+ if (!s) return;
601
+ const v = s.view();
602
+ if (micStatus(v.state, v.pending, v.speaking) === "speaking" && s.voice()?.interrupt) return s.voice()?.interrupt?.("tap");
603
+ this.toggle();
604
+ });
605
+ root.append(note, btn);
606
+ }
607
+ render(v) {
608
+ const btn = this.btn;
609
+ if (!btn || !this.path || !this.recEl || !this.noteEl) return;
610
+ const t = this.t;
611
+ const status = micStatus(v.state, v.pending, v.speaking);
612
+ btn.dataset.state = v.state;
613
+ btn.dataset.status = status;
614
+ btn.toggleAttribute("data-resume", v.resume && v.state === "off");
615
+ btn.setAttribute("aria-pressed", String(v.state !== "off"));
616
+ btn.setAttribute("aria-label", micLabel(v, status, t));
617
+ btn.title = micLabel(v, status, t);
618
+ this.path.setAttribute("d", v.state === "off" ? MIC_PATH.regular : MIC_PATH.fill);
619
+ this.recEl.textContent = t.rec;
620
+ this.recEl.hidden = v.state !== "capturing";
621
+ const note = v.error ? v.error instanceof NoStt ? t.noStt : micErrorNote(v.error, t) : "";
622
+ this.noteEl.textContent = note;
623
+ if (v.error && v.error !== this.last.error) this.fire("mic-error", {
624
+ error: v.error,
625
+ note
626
+ });
627
+ this.last.error = v.error;
628
+ if (v.state !== this.last.state || status !== this.last.status) {
629
+ this.last.state = v.state;
630
+ this.last.status = status;
631
+ this.fire("mic-state", {
632
+ state: v.state,
633
+ status
634
+ });
635
+ }
636
+ if (v.resumed && v.resumed !== this.lastResume) {
637
+ this.lastResume = v.resumed;
638
+ this.fire("mic-resume", v.resumed);
639
+ }
640
+ this.syncActive();
641
+ }
642
+ };
643
+ /** Register the element (once) under `tag`. Returns the class. */
644
+ function defineMic(tag = "liagent-mic") {
645
+ if (typeof customElements !== "undefined" && !customElements.get(tag)) customElements.define(tag, MicElement);
646
+ return MicElement;
647
+ }
648
+
649
+ //#endregion
650
+ export { isDictationTarget as a, MIC_SESSION_KEY as c, micSession as d, dictate as i, MIC_SESSION_V as l, MicElement as n, micLabel as o, defineMic as r, MIC_ON_KEY as s, MIC_STRINGS_EN as t, createMicSession as u };
651
+ //# sourceMappingURL=mic-button-CtZHFBSG.js.map