@agent-relay/bridge 0.1.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.
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/multi-project-client.d.ts +99 -0
- package/dist/multi-project-client.d.ts.map +1 -0
- package/dist/multi-project-client.js +389 -0
- package/dist/multi-project-client.js.map +1 -0
- package/dist/shadow-cli.d.ts +17 -0
- package/dist/shadow-cli.d.ts.map +1 -0
- package/dist/shadow-cli.js +75 -0
- package/dist/shadow-cli.js.map +1 -0
- package/dist/spawner.d.ts +210 -0
- package/dist/spawner.d.ts.map +1 -0
- package/dist/spawner.js +1276 -0
- package/dist/spawner.js.map +1 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +60 -0
- package/dist/utils.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Spawner
|
|
3
|
+
* Handles spawning and releasing worker agents via relay-pty.
|
|
4
|
+
* Workers run headlessly with output capture for logs.
|
|
5
|
+
*/
|
|
6
|
+
import type { SummaryEvent, SessionEndEvent } from '@agent-relay/wrapper';
|
|
7
|
+
import { AgentPolicyService, type CloudPolicyFetcher } from '@agent-relay/policy';
|
|
8
|
+
import type { SpawnRequest, SpawnResult, WorkerInfo, SpawnWithShadowRequest, SpawnWithShadowResult } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Cloud persistence handler interface.
|
|
11
|
+
* Implement this to persist agent session data to cloud storage.
|
|
12
|
+
*/
|
|
13
|
+
export interface CloudPersistenceHandler {
|
|
14
|
+
onSummary: (agentName: string, event: SummaryEvent) => Promise<void>;
|
|
15
|
+
onSessionEnd: (agentName: string, event: SessionEndEvent) => Promise<void>;
|
|
16
|
+
/** Optional cleanup method for tests and graceful shutdown */
|
|
17
|
+
destroy?: () => void;
|
|
18
|
+
}
|
|
19
|
+
/** Worker metadata stored in workers.json */
|
|
20
|
+
interface WorkerMeta {
|
|
21
|
+
name: string;
|
|
22
|
+
cli: string;
|
|
23
|
+
task: string;
|
|
24
|
+
/** Optional team name this agent belongs to */
|
|
25
|
+
team?: string;
|
|
26
|
+
/** Optional user ID for per-user credential scoping */
|
|
27
|
+
userId?: string;
|
|
28
|
+
spawnedAt: number;
|
|
29
|
+
pid?: number;
|
|
30
|
+
logFile?: string;
|
|
31
|
+
}
|
|
32
|
+
/** Callback for agent death notifications */
|
|
33
|
+
export type OnAgentDeathCallback = (info: {
|
|
34
|
+
name: string;
|
|
35
|
+
exitCode: number | null;
|
|
36
|
+
agentId?: string;
|
|
37
|
+
resumeInstructions?: string;
|
|
38
|
+
/** Traceable error ID for support lookup */
|
|
39
|
+
errorId?: string;
|
|
40
|
+
}) => void;
|
|
41
|
+
/** Options for AgentSpawner constructor */
|
|
42
|
+
export interface AgentSpawnerOptions {
|
|
43
|
+
projectRoot: string;
|
|
44
|
+
tmuxSession?: string;
|
|
45
|
+
dashboardPort?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Callback to mark an agent as spawning (before HELLO completes).
|
|
48
|
+
* Messages sent to this agent will be queued for delivery after registration.
|
|
49
|
+
*/
|
|
50
|
+
onMarkSpawning?: (agentName: string) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Callback to clear the spawning flag for an agent.
|
|
53
|
+
* Called when spawn fails or is cancelled.
|
|
54
|
+
*/
|
|
55
|
+
onClearSpawning?: (agentName: string) => void;
|
|
56
|
+
}
|
|
57
|
+
export declare class AgentSpawner {
|
|
58
|
+
private static readonly ONLINE_THRESHOLD_MS;
|
|
59
|
+
private activeWorkers;
|
|
60
|
+
private agentsPath;
|
|
61
|
+
private registryPath;
|
|
62
|
+
private projectRoot;
|
|
63
|
+
private socketPath?;
|
|
64
|
+
private logsDir;
|
|
65
|
+
private workersPath;
|
|
66
|
+
private dashboardPort?;
|
|
67
|
+
private onAgentDeath?;
|
|
68
|
+
private cloudPersistence?;
|
|
69
|
+
private policyService?;
|
|
70
|
+
private policyEnforcementEnabled;
|
|
71
|
+
private onMarkSpawning?;
|
|
72
|
+
private onClearSpawning?;
|
|
73
|
+
constructor(projectRoot: string, _tmuxSession?: string, dashboardPort?: number);
|
|
74
|
+
constructor(options: AgentSpawnerOptions);
|
|
75
|
+
/**
|
|
76
|
+
* Set cloud policy fetcher for workspace-level policies
|
|
77
|
+
*/
|
|
78
|
+
setCloudPolicyFetcher(fetcher: CloudPolicyFetcher): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get the policy service (for external access to policy checks)
|
|
81
|
+
*/
|
|
82
|
+
getPolicyService(): AgentPolicyService | undefined;
|
|
83
|
+
private fetchGhTokenFromCloud;
|
|
84
|
+
private resolveGhTokenFromHostsFile;
|
|
85
|
+
private resolveGhTokenFromGhCli;
|
|
86
|
+
/**
|
|
87
|
+
* Resolve GitHub token using multiple fallback sources.
|
|
88
|
+
*
|
|
89
|
+
* Fallback order (same as git-credential-relay for consistency):
|
|
90
|
+
* 1. Environment - GH_TOKEN or GITHUB_TOKEN (fastest, set by entrypoint)
|
|
91
|
+
* 2. hosts.yml - gh CLI config file (~/.config/gh/hosts.yml)
|
|
92
|
+
* 3. gh CLI - execute `gh auth token` command
|
|
93
|
+
* 4. Cloud API - workspace-scoped token from Nango (requires network)
|
|
94
|
+
*
|
|
95
|
+
* Environment is checked first because:
|
|
96
|
+
* - It's the fastest (no I/O or network)
|
|
97
|
+
* - The entrypoint pre-fetches and caches GH_TOKEN at startup
|
|
98
|
+
* - This avoids delays when cloud API is slow/unreachable
|
|
99
|
+
*/
|
|
100
|
+
private resolveGhToken;
|
|
101
|
+
/**
|
|
102
|
+
* Set the dashboard port (for nested spawn API calls).
|
|
103
|
+
* Called after the dashboard server starts and we know the actual port.
|
|
104
|
+
*/
|
|
105
|
+
setDashboardPort(port: number): void;
|
|
106
|
+
/**
|
|
107
|
+
* Set callback for agent death notifications.
|
|
108
|
+
* Called when an agent exits unexpectedly (non-zero exit code).
|
|
109
|
+
*/
|
|
110
|
+
setOnAgentDeath(callback: OnAgentDeathCallback): void;
|
|
111
|
+
/**
|
|
112
|
+
* Set cloud persistence handler for forwarding RelayPtyOrchestrator events.
|
|
113
|
+
* When set, 'summary' and 'session-end' events from spawned agents
|
|
114
|
+
* are forwarded to the handler for cloud persistence (PostgreSQL/Redis).
|
|
115
|
+
*
|
|
116
|
+
* Note: Enable via RELAY_CLOUD_ENABLED=true environment variable.
|
|
117
|
+
*/
|
|
118
|
+
setCloudPersistence(handler: CloudPersistenceHandler): void;
|
|
119
|
+
/**
|
|
120
|
+
* Bind cloud persistence event handlers to a RelayPtyOrchestrator.
|
|
121
|
+
* Returns the listener references for cleanup.
|
|
122
|
+
*/
|
|
123
|
+
private bindCloudPersistenceEvents;
|
|
124
|
+
/**
|
|
125
|
+
* Unbind all tracked listeners from a RelayPtyOrchestrator.
|
|
126
|
+
*/
|
|
127
|
+
private unbindListeners;
|
|
128
|
+
/**
|
|
129
|
+
* Spawn a new worker agent using relay-pty
|
|
130
|
+
*/
|
|
131
|
+
spawn(request: SpawnRequest): Promise<SpawnResult>;
|
|
132
|
+
/** Role presets for shadow agents */
|
|
133
|
+
private static readonly ROLE_PRESETS;
|
|
134
|
+
/**
|
|
135
|
+
* Spawn a primary agent with its shadow agent
|
|
136
|
+
*
|
|
137
|
+
* Example usage:
|
|
138
|
+
* ```ts
|
|
139
|
+
* const result = await spawner.spawnWithShadow({
|
|
140
|
+
* primary: { name: 'Lead', command: 'claude', task: 'Implement feature X' },
|
|
141
|
+
* shadow: { name: 'Auditor', role: 'reviewer', speakOn: ['CODE_WRITTEN'] }
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
spawnWithShadow(request: SpawnWithShadowRequest): Promise<SpawnWithShadowResult>;
|
|
146
|
+
/**
|
|
147
|
+
* Release (terminate) a worker
|
|
148
|
+
*/
|
|
149
|
+
release(name: string): Promise<boolean>;
|
|
150
|
+
/**
|
|
151
|
+
* Release all workers
|
|
152
|
+
*/
|
|
153
|
+
releaseAll(): Promise<void>;
|
|
154
|
+
/**
|
|
155
|
+
* Get all active workers (returns WorkerInfo without pty reference)
|
|
156
|
+
*/
|
|
157
|
+
getActiveWorkers(): WorkerInfo[];
|
|
158
|
+
/**
|
|
159
|
+
* Check if a worker exists
|
|
160
|
+
*/
|
|
161
|
+
hasWorker(name: string): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Get worker info
|
|
164
|
+
*/
|
|
165
|
+
getWorker(name: string): WorkerInfo | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Get output logs from a worker
|
|
168
|
+
*/
|
|
169
|
+
getWorkerOutput(name: string, limit?: number): string[] | null;
|
|
170
|
+
/**
|
|
171
|
+
* Get raw output from a worker
|
|
172
|
+
*/
|
|
173
|
+
getWorkerRawOutput(name: string): string | null;
|
|
174
|
+
/**
|
|
175
|
+
* Send input to a worker's PTY (for interactive terminal support)
|
|
176
|
+
* @param name - Worker name
|
|
177
|
+
* @param data - Input data to send (keystrokes, text, etc.)
|
|
178
|
+
* @returns true if input was sent, false if worker not found
|
|
179
|
+
*/
|
|
180
|
+
sendWorkerInput(name: string, data: string): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Wait for an agent to appear in the connected list and registry (connected-agents.json + agents.json).
|
|
183
|
+
*/
|
|
184
|
+
private waitForAgentRegistration;
|
|
185
|
+
private isAgentRegistered;
|
|
186
|
+
private isAgentConnected;
|
|
187
|
+
private isAgentRecentlySeen;
|
|
188
|
+
/**
|
|
189
|
+
* Save workers metadata to disk for CLI access
|
|
190
|
+
*/
|
|
191
|
+
private saveWorkersMetadata;
|
|
192
|
+
/**
|
|
193
|
+
* Get path to logs directory
|
|
194
|
+
*/
|
|
195
|
+
getLogsDir(): string;
|
|
196
|
+
/**
|
|
197
|
+
* Get path to workers metadata file
|
|
198
|
+
*/
|
|
199
|
+
getWorkersPath(): string;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Read workers metadata from disk (for CLI use)
|
|
203
|
+
*/
|
|
204
|
+
export declare function readWorkersMetadata(projectRoot: string): WorkerMeta[];
|
|
205
|
+
/**
|
|
206
|
+
* Get the worker logs directory path
|
|
207
|
+
*/
|
|
208
|
+
export declare function getWorkerLogsDir(projectRoot: string): string;
|
|
209
|
+
export {};
|
|
210
|
+
//# sourceMappingURL=spawner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spawner.d.ts","sourceRoot":"","sources":["../src/spawner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAaH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAM1E,OAAO,EAAE,kBAAkB,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAIlF,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,UAAU,EACV,sBAAsB,EACtB,qBAAqB,EAEtB,MAAM,YAAY,CAAC;AA4CpB;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC,SAAS,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE,YAAY,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3E,8DAA8D;IAC9D,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,6CAA6C;AAC7C,UAAU,UAAU;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,+CAA+C;IAC/C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,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