@piotr-oles/pi-subagents 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -0
- package/package.json +49 -0
- package/src/commands/components/subagent-template-details-component.ts +153 -0
- package/src/commands/components/subagent-templates-component.ts +93 -0
- package/src/commands/components/subagent-templates-list-component.ts +118 -0
- package/src/commands/subagent-templates-menu.ts +27 -0
- package/src/domain/instance/done-subagent.ts +101 -0
- package/src/domain/instance/queued-subagent.ts +87 -0
- package/src/domain/instance/running-subagent.ts +224 -0
- package/src/domain/subagent-config.ts +13 -0
- package/src/domain/subagent-instances-manager.ts +248 -0
- package/src/domain/subagent-resource-loader.ts +53 -0
- package/src/domain/subagent-store.ts +208 -0
- package/src/domain/subagent-template.ts +30 -0
- package/src/domain/subagent-templates-manager.ts +183 -0
- package/src/domain/types.ts +26 -0
- package/src/flags.ts +14 -0
- package/src/format.ts +48 -0
- package/src/index.ts +76 -0
- package/src/test-helpers/scripted-session-builder.ts +62 -0
- package/src/test-helpers/scripted-session.ts +95 -0
- package/src/test-helpers.ts +184 -0
- package/src/throttle.ts +14 -0
- package/src/tools/components/inline-container.ts +55 -0
- package/src/tools/components/subagent-tool-call-component.ts +37 -0
- package/src/tools/components/subagent-tool-result-component.ts +362 -0
- package/src/tools/subagent-tool.ts +152 -0
- package/src/tools/types.ts +19 -0
- package/src/xml.ts +12 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { AgentSessionEvent, ContextUsage } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export interface TurnStep {
|
|
4
|
+
readonly kind: "turn";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface EventStep {
|
|
8
|
+
readonly kind: "event";
|
|
9
|
+
readonly eventType: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SideEffectStep {
|
|
13
|
+
readonly kind: "side-effect";
|
|
14
|
+
readonly run: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type Step = TurnStep | EventStep | SideEffectStep;
|
|
18
|
+
|
|
19
|
+
export type Completion =
|
|
20
|
+
| { readonly kind: "complete"; readonly text: string }
|
|
21
|
+
| { readonly kind: "error"; readonly message: string };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Controllable session for scenario-based testing of RunningAgentInstance.
|
|
25
|
+
* Produced by ScriptedSessionBuilder — do not construct directly.
|
|
26
|
+
*
|
|
27
|
+
* Build a script of turns and events via ScriptedSessionBuilder, then call build().
|
|
28
|
+
* When prompt() is called by the running instance the script replays in order:
|
|
29
|
+
* - each turn() fires a turn_end event and awaits the subscriber
|
|
30
|
+
* - each event(type) fires that event type and awaits the subscriber
|
|
31
|
+
* - after all steps the session resolves or throws based on complete()/fail()
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
export class ScriptedSession {
|
|
35
|
+
private handler: ((event: AgentSessionEvent) => void | Promise<void>) | null = null;
|
|
36
|
+
private aborted = false;
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
private readonly steps: readonly Step[],
|
|
40
|
+
private readonly completion: Completion,
|
|
41
|
+
private readonly steeredMessages: string[],
|
|
42
|
+
) {}
|
|
43
|
+
|
|
44
|
+
readonly sessionId = "scripted";
|
|
45
|
+
|
|
46
|
+
subscribe(listener: (event: AgentSessionEvent) => void | Promise<void>): () => void {
|
|
47
|
+
this.handler = listener;
|
|
48
|
+
return () => {
|
|
49
|
+
this.handler = null;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async prompt(_text: string): Promise<void> {
|
|
54
|
+
for (const step of this.steps) {
|
|
55
|
+
if (this.aborted) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (step.kind === "turn") {
|
|
60
|
+
await this.handler?.({
|
|
61
|
+
type: "turn_end",
|
|
62
|
+
message: { role: "assistant", content: [] },
|
|
63
|
+
} as unknown as AgentSessionEvent);
|
|
64
|
+
} else if (step.kind === "event") {
|
|
65
|
+
await this.handler?.({ type: step.eventType } as AgentSessionEvent);
|
|
66
|
+
} else {
|
|
67
|
+
step.run();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.aborted) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (this.completion.kind === "error") {
|
|
76
|
+
throw new Error(this.completion.message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async steer(message: string): Promise<void> {
|
|
81
|
+
this.steeredMessages.push(message);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async abort(): Promise<void> {
|
|
85
|
+
this.aborted = true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getLastAssistantText(): string {
|
|
89
|
+
return this.completion.kind === "complete" ? this.completion.text : "";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
getContextUsage(): ContextUsage | undefined {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { type AgentSession, Theme, type ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { TUI } from "@earendil-works/pi-tui";
|
|
3
|
+
import { type DoneResult, DoneSubagent } from "./domain/instance/done-subagent.js";
|
|
4
|
+
import { QueuedSubagent } from "./domain/instance/queued-subagent.js";
|
|
5
|
+
import { RunningSubagent } from "./domain/instance/running-subagent.js";
|
|
6
|
+
import type { SubagentConfig } from "./domain/subagent-config.js";
|
|
7
|
+
import type { SubagentTemplate } from "./domain/subagent-template.js";
|
|
8
|
+
|
|
9
|
+
const FG_COLORS: Record<ThemeColor, string> = {
|
|
10
|
+
accent: "",
|
|
11
|
+
border: "",
|
|
12
|
+
borderAccent: "",
|
|
13
|
+
borderMuted: "",
|
|
14
|
+
success: "",
|
|
15
|
+
error: "",
|
|
16
|
+
warning: "",
|
|
17
|
+
muted: "",
|
|
18
|
+
dim: "",
|
|
19
|
+
text: "",
|
|
20
|
+
thinkingText: "",
|
|
21
|
+
userMessageText: "",
|
|
22
|
+
customMessageText: "",
|
|
23
|
+
customMessageLabel: "",
|
|
24
|
+
toolTitle: "",
|
|
25
|
+
toolOutput: "",
|
|
26
|
+
mdHeading: "",
|
|
27
|
+
mdLink: "",
|
|
28
|
+
mdLinkUrl: "",
|
|
29
|
+
mdCode: "",
|
|
30
|
+
mdCodeBlock: "",
|
|
31
|
+
mdCodeBlockBorder: "",
|
|
32
|
+
mdQuote: "",
|
|
33
|
+
mdQuoteBorder: "",
|
|
34
|
+
mdHr: "",
|
|
35
|
+
mdListBullet: "",
|
|
36
|
+
toolDiffAdded: "",
|
|
37
|
+
toolDiffRemoved: "",
|
|
38
|
+
toolDiffContext: "",
|
|
39
|
+
syntaxComment: "",
|
|
40
|
+
syntaxKeyword: "",
|
|
41
|
+
syntaxFunction: "",
|
|
42
|
+
syntaxVariable: "",
|
|
43
|
+
syntaxString: "",
|
|
44
|
+
syntaxNumber: "",
|
|
45
|
+
syntaxType: "",
|
|
46
|
+
syntaxOperator: "",
|
|
47
|
+
syntaxPunctuation: "",
|
|
48
|
+
thinkingOff: "",
|
|
49
|
+
thinkingMinimal: "",
|
|
50
|
+
thinkingLow: "",
|
|
51
|
+
thinkingMedium: "",
|
|
52
|
+
thinkingHigh: "",
|
|
53
|
+
thinkingXhigh: "",
|
|
54
|
+
bashMode: "",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const BG_COLORS = {
|
|
58
|
+
selectedBg: "",
|
|
59
|
+
userMessageBg: "",
|
|
60
|
+
customMessageBg: "",
|
|
61
|
+
toolPendingBg: "",
|
|
62
|
+
toolSuccessBg: "",
|
|
63
|
+
toolErrorBg: "",
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const mockTheme = new Theme(FG_COLORS, BG_COLORS, "truecolor");
|
|
67
|
+
export const mockTui = { requestRender: () => {} } as unknown as TUI;
|
|
68
|
+
|
|
69
|
+
export const mockSession: AgentSession = {
|
|
70
|
+
sessionId: "mock",
|
|
71
|
+
steer: async () => {},
|
|
72
|
+
abort: async () => {},
|
|
73
|
+
prompt: async () => {},
|
|
74
|
+
subscribe: () => () => {},
|
|
75
|
+
getLastAssistantText: () => undefined,
|
|
76
|
+
getContextUsage: () => undefined,
|
|
77
|
+
} as unknown as AgentSession;
|
|
78
|
+
|
|
79
|
+
export function makeAgentTemplate(overrides: Partial<SubagentTemplate> = {}): SubagentTemplate {
|
|
80
|
+
return {
|
|
81
|
+
name: "my-agent",
|
|
82
|
+
description: "",
|
|
83
|
+
instructions: "",
|
|
84
|
+
source: "global",
|
|
85
|
+
includedTools: undefined,
|
|
86
|
+
includedSkills: undefined,
|
|
87
|
+
enabled: true,
|
|
88
|
+
filePath: undefined,
|
|
89
|
+
...overrides,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function makeAgentConfig(overrides: Partial<SubagentConfig> = {}): SubagentConfig {
|
|
94
|
+
return {
|
|
95
|
+
name: "my-agent",
|
|
96
|
+
model: "claude-haiku",
|
|
97
|
+
thinkingLevel: "medium",
|
|
98
|
+
graceTurns: 5,
|
|
99
|
+
template: makeAgentTemplate(),
|
|
100
|
+
includedTools: [],
|
|
101
|
+
includedSubagents: [],
|
|
102
|
+
...overrides,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export const mockTemplate: SubagentTemplate = makeAgentTemplate();
|
|
107
|
+
export const mockConfig: SubagentConfig = makeAgentConfig();
|
|
108
|
+
|
|
109
|
+
export interface MakeQueuedOptions {
|
|
110
|
+
id?: string;
|
|
111
|
+
prompt?: string;
|
|
112
|
+
description?: string;
|
|
113
|
+
config?: SubagentConfig;
|
|
114
|
+
session?: AgentSession;
|
|
115
|
+
maxTurns?: number;
|
|
116
|
+
graceTurns?: number;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function makeQueued({
|
|
120
|
+
id = "test-id",
|
|
121
|
+
prompt = "do something",
|
|
122
|
+
description = "doing a task",
|
|
123
|
+
config,
|
|
124
|
+
session = mockSession,
|
|
125
|
+
maxTurns,
|
|
126
|
+
graceTurns,
|
|
127
|
+
}: MakeQueuedOptions = {}): QueuedSubagent {
|
|
128
|
+
return new QueuedSubagent({
|
|
129
|
+
id,
|
|
130
|
+
prompt,
|
|
131
|
+
description,
|
|
132
|
+
config:
|
|
133
|
+
config ??
|
|
134
|
+
makeAgentConfig({
|
|
135
|
+
template: makeAgentTemplate({ maxTurns, graceTurns }),
|
|
136
|
+
...(maxTurns !== undefined && { maxTurns }),
|
|
137
|
+
...(graceTurns !== undefined && { graceTurns }),
|
|
138
|
+
}),
|
|
139
|
+
session,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface MakeRunningOptions {
|
|
144
|
+
id?: string;
|
|
145
|
+
prompt?: string;
|
|
146
|
+
config?: SubagentConfig;
|
|
147
|
+
session?: AgentSession;
|
|
148
|
+
startedAt?: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function makeRunning({
|
|
152
|
+
id = "test-id",
|
|
153
|
+
prompt = "do something",
|
|
154
|
+
config,
|
|
155
|
+
session = mockSession,
|
|
156
|
+
startedAt = 0,
|
|
157
|
+
}: MakeRunningOptions = {}): RunningSubagent {
|
|
158
|
+
return RunningSubagent.start({
|
|
159
|
+
instance: makeQueued({ id, config, session }),
|
|
160
|
+
prompt,
|
|
161
|
+
description: "doing a task",
|
|
162
|
+
startedAt,
|
|
163
|
+
onUpdate: () => {},
|
|
164
|
+
onDone: () => {},
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface MakeDoneOptions {
|
|
169
|
+
id?: string;
|
|
170
|
+
config?: SubagentConfig;
|
|
171
|
+
session?: AgentSession;
|
|
172
|
+
result?: DoneResult;
|
|
173
|
+
startedAt?: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function makeDone({
|
|
177
|
+
id = "test-id",
|
|
178
|
+
config,
|
|
179
|
+
session = mockSession,
|
|
180
|
+
result = { status: "completed", message: "", steered: false },
|
|
181
|
+
startedAt = 0,
|
|
182
|
+
}: MakeDoneOptions = {}) {
|
|
183
|
+
return new DoneSubagent({ instance: makeRunning({ id, config, session, startedAt }), result });
|
|
184
|
+
}
|
package/src/throttle.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function throttle<T extends unknown[]>(
|
|
2
|
+
fn: (...args: T) => void,
|
|
3
|
+
ms: number,
|
|
4
|
+
): (...args: T) => void {
|
|
5
|
+
let last = 0;
|
|
6
|
+
|
|
7
|
+
return (...args: T): void => {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
if (now - last >= ms) {
|
|
10
|
+
last = now;
|
|
11
|
+
fn(...args);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import { Container, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
export class InlineContainer implements Component {
|
|
5
|
+
private readonly container: Container;
|
|
6
|
+
private readonly gap: string;
|
|
7
|
+
|
|
8
|
+
constructor(gap: string = " ") {
|
|
9
|
+
this.container = new Container();
|
|
10
|
+
this.gap = gap;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
clear() {
|
|
14
|
+
this.container.clear();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
addChild(component: Component) {
|
|
18
|
+
this.container.addChild(component);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
removeChild(component: Component) {
|
|
22
|
+
this.container.removeChild(component);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
render(width: number): string[] {
|
|
26
|
+
const parts: string[] = [];
|
|
27
|
+
let remainingWidth = width - 1; // add 1 for padding, so background color doesn't get trimmed
|
|
28
|
+
for (let index = 0; index < this.container.children.length; ++index) {
|
|
29
|
+
const child = this.container.children[index];
|
|
30
|
+
const rendered = child.render(remainingWidth);
|
|
31
|
+
if (rendered.length === 0) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const part = rendered[0].trim();
|
|
35
|
+
if (rendered.length > 1 || part.includes("\n")) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Cannot render multi-line component in InlineContainer, index=${index}, rendered=${JSON.stringify(rendered)}.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
parts.push(part);
|
|
41
|
+
remainingWidth -= visibleWidth(part + this.gap);
|
|
42
|
+
if (remainingWidth <= 0) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const text = parts.join(this.gap);
|
|
48
|
+
|
|
49
|
+
return [truncateToWidth(text, width, "", true)];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
invalidate() {
|
|
53
|
+
this.container.invalidate();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type Component, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { SubagentToolParamsType } from "../types.js";
|
|
4
|
+
|
|
5
|
+
export class SubagentToolCallComponent implements Component {
|
|
6
|
+
constructor(
|
|
7
|
+
private params: Partial<SubagentToolParamsType>,
|
|
8
|
+
private theme: Theme,
|
|
9
|
+
private expanded: boolean,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
update(params: Partial<SubagentToolParamsType>, theme: Theme, expanded: boolean): void {
|
|
13
|
+
this.params = params;
|
|
14
|
+
this.theme = theme;
|
|
15
|
+
this.expanded = expanded;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
render(width: number): string[] {
|
|
19
|
+
const { theme, expanded, params } = this;
|
|
20
|
+
const toolTitle = theme.fg("toolTitle", "subagent");
|
|
21
|
+
const id = params.id ? theme.fg("syntaxNumber", `#${params.id}`) : undefined;
|
|
22
|
+
const name = params.name ? theme.fg("accent", params.name) : undefined;
|
|
23
|
+
const description = params.description ? theme.fg("dim", params.description) : undefined;
|
|
24
|
+
const header = truncateToWidth(
|
|
25
|
+
[toolTitle, id, name, description].filter(Boolean).join(" "),
|
|
26
|
+
width,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
if (expanded && params.prompt) {
|
|
30
|
+
const prompt = new Text(theme.fg("dim", params.prompt.trim()), 0, 1);
|
|
31
|
+
return [header, ...prompt.render(width)];
|
|
32
|
+
}
|
|
33
|
+
return [header];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
invalidate() {}
|
|
37
|
+
}
|