@timmyjl12/pi-subagents 0.0.0-stage → 0.20.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/CHANGELOG.md +827 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1079 -2
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +18 -0
- package/dist/active-agents-prompt.js +34 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +123 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +485 -0
- package/dist/agent-manager.js +1380 -0
- package/dist/agent-runner.d.ts +319 -0
- package/dist/agent-runner.js +1128 -0
- package/dist/agent-types.d.ts +119 -0
- package/dist/agent-types.js +286 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +3 -0
- package/dist/child-session-shutdown.js +48 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +66 -0
- package/dist/cross-extension-rpc.js +138 -0
- package/dist/custom-agents.d.ts +54 -0
- package/dist/custom-agents.js +386 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/error-log.d.ts +58 -0
- package/dist/error-log.js +168 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +3896 -0
- package/dist/invocation-config.d.ts +137 -0
- package/dist/invocation-config.js +129 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +55 -0
- package/dist/nested-tools.js +299 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +53 -0
- package/dist/output-file.js +248 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/schedule-store.d.ts +44 -0
- package/dist/schedule-store.js +210 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +359 -0
- package/dist/settings.d.ts +360 -0
- package/dist/settings.js +251 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +382 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +235 -0
- package/dist/ui/agent-widget.js +652 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +540 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +172 -0
- package/dist/ui/fleet-overlay.js +691 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +230 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +843 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +135 -0
- package/dist/usage.js +120 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +62 -0
- package/dist/workflow/host.js +363 -0
- package/dist/workflow/journal.d.ts +97 -0
- package/dist/workflow/journal.js +120 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +334 -0
- package/dist/workflow/runtime.js +830 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +136 -0
- package/dist/workflow/task.js +207 -0
- package/dist/workflow/tool-description.d.ts +38 -0
- package/dist/workflow/tool-description.js +199 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/FORK.md +40 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +189 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +39 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +1601 -0
- package/src/agent-runner.ts +1372 -0
- package/src/agent-types.ts +345 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +46 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +198 -0
- package/src/custom-agents.ts +417 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +193 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +4218 -0
- package/src/invocation-config.ts +230 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +422 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +258 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/schedule-store.ts +201 -0
- package/src/schedule.ts +386 -0
- package/src/settings.ts +587 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +394 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +732 -0
- package/src/ui/conversation-viewer.ts +523 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +847 -0
- package/src/ui/native-transcript.ts +220 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +167 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +404 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/** Native Pi cards without a second InteractiveMode or ownership of the child session. */
|
|
2
|
+
import type { AssistantMessage, ToolResultMessage } from "@earendil-works/pi-ai";
|
|
3
|
+
import {
|
|
4
|
+
type AgentSession, type AgentSessionEvent, AssistantMessageComponent, CustomMessageComponent,
|
|
5
|
+
createBashToolDefinition, createEditToolDefinition, createFindToolDefinition,
|
|
6
|
+
createGrepToolDefinition, createLsToolDefinition, createPowerShellToolDefinition,
|
|
7
|
+
createReadToolDefinition, createWriteToolDefinition, ToolExecutionComponent, UserMessageComponent,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { type Component, type MarkdownTheme, Text, type TUI, type TuiMouseEvent, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
10
|
+
import { extractText } from "../context.js";
|
|
11
|
+
import { boundToolResult, getToolProgress, RESULT_MAX_CHARS, type ToolDisplayResult, truncationNote } from "./tool-progress.js";
|
|
12
|
+
|
|
13
|
+
type AgentMessage = AgentSession["messages"][number];
|
|
14
|
+
type Renderers = NonNullable<ConstructorParameters<typeof ToolExecutionComponent>[4]>;
|
|
15
|
+
type Entry = { component: Component; raw: () => string; failed?: boolean; failedPrefix?: string; elided?: number; top: number; height: number };
|
|
16
|
+
type ToolEntry = Entry & { component: ToolExecutionComponent; name: string; args: unknown; result?: ToolDisplayResult };
|
|
17
|
+
const factories = {
|
|
18
|
+
read: createReadToolDefinition, bash: createBashToolDefinition, powershell: createPowerShellToolDefinition,
|
|
19
|
+
edit: createEditToolDefinition, write: createWriteToolDefinition, grep: createGrepToolDefinition,
|
|
20
|
+
find: createFindToolDefinition, ls: createLsToolDefinition,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export class NativeTranscript implements Component {
|
|
24
|
+
private entries: Entry[] = [];
|
|
25
|
+
private tools = new Map<string, ToolEntry>();
|
|
26
|
+
private seen = new WeakSet<AgentMessage>();
|
|
27
|
+
private streaming: { message: AssistantMessage; entry: Entry; component: AssistantMessageComponent } | undefined;
|
|
28
|
+
private readonly builtIns = new Map<string, Renderers>();
|
|
29
|
+
private expanded = false;
|
|
30
|
+
|
|
31
|
+
constructor(private session: AgentSession, private tui: TUI, private markdownTheme: MarkdownTheme) {}
|
|
32
|
+
|
|
33
|
+
/** Synchronous replay, called after subscribing. No await/gap between snapshot and event delivery. */
|
|
34
|
+
hydrate(): void {
|
|
35
|
+
this.entries = [];
|
|
36
|
+
this.tools.clear();
|
|
37
|
+
this.seen = new WeakSet();
|
|
38
|
+
this.streaming = undefined;
|
|
39
|
+
for (const message of this.session.messages) this.addMessage(message);
|
|
40
|
+
const partial = this.session.agent?.state.streamingMessage;
|
|
41
|
+
if (partial?.role === "assistant" && !this.seen.has(partial)) this.updateAssistant(partial, true);
|
|
42
|
+
for (const progress of getToolProgress(this.session)) {
|
|
43
|
+
const entry = this.ensureTool(progress.toolCallId, progress.toolName, progress.args);
|
|
44
|
+
entry.component.markExecutionStarted();
|
|
45
|
+
entry.component.setArgsComplete();
|
|
46
|
+
if (progress.result) this.updateTool(progress.toolCallId, progress.result, true, progress.elided);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
handleEvent(event: AgentSessionEvent): void {
|
|
51
|
+
if (event.type === "message_start" || event.type === "message_end") {
|
|
52
|
+
if (event.message.role === "assistant") {
|
|
53
|
+
this.updateAssistant(event.message, event.type === "message_start");
|
|
54
|
+
} else if (event.type === "message_end" || event.message.role === "user") {
|
|
55
|
+
this.addMessage(event.message);
|
|
56
|
+
}
|
|
57
|
+
} else if (event.type === "message_update" && event.message.role === "assistant") {
|
|
58
|
+
this.updateAssistant(event.message, true);
|
|
59
|
+
} else if (event.type === "tool_execution_start" || event.type === "tool_execution_update") {
|
|
60
|
+
const entry = this.ensureTool(event.toolCallId, event.toolName, event.args);
|
|
61
|
+
entry.component.markExecutionStarted();
|
|
62
|
+
entry.component.setArgsComplete();
|
|
63
|
+
if (event.type === "tool_execution_update") this.updateTool(event.toolCallId, { ...event.partialResult, isError: false }, true);
|
|
64
|
+
} else if (event.type === "tool_execution_end") {
|
|
65
|
+
this.ensureTool(event.toolCallId, event.toolName, {});
|
|
66
|
+
this.updateTool(event.toolCallId, { ...event.result, isError: event.isError }, false);
|
|
67
|
+
} else if (event.type === "compaction_end" && event.result && !event.aborted) {
|
|
68
|
+
this.hydrate();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private renderers(name: string): Renderers | undefined {
|
|
73
|
+
const definition = this.session.getToolDefinition?.(name);
|
|
74
|
+
if (!this.builtIns.has(name) && Object.hasOwn(factories, name)) {
|
|
75
|
+
// Public factories provide the callbacks missing from session built-ins. Never execute them.
|
|
76
|
+
this.builtIns.set(name, factories[name as keyof typeof factories](this.cwd));
|
|
77
|
+
}
|
|
78
|
+
const fallback = this.builtIns.get(name);
|
|
79
|
+
return definition ? {
|
|
80
|
+
...definition,
|
|
81
|
+
renderCall: definition.renderCall ?? fallback?.renderCall,
|
|
82
|
+
renderResult: definition.renderResult ?? fallback?.renderResult,
|
|
83
|
+
} : fallback;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private get cwd(): string { return this.session.sessionManager?.getCwd() ?? process.cwd(); }
|
|
87
|
+
|
|
88
|
+
private ensureTool(id: string, name: string, args: unknown): ToolEntry {
|
|
89
|
+
const existing = this.tools.get(id);
|
|
90
|
+
if (existing) return existing;
|
|
91
|
+
const component = new ToolExecutionComponent(name, id, args, {
|
|
92
|
+
showImages: this.session.settingsManager?.getShowImages() ?? true,
|
|
93
|
+
}, this.renderers(name), this.tui, this.cwd);
|
|
94
|
+
component.setExpanded(this.expanded);
|
|
95
|
+
const entry: ToolEntry = {
|
|
96
|
+
component, name, args, top: 0, height: 0,
|
|
97
|
+
raw: () => `${name} ${JSON.stringify(entry.args)}\n${entry.result?.content.filter(p => p.type === "text").map(p => p.text ?? "").join("\n") ?? ""}`,
|
|
98
|
+
};
|
|
99
|
+
this.entries.push(entry);
|
|
100
|
+
this.tools.set(id, entry);
|
|
101
|
+
return entry;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private updateTool(id: string, result: ToolDisplayResult, partial: boolean, alreadyElided = 0): void {
|
|
105
|
+
const entry = this.tools.get(id);
|
|
106
|
+
if (!entry) return;
|
|
107
|
+
const bounded = boundToolResult(result);
|
|
108
|
+
entry.result = bounded.result;
|
|
109
|
+
entry.elided = bounded.elided + alreadyElided;
|
|
110
|
+
entry.component.updateResult(bounded.result, partial);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private updateAssistant(message: AssistantMessage, partial: boolean): void {
|
|
114
|
+
if (!this.streaming && this.seen.has(message)) return;
|
|
115
|
+
if (!this.streaming) {
|
|
116
|
+
const component = new AssistantMessageComponent(undefined,
|
|
117
|
+
this.session.settingsManager?.getHideThinkingBlock() ?? false, this.markdownTheme, undefined,
|
|
118
|
+
this.session.settingsManager?.getOutputPad() ?? 1, this.session.extensionRunner?.getMarkdownTransformers());
|
|
119
|
+
const entry: Entry = { component, raw: () => extractText(message.content), top: 0, height: 0 };
|
|
120
|
+
this.streaming = { message, entry, component };
|
|
121
|
+
this.entries.push(entry);
|
|
122
|
+
}
|
|
123
|
+
const current = this.streaming;
|
|
124
|
+
current.message = message;
|
|
125
|
+
current.entry.raw = () => message.content.map(p => p.type === "thinking" ? p.thinking : p.type === "text" ? p.text : "").join("\n");
|
|
126
|
+
current.component.updateContent(message, partial);
|
|
127
|
+
for (const part of message.content) {
|
|
128
|
+
if (part.type !== "toolCall") continue;
|
|
129
|
+
const entry = this.ensureTool(part.id, part.name, part.arguments);
|
|
130
|
+
entry.args = part.arguments;
|
|
131
|
+
entry.component.updateArgs(part.arguments);
|
|
132
|
+
if (!partial) entry.component.setArgsComplete();
|
|
133
|
+
}
|
|
134
|
+
if (!partial) {
|
|
135
|
+
this.seen.add(message);
|
|
136
|
+
this.streaming = undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private addMessage(message: AgentMessage): void {
|
|
141
|
+
if (this.seen.has(message)) return;
|
|
142
|
+
if (message.role === "assistant") { this.updateAssistant(message, false); return; }
|
|
143
|
+
this.seen.add(message);
|
|
144
|
+
if (message.role === "toolResult") {
|
|
145
|
+
const entry = this.ensureTool(message.toolCallId, message.toolName, {});
|
|
146
|
+
entry.component.setArgsComplete();
|
|
147
|
+
this.updateTool(message.toolCallId, message, false);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
let component: Component;
|
|
151
|
+
let raw: () => string;
|
|
152
|
+
if (message.role === "user") {
|
|
153
|
+
const text = typeof message.content === "string" ? message.content : extractText(message.content);
|
|
154
|
+
raw = () => text;
|
|
155
|
+
component = new UserMessageComponent(text, this.markdownTheme, this.session.settingsManager?.getOutputPad() ?? 1,
|
|
156
|
+
this.session.extensionRunner?.getMarkdownTransformers());
|
|
157
|
+
} else if (message.role === "custom") {
|
|
158
|
+
if (!message.display) return;
|
|
159
|
+
raw = () => typeof message.content === "string" ? message.content : extractText(message.content);
|
|
160
|
+
component = new CustomMessageComponent(message, this.session.extensionRunner?.getMessageRenderer(message.customType),
|
|
161
|
+
this.markdownTheme, this.session.settingsManager?.getOutputPad() ?? 1);
|
|
162
|
+
} else if (message.role === "bashExecution") {
|
|
163
|
+
raw = () => `$ ${message.command}\n${message.output.slice(0, RESULT_MAX_CHARS)}`;
|
|
164
|
+
component = new Text(raw(), 0, 0);
|
|
165
|
+
this.entries.push({ component, raw, top: 0, height: 0, elided: Math.max(0, message.output.length - RESULT_MAX_CHARS) });
|
|
166
|
+
return;
|
|
167
|
+
} else if (message.role === "compactionSummary" || message.role === "branchSummary") {
|
|
168
|
+
raw = () => message.summary;
|
|
169
|
+
component = new Text(raw(), 0, 0);
|
|
170
|
+
} else { return; }
|
|
171
|
+
this.entries.push({ component, raw, top: 0, height: 0 });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Inspection uses the same live source, not only committed history. */
|
|
175
|
+
inspectionMessages(): (AgentMessage & { viewerElided?: number })[] {
|
|
176
|
+
const messages: (AgentMessage & { viewerElided?: number })[] = [...this.session.messages];
|
|
177
|
+
if (this.streaming && !messages.includes(this.streaming.message)) messages.push(this.streaming.message);
|
|
178
|
+
const committed = new Set(messages.filter(m => m.role === "toolResult").map(m => m.toolCallId));
|
|
179
|
+
for (const [id, entry] of this.tools) {
|
|
180
|
+
if (!entry.result || committed.has(id)) continue;
|
|
181
|
+
messages.push({ role: "toolResult", toolCallId: id, toolName: entry.name,
|
|
182
|
+
content: entry.result.content as ToolResultMessage["content"], details: entry.result.details,
|
|
183
|
+
isError: entry.result.isError, timestamp: 0, viewerElided: entry.elided });
|
|
184
|
+
}
|
|
185
|
+
return messages;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
toggleExpanded(): void {
|
|
189
|
+
this.expanded = !this.expanded;
|
|
190
|
+
for (const entry of this.tools.values()) entry.component.setExpanded(this.expanded);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
render(width: number): string[] {
|
|
194
|
+
const lines: string[] = [];
|
|
195
|
+
for (const entry of this.entries) {
|
|
196
|
+
entry.top = lines.length;
|
|
197
|
+
if (entry.failed && !entry.raw().startsWith(entry.failedPrefix ?? "")) entry.failed = false;
|
|
198
|
+
try {
|
|
199
|
+
lines.push(...(entry.failed ? wrapTextWithAnsi(entry.raw(), width) : entry.component.render(width)));
|
|
200
|
+
} catch {
|
|
201
|
+
// Arbitrary extension renderers/Markdown must not take down the parent TUI.
|
|
202
|
+
entry.failed = true;
|
|
203
|
+
entry.failedPrefix = entry.raw();
|
|
204
|
+
lines.push(...wrapTextWithAnsi(entry.failedPrefix, width));
|
|
205
|
+
}
|
|
206
|
+
if (entry.elided) lines.push(truncationNote(entry.elided));
|
|
207
|
+
entry.height = lines.length - entry.top;
|
|
208
|
+
}
|
|
209
|
+
return lines.map(line => truncateToWidth(line, width));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
handleMouse(event: TuiMouseEvent) {
|
|
213
|
+
const entry = this.entries.find(item => event.y >= item.top && event.y < item.top + item.height);
|
|
214
|
+
return entry?.component.handleMouse?.({ ...event, y: event.y - entry.top, height: entry.height });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
invalidate(): void {
|
|
218
|
+
for (const entry of this.entries) { entry.failed = false; entry.component.invalidate(); }
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* schedule-menu.ts — `/agents → Scheduled jobs` submenu.
|
|
3
|
+
*
|
|
4
|
+
* Minimal v1 surface: list scheduled jobs, select one to inspect details +
|
|
5
|
+
* confirm cancellation. No create wizard (the `Agent` tool's `schedule` param
|
|
6
|
+
* is the canonical creation path), no toggle/cleanup (cancel is enough for
|
|
7
|
+
* "I scheduled something dumb, get rid of it"). Add management surfaces here
|
|
8
|
+
* if real demand emerges.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
12
|
+
import type { SubagentScheduler } from "../schedule.js";
|
|
13
|
+
import type { ScheduledSubagent } from "../types.js";
|
|
14
|
+
import { selectItem } from "./select-item.js";
|
|
15
|
+
|
|
16
|
+
/** Format an ISO timestamp as relative time ("in 4h", "2d ago", "—"). */
|
|
17
|
+
function relTime(iso: string | undefined, now = Date.now()): string {
|
|
18
|
+
if (!iso) return "—";
|
|
19
|
+
const t = new Date(iso).getTime();
|
|
20
|
+
if (Number.isNaN(t)) return "—";
|
|
21
|
+
const diff = t - now;
|
|
22
|
+
const abs = Math.abs(diff);
|
|
23
|
+
const future = diff > 0;
|
|
24
|
+
if (abs < 60_000) return future ? "in <1m" : "<1m ago";
|
|
25
|
+
const m = Math.round(abs / 60_000);
|
|
26
|
+
if (m < 60) return future ? `in ${m}m` : `${m}m ago`;
|
|
27
|
+
const h = Math.round(abs / 3_600_000);
|
|
28
|
+
if (h < 24) return future ? `in ${h}h` : `${h}h ago`;
|
|
29
|
+
const d = Math.round(abs / 86_400_000);
|
|
30
|
+
return future ? `in ${d}d` : `${d}d ago`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One-line status icon. */
|
|
34
|
+
function statusIcon(j: ScheduledSubagent): string {
|
|
35
|
+
if (!j.enabled) return "✗";
|
|
36
|
+
if (j.lastStatus === "error") return "!";
|
|
37
|
+
if (j.lastStatus === "running") return "⋯";
|
|
38
|
+
return "✓";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Compact selectable row — name, schedule, agent type, next/last run, count. */
|
|
42
|
+
function formatJob(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
|
|
43
|
+
const next = scheduler.getNextRun(j.id);
|
|
44
|
+
return [
|
|
45
|
+
statusIcon(j),
|
|
46
|
+
j.name.padEnd(18).slice(0, 18),
|
|
47
|
+
j.schedule.padEnd(14).slice(0, 14),
|
|
48
|
+
`[${j.subagent_type}]`,
|
|
49
|
+
`next ${relTime(next)}`,
|
|
50
|
+
`last ${relTime(j.lastRun)}`,
|
|
51
|
+
`runs ${j.runCount}`,
|
|
52
|
+
].join(" ");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Multi-line details block for the cancel confirm. */
|
|
56
|
+
function formatDetails(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
|
|
57
|
+
const next = scheduler.getNextRun(j.id) ?? "—";
|
|
58
|
+
return [
|
|
59
|
+
`name: ${j.name}`,
|
|
60
|
+
`schedule: ${j.schedule} (${j.scheduleType})`,
|
|
61
|
+
`agent: ${j.subagent_type}`,
|
|
62
|
+
`prompt: ${j.prompt.slice(0, 200)}${j.prompt.length > 200 ? "…" : ""}`,
|
|
63
|
+
`created: ${j.createdAt}`,
|
|
64
|
+
`last run: ${j.lastRun ?? "—"} (${j.lastStatus ?? "—"})`,
|
|
65
|
+
`next run: ${next}`,
|
|
66
|
+
`runs: ${j.runCount}`,
|
|
67
|
+
].join("\n");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* List scheduled jobs; selecting one opens a cancel-confirm with details.
|
|
72
|
+
* Returns when the user backs out or after a cancellation.
|
|
73
|
+
*/
|
|
74
|
+
export async function showSchedulesMenu(
|
|
75
|
+
ctx: ExtensionCommandContext,
|
|
76
|
+
scheduler: SubagentScheduler,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
if (!scheduler.isActive()) {
|
|
79
|
+
ctx.ui.notify("Scheduler is not active in this session.", "warning");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const jobs = scheduler.list();
|
|
84
|
+
if (jobs.length === 0) {
|
|
85
|
+
ctx.ui.notify("No scheduled jobs.", "info");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Numbered + item-paired: two jobs whose names agree in the first 18
|
|
90
|
+
// characters format identically, and matching the returned string back
|
|
91
|
+
// against a parallel label array cancelled whichever came first.
|
|
92
|
+
const job = await selectItem(
|
|
93
|
+
ctx.ui,
|
|
94
|
+
`Scheduled jobs (${jobs.length}) — select to cancel`,
|
|
95
|
+
jobs,
|
|
96
|
+
j => formatJob(j, scheduler),
|
|
97
|
+
);
|
|
98
|
+
if (!job) return;
|
|
99
|
+
|
|
100
|
+
const ok = await ctx.ui.confirm(`Cancel "${job.name}"?`, formatDetails(job, scheduler));
|
|
101
|
+
if (!ok) return;
|
|
102
|
+
|
|
103
|
+
scheduler.removeJob(job.id);
|
|
104
|
+
ctx.ui.notify(`Cancelled "${job.name}".`, "info");
|
|
105
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* select-item.ts — pick an item from a list via `ctx.ui.select`, safely.
|
|
3
|
+
*
|
|
4
|
+
* Pi's dialog API is `select(title, options: string[]) => Promise<string | undefined>`:
|
|
5
|
+
* strings in, string out, with no index or value form. Callers therefore have to
|
|
6
|
+
* map the returned string back to the item it came from, and the obvious way —
|
|
7
|
+
* `labels.indexOf(choice)` over a parallel array — silently resolves to the
|
|
8
|
+
* FIRST match whenever two rows format identically. Row formatters here truncate
|
|
9
|
+
* (job names to 18 chars, agent descriptions to whatever fits), and the text they
|
|
10
|
+
* truncate is LLM-authored, so collisions are ordinary rather than exotic.
|
|
11
|
+
*
|
|
12
|
+
* This numbers every row, which makes the labels unique by construction — no
|
|
13
|
+
* data-dependent branch that only executes in the case nobody exercises — and
|
|
14
|
+
* keeps each label paired with its item so a later edit that sorts or filters
|
|
15
|
+
* between building and resolving cannot desync them.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Minimal shape of the `ctx.ui` surface this needs. */
|
|
19
|
+
export interface SelectUI {
|
|
20
|
+
select(title: string, options: string[]): Promise<string | undefined>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Show a numbered picker and return the chosen item (not its label).
|
|
25
|
+
*
|
|
26
|
+
* Returns undefined when the user escapes, or when the returned string is not
|
|
27
|
+
* one we offered.
|
|
28
|
+
*/
|
|
29
|
+
export async function selectItem<T>(
|
|
30
|
+
ui: SelectUI,
|
|
31
|
+
title: string,
|
|
32
|
+
items: readonly T[],
|
|
33
|
+
format: (item: T, index: number) => string,
|
|
34
|
+
): Promise<T | undefined> {
|
|
35
|
+
// Pad the number so a 10+ item list keeps its columns aligned.
|
|
36
|
+
const width = String(items.length).length;
|
|
37
|
+
const rows = items.map((item, i) => ({
|
|
38
|
+
item,
|
|
39
|
+
label: `${String(i + 1).padStart(width)}. ${format(item, i)}`,
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
const choice = await ui.select(title, rows.map(r => r.label));
|
|
43
|
+
if (!choice) return undefined;
|
|
44
|
+
return rows.find(r => r.label === choice)?.item;
|
|
45
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** Transient, session-owned tool progress. Attach before the child starts, not when a viewer opens. */
|
|
2
|
+
import type { AgentSession, ToolExecutionComponent } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
export const RESULT_MAX_CHARS = 16_000;
|
|
5
|
+
export function truncationNote(elided: number): string {
|
|
6
|
+
const thousands = elided < 999_950;
|
|
7
|
+
const value = thousands ? elided / 1_000 : elided / 1_000_000;
|
|
8
|
+
const count = elided < 1_000 ? `${elided}` : `${value.toFixed(1).replace(/\.0$/, "")}${thousands ? "k" : "M"}`;
|
|
9
|
+
return `... (truncated, ${count} more character${elided === 1 ? "" : "s"})`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type ToolDisplayResult = Parameters<ToolExecutionComponent["updateResult"]>[0];
|
|
13
|
+
export interface ToolProgress {
|
|
14
|
+
toolCallId: string;
|
|
15
|
+
toolName: string;
|
|
16
|
+
args: unknown;
|
|
17
|
+
result?: ToolDisplayResult;
|
|
18
|
+
elided: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Bound text before retaining it or handing it to a renderer; never change the session's result. */
|
|
22
|
+
export function boundToolResult(result: ToolDisplayResult): { result: ToolDisplayResult; elided: number } {
|
|
23
|
+
let remaining = RESULT_MAX_CHARS;
|
|
24
|
+
let elided = 0;
|
|
25
|
+
const content = result.content.map(part => {
|
|
26
|
+
if (part.type !== "text" || part.text === undefined) return part;
|
|
27
|
+
const text = part.text.slice(0, remaining);
|
|
28
|
+
remaining -= text.length;
|
|
29
|
+
elided += part.text.length - text.length;
|
|
30
|
+
return { ...part, text };
|
|
31
|
+
});
|
|
32
|
+
return { result: { ...result, content }, elided };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const caches = new WeakMap<AgentSession, { active: Map<string, ToolProgress>; unsubscribe: () => void }>();
|
|
36
|
+
|
|
37
|
+
/** Idempotent across resumes. The manager owns this subscription until session eviction/disposal. */
|
|
38
|
+
export function attachToolProgress(session: AgentSession): void {
|
|
39
|
+
if (caches.has(session)) return;
|
|
40
|
+
const active = new Map<string, ToolProgress>();
|
|
41
|
+
const unsubscribe = session.subscribe(event => {
|
|
42
|
+
if (event.type === "tool_execution_start") {
|
|
43
|
+
active.set(event.toolCallId, { toolCallId: event.toolCallId, toolName: event.toolName, args: event.args, elided: 0 });
|
|
44
|
+
} else if (event.type === "tool_execution_update") {
|
|
45
|
+
const bounded = boundToolResult({ ...event.partialResult, isError: false });
|
|
46
|
+
active.set(event.toolCallId, { toolCallId: event.toolCallId, toolName: event.toolName, args: event.args, ...bounded });
|
|
47
|
+
} else if (event.type === "tool_execution_end") {
|
|
48
|
+
active.delete(event.toolCallId);
|
|
49
|
+
} else if (event.type === "agent_end") {
|
|
50
|
+
active.clear();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
caches.set(session, { active, unsubscribe });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function getToolProgress(session: AgentSession): readonly ToolProgress[] {
|
|
57
|
+
return [...(caches.get(session)?.active.values() ?? [])];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Never called by the viewer: closing a view must not stop collecting the child's progress. */
|
|
61
|
+
export function releaseToolProgress(session: AgentSession): void {
|
|
62
|
+
const cache = caches.get(session);
|
|
63
|
+
if (!cache) return;
|
|
64
|
+
cache.unsubscribe();
|
|
65
|
+
cache.active.clear();
|
|
66
|
+
caches.delete(session);
|
|
67
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* viewer-keys.ts — Scroll key matchers for the conversation viewer.
|
|
3
|
+
*
|
|
4
|
+
* Resolves `tui.select.*` through the user's keybindings when pi provides a
|
|
5
|
+
* manager, falling back to the previous hardcoded keys otherwise. The viewer's
|
|
6
|
+
* k/j and shift+arrow aliases always work alongside whatever is bound.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { type KeyId, matchesKey } from "@earendil-works/pi-tui";
|
|
10
|
+
|
|
11
|
+
/** The `tui.select.*` keybinding ids the viewer resolves. */
|
|
12
|
+
export type ViewerScrollKeybinding =
|
|
13
|
+
| "tui.select.up"
|
|
14
|
+
| "tui.select.down"
|
|
15
|
+
| "tui.select.pageUp"
|
|
16
|
+
| "tui.select.pageDown";
|
|
17
|
+
|
|
18
|
+
/** Structural subset of pi-tui's `KeybindingsManager` (which satisfies it). */
|
|
19
|
+
export interface ViewerKeybindings {
|
|
20
|
+
matches(data: string, keybinding: ViewerScrollKeybinding): boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ViewerKeys {
|
|
24
|
+
scrollUp(data: string): boolean;
|
|
25
|
+
scrollDown(data: string): boolean;
|
|
26
|
+
pageUp(data: string): boolean;
|
|
27
|
+
pageDown(data: string): boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createViewerKeys(keybindings?: ViewerKeybindings): ViewerKeys {
|
|
31
|
+
const matches = (data: string, id: ViewerScrollKeybinding, fallback: KeyId): boolean =>
|
|
32
|
+
keybindings ? keybindings.matches(data, id) : matchesKey(data, fallback);
|
|
33
|
+
return {
|
|
34
|
+
scrollUp: (data) => matches(data, "tui.select.up", "up") || matchesKey(data, "k"),
|
|
35
|
+
scrollDown: (data) => matches(data, "tui.select.down", "down") || matchesKey(data, "j"),
|
|
36
|
+
pageUp: (data) => matches(data, "tui.select.pageUp", "pageUp") || matchesKey(data, "shift+up"),
|
|
37
|
+
pageDown: (data) => matches(data, "tui.select.pageDown", "pageDown") || matchesKey(data, "shift+down"),
|
|
38
|
+
};
|
|
39
|
+
}
|