indusagi-coding-agent 0.1.28 → 0.1.30

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 (147) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE.md +22 -0
  3. package/README.md +2 -0
  4. package/dist/core/messages.d.ts +1 -76
  5. package/dist/core/messages.d.ts.map +1 -1
  6. package/dist/core/messages.js +1 -122
  7. package/dist/core/messages.js.map +1 -1
  8. package/dist/core/session-manager.d.ts +1 -447
  9. package/dist/core/session-manager.d.ts.map +1 -1
  10. package/dist/core/session-manager.js +1 -1203
  11. package/dist/core/session-manager.js.map +1 -1
  12. package/package.json +2 -2
  13. package/docs/COMPLETE-GUIDE.md +0 -300
  14. package/docs/COMPREHENSIVE-CLI-SUMMARY.md +0 -900
  15. package/docs/MODES-ARCHITECTURE.md +0 -565
  16. package/docs/PRINT-MODE-GUIDE.md +0 -456
  17. package/docs/README.md +0 -78
  18. package/docs/RPC-GUIDE.md +0 -705
  19. package/docs/UTILS-IMPLEMENTATION-SUMMARY.md +0 -647
  20. package/docs/UTILS-MODULE-OVERVIEW.md +0 -1480
  21. package/docs/UTILS-QA-CHECKLIST.md +0 -1061
  22. package/docs/UTILS-USAGE-GUIDE.md +0 -1419
  23. package/docs/compaction.md +0 -390
  24. package/docs/custom-provider.md +0 -538
  25. package/docs/development.md +0 -69
  26. package/docs/extensions.md +0 -1733
  27. package/docs/hooks.md +0 -378
  28. package/docs/images/doom-extension.png +0 -0
  29. package/docs/images/interactive-mode.png +0 -0
  30. package/docs/images/tree-view.png +0 -0
  31. package/docs/json.md +0 -79
  32. package/docs/keybindings.md +0 -162
  33. package/docs/models.md +0 -193
  34. package/docs/packages.md +0 -163
  35. package/docs/prompt-templates.md +0 -67
  36. package/docs/providers.md +0 -147
  37. package/docs/rpc.md +0 -1048
  38. package/docs/sdk.md +0 -969
  39. package/docs/session.md +0 -412
  40. package/docs/settings.md +0 -219
  41. package/docs/shell-aliases.md +0 -13
  42. package/docs/skills.md +0 -226
  43. package/docs/subagents.md +0 -225
  44. package/docs/terminal-setup.md +0 -65
  45. package/docs/themes.md +0 -295
  46. package/docs/tree.md +0 -219
  47. package/docs/tui.md +0 -887
  48. package/docs/web-tools.md +0 -304
  49. package/docs/windows.md +0 -17
  50. package/examples/README.md +0 -25
  51. package/examples/extensions/README.md +0 -192
  52. package/examples/extensions/antigravity-image-gen.ts +0 -414
  53. package/examples/extensions/auto-commit-on-exit.ts +0 -49
  54. package/examples/extensions/bookmark.ts +0 -50
  55. package/examples/extensions/claude-rules.ts +0 -86
  56. package/examples/extensions/confirm-destructive.ts +0 -59
  57. package/examples/extensions/custom-compaction.ts +0 -115
  58. package/examples/extensions/custom-footer.ts +0 -65
  59. package/examples/extensions/custom-header.ts +0 -73
  60. package/examples/extensions/custom-provider-anthropic/index.ts +0 -605
  61. package/examples/extensions/custom-provider-anthropic/package-lock.json +0 -24
  62. package/examples/extensions/custom-provider-anthropic/package.json +0 -19
  63. package/examples/extensions/custom-provider-gitlab-duo/index.ts +0 -350
  64. package/examples/extensions/custom-provider-gitlab-duo/package.json +0 -16
  65. package/examples/extensions/custom-provider-gitlab-duo/test.ts +0 -83
  66. package/examples/extensions/dirty-repo-guard.ts +0 -56
  67. package/examples/extensions/doom-overlay/README.md +0 -46
  68. package/examples/extensions/doom-overlay/doom/build/doom.js +0 -21
  69. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  70. package/examples/extensions/doom-overlay/doom/build.sh +0 -152
  71. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +0 -72
  72. package/examples/extensions/doom-overlay/doom-component.ts +0 -133
  73. package/examples/extensions/doom-overlay/doom-engine.ts +0 -173
  74. package/examples/extensions/doom-overlay/doom-keys.ts +0 -105
  75. package/examples/extensions/doom-overlay/index.ts +0 -74
  76. package/examples/extensions/doom-overlay/wad-finder.ts +0 -51
  77. package/examples/extensions/event-bus.ts +0 -43
  78. package/examples/extensions/file-trigger.ts +0 -41
  79. package/examples/extensions/git-checkpoint.ts +0 -53
  80. package/examples/extensions/handoff.ts +0 -151
  81. package/examples/extensions/hello.ts +0 -25
  82. package/examples/extensions/inline-bash.ts +0 -94
  83. package/examples/extensions/input-transform.ts +0 -43
  84. package/examples/extensions/interactive-shell.ts +0 -196
  85. package/examples/extensions/mac-system-theme.ts +0 -47
  86. package/examples/extensions/message-renderer.ts +0 -60
  87. package/examples/extensions/modal-editor.ts +0 -86
  88. package/examples/extensions/model-status.ts +0 -31
  89. package/examples/extensions/notify.ts +0 -25
  90. package/examples/extensions/overlay-qa-tests.ts +0 -882
  91. package/examples/extensions/overlay-test.ts +0 -151
  92. package/examples/extensions/permission-gate.ts +0 -34
  93. package/examples/extensions/pirate.ts +0 -47
  94. package/examples/extensions/plan-mode/README.md +0 -65
  95. package/examples/extensions/plan-mode/index.ts +0 -341
  96. package/examples/extensions/plan-mode/utils.ts +0 -168
  97. package/examples/extensions/preset.ts +0 -399
  98. package/examples/extensions/protected-paths.ts +0 -30
  99. package/examples/extensions/qna.ts +0 -120
  100. package/examples/extensions/question.ts +0 -265
  101. package/examples/extensions/questionnaire.ts +0 -428
  102. package/examples/extensions/rainbow-editor.ts +0 -88
  103. package/examples/extensions/sandbox/index.ts +0 -318
  104. package/examples/extensions/sandbox/package-lock.json +0 -92
  105. package/examples/extensions/sandbox/package.json +0 -19
  106. package/examples/extensions/send-user-message.ts +0 -97
  107. package/examples/extensions/session-name.ts +0 -27
  108. package/examples/extensions/shutdown-command.ts +0 -63
  109. package/examples/extensions/snake.ts +0 -344
  110. package/examples/extensions/space-invaders.ts +0 -561
  111. package/examples/extensions/ssh.ts +0 -220
  112. package/examples/extensions/status-line.ts +0 -40
  113. package/examples/extensions/subagent/README.md +0 -172
  114. package/examples/extensions/subagent/agents/planner.md +0 -37
  115. package/examples/extensions/subagent/agents/reviewer.md +0 -35
  116. package/examples/extensions/subagent/agents/scout.md +0 -50
  117. package/examples/extensions/subagent/agents/worker.md +0 -24
  118. package/examples/extensions/subagent/agents.ts +0 -127
  119. package/examples/extensions/subagent/index.ts +0 -964
  120. package/examples/extensions/subagent/prompts/implement-and-review.md +0 -10
  121. package/examples/extensions/subagent/prompts/implement.md +0 -10
  122. package/examples/extensions/subagent/prompts/scout-and-plan.md +0 -9
  123. package/examples/extensions/summarize.ts +0 -196
  124. package/examples/extensions/timed-confirm.ts +0 -70
  125. package/examples/extensions/todo.ts +0 -300
  126. package/examples/extensions/tool-override.ts +0 -144
  127. package/examples/extensions/tools.ts +0 -147
  128. package/examples/extensions/trigger-compact.ts +0 -40
  129. package/examples/extensions/truncated-tool.ts +0 -193
  130. package/examples/extensions/widget-placement.ts +0 -17
  131. package/examples/extensions/with-deps/index.ts +0 -36
  132. package/examples/extensions/with-deps/package-lock.json +0 -31
  133. package/examples/extensions/with-deps/package.json +0 -22
  134. package/examples/sdk/01-minimal.ts +0 -22
  135. package/examples/sdk/02-custom-model.ts +0 -50
  136. package/examples/sdk/03-custom-prompt.ts +0 -55
  137. package/examples/sdk/04-skills.ts +0 -46
  138. package/examples/sdk/05-tools.ts +0 -56
  139. package/examples/sdk/06-extensions.ts +0 -88
  140. package/examples/sdk/07-context-files.ts +0 -40
  141. package/examples/sdk/08-prompt-templates.ts +0 -47
  142. package/examples/sdk/09-api-keys-and-oauth.ts +0 -48
  143. package/examples/sdk/10-settings.ts +0 -38
  144. package/examples/sdk/11-sessions.ts +0 -48
  145. package/examples/sdk/12-full-control.ts +0 -82
  146. package/examples/sdk/13-codex-oauth.ts +0 -37
  147. package/examples/sdk/README.md +0 -144
