@lamplitisles/dsh-nanocodex 0.1.0-beta.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 (276) hide show
  1. package/LICENSE +65 -0
  2. package/NOTICE +2 -0
  3. package/README.md +182 -0
  4. package/THIRD_PARTY_NOTICES.md +29 -0
  5. package/cordis.patch.yml +52 -0
  6. package/dist/index.d.ts +435 -0
  7. package/dist/index.js +3405 -0
  8. package/dist/standard-preset.d.ts +7 -0
  9. package/dist/standard-preset.js +25 -0
  10. package/engine-release.json +14 -0
  11. package/package.json +119 -0
  12. package/presets/standard/agent.cordis.yml +6 -0
  13. package/presets/standard/preset.yml +2 -0
  14. package/vendor/nanocodex/README.md +1344 -0
  15. package/vendor/nanocodex/actions/events.d.mts +10 -0
  16. package/vendor/nanocodex/actions/events.mjs +163 -0
  17. package/vendor/nanocodex/actions/index.d.mts +13 -0
  18. package/vendor/nanocodex/actions/index.mjs +35 -0
  19. package/vendor/nanocodex/actions/session.d.mts +66 -0
  20. package/vendor/nanocodex/actions/session.mjs +59 -0
  21. package/vendor/nanocodex/actions/turn.d.mts +59 -0
  22. package/vendor/nanocodex/actions/turn.mjs +37 -0
  23. package/vendor/nanocodex/actions/voice.d.mts +22 -0
  24. package/vendor/nanocodex/actions/voice.mjs +50 -0
  25. package/vendor/nanocodex/browser/Agent.d.mts +55 -0
  26. package/vendor/nanocodex/browser/Agent.mjs +11 -0
  27. package/vendor/nanocodex/browser/ChatGptSubscription.d.mts +7 -0
  28. package/vendor/nanocodex/browser/ChatGptSubscription.mjs +19 -0
  29. package/vendor/nanocodex/browser/InlineAgent.mjs +266 -0
  30. package/vendor/nanocodex/browser/Transport.d.mts +74 -0
  31. package/vendor/nanocodex/browser/Transport.mjs +61 -0
  32. package/vendor/nanocodex/browser/Voice.d.mts +71 -0
  33. package/vendor/nanocodex/browser/Voice.mjs +291 -0
  34. package/vendor/nanocodex/browser/VoiceSession.mjs +688 -0
  35. package/vendor/nanocodex/browser/WorkerAgent.d.mts +65 -0
  36. package/vendor/nanocodex/browser/WorkerAgent.mjs +1472 -0
  37. package/vendor/nanocodex/browser/agent.worker.mjs +3 -0
  38. package/vendor/nanocodex/browser/config.d.mts +36 -0
  39. package/vendor/nanocodex/browser/config.mjs +439 -0
  40. package/vendor/nanocodex/browser/engine.mjs +13 -0
  41. package/vendor/nanocodex/browser/harness.mjs +55 -0
  42. package/vendor/nanocodex/browser/host.d.mts +83 -0
  43. package/vendor/nanocodex/browser/host.mjs +555 -0
  44. package/vendor/nanocodex/browser/hostManagedWebSocket.d.mts +14 -0
  45. package/vendor/nanocodex/browser/hostManagedWebSocket.mjs +110 -0
  46. package/vendor/nanocodex/browser/index.d.mts +92 -0
  47. package/vendor/nanocodex/browser/index.mjs +23 -0
  48. package/vendor/nanocodex/browser/indexeddb-durability-store.mjs +197 -0
  49. package/vendor/nanocodex/browser/workspace.d.mts +16 -0
  50. package/vendor/nanocodex/browser/workspace.mjs +107 -0
  51. package/vendor/nanocodex/cloud/Client.d.mts +83 -0
  52. package/vendor/nanocodex/cloud/Client.mjs +231 -0
  53. package/vendor/nanocodex/cloud/Decorator.d.mts +31 -0
  54. package/vendor/nanocodex/cloud/Decorator.mjs +37 -0
  55. package/vendor/nanocodex/cloud/Dialog.d.mts +113 -0
  56. package/vendor/nanocodex/cloud/Dialog.mjs +366 -0
  57. package/vendor/nanocodex/cloud/Errors.d.mts +22 -0
  58. package/vendor/nanocodex/cloud/Errors.mjs +39 -0
  59. package/vendor/nanocodex/cloud/Principal.d.mts +26 -0
  60. package/vendor/nanocodex/cloud/Principal.mjs +88 -0
  61. package/vendor/nanocodex/cloud/RemoteProvider.mjs +88 -0
  62. package/vendor/nanocodex/cloud/Transport.d.mts +47 -0
  63. package/vendor/nanocodex/cloud/Transport.mjs +454 -0
  64. package/vendor/nanocodex/cloud/actions/account.d.mts +9 -0
  65. package/vendor/nanocodex/cloud/actions/account.mjs +19 -0
  66. package/vendor/nanocodex/cloud/actions/agent.d.mts +18 -0
  67. package/vendor/nanocodex/cloud/actions/agent.mjs +428 -0
  68. package/vendor/nanocodex/cloud/actions/connection.d.mts +133 -0
  69. package/vendor/nanocodex/cloud/actions/connection.mjs +580 -0
  70. package/vendor/nanocodex/cloud/actions/grant.d.mts +10 -0
  71. package/vendor/nanocodex/cloud/actions/grant.mjs +12 -0
  72. package/vendor/nanocodex/cloud/actions/index.d.mts +7 -0
  73. package/vendor/nanocodex/cloud/actions/index.mjs +7 -0
  74. package/vendor/nanocodex/cloud/actions/machineUsd.d.mts +23 -0
  75. package/vendor/nanocodex/cloud/actions/machineUsd.mjs +37 -0
  76. package/vendor/nanocodex/cloud/actions/model.d.mts +12 -0
  77. package/vendor/nanocodex/cloud/actions/model.mjs +50 -0
  78. package/vendor/nanocodex/cloud/actions/mpp.d.mts +28 -0
  79. package/vendor/nanocodex/cloud/actions/mpp.mjs +39 -0
  80. package/vendor/nanocodex/cloud/index.d.mts +30 -0
  81. package/vendor/nanocodex/cloud/index.mjs +9 -0
  82. package/vendor/nanocodex/cloud/internal.mjs +533 -0
  83. package/vendor/nanocodex/cloud/mercator.mjs +108 -0
  84. package/vendor/nanocodex/cloud/server/HostPrincipal.d.mts +34 -0
  85. package/vendor/nanocodex/cloud/server/HostPrincipal.mjs +202 -0
  86. package/vendor/nanocodex/cloud/server/index.d.mts +1 -0
  87. package/vendor/nanocodex/cloud/server/index.mjs +1 -0
  88. package/vendor/nanocodex/cloud/types.d.mts +211 -0
  89. package/vendor/nanocodex/cloudflare/Agent.d.mts +133 -0
  90. package/vendor/nanocodex/cloudflare/Agent.mjs +811 -0
  91. package/vendor/nanocodex/cloudflare/egress-subject.mjs +19 -0
  92. package/vendor/nanocodex/cloudflare/egress.d.mts +41 -0
  93. package/vendor/nanocodex/cloudflare/egress.mjs +119 -0
  94. package/vendor/nanocodex/cloudflare/event-socket.mjs +316 -0
  95. package/vendor/nanocodex/cloudflare/index.d.mts +7 -0
  96. package/vendor/nanocodex/cloudflare/index.mjs +6 -0
  97. package/vendor/nanocodex/host/Agent.d.mts +55 -0
  98. package/vendor/nanocodex/host/Agent.mjs +1 -0
  99. package/vendor/nanocodex/host/index.d.mts +82 -0
  100. package/vendor/nanocodex/host/index.mjs +10 -0
  101. package/vendor/nanocodex/index.d.mts +81 -0
  102. package/vendor/nanocodex/index.mjs +12 -0
  103. package/vendor/nanocodex/internal.mjs +1275 -0
  104. package/vendor/nanocodex/managed/Agent.d.mts +380 -0
  105. package/vendor/nanocodex/managed/Agent.mjs +1694 -0
  106. package/vendor/nanocodex/managed/ManagedError.d.mts +9 -0
  107. package/vendor/nanocodex/managed/ManagedError.mjs +8 -0
  108. package/vendor/nanocodex/managed/README.md +241 -0
  109. package/vendor/nanocodex/managed/Voice.mjs +202 -0
  110. package/vendor/nanocodex/managed/index.d.mts +20 -0
  111. package/vendor/nanocodex/managed/index.mjs +2 -0
  112. package/vendor/nanocodex/managed/internal.mjs +119 -0
  113. package/vendor/nanocodex/node/Agent.d.mts +50 -0
  114. package/vendor/nanocodex/node/Agent.mjs +203 -0
  115. package/vendor/nanocodex/node/ChatGptSubscription.d.mts +9 -0
  116. package/vendor/nanocodex/node/ChatGptSubscription.mjs +17 -0
  117. package/vendor/nanocodex/node/Transport.d.mts +44 -0
  118. package/vendor/nanocodex/node/Transport.mjs +46 -0
  119. package/vendor/nanocodex/node/host.mjs +731 -0
  120. package/vendor/nanocodex/node/index.d.mts +73 -0
  121. package/vendor/nanocodex/node/index.mjs +17 -0
  122. package/vendor/nanocodex/node/workspace.d.mts +10 -0
  123. package/vendor/nanocodex/node/workspace.mjs +198 -0
  124. package/vendor/nanocodex/package.json +200 -0
  125. package/vendor/nanocodex/pkg-node/nanocodex.d.ts +675 -0
  126. package/vendor/nanocodex/pkg-node/nanocodex.js +2628 -0
  127. package/vendor/nanocodex/pkg-node/nanocodex_bg.wasm.d.ts +105 -0
  128. package/vendor/nanocodex/pkg-node/package.json +1 -0
  129. package/vendor/nanocodex/pkg-web/.nanocodex-bindgen-stamp +6 -0
  130. package/vendor/nanocodex/pkg-web/nanocodex-build.json +1 -0
  131. package/vendor/nanocodex/pkg-web/nanocodex.d.ts +805 -0
  132. package/vendor/nanocodex/pkg-web/nanocodex.js +2714 -0
  133. package/vendor/nanocodex/pkg-web/nanocodex_bg.js +2615 -0
  134. package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm +0 -0
  135. package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm.d.ts +105 -0
  136. package/vendor/nanocodex/pkg-web/nanocodex_worker.js +9 -0
  137. package/vendor/nanocodex/pkg-web/package.json +1 -0
  138. package/vendor/nanocodex/runtime/chatgpt-subscription.mjs +212 -0
  139. package/vendor/nanocodex/runtime/cloudflare-durability-store.d.mts +26 -0
  140. package/vendor/nanocodex/runtime/cloudflare-durability-store.mjs +168 -0
  141. package/vendor/nanocodex/runtime/code-evaluator.worker.mjs +137 -0
  142. package/vendor/nanocodex/runtime/code-runtime.mjs +1 -0
  143. package/vendor/nanocodex/runtime/durability-store.d.mts +85 -0
  144. package/vendor/nanocodex/runtime/durability-store.mjs +631 -0
  145. package/vendor/nanocodex/runtime/durability.mjs +153 -0
  146. package/vendor/nanocodex/runtime/managed-transport.mjs +460 -0
  147. package/vendor/nanocodex/runtime/mcp-runtime.mjs +534 -0
  148. package/vendor/nanocodex/runtime/postgres-durability-store.d.mts +64 -0
  149. package/vendor/nanocodex/runtime/postgres-durability-store.mjs +532 -0
  150. package/vendor/nanocodex/runtime/quickjs-evaluator.d.mts +17 -0
  151. package/vendor/nanocodex/runtime/quickjs-evaluator.mjs +236 -0
  152. package/vendor/nanocodex/runtime/responses-transport.mjs +22 -0
  153. package/vendor/nanocodex/runtime/subagents.d.mts +107 -0
  154. package/vendor/nanocodex/runtime/subagents.mjs +54 -0
  155. package/vendor/nanocodex/runtime/subscription-store.d.mts +8 -0
  156. package/vendor/nanocodex/runtime/subscription-store.mjs +42 -0
  157. package/vendor/nanocodex/runtime/tempo-provider.d.mts +125 -0
  158. package/vendor/nanocodex/runtime/tempo-provider.mjs +264 -0
  159. package/vendor/nanocodex/runtime/tool-configuration.mjs +1 -0
  160. package/vendor/nanocodex/runtime/tool-router.mjs +1 -0
  161. package/vendor/nanocodex/runtime/utf8.mjs +1 -0
  162. package/vendor/nanocodex/runtime/worker-evaluator.mjs +162 -0
  163. package/vendor/nanocodex/runtime/workspace.d.mts +1 -0
  164. package/vendor/nanocodex/runtime/workspace.mjs +1 -0
  165. package/vendor/nanocodex/tools/Tools.d.mts +56 -0
  166. package/vendor/nanocodex/tools/Tools.mjs +136 -0
  167. package/vendor/nanocodex/tools/artifact.d.mts +1 -0
  168. package/vendor/nanocodex/tools/artifact.mjs +1 -0
  169. package/vendor/nanocodex/tools/attachment.mjs +1 -0
  170. package/vendor/nanocodex/tools/bash.d.mts +1 -0
  171. package/vendor/nanocodex/tools/bash.mjs +1 -0
  172. package/vendor/nanocodex/tools/browser/accountInfo.mjs +795 -0
  173. package/vendor/nanocodex/tools/browser/browserBuffer.mjs +18 -0
  174. package/vendor/nanocodex/tools/browser/browserCompiler.mjs +155 -0
  175. package/vendor/nanocodex/tools/browser/browserEgress.mjs +161 -0
  176. package/vendor/nanocodex/tools/browser/browserPython.mjs +133 -0
  177. package/vendor/nanocodex/tools/browser/browserShell.mjs +1075 -0
  178. package/vendor/nanocodex/tools/browser/browserSprintf.mjs +4 -0
  179. package/vendor/nanocodex/tools/browser/browserSsh.mjs +201 -0
  180. package/vendor/nanocodex/tools/browser/browserZlib.mjs +136 -0
  181. package/vendor/nanocodex/tools/browser/compiler.worker.mjs +160 -0
  182. package/vendor/nanocodex/tools/browser/devTunnelsSshBrowser.mjs +13 -0
  183. package/vendor/nanocodex/tools/browser/index.d.mts +209 -0
  184. package/vendor/nanocodex/tools/browser/index.mjs +174 -0
  185. package/vendor/nanocodex/tools/browser/opfsGit.mjs +248 -0
  186. package/vendor/nanocodex/tools/browser/python.worker.mjs +114 -0
  187. package/vendor/nanocodex/tools/browser/threadGit.mjs +197 -0
  188. package/vendor/nanocodex/tools/browser/unsupportedNodeRsa.mjs +5 -0
  189. package/vendor/nanocodex/tools/browser/workspace.mjs +83 -0
  190. package/vendor/nanocodex/tools/dataset.d.mts +1 -0
  191. package/vendor/nanocodex/tools/dataset.mjs +1 -0
  192. package/vendor/nanocodex/tools/datasetContract.mjs +1 -0
  193. package/vendor/nanocodex/tools/datasetEngine.mjs +1 -0
  194. package/vendor/nanocodex/tools/hostedCatalog.d.mts +1 -0
  195. package/vendor/nanocodex/tools/hostedCatalog.mjs +1 -0
  196. package/vendor/nanocodex/tools/index.d.mts +49 -0
  197. package/vendor/nanocodex/tools/index.mjs +19 -0
  198. package/vendor/nanocodex/tools/namedTool.mjs +1 -0
  199. package/vendor/nanocodex/tools/repository-workspace.d.mts +1 -0
  200. package/vendor/nanocodex/tools/repository-workspace.mjs +1 -0
  201. package/vendor/nanocodex/tools/ssh.d.mts +1 -0
  202. package/vendor/nanocodex/tools/ssh.mjs +1 -0
  203. package/vendor/nanocodex/tools/standard.mjs +1 -0
  204. package/vendor/nanocodex/tools/standardDescriptions.mjs +1 -0
  205. package/vendor/nanocodex/types.d.mts +663 -0
  206. package/vendor/nanocodex/wasm.d.mts +4 -0
  207. package/vendor/nanocodex/worker/ChatGptSubscription.d.mts +9 -0
  208. package/vendor/nanocodex/worker/ChatGptSubscription.mjs +29 -0
  209. package/vendor/nanocodex/worker/index.d.mts +18 -0
  210. package/vendor/nanocodex/worker/index.mjs +5 -0
  211. package/vendor/nanocodex-tools/README.md +65 -0
  212. package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.d.ts +7 -0
  213. package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.js +14 -0
  214. package/vendor/nanocodex-tools/dist/hosted/broker-core.d.ts +172 -0
  215. package/vendor/nanocodex-tools/dist/hosted/broker-core.js +1278 -0
  216. package/vendor/nanocodex-tools/dist/hosted/index.d.ts +3 -0
  217. package/vendor/nanocodex-tools/dist/hosted/index.js +3 -0
  218. package/vendor/nanocodex-tools/dist/hosted/protocol.d.ts +120 -0
  219. package/vendor/nanocodex-tools/dist/hosted/protocol.js +415 -0
  220. package/vendor/nanocodex-tools/dist/index.d.ts +19 -0
  221. package/vendor/nanocodex-tools/dist/index.js +14 -0
  222. package/vendor/nanocodex-tools/dist/memory.d.ts +204 -0
  223. package/vendor/nanocodex-tools/dist/memory.js +411 -0
  224. package/vendor/nanocodex-tools/dist/namespace.d.ts +87 -0
  225. package/vendor/nanocodex-tools/dist/namespace.js +338 -0
  226. package/vendor/nanocodex-tools/dist/repository-archive.d.ts +4 -0
  227. package/vendor/nanocodex-tools/dist/repository-archive.js +104 -0
  228. package/vendor/nanocodex-tools/dist/runtime.d.ts +30 -0
  229. package/vendor/nanocodex-tools/dist/runtime.js +37 -0
  230. package/vendor/nanocodex-tools/dist/session.d.ts +167 -0
  231. package/vendor/nanocodex-tools/dist/session.js +286 -0
  232. package/vendor/nanocodex-tools/dist/shell.d.ts +51 -0
  233. package/vendor/nanocodex-tools/dist/shell.js +553 -0
  234. package/vendor/nanocodex-tools/dist/ssh.d.ts +29 -0
  235. package/vendor/nanocodex-tools/dist/ssh.js +66 -0
  236. package/vendor/nanocodex-tools/dist/workspace.d.ts +45 -0
  237. package/vendor/nanocodex-tools/dist/workspace.js +237 -0
  238. package/vendor/nanocodex-tools/package.json +122 -0
  239. package/vendor/nanocodex-tools/runtime/code-runtime.mjs +817 -0
  240. package/vendor/nanocodex-tools/runtime/tool-configuration.mjs +50 -0
  241. package/vendor/nanocodex-tools/runtime/tool-router.mjs +730 -0
  242. package/vendor/nanocodex-tools/runtime/utf8.d.mts +2 -0
  243. package/vendor/nanocodex-tools/runtime/utf8.mjs +25 -0
  244. package/vendor/nanocodex-tools/tools/artifact.d.mts +69 -0
  245. package/vendor/nanocodex-tools/tools/artifact.mjs +270 -0
  246. package/vendor/nanocodex-tools/tools/attachment.mjs +603 -0
  247. package/vendor/nanocodex-tools/tools/bash.d.mts +90 -0
  248. package/vendor/nanocodex-tools/tools/bash.mjs +742 -0
  249. package/vendor/nanocodex-tools/tools/dataset.d.mts +9 -0
  250. package/vendor/nanocodex-tools/tools/dataset.mjs +47 -0
  251. package/vendor/nanocodex-tools/tools/datasetContract.mjs +54 -0
  252. package/vendor/nanocodex-tools/tools/datasetEngine.mjs +1167 -0
  253. package/vendor/nanocodex-tools/tools/execution-contract.d.mts +5 -0
  254. package/vendor/nanocodex-tools/tools/execution-contract.mjs +63 -0
  255. package/vendor/nanocodex-tools/tools/hostedCatalog.d.mts +20 -0
  256. package/vendor/nanocodex-tools/tools/hostedCatalog.mjs +67 -0
  257. package/vendor/nanocodex-tools/tools/hostedMachine.d.mts +10 -0
  258. package/vendor/nanocodex-tools/tools/hostedMachine.mjs +56 -0
  259. package/vendor/nanocodex-tools/tools/just-bash-browser.d.mts +1 -0
  260. package/vendor/nanocodex-tools/tools/just-bash-browser.mjs +1 -0
  261. package/vendor/nanocodex-tools/tools/namedTool.d.mts +3 -0
  262. package/vendor/nanocodex-tools/tools/namedTool.mjs +3 -0
  263. package/vendor/nanocodex-tools/tools/nodeProcess.d.mts +10 -0
  264. package/vendor/nanocodex-tools/tools/nodeProcess.mjs +314 -0
  265. package/vendor/nanocodex-tools/tools/processOutput.mjs +56 -0
  266. package/vendor/nanocodex-tools/tools/repository-workspace.d.mts +55 -0
  267. package/vendor/nanocodex-tools/tools/repository-workspace.mjs +397 -0
  268. package/vendor/nanocodex-tools/tools/ssh.d.mts +56 -0
  269. package/vendor/nanocodex-tools/tools/ssh.mjs +503 -0
  270. package/vendor/nanocodex-tools/tools/standard.mjs +458 -0
  271. package/vendor/nanocodex-tools/tools/standardDescriptions.mjs +3 -0
  272. package/vendor/nanocodex-tools/tools/types.d.mts +83 -0
  273. package/vendor/nanocodex-tools/tools/workspace.d.mts +25 -0
  274. package/vendor/nanocodex-tools/tools/workspace.mjs +282 -0
  275. package/vendor/nanocodex-tools/tools/x.d.mts +30 -0
  276. package/vendor/nanocodex-tools/tools/x.mjs +91 -0
