indus-swarms 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.
Files changed (52) hide show
  1. package/AGENTS.md +14 -0
  2. package/LICENSE +21 -0
  3. package/README.md +119 -0
  4. package/docs/claude-parity.md +151 -0
  5. package/docs/field-notes-teams-setup.md +107 -0
  6. package/docs/smoke-test-plan.md +146 -0
  7. package/eslint.config.js +74 -0
  8. package/extensions/teams/README.md +23 -0
  9. package/extensions/teams/activity-tracker.ts +234 -0
  10. package/extensions/teams/cleanup.ts +31 -0
  11. package/extensions/teams/fs-lock.ts +87 -0
  12. package/extensions/teams/hooks.ts +363 -0
  13. package/extensions/teams/index.ts +18 -0
  14. package/extensions/teams/leader-attach-commands.ts +221 -0
  15. package/extensions/teams/leader-inbox.ts +214 -0
  16. package/extensions/teams/leader-info-commands.ts +140 -0
  17. package/extensions/teams/leader-lifecycle-commands.ts +559 -0
  18. package/extensions/teams/leader-messaging-commands.ts +148 -0
  19. package/extensions/teams/leader-plan-commands.ts +95 -0
  20. package/extensions/teams/leader-spawn-command.ts +149 -0
  21. package/extensions/teams/leader-task-commands.ts +435 -0
  22. package/extensions/teams/leader-team-command.ts +382 -0
  23. package/extensions/teams/leader-teams-tool.ts +1075 -0
  24. package/extensions/teams/leader.ts +925 -0
  25. package/extensions/teams/mailbox.ts +131 -0
  26. package/extensions/teams/model-policy.ts +142 -0
  27. package/extensions/teams/names.ts +121 -0
  28. package/extensions/teams/paths.ts +37 -0
  29. package/extensions/teams/protocol.ts +241 -0
  30. package/extensions/teams/spawn-types.ts +36 -0
  31. package/extensions/teams/task-store.ts +544 -0
  32. package/extensions/teams/team-attach-claim.ts +205 -0
  33. package/extensions/teams/team-config.ts +335 -0
  34. package/extensions/teams/team-discovery.ts +59 -0
  35. package/extensions/teams/teammate-rpc.ts +261 -0
  36. package/extensions/teams/teams-panel.ts +1186 -0
  37. package/extensions/teams/teams-style.ts +322 -0
  38. package/extensions/teams/teams-ui-shared.ts +89 -0
  39. package/extensions/teams/teams-widget.ts +212 -0
  40. package/extensions/teams/worker.ts +605 -0
  41. package/extensions/teams/worktree.ts +103 -0
  42. package/package.json +53 -0
  43. package/scripts/e2e-rpc-test.mjs +277 -0
  44. package/scripts/integration-claim-test.mts +157 -0
  45. package/scripts/integration-hooks-remediation-test.mts +382 -0
  46. package/scripts/integration-spawn-overrides-test.mts +398 -0
  47. package/scripts/integration-todo-test.mts +533 -0
  48. package/scripts/lib/pi-workers.ts +105 -0
  49. package/scripts/smoke-test.mts +764 -0
  50. package/scripts/start-tmux-team.sh +91 -0
  51. package/skills/agent-teams/SKILL.md +180 -0
  52. package/tsconfig.strict.json +22 -0