@@ -1,964 +0,0 @@
1
- /**
2
- * Subagent Tool - Delegate tasks to specialized agents
3
- *
4
- * Spawns a separate `indusagi` process for each subagent invocation,
5
- * giving it an isolated context window.
6
- *
7
- * Supports three modes:
8
- * - Single: { agent: "name", task: "..." }
9
- * - Parallel: { tasks: [{ agent: "name", task: "..." }, ...] }
10
- * - Chain: { chain: [{ agent: "name", task: "... {previous} ..." }, ...] }
11
- *
12
- * Uses JSON mode to capture structured output from subagents.
13
- */
14
-
15
- import { spawn } from "node:child_process";
16
- import * as fs from "node:fs";
17
- import * as os from "node:os";
18
- import * as path from "node:path";
19
- import type { AgentToolResult } from "indusagi/agent";
20
- import type { Message } from "indusagi/ai";
21
- import { StringEnum } from "indusagi/ai";
22
- import { type ExtensionAPI, getMarkdownTheme } from "indusagi-coding-agent";
23
- import { Container, Markdown, Spacer, Text } from "indusagi/tui";
24
- import { Type } from "@sinclair/typebox";
25
- import { type AgentConfig, type AgentScope, discoverAgents } from "./agents.js";
26
-
27
- const MAX_PARALLEL_TASKS = 8;
28
- const MAX_CONCURRENCY = 4;
29
- const COLLAPSED_ITEM_COUNT = 10;
30
-
31
- function formatTokens(count: number): string {
32
- if (count < 1000) return count.toString();
33
- if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
34
- if (count < 1000000) return `${Math.round(count / 1000)}k`;
35
- return `${(count / 1000000).toFixed(1)}M`;
36
- }
37
-
38
- function formatUsageStats(
39
- usage: {
40
- input: number;
41
- output: number;
42
- cacheRead: number;
43
- cacheWrite: number;
44
- cost: number;
45
- contextTokens?: number;
46
- turns?: number;
47
- },
48
- model?: string,
49
- ): string {
50
- const parts: string[] = [];
51
- if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
52
- if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
53
- if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
54
- if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
55
- if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
56
- if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
57
- if (usage.contextTokens && usage.contextTokens > 0) {
58
- parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
59
- }
60
- if (model) parts.push(model);
61
- return parts.join(" ");
62
- }
63
-
64
- function formatToolCall(
65
- toolName: string,
66
- args: Record<string, unknown>,
67
- themeFg: (color: any, text: string) => string,
68
- ): string {
69
- const shortenPath = (p: string) => {
70
- const home = os.homedir();
71
- return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
72
- };
73
-
74
- switch (toolName) {
75
- case "bash": {
76
- const command = (args.command as string) || "...";
77
- const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
78
- return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
79
- }
80
- case "read": {
81
- const rawPath = (args.file_path || args.path || "...") as string;
82
- const filePath = shortenPath(rawPath);
83
- const offset = args.offset as number | undefined;
84
- const limit = args.limit as number | undefined;
85
- let text = themeFg("accent", filePath);
86
- if (offset !== undefined || limit !== undefined) {
87
- const startLine = offset ?? 1;
88
- const endLine = limit !== undefined ? startLine + limit - 1 : "";
89
- text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
90
- }
91
- return themeFg("muted", "read ") + text;
92
- }
93
- case "write": {
94
- const rawPath = (args.file_path || args.path || "...") as string;
95
- const filePath = shortenPath(rawPath);
96
- const content = (args.content || "") as string;
97
- const lines = content.split("\n").length;
98
- let text = themeFg("muted", "write ") + themeFg("accent", filePath);
99
- if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
100
- return text;
101
- }
102
- case "edit": {
103
- const rawPath = (args.file_path || args.path || "...") as string;
104
- return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
105
- }
106
- case "ls": {
107
- const rawPath = (args.path || ".") as string;
108
- return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
109
- }
110
- case "find": {
111
- const pattern = (args.pattern || "*") as string;
112
- const rawPath = (args.path || ".") as string;
113
- return themeFg("muted", "find ") + themeFg("accent", pattern) + themeFg("dim", ` in ${shortenPath(rawPath)}`);
114
- }
115
- case "grep": {
116
- const pattern = (args.pattern || "") as string;
117
- const rawPath = (args.path || ".") as string;
118
- return (
119
- themeFg("muted", "grep ") +
120
- themeFg("accent", `/${pattern}/`) +
121
- themeFg("dim", ` in ${shortenPath(rawPath)}`)
122
- );
123
- }
124
- default: {
125
- const argsStr = JSON.stringify(args);
126
- const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
127
- return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
128
- }
129
- }
130
- }
131
-
132
- interface UsageStats {
133
- input: number;
134
- output: number;
135
- cacheRead: number;
136
- cacheWrite: number;
137
- cost: number;
138
- contextTokens: number;
139
- turns: number;
140
- }
141
-
142
- interface SingleResult {
143
- agent: string;
144
- agentSource: "user" | "project" | "unknown";
145
- task: string;
146
- exitCode: number;
147
- messages: Message[];
148
- stderr: string;
149
- usage: UsageStats;
150
- model?: string;
151
- stopReason?: string;
152
- errorMessage?: string;
153
- step?: number;
154
- }
155
-
156
- interface SubagentDetails {
157
- mode: "single" | "parallel" | "chain";
158
- agentScope: AgentScope;
159
- projectAgentsDir: string | null;
160
- results: SingleResult[];
161
- }
162
-
163
- function getFinalOutput(messages: Message[]): string {
164
- for (let i = messages.length - 1; i >= 0; i--) {
165
- const msg = messages[i];
166
- if (msg.role === "assistant") {
167
- for (const part of msg.content) {
168
- if (part.type === "text") return part.text;
169
- }
170
- }
171
- }
172
- return "";
173
- }
174
-
175
- type DisplayItem = { type: "text"; text: string } | { type: "toolCall"; name: string; args: Record<string, any> };
176
-
177
- function getDisplayItems(messages: Message[]): DisplayItem[] {
178
- const items: DisplayItem[] = [];
179
- for (const msg of messages) {
180
- if (msg.role === "assistant") {
181
- for (const part of msg.content) {
182
- if (part.type === "text") items.push({ type: "text", text: part.text });
183
- else if (part.type === "toolCall") items.push({ type: "toolCall", name: part.name, args: part.arguments });
184
- }
185
- }
186
- }
187
- return items;
188
- }
189
-
190
- async function mapWithConcurrencyLimit<TIn, TOut>(
191
- items: TIn[],
192
- concurrency: number,
193
- fn: (item: TIn, index: number) => Promise<TOut>,
194
- ): Promise<TOut[]> {
195
- if (items.length === 0) return [];
196
- const limit = Math.max(1, Math.min(concurrency, items.length));
197
- const results: TOut[] = new Array(items.length);
198
- let nextIndex = 0;
199
- const workers = new Array(limit).fill(null).map(async () => {
200
- while (true) {
201
- const current = nextIndex++;
202
- if (current >= items.length) return;
203
- results[current] = await fn(items[current], current);
204
- }
205
- });
206
- await Promise.all(workers);
207
- return results;
208
- }
209
-
210
- function writePromptToTempFile(agentName: string, prompt: string): { dir: string; filePath: string } {
211
- const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "indusagi-subagent-"));
212
- const safeName = agentName.replace(/[^\w.-]+/g, "_");
213
- const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
214
- fs.writeFileSync(filePath, prompt, { encoding: "utf-8", mode: 0o600 });
215
- return { dir: tmpDir, filePath };
216
- }
217
-
218
- type OnUpdateCallback = (partial: AgentToolResult<SubagentDetails>) => void;
219
-
220
- async function runSingleAgent(
221
- defaultCwd: string,
222
- agents: AgentConfig[],
223
- agentName: string,
224
- task: string,
225
- cwd: string | undefined,
226
- step: number | undefined,
227
- signal: AbortSignal | undefined,
228
- onUpdate: OnUpdateCallback | undefined,
229
- makeDetails: (results: SingleResult[]) => SubagentDetails,
230
- ): Promise<SingleResult> {
231
- const agent = agents.find((a) => a.name === agentName);
232
-
233
- if (!agent) {
234
- return {
235
- agent: agentName,
236
- agentSource: "unknown",
237
- task,
238
- exitCode: 1,
239
- messages: [],
240
- stderr: `Unknown agent: ${agentName}`,
241
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
242
- step,
243
- };
244
- }
245
-
246
- const args: string[] = ["--mode", "json", "-p", "--no-session"];
247
- if (agent.model) args.push("--model", agent.model);
248
- if (agent.tools && agent.tools.length > 0) args.push("--tools", agent.tools.join(","));
249
-
250
- let tmpPromptDir: string | null = null;
251
- let tmpPromptPath: string | null = null;
252
-
253
- const currentResult: SingleResult = {
254
- agent: agentName,
255
- agentSource: agent.source,
256
- task,
257
- exitCode: 0,
258
- messages: [],
259
- stderr: "",
260
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
261
- model: agent.model,
262
- step,
263
- };
264
-
265
- const emitUpdate = () => {
266
- if (onUpdate) {
267
- onUpdate({
268
- content: [{ type: "text", text: getFinalOutput(currentResult.messages) || "(running...)" }],
269
- details: makeDetails([currentResult]),
270
- });
271
- }
272
- };
273
-
274
- try {
275
- if (agent.systemPrompt.trim()) {
276
- const tmp = writePromptToTempFile(agent.name, agent.systemPrompt);
277
- tmpPromptDir = tmp.dir;
278
- tmpPromptPath = tmp.filePath;
279
- args.push("--append-system-prompt", tmpPromptPath);
280
- }
281
-
282
- args.push(`Task: ${task}`);
283
- let wasAborted = false;
284
-
285
- const exitCode = await new Promise<number>((resolve) => {
286
- const proc = spawn("indusagi", args, { cwd: cwd ?? defaultCwd, shell: false, stdio: ["ignore", "pipe", "pipe"] });
287
- let buffer = "";
288
-
289
- const processLine = (line: string) => {
290
- if (!line.trim()) return;
291
- let event: any;
292
- try {
293
- event = JSON.parse(line);
294
- } catch {
295
- return;
296
- }
297
-
298
- if (event.type === "message_end" && event.message) {
299
- const msg = event.message as Message;
300
- currentResult.messages.push(msg);
301
-
302
- if (msg.role === "assistant") {
303
- currentResult.usage.turns++;
304
- const usage = msg.usage;
305
- if (usage) {
306
- currentResult.usage.input += usage.input || 0;
307
- currentResult.usage.output += usage.output || 0;
308
- currentResult.usage.cacheRead += usage.cacheRead || 0;
309
- currentResult.usage.cacheWrite += usage.cacheWrite || 0;
310
- currentResult.usage.cost += usage.cost?.total || 0;
311
- currentResult.usage.contextTokens = usage.totalTokens || 0;
312
- }
313
- if (!currentResult.model && msg.model) currentResult.model = msg.model;
314
- if (msg.stopReason) currentResult.stopReason = msg.stopReason;
315
- if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
316
- }
317
- emitUpdate();
318
- }
319
-
320
- if (event.type === "tool_result_end" && event.message) {
321
- currentResult.messages.push(event.message as Message);
322
- emitUpdate();
323
- }
324
- };
325
-
326
- proc.stdout.on("data", (data) => {
327
- buffer += data.toString();
328
- const lines = buffer.split("\n");
329
- buffer = lines.pop() || "";
330
- for (const line of lines) processLine(line);
331
- });
332
-
333
- proc.stderr.on("data", (data) => {
334
- currentResult.stderr += data.toString();
335
- });
336
-
337
- proc.on("close", (code) => {
338
- if (buffer.trim()) processLine(buffer);
339
- resolve(code ?? 0);
340
- });
341
-
342
- proc.on("error", () => {
343
- resolve(1);
344
- });
345
-
346
- if (signal) {
347
- const killProc = () => {
348
- wasAborted = true;
349
- proc.kill("SIGTERM");
350
- setTimeout(() => {
351
- if (!proc.killed) proc.kill("SIGKILL");
352
- }, 5000);
353
- };
354
- if (signal.aborted) killProc();
355
- else signal.addEventListener("abort", killProc, { once: true });
356
- }
357
- });
358
-
359
- currentResult.exitCode = exitCode;
360
- if (wasAborted) throw new Error("Subagent was aborted");
361
- return currentResult;
362
- } finally {
363
- if (tmpPromptPath)
364
- try {
365
- fs.unlinkSync(tmpPromptPath);
366
- } catch {
367
- /* ignore */
368
- }
369
- if (tmpPromptDir)
370
- try {
371
- fs.rmdirSync(tmpPromptDir);
372
- } catch {
373
- /* ignore */
374
- }
375
- }
376
- }
377
-
378
- const TaskItem = Type.Object({
379
- agent: Type.String({ description: "Name of the agent to invoke" }),
380
- task: Type.String({ description: "Task to delegate to the agent" }),
381
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
382
- });
383
-
384
- const ChainItem = Type.Object({
385
- agent: Type.String({ description: "Name of the agent to invoke" }),
386
- task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
387
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
388
- });
389
-
390
- const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
391
- description: 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
392
- default: "user",
393
- });
394
-
395
- const SubagentParams = Type.Object({
396
- agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
397
- task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
398
- tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
399
- chain: Type.Optional(Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" })),
400
- agentScope: Type.Optional(AgentScopeSchema),
401
- confirmProjectAgents: Type.Optional(
402
- Type.Boolean({ description: "Prompt before running project-local agents. Default: true.", default: true }),
403
- ),
404
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
405
- });
406
-
407
- export default function (indusagi: ExtensionAPI) {
408
- indusagi.registerTool({
409
- name: "subagent",
410
- label: "Subagent",
411
- description: [
412
- "Delegate tasks to specialized subagents with isolated context.",
413
- "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
414
- 'Default agent scope is "user" (from ~/.indusagi/agent/agents).',
415
- 'To enable project-local agents in .indusagi/agents, set agentScope: "both" (or "project").',
416
- ].join(" "),
417
- parameters: SubagentParams,
418
-
419
- async execute(_toolCallId, params, onUpdate, ctx, signal) {
420
- const agentScope: AgentScope = params.agentScope ?? "user";
421
- const discovery = discoverAgents(ctx.cwd, agentScope);
422
- const agents = discovery.agents;
423
- const confirmProjectAgents = params.confirmProjectAgents ?? true;
424
-
425
- const hasChain = (params.chain?.length ?? 0) > 0;
426
- const hasTasks = (params.tasks?.length ?? 0) > 0;
427
- const hasSingle = Boolean(params.agent && params.task);
428
- const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
429
-
430
- const makeDetails =
431
- (mode: "single" | "parallel" | "chain") =>
432
- (results: SingleResult[]): SubagentDetails => ({
433
- mode,
434
- agentScope,
435
- projectAgentsDir: discovery.projectAgentsDir,
436
- results,
437
- });
438
-
439
- if (modeCount !== 1) {
440
- const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
441
- return {
442
- content: [
443
- {
444
- type: "text",
445
- text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
446
- },
447
- ],
448
- details: makeDetails("single")([]),
449
- };
450
- }
451
-
452
- if ((agentScope === "project" || agentScope === "both") && confirmProjectAgents && ctx.hasUI) {
453
- const requestedAgentNames = new Set<string>();
454
- if (params.chain) for (const step of params.chain) requestedAgentNames.add(step.agent);
455
- if (params.tasks) for (const t of params.tasks) requestedAgentNames.add(t.agent);
456
- if (params.agent) requestedAgentNames.add(params.agent);
457
-
458
- const projectAgentsRequested = Array.from(requestedAgentNames)
459
- .map((name) => agents.find((a) => a.name === name))
460
- .filter((a): a is AgentConfig => a?.source === "project");
461
-
462
- if (projectAgentsRequested.length > 0) {
463
- const names = projectAgentsRequested.map((a) => a.name).join(", ");
464
- const dir = discovery.projectAgentsDir ?? "(unknown)";
465
- const ok = await ctx.ui.confirm(
466
- "Run project-local agents?",
467
- `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
468
- );
469
- if (!ok)
470
- return {
471
- content: [{ type: "text", text: "Canceled: project-local agents not approved." }],
472
- details: makeDetails(hasChain ? "chain" : hasTasks ? "parallel" : "single")([]),
473
- };
474
- }
475
- }
476
-
477
- if (params.chain && params.chain.length > 0) {
478
- const results: SingleResult[] = [];
479
- let previousOutput = "";
480
-
481
- for (let i = 0; i < params.chain.length; i++) {
482
- const step = params.chain[i];
483
- const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
484
-
485
- // Create update callback that includes all previous results
486
- const chainUpdate: OnUpdateCallback | undefined = onUpdate
487
- ? (partial) => {
488
- // Combine completed results with current streaming result
489
- const currentResult = partial.details?.results[0];
490
- if (currentResult) {
491
- const allResults = [...results, currentResult];
492
- onUpdate({
493
- content: partial.content,
494
- details: makeDetails("chain")(allResults),
495
- });
496
- }
497
- }
498
- : undefined;
499
-
500
- const result = await runSingleAgent(
501
- ctx.cwd,
502
- agents,
503
- step.agent,
504
- taskWithContext,
505
- step.cwd,
506
- i + 1,
507
- signal,
508
- chainUpdate,
509
- makeDetails("chain"),
510
- );
511
- results.push(result);
512
-
513
- const isError =
514
- result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
515
- if (isError) {
516
- const errorMsg =
517
- result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
518
- return {
519
- content: [{ type: "text", text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}` }],
520
- details: makeDetails("chain")(results),
521
- isError: true,
522
- };
523
- }
524
- previousOutput = getFinalOutput(result.messages);
525
- }
526
- return {
527
- content: [{ type: "text", text: getFinalOutput(results[results.length - 1].messages) || "(no output)" }],
528
- details: makeDetails("chain")(results),
529
- };
530
- }
531
-
532
- if (params.tasks && params.tasks.length > 0) {
533
- if (params.tasks.length > MAX_PARALLEL_TASKS)
534
- return {
535
- content: [
536
- {
537
- type: "text",
538
- text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
539
- },
540
- ],
541
- details: makeDetails("parallel")([]),
542
- };
543
-
544
- // Track all results for streaming updates
545
- const allResults: SingleResult[] = new Array(params.tasks.length);
546
-
547
- // Initialize placeholder results
548
- for (let i = 0; i < params.tasks.length; i++) {
549
- allResults[i] = {
550
- agent: params.tasks[i].agent,
551
- agentSource: "unknown",
552
- task: params.tasks[i].task,
553
- exitCode: -1, // -1 = still running
554
- messages: [],
555
- stderr: "",
556
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
557
- };
558
- }
559
-
560
- const emitParallelUpdate = () => {
561
- if (onUpdate) {
562
- const running = allResults.filter((r) => r.exitCode === -1).length;
563
- const done = allResults.filter((r) => r.exitCode !== -1).length;
564
- onUpdate({
565
- content: [
566
- { type: "text", text: `Parallel: ${done}/${allResults.length} done, ${running} running...` },
567
- ],
568
- details: makeDetails("parallel")([...allResults]),
569
- });
570
- }
571
- };
572
-
573
- const results = await mapWithConcurrencyLimit(params.tasks, MAX_CONCURRENCY, async (t, index) => {
574
- const result = await runSingleAgent(
575
- ctx.cwd,
576
- agents,
577
- t.agent,
578
- t.task,
579
- t.cwd,
580
- undefined,
581
- signal,
582
- // Per-task update callback
583
- (partial) => {
584
- if (partial.details?.results[0]) {
585
- allResults[index] = partial.details.results[0];
586
- emitParallelUpdate();
587
- }
588
- },
589
- makeDetails("parallel"),
590
- );
591
- allResults[index] = result;
592
- emitParallelUpdate();
593
- return result;
594
- });
595
-
596
- const successCount = results.filter((r) => r.exitCode === 0).length;
597
- const summaries = results.map((r) => {
598
- const output = getFinalOutput(r.messages);
599
- const preview = output.slice(0, 100) + (output.length > 100 ? "..." : "");
600
- return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
601
- });
602
- return {
603
- content: [
604
- {
605
- type: "text",
606
- text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
607
- },
608
- ],
609
- details: makeDetails("parallel")(results),
610
- };
611
- }
612
-
613
- if (params.agent && params.task) {
614
- const result = await runSingleAgent(
615
- ctx.cwd,
616
- agents,
617
- params.agent,
618
- params.task,
619
- params.cwd,
620
- undefined,
621
- signal,
622
- onUpdate,
623
- makeDetails("single"),
624
- );
625
- const isError = result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
626
- if (isError) {
627
- const errorMsg =
628
- result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
629
- return {
630
- content: [{ type: "text", text: `Agent ${result.stopReason || "failed"}: ${errorMsg}` }],
631
- details: makeDetails("single")([result]),
632
- isError: true,
633
- };
634
- }
635
- return {
636
- content: [{ type: "text", text: getFinalOutput(result.messages) || "(no output)" }],
637
- details: makeDetails("single")([result]),
638
- };
639
- }
640
-
641
- const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
642
- return {
643
- content: [{ type: "text", text: `Invalid parameters. Available agents: ${available}` }],
644
- details: makeDetails("single")([]),
645
- };
646
- },
647
-
648
- renderCall(args, theme) {
649
- const scope: AgentScope = args.agentScope ?? "user";
650
- if (args.chain && args.chain.length > 0) {
651
- let text =
652
- theme.fg("toolTitle", theme.bold("subagent ")) +
653
- theme.fg("accent", `chain (${args.chain.length} steps)`) +
654
- theme.fg("muted", ` [${scope}]`);
655
- for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
656
- const step = args.chain[i];
657
- // Clean up {previous} placeholder for display
658
- const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
659
- const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
660
- text +=
661
- "\n " +
662
- theme.fg("muted", `${i + 1}.`) +
663
- " " +
664
- theme.fg("accent", step.agent) +
665
- theme.fg("dim", ` ${preview}`);
666
- }
667
- if (args.chain.length > 3) text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
668
- return new Text(text, 0, 0);
669
- }
670
- if (args.tasks && args.tasks.length > 0) {
671
- let text =
672
- theme.fg("toolTitle", theme.bold("subagent ")) +
673
- theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
674
- theme.fg("muted", ` [${scope}]`);
675
- for (const t of args.tasks.slice(0, 3)) {
676
- const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
677
- text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
678
- }
679
- if (args.tasks.length > 3) text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
680
- return new Text(text, 0, 0);
681
- }
682
- const agentName = args.agent || "...";
683
- const preview = args.task ? (args.task.length > 60 ? `${args.task.slice(0, 60)}...` : args.task) : "...";
684
- let text =
685
- theme.fg("toolTitle", theme.bold("subagent ")) +
686
- theme.fg("accent", agentName) +
687
- theme.fg("muted", ` [${scope}]`);
688
- text += `\n ${theme.fg("dim", preview)}`;
689
- return new Text(text, 0, 0);
690
- },
691
-
692
- renderResult(result, { expanded }, theme) {
693
- const details = result.details as SubagentDetails | undefined;
694
- if (!details || details.results.length === 0) {
695
- const text = result.content[0];
696
- return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
697
- }
698
-
699
- const mdTheme = getMarkdownTheme();
700
-
701
- const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
702
- const toShow = limit ? items.slice(-limit) : items;
703
- const skipped = limit && items.length > limit ? items.length - limit : 0;
704
- let text = "";
705
- if (skipped > 0) text += theme.fg("muted", `... ${skipped} earlier items\n`);
706
- for (const item of toShow) {
707
- if (item.type === "text") {
708
- const preview = expanded ? item.text : item.text.split("\n").slice(0, 3).join("\n");
709
- text += `${theme.fg("toolOutput", preview)}\n`;
710
- } else {
711
- text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
712
- }
713
- }
714
- return text.trimEnd();
715
- };
716
-
717
- if (details.mode === "single" && details.results.length === 1) {
718
- const r = details.results[0];
719
- const isError = r.exitCode !== 0 || r.stopReason === "error" || r.stopReason === "aborted";
720
- const icon = isError ? theme.fg("error", "✗") : theme.fg("success", "✓");
721
- const displayItems = getDisplayItems(r.messages);
722
- const finalOutput = getFinalOutput(r.messages);
723
-
724
- if (expanded) {
725
- const container = new Container();
726
- let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
727
- if (isError && r.stopReason) header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
728
- container.addChild(new Text(header, 0, 0));
729
- if (isError && r.errorMessage)
730
- container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
731
- container.addChild(new Spacer(1));
732
- container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
733
- container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
734
- container.addChild(new Spacer(1));
735
- container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
736
- if (displayItems.length === 0 && !finalOutput) {
737
- container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
738
- } else {
739
- for (const item of displayItems) {
740
- if (item.type === "toolCall")
741
- container.addChild(
742
- new Text(
743
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
744
- 0,
745
- 0,
746
- ),
747
- );
748
- }
749
- if (finalOutput) {
750
- container.addChild(new Spacer(1));
751
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
752
- }
753
- }
754
- const usageStr = formatUsageStats(r.usage, r.model);
755
- if (usageStr) {
756
- container.addChild(new Spacer(1));
757
- container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
758
- }
759
- return container;
760
- }
761
-
762
- let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
763
- if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
764
- if (isError && r.errorMessage) text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
765
- else if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
766
- else {
767
- text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
768
- if (displayItems.length > COLLAPSED_ITEM_COUNT) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
769
- }
770
- const usageStr = formatUsageStats(r.usage, r.model);
771
- if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
772
- return new Text(text, 0, 0);
773
- }
774
-
775
- const aggregateUsage = (results: SingleResult[]) => {
776
- const total = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
777
- for (const r of results) {
778
- total.input += r.usage.input;
779
- total.output += r.usage.output;
780
- total.cacheRead += r.usage.cacheRead;
781
- total.cacheWrite += r.usage.cacheWrite;
782
- total.cost += r.usage.cost;
783
- total.turns += r.usage.turns;
784
- }
785
- return total;
786
- };
787
-
788
- if (details.mode === "chain") {
789
- const successCount = details.results.filter((r) => r.exitCode === 0).length;
790
- const icon = successCount === details.results.length ? theme.fg("success", "✓") : theme.fg("error", "✗");
791
-
792
- if (expanded) {
793
- const container = new Container();
794
- container.addChild(
795
- new Text(
796
- icon +
797
- " " +
798
- theme.fg("toolTitle", theme.bold("chain ")) +
799
- theme.fg("accent", `${successCount}/${details.results.length} steps`),
800
- 0,
801
- 0,
802
- ),
803
- );
804
-
805
- for (const r of details.results) {
806
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
807
- const displayItems = getDisplayItems(r.messages);
808
- const finalOutput = getFinalOutput(r.messages);
809
-
810
- container.addChild(new Spacer(1));
811
- container.addChild(
812
- new Text(
813
- `${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
814
- 0,
815
- 0,
816
- ),
817
- );
818
- container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
819
-
820
- // Show tool calls
821
- for (const item of displayItems) {
822
- if (item.type === "toolCall") {
823
- container.addChild(
824
- new Text(
825
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
826
- 0,
827
- 0,
828
- ),
829
- );
830
- }
831
- }
832
-
833
- // Show final output as markdown
834
- if (finalOutput) {
835
- container.addChild(new Spacer(1));
836
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
837
- }
838
-
839
- const stepUsage = formatUsageStats(r.usage, r.model);
840
- if (stepUsage) container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
841
- }
842
-
843
- const usageStr = formatUsageStats(aggregateUsage(details.results));
844
- if (usageStr) {
845
- container.addChild(new Spacer(1));
846
- container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
847
- }
848
- return container;
849
- }
850
-
851
- // Collapsed view
852
- let text =
853
- icon +
854
- " " +
855
- theme.fg("toolTitle", theme.bold("chain ")) +
856
- theme.fg("accent", `${successCount}/${details.results.length} steps`);
857
- for (const r of details.results) {
858
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
859
- const displayItems = getDisplayItems(r.messages);
860
- text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
861
- if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
862
- else text += `\n${renderDisplayItems(displayItems, 5)}`;
863
- }
864
- const usageStr = formatUsageStats(aggregateUsage(details.results));
865
- if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
866
- text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
867
- return new Text(text, 0, 0);
868
- }
869
-
870
- if (details.mode === "parallel") {
871
- const running = details.results.filter((r) => r.exitCode === -1).length;
872
- const successCount = details.results.filter((r) => r.exitCode === 0).length;
873
- const failCount = details.results.filter((r) => r.exitCode > 0).length;
874
- const isRunning = running > 0;
875
- const icon = isRunning
876
- ? theme.fg("warning", "⏳")
877
- : failCount > 0
878
- ? theme.fg("warning", "◐")
879
- : theme.fg("success", "✓");
880
- const status = isRunning
881
- ? `${successCount + failCount}/${details.results.length} done, ${running} running`
882
- : `${successCount}/${details.results.length} tasks`;
883
-
884
- if (expanded && !isRunning) {
885
- const container = new Container();
886
- container.addChild(
887
- new Text(
888
- `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
889
- 0,
890
- 0,
891
- ),
892
- );
893
-
894
- for (const r of details.results) {
895
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
896
- const displayItems = getDisplayItems(r.messages);
897
- const finalOutput = getFinalOutput(r.messages);
898
-
899
- container.addChild(new Spacer(1));
900
- container.addChild(
901
- new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0),
902
- );
903
- container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
904
-
905
- // Show tool calls
906
- for (const item of displayItems) {
907
- if (item.type === "toolCall") {
908
- container.addChild(
909
- new Text(
910
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
911
- 0,
912
- 0,
913
- ),
914
- );
915
- }
916
- }
917
-
918
- // Show final output as markdown
919
- if (finalOutput) {
920
- container.addChild(new Spacer(1));
921
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
922
- }
923
-
924
- const taskUsage = formatUsageStats(r.usage, r.model);
925
- if (taskUsage) container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
926
- }
927
-
928
- const usageStr = formatUsageStats(aggregateUsage(details.results));
929
- if (usageStr) {
930
- container.addChild(new Spacer(1));
931
- container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
932
- }
933
- return container;
934
- }
935
-
936
- // Collapsed view (or still running)
937
- let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
938
- for (const r of details.results) {
939
- const rIcon =
940
- r.exitCode === -1
941
- ? theme.fg("warning", "⏳")
942
- : r.exitCode === 0
943
- ? theme.fg("success", "✓")
944
- : theme.fg("error", "✗");
945
- const displayItems = getDisplayItems(r.messages);
946
- text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
947
- if (displayItems.length === 0)
948
- text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
949
- else text += `\n${renderDisplayItems(displayItems, 5)}`;
950
- }
951
- if (!isRunning) {
952
- const usageStr = formatUsageStats(aggregateUsage(details.results));
953
- if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
954
- }
955
- if (!expanded) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
956
- return new Text(text, 0, 0);
957
- }
958
-
959
- const text = result.content[0];
960
- return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
961
- },
962
- });
963
- }
964
-