@ymy88/pi-subagents 0.0.0-stage → 0.24.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 (196) hide show
  1. package/CHANGELOG.md +904 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1170 -2
  5. package/SECURITY.md +95 -0
  6. package/dist/abortable.d.ts +12 -0
  7. package/dist/abortable.js +42 -0
  8. package/dist/agent-color.d.ts +35 -0
  9. package/dist/agent-color.js +123 -0
  10. package/dist/agent-file-toggle.d.ts +133 -0
  11. package/dist/agent-file-toggle.js +287 -0
  12. package/dist/agent-manager.d.ts +552 -0
  13. package/dist/agent-manager.js +1466 -0
  14. package/dist/agent-record-entry.d.ts +96 -0
  15. package/dist/agent-record-entry.js +121 -0
  16. package/dist/agent-runner.d.ts +310 -0
  17. package/dist/agent-runner.js +1195 -0
  18. package/dist/agent-source-badge.d.ts +50 -0
  19. package/dist/agent-source-badge.js +74 -0
  20. package/dist/agent-types.d.ts +119 -0
  21. package/dist/agent-types.js +286 -0
  22. package/dist/child-context.d.ts +2 -0
  23. package/dist/child-context.js +12 -0
  24. package/dist/completion-outbox.d.ts +63 -0
  25. package/dist/completion-outbox.js +104 -0
  26. package/dist/context.d.ts +12 -0
  27. package/dist/context.js +56 -0
  28. package/dist/cross-extension-rpc.d.ts +72 -0
  29. package/dist/cross-extension-rpc.js +153 -0
  30. package/dist/custom-agents.d.ts +77 -0
  31. package/dist/custom-agents.js +396 -0
  32. package/dist/default-agents.d.ts +7 -0
  33. package/dist/default-agents.js +121 -0
  34. package/dist/enabled-models.d.ts +49 -0
  35. package/dist/enabled-models.js +145 -0
  36. package/dist/env.d.ts +6 -0
  37. package/dist/env.js +28 -0
  38. package/dist/group-join.d.ts +32 -0
  39. package/dist/group-join.js +116 -0
  40. package/dist/index.d.ts +50 -0
  41. package/dist/index.js +4111 -0
  42. package/dist/invocation-config.d.ts +108 -0
  43. package/dist/invocation-config.js +83 -0
  44. package/dist/memory.d.ts +53 -0
  45. package/dist/memory.js +165 -0
  46. package/dist/mention-clone.d.ts +79 -0
  47. package/dist/mention-clone.js +155 -0
  48. package/dist/mention.d.ts +81 -0
  49. package/dist/mention.js +131 -0
  50. package/dist/model-resolver.d.ts +36 -0
  51. package/dist/model-resolver.js +95 -0
  52. package/dist/model-scope.d.ts +49 -0
  53. package/dist/model-scope.js +48 -0
  54. package/dist/nested-tools.d.ts +57 -0
  55. package/dist/nested-tools.js +303 -0
  56. package/dist/output-file.d.ts +43 -0
  57. package/dist/output-file.js +153 -0
  58. package/dist/package-resources.d.ts +240 -0
  59. package/dist/package-resources.js +519 -0
  60. package/dist/prompts.d.ts +76 -0
  61. package/dist/prompts.js +121 -0
  62. package/dist/schedule-store.d.ts +38 -0
  63. package/dist/schedule-store.js +155 -0
  64. package/dist/schedule.d.ts +109 -0
  65. package/dist/schedule.js +359 -0
  66. package/dist/settings.d.ts +398 -0
  67. package/dist/settings.js +282 -0
  68. package/dist/skill-loader.d.ts +24 -0
  69. package/dist/skill-loader.js +93 -0
  70. package/dist/status-note.d.ts +61 -0
  71. package/dist/status-note.js +85 -0
  72. package/dist/structured-output.d.ts +61 -0
  73. package/dist/structured-output.js +112 -0
  74. package/dist/types.d.ts +391 -0
  75. package/dist/types.js +5 -0
  76. package/dist/ui/agent-mention.d.ts +82 -0
  77. package/dist/ui/agent-mention.js +187 -0
  78. package/dist/ui/agent-widget.d.ts +219 -0
  79. package/dist/ui/agent-widget.js +585 -0
  80. package/dist/ui/conversation-viewer.d.ts +177 -0
  81. package/dist/ui/conversation-viewer.js +905 -0
  82. package/dist/ui/fleet-list.d.ts +211 -0
  83. package/dist/ui/fleet-list.js +489 -0
  84. package/dist/ui/schedule-menu.d.ts +16 -0
  85. package/dist/ui/schedule-menu.js +94 -0
  86. package/dist/ui/select-item.d.ts +27 -0
  87. package/dist/ui/select-item.js +34 -0
  88. package/dist/ui/viewer-keys.d.ts +22 -0
  89. package/dist/ui/viewer-keys.js +18 -0
  90. package/dist/ui/workflow-card.d.ts +175 -0
  91. package/dist/ui/workflow-card.js +332 -0
  92. package/dist/ui/workflow-dialog.d.ts +305 -0
  93. package/dist/ui/workflow-dialog.js +843 -0
  94. package/dist/ui/workflow-menu.d.ts +60 -0
  95. package/dist/ui/workflow-menu.js +147 -0
  96. package/dist/usage.d.ts +135 -0
  97. package/dist/usage.js +120 -0
  98. package/dist/workflow/collisions.d.ts +95 -0
  99. package/dist/workflow/collisions.js +88 -0
  100. package/dist/workflow/entry.d.ts +32 -0
  101. package/dist/workflow/entry.js +29 -0
  102. package/dist/workflow/host.d.ts +62 -0
  103. package/dist/workflow/host.js +419 -0
  104. package/dist/workflow/journal.d.ts +97 -0
  105. package/dist/workflow/journal.js +120 -0
  106. package/dist/workflow/json-schema.d.ts +52 -0
  107. package/dist/workflow/json-schema.js +112 -0
  108. package/dist/workflow/meta.d.ts +67 -0
  109. package/dist/workflow/meta.js +317 -0
  110. package/dist/workflow/progress.d.ts +224 -0
  111. package/dist/workflow/progress.js +361 -0
  112. package/dist/workflow/runtime.d.ts +334 -0
  113. package/dist/workflow/runtime.js +830 -0
  114. package/dist/workflow/saved.d.ts +102 -0
  115. package/dist/workflow/saved.js +247 -0
  116. package/dist/workflow/task.d.ts +136 -0
  117. package/dist/workflow/task.js +207 -0
  118. package/dist/workflow/tool-description.d.ts +38 -0
  119. package/dist/workflow/tool-description.js +199 -0
  120. package/dist/workflow/worker-source.d.ts +47 -0
  121. package/dist/workflow/worker-source.js +778 -0
  122. package/dist/worktree.d.ts +52 -0
  123. package/dist/worktree.js +164 -0
  124. package/dist/xml.d.ts +10 -0
  125. package/dist/xml.js +12 -0
  126. package/docs/packages.md +211 -0
  127. package/docs/rpc.md +192 -0
  128. package/docs/workflows.md +447 -0
  129. package/examples/agent-tool-description.md +42 -0
  130. package/examples/workflows/compose.js +51 -0
  131. package/examples/workflows/fan-out-audit.js +47 -0
  132. package/examples/workflows/gated-fix.js +60 -0
  133. package/examples/workflows/lib/count-child.js +27 -0
  134. package/examples/workflows/review-panel.js +63 -0
  135. package/examples/workflows/structured-findings.js +78 -0
  136. package/package.json +74 -4
  137. package/src/abortable.ts +43 -0
  138. package/src/agent-color.ts +161 -0
  139. package/src/agent-file-toggle.ts +297 -0
  140. package/src/agent-manager.ts +1735 -0
  141. package/src/agent-record-entry.ts +153 -0
  142. package/src/agent-runner.ts +1468 -0
  143. package/src/agent-source-badge.ts +75 -0
  144. package/src/agent-types.ts +346 -0
  145. package/src/child-context.ts +15 -0
  146. package/src/completion-outbox.ts +116 -0
  147. package/src/context.ts +58 -0
  148. package/src/cross-extension-rpc.ts +221 -0
  149. package/src/custom-agents.ts +452 -0
  150. package/src/default-agents.ts +125 -0
  151. package/src/enabled-models.ts +180 -0
  152. package/src/env.ts +33 -0
  153. package/src/group-join.ts +141 -0
  154. package/src/index.ts +4433 -0
  155. package/src/invocation-config.ts +155 -0
  156. package/src/memory.ts +179 -0
  157. package/src/mention-clone.ts +195 -0
  158. package/src/mention.ts +141 -0
  159. package/src/model-resolver.ts +118 -0
  160. package/src/model-scope.ts +70 -0
  161. package/src/nested-tools.ts +426 -0
  162. package/src/output-file.ts +164 -0
  163. package/src/package-resources.ts +652 -0
  164. package/src/prompts.ts +171 -0
  165. package/src/schedule-store.ts +153 -0
  166. package/src/schedule.ts +386 -0
  167. package/src/settings.ts +650 -0
  168. package/src/skill-loader.ts +102 -0
  169. package/src/status-note.ts +90 -0
  170. package/src/structured-output.ts +130 -0
  171. package/src/types.ts +404 -0
  172. package/src/ui/agent-mention.ts +216 -0
  173. package/src/ui/agent-widget.ts +661 -0
  174. package/src/ui/conversation-viewer.ts +928 -0
  175. package/src/ui/fleet-list.ts +559 -0
  176. package/src/ui/schedule-menu.ts +105 -0
  177. package/src/ui/select-item.ts +45 -0
  178. package/src/ui/viewer-keys.ts +43 -0
  179. package/src/ui/workflow-card.ts +470 -0
  180. package/src/ui/workflow-dialog.ts +1115 -0
  181. package/src/ui/workflow-menu.ts +193 -0
  182. package/src/usage.ts +167 -0
  183. package/src/workflow/collisions.ts +123 -0
  184. package/src/workflow/entry.ts +47 -0
  185. package/src/workflow/host.ts +467 -0
  186. package/src/workflow/journal.ts +164 -0
  187. package/src/workflow/json-schema.ts +129 -0
  188. package/src/workflow/meta.ts +325 -0
  189. package/src/workflow/progress.ts +551 -0
  190. package/src/workflow/runtime.ts +1219 -0
  191. package/src/workflow/saved.ts +259 -0
  192. package/src/workflow/task.ts +302 -0
  193. package/src/workflow/tool-description.ts +200 -0
  194. package/src/workflow/worker-source.ts +781 -0
  195. package/src/worktree.ts +205 -0
  196. package/src/xml.ts +13 -0
