@cortex-agent/server 2026.10.15 → 2026.10.21
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/README.md +76 -50
- package/defaults/board/board.css +140 -62
- package/defaults/board/scaffold.html +36 -13
- package/defaults/config/environments/creative.json +0 -1
- package/defaults/config/environments/main.json +1 -2
- package/defaults/plugins/cortex-board/skills/board/SKILL.md +30 -22
- package/defaults/plugins/cortex-system/plugin.json +2 -2
- package/defaults/plugins/cortex-system/skills/browser/SKILL.md +56 -0
- package/dist/agent-adapter/claude/adapter.d.ts +2 -5
- package/dist/agent-adapter/claude/adapter.js +6 -5
- package/dist/agent-adapter/claude/assistant-blocks.d.ts +5 -0
- package/dist/agent-adapter/claude/assistant-blocks.js +61 -0
- package/dist/agent-adapter/claude/cache-refresh.d.ts +58 -0
- package/dist/agent-adapter/claude/cache-refresh.js +145 -0
- package/dist/agent-adapter/claude/cache-touch.d.ts +12 -0
- package/dist/agent-adapter/claude/cache-touch.js +75 -0
- package/dist/agent-adapter/claude/event-parser.d.ts +8 -3
- package/dist/agent-adapter/claude/event-parser.js +33 -17
- package/dist/agent-adapter/claude/event-translator.d.ts +2 -0
- package/dist/agent-adapter/claude/event-translator.js +2 -0
- package/dist/agent-adapter/claude/session-options.d.ts +6 -13
- package/dist/agent-adapter/claude/session-options.js +2 -13
- package/dist/agent-adapter/claude/spawn-args.d.ts +4 -21
- package/dist/agent-adapter/claude/spawn-args.js +17 -24
- package/dist/agent-adapter/claude/transcript-clear.d.ts +42 -0
- package/dist/agent-adapter/claude/transcript-clear.js +217 -0
- package/dist/agent-adapter/claude/turn-machine.d.ts +3 -2
- package/dist/agent-adapter/claude/turn-machine.js +21 -68
- package/dist/agent-adapter/claude/window-warmup.d.ts +13 -0
- package/dist/agent-adapter/claude/window-warmup.js +38 -0
- package/dist/agent-adapter/continuation-phase.js +3 -0
- package/dist/agent-adapter/normalize/event-types.d.ts +8 -0
- package/dist/agent-adapter/pi/adapter.d.ts +0 -1
- package/dist/agent-adapter/pi/adapter.js +5 -13
- package/dist/agent-adapter/pi/event-parser.d.ts +1 -1
- package/dist/agent-adapter/pi/event-parser.js +43 -27
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +1 -1
- package/dist/agent-adapter/pi/pi-session.d.ts +2 -0
- package/dist/agent-adapter/pi/pi-session.js +15 -0
- package/dist/agent-adapter/pi/providers-config.d.ts +2 -2
- package/dist/agent-adapter/pi/providers-config.js +1 -1
- package/dist/agent-adapter/pi/quota-sink.d.ts +3 -3
- package/dist/agent-adapter/pi/quota-sink.js +3 -3
- package/dist/agent-adapter/pi/runtime.js +6 -1
- package/dist/agent-adapter/pi/session-options.d.ts +9 -13
- package/dist/agent-adapter/pi/session-options.js +12 -25
- package/dist/agent-adapter/run-events.d.ts +11 -10
- package/dist/agent-adapter/run-events.js +2 -0
- package/dist/agent-adapter/types.d.ts +5 -4
- package/dist/client-bundle/manifest.json +2 -2
- package/dist/core/codex-quota.d.ts +1 -1
- package/dist/core/codex-quota.js +1 -1
- package/dist/core/gateway-generator.d.ts +9 -11
- package/dist/core/gateway-generator.js +31 -58
- package/dist/core/locales/en.d.ts +51 -77
- package/dist/core/locales/slices/commands.d.ts +26 -31
- package/dist/core/locales/slices/commands.js +54 -64
- package/dist/core/locales/slices/init.d.ts +8 -19
- package/dist/core/locales/slices/init.js +17 -53
- package/dist/core/locales/slices/interactions.d.ts +3 -3
- package/dist/core/locales/slices/interactions.js +6 -6
- package/dist/core/locales/slices/notices.d.ts +1 -3
- package/dist/core/locales/slices/notices.js +2 -6
- package/dist/core/locales/slices/scheduling.d.ts +9 -9
- package/dist/core/locales/slices/scheduling.js +18 -18
- package/dist/core/locales/slices/status.d.ts +0 -4
- package/dist/core/locales/slices/status.js +0 -8
- package/dist/core/locales/slices/tui.d.ts +0 -1
- package/dist/core/locales/slices/tui.js +0 -2
- package/dist/core/locales/slices/uimsg.d.ts +4 -7
- package/dist/core/locales/slices/uimsg.js +9 -15
- package/dist/core/models-generator.d.ts +29 -0
- package/dist/core/models-generator.js +82 -0
- package/dist/core/paths.d.ts +1 -1
- package/dist/core/paths.js +1 -1
- package/dist/core/settings-spec.d.ts +25 -0
- package/dist/core/settings-spec.js +34 -0
- package/dist/core/status-format.d.ts +3 -2
- package/dist/core/status-format.js +2 -2
- package/dist/core/types/environment-types.d.ts +0 -2
- package/dist/domain/agents/agent-state.d.ts +11 -56
- package/dist/domain/agents/agent-state.js +48 -115
- package/dist/domain/agents/agent-switch.d.ts +2 -24
- package/dist/domain/agents/agent-switch.js +9 -72
- package/dist/domain/agents/config.d.ts +11 -61
- package/dist/domain/agents/config.js +47 -154
- package/dist/domain/agents/engine-selection.d.ts +21 -0
- package/dist/domain/agents/engine-selection.js +37 -0
- package/dist/domain/agents/environments/registry.js +2 -1
- package/dist/domain/agents/environments/resolve.js +0 -1
- package/dist/domain/agents/environments/schema.d.ts +0 -1
- package/dist/domain/agents/environments/schema.js +0 -1
- package/dist/domain/agents/index.d.ts +2 -1
- package/dist/domain/agents/index.js +3 -7
- package/dist/domain/agents/model-selection.d.ts +18 -70
- package/dist/domain/agents/model-selection.js +100 -150
- package/dist/domain/agents/models.d.ts +12 -0
- package/dist/domain/agents/models.js +94 -0
- package/dist/domain/agents/subagent/registry.d.ts +2 -0
- package/dist/domain/agents/subagent/registry.js +8 -0
- package/dist/domain/agents/subagent/runner.d.ts +5 -5
- package/dist/domain/agents/subagent/runner.js +12 -35
- package/dist/domain/auth/auth-status.d.ts +4 -4
- package/dist/domain/auth/auth-status.js +17 -28
- package/dist/domain/auth/gateway-sync.d.ts +6 -26
- package/dist/domain/auth/gateway-sync.js +31 -75
- package/dist/domain/benchmark/identity.d.ts +3 -5
- package/dist/domain/benchmark/identity.js +0 -1
- package/dist/domain/benchmark/production-attempt-identity.d.ts +6 -5
- package/dist/domain/benchmark/production-attempt-identity.js +32 -35
- package/dist/domain/benchmark/session-evidence.d.ts +2 -1
- package/dist/domain/benchmark/session-evidence.js +8 -1
- package/dist/domain/browser/broker.d.ts +99 -0
- package/dist/domain/browser/broker.js +487 -0
- package/dist/domain/costs/claude-account.js +21 -0
- package/dist/domain/costs/rate-limit-throttle.js +3 -1
- package/dist/domain/costs/reset-account.d.ts +2 -0
- package/dist/domain/costs/usage-service.d.ts +1 -1
- package/dist/domain/costs/usage-service.js +20 -7
- package/dist/domain/mcp/tools/context.d.ts +5 -6
- package/dist/domain/mcp/tools/context.js +17 -3
- package/dist/domain/mcp/tools/subagent.js +1 -1
- package/dist/domain/mcp/tools/ui-file.js +1 -1
- package/dist/domain/pi-providers/custom-provider-model.d.ts +1 -1
- package/dist/domain/pi-providers/custom-provider-model.js +1 -1
- package/dist/domain/remote/device-browser.js +1 -1
- package/dist/domain/remote/device-chrome-commands.js +2 -0
- package/dist/domain/remote/device-port.js +2 -2
- package/dist/domain/runs/attempt.d.ts +3 -3
- package/dist/domain/runs/attempt.js +2 -3
- package/dist/domain/runs/builders.d.ts +4 -4
- package/dist/domain/runs/builders.js +4 -5
- package/dist/domain/runs/compact.d.ts +4 -17
- package/dist/domain/runs/compact.js +14 -56
- package/dist/domain/runs/config-resolver.d.ts +15 -83
- package/dist/domain/runs/config-resolver.js +24 -150
- package/dist/domain/runs/engine-spec.d.ts +8 -8
- package/dist/domain/runs/engine-spec.js +15 -31
- package/dist/domain/runs/events.d.ts +1 -1
- package/dist/domain/runs/fallback.d.ts +6 -38
- package/dist/domain/runs/fallback.js +10 -72
- package/dist/domain/runs/notices.d.ts +1 -11
- package/dist/domain/runs/notices.js +3 -30
- package/dist/domain/runs/request.d.ts +4 -3
- package/dist/domain/runs/retryable-errors.d.ts +0 -13
- package/dist/domain/runs/retryable-errors.js +0 -17
- package/dist/domain/runs/run.d.ts +12 -42
- package/dist/domain/runs/run.js +40 -147
- package/dist/domain/runs/service.d.ts +1 -1
- package/dist/domain/runs/service.js +8 -3
- package/dist/domain/runs/spec-loader.d.ts +1 -1
- package/dist/domain/runs/spend.d.ts +1 -1
- package/dist/domain/runs/spend.js +1 -1
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +3 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +25 -19
- package/dist/domain/scheduling/schedule-ops.d.ts +0 -1
- package/dist/domain/scheduling/schedule-ops.js +2 -5
- package/dist/domain/scheduling/schedule-selection.d.ts +3 -0
- package/dist/domain/scheduling/schedule-selection.js +16 -0
- package/dist/domain/scheduling/schedule-spec.d.ts +6 -5
- package/dist/domain/scheduling/schedule-spec.js +6 -6
- package/dist/domain/scheduling/scheduler.d.ts +3 -1
- package/dist/domain/scheduling/scheduler.js +22 -21
- package/dist/domain/sessions/session-hooks.d.ts +5 -4
- package/dist/domain/sessions/session-hooks.js +15 -33
- package/dist/domain/sessions/session-lifecycle.d.ts +19 -51
- package/dist/domain/sessions/session-lifecycle.js +34 -96
- package/dist/domain/system/doctor.d.ts +3 -3
- package/dist/domain/system/doctor.js +22 -26
- package/dist/domain/system/own-bin-path.d.ts +13 -0
- package/dist/domain/system/own-bin-path.js +36 -0
- package/dist/domain/system/terminal-cli/index.d.ts +58 -0
- package/dist/domain/system/terminal-cli/index.js +100 -0
- package/dist/domain/system/terminal-cli/posix-profiles.d.ts +23 -0
- package/dist/domain/system/terminal-cli/posix-profiles.js +74 -0
- package/dist/domain/system/terminal-cli/registrar.d.ts +12 -0
- package/dist/domain/system/terminal-cli/registrar.js +1 -0
- package/dist/domain/system/terminal-cli/shims.d.ts +27 -0
- package/dist/domain/system/terminal-cli/shims.js +108 -0
- package/dist/domain/system/terminal-cli/state.d.ts +7 -0
- package/dist/domain/system/terminal-cli/state.js +36 -0
- package/dist/domain/system/terminal-cli/windows-user-path.d.ts +13 -0
- package/dist/domain/system/terminal-cli/windows-user-path.js +67 -0
- package/dist/domain/tui-session/tui-session-service.js +1 -1
- package/dist/domain/tui-session/types.d.ts +2 -2
- package/dist/domain/ui-service/app-router.d.ts +49 -71
- package/dist/domain/ui-service/app-router.js +9 -13
- package/dist/domain/ui-service/assistant-snapshots.js +2 -0
- package/dist/domain/ui-service/contract.parity.d.ts +19 -0
- package/dist/domain/ui-service/contract.parity.js +1 -0
- package/dist/domain/ui-service/inline-media-types.d.ts +18 -0
- package/dist/domain/ui-service/inline-media-types.js +1 -0
- package/dist/domain/ui-service/input-schemas.d.ts +52 -115
- package/dist/domain/ui-service/input-schemas.js +38 -84
- package/dist/domain/ui-service/mutate/auth.d.ts +1 -1
- package/dist/domain/ui-service/mutate/auth.js +3 -2
- package/dist/domain/ui-service/mutate/config.d.ts +0 -8
- package/dist/domain/ui-service/mutate/config.js +0 -22
- package/dist/domain/ui-service/mutate/models.d.ts +13 -0
- package/dist/domain/ui-service/mutate/models.js +137 -0
- package/dist/domain/ui-service/mutate/resolve-media.d.ts +4 -0
- package/dist/domain/ui-service/mutate/resolve-media.js +17 -0
- package/dist/domain/ui-service/mutate/sessions.d.ts +2 -2
- package/dist/domain/ui-service/mutate/sessions.js +29 -126
- package/dist/domain/ui-service/query/config.js +22 -42
- package/dist/domain/ui-service/query/models.js +2 -2
- package/dist/domain/ui-service/query/schedules.js +1 -1
- package/dist/domain/ui-service/query/sessions.js +7 -13
- package/dist/domain/ui-service/types.d.ts +51 -177
- package/dist/domain/ui-service/ui-service.d.ts +23 -49
- package/dist/domain/ui-service/ui-service.js +8 -6
- package/dist/entry/app.js +42 -19
- package/dist/entry/browser-cli.d.ts +39 -0
- package/dist/entry/browser-cli.js +317 -0
- package/dist/entry/cli-help.d.ts +1 -0
- package/dist/entry/cli-help.js +37 -6
- package/dist/entry/cli.js +16 -14
- package/dist/entry/daemon.js +2 -0
- package/dist/entry/init-auth.js +1 -1
- package/dist/entry/init.d.ts +7 -8
- package/dist/entry/init.js +89 -249
- package/dist/entry/path-cli.d.ts +4 -0
- package/dist/entry/path-cli.js +65 -0
- package/dist/entry/schedule-cli.js +13 -6
- package/dist/entry/shutdown.d.ts +1 -1
- package/dist/entry/shutdown.js +8 -1
- package/dist/entry/start-ui-http.js +8 -0
- package/dist/entry/ui-service-wiring.js +8 -10
- package/dist/events/event-types.d.ts +3 -2
- package/dist/orchestration/agent-runner.d.ts +2 -2
- package/dist/orchestration/agent-runner.js +10 -9
- package/dist/orchestration/benchmark-session-webhook.js +13 -1
- package/dist/orchestration/browser-webhook.d.ts +11 -0
- package/dist/orchestration/browser-webhook.js +28 -0
- package/dist/orchestration/cache-keep-warm.d.ts +77 -0
- package/dist/orchestration/cache-keep-warm.js +213 -0
- package/dist/orchestration/claude-window-warmup.d.ts +40 -0
- package/dist/orchestration/claude-window-warmup.js +126 -0
- package/dist/orchestration/conversation-request.d.ts +6 -10
- package/dist/orchestration/conversation-request.js +5 -23
- package/dist/orchestration/delta-coalescer.d.ts +4 -2
- package/dist/orchestration/delta-coalescer.js +5 -5
- package/dist/orchestration/edit-retry.js +9 -11
- package/dist/orchestration/inline-media-eager.d.ts +13 -0
- package/dist/orchestration/inline-media-eager.js +58 -0
- package/dist/orchestration/inline-media.d.ts +22 -0
- package/dist/orchestration/inline-media.js +151 -0
- package/dist/orchestration/interactions/ask-user-resume.js +3 -7
- package/dist/orchestration/interactions/interaction-handlers.js +6 -6
- package/dist/orchestration/mid-turn-inject.d.ts +2 -1
- package/dist/orchestration/mid-turn-inject.js +1 -1
- package/dist/orchestration/pending-injection-recovery.d.ts +2 -1
- package/dist/orchestration/pending-injection-recovery.js +1 -1
- package/dist/orchestration/routing/commands/index.js +4 -4
- package/dist/orchestration/routing/commands/mode.d.ts +5 -44
- package/dist/orchestration/routing/commands/mode.js +98 -272
- package/dist/orchestration/routing/commands/schedule.js +29 -18
- package/dist/orchestration/routing/commands/session.js +9 -9
- package/dist/orchestration/routing/commands/status.js +1 -1
- package/dist/orchestration/routing/commands/usage-format.js +3 -0
- package/dist/orchestration/routing/webhook.js +4 -0
- package/dist/orchestration/session-compact.d.ts +0 -1
- package/dist/orchestration/session-compact.js +5 -7
- package/dist/orchestration/session-events.d.ts +2 -1
- package/dist/orchestration/session-events.js +1 -0
- package/dist/orchestration/status-helpers.d.ts +0 -4
- package/dist/orchestration/status-helpers.js +0 -18
- package/dist/orchestration/subagent-webhook.js +9 -34
- package/dist/orchestration/team-webhook.d.ts +8 -9
- package/dist/orchestration/team-webhook.js +16 -42
- package/dist/orchestration/transcript-sink.d.ts +6 -0
- package/dist/orchestration/transcript-sink.js +21 -2
- package/dist/orchestration/turn/terminal.js +7 -7
- package/dist/orchestration/turn/turn-prep.d.ts +0 -8
- package/dist/orchestration/turn/turn-prep.js +0 -51
- package/dist/orchestration/turn/turn-tracking.d.ts +2 -2
- package/dist/orchestration/turn/turn-tracking.js +8 -5
- package/dist/orchestration/turn/turn.d.ts +0 -2
- package/dist/orchestration/turn/turn.js +13 -15
- package/dist/platform/adapters/tui/ports.d.ts +1 -1
- package/dist/platform/adapters/tui/tui-transcript.js +2 -0
- package/dist/platform/browser/agent-browser.d.ts +50 -0
- package/dist/platform/browser/agent-browser.js +165 -0
- package/dist/platform/browser/managed-browser.d.ts +1 -11
- package/dist/platform/browser/managed-browser.js +4 -12
- package/dist/platform/ui-http/browser-stream.d.ts +110 -0
- package/dist/platform/ui-http/browser-stream.js +447 -0
- package/dist/platform/ui-http/ui-http-server.d.ts +12 -0
- package/dist/platform/ui-http/ui-http-server.js +23 -6
- package/dist/store/conversation-history-reader.js +8 -0
- package/dist/store/conversation-history-repo.d.ts +8 -1
- package/dist/store/conversation-history-repo.js +5 -0
- package/dist/store/conversation-ledger-repo.d.ts +6 -6
- package/dist/store/conversation-ledger-repo.js +4 -4
- package/dist/store/models-repo.d.ts +18 -0
- package/dist/store/models-repo.js +74 -0
- package/dist/store/pending-injection-repo.d.ts +2 -1
- package/dist/store/pending-injection-repo.js +3 -1
- package/dist/store/retire-profiles-migration.d.ts +28 -0
- package/dist/store/retire-profiles-migration.js +335 -0
- package/dist/store/schedule-repo.d.ts +2 -1
- package/dist/store/session-registry-import.js +3 -2
- package/dist/store/session-registry-journal.d.ts +10 -13
- package/dist/store/session-registry-journal.js +48 -25
- package/dist/store/session-registry-repo.d.ts +4 -4
- package/dist/store/session-registry-repo.js +4 -5
- package/dist/store/version-migrations.d.ts +2 -3
- package/dist/store/version-migrations.js +19 -3
- package/dist/tui/slash-commands.js +2 -2
- package/dist/tui/turn-status.js +1 -1
- package/package.json +6 -4
- package/web/dist/assets/board-math-css-CB4GJ6qO.js +2 -0
- package/web/dist/assets/index-CaSZPg_H.css +1 -0
- package/web/dist/assets/index-CzOR7Edh.js +522 -0
- package/web/dist/assets/{index-CndZZ1IQ.js → index-cIGFv27z.js} +1 -1
- package/web/dist/index.html +3 -2
- package/web/dist/theme.css +6 -0
- package/dist/agent-adapter/browser-mcp-server.d.ts +0 -13
- package/dist/agent-adapter/browser-mcp-server.js +0 -28
- package/dist/agent-adapter/claude/browser-mcp.d.ts +0 -9
- package/dist/agent-adapter/claude/browser-mcp.js +0 -10
- package/dist/core/profile-generator.d.ts +0 -79
- package/dist/core/profile-generator.js +0 -215
- package/dist/domain/agents/profile-manager.d.ts +0 -103
- package/dist/domain/agents/profile-manager.js +0 -286
- package/dist/domain/agents/profile-switch.d.ts +0 -49
- package/dist/domain/agents/profile-switch.js +0 -62
- package/dist/domain/ui-service/mutate/profiles.d.ts +0 -36
- package/dist/domain/ui-service/mutate/profiles.js +0 -151
- package/dist/store/profile-repo.d.ts +0 -20
- package/dist/store/profile-repo.js +0 -97
- package/web/dist/assets/index-B76ueW3q.js +0 -494
- package/web/dist/assets/index-C8O0yxg8.css +0 -1
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
import { profileRepo } from '../../store/profile-repo.js';
|
|
2
|
-
const PROFILE_NAME_RE = /^[a-zA-Z0-9_-]+$/;
|
|
3
|
-
const MODE_NAME_RE = /^[a-zA-Z0-9_-]+$/;
|
|
4
|
-
// provider shares mode's safe-name shape (alphanumeric, dash, underscore).
|
|
5
|
-
const PROVIDER_NAME_RE = /^[a-zA-Z0-9_-]+$/;
|
|
6
|
-
const ENV_KEY_RE = /^[A-Z_][A-Z0-9_]*$/;
|
|
7
|
-
const VALID_BACKENDS = new Set(['claude', 'pi']);
|
|
8
|
-
/** The one table of legal thinking levels per backend. Exported so the selection path (a composer
|
|
9
|
-
* picking a level for a session) and the Web catalog validate against the SAME set the profile
|
|
10
|
-
* validator enforces — two tables would drift the moment a backend gained a level. Ordered
|
|
11
|
-
* low → high because the UI renders them in this order. */
|
|
12
|
-
export const THINKING_LEVELS_BY_BACKEND = {
|
|
13
|
-
claude: ['low', 'medium', 'high', 'xhigh', 'max'],
|
|
14
|
-
pi: ['off', 'minimal', 'low', 'medium', 'high', 'xhigh'],
|
|
15
|
-
};
|
|
16
|
-
const THINKING_LEVEL_SETS = {
|
|
17
|
-
claude: new Set(THINKING_LEVELS_BY_BACKEND.claude),
|
|
18
|
-
pi: new Set(THINKING_LEVELS_BY_BACKEND.pi),
|
|
19
|
-
};
|
|
20
|
-
/** Is `level` a value this backend accepts? The selection path's gate, the profile validator's
|
|
21
|
-
* rule — one predicate. */
|
|
22
|
-
export function isValidThinkingLevel(backend, level) {
|
|
23
|
-
return THINKING_LEVEL_SETS[backend]?.has(level) ?? false;
|
|
24
|
-
}
|
|
25
|
-
/** The profiles object whose validation already passed. `profileRepo` caches the PARSE; the
|
|
26
|
-
* validation was left to run again on every call, and every caller of this module goes through
|
|
27
|
-
* here — a 300-session `sessions.list` revalidated the whole file 300 times per request. */
|
|
28
|
-
let validatedProfiles = null;
|
|
29
|
-
/**
|
|
30
|
-
* Identity, not a revision counter, is what makes this safe: the only path that can change the
|
|
31
|
-
* cached profiles — the hot-reload watcher's `invalidate` + `readSync` — installs a NEW object, so
|
|
32
|
-
* a stale verdict cannot outlive a change. A file that fails validation is never recorded, so it
|
|
33
|
-
* keeps throwing until it is fixed.
|
|
34
|
-
*/
|
|
35
|
-
function loadProfilesFile() {
|
|
36
|
-
try {
|
|
37
|
-
const data = profileRepo.readSync();
|
|
38
|
-
if (data !== validatedProfiles) {
|
|
39
|
-
validateProfilesFile(data);
|
|
40
|
-
validatedProfiles = data;
|
|
41
|
-
}
|
|
42
|
-
return data;
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
throw new Error(`Failed to load profiles.json: ${error.message}`);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function validateMaxOutputTokens(value, backend, label) {
|
|
49
|
-
if (value === undefined)
|
|
50
|
-
return;
|
|
51
|
-
if (backend !== 'pi' || !Number.isInteger(value) || Number(value) <= 0) {
|
|
52
|
-
throw new Error(`${label} has invalid maxOutputTokens for backend '${backend}'`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function validateProfileEntry(profile, label, inheritedBackend = 'claude') {
|
|
56
|
-
const p = profile;
|
|
57
|
-
if (!p || typeof p !== 'object' || Array.isArray(p)) {
|
|
58
|
-
throw new Error(`${label} must be an object`);
|
|
59
|
-
}
|
|
60
|
-
if (!p.model || typeof p.model !== 'string') {
|
|
61
|
-
throw new Error(`${label} must define a non-empty string model`);
|
|
62
|
-
}
|
|
63
|
-
if (p.backend !== undefined && !VALID_BACKENDS.has(p.backend)) {
|
|
64
|
-
throw new Error(`${label} has invalid backend: ${p.backend}`);
|
|
65
|
-
}
|
|
66
|
-
// Effective backend resolves to the explicit value, else the inherited one (primary → fallback).
|
|
67
|
-
const effectiveBackend = (typeof p.backend === 'string' ? p.backend : inheritedBackend);
|
|
68
|
-
// PI requires an explicit provider — no default, no fallback (the PI `--provider` / gateway
|
|
69
|
-
// endpoint group must be stated outright, never silently assumed).
|
|
70
|
-
if (effectiveBackend === 'pi' && p.provider === undefined) {
|
|
71
|
-
throw new Error(`${label} uses backend 'pi' and must declare an explicit provider (no default)`);
|
|
72
|
-
}
|
|
73
|
-
if (p.mode !== undefined) {
|
|
74
|
-
if (typeof p.mode !== 'string' || !MODE_NAME_RE.test(p.mode)) {
|
|
75
|
-
throw new Error(`${label} has invalid mode: ${p.mode}`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (p.provider !== undefined) {
|
|
79
|
-
if (typeof p.provider !== 'string' || !PROVIDER_NAME_RE.test(p.provider)) {
|
|
80
|
-
throw new Error(`${label} has invalid provider: ${String(p.provider)}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (p.extraEnv !== undefined) {
|
|
84
|
-
if (!p.extraEnv || typeof p.extraEnv !== 'object' || Array.isArray(p.extraEnv)) {
|
|
85
|
-
throw new Error(`${label} extraEnv must be a plain object`);
|
|
86
|
-
}
|
|
87
|
-
for (const [k, v] of Object.entries(p.extraEnv)) {
|
|
88
|
-
if (!ENV_KEY_RE.test(k)) {
|
|
89
|
-
throw new Error(`${label} extraEnv has invalid key: ${k}`);
|
|
90
|
-
}
|
|
91
|
-
if (typeof v !== 'string') {
|
|
92
|
-
throw new Error(`${label} extraEnv["${k}"] must be a string`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (p.extraOption !== undefined) {
|
|
97
|
-
if (!p.extraOption || typeof p.extraOption !== 'object' || Array.isArray(p.extraOption)) {
|
|
98
|
-
throw new Error(`${label} extraOption must be a plain object`);
|
|
99
|
-
}
|
|
100
|
-
for (const [k, v] of Object.entries(p.extraOption)) {
|
|
101
|
-
if (typeof k !== 'string' || !k.startsWith('--')) {
|
|
102
|
-
throw new Error(`${label} extraOption key must start with --: ${k}`);
|
|
103
|
-
}
|
|
104
|
-
if (typeof v !== 'string') {
|
|
105
|
-
throw new Error(`${label} extraOption["${k}"] must be a string`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (p.thinking !== undefined) {
|
|
110
|
-
const levels = THINKING_LEVELS_BY_BACKEND[effectiveBackend];
|
|
111
|
-
if (!levels) {
|
|
112
|
-
throw new Error(`${label} declares thinking but backend '${effectiveBackend}' does not support it`);
|
|
113
|
-
}
|
|
114
|
-
if (typeof p.thinking !== 'string' || !isValidThinkingLevel(effectiveBackend, p.thinking)) {
|
|
115
|
-
throw new Error(`${label} has invalid thinking: ${String(p.thinking)} (backend '${effectiveBackend}' expects one of: ${levels.join(', ')})`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
validateMaxOutputTokens(p.maxOutputTokens, effectiveBackend, label);
|
|
119
|
-
}
|
|
120
|
-
function validateProfilesFile(data) {
|
|
121
|
-
const d = data;
|
|
122
|
-
if (!d || typeof d !== 'object' || Array.isArray(d)) {
|
|
123
|
-
throw new Error('top-level value must be an object');
|
|
124
|
-
}
|
|
125
|
-
const { defaultProfile, profiles } = d;
|
|
126
|
-
if (!defaultProfile || typeof defaultProfile !== 'string') {
|
|
127
|
-
throw new Error('defaultProfile must be a non-empty string');
|
|
128
|
-
}
|
|
129
|
-
if (!profiles || typeof profiles !== 'object' || Array.isArray(profiles)) {
|
|
130
|
-
throw new Error('profiles must be an object');
|
|
131
|
-
}
|
|
132
|
-
const profs = profiles;
|
|
133
|
-
if (!profs[defaultProfile]) {
|
|
134
|
-
throw new Error(`defaultProfile "${defaultProfile}" is missing from profiles`);
|
|
135
|
-
}
|
|
136
|
-
const byBackend = d.defaultProfileByBackend;
|
|
137
|
-
if (byBackend !== undefined) {
|
|
138
|
-
if (!byBackend || typeof byBackend !== 'object' || Array.isArray(byBackend)) {
|
|
139
|
-
throw new Error('defaultProfileByBackend must be an object');
|
|
140
|
-
}
|
|
141
|
-
for (const [backend, name] of Object.entries(byBackend)) {
|
|
142
|
-
if (!VALID_BACKENDS.has(backend)) {
|
|
143
|
-
throw new Error(`defaultProfileByBackend has unknown backend: ${backend}`);
|
|
144
|
-
}
|
|
145
|
-
if (typeof name !== 'string' || !profs[name]) {
|
|
146
|
-
throw new Error(`defaultProfileByBackend.${backend} = "${String(name)}" is missing from profiles`);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
for (const [name, profile] of Object.entries(profs)) {
|
|
151
|
-
if (!PROFILE_NAME_RE.test(name)) {
|
|
152
|
-
throw new Error(`invalid profile name: ${name}`);
|
|
153
|
-
}
|
|
154
|
-
validateProfileEntry(profile, `profile "${name}"`);
|
|
155
|
-
const pe = profile;
|
|
156
|
-
const primaryBackend = (typeof pe.backend === 'string' ? pe.backend : 'claude');
|
|
157
|
-
if (pe.fallback !== undefined) {
|
|
158
|
-
if (!Array.isArray(pe.fallback)) {
|
|
159
|
-
throw new Error(`profile "${name}" fallback must be an array`);
|
|
160
|
-
}
|
|
161
|
-
pe.fallback.forEach((fb, i) => {
|
|
162
|
-
validateProfileEntry(fb, `profile "${name}" fallback[${i}]`, primaryBackend);
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
function listProfiles() {
|
|
168
|
-
const data = loadProfilesFile();
|
|
169
|
-
return Object.entries(data.profiles).map(([name, profile]) => ({ name, ...profile }));
|
|
170
|
-
}
|
|
171
|
-
function getDefaultProfileName() {
|
|
172
|
-
return loadProfilesFile().defaultProfile;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* The profile `!backend <name>` switches a channel to.
|
|
176
|
-
*
|
|
177
|
-
* `defaultProfileByBackend` wins when it names one; otherwise the FIRST profile declared for that
|
|
178
|
-
* backend, because declaration order in profiles.json is the only preference signal a user has
|
|
179
|
-
* already expressed. Returns null when the home has no profile on that backend at all — the caller
|
|
180
|
-
* must say so rather than silently switching to something else.
|
|
181
|
-
*/
|
|
182
|
-
export function getDefaultProfileForBackend(backend) {
|
|
183
|
-
let file;
|
|
184
|
-
try {
|
|
185
|
-
file = loadProfilesFile();
|
|
186
|
-
}
|
|
187
|
-
catch {
|
|
188
|
-
return null;
|
|
189
|
-
}
|
|
190
|
-
const declared = file.defaultProfileByBackend?.[backend];
|
|
191
|
-
if (declared && file.profiles[declared])
|
|
192
|
-
return declared;
|
|
193
|
-
for (const [name, profile] of Object.entries(file.profiles)) {
|
|
194
|
-
if ((profile.backend ?? 'claude') === backend)
|
|
195
|
-
return name;
|
|
196
|
-
}
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* The first profile declared for a (backend, provider) pair, if the home has one.
|
|
201
|
-
*
|
|
202
|
-
* Used when a selection moves to a provider the current profile does not use: that provider's own
|
|
203
|
-
* profile already states the gateway route (and the rest of the routing) the user configured for
|
|
204
|
-
* it, so borrowing from it beats guessing. Declaration order decides among several, the same
|
|
205
|
-
* preference signal `getDefaultProfileForBackend` reads.
|
|
206
|
-
*/
|
|
207
|
-
export function findProfileForProvider(backend, provider) {
|
|
208
|
-
let file;
|
|
209
|
-
try {
|
|
210
|
-
file = loadProfilesFile();
|
|
211
|
-
}
|
|
212
|
-
catch {
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
for (const [name, profile] of Object.entries(file.profiles)) {
|
|
216
|
-
if ((profile.backend ?? 'claude') !== backend)
|
|
217
|
-
continue;
|
|
218
|
-
if ((profile.provider ?? null) !== provider)
|
|
219
|
-
continue;
|
|
220
|
-
return { name, ...profile };
|
|
221
|
-
}
|
|
222
|
-
return null;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* The gateway route a selection on `provider` should run under.
|
|
226
|
-
*
|
|
227
|
-
* A profile already using that provider is authoritative; without one, `cortex init` names each PI
|
|
228
|
-
* endpoint after its provider (`mode = endpoint = provider`, see core/gateway-generator.ts), so the
|
|
229
|
-
* provider name is the route the generated gateway.yaml holds.
|
|
230
|
-
*/
|
|
231
|
-
export function resolveModeForProvider(backend, provider) {
|
|
232
|
-
return findProfileForProvider(backend, provider)?.mode || provider;
|
|
233
|
-
}
|
|
234
|
-
function getProfile(name) {
|
|
235
|
-
if (!name)
|
|
236
|
-
return null;
|
|
237
|
-
const data = loadProfilesFile();
|
|
238
|
-
const profile = data.profiles[name];
|
|
239
|
-
return profile ? { name, ...profile } : null;
|
|
240
|
-
}
|
|
241
|
-
function resolveProfile(name = null) {
|
|
242
|
-
const data = loadProfilesFile();
|
|
243
|
-
const resolvedName = name || data.defaultProfile;
|
|
244
|
-
const profile = data.profiles[resolvedName];
|
|
245
|
-
if (!profile) {
|
|
246
|
-
throw new Error(`Unknown profile: ${resolvedName}`);
|
|
247
|
-
}
|
|
248
|
-
return { name: resolvedName, ...profile };
|
|
249
|
-
}
|
|
250
|
-
function getProfileModel(name = null) {
|
|
251
|
-
return resolveProfile(name).model;
|
|
252
|
-
}
|
|
253
|
-
function resolveProfileConfig(name = null) {
|
|
254
|
-
const data = loadProfilesFile();
|
|
255
|
-
const resolvedName = name || data.defaultProfile;
|
|
256
|
-
const profile = data.profiles[resolvedName];
|
|
257
|
-
if (!profile) {
|
|
258
|
-
throw new Error(`Unknown profile: ${resolvedName}`);
|
|
259
|
-
}
|
|
260
|
-
const primary = {
|
|
261
|
-
model: profile.model,
|
|
262
|
-
backend: profile.backend || 'claude',
|
|
263
|
-
mode: profile.mode || null,
|
|
264
|
-
provider: profile.provider || null,
|
|
265
|
-
extraEnv: { ...(profile.extraEnv || {}) },
|
|
266
|
-
extraOption: { ...(profile.extraOption || {}) },
|
|
267
|
-
thinking: profile.thinking || null,
|
|
268
|
-
maxOutputTokens: profile.maxOutputTokens ?? null,
|
|
269
|
-
};
|
|
270
|
-
const fallback = (profile.fallback || []).map(fb => ({
|
|
271
|
-
model: fb.model,
|
|
272
|
-
backend: fb.backend || primary.backend,
|
|
273
|
-
mode: fb.mode || primary.mode,
|
|
274
|
-
// provider does NOT inherit from primary — pi entries must each declare it explicitly (enforced
|
|
275
|
-
// by validation), and non-pi entries leave it null.
|
|
276
|
-
provider: fb.provider || null,
|
|
277
|
-
extraEnv: { ...(fb.extraEnv || {}) },
|
|
278
|
-
extraOption: { ...(fb.extraOption || {}) },
|
|
279
|
-
// thinking does NOT inherit from primary (like provider) — value sets are backend-specific,
|
|
280
|
-
// so each entry must declare its own; undeclared → nothing is passed.
|
|
281
|
-
thinking: fb.thinking || null,
|
|
282
|
-
maxOutputTokens: fb.maxOutputTokens ?? null,
|
|
283
|
-
}));
|
|
284
|
-
return { name: resolvedName, ...primary, fallback };
|
|
285
|
-
}
|
|
286
|
-
export { loadProfilesFile, listProfiles, getDefaultProfileName, getProfile, resolveProfile, getProfileModel, resolveProfileConfig, validateProfilesFile, };
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
export type ProfileSwitchDecision = {
|
|
2
|
-
allowed: true;
|
|
3
|
-
backendChanged: boolean;
|
|
4
|
-
} | {
|
|
5
|
-
allowed: false;
|
|
6
|
-
reason: 'cross-backend-live-session';
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Pure switch policy shared by every surface (Slack / Feishu / Web).
|
|
10
|
-
*
|
|
11
|
-
* - No history (fresh session) → any switch is allowed, including across backends. `backendChanged`
|
|
12
|
-
* reports whether the backend moved (the caller may want it for messaging), but nothing is blocked.
|
|
13
|
-
* - Has history (live conversation) → only a same-backend switch is allowed. A cross-backend switch
|
|
14
|
-
* is rejected because the running conversation cannot be resumed on a different backend; the user
|
|
15
|
-
* must start a new session to change backend.
|
|
16
|
-
*/
|
|
17
|
-
export declare function decideProfileSwitch(opts: {
|
|
18
|
-
currentBackend: string;
|
|
19
|
-
targetBackend: string;
|
|
20
|
-
hasHistory: boolean;
|
|
21
|
-
}): ProfileSwitchDecision;
|
|
22
|
-
export type SwitchProfileFailure = 'unknown-profile' | 'cross-backend-live-session';
|
|
23
|
-
export interface SwitchProfileResult {
|
|
24
|
-
ok: boolean;
|
|
25
|
-
/** The requested profile name (echoed back). */
|
|
26
|
-
name: string;
|
|
27
|
-
/** The channel's effective backend before the switch (empty when the profile was unknown). */
|
|
28
|
-
currentBackend: string;
|
|
29
|
-
/** The target profile's backend (empty when the profile was unknown). */
|
|
30
|
-
targetBackend: string;
|
|
31
|
-
/** True when the switch moved to a different backend (only possible on a fresh session). */
|
|
32
|
-
backendChanged: boolean;
|
|
33
|
-
/** Present only when `ok` is false. */
|
|
34
|
-
reason?: SwitchProfileFailure;
|
|
35
|
-
}
|
|
36
|
-
/** Does the channel's session already carry conversation history? A fresh (just-created) session has
|
|
37
|
-
* zero turns; a live conversation has ≥1 turn. Read straight from the registry's channel turns. */
|
|
38
|
-
export declare function channelHasHistory(channel: string): Promise<boolean>;
|
|
39
|
-
/**
|
|
40
|
-
* Switch the active profile for a channel/session under the shared rule. Validates the profile,
|
|
41
|
-
* applies {@link decideProfileSwitch}, and on success sets the channel's active profile AND syncs the
|
|
42
|
-
* session-registry record's `profileName` (so `!resume` and the Web session list reflect it). Never
|
|
43
|
-
* resets the session — a same-backend switch keeps the conversation, the model changes on the next
|
|
44
|
-
* turn. Returns a structured result; the caller maps it to a reply / tRPC error.
|
|
45
|
-
*/
|
|
46
|
-
export declare function switchChannelProfile(opts: {
|
|
47
|
-
channel: string;
|
|
48
|
-
name: string;
|
|
49
|
-
}): Promise<SwitchProfileResult>;
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { sessionStore } from '../../store/session-registry-repo.js';
|
|
2
|
-
import { resolveBackendForChannel, setActiveProfile } from './config.js';
|
|
3
|
-
import { resolveProfile, resolveProfileConfig } from './profile-manager.js';
|
|
4
|
-
/**
|
|
5
|
-
* Pure switch policy shared by every surface (Slack / Feishu / Web).
|
|
6
|
-
*
|
|
7
|
-
* - No history (fresh session) → any switch is allowed, including across backends. `backendChanged`
|
|
8
|
-
* reports whether the backend moved (the caller may want it for messaging), but nothing is blocked.
|
|
9
|
-
* - Has history (live conversation) → only a same-backend switch is allowed. A cross-backend switch
|
|
10
|
-
* is rejected because the running conversation cannot be resumed on a different backend; the user
|
|
11
|
-
* must start a new session to change backend.
|
|
12
|
-
*/
|
|
13
|
-
export function decideProfileSwitch(opts) {
|
|
14
|
-
const backendChanged = opts.currentBackend !== opts.targetBackend;
|
|
15
|
-
if (opts.hasHistory && backendChanged) {
|
|
16
|
-
return { allowed: false, reason: 'cross-backend-live-session' };
|
|
17
|
-
}
|
|
18
|
-
return { allowed: true, backendChanged };
|
|
19
|
-
}
|
|
20
|
-
/** Does the channel's session already carry conversation history? A fresh (just-created) session has
|
|
21
|
-
* zero turns; a live conversation has ≥1 turn. Read straight from the registry's channel turns. */
|
|
22
|
-
export async function channelHasHistory(channel) {
|
|
23
|
-
return (await sessionStore.getTurns(channel)).length > 0;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Switch the active profile for a channel/session under the shared rule. Validates the profile,
|
|
27
|
-
* applies {@link decideProfileSwitch}, and on success sets the channel's active profile AND syncs the
|
|
28
|
-
* session-registry record's `profileName` (so `!resume` and the Web session list reflect it). Never
|
|
29
|
-
* resets the session — a same-backend switch keeps the conversation, the model changes on the next
|
|
30
|
-
* turn. Returns a structured result; the caller maps it to a reply / tRPC error.
|
|
31
|
-
*/
|
|
32
|
-
export async function switchChannelProfile(opts) {
|
|
33
|
-
const { channel, name } = opts;
|
|
34
|
-
let targetBackend;
|
|
35
|
-
try {
|
|
36
|
-
resolveProfile(name); // existence check — throws "Unknown profile: …"
|
|
37
|
-
targetBackend = resolveProfileConfig(name).backend;
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
return { ok: false, name, currentBackend: '', targetBackend: '', backendChanged: false, reason: 'unknown-profile' };
|
|
41
|
-
}
|
|
42
|
-
const currentBackend = resolveBackendForChannel(channel);
|
|
43
|
-
const hasHistory = await channelHasHistory(channel);
|
|
44
|
-
const decision = decideProfileSwitch({ currentBackend, targetBackend, hasHistory });
|
|
45
|
-
if (!decision.allowed) {
|
|
46
|
-
return { ok: false, name, currentBackend, targetBackend, backendChanged: true, reason: 'cross-backend-live-session' };
|
|
47
|
-
}
|
|
48
|
-
setActiveProfile(name, channel);
|
|
49
|
-
// Keep the session-registry record's profileName in lock-step (best-effort). A channel has
|
|
50
|
-
// ONE binding, so the lookup does not have to guess which backend it was filed under — the
|
|
51
|
-
// dance of "use currentBackend when the backend changed" is gone with the key.
|
|
52
|
-
// The cross-backend rule above is unchanged: a live conversation still cannot move.
|
|
53
|
-
try {
|
|
54
|
-
const session = await sessionStore.getById((await sessionStore.getBoundSessionId(channel)) ?? '');
|
|
55
|
-
if (session)
|
|
56
|
-
await sessionStore.updateSession(session.sessionId, { profileName: name });
|
|
57
|
-
}
|
|
58
|
-
catch {
|
|
59
|
-
// record sync is non-fatal — the channel profile (the runtime source of truth) is already set
|
|
60
|
-
}
|
|
61
|
-
return { ok: true, name, currentBackend, targetBackend, backendChanged: decision.backendChanged };
|
|
62
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { type ProfileEntry } from '../../agents/profile-manager.js';
|
|
2
|
-
import type { UiServiceDeps, Result, ProfileDraftInput, ProfilesCreateReturn, ProfilesUpdateReturn, ProfilesRemoveReturn, MutateArgs } from '../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* These handlers own the `profiles` map of profiles.json; `config.set {section:'profiles'}` still
|
|
5
|
-
* owns `defaultProfile`. Writes land through atomicWrite, which trips the profiles.json watcher
|
|
6
|
-
* (store/profile-repo.ts startProfileWatcher) — a running server picks the change up without a
|
|
7
|
-
* restart, exactly as a hand edit does.
|
|
8
|
-
*/
|
|
9
|
-
interface RawProfilesFile {
|
|
10
|
-
defaultProfile?: unknown;
|
|
11
|
-
profiles: Record<string, ProfileEntry>;
|
|
12
|
-
[key: string]: unknown;
|
|
13
|
-
}
|
|
14
|
-
/** Read the file whole so every field we do not manage (defaultProfile, any future key) survives. */
|
|
15
|
-
export declare function readProfilesFile(configDir: string): Promise<RawProfilesFile>;
|
|
16
|
-
export declare function writeProfilesFile(configDir: string, next: RawProfilesFile): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* Assemble the stored entry from the draft. Optional fields the draft omits are DROPPED (the draft
|
|
19
|
-
* is the complete desired state for everything the editor can express), while `extraEnv` and
|
|
20
|
-
* `fallback` are carried over from `preserved` — the UI cannot express either (extraEnv values are
|
|
21
|
-
* secret-bearing and never leave the server; fallback is a nested chain with no editor), so an edit
|
|
22
|
-
* must not silently delete them. Key order matches how init writes the file.
|
|
23
|
-
*/
|
|
24
|
-
export declare function buildProfileEntry(draft: ProfileDraftInput, preserved?: ProfileEntry): ProfileEntry;
|
|
25
|
-
export declare function createProfile(configDir: string, args: MutateArgs<'profiles.create'>): Promise<void>;
|
|
26
|
-
export declare function updateProfile(configDir: string, args: MutateArgs<'profiles.update'>): Promise<boolean>;
|
|
27
|
-
/**
|
|
28
|
-
* Deleting the profile `defaultProfile` points at would leave every agent start resolving a
|
|
29
|
-
* missing profile, and deleting the last one leaves nothing to resolve at all. Both are refused
|
|
30
|
-
* with the remedy in the message rather than written and then discovered at spawn time.
|
|
31
|
-
*/
|
|
32
|
-
export declare function removeProfile(configDir: string, args: MutateArgs<'profiles.remove'>): Promise<void>;
|
|
33
|
-
export declare function handleProfilesCreate(_deps: UiServiceDeps, args: MutateArgs<'profiles.create'>): Promise<Result<ProfilesCreateReturn>>;
|
|
34
|
-
export declare function handleProfilesUpdate(_deps: UiServiceDeps, args: MutateArgs<'profiles.update'>): Promise<Result<ProfilesUpdateReturn>>;
|
|
35
|
-
export declare function handleProfilesRemove(_deps: UiServiceDeps, args: MutateArgs<'profiles.remove'>): Promise<Result<ProfilesRemoveReturn>>;
|
|
36
|
-
export {};
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { t } from '../../../core/i18n.js';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import fs from 'node:fs/promises';
|
|
4
|
-
import { CONFIG_DIR } from '../../../core/paths.js';
|
|
5
|
-
import { atomicWrite } from '../../../core/atomic-write.js';
|
|
6
|
-
import { validateProfilesFile } from '../../agents/profile-manager.js';
|
|
7
|
-
function fail(code, message) {
|
|
8
|
-
return Object.assign(new Error(message), { code });
|
|
9
|
-
}
|
|
10
|
-
function profilesPath(configDir) {
|
|
11
|
-
return path.join(configDir, 'profiles.json');
|
|
12
|
-
}
|
|
13
|
-
/** Read the file whole so every field we do not manage (defaultProfile, any future key) survives. */
|
|
14
|
-
export async function readProfilesFile(configDir) {
|
|
15
|
-
let raw;
|
|
16
|
-
try {
|
|
17
|
-
raw = JSON.parse(await fs.readFile(profilesPath(configDir), 'utf8'));
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
throw fail('invalid-args', t('ui.profile.fileUnreadable'));
|
|
21
|
-
}
|
|
22
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw) || !raw.profiles
|
|
23
|
-
|| typeof raw.profiles !== 'object' || Array.isArray(raw.profiles)) {
|
|
24
|
-
throw fail('invalid-args', t('ui.profile.noProfilesMap'));
|
|
25
|
-
}
|
|
26
|
-
return raw;
|
|
27
|
-
}
|
|
28
|
-
export async function writeProfilesFile(configDir, next) {
|
|
29
|
-
await atomicWrite(profilesPath(configDir), JSON.stringify(next, null, 2) + '\n');
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Assemble the stored entry from the draft. Optional fields the draft omits are DROPPED (the draft
|
|
33
|
-
* is the complete desired state for everything the editor can express), while `extraEnv` and
|
|
34
|
-
* `fallback` are carried over from `preserved` — the UI cannot express either (extraEnv values are
|
|
35
|
-
* secret-bearing and never leave the server; fallback is a nested chain with no editor), so an edit
|
|
36
|
-
* must not silently delete them. Key order matches how init writes the file.
|
|
37
|
-
*/
|
|
38
|
-
export function buildProfileEntry(draft, preserved) {
|
|
39
|
-
const entry = { model: draft.model };
|
|
40
|
-
if (draft.backend !== undefined)
|
|
41
|
-
entry.backend = draft.backend;
|
|
42
|
-
if (draft.mode !== undefined)
|
|
43
|
-
entry.mode = draft.mode;
|
|
44
|
-
if (draft.provider !== undefined)
|
|
45
|
-
entry.provider = draft.provider;
|
|
46
|
-
if (preserved?.extraEnv !== undefined)
|
|
47
|
-
entry.extraEnv = preserved.extraEnv;
|
|
48
|
-
if (draft.extraOption !== undefined && Object.keys(draft.extraOption).length > 0) {
|
|
49
|
-
entry.extraOption = draft.extraOption;
|
|
50
|
-
}
|
|
51
|
-
if (draft.thinking !== undefined)
|
|
52
|
-
entry.thinking = draft.thinking;
|
|
53
|
-
if (preserved?.fallback !== undefined)
|
|
54
|
-
entry.fallback = preserved.fallback;
|
|
55
|
-
return entry;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Run the shipped file validator over a ONE-ENTRY probe file rather than the assembled result.
|
|
59
|
-
* Same code path — name charset, backend enum, the pi-needs-a-provider rule, per-backend thinking
|
|
60
|
-
* levels, extraEnv/extraOption shapes, and the fallback chain re-checked against the (possibly
|
|
61
|
-
* changed) primary backend — but scoped to the entry being written, so a pre-existing hand-broken
|
|
62
|
-
* neighbour entry cannot block editing an unrelated profile.
|
|
63
|
-
*/
|
|
64
|
-
function assertValidEntry(name, entry) {
|
|
65
|
-
try {
|
|
66
|
-
validateProfilesFile({ defaultProfile: name, profiles: { [name]: entry } });
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
throw fail('invalid-args', error instanceof Error ? error.message : String(error));
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export async function createProfile(configDir, args) {
|
|
73
|
-
const { name, ...draft } = args;
|
|
74
|
-
const raw = await readProfilesFile(configDir);
|
|
75
|
-
if (Object.prototype.hasOwnProperty.call(raw.profiles, name)) {
|
|
76
|
-
throw fail('invalid-args', t('ui.profile.exists', { name }));
|
|
77
|
-
}
|
|
78
|
-
const entry = buildProfileEntry(draft);
|
|
79
|
-
assertValidEntry(name, entry);
|
|
80
|
-
await writeProfilesFile(configDir, { ...raw, profiles: { ...raw.profiles, [name]: entry } });
|
|
81
|
-
}
|
|
82
|
-
export async function updateProfile(configDir, args) {
|
|
83
|
-
const { name, ...draft } = args;
|
|
84
|
-
const raw = await readProfilesFile(configDir);
|
|
85
|
-
const current = raw.profiles[name];
|
|
86
|
-
if (!current)
|
|
87
|
-
throw fail('not-found', t('ui.profile.notFound', { name }));
|
|
88
|
-
const entry = buildProfileEntry(draft, current);
|
|
89
|
-
assertValidEntry(name, entry);
|
|
90
|
-
const changed = JSON.stringify(entry) !== JSON.stringify(current);
|
|
91
|
-
if (changed) {
|
|
92
|
-
await writeProfilesFile(configDir, { ...raw, profiles: { ...raw.profiles, [name]: entry } });
|
|
93
|
-
}
|
|
94
|
-
return changed;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Deleting the profile `defaultProfile` points at would leave every agent start resolving a
|
|
98
|
-
* missing profile, and deleting the last one leaves nothing to resolve at all. Both are refused
|
|
99
|
-
* with the remedy in the message rather than written and then discovered at spawn time.
|
|
100
|
-
*/
|
|
101
|
-
export async function removeProfile(configDir, args) {
|
|
102
|
-
const { name } = args;
|
|
103
|
-
const raw = await readProfilesFile(configDir);
|
|
104
|
-
if (!Object.prototype.hasOwnProperty.call(raw.profiles, name)) {
|
|
105
|
-
throw fail('not-found', t('ui.profile.notFound', { name }));
|
|
106
|
-
}
|
|
107
|
-
if (raw.defaultProfile === name) {
|
|
108
|
-
throw fail('invalid-args', t('ui.profile.isDefault', { name }));
|
|
109
|
-
}
|
|
110
|
-
const rest = { ...raw.profiles };
|
|
111
|
-
delete rest[name];
|
|
112
|
-
if (Object.keys(rest).length === 0) {
|
|
113
|
-
throw fail('invalid-args', t('ui.profile.lastOne'));
|
|
114
|
-
}
|
|
115
|
-
await writeProfilesFile(configDir, { ...raw, profiles: rest });
|
|
116
|
-
}
|
|
117
|
-
/** Writer errors carry `code`; anything else is a genuine internal failure. */
|
|
118
|
-
function toErr(error) {
|
|
119
|
-
const code = error?.code;
|
|
120
|
-
return {
|
|
121
|
-
ok: false,
|
|
122
|
-
code: code === 'not-found' || code === 'invalid-args' ? code : 'internal',
|
|
123
|
-
message: error instanceof Error ? error.message : String(error),
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
export async function handleProfilesCreate(_deps, args) {
|
|
127
|
-
try {
|
|
128
|
-
await createProfile(CONFIG_DIR, args);
|
|
129
|
-
return { ok: true, data: { name: args.name } };
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
return toErr(error);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
export async function handleProfilesUpdate(_deps, args) {
|
|
136
|
-
try {
|
|
137
|
-
return { ok: true, data: { changed: await updateProfile(CONFIG_DIR, args) } };
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
return toErr(error);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
export async function handleProfilesRemove(_deps, args) {
|
|
144
|
-
try {
|
|
145
|
-
await removeProfile(CONFIG_DIR, args);
|
|
146
|
-
return { ok: true, data: { removed: true } };
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
return toErr(error);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { ProfilesFile } from '../domain/agents/profile-manager.js';
|
|
2
|
-
export declare const PROFILES_FILE: string;
|
|
3
|
-
export declare function getProfileConfigRevision(): number;
|
|
4
|
-
export declare class ProfileRepo {
|
|
5
|
-
private readonly _filePath;
|
|
6
|
-
private _syncCache;
|
|
7
|
-
constructor(filePath?: string);
|
|
8
|
-
/**
|
|
9
|
-
* Synchronous read for profile-manager.ts. First call reads from disk; subsequent
|
|
10
|
-
* calls serve from cache until invalidate() drops it.
|
|
11
|
-
*/
|
|
12
|
-
readSync(): ProfilesFile;
|
|
13
|
-
/** readSync(), except that a missing file — a home with no provider configured yet — is null. */
|
|
14
|
-
readIfPresent(): ProfilesFile | null;
|
|
15
|
-
/** Drop the in-memory cache so the next readSync() fetches from disk. */
|
|
16
|
-
invalidate(): void;
|
|
17
|
-
}
|
|
18
|
-
export declare const profileRepo: ProfileRepo;
|
|
19
|
-
export declare function setAdminNotifier(fn: (text: string) => void): void;
|
|
20
|
-
export declare function startProfileWatcher(repo?: ProfileRepo, filePath?: string, onReload?: () => void): () => void;
|