somora 2026.930.1
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 +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
// The OpenAI adapter: somora's contract on one side, OpenAI's realtime
|
|
2
|
+
// event vocabulary on the other.
|
|
3
|
+
//
|
|
4
|
+
// Transport is the server-side WebSocket, deliberately:
|
|
5
|
+
//
|
|
6
|
+
// - Tool calls arrive HERE, so somora executes them on exactly one
|
|
7
|
+
// path. With WebRTC the control channel sits in the browser, and
|
|
8
|
+
// OpenAI's sideband for attaching a server to a browser call
|
|
9
|
+
// (`wss://…/v1/realtime?call_id=…`) answered `call_id_not_found`
|
|
10
|
+
// for ephemeral-key calls through 2026-09-10, working only when the
|
|
11
|
+
// SDP offer was posted with the real API key.
|
|
12
|
+
// - OpenClaw does the same for its tool-backed calls (read from their
|
|
13
|
+
// bundle 2026-09-11): a server WebSocket to
|
|
14
|
+
// `wss://api.openai.com/v1/realtime?model=…`, tool answers as
|
|
15
|
+
// `conversation.item.create` + `response.create`.
|
|
16
|
+
//
|
|
17
|
+
// The cost is one extra hop for the audio. On a tailnet, against a
|
|
18
|
+
// model that thinks for hundreds of milliseconds, that is the cheaper
|
|
19
|
+
// trade than an ambiguous tool path.
|
|
20
|
+
|
|
21
|
+
import { readFileSync } from 'node:fs';
|
|
22
|
+
import WebSocket from 'ws';
|
|
23
|
+
import { logger } from '../../server/logger.ts';
|
|
24
|
+
import type { SpeakOptions } from './types.ts';
|
|
25
|
+
import type {
|
|
26
|
+
RealtimeAudioChunk,
|
|
27
|
+
RealtimeCapabilities,
|
|
28
|
+
RealtimeClientHandshake,
|
|
29
|
+
RealtimeEvent,
|
|
30
|
+
RealtimeProvider,
|
|
31
|
+
RealtimeSession,
|
|
32
|
+
RealtimeSessionRequest,
|
|
33
|
+
} from './types.ts';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Where OpenAI answers. Overridable per instance, because the adapter
|
|
37
|
+
* is the protocol and not the vendor: a local service that speaks the
|
|
38
|
+
* same session/event language plugs in here instead of getting a second
|
|
39
|
+
* adapter that would drift from this one (Rene, 2026-09-12).
|
|
40
|
+
*/
|
|
41
|
+
const REALTIME_URL = 'wss://api.openai.com/v1/realtime';
|
|
42
|
+
/** OpenAI's realtime audio is PCM16; 24 kHz is what the session
|
|
43
|
+
* defaults to and what the browser side has to match. */
|
|
44
|
+
const AUDIO_RATE_HZ = 24_000;
|
|
45
|
+
|
|
46
|
+
export interface OpenAiRealtimeOptions {
|
|
47
|
+
/** Read from a file, never from config.yaml — a realtime key buys
|
|
48
|
+
* billed minutes and config is read by more eyes than a 600 file. */
|
|
49
|
+
apiKeyFile?: string;
|
|
50
|
+
apiKey?: string;
|
|
51
|
+
/** Endpoint override. Defaults to OpenAI's. */
|
|
52
|
+
url?: string;
|
|
53
|
+
/** Injectable for tests: anything that behaves like a ws socket. */
|
|
54
|
+
connect?: (url: string, headers: Record<string, string>) => WebSocketLike;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The slice of `ws` this adapter uses. */
|
|
58
|
+
export interface WebSocketLike {
|
|
59
|
+
on(event: 'open' | 'close', cb: () => void): void;
|
|
60
|
+
on(event: 'message', cb: (data: unknown) => void): void;
|
|
61
|
+
on(event: 'error', cb: (err: Error) => void): void;
|
|
62
|
+
send(data: string): void;
|
|
63
|
+
close(): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The credential, or nothing.
|
|
68
|
+
*
|
|
69
|
+
* A service of your own on localhost has no key to give, and demanding
|
|
70
|
+
* one would make the endpoint override useless. So: a key is required
|
|
71
|
+
* for OpenAI's own endpoint and optional everywhere else. Without one,
|
|
72
|
+
* the socket simply carries no Authorization header.
|
|
73
|
+
*/
|
|
74
|
+
function loadKey(opts: OpenAiRealtimeOptions): string | undefined {
|
|
75
|
+
if (opts.apiKey) return opts.apiKey;
|
|
76
|
+
if (opts.apiKeyFile) {
|
|
77
|
+
const key = readFileSync(opts.apiKeyFile, 'utf8').trim();
|
|
78
|
+
if (key.length === 0) throw new Error(`realtime key file ${opts.apiKeyFile} is empty`);
|
|
79
|
+
return key;
|
|
80
|
+
}
|
|
81
|
+
const endpoint = opts.url ?? REALTIME_URL;
|
|
82
|
+
if (endpoint.startsWith(REALTIME_URL)) {
|
|
83
|
+
throw new Error('no realtime key: set realtimeVoice.apiKeyFile');
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
class OpenAiRealtimeSession implements RealtimeSession {
|
|
89
|
+
readonly id: string;
|
|
90
|
+
private queue: RealtimeEvent[] = [];
|
|
91
|
+
private waiter: (() => void) | undefined;
|
|
92
|
+
private done = false;
|
|
93
|
+
/** The model is mid-answer. Used to tell a barge-in apart from the
|
|
94
|
+
* normal start of a user turn. */
|
|
95
|
+
private speaking = false;
|
|
96
|
+
/**
|
|
97
|
+
* A response is open on the provider's side.
|
|
98
|
+
*
|
|
99
|
+
* Asking for a second one while the first runs is refused:
|
|
100
|
+
* "Conversation already has an active response in progress"
|
|
101
|
+
* (seen live 2026-09-11, right after a tool answer landed while the
|
|
102
|
+
* voice self was still saying "moment, ich schau nach"). The answer
|
|
103
|
+
* item is accepted either way — only the request to SPEAK has to
|
|
104
|
+
* wait, so it is deferred to the next `response.done`.
|
|
105
|
+
*/
|
|
106
|
+
private responseActive = false;
|
|
107
|
+
private speakWhenFree = false;
|
|
108
|
+
/** Instructions that MUST be spoken (SpeakOptions.deliver), waiting
|
|
109
|
+
* for the model to fall silent. Oldest first. */
|
|
110
|
+
private pendingDeliveries: string[] = [];
|
|
111
|
+
/** What the last response.create asked for, so a "response in
|
|
112
|
+
* progress" refusal can put a delivery back in line. */
|
|
113
|
+
private lastRequest: { instructions?: string; deliver: boolean } | null = null;
|
|
114
|
+
|
|
115
|
+
/** Ask the model to speak, or remember to ask once it is free.
|
|
116
|
+
*
|
|
117
|
+
* The flag is set the moment WE ask, not when the server confirms:
|
|
118
|
+
* `response.created` comes back over the network, and between our
|
|
119
|
+
* request and that echo a second request slips through. That gap is
|
|
120
|
+
* how the error came back on 2026-09-11 even with a guard in place —
|
|
121
|
+
* the filler for a lookup and the answer to it were sent
|
|
122
|
+
* milliseconds apart. */
|
|
123
|
+
private requestResponse(payload?: Record<string, unknown>, deliver = false): void {
|
|
124
|
+
if (this.responseActive) {
|
|
125
|
+
this.speakWhenFree = true;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.responseActive = true;
|
|
129
|
+
const instructions = typeof payload?.instructions === 'string' ? payload.instructions : undefined;
|
|
130
|
+
this.lastRequest = { ...(instructions !== undefined ? { instructions } : {}), deliver };
|
|
131
|
+
this.send({ type: 'response.create', ...(payload ? { response: payload } : {}) });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The model fell silent: a kept delivery goes first, then whatever
|
|
135
|
+
* was waiting to speak without words of its own. */
|
|
136
|
+
private speakNext(): void {
|
|
137
|
+
const next = this.pendingDeliveries.shift();
|
|
138
|
+
if (next !== undefined) {
|
|
139
|
+
this.speakWhenFree = false;
|
|
140
|
+
this.requestResponse({ instructions: next }, true);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (this.speakWhenFree) {
|
|
144
|
+
this.speakWhenFree = false;
|
|
145
|
+
this.requestResponse();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
constructor(
|
|
150
|
+
private readonly ws: WebSocketLike,
|
|
151
|
+
private readonly req: RealtimeSessionRequest,
|
|
152
|
+
id: string,
|
|
153
|
+
) {
|
|
154
|
+
this.id = id;
|
|
155
|
+
this.ws.on('message', (data: unknown) => this.onMessage(String(data)));
|
|
156
|
+
this.ws.on('error', (err: Error) => this.push({ kind: 'error', ts: Date.now(), message: err.message, fatal: true }));
|
|
157
|
+
this.ws.on('close', () => {
|
|
158
|
+
this.push({ kind: 'closed', ts: Date.now(), reason: 'provider closed the connection' });
|
|
159
|
+
this.finish();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Configure the session the moment it opens: instructions, voice,
|
|
164
|
+
* the one tool, and transcription of the USER's audio — without
|
|
165
|
+
* that last one somora never learns what was actually said and the
|
|
166
|
+
* session history stays empty. */
|
|
167
|
+
configure(): void {
|
|
168
|
+
this.send({
|
|
169
|
+
type: 'session.update',
|
|
170
|
+
session: {
|
|
171
|
+
type: 'realtime',
|
|
172
|
+
model: this.req.model,
|
|
173
|
+
instructions: this.req.instructions,
|
|
174
|
+
output_modalities: ['audio'],
|
|
175
|
+
audio: {
|
|
176
|
+
input: {
|
|
177
|
+
format: { type: 'audio/pcm', rate: AUDIO_RATE_HZ },
|
|
178
|
+
transcription: { model: 'whisper-1', language: this.req.language },
|
|
179
|
+
turn_detection: {
|
|
180
|
+
type: 'server_vad',
|
|
181
|
+
// Tuned to be interruptible: the defaults wait for a
|
|
182
|
+
// confident, sustained speaker, which makes talking over
|
|
183
|
+
// the model hard (Rene, 2026-09-12).
|
|
184
|
+
threshold: this.req.turnDetection?.threshold ?? 0.4,
|
|
185
|
+
prefix_padding_ms: this.req.turnDetection?.prefixPaddingMs ?? 200,
|
|
186
|
+
silence_duration_ms: this.req.turnDetection?.silenceDurationMs ?? 420,
|
|
187
|
+
interrupt_response: true,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
output: { voice: this.req.voice },
|
|
191
|
+
},
|
|
192
|
+
tools: this.req.tools.map((t) => ({
|
|
193
|
+
type: 'function',
|
|
194
|
+
name: t.name,
|
|
195
|
+
description: t.description,
|
|
196
|
+
parameters: t.parameters,
|
|
197
|
+
})),
|
|
198
|
+
tool_choice: 'auto',
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private send(obj: Record<string, unknown>): void {
|
|
204
|
+
this.ws.send(JSON.stringify(obj));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private push(ev: RealtimeEvent): void {
|
|
208
|
+
if (this.done) return;
|
|
209
|
+
this.queue.push(ev);
|
|
210
|
+
this.waiter?.();
|
|
211
|
+
this.waiter = undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private finish(): void {
|
|
215
|
+
this.done = true;
|
|
216
|
+
this.waiter?.();
|
|
217
|
+
this.waiter = undefined;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private onMessage(raw: string): void {
|
|
221
|
+
let msg: Record<string, unknown>;
|
|
222
|
+
try {
|
|
223
|
+
msg = JSON.parse(raw) as Record<string, unknown>;
|
|
224
|
+
} catch {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const type = typeof msg.type === 'string' ? msg.type : '';
|
|
228
|
+
const ts = Date.now();
|
|
229
|
+
switch (type) {
|
|
230
|
+
case 'session.created':
|
|
231
|
+
case 'session.updated':
|
|
232
|
+
this.push({ kind: 'ready', ts });
|
|
233
|
+
break;
|
|
234
|
+
case 'input_audio_buffer.speech_started':
|
|
235
|
+
this.push({ kind: 'user_speech', ts, phase: 'start' });
|
|
236
|
+
// Talking over the model IS the interruption signal; there is
|
|
237
|
+
// no separate event for it on this transport.
|
|
238
|
+
if (this.speaking) {
|
|
239
|
+
this.speaking = false;
|
|
240
|
+
// Stop the answer at the source as well: dropping the
|
|
241
|
+
// playback alone leaves the model talking into a void and
|
|
242
|
+
// counting it as said.
|
|
243
|
+
if (this.responseActive) this.send({ type: 'response.cancel' });
|
|
244
|
+
this.push({ kind: 'interrupted', ts });
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
case 'input_audio_buffer.speech_stopped':
|
|
248
|
+
this.push({ kind: 'user_speech', ts, phase: 'end' });
|
|
249
|
+
break;
|
|
250
|
+
case 'conversation.item.input_audio_transcription.completed': {
|
|
251
|
+
const text = typeof msg.transcript === 'string' ? msg.transcript : '';
|
|
252
|
+
if (text) this.push({ kind: 'user_transcript', ts, text, final: true });
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
case 'response.output_audio_transcript.delta': {
|
|
256
|
+
const text = typeof msg.delta === 'string' ? msg.delta : '';
|
|
257
|
+
if (text) this.push({ kind: 'model_transcript', ts, text, final: false });
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case 'response.output_audio_transcript.done': {
|
|
261
|
+
const text = typeof msg.transcript === 'string' ? msg.transcript : '';
|
|
262
|
+
if (text) this.push({ kind: 'model_transcript', ts, text, final: true });
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case 'response.output_audio.delta': {
|
|
266
|
+
const b64 = typeof msg.delta === 'string' ? msg.delta : '';
|
|
267
|
+
if (b64) {
|
|
268
|
+
if (!this.speaking) {
|
|
269
|
+
this.speaking = true;
|
|
270
|
+
this.push({ kind: 'model_speech', ts, phase: 'start' });
|
|
271
|
+
}
|
|
272
|
+
this.push({ kind: 'audio', ts, base64: b64, rateHz: AUDIO_RATE_HZ });
|
|
273
|
+
}
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case 'response.function_call_arguments.done': {
|
|
277
|
+
const callId = typeof msg.call_id === 'string' ? msg.call_id : '';
|
|
278
|
+
const name = typeof msg.name === 'string' ? msg.name : '';
|
|
279
|
+
const args = typeof msg.arguments === 'string' ? msg.arguments : '';
|
|
280
|
+
if (callId && name) this.push({ kind: 'tool_call', ts, callId, name, args });
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case 'response.created':
|
|
284
|
+
this.responseActive = true;
|
|
285
|
+
break;
|
|
286
|
+
case 'response.done': {
|
|
287
|
+
this.responseActive = false;
|
|
288
|
+
this.speakNext();
|
|
289
|
+
if (this.speaking) {
|
|
290
|
+
this.speaking = false;
|
|
291
|
+
this.push({ kind: 'model_speech', ts, phase: 'end' });
|
|
292
|
+
}
|
|
293
|
+
const response = msg.response as { usage?: Record<string, unknown> } | undefined;
|
|
294
|
+
const usage = response?.usage;
|
|
295
|
+
if (usage) {
|
|
296
|
+
this.push({
|
|
297
|
+
kind: 'usage',
|
|
298
|
+
ts,
|
|
299
|
+
...(typeof usage.input_tokens === 'number' ? { inputTokens: usage.input_tokens } : {}),
|
|
300
|
+
...(typeof usage.output_tokens === 'number' ? { outputTokens: usage.output_tokens } : {}),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
case 'error': {
|
|
306
|
+
const err = msg.error as { message?: unknown; type?: unknown } | undefined;
|
|
307
|
+
const message = typeof err?.message === 'string' ? err.message : JSON.stringify(msg).slice(0, 300);
|
|
308
|
+
// The server knows better than our bookkeeping: if it says a
|
|
309
|
+
// response is running, one is. Remember to ask again when that
|
|
310
|
+
// response reports done, so the answer is not lost.
|
|
311
|
+
// Cancelling a response that just finished is the normal shape
|
|
312
|
+
// of an interruption, not a fault: the user talks over the last
|
|
313
|
+
// syllable and the cancel arrives a moment late. It was shown
|
|
314
|
+
// in red to the human (Rene, 2026-09-12) — it belongs in the
|
|
315
|
+
// log, nowhere else.
|
|
316
|
+
if (/no active response/i.test(message)) {
|
|
317
|
+
this.responseActive = false;
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
if (/active response in progress/i.test(message)) {
|
|
321
|
+
this.responseActive = true;
|
|
322
|
+
// A refused delivery is not lost: back in line, first.
|
|
323
|
+
if (this.lastRequest?.deliver && this.lastRequest.instructions !== undefined) {
|
|
324
|
+
this.pendingDeliveries.unshift(this.lastRequest.instructions);
|
|
325
|
+
} else {
|
|
326
|
+
this.speakWhenFree = true;
|
|
327
|
+
}
|
|
328
|
+
this.lastRequest = null;
|
|
329
|
+
// Handled here: the request is asked again at the next silence.
|
|
330
|
+
// Shown to the person it read as a fault (red in the web call
|
|
331
|
+
// window, Rene 2026-09-13) — it belongs in the log.
|
|
332
|
+
logger.info({ msg: 'voice.response_in_progress', message, requeued: this.pendingDeliveries.length, speakWhenFree: this.speakWhenFree });
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
// A session-level error kills the call; an item-level one (a
|
|
336
|
+
// rejected tool argument, say) must not — the conversation is
|
|
337
|
+
// still alive and the model can be told.
|
|
338
|
+
const fatal = typeof err?.type === 'string' && /session|invalid_request_error/i.test(err.type) === false;
|
|
339
|
+
this.push({ kind: 'error', ts, message, fatal });
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
default:
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async *events(): AsyncGenerator<RealtimeEvent> {
|
|
348
|
+
while (!this.done || this.queue.length > 0) {
|
|
349
|
+
const next = this.queue.shift();
|
|
350
|
+
if (next) {
|
|
351
|
+
yield next;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
await new Promise<void>((resolve) => {
|
|
355
|
+
this.waiter = resolve;
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Microphone audio, as it arrives.
|
|
362
|
+
*
|
|
363
|
+
* The client must keep sending while nobody talks. Server-side turn
|
|
364
|
+
* detection closes a turn on SILENCE, not on the absence of packets:
|
|
365
|
+
* measured live 2026-09-11, a client that stopped sending at the end
|
|
366
|
+
* of the sentence got `speech_started` and then nothing at all — no
|
|
367
|
+
* transcript, no answer, a call that simply hung. A real microphone
|
|
368
|
+
* streams silence; anything feeding this from a file has to do the
|
|
369
|
+
* same.
|
|
370
|
+
*/
|
|
371
|
+
async sendAudio(chunk: RealtimeAudioChunk): Promise<void> {
|
|
372
|
+
this.send({ type: 'input_audio_buffer.append', audio: chunk.base64 });
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async sendToolResult(callId: string, result: string): Promise<void> {
|
|
376
|
+
// Two events, in this order: the answer becomes a conversation
|
|
377
|
+
// item, then the model is asked to speak again. Without the second
|
|
378
|
+
// one the call goes silent after a tool call — the model is waiting
|
|
379
|
+
// for permission it never gets. But asking while it is still
|
|
380
|
+
// talking is an error, so that half waits for the current response.
|
|
381
|
+
this.send({
|
|
382
|
+
type: 'conversation.item.create',
|
|
383
|
+
item: { type: 'function_call_output', call_id: callId, output: result },
|
|
384
|
+
});
|
|
385
|
+
this.requestResponse();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async updateInstructions(instructions: string): Promise<void> {
|
|
389
|
+
this.send({ type: 'session.update', session: { instructions } });
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** One spoken line under a one-off instruction, without touching the
|
|
393
|
+
* session's own. Skipped while the model is already talking — the
|
|
394
|
+
* filler exists to fill a silence, not to talk over an answer. */
|
|
395
|
+
async speak(instructions: string, opts?: SpeakOptions): Promise<void> {
|
|
396
|
+
// A filler only makes sense in a silence: if the model is talking,
|
|
397
|
+
// dropping it is right. A delivery (the answer to a handed-over
|
|
398
|
+
// question, a follow-up) must be said — kept until the model falls
|
|
399
|
+
// silent. Until 2026-09-13 both were dropped, and the announcement
|
|
400
|
+
// of a late answer went silent whenever the voice self happened to
|
|
401
|
+
// be mid-sentence (its own "still working" after a status check).
|
|
402
|
+
if (this.responseActive) {
|
|
403
|
+
if (opts?.deliver) this.pendingDeliveries.push(instructions);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
this.requestResponse({ instructions }, opts?.deliver === true);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async interrupt(): Promise<void> {
|
|
410
|
+
this.send({ type: 'response.cancel' });
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async close(reason: string): Promise<void> {
|
|
414
|
+
this.push({ kind: 'closed', ts: Date.now(), reason });
|
|
415
|
+
this.ws.close();
|
|
416
|
+
this.finish();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export class OpenAiRealtimeProvider implements RealtimeProvider {
|
|
421
|
+
readonly name = 'openai';
|
|
422
|
+
|
|
423
|
+
constructor(private readonly opts: OpenAiRealtimeOptions) {}
|
|
424
|
+
|
|
425
|
+
capabilities(): RealtimeCapabilities {
|
|
426
|
+
return {
|
|
427
|
+
transport: 'websocket',
|
|
428
|
+
serverVad: true,
|
|
429
|
+
bargeIn: true,
|
|
430
|
+
truncation: true,
|
|
431
|
+
tools: true,
|
|
432
|
+
liveInstructionUpdate: true,
|
|
433
|
+
// Measured 2026-09-11: every session field may be updated at any
|
|
434
|
+
// time except model and voice, voice only until the first audio.
|
|
435
|
+
voiceFixedAfterFirstAudio: true,
|
|
436
|
+
usage: true,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async open(req: RealtimeSessionRequest): Promise<RealtimeSession> {
|
|
441
|
+
const key = loadKey(this.opts);
|
|
442
|
+
const base = this.opts.url ?? REALTIME_URL;
|
|
443
|
+
// The model travels as a query parameter, the way OpenAI wants it.
|
|
444
|
+
// A service that only serves one model can ignore the parameter.
|
|
445
|
+
const url = `${base}${base.includes('?') ? '&' : '?'}model=${encodeURIComponent(req.model)}`;
|
|
446
|
+
const headers: Record<string, string> = key ? { Authorization: `Bearer ${key}` } : {};
|
|
447
|
+
const ws: WebSocketLike = this.opts.connect
|
|
448
|
+
? this.opts.connect(url, headers)
|
|
449
|
+
: (new WebSocket(url, { headers }) as unknown as WebSocketLike);
|
|
450
|
+
const session = new OpenAiRealtimeSession(ws, req, `oai-${Date.now()}`);
|
|
451
|
+
await new Promise<void>((resolve, reject) => {
|
|
452
|
+
const timer = setTimeout(() => reject(new Error('realtime connect timed out after 20s')), 20_000);
|
|
453
|
+
ws.on('open', () => {
|
|
454
|
+
clearTimeout(timer);
|
|
455
|
+
session.configure();
|
|
456
|
+
resolve();
|
|
457
|
+
});
|
|
458
|
+
ws.on('error', (err: Error) => {
|
|
459
|
+
clearTimeout(timer);
|
|
460
|
+
reject(err);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
return session;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
async handshake(): Promise<RealtimeClientHandshake> {
|
|
467
|
+
// On this transport the browser never talks to OpenAI: it streams
|
|
468
|
+
// its microphone to somora and plays back what somora forwards.
|
|
469
|
+
return { kind: 'sdp_broker', value: 'somora', url: '/voice/attach' };
|
|
470
|
+
}
|
|
471
|
+
}
|