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,69 @@
|
|
|
1
|
+
// Catalog snapshot — the cross-process contract between the hub (main
|
|
2
|
+
// server, single writer) and the per-turn MCP child in proxy mode
|
|
3
|
+
// (reader). Design: private/mcp-hub-design.md §4.3.
|
|
4
|
+
//
|
|
5
|
+
// Single-writer means no lockfile protocol is needed (the sentinel
|
|
6
|
+
// store's lock exists because BOTH main server and child write there);
|
|
7
|
+
// atomic tmp+rename is enough for readers to never see a torn file.
|
|
8
|
+
|
|
9
|
+
import { existsSync } from 'node:fs';
|
|
10
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import type { DiscoveredTool, McpServerStatus } from './manager.ts';
|
|
14
|
+
|
|
15
|
+
const SOMORA_HOME = process.env.SOMORA_HOME ?? join(homedir(), '.somora');
|
|
16
|
+
const MCP_DIR = join(SOMORA_HOME, 'mcp');
|
|
17
|
+
export const CATALOG_PATH = join(MCP_DIR, 'catalog.json');
|
|
18
|
+
|
|
19
|
+
export interface CatalogServer {
|
|
20
|
+
state: McpServerStatus['state'];
|
|
21
|
+
tools: Array<{
|
|
22
|
+
rawName: string;
|
|
23
|
+
fullName: string;
|
|
24
|
+
description: string;
|
|
25
|
+
inputSchema: Record<string, unknown>;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Catalog {
|
|
30
|
+
version: 1;
|
|
31
|
+
updatedAt: number;
|
|
32
|
+
servers: Record<string, CatalogServer>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function writeCatalog(
|
|
36
|
+
status: Record<string, McpServerStatus>,
|
|
37
|
+
tools: Map<string, DiscoveredTool[]>,
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
const servers: Record<string, CatalogServer> = {};
|
|
40
|
+
for (const [name, st] of Object.entries(status)) {
|
|
41
|
+
servers[name] = {
|
|
42
|
+
state: st.state,
|
|
43
|
+
tools: (tools.get(name) ?? []).map((t) => ({
|
|
44
|
+
rawName: t.rawName,
|
|
45
|
+
fullName: t.fullName,
|
|
46
|
+
description: t.description,
|
|
47
|
+
inputSchema: t.inputSchema,
|
|
48
|
+
})),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const catalog: Catalog = { version: 1, updatedAt: Date.now(), servers };
|
|
52
|
+
if (!existsSync(MCP_DIR)) await mkdir(MCP_DIR, { recursive: true });
|
|
53
|
+
const tmp = `${CATALOG_PATH}.tmp.${process.pid}.${Date.now().toString(36)}`;
|
|
54
|
+
await writeFile(tmp, `${JSON.stringify(catalog, null, 2)}\n`, 'utf8');
|
|
55
|
+
await rename(tmp, CATALOG_PATH);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Reader side (MCP child proxy mode). Missing/corrupt file → null; the
|
|
59
|
+
* proxy then exposes zero tools instead of failing the CLI turn. */
|
|
60
|
+
export async function readCatalog(): Promise<Catalog | null> {
|
|
61
|
+
try {
|
|
62
|
+
const raw = await readFile(CATALOG_PATH, 'utf8');
|
|
63
|
+
const parsed = JSON.parse(raw) as Catalog;
|
|
64
|
+
if (parsed?.version !== 1 || typeof parsed.servers !== 'object') return null;
|
|
65
|
+
return parsed;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
// Credential providers for the MCP hub (design §4.8). A provider yields
|
|
2
|
+
// the auth headers a server connection needs, resolved fresh at connect
|
|
3
|
+
// time. Two kinds today:
|
|
4
|
+
//
|
|
5
|
+
// - static: config `headers` with `${VAR}` env expansion (Phase 1).
|
|
6
|
+
// - oauth-refresh: read an access token from a login-written JSON file
|
|
7
|
+
// (e.g. `/design-login`'s `designOauth`), refresh it against the
|
|
8
|
+
// token endpoint when it nears expiry, and write the rotated token
|
|
9
|
+
// back. Cross-process safe via a lockfile so two agent turns don't
|
|
10
|
+
// both refresh (and clobber each other's rotated refresh_token).
|
|
11
|
+
//
|
|
12
|
+
// The credential file is NEVER config — it's operator-provisioned by an
|
|
13
|
+
// interactive login. The hub only reads/refreshes it.
|
|
14
|
+
|
|
15
|
+
import { existsSync } from 'node:fs';
|
|
16
|
+
import { open, readFile, rename, writeFile } from 'node:fs/promises';
|
|
17
|
+
import { homedir } from 'node:os';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import type { McpOAuthRefresh, McpServerConfig } from '../../config/types.ts';
|
|
20
|
+
import { logger } from '../../server/logger.ts';
|
|
21
|
+
import { expandEnvString } from './env-expand.ts';
|
|
22
|
+
|
|
23
|
+
export interface CredentialProvider {
|
|
24
|
+
/** Auth + static headers for a connection, resolved fresh (refreshing
|
|
25
|
+
* an OAuth token if it's near expiry). Throws on unrecoverable auth
|
|
26
|
+
* failure (missing file/key, refresh rejected). */
|
|
27
|
+
resolveHeaders(): Promise<Record<string, string>>;
|
|
28
|
+
/** True when the token handed out last is about to expire AND this
|
|
29
|
+
* provider is allowed to rotate it — the hub then reconnects early
|
|
30
|
+
* so the rotation happens before the upstream starts refusing the
|
|
31
|
+
* old bearer (a live streamable-http session keeps sending the
|
|
32
|
+
* token it connected with). Optional: static providers never rotate. */
|
|
33
|
+
refreshDue?(): boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function expandHome(p: string): string {
|
|
37
|
+
if (p === '~') return homedir();
|
|
38
|
+
if (p.startsWith('~/')) return join(homedir(), p.slice(2));
|
|
39
|
+
return p;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Static headers with env expansion — the Phase 1 behavior. */
|
|
43
|
+
export class StaticHeaderProvider implements CredentialProvider {
|
|
44
|
+
constructor(private headers: Record<string, string>) {}
|
|
45
|
+
resolveHeaders(): Promise<Record<string, string>> {
|
|
46
|
+
const out: Record<string, string> = {};
|
|
47
|
+
for (const [k, v] of Object.entries(this.headers)) {
|
|
48
|
+
out[k.toLowerCase()] = expandEnvString(v);
|
|
49
|
+
}
|
|
50
|
+
return Promise.resolve(out);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface OAuthCredential {
|
|
55
|
+
accessToken: string;
|
|
56
|
+
refreshToken?: string;
|
|
57
|
+
expiresAt?: number;
|
|
58
|
+
clientId?: string;
|
|
59
|
+
scopes?: string[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const REFRESH_SKEW_MS = 5 * 60 * 1000;
|
|
63
|
+
const LOCK_TIMEOUT_MS = 10_000;
|
|
64
|
+
const LOCK_RETRY_MS = 100;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* OAuth-login credential with self-refresh. Reads `credentialKey` from
|
|
68
|
+
* `credentialFile`, hands out `Authorization: Bearer <token>` merged
|
|
69
|
+
* with the server's static headers. Refreshes when the token is within
|
|
70
|
+
* REFRESH_SKEW_MS of expiry.
|
|
71
|
+
*/
|
|
72
|
+
export class OAuthRefreshProvider implements CredentialProvider {
|
|
73
|
+
private file: string;
|
|
74
|
+
private lockPath: string;
|
|
75
|
+
|
|
76
|
+
constructor(
|
|
77
|
+
private auth: McpOAuthRefresh,
|
|
78
|
+
private staticHeaders: Record<string, string>,
|
|
79
|
+
) {
|
|
80
|
+
this.file = expandHome(
|
|
81
|
+
auth.credentialFile ?? join(homedir(), '.somora', 'claude-home', '.credentials.json'),
|
|
82
|
+
);
|
|
83
|
+
// The lock is per FILE, not per key: two keys in one file are
|
|
84
|
+
// rewritten through the same read-modify-write.
|
|
85
|
+
this.lockPath = `${this.file}.${this.keys()[0]}.refresh.lock`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Candidate keys, in preference order. */
|
|
89
|
+
private keys(): string[] {
|
|
90
|
+
const k = this.auth.credentialKey;
|
|
91
|
+
return Array.isArray(k) ? k : [k];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Whether this provider owns the refresh chain of the key in use.
|
|
95
|
+
* `refresh: true` owns every key, a list owns exactly those keys,
|
|
96
|
+
* `false` owns none. */
|
|
97
|
+
private refreshAllowed(): boolean {
|
|
98
|
+
const r = this.auth.refresh;
|
|
99
|
+
if (typeof r === 'boolean') return r;
|
|
100
|
+
const key = this.usedKey ?? this.keys()[0]!;
|
|
101
|
+
return r.includes(key);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Expiry of the credential handed out last (ms epoch), for the
|
|
105
|
+
* hub's proactive rotation. Undefined until the first read. */
|
|
106
|
+
private lastExpiresAt: number | undefined;
|
|
107
|
+
|
|
108
|
+
refreshDue(): boolean {
|
|
109
|
+
if (!this.refreshAllowed()) return false;
|
|
110
|
+
if (this.lastExpiresAt === undefined) return false;
|
|
111
|
+
return this.lastExpiresAt - Date.now() < REFRESH_SKEW_MS;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async resolveHeaders(): Promise<Record<string, string>> {
|
|
115
|
+
let cred = await this.read();
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
if (cred.expiresAt !== undefined && cred.expiresAt - now < REFRESH_SKEW_MS) {
|
|
118
|
+
if (this.refreshAllowed()) {
|
|
119
|
+
cred = await this.refreshLocked(cred);
|
|
120
|
+
} else {
|
|
121
|
+
// Read-only credential: another process owns the refresh chain.
|
|
122
|
+
// Use what's on disk; if upstream rejects it the server parks
|
|
123
|
+
// as needs-auth and re-reads the file on the next probe — by
|
|
124
|
+
// then the owner has usually refreshed it.
|
|
125
|
+
logger.debug({
|
|
126
|
+
msg: 'mcp.hub.oauth_near_expiry_readonly',
|
|
127
|
+
key: this.activeKeyLabel(),
|
|
128
|
+
expiresInMs: cred.expiresAt - now,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
this.lastExpiresAt = cred.expiresAt;
|
|
133
|
+
const out: Record<string, string> = { authorization: `Bearer ${cred.accessToken}` };
|
|
134
|
+
for (const [k, v] of Object.entries(this.staticHeaders)) {
|
|
135
|
+
// Static headers win only if they're not the auth header.
|
|
136
|
+
if (k.toLowerCase() !== 'authorization') out[k.toLowerCase()] = expandEnvString(v);
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async read(): Promise<OAuthCredential> {
|
|
142
|
+
let raw: string;
|
|
143
|
+
try {
|
|
144
|
+
raw = await readFile(this.file, 'utf8');
|
|
145
|
+
} catch {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`MCP oauth credential file not found: ${this.file} — run the service's interactive login first`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
const doc = JSON.parse(raw) as Record<string, unknown>;
|
|
151
|
+
// First key that is actually there wins. Presence, not preference
|
|
152
|
+
// on paper: the preferred credential only exists once its login has
|
|
153
|
+
// been run, and falling through to the next one is what lets the
|
|
154
|
+
// same config work before and after that.
|
|
155
|
+
for (const key of this.keys()) {
|
|
156
|
+
const entry = doc[key] as OAuthCredential | undefined;
|
|
157
|
+
if (entry?.accessToken) {
|
|
158
|
+
if (key !== this.usedKey) {
|
|
159
|
+
logger.debug({ msg: 'mcp.hub.oauth_key_selected', key, file: this.file });
|
|
160
|
+
this.usedKey = key;
|
|
161
|
+
}
|
|
162
|
+
return entry;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const tried = this.keys().join(', ');
|
|
166
|
+
throw new Error(
|
|
167
|
+
`MCP oauth credential key${this.keys().length > 1 ? 's' : ''} "${tried}" missing/empty in ` +
|
|
168
|
+
`${this.file} — run the service's interactive login`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The key actually in use, or the configured list when nothing has
|
|
173
|
+
* been read yet — so a message never claims a credential it didn't
|
|
174
|
+
* touch. */
|
|
175
|
+
private activeKeyLabel(): string {
|
|
176
|
+
return this.usedKey ?? this.keys().join(' | ');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Which key the last read resolved to. Reported in errors so a
|
|
180
|
+
* rejection can be traced to the credential it used. */
|
|
181
|
+
private usedKey: string | null = null;
|
|
182
|
+
|
|
183
|
+
/** The credential this provider is currently speaking with. */
|
|
184
|
+
activeKey(): string | null {
|
|
185
|
+
return this.usedKey;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Refresh under a cross-process lockfile. If another process already
|
|
189
|
+
* refreshed (fresh token on re-read after acquiring the lock), use
|
|
190
|
+
* that instead of a second network round-trip. */
|
|
191
|
+
private async refreshLocked(stale: OAuthCredential): Promise<OAuthCredential> {
|
|
192
|
+
const release = await this.acquireLock();
|
|
193
|
+
try {
|
|
194
|
+
// Re-read: another holder may have refreshed while we waited.
|
|
195
|
+
const current = await this.read();
|
|
196
|
+
if (current.expiresAt !== undefined && current.expiresAt - Date.now() >= REFRESH_SKEW_MS) {
|
|
197
|
+
return current;
|
|
198
|
+
}
|
|
199
|
+
return await this.doRefresh(current.refreshToken ? current : stale);
|
|
200
|
+
} finally {
|
|
201
|
+
await release();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private async doRefresh(cred: OAuthCredential): Promise<OAuthCredential> {
|
|
206
|
+
if (!cred.refreshToken) {
|
|
207
|
+
throw new Error(`MCP oauth key "${this.activeKeyLabel()}" has no refresh_token — re-login required`);
|
|
208
|
+
}
|
|
209
|
+
const body = JSON.stringify({
|
|
210
|
+
grant_type: 'refresh_token',
|
|
211
|
+
refresh_token: cred.refreshToken,
|
|
212
|
+
...(cred.clientId ? { client_id: cred.clientId } : {}),
|
|
213
|
+
});
|
|
214
|
+
const res = await fetch(this.auth.tokenEndpoint, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
217
|
+
body,
|
|
218
|
+
});
|
|
219
|
+
if (!res.ok) {
|
|
220
|
+
const status = res.status;
|
|
221
|
+
if (status === 400 || status === 401) {
|
|
222
|
+
// The chain is dead: nothing will make this refresh_token work
|
|
223
|
+
// again. Move the entry aside so the interactive login can
|
|
224
|
+
// write a fresh one — Claude Code's `/design-login` refuses with
|
|
225
|
+
// "A design credential is already stored" while the dead key
|
|
226
|
+
// sits in the file, and nobody should have to hand-edit
|
|
227
|
+
// .credentials.json to get out of that (2026-08-31 report).
|
|
228
|
+
const retiredAs = await this.retireCredential();
|
|
229
|
+
throw new Error(
|
|
230
|
+
`MCP oauth refresh rejected (HTTP ${status}) for "${this.activeKeyLabel()}" — refresh token ` +
|
|
231
|
+
`expired/revoked. The stale entry was moved to "${retiredAs}" in ${this.file}; ` +
|
|
232
|
+
`run the service's interactive login again (Claude Design: ` +
|
|
233
|
+
`CLAUDE_CONFIG_DIR=~/.somora/claude-home claude → /design-login), then reconnect the server.`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
throw new Error(`MCP oauth refresh failed (HTTP ${status}) for "${this.activeKeyLabel()}"`);
|
|
237
|
+
}
|
|
238
|
+
const data = (await res.json()) as {
|
|
239
|
+
access_token?: string;
|
|
240
|
+
refresh_token?: string;
|
|
241
|
+
expires_in?: number;
|
|
242
|
+
scope?: string;
|
|
243
|
+
};
|
|
244
|
+
if (!data.access_token) {
|
|
245
|
+
throw new Error(`MCP oauth refresh returned no access_token for "${this.activeKeyLabel()}"`);
|
|
246
|
+
}
|
|
247
|
+
const updated: OAuthCredential = {
|
|
248
|
+
accessToken: data.access_token,
|
|
249
|
+
// Server rotates the refresh_token — persisting the new one is
|
|
250
|
+
// mandatory, the old one is invalidated after this call.
|
|
251
|
+
refreshToken: data.refresh_token ?? cred.refreshToken,
|
|
252
|
+
expiresAt: data.expires_in ? Date.now() + data.expires_in * 1000 : cred.expiresAt,
|
|
253
|
+
...(cred.clientId ? { clientId: cred.clientId } : {}),
|
|
254
|
+
scopes: data.scope ? data.scope.split(' ') : cred.scopes,
|
|
255
|
+
};
|
|
256
|
+
await this.writeBack(updated);
|
|
257
|
+
logger.info({
|
|
258
|
+
msg: 'mcp.hub.oauth_refreshed',
|
|
259
|
+
key: this.activeKeyLabel(),
|
|
260
|
+
expiresInSec: data.expires_in,
|
|
261
|
+
});
|
|
262
|
+
return updated;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Merge the refreshed credential back into the file, preserving every
|
|
266
|
+
* other top-level key (claudeAiOauth etc.). Atomic tmp+rename. */
|
|
267
|
+
private async writeBack(updated: OAuthCredential): Promise<void> {
|
|
268
|
+
await this.rewriteFile((doc) => {
|
|
269
|
+
doc[this.usedKey ?? this.keys()[0]!] = updated;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Rename the dead credential's key to `<key>_stale_<timestamp>` —
|
|
274
|
+
* kept for forensics, out of the login's way. Returns the new key.
|
|
275
|
+
* Called under the refresh lock. */
|
|
276
|
+
private async retireCredential(): Promise<string> {
|
|
277
|
+
const key = this.usedKey ?? this.keys()[0]!;
|
|
278
|
+
const retiredAs = `${key}_stale_${new Date().toISOString().replace(/[:.]/g, '-')}`;
|
|
279
|
+
await this.rewriteFile((doc) => {
|
|
280
|
+
if (doc[key] !== undefined) {
|
|
281
|
+
doc[retiredAs] = doc[key];
|
|
282
|
+
delete doc[key];
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
logger.warn({ msg: 'mcp.hub.oauth_credential_retired', key, retiredAs, file: this.file });
|
|
286
|
+
this.usedKey = null;
|
|
287
|
+
this.lastExpiresAt = undefined;
|
|
288
|
+
return retiredAs;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private async rewriteFile(mutate: (doc: Record<string, unknown>) => void): Promise<void> {
|
|
292
|
+
let doc: Record<string, unknown> = {};
|
|
293
|
+
try {
|
|
294
|
+
doc = JSON.parse(await readFile(this.file, 'utf8')) as Record<string, unknown>;
|
|
295
|
+
} catch {
|
|
296
|
+
// File vanished — recreate with just what mutate() adds.
|
|
297
|
+
}
|
|
298
|
+
mutate(doc);
|
|
299
|
+
const tmp = `${this.file}.hubtmp.${process.pid}`;
|
|
300
|
+
await writeFile(tmp, `${JSON.stringify(doc, null, 2)}\n`, { mode: 0o600 });
|
|
301
|
+
await rename(tmp, this.file);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private async acquireLock(): Promise<() => Promise<void>> {
|
|
305
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
306
|
+
for (;;) {
|
|
307
|
+
try {
|
|
308
|
+
const fh = await open(this.lockPath, 'wx');
|
|
309
|
+
await fh.close();
|
|
310
|
+
return async () => {
|
|
311
|
+
try {
|
|
312
|
+
await rename(this.lockPath, `${this.lockPath}.done`);
|
|
313
|
+
await (await import('node:fs/promises')).unlink(`${this.lockPath}.done`).catch(() => {});
|
|
314
|
+
} catch {
|
|
315
|
+
/* best effort */
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
} catch (err) {
|
|
319
|
+
if ((err as NodeJS.ErrnoException).code !== 'EEXIST') throw err;
|
|
320
|
+
if (Date.now() > deadline) {
|
|
321
|
+
// Stale lock: reclaim and proceed (best effort).
|
|
322
|
+
logger.warn({ msg: 'mcp.hub.oauth_lock_stale', key: this.activeKeyLabel() });
|
|
323
|
+
try {
|
|
324
|
+
await (await import('node:fs/promises')).unlink(this.lockPath);
|
|
325
|
+
} catch {
|
|
326
|
+
/* ignore */
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
await new Promise((r) => setTimeout(r, LOCK_RETRY_MS));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Preset expander: fills url/auth/headers for known services so the
|
|
336
|
+
* operator only writes `preset: claude-design`. Explicit config fields
|
|
337
|
+
* always override the preset. */
|
|
338
|
+
export function applyMcpPreset(name: string, cfg: McpServerConfig): McpServerConfig {
|
|
339
|
+
if (cfg.preset === 'claude-design') {
|
|
340
|
+
return {
|
|
341
|
+
...cfg,
|
|
342
|
+
url: cfg.url ?? 'https://api.anthropic.com/v1/design/mcp',
|
|
343
|
+
// Which credential authenticates this has now changed twice, so
|
|
344
|
+
// the preset names BOTH and takes whichever exists.
|
|
345
|
+
//
|
|
346
|
+
// until 2026-08-25 a separate `/design-login` credential
|
|
347
|
+
// (`designOauth`) — then that flow broke on
|
|
348
|
+
// recent CLI versions
|
|
349
|
+
// 2026-08-25 the ordinary `claudeAiOauth` login worked,
|
|
350
|
+
// verified against the live endpoint
|
|
351
|
+
// 2026-08-28 Anthropic split out a `user:design:*` scope.
|
|
352
|
+
// The ordinary token does not carry it and the
|
|
353
|
+
// endpoint answers 403 `needs_design_scopes`;
|
|
354
|
+
// `/design-login` is back and required.
|
|
355
|
+
// Measured both ways on the day: designOauth
|
|
356
|
+
// → 200 with tools, claudeAiOauth → 403.
|
|
357
|
+
//
|
|
358
|
+
// Listing both rather than flipping the constant means the next
|
|
359
|
+
// move in either direction costs nobody a config edit. `read()`
|
|
360
|
+
// takes the first key present in the file, and `designOauth` only
|
|
361
|
+
// exists once its login has been run.
|
|
362
|
+
//
|
|
363
|
+
// Refresh ownership, per key:
|
|
364
|
+
// claudeAiOauth — the CLI's. Two refreshers on one rotating
|
|
365
|
+
// chain invalidate each other; that is how a
|
|
366
|
+
// Design credential got revoked on 2026-08-25.
|
|
367
|
+
// designOauth — somora's, since 2026-08-31. Nothing else
|
|
368
|
+
// keeps it alive: the CLI only touches it when
|
|
369
|
+
// an interactive session in this config dir
|
|
370
|
+
// uses the Design MCP, so with somora as the
|
|
371
|
+
// only steady consumer the token simply expired
|
|
372
|
+
// every ~8 h and needed a manual /design-login
|
|
373
|
+
// (plus a hand-edit to get past "already
|
|
374
|
+
// stored"). The refresh runs under the file
|
|
375
|
+
// lock with a re-read, and the rotated token is
|
|
376
|
+
// written back for the CLI to pick up.
|
|
377
|
+
auth:
|
|
378
|
+
cfg.auth ??
|
|
379
|
+
({
|
|
380
|
+
type: 'oauth-refresh',
|
|
381
|
+
credentialKey: ['designOauth', 'claudeAiOauth'],
|
|
382
|
+
tokenEndpoint: 'https://platform.claude.com/v1/oauth/token',
|
|
383
|
+
refresh: ['designOauth'],
|
|
384
|
+
} as McpOAuthRefresh),
|
|
385
|
+
headers: {
|
|
386
|
+
'X-Anthropic-Client': 'claude-cli-design-tool',
|
|
387
|
+
...cfg.headers,
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return cfg;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export function buildCredentialProvider(cfg: McpServerConfig): CredentialProvider {
|
|
395
|
+
if (cfg.auth?.type === 'oauth-refresh') {
|
|
396
|
+
return new OAuthRefreshProvider(cfg.auth, cfg.headers);
|
|
397
|
+
}
|
|
398
|
+
return new StaticHeaderProvider(cfg.headers);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function assertHasUrl(name: string, cfg: McpServerConfig): string {
|
|
402
|
+
if (!cfg.url) {
|
|
403
|
+
throw new Error(`MCP server "${name}" has no url and no preset that supplies one`);
|
|
404
|
+
}
|
|
405
|
+
return cfg.url;
|
|
406
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// `${VAR}` / `${VAR:-default}` expansion for MCP config strings
|
|
2
|
+
// (claude-code idiom). Extracted so both the manager and the credential
|
|
3
|
+
// providers can use it without a circular import.
|
|
4
|
+
|
|
5
|
+
export class MissingEnvVarError extends Error {}
|
|
6
|
+
|
|
7
|
+
export function expandEnvString(input: string, env: NodeJS.ProcessEnv = process.env): string {
|
|
8
|
+
return input.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g, (_m, name: string, def?: string) => {
|
|
9
|
+
const v = env[name];
|
|
10
|
+
if (v !== undefined && v !== '') return v;
|
|
11
|
+
if (def !== undefined) return def;
|
|
12
|
+
throw new MissingEnvVarError(`missing env var \${${name}}`);
|
|
13
|
+
});
|
|
14
|
+
}
|