@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.
- package/LICENSE +65 -0
- package/NOTICE +2 -0
- package/README.md +182 -0
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/cordis.patch.yml +52 -0
- package/dist/index.d.ts +435 -0
- package/dist/index.js +3405 -0
- package/dist/standard-preset.d.ts +7 -0
- package/dist/standard-preset.js +25 -0
- package/engine-release.json +14 -0
- package/package.json +119 -0
- package/presets/standard/agent.cordis.yml +6 -0
- package/presets/standard/preset.yml +2 -0
- package/vendor/nanocodex/README.md +1344 -0
- package/vendor/nanocodex/actions/events.d.mts +10 -0
- package/vendor/nanocodex/actions/events.mjs +163 -0
- package/vendor/nanocodex/actions/index.d.mts +13 -0
- package/vendor/nanocodex/actions/index.mjs +35 -0
- package/vendor/nanocodex/actions/session.d.mts +66 -0
- package/vendor/nanocodex/actions/session.mjs +59 -0
- package/vendor/nanocodex/actions/turn.d.mts +59 -0
- package/vendor/nanocodex/actions/turn.mjs +37 -0
- package/vendor/nanocodex/actions/voice.d.mts +22 -0
- package/vendor/nanocodex/actions/voice.mjs +50 -0
- package/vendor/nanocodex/browser/Agent.d.mts +55 -0
- package/vendor/nanocodex/browser/Agent.mjs +11 -0
- package/vendor/nanocodex/browser/ChatGptSubscription.d.mts +7 -0
- package/vendor/nanocodex/browser/ChatGptSubscription.mjs +19 -0
- package/vendor/nanocodex/browser/InlineAgent.mjs +266 -0
- package/vendor/nanocodex/browser/Transport.d.mts +74 -0
- package/vendor/nanocodex/browser/Transport.mjs +61 -0
- package/vendor/nanocodex/browser/Voice.d.mts +71 -0
- package/vendor/nanocodex/browser/Voice.mjs +291 -0
- package/vendor/nanocodex/browser/VoiceSession.mjs +688 -0
- package/vendor/nanocodex/browser/WorkerAgent.d.mts +65 -0
- package/vendor/nanocodex/browser/WorkerAgent.mjs +1472 -0
- package/vendor/nanocodex/browser/agent.worker.mjs +3 -0
- package/vendor/nanocodex/browser/config.d.mts +36 -0
- package/vendor/nanocodex/browser/config.mjs +439 -0
- package/vendor/nanocodex/browser/engine.mjs +13 -0
- package/vendor/nanocodex/browser/harness.mjs +55 -0
- package/vendor/nanocodex/browser/host.d.mts +83 -0
- package/vendor/nanocodex/browser/host.mjs +555 -0
- package/vendor/nanocodex/browser/hostManagedWebSocket.d.mts +14 -0
- package/vendor/nanocodex/browser/hostManagedWebSocket.mjs +110 -0
- package/vendor/nanocodex/browser/index.d.mts +92 -0
- package/vendor/nanocodex/browser/index.mjs +23 -0
- package/vendor/nanocodex/browser/indexeddb-durability-store.mjs +197 -0
- package/vendor/nanocodex/browser/workspace.d.mts +16 -0
- package/vendor/nanocodex/browser/workspace.mjs +107 -0
- package/vendor/nanocodex/cloud/Client.d.mts +83 -0
- package/vendor/nanocodex/cloud/Client.mjs +231 -0
- package/vendor/nanocodex/cloud/Decorator.d.mts +31 -0
- package/vendor/nanocodex/cloud/Decorator.mjs +37 -0
- package/vendor/nanocodex/cloud/Dialog.d.mts +113 -0
- package/vendor/nanocodex/cloud/Dialog.mjs +366 -0
- package/vendor/nanocodex/cloud/Errors.d.mts +22 -0
- package/vendor/nanocodex/cloud/Errors.mjs +39 -0
- package/vendor/nanocodex/cloud/Principal.d.mts +26 -0
- package/vendor/nanocodex/cloud/Principal.mjs +88 -0
- package/vendor/nanocodex/cloud/RemoteProvider.mjs +88 -0
- package/vendor/nanocodex/cloud/Transport.d.mts +47 -0
- package/vendor/nanocodex/cloud/Transport.mjs +454 -0
- package/vendor/nanocodex/cloud/actions/account.d.mts +9 -0
- package/vendor/nanocodex/cloud/actions/account.mjs +19 -0
- package/vendor/nanocodex/cloud/actions/agent.d.mts +18 -0
- package/vendor/nanocodex/cloud/actions/agent.mjs +428 -0
- package/vendor/nanocodex/cloud/actions/connection.d.mts +133 -0
- package/vendor/nanocodex/cloud/actions/connection.mjs +580 -0
- package/vendor/nanocodex/cloud/actions/grant.d.mts +10 -0
- package/vendor/nanocodex/cloud/actions/grant.mjs +12 -0
- package/vendor/nanocodex/cloud/actions/index.d.mts +7 -0
- package/vendor/nanocodex/cloud/actions/index.mjs +7 -0
- package/vendor/nanocodex/cloud/actions/machineUsd.d.mts +23 -0
- package/vendor/nanocodex/cloud/actions/machineUsd.mjs +37 -0
- package/vendor/nanocodex/cloud/actions/model.d.mts +12 -0
- package/vendor/nanocodex/cloud/actions/model.mjs +50 -0
- package/vendor/nanocodex/cloud/actions/mpp.d.mts +28 -0
- package/vendor/nanocodex/cloud/actions/mpp.mjs +39 -0
- package/vendor/nanocodex/cloud/index.d.mts +30 -0
- package/vendor/nanocodex/cloud/index.mjs +9 -0
- package/vendor/nanocodex/cloud/internal.mjs +533 -0
- package/vendor/nanocodex/cloud/mercator.mjs +108 -0
- package/vendor/nanocodex/cloud/server/HostPrincipal.d.mts +34 -0
- package/vendor/nanocodex/cloud/server/HostPrincipal.mjs +202 -0
- package/vendor/nanocodex/cloud/server/index.d.mts +1 -0
- package/vendor/nanocodex/cloud/server/index.mjs +1 -0
- package/vendor/nanocodex/cloud/types.d.mts +211 -0
- package/vendor/nanocodex/cloudflare/Agent.d.mts +133 -0
- package/vendor/nanocodex/cloudflare/Agent.mjs +811 -0
- package/vendor/nanocodex/cloudflare/egress-subject.mjs +19 -0
- package/vendor/nanocodex/cloudflare/egress.d.mts +41 -0
- package/vendor/nanocodex/cloudflare/egress.mjs +119 -0
- package/vendor/nanocodex/cloudflare/event-socket.mjs +316 -0
- package/vendor/nanocodex/cloudflare/index.d.mts +7 -0
- package/vendor/nanocodex/cloudflare/index.mjs +6 -0
- package/vendor/nanocodex/host/Agent.d.mts +55 -0
- package/vendor/nanocodex/host/Agent.mjs +1 -0
- package/vendor/nanocodex/host/index.d.mts +82 -0
- package/vendor/nanocodex/host/index.mjs +10 -0
- package/vendor/nanocodex/index.d.mts +81 -0
- package/vendor/nanocodex/index.mjs +12 -0
- package/vendor/nanocodex/internal.mjs +1275 -0
- package/vendor/nanocodex/managed/Agent.d.mts +380 -0
- package/vendor/nanocodex/managed/Agent.mjs +1694 -0
- package/vendor/nanocodex/managed/ManagedError.d.mts +9 -0
- package/vendor/nanocodex/managed/ManagedError.mjs +8 -0
- package/vendor/nanocodex/managed/README.md +241 -0
- package/vendor/nanocodex/managed/Voice.mjs +202 -0
- package/vendor/nanocodex/managed/index.d.mts +20 -0
- package/vendor/nanocodex/managed/index.mjs +2 -0
- package/vendor/nanocodex/managed/internal.mjs +119 -0
- package/vendor/nanocodex/node/Agent.d.mts +50 -0
- package/vendor/nanocodex/node/Agent.mjs +203 -0
- package/vendor/nanocodex/node/ChatGptSubscription.d.mts +9 -0
- package/vendor/nanocodex/node/ChatGptSubscription.mjs +17 -0
- package/vendor/nanocodex/node/Transport.d.mts +44 -0
- package/vendor/nanocodex/node/Transport.mjs +46 -0
- package/vendor/nanocodex/node/host.mjs +731 -0
- package/vendor/nanocodex/node/index.d.mts +73 -0
- package/vendor/nanocodex/node/index.mjs +17 -0
- package/vendor/nanocodex/node/workspace.d.mts +10 -0
- package/vendor/nanocodex/node/workspace.mjs +198 -0
- package/vendor/nanocodex/package.json +200 -0
- package/vendor/nanocodex/pkg-node/nanocodex.d.ts +675 -0
- package/vendor/nanocodex/pkg-node/nanocodex.js +2628 -0
- package/vendor/nanocodex/pkg-node/nanocodex_bg.wasm.d.ts +105 -0
- package/vendor/nanocodex/pkg-node/package.json +1 -0
- package/vendor/nanocodex/pkg-web/.nanocodex-bindgen-stamp +6 -0
- package/vendor/nanocodex/pkg-web/nanocodex-build.json +1 -0
- package/vendor/nanocodex/pkg-web/nanocodex.d.ts +805 -0
- package/vendor/nanocodex/pkg-web/nanocodex.js +2714 -0
- package/vendor/nanocodex/pkg-web/nanocodex_bg.js +2615 -0
- package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm +0 -0
- package/vendor/nanocodex/pkg-web/nanocodex_bg.wasm.d.ts +105 -0
- package/vendor/nanocodex/pkg-web/nanocodex_worker.js +9 -0
- package/vendor/nanocodex/pkg-web/package.json +1 -0
- package/vendor/nanocodex/runtime/chatgpt-subscription.mjs +212 -0
- package/vendor/nanocodex/runtime/cloudflare-durability-store.d.mts +26 -0
- package/vendor/nanocodex/runtime/cloudflare-durability-store.mjs +168 -0
- package/vendor/nanocodex/runtime/code-evaluator.worker.mjs +137 -0
- package/vendor/nanocodex/runtime/code-runtime.mjs +1 -0
- package/vendor/nanocodex/runtime/durability-store.d.mts +85 -0
- package/vendor/nanocodex/runtime/durability-store.mjs +631 -0
- package/vendor/nanocodex/runtime/durability.mjs +153 -0
- package/vendor/nanocodex/runtime/managed-transport.mjs +460 -0
- package/vendor/nanocodex/runtime/mcp-runtime.mjs +534 -0
- package/vendor/nanocodex/runtime/postgres-durability-store.d.mts +64 -0
- package/vendor/nanocodex/runtime/postgres-durability-store.mjs +532 -0
- package/vendor/nanocodex/runtime/quickjs-evaluator.d.mts +17 -0
- package/vendor/nanocodex/runtime/quickjs-evaluator.mjs +236 -0
- package/vendor/nanocodex/runtime/responses-transport.mjs +22 -0
- package/vendor/nanocodex/runtime/subagents.d.mts +107 -0
- package/vendor/nanocodex/runtime/subagents.mjs +54 -0
- package/vendor/nanocodex/runtime/subscription-store.d.mts +8 -0
- package/vendor/nanocodex/runtime/subscription-store.mjs +42 -0
- package/vendor/nanocodex/runtime/tempo-provider.d.mts +125 -0
- package/vendor/nanocodex/runtime/tempo-provider.mjs +264 -0
- package/vendor/nanocodex/runtime/tool-configuration.mjs +1 -0
- package/vendor/nanocodex/runtime/tool-router.mjs +1 -0
- package/vendor/nanocodex/runtime/utf8.mjs +1 -0
- package/vendor/nanocodex/runtime/worker-evaluator.mjs +162 -0
- package/vendor/nanocodex/runtime/workspace.d.mts +1 -0
- package/vendor/nanocodex/runtime/workspace.mjs +1 -0
- package/vendor/nanocodex/tools/Tools.d.mts +56 -0
- package/vendor/nanocodex/tools/Tools.mjs +136 -0
- package/vendor/nanocodex/tools/artifact.d.mts +1 -0
- package/vendor/nanocodex/tools/artifact.mjs +1 -0
- package/vendor/nanocodex/tools/attachment.mjs +1 -0
- package/vendor/nanocodex/tools/bash.d.mts +1 -0
- package/vendor/nanocodex/tools/bash.mjs +1 -0
- package/vendor/nanocodex/tools/browser/accountInfo.mjs +795 -0
- package/vendor/nanocodex/tools/browser/browserBuffer.mjs +18 -0
- package/vendor/nanocodex/tools/browser/browserCompiler.mjs +155 -0
- package/vendor/nanocodex/tools/browser/browserEgress.mjs +161 -0
- package/vendor/nanocodex/tools/browser/browserPython.mjs +133 -0
- package/vendor/nanocodex/tools/browser/browserShell.mjs +1075 -0
- package/vendor/nanocodex/tools/browser/browserSprintf.mjs +4 -0
- package/vendor/nanocodex/tools/browser/browserSsh.mjs +201 -0
- package/vendor/nanocodex/tools/browser/browserZlib.mjs +136 -0
- package/vendor/nanocodex/tools/browser/compiler.worker.mjs +160 -0
- package/vendor/nanocodex/tools/browser/devTunnelsSshBrowser.mjs +13 -0
- package/vendor/nanocodex/tools/browser/index.d.mts +209 -0
- package/vendor/nanocodex/tools/browser/index.mjs +174 -0
- package/vendor/nanocodex/tools/browser/opfsGit.mjs +248 -0
- package/vendor/nanocodex/tools/browser/python.worker.mjs +114 -0
- package/vendor/nanocodex/tools/browser/threadGit.mjs +197 -0
- package/vendor/nanocodex/tools/browser/unsupportedNodeRsa.mjs +5 -0
- package/vendor/nanocodex/tools/browser/workspace.mjs +83 -0
- package/vendor/nanocodex/tools/dataset.d.mts +1 -0
- package/vendor/nanocodex/tools/dataset.mjs +1 -0
- package/vendor/nanocodex/tools/datasetContract.mjs +1 -0
- package/vendor/nanocodex/tools/datasetEngine.mjs +1 -0
- package/vendor/nanocodex/tools/hostedCatalog.d.mts +1 -0
- package/vendor/nanocodex/tools/hostedCatalog.mjs +1 -0
- package/vendor/nanocodex/tools/index.d.mts +49 -0
- package/vendor/nanocodex/tools/index.mjs +19 -0
- package/vendor/nanocodex/tools/namedTool.mjs +1 -0
- package/vendor/nanocodex/tools/repository-workspace.d.mts +1 -0
- package/vendor/nanocodex/tools/repository-workspace.mjs +1 -0
- package/vendor/nanocodex/tools/ssh.d.mts +1 -0
- package/vendor/nanocodex/tools/ssh.mjs +1 -0
- package/vendor/nanocodex/tools/standard.mjs +1 -0
- package/vendor/nanocodex/tools/standardDescriptions.mjs +1 -0
- package/vendor/nanocodex/types.d.mts +663 -0
- package/vendor/nanocodex/wasm.d.mts +4 -0
- package/vendor/nanocodex/worker/ChatGptSubscription.d.mts +9 -0
- package/vendor/nanocodex/worker/ChatGptSubscription.mjs +29 -0
- package/vendor/nanocodex/worker/index.d.mts +18 -0
- package/vendor/nanocodex/worker/index.mjs +5 -0
- package/vendor/nanocodex-tools/README.md +65 -0
- package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.d.ts +7 -0
- package/vendor/nanocodex-tools/dist/hosted/app-tool-catalog.js +14 -0
- package/vendor/nanocodex-tools/dist/hosted/broker-core.d.ts +172 -0
- package/vendor/nanocodex-tools/dist/hosted/broker-core.js +1278 -0
- package/vendor/nanocodex-tools/dist/hosted/index.d.ts +3 -0
- package/vendor/nanocodex-tools/dist/hosted/index.js +3 -0
- package/vendor/nanocodex-tools/dist/hosted/protocol.d.ts +120 -0
- package/vendor/nanocodex-tools/dist/hosted/protocol.js +415 -0
- package/vendor/nanocodex-tools/dist/index.d.ts +19 -0
- package/vendor/nanocodex-tools/dist/index.js +14 -0
- package/vendor/nanocodex-tools/dist/memory.d.ts +204 -0
- package/vendor/nanocodex-tools/dist/memory.js +411 -0
- package/vendor/nanocodex-tools/dist/namespace.d.ts +87 -0
- package/vendor/nanocodex-tools/dist/namespace.js +338 -0
- package/vendor/nanocodex-tools/dist/repository-archive.d.ts +4 -0
- package/vendor/nanocodex-tools/dist/repository-archive.js +104 -0
- package/vendor/nanocodex-tools/dist/runtime.d.ts +30 -0
- package/vendor/nanocodex-tools/dist/runtime.js +37 -0
- package/vendor/nanocodex-tools/dist/session.d.ts +167 -0
- package/vendor/nanocodex-tools/dist/session.js +286 -0
- package/vendor/nanocodex-tools/dist/shell.d.ts +51 -0
- package/vendor/nanocodex-tools/dist/shell.js +553 -0
- package/vendor/nanocodex-tools/dist/ssh.d.ts +29 -0
- package/vendor/nanocodex-tools/dist/ssh.js +66 -0
- package/vendor/nanocodex-tools/dist/workspace.d.ts +45 -0
- package/vendor/nanocodex-tools/dist/workspace.js +237 -0
- package/vendor/nanocodex-tools/package.json +122 -0
- package/vendor/nanocodex-tools/runtime/code-runtime.mjs +817 -0
- package/vendor/nanocodex-tools/runtime/tool-configuration.mjs +50 -0
- package/vendor/nanocodex-tools/runtime/tool-router.mjs +730 -0
- package/vendor/nanocodex-tools/runtime/utf8.d.mts +2 -0
- package/vendor/nanocodex-tools/runtime/utf8.mjs +25 -0
- package/vendor/nanocodex-tools/tools/artifact.d.mts +69 -0
- package/vendor/nanocodex-tools/tools/artifact.mjs +270 -0
- package/vendor/nanocodex-tools/tools/attachment.mjs +603 -0
- package/vendor/nanocodex-tools/tools/bash.d.mts +90 -0
- package/vendor/nanocodex-tools/tools/bash.mjs +742 -0
- package/vendor/nanocodex-tools/tools/dataset.d.mts +9 -0
- package/vendor/nanocodex-tools/tools/dataset.mjs +47 -0
- package/vendor/nanocodex-tools/tools/datasetContract.mjs +54 -0
- package/vendor/nanocodex-tools/tools/datasetEngine.mjs +1167 -0
- package/vendor/nanocodex-tools/tools/execution-contract.d.mts +5 -0
- package/vendor/nanocodex-tools/tools/execution-contract.mjs +63 -0
- package/vendor/nanocodex-tools/tools/hostedCatalog.d.mts +20 -0
- package/vendor/nanocodex-tools/tools/hostedCatalog.mjs +67 -0
- package/vendor/nanocodex-tools/tools/hostedMachine.d.mts +10 -0
- package/vendor/nanocodex-tools/tools/hostedMachine.mjs +56 -0
- package/vendor/nanocodex-tools/tools/just-bash-browser.d.mts +1 -0
- package/vendor/nanocodex-tools/tools/just-bash-browser.mjs +1 -0
- package/vendor/nanocodex-tools/tools/namedTool.d.mts +3 -0
- package/vendor/nanocodex-tools/tools/namedTool.mjs +3 -0
- package/vendor/nanocodex-tools/tools/nodeProcess.d.mts +10 -0
- package/vendor/nanocodex-tools/tools/nodeProcess.mjs +314 -0
- package/vendor/nanocodex-tools/tools/processOutput.mjs +56 -0
- package/vendor/nanocodex-tools/tools/repository-workspace.d.mts +55 -0
- package/vendor/nanocodex-tools/tools/repository-workspace.mjs +397 -0
- package/vendor/nanocodex-tools/tools/ssh.d.mts +56 -0
- package/vendor/nanocodex-tools/tools/ssh.mjs +503 -0
- package/vendor/nanocodex-tools/tools/standard.mjs +458 -0
- package/vendor/nanocodex-tools/tools/standardDescriptions.mjs +3 -0
- package/vendor/nanocodex-tools/tools/types.d.mts +83 -0
- package/vendor/nanocodex-tools/tools/workspace.d.mts +25 -0
- package/vendor/nanocodex-tools/tools/workspace.mjs +282 -0
- package/vendor/nanocodex-tools/tools/x.d.mts +30 -0
- 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
|
+
}
|