@elizaos/plugin-coding-agent 0.1.0-next.0

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.
Files changed (79) hide show
  1. package/README.md +169 -0
  2. package/dist/index.js +5788 -0
  3. package/dist/index.js.map +45 -0
  4. package/dist/src/actions/coding-task-handlers.d.ts +44 -0
  5. package/dist/src/actions/coding-task-handlers.d.ts.map +1 -0
  6. package/dist/src/actions/coding-task-helpers.d.ts +27 -0
  7. package/dist/src/actions/coding-task-helpers.d.ts.map +1 -0
  8. package/dist/src/actions/finalize-workspace.d.ts +11 -0
  9. package/dist/src/actions/finalize-workspace.d.ts.map +1 -0
  10. package/dist/src/actions/list-agents.d.ts +11 -0
  11. package/dist/src/actions/list-agents.d.ts.map +1 -0
  12. package/dist/src/actions/manage-issues.d.ts +11 -0
  13. package/dist/src/actions/manage-issues.d.ts.map +1 -0
  14. package/dist/src/actions/provision-workspace.d.ts +11 -0
  15. package/dist/src/actions/provision-workspace.d.ts.map +1 -0
  16. package/dist/src/actions/send-to-agent.d.ts +11 -0
  17. package/dist/src/actions/send-to-agent.d.ts.map +1 -0
  18. package/dist/src/actions/spawn-agent.d.ts +11 -0
  19. package/dist/src/actions/spawn-agent.d.ts.map +1 -0
  20. package/dist/src/actions/start-coding-task.d.ts +17 -0
  21. package/dist/src/actions/start-coding-task.d.ts.map +1 -0
  22. package/dist/src/actions/stop-agent.d.ts +11 -0
  23. package/dist/src/actions/stop-agent.d.ts.map +1 -0
  24. package/dist/src/api/agent-routes.d.ts +18 -0
  25. package/dist/src/api/agent-routes.d.ts.map +1 -0
  26. package/dist/src/api/coordinator-routes.d.ts +22 -0
  27. package/dist/src/api/coordinator-routes.d.ts.map +1 -0
  28. package/dist/src/api/issue-routes.d.ts +17 -0
  29. package/dist/src/api/issue-routes.d.ts.map +1 -0
  30. package/dist/src/api/routes.d.ts +36 -0
  31. package/dist/src/api/routes.d.ts.map +1 -0
  32. package/dist/src/api/workspace-routes.d.ts +17 -0
  33. package/dist/src/api/workspace-routes.d.ts.map +1 -0
  34. package/dist/src/index.d.ts +32 -0
  35. package/dist/src/index.d.ts.map +1 -0
  36. package/dist/src/providers/action-examples.d.ts +13 -0
  37. package/dist/src/providers/action-examples.d.ts.map +1 -0
  38. package/dist/src/providers/active-workspace-context.d.ts +13 -0
  39. package/dist/src/providers/active-workspace-context.d.ts.map +1 -0
  40. package/dist/src/services/agent-metrics.d.ts +28 -0
  41. package/dist/src/services/agent-metrics.d.ts.map +1 -0
  42. package/dist/src/services/agent-selection.d.ts +53 -0
  43. package/dist/src/services/agent-selection.d.ts.map +1 -0
  44. package/dist/src/services/ansi-utils.d.ts +48 -0
  45. package/dist/src/services/ansi-utils.d.ts.map +1 -0
  46. package/dist/src/services/pty-auto-response.d.ts +30 -0
  47. package/dist/src/services/pty-auto-response.d.ts.map +1 -0
  48. package/dist/src/services/pty-init.d.ts +43 -0
  49. package/dist/src/services/pty-init.d.ts.map +1 -0
  50. package/dist/src/services/pty-service.d.ts +92 -0
  51. package/dist/src/services/pty-service.d.ts.map +1 -0
  52. package/dist/src/services/pty-session-io.d.ts +46 -0
  53. package/dist/src/services/pty-session-io.d.ts.map +1 -0
  54. package/dist/src/services/pty-spawn.d.ts +44 -0
  55. package/dist/src/services/pty-spawn.d.ts.map +1 -0
  56. package/dist/src/services/pty-types.d.ts +80 -0
  57. package/dist/src/services/pty-types.d.ts.map +1 -0
  58. package/dist/src/services/stall-classifier.d.ts +44 -0
  59. package/dist/src/services/stall-classifier.d.ts.map +1 -0
  60. package/dist/src/services/swarm-coordinator-prompts.d.ts +61 -0
  61. package/dist/src/services/swarm-coordinator-prompts.d.ts.map +1 -0
  62. package/dist/src/services/swarm-coordinator.d.ts +155 -0
  63. package/dist/src/services/swarm-coordinator.d.ts.map +1 -0
  64. package/dist/src/services/swarm-decision-loop.d.ts +37 -0
  65. package/dist/src/services/swarm-decision-loop.d.ts.map +1 -0
  66. package/dist/src/services/swarm-idle-watchdog.d.ts +22 -0
  67. package/dist/src/services/swarm-idle-watchdog.d.ts.map +1 -0
  68. package/dist/src/services/workspace-git-ops.d.ts +28 -0
  69. package/dist/src/services/workspace-git-ops.d.ts.map +1 -0
  70. package/dist/src/services/workspace-github.d.ts +58 -0
  71. package/dist/src/services/workspace-github.d.ts.map +1 -0
  72. package/dist/src/services/workspace-lifecycle.d.ts +18 -0
  73. package/dist/src/services/workspace-lifecycle.d.ts.map +1 -0
  74. package/dist/src/services/workspace-service.d.ts +84 -0
  75. package/dist/src/services/workspace-service.d.ts.map +1 -0
  76. package/dist/src/services/workspace-types.d.ts +81 -0
  77. package/dist/src/services/workspace-types.d.ts.map +1 -0
  78. package/dist/tsconfig.build.tsbuildinfo +1 -0
  79. package/package.json +66 -0
