@cortex-agent/server 2026.5.19-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/defaults/.claude/settings.json +13 -0
- package/defaults/CORTEX.md +15 -0
- package/defaults/config/budget.json +4 -0
- package/defaults/config/session-hooks.json +7 -0
- package/defaults/config/thread-templates.json +528 -0
- package/defaults/context/CORTEX.md +40 -0
- package/defaults/context/decisions/DR-0012.md +54 -0
- package/defaults/context/ideas/CORTEX.md +10 -0
- package/defaults/context/projects/CORTEX.md +25 -0
- package/defaults/context/scans/CORTEX.md +13 -0
- package/defaults/context/user/CORTEX.md +21 -0
- package/defaults/data/schedules.json +52 -0
- package/defaults/gitignore +44 -0
- package/defaults/hooks/ask-user-question-hook.mjs +66 -0
- package/defaults/hooks/cortex-md-injector.mjs +237 -0
- package/defaults/hooks/exit-plan-mode-hook.mjs +84 -0
- package/defaults/hooks/memory-ref-tracker.mjs +154 -0
- package/defaults/hooks/new-session-hook.mjs +83 -0
- package/defaults/hooks/post-task-hook.mjs +63 -0
- package/defaults/hooks/rules-loader.mjs +212 -0
- package/defaults/hooks/sensitive-file-edit.mjs +84 -0
- package/defaults/hooks/session-activity-tracker.mjs +254 -0
- package/defaults/hooks/task-status-check.mjs +218 -0
- package/defaults/hooks/tasks-yaml-guard.mjs +230 -0
- package/defaults/plugins/cortex-coder/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-coder/skills/cli-standards/SKILL.md +208 -0
- package/defaults/plugins/cortex-coder/skills/code-standards/SKILL.md +146 -0
- package/defaults/plugins/cortex-coder/skills/develop/SKILL.md +164 -0
- package/defaults/plugins/cortex-common/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-common/skills/compound/SKILL.md +349 -0
- package/defaults/plugins/cortex-common/skills/compound-simple/SKILL.md +104 -0
- package/defaults/plugins/cortex-common/skills/critique/SKILL.md +88 -0
- package/defaults/plugins/cortex-common/skills/debug-campaign/SKILL.md +508 -0
- package/defaults/plugins/cortex-common/skills/diagnose/SKILL.md +169 -0
- package/defaults/plugins/cortex-common/skills/postmortem/SKILL.md +135 -0
- package/defaults/plugins/cortex-common/skills/solution-design/SKILL.md +183 -0
- package/defaults/plugins/cortex-common/skills/synthesize/SKILL.md +120 -0
- package/defaults/plugins/cortex-stage-gate/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-stage-gate/skills/need-approval/SKILL.md +89 -0
- package/defaults/plugins/cortex-stage-gate/skills/reorient/SKILL.md +163 -0
- package/defaults/plugins/cortex-stage-gate/skills/task/SKILL.md +425 -0
- package/defaults/plugins/cortex-system/.claude-plugin/plugin.json +5 -0
- package/defaults/plugins/cortex-system/skills/approval/SKILL.md +84 -0
- package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +199 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/SKILL.md +201 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/CORTEX.md +8 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/extract_session.py +165 -0
- package/defaults/plugins/cortex-system/skills/deep-retrospective/scripts/scan_logs.py +402 -0
- package/defaults/plugins/cortex-system/skills/evolve/SKILL.md +115 -0
- package/defaults/plugins/cortex-system/skills/feedback/SKILL.md +262 -0
- package/defaults/plugins/cortex-system/skills/gravity/SKILL.md +125 -0
- package/defaults/plugins/cortex-system/skills/orient/SKILL.md +310 -0
- package/defaults/plugins/cortex-system/skills/orient-project/SKILL.md +85 -0
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +167 -0
- package/defaults/plugins/cortex-system/skills/refresh-skills/SKILL.md +153 -0
- package/defaults/plugins/cortex-system/skills/schedule/SKILL.md +209 -0
- package/defaults/plugins/cortex-system/skills/thread/SKILL.md +478 -0
- package/defaults/plugins/cortex-system/skills/user-learn/SKILL.md +95 -0
- package/defaults/prompts/directives/coder-reviewer.md +106 -0
- package/defaults/prompts/directives/coder.md +90 -0
- package/defaults/prompts/directives/director.md +97 -0
- package/defaults/prompts/directives/doc-reviewer.md +93 -0
- package/defaults/prompts/directives/doc-writer.md +85 -0
- package/defaults/prompts/directives/executor-reviewer.md +96 -0
- package/defaults/prompts/directives/executor.md +85 -0
- package/defaults/prompts/directives/milestone-executor.md +119 -0
- package/defaults/prompts/directives/milestone-reviewer.md +108 -0
- package/defaults/prompts/directives/plan-executor.md +121 -0
- package/defaults/prompts/directives/plan-reviewer.md +99 -0
- package/defaults/prompts/systemPrompts/coder.md +64 -0
- package/defaults/prompts/systemPrompts/direct.md +92 -0
- package/defaults/prompts/systemPrompts/web-opus-4-6.md +68 -0
- package/defaults/prompts/systemPrompts/web-sonnet-4-6.md +76 -0
- package/defaults/prompts/systemPrompts/web.md +50 -0
- package/defaults/prompts/systemPrompts/worker.md +67 -0
- package/defaults/rules/correction-propagation.md +31 -0
- package/defaults/rules/decision-records.md +49 -0
- package/defaults/rules/experiment-format.md +165 -0
- package/defaults/rules/issues-md.md +37 -0
- package/defaults/rules/meta-conventions.md +45 -0
- package/defaults/rules/mission-md.md +47 -0
- package/defaults/rules/overview-md.md +41 -0
- package/defaults/rules/roadmap-md.md +55 -0
- package/defaults/rules/status-md.md +49 -0
- package/defaults/rules/task-system.md +53 -0
- package/dist/agent-adapter/capabilities.d.ts +12 -0
- package/dist/agent-adapter/capabilities.js +52 -0
- package/dist/agent-adapter/claude/adapter-tui.d.ts +140 -0
- package/dist/agent-adapter/claude/adapter-tui.js +422 -0
- package/dist/agent-adapter/claude/adapter.d.ts +85 -0
- package/dist/agent-adapter/claude/adapter.js +771 -0
- package/dist/agent-adapter/claude/cost-from-usage.d.ts +62 -0
- package/dist/agent-adapter/claude/cost-from-usage.js +97 -0
- package/dist/agent-adapter/claude/defaults.d.ts +30 -0
- package/dist/agent-adapter/claude/defaults.js +49 -0
- package/dist/agent-adapter/claude/event-parser.d.ts +28 -0
- package/dist/agent-adapter/claude/event-parser.js +173 -0
- package/dist/agent-adapter/claude/hooks-builder.d.ts +36 -0
- package/dist/agent-adapter/claude/hooks-builder.js +57 -0
- package/dist/agent-adapter/claude/jsonl-tail.d.ts +68 -0
- package/dist/agent-adapter/claude/jsonl-tail.js +344 -0
- package/dist/agent-adapter/claude/spawn-args.d.ts +38 -0
- package/dist/agent-adapter/claude/spawn-args.js +83 -0
- package/dist/agent-adapter/claude/tmux-control.d.ts +69 -0
- package/dist/agent-adapter/claude/tmux-control.js +127 -0
- package/dist/agent-adapter/claude/tool-summarizers.d.ts +2 -0
- package/dist/agent-adapter/claude/tool-summarizers.js +24 -0
- package/dist/agent-adapter/codex/adapter.d.ts +42 -0
- package/dist/agent-adapter/codex/adapter.js +916 -0
- package/dist/agent-adapter/codex/event-parser.d.ts +17 -0
- package/dist/agent-adapter/codex/event-parser.js +41 -0
- package/dist/agent-adapter/codex/spawn-args.d.ts +6 -0
- package/dist/agent-adapter/codex/spawn-args.js +30 -0
- package/dist/agent-adapter/index.d.ts +8 -0
- package/dist/agent-adapter/index.js +33 -0
- package/dist/agent-adapter/normalize/event-stream.d.ts +6 -0
- package/dist/agent-adapter/normalize/event-stream.js +47 -0
- package/dist/agent-adapter/normalize/event-types.d.ts +58 -0
- package/dist/agent-adapter/normalize/event-types.js +5 -0
- package/dist/agent-adapter/normalize/hooks.d.ts +26 -0
- package/dist/agent-adapter/normalize/hooks.js +5 -0
- package/dist/agent-adapter/normalize/prompt-builder.d.ts +6 -0
- package/dist/agent-adapter/normalize/prompt-builder.js +23 -0
- package/dist/agent-adapter/normalize/tool-names.d.ts +6 -0
- package/dist/agent-adapter/normalize/tool-names.js +73 -0
- package/dist/agent-adapter/pi/adapter.d.ts +60 -0
- package/dist/agent-adapter/pi/adapter.js +626 -0
- package/dist/agent-adapter/pi/agent-dir.d.ts +11 -0
- package/dist/agent-adapter/pi/agent-dir.js +34 -0
- package/dist/agent-adapter/pi/event-parser.d.ts +26 -0
- package/dist/agent-adapter/pi/event-parser.js +313 -0
- package/dist/agent-adapter/pi/framing.d.ts +6 -0
- package/dist/agent-adapter/pi/framing.js +35 -0
- package/dist/agent-adapter/pi/hook-bridge.d.ts +59 -0
- package/dist/agent-adapter/pi/hook-bridge.js +233 -0
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +26 -0
- package/dist/agent-adapter/pi/mcp-bridge.js +140 -0
- package/dist/agent-adapter/pi/pi-ext-types.d.ts +90 -0
- package/dist/agent-adapter/pi/pi-ext-types.js +5 -0
- package/dist/agent-adapter/pi/spawn-args.d.ts +20 -0
- package/dist/agent-adapter/pi/spawn-args.js +54 -0
- package/dist/agent-adapter/pi/tool-shims.d.ts +2 -0
- package/dist/agent-adapter/pi/tool-shims.js +181 -0
- package/dist/agent-adapter/types.d.ts +94 -0
- package/dist/agent-adapter/types.js +5 -0
- package/dist/core/async-mutex.d.ts +6 -0
- package/dist/core/async-mutex.js +36 -0
- package/dist/core/cli-utils.d.ts +37 -0
- package/dist/core/cli-utils.js +69 -0
- package/dist/core/config-generator.d.ts +9 -0
- package/dist/core/config-generator.js +58 -0
- package/dist/core/gateway-generator.d.ts +46 -0
- package/dist/core/gateway-generator.js +336 -0
- package/dist/core/log.d.ts +7 -0
- package/dist/core/log.js +103 -0
- package/dist/core/paths.d.ts +32 -0
- package/dist/core/paths.js +49 -0
- package/dist/core/profile-generator.d.ts +65 -0
- package/dist/core/profile-generator.js +310 -0
- package/dist/core/running-executions.d.ts +88 -0
- package/dist/core/running-executions.js +186 -0
- package/dist/core/status-format.d.ts +18 -0
- package/dist/core/status-format.js +31 -0
- package/dist/core/task-parser.d.ts +69 -0
- package/dist/core/task-parser.js +423 -0
- package/dist/core/types/agent-types.d.ts +31 -0
- package/dist/core/types/agent-types.js +5 -0
- package/dist/core/types/thread-types.d.ts +261 -0
- package/dist/core/types/thread-types.js +5 -0
- package/dist/core/utils.d.ts +29 -0
- package/dist/core/utils.js +98 -0
- package/dist/core/version.d.ts +3 -0
- package/dist/core/version.js +12 -0
- package/dist/domain/agents/config.d.ts +58 -0
- package/dist/domain/agents/config.js +313 -0
- package/dist/domain/agents/facade.d.ts +52 -0
- package/dist/domain/agents/facade.js +264 -0
- package/dist/domain/agents/index.d.ts +2 -0
- package/dist/domain/agents/index.js +4 -0
- package/dist/domain/agents/profile-manager.d.ts +54 -0
- package/dist/domain/agents/profile-manager.js +161 -0
- package/dist/domain/costs/codex-event-format.d.ts +4 -0
- package/dist/domain/costs/codex-event-format.js +66 -0
- package/dist/domain/costs/codex-usage-monitor.d.ts +7 -0
- package/dist/domain/costs/codex-usage-monitor.js +72 -0
- package/dist/domain/costs/cost-tracker.d.ts +115 -0
- package/dist/domain/costs/cost-tracker.js +327 -0
- package/dist/domain/costs/gateway-manager.d.ts +6 -0
- package/dist/domain/costs/gateway-manager.js +228 -0
- package/dist/domain/costs/rate-limit-parser.d.ts +11 -0
- package/dist/domain/costs/rate-limit-parser.js +87 -0
- package/dist/domain/costs/rate-limit-throttle.d.ts +32 -0
- package/dist/domain/costs/rate-limit-throttle.js +129 -0
- package/dist/domain/executions/registry.d.ts +20 -0
- package/dist/domain/executions/registry.js +124 -0
- package/dist/domain/mcp/core-server.d.ts +2 -0
- package/dist/domain/mcp/core-server.js +34 -0
- package/dist/domain/mcp/server.d.ts +3 -0
- package/dist/domain/mcp/server.js +78 -0
- package/dist/domain/mcp/tools/context.d.ts +23 -0
- package/dist/domain/mcp/tools/context.js +51 -0
- package/dist/domain/mcp/tools/cortex-md.d.ts +17 -0
- package/dist/domain/mcp/tools/cortex-md.js +36 -0
- package/dist/domain/mcp/tools/cost.d.ts +2 -0
- package/dist/domain/mcp/tools/cost.js +54 -0
- package/dist/domain/mcp/tools/executions.d.ts +2 -0
- package/dist/domain/mcp/tools/executions.js +94 -0
- package/dist/domain/mcp/tools/schedule.d.ts +23 -0
- package/dist/domain/mcp/tools/schedule.js +245 -0
- package/dist/domain/mcp/tools/slack.d.ts +21 -0
- package/dist/domain/mcp/tools/slack.js +124 -0
- package/dist/domain/mcp/tools/task-ops.d.ts +2 -0
- package/dist/domain/mcp/tools/task-ops.js +509 -0
- package/dist/domain/mcp/tools/tui-ask.d.ts +31 -0
- package/dist/domain/mcp/tools/tui-ask.js +108 -0
- package/dist/domain/mcp/tools/tui-plan.d.ts +28 -0
- package/dist/domain/mcp/tools/tui-plan.js +118 -0
- package/dist/domain/mcp/tui-server.d.ts +2 -0
- package/dist/domain/mcp/tui-server.js +69 -0
- package/dist/domain/memory/consolidate.d.ts +55 -0
- package/dist/domain/memory/consolidate.js +471 -0
- package/dist/domain/memory/cortex-md-injector.d.ts +30 -0
- package/dist/domain/memory/cortex-md-injector.js +123 -0
- package/dist/domain/memory/cortex-md-scanner.d.ts +9 -0
- package/dist/domain/memory/cortex-md-scanner.js +67 -0
- package/dist/domain/memory/index-regen.d.ts +19 -0
- package/dist/domain/memory/index-regen.js +398 -0
- package/dist/domain/memory/rules-loader.d.ts +27 -0
- package/dist/domain/memory/rules-loader.js +97 -0
- package/dist/domain/memory/skill-scanner.d.ts +5 -0
- package/dist/domain/memory/skill-scanner.js +132 -0
- package/dist/domain/memory/user-context.d.ts +5 -0
- package/dist/domain/memory/user-context.js +34 -0
- package/dist/domain/memory/watcher.d.ts +2 -0
- package/dist/domain/memory/watcher.js +81 -0
- package/dist/domain/monitor/disk-monitor.d.ts +20 -0
- package/dist/domain/monitor/disk-monitor.js +97 -0
- package/dist/domain/monitor/gpu-monitor.d.ts +26 -0
- package/dist/domain/monitor/gpu-monitor.js +135 -0
- package/dist/domain/remote/client-bootstrap.d.ts +1 -0
- package/dist/domain/remote/client-bootstrap.js +139 -0
- package/dist/domain/remote/client-hot-reload.d.ts +18 -0
- package/dist/domain/remote/client-hot-reload.js +142 -0
- package/dist/domain/remote/client-manager.d.ts +50 -0
- package/dist/domain/remote/client-manager.js +452 -0
- package/dist/domain/remote/cortex-client.d.ts +1 -0
- package/dist/domain/remote/cortex-client.js +704 -0
- package/dist/domain/scheduling/job-registry.d.ts +31 -0
- package/dist/domain/scheduling/job-registry.js +34 -0
- package/dist/domain/scheduling/jobs/_shared.d.ts +14 -0
- package/dist/domain/scheduling/jobs/_shared.js +45 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.d.ts +1 -0
- package/dist/domain/scheduling/jobs/memory-index-regen.js +16 -0
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +11 -0
- package/dist/domain/scheduling/jobs/scheduled-task.js +186 -0
- package/dist/domain/scheduling/jobs/target-dispatch.d.ts +41 -0
- package/dist/domain/scheduling/jobs/target-dispatch.js +48 -0
- package/dist/domain/scheduling/jobs/task-archive.d.ts +1 -0
- package/dist/domain/scheduling/jobs/task-archive.js +19 -0
- package/dist/domain/scheduling/jobs/task-dispatch.d.ts +12 -0
- package/dist/domain/scheduling/jobs/task-dispatch.js +214 -0
- package/dist/domain/scheduling/runner.d.ts +13 -0
- package/dist/domain/scheduling/runner.js +24 -0
- package/dist/domain/scheduling/schedule-cli.d.ts +16 -0
- package/dist/domain/scheduling/schedule-cli.js +268 -0
- package/dist/domain/scheduling/schedule-command.d.ts +4 -0
- package/dist/domain/scheduling/schedule-command.js +274 -0
- package/dist/domain/scheduling/scheduler.d.ts +86 -0
- package/dist/domain/scheduling/scheduler.js +544 -0
- package/dist/domain/sessions/session-backup.d.ts +49 -0
- package/dist/domain/sessions/session-backup.js +217 -0
- package/dist/domain/sessions/session-hooks.d.ts +103 -0
- package/dist/domain/sessions/session-hooks.js +311 -0
- package/dist/domain/sessions/session-registry.d.ts +21 -0
- package/dist/domain/sessions/session-registry.js +31 -0
- package/dist/domain/sessions/session.d.ts +3 -0
- package/dist/domain/sessions/session.js +14 -0
- package/dist/domain/tasks/archiver.d.ts +9 -0
- package/dist/domain/tasks/archiver.js +123 -0
- package/dist/domain/tasks/dispatch-utils.d.ts +37 -0
- package/dist/domain/tasks/dispatch-utils.js +143 -0
- package/dist/domain/tasks/dispatcher.d.ts +48 -0
- package/dist/domain/tasks/dispatcher.js +285 -0
- package/dist/domain/tasks/lint.d.ts +12 -0
- package/dist/domain/tasks/lint.js +102 -0
- package/dist/domain/tasks/mutator.d.ts +21 -0
- package/dist/domain/tasks/mutator.js +208 -0
- package/dist/domain/tasks/parser.d.ts +1 -0
- package/dist/domain/tasks/parser.js +6 -0
- package/dist/domain/tasks/pending-tracker.d.ts +67 -0
- package/dist/domain/tasks/pending-tracker.js +163 -0
- package/dist/domain/tasks/recommendation/extractor.d.ts +12 -0
- package/dist/domain/tasks/recommendation/extractor.js +563 -0
- package/dist/domain/tasks/recommendation/patterns.d.ts +11 -0
- package/dist/domain/tasks/recommendation/patterns.js +99 -0
- package/dist/domain/tasks/recommendation/types.d.ts +67 -0
- package/dist/domain/tasks/recommendation/types.js +5 -0
- package/dist/domain/tasks/store.d.ts +1 -0
- package/dist/domain/tasks/store.js +3 -0
- package/dist/domain/tasks/system/cortex-run.d.ts +24 -0
- package/dist/domain/tasks/system/cortex-run.js +342 -0
- package/dist/domain/tasks/system/task-cli.d.ts +49 -0
- package/dist/domain/tasks/system/task-cli.js +664 -0
- package/dist/domain/tasks/system/task-completion.d.ts +22 -0
- package/dist/domain/tasks/system/task-completion.js +89 -0
- package/dist/domain/tasks/system/task-id-utils.d.ts +24 -0
- package/dist/domain/tasks/system/task-id-utils.js +84 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.d.ts +34 -0
- package/dist/domain/tasks/system/task-lifecycle-edit.js +208 -0
- package/dist/domain/tasks/system/task-lock.d.ts +25 -0
- package/dist/domain/tasks/system/task-lock.js +139 -0
- package/dist/domain/tasks/system/task-mutations.d.ts +31 -0
- package/dist/domain/tasks/system/task-mutations.js +110 -0
- package/dist/domain/tasks/system/task-process.d.ts +37 -0
- package/dist/domain/tasks/system/task-process.js +119 -0
- package/dist/domain/tasks/system/task-state.d.ts +70 -0
- package/dist/domain/tasks/system/task-state.js +176 -0
- package/dist/domain/threads/artifact-io.d.ts +18 -0
- package/dist/domain/threads/artifact-io.js +182 -0
- package/dist/domain/threads/auto-thread.d.ts +3 -0
- package/dist/domain/threads/auto-thread.js +19 -0
- package/dist/domain/threads/hook-runner.d.ts +4 -0
- package/dist/domain/threads/hook-runner.js +250 -0
- package/dist/domain/threads/index.d.ts +5 -0
- package/dist/domain/threads/index.js +7 -0
- package/dist/domain/threads/prompt-builder.d.ts +25 -0
- package/dist/domain/threads/prompt-builder.js +176 -0
- package/dist/domain/threads/runner.d.ts +83 -0
- package/dist/domain/threads/runner.js +434 -0
- package/dist/domain/threads/state-machine.d.ts +61 -0
- package/dist/domain/threads/state-machine.js +440 -0
- package/dist/domain/threads/template-loader.d.ts +20 -0
- package/dist/domain/threads/template-loader.js +189 -0
- package/dist/domain/threads/template-resolver.d.ts +1 -0
- package/dist/domain/threads/template-resolver.js +95 -0
- package/dist/domain/threads/utils.d.ts +19 -0
- package/dist/domain/threads/utils.js +49 -0
- package/dist/entry/app.d.ts +1 -0
- package/dist/entry/app.js +234 -0
- package/dist/entry/cli.d.ts +12 -0
- package/dist/entry/cli.js +254 -0
- package/dist/entry/daemon.d.ts +11 -0
- package/dist/entry/daemon.js +521 -0
- package/dist/entry/init.d.ts +93 -0
- package/dist/entry/init.js +1299 -0
- package/dist/entry/startup-helpers.d.ts +2 -0
- package/dist/entry/startup-helpers.js +56 -0
- package/dist/entry/startup-notify.d.ts +10 -0
- package/dist/entry/startup-notify.js +20 -0
- package/dist/events/event-bus.d.ts +42 -0
- package/dist/events/event-bus.js +84 -0
- package/dist/events/event-logger.d.ts +12 -0
- package/dist/events/event-logger.js +135 -0
- package/dist/events/event-replay.d.ts +1 -0
- package/dist/events/event-replay.js +51 -0
- package/dist/events/event-types.d.ts +145 -0
- package/dist/events/event-types.js +5 -0
- package/dist/events/index.d.ts +5 -0
- package/dist/events/index.js +6 -0
- package/dist/hooks/ask-user-question-hook.mjs +66 -0
- package/dist/hooks/cortex-md-injector.mjs +237 -0
- package/dist/hooks/exit-plan-mode-hook.mjs +84 -0
- package/dist/hooks/memory-ref-tracker.mjs +154 -0
- package/dist/hooks/new-session-hook.mjs +83 -0
- package/dist/hooks/post-task-hook.mjs +63 -0
- package/dist/hooks/rules-loader.mjs +212 -0
- package/dist/hooks/sensitive-file-edit.mjs +84 -0
- package/dist/hooks/session-activity-tracker.mjs +254 -0
- package/dist/hooks/task-status-check.mjs +218 -0
- package/dist/hooks/tasks-yaml-guard.mjs +230 -0
- package/dist/orchestration/agent-runner.d.ts +72 -0
- package/dist/orchestration/agent-runner.js +289 -0
- package/dist/orchestration/busy-tracker.d.ts +12 -0
- package/dist/orchestration/busy-tracker.js +35 -0
- package/dist/orchestration/channel-queue.d.ts +18 -0
- package/dist/orchestration/channel-queue.js +32 -0
- package/dist/orchestration/dispatch-reconciler.d.ts +1 -0
- package/dist/orchestration/dispatch-reconciler.js +16 -0
- package/dist/orchestration/durable-helpers.d.ts +4 -0
- package/dist/orchestration/durable-helpers.js +42 -0
- package/dist/orchestration/interactions/ask-user-question.d.ts +32 -0
- package/dist/orchestration/interactions/ask-user-question.js +182 -0
- package/dist/orchestration/interactions/command-action-router.d.ts +47 -0
- package/dist/orchestration/interactions/command-action-router.js +91 -0
- package/dist/orchestration/interactions/interaction-handlers.d.ts +5 -0
- package/dist/orchestration/interactions/interaction-handlers.js +279 -0
- package/dist/orchestration/interactions/plan-approvals.d.ts +47 -0
- package/dist/orchestration/interactions/plan-approvals.js +77 -0
- package/dist/orchestration/interactions/plan-handler.d.ts +6 -0
- package/dist/orchestration/interactions/plan-handler.js +12 -0
- package/dist/orchestration/lifecycle.d.ts +50 -0
- package/dist/orchestration/lifecycle.js +308 -0
- package/dist/orchestration/orchestrator.d.ts +29 -0
- package/dist/orchestration/orchestrator.js +25 -0
- package/dist/orchestration/routing/commands/cancel.d.ts +10 -0
- package/dist/orchestration/routing/commands/cancel.js +136 -0
- package/dist/orchestration/routing/commands/channel.d.ts +11 -0
- package/dist/orchestration/routing/commands/channel.js +219 -0
- package/dist/orchestration/routing/commands/command-context.d.ts +29 -0
- package/dist/orchestration/routing/commands/command-context.js +4 -0
- package/dist/orchestration/routing/commands/cost.d.ts +3 -0
- package/dist/orchestration/routing/commands/cost.js +22 -0
- package/dist/orchestration/routing/commands/device.d.ts +6 -0
- package/dist/orchestration/routing/commands/device.js +83 -0
- package/dist/orchestration/routing/commands/dispatch.d.ts +2 -0
- package/dist/orchestration/routing/commands/dispatch.js +49 -0
- package/dist/orchestration/routing/commands/index.d.ts +15 -0
- package/dist/orchestration/routing/commands/index.js +130 -0
- package/dist/orchestration/routing/commands/mode.d.ts +13 -0
- package/dist/orchestration/routing/commands/mode.js +290 -0
- package/dist/orchestration/routing/commands/nvtop.d.ts +3 -0
- package/dist/orchestration/routing/commands/nvtop.js +103 -0
- package/dist/orchestration/routing/commands/orient.d.ts +2 -0
- package/dist/orchestration/routing/commands/orient.js +3 -0
- package/dist/orchestration/routing/commands/schedule.d.ts +4 -0
- package/dist/orchestration/routing/commands/schedule.js +132 -0
- package/dist/orchestration/routing/commands/sendfile.d.ts +2 -0
- package/dist/orchestration/routing/commands/sendfile.js +92 -0
- package/dist/orchestration/routing/commands/session.d.ts +9 -0
- package/dist/orchestration/routing/commands/session.js +153 -0
- package/dist/orchestration/routing/commands/status.d.ts +7 -0
- package/dist/orchestration/routing/commands/status.js +180 -0
- package/dist/orchestration/routing/commands/tail.d.ts +2 -0
- package/dist/orchestration/routing/commands/tail.js +73 -0
- package/dist/orchestration/routing/commands/task.d.ts +6 -0
- package/dist/orchestration/routing/commands/task.js +142 -0
- package/dist/orchestration/routing/commands/thread-handlers.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread-handlers.js +138 -0
- package/dist/orchestration/routing/commands/thread.d.ts +2 -0
- package/dist/orchestration/routing/commands/thread.js +2 -0
- package/dist/orchestration/routing/edit-handler.d.ts +24 -0
- package/dist/orchestration/routing/edit-handler.js +161 -0
- package/dist/orchestration/routing/file-handler.d.ts +6 -0
- package/dist/orchestration/routing/file-handler.js +23 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.d.ts +4 -0
- package/dist/orchestration/routing/hook-bridge-subscribers.js +79 -0
- package/dist/orchestration/routing/hook-bridge.d.ts +30 -0
- package/dist/orchestration/routing/hook-bridge.js +112 -0
- package/dist/orchestration/routing/message-router.d.ts +6 -0
- package/dist/orchestration/routing/message-router.js +70 -0
- package/dist/orchestration/routing/webhook.d.ts +10 -0
- package/dist/orchestration/routing/webhook.js +239 -0
- package/dist/orchestration/status-helpers.d.ts +66 -0
- package/dist/orchestration/status-helpers.js +222 -0
- package/dist/orchestration/superseded-edits.d.ts +22 -0
- package/dist/orchestration/superseded-edits.js +31 -0
- package/dist/orchestration/thread-executor.d.ts +31 -0
- package/dist/orchestration/thread-executor.js +234 -0
- package/dist/platform/adapter.d.ts +28 -0
- package/dist/platform/adapter.js +5 -0
- package/dist/platform/adapters/feishu.d.ts +66 -0
- package/dist/platform/adapters/feishu.js +558 -0
- package/dist/platform/adapters/index.d.ts +16 -0
- package/dist/platform/adapters/index.js +65 -0
- package/dist/platform/adapters/slack.d.ts +90 -0
- package/dist/platform/adapters/slack.js +630 -0
- package/dist/platform/index.d.ts +12 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/interactive-builder.d.ts +27 -0
- package/dist/platform/interactive-builder.js +120 -0
- package/dist/platform/testing.d.ts +100 -0
- package/dist/platform/testing.js +190 -0
- package/dist/platform/tool-trace.d.ts +33 -0
- package/dist/platform/tool-trace.js +165 -0
- package/dist/platform/types.d.ts +176 -0
- package/dist/platform/types.js +5 -0
- package/dist/platform/utils/rate-limiter.d.ts +92 -0
- package/dist/platform/utils/rate-limiter.js +213 -0
- package/dist/platform/virtual-message.d.ts +68 -0
- package/dist/platform/virtual-message.js +464 -0
- package/dist/store/atomic-write.d.ts +6 -0
- package/dist/store/atomic-write.js +18 -0
- package/dist/store/channel-repo.d.ts +21 -0
- package/dist/store/channel-repo.js +55 -0
- package/dist/store/conversation-ledger-repo.d.ts +86 -0
- package/dist/store/conversation-ledger-repo.js +220 -0
- package/dist/store/cost-repo.d.ts +57 -0
- package/dist/store/cost-repo.js +135 -0
- package/dist/store/execution-repo.d.ts +172 -0
- package/dist/store/execution-repo.js +343 -0
- package/dist/store/in-memory-repository.d.ts +14 -0
- package/dist/store/in-memory-repository.js +31 -0
- package/dist/store/json-repository.d.ts +51 -0
- package/dist/store/json-repository.js +118 -0
- package/dist/store/outbound-queue.d.ts +65 -0
- package/dist/store/outbound-queue.js +269 -0
- package/dist/store/profile-repo.d.ts +36 -0
- package/dist/store/profile-repo.js +144 -0
- package/dist/store/project-dir-repo.d.ts +22 -0
- package/dist/store/project-dir-repo.js +60 -0
- package/dist/store/schedule-repo.d.ts +92 -0
- package/dist/store/schedule-repo.js +88 -0
- package/dist/store/session-registry-repo.d.ts +38 -0
- package/dist/store/session-registry-repo.js +96 -0
- package/dist/store/session-repo.d.ts +14 -0
- package/dist/store/session-repo.js +56 -0
- package/dist/store/task-repo.d.ts +41 -0
- package/dist/store/task-repo.js +137 -0
- package/dist/store/thread-repo.d.ts +50 -0
- package/dist/store/thread-repo.js +190 -0
- package/package.json +58 -0
- package/scripts/postinstall-restart-trigger.mjs +39 -0
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
// input: AgentSpawnConfig, session keys, injectable spawner
|
|
2
|
+
// output: PIAdapter + PISession + switch_session API
|
|
3
|
+
// pos: PI CLI session pool and AgentAdapter implementation
|
|
4
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
5
|
+
import { spawn as defaultSpawn } from 'child_process';
|
|
6
|
+
import { mkdirSync } from 'fs';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
import { DATA_DIR, INSTALL_ROOT } from '../../core/utils.js';
|
|
9
|
+
import { createLogger } from '../../core/log.js';
|
|
10
|
+
import { CAPABILITIES_BY_BACKEND } from '../capabilities.js';
|
|
11
|
+
import { buildSpawnArgs } from './spawn-args.js';
|
|
12
|
+
import { createLineSplitter, encodeCommand } from './framing.js';
|
|
13
|
+
import { piRpcLineToNormalized, createPIEventParserState } from './event-parser.js';
|
|
14
|
+
import { PI_AGENT_DIR, PI_SESSIONS_DIR, writeAnthropicBaseUrl } from './agent-dir.js';
|
|
15
|
+
import { buildPrompt } from '../normalize/prompt-builder.js';
|
|
16
|
+
const log = createLogger('pi-adapter');
|
|
17
|
+
function buildPromptText(msg) {
|
|
18
|
+
return buildPrompt(msg.text, msg.attachments ?? []);
|
|
19
|
+
}
|
|
20
|
+
const DEFAULT_PI_BINARY = 'pi';
|
|
21
|
+
// DEFAULT_SESSION_DIR: sessionId.jsonl files are stored here (convention: <sessionDir>/<sessionId>.jsonl).
|
|
22
|
+
// Sessions stored under logs/sessions-pi/; PI_CODING_AGENT_DIR points to data/ for models.json.
|
|
23
|
+
const DEFAULT_SESSION_DIR = PI_SESSIONS_DIR;
|
|
24
|
+
// PI extensions point at compiled dist/.js files — PI's extension loader accepts both .ts and .js
|
|
25
|
+
// (pi-coding-agent dist/core/extensions/loader.js:367). Shipping .js avoids depending on src/ in
|
|
26
|
+
// the installed package (package.json#files only ships dist/ + defaults/).
|
|
27
|
+
const MCP_BRIDGE_PATH = path.join(INSTALL_ROOT, 'dist/agent-adapter/pi/mcp-bridge.js');
|
|
28
|
+
const TOOL_SHIMS_PATH = path.join(INSTALL_ROOT, 'dist/agent-adapter/pi/tool-shims.js');
|
|
29
|
+
const HOOK_BRIDGE_PATH = path.join(INSTALL_ROOT, 'dist/agent-adapter/pi/hook-bridge.js');
|
|
30
|
+
// Per FINDINGS.md §S1, pi rpc exits cleanly on stdin close; observed bootstrap cost ≈ 500 ms.
|
|
31
|
+
// 5 s is a safety margin; if pi does not exit in that window close() falls back to SIGTERM.
|
|
32
|
+
const CLOSE_EXIT_WAIT_MS = 5000;
|
|
33
|
+
// switch_session response timeout: if pi does not ack within this window, reject the pending promise.
|
|
34
|
+
const SWITCH_SESSION_TIMEOUT_MS = 5000;
|
|
35
|
+
const PI_IDLE_SESSION_TIMEOUT = 65 * 60 * 1000;
|
|
36
|
+
const PI_TURN_IDLE_TIMEOUT = 60 * 60 * 1000;
|
|
37
|
+
const PI_MAX_TIMEOUT = 30_000_000;
|
|
38
|
+
class EventQueue {
|
|
39
|
+
pending = [];
|
|
40
|
+
waiters = [];
|
|
41
|
+
closed = false;
|
|
42
|
+
push(evt) {
|
|
43
|
+
if (this.closed)
|
|
44
|
+
return;
|
|
45
|
+
const waiter = this.waiters.shift();
|
|
46
|
+
if (waiter)
|
|
47
|
+
waiter({ value: evt, done: false });
|
|
48
|
+
else
|
|
49
|
+
this.pending.push(evt);
|
|
50
|
+
}
|
|
51
|
+
next() {
|
|
52
|
+
const buffered = this.pending.shift();
|
|
53
|
+
if (buffered)
|
|
54
|
+
return Promise.resolve({ value: buffered, done: false });
|
|
55
|
+
if (this.closed)
|
|
56
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
this.waiters.push(resolve);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
close() {
|
|
62
|
+
if (this.closed)
|
|
63
|
+
return;
|
|
64
|
+
this.closed = true;
|
|
65
|
+
for (const w of this.waiters.splice(0)) {
|
|
66
|
+
w({ value: undefined, done: true });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
class PISession {
|
|
71
|
+
sessionKey;
|
|
72
|
+
/** Session ID assigned at bootstrap (immutable after first session_started). */
|
|
73
|
+
sessionId = null;
|
|
74
|
+
/** Absolute path to the session JSONL file (from bootstrap get_state.sessionFile). */
|
|
75
|
+
sessionFile = null;
|
|
76
|
+
/**
|
|
77
|
+
* Session currently active in the subprocess (updated on successful switch_session).
|
|
78
|
+
* Distinct from sessionId: sessionId is the bootstrapped session and never changes;
|
|
79
|
+
* currentSessionId tracks which session the subprocess is presently serving after any
|
|
80
|
+
* switch_session calls.
|
|
81
|
+
*/
|
|
82
|
+
currentSessionId = null;
|
|
83
|
+
proc;
|
|
84
|
+
events = new EventQueue();
|
|
85
|
+
splitter = createLineSplitter();
|
|
86
|
+
parserState = createPIEventParserState();
|
|
87
|
+
registry;
|
|
88
|
+
registrySessionDir;
|
|
89
|
+
onClose;
|
|
90
|
+
stderrTail = '';
|
|
91
|
+
alive = true;
|
|
92
|
+
exitPromise;
|
|
93
|
+
/** Buffer for assistant_text deltas; flushed on message_end / turn_complete / non-text events. */
|
|
94
|
+
textBuffer = '';
|
|
95
|
+
idleTimer = null;
|
|
96
|
+
turnIdleTimer = null;
|
|
97
|
+
maxTimer = null;
|
|
98
|
+
pendingSwitch = null;
|
|
99
|
+
pendingSwitchTimer = null;
|
|
100
|
+
/** Accumulator for the current in-flight turn. Resolved/rejected by handleRawLine. */
|
|
101
|
+
pendingTurn = null;
|
|
102
|
+
constructor(opts) {
|
|
103
|
+
this.sessionKey = opts.sessionKey;
|
|
104
|
+
this.registry = opts.registry;
|
|
105
|
+
this.registrySessionDir = opts.registrySessionDir;
|
|
106
|
+
this.onClose = opts.onClose;
|
|
107
|
+
this.proc = opts.spawner(DEFAULT_PI_BINARY, opts.cliArgs, {
|
|
108
|
+
cwd: opts.cwd,
|
|
109
|
+
env: opts.env,
|
|
110
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
111
|
+
});
|
|
112
|
+
this.proc.stdout?.on('data', (chunk) => {
|
|
113
|
+
for (const line of this.splitter.push(chunk))
|
|
114
|
+
this.handleRawLine(line);
|
|
115
|
+
});
|
|
116
|
+
this.proc.stderr?.on('data', (chunk) => {
|
|
117
|
+
const s = typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
118
|
+
this.stderrTail = (this.stderrTail + s).slice(-2000);
|
|
119
|
+
});
|
|
120
|
+
this.exitPromise = new Promise((resolve) => {
|
|
121
|
+
this.proc.once('close', (code) => {
|
|
122
|
+
this.alive = false;
|
|
123
|
+
// Reject any pending switch_session promise on unexpected subprocess exit.
|
|
124
|
+
if (this.pendingSwitch !== null) {
|
|
125
|
+
const entry = this.pendingSwitch;
|
|
126
|
+
this.pendingSwitch = null;
|
|
127
|
+
if (this.pendingSwitchTimer !== null) {
|
|
128
|
+
clearTimeout(this.pendingSwitchTimer);
|
|
129
|
+
this.pendingSwitchTimer = null;
|
|
130
|
+
}
|
|
131
|
+
entry.reject(new Error('pi subprocess exited while switch_session was pending'));
|
|
132
|
+
}
|
|
133
|
+
// Reject any pending turn promise if the process exits without a turn_complete.
|
|
134
|
+
if (this.pendingTurn !== null) {
|
|
135
|
+
const t = this.pendingTurn;
|
|
136
|
+
this.pendingTurn = null;
|
|
137
|
+
const msg = code !== null && code !== 0
|
|
138
|
+
? this.stderrTail || `pi exited with code ${code}`
|
|
139
|
+
: 'pi subprocess exited before turn_complete';
|
|
140
|
+
t.reject(new Error(msg));
|
|
141
|
+
}
|
|
142
|
+
if (code !== null && code !== 0) {
|
|
143
|
+
// Nice-to-have #1 from Plan Review iter1: surface abrupt failure as a single fatal error event
|
|
144
|
+
// so downstream consumers don't see a silent iterator termination. Full event-parser coverage is task a7f9.
|
|
145
|
+
this.events.push({
|
|
146
|
+
type: 'error',
|
|
147
|
+
message: this.stderrTail || `pi exited with code ${code}`,
|
|
148
|
+
fatal: true,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
this.events.close();
|
|
152
|
+
// Remove stream listeners and destroy streams so stub PassThrough streams
|
|
153
|
+
// (used in tests) don't keep the event loop alive after close.
|
|
154
|
+
this.proc.stdout?.removeAllListeners('data');
|
|
155
|
+
this.proc.stderr?.removeAllListeners('data');
|
|
156
|
+
try {
|
|
157
|
+
this.proc.stdout?.destroy?.();
|
|
158
|
+
}
|
|
159
|
+
catch { /* ignore */ }
|
|
160
|
+
try {
|
|
161
|
+
this.proc.stderr?.destroy?.();
|
|
162
|
+
}
|
|
163
|
+
catch { /* ignore */ }
|
|
164
|
+
resolve();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
// Send bootstrap frame. Must be the FIRST write. Any additional spawn-time writes would break the
|
|
168
|
+
// id='bootstrap' correlation invariant this skeleton relies on (see Plan Review iter1 nice-to-have #4).
|
|
169
|
+
this.proc.stdin?.write(encodeCommand({ id: 'bootstrap', type: 'get_state' }));
|
|
170
|
+
this.resetIdleTimer();
|
|
171
|
+
this.maxTimer = setTimeout(() => {
|
|
172
|
+
log.info(`Session ${this.sessionKey} hit max timeout, killing`);
|
|
173
|
+
this.kill();
|
|
174
|
+
this.onClose?.(this.sessionKey);
|
|
175
|
+
}, PI_MAX_TIMEOUT);
|
|
176
|
+
}
|
|
177
|
+
/** Flush buffered text as a single assistant_text event. */
|
|
178
|
+
flushTextBuffer() {
|
|
179
|
+
if (this.textBuffer.length > 0) {
|
|
180
|
+
this.events.push({ type: 'assistant_text', text: this.textBuffer });
|
|
181
|
+
this.textBuffer = '';
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
clearTimers() {
|
|
185
|
+
if (this.idleTimer) {
|
|
186
|
+
clearTimeout(this.idleTimer);
|
|
187
|
+
this.idleTimer = null;
|
|
188
|
+
}
|
|
189
|
+
if (this.turnIdleTimer) {
|
|
190
|
+
clearTimeout(this.turnIdleTimer);
|
|
191
|
+
this.turnIdleTimer = null;
|
|
192
|
+
}
|
|
193
|
+
if (this.maxTimer) {
|
|
194
|
+
clearTimeout(this.maxTimer);
|
|
195
|
+
this.maxTimer = null;
|
|
196
|
+
}
|
|
197
|
+
this.flushTextBuffer();
|
|
198
|
+
}
|
|
199
|
+
resetIdleTimer() {
|
|
200
|
+
if (this.idleTimer)
|
|
201
|
+
clearTimeout(this.idleTimer);
|
|
202
|
+
this.idleTimer = setTimeout(() => {
|
|
203
|
+
log.info(`Session ${this.sessionKey} idle for 65min, closing`);
|
|
204
|
+
this.close();
|
|
205
|
+
this.onClose?.(this.sessionKey);
|
|
206
|
+
}, PI_IDLE_SESSION_TIMEOUT);
|
|
207
|
+
}
|
|
208
|
+
startTurnIdleTimer() {
|
|
209
|
+
this.turnIdleTimer = setTimeout(() => {
|
|
210
|
+
log.info(`Session ${this.sessionKey} turn idle for 60min, killing`);
|
|
211
|
+
this.kill();
|
|
212
|
+
this.onClose?.(this.sessionKey);
|
|
213
|
+
}, PI_TURN_IDLE_TIMEOUT);
|
|
214
|
+
}
|
|
215
|
+
bumpTurnIdleTimer() {
|
|
216
|
+
if (!this.turnIdleTimer)
|
|
217
|
+
return;
|
|
218
|
+
clearTimeout(this.turnIdleTimer);
|
|
219
|
+
this.startTurnIdleTimer();
|
|
220
|
+
}
|
|
221
|
+
get eventsIterable() {
|
|
222
|
+
return {
|
|
223
|
+
[Symbol.asyncIterator]: () => ({
|
|
224
|
+
next: () => this.events.next(),
|
|
225
|
+
}),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
handleRawLine(line) {
|
|
229
|
+
if (line.length === 0)
|
|
230
|
+
return;
|
|
231
|
+
this.resetIdleTimer();
|
|
232
|
+
this.bumpTurnIdleTimer();
|
|
233
|
+
// Intercept switch_session response BEFORE event-parser: event-parser silently drops
|
|
234
|
+
// successful non-bootstrap responses (return []), so switch_session acks must be
|
|
235
|
+
// correlated here to resolve/reject the pending promise.
|
|
236
|
+
if (this.pendingSwitch !== null) {
|
|
237
|
+
let obj = null;
|
|
238
|
+
try {
|
|
239
|
+
obj = JSON.parse(line);
|
|
240
|
+
}
|
|
241
|
+
catch { /* fall through to event-parser */ }
|
|
242
|
+
if (obj && typeof obj === 'object') {
|
|
243
|
+
const r = obj;
|
|
244
|
+
if (r['type'] === 'response' &&
|
|
245
|
+
r['command'] === 'switch_session' &&
|
|
246
|
+
r['id'] === this.pendingSwitch.id) {
|
|
247
|
+
const entry = this.pendingSwitch;
|
|
248
|
+
this.pendingSwitch = null;
|
|
249
|
+
if (this.pendingSwitchTimer !== null) {
|
|
250
|
+
clearTimeout(this.pendingSwitchTimer);
|
|
251
|
+
this.pendingSwitchTimer = null;
|
|
252
|
+
}
|
|
253
|
+
const data = r['data'];
|
|
254
|
+
const cancelled = data && typeof data === 'object'
|
|
255
|
+
? Boolean(data['cancelled'])
|
|
256
|
+
: false;
|
|
257
|
+
entry.resolve({ ok: r['success'] === true, cancelled });
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
for (const evt of piRpcLineToNormalized(line, this.parserState)) {
|
|
263
|
+
if (evt.type === 'session_started' && this.sessionId === null) {
|
|
264
|
+
this.sessionId = evt.sessionId;
|
|
265
|
+
this.currentSessionId = evt.sessionId;
|
|
266
|
+
// Use sessionFile from bootstrap if available; fall back to legacy path guess.
|
|
267
|
+
if (evt.sessionFile) {
|
|
268
|
+
this.sessionFile = evt.sessionFile;
|
|
269
|
+
this.registry.set(evt.sessionId, evt.sessionFile);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
this.registry.set(evt.sessionId, path.join(this.registrySessionDir, `${evt.sessionId}.jsonl`));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// Accumulate turn result data for pending send() promise.
|
|
276
|
+
if (this.pendingTurn !== null) {
|
|
277
|
+
if (evt.type === 'plan_written') {
|
|
278
|
+
this.pendingTurn.planFilePath = evt.path;
|
|
279
|
+
}
|
|
280
|
+
else if (evt.type === 'ask_user_question') {
|
|
281
|
+
// Do NOT accumulate in pendingTurn.askUserQuestions: PI ask_user_question
|
|
282
|
+
// events are handled in real-time via the facade event loop → onAskUserQuestion
|
|
283
|
+
// callback → Slack interaction → sendExtensionUiResponse. Accumulating here
|
|
284
|
+
// would cause handleAgentSuccess to re-post the already-answered questions
|
|
285
|
+
// via sendMessages after turn_complete, leaving the agent stuck in
|
|
286
|
+
// "Waiting for user input" forever.
|
|
287
|
+
}
|
|
288
|
+
else if (evt.type === 'rate_limit') {
|
|
289
|
+
this.pendingTurn.rateLimited = true;
|
|
290
|
+
}
|
|
291
|
+
else if (evt.type === 'turn_complete') {
|
|
292
|
+
// Flush buffered text before resolving the turn so downstream sees all text.
|
|
293
|
+
this.flushTextBuffer();
|
|
294
|
+
if (this.turnIdleTimer) {
|
|
295
|
+
clearTimeout(this.turnIdleTimer);
|
|
296
|
+
this.turnIdleTimer = null;
|
|
297
|
+
}
|
|
298
|
+
const t = this.pendingTurn;
|
|
299
|
+
this.pendingTurn = null;
|
|
300
|
+
const result = {
|
|
301
|
+
sessionId: this.sessionId,
|
|
302
|
+
total_cost_usd: evt.totalCostUsd,
|
|
303
|
+
num_turns: evt.numTurns,
|
|
304
|
+
rateLimited: t.rateLimited,
|
|
305
|
+
rateLimitMessage: null,
|
|
306
|
+
planFilePath: t.planFilePath,
|
|
307
|
+
enteredPlanMode: false,
|
|
308
|
+
exitedPlanMode: t.planFilePath !== null,
|
|
309
|
+
askUserQuestions: t.askUserQuestions.length > 0 ? t.askUserQuestions : undefined,
|
|
310
|
+
finalOutput: null,
|
|
311
|
+
};
|
|
312
|
+
t.resolve(result);
|
|
313
|
+
}
|
|
314
|
+
else if (evt.type === 'error' && evt.fatal) {
|
|
315
|
+
this.flushTextBuffer();
|
|
316
|
+
if (this.turnIdleTimer) {
|
|
317
|
+
clearTimeout(this.turnIdleTimer);
|
|
318
|
+
this.turnIdleTimer = null;
|
|
319
|
+
}
|
|
320
|
+
const t = this.pendingTurn;
|
|
321
|
+
this.pendingTurn = null;
|
|
322
|
+
t.reject(new Error(evt.message));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Buffer assistant_text deltas until a message boundary (turn_progress from
|
|
326
|
+
// message_end) or a non-text event arrives, then flush as a single event.
|
|
327
|
+
// This matches Claude adapter behavior where onAssistantMessage fires once
|
|
328
|
+
// per complete assistant message block, not per token.
|
|
329
|
+
if (evt.type === 'assistant_text') {
|
|
330
|
+
this.textBuffer += evt.text;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
this.flushTextBuffer();
|
|
334
|
+
this.events.push(evt);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
send(msg) {
|
|
339
|
+
if (!this.alive)
|
|
340
|
+
throw new Error('PISession.send: subprocess is not alive');
|
|
341
|
+
this.proc.stdin?.write(encodeCommand({ type: 'prompt', message: buildPromptText(msg) }));
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Send switch_session RPC and await ack from pi.
|
|
345
|
+
* Returns {ok:false, cancelled:false} if subprocess is dead (no-op, no throw).
|
|
346
|
+
* Rejects if another switch is already pending (programming error).
|
|
347
|
+
*/
|
|
348
|
+
sendSwitchSession(targetPath) {
|
|
349
|
+
if (!this.alive)
|
|
350
|
+
return Promise.resolve({ ok: false, cancelled: false });
|
|
351
|
+
if (this.pendingSwitch !== null) {
|
|
352
|
+
return Promise.reject(new Error('PISession.sendSwitchSession: switch already pending'));
|
|
353
|
+
}
|
|
354
|
+
const id = `sw-${Date.now()}`;
|
|
355
|
+
return new Promise((resolve, reject) => {
|
|
356
|
+
this.pendingSwitch = { id, resolve, reject };
|
|
357
|
+
this.pendingSwitchTimer = setTimeout(() => {
|
|
358
|
+
if (this.pendingSwitch?.id === id) {
|
|
359
|
+
this.pendingSwitch = null;
|
|
360
|
+
this.pendingSwitchTimer = null;
|
|
361
|
+
reject(new Error(`PISession.sendSwitchSession: timeout after ${SWITCH_SESSION_TIMEOUT_MS}ms`));
|
|
362
|
+
}
|
|
363
|
+
}, SWITCH_SESSION_TIMEOUT_MS);
|
|
364
|
+
this.proc.stdin?.write(encodeCommand({ id, type: 'switch_session', sessionPath: targetPath }));
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Send a user message, auto-switching to targetSessionId first if the subprocess
|
|
369
|
+
* is currently serving a different session.
|
|
370
|
+
*
|
|
371
|
+
* BLOCKER-1 fix: prompt is written in both the switch and no-switch branches.
|
|
372
|
+
* BLOCKER-2 wire-up: spawn closure calls this instead of send() so auto-switch fires.
|
|
373
|
+
*/
|
|
374
|
+
async sendTurn(targetSessionId, targetPath, message) {
|
|
375
|
+
if (!this.alive)
|
|
376
|
+
throw new Error('PISession.sendTurn: subprocess is not alive');
|
|
377
|
+
const promptText = buildPromptText(message);
|
|
378
|
+
if (this.currentSessionId !== targetSessionId) {
|
|
379
|
+
if (targetPath === null) {
|
|
380
|
+
// Can't switch without a path; write prompt to current session as fallback.
|
|
381
|
+
this.proc.stdin?.write(encodeCommand({ type: 'prompt', message: promptText }));
|
|
382
|
+
return { switched: false, cancelled: false };
|
|
383
|
+
}
|
|
384
|
+
const result = await this.sendSwitchSession(targetPath);
|
|
385
|
+
if (result.ok) {
|
|
386
|
+
this.currentSessionId = targetSessionId;
|
|
387
|
+
}
|
|
388
|
+
// BLOCKER-1 fix: write prompt in every branch regardless of result.ok.
|
|
389
|
+
// NTH-A: if result.ok===false (pi rejected switch), the prompt goes to the current
|
|
390
|
+
// (un-switched) session — intentional best-effort, caller can inspect result.ok.
|
|
391
|
+
this.proc.stdin?.write(encodeCommand({ type: 'prompt', message: promptText }));
|
|
392
|
+
return { switched: result.ok, cancelled: result.cancelled };
|
|
393
|
+
}
|
|
394
|
+
// Same session: write prompt directly.
|
|
395
|
+
this.proc.stdin?.write(encodeCommand({ type: 'prompt', message: promptText }));
|
|
396
|
+
return { switched: false, cancelled: false };
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Send extension_ui_response for a pending extension_ui_request dialog.
|
|
400
|
+
* Call this after receiving ask_user_question NormalizedEvent to unblock the tool shim.
|
|
401
|
+
* Payload fields depend on the dialog method:
|
|
402
|
+
* select/input/editor: { value: string } or { cancelled: true }
|
|
403
|
+
* confirm: { confirmed: boolean } or { cancelled: true }
|
|
404
|
+
*/
|
|
405
|
+
sendExtensionUiResponse(id, payload) {
|
|
406
|
+
if (!this.alive)
|
|
407
|
+
return;
|
|
408
|
+
this.proc.stdin?.write(encodeCommand({ type: 'extension_ui_response', id, ...payload }));
|
|
409
|
+
}
|
|
410
|
+
/** Begin a new turn: set up the pendingTurn accumulator before writing the prompt.
|
|
411
|
+
* If a turn is already in-flight, reject it (superseded) before opening the new one
|
|
412
|
+
* so the orphaned Promise doesn't leak and keep the event loop alive. */
|
|
413
|
+
beginTurn(resolve, reject) {
|
|
414
|
+
// Reject any turn that was already in-flight before this one overwrites it.
|
|
415
|
+
// Without this, calling send() before the previous turn completes orphans the
|
|
416
|
+
// previous Promise, which holds a pending ref that keeps the event loop alive.
|
|
417
|
+
this.beginTurnReject(new Error('PISession.beginTurn: superseded by a newer send()'));
|
|
418
|
+
this.pendingTurn = {
|
|
419
|
+
resolve,
|
|
420
|
+
reject,
|
|
421
|
+
planFilePath: null,
|
|
422
|
+
askUserQuestions: [],
|
|
423
|
+
rateLimited: false,
|
|
424
|
+
numTurns: null,
|
|
425
|
+
totalCostUsd: null,
|
|
426
|
+
};
|
|
427
|
+
this.startTurnIdleTimer();
|
|
428
|
+
}
|
|
429
|
+
/** Belt-and-suspenders: reject the pendingTurn if it is still outstanding (i.e., not yet resolved by events). */
|
|
430
|
+
beginTurnReject(err) {
|
|
431
|
+
if (this.pendingTurn !== null) {
|
|
432
|
+
const t = this.pendingTurn;
|
|
433
|
+
this.pendingTurn = null;
|
|
434
|
+
t.reject(err);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
async close() {
|
|
438
|
+
this.clearTimers();
|
|
439
|
+
if (!this.alive)
|
|
440
|
+
return;
|
|
441
|
+
try {
|
|
442
|
+
this.proc.stdin?.end();
|
|
443
|
+
}
|
|
444
|
+
catch {
|
|
445
|
+
// best-effort
|
|
446
|
+
}
|
|
447
|
+
const timer = new Promise((resolve) => setTimeout(() => resolve('timeout'), CLOSE_EXIT_WAIT_MS));
|
|
448
|
+
const outcome = await Promise.race([this.exitPromise.then(() => 'exited'), timer]);
|
|
449
|
+
if (outcome === 'timeout' && this.alive) {
|
|
450
|
+
this.kill();
|
|
451
|
+
await this.exitPromise;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
kill() {
|
|
455
|
+
this.clearTimers();
|
|
456
|
+
if (!this.alive)
|
|
457
|
+
return false;
|
|
458
|
+
const ok = this.proc.kill('SIGTERM');
|
|
459
|
+
return ok;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
export class PIAdapter {
|
|
463
|
+
backend = 'pi';
|
|
464
|
+
capabilities = CAPABILITIES_BY_BACKEND.pi;
|
|
465
|
+
sessions = new Map();
|
|
466
|
+
spawner;
|
|
467
|
+
sessionPathRegistry = new Map();
|
|
468
|
+
/** sessionDir for the <sessionId>.jsonl path convention. Exposed for tests. */
|
|
469
|
+
sessionDir;
|
|
470
|
+
constructor(spawner = defaultSpawn, sessionDir = DEFAULT_SESSION_DIR) {
|
|
471
|
+
this.spawner = spawner;
|
|
472
|
+
this.sessionDir = sessionDir;
|
|
473
|
+
}
|
|
474
|
+
spawn(config) {
|
|
475
|
+
const sessionDir = this.sessionDir;
|
|
476
|
+
mkdirSync(sessionDir, { recursive: true });
|
|
477
|
+
// Resume: pass the session ID (UUID) directly to --session. PI scans the
|
|
478
|
+
// --session-dir and matches by filename or internal session `id` field, so we
|
|
479
|
+
// don't need to construct the exact file path (which may differ from the
|
|
480
|
+
// session UUID due to PI's internal session chaining).
|
|
481
|
+
//
|
|
482
|
+
// The sessionPathRegistry is populated below from the bootstrap session_started
|
|
483
|
+
// event for switchSession / sendTurn path lookups — it is NOT used for the
|
|
484
|
+
// --session CLI flag here.
|
|
485
|
+
const sessionIdForSpawn = (config.resume && config.sessionId) ? config.sessionId : null;
|
|
486
|
+
const cliArgs = buildSpawnArgs({
|
|
487
|
+
sessionDir,
|
|
488
|
+
sessionId: sessionIdForSpawn,
|
|
489
|
+
model: config.model ?? null,
|
|
490
|
+
systemPrompt: config.systemPrompt ?? null,
|
|
491
|
+
appendSystemPrompt: config.appendSystemPrompt ?? null,
|
|
492
|
+
pluginDirs: config.pluginDirs ?? null,
|
|
493
|
+
// All three extensions always injected: MCP bridge (task 5754) + tool shims (task 5b5c) + hook bridge (task d3ae).
|
|
494
|
+
extensionPaths: [MCP_BRIDGE_PATH, TOOL_SHIMS_PATH, HOOK_BRIDGE_PATH],
|
|
495
|
+
extraOption: config.extraOption ?? null,
|
|
496
|
+
});
|
|
497
|
+
// Write models.json before spawn so PI subprocess reads the correct baseUrl.
|
|
498
|
+
// PI adapter is the sole writer of this file — no other code path touches it.
|
|
499
|
+
if (config.anthropicBaseUrl) {
|
|
500
|
+
try {
|
|
501
|
+
writeAnthropicBaseUrl(config.anthropicBaseUrl);
|
|
502
|
+
}
|
|
503
|
+
catch (err) {
|
|
504
|
+
log.warn(`Failed to write models.json: ${err.message}`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const env = { ...process.env, ...(config.env ?? {}), PI_CODING_AGENT_DIR: PI_AGENT_DIR };
|
|
508
|
+
if (config.channel)
|
|
509
|
+
env.SLACK_CHANNEL = config.channel;
|
|
510
|
+
const cwd = config.cwd ?? DATA_DIR;
|
|
511
|
+
const session = new PISession({
|
|
512
|
+
sessionKey: config.sessionKey,
|
|
513
|
+
sessionDir,
|
|
514
|
+
cliArgs,
|
|
515
|
+
cwd,
|
|
516
|
+
env,
|
|
517
|
+
spawner: this.spawner,
|
|
518
|
+
registry: this.sessionPathRegistry,
|
|
519
|
+
registrySessionDir: sessionDir,
|
|
520
|
+
onClose: (key) => this.sessions.delete(key),
|
|
521
|
+
});
|
|
522
|
+
this.sessions.set(config.sessionKey, session);
|
|
523
|
+
return {
|
|
524
|
+
sessionKey: config.sessionKey,
|
|
525
|
+
get sessionId() {
|
|
526
|
+
return session.sessionId;
|
|
527
|
+
},
|
|
528
|
+
// BLOCKER-2 fix: route through session.sendTurn so auto-switch fires when subprocess
|
|
529
|
+
// was diverted to a different session via PIAdapter.switchSession().
|
|
530
|
+
// targetId = session.sessionId keeps this AgentProcess faithful to its bootstrapped session.
|
|
531
|
+
// AgentResult reconstruction (task 5b5c): beginTurn() sets up the accumulator; the
|
|
532
|
+
// pendingTurn promise is resolved/rejected by handleRawLine as events arrive.
|
|
533
|
+
send: (msg) => {
|
|
534
|
+
return new Promise((resolve, reject) => {
|
|
535
|
+
// Register accumulator BEFORE writing the prompt so no events are missed.
|
|
536
|
+
session.beginTurn(resolve, reject);
|
|
537
|
+
const targetId = session.sessionId;
|
|
538
|
+
if (targetId !== null) {
|
|
539
|
+
const targetPath = this.sessionPathRegistry.get(targetId) ?? null;
|
|
540
|
+
// sendTurn errors (e.g. switch_session timeout) surface via the events stream
|
|
541
|
+
// as a fatal error event, which will reject pendingTurn. The catch here is a
|
|
542
|
+
// belt-and-suspenders guard for programming errors in sendTurn itself.
|
|
543
|
+
session.sendTurn(targetId, targetPath, msg).catch((err) => {
|
|
544
|
+
// Only reject if pendingTurn is still ours (hasn't been resolved by events).
|
|
545
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
546
|
+
session.beginTurnReject(e);
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
// Bootstrap not yet complete; send directly (no switch possible yet).
|
|
551
|
+
session.send(msg);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
sendExtensionUiResponse: (id, payload) => {
|
|
556
|
+
session.sendExtensionUiResponse(id, payload);
|
|
557
|
+
},
|
|
558
|
+
events: session.eventsIterable,
|
|
559
|
+
close: async () => {
|
|
560
|
+
await session.close();
|
|
561
|
+
this.sessions.delete(config.sessionKey);
|
|
562
|
+
},
|
|
563
|
+
kill: () => {
|
|
564
|
+
const ok = session.kill();
|
|
565
|
+
if (ok)
|
|
566
|
+
this.sessions.delete(config.sessionKey);
|
|
567
|
+
return ok;
|
|
568
|
+
},
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Resolve the JSONL file path for a given PI session ID.
|
|
573
|
+
* Returns null if the session has not been registered yet (not spawned or bootstrap pending).
|
|
574
|
+
*/
|
|
575
|
+
resolveSessionPath(sessionId) {
|
|
576
|
+
return this.sessionPathRegistry.get(sessionId) ?? null;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Switch an existing subprocess (identified by onSessionKey) to serve a different PI session.
|
|
580
|
+
* Returns {ok:false, cancelled:false} if the session key or target session ID is unknown.
|
|
581
|
+
* NTH-1: onSessionKey routes the switch to the correct subprocess (spec done-when #1 omits it,
|
|
582
|
+
* but it is architecturally required for multi-session adapters).
|
|
583
|
+
*/
|
|
584
|
+
async switchSession(sessionId, onSessionKey) {
|
|
585
|
+
const session = this.sessions.get(onSessionKey);
|
|
586
|
+
if (!session)
|
|
587
|
+
return { ok: false, cancelled: false };
|
|
588
|
+
const targetPath = this.resolveSessionPath(sessionId);
|
|
589
|
+
if (targetPath === null)
|
|
590
|
+
return { ok: false, cancelled: false };
|
|
591
|
+
const result = await session.sendSwitchSession(targetPath);
|
|
592
|
+
if (result.ok)
|
|
593
|
+
session.currentSessionId = sessionId;
|
|
594
|
+
return result;
|
|
595
|
+
}
|
|
596
|
+
async close(sessionKey) {
|
|
597
|
+
const session = this.sessions.get(sessionKey);
|
|
598
|
+
if (!session)
|
|
599
|
+
return;
|
|
600
|
+
await session.close();
|
|
601
|
+
this.sessions.delete(sessionKey);
|
|
602
|
+
}
|
|
603
|
+
kill(sessionKey) {
|
|
604
|
+
const session = this.sessions.get(sessionKey);
|
|
605
|
+
if (!session)
|
|
606
|
+
return false;
|
|
607
|
+
const ok = session.kill();
|
|
608
|
+
if (ok)
|
|
609
|
+
this.sessions.delete(sessionKey);
|
|
610
|
+
return ok;
|
|
611
|
+
}
|
|
612
|
+
listSessions() {
|
|
613
|
+
return Array.from(this.sessions.keys());
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
export const _test = {
|
|
617
|
+
buildSpawnArgs,
|
|
618
|
+
encodeCommand,
|
|
619
|
+
createLineSplitter,
|
|
620
|
+
DEFAULT_SESSION_DIR,
|
|
621
|
+
MCP_BRIDGE_PATH,
|
|
622
|
+
TOOL_SHIMS_PATH,
|
|
623
|
+
HOOK_BRIDGE_PATH,
|
|
624
|
+
CLOSE_EXIT_WAIT_MS,
|
|
625
|
+
SWITCH_SESSION_TIMEOUT_MS,
|
|
626
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** PI_CODING_AGENT_DIR: PI reads models.json, auth.json from this dir. */
|
|
2
|
+
export declare const PI_AGENT_DIR: string;
|
|
3
|
+
export declare const PI_SESSIONS_DIR: string;
|
|
4
|
+
export declare const PI_MODELS_PATH: string;
|
|
5
|
+
/**
|
|
6
|
+
* Atomic-write models.json with anthropic provider config.
|
|
7
|
+
* Called by PIAdapter.spawn() — sole writer, no other code path touches this file.
|
|
8
|
+
* Uses a placeholder apiKey ("x") because the gateway replaces it with the real key.
|
|
9
|
+
*/
|
|
10
|
+
export declare function writeAnthropicBaseUrl(baseUrl: string): void;
|
|
11
|
+
export declare function ensurePIAgentDirs(): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// input: DATA_DIR
|
|
2
|
+
// output: PI_AGENT_DIR / PI_SESSIONS_DIR / PI_MODELS_PATH constants + writeAnthropicBaseUrl + ensurePIAgentDirs
|
|
3
|
+
// pos: PI agent directory management; models.json is written exclusively by PI adapter spawn
|
|
4
|
+
// layout: data/pi/models.json logs/sessions-pi/
|
|
5
|
+
// PI_CODING_AGENT_DIR → DATA_DIR/data/pi (PI reads models.json from here)
|
|
6
|
+
// >>> If I am updated, update my header comment and the parent folder's CORTEX.md <<<
|
|
7
|
+
import { mkdirSync, writeFileSync, renameSync } from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { DATA_DIR } from '../../core/utils.js';
|
|
10
|
+
/** PI_CODING_AGENT_DIR: PI reads models.json, auth.json from this dir. */
|
|
11
|
+
export const PI_AGENT_DIR = path.join(DATA_DIR, 'data', 'pi');
|
|
12
|
+
export const PI_SESSIONS_DIR = path.join(DATA_DIR, 'logs', 'sessions-pi');
|
|
13
|
+
export const PI_MODELS_PATH = path.join(PI_AGENT_DIR, 'models.json');
|
|
14
|
+
/**
|
|
15
|
+
* Atomic-write models.json with anthropic provider config.
|
|
16
|
+
* Called by PIAdapter.spawn() — sole writer, no other code path touches this file.
|
|
17
|
+
* Uses a placeholder apiKey ("x") because the gateway replaces it with the real key.
|
|
18
|
+
*/
|
|
19
|
+
export function writeAnthropicBaseUrl(baseUrl) {
|
|
20
|
+
const data = {
|
|
21
|
+
providers: {
|
|
22
|
+
anthropic: { baseUrl, apiKey: 'x' },
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const content = JSON.stringify(data, null, 2) + '\n';
|
|
26
|
+
// Atomic write: tmp + rename
|
|
27
|
+
const tmp = `${PI_MODELS_PATH}.tmp.${process.pid}.${Date.now()}`;
|
|
28
|
+
writeFileSync(tmp, content, 'utf8');
|
|
29
|
+
renameSync(tmp, PI_MODELS_PATH);
|
|
30
|
+
}
|
|
31
|
+
export function ensurePIAgentDirs() {
|
|
32
|
+
mkdirSync(PI_AGENT_DIR, { recursive: true });
|
|
33
|
+
mkdirSync(PI_SESSIONS_DIR, { recursive: true });
|
|
34
|
+
}
|