@giladbarnea/pi-simple-team 1.0.13 → 1.0.14
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/child-tools.ts +20 -0
- package/context-window.ts +19 -0
- package/index.ts +56 -1
- package/package.json +1 -1
package/child-tools.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as http from "node:http";
|
|
2
2
|
import { defineTool, getMarkdownTheme, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { Type } from "typebox";
|
|
4
|
+
import { formatContextWindowReport, requireKnownContextUsage } from "./context-window.ts";
|
|
4
5
|
import { renderTeamMessage } from "./render.ts";
|
|
5
6
|
|
|
6
7
|
type JsonRecord = Record<string, unknown>;
|
|
@@ -178,6 +179,11 @@ function startVisibleChild(pi: ExtensionAPI, config: ChildRuntimeConfig): void {
|
|
|
178
179
|
return;
|
|
179
180
|
}
|
|
180
181
|
|
|
182
|
+
if (body.tool === "report_context_window") {
|
|
183
|
+
writeJson(response, 200, { contextUsage: requireKnownContextUsage(context.getContextUsage()) });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
181
187
|
if (body.tool !== "deliver") {
|
|
182
188
|
writeJson(response, 400, { error: `unknown tool: ${String(body.tool)}` });
|
|
183
189
|
return;
|
|
@@ -244,6 +250,20 @@ export function registerChildTools(pi: ExtensionAPI, config: ChildRuntimeConfig)
|
|
|
244
250
|
pi.registerMessageRenderer(teamMessageType, (message, _options, theme) => renderTeamMessage(message, theme, getMarkdownTheme(), config.participants));
|
|
245
251
|
if (config.visible) startVisibleChild(pi, config);
|
|
246
252
|
|
|
253
|
+
pi.registerTool(
|
|
254
|
+
defineTool({
|
|
255
|
+
name: "report_context_window",
|
|
256
|
+
label: "Report Context Window",
|
|
257
|
+
description: "Report your current context-window use.",
|
|
258
|
+
promptSnippet: "Report your current context-window use",
|
|
259
|
+
parameters: Type.Object({}),
|
|
260
|
+
async execute(_toolCallId, _params, _signal, _onUpdate, context) {
|
|
261
|
+
const text = formatContextWindowReport("You have", requireKnownContextUsage(context.getContextUsage()));
|
|
262
|
+
return { content: [{ type: "text" as const, text }], details: {} };
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
|
|
247
267
|
pi.registerTool(
|
|
248
268
|
defineTool({
|
|
249
269
|
name: "teamsend",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ContextUsage } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export interface KnownContextUsage {
|
|
4
|
+
tokens: number;
|
|
5
|
+
contextWindow: number;
|
|
6
|
+
percent: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function requireKnownContextUsage(usage: ContextUsage | undefined): KnownContextUsage {
|
|
10
|
+
if (!usage || usage.tokens === null || usage.percent === null) throw new Error("Context usage is unavailable");
|
|
11
|
+
return { tokens: usage.tokens, contextWindow: usage.contextWindow, percent: usage.percent };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** @example formatContextWindowReport("You have", { tokens: 87_000, contextWindow: 272_000, percent: 32 }) */
|
|
15
|
+
export function formatContextWindowReport(subjectWithVerb: string, usage: KnownContextUsage): string {
|
|
16
|
+
const tokens = `${Math.round(usage.tokens / 1_000)}k`;
|
|
17
|
+
const contextWindow = `${Math.round(usage.contextWindow / 1_000)}k`;
|
|
18
|
+
return `${subjectWithVerb} used ${tokens} tokens out of ${contextWindow} available (${Math.round(usage.percent)}%).`;
|
|
19
|
+
}
|
package/index.ts
CHANGED
|
@@ -4,7 +4,8 @@ import crypto from "node:crypto";
|
|
|
4
4
|
import http from "node:http";
|
|
5
5
|
import { StringDecoder } from "node:string_decoder";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { defineTool, type ExtensionAPI, getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { defineTool, type ContextUsage, type ExtensionAPI, getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { formatContextWindowReport, requireKnownContextUsage, type KnownContextUsage } from "./context-window.ts";
|
|
8
9
|
import { validateTeammateModels } from "./model-preflight.ts";
|
|
9
10
|
import { composeSystemPrompt } from "./system-prompt.ts";
|
|
10
11
|
import { readChildRuntimeConfig, registerChildTools } from "./child-tools.ts";
|
|
@@ -408,6 +409,40 @@ function resolveRecipients(team: TeamState, recipientNames: string[]): TeammateS
|
|
|
408
409
|
return recipients as TeammateState[];
|
|
409
410
|
}
|
|
410
411
|
|
|
412
|
+
function resolveContextTargets(owner: symbol, targetNames: string[]): TeammateState[] {
|
|
413
|
+
const ownedTeams = [...teams.values()].filter((team) => team.owner === owner);
|
|
414
|
+
const names = targetNames.map(compactName).filter((name) => name !== "main");
|
|
415
|
+
return names.map((name) => {
|
|
416
|
+
const matches = ownedTeams.flatMap((team) => {
|
|
417
|
+
const teammate = team.members.get(name);
|
|
418
|
+
return teammate ? [teammate] : [];
|
|
419
|
+
});
|
|
420
|
+
if (matches.length === 0) throw new Error(`Unknown teammate: ${name}`);
|
|
421
|
+
if (matches.length > 1) throw new Error(`Ambiguous teammate across teams: ${name}`);
|
|
422
|
+
return matches[0];
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
async function getTeammateContextUsage(teammate: TeammateState, signal?: AbortSignal): Promise<KnownContextUsage> {
|
|
427
|
+
if (teammate.transport === "rpc") {
|
|
428
|
+
const response = await sendRpc(teammate, { type: "get_session_stats" });
|
|
429
|
+
await requireSuccessfulResponse(response, `read context usage from ${teammate.name}`);
|
|
430
|
+
const stats = response.data as { contextUsage?: ContextUsage };
|
|
431
|
+
return requireKnownContextUsage(stats.contextUsage);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!teammate.alive || !teammate.visibleUrl) throw new Error(`Visible teammate ${teammate.name} is not ready`);
|
|
435
|
+
const response = await fetch(teammate.visibleUrl, {
|
|
436
|
+
method: "POST",
|
|
437
|
+
headers: { "content-type": "application/json" },
|
|
438
|
+
body: JSON.stringify({ token: callbackToken, tool: "report_context_window", args: {} }),
|
|
439
|
+
signal,
|
|
440
|
+
});
|
|
441
|
+
if (!response.ok) throw new Error(`Visible teammate ${teammate.name} rejected context-window query: ${response.status} ${await response.text()}`);
|
|
442
|
+
const payload = (await response.json()) as { contextUsage?: ContextUsage };
|
|
443
|
+
return requireKnownContextUsage(payload.contextUsage);
|
|
444
|
+
}
|
|
445
|
+
|
|
411
446
|
function formatStatus(team: TeamState): Record<string, TeamStatus> {
|
|
412
447
|
return Object.fromEntries([...team.statuses.entries()].sort(([a], [b]) => a.localeCompare(b)));
|
|
413
448
|
}
|
|
@@ -907,6 +942,26 @@ export default function (pi: ExtensionAPI) {
|
|
|
907
942
|
}),
|
|
908
943
|
);
|
|
909
944
|
|
|
945
|
+
pi.registerTool(
|
|
946
|
+
defineTool({
|
|
947
|
+
name: "report_context_window",
|
|
948
|
+
label: "Report Context Window",
|
|
949
|
+
description: "Report context-window use for selected teammates and main. Main's report is always last.",
|
|
950
|
+
promptSnippet: "Report context-window use for selected teammates and main",
|
|
951
|
+
parameters: Type.Object({
|
|
952
|
+
targets: Type.Array(Type.String({ minLength: 1 }), { description: "Teammate names. Use an empty list to report only main." }),
|
|
953
|
+
}),
|
|
954
|
+
async execute(_toolCallId, params, signal, _onUpdate, context) {
|
|
955
|
+
const teammates = resolveContextTargets(owner, params.targets);
|
|
956
|
+
const reports = await Promise.all(
|
|
957
|
+
teammates.map(async (teammate) => formatContextWindowReport(`Teammate ${teammate.name} has`, await getTeammateContextUsage(teammate, signal))),
|
|
958
|
+
);
|
|
959
|
+
reports.push(formatContextWindowReport("You have", requireKnownContextUsage(context.getContextUsage())));
|
|
960
|
+
return { content: [{ type: "text" as const, text: reports.join("\n") }], details: {} };
|
|
961
|
+
},
|
|
962
|
+
}),
|
|
963
|
+
);
|
|
964
|
+
|
|
910
965
|
// TODO: main could use more automatic meta/discoverability information in the return payload. To help orient around what has been read already, what hasn't been read, inside and outside the filtered space, how long is the log, etc.
|
|
911
966
|
pi.registerTool(
|
|
912
967
|
defineTool({
|