@@ -0,0 +1,43 @@
1
+ /**
2
+ * PTY manager initialization — extracted from PTYService.initialize().
3
+ *
4
+ * Creates either a BunCompatiblePTYManager (for Bun runtime) or PTYManager
5
+ * (for Node), wires up event handlers, and returns the configured manager.
6
+ *
7
+ * @module services/pty-init
8
+ */
9
+ import { BunCompatiblePTYManager, PTYManager, type StallClassification } from "pty-manager";
10
+ import type { PTYServiceConfig } from "./pty-types.js";
11
+ /**
12
+ * All callbacks and state that the initialization logic needs
13
+ * from the surrounding PTYService instance.
14
+ */
15
+ export interface InitContext {
16
+ serviceConfig: PTYServiceConfig;
17
+ classifyStall: (sessionId: string, recentOutput: string) => Promise<StallClassification | null>;
18
+ emitEvent: (sessionId: string, event: string, data: unknown) => void;
19
+ handleGeminiAuth: (sessionId: string) => void;
20
+ sessionOutputBuffers: Map<string, string[]>;
21
+ taskResponseMarkers: Map<string, number>;
22
+ metricsTracker: {
23
+ recordCompletion(type: string, method: string, durationMs: number): void;
24
+ };
25
+ traceEntries: Array<string | Record<string, unknown>>;
26
+ maxTraceEntries: number;
27
+ log: (msg: string) => void;
28
+ }
29
+ /** Value returned by {@link initializePTYManager}. */
30
+ export interface InitResult {
31
+ manager: PTYManager | BunCompatiblePTYManager;
32
+ usingBunWorker: boolean;
33
+ }
34
+ /**
35
+ * Create and configure a PTY manager for the current runtime.
36
+ *
37
+ * - **Bun**: instantiates a {@link BunCompatiblePTYManager} that spawns a
38
+ * Node worker process and communicates via JSON-RPC over stdio.
39
+ * - **Node**: instantiates a {@link PTYManager} directly and registers
40
+ * all built-in adapters in-process.
41
+ */
42
+ export declare function initializePTYManager(ctx: InitContext): Promise<InitResult>;
43
+ //# sourceMappingURL=pty-init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pty-init.d.ts","sourceRoot":"","sources":["../../../src/services/pty-init.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EACL,uBAAuB,EAEvB,UAAU,EAKV,KAAK,mBAAmB,EAGzB,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAcvD;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,aAAa,EAAE,gBAAgB,CAAC;IAChC,aAAa,EAAE,CACb,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,KACjB,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;IACzC,SAAS,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACrE,gBAAgB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC5C,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,cAAc,EAAE;QACd,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;KAC1E,CAAC;IACF,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACtD,eAAe,EAAE,MAAM,CAAC;IACxB,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC5B;AAED,sDAAsD;AACtD,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,UAAU,GAAG,uBAAuB,CAAC;IAC9C,cAAc,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,WAAW,GACf,OAAO,CAAC,UAAU,CAAC,CAgPrB"}
@@ -0,0 +1,92 @@
1
+ /** @module services/pty-service */
2
+ import { type IAgentRuntime } from "@elizaos/core";
3
+ import { type AdapterType, type AgentFileDescriptor, type ApprovalConfig, type ApprovalPreset, type PreflightResult, type WriteMemoryOptions } from "coding-agent-adapters";
4
+ import { PTYConsoleBridge } from "pty-console";
5
+ import type { SessionFilter, SessionMessage } from "pty-manager";
6
+ import { type AgentSelectionStrategy } from "./agent-selection.js";
7
+ import type { CodingAgentType, PTYServiceConfig, SessionEventCallback, SessionInfo, SpawnSessionOptions } from "./pty-types.js";
8
+ import { SwarmCoordinator } from "./swarm-coordinator.js";
9
+ export type { CodingAgentType, PTYServiceConfig, SessionEventName, SessionInfo, SpawnSessionOptions, } from "./pty-types.js";
10
+ export { normalizeAgentType } from "./pty-types.js";
11
+ /**
12
+ * Retrieve the SwarmCoordinator from the PTYService registered on the runtime.
13
+ * Returns undefined if PTYService or coordinator is not available.
14
+ */
15
+ export declare function getCoordinator(runtime: IAgentRuntime): SwarmCoordinator | undefined;
16
+ export declare class PTYService {
17
+ static serviceType: string;
18
+ capabilityDescription: string;
19
+ private runtime;
20
+ private manager;
21
+ private usingBunWorker;
22
+ private serviceConfig;
23
+ private sessionMetadata;
24
+ private sessionWorkdirs;
25
+ private eventCallbacks;
26
+ private outputUnsubscribers;
27
+ private sessionOutputBuffers;
28
+ private adapterCache;
29
+ /** Tracks the buffer index when a task was sent, so we can capture the response on completion */
30
+ private taskResponseMarkers;
31
+ /** Captures "Task completion trace" log entries from worker stderr (rolling, capped at 200) */
32
+ private traceEntries;
33
+ private static readonly MAX_TRACE_ENTRIES;
34
+ /** Lightweight per-agent-type metrics for observability */
35
+ private metricsTracker;
36
+ /** Console bridge for terminal output streaming and buffered hydration */
37
+ consoleBridge: PTYConsoleBridge | null;
38
+ /** Swarm coordinator instance (if active). Accessed via getCoordinator(runtime). */
39
+ coordinator: SwarmCoordinator | null;
40
+ constructor(runtime: IAgentRuntime, config?: PTYServiceConfig);
41
+ static start(runtime: IAgentRuntime): Promise<PTYService>;
42
+ static stopRuntime(runtime: IAgentRuntime): Promise<void>;
43
+ private initialize;
44
+ stop(): Promise<void>;
45
+ private generateSessionId;
46
+ /** Build a SessionIOContext from current instance state. */
47
+ private ioContext;
48
+ /**
49
+ * Spawn a new PTY session for a coding agent
50
+ */
51
+ spawnSession(options: SpawnSessionOptions): Promise<SessionInfo>;
52
+ private autoResponseContext;
53
+ private pushDefaultRules;
54
+ private handleGeminiAuth;
55
+ sendToSession(sessionId: string, input: string): Promise<SessionMessage | undefined>;
56
+ sendKeysToSession(sessionId: string, keys: string | string[]): Promise<void>;
57
+ stopSession(sessionId: string): Promise<void>;
58
+ /** Default approval preset — runtime env var takes precedence over config. */
59
+ get defaultApprovalPreset(): ApprovalPreset;
60
+ /** Agent selection strategy — env var takes precedence. */
61
+ get agentSelectionStrategy(): AgentSelectionStrategy;
62
+ /** Default agent type when strategy is "fixed" — env var takes precedence. */
63
+ get defaultAgentType(): AdapterType;
64
+ /**
65
+ * Resolve which agent type to use when the caller didn't specify one.
66
+ *
67
+ * - **fixed**: returns `defaultAgentType` immediately
68
+ * - **ranked**: fetches preflight data, scores installed agents via
69
+ * metrics, and returns the highest scorer
70
+ */
71
+ resolveAgentType(): Promise<string>;
72
+ getSession(sessionId: string): SessionInfo | undefined;
73
+ listSessions(filter?: SessionFilter): Promise<SessionInfo[]>;
74
+ subscribeToOutput(sessionId: string, callback: (data: string) => void): () => void;
75
+ getSessionOutput(sessionId: string, lines?: number): Promise<string>;
76
+ isSessionBlocked(sessionId: string): boolean;
77
+ checkAvailableAgents(types?: AdapterType[]): Promise<PreflightResult[]>;
78
+ getSupportedAgentTypes(): CodingAgentType[];
79
+ private classifyStall;
80
+ private getAdapter;
81
+ getWorkspaceFiles(agentType: AdapterType): AgentFileDescriptor[];
82
+ getMemoryFilePath(agentType: AdapterType): string;
83
+ getApprovalConfig(agentType: AdapterType, preset: ApprovalPreset): ApprovalConfig;
84
+ writeMemoryFile(agentType: AdapterType, workspacePath: string, content: string, options?: WriteMemoryOptions): Promise<string>;
85
+ onSessionEvent(callback: SessionEventCallback): () => void;
86
+ registerAdapter(adapter: unknown): void;
87
+ private toSessionInfo;
88
+ private emitEvent;
89
+ getAgentMetrics(): Record<string, Omit<import("./agent-metrics.js").AgentMetrics, "totalCompletionMs">>;
90
+ private log;
91
+ }
92
+ //# sourceMappingURL=pty-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pty-service.d.ts","sourceRoot":"","sources":["../../../src/services/pty-service.ts"],"names":[],"mappings":"AAAA,mCAAmC;AAEnC,OAAO,EAAE,KAAK,aAAa,EAAU,MAAM,eAAe,CAAC;AAC3D,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,cAAc,EAKnB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAGV,aAAa,EAEb,cAAc,EAIf,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,KAAK,sBAAsB,EAE5B,MAAM,sBAAsB,CAAC;AAmB9B,OAAO,KAAK,EACV,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,WAAW,EACX,mBAAmB,EACpB,MAAM,gBAAgB,CAAC;AAGxB,OAAO,E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M7B,sBAAsB,IAAI,eAAe,EAAE;YAI7B,aAAa;IAsB3B,OAAO,CAAC,UAAU;IASlB,iBAAiB,CAAC,SAAS,EAAE,WAAW,GAAG,mBAAmB,EAAE;IAIhE,iBAAiB,CAAC,SAAS,EAAE,WAAW,GAAG,MAAM;IAIjD,iBAAiB,CACf,SAAS,EAAE,WAAW,EACtB,MAAM,EAAE,cAAc,GACrB,cAAc;IAIX,eAAe,CACnB,SAAS,EAAE,WAAW,EACtB,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,kBAAkB,GAC3B,OAAO,CAAC,MAAM,CAAC;IAUlB,cAAc,CAAC,QAAQ,EAAE,oBAAoB,GAAG,MAAM,IAAI;IAQ1D,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAkBvC,OAAO,CAAC,aAAa;IA2BrB,OAAO,CAAC,SAAS;IAYjB,eAAe;IAIf,OAAO,CAAC,GAAG;CAKZ"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Session I/O helpers — extracted from PTYService for maintainability.
3
+ *
4
+ * Standalone functions for sending input/keys to sessions and stopping
5
+ * sessions. Each function takes a {@link SessionIOContext} that provides
6
+ * the manager instance and shared state maps.
7
+ *
8
+ * @module services/pty-session-io
9
+ */
10
+ import type { BunCompatiblePTYManager, PTYManager, SessionMessage } from "pty-manager";
11
+ /**
12
+ * Shared context required by all session I/O functions.
13
+ * Built inline from PTYService instance fields.
14
+ */
15
+ export interface SessionIOContext {
16
+ manager: PTYManager | BunCompatiblePTYManager;
17
+ usingBunWorker: boolean;
18
+ sessionOutputBuffers: Map<string, string[]>;
19
+ taskResponseMarkers: Map<string, number>;
20
+ outputUnsubscribers: Map<string, () => void>;
21
+ }
22
+ /**
23
+ * Send text input to a session.
24
+ *
25
+ * Marks the buffer position for task response capture, then writes the
26
+ * input via the appropriate manager API.
27
+ */
28
+ export declare function sendToSession(ctx: SessionIOContext, sessionId: string, input: string): Promise<SessionMessage | undefined>;
29
+ /**
30
+ * Send key sequences to a session (for special keys like arrows, enter, etc.).
31
+ */
32
+ export declare function sendKeysToSession(ctx: SessionIOContext, sessionId: string, keys: string | string[]): Promise<void>;
33
+ /**
34
+ * Stop a PTY session and clean up all associated state.
35
+ */
36
+ export declare function stopSession(ctx: SessionIOContext, sessionId: string, sessionMetadata: Map<string, Record<string, unknown>>, sessionWorkdirs: Map<string, string>, log: (msg: string) => void): Promise<void>;
37
+ /**
38
+ * Subscribe to live output from a session.
39
+ * Returns an unsubscribe function.
40
+ */
41
+ export declare function subscribeToOutput(ctx: SessionIOContext, sessionId: string, callback: (data: string) => void): () => void;
42
+ /**
43
+ * Get buffered or logged output from a session.
44
+ */
45
+ export declare function getSessionOutput(ctx: SessionIOContext, sessionId: string, lines?: number): Promise<string>;
46
+ //# sourceMappingURL=pty-session-io.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pty-session-io.d.ts","sourceRoot":"","sources":["../../../src/services/pty-session-io.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,uBAAuB,EACvB,UAAU,EACV,cAAc,EACf,MAAM,aAAa,CAAC;AAErB;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,UAAU,GAAG,uBAAuB,CAAC;IAC9C,cAAc,EAAE,OAAO,CAAC;IACxB,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC5C,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,IAAI,CAAC,CAAC;CAC9C;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,gBAAgB,EACrB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAoBrC;AAED;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,GAAG,EAAE,gBAAgB,EACrB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GACtB,OAAO,CAAC,IAAI,CAAC,CAUf;AAED;;GAEG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,gBAAgB,EACrB,SAAS,EAAE,MAAM,EACjB,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EACrD,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,EACpC,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GACzB,OAAO,CAAC,IAAI,CAAC,CAwBf;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,gBAAgB,EACrB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,GAC/B,MAAM,IAAI,CAiBZ;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,gBAAgB,EACrB,SAAS,EAAE,MAAM,EACjB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * PTY session spawning logic — extracted from PTYService for maintainability.
3
+ *
4
+ * Contains the deferred task delivery, retry logic, per-agent settle delays,
5
+ * and session buffer setup that runs during spawnSession().
6
+ *
7
+ * @module services/pty-spawn
8
+ */
9
+ import type { AdapterType, BaseCodingAdapter } from "coding-agent-adapters";
10
+ import type { BunCompatiblePTYManager, PTYManager, SessionHandle, SpawnConfig, WorkerSessionHandle } from "pty-manager";
11
+ import type { PTYServiceConfig, SessionInfo, SpawnSessionOptions } from "./pty-types.js";
12
+ export interface SpawnContext {
13
+ manager: PTYManager | BunCompatiblePTYManager;
14
+ usingBunWorker: boolean;
15
+ serviceConfig: PTYServiceConfig;
16
+ sessionMetadata: Map<string, Record<string, unknown>>;
17
+ sessionWorkdirs: Map<string, string>;
18
+ sessionOutputBuffers: Map<string, string[]>;
19
+ outputUnsubscribers: Map<string, () => void>;
20
+ taskResponseMarkers: Map<string, number>;
21
+ getAdapter: (agentType: AdapterType) => BaseCodingAdapter;
22
+ sendToSession: (sessionId: string, input: string) => Promise<unknown>;
23
+ sendKeysToSession: (sessionId: string, keys: string | string[]) => Promise<void>;
24
+ pushDefaultRules: (sessionId: string, agentType: string) => Promise<void>;
25
+ toSessionInfo: (session: SessionHandle | WorkerSessionHandle, workdir?: string) => SessionInfo;
26
+ log: (msg: string) => void;
27
+ }
28
+ /**
29
+ * Set up session output buffering for Bun worker path.
30
+ */
31
+ export declare function setupOutputBuffer(ctx: SpawnContext, sessionId: string): void;
32
+ /**
33
+ * Set up deferred task delivery with retry logic.
34
+ * IMPORTANT: Must be called BEFORE pushDefaultRules (which has a 1500ms sleep),
35
+ * otherwise session_ready fires during pushDefaultRules and the listener misses it.
36
+ */
37
+ export declare function setupDeferredTaskDelivery(ctx: SpawnContext, session: SessionHandle | WorkerSessionHandle, task: string, agentType: string): void;
38
+ /**
39
+ * Build the SpawnConfig and env vars from SpawnSessionOptions.
40
+ */
41
+ export declare function buildSpawnConfig(sessionId: string, options: SpawnSessionOptions, workdir: string): SpawnConfig & {
42
+ id: string;
43
+ };
44
+ //# sourceMappingURL=pty-spawn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pty-spawn.d.ts","sourceRoot":"","sources":["../../../src/services/pty-spawn.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,KAAK,EACV,uBAAuB,EACvB,UAAU,EACV,aAAa,EACb,WAAW,EACX,mBAAmB,EACpB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EACV,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACpB,M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EACjB,OAAO,EAAE,aAAa,GAAG,mBAAmB,EAC5C,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAChB,IAAI,CAkFN;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,MAAM,GACd,WAAW,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CA0C9B"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Shared types and helpers for the PTY service layer.
3
+ *
4
+ * Extracted from pty-service.ts to keep that module lean and allow
5
+ * other modules (pty-spawn, pty-init, actions) to import lightweight
6
+ * type-only dependencies without pulling in the full PTYService class.
7
+ *
8
+ * @module services/pty-types
9
+ */
10
+ import type { AdapterType, AgentCredentials, ApprovalPreset } from "coding-agent-adapters";
11
+ import type { SessionHandle } from "pty-manager";
12
+ export interface PTYServiceConfig {
13
+ /** Maximum output lines to keep per session (default: 1000) */
14
+ maxLogLines?: number;
15
+ /** Enable debug logging */
16
+ debug?: boolean;
17
+ /** Auto-register coding agent adapters (default: true) */
18
+ registerCodingAdapters?: boolean;
19
+ /** Maximum concurrent PTY sessions (default: 8) */
20
+ maxConcurrentSessions?: number;
21
+ /**
22
+ * Default approval preset for coding agents when not specified per-spawn.
23
+ * Controls what tools the agent can use without asking for permission.
24
+ * - "readonly" — Read-only tools only
25
+ * - "standard" — Read + write, asks for shell/network
26
+ * - "permissive" — Most tools auto-approved, asks for destructive ops
27
+ * - "autonomous" — All tools auto-approved (yolo mode)
28
+ * Default: "permissive"
29
+ */
30
+ defaultApprovalPreset?: ApprovalPreset;
31
+ }
32
+ /** Available coding agent types */
33
+ export type CodingAgentType = "shell" | "pi" | AdapterType;
34
+ /** True when the user requested the Pi coding agent. */
35
+ export declare const isPiAgentType: (input: string | undefined | null) => boolean;
36
+ /** Normalize user-provided agent type string to a valid CodingAgentType */
37
+ export declare const normalizeAgentType: (input: string) => CodingAgentType;
38
+ /** Build the initial shell command for Pi agent sessions. */
39
+ export declare const toPiCommand: (task: string | undefined) => string;
40
+ export interface SpawnSessionOptions {
41
+ /** Human-readable session name */
42
+ name: string;
43
+ /** Adapter type: "shell" | "pi" | "claude" | "gemini" | "codex" | "aider" */
44
+ agentType: CodingAgentType;
45
+ /** Working directory for the session */
46
+ workdir?: string;
47
+ /** Initial command/task to send */
48
+ initialTask?: string;
49
+ /** Environment variables */
50
+ env?: Record<string, string>;
51
+ /** Session metadata for tracking */
52
+ metadata?: Record<string, unknown>;
53
+ /** Credentials for coding agents (API keys, tokens) */
54
+ credentials?: AgentCredentials;
55
+ /** Memory/instructions content to write to the agent's memory file before spawning */
56
+ memoryContent?: string;
57
+ /** Approval preset controlling tool permissions (readonly, standard, permissive, autonomous) */
58
+ approvalPreset?: ApprovalPreset;
59
+ /** Custom credentials for MCP servers or other integrations */
60
+ customCredentials?: Record<string, string>;
61
+ /** When true, adapter-level blocking prompts (tool permissions, file access)
62
+ * are emitted with autoResponded=false instead of being auto-handled.
63
+ * Used by the swarm coordinator to route decisions through its LLM loop. */
64
+ skipAdapterAutoResponse?: boolean;
65
+ }
66
+ export interface SessionInfo {
67
+ id: string;
68
+ name: string;
69
+ agentType: string;
70
+ workdir: string;
71
+ status: SessionHandle["status"];
72
+ createdAt: Date;
73
+ lastActivityAt: Date;
74
+ metadata?: Record<string, unknown>;
75
+ }
76
+ /** Known event names emitted by the PTY layer. */
77
+ export type SessionEventName = "ready" | "blocked" | "login_required" | "task_complete" | "tool_running" | "stopped" | "error" | "message";
78
+ type SessionEventCallback = (sessionId: string, event: string, data: unknown) => void;
79
+ export type { SessionEventCallback };
80
+ //# sourceMappingURL=pty-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pty-types.d.ts","sourceRoot":"","sources":["../../../src/services/pty-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,cAAc,EACf,MA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ChG,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,+DAA+D;IAC/D,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC3C;;iFAE6E;IAC7E,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChC,SAAS,EAAE,IAAI,CAAC;IAChB,cAAc,EAAE,IAAI,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,kDAAkD;AAClD,MAAM,MAAM,gBAAgB,GACxB,OAAO,GACP,SAAS,GACT,gBAAgB,GAChB,eAAe,GACf,cAAc,GACd,SAAS,GACT,OAAO,GACP,SAAS,CAAC;AAEd,KAAK,oBAAoB,GAAG,CAC1B,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,OAAO,KACV,IAAI,CAAC;AAEV,YAAY,EAAE,oBAAoB,EAAE,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Stall classification subsystem — determines what a "stalled" coding agent
3
+ * session is doing (finished, waiting for input, still working, or errored).
4
+ *
5
+ * Extracted as standalone functions that receive dependencies as parameters,
6
+ * making them easy to test without coupling to PTYService.
7
+ *
8
+ * @module services/stall-classifier
9
+ */
10
+ import { type IAgentRuntime } from "@elizaos/core";
11
+ import { type StallClassification } from "pty-manager";
12
+ import type { AgentMetricsTracker } from "./agent-metrics.js";
13
+ /** Everything the classifier needs, passed in from PTYService. */
14
+ export interface StallClassifierContext {
15
+ sessionId: string;
16
+ recentOutput: string;
17
+ agentType: string;
18
+ buffers: Map<string, string[]>;
19
+ traceEntries: Array<string | Record<string, unknown>>;
20
+ runtime: IAgentRuntime;
21
+ manager: {
22
+ get(id: string): {
23
+ startedAt?: string | Date;
24
+ } | null | undefined;
25
+ } | null;
26
+ metricsTracker: AgentMetricsTracker;
27
+ /** Write debug snapshots to ~/.milaidy/debug/ on stall (default: false) */
28
+ debugSnapshots?: boolean;
29
+ log: (msg: string) => void;
30
+ }
31
+ /**
32
+ * Build the LLM system prompt used to classify stalled output.
33
+ */
34
+ export declare function buildStallClassificationPrompt(agentType: string, sessionId: string, output: string): string;
35
+ /**
36
+ * Write a debug snapshot to ~/.milaidy/debug/ for offline stall analysis.
37
+ */
38
+ export declare function writeStallSnapshot(sessionId: string, agentType: string, recentOutput: string, effectiveOutput: string, buffers: Map<string, string[]>, traceEntries: Array<string | Record<string, unknown>>, log: (msg: string) => void): Promise<void>;
39
+ /**
40
+ * Main stall classification logic. Determines what a stalled session is doing
41
+ * by checking the buffer, building a prompt, and asking the LLM.
42
+ */
43
+ export declare function classifyStallOutput(ctx: StallClassifierContext): Promise<StallClassification | null>;
44
+ //# sourceMappingURL=stall-classifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stall-classifier.d.ts","sourceRoot":"","sources":["../../../src/services/stall-classifier.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,KAAK,aAAa,EAAa,MAAM,eAAe,CAAC;AAC9D,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAG9D,kEAAkE;AAClE,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC/B,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACtD,OAAO,EAAE,aAAa,CAAC;IACvB,OAAO,EAAE;QACP,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG;YAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;SAAE,GAAG,IAAI,GAAG,SAAS,CAAC;KACnE,GAAG,IAAI,CAAC;IACT,cAAc,EAAE,mBAAmB,CAAC;IACpC,2EAA2E;IAC3E,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC5B;AAED;;GAEG;AACH,wBAAgB,8BAA8B,CAC5C,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,MAAM,CAmCR;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CACtC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,EAC9B,YAAY,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EACrD,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GACzB,OAAO,CAAC,IAAI,CAAC,CA8Cf;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,sBAAsB,GAC1B,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAiGrC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Prompt construction and response parsing for the Swarm Coordinator's
3
+ * LLM-driven coordination decisions.
4
+ *
5
+ * Pure functions — no side effects, easy to test.
6
+ * Pattern follows stall-classifier.ts:buildStallClassificationPrompt().
7
+ *
8
+ * @module services/swarm-coordinator-prompts
9
+ */
10
+ /** Per-session task context provided to the LLM for decision-making. */
11
+ export interface TaskContextSummary {
12
+ sessionId: string;
13
+ agentType: string;
14
+ label: string;
15
+ originalTask: string;
16
+ workdir: string;
17
+ }
18
+ /** A previous coordination decision, included for context continuity. */
19
+ export interface DecisionHistoryEntry {
20
+ event: string;
21
+ promptText: string;
22
+ action: string;
23
+ response?: string;
24
+ reasoning: string;
25
+ }
26
+ /** Parsed LLM response for a coordination decision. */
27
+ export interface CoordinationLLMResponse {
28
+ action: "respond" | "escalate" | "ignore" | "complete";
29
+ /** Text to send (for action=respond with plain text input). */
30
+ response?: string;
31
+ /** Whether to use sendKeysToSession instead of sendToSession. */
32
+ useKeys?: boolean;
33
+ /** Key sequence to send (for TUI interactions). e.g. ["enter"] or ["down","enter"]. */
34
+ keys?: string[];
35
+ /** LLM's reasoning for the decision. */
36
+ reasoning: string;
37
+ }
38
+ /**
39
+ * Build the LLM prompt for making a coordination decision about a blocked agent.
40
+ */
41
+ export declare function buildCoordinationPrompt(taskCtx: TaskContextSummary, promptText: string, recentOutput: string, decisionHistory: DecisionHistoryEntry[]): string;
42
+ /**
43
+ * Build the LLM prompt for checking on an idle session that hasn't
44
+ * produced any events for a while.
45
+ */
46
+ export declare function buildIdleCheckPrompt(taskCtx: TaskContextSummary, recentOutput: string, idleMinutes: number, idleCheckNumber: number, maxIdleChecks: number, decisionHistory: DecisionHistoryEntry[]): string;
47
+ /**
48
+ * Build the LLM prompt for assessing whether a completed turn means the
49
+ * overall task is done, or if the agent needs more turns.
50
+ *
51
+ * Called when the adapter detects "task_complete" (agent finished a turn and
52
+ * returned to the idle prompt). The LLM decides whether to stop the session
53
+ * or send a follow-up instruction.
54
+ */
55
+ export declare function buildTurnCompletePrompt(taskCtx: TaskContextSummary, turnOutput: string, decisionHistory: DecisionHistoryEntry[]): string;
56
+ /**
57
+ * Parse the LLM's coordination response from raw text output.
58
+ * Returns null if the response is invalid or unparseable.
59
+ */
60
+ export declare function parseCoordinationResponse(llmOutput: string): CoordinationLLMResponse | null;
61
+ //# sourceMappingURL=swarm-coordinator-prompts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm-coordinator-prompts.d.ts","sourceRoot":"","sources":["../../../src/services/swarm-coordinator-prompts.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,wEAAwE;AACxE,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,yEAAyE;AACzE,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,uDAAuD;AACvD,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,SAAS,GAAG,UAAU,GAAG,QAAQ,GAAG,UAAU,CAAC;IACvD,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uFAAuF;IACvF,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,wCAAwC;IACxC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,kBAAkB,EAC3B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,eAAe,EAAE,oBAAoB,EAAE,GACtC,MAAM,CAmDR;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,kBAAkB,EAC3B,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,MAAM,EACvB,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,oBAAoB,EAAE,GACtC,MAAM,CA4CR;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,kBAAkB,EAC3B,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,oBAAoB,EAAE,GACtC,MAAM,CAgER;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,MAAM,GAChB,uBAAuB,GAAG,IAAI,CA+BhC"}
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Swarm Coordinator — Event Bridge & Autonomous Coordination Loop
3
+ *
4
+ * Bridges PTY session events to:
5
+ * 1. SSE clients (frontend dashboard) for real-time status
6
+ * 2. LLM coordination decisions for unhandled blocking prompts
7
+ *
8
+ * The coordinator subscribes to PTYService session events and:
9
+ * - Skips events already handled by auto-response rules (autoResponded=true)
10
+ * - Routes unhandled blocking prompts through supervision levels:
11
+ * - autonomous: LLM decides immediately
12
+ * - confirm: queued for human approval
13
+ * - notify: broadcast only (no action)
14
+ *
15
+ * Heavy logic is extracted into:
16
+ * - swarm-decision-loop.ts (blocked, turn-complete, LLM decisions)
17
+ * - swarm-idle-watchdog.ts (idle session scanning)
18
+ *
19
+ * @module services/swarm-coordinator
20
+ */
21
+ import type { ServerResponse } from "node:http";
22
+ import type { IAgentRuntime } from "@elizaos/core";
23
+ import type { PTYService } from "./pty-service.js";
24
+ import type { CodingAgentType } from "./pty-types.js";
25
+ import type { CoordinationLLMResponse } from "./swarm-coordinator-prompts.js";
26
+ /** Callback injected by server.ts to route chat messages to the user's conversation. */
27
+ export type ChatMessageCallback = (text: string, source?: string) => Promise<void>;
28
+ /** Callback injected by server.ts to relay coordinator events to WebSocket clients. */
29
+ export type WsBroadcastCallback = (event: SwarmEvent) => void;
30
+ export type SupervisionLevel = "autonomous" | "confirm" | "notify";
31
+ export interface TaskContext {
32
+ sessionId: string;
33
+ agentType: CodingAgentType;
34
+ label: string;
35
+ originalTask: string;
36
+ workdir: string;
37
+ status: "active" | "completed" | "error" | "stopped";
38
+ decisions: CoordinationDecision[];
39
+ autoResolvedCount: number;
40
+ registeredAt: number;
41
+ /** Timestamp of the last session event (any type). Used by idle watchdog. */
42
+ lastActivityAt: number;
43
+ /** How many idle checks have been performed on this session. */
44
+ idleCheckCount: number;
45
+ }
46
+ export interface CoordinationDecision {
47
+ timestamp: number;
48
+ event: string;
49
+ promptText: string;
50
+ decision: "respond" | "escalate" | "ignore" | "complete" | "auto_resolved";
51
+ response?: string;
52
+ reasoning: string;
53
+ }
54
+ export interface SwarmEvent {
55
+ type: string;
56
+ sessionId: string;
57
+ timestamp: number;
58
+ data: unknown;
59
+ }
60
+ export interface PendingDecision {
61
+ sessionId: string;
62
+ promptText: string;
63
+ recentOutput: string;
64
+ llmDecision: CoordinationLLMResponse;
65
+ taskContext: TaskContext;
66
+ createdAt: number;
67
+ }
68
+ /**
69
+ * Context interface exposing internal state and helpers to extracted modules.
70
+ * Implemented by SwarmCoordinator — passed as `this` to module-level functions.
71
+ */
72
+ export interface SwarmCoordinatorContext {
73
+ readonly runtime: IAgentRuntime;
74
+ readonly ptyService: PTYService | null;
75
+ readonly tasks: Map<string, TaskContext>;
76
+ readonly inFlightDecisions: Set<string>;
77
+ readonly pendingDecisions: Map<string, PendingDecision>;
78
+ /** Last-seen output snapshot per session — used by idle watchdog. */
79
+ readonly lastSeenOutput: Map<string, string>;
80
+ /** Timestamp of last tool_running chat notification per session — for throttling. */
81
+ readonly lastToolNotification: Map<string, number>;
82
+ broadcast(event: SwarmEvent): void;
83
+ sendChatMessage(text: string, source?: string): void;
84
+ log(message: string): void;
85
+ getSupervisionLevel(): SupervisionLevel;
86
+ }
87
+ export declare class SwarmCoordinator implements SwarmCoordinatorContext {
88
+ static serviceType: string;
89
+ readonly runtime: IAgentRuntime;
90
+ ptyService: PTYService | null;
91
+ private unsubscribeEvents;
92
+ /** Per-session task context. */
93
+ readonly tasks: Map<string, TaskContext>;
94
+ /** SSE clients receiving live events. */
95
+ private sseClients;
96
+ /** Supervision level (default: autonomous). */
97
+ private supervisionLevel;
98
+ /** Pending confirmations for "confirm" mode. */
99
+ readonly pendingDecisions: Map<string, PendingDecision>;
100
+ /** In-flight decision lock — prevents parallel LLM calls for same session. */
101
+ readonly inFlightDecisions: Set<string>;
102
+ /** Callback to send chat messages to the user's conversation UI. */
103
+ private chatCallback;
104
+ /** Callback to relay coordinator events to WebSocket clients. */
105
+ private wsBroadcast;
106
+ /** Buffer for events arriving before task registration. */
107
+ private unregisteredBuffer;
108
+ /** Idle watchdog timer handle. */
109
+ private idleWatchdogTimer;
110
+ /** Last-seen output snapshot per session — used by idle watchdog to detect data flow. */
111
+ readonly lastSeenOutput: Map<string, string>;
112
+ /** Timestamp of last tool_running chat notification per session — for throttling. */
113
+ readonly lastToolNotification: Map<string, number>;
114
+ constructor(runtime: IAgentRuntime);
115
+ /** Inject a callback (from server.ts) to route messages to the user's chat UI. */
116
+ setChatCallback(cb: ChatMessageCallback): void;
117
+ /** Inject a callback (from server.ts) to relay events to WebSocket clients. */
118
+ setWsBroadcast(cb: WsBroadcastCallback): void;
119
+ /** Null-safe wrapper — sends a message to the user's conversation if callback is set. */
120
+ sendChatMessage(text: string, source?: string): void;
121
+ /**
122
+ * Initialize the coordinator by subscribing to PTY session events.
123
+ * Called from plugin init after services are ready.
124
+ */
125
+ start(ptyService: PTYService): void;
126
+ stop(): void;
127
+ registerTask(sessionId: string, context: {
128
+ agentType: CodingAgentType;
129
+ label: string;
130
+ originalTask: string;
131
+ workdir: string;
132
+ }): void;
133
+ getTaskContext(sessionId: string): TaskContext | undefined;
134
+ getAllTaskContexts(): TaskContext[];
135
+ /**
136
+ * Register an SSE client. Returns an unsubscribe function.
137
+ * Sends a snapshot of current state on connect.
138
+ */
139
+ addSseClient(res: ServerResponse): () => void;
140
+ broadcast(event: SwarmEvent): void;
141
+ private writeSseEvent;
142
+ handleSessionEvent(sessionId: string, event: string, data: unknown): Promise<void>;
143
+ makeCoordinationDecision(taskCtx: TaskContext, promptText: string, recentOutput: string): Promise<CoordinationLLMResponse | null>;
144
+ executeDecision(sessionId: string, decision: CoordinationLLMResponse): Promise<void>;
145
+ setSupervisionLevel(level: SupervisionLevel): void;
146
+ getSupervisionLevel(): SupervisionLevel;
147
+ getPendingConfirmations(): PendingDecision[];
148
+ confirmDecision(sessionId: string, approved: boolean, override?: {
149
+ response?: string;
150
+ useKeys?: boolean;
151
+ keys?: string[];
152
+ }): Promise<void>;
153
+ log(message: string): void;
154
+ }
155
+ //# sourceMappingURL=swarm-coordinator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm-coordinator.d.ts","sourceRoot":"","sources":["../../../src/services/swarm-coordinator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAU9E,wFAAwF;AACxF,MAAM,MAAM,mBAAmB,GAAG,CAChC,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,MAAM,KACZ,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,uFAAuF;AACvF,MAAM,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;AAE9D,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEnE,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,eAAe,CAAC;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,QAAQ,GAAG,WAAW,GAA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