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,162 @@
|
|
|
1
|
+
// Sampling-parameter helpers for the `/sampling` and `/temp` slash
|
|
2
|
+
// commands: the key catalogue, the `key=value` argument parser and the
|
|
3
|
+
// one-line formatter used by notices and the header badge.
|
|
4
|
+
//
|
|
5
|
+
// The web client has a deliberate twin of this file at
|
|
6
|
+
// web/src/lib/sampling.ts (the two clients share no code). Keep the two
|
|
7
|
+
// in sync by hand.
|
|
8
|
+
|
|
9
|
+
export interface SamplingParams {
|
|
10
|
+
temperature?: number;
|
|
11
|
+
top_p?: number;
|
|
12
|
+
top_k?: number;
|
|
13
|
+
min_p?: number;
|
|
14
|
+
frequency_penalty?: number;
|
|
15
|
+
presence_penalty?: number;
|
|
16
|
+
repetition_penalty?: number;
|
|
17
|
+
seed?: number;
|
|
18
|
+
stop?: string | string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type SamplingKey = keyof SamplingParams;
|
|
22
|
+
|
|
23
|
+
/** A PUT body: keys set to `null` remove that key from the override. */
|
|
24
|
+
export type SamplingPatch = { [K in SamplingKey]?: SamplingParams[K] | null };
|
|
25
|
+
|
|
26
|
+
/** Display + sort order for all keys — the order the server contract lists them. */
|
|
27
|
+
export const SAMPLING_KEYS: readonly SamplingKey[] = [
|
|
28
|
+
'temperature',
|
|
29
|
+
'top_p',
|
|
30
|
+
'top_k',
|
|
31
|
+
'min_p',
|
|
32
|
+
'frequency_penalty',
|
|
33
|
+
'presence_penalty',
|
|
34
|
+
'repetition_penalty',
|
|
35
|
+
'seed',
|
|
36
|
+
'stop',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
export const SAMPLING_KEY_HINTS: Record<SamplingKey, string> = {
|
|
40
|
+
temperature: '0–2 · randomness, 1 = model default',
|
|
41
|
+
top_p: '0–1 · nucleus sampling cutoff',
|
|
42
|
+
top_k: 'integer ≥ 1 · candidate pool size',
|
|
43
|
+
min_p: '0–1 · minimum relative token probability',
|
|
44
|
+
frequency_penalty: '-2–2 · penalise repeated tokens by count',
|
|
45
|
+
presence_penalty: '-2–2 · penalise tokens already present',
|
|
46
|
+
repetition_penalty: '> 0 · multiplicative repetition penalty',
|
|
47
|
+
seed: 'integer · deterministic sampling seed',
|
|
48
|
+
stop: 'comma-separated stop sequences',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const SAMPLING_USAGE =
|
|
52
|
+
`usage: /sampling [key=value …|default] — keys: ${SAMPLING_KEYS.join(' ')} · ` +
|
|
53
|
+
'value "null" or "-" removes a key';
|
|
54
|
+
|
|
55
|
+
export const TEMP_USAGE = 'usage: /temp <0–2>|default';
|
|
56
|
+
|
|
57
|
+
export type SamplingParseResult =
|
|
58
|
+
| { ok: true; params: SamplingPatch }
|
|
59
|
+
| { ok: false; error: string };
|
|
60
|
+
|
|
61
|
+
function numberIn(
|
|
62
|
+
key: SamplingKey,
|
|
63
|
+
raw: string,
|
|
64
|
+
opts: { min?: number; max?: number; int?: boolean; gtZero?: boolean },
|
|
65
|
+
): { ok: true; value: number } | { ok: false; error: string } {
|
|
66
|
+
const n = Number(raw);
|
|
67
|
+
const bounds =
|
|
68
|
+
opts.min !== undefined && opts.max !== undefined
|
|
69
|
+
? ` between ${opts.min} and ${opts.max}`
|
|
70
|
+
: opts.gtZero
|
|
71
|
+
? ' greater than 0'
|
|
72
|
+
: '';
|
|
73
|
+
const what = `${opts.int ? 'an integer' : 'a number'}${bounds}`;
|
|
74
|
+
if (raw.trim() === '' || !Number.isFinite(n)) {
|
|
75
|
+
return { ok: false, error: `${key} must be ${what}` };
|
|
76
|
+
}
|
|
77
|
+
if (opts.int && !Number.isInteger(n)) return { ok: false, error: `${key} must be ${what}` };
|
|
78
|
+
if (opts.min !== undefined && n < opts.min) return { ok: false, error: `${key} must be ${what}` };
|
|
79
|
+
if (opts.max !== undefined && n > opts.max) return { ok: false, error: `${key} must be ${what}` };
|
|
80
|
+
if (opts.gtZero && n <= 0) return { ok: false, error: `${key} must be ${what}` };
|
|
81
|
+
return { ok: true, value: n };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Parse one value for one key. Exposed so `/temp` can reuse the
|
|
85
|
+
* temperature rule without going through the `key=value` split. */
|
|
86
|
+
export function parseSamplingValue(
|
|
87
|
+
key: SamplingKey,
|
|
88
|
+
raw: string,
|
|
89
|
+
): { ok: true; value: SamplingParams[SamplingKey] } | { ok: false; error: string } {
|
|
90
|
+
switch (key) {
|
|
91
|
+
case 'temperature':
|
|
92
|
+
return numberIn(key, raw, { min: 0, max: 2 });
|
|
93
|
+
case 'top_p':
|
|
94
|
+
case 'min_p':
|
|
95
|
+
return numberIn(key, raw, { min: 0, max: 1 });
|
|
96
|
+
case 'top_k':
|
|
97
|
+
return numberIn(key, raw, { min: 1, int: true });
|
|
98
|
+
case 'frequency_penalty':
|
|
99
|
+
case 'presence_penalty':
|
|
100
|
+
return numberIn(key, raw, { min: -2, max: 2 });
|
|
101
|
+
case 'repetition_penalty':
|
|
102
|
+
return numberIn(key, raw, { gtZero: true });
|
|
103
|
+
case 'seed':
|
|
104
|
+
return numberIn(key, raw, { int: true });
|
|
105
|
+
case 'stop': {
|
|
106
|
+
const parts = raw
|
|
107
|
+
.split(',')
|
|
108
|
+
.map((s) => s.trim())
|
|
109
|
+
.filter((s) => s.length > 0);
|
|
110
|
+
if (parts.length === 0) return { ok: false, error: 'stop needs at least one sequence' };
|
|
111
|
+
return { ok: true, value: parts.length === 1 ? parts[0] : parts };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function isSamplingKey(s: string): s is SamplingKey {
|
|
117
|
+
return (SAMPLING_KEYS as readonly string[]).includes(s);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Parse `key=value` tokens into a PUT patch. `null` / `-` as value
|
|
121
|
+
* means "remove this key". Any bad token fails the whole parse so
|
|
122
|
+
* the caller shows a usage notice and sends nothing. */
|
|
123
|
+
export function parseSamplingArgs(tokens: readonly string[]): SamplingParseResult {
|
|
124
|
+
const params: SamplingPatch = {};
|
|
125
|
+
const clean = tokens.map((t) => t.trim()).filter((t) => t.length > 0);
|
|
126
|
+
if (clean.length === 0) return { ok: false, error: 'no key=value pairs given' };
|
|
127
|
+
for (const tok of clean) {
|
|
128
|
+
const eq = tok.indexOf('=');
|
|
129
|
+
if (eq <= 0) return { ok: false, error: `expected key=value, got "${tok}"` };
|
|
130
|
+
const key = tok.slice(0, eq).trim().toLowerCase();
|
|
131
|
+
const raw = tok.slice(eq + 1).trim();
|
|
132
|
+
if (!isSamplingKey(key)) return { ok: false, error: `unknown key "${key}"` };
|
|
133
|
+
if (raw === 'null' || raw === '-') {
|
|
134
|
+
params[key] = null;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const v = parseSamplingValue(key, raw);
|
|
138
|
+
if (!v.ok) return { ok: false, error: v.error };
|
|
139
|
+
(params as Record<string, unknown>)[key] = v.value;
|
|
140
|
+
}
|
|
141
|
+
return { ok: true, params };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** `temperature=1 top_p=0.95` in catalogue order; `(engine default)`
|
|
145
|
+
* when nothing is set. Null entries (from a patch) render as `key=-`. */
|
|
146
|
+
export function formatSamplingParams(params: SamplingPatch | SamplingParams | null | undefined): string {
|
|
147
|
+
if (!params) return '(engine default)';
|
|
148
|
+
const parts: string[] = [];
|
|
149
|
+
for (const key of SAMPLING_KEYS) {
|
|
150
|
+
if (!(key in params)) continue;
|
|
151
|
+
const v = (params as SamplingPatch)[key];
|
|
152
|
+
if (v === undefined) continue;
|
|
153
|
+
if (v === null) parts.push(`${key}=-`);
|
|
154
|
+
else if (Array.isArray(v)) parts.push(`${key}=${v.join(',')}`);
|
|
155
|
+
else parts.push(`${key}=${String(v)}`);
|
|
156
|
+
}
|
|
157
|
+
return parts.length ? parts.join(' ') : '(engine default)';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function hasSamplingParams(params: SamplingParams | null | undefined): boolean {
|
|
161
|
+
return !!params && SAMPLING_KEYS.some((k) => params[k] !== undefined && params[k] !== null);
|
|
162
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Text, useStdout } from 'ink';
|
|
2
|
+
|
|
3
|
+
// Adapts to the actual terminal width — falls back to 80 columns when
|
|
4
|
+
// stdout doesn't report (e.g. piped output, edge cases).
|
|
5
|
+
//
|
|
6
|
+
// `gray` instead of dimColor: on dark terminals dimColor is near-invisible
|
|
7
|
+
// because it just halves intensity; gray is a real color with usable
|
|
8
|
+
// contrast against both backgrounds.
|
|
9
|
+
export function Separator() {
|
|
10
|
+
const { stdout } = useStdout();
|
|
11
|
+
const width = stdout?.columns ?? 80;
|
|
12
|
+
return <Text color="gray">{'─'.repeat(Math.max(20, width))}</Text>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Per-user TUI state persistence — last-used agent and (future) other
|
|
2
|
+
// UI prefs. Lives in `~/.somora/tui-state.json`. Best-effort: read and
|
|
3
|
+
// write failures degrade silently, the TUI just falls back to defaults.
|
|
4
|
+
|
|
5
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
|
|
9
|
+
const SOMORA_HOME = process.env.SOMORA_HOME ?? join(homedir(), '.somora');
|
|
10
|
+
const STATE_FILE = join(SOMORA_HOME, 'tui-state.json');
|
|
11
|
+
|
|
12
|
+
export interface TuiState {
|
|
13
|
+
/** Last agent the TUI was active on. Restored on next startup if the
|
|
14
|
+
* agent still exists. */
|
|
15
|
+
lastAgent?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function readTuiState(): TuiState {
|
|
19
|
+
try {
|
|
20
|
+
const raw = readFileSync(STATE_FILE, 'utf8');
|
|
21
|
+
const parsed = JSON.parse(raw);
|
|
22
|
+
if (!parsed || typeof parsed !== 'object') return {};
|
|
23
|
+
return parsed as TuiState;
|
|
24
|
+
} catch {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function writeTuiState(state: TuiState): void {
|
|
30
|
+
try {
|
|
31
|
+
writeFileSync(STATE_FILE, JSON.stringify(state, null, 2), 'utf8');
|
|
32
|
+
} catch {
|
|
33
|
+
// best-effort — silently drop write failures (file system full,
|
|
34
|
+
// permissions, etc.). Persistence is a nice-to-have, not load-bearing.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function rememberAgent(agent: string): void {
|
|
39
|
+
const cur = readTuiState();
|
|
40
|
+
if (cur.lastAgent === agent) return;
|
|
41
|
+
writeTuiState({ ...cur, lastAgent: agent });
|
|
42
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// SSE consumer for /chat/stream. Yields normalized StreamEvent values via
|
|
2
|
+
// an async iterator so the React side can pull events with a simple for-await
|
|
3
|
+
// loop. Auto-reconnect with exponential backoff lives here too.
|
|
4
|
+
//
|
|
5
|
+
// Uses loopbackFetch so undici's default 5-min headersTimeout doesn't
|
|
6
|
+
// kill the initial response wait when the server takes a while to flush
|
|
7
|
+
// the first SSE frame (slow models, busy session lock, etc.). Body
|
|
8
|
+
// streaming is chunk-based and doesn't have an undici body timeout.
|
|
9
|
+
|
|
10
|
+
import { loopbackFetch } from '../../server/loopback-fetch.ts';
|
|
11
|
+
import type { FromSystemKind, TurnOrigin } from './types.ts';
|
|
12
|
+
|
|
13
|
+
const FROM_SYSTEM_KINDS: ReadonlySet<string> = new Set([
|
|
14
|
+
'sentinel',
|
|
15
|
+
'tmux',
|
|
16
|
+
'subagent',
|
|
17
|
+
'browser',
|
|
18
|
+
'voice',
|
|
19
|
+
'a2a',
|
|
20
|
+
'job',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
function isFromSystemKind(v: unknown): v is FromSystemKind {
|
|
24
|
+
return typeof v === 'string' && FROM_SYSTEM_KINDS.has(v);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Shape check only — the union's per-kind fields are read defensively
|
|
28
|
+
// by the renderer, so an unknown kind from a newer server is kept
|
|
29
|
+
// rather than dropped.
|
|
30
|
+
function isTurnOrigin(v: unknown): v is TurnOrigin {
|
|
31
|
+
return typeof v === 'object' && v !== null && typeof (v as { kind?: unknown }).kind === 'string';
|
|
32
|
+
}
|
|
33
|
+
import type { StreamEvent } from './types.ts';
|
|
34
|
+
|
|
35
|
+
export interface StreamHandle {
|
|
36
|
+
events: AsyncIterable<StreamEvent>;
|
|
37
|
+
close(): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const RECONNECT_INITIAL_MS = 500;
|
|
41
|
+
const RECONNECT_MAX_MS = 10_000;
|
|
42
|
+
|
|
43
|
+
export function openStream(
|
|
44
|
+
url: string,
|
|
45
|
+
onNotice: (text: string, tone: 'info' | 'warn' | 'error') => void,
|
|
46
|
+
): StreamHandle {
|
|
47
|
+
let abort: AbortController | null = null;
|
|
48
|
+
let reconnectMs = RECONNECT_INITIAL_MS;
|
|
49
|
+
let closed = false;
|
|
50
|
+
|
|
51
|
+
// Promise-queue async iterator: we push events as they arrive, consumer
|
|
52
|
+
// pulls via `for await`. Resolves the next pending promise when we have an
|
|
53
|
+
// event ready, or buffers if no consumer is waiting yet.
|
|
54
|
+
const queue: StreamEvent[] = [];
|
|
55
|
+
const waiters: Array<(e: IteratorResult<StreamEvent>) => void> = [];
|
|
56
|
+
|
|
57
|
+
function push(e: StreamEvent): void {
|
|
58
|
+
if (closed) return;
|
|
59
|
+
const w = waiters.shift();
|
|
60
|
+
if (w) w({ value: e, done: false });
|
|
61
|
+
else queue.push(e);
|
|
62
|
+
}
|
|
63
|
+
function resolveDone(): void {
|
|
64
|
+
closed = true;
|
|
65
|
+
while (waiters.length) {
|
|
66
|
+
const w = waiters.shift();
|
|
67
|
+
if (w) w({ value: undefined, done: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const events: AsyncIterable<StreamEvent> = {
|
|
72
|
+
[Symbol.asyncIterator]() {
|
|
73
|
+
return {
|
|
74
|
+
next(): Promise<IteratorResult<StreamEvent>> {
|
|
75
|
+
if (queue.length > 0) {
|
|
76
|
+
return Promise.resolve({ value: queue.shift()!, done: false });
|
|
77
|
+
}
|
|
78
|
+
if (closed) return Promise.resolve({ value: undefined, done: true });
|
|
79
|
+
return new Promise((resolve) => waiters.push(resolve));
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
async function consume(): Promise<void> {
|
|
86
|
+
if (closed) return;
|
|
87
|
+
abort = new AbortController();
|
|
88
|
+
let res: Response;
|
|
89
|
+
try {
|
|
90
|
+
res = await loopbackFetch(url, {
|
|
91
|
+
headers: { Accept: 'text/event-stream' },
|
|
92
|
+
signal: abort.signal,
|
|
93
|
+
});
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (abort.signal.aborted || closed) return;
|
|
96
|
+
onNotice(`stream-connect failed: ${(err as Error).message} — retry in ${reconnectMs}ms`, 'warn');
|
|
97
|
+
schedule();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (!res.ok || !res.body) {
|
|
101
|
+
const body = await res.text().catch(() => '');
|
|
102
|
+
// 4xx means "won't be fixed by retrying" (unknown agent/session,
|
|
103
|
+
// bad query, etc). Surface it once and stop reconnecting; the user
|
|
104
|
+
// can switch to a valid target. 5xx + transport errors keep the
|
|
105
|
+
// backoff loop because those are typically transient.
|
|
106
|
+
if (res.status >= 400 && res.status < 500) {
|
|
107
|
+
onNotice(`stream-connect ${res.status}: ${body} — giving up; switch with /agent or /session`, 'error');
|
|
108
|
+
resolveDone();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
onNotice(`stream-connect ${res.status}: ${body}`, 'error');
|
|
112
|
+
schedule();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const reader = res.body.getReader();
|
|
117
|
+
const decoder = new TextDecoder();
|
|
118
|
+
let buf = '';
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
while (true) {
|
|
122
|
+
const { value, done } = await reader.read();
|
|
123
|
+
if (done) break;
|
|
124
|
+
buf += decoder.decode(value, { stream: true });
|
|
125
|
+
let idx;
|
|
126
|
+
while ((idx = buf.indexOf('\n\n')) >= 0) {
|
|
127
|
+
const raw = buf.slice(0, idx);
|
|
128
|
+
buf = buf.slice(idx + 2);
|
|
129
|
+
const parsed = parseFrame(raw);
|
|
130
|
+
if (parsed) push(parsed);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!abort.signal.aborted && !closed) {
|
|
134
|
+
onNotice(`stream closed by server — reconnect in ${reconnectMs}ms`, 'warn');
|
|
135
|
+
schedule();
|
|
136
|
+
}
|
|
137
|
+
} catch (err) {
|
|
138
|
+
if (abort.signal.aborted || closed) return;
|
|
139
|
+
onNotice(`stream error: ${(err as Error).message} — reconnect in ${reconnectMs}ms`, 'warn');
|
|
140
|
+
schedule();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function schedule(): void {
|
|
145
|
+
const delay = reconnectMs;
|
|
146
|
+
reconnectMs = Math.min(reconnectMs * 2, RECONNECT_MAX_MS);
|
|
147
|
+
setTimeout(() => {
|
|
148
|
+
if (closed) return;
|
|
149
|
+
void consume();
|
|
150
|
+
}, delay);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function parseFrame(raw: string): StreamEvent | null {
|
|
154
|
+
let evName = 'message';
|
|
155
|
+
let dataStr = '';
|
|
156
|
+
for (const line of raw.split('\n')) {
|
|
157
|
+
if (line.startsWith('event:')) evName = line.slice(6).trim();
|
|
158
|
+
else if (line.startsWith('data:')) dataStr += line.slice(5).trim();
|
|
159
|
+
}
|
|
160
|
+
if (!dataStr) return null;
|
|
161
|
+
let data: any;
|
|
162
|
+
try {
|
|
163
|
+
data = JSON.parse(dataStr);
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
switch (evName) {
|
|
168
|
+
case 'chat':
|
|
169
|
+
if (data.state === 'delta') return { kind: 'chat-delta', text: data.text };
|
|
170
|
+
if (data.state === 'final') return { kind: 'chat-final', text: data.text };
|
|
171
|
+
return null;
|
|
172
|
+
case 'thinking': {
|
|
173
|
+
if (typeof data.text !== 'string') return null;
|
|
174
|
+
if (data.state === 'delta') return { kind: 'thinking-delta', text: data.text };
|
|
175
|
+
if (data.state === 'final') {
|
|
176
|
+
return {
|
|
177
|
+
kind: 'thinking-final',
|
|
178
|
+
text: data.text,
|
|
179
|
+
...(data.truncated === true ? { truncated: true } : {}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
case 'agent':
|
|
185
|
+
if (data.phase === 'start') {
|
|
186
|
+
return {
|
|
187
|
+
kind: 'agent-start',
|
|
188
|
+
thinking:
|
|
189
|
+
data.thinking && typeof data.thinking === 'object'
|
|
190
|
+
? {
|
|
191
|
+
level: data.thinking.level,
|
|
192
|
+
active: Boolean(data.thinking.active),
|
|
193
|
+
...(typeof data.thinking.wire === 'string' ? { wire: data.thinking.wire } : {}),
|
|
194
|
+
}
|
|
195
|
+
: undefined,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
if (data.phase === 'end') {
|
|
199
|
+
return {
|
|
200
|
+
kind: 'agent-end',
|
|
201
|
+
usage: data.usage,
|
|
202
|
+
contextWindow: data.contextWindow,
|
|
203
|
+
provider: data.provider,
|
|
204
|
+
model: data.model,
|
|
205
|
+
thinking:
|
|
206
|
+
data.thinking && typeof data.thinking === 'object'
|
|
207
|
+
? {
|
|
208
|
+
level: data.thinking.level,
|
|
209
|
+
active: Boolean(data.thinking.active),
|
|
210
|
+
...(typeof data.thinking.wire === 'string' ? { wire: data.thinking.wire } : {}),
|
|
211
|
+
}
|
|
212
|
+
: undefined,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
case 'memory':
|
|
217
|
+
return {
|
|
218
|
+
kind: 'memory',
|
|
219
|
+
count: data.count ?? 0,
|
|
220
|
+
topScore: typeof data.topScore === 'number' ? data.topScore : null,
|
|
221
|
+
refs: Array.isArray(data.refs) ? data.refs : [],
|
|
222
|
+
fullText: typeof data.fullText === 'string' ? data.fullText : undefined,
|
|
223
|
+
};
|
|
224
|
+
case 'assistant_media': {
|
|
225
|
+
const raw = Array.isArray((data as { media?: unknown }).media)
|
|
226
|
+
? ((data as { media: unknown[] }).media)
|
|
227
|
+
: [];
|
|
228
|
+
const items = raw
|
|
229
|
+
.map((m) => m as Record<string, unknown>)
|
|
230
|
+
.filter((m) => m.type === 'image' || m.type === 'video')
|
|
231
|
+
.map((m) => ({
|
|
232
|
+
type: m.type as 'image' | 'video',
|
|
233
|
+
filename: typeof m.filename === 'string' ? m.filename : 'file',
|
|
234
|
+
...(typeof m.durationSec === 'number' ? { durationSec: m.durationSec } : {}),
|
|
235
|
+
}));
|
|
236
|
+
return items.length > 0 ? { kind: 'assistant-media', items } : null;
|
|
237
|
+
}
|
|
238
|
+
case 'tool':
|
|
239
|
+
return {
|
|
240
|
+
kind: 'tool',
|
|
241
|
+
tool: data.tool ?? '',
|
|
242
|
+
phase: data.phase ?? '?',
|
|
243
|
+
summary: typeof data.summary === 'string' ? data.summary : undefined,
|
|
244
|
+
error: typeof data.error === 'string' ? data.error : undefined,
|
|
245
|
+
details: typeof data.details === 'string' ? data.details : undefined,
|
|
246
|
+
};
|
|
247
|
+
case 'engine_meta':
|
|
248
|
+
if (typeof data.engine !== 'string' || typeof data.itemType !== 'string') return null;
|
|
249
|
+
return {
|
|
250
|
+
kind: 'engine_meta',
|
|
251
|
+
engine: data.engine,
|
|
252
|
+
itemType: data.itemType,
|
|
253
|
+
label: typeof data.label === 'string' ? data.label : data.itemType,
|
|
254
|
+
...(typeof data.summary === 'string' ? { summary: data.summary } : {}),
|
|
255
|
+
payload: data.payload,
|
|
256
|
+
};
|
|
257
|
+
case 'model_fallback':
|
|
258
|
+
if (typeof data.requested !== 'string' || typeof data.actual !== 'string') return null;
|
|
259
|
+
return {
|
|
260
|
+
kind: 'model-fallback',
|
|
261
|
+
requested: data.requested,
|
|
262
|
+
actual: data.actual,
|
|
263
|
+
reason: typeof data.reason === 'string' ? data.reason : '',
|
|
264
|
+
...(Array.isArray(data.hops) ? { hops: data.hops as Array<{ model: string; reason: string }> } : {}),
|
|
265
|
+
};
|
|
266
|
+
case 'status':
|
|
267
|
+
if (data.msg === 'connected') {
|
|
268
|
+
reconnectMs = RECONNECT_INITIAL_MS;
|
|
269
|
+
return { kind: 'connected' };
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
case 'user_message':
|
|
273
|
+
if (typeof data.text !== 'string') return null;
|
|
274
|
+
return {
|
|
275
|
+
kind: 'user-message',
|
|
276
|
+
text: data.text,
|
|
277
|
+
// turnId is the server-issued id echoed back so a consumer
|
|
278
|
+
// that buffered a pending-queued entry can pair it now and
|
|
279
|
+
// promote the entry into a real Static-flushed user turn.
|
|
280
|
+
...(typeof data.turnId === 'string' ? { turnId: data.turnId } : {}),
|
|
281
|
+
// from_agent is optional — server now echoes self-typed
|
|
282
|
+
// turns too so other clients (web tab, TUI tail) see
|
|
283
|
+
// them live. Self-echoes carry no from_agent; consumer
|
|
284
|
+
// dedupes by recent-text against its own optimistic copy.
|
|
285
|
+
...(typeof data.from_agent === 'string' ? { fromAgent: data.from_agent } : {}),
|
|
286
|
+
...(typeof data.from_agent === 'string' && typeof data.from_session === 'string'
|
|
287
|
+
? { fromSession: data.from_session }
|
|
288
|
+
: {}),
|
|
289
|
+
// Every from_system word passes through — a value the
|
|
290
|
+
// renderer doesn't know yet still beats rendering the
|
|
291
|
+
// synthesized prompt as a human turn.
|
|
292
|
+
...(isFromSystemKind(data.from_system) ? { fromSystem: data.from_system } : {}),
|
|
293
|
+
...(isTurnOrigin(data.origin) ? { origin: data.origin } : {}),
|
|
294
|
+
callId: typeof data.agent_ask_call_id === 'string' ? data.agent_ask_call_id : undefined,
|
|
295
|
+
};
|
|
296
|
+
case 'turn_queued':
|
|
297
|
+
if (typeof data.turnId !== 'string' || typeof data.ahead !== 'number') return null;
|
|
298
|
+
return {
|
|
299
|
+
kind: 'turn-queued',
|
|
300
|
+
turnId: data.turnId,
|
|
301
|
+
ahead: data.ahead,
|
|
302
|
+
};
|
|
303
|
+
case 'turn_dequeued': {
|
|
304
|
+
const id = typeof data.turnId === 'string' ? data.turnId : typeof data.workId === 'string' ? data.workId : null;
|
|
305
|
+
if (!id) return null;
|
|
306
|
+
return { kind: 'turn-dequeued', turnId: id };
|
|
307
|
+
}
|
|
308
|
+
case 'project':
|
|
309
|
+
return {
|
|
310
|
+
kind: 'project',
|
|
311
|
+
from: typeof data.from === 'string' ? data.from : null,
|
|
312
|
+
to: typeof data.to === 'string' ? data.to : null,
|
|
313
|
+
via: data.via === 'tool' ? 'tool' : 'slash_command',
|
|
314
|
+
};
|
|
315
|
+
default:
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
void consume();
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
events,
|
|
324
|
+
close() {
|
|
325
|
+
closed = true;
|
|
326
|
+
if (abort) abort.abort();
|
|
327
|
+
resolveDone();
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Pure text helpers for rendering thinking content in the TUI.
|
|
2
|
+
//
|
|
3
|
+
// Kept free of React/Ink so they can be unit-tested with plain node
|
|
4
|
+
// (see thinking-view.test.mts). Two concerns:
|
|
5
|
+
// - the live streaming view shows only the TAIL of the thinking text
|
|
6
|
+
// (the model is still thinking; the last few lines are what matters)
|
|
7
|
+
// - the finalized scrollback turn is CAPPED so a huge reasoning dump
|
|
8
|
+
// (Qwen-style, thousands of lines) cannot flood the terminal.
|
|
9
|
+
|
|
10
|
+
/** Lines shown in the streaming view while the model is still thinking. */
|
|
11
|
+
export const THINKING_TAIL_LINES = 6;
|
|
12
|
+
/** Max lines a finalized thinking turn renders in the scrollback. */
|
|
13
|
+
export const THINKING_MAX_LINES = 40;
|
|
14
|
+
|
|
15
|
+
/** Split into lines, dropping trailing whitespace-only lines. */
|
|
16
|
+
function splitLines(text: string): string[] {
|
|
17
|
+
const trimmed = text.replace(/\s+$/, '');
|
|
18
|
+
if (trimmed.length === 0) return [];
|
|
19
|
+
return trimmed.split('\n');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Last `n` lines of `text`. Empty text → empty array. Trailing blank
|
|
24
|
+
* lines are dropped first so a delta ending in "\n" doesn't waste a
|
|
25
|
+
* slot on an empty row.
|
|
26
|
+
*/
|
|
27
|
+
export function tailLines(text: string, n: number = THINKING_TAIL_LINES): string[] {
|
|
28
|
+
if (n <= 0) return [];
|
|
29
|
+
const lines = splitLines(text);
|
|
30
|
+
return lines.length > n ? lines.slice(lines.length - n) : lines;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CappedLines {
|
|
34
|
+
/** The lines to render (at most `max`). */
|
|
35
|
+
lines: string[];
|
|
36
|
+
/** How many lines were cut off after `lines`; 0 = nothing hidden. */
|
|
37
|
+
hidden: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* First `max` lines of `text` plus the number of lines left out. The
|
|
42
|
+
* caller appends the `… (+N lines)` marker when `hidden > 0`.
|
|
43
|
+
*/
|
|
44
|
+
export function capLines(text: string, max: number = THINKING_MAX_LINES): CappedLines {
|
|
45
|
+
const lines = splitLines(text);
|
|
46
|
+
if (max <= 0) return { lines: [], hidden: lines.length };
|
|
47
|
+
if (lines.length <= max) return { lines, hidden: 0 };
|
|
48
|
+
return { lines: lines.slice(0, max), hidden: lines.length - max };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Marker line appended under a capped block. */
|
|
52
|
+
export function hiddenLinesMarker(hidden: number): string {
|
|
53
|
+
return `… (+${hidden} line${hidden === 1 ? '' : 's'})`;
|
|
54
|
+
}
|