@giladbarnea/pi-simple-team 1.0.16 → 1.0.17
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/index.ts +78 -73
- package/model-preflight.ts +18 -58
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { StringDecoder } from "node:string_decoder";
|
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import { defineTool, type ContextUsage, type ExtensionAPI, getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { formatContextWindowReport, requireKnownContextUsage, type KnownContextUsage } from "./context-window.ts";
|
|
9
|
-
import { validateTeammateModels } from "./model-preflight.ts";
|
|
9
|
+
import { formatScopedModelGuidance, validateTeammateModels, type ModelReference } from "./model-preflight.ts";
|
|
10
10
|
import { composeSystemPrompt } from "./system-prompt.ts";
|
|
11
11
|
import { readChildRuntimeConfig, registerChildTools } from "./child-tools.ts";
|
|
12
12
|
import { appendTeamLog, filterTeamLog, normalizeChildEvent, nowText, pageTeamLog, preview, renderTeamLogPage, type TeamLogEntry } from "./teamlog.ts";
|
|
@@ -797,12 +797,14 @@ async function handleCallbackRequest(request: http.IncomingMessage, response: ht
|
|
|
797
797
|
}
|
|
798
798
|
}
|
|
799
799
|
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
})
|
|
800
|
+
function teammateSchema(modelGuidance: string) {
|
|
801
|
+
return Type.Object({
|
|
802
|
+
name: Type.String({ description: "Teammate name" }),
|
|
803
|
+
prompt: Type.String({ description: "Individual teammate system prompt" }),
|
|
804
|
+
model: Type.String({ description: `Canonical provider/model id for this teammate. ${modelGuidance}` }),
|
|
805
|
+
thinking: Type.Optional(StringEnum(thinkingLevels, { description: "Thinking level for this teammate. Defaults to xhigh.", default: defaultThinkingLevel })),
|
|
806
|
+
});
|
|
807
|
+
}
|
|
806
808
|
|
|
807
809
|
export default function (pi: ExtensionAPI) {
|
|
808
810
|
const childRuntimeConfig = readChildRuntimeConfig();
|
|
@@ -822,73 +824,76 @@ export default function (pi: ExtensionAPI) {
|
|
|
822
824
|
closeCallbackServerIfUnused();
|
|
823
825
|
});
|
|
824
826
|
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
827
|
+
pi.on("session_start", (_event, context) => {
|
|
828
|
+
const scopedModels = (context as typeof context & { scopedModels?: ReadonlyArray<{ model: ModelReference }> }).scopedModels ?? [];
|
|
829
|
+
const modelGuidance = formatScopedModelGuidance(scopedModels.map(({ model }) => model));
|
|
830
|
+
pi.registerTool(
|
|
831
|
+
defineTool({
|
|
832
|
+
name: "team_spawn",
|
|
833
|
+
label: "Team Spawn",
|
|
834
|
+
description: `Spawn a persistent team of Pi teammates with fresh context windows. If the user is interested, set \`showOnHerdrPanes\` to run each teammate in a visible Herdr pane. The main agent (you) is included automatically; do not specify it as a teammate. ${modelGuidance}`,
|
|
835
|
+
promptSnippet: `Spawn persistent Pi teammates. ${modelGuidance} Unless required, don’t fill up your time by repeatedly busy-polling team information. Don’t bash sleep to wait for progress; instead, set your status to advertise that you are counting on teammates to send you important milestones or requests for help, and that otherwise you are staying idle. Send this actively to the team. Then end your turn by sending a simple message to the user, and finally stay put.`,
|
|
836
|
+
renderShell: "self",
|
|
837
|
+
renderCall: (args, theme, context) => renderTeamToolCall("team_spawn", args, theme, context, sessionTeammateRoster),
|
|
838
|
+
renderResult: (result, options, theme, context) => renderTeamToolResult("team_spawn", result, options, theme, context, undefined, sessionTeammateRoster),
|
|
839
|
+
parameters: Type.Object({
|
|
840
|
+
team: Type.String({ description: "Team name" }),
|
|
841
|
+
teamPrompt: Type.String({ description: "Common team system prompt" }),
|
|
842
|
+
teammates: Type.Array(teammateSchema(modelGuidance), { description: "Teammates to spawn" }),
|
|
843
|
+
showOnHerdrPanes: Type.Optional(Type.Boolean({ default: false })),
|
|
844
|
+
}),
|
|
845
|
+
async execute(_toolCallId, params, _signal, _onUpdate, context) {
|
|
846
|
+
const teamName = compactName(params.team);
|
|
847
|
+
if (teams.has(teamName)) throw new Error(`Team already exists: ${teamName}`);
|
|
848
|
+
const showOnHerdrPanes = Boolean(params.showOnHerdrPanes);
|
|
849
|
+
const herdrTabId = showOnHerdrPanes ? await validateHerdrAvailability() : undefined;
|
|
850
|
+
|
|
851
|
+
const teammateSpecs = params.teammates as TeammateSpec[];
|
|
852
|
+
const teammateNames = teammateSpecs.map((teammate) => compactName(teammate.name));
|
|
853
|
+
const duplicateNames = teammateNames.filter((name, index) => teammateNames.indexOf(name) !== index);
|
|
854
|
+
if (duplicateNames.length > 0) throw new Error(`Duplicate teammate name(s): ${[...new Set(duplicateNames)].join(", ")}`);
|
|
855
|
+
if (teammateNames.includes("main")) throw new Error('"main" is reserved');
|
|
856
|
+
|
|
857
|
+
validateTeammateModels(teammateSpecs, context.modelRegistry.getAvailable());
|
|
858
|
+
sessionTeammateRoster.push(...teammateNames.filter((teammateName) => !sessionTeammateRoster.includes(teammateName)));
|
|
859
|
+
await ensureCallbackServer();
|
|
860
|
+
|
|
861
|
+
const team: TeamState = {
|
|
862
|
+
owner,
|
|
863
|
+
ownerPi: pi,
|
|
864
|
+
name: teamName,
|
|
865
|
+
showOnHerdrPanes,
|
|
866
|
+
teamPrompt: params.teamPrompt,
|
|
867
|
+
members: new Map(),
|
|
868
|
+
statuses: new Map([["main", status("available", "Main agent")]]),
|
|
869
|
+
created: nowText(),
|
|
870
|
+
log: [],
|
|
871
|
+
nextLogSequence: 1,
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
teams.set(teamName, team);
|
|
875
|
+
try {
|
|
876
|
+
for (const teammateSpec of teammateSpecs) {
|
|
877
|
+
const teammate = createTeammateState(team, teammateSpec);
|
|
878
|
+
team.members.set(teammate.name, teammate);
|
|
879
|
+
team.statuses.set(teammate.name, status("idle", "Spawned"));
|
|
880
|
+
await startTeammate(team, teammate, teammateNames, herdrTabId);
|
|
881
|
+
}
|
|
882
|
+
} catch (error) {
|
|
883
|
+
await shutdownTeam(team);
|
|
884
|
+
closeCallbackServerIfUnused();
|
|
885
|
+
throw error;
|
|
877
886
|
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
});
|
|
889
|
-
},
|
|
890
|
-
}),
|
|
891
|
-
);
|
|
887
|
+
return toolResult({
|
|
888
|
+
accepted: true,
|
|
889
|
+
team: team.name,
|
|
890
|
+
teammates: [...team.members.keys()],
|
|
891
|
+
status: formatStatus(team),
|
|
892
|
+
});
|
|
893
|
+
},
|
|
894
|
+
}),
|
|
895
|
+
);
|
|
896
|
+
});
|
|
892
897
|
|
|
893
898
|
pi.registerTool(
|
|
894
899
|
defineTool({
|
package/model-preflight.ts
CHANGED
|
@@ -1,72 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
export interface ModelReference {
|
|
2
|
+
provider: string;
|
|
3
|
+
id: string;
|
|
4
|
+
}
|
|
2
5
|
|
|
3
6
|
export interface TeammateModelSpec {
|
|
4
7
|
name: string;
|
|
5
8
|
model: string;
|
|
6
9
|
}
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const listModelsTimeoutMilliseconds = 15_000;
|
|
12
|
-
|
|
13
|
-
export function stripThinkingSuffix(model: string): string {
|
|
14
|
-
const suffixSeparatorIndex = model.lastIndexOf(":");
|
|
15
|
-
if (suffixSeparatorIndex === -1) return model;
|
|
16
|
-
|
|
17
|
-
const suffix = model.slice(suffixSeparatorIndex + 1);
|
|
18
|
-
if (!thinkingSuffixes.includes(suffix as (typeof thinkingSuffixes)[number])) return model;
|
|
19
|
-
|
|
20
|
-
return model.slice(0, suffixSeparatorIndex);
|
|
11
|
+
/** @example formatModelReference({ provider: "anthropic", id: "claude-sonnet-4-6" }) // "anthropic/claude-sonnet-4-6" */
|
|
12
|
+
export function formatModelReference(model: ModelReference): string {
|
|
13
|
+
return `${model.provider}/${model.id}`;
|
|
21
14
|
}
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.
|
|
28
|
-
.
|
|
29
|
-
.filter(Boolean);
|
|
30
|
-
const headerIndex = lines.findIndex((line) => line.startsWith("provider ") && line.includes(" model"));
|
|
31
|
-
if (headerIndex === -1) return false;
|
|
32
|
-
|
|
33
|
-
return lines.slice(headerIndex + 1).some((line) => {
|
|
34
|
-
if (line.startsWith("No models matching ")) return false;
|
|
35
|
-
const columns = line.split(/\s+/);
|
|
36
|
-
return columns.length >= 2 && columns[0] !== "provider" && columns[1] !== "model";
|
|
37
|
-
});
|
|
16
|
+
/** @example formatScopedModelGuidance([{ provider: "anthropic", id: "claude-sonnet-4-6" }]) */
|
|
17
|
+
export function formatScopedModelGuidance(scopedModels: readonly ModelReference[]): string {
|
|
18
|
+
const modelReferences = scopedModels.map(formatModelReference);
|
|
19
|
+
return modelReferences.length > 0
|
|
20
|
+
? `You should probably use one of these user-scoped models: ${modelReferences.join(", ")}.`
|
|
21
|
+
: "The user has not defined a list of preferred models explicitly. Figure out which model _you_ are by reading the value of the PI_PROVIDER, PI_MODEL, and PI_REASONING_LEVEL environment variables. That should give you something to start with. Confirm with the user before picking any model id.";
|
|
38
22
|
}
|
|
39
23
|
|
|
40
|
-
export
|
|
24
|
+
export function validateTeammateModels(teammates: readonly TeammateModelSpec[], availableModels: readonly ModelReference[]): void {
|
|
25
|
+
const availableModelReferences = new Set(availableModels.map(formatModelReference));
|
|
26
|
+
const errors = teammates
|
|
27
|
+
.filter((teammate) => !availableModelReferences.has(teammate.model))
|
|
28
|
+
.map((teammate) => `Model ${JSON.stringify(teammate.model)} for teammate ${JSON.stringify(teammate.name)} is not available.`);
|
|
41
29
|
|
|
42
|
-
const runPiListModels: RunListModels = (modelPattern) => {
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
childProcess.execFile("pi", ["--list-models", modelPattern], { timeout: listModelsTimeoutMilliseconds, maxBuffer: 1024 * 1024 }, (error, stdout, stderr) => {
|
|
45
|
-
const output = `${stdout}${stderr}`;
|
|
46
|
-
if (!error) {
|
|
47
|
-
resolve(output);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const details = output.trim() ? `\n${output.trim()}` : "";
|
|
52
|
-
reject(new Error(`pi --list-models ${JSON.stringify(modelPattern)} failed: ${error.message}${details}`));
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
async function validateTeammateModel(teammate: TeammateModelSpec, runListModels: RunListModels): Promise<void> {
|
|
58
|
-
const modelPattern = stripThinkingSuffix(teammate.model);
|
|
59
|
-
const output = await runListModels(modelPattern);
|
|
60
|
-
if (parseListModelsOutput(output)) return;
|
|
61
|
-
|
|
62
|
-
const details = output.trim() || "(no output)";
|
|
63
|
-
throw new Error(`No configured model matched ${JSON.stringify(teammate.model)} for teammate ${JSON.stringify(teammate.name)}. Checked ${JSON.stringify(modelPattern)} with pi --list-models:\n${details}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export async function validateTeammateModels(teammates: TeammateModelSpec[], runListModels: RunListModels = runPiListModels): Promise<void> {
|
|
67
|
-
const results = await Promise.allSettled(teammates.map((teammate) => validateTeammateModel(teammate, runListModels)));
|
|
68
|
-
const errors = results.flatMap((result) => (result.status === "rejected" ? [result.reason instanceof Error ? result.reason.message : String(result.reason)] : []));
|
|
69
30
|
if (errors.length === 0) return;
|
|
70
|
-
|
|
71
31
|
throw new Error(`Model preflight failed:\n${errors.map((error) => `- ${error}`).join("\n")}`);
|
|
72
32
|
}
|