@clinebot/core 0.0.37 → 0.0.38
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 +11 -1
- package/dist/ClineCore.d.ts +18 -214
- package/dist/account/cline-account-service.d.ts +0 -1
- package/dist/account/index.d.ts +0 -1
- package/dist/account/rpc.d.ts +0 -1
- package/dist/account/types.d.ts +0 -1
- package/dist/auth/bounded-ttl-cache.d.ts +0 -1
- package/dist/auth/client.d.ts +0 -1
- package/dist/auth/cline.d.ts +0 -5
- package/dist/auth/codex.d.ts +0 -1
- package/dist/auth/oca.d.ts +0 -1
- package/dist/auth/server.d.ts +0 -1
- package/dist/auth/types.d.ts +0 -1
- package/dist/auth/utils.d.ts +0 -1
- package/dist/cline-core/automation.d.ts +34 -0
- package/dist/cline-core/runtime-services.d.ts +5 -0
- package/dist/cline-core/start-input.d.ts +10 -0
- package/dist/cline-core/telemetry.d.ts +10 -0
- package/dist/cline-core/types.d.ts +221 -0
- package/dist/cron/{cron-event-ingress.d.ts → events/cron-event-ingress.d.ts} +1 -2
- package/dist/cron/{cron-report-writer.d.ts → reports/cron-report-writer.d.ts} +1 -2
- package/dist/cron/{cron-materializer.d.ts → runner/cron-materializer.d.ts} +1 -2
- package/dist/cron/{cron-runner.d.ts → runner/cron-runner.d.ts} +6 -4
- package/dist/cron/{resource-limiter.d.ts → runner/resource-limiter.d.ts} +0 -1
- package/dist/cron/schedule/scheduler.d.ts +12 -0
- package/dist/cron/{cron-service.d.ts → service/cron-service.d.ts} +4 -5
- package/dist/cron/{schedule-command-service.d.ts → service/schedule-command-service.d.ts} +0 -1
- package/dist/cron/{schedule-service.d.ts → service/schedule-service.d.ts} +20 -22
- package/dist/cron/{cron-reconciler.d.ts → specs/cron-reconciler.d.ts} +1 -2
- package/dist/cron/{cron-spec-parser.d.ts → specs/cron-spec-parser.d.ts} +0 -1
- package/dist/cron/{cron-watcher.d.ts → specs/cron-watcher.d.ts} +0 -1
- package/dist/cron/{cron-schema.d.ts → store/cron-schema.d.ts} +0 -1
- package/dist/cron/{sqlite-cron-store.d.ts → store/sqlite-cron-store.d.ts} +12 -2
- package/dist/extensions/config/index.d.ts +4 -7
- package/dist/extensions/config/runtime-commands.d.ts +0 -1
- package/dist/extensions/config/skill-frontmatter-toggle.d.ts +11 -0
- package/dist/extensions/config/unified-config-file-watcher.d.ts +0 -1
- package/dist/extensions/config/user-instruction-config-loader.d.ts +0 -1
- package/dist/extensions/config/user-instruction-plugin.d.ts +25 -0
- package/dist/extensions/config/user-instruction-service.d.ts +23 -0
- package/dist/extensions/context/agentic-compaction.d.ts +0 -1
- package/dist/extensions/context/basic-compaction.d.ts +0 -1
- package/dist/extensions/context/compaction-shared.d.ts +0 -1
- package/dist/extensions/context/compaction.d.ts +0 -1
- package/dist/extensions/index.d.ts +0 -1
- package/dist/extensions/mcp/client.d.ts +0 -1
- package/dist/extensions/mcp/config-loader.d.ts +0 -1
- package/dist/extensions/mcp/index.d.ts +0 -1
- package/dist/extensions/mcp/manager.d.ts +0 -1
- package/dist/extensions/mcp/name-transform.d.ts +0 -1
- package/dist/extensions/mcp/policies.d.ts +0 -1
- package/dist/extensions/mcp/tools.d.ts +2 -3
- package/dist/extensions/mcp/types.d.ts +3 -4
- package/dist/extensions/plugin/plugin-config-loader.d.ts +0 -1
- package/dist/extensions/plugin/plugin-load-report.d.ts +0 -1
- package/dist/extensions/plugin/plugin-loader.d.ts +0 -1
- package/dist/extensions/plugin/plugin-module-import.d.ts +0 -1
- package/dist/extensions/plugin/plugin-sandbox.d.ts +0 -1
- package/dist/extensions/plugin/plugin-targeting.d.ts +0 -1
- package/dist/extensions/plugin-sandbox-bootstrap.js +1 -446
- package/dist/extensions/tools/constants.d.ts +0 -1
- package/dist/extensions/tools/definitions.d.ts +13 -14
- package/dist/extensions/tools/executors/apply-patch-parser.d.ts +0 -1
- package/dist/extensions/tools/executors/apply-patch.d.ts +0 -1
- package/dist/extensions/tools/executors/bash.d.ts +0 -1
- package/dist/extensions/tools/executors/editor.d.ts +0 -1
- package/dist/extensions/tools/executors/file-read.d.ts +0 -1
- package/dist/extensions/tools/executors/index.d.ts +0 -1
- package/dist/extensions/tools/executors/search.d.ts +0 -1
- package/dist/extensions/tools/executors/web-fetch.d.ts +0 -1
- package/dist/extensions/tools/helpers.d.ts +0 -1
- package/dist/extensions/tools/index.d.ts +2 -3
- package/dist/extensions/tools/model-tool-routing.d.ts +0 -1
- package/dist/extensions/tools/presets.d.ts +4 -5
- package/dist/extensions/tools/runtime.d.ts +0 -1
- package/dist/extensions/tools/schemas.d.ts +0 -1
- package/dist/extensions/tools/team/delegated-agent.d.ts +3 -4
- package/dist/extensions/tools/team/index.d.ts +0 -1
- package/dist/extensions/tools/team/multi-agent.d.ts +1 -2
- package/dist/extensions/tools/team/projections.d.ts +0 -1
- package/dist/extensions/tools/team/runtime.d.ts +0 -1
- package/dist/extensions/tools/team/spawn-agent-tool.d.ts +4 -5
- package/dist/extensions/tools/team/subagent-prompts.d.ts +0 -1
- package/dist/extensions/tools/team/team-tools.d.ts +7 -8
- package/dist/extensions/tools/types.d.ts +11 -12
- package/dist/hooks/checkpoint-hooks.d.ts +9 -2
- package/dist/hooks/hook-extension.d.ts +2 -0
- package/dist/{extensions/config/hooks-config-loader.d.ts → hooks/hook-file-config.d.ts} +1 -2
- package/dist/hooks/hook-file-hooks.d.ts +2 -2
- package/dist/hooks/index.d.ts +3 -1
- package/dist/hooks/subprocess-runner.d.ts +0 -1
- package/dist/hooks/subprocess.d.ts +0 -1
- package/dist/hub/{connect.d.ts → client/connect.d.ts} +1 -2
- package/dist/hub/{client.d.ts → client/index.d.ts} +41 -4
- package/dist/hub/{session-client.d.ts → client/session-client.d.ts} +41 -13
- package/dist/hub/{ui-client.d.ts → client/ui-client.d.ts} +1 -1
- package/dist/hub/daemon/entry.d.ts +1 -0
- package/dist/hub/daemon/entry.js +720 -0
- package/dist/hub/{daemon.d.ts → daemon/index.d.ts} +6 -3
- package/dist/hub/{runtime-handlers.d.ts → daemon/runtime-handlers.d.ts} +3 -2
- package/dist/hub/{start-shared-server.d.ts → daemon/start-shared-server.d.ts} +1 -2
- package/dist/hub/{defaults.d.ts → discovery/defaults.d.ts} +0 -1
- package/dist/hub/{discovery.d.ts → discovery/index.d.ts} +2 -1
- package/dist/hub/{workspace.d.ts → discovery/workspace.d.ts} +1 -2
- package/dist/hub/index.d.ts +28 -11
- package/dist/hub/index.js +484 -1061
- package/dist/hub/runtime-host/hub-runtime-host.d.ts +73 -0
- package/dist/{transports/remote.d.ts → hub/runtime-host/remote-runtime-host.d.ts} +1 -2
- package/dist/hub/{browser-websocket.d.ts → server/browser-websocket.d.ts} +1 -2
- package/dist/hub/{transport.d.ts → server/command-transport.d.ts} +0 -1
- package/dist/hub/server/handlers/approval-handlers.d.ts +17 -0
- package/dist/hub/server/handlers/capability-handlers.d.ts +12 -0
- package/dist/hub/server/handlers/client-handlers.d.ts +6 -0
- package/dist/hub/server/handlers/context.d.ts +56 -0
- package/dist/hub/server/handlers/run-handlers.d.ts +5 -0
- package/dist/hub/server/handlers/session-event-projector.d.ts +7 -0
- package/dist/hub/server/handlers/session-handlers.d.ts +20 -0
- package/dist/hub/server/hub-client-contributions.d.ts +19 -0
- package/dist/hub/server/hub-notifications.d.ts +7 -0
- package/dist/hub/server/hub-schedule-events.d.ts +2 -0
- package/dist/hub/server/hub-server-logging.d.ts +2 -0
- package/dist/hub/server/hub-server-options.d.ts +55 -0
- package/dist/hub/server/hub-server-transport.d.ts +34 -0
- package/dist/hub/server/hub-session-records.d.ts +8 -0
- package/dist/hub/server/hub-websocket-server.d.ts +6 -0
- package/dist/hub/server/index.d.ts +4 -0
- package/dist/hub/{native-transport.d.ts → server/native-transport.d.ts} +1 -2
- package/dist/index.d.ts +51 -41
- package/dist/index.js +214 -872
- package/dist/runtime/capabilities/index.d.ts +2 -0
- package/dist/runtime/capabilities/normalize-runtime-capabilities.d.ts +2 -0
- package/dist/runtime/capabilities/runtime-capabilities.d.ts +6 -0
- package/dist/runtime/config/agent-message-codec.d.ts +6 -0
- package/dist/runtime/{agent-runtime-config-builder.d.ts → config/agent-runtime-config-builder.d.ts} +12 -21
- package/dist/runtime/host/history.d.ts +18 -0
- package/dist/runtime/{host.d.ts → host/host.d.ts} +3 -4
- package/dist/runtime/host/local/agent-event-bridge.d.ts +29 -0
- package/dist/runtime/host/local/session-record.d.ts +6 -0
- package/dist/runtime/host/local/session-service-invoker.d.ts +4 -0
- package/dist/runtime/host/local/spawn-tool.d.ts +15 -0
- package/dist/runtime/host/local/user-files.d.ts +1 -0
- package/dist/runtime/host/local-runtime-host.d.ts +118 -0
- package/dist/{transports → runtime/host}/runtime-host-support.d.ts +3 -5
- package/dist/runtime/{runtime-host.d.ts → host/runtime-host.d.ts} +66 -31
- package/dist/runtime/{runtime-builder.d.ts → orchestration/runtime-builder.d.ts} +0 -1
- package/dist/runtime/{runtime-event-adapter.d.ts → orchestration/runtime-event-adapter.d.ts} +0 -1
- package/dist/runtime/{runtime-oauth-token-manager.d.ts → orchestration/runtime-oauth-token-manager.d.ts} +1 -2
- package/dist/runtime/{session-runtime-orchestrator.d.ts → orchestration/session-runtime-orchestrator.d.ts} +20 -63
- package/dist/runtime/{session-runtime.d.ts → orchestration/session-runtime.d.ts} +14 -12
- package/dist/runtime/{user-input-builder.d.ts → orchestration/user-input-builder.d.ts} +2 -11
- package/dist/runtime/{loop-detection.d.ts → safety/loop-detection.d.ts} +0 -1
- package/dist/runtime/{mistake-tracker.d.ts → safety/mistake-tracker.d.ts} +0 -1
- package/dist/runtime/{rules.d.ts → safety/rules.d.ts} +1 -2
- package/dist/runtime/{subprocess-sandbox.d.ts → tools/subprocess-sandbox.d.ts} +15 -1
- package/dist/runtime/{tool-approval.d.ts → tools/tool-approval.d.ts} +0 -1
- package/dist/runtime/turn-queue/pending-prompt-service.d.ts +64 -0
- package/dist/services/agent-events.d.ts +1 -2
- package/dist/services/config.d.ts +0 -1
- package/dist/services/global-settings.d.ts +27 -5
- package/dist/{llms → services/llms}/cline-recommended-models.d.ts +1 -2
- package/dist/{llms → services/llms}/configured-provider-registry.d.ts +0 -1
- package/dist/services/llms/handler-factory.d.ts +3 -0
- package/dist/{llms → services/llms}/provider-defaults.d.ts +2 -1
- package/dist/{llms → services/llms}/provider-settings.d.ts +5 -3
- package/dist/{llms → services/llms}/runtime-config.d.ts +0 -1
- package/dist/{llms → services/llms}/runtime-registry.d.ts +0 -1
- package/dist/{llms → services/llms}/runtime-types.d.ts +0 -1
- package/dist/services/local-runtime-bootstrap.d.ts +7 -9
- package/dist/services/plugin-tools.d.ts +0 -1
- package/dist/services/providers/local-provider-registry.d.ts +9 -10
- package/dist/services/providers/local-provider-service.d.ts +36 -3
- package/dist/services/providers/model-source.d.ts +3 -0
- package/dist/services/session-artifacts.d.ts +0 -1
- package/dist/services/session-data.d.ts +2 -3
- package/dist/services/session-telemetry.d.ts +0 -1
- package/dist/services/storage/file-team-store.d.ts +0 -1
- package/dist/services/storage/provider-settings-legacy-migration.d.ts +0 -1
- package/dist/services/storage/provider-settings-manager.d.ts +3 -4
- package/dist/services/storage/sqlite-session-store.d.ts +0 -1
- package/dist/services/storage/sqlite-team-store.d.ts +0 -1
- package/dist/services/storage/team-store.d.ts +0 -1
- package/dist/services/telemetry/ITelemetryAdapter.d.ts +0 -1
- package/dist/services/telemetry/OpenTelemetryAdapter.d.ts +0 -1
- package/dist/services/telemetry/OpenTelemetryProvider.d.ts +32 -1
- package/dist/services/telemetry/TelemetryLoggerSink.d.ts +0 -1
- package/dist/services/telemetry/TelemetryService.d.ts +0 -1
- package/dist/services/telemetry/core-events.d.ts +46 -3
- package/dist/services/telemetry/distinct-id.d.ts +0 -1
- package/dist/services/telemetry/index.d.ts +1 -2
- package/dist/services/telemetry/index.js +1 -0
- package/dist/services/usage.d.ts +10 -2
- package/dist/services/workspace/file-indexer.d.ts +0 -1
- package/dist/services/workspace/index.d.ts +0 -1
- package/dist/services/workspace/mention-enricher.d.ts +0 -1
- package/dist/{session → services/workspace}/workspace-manager.d.ts +0 -1
- package/dist/services/{workspace-manifest.d.ts → workspace/workspace-manifest.d.ts} +20 -5
- package/dist/services/workspace/workspace-telemetry.d.ts +18 -0
- package/dist/session/checkpoint-restore.d.ts +20 -0
- package/dist/session/{session-graph.d.ts → models/session-graph.d.ts} +2 -3
- package/dist/session/{session-manifest.d.ts → models/session-manifest.d.ts} +0 -1
- package/dist/session/{session-row.d.ts → models/session-row.d.ts} +2 -3
- package/dist/session/{file-session-service.d.ts → services/file-session-service.d.ts} +1 -2
- package/dist/session/{message-builder.d.ts → services/message-builder.d.ts} +7 -16
- package/dist/session/{persistence-service.d.ts → services/persistence-service.d.ts} +10 -11
- package/dist/session/{session-service.d.ts → services/session-service.d.ts} +4 -5
- package/dist/session/session-snapshot.d.ts +57 -0
- package/dist/session/session-versioning-service.d.ts +48 -0
- package/dist/session/{conversation-store.d.ts → stores/conversation-store.d.ts} +0 -1
- package/dist/session/{session-manifest-store.d.ts → stores/session-manifest-store.d.ts} +3 -4
- package/dist/session/{team-persistence-store.d.ts → stores/team-persistence-store.d.ts} +2 -3
- package/dist/session/team/index.d.ts +2 -0
- package/dist/session/{subagent-session-manager.d.ts → team/team-child-session-manager.d.ts} +7 -8
- package/dist/session/{session-team-coordination.d.ts → team/team-session-coordinator.d.ts} +3 -4
- package/dist/settings/index.d.ts +2 -0
- package/dist/settings/settings-service.d.ts +6 -0
- package/dist/settings/types.d.ts +42 -0
- package/dist/types/chat-schema.d.ts +24 -7
- package/dist/types/common.d.ts +0 -1
- package/dist/types/config.d.ts +2 -3
- package/dist/types/events.d.ts +8 -1
- package/dist/types/provider-settings.d.ts +2 -3
- package/dist/types/session.d.ts +22 -6
- package/dist/types/sessions.d.ts +0 -1
- package/dist/types/storage.d.ts +0 -1
- package/dist/types.d.ts +17 -14
- package/dist/version.d.ts +0 -1
- package/package.json +16 -12
- package/dist/ClineCore.d.ts.map +0 -1
- package/dist/account/cline-account-service.d.ts.map +0 -1
- package/dist/account/index.d.ts.map +0 -1
- package/dist/account/rpc.d.ts.map +0 -1
- package/dist/account/types.d.ts.map +0 -1
- package/dist/auth/bounded-ttl-cache.d.ts.map +0 -1
- package/dist/auth/client.d.ts.map +0 -1
- package/dist/auth/cline.d.ts.map +0 -1
- package/dist/auth/codex.d.ts.map +0 -1
- package/dist/auth/oca.d.ts.map +0 -1
- package/dist/auth/server.d.ts.map +0 -1
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/auth/utils.d.ts.map +0 -1
- package/dist/cron/cron-event-ingress.d.ts.map +0 -1
- package/dist/cron/cron-materializer.d.ts.map +0 -1
- package/dist/cron/cron-reconciler.d.ts.map +0 -1
- package/dist/cron/cron-report-writer.d.ts.map +0 -1
- package/dist/cron/cron-runner.d.ts.map +0 -1
- package/dist/cron/cron-schema.d.ts.map +0 -1
- package/dist/cron/cron-service.d.ts.map +0 -1
- package/dist/cron/cron-spec-parser.d.ts.map +0 -1
- package/dist/cron/cron-watcher.d.ts.map +0 -1
- package/dist/cron/resource-limiter.d.ts.map +0 -1
- package/dist/cron/schedule-command-service.d.ts.map +0 -1
- package/dist/cron/schedule-service.d.ts.map +0 -1
- package/dist/cron/scheduler.d.ts +0 -68
- package/dist/cron/scheduler.d.ts.map +0 -1
- package/dist/cron/sqlite-cron-store.d.ts.map +0 -1
- package/dist/cron/sqlite-schedule-store.d.ts +0 -52
- package/dist/cron/sqlite-schedule-store.d.ts.map +0 -1
- package/dist/extensions/config/agent-config-loader.d.ts +0 -20
- package/dist/extensions/config/agent-config-loader.d.ts.map +0 -1
- package/dist/extensions/config/agent-config-parser.d.ts +0 -29
- package/dist/extensions/config/agent-config-parser.d.ts.map +0 -1
- package/dist/extensions/config/hooks-config-loader.d.ts.map +0 -1
- package/dist/extensions/config/index.d.ts.map +0 -1
- package/dist/extensions/config/runtime-commands.d.ts.map +0 -1
- package/dist/extensions/config/unified-config-file-watcher.d.ts.map +0 -1
- package/dist/extensions/config/user-instruction-config-loader.d.ts.map +0 -1
- package/dist/extensions/context/agentic-compaction.d.ts.map +0 -1
- package/dist/extensions/context/basic-compaction.d.ts.map +0 -1
- package/dist/extensions/context/compaction-shared.d.ts.map +0 -1
- package/dist/extensions/context/compaction.d.ts.map +0 -1
- package/dist/extensions/index.d.ts.map +0 -1
- package/dist/extensions/mcp/client.d.ts.map +0 -1
- package/dist/extensions/mcp/config-loader.d.ts.map +0 -1
- package/dist/extensions/mcp/index.d.ts.map +0 -1
- package/dist/extensions/mcp/manager.d.ts.map +0 -1
- package/dist/extensions/mcp/name-transform.d.ts.map +0 -1
- package/dist/extensions/mcp/policies.d.ts.map +0 -1
- package/dist/extensions/mcp/tools.d.ts.map +0 -1
- package/dist/extensions/mcp/types.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-config-loader.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-load-report.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-loader.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-module-import.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-sandbox.d.ts.map +0 -1
- package/dist/extensions/plugin/plugin-targeting.d.ts.map +0 -1
- package/dist/extensions/tools/constants.d.ts.map +0 -1
- package/dist/extensions/tools/definitions.d.ts.map +0 -1
- package/dist/extensions/tools/executors/apply-patch-parser.d.ts.map +0 -1
- package/dist/extensions/tools/executors/apply-patch.d.ts.map +0 -1
- package/dist/extensions/tools/executors/bash.d.ts.map +0 -1
- package/dist/extensions/tools/executors/editor.d.ts.map +0 -1
- package/dist/extensions/tools/executors/file-read.d.ts.map +0 -1
- package/dist/extensions/tools/executors/index.d.ts.map +0 -1
- package/dist/extensions/tools/executors/search.d.ts.map +0 -1
- package/dist/extensions/tools/executors/web-fetch.d.ts.map +0 -1
- package/dist/extensions/tools/helpers.d.ts.map +0 -1
- package/dist/extensions/tools/index.d.ts.map +0 -1
- package/dist/extensions/tools/model-tool-routing.d.ts.map +0 -1
- package/dist/extensions/tools/presets.d.ts.map +0 -1
- package/dist/extensions/tools/runtime.d.ts.map +0 -1
- package/dist/extensions/tools/schemas.d.ts.map +0 -1
- package/dist/extensions/tools/team/delegated-agent.d.ts.map +0 -1
- package/dist/extensions/tools/team/index.d.ts.map +0 -1
- package/dist/extensions/tools/team/multi-agent.d.ts.map +0 -1
- package/dist/extensions/tools/team/projections.d.ts.map +0 -1
- package/dist/extensions/tools/team/runtime.d.ts.map +0 -1
- package/dist/extensions/tools/team/spawn-agent-tool.d.ts.map +0 -1
- package/dist/extensions/tools/team/subagent-prompts.d.ts.map +0 -1
- package/dist/extensions/tools/team/team-tools.d.ts.map +0 -1
- package/dist/extensions/tools/types.d.ts.map +0 -1
- package/dist/hooks/checkpoint-hooks.d.ts.map +0 -1
- package/dist/hooks/hook-bridge.d.ts +0 -118
- package/dist/hooks/hook-bridge.d.ts.map +0 -1
- package/dist/hooks/hook-file-hooks.d.ts.map +0 -1
- package/dist/hooks/hook-registry.d.ts +0 -16
- package/dist/hooks/hook-registry.d.ts.map +0 -1
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/subprocess-runner.d.ts.map +0 -1
- package/dist/hooks/subprocess.d.ts.map +0 -1
- package/dist/hub/browser-websocket.d.ts.map +0 -1
- package/dist/hub/client.d.ts.map +0 -1
- package/dist/hub/connect.d.ts.map +0 -1
- package/dist/hub/daemon-entry.d.ts +0 -2
- package/dist/hub/daemon-entry.d.ts.map +0 -1
- package/dist/hub/daemon-entry.js +0 -1305
- package/dist/hub/daemon.d.ts.map +0 -1
- package/dist/hub/defaults.d.ts.map +0 -1
- package/dist/hub/discovery.d.ts.map +0 -1
- package/dist/hub/index.d.ts.map +0 -1
- package/dist/hub/native-transport.d.ts.map +0 -1
- package/dist/hub/runtime-handlers.d.ts.map +0 -1
- package/dist/hub/server.d.ts +0 -104
- package/dist/hub/server.d.ts.map +0 -1
- package/dist/hub/session-client.d.ts.map +0 -1
- package/dist/hub/start-shared-server.d.ts.map +0 -1
- package/dist/hub/transport.d.ts.map +0 -1
- package/dist/hub/ui-client.d.ts.map +0 -1
- package/dist/hub/workspace.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/llms/cline-recommended-models.d.ts.map +0 -1
- package/dist/llms/configured-provider-registry.d.ts.map +0 -1
- package/dist/llms/handler-factory.d.ts +0 -16
- package/dist/llms/handler-factory.d.ts.map +0 -1
- package/dist/llms/provider-defaults.d.ts.map +0 -1
- package/dist/llms/provider-settings.d.ts.map +0 -1
- package/dist/llms/runtime-config.d.ts.map +0 -1
- package/dist/llms/runtime-registry.d.ts.map +0 -1
- package/dist/llms/runtime-types.d.ts.map +0 -1
- package/dist/runtime/agent-config-adapter.d.ts +0 -148
- package/dist/runtime/agent-config-adapter.d.ts.map +0 -1
- package/dist/runtime/agent-runtime-config-builder.d.ts.map +0 -1
- package/dist/runtime/history.d.ts +0 -10
- package/dist/runtime/history.d.ts.map +0 -1
- package/dist/runtime/host.d.ts.map +0 -1
- package/dist/runtime/loop-detection.d.ts.map +0 -1
- package/dist/runtime/mistake-tracker.d.ts.map +0 -1
- package/dist/runtime/rules.d.ts.map +0 -1
- package/dist/runtime/runtime-builder.d.ts.map +0 -1
- package/dist/runtime/runtime-event-adapter.d.ts.map +0 -1
- package/dist/runtime/runtime-host.d.ts.map +0 -1
- package/dist/runtime/runtime-oauth-token-manager.d.ts.map +0 -1
- package/dist/runtime/session-runtime-orchestrator.d.ts.map +0 -1
- package/dist/runtime/session-runtime.d.ts.map +0 -1
- package/dist/runtime/subprocess-sandbox.d.ts.map +0 -1
- package/dist/runtime/tool-approval.d.ts.map +0 -1
- package/dist/runtime/user-input-builder.d.ts.map +0 -1
- package/dist/services/agent-events.d.ts.map +0 -1
- package/dist/services/config.d.ts.map +0 -1
- package/dist/services/global-settings.d.ts.map +0 -1
- package/dist/services/index.js +0 -28
- package/dist/services/local-runtime-bootstrap.d.ts.map +0 -1
- package/dist/services/plugin-tools.d.ts.map +0 -1
- package/dist/services/providers/local-provider-registry.d.ts.map +0 -1
- package/dist/services/providers/local-provider-service.d.ts.map +0 -1
- package/dist/services/session-artifacts.d.ts.map +0 -1
- package/dist/services/session-data.d.ts.map +0 -1
- package/dist/services/session-telemetry.d.ts.map +0 -1
- package/dist/services/storage/file-team-store.d.ts.map +0 -1
- package/dist/services/storage/provider-settings-legacy-migration.d.ts.map +0 -1
- package/dist/services/storage/provider-settings-manager.d.ts.map +0 -1
- package/dist/services/storage/sqlite-session-store.d.ts.map +0 -1
- package/dist/services/storage/sqlite-team-store.d.ts.map +0 -1
- package/dist/services/storage/team-store.d.ts.map +0 -1
- package/dist/services/telemetry/ITelemetryAdapter.d.ts.map +0 -1
- package/dist/services/telemetry/OpenTelemetryAdapter.d.ts.map +0 -1
- package/dist/services/telemetry/OpenTelemetryProvider.d.ts.map +0 -1
- package/dist/services/telemetry/TelemetryLoggerSink.d.ts.map +0 -1
- package/dist/services/telemetry/TelemetryService.d.ts.map +0 -1
- package/dist/services/telemetry/core-events.d.ts.map +0 -1
- package/dist/services/telemetry/distinct-id.d.ts.map +0 -1
- package/dist/services/telemetry/index.d.ts.map +0 -1
- package/dist/services/usage.d.ts.map +0 -1
- package/dist/services/workspace/file-indexer.d.ts.map +0 -1
- package/dist/services/workspace/index.d.ts.map +0 -1
- package/dist/services/workspace/mention-enricher.d.ts.map +0 -1
- package/dist/services/workspace-manifest.d.ts.map +0 -1
- package/dist/session/conversation-store.d.ts.map +0 -1
- package/dist/session/file-session-service.d.ts.map +0 -1
- package/dist/session/message-builder.d.ts.map +0 -1
- package/dist/session/persistence-service.d.ts.map +0 -1
- package/dist/session/session-graph.d.ts.map +0 -1
- package/dist/session/session-manifest-store.d.ts.map +0 -1
- package/dist/session/session-manifest.d.ts.map +0 -1
- package/dist/session/session-row.d.ts.map +0 -1
- package/dist/session/session-service.d.ts.map +0 -1
- package/dist/session/session-team-coordination.d.ts.map +0 -1
- package/dist/session/subagent-session-manager.d.ts.map +0 -1
- package/dist/session/team-persistence-store.d.ts.map +0 -1
- package/dist/session/workspace-manager.d.ts.map +0 -1
- package/dist/transports/hub.d.ts +0 -58
- package/dist/transports/hub.d.ts.map +0 -1
- package/dist/transports/local.d.ts +0 -119
- package/dist/transports/local.d.ts.map +0 -1
- package/dist/transports/remote.d.ts.map +0 -1
- package/dist/transports/runtime-host-support.d.ts.map +0 -1
- package/dist/types/chat-schema.d.ts.map +0 -1
- package/dist/types/common.d.ts.map +0 -1
- package/dist/types/config.d.ts.map +0 -1
- package/dist/types/events.d.ts.map +0 -1
- package/dist/types/provider-settings.d.ts.map +0 -1
- package/dist/types/session.d.ts.map +0 -1
- package/dist/types/sessions.d.ts.map +0 -1
- package/dist/types/storage.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/version.d.ts.map +0 -1
- package/src/ClineCore.ts +0 -1026
- package/src/account/cline-account-service.ts +0 -338
- package/src/account/index.ts +0 -24
- package/src/account/rpc.ts +0 -185
- package/src/account/types.ts +0 -108
- package/src/auth/bounded-ttl-cache.ts +0 -53
- package/src/auth/client.ts +0 -46
- package/src/auth/cline.ts +0 -724
- package/src/auth/codex.ts +0 -491
- package/src/auth/oca.ts +0 -588
- package/src/auth/server.ts +0 -265
- package/src/auth/types.ts +0 -110
- package/src/auth/utils.ts +0 -247
- package/src/cron/cron-event-ingress.ts +0 -357
- package/src/cron/cron-materializer.ts +0 -97
- package/src/cron/cron-reconciler.ts +0 -241
- package/src/cron/cron-report-writer.ts +0 -153
- package/src/cron/cron-runner.ts +0 -495
- package/src/cron/cron-schema.ts +0 -127
- package/src/cron/cron-service.ts +0 -163
- package/src/cron/cron-spec-parser.ts +0 -489
- package/src/cron/cron-watcher.ts +0 -102
- package/src/cron/index.ts +0 -15
- package/src/cron/resource-limiter.ts +0 -46
- package/src/cron/schedule-command-service.ts +0 -193
- package/src/cron/schedule-service.ts +0 -703
- package/src/cron/scheduler.ts +0 -772
- package/src/cron/sqlite-cron-store.ts +0 -1286
- package/src/cron/sqlite-schedule-store.ts +0 -708
- package/src/extensions/config/agent-config-loader.ts +0 -114
- package/src/extensions/config/agent-config-parser.ts +0 -198
- package/src/extensions/config/hooks-config-loader.ts +0 -117
- package/src/extensions/config/index.ts +0 -77
- package/src/extensions/config/runtime-commands.ts +0 -82
- package/src/extensions/config/unified-config-file-watcher.ts +0 -496
- package/src/extensions/config/user-instruction-config-loader.ts +0 -549
- package/src/extensions/context/agentic-compaction.ts +0 -131
- package/src/extensions/context/basic-compaction.ts +0 -300
- package/src/extensions/context/compaction-shared.ts +0 -459
- package/src/extensions/context/compaction.ts +0 -226
- package/src/extensions/index.ts +0 -18
- package/src/extensions/mcp/client.ts +0 -420
- package/src/extensions/mcp/config-loader.ts +0 -219
- package/src/extensions/mcp/index.ts +0 -42
- package/src/extensions/mcp/manager.ts +0 -260
- package/src/extensions/mcp/name-transform.ts +0 -33
- package/src/extensions/mcp/policies.ts +0 -47
- package/src/extensions/mcp/tools.ts +0 -47
- package/src/extensions/mcp/types.ts +0 -116
- package/src/extensions/plugin/plugin-config-loader.ts +0 -140
- package/src/extensions/plugin/plugin-load-report.ts +0 -20
- package/src/extensions/plugin/plugin-loader.ts +0 -220
- package/src/extensions/plugin/plugin-module-import.ts +0 -276
- package/src/extensions/plugin/plugin-sandbox-bootstrap.ts +0 -662
- package/src/extensions/plugin/plugin-sandbox.ts +0 -586
- package/src/extensions/plugin/plugin-targeting.ts +0 -32
- package/src/extensions/tools/constants.ts +0 -37
- package/src/extensions/tools/definitions.ts +0 -738
- package/src/extensions/tools/executors/apply-patch-parser.ts +0 -520
- package/src/extensions/tools/executors/apply-patch.ts +0 -348
- package/src/extensions/tools/executors/bash.ts +0 -207
- package/src/extensions/tools/executors/editor.ts +0 -220
- package/src/extensions/tools/executors/file-read.ts +0 -135
- package/src/extensions/tools/executors/index.ts +0 -87
- package/src/extensions/tools/executors/search.ts +0 -470
- package/src/extensions/tools/executors/web-fetch.ts +0 -259
- package/src/extensions/tools/helpers.ts +0 -154
- package/src/extensions/tools/index.ts +0 -183
- package/src/extensions/tools/model-tool-routing.ts +0 -134
- package/src/extensions/tools/presets.ts +0 -190
- package/src/extensions/tools/runtime.ts +0 -261
- package/src/extensions/tools/schemas.ts +0 -343
- package/src/extensions/tools/team/delegated-agent.ts +0 -136
- package/src/extensions/tools/team/index.ts +0 -5
- package/src/extensions/tools/team/multi-agent.ts +0 -1845
- package/src/extensions/tools/team/projections.ts +0 -283
- package/src/extensions/tools/team/runtime.ts +0 -54
- package/src/extensions/tools/team/spawn-agent-tool.ts +0 -201
- package/src/extensions/tools/team/subagent-prompts.ts +0 -41
- package/src/extensions/tools/team/team-tools.ts +0 -902
- package/src/extensions/tools/types.ts +0 -354
- package/src/hooks/checkpoint-hooks.ts +0 -238
- package/src/hooks/hook-bridge.ts +0 -489
- package/src/hooks/hook-file-hooks.ts +0 -934
- package/src/hooks/hook-registry.ts +0 -257
- package/src/hooks/index.ts +0 -18
- package/src/hooks/subprocess-runner.ts +0 -196
- package/src/hooks/subprocess.ts +0 -469
- package/src/hub/browser-websocket.ts +0 -159
- package/src/hub/client.ts +0 -633
- package/src/hub/connect.ts +0 -156
- package/src/hub/daemon-entry.ts +0 -122
- package/src/hub/daemon.ts +0 -284
- package/src/hub/defaults.ts +0 -70
- package/src/hub/discovery.ts +0 -247
- package/src/hub/index.ts +0 -14
- package/src/hub/native-transport.ts +0 -31
- package/src/hub/runtime-handlers.ts +0 -141
- package/src/hub/server.ts +0 -2317
- package/src/hub/session-client.ts +0 -502
- package/src/hub/start-shared-server.ts +0 -61
- package/src/hub/transport.ts +0 -14
- package/src/hub/ui-client.ts +0 -126
- package/src/hub/workspace.ts +0 -19
- package/src/index.ts +0 -688
- package/src/llms/cline-recommended-models.ts +0 -167
- package/src/llms/configured-provider-registry.ts +0 -193
- package/src/llms/handler-factory.ts +0 -56
- package/src/llms/provider-defaults.ts +0 -653
- package/src/llms/provider-settings.ts +0 -310
- package/src/llms/runtime-config.ts +0 -43
- package/src/llms/runtime-registry.ts +0 -172
- package/src/llms/runtime-types.ts +0 -121
- package/src/runtime/agent-config-adapter.ts +0 -636
- package/src/runtime/agent-runtime-config-builder.ts +0 -205
- package/src/runtime/error-feedback.ts +0 -142
- package/src/runtime/history.ts +0 -374
- package/src/runtime/host.ts +0 -222
- package/src/runtime/index.ts +0 -23
- package/src/runtime/loop-detection.ts +0 -162
- package/src/runtime/mistake-tracker.ts +0 -221
- package/src/runtime/rules.ts +0 -49
- package/src/runtime/runtime-builder.ts +0 -814
- package/src/runtime/runtime-event-adapter.ts +0 -412
- package/src/runtime/runtime-host.ts +0 -250
- package/src/runtime/runtime-oauth-token-manager.ts +0 -268
- package/src/runtime/session-runtime-orchestrator.ts +0 -1253
- package/src/runtime/session-runtime.ts +0 -69
- package/src/runtime/subprocess-sandbox.ts +0 -255
- package/src/runtime/tool-approval.ts +0 -102
- package/src/runtime/user-input-builder.ts +0 -167
- package/src/services/agent-events.ts +0 -256
- package/src/services/config.ts +0 -5
- package/src/services/global-settings.ts +0 -122
- package/src/services/local-runtime-bootstrap.ts +0 -424
- package/src/services/plugin-tools.ts +0 -86
- package/src/services/providers/local-provider-registry.ts +0 -456
- package/src/services/providers/local-provider-service.ts +0 -772
- package/src/services/session-artifacts.ts +0 -138
- package/src/services/session-data.ts +0 -398
- package/src/services/session-telemetry.ts +0 -89
- package/src/services/storage/artifact-store.ts +0 -1
- package/src/services/storage/file-team-store.ts +0 -250
- package/src/services/storage/index.ts +0 -11
- package/src/services/storage/provider-settings-legacy-migration.ts +0 -789
- package/src/services/storage/provider-settings-manager.ts +0 -167
- package/src/services/storage/session-store.ts +0 -1
- package/src/services/storage/sqlite-session-store.ts +0 -270
- package/src/services/storage/sqlite-team-store.ts +0 -537
- package/src/services/storage/team-store.ts +0 -36
- package/src/services/telemetry/ITelemetryAdapter.ts +0 -94
- package/src/services/telemetry/OpenTelemetryAdapter.ts +0 -348
- package/src/services/telemetry/OpenTelemetryProvider.ts +0 -415
- package/src/services/telemetry/TelemetryLoggerSink.ts +0 -121
- package/src/services/telemetry/TelemetryService.ts +0 -139
- package/src/services/telemetry/core-events.ts +0 -400
- package/src/services/telemetry/distinct-id.ts +0 -58
- package/src/services/telemetry/index.ts +0 -20
- package/src/services/usage.ts +0 -32
- package/src/services/workspace/file-indexer.ts +0 -351
- package/src/services/workspace/index.ts +0 -7
- package/src/services/workspace/mention-enricher.ts +0 -122
- package/src/services/workspace-manifest.ts +0 -72
- package/src/session/conversation-store.ts +0 -77
- package/src/session/file-session-service.ts +0 -282
- package/src/session/index.ts +0 -32
- package/src/session/message-builder.ts +0 -941
- package/src/session/persistence-service.ts +0 -559
- package/src/session/session-graph.ts +0 -92
- package/src/session/session-manifest-store.ts +0 -158
- package/src/session/session-manifest.ts +0 -29
- package/src/session/session-row.ts +0 -199
- package/src/session/session-service.ts +0 -324
- package/src/session/session-team-coordination.ts +0 -240
- package/src/session/subagent-session-manager.ts +0 -397
- package/src/session/team-persistence-store.ts +0 -176
- package/src/session/workspace-manager.ts +0 -100
- package/src/transports/hub.ts +0 -1081
- package/src/transports/local.ts +0 -1826
- package/src/transports/remote.ts +0 -27
- package/src/transports/runtime-host-support.ts +0 -140
- package/src/types/chat-schema.ts +0 -81
- package/src/types/common.ts +0 -26
- package/src/types/config.ts +0 -220
- package/src/types/events.ts +0 -80
- package/src/types/index.ts +0 -26
- package/src/types/provider-settings.ts +0 -54
- package/src/types/session.ts +0 -111
- package/src/types/sessions.ts +0 -37
- package/src/types/storage.ts +0 -56
- package/src/types.ts +0 -167
- package/src/version.ts +0 -3
package/src/transports/local.ts
DELETED
|
@@ -1,1826 +0,0 @@
|
|
|
1
|
-
import { readFile, stat } from "node:fs/promises";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
4
|
-
import type * as LlmsProviders from "@clinebot/llms";
|
|
5
|
-
import {
|
|
6
|
-
type AgentConfig,
|
|
7
|
-
type AgentEvent,
|
|
8
|
-
type AgentResult,
|
|
9
|
-
type AutomationEventEnvelope,
|
|
10
|
-
type BasicLogger,
|
|
11
|
-
type BasicLogMetadata,
|
|
12
|
-
createSessionId,
|
|
13
|
-
type ITelemetryService,
|
|
14
|
-
isLikelyAuthError,
|
|
15
|
-
normalizeUserInput,
|
|
16
|
-
type Tool,
|
|
17
|
-
type ToolApprovalRequest,
|
|
18
|
-
type ToolApprovalResult,
|
|
19
|
-
} from "@clinebot/shared";
|
|
20
|
-
import { setHomeDirIfUnset } from "@clinebot/shared/storage";
|
|
21
|
-
import { nanoid } from "nanoid";
|
|
22
|
-
import { createContextCompactionPrepareTurn } from "../extensions/context/compaction";
|
|
23
|
-
import {
|
|
24
|
-
createBuiltinTools,
|
|
25
|
-
resolveToolPresetName,
|
|
26
|
-
type ToolExecutors,
|
|
27
|
-
ToolPresets,
|
|
28
|
-
} from "../extensions/tools";
|
|
29
|
-
import { createSpawnAgentTool, type TeamEvent } from "../extensions/tools/team";
|
|
30
|
-
import type { HookEventPayload } from "../hooks";
|
|
31
|
-
import { DefaultRuntimeBuilder } from "../runtime/runtime-builder";
|
|
32
|
-
import type {
|
|
33
|
-
PendingPromptMutationResult,
|
|
34
|
-
PendingPromptsAction,
|
|
35
|
-
PendingPromptsDeleteInput,
|
|
36
|
-
PendingPromptsListInput,
|
|
37
|
-
PendingPromptsUpdateInput,
|
|
38
|
-
RuntimeHost,
|
|
39
|
-
RuntimeHostSubscribeOptions,
|
|
40
|
-
SendSessionInput,
|
|
41
|
-
SessionAccumulatedUsage,
|
|
42
|
-
StartSessionInput,
|
|
43
|
-
StartSessionResult,
|
|
44
|
-
} from "../runtime/runtime-host";
|
|
45
|
-
import {
|
|
46
|
-
OAuthReauthRequiredError,
|
|
47
|
-
type RuntimeOAuthResolution,
|
|
48
|
-
RuntimeOAuthTokenManager,
|
|
49
|
-
} from "../runtime/runtime-oauth-token-manager";
|
|
50
|
-
import type { RuntimeBuilder } from "../runtime/session-runtime";
|
|
51
|
-
import { SessionRuntime } from "../runtime/session-runtime-orchestrator";
|
|
52
|
-
import {
|
|
53
|
-
type AgentEventContext,
|
|
54
|
-
buildTelemetryAgentIdentity,
|
|
55
|
-
extractAgentEventMetadata,
|
|
56
|
-
handleAgentEvent,
|
|
57
|
-
} from "../services/agent-events";
|
|
58
|
-
import { resolveWorkspacePath } from "../services/config";
|
|
59
|
-
import { filterDisabledTools } from "../services/global-settings";
|
|
60
|
-
import { prepareLocalRuntimeBootstrap } from "../services/local-runtime-bootstrap";
|
|
61
|
-
import { nowIso } from "../services/session-artifacts";
|
|
62
|
-
import {
|
|
63
|
-
toSessionRecord,
|
|
64
|
-
withLatestAssistantTurnMetadata,
|
|
65
|
-
} from "../services/session-data";
|
|
66
|
-
import {
|
|
67
|
-
emitMentionTelemetry,
|
|
68
|
-
emitSessionCreationTelemetry,
|
|
69
|
-
} from "../services/session-telemetry";
|
|
70
|
-
import { ProviderSettingsManager } from "../services/storage/provider-settings-manager";
|
|
71
|
-
import {
|
|
72
|
-
captureAgentCreated,
|
|
73
|
-
captureAgentTeamCreated,
|
|
74
|
-
captureConversationTurnEvent,
|
|
75
|
-
captureModeSwitch,
|
|
76
|
-
captureSubagentExecution,
|
|
77
|
-
captureTaskCompleted,
|
|
78
|
-
} from "../services/telemetry/core-events";
|
|
79
|
-
import { resolveCoreDistinctId } from "../services/telemetry/distinct-id";
|
|
80
|
-
import {
|
|
81
|
-
accumulateUsageTotals,
|
|
82
|
-
createInitialAccumulatedUsage,
|
|
83
|
-
} from "../services/usage";
|
|
84
|
-
import { enrichPromptWithMentions } from "../services/workspace";
|
|
85
|
-
import type { FileSessionService } from "../session/file-session-service";
|
|
86
|
-
import {
|
|
87
|
-
type SessionManifest,
|
|
88
|
-
SessionManifestSchema,
|
|
89
|
-
} from "../session/session-manifest";
|
|
90
|
-
import type { SessionRow } from "../session/session-row";
|
|
91
|
-
import type {
|
|
92
|
-
CoreSessionService,
|
|
93
|
-
RootSessionArtifacts,
|
|
94
|
-
} from "../session/session-service";
|
|
95
|
-
import {
|
|
96
|
-
buildTeamRunContinuationPrompt,
|
|
97
|
-
dispatchTeamEventToBackend,
|
|
98
|
-
emitTeamProgress,
|
|
99
|
-
formatModePrompt,
|
|
100
|
-
hasPendingTeamRunWork,
|
|
101
|
-
notifyTeamRunWaiters,
|
|
102
|
-
shouldAutoContinueTeamRuns,
|
|
103
|
-
trackTeamRunState,
|
|
104
|
-
waitForTeamRunUpdates,
|
|
105
|
-
} from "../session/session-team-coordination";
|
|
106
|
-
import { SessionSource, type SessionStatus } from "../types/common";
|
|
107
|
-
import type { CoreSessionConfig } from "../types/config";
|
|
108
|
-
import type { CoreSessionEvent, SessionPendingPrompt } from "../types/events";
|
|
109
|
-
import type {
|
|
110
|
-
ActiveSession,
|
|
111
|
-
PendingPrompt,
|
|
112
|
-
PreparedTurnInput,
|
|
113
|
-
} from "../types/session";
|
|
114
|
-
import type { SessionRecord } from "../types/sessions";
|
|
115
|
-
import {
|
|
116
|
-
cloneAccumulatedUsage,
|
|
117
|
-
RuntimeHostEventBus,
|
|
118
|
-
readPersistedMessagesFile,
|
|
119
|
-
replaySubagentHookEvent,
|
|
120
|
-
} from "./runtime-host-support";
|
|
121
|
-
|
|
122
|
-
type SessionBackend = CoreSessionService | FileSessionService;
|
|
123
|
-
|
|
124
|
-
const MAX_SCAN_LIMIT = 5000;
|
|
125
|
-
const MAX_USER_FILE_BYTES = 20 * 1_000 * 1_024;
|
|
126
|
-
|
|
127
|
-
async function loadUserFileContent(path: string): Promise<string> {
|
|
128
|
-
const fileStat = await stat(path);
|
|
129
|
-
if (!fileStat.isFile()) {
|
|
130
|
-
throw new Error("Path is not a file");
|
|
131
|
-
}
|
|
132
|
-
if (fileStat.size > MAX_USER_FILE_BYTES) {
|
|
133
|
-
throw new Error("File is too large to read into context.");
|
|
134
|
-
}
|
|
135
|
-
const content = await readFile(path, "utf8");
|
|
136
|
-
if (content.includes("\u0000")) {
|
|
137
|
-
throw new Error("Cannot read binary file into context.");
|
|
138
|
-
}
|
|
139
|
-
return content;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function toActiveSessionRecord(session: ActiveSession): SessionRecord {
|
|
143
|
-
return {
|
|
144
|
-
sessionId: session.sessionId,
|
|
145
|
-
source: session.source,
|
|
146
|
-
pid: process.pid,
|
|
147
|
-
startedAt: session.startedAt,
|
|
148
|
-
endedAt: session.endedAt ?? null,
|
|
149
|
-
exitCode: session.exitCode ?? null,
|
|
150
|
-
status: session.status,
|
|
151
|
-
interactive: session.interactive,
|
|
152
|
-
provider: session.config.providerId,
|
|
153
|
-
model: session.config.modelId,
|
|
154
|
-
cwd: session.config.cwd,
|
|
155
|
-
workspaceRoot: resolveWorkspacePath(session.config),
|
|
156
|
-
teamName: session.config.teamName?.trim() || undefined,
|
|
157
|
-
enableTools: session.config.enableTools,
|
|
158
|
-
enableSpawn: session.config.enableSpawnAgent,
|
|
159
|
-
enableTeams: session.config.enableAgentTeams,
|
|
160
|
-
parentSessionId:
|
|
161
|
-
typeof session.sessionMetadata?.parentSessionId === "string"
|
|
162
|
-
? session.sessionMetadata.parentSessionId
|
|
163
|
-
: undefined,
|
|
164
|
-
parentAgentId:
|
|
165
|
-
typeof session.sessionMetadata?.parentAgentId === "string"
|
|
166
|
-
? session.sessionMetadata.parentAgentId
|
|
167
|
-
: undefined,
|
|
168
|
-
agentId:
|
|
169
|
-
typeof session.sessionMetadata?.agentId === "string"
|
|
170
|
-
? session.sessionMetadata.agentId
|
|
171
|
-
: undefined,
|
|
172
|
-
conversationId:
|
|
173
|
-
typeof session.sessionMetadata?.conversationId === "string"
|
|
174
|
-
? session.sessionMetadata.conversationId
|
|
175
|
-
: undefined,
|
|
176
|
-
isSubagent:
|
|
177
|
-
typeof session.sessionMetadata?.isSubagent === "boolean"
|
|
178
|
-
? session.sessionMetadata.isSubagent
|
|
179
|
-
: false,
|
|
180
|
-
prompt: session.pendingPrompt,
|
|
181
|
-
metadata: session.sessionMetadata,
|
|
182
|
-
messagesPath: session.artifacts?.messagesPath,
|
|
183
|
-
updatedAt: session.startedAt,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
export interface LocalRuntimeHostOptions {
|
|
188
|
-
distinctId?: string;
|
|
189
|
-
sessionService: SessionBackend;
|
|
190
|
-
runtimeBuilder?: RuntimeBuilder;
|
|
191
|
-
createAgent?: (config: AgentConfig) => SessionRuntime;
|
|
192
|
-
defaultToolExecutors?: Partial<ToolExecutors>;
|
|
193
|
-
toolPolicies?: AgentConfig["toolPolicies"];
|
|
194
|
-
providerSettingsManager?: ProviderSettingsManager;
|
|
195
|
-
oauthTokenManager?: RuntimeOAuthTokenManager;
|
|
196
|
-
telemetry?: ITelemetryService;
|
|
197
|
-
requestToolApproval?: (
|
|
198
|
-
request: ToolApprovalRequest,
|
|
199
|
-
) => Promise<ToolApprovalResult>;
|
|
200
|
-
/**
|
|
201
|
-
* Default custom `fetch` implementation threaded into every
|
|
202
|
-
* `ProviderConfig.fetch` built during local session bootstrap. Used by
|
|
203
|
-
* the AI gateway providers when issuing HTTP requests.
|
|
204
|
-
*/
|
|
205
|
-
fetch?: typeof fetch;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export class LocalRuntimeHost implements RuntimeHost {
|
|
209
|
-
public readonly runtimeAddress = undefined;
|
|
210
|
-
private readonly sessionService: SessionBackend;
|
|
211
|
-
private readonly runtimeBuilder: RuntimeBuilder;
|
|
212
|
-
private readonly createAgentInstance: (config: AgentConfig) => SessionRuntime;
|
|
213
|
-
private readonly defaultToolExecutors?: Partial<ToolExecutors>;
|
|
214
|
-
private readonly defaultToolPolicies?: AgentConfig["toolPolicies"];
|
|
215
|
-
private readonly providerSettingsManager: ProviderSettingsManager;
|
|
216
|
-
private readonly oauthTokenManager: RuntimeOAuthTokenManager;
|
|
217
|
-
private readonly defaultTelemetry?: ITelemetryService;
|
|
218
|
-
private readonly defaultFetch?: typeof fetch;
|
|
219
|
-
private readonly defaultRequestToolApproval?: (
|
|
220
|
-
request: ToolApprovalRequest,
|
|
221
|
-
) => Promise<ToolApprovalResult>;
|
|
222
|
-
private readonly events = new RuntimeHostEventBus();
|
|
223
|
-
private readonly sessions = new Map<string, ActiveSession>();
|
|
224
|
-
private readonly usageBySession = new Map<string, SessionAccumulatedUsage>();
|
|
225
|
-
private readonly subAgentStarts = new Map<
|
|
226
|
-
string,
|
|
227
|
-
{ startedAt: number; rootSessionId: string }
|
|
228
|
-
>();
|
|
229
|
-
|
|
230
|
-
constructor(options: LocalRuntimeHostOptions) {
|
|
231
|
-
const homeDir = homedir();
|
|
232
|
-
if (homeDir) setHomeDirIfUnset(homeDir);
|
|
233
|
-
const distinctId = resolveCoreDistinctId(options.distinctId);
|
|
234
|
-
this.sessionService = options.sessionService;
|
|
235
|
-
this.runtimeBuilder = options.runtimeBuilder ?? new DefaultRuntimeBuilder();
|
|
236
|
-
this.createAgentInstance =
|
|
237
|
-
options.createAgent ?? ((config) => new SessionRuntime(config));
|
|
238
|
-
this.defaultToolExecutors = options.defaultToolExecutors;
|
|
239
|
-
this.defaultToolPolicies = options.toolPolicies;
|
|
240
|
-
this.providerSettingsManager =
|
|
241
|
-
options.providerSettingsManager ?? new ProviderSettingsManager();
|
|
242
|
-
this.oauthTokenManager =
|
|
243
|
-
options.oauthTokenManager ??
|
|
244
|
-
new RuntimeOAuthTokenManager({
|
|
245
|
-
providerSettingsManager: this.providerSettingsManager,
|
|
246
|
-
telemetry: options.telemetry,
|
|
247
|
-
});
|
|
248
|
-
this.defaultTelemetry = options.telemetry;
|
|
249
|
-
this.defaultTelemetry?.setDistinctId(distinctId);
|
|
250
|
-
this.defaultFetch = options.fetch;
|
|
251
|
-
this.defaultRequestToolApproval = options.requestToolApproval;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// ── Public API ──────────────────────────────────────────────────────
|
|
255
|
-
|
|
256
|
-
async start(input: StartSessionInput): Promise<StartSessionResult> {
|
|
257
|
-
const source = input.source ?? SessionSource.CLI;
|
|
258
|
-
const startedAt = nowIso();
|
|
259
|
-
const requestedSessionId = input.config.sessionId?.trim() ?? "";
|
|
260
|
-
const sessionId = requestedSessionId || createSessionId();
|
|
261
|
-
const startInput: StartSessionInput = input;
|
|
262
|
-
this.usageBySession.set(sessionId, createInitialAccumulatedUsage());
|
|
263
|
-
|
|
264
|
-
const sessionsDir =
|
|
265
|
-
((await this.invokeOptionalValue("ensureSessionsDir")) as
|
|
266
|
-
| string
|
|
267
|
-
| undefined) ?? "";
|
|
268
|
-
if (!sessionsDir) {
|
|
269
|
-
throw new Error(
|
|
270
|
-
"session service method not available: ensureSessionsDir",
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const sessionDir = join(sessionsDir, sessionId);
|
|
275
|
-
const messagesPath = join(sessionDir, `${sessionId}.messages.json`);
|
|
276
|
-
const manifestPath = join(sessionDir, `${sessionId}.json`);
|
|
277
|
-
const workspacePath = resolveWorkspacePath(input.config);
|
|
278
|
-
|
|
279
|
-
const manifest = SessionManifestSchema.parse({
|
|
280
|
-
version: 1,
|
|
281
|
-
session_id: sessionId,
|
|
282
|
-
source,
|
|
283
|
-
pid: process.pid,
|
|
284
|
-
started_at: startedAt,
|
|
285
|
-
status: "running",
|
|
286
|
-
interactive: input.interactive === true,
|
|
287
|
-
provider: startInput.config.providerId,
|
|
288
|
-
model: startInput.config.modelId,
|
|
289
|
-
cwd: startInput.config.cwd,
|
|
290
|
-
workspace_root: workspacePath,
|
|
291
|
-
team_name: startInput.config.teamName,
|
|
292
|
-
enable_tools: startInput.config.enableTools,
|
|
293
|
-
enable_spawn: startInput.config.enableSpawnAgent,
|
|
294
|
-
enable_teams: startInput.config.enableAgentTeams,
|
|
295
|
-
prompt: startInput.prompt?.trim() || undefined,
|
|
296
|
-
messages_path: messagesPath,
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
const sessionToolExecutors =
|
|
300
|
-
input.localRuntime?.defaultToolExecutors ?? this.defaultToolExecutors;
|
|
301
|
-
const inputLocalConfig = input.localRuntime?.configOverrides as
|
|
302
|
-
| Partial<CoreSessionConfig>
|
|
303
|
-
| undefined;
|
|
304
|
-
const pluginEventFallbackLogger =
|
|
305
|
-
inputLocalConfig?.extensionContext?.logger ?? inputLocalConfig?.logger;
|
|
306
|
-
const pluginEventFallbackAutomation =
|
|
307
|
-
inputLocalConfig?.extensionContext?.automation;
|
|
308
|
-
let bootstrap!: Awaited<ReturnType<typeof prepareLocalRuntimeBootstrap>>;
|
|
309
|
-
bootstrap = await prepareLocalRuntimeBootstrap({
|
|
310
|
-
input: startInput,
|
|
311
|
-
localRuntime: input.localRuntime,
|
|
312
|
-
sessionId,
|
|
313
|
-
providerSettingsManager: this.providerSettingsManager,
|
|
314
|
-
defaultTelemetry: this.defaultTelemetry,
|
|
315
|
-
defaultToolExecutors: sessionToolExecutors,
|
|
316
|
-
defaultToolPolicies: this.defaultToolPolicies,
|
|
317
|
-
defaultRequestToolApproval: this.defaultRequestToolApproval,
|
|
318
|
-
defaultFetch: this.defaultFetch,
|
|
319
|
-
onPluginEvent: (event) => {
|
|
320
|
-
if (event.name === "plugin_log") {
|
|
321
|
-
this.handlePluginLog(
|
|
322
|
-
sessionId,
|
|
323
|
-
event.payload,
|
|
324
|
-
pluginEventFallbackLogger,
|
|
325
|
-
);
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
void this.handlePluginEvent(
|
|
329
|
-
sessionId,
|
|
330
|
-
event,
|
|
331
|
-
pluginEventFallbackAutomation,
|
|
332
|
-
);
|
|
333
|
-
},
|
|
334
|
-
onTeamEvent: (event: TeamEvent) => {
|
|
335
|
-
void this.handleTeamEvent(sessionId, event);
|
|
336
|
-
bootstrap.config.onTeamEvent?.(event);
|
|
337
|
-
},
|
|
338
|
-
createSpawnTool: () =>
|
|
339
|
-
this.createSpawnTool(bootstrap.config, sessionId, sessionToolExecutors),
|
|
340
|
-
readSessionMetadata: async () =>
|
|
341
|
-
(await this.get(sessionId))?.metadata as
|
|
342
|
-
| Record<string, unknown>
|
|
343
|
-
| undefined,
|
|
344
|
-
writeSessionMetadata: async (metadata) => {
|
|
345
|
-
await this.persistSessionMetadata(sessionId, () => metadata);
|
|
346
|
-
},
|
|
347
|
-
});
|
|
348
|
-
const runtime = await this.runtimeBuilder.build(
|
|
349
|
-
bootstrap.runtimeBuilderInput,
|
|
350
|
-
);
|
|
351
|
-
const configWithProvider = bootstrap.config;
|
|
352
|
-
const providerConfig = bootstrap.providerConfig;
|
|
353
|
-
if (runtime.teamRuntime && !configWithProvider.teamName?.trim()) {
|
|
354
|
-
configWithProvider.teamName = runtime.teamRuntime.getTeamName();
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const tools = [...runtime.tools, ...(configWithProvider.extraTools ?? [])];
|
|
358
|
-
|
|
359
|
-
const agentConfig = {
|
|
360
|
-
sessionId,
|
|
361
|
-
providerId: providerConfig.providerId,
|
|
362
|
-
modelId: providerConfig.modelId,
|
|
363
|
-
apiKey: providerConfig.apiKey,
|
|
364
|
-
baseUrl: providerConfig.baseUrl,
|
|
365
|
-
headers: providerConfig.headers,
|
|
366
|
-
knownModels: providerConfig.knownModels,
|
|
367
|
-
providerConfig,
|
|
368
|
-
thinking: configWithProvider.thinking,
|
|
369
|
-
reasoningEffort:
|
|
370
|
-
configWithProvider.reasoningEffort ?? providerConfig.reasoningEffort,
|
|
371
|
-
systemPrompt: configWithProvider.systemPrompt,
|
|
372
|
-
maxIterations: configWithProvider.maxIterations,
|
|
373
|
-
execution: configWithProvider.execution,
|
|
374
|
-
prepareTurn: createContextCompactionPrepareTurn(configWithProvider),
|
|
375
|
-
tools,
|
|
376
|
-
hooks: bootstrap.hooks,
|
|
377
|
-
extensions: bootstrap.extensions,
|
|
378
|
-
hookErrorMode: configWithProvider.hookErrorMode,
|
|
379
|
-
initialMessages: bootstrap.effectiveInput.initialMessages,
|
|
380
|
-
userFileContentLoader: loadUserFileContent,
|
|
381
|
-
toolPolicies: bootstrap.toolPolicies,
|
|
382
|
-
requestToolApproval: bootstrap.requestToolApproval,
|
|
383
|
-
onConsecutiveMistakeLimitReached:
|
|
384
|
-
configWithProvider.onConsecutiveMistakeLimitReached,
|
|
385
|
-
completionGuard: runtime.completionGuard,
|
|
386
|
-
consumePendingUserMessage: () => this.consumeSteerMessage(sessionId),
|
|
387
|
-
logger: runtime.logger ?? configWithProvider.logger,
|
|
388
|
-
extensionContext: configWithProvider.extensionContext,
|
|
389
|
-
onEvent: (event: AgentEvent) =>
|
|
390
|
-
this.onAgentEvent(sessionId, configWithProvider, event),
|
|
391
|
-
} as AgentConfig;
|
|
392
|
-
const agent = this.createAgentInstance(agentConfig);
|
|
393
|
-
if (agentConfig.onEvent) {
|
|
394
|
-
agent.subscribeEvents(agentConfig.onEvent);
|
|
395
|
-
}
|
|
396
|
-
runtime.registerLeadAgent?.(agent);
|
|
397
|
-
const rootAgentIdentity = buildTelemetryAgentIdentity({
|
|
398
|
-
agentId: agent.getAgentId(),
|
|
399
|
-
conversationId: agent.getConversationId(),
|
|
400
|
-
teamId: runtime.teamRuntime?.getTeamId(),
|
|
401
|
-
teamName: runtime.teamRuntime?.getTeamName(),
|
|
402
|
-
teamRole: runtime.teamRuntime ? "lead" : undefined,
|
|
403
|
-
});
|
|
404
|
-
emitSessionCreationTelemetry(
|
|
405
|
-
configWithProvider,
|
|
406
|
-
sessionId,
|
|
407
|
-
requestedSessionId.length > 0,
|
|
408
|
-
workspacePath,
|
|
409
|
-
rootAgentIdentity,
|
|
410
|
-
);
|
|
411
|
-
if (rootAgentIdentity) {
|
|
412
|
-
captureAgentCreated(configWithProvider.telemetry, {
|
|
413
|
-
ulid: sessionId,
|
|
414
|
-
modelId: configWithProvider.modelId,
|
|
415
|
-
provider: configWithProvider.providerId,
|
|
416
|
-
...rootAgentIdentity,
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
if (runtime.teamRuntime) {
|
|
420
|
-
captureAgentTeamCreated(configWithProvider.telemetry, {
|
|
421
|
-
ulid: sessionId,
|
|
422
|
-
teamId: runtime.teamRuntime.getTeamId(),
|
|
423
|
-
teamName: runtime.teamRuntime.getTeamName(),
|
|
424
|
-
leadAgentId: agent.getAgentId(),
|
|
425
|
-
restoredFromPersistence: runtime.teamRestoredFromPersistence === true,
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
const active: ActiveSession = {
|
|
430
|
-
sessionId,
|
|
431
|
-
config: configWithProvider,
|
|
432
|
-
sessionMetadata: startInput.sessionMetadata,
|
|
433
|
-
source,
|
|
434
|
-
startedAt,
|
|
435
|
-
pendingPrompt: manifest.prompt,
|
|
436
|
-
runtime,
|
|
437
|
-
agent,
|
|
438
|
-
started: false,
|
|
439
|
-
status: "running",
|
|
440
|
-
aborting: false,
|
|
441
|
-
interactive: input.interactive === true,
|
|
442
|
-
persistedMessages: startInput.initialMessages,
|
|
443
|
-
activeTeamRunIds: new Set<string>(),
|
|
444
|
-
pendingTeamRunUpdates: [],
|
|
445
|
-
teamRunWaiters: [],
|
|
446
|
-
pendingPrompts: [],
|
|
447
|
-
drainingPendingPrompts: false,
|
|
448
|
-
pluginSandboxShutdown: bootstrap.pluginSandboxShutdown,
|
|
449
|
-
};
|
|
450
|
-
this.sessions.set(sessionId, active);
|
|
451
|
-
this.emitStatus(sessionId, "running");
|
|
452
|
-
if ((startInput.initialMessages?.length ?? 0) > 0) {
|
|
453
|
-
await this.ensureSessionPersisted(active);
|
|
454
|
-
await this.invoke<void>(
|
|
455
|
-
"persistSessionMessages",
|
|
456
|
-
active.sessionId,
|
|
457
|
-
startInput.initialMessages,
|
|
458
|
-
active.config.systemPrompt,
|
|
459
|
-
);
|
|
460
|
-
if (!startInput.prompt?.trim()) {
|
|
461
|
-
await this.updateStatus(active, "completed", 0);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
let result: AgentResult | undefined;
|
|
466
|
-
try {
|
|
467
|
-
if (startInput.prompt?.trim()) {
|
|
468
|
-
result = await this.runTurn(active, {
|
|
469
|
-
prompt: startInput.prompt,
|
|
470
|
-
userImages: startInput.userImages,
|
|
471
|
-
userFiles: startInput.userFiles,
|
|
472
|
-
});
|
|
473
|
-
if (!active.interactive) {
|
|
474
|
-
await this.finalizeSingleRun(active, result.finishReason);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
} catch (error) {
|
|
478
|
-
await this.failSession(active);
|
|
479
|
-
throw error;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
return {
|
|
483
|
-
sessionId,
|
|
484
|
-
manifest,
|
|
485
|
-
manifestPath,
|
|
486
|
-
messagesPath,
|
|
487
|
-
result,
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
async send(input: SendSessionInput): Promise<AgentResult | undefined> {
|
|
492
|
-
const session = this.getSessionOrThrow(input.sessionId);
|
|
493
|
-
const canStartRun = session.agent.canStartRun();
|
|
494
|
-
const delivery =
|
|
495
|
-
input.delivery ??
|
|
496
|
-
(session.interactive && !canStartRun ? ("queue" as const) : undefined);
|
|
497
|
-
session.config.telemetry?.capture({
|
|
498
|
-
event: "session.input_sent",
|
|
499
|
-
properties: {
|
|
500
|
-
sessionId: input.sessionId,
|
|
501
|
-
promptLength: input.prompt.length,
|
|
502
|
-
userImageCount: input.userImages?.length ?? 0,
|
|
503
|
-
userFileCount: input.userFiles?.length ?? 0,
|
|
504
|
-
delivery: delivery ?? "immediate",
|
|
505
|
-
},
|
|
506
|
-
});
|
|
507
|
-
if (delivery === "queue" || delivery === "steer") {
|
|
508
|
-
this.enqueuePendingPrompt(input.sessionId, {
|
|
509
|
-
prompt: input.prompt,
|
|
510
|
-
delivery,
|
|
511
|
-
userImages: input.userImages,
|
|
512
|
-
userFiles: input.userFiles,
|
|
513
|
-
});
|
|
514
|
-
return undefined;
|
|
515
|
-
}
|
|
516
|
-
try {
|
|
517
|
-
const result = await this.runTurn(session, {
|
|
518
|
-
prompt: input.prompt,
|
|
519
|
-
userImages: input.userImages,
|
|
520
|
-
userFiles: input.userFiles,
|
|
521
|
-
});
|
|
522
|
-
if (!session.interactive) {
|
|
523
|
-
await this.finalizeSingleRun(session, result.finishReason);
|
|
524
|
-
}
|
|
525
|
-
queueMicrotask(() => {
|
|
526
|
-
void this.drainPendingPrompts(input.sessionId);
|
|
527
|
-
});
|
|
528
|
-
return result;
|
|
529
|
-
} catch (error) {
|
|
530
|
-
await this.failSession(session);
|
|
531
|
-
throw error;
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
async pendingPrompts(
|
|
536
|
-
action: "list",
|
|
537
|
-
input: PendingPromptsListInput,
|
|
538
|
-
): Promise<SessionPendingPrompt[]>;
|
|
539
|
-
async pendingPrompts(
|
|
540
|
-
action: "update",
|
|
541
|
-
input: PendingPromptsUpdateInput,
|
|
542
|
-
): Promise<PendingPromptMutationResult>;
|
|
543
|
-
async pendingPrompts(
|
|
544
|
-
action: "delete",
|
|
545
|
-
input: PendingPromptsDeleteInput,
|
|
546
|
-
): Promise<PendingPromptMutationResult>;
|
|
547
|
-
async pendingPrompts(
|
|
548
|
-
action: PendingPromptsAction,
|
|
549
|
-
input:
|
|
550
|
-
| PendingPromptsListInput
|
|
551
|
-
| PendingPromptsUpdateInput
|
|
552
|
-
| PendingPromptsDeleteInput,
|
|
553
|
-
): Promise<SessionPendingPrompt[] | PendingPromptMutationResult> {
|
|
554
|
-
switch (action) {
|
|
555
|
-
case "list":
|
|
556
|
-
return this.listPendingPromptEntries(input.sessionId);
|
|
557
|
-
case "update":
|
|
558
|
-
return this.editPendingPromptEntry(input as PendingPromptsUpdateInput);
|
|
559
|
-
case "delete":
|
|
560
|
-
return this.deletePendingPromptEntry(
|
|
561
|
-
input as PendingPromptsDeleteInput,
|
|
562
|
-
);
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
private listPendingPromptEntries(sessionId: string): SessionPendingPrompt[] {
|
|
567
|
-
const session = this.sessions.get(sessionId);
|
|
568
|
-
return session ? this.snapshotPendingPrompts(session) : [];
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
private editPendingPromptEntry(
|
|
572
|
-
input: PendingPromptsUpdateInput,
|
|
573
|
-
): PendingPromptMutationResult {
|
|
574
|
-
const session = this.sessions.get(input.sessionId);
|
|
575
|
-
if (!session || session.aborting) {
|
|
576
|
-
return { sessionId: input.sessionId, prompts: [], updated: false };
|
|
577
|
-
}
|
|
578
|
-
const promptId = input.promptId.trim();
|
|
579
|
-
const index = session.pendingPrompts.findIndex(
|
|
580
|
-
(entry) => entry.id === promptId,
|
|
581
|
-
);
|
|
582
|
-
if (index < 0) {
|
|
583
|
-
return {
|
|
584
|
-
sessionId: input.sessionId,
|
|
585
|
-
prompts: this.snapshotPendingPrompts(session),
|
|
586
|
-
updated: false,
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
const existing = session.pendingPrompts[index]!;
|
|
591
|
-
const prompt =
|
|
592
|
-
input.prompt === undefined
|
|
593
|
-
? existing.prompt
|
|
594
|
-
: normalizeUserInput(input.prompt).trim();
|
|
595
|
-
if (!prompt) {
|
|
596
|
-
throw new Error("prompt cannot be empty");
|
|
597
|
-
}
|
|
598
|
-
const delivery = input.delivery ?? existing.delivery;
|
|
599
|
-
const next: PendingPrompt = {
|
|
600
|
-
...existing,
|
|
601
|
-
prompt,
|
|
602
|
-
delivery,
|
|
603
|
-
};
|
|
604
|
-
session.pendingPrompts.splice(index, 1);
|
|
605
|
-
if (delivery === "steer") {
|
|
606
|
-
session.pendingPrompts.unshift(next);
|
|
607
|
-
} else if (existing.delivery === "steer") {
|
|
608
|
-
session.pendingPrompts.push(next);
|
|
609
|
-
} else {
|
|
610
|
-
session.pendingPrompts.splice(index, 0, next);
|
|
611
|
-
}
|
|
612
|
-
this.emitPendingPrompts(session);
|
|
613
|
-
this.schedulePendingPromptDrain(input.sessionId, session);
|
|
614
|
-
return {
|
|
615
|
-
sessionId: input.sessionId,
|
|
616
|
-
prompts: this.snapshotPendingPrompts(session),
|
|
617
|
-
prompt: this.snapshotPendingPrompt(next),
|
|
618
|
-
updated: true,
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
private deletePendingPromptEntry(
|
|
623
|
-
input: PendingPromptsDeleteInput,
|
|
624
|
-
): PendingPromptMutationResult {
|
|
625
|
-
const session = this.sessions.get(input.sessionId);
|
|
626
|
-
if (!session || session.aborting) {
|
|
627
|
-
return { sessionId: input.sessionId, prompts: [], removed: false };
|
|
628
|
-
}
|
|
629
|
-
const promptId = input.promptId.trim();
|
|
630
|
-
const index = session.pendingPrompts.findIndex(
|
|
631
|
-
(entry) => entry.id === promptId,
|
|
632
|
-
);
|
|
633
|
-
if (index < 0) {
|
|
634
|
-
return {
|
|
635
|
-
sessionId: input.sessionId,
|
|
636
|
-
prompts: this.snapshotPendingPrompts(session),
|
|
637
|
-
removed: false,
|
|
638
|
-
};
|
|
639
|
-
}
|
|
640
|
-
const [removed] = session.pendingPrompts.splice(index, 1);
|
|
641
|
-
this.emitPendingPrompts(session);
|
|
642
|
-
this.schedulePendingPromptDrain(input.sessionId, session);
|
|
643
|
-
return {
|
|
644
|
-
sessionId: input.sessionId,
|
|
645
|
-
prompts: this.snapshotPendingPrompts(session),
|
|
646
|
-
prompt: removed ? this.snapshotPendingPrompt(removed) : undefined,
|
|
647
|
-
removed: true,
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
async getAccumulatedUsage(
|
|
652
|
-
sessionId: string,
|
|
653
|
-
): Promise<SessionAccumulatedUsage | undefined> {
|
|
654
|
-
return cloneAccumulatedUsage(this.usageBySession.get(sessionId));
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
async abort(sessionId: string, reason?: unknown): Promise<void> {
|
|
658
|
-
const session = this.sessions.get(sessionId);
|
|
659
|
-
if (!session) return;
|
|
660
|
-
session.config.telemetry?.capture({
|
|
661
|
-
event: "session.aborted",
|
|
662
|
-
properties: { sessionId },
|
|
663
|
-
});
|
|
664
|
-
session.aborting = true;
|
|
665
|
-
if (session.pendingPrompts.length > 0) {
|
|
666
|
-
session.pendingPrompts.length = 0;
|
|
667
|
-
this.emitPendingPrompts(session);
|
|
668
|
-
}
|
|
669
|
-
session.agent.abort(reason);
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
async stop(sessionId: string): Promise<void> {
|
|
673
|
-
const session = this.sessions.get(sessionId);
|
|
674
|
-
if (!session) return;
|
|
675
|
-
session.config.telemetry?.capture({
|
|
676
|
-
event: "session.stopped",
|
|
677
|
-
properties: { sessionId },
|
|
678
|
-
});
|
|
679
|
-
// Abort the agent first if it's running, so shutdown can proceed
|
|
680
|
-
session.aborting = true;
|
|
681
|
-
session.agent.abort(new Error("session_stop"));
|
|
682
|
-
await this.shutdownSession(session, {
|
|
683
|
-
status: "cancelled",
|
|
684
|
-
exitCode: 0,
|
|
685
|
-
shutdownReason: "session_stop",
|
|
686
|
-
endReason: "stopped",
|
|
687
|
-
});
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
async dispose(reason = "session_manager_dispose"): Promise<void> {
|
|
691
|
-
const sessions = [...this.sessions.values()];
|
|
692
|
-
if (sessions.length === 0) return;
|
|
693
|
-
await Promise.allSettled(
|
|
694
|
-
sessions.map((session) =>
|
|
695
|
-
this.shutdownSession(session, {
|
|
696
|
-
status: "cancelled",
|
|
697
|
-
exitCode: 0,
|
|
698
|
-
shutdownReason: reason,
|
|
699
|
-
endReason: "disposed",
|
|
700
|
-
}),
|
|
701
|
-
),
|
|
702
|
-
);
|
|
703
|
-
this.usageBySession.clear();
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
async get(sessionId: string): Promise<SessionRecord | undefined> {
|
|
707
|
-
const active = this.sessions.get(sessionId);
|
|
708
|
-
if (active) {
|
|
709
|
-
return toActiveSessionRecord(active);
|
|
710
|
-
}
|
|
711
|
-
const row = await this.getRow(sessionId);
|
|
712
|
-
return row ? toSessionRecord(row) : undefined;
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
async list(limit = 200): Promise<SessionRecord[]> {
|
|
716
|
-
const rows = await this.listRows(limit);
|
|
717
|
-
const persisted = rows.map(toSessionRecord);
|
|
718
|
-
const seen = new Set(persisted.map((row) => row.sessionId));
|
|
719
|
-
for (const active of this.sessions.values()) {
|
|
720
|
-
if (seen.has(active.sessionId)) {
|
|
721
|
-
continue;
|
|
722
|
-
}
|
|
723
|
-
persisted.unshift(toActiveSessionRecord(active));
|
|
724
|
-
}
|
|
725
|
-
return persisted.slice(0, limit);
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
async delete(sessionId: string): Promise<boolean> {
|
|
729
|
-
if (this.sessions.has(sessionId)) {
|
|
730
|
-
await this.stop(sessionId);
|
|
731
|
-
}
|
|
732
|
-
const result = await this.invoke<{ deleted: boolean }>(
|
|
733
|
-
"deleteSession",
|
|
734
|
-
sessionId,
|
|
735
|
-
);
|
|
736
|
-
if (result.deleted) {
|
|
737
|
-
this.usageBySession.delete(sessionId);
|
|
738
|
-
}
|
|
739
|
-
return result.deleted;
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
async update(
|
|
743
|
-
sessionId: string,
|
|
744
|
-
updates: {
|
|
745
|
-
prompt?: string | null;
|
|
746
|
-
metadata?: Record<string, unknown> | null;
|
|
747
|
-
title?: string | null;
|
|
748
|
-
},
|
|
749
|
-
): Promise<{ updated: boolean }> {
|
|
750
|
-
const result = await this.invokeOptionalValue<{ updated?: boolean }>(
|
|
751
|
-
"updateSession",
|
|
752
|
-
{
|
|
753
|
-
sessionId,
|
|
754
|
-
prompt: updates.prompt,
|
|
755
|
-
metadata: updates.metadata,
|
|
756
|
-
title: updates.title,
|
|
757
|
-
},
|
|
758
|
-
);
|
|
759
|
-
return { updated: result?.updated === true };
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
async readMessages(sessionId: string): Promise<LlmsProviders.Message[]> {
|
|
763
|
-
const row = await this.getRow(sessionId);
|
|
764
|
-
return readPersistedMessagesFile(row?.messagesPath);
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
async handleHookEvent(payload: HookEventPayload): Promise<void> {
|
|
768
|
-
await replaySubagentHookEvent(payload, {
|
|
769
|
-
queueSpawnRequest: (event: HookEventPayload) =>
|
|
770
|
-
this.invokeOptional("queueSpawnRequest", event),
|
|
771
|
-
upsertSubagentSessionFromHook: (event: HookEventPayload) =>
|
|
772
|
-
this.invokeOptionalValue<string | undefined>(
|
|
773
|
-
"upsertSubagentSessionFromHook",
|
|
774
|
-
event,
|
|
775
|
-
),
|
|
776
|
-
appendSubagentHookAudit: (sessionId: string, event: HookEventPayload) =>
|
|
777
|
-
this.invokeOptional("appendSubagentHookAudit", sessionId, event),
|
|
778
|
-
applySubagentStatus: (sessionId: string, event: HookEventPayload) =>
|
|
779
|
-
this.invokeOptional("applySubagentStatus", sessionId, event),
|
|
780
|
-
});
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
subscribe(
|
|
784
|
-
listener: (event: CoreSessionEvent) => void,
|
|
785
|
-
options?: RuntimeHostSubscribeOptions,
|
|
786
|
-
): () => void {
|
|
787
|
-
return this.events.subscribe(listener, options);
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
async updateSessionModel(sessionId: string, modelId: string): Promise<void> {
|
|
791
|
-
const session = this.getSessionOrThrow(sessionId);
|
|
792
|
-
session.config.modelId = modelId;
|
|
793
|
-
session.runtime.delegatedAgentConfigProvider?.updateConnectionDefaults({
|
|
794
|
-
modelId,
|
|
795
|
-
});
|
|
796
|
-
this.updateAgentConnection(session, { modelId });
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
// ── Turn execution ──────────────────────────────────────────────────
|
|
800
|
-
|
|
801
|
-
private async runTurn(
|
|
802
|
-
session: ActiveSession,
|
|
803
|
-
input: {
|
|
804
|
-
prompt: string;
|
|
805
|
-
userImages?: string[];
|
|
806
|
-
userFiles?: string[];
|
|
807
|
-
},
|
|
808
|
-
): Promise<AgentResult> {
|
|
809
|
-
const preparedInput = await this.prepareTurnInput(session, input);
|
|
810
|
-
const prompt = preparedInput.prompt.trim();
|
|
811
|
-
if (!prompt) throw new Error("prompt cannot be empty");
|
|
812
|
-
|
|
813
|
-
if (!session.artifacts && !session.pendingPrompt) {
|
|
814
|
-
session.pendingPrompt = prompt;
|
|
815
|
-
}
|
|
816
|
-
await this.ensureSessionPersisted(session);
|
|
817
|
-
await this.syncOAuthCredentials(session);
|
|
818
|
-
|
|
819
|
-
let result = await this.executeAgentTurn(
|
|
820
|
-
session,
|
|
821
|
-
prompt,
|
|
822
|
-
preparedInput.userImages,
|
|
823
|
-
preparedInput.userFiles,
|
|
824
|
-
);
|
|
825
|
-
|
|
826
|
-
while (shouldAutoContinueTeamRuns(session, result.finishReason)) {
|
|
827
|
-
const updates = await waitForTeamRunUpdates(session);
|
|
828
|
-
if (updates.length === 0) break;
|
|
829
|
-
const continuationPrompt = buildTeamRunContinuationPrompt(
|
|
830
|
-
session,
|
|
831
|
-
updates,
|
|
832
|
-
);
|
|
833
|
-
result = await this.executeAgentTurn(session, continuationPrompt);
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
return result;
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
private async executeAgentTurn(
|
|
840
|
-
session: ActiveSession,
|
|
841
|
-
prompt: string,
|
|
842
|
-
userImages?: string[],
|
|
843
|
-
userFiles?: string[],
|
|
844
|
-
): Promise<AgentResult> {
|
|
845
|
-
const shouldContinue =
|
|
846
|
-
session.started || session.agent.getMessages().length > 0;
|
|
847
|
-
const baselineMessages =
|
|
848
|
-
session.persistedMessages ?? session.agent.getMessages();
|
|
849
|
-
const usageBaseline =
|
|
850
|
-
this.usageBySession.get(session.sessionId) ??
|
|
851
|
-
createInitialAccumulatedUsage();
|
|
852
|
-
session.turnUsageBaseline = usageBaseline;
|
|
853
|
-
|
|
854
|
-
captureModeSwitch(
|
|
855
|
-
session.config.telemetry,
|
|
856
|
-
session.sessionId,
|
|
857
|
-
session.config.mode,
|
|
858
|
-
);
|
|
859
|
-
captureConversationTurnEvent(session.config.telemetry, {
|
|
860
|
-
ulid: session.sessionId,
|
|
861
|
-
provider: session.config.providerId,
|
|
862
|
-
model: session.config.modelId,
|
|
863
|
-
source: "user",
|
|
864
|
-
mode: session.config.mode,
|
|
865
|
-
...this.getSessionAgentTelemetryIdentity(session),
|
|
866
|
-
});
|
|
867
|
-
|
|
868
|
-
try {
|
|
869
|
-
const runFn = shouldContinue
|
|
870
|
-
? () => session.agent.continue(prompt, userImages, userFiles)
|
|
871
|
-
: () => session.agent.run(prompt, userImages, userFiles);
|
|
872
|
-
const result = await this.runWithAuthRetry(
|
|
873
|
-
session,
|
|
874
|
-
runFn,
|
|
875
|
-
baselineMessages,
|
|
876
|
-
);
|
|
877
|
-
|
|
878
|
-
session.started = true;
|
|
879
|
-
const persistedMessages = withLatestAssistantTurnMetadata(
|
|
880
|
-
result.messages,
|
|
881
|
-
result,
|
|
882
|
-
baselineMessages,
|
|
883
|
-
);
|
|
884
|
-
session.persistedMessages = persistedMessages;
|
|
885
|
-
const accumulatedUsage = accumulateUsageTotals(
|
|
886
|
-
usageBaseline,
|
|
887
|
-
result.usage,
|
|
888
|
-
);
|
|
889
|
-
this.usageBySession.set(session.sessionId, accumulatedUsage);
|
|
890
|
-
await this.persistSessionMetadata(session.sessionId, (current) => ({
|
|
891
|
-
...(current ?? {}),
|
|
892
|
-
totalCost: accumulatedUsage.totalCost,
|
|
893
|
-
}));
|
|
894
|
-
await this.invoke<void>(
|
|
895
|
-
"persistSessionMessages",
|
|
896
|
-
session.sessionId,
|
|
897
|
-
persistedMessages,
|
|
898
|
-
session.config.systemPrompt,
|
|
899
|
-
);
|
|
900
|
-
return result;
|
|
901
|
-
} catch (error) {
|
|
902
|
-
await this.invoke<void>(
|
|
903
|
-
"persistSessionMessages",
|
|
904
|
-
session.sessionId,
|
|
905
|
-
session.agent.getMessages(),
|
|
906
|
-
session.config.systemPrompt,
|
|
907
|
-
);
|
|
908
|
-
throw error;
|
|
909
|
-
} finally {
|
|
910
|
-
session.turnUsageBaseline = undefined;
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
private async prepareTurnInput(
|
|
915
|
-
session: ActiveSession,
|
|
916
|
-
input: {
|
|
917
|
-
prompt: string;
|
|
918
|
-
userImages?: string[];
|
|
919
|
-
userFiles?: string[];
|
|
920
|
-
},
|
|
921
|
-
): Promise<PreparedTurnInput> {
|
|
922
|
-
const mentionBaseDir = resolveWorkspacePath(session.config);
|
|
923
|
-
const normalizedPrompt = normalizeUserInput(input.prompt).trim();
|
|
924
|
-
if (!normalizedPrompt) {
|
|
925
|
-
return {
|
|
926
|
-
prompt: "",
|
|
927
|
-
userImages: input.userImages,
|
|
928
|
-
userFiles: this.resolveAbsoluteFilePaths(
|
|
929
|
-
session.config.cwd,
|
|
930
|
-
input.userFiles,
|
|
931
|
-
),
|
|
932
|
-
};
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
const enriched = await enrichPromptWithMentions(
|
|
936
|
-
normalizedPrompt,
|
|
937
|
-
mentionBaseDir,
|
|
938
|
-
);
|
|
939
|
-
emitMentionTelemetry(session.config.telemetry, enriched);
|
|
940
|
-
|
|
941
|
-
const prompt = formatModePrompt(enriched.prompt, session.config.mode);
|
|
942
|
-
const explicitUserFiles = this.resolveAbsoluteFilePaths(
|
|
943
|
-
session.config.cwd,
|
|
944
|
-
input.userFiles,
|
|
945
|
-
);
|
|
946
|
-
const mentionedFiles = this.resolveAbsoluteFilePaths(
|
|
947
|
-
mentionBaseDir,
|
|
948
|
-
enriched.matchedFiles,
|
|
949
|
-
);
|
|
950
|
-
const mergedUserFiles = Array.from(
|
|
951
|
-
new Set([...explicitUserFiles, ...mentionedFiles]),
|
|
952
|
-
);
|
|
953
|
-
|
|
954
|
-
return {
|
|
955
|
-
prompt,
|
|
956
|
-
userImages: input.userImages,
|
|
957
|
-
userFiles: mergedUserFiles.length > 0 ? mergedUserFiles : undefined,
|
|
958
|
-
};
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
// ── Session lifecycle ───────────────────────────────────────────────
|
|
962
|
-
|
|
963
|
-
private async ensureSessionPersisted(session: ActiveSession): Promise<void> {
|
|
964
|
-
if (session.artifacts) return;
|
|
965
|
-
const workspacePath = resolveWorkspacePath(session.config);
|
|
966
|
-
session.artifacts = (await this.invoke("createRootSessionWithArtifacts", {
|
|
967
|
-
sessionId: session.sessionId,
|
|
968
|
-
source: session.source,
|
|
969
|
-
pid: process.pid,
|
|
970
|
-
interactive: session.interactive,
|
|
971
|
-
provider: session.config.providerId,
|
|
972
|
-
model: session.config.modelId,
|
|
973
|
-
cwd: session.config.cwd,
|
|
974
|
-
workspaceRoot: workspacePath,
|
|
975
|
-
teamName: session.config.teamName,
|
|
976
|
-
enableTools: session.config.enableTools,
|
|
977
|
-
enableSpawn: session.config.enableSpawnAgent,
|
|
978
|
-
enableTeams: session.config.enableAgentTeams,
|
|
979
|
-
prompt: session.pendingPrompt,
|
|
980
|
-
metadata: session.sessionMetadata,
|
|
981
|
-
startedAt: session.startedAt,
|
|
982
|
-
})) as RootSessionArtifacts;
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
private async persistSessionMetadata(
|
|
986
|
-
sessionId: string,
|
|
987
|
-
resolveMetadata: (
|
|
988
|
-
current: Record<string, unknown> | undefined,
|
|
989
|
-
) => Record<string, unknown> | undefined,
|
|
990
|
-
): Promise<void> {
|
|
991
|
-
const session = this.sessions.get(sessionId);
|
|
992
|
-
const currentManifest =
|
|
993
|
-
(await this.invokeOptionalValue<SessionManifest>(
|
|
994
|
-
"readSessionManifest",
|
|
995
|
-
sessionId,
|
|
996
|
-
)) ?? session?.artifacts?.manifest;
|
|
997
|
-
const metadata = resolveMetadata(
|
|
998
|
-
currentManifest?.metadata as Record<string, unknown> | undefined,
|
|
999
|
-
);
|
|
1000
|
-
if (!session?.artifacts) {
|
|
1001
|
-
return;
|
|
1002
|
-
}
|
|
1003
|
-
const result = await this.invokeOptionalValue<{ updated?: boolean }>(
|
|
1004
|
-
"updateSession",
|
|
1005
|
-
{
|
|
1006
|
-
sessionId,
|
|
1007
|
-
metadata,
|
|
1008
|
-
},
|
|
1009
|
-
);
|
|
1010
|
-
if (result?.updated === false) {
|
|
1011
|
-
return;
|
|
1012
|
-
}
|
|
1013
|
-
session.artifacts.manifest.metadata = metadata;
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
private async finalizeSingleRun(
|
|
1017
|
-
session: ActiveSession,
|
|
1018
|
-
finishReason: AgentResult["finishReason"],
|
|
1019
|
-
): Promise<void> {
|
|
1020
|
-
if (hasPendingTeamRunWork(session)) return;
|
|
1021
|
-
const isAborted = finishReason === "aborted" || session.aborting;
|
|
1022
|
-
const isError = finishReason === "error";
|
|
1023
|
-
await this.shutdownSession(session, {
|
|
1024
|
-
status: isAborted ? "cancelled" : isError ? "failed" : "completed",
|
|
1025
|
-
exitCode: isError ? 1 : 0,
|
|
1026
|
-
shutdownReason: isError ? "session_error" : "session_complete",
|
|
1027
|
-
endReason: finishReason,
|
|
1028
|
-
});
|
|
1029
|
-
}
|
|
1030
|
-
|
|
1031
|
-
private async failSession(session: ActiveSession): Promise<void> {
|
|
1032
|
-
await this.shutdownSession(session, {
|
|
1033
|
-
status: "failed",
|
|
1034
|
-
exitCode: 1,
|
|
1035
|
-
shutdownReason: "session_error",
|
|
1036
|
-
endReason: "error",
|
|
1037
|
-
});
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
private async shutdownSession(
|
|
1041
|
-
session: ActiveSession,
|
|
1042
|
-
input: {
|
|
1043
|
-
status: SessionStatus;
|
|
1044
|
-
exitCode: number | null;
|
|
1045
|
-
shutdownReason: string;
|
|
1046
|
-
endReason: string;
|
|
1047
|
-
},
|
|
1048
|
-
): Promise<void> {
|
|
1049
|
-
if (input.status === "completed") {
|
|
1050
|
-
captureTaskCompleted(session.config.telemetry, {
|
|
1051
|
-
ulid: session.sessionId,
|
|
1052
|
-
provider: session.config.providerId,
|
|
1053
|
-
modelId: session.config.modelId,
|
|
1054
|
-
mode: session.config.mode,
|
|
1055
|
-
durationMs: Date.now() - Date.parse(session.startedAt),
|
|
1056
|
-
...this.getSessionAgentTelemetryIdentity(session),
|
|
1057
|
-
});
|
|
1058
|
-
}
|
|
1059
|
-
notifyTeamRunWaiters(session);
|
|
1060
|
-
|
|
1061
|
-
const cleanupErrors: unknown[] = [];
|
|
1062
|
-
const recordCleanupError = (stage: string, error: unknown) => {
|
|
1063
|
-
cleanupErrors.push(error);
|
|
1064
|
-
session.config.logger?.log("Session shutdown cleanup failed", {
|
|
1065
|
-
sessionId: session.sessionId,
|
|
1066
|
-
stage,
|
|
1067
|
-
error,
|
|
1068
|
-
severity: "warn",
|
|
1069
|
-
});
|
|
1070
|
-
};
|
|
1071
|
-
|
|
1072
|
-
if (session.artifacts) {
|
|
1073
|
-
try {
|
|
1074
|
-
await this.updateStatus(session, input.status, input.exitCode);
|
|
1075
|
-
} catch (error) {
|
|
1076
|
-
recordCleanupError("update_status", error);
|
|
1077
|
-
}
|
|
1078
|
-
try {
|
|
1079
|
-
await session.agent.shutdown(input.shutdownReason);
|
|
1080
|
-
} catch (error) {
|
|
1081
|
-
recordCleanupError("agent_shutdown", error);
|
|
1082
|
-
}
|
|
1083
|
-
}
|
|
1084
|
-
try {
|
|
1085
|
-
await Promise.resolve(session.runtime.shutdown(input.shutdownReason));
|
|
1086
|
-
} catch (error) {
|
|
1087
|
-
recordCleanupError("runtime_shutdown", error);
|
|
1088
|
-
}
|
|
1089
|
-
try {
|
|
1090
|
-
await session.pluginSandboxShutdown?.();
|
|
1091
|
-
} catch (error) {
|
|
1092
|
-
recordCleanupError("plugin_sandbox_shutdown", error);
|
|
1093
|
-
}
|
|
1094
|
-
this.sessions.delete(session.sessionId);
|
|
1095
|
-
this.emit({
|
|
1096
|
-
type: "ended",
|
|
1097
|
-
payload: {
|
|
1098
|
-
sessionId: session.sessionId,
|
|
1099
|
-
reason: input.endReason,
|
|
1100
|
-
ts: Date.now(),
|
|
1101
|
-
},
|
|
1102
|
-
});
|
|
1103
|
-
if (cleanupErrors.length > 0 && input.status === "failed") {
|
|
1104
|
-
throw cleanupErrors[0];
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
private async updateStatus(
|
|
1109
|
-
session: ActiveSession,
|
|
1110
|
-
status: SessionStatus,
|
|
1111
|
-
exitCode?: number | null,
|
|
1112
|
-
): Promise<void> {
|
|
1113
|
-
if (!session.artifacts) return;
|
|
1114
|
-
const result = await this.invoke<{ updated: boolean; endedAt?: string }>(
|
|
1115
|
-
"updateSessionStatus",
|
|
1116
|
-
session.sessionId,
|
|
1117
|
-
status,
|
|
1118
|
-
exitCode,
|
|
1119
|
-
);
|
|
1120
|
-
if (!result.updated) return;
|
|
1121
|
-
const latestManifest =
|
|
1122
|
-
(await this.invokeOptionalValue<SessionManifest>(
|
|
1123
|
-
"readSessionManifest",
|
|
1124
|
-
session.sessionId,
|
|
1125
|
-
)) ?? session.artifacts.manifest;
|
|
1126
|
-
latestManifest.status = status;
|
|
1127
|
-
latestManifest.ended_at = result.endedAt ?? nowIso();
|
|
1128
|
-
latestManifest.exit_code = typeof exitCode === "number" ? exitCode : null;
|
|
1129
|
-
session.artifacts.manifest = latestManifest;
|
|
1130
|
-
session.status = status;
|
|
1131
|
-
session.endedAt = latestManifest.ended_at;
|
|
1132
|
-
session.exitCode = latestManifest.exit_code;
|
|
1133
|
-
await this.invoke<void>(
|
|
1134
|
-
"writeSessionManifest",
|
|
1135
|
-
session.artifacts.manifestPath,
|
|
1136
|
-
latestManifest,
|
|
1137
|
-
);
|
|
1138
|
-
this.emitStatus(session.sessionId, status);
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
private async handlePluginEvent(
|
|
1142
|
-
rootSessionId: string,
|
|
1143
|
-
event: { name: string; payload?: unknown },
|
|
1144
|
-
fallbackAutomation?: NonNullable<
|
|
1145
|
-
CoreSessionConfig["extensionContext"]
|
|
1146
|
-
>["automation"],
|
|
1147
|
-
): Promise<void> {
|
|
1148
|
-
if (event.name === "plugin_log") {
|
|
1149
|
-
this.handlePluginLog(rootSessionId, event.payload);
|
|
1150
|
-
return;
|
|
1151
|
-
}
|
|
1152
|
-
if (event.name === "automation_event") {
|
|
1153
|
-
const session = this.sessions.get(rootSessionId);
|
|
1154
|
-
const automation =
|
|
1155
|
-
session?.config.extensionContext?.automation ?? fallbackAutomation;
|
|
1156
|
-
if (!automation) {
|
|
1157
|
-
return;
|
|
1158
|
-
}
|
|
1159
|
-
const payload =
|
|
1160
|
-
event.payload && typeof event.payload === "object"
|
|
1161
|
-
? (event.payload as AutomationEventEnvelope)
|
|
1162
|
-
: undefined;
|
|
1163
|
-
if (!payload) {
|
|
1164
|
-
return;
|
|
1165
|
-
}
|
|
1166
|
-
await automation.ingestEvent(payload);
|
|
1167
|
-
return;
|
|
1168
|
-
}
|
|
1169
|
-
if (
|
|
1170
|
-
event.name !== "steer_message" &&
|
|
1171
|
-
event.name !== "queue_message" &&
|
|
1172
|
-
event.name !== "pending_prompt"
|
|
1173
|
-
) {
|
|
1174
|
-
return;
|
|
1175
|
-
}
|
|
1176
|
-
const payload =
|
|
1177
|
-
event.payload && typeof event.payload === "object"
|
|
1178
|
-
? (event.payload as Record<string, unknown>)
|
|
1179
|
-
: undefined;
|
|
1180
|
-
const targetSessionId =
|
|
1181
|
-
typeof payload?.sessionId === "string" &&
|
|
1182
|
-
payload.sessionId.trim().length > 0
|
|
1183
|
-
? payload.sessionId.trim()
|
|
1184
|
-
: rootSessionId;
|
|
1185
|
-
const prompt =
|
|
1186
|
-
typeof payload?.prompt === "string" ? payload.prompt.trim() : "";
|
|
1187
|
-
if (!prompt) {
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
const delivery =
|
|
1191
|
-
event.name === "steer_message"
|
|
1192
|
-
? "steer"
|
|
1193
|
-
: event.name === "queue_message"
|
|
1194
|
-
? "queue"
|
|
1195
|
-
: payload?.delivery === "steer"
|
|
1196
|
-
? "steer"
|
|
1197
|
-
: "queue";
|
|
1198
|
-
this.enqueuePendingPrompt(targetSessionId, {
|
|
1199
|
-
prompt,
|
|
1200
|
-
delivery,
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
|
-
private handlePluginLog(
|
|
1205
|
-
rootSessionId: string,
|
|
1206
|
-
payload: unknown,
|
|
1207
|
-
fallbackLogger?: BasicLogger,
|
|
1208
|
-
): void {
|
|
1209
|
-
const session = this.sessions.get(rootSessionId);
|
|
1210
|
-
const logger =
|
|
1211
|
-
fallbackLogger ??
|
|
1212
|
-
session?.config.extensionContext?.logger ??
|
|
1213
|
-
session?.config.logger;
|
|
1214
|
-
if (!logger || !payload || typeof payload !== "object") {
|
|
1215
|
-
return;
|
|
1216
|
-
}
|
|
1217
|
-
const record = payload as Record<string, unknown>;
|
|
1218
|
-
const message = typeof record.message === "string" ? record.message : "";
|
|
1219
|
-
if (!message) {
|
|
1220
|
-
return;
|
|
1221
|
-
}
|
|
1222
|
-
const metadata =
|
|
1223
|
-
record.metadata && typeof record.metadata === "object"
|
|
1224
|
-
? ({
|
|
1225
|
-
...(record.metadata as Record<string, unknown>),
|
|
1226
|
-
} as BasicLogMetadata)
|
|
1227
|
-
: {};
|
|
1228
|
-
metadata.sessionId ??= rootSessionId;
|
|
1229
|
-
if (typeof record.pluginName === "string" && record.pluginName) {
|
|
1230
|
-
metadata.pluginName = record.pluginName;
|
|
1231
|
-
}
|
|
1232
|
-
if (record.level === "debug") {
|
|
1233
|
-
logger.debug(message, metadata);
|
|
1234
|
-
return;
|
|
1235
|
-
}
|
|
1236
|
-
if (record.level === "error") {
|
|
1237
|
-
if (logger.error) {
|
|
1238
|
-
logger.error(message, metadata);
|
|
1239
|
-
} else {
|
|
1240
|
-
logger.log(message, { ...metadata, severity: "error" });
|
|
1241
|
-
}
|
|
1242
|
-
return;
|
|
1243
|
-
}
|
|
1244
|
-
logger.log(message, metadata);
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
/**
|
|
1248
|
-
* Consume the first steer-delivery pending prompt for injection into the
|
|
1249
|
-
* running agent loop. Called synchronously by the agent between iterations.
|
|
1250
|
-
*/
|
|
1251
|
-
private consumeSteerMessage(sessionId: string): string | undefined {
|
|
1252
|
-
const session = this.sessions.get(sessionId);
|
|
1253
|
-
if (!session) {
|
|
1254
|
-
return undefined;
|
|
1255
|
-
}
|
|
1256
|
-
const steerIndex = session.pendingPrompts.findIndex(
|
|
1257
|
-
(entry) => entry.delivery === "steer",
|
|
1258
|
-
);
|
|
1259
|
-
if (steerIndex < 0) {
|
|
1260
|
-
return undefined;
|
|
1261
|
-
}
|
|
1262
|
-
const [steer] = session.pendingPrompts.splice(steerIndex, 1);
|
|
1263
|
-
this.emitPendingPrompts(session);
|
|
1264
|
-
this.emitPendingPromptSubmitted(session, steer);
|
|
1265
|
-
return steer.prompt;
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
|
-
private enqueuePendingPrompt(
|
|
1269
|
-
sessionId: string,
|
|
1270
|
-
entry: {
|
|
1271
|
-
prompt: string;
|
|
1272
|
-
delivery: "queue" | "steer";
|
|
1273
|
-
userImages?: string[];
|
|
1274
|
-
userFiles?: string[];
|
|
1275
|
-
},
|
|
1276
|
-
): void {
|
|
1277
|
-
const session = this.sessions.get(sessionId);
|
|
1278
|
-
if (!session || session.aborting) {
|
|
1279
|
-
return;
|
|
1280
|
-
}
|
|
1281
|
-
const { prompt, delivery, userImages, userFiles } = entry;
|
|
1282
|
-
const existingIndex = session.pendingPrompts.findIndex(
|
|
1283
|
-
(queued) => queued.prompt === prompt,
|
|
1284
|
-
);
|
|
1285
|
-
if (existingIndex >= 0) {
|
|
1286
|
-
const [existing] = session.pendingPrompts.splice(existingIndex, 1);
|
|
1287
|
-
if (delivery === "steer" || existing.delivery === "steer") {
|
|
1288
|
-
session.pendingPrompts.unshift({
|
|
1289
|
-
id: existing.id,
|
|
1290
|
-
prompt,
|
|
1291
|
-
delivery: "steer",
|
|
1292
|
-
userImages: userImages ?? existing.userImages,
|
|
1293
|
-
userFiles: userFiles ?? existing.userFiles,
|
|
1294
|
-
});
|
|
1295
|
-
} else {
|
|
1296
|
-
session.pendingPrompts.push({
|
|
1297
|
-
...existing,
|
|
1298
|
-
userImages: userImages ?? existing.userImages,
|
|
1299
|
-
userFiles: userFiles ?? existing.userFiles,
|
|
1300
|
-
});
|
|
1301
|
-
}
|
|
1302
|
-
} else if (delivery === "steer") {
|
|
1303
|
-
session.pendingPrompts.unshift({
|
|
1304
|
-
id: `pending_${Date.now()}_${nanoid(5)}`,
|
|
1305
|
-
prompt,
|
|
1306
|
-
delivery,
|
|
1307
|
-
userImages,
|
|
1308
|
-
userFiles,
|
|
1309
|
-
});
|
|
1310
|
-
} else {
|
|
1311
|
-
session.pendingPrompts.push({
|
|
1312
|
-
id: `pending_${Date.now()}_${nanoid(5)}`,
|
|
1313
|
-
prompt,
|
|
1314
|
-
delivery,
|
|
1315
|
-
userImages,
|
|
1316
|
-
userFiles,
|
|
1317
|
-
});
|
|
1318
|
-
}
|
|
1319
|
-
this.emitPendingPrompts(session);
|
|
1320
|
-
this.schedulePendingPromptDrain(sessionId, session);
|
|
1321
|
-
}
|
|
1322
|
-
|
|
1323
|
-
private schedulePendingPromptDrain(
|
|
1324
|
-
sessionId: string,
|
|
1325
|
-
session: ActiveSession,
|
|
1326
|
-
): void {
|
|
1327
|
-
if (
|
|
1328
|
-
session.pendingPrompts.length === 0 ||
|
|
1329
|
-
session.aborting ||
|
|
1330
|
-
session.drainingPendingPrompts ||
|
|
1331
|
-
!session.agent.canStartRun()
|
|
1332
|
-
) {
|
|
1333
|
-
return;
|
|
1334
|
-
}
|
|
1335
|
-
queueMicrotask(() => {
|
|
1336
|
-
void this.drainPendingPrompts(sessionId);
|
|
1337
|
-
});
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
private async drainPendingPrompts(sessionId: string): Promise<void> {
|
|
1341
|
-
const session = this.sessions.get(sessionId);
|
|
1342
|
-
if (!session || session.aborting || session.drainingPendingPrompts) {
|
|
1343
|
-
return;
|
|
1344
|
-
}
|
|
1345
|
-
const canStartRun = session.agent.canStartRun();
|
|
1346
|
-
if (!canStartRun) {
|
|
1347
|
-
return;
|
|
1348
|
-
}
|
|
1349
|
-
const next = session.pendingPrompts.shift();
|
|
1350
|
-
if (!next) {
|
|
1351
|
-
return;
|
|
1352
|
-
}
|
|
1353
|
-
this.emitPendingPrompts(session);
|
|
1354
|
-
this.emitPendingPromptSubmitted(session, next);
|
|
1355
|
-
session.drainingPendingPrompts = true;
|
|
1356
|
-
try {
|
|
1357
|
-
await this.send({
|
|
1358
|
-
sessionId,
|
|
1359
|
-
prompt: next.prompt,
|
|
1360
|
-
userImages: next.userImages,
|
|
1361
|
-
userFiles: next.userFiles,
|
|
1362
|
-
});
|
|
1363
|
-
} catch (error) {
|
|
1364
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1365
|
-
if (message.includes("already in progress")) {
|
|
1366
|
-
session.pendingPrompts.unshift(next);
|
|
1367
|
-
this.emitPendingPrompts(session);
|
|
1368
|
-
} else {
|
|
1369
|
-
throw error;
|
|
1370
|
-
}
|
|
1371
|
-
} finally {
|
|
1372
|
-
session.drainingPendingPrompts = false;
|
|
1373
|
-
if (session.pendingPrompts.length > 0) {
|
|
1374
|
-
queueMicrotask(() => {
|
|
1375
|
-
void this.drainPendingPrompts(sessionId);
|
|
1376
|
-
});
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
// ── Agent event handling ────────────────────────────────────────────
|
|
1382
|
-
|
|
1383
|
-
private onAgentEvent(
|
|
1384
|
-
sessionId: string,
|
|
1385
|
-
config: CoreSessionConfig,
|
|
1386
|
-
event: AgentEvent,
|
|
1387
|
-
): void {
|
|
1388
|
-
const liveSession = this.sessions.get(sessionId);
|
|
1389
|
-
const ctx: AgentEventContext = {
|
|
1390
|
-
sessionId,
|
|
1391
|
-
config,
|
|
1392
|
-
liveSession,
|
|
1393
|
-
usageBySession: this.usageBySession,
|
|
1394
|
-
persistMessages: (sid, messages, systemPrompt) => {
|
|
1395
|
-
void this.invoke<void>(
|
|
1396
|
-
"persistSessionMessages",
|
|
1397
|
-
sid,
|
|
1398
|
-
messages,
|
|
1399
|
-
systemPrompt,
|
|
1400
|
-
);
|
|
1401
|
-
},
|
|
1402
|
-
emit: (e) => this.emit(e),
|
|
1403
|
-
};
|
|
1404
|
-
const eventMetadata = extractAgentEventMetadata(event);
|
|
1405
|
-
const isRootAgentEvent =
|
|
1406
|
-
liveSession &&
|
|
1407
|
-
eventMetadata.agentId === this.readAgentId(liveSession.agent);
|
|
1408
|
-
handleAgentEvent(
|
|
1409
|
-
ctx,
|
|
1410
|
-
event,
|
|
1411
|
-
isRootAgentEvent
|
|
1412
|
-
? {
|
|
1413
|
-
isPrimaryAgentEvent: true,
|
|
1414
|
-
...(liveSession?.runtime.teamRuntime
|
|
1415
|
-
? { teamRole: "lead" as const }
|
|
1416
|
-
: {}),
|
|
1417
|
-
}
|
|
1418
|
-
: { isPrimaryAgentEvent: false },
|
|
1419
|
-
);
|
|
1420
|
-
}
|
|
1421
|
-
|
|
1422
|
-
private emitPendingPrompts(session: ActiveSession): void {
|
|
1423
|
-
this.emit({
|
|
1424
|
-
type: "pending_prompts",
|
|
1425
|
-
payload: {
|
|
1426
|
-
sessionId: session.sessionId,
|
|
1427
|
-
prompts: this.snapshotPendingPrompts(session),
|
|
1428
|
-
},
|
|
1429
|
-
});
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1432
|
-
private snapshotPendingPrompt(entry: PendingPrompt): SessionPendingPrompt {
|
|
1433
|
-
return {
|
|
1434
|
-
id: entry.id,
|
|
1435
|
-
prompt: entry.prompt,
|
|
1436
|
-
delivery: entry.delivery,
|
|
1437
|
-
attachmentCount:
|
|
1438
|
-
(entry.userImages?.length ?? 0) + (entry.userFiles?.length ?? 0),
|
|
1439
|
-
};
|
|
1440
|
-
}
|
|
1441
|
-
|
|
1442
|
-
private snapshotPendingPrompts(
|
|
1443
|
-
session: ActiveSession,
|
|
1444
|
-
): SessionPendingPrompt[] {
|
|
1445
|
-
return session.pendingPrompts.map((entry) =>
|
|
1446
|
-
this.snapshotPendingPrompt(entry),
|
|
1447
|
-
);
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
private emitPendingPromptSubmitted(
|
|
1451
|
-
session: ActiveSession,
|
|
1452
|
-
entry: {
|
|
1453
|
-
id: string;
|
|
1454
|
-
prompt: string;
|
|
1455
|
-
delivery: "queue" | "steer";
|
|
1456
|
-
userImages?: string[];
|
|
1457
|
-
userFiles?: string[];
|
|
1458
|
-
},
|
|
1459
|
-
): void {
|
|
1460
|
-
this.emit({
|
|
1461
|
-
type: "pending_prompt_submitted",
|
|
1462
|
-
payload: {
|
|
1463
|
-
sessionId: session.sessionId,
|
|
1464
|
-
id: entry.id,
|
|
1465
|
-
prompt: entry.prompt,
|
|
1466
|
-
delivery: entry.delivery,
|
|
1467
|
-
attachmentCount:
|
|
1468
|
-
(entry.userImages?.length ?? 0) + (entry.userFiles?.length ?? 0),
|
|
1469
|
-
},
|
|
1470
|
-
});
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
// ── Spawn / sub-agents ──────────────────────────────────────────────
|
|
1474
|
-
|
|
1475
|
-
private createSpawnTool(
|
|
1476
|
-
config: CoreSessionConfig,
|
|
1477
|
-
rootSessionId: string,
|
|
1478
|
-
toolExecutors?: Partial<ToolExecutors>,
|
|
1479
|
-
): Tool {
|
|
1480
|
-
const createSubAgentTools = () => {
|
|
1481
|
-
const tools: Tool[] = config.enableTools
|
|
1482
|
-
? createBuiltinTools({
|
|
1483
|
-
cwd: config.cwd,
|
|
1484
|
-
...ToolPresets[
|
|
1485
|
-
resolveToolPresetName({
|
|
1486
|
-
mode: config.mode,
|
|
1487
|
-
})
|
|
1488
|
-
],
|
|
1489
|
-
executors: toolExecutors,
|
|
1490
|
-
})
|
|
1491
|
-
: [];
|
|
1492
|
-
if (config.enableSpawnAgent) {
|
|
1493
|
-
tools.push(this.createSpawnTool(config, rootSessionId, toolExecutors));
|
|
1494
|
-
}
|
|
1495
|
-
return filterDisabledTools(tools);
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
|
-
return createSpawnAgentTool({
|
|
1499
|
-
configProvider: {
|
|
1500
|
-
getRuntimeConfig: () =>
|
|
1501
|
-
this.sessions
|
|
1502
|
-
.get(rootSessionId)
|
|
1503
|
-
?.runtime.delegatedAgentConfigProvider?.getRuntimeConfig() ?? {
|
|
1504
|
-
providerId: config.providerId,
|
|
1505
|
-
modelId: config.modelId,
|
|
1506
|
-
cwd: config.cwd,
|
|
1507
|
-
apiKey: config.apiKey,
|
|
1508
|
-
baseUrl: config.baseUrl,
|
|
1509
|
-
headers: config.headers,
|
|
1510
|
-
providerConfig: config.providerConfig,
|
|
1511
|
-
knownModels: config.knownModels,
|
|
1512
|
-
thinking: config.thinking,
|
|
1513
|
-
maxIterations: config.maxIterations,
|
|
1514
|
-
hooks: config.hooks,
|
|
1515
|
-
extensions: config.extensions,
|
|
1516
|
-
logger: config.logger,
|
|
1517
|
-
telemetry: config.telemetry,
|
|
1518
|
-
},
|
|
1519
|
-
getConnectionConfig: () =>
|
|
1520
|
-
this.sessions
|
|
1521
|
-
.get(rootSessionId)
|
|
1522
|
-
?.runtime.delegatedAgentConfigProvider?.getConnectionConfig() ?? {
|
|
1523
|
-
providerId: config.providerId,
|
|
1524
|
-
modelId: config.modelId,
|
|
1525
|
-
apiKey: config.apiKey,
|
|
1526
|
-
baseUrl: config.baseUrl,
|
|
1527
|
-
headers: config.headers,
|
|
1528
|
-
providerConfig: config.providerConfig,
|
|
1529
|
-
knownModels: config.knownModels,
|
|
1530
|
-
thinking: config.thinking,
|
|
1531
|
-
},
|
|
1532
|
-
updateConnectionDefaults: () => {},
|
|
1533
|
-
},
|
|
1534
|
-
createSubAgentTools,
|
|
1535
|
-
onSubAgentEvent: (event) =>
|
|
1536
|
-
this.onAgentEvent(rootSessionId, config, event),
|
|
1537
|
-
onSubAgentStart: (context) => {
|
|
1538
|
-
const teamRuntime =
|
|
1539
|
-
this.sessions.get(rootSessionId)?.runtime.teamRuntime;
|
|
1540
|
-
this.subAgentStarts.set(context.subAgentId, {
|
|
1541
|
-
startedAt: Date.now(),
|
|
1542
|
-
rootSessionId,
|
|
1543
|
-
});
|
|
1544
|
-
const agentIdentity = buildTelemetryAgentIdentity({
|
|
1545
|
-
agentId: context.subAgentId,
|
|
1546
|
-
conversationId: context.conversationId,
|
|
1547
|
-
parentAgentId: context.parentAgentId,
|
|
1548
|
-
teamId: teamRuntime?.getTeamId(),
|
|
1549
|
-
teamName: teamRuntime?.getTeamName(),
|
|
1550
|
-
createdByAgentId: context.parentAgentId,
|
|
1551
|
-
});
|
|
1552
|
-
if (agentIdentity) {
|
|
1553
|
-
captureAgentCreated(config.telemetry, {
|
|
1554
|
-
ulid: rootSessionId,
|
|
1555
|
-
modelId: config.modelId,
|
|
1556
|
-
provider: config.providerId,
|
|
1557
|
-
...agentIdentity,
|
|
1558
|
-
});
|
|
1559
|
-
}
|
|
1560
|
-
captureSubagentExecution(config.telemetry, {
|
|
1561
|
-
event: "started",
|
|
1562
|
-
ulid: rootSessionId,
|
|
1563
|
-
durationMs: 0,
|
|
1564
|
-
parentId: context.parentAgentId,
|
|
1565
|
-
agentId: context.subAgentId,
|
|
1566
|
-
...agentIdentity,
|
|
1567
|
-
});
|
|
1568
|
-
void this.invokeOptional("handleSubAgentStart", rootSessionId, context);
|
|
1569
|
-
},
|
|
1570
|
-
onSubAgentEnd: (context) => {
|
|
1571
|
-
const teamRuntime =
|
|
1572
|
-
this.sessions.get(rootSessionId)?.runtime.teamRuntime;
|
|
1573
|
-
const started = this.subAgentStarts.get(context.subAgentId);
|
|
1574
|
-
const durationMs = started ? Date.now() - started.startedAt : 0;
|
|
1575
|
-
const outputLines = context.result?.text
|
|
1576
|
-
? context.result.text.split("\n").length
|
|
1577
|
-
: 0;
|
|
1578
|
-
captureSubagentExecution(config.telemetry, {
|
|
1579
|
-
event: "ended",
|
|
1580
|
-
ulid: rootSessionId,
|
|
1581
|
-
durationMs,
|
|
1582
|
-
outputLines,
|
|
1583
|
-
errorMessage: context.error ? String(context.error) : undefined,
|
|
1584
|
-
agentId: context.subAgentId,
|
|
1585
|
-
parentId: context.parentAgentId,
|
|
1586
|
-
...buildTelemetryAgentIdentity({
|
|
1587
|
-
agentId: context.subAgentId,
|
|
1588
|
-
conversationId: context.conversationId,
|
|
1589
|
-
parentAgentId: context.parentAgentId,
|
|
1590
|
-
teamId: teamRuntime?.getTeamId(),
|
|
1591
|
-
teamName: teamRuntime?.getTeamName(),
|
|
1592
|
-
createdByAgentId: context.parentAgentId,
|
|
1593
|
-
}),
|
|
1594
|
-
});
|
|
1595
|
-
this.subAgentStarts.delete(context.subAgentId);
|
|
1596
|
-
void this.invokeOptional("handleSubAgentEnd", rootSessionId, context);
|
|
1597
|
-
},
|
|
1598
|
-
}) as Tool;
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
// ── Team run coordination ───────────────────────────────────────────
|
|
1602
|
-
|
|
1603
|
-
private async handleTeamEvent(
|
|
1604
|
-
rootSessionId: string,
|
|
1605
|
-
event: TeamEvent,
|
|
1606
|
-
): Promise<void> {
|
|
1607
|
-
const session = this.sessions.get(rootSessionId);
|
|
1608
|
-
if (session) {
|
|
1609
|
-
trackTeamRunState(session, event);
|
|
1610
|
-
if (event.type === "agent_event") {
|
|
1611
|
-
const ctx: AgentEventContext = {
|
|
1612
|
-
sessionId: rootSessionId,
|
|
1613
|
-
config: session.config,
|
|
1614
|
-
liveSession: session,
|
|
1615
|
-
usageBySession: this.usageBySession,
|
|
1616
|
-
persistMessages: (sid, messages, systemPrompt) => {
|
|
1617
|
-
void this.invoke<void>(
|
|
1618
|
-
"persistSessionMessages",
|
|
1619
|
-
sid,
|
|
1620
|
-
messages,
|
|
1621
|
-
systemPrompt,
|
|
1622
|
-
);
|
|
1623
|
-
},
|
|
1624
|
-
emit: (e) => this.emit(e),
|
|
1625
|
-
};
|
|
1626
|
-
handleAgentEvent(ctx, event.event, {
|
|
1627
|
-
teamRole: "teammate",
|
|
1628
|
-
teamAgentId: event.agentId,
|
|
1629
|
-
isPrimaryAgentEvent: false,
|
|
1630
|
-
});
|
|
1631
|
-
}
|
|
1632
|
-
if (event.type === "teammate_spawned") {
|
|
1633
|
-
const agentIdentity = buildTelemetryAgentIdentity({
|
|
1634
|
-
agentId: event.teammate.runtimeAgentId ?? event.agentId,
|
|
1635
|
-
conversationId: event.teammate.conversationId,
|
|
1636
|
-
parentAgentId: event.teammate.parentAgentId,
|
|
1637
|
-
createdByAgentId: this.readAgentId(session.agent),
|
|
1638
|
-
teamId: session.runtime.teamRuntime?.getTeamId(),
|
|
1639
|
-
teamName: session.runtime.teamRuntime?.getTeamName(),
|
|
1640
|
-
teamRole: "teammate",
|
|
1641
|
-
teamAgentId: event.agentId,
|
|
1642
|
-
});
|
|
1643
|
-
if (agentIdentity) {
|
|
1644
|
-
captureAgentCreated(session.config.telemetry, {
|
|
1645
|
-
ulid: rootSessionId,
|
|
1646
|
-
modelId: event.teammate.modelId ?? session.config.modelId,
|
|
1647
|
-
provider: session.config.providerId,
|
|
1648
|
-
...agentIdentity,
|
|
1649
|
-
});
|
|
1650
|
-
}
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
|
|
1654
|
-
await dispatchTeamEventToBackend(
|
|
1655
|
-
rootSessionId,
|
|
1656
|
-
event,
|
|
1657
|
-
this.invokeOptional.bind(this),
|
|
1658
|
-
);
|
|
1659
|
-
|
|
1660
|
-
if (session) {
|
|
1661
|
-
emitTeamProgress(session, rootSessionId, event, (e) => this.emit(e));
|
|
1662
|
-
}
|
|
1663
|
-
}
|
|
1664
|
-
|
|
1665
|
-
// ── OAuth & auth ────────────────────────────────────────────────────
|
|
1666
|
-
|
|
1667
|
-
private async runWithAuthRetry(
|
|
1668
|
-
session: ActiveSession,
|
|
1669
|
-
run: () => Promise<AgentResult>,
|
|
1670
|
-
baselineMessages: LlmsProviders.Message[],
|
|
1671
|
-
): Promise<AgentResult> {
|
|
1672
|
-
try {
|
|
1673
|
-
return await run();
|
|
1674
|
-
} catch (error) {
|
|
1675
|
-
if (!isLikelyAuthError(error, session.config.providerId)) {
|
|
1676
|
-
throw error;
|
|
1677
|
-
}
|
|
1678
|
-
await this.syncOAuthCredentials(session, { forceRefresh: true });
|
|
1679
|
-
session.agent.restore(baselineMessages);
|
|
1680
|
-
return run();
|
|
1681
|
-
}
|
|
1682
|
-
}
|
|
1683
|
-
|
|
1684
|
-
private async syncOAuthCredentials(
|
|
1685
|
-
session: ActiveSession,
|
|
1686
|
-
options?: { forceRefresh?: boolean },
|
|
1687
|
-
): Promise<void> {
|
|
1688
|
-
let resolved: RuntimeOAuthResolution | null = null;
|
|
1689
|
-
try {
|
|
1690
|
-
resolved = await this.oauthTokenManager.resolveProviderApiKey({
|
|
1691
|
-
providerId: session.config.providerId,
|
|
1692
|
-
forceRefresh: options?.forceRefresh,
|
|
1693
|
-
});
|
|
1694
|
-
} catch (error) {
|
|
1695
|
-
if (error instanceof OAuthReauthRequiredError) {
|
|
1696
|
-
throw new Error(`${error.providerId} requires re-authentication.`);
|
|
1697
|
-
}
|
|
1698
|
-
throw error;
|
|
1699
|
-
}
|
|
1700
|
-
if (!resolved?.apiKey || session.config.apiKey === resolved.apiKey) return;
|
|
1701
|
-
session.config.apiKey = resolved.apiKey;
|
|
1702
|
-
this.updateAgentConnection(session, { apiKey: resolved.apiKey });
|
|
1703
|
-
session.runtime.delegatedAgentConfigProvider?.updateConnectionDefaults({
|
|
1704
|
-
apiKey: resolved.apiKey,
|
|
1705
|
-
});
|
|
1706
|
-
session.runtime.teamRuntime?.updateTeammateConnections({
|
|
1707
|
-
apiKey: resolved.apiKey,
|
|
1708
|
-
});
|
|
1709
|
-
}
|
|
1710
|
-
|
|
1711
|
-
// ── Utility methods ─────────────────────────────────────────────────
|
|
1712
|
-
|
|
1713
|
-
private getSessionOrThrow(sessionId: string): ActiveSession {
|
|
1714
|
-
const session = this.sessions.get(sessionId);
|
|
1715
|
-
if (!session) throw new Error(`session not found: ${sessionId}`);
|
|
1716
|
-
return session;
|
|
1717
|
-
}
|
|
1718
|
-
|
|
1719
|
-
private resolveAbsoluteFilePaths(cwd: string, paths?: string[]): string[] {
|
|
1720
|
-
if (!paths || paths.length === 0) return [];
|
|
1721
|
-
const resolved = paths
|
|
1722
|
-
.map((p) => p.trim())
|
|
1723
|
-
.filter((p) => p.length > 0)
|
|
1724
|
-
.map((p) => (isAbsolute(p) ? p : resolve(cwd, p)));
|
|
1725
|
-
return Array.from(new Set(resolved));
|
|
1726
|
-
}
|
|
1727
|
-
|
|
1728
|
-
private updateAgentConnection(
|
|
1729
|
-
session: ActiveSession,
|
|
1730
|
-
overrides: { apiKey?: string; modelId?: string },
|
|
1731
|
-
): void {
|
|
1732
|
-
session.agent.updateConnection(overrides);
|
|
1733
|
-
}
|
|
1734
|
-
|
|
1735
|
-
private getSessionAgentTelemetryIdentity(session: ActiveSession) {
|
|
1736
|
-
return buildTelemetryAgentIdentity({
|
|
1737
|
-
agentId: this.readAgentId(session.agent),
|
|
1738
|
-
conversationId: this.readAgentConversationId(session.agent),
|
|
1739
|
-
teamId: session.runtime.teamRuntime?.getTeamId(),
|
|
1740
|
-
teamName: session.runtime.teamRuntime?.getTeamName(),
|
|
1741
|
-
teamRole: session.runtime.teamRuntime ? "lead" : undefined,
|
|
1742
|
-
});
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
private readAgentId(agent: SessionRuntime): string {
|
|
1746
|
-
return agent.getAgentId();
|
|
1747
|
-
}
|
|
1748
|
-
|
|
1749
|
-
private readAgentConversationId(agent: SessionRuntime): string {
|
|
1750
|
-
return agent.getConversationId();
|
|
1751
|
-
}
|
|
1752
|
-
|
|
1753
|
-
private emitStatus(sessionId: string, status: string): void {
|
|
1754
|
-
this.emit({
|
|
1755
|
-
type: "status",
|
|
1756
|
-
payload: { sessionId, status },
|
|
1757
|
-
});
|
|
1758
|
-
}
|
|
1759
|
-
|
|
1760
|
-
private emit(event: CoreSessionEvent): void {
|
|
1761
|
-
this.events.emit(event);
|
|
1762
|
-
}
|
|
1763
|
-
|
|
1764
|
-
private async listRows(limit: number): Promise<SessionRow[]> {
|
|
1765
|
-
return this.invoke<SessionRow[]>(
|
|
1766
|
-
"listSessions",
|
|
1767
|
-
Math.min(Math.max(1, Math.floor(limit)), MAX_SCAN_LIMIT),
|
|
1768
|
-
);
|
|
1769
|
-
}
|
|
1770
|
-
|
|
1771
|
-
private async getRow(sessionId: string): Promise<SessionRow | undefined> {
|
|
1772
|
-
const target = sessionId.trim();
|
|
1773
|
-
if (!target) return undefined;
|
|
1774
|
-
const rows = await this.listRows(MAX_SCAN_LIMIT);
|
|
1775
|
-
return rows.find((row) => row.sessionId === target);
|
|
1776
|
-
}
|
|
1777
|
-
|
|
1778
|
-
// ── Session service invocation ──────────────────────────────────────
|
|
1779
|
-
|
|
1780
|
-
private async invoke<T>(method: string, ...args: unknown[]): Promise<T> {
|
|
1781
|
-
const callable = (
|
|
1782
|
-
this.sessionService as unknown as Record<string, unknown>
|
|
1783
|
-
)[method];
|
|
1784
|
-
if (typeof callable !== "function") {
|
|
1785
|
-
throw new Error(`session service method not available: ${method}`);
|
|
1786
|
-
}
|
|
1787
|
-
return Promise.resolve(
|
|
1788
|
-
(callable as (...params: unknown[]) => T | Promise<T>).apply(
|
|
1789
|
-
this.sessionService,
|
|
1790
|
-
args,
|
|
1791
|
-
),
|
|
1792
|
-
);
|
|
1793
|
-
}
|
|
1794
|
-
|
|
1795
|
-
private async invokeOptional(
|
|
1796
|
-
method: string,
|
|
1797
|
-
...args: unknown[]
|
|
1798
|
-
): Promise<void> {
|
|
1799
|
-
const callable = (
|
|
1800
|
-
this.sessionService as unknown as Record<string, unknown>
|
|
1801
|
-
)[method];
|
|
1802
|
-
if (typeof callable !== "function") return;
|
|
1803
|
-
await Promise.resolve(
|
|
1804
|
-
(callable as (...params: unknown[]) => unknown).apply(
|
|
1805
|
-
this.sessionService,
|
|
1806
|
-
args,
|
|
1807
|
-
),
|
|
1808
|
-
);
|
|
1809
|
-
}
|
|
1810
|
-
|
|
1811
|
-
private async invokeOptionalValue<T = unknown>(
|
|
1812
|
-
method: string,
|
|
1813
|
-
...args: unknown[]
|
|
1814
|
-
): Promise<T | undefined> {
|
|
1815
|
-
const callable = (
|
|
1816
|
-
this.sessionService as unknown as Record<string, unknown>
|
|
1817
|
-
)[method];
|
|
1818
|
-
if (typeof callable !== "function") return undefined;
|
|
1819
|
-
return await Promise.resolve(
|
|
1820
|
-
(callable as (...params: unknown[]) => T | Promise<T>).apply(
|
|
1821
|
-
this.sessionService,
|
|
1822
|
-
args,
|
|
1823
|
-
),
|
|
1824
|
-
);
|
|
1825
|
-
}
|
|
1826
|
-
}
|