@@ -0,0 +1,261 @@
1
+ import { spawn } from "node:child_process";
2
+ import type { AgentEvent } from "indusagi/agent";
3
+
4
+ export type TeammateStatus = "starting" | "idle" | "streaming" | "stopped" | "error";
5
+
6
+ type RpcCommand =
7
+ | { id: string; type: "prompt"; message: string }
8
+ | { id: string; type: "steer"; message: string }
9
+ | { id: string; type: "follow_up"; message: string }
10
+ | { id: string; type: "abort" }
11
+ | { id: string; type: "get_state" }
12
+ | { id: string; type: "set_session_name"; name: string };
13
+
14
+ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
15
+
16
+ type RpcCommandWithoutId = DistributiveOmit<RpcCommand, "id">;
17
+
18
+ type RpcResponse = {
19
+ id?: string;
20
+ type: "response";
21
+ command: string;
22
+ success: boolean;
23
+ data?: unknown;
24
+ error?: string;
25
+ };
26
+
27
+ function isRecord(v: unknown): v is Record<string, unknown> {
28
+ return typeof v === "object" && v !== null;
29
+ }
30
+
31
+ function safeParseJsonLine(line: string): unknown | null {
32
+ try {
33
+ return JSON.parse(line);
34
+ } catch {
35
+ return null;
36
+ }
37
+ }
38
+
39
+ function isRpcResponse(v: unknown): v is RpcResponse {
40
+ if (!isRecord(v)) return false;
41
+ if (v.type !== "response") return false;
42
+ if (typeof v.command !== "string") return false;
43
+ if (typeof v.success !== "boolean") return false;
44
+ if (v.id !== undefined && typeof v.id !== "string") return false;
45
+ if (v.error !== undefined && typeof v.error !== "string") return false;
46
+ return true;
47
+ }
48
+
49
+ function isAgentEvent(v: unknown): v is AgentEvent {
50
+ if (!isRecord(v)) return false;
51
+ if (typeof v.type !== "string") return false;
52
+
53
+ // Validate the minimal shapes we actually dereference below.
54
+ if (v.type === "message_update") {
55
+ const ame = v.assistantMessageEvent;
56
+ if (!isRecord(ame)) return false;
57
+ if (typeof ame.type !== "string") return false;
58
+ if (ame.type === "text_delta" && typeof ame.delta !== "string") return false;
59
+ return true;
60
+ }
61
+
62
+ if (v.type === "tool_execution_start" || v.type === "tool_execution_update" || v.type === "tool_execution_end") {
63
+ if (typeof v.toolCallId !== "string") return false;
64
+ if (typeof v.toolName !== "string") return false;
65
+ return true;
66
+ }
67
+
68
+ return (
69
+ v.type === "agent_start" ||
70
+ v.type === "agent_end" ||
71
+ v.type === "turn_start" ||
72
+ v.type === "turn_end" ||
73
+ v.type === "message_start" ||
74
+ v.type === "message_end"
75
+ );
76
+ }
77
+
78
+ export class TeammateRpc {
79
+ readonly name: string;
80
+ readonly sessionFile?: string;
81
+
82
+ status: TeammateStatus = "starting";
83
+ lastAssistantText = "";
84
+ lastError: string | null = null;
85
+
86
+ /** Task currently assigned by the team lead (if any). */
87
+ currentTaskId: string | null = null;
88
+
89
+ private proc: ReturnType<typeof spawn> | null = null;
90
+ private pending = new Map<string, { resolve: (v: RpcResponse) => void; reject: (e: Error) => void }>();
91
+ private nextId = 0;
92
+ private buffer = "";
93
+ private stderr = "";
94
+ private eventListeners: Array<(ev: AgentEvent) => void> = [];
95
+ private closeListeners: Array<(code: number | null) => void> = [];
96
+
97
+ constructor(name: string, sessionFile?: string) {
98
+ this.name = name;
99
+ this.sessionFile = sessionFile;
100
+ }
101
+
102
+ onEvent(listener: (ev: AgentEvent) => void): () => void {
103
+ this.eventListeners.push(listener);
104
+ return () => {
105
+ const idx = this.eventListeners.indexOf(listener);
106
+ if (idx >= 0) this.eventListeners.splice(idx, 1);
107
+ };
108
+ }
109
+
110
+ onClose(listener: (code: number | null) => void): () => void {
111
+ this.closeListeners.push(listener);
112
+ return () => {
113
+ const idx = this.closeListeners.indexOf(listener);
114
+ if (idx >= 0) this.closeListeners.splice(idx, 1);
115
+ };
116
+ }
117
+
118
+ getStderr(): string {
119
+ return this.stderr;
120
+ }
121
+
122
+ async start(opts: { cwd: string; env: Record<string, string>; args: string[] }): Promise<void> {
123
+ if (this.proc) throw new Error("Teammate already started");
124
+
125
+ this.proc = spawn("indusagi", ["--mode", "rpc", ...opts.args], {
126
+ cwd: opts.cwd,
127
+ env: { ...process.env, ...opts.env },
128
+ stdio: ["pipe", "pipe", "pipe"],
129
+ });
130
+
131
+ this.proc.on("error", (err) => {
132
+ this.status = "error";
133
+ this.lastError = String(err);
134
+ for (const [id, p] of this.pending.entries()) {
135
+ p.reject(new Error(`Process error before response (id=${id}): ${String(err)}`));
136
+ }
137
+ this.pending.clear();
138
+ });
139
+
140
+ this.proc.stderr?.on("data", (d) => {
141
+ this.stderr += d.toString();
142
+ });
143
+
144
+ this.proc.stdout?.on("data", (d) => {
145
+ this.buffer += d.toString();
146
+ let idx: number;
147
+ while ((idx = this.buffer.indexOf("\n")) >= 0) {
148
+ const line = this.buffer.slice(0, idx);
149
+ this.buffer = this.buffer.slice(idx + 1);
150
+ this.handleLine(line);
151
+ }
152
+ });
153
+
154
+ this.proc.on("close", (code) => {
155
+ this.status = code === 0 ? "stopped" : "error";
156
+ if (code !== 0) this.lastError = `Teammate process exited with code ${code}`;
157
+ for (const [id, p] of this.pending.entries()) {
158
+ p.reject(new Error(`Process exited before response (id=${id})`));
159
+ }
160
+ this.pending.clear();
161
+ for (const l of this.closeListeners) l(code);
162
+ });
163
+
164
+ // Give the child a moment to boot.
165
+ await new Promise((r) => setTimeout(r, 120));
166
+ this.status = "idle";
167
+ }
168
+
169
+ async stop(): Promise<void> {
170
+ if (!this.proc) return;
171
+ try {
172
+ await this.abort();
173
+ } catch {
174
+ // ignore
175
+ }
176
+ this.proc.kill("SIGTERM");
177
+ setTimeout(() => {
178
+ if (this.proc && !this.proc.killed) this.proc.kill("SIGKILL");
179
+ }, 1000);
180
+ this.proc = null;
181
+ this.status = "stopped";
182
+ }
183
+
184
+ async prompt(message: string): Promise<void> {
185
+ await this.send({ type: "prompt", message });
186
+ }
187
+
188
+ async steer(message: string): Promise<void> {
189
+ await this.send({ type: "steer", message });
190
+ }
191
+
192
+ async followUp(message: string): Promise<void> {
193
+ await this.send({ type: "follow_up", message });
194
+ }
195
+
196
+ async abort(): Promise<void> {
197
+ await this.send({ type: "abort" });
198
+ }
199
+
200
+ async getState(): Promise<unknown> {
201
+ const resp = await this.send({ type: "get_state" });
202
+ return resp.data;
203
+ }
204
+
205
+ async setSessionName(name: string): Promise<void> {
206
+ await this.send({ type: "set_session_name", name });
207
+ }
208
+
209
+ private handleLine(line: string) {
210
+ if (!line.trim()) return;
211
+ const obj = safeParseJsonLine(line);
212
+ if (obj === null) return;
213
+
214
+ // Response
215
+ if (isRpcResponse(obj)) {
216
+ if (typeof obj.id !== "string") return;
217
+ const pending = this.pending.get(obj.id);
218
+ if (!pending) return;
219
+ this.pending.delete(obj.id);
220
+ pending.resolve(obj);
221
+ return;
222
+ }
223
+
224
+ // Agent event
225
+ if (!isAgentEvent(obj)) return;
226
+ const ev = obj;
227
+ if (ev.type === "agent_start") {
228
+ this.status = "streaming";
229
+ this.lastAssistantText = "";
230
+ }
231
+ if (ev.type === "agent_end") {
232
+ this.status = "idle";
233
+ }
234
+ if (ev.type === "message_update") {
235
+ const ame = ev.assistantMessageEvent;
236
+ if (ame.type === "text_delta") {
237
+ this.lastAssistantText += ame.delta;
238
+ }
239
+ }
240
+
241
+ for (const l of this.eventListeners) l(ev);
242
+ }
243
+
244
+ private async send(cmd: RpcCommandWithoutId): Promise<RpcResponse> {
245
+ if (!this.proc || !this.proc.stdin) throw new Error("Teammate is not running");
246
+ const id = `req-${this.name}-${this.nextId++}`;
247
+ const full = { id, ...cmd } satisfies RpcCommand;
248
+
249
+ const payload = JSON.stringify(full) + "\n";
250
+ this.proc.stdin.write(payload);
251
+
252
+ return await new Promise<RpcResponse>((resolve, reject) => {
253
+ this.pending.set(id, { resolve, reject });
254
+ setTimeout(() => {
255
+ if (!this.pending.has(id)) return;
256
+ this.pending.delete(id);
257
+ reject(new Error(`Timeout waiting for response (id=${id}, cmd=${full.type})`));
258
+ }, 60_000);
259
+ });
260
+ }
261
+ }