@@ -0,0 +1,291 @@
1
+ import { createBrowserVoice } from "../internal.mjs";
2
+ import {
3
+ managedBrowserVoiceTransport,
4
+ observeManagedAgentEvents,
5
+ } from "../managed/internal.mjs";
6
+ import { createManagedBrowserVoice } from "../managed/Voice.mjs";
7
+ import { BrowserVoiceSession } from "./VoiceSession.mjs";
8
+
9
+ export { VoiceError } from "./VoiceSession.mjs";
10
+
11
+ export const voices = Object.freeze([
12
+ "juniper", "maple", "spruce", "ember", "vale", "breeze", "arbor", "sol", "cove",
13
+ ]);
14
+ export const defaultVoice = "cove";
15
+
16
+ const IDLE_SNAPSHOT = Object.freeze({
17
+ error: undefined,
18
+ status: "idle",
19
+ statusText: undefined,
20
+ transcripts: Object.freeze([]),
21
+ voice: undefined,
22
+ });
23
+ const activeResources = new WeakMap();
24
+
25
+ /** Creates a thin browser binding over the Rust-owned Codex voice controller. */
26
+ export function create(agent, options = {}) {
27
+ validateAgent(agent);
28
+ validateOptions(options);
29
+ const listeners = new Set();
30
+ const eventListeners = new Set();
31
+ const managed = agent.type === "managed" || agent.type === "connect";
32
+ const managedTransport = managed ? managedBrowserVoiceTransport(agent) : undefined;
33
+ if (agent.type === "managed" && managedTransport?.sameOrigin) {
34
+ const browserOrigin = browserLocationOrigin();
35
+ if (browserOrigin !== undefined && browserOrigin !== managedTransport.origin) {
36
+ throw new TypeError("Voice.create requires a same-origin managed Agent host; use Connect for cross-origin agents");
37
+ }
38
+ }
39
+ const sessionId = managed ? agent.id : agent.sessionId;
40
+ const target = Object.freeze({ pane: "main", branchId: sessionId });
41
+ let snapshot = IDLE_SNAPSHOT;
42
+ let session;
43
+ let startPromise;
44
+ let stopPromise;
45
+ let watcher;
46
+ let releaseEvents;
47
+ let destroyed = false;
48
+ let generation = 0;
49
+ let resource;
50
+
51
+ function publish(next) {
52
+ snapshot = Object.freeze({
53
+ error: next.error,
54
+ status: next.status,
55
+ statusText: next.statusText,
56
+ transcripts: next.transcripts ?? snapshot.transcripts,
57
+ voice: next.voice,
58
+ });
59
+ for (const listener of listeners) listener();
60
+ }
61
+
62
+ function emit(event) {
63
+ for (const listener of eventListeners) listener(event);
64
+ }
65
+
66
+ function cleanupWatcher() {
67
+ releaseEvents?.();
68
+ releaseEvents = undefined;
69
+ watcher?.off();
70
+ watcher = undefined;
71
+ }
72
+
73
+ function observeAgentEvents(active) {
74
+ if (managed) {
75
+ releaseEvents = observeManagedAgentEvents(agent, ({ event, turnId, cursor }) => (
76
+ active.observe({ type: "event", target, event, turnId, cursor })
77
+ ));
78
+ return;
79
+ }
80
+ watcher = agent.events.watch({ includeAllSessions: false });
81
+ releaseEvents = watcher.onEvent((event) => active.observe({ type: "event", target, event }));
82
+ }
83
+
84
+ async function start(parameters = {}) {
85
+ if (destroyed) throw new Error("voice resource is destroyed");
86
+ const selectedVoice = parameters.voice ?? options.voice ?? defaultVoice;
87
+ if (!voices.includes(selectedVoice)) throw new TypeError(`unsupported ChatGPT voice: ${selectedVoice}`);
88
+ if (session) return startPromise;
89
+ if (stopPromise) await stopPromise.catch(() => {});
90
+ if (destroyed) throw new Error("voice resource is destroyed");
91
+ if (session) return startPromise;
92
+ const previous = activeResources.get(agent);
93
+ if (previous && previous !== resource) await previous.stop().catch(() => {});
94
+ activeResources.set(agent, resource);
95
+ const current = ++generation;
96
+ publish({
97
+ error: undefined,
98
+ status: "connecting",
99
+ statusText: undefined,
100
+ transcripts: snapshot.transcripts,
101
+ voice: selectedVoice,
102
+ });
103
+ emit(Object.freeze({ type: "connecting", voice: selectedVoice }));
104
+
105
+ const core = Promise.resolve().then(() => managed
106
+ ? createManagedBrowserVoice(agent, selectedVoice)
107
+ : createBrowserVoice(agent, selectedVoice));
108
+ const transport = managedTransport;
109
+ const transcriptRows = new Map();
110
+ const next = new BrowserVoiceSession({
111
+ core,
112
+ sessionId,
113
+ voice: selectedVoice,
114
+ settings: voiceSettings({ ...options, ...parameters, voice: selectedVoice }),
115
+ ...(transport?.call === undefined ? {} : { call: transport.call }),
116
+ ...(transport?.sidebandUrl === undefined ? {} : { sidebandUrl: transport.sidebandUrl }),
117
+ ...(options.callUrl === undefined ? {} : { callUrl: options.callUrl }),
118
+ ...(options.sidebandUrl === undefined ? {} : { sidebandUrl: options.sidebandUrl }),
119
+ ...(options.captureMicrophone === undefined ? {} : { captureMicrophone: options.captureMicrophone }),
120
+ ...(options.beforeAgentTurn === undefined ? {} : { beforeAgentTurn: options.beforeAgentTurn }),
121
+ onStatus(text) {
122
+ if (session === next && generation === current && snapshot.status !== "idle" && snapshot.status !== "error") {
123
+ publish({ ...snapshot, statusText: text });
124
+ }
125
+ },
126
+ onTranscript(speaker, text, metadata = {}) {
127
+ if (session !== next || generation !== current) return;
128
+ const key = metadata.id === undefined ? undefined : `${speaker}:${metadata.id}`;
129
+ const index = key === undefined ? undefined : transcriptRows.get(key);
130
+ if (!text.trim() && index === undefined) return;
131
+ const entry = Object.freeze({ speaker, text, ...(key === undefined ? {} : {
132
+ id: `${current}:${key}`, isPartial: metadata.is_partial === true,
133
+ }) });
134
+ const transcripts = [...snapshot.transcripts];
135
+ if (index === undefined) {
136
+ if (key !== undefined) transcriptRows.set(key, transcripts.length);
137
+ transcripts.push(entry);
138
+ } else {
139
+ transcripts[index] = entry;
140
+ }
141
+ publish({ ...snapshot, transcripts: Object.freeze(transcripts) });
142
+ emit(Object.freeze({ type: metadata.is_partial ? "transcript.delta" : "transcript", ...entry }));
143
+ },
144
+ onTerminated(message) {
145
+ if (session !== next || destroyed || generation !== current) return;
146
+ generation += 1;
147
+ session = undefined;
148
+ cleanupWatcher();
149
+ const closing = next.close().catch(() => next.abort()).finally(() => {
150
+ if (stopPromise === closing) stopPromise = undefined;
151
+ });
152
+ stopPromise = closing;
153
+ if (activeResources.get(agent) === resource) activeResources.delete(agent);
154
+ const error = new Error(message);
155
+ publish({ ...snapshot, error, status: "error", statusText: message });
156
+ emit(Object.freeze({ type: "error", error }));
157
+ },
158
+ });
159
+ session = next;
160
+ observeAgentEvents(next);
161
+ startPromise = next.start().then(() => {
162
+ if (destroyed || session !== next || generation !== current) return;
163
+ publish({ ...snapshot, error: undefined, status: "active", statusText: `Voice active (${selectedVoice})` });
164
+ emit(Object.freeze({ type: "started", voice: selectedVoice }));
165
+ }).catch(async (cause) => {
166
+ if (session === next) session = undefined;
167
+ if (activeResources.get(agent) === resource) activeResources.delete(agent);
168
+ cleanupWatcher();
169
+ await next.close().catch(() => next.abort());
170
+ if (destroyed || generation !== current) return;
171
+ const error = cause instanceof Error ? cause : new Error(String(cause));
172
+ publish({ ...snapshot, error, status: "error", statusText: error.message });
173
+ emit(Object.freeze({ type: "error", error }));
174
+ throw error;
175
+ }).finally(() => {
176
+ if (generation === current) startPromise = undefined;
177
+ });
178
+ return startPromise;
179
+ }
180
+
181
+ async function stop() {
182
+ generation += 1;
183
+ if (stopPromise) return stopPromise;
184
+ const active = session;
185
+ session = undefined;
186
+ cleanupWatcher();
187
+ if (activeResources.get(agent) === resource) activeResources.delete(agent);
188
+ if (!active) {
189
+ if (snapshot.status !== "idle") publish(IDLE_SNAPSHOT);
190
+ return;
191
+ }
192
+ if (!destroyed) {
193
+ publish(IDLE_SNAPSHOT);
194
+ emit(Object.freeze({ type: "stopped" }));
195
+ }
196
+ stopPromise = active.close().catch((cause) => {
197
+ active.abort();
198
+ if (!destroyed) {
199
+ const error = cause instanceof Error ? cause : new Error(String(cause));
200
+ publish({ ...IDLE_SNAPSHOT, error, status: "error", statusText: error.message });
201
+ emit(Object.freeze({ type: "error", error }));
202
+ }
203
+ throw cause;
204
+ }).finally(() => { stopPromise = undefined; });
205
+ return stopPromise;
206
+ }
207
+
208
+ async function destroy() {
209
+ if (destroyed) return;
210
+ destroyed = true;
211
+ generation += 1;
212
+ const active = session;
213
+ session = undefined;
214
+ cleanupWatcher();
215
+ if (activeResources.get(agent) === resource) activeResources.delete(agent);
216
+ if (active) await active.close().catch(() => active.abort());
217
+ else if (stopPromise) await stopPromise.catch(() => {});
218
+ listeners.clear();
219
+ eventListeners.clear();
220
+ }
221
+
222
+ function command(method, ...args) {
223
+ if (!session || snapshot.status !== "active") return Promise.reject(new Error("voice is not active"));
224
+ return session.command(method, ...args);
225
+ }
226
+
227
+ resource = Object.freeze({
228
+ cancel: async () => {
229
+ if (!session) return false;
230
+ if (snapshot.status === "connecting") {
231
+ await stop();
232
+ return true;
233
+ }
234
+ return session.cancel();
235
+ },
236
+ destroy,
237
+ speak: (text) => command("appendSpeech", text),
238
+ appendText: (text, { role = "user" } = {}) => command("appendText", role, text),
239
+ appendContext: (text) => command("appendContext", text),
240
+ getSnapshot: () => snapshot,
241
+ onEvent(listener) {
242
+ if (typeof listener !== "function") throw new TypeError("voice event listener must be a function");
243
+ eventListeners.add(listener);
244
+ return () => eventListeners.delete(listener);
245
+ },
246
+ start,
247
+ stop,
248
+ subscribe(listener) {
249
+ if (typeof listener !== "function") throw new TypeError("voice listener must be a function");
250
+ listeners.add(listener);
251
+ return () => listeners.delete(listener);
252
+ },
253
+ toggle: (parameters) => session ? stop() : start(parameters),
254
+ });
255
+ return resource;
256
+ }
257
+
258
+ function validateAgent(agent) {
259
+ const local = typeof agent?.sessionId === "string";
260
+ const managed = (agent?.type === "managed" || agent?.type === "connect") && typeof agent.id === "string"
261
+ && typeof agent.turn?.prompt === "function";
262
+ if (!agent || typeof agent !== "object" || (!local && !managed) || typeof agent.events?.watch !== "function") {
263
+ throw new TypeError("Voice.create requires a Nanocodex Agent");
264
+ }
265
+ }
266
+
267
+ function validateOptions(options) {
268
+ if (!options || typeof options !== "object" || Array.isArray(options)) {
269
+ throw new TypeError("Voice.create options must be an object");
270
+ }
271
+ if (options.captureMicrophone !== undefined && typeof options.captureMicrophone !== "function") {
272
+ throw new TypeError("voice captureMicrophone must be a function");
273
+ }
274
+ if (options.beforeAgentTurn !== undefined && typeof options.beforeAgentTurn !== "function") {
275
+ throw new TypeError("voice beforeAgentTurn must be a function");
276
+ }
277
+ }
278
+
279
+ function browserLocationOrigin() {
280
+ try {
281
+ const origin = globalThis.location?.origin;
282
+ return typeof origin === "string" && origin !== "null" ? origin : undefined;
283
+ } catch {
284
+ return undefined;
285
+ }
286
+ }
287
+
288
+ function voiceSettings(options) {
289
+ return Object.fromEntries(["voice", "instructions", "pace", "updates", "handoffMode", "acknowledgements"]
290
+ .filter((key) => options[key] !== undefined).map((key) => [key, options[key]]));
291
+ }