Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.1 kB · 34 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435/** * Agent Message Context Usage Extension * * Automatically appends the agent's current context usage to messages sent * to its parent, so the orchestrator/parent is always aware of subagent context load. * * Usage: * Copy to ~/.prime/agent/extensions/agent-message-context-usage.ts * or run with `prime-agent -e ./examples/extensions/agent-message-context-usage.ts` */import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
export default function (pi: ExtensionAPI) { pi.on("before_agent_message_send", async (event, ctx) => { // Only attach context usage when sending to the parent if (event.receiverRole !== "parent") { return; }
const usage = ctx.getContextUsage(); if (!usage) { return; }
const tokensStr = usage.tokens !== null ? usage.tokens.toLocaleString() : "unknown"; const windowStr = usage.contextWindow.toLocaleString(); const percentStr = usage.percent !== null ? `${usage.percent.toFixed(1)}%` : "unknown";
const footer = `\n\n[Context usage: ${tokensStr} / ${windowStr} tokens (${percentStr})]`; return { message: event.message + footer, }; });}