@@ -0,0 +1,164 @@
1
+ /**
2
+ * output-file.ts — Streaming JSONL output file for agent transcripts.
3
+ *
4
+ * Creates a per-agent output file that streams conversation turns as JSONL,
5
+ * matching Claude Code's task output file format.
6
+ */
7
+
8
+ import { appendFileSync, chmodSync, mkdirSync, writeFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import type { AgentSession, AgentSessionEvent } from "@earendil-works/pi-coding-agent";
12
+
13
+ /**
14
+ * Project/global default for writing a subagent's `.output` transcript; a custom
15
+ * agent's `output_transcript` overrides it per agent.
16
+ *
17
+ * State lives here rather than in an index.ts closure because both spawn paths
18
+ * need it — the top-level Agent tool and the nested delegation tools. Same
19
+ * reason `scopeModels` lives in model-scope.ts: a setting only one path can read
20
+ * is a setting the other path silently ignores.
21
+ */
22
+ let outputTranscriptDefault = true;
23
+
24
+ export function getOutputTranscriptDefault(): boolean { return outputTranscriptDefault; }
25
+ export function setOutputTranscriptDefault(b: boolean): void { outputTranscriptDefault = b; }
26
+
27
+ /**
28
+ * Encode a cwd path as a filesystem-safe directory name. Handles:
29
+ * - POSIX: "/home/user/project" → "home-user-project"
30
+ * - Windows: "C:\Users\foo\project" → "Users-foo-project"
31
+ * - UNC: "\\\\server\\share\\project" → "server-share-project"
32
+ */
33
+ export function encodeCwd(cwd: string): string {
34
+ return cwd
35
+ .replace(/[/\\]/g, "-") // both separators → dash
36
+ .replace(/^[A-Za-z]:-/, "") // strip Windows drive prefix ("C:-")
37
+ .replace(/^-+/, ""); // strip leading dashes (POSIX root, UNC)
38
+ }
39
+
40
+ /**
41
+ * The per-session scratch directory, created if missing.
42
+ * Mirrors Claude Code's layout: /tmp/{prefix}-{uid}/{encoded-cwd}/{sessionId}/tasks
43
+ *
44
+ * Shared with the workflow tool, which persists each invocation's script here so
45
+ * iterating on one is edit-file-then-rerun — the same convention, one directory.
46
+ */
47
+ export function sessionTaskDir(cwd: string, sessionId: string): string {
48
+ const encoded = encodeCwd(cwd);
49
+ const root = join(tmpdir(), `pi-subagents-${process.getuid?.() ?? 0}`);
50
+ mkdirSync(root, { recursive: true, mode: 0o700 });
51
+ // chmod is a no-op on Windows and throws on some Windows filesystems.
52
+ // On Unix we still want to enforce 0o700 past umask, so only swallow on Windows.
53
+ try {
54
+ chmodSync(root, 0o700);
55
+ } catch (err) {
56
+ if (process.platform !== "win32") throw err;
57
+ }
58
+ const dir = join(root, encoded, sessionId, "tasks");
59
+ mkdirSync(dir, { recursive: true });
60
+ return dir;
61
+ }
62
+
63
+ /** Create the output file path, ensuring the directory exists. */
64
+ export function createOutputFilePath(cwd: string, agentId: string, sessionId: string): string {
65
+ return join(sessionTaskDir(cwd, sessionId), `${agentId}.output`);
66
+ }
67
+
68
+ /**
69
+ * Ensure a transcript file exists without disturbing what is already in it.
70
+ *
71
+ * A resume reuses the agent's existing transcript (same deterministic path), so
72
+ * it must never call `writeInitialEntry` — that truncates, discarding turns the
73
+ * completion notification still points the user at, and any history the session
74
+ * has since compacted away is gone for good. Appending nothing creates the file
75
+ * when this is the agent's first transcript and is a no-op when it is not.
76
+ */
77
+ export function ensureOutputFile(path: string): void {
78
+ try {
79
+ appendFileSync(path, "", "utf-8");
80
+ } catch { /* ignore — streaming writes are best-effort too */ }
81
+ }
82
+
83
+ /** Write the initial user prompt entry. */
84
+ export function writeInitialEntry(path: string, agentId: string, prompt: string, cwd: string): void {
85
+ const entry = {
86
+ isSidechain: true,
87
+ agentId,
88
+ type: "user",
89
+ message: { role: "user", content: prompt },
90
+ timestamp: new Date().toISOString(),
91
+ cwd,
92
+ };
93
+ writeFileSync(path, JSON.stringify(entry) + "\n", "utf-8");
94
+ }
95
+
96
+ /**
97
+ * Subscribe to session events and flush new messages to the output file on each turn_end.
98
+ * Returns a cleanup function that does a final flush and unsubscribes.
99
+ */
100
+ export function streamToOutputFile(
101
+ session: AgentSession,
102
+ path: string,
103
+ agentId: string,
104
+ cwd: string,
105
+ startIndex?: number,
106
+ ): () => void {
107
+ // Index of the first message this stream is responsible for. A resume hands
108
+ // in the session's length as of just before the run: the session already
109
+ // holds every prior turn, and re-emitting those would duplicate history that
110
+ // is already in the file. A spawn has already written its prompt as the
111
+ // initial entry, so it starts after the first user message — found on the
112
+ // first flush, because pi puts a system message ahead of it and adds both
113
+ // only once the prompt runs.
114
+ let writtenCount = startIndex;
115
+
116
+ const flush = () => {
117
+ const messages = session.messages;
118
+ if (writtenCount === undefined) {
119
+ const prompt = messages.findIndex((m) => m.role === "user");
120
+ if (prompt === -1) return;
121
+ writtenCount = prompt + 1;
122
+ }
123
+ while (writtenCount < messages.length) {
124
+ const msg = messages[writtenCount++];
125
+ // Pi's prompt and tool-loadout records; a Claude Code transcript has no
126
+ // such entry.
127
+ if (msg.role === "system") continue;
128
+ const entry = {
129
+ isSidechain: true,
130
+ agentId,
131
+ type: msg.role === "assistant" ? "assistant" : msg.role === "user" ? "user" : "toolResult",
132
+ message: msg,
133
+ timestamp: new Date().toISOString(),
134
+ cwd,
135
+ };
136
+ try {
137
+ appendFileSync(path, JSON.stringify(entry) + "\n", "utf-8");
138
+ } catch { /* ignore write errors */ }
139
+ }
140
+ };
141
+
142
+ const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
143
+ if (event.type === "turn_end") flush();
144
+ // Compaction replaces session.messages with a shorter, summarized array,
145
+ // leaving writtenCount past the new end — without re-anchoring, the flush
146
+ // loop would never match again and streaming would halt for good (#145).
147
+ // Flush before it runs so any not-yet-flushed tail still reaches the file,
148
+ // then re-anchor to the rebuilt array once it lands. The re-anchor is
149
+ // deferred a microtask because on the overflow-retry path pi trims the
150
+ // trailing error assistant message AFTER emitting compaction_end —
151
+ // anchoring synchronously would sit one past the trimmed array and skip
152
+ // the first post-compaction message. Aborted/failed compactions leave
153
+ // session.messages untouched, so only successful ones re-anchor.
154
+ if (event.type === "compaction_start") flush();
155
+ if (event.type === "compaction_end" && !event.aborted && event.result) {
156
+ queueMicrotask(() => { writtenCount = session.messages.length; });
157
+ }
158
+ });
159
+
160
+ return () => {
161
+ flush();
162
+ unsubscribe();
163
+ };
164
+ }