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,227 @@
|
|
|
1
|
+
// The voice self: who the agent is when it speaks.
|
|
2
|
+
//
|
|
3
|
+
// Derived from the agent's own persona rather than written a second
|
|
4
|
+
// time, because two personas drift and the one nobody reads is the one
|
|
5
|
+
// that lies. What lives in `agent.yaml voice:` is only what is specific
|
|
6
|
+
// to SPEAKING: voice, language, tone, brevity.
|
|
7
|
+
//
|
|
8
|
+
// Length is a cost budget, not thrift. The instructions are sent once
|
|
9
|
+
// when the session opens, but they sit in the realtime model's context
|
|
10
|
+
// and are read again before every answer, so every line is paid for per
|
|
11
|
+
// turn. The ceiling is ~2300 characters and the builder reports what it
|
|
12
|
+
// produced. It was ~1500 until 2026-09-12, when three rules were added:
|
|
13
|
+
// agents slipped into English mid-call, every call opened with a recital
|
|
14
|
+
// of the persona, and what the caller merely stated was never passed on
|
|
15
|
+
// and therefore never remembered. 2026-09-21 added who is on the line
|
|
16
|
+
// (an excerpt of the agent's USER.md, up to 280 chars) and the call's
|
|
17
|
+
// start time: asked "who am I?", the voice self had nothing to go on.
|
|
18
|
+
|
|
19
|
+
import type { Persona } from '../../persona/loader.ts';
|
|
20
|
+
|
|
21
|
+
export interface VoiceInstructionsInput {
|
|
22
|
+
persona: Persona;
|
|
23
|
+
/**
|
|
24
|
+
* Hand-written character for the voice self, from
|
|
25
|
+
* `~/.somora/agents/<name>/VOICE.md`. Replaces the part derived from
|
|
26
|
+
* the persona — never the rules below it.
|
|
27
|
+
*
|
|
28
|
+
* The split is deliberate. OpenClaw writes the whole thing by hand
|
|
29
|
+
* (their realtime call takes a configured `instructions` string, read
|
|
30
|
+
* from their bundle 2026-09-11), which is full control and a file per
|
|
31
|
+
* agent to maintain. somora derives it, which is nothing to maintain
|
|
32
|
+
* for eight agents and no second place to drift. With this override
|
|
33
|
+
* you get both: you own the character where you care, somora owns the
|
|
34
|
+
* contract that keeps it honest — delegate, do not invent, do not
|
|
35
|
+
* refuse work on your own authority.
|
|
36
|
+
*/
|
|
37
|
+
override?: string;
|
|
38
|
+
/** Effective policy: the agent's, else the instance default. */
|
|
39
|
+
consultPolicy: 'auto' | 'substantive' | 'always';
|
|
40
|
+
language: string;
|
|
41
|
+
/** Name of the tool the voice self uses to reach the real agent. */
|
|
42
|
+
consultToolName: string;
|
|
43
|
+
/** Call start; defaults to now. Test seam. */
|
|
44
|
+
now?: Date;
|
|
45
|
+
/** Which session this call is bound to — the voice self may say
|
|
46
|
+
* where it is, and it must never imply it can switch. */
|
|
47
|
+
sessionSlug: string;
|
|
48
|
+
/** Agents this call may be handed over to. Empty = no switching, and
|
|
49
|
+
* the instruction says so instead of teasing an ability. */
|
|
50
|
+
switchTo?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What the language is CALLED, for a prompt that is otherwise English.
|
|
55
|
+
*
|
|
56
|
+
* "You speak de." is a two-letter code buried in an English paragraph,
|
|
57
|
+
* and the model followed the paragraph: agents slipped into English
|
|
58
|
+
* mid-call (Rene, 2026-09-12). The wiki already learned this — it keeps
|
|
59
|
+
* a `languageName` next to its code for exactly the same reason.
|
|
60
|
+
* Unknown codes fall through as themselves, which is no worse than
|
|
61
|
+
* before.
|
|
62
|
+
*/
|
|
63
|
+
const LANGUAGE_NAMES: Record<string, string> = {
|
|
64
|
+
de: 'German',
|
|
65
|
+
en: 'English',
|
|
66
|
+
fr: 'French',
|
|
67
|
+
es: 'Spanish',
|
|
68
|
+
it: 'Italian',
|
|
69
|
+
nl: 'Dutch',
|
|
70
|
+
pt: 'Portuguese',
|
|
71
|
+
pl: 'Polish',
|
|
72
|
+
tr: 'Turkish',
|
|
73
|
+
ru: 'Russian',
|
|
74
|
+
cs: 'Czech',
|
|
75
|
+
da: 'Danish',
|
|
76
|
+
sv: 'Swedish',
|
|
77
|
+
no: 'Norwegian',
|
|
78
|
+
fi: 'Finnish',
|
|
79
|
+
ja: 'Japanese',
|
|
80
|
+
ko: 'Korean',
|
|
81
|
+
zh: 'Chinese',
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function languageName(code: string): string {
|
|
85
|
+
const key = code.trim().toLowerCase().split(/[-_]/)[0] ?? '';
|
|
86
|
+
return LANGUAGE_NAMES[key] ?? code;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** First paragraph of the persona's own description, trimmed. The
|
|
90
|
+
* system prompt starts with the self-pointer and the persona files;
|
|
91
|
+
* the first substantial lines carry the character. */
|
|
92
|
+
function personaEssence(persona: Persona, limit = 420): string {
|
|
93
|
+
const text = (persona.description || '').trim();
|
|
94
|
+
if (text.length > 0) return text.slice(0, limit);
|
|
95
|
+
const firstPara = persona.systemPrompt
|
|
96
|
+
.split(/\n\s*\n/)
|
|
97
|
+
.map((p) => p.trim())
|
|
98
|
+
.find((p) => p.length > 40 && !p.startsWith('#'));
|
|
99
|
+
return (firstPara ?? '').slice(0, limit);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Who the agent is talking to, from the agent's own USER.md: the
|
|
104
|
+
* opening of the file, up to the first `##` section. That is where
|
|
105
|
+
* every USER.md puts the name and how to address the person; the rest
|
|
106
|
+
* (work, family, projects) stays with the real agent and is a lookup.
|
|
107
|
+
* Nothing about any particular user lives in code or config — no
|
|
108
|
+
* USER.md, no line.
|
|
109
|
+
*/
|
|
110
|
+
export function userEssence(persona: Persona, limit = 280): string {
|
|
111
|
+
const raw = (persona.aboutUser ?? '').trim();
|
|
112
|
+
if (!raw) return '';
|
|
113
|
+
const opening = raw.split(/^##\s/m)[0] ?? '';
|
|
114
|
+
// Bullets and wrapped lines become one line for the ear. A wrapped
|
|
115
|
+
// line (indented, not a bullet of its own — or starting lower-case)
|
|
116
|
+
// continues the previous one; anything else is closed with "; "
|
|
117
|
+
// unless the previous line already ended a sentence.
|
|
118
|
+
let text = '';
|
|
119
|
+
for (const rawLine of opening.split('\n')) {
|
|
120
|
+
if (/^#\s/.test(rawLine)) continue; // the file's own title
|
|
121
|
+
const isBullet = /^\s*[-*]\s+/.test(rawLine);
|
|
122
|
+
const line = rawLine.replace(/^\s*[-*]\s+/, '').replace(/\*\*/g, '').trim();
|
|
123
|
+
if (!line) continue;
|
|
124
|
+
if (!text) {
|
|
125
|
+
text = line;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const continues = !isBullet && (/^\s+\S/.test(rawLine) || /^[a-zäöü(]/.test(line));
|
|
129
|
+
const closed = /[.;:!?,—–-]$/.test(text);
|
|
130
|
+
text += `${closed || continues ? ' ' : '; '}${line}`;
|
|
131
|
+
}
|
|
132
|
+
text = text.replace(/\s+/g, ' ');
|
|
133
|
+
if (text.length <= limit) return text;
|
|
134
|
+
// Cut at a sentence or clause end, not mid-word.
|
|
135
|
+
const cut = text.slice(0, limit);
|
|
136
|
+
const stop = Math.max(cut.lastIndexOf('. '), cut.lastIndexOf('; '), cut.lastIndexOf(', '));
|
|
137
|
+
return (stop > limit * 0.5 ? cut.slice(0, stop + 1) : cut.slice(0, cut.lastIndexOf(' '))).trim();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface BuiltVoiceInstructions {
|
|
141
|
+
text: string;
|
|
142
|
+
chars: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** "Monday, 2026-09-21, 14:32 (Europe/Vienna)" in the server's zone. */
|
|
146
|
+
export function callStartStamp(now: Date): string {
|
|
147
|
+
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
148
|
+
const weekday = new Intl.DateTimeFormat('en-US', { weekday: 'long' }).format(now);
|
|
149
|
+
const p = (n: number): string => String(n).padStart(2, '0');
|
|
150
|
+
const date = `${now.getFullYear()}-${p(now.getMonth() + 1)}-${p(now.getDate())}`;
|
|
151
|
+
return `${weekday}, ${date}, ${p(now.getHours())}:${p(now.getMinutes())} (${zone})`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function buildVoiceInstructions(input: VoiceInstructionsInput): BuiltVoiceInstructions {
|
|
155
|
+
const { persona, consultPolicy, language, consultToolName, sessionSlug } = input;
|
|
156
|
+
const caller = userEssence(persona);
|
|
157
|
+
const override = input.override?.trim();
|
|
158
|
+
const style = persona.voice?.style?.trim();
|
|
159
|
+
const sentences = persona.voice?.maxSpokenSentences ?? 4;
|
|
160
|
+
|
|
161
|
+
// What the voice self must ask about, and what it simply IS.
|
|
162
|
+
//
|
|
163
|
+
// Rene, 2026-09-11, after the first call: asked who he was, the voice
|
|
164
|
+
// self consulted the agent about its own identity and then said "ich
|
|
165
|
+
// bin hans und nicht er". Two faults in one sentence — it treated its
|
|
166
|
+
// own name as a fact it had to look up, and it spoke about the agent
|
|
167
|
+
// in the third person. Identity, role, manner and what it can do are
|
|
168
|
+
// ITS OWN; only the work is the agent's.
|
|
169
|
+
// Three rules that kept fighting each other in the first live calls,
|
|
170
|
+
// now one line each.
|
|
171
|
+
//
|
|
172
|
+
// - It refused work on its own authority ("den Browser hab ich
|
|
173
|
+
// nicht") because the prompt told it that it knew what it could do.
|
|
174
|
+
// - Told to announce a lookup and then make it, it announced and
|
|
175
|
+
// stopped — both models, so the filler comes from somora instead.
|
|
176
|
+
// - And it narrated the lookup as asking a third party, which breaks
|
|
177
|
+
// the one thing this whole design is about: it IS the agent.
|
|
178
|
+
const consultLine =
|
|
179
|
+
consultPolicy === 'always'
|
|
180
|
+
? `Everything factual and every request to act goes through ${consultToolName}: work, projects, files, status, memory, opening a browser, starting, writing or sending something. Greeting, small talk and who you are need no call.`
|
|
181
|
+
: consultPolicy === 'substantive'
|
|
182
|
+
? `Anything about your work, memory, files or projects, and every request to act, goes through ${consultToolName}. Small talk, clarifying questions and who you are need no call.`
|
|
183
|
+
: `Call ${consultToolName} whenever the answer needs your files, tools or memory.`;
|
|
184
|
+
|
|
185
|
+
const spoken = languageName(language);
|
|
186
|
+
const parts = [
|
|
187
|
+
// The contrast only makes sense when the two differ. Told "these
|
|
188
|
+
// instructions are English, your speech is not" while speaking
|
|
189
|
+
// English, the line contradicts itself (Rene, 2026-09-12).
|
|
190
|
+
spoken === 'English'
|
|
191
|
+
? `Speak English. Never switch language mid-call, not for a word.`
|
|
192
|
+
: `Speak ${spoken}. These instructions are English, your speech is not — never switch language mid-call, not for a word. Tool and file names stay as written.`,
|
|
193
|
+
`You are ${persona.name}, speaking out loud — not an assistant for ${persona.name}, not a voice channel. Say "I", never talk about ${persona.name} as someone else.`,
|
|
194
|
+
override ? override : personaEssence(persona),
|
|
195
|
+
override || !style ? '' : `Tone: ${style}.`,
|
|
196
|
+
// Two things the voice self could not know and had to consult for
|
|
197
|
+
// (report 2026-09-16: "Wer bin ich?" / a wrong time of day): who is
|
|
198
|
+
// calling, and what day it is. The time is the call's START — a
|
|
199
|
+
// call runs up to maxCallMinutes, so it is said as such.
|
|
200
|
+
caller
|
|
201
|
+
? `The person on this call, from your own notes: ${caller} That much is yours to know — no lookup for it; anything more about them is.`
|
|
202
|
+
: '',
|
|
203
|
+
`This call started ${callStartStamp(input.now ?? new Date())}. For the exact time now, look it up.`,
|
|
204
|
+
`At most ${sentences} sentences per answer: a conversation, not a lecture, and interruptible.`,
|
|
205
|
+
// The caller picked this agent by name in a picker and talks to it
|
|
206
|
+
// daily. An introduction is a wall in front of the first question,
|
|
207
|
+
// and jarvis, who is formal by persona, made it a ceremony (Rene,
|
|
208
|
+
// 2026-09-12: "die brauchen nicht immer eine lange intro davor").
|
|
209
|
+
`Do not introduce yourself and do not list what you can do — they chose you and know you. Answer the first thing they say as if the call had been running. Who you are and how you talk needs no lookup: say it when asked, not before, and you are an agent in somora, not a human. Handed a call: one short sentence that you are here, then on.`,
|
|
210
|
+
consultLine,
|
|
211
|
+
// What falls through otherwise. A call is not only requests: the
|
|
212
|
+
// sentence "die IXO Holding ist schon Vergangenheit" was a remark,
|
|
213
|
+
// not a question, and the dream phase turned it into a memory note
|
|
214
|
+
// that corrected three wiki pages. Since only what reaches the agent
|
|
215
|
+
// is recorded, a remark nobody forwards is gone (Rene, 2026-09-12).
|
|
216
|
+
`Pass on what they state or correct too — a decision, a date, "X is history" — as a short note, not only what they ask for.`,
|
|
217
|
+
`What you can do is not yours to judge: never say you cannot do something, never claim a missing tool, never offer a workaround.`,
|
|
218
|
+
`Call the moment something is asked of you — do not announce it, do not ask whether you should, do not wait. You are looking it up, not asking someone else. Keep the request to one short sentence.`,
|
|
219
|
+
`Then answer in your own words, shortened for the ear, no lists or paths read aloud. Never invent a fact, a name or a number, and never say you did something before you have.`,
|
|
220
|
+
input.switchTo && input.switchTo.length > 0
|
|
221
|
+
? `You are in your session "${sessionSlug}". If they ask for someone else — ${input.switchTo.join(', ')} — or for another session of yours, move the call with the switch tool, never unasked. Pass a session only when they named one.`
|
|
222
|
+
: `This conversation runs in your session "${sessionSlug}". You cannot switch to another agent or session.`,
|
|
223
|
+
].filter((p) => p.length > 0);
|
|
224
|
+
|
|
225
|
+
const text = parts.join('\n');
|
|
226
|
+
return { text, chars: text.length };
|
|
227
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Finding the session somebody just said out loud.
|
|
2
|
+
//
|
|
3
|
+
// This exists ONLY for voice. Everywhere else a session name is typed,
|
|
4
|
+
// and a typed name that is wrong must stay wrong — a slash command or an
|
|
5
|
+
// API call that lands "approximately" somewhere is a bug, not a
|
|
6
|
+
// convenience. Here the name went through a microphone, a speech
|
|
7
|
+
// recogniser and a model that writes what it thinks it heard, and it
|
|
8
|
+
// comes out different every single time:
|
|
9
|
+
//
|
|
10
|
+
// said "CerebroCraft" → Cerebokräft · craft · CerebroCraft
|
|
11
|
+
// said "voicecheck" → voice-check
|
|
12
|
+
//
|
|
13
|
+
// All four reached the server on 2026-09-12 and all four missed
|
|
14
|
+
// `cerebrocraft` and `voicecheck`, which is why no call ever got into a
|
|
15
|
+
// session other than main. Matching is therefore done on sound-ish
|
|
16
|
+
// shape, not on spelling — and where the shape is ambiguous the voice
|
|
17
|
+
// asks instead of guessing, because landing in the wrong conversation is
|
|
18
|
+
// worse than one more question.
|
|
19
|
+
|
|
20
|
+
/** Letters and digits only, lower case, umlauts folded. What survives is
|
|
21
|
+
* roughly what two people would agree they heard. */
|
|
22
|
+
export function normalizeSpokenName(text: string): string {
|
|
23
|
+
return text
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.replace(/ä/g, 'a')
|
|
26
|
+
.replace(/ö/g, 'o')
|
|
27
|
+
.replace(/ü/g, 'u')
|
|
28
|
+
.replace(/ß/g, 'ss')
|
|
29
|
+
.normalize('NFD')
|
|
30
|
+
.replace(/[̀-ͯ]/g, '')
|
|
31
|
+
.replace(/[^a-z0-9]/g, '');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Levenshtein distance, iterative, two rows. Names are short. */
|
|
35
|
+
function distance(a: string, b: string): number {
|
|
36
|
+
if (a === b) return 0;
|
|
37
|
+
if (a.length === 0) return b.length;
|
|
38
|
+
if (b.length === 0) return a.length;
|
|
39
|
+
let prev = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
40
|
+
for (let i = 1; i <= a.length; i++) {
|
|
41
|
+
const row = [i];
|
|
42
|
+
for (let j = 1; j <= b.length; j++) {
|
|
43
|
+
row[j] = Math.min(
|
|
44
|
+
prev[j]! + 1,
|
|
45
|
+
row[j - 1]! + 1,
|
|
46
|
+
prev[j - 1]! + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
prev = row;
|
|
50
|
+
}
|
|
51
|
+
return prev[b.length]!;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 1 = identical, 0 = nothing in common. */
|
|
55
|
+
function similarity(a: string, b: string): number {
|
|
56
|
+
const longest = Math.max(a.length, b.length);
|
|
57
|
+
if (longest === 0) return 1;
|
|
58
|
+
return 1 - distance(a, b) / longest;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* How close a heard name has to be before we accept it.
|
|
63
|
+
*
|
|
64
|
+
* `Cerebokräft` against `cerebrocraft` scores 0.83 — a missing letter
|
|
65
|
+
* and a k for a c, which is what German speech recognition does to an
|
|
66
|
+
* English word. Below 0.7 the two words no longer sound like each other
|
|
67
|
+
* and a match would be a guess.
|
|
68
|
+
*/
|
|
69
|
+
const MIN_SIMILARITY = 0.7;
|
|
70
|
+
|
|
71
|
+
/** A clear winner has to beat the runner-up by this much. Two sessions
|
|
72
|
+
* that sound equally close are a question, not a decision. */
|
|
73
|
+
const MIN_MARGIN = 0.08;
|
|
74
|
+
|
|
75
|
+
export type SessionMatch =
|
|
76
|
+
| { kind: 'one'; slug: string }
|
|
77
|
+
| { kind: 'many'; candidates: string[] }
|
|
78
|
+
| { kind: 'none' };
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Pick the session a caller meant.
|
|
82
|
+
*
|
|
83
|
+
* Exact first, then containment (a model that heard "CerebroCraft" may
|
|
84
|
+
* pass only "craft"), then similarity. `main` always exists and is
|
|
85
|
+
* always a candidate.
|
|
86
|
+
*/
|
|
87
|
+
export function matchSpokenSession(spoken: string, slugs: readonly string[]): SessionMatch {
|
|
88
|
+
const needle = normalizeSpokenName(spoken);
|
|
89
|
+
if (needle.length === 0) return { kind: 'none' };
|
|
90
|
+
const pool = [...new Set(['main', ...slugs])];
|
|
91
|
+
|
|
92
|
+
const exact = pool.filter((s) => normalizeSpokenName(s) === needle);
|
|
93
|
+
if (exact.length === 1) return { kind: 'one', slug: exact[0]! };
|
|
94
|
+
if (exact.length > 1) return { kind: 'many', candidates: exact };
|
|
95
|
+
|
|
96
|
+
// Containment, but only for a fragment long enough to mean something:
|
|
97
|
+
// "ai" inside half the session names is not a wish.
|
|
98
|
+
if (needle.length >= 4) {
|
|
99
|
+
const contained = pool.filter((s) => {
|
|
100
|
+
const n = normalizeSpokenName(s);
|
|
101
|
+
return n.includes(needle) || needle.includes(n);
|
|
102
|
+
});
|
|
103
|
+
if (contained.length === 1) return { kind: 'one', slug: contained[0]! };
|
|
104
|
+
if (contained.length > 1) return { kind: 'many', candidates: contained.slice(0, 4) };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const scored = pool
|
|
108
|
+
.map((slug) => ({ slug, score: similarity(needle, normalizeSpokenName(slug)) }))
|
|
109
|
+
.filter((s) => s.score >= MIN_SIMILARITY)
|
|
110
|
+
.sort((a, b) => b.score - a.score);
|
|
111
|
+
if (scored.length === 0) return { kind: 'none' };
|
|
112
|
+
const best = scored[0]!;
|
|
113
|
+
const runnerUp = scored[1];
|
|
114
|
+
if (!runnerUp || best.score - runnerUp.score >= MIN_MARGIN) return { kind: 'one', slug: best.slug };
|
|
115
|
+
return { kind: 'many', candidates: scored.slice(0, 4).map((s) => s.slug) };
|
|
116
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// The contract somora speaks to a realtime voice provider.
|
|
2
|
+
//
|
|
3
|
+
// Three providers are in view and none of them share a protocol:
|
|
4
|
+
// OpenAI's realtime API, Google's `BidiGenerateContent`, and later our
|
|
5
|
+
// own local service. Verified 2026-09-11: Google's wire format is
|
|
6
|
+
// explicitly not the OpenAI one (different message names, 16 kHz in /
|
|
7
|
+
// 24 kHz out, its own interruption signal), and OpenRouter cannot carry
|
|
8
|
+
// either — its infrastructure is request/response, so a standing
|
|
9
|
+
// connection never gets through. So somora defines its own vocabulary
|
|
10
|
+
// here and each provider gets a thin adapter.
|
|
11
|
+
//
|
|
12
|
+
// The one rule that keeps this honest: an adapter DECLARES what it can
|
|
13
|
+
// do. A contract that pretends every provider can truncate a half-heard
|
|
14
|
+
// answer produces a silent no-op somewhere (the Hugging Face
|
|
15
|
+
// speech-to-speech server does exactly that today), and a feature that
|
|
16
|
+
// silently does nothing is worse than one that says it is missing.
|
|
17
|
+
|
|
18
|
+
/** What a provider can actually do. No optimistic defaults. */
|
|
19
|
+
export interface RealtimeCapabilities {
|
|
20
|
+
/** How the browser's audio reaches the provider. */
|
|
21
|
+
transport: 'webrtc' | 'websocket';
|
|
22
|
+
/** The provider detects end-of-speech itself (server-side VAD). */
|
|
23
|
+
serverVad: boolean;
|
|
24
|
+
/** The user can talk over the model and the model stops. */
|
|
25
|
+
bargeIn: boolean;
|
|
26
|
+
/** A half-played answer can be cut back to what was really heard —
|
|
27
|
+
* without it the model believes it said things the user never got. */
|
|
28
|
+
truncation: boolean;
|
|
29
|
+
/** Function calling, i.e. the consult tool. Without it there is no
|
|
30
|
+
* point: the voice self could only talk, never ask the agent. */
|
|
31
|
+
tools: boolean;
|
|
32
|
+
/** The instructions (the voice persona) can change mid-call.
|
|
33
|
+
* OpenAI: yes. The VOICE cannot — see `voiceFixedAfterFirstAudio`. */
|
|
34
|
+
liveInstructionUpdate: boolean;
|
|
35
|
+
/** Measured on OpenAI 2026-09-11: every session field may be updated
|
|
36
|
+
* at any time EXCEPT model and voice, and voice only until the model
|
|
37
|
+
* has produced audio once. That is why switching agents mid-call is
|
|
38
|
+
* a fresh connection rather than a settings change. */
|
|
39
|
+
voiceFixedAfterFirstAudio: boolean;
|
|
40
|
+
/** The provider reports what a call cost. */
|
|
41
|
+
usage: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Everything a call needs to open a provider session. */
|
|
45
|
+
export interface RealtimeSessionRequest {
|
|
46
|
+
/** Provider model id, from config — never guessed. */
|
|
47
|
+
model: string;
|
|
48
|
+
/** Provider voice id, already mapped from the logical name. */
|
|
49
|
+
voice: string;
|
|
50
|
+
/** The voice self: who it is, how it speaks, and the hard rules. */
|
|
51
|
+
instructions: string;
|
|
52
|
+
/** Spoken language, BCP-47-ish ('de'), for transcription hints. */
|
|
53
|
+
language: string;
|
|
54
|
+
/** How eagerly the provider hears an interruption and an end of
|
|
55
|
+
* turn. Defaults live in config; see realtimeVoice.turnDetection. */
|
|
56
|
+
turnDetection?: { threshold: number; prefixPaddingMs: number; silenceDurationMs: number };
|
|
57
|
+
/** The tools the voice self may call. Always small — in v1 exactly
|
|
58
|
+
* one. These exist ONLY inside the provider session: they are not in
|
|
59
|
+
* somora's ToolRegistry, no agent can see or call them. */
|
|
60
|
+
tools: RealtimeToolSpec[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RealtimeToolSpec {
|
|
64
|
+
name: string;
|
|
65
|
+
description: string;
|
|
66
|
+
/** JSON Schema of the arguments. */
|
|
67
|
+
parameters: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* What a provider session tells somora. Deliberately small: everything
|
|
72
|
+
* a UI or the call state machine needs, nothing provider-shaped.
|
|
73
|
+
*/
|
|
74
|
+
export type RealtimeEvent =
|
|
75
|
+
/** The connection is up and the model is listening. */
|
|
76
|
+
| { kind: 'ready'; ts: number }
|
|
77
|
+
/** The user started/stopped speaking (provider VAD). */
|
|
78
|
+
| { kind: 'user_speech'; ts: number; phase: 'start' | 'end' }
|
|
79
|
+
/** Transcript of the user's speech; `final` is what gets persisted. */
|
|
80
|
+
| { kind: 'user_transcript'; ts: number; text: string; final: boolean }
|
|
81
|
+
/** The model started/stopped speaking. */
|
|
82
|
+
| { kind: 'model_speech'; ts: number; phase: 'start' | 'end' }
|
|
83
|
+
/** What the model said, as text. `final` is the spoken record. */
|
|
84
|
+
| { kind: 'model_transcript'; ts: number; text: string; final: boolean }
|
|
85
|
+
/** The voice self wants somora to run one of its tools. */
|
|
86
|
+
| { kind: 'tool_call'; ts: number; callId: string; name: string; args: string }
|
|
87
|
+
/** The user talked over the model; playback should stop. */
|
|
88
|
+
| { kind: 'interrupted'; ts: number }
|
|
89
|
+
/** Audio the model is speaking, when it flows through somora. */
|
|
90
|
+
| { kind: 'audio'; ts: number; base64: string; rateHz: number }
|
|
91
|
+
/** Cost/usage, when the provider reports it. */
|
|
92
|
+
| { kind: 'usage'; ts: number; inputTokens?: number; outputTokens?: number; seconds?: number }
|
|
93
|
+
/** Terminal. `reason` is for the log and the UI, not for the model. */
|
|
94
|
+
| { kind: 'closed'; ts: number; reason: string }
|
|
95
|
+
| { kind: 'error'; ts: number; message: string; fatal: boolean };
|
|
96
|
+
|
|
97
|
+
/** A chunk of audio, base64 PCM16 at the rate the adapter declared. */
|
|
98
|
+
export interface RealtimeAudioChunk {
|
|
99
|
+
base64: string;
|
|
100
|
+
rateHz: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A live provider session.
|
|
105
|
+
*
|
|
106
|
+
* Two transports, two audio paths, and the difference is not cosmetic:
|
|
107
|
+
*
|
|
108
|
+
* - `websocket`: audio flows browser → somora → provider. One extra
|
|
109
|
+
* hop, but tool calls, session control and the key live in exactly
|
|
110
|
+
* one place. This is what OpenClaw uses for its tool-backed calls
|
|
111
|
+
* (read 2026-09-11: their bridge answers a tool with
|
|
112
|
+
* `conversation.item.create` + `response.create` over that socket).
|
|
113
|
+
* - `webrtc`: audio flows browser ↔ provider directly. Lower latency,
|
|
114
|
+
* but then the control channel is in the BROWSER — and somora's
|
|
115
|
+
* rule is that tools run server-side, on one path only. OpenAI's
|
|
116
|
+
* sideband (`wss://…/v1/realtime?call_id=…`) exists for exactly
|
|
117
|
+
* that, but as of 2026-09-09/10 it answers `call_id_not_found` for
|
|
118
|
+
* calls created with an ephemeral key; it works when the SDP offer
|
|
119
|
+
* was posted with the real API key. So WebRTC here means somora
|
|
120
|
+
* brokers the offer, never the browser.
|
|
121
|
+
*
|
|
122
|
+
* v1 ships the websocket path because it is provable end to end from a
|
|
123
|
+
* script, without a browser and without ambiguity about who executes a
|
|
124
|
+
* tool. The contract carries both so the faster one is an optimisation,
|
|
125
|
+
* not a rewrite.
|
|
126
|
+
*/
|
|
127
|
+
export interface SpeakOptions {
|
|
128
|
+
/** This must be said: if the model is talking right now, keep it and
|
|
129
|
+
* say it as soon as the model falls silent. Without it a request
|
|
130
|
+
* that arrives mid-response is dropped — right for a filler ("one
|
|
131
|
+
* moment"), wrong for the answer to a handed-over question, which
|
|
132
|
+
* went silent that way on 2026-09-13 while the log said announced. */
|
|
133
|
+
deliver?: boolean;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface RealtimeSession {
|
|
137
|
+
readonly id: string;
|
|
138
|
+
/** Provider events, in order. Ends when the session closes. */
|
|
139
|
+
events(): AsyncGenerator<RealtimeEvent>;
|
|
140
|
+
/** Microphone audio, when the transport routes it through somora. */
|
|
141
|
+
sendAudio?(chunk: RealtimeAudioChunk): Promise<void>;
|
|
142
|
+
/** Answer a tool call the voice self made. */
|
|
143
|
+
sendToolResult(callId: string, result: string): Promise<void>;
|
|
144
|
+
/** Swap the voice persona mid-call (capability-gated). */
|
|
145
|
+
updateInstructions(instructions: string): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Make the model say one thing now, under a one-off instruction.
|
|
148
|
+
*
|
|
149
|
+
* This is how the filler during a lookup is produced. Leaving it to
|
|
150
|
+
* the model does not work: told to announce and then call, it
|
|
151
|
+
* announced and stopped — measured on both gpt-realtime-2.1-mini and
|
|
152
|
+
* gpt-realtime-2.1 on 2026-09-11, asked to open a browser, it said
|
|
153
|
+
* "moment, ich schau nach" and waited for the user instead of making
|
|
154
|
+
* the call. OpenClaw drives the same pattern from the host side.
|
|
155
|
+
*/
|
|
156
|
+
speak?(instructions: string, opts?: SpeakOptions): Promise<void>;
|
|
157
|
+
/** Stop the model talking right now (barge-in, or the user hung up). */
|
|
158
|
+
interrupt(): Promise<void>;
|
|
159
|
+
close(reason: string): Promise<void>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** How the browser is told to reach the provider. The long-lived key
|
|
163
|
+
* NEVER leaves the server: an adapter hands out a short-lived token or
|
|
164
|
+
* brokers the handshake itself. */
|
|
165
|
+
export interface RealtimeClientHandshake {
|
|
166
|
+
kind: 'ephemeral_token' | 'sdp_broker';
|
|
167
|
+
/** For 'ephemeral_token': the short-lived credential and where to
|
|
168
|
+
* send the offer. For 'sdp_broker': the somora route to post to. */
|
|
169
|
+
value: string;
|
|
170
|
+
url?: string;
|
|
171
|
+
expiresAt?: number;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface RealtimeProvider {
|
|
175
|
+
readonly name: string;
|
|
176
|
+
capabilities(): RealtimeCapabilities;
|
|
177
|
+
/** Open a session. The adapter is responsible for auth; the caller
|
|
178
|
+
* passes only what the contract describes. */
|
|
179
|
+
open(req: RealtimeSessionRequest): Promise<RealtimeSession>;
|
|
180
|
+
/** What the browser needs to attach its microphone and speaker. */
|
|
181
|
+
handshake(session: RealtimeSession): Promise<RealtimeClientHandshake>;
|
|
182
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// mtime-based optimistic-concurrency helper for wiki + memory writes.
|
|
2
|
+
//
|
|
3
|
+
// Used by Dream-B (memory → wiki) and Dream-C (wiki lint corrections):
|
|
4
|
+
// before writing a file the worker has read+reasoned about, re-check
|
|
5
|
+
// that the on-disk mtime hasn't shifted in the meantime. If it has, the
|
|
6
|
+
// user (or another worker) edited the file during our reasoning window;
|
|
7
|
+
// abort and retry next run rather than clobber.
|
|
8
|
+
//
|
|
9
|
+
// Single-writer-pro-File-Invariante (DECISIONS-Anhang in
|
|
10
|
+
// `private/wiki-design.md`) makes this small and sufficient: there's
|
|
11
|
+
// no merge logic to write because parallel writes are designed out.
|
|
12
|
+
//
|
|
13
|
+
// See `private/wiki-design.md` § "Konflikt-Strategie".
|
|
14
|
+
|
|
15
|
+
import { readFile, stat, writeFile } from 'node:fs/promises';
|
|
16
|
+
|
|
17
|
+
export interface ReadWithMtimeResult {
|
|
18
|
+
/** File contents as UTF-8 string. */
|
|
19
|
+
text: string;
|
|
20
|
+
/** mtime in ms-since-epoch — pass this back to writeIfMtimeUnchanged. */
|
|
21
|
+
mtimeMs: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Read a file plus its mtime in one shot. Returns null if the file does
|
|
26
|
+
* not exist (caller can interpret that as "fresh write" rather than
|
|
27
|
+
* conflict).
|
|
28
|
+
*/
|
|
29
|
+
export async function readWithMtime(path: string): Promise<ReadWithMtimeResult | null> {
|
|
30
|
+
try {
|
|
31
|
+
const [buf, st] = await Promise.all([readFile(path, 'utf8'), stat(path)]);
|
|
32
|
+
return { text: buf, mtimeMs: st.mtimeMs };
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type WriteOutcome =
|
|
40
|
+
| { kind: 'written'; mtimeMs: number }
|
|
41
|
+
| { kind: 'skipped_conflict'; observedMtimeMs: number; expectedMtimeMs: number }
|
|
42
|
+
| { kind: 'skipped_missing'; expectedMtimeMs: number };
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Write `content` to `path` only if the on-disk mtime still matches
|
|
46
|
+
* `expectedMtimeMs` (the value the caller saw at read time).
|
|
47
|
+
*
|
|
48
|
+
* Behavior:
|
|
49
|
+
* - mtime matches → write succeeds, returns 'written' with new mtime
|
|
50
|
+
* - mtime differs → 'skipped_conflict' (user/another worker touched it)
|
|
51
|
+
* - file vanished → 'skipped_missing' (callers may re-create on retry)
|
|
52
|
+
*
|
|
53
|
+
* `expectedMtimeMs` semantics: pass `null` to enable "create-or-fail-if-
|
|
54
|
+
* exists". Pass a number to require an exact match.
|
|
55
|
+
*
|
|
56
|
+
* NOTE: this is optimistic concurrency, not a lock. Two writers racing
|
|
57
|
+
* the same file in the < 1ms window between stat and write can still
|
|
58
|
+
* collide. For the wiki use case (one Dream-B worker server-side, plus
|
|
59
|
+
* occasional manual user edits), the race window is tiny and we accept
|
|
60
|
+
* it. If we later run multiple writer processes against the same wiki,
|
|
61
|
+
* upgrade to a real flock-based lock.
|
|
62
|
+
*/
|
|
63
|
+
export async function writeIfMtimeUnchanged(
|
|
64
|
+
path: string,
|
|
65
|
+
content: string,
|
|
66
|
+
expectedMtimeMs: number,
|
|
67
|
+
): Promise<WriteOutcome> {
|
|
68
|
+
let observedMtimeMs: number;
|
|
69
|
+
try {
|
|
70
|
+
const st = await stat(path);
|
|
71
|
+
observedMtimeMs = st.mtimeMs;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
74
|
+
return { kind: 'skipped_missing', expectedMtimeMs };
|
|
75
|
+
}
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
if (observedMtimeMs !== expectedMtimeMs) {
|
|
79
|
+
return { kind: 'skipped_conflict', observedMtimeMs, expectedMtimeMs };
|
|
80
|
+
}
|
|
81
|
+
await writeFile(path, content, 'utf8');
|
|
82
|
+
const finalSt = await stat(path);
|
|
83
|
+
return { kind: 'written', mtimeMs: finalSt.mtimeMs };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Create a new file iff it doesn't exist yet. Returns 'written' on
|
|
88
|
+
* success, 'skipped_conflict' if the path already exists.
|
|
89
|
+
*
|
|
90
|
+
* Useful for Dream-B initial promotions where we don't have a previous
|
|
91
|
+
* mtime to compare against — the contract is "we expect this slug to
|
|
92
|
+
* be net-new; if someone got there first, defer to them".
|
|
93
|
+
*/
|
|
94
|
+
export async function writeIfNotExists(
|
|
95
|
+
path: string,
|
|
96
|
+
content: string,
|
|
97
|
+
): Promise<WriteOutcome> {
|
|
98
|
+
// Exclusive-create via O_EXCL (`wx` flag). Pre-audit-2026-05-16 this
|
|
99
|
+
// was stat-then-write — a classic TOCTOU window let two parallel
|
|
100
|
+
// wiki_create calls both pass the existence check and the second
|
|
101
|
+
// overwrite the first. EEXIST now maps to skipped_conflict.
|
|
102
|
+
try {
|
|
103
|
+
await writeFile(path, content, { encoding: 'utf8', flag: 'wx' });
|
|
104
|
+
} catch (err) {
|
|
105
|
+
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
106
|
+
const st = await stat(path);
|
|
107
|
+
return { kind: 'skipped_conflict', observedMtimeMs: st.mtimeMs, expectedMtimeMs: 0 };
|
|
108
|
+
}
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
const finalSt = await stat(path);
|
|
112
|
+
return { kind: 'written', mtimeMs: finalSt.mtimeMs };
|
|
113
|
+
}
|