778
Pi
pi-extensions / files
pi-extensions / extensions / adversary / index.ts
7.1 KB
1import { readFile } from "node:fs/promises"; 2import path from "node:path"; 3import { fileURLToPath } from "node:url"; 4import type { AssistantMessage, Usage } from "@earendil-works/pi-ai"; 5import { 6 type ExtensionAPI, 7 getPackageDir, 8 parseFrontmatter, 9 RpcClient, 10} from "@earendil-works/pi-coding-agent"; 11import { Type } from "typebox"; 12 13const TIMEOUT_MS = 30 * 60 * 1_000; 14const THINKING_LEVELS = [ 15 "off", 16 "minimal", 17 "low", 18 "medium", 19 "high", 20 "xhigh", 21 "max", 22] as const; 23 24type ThinkingLevel = (typeof THINKING_LEVELS)[number]; 25type ProgressUpdate = (text: string, details: Record<string, unknown>) => void; 26 27interface AdversaryConfig { 28 model: string; 29 thinking: ThinkingLevel; 30 systemPrompt: string; 31} 32 33const extensionPath = fileURLToPath(import.meta.url); 34const promptPath = path.join(path.dirname(extensionPath), "adversary.md"); 35 36const AdversaryParams = Type.Object({ 37 task: Type.String({ 38 description: 39 "Review brief containing the intended behavior, scope, constraints, and relevant context from the main conversation", 40 }), 41}); 42 43async function loadAdversaryConfig(): Promise<AdversaryConfig> { 44 const content = await readFile(promptPath, "utf8"); 45 const { frontmatter, body } = 46 parseFrontmatter<Record<string, unknown>>(content); 47 const model = 48 typeof frontmatter.model === "string" ? frontmatter.model.trim() : ""; 49 const thinking = frontmatter.thinking; 50 51 if (!model) { 52 throw new Error( 53 `${promptPath} must declare a non-empty "model" in its frontmatter`, 54 ); 55 } 56 if ( 57 typeof thinking !== "string" || 58 !THINKING_LEVELS.includes(thinking as ThinkingLevel) 59 ) { 60 throw new Error( 61 `${promptPath} must declare "thinking" as one of: ${THINKING_LEVELS.join(", ")}`, 62 ); 63 } 64 if (!body.trim()) { 65 throw new Error( 66 `${promptPath} must contain reviewer instructions after its frontmatter`, 67 ); 68 } 69 70 return { 71 model, 72 thinking: thinking as ThinkingLevel, 73 systemPrompt: body.trim(), 74 }; 75} 76 77function compactAction(value: unknown, maxLength = 120): string { 78 const text = 79 typeof value === "string" 80 ? value.replace(/\s+/g, " ").trim() 81 : JSON.stringify(value); 82 if (!text) return ""; 83 return text.length > maxLength ? `${text.slice(0, maxLength - 1)}…` : text; 84} 85 86function describeToolAction(toolName: string, args: unknown): string { 87 if (args && typeof args === "object") { 88 const input = args as Record<string, unknown>; 89 if (toolName === "bash" && typeof input.command === "string") { 90 return `${toolName}: ${compactAction(input.command)}`; 91 } 92 if (toolName === "read" && typeof input.path === "string") { 93 return `${toolName}: ${compactAction(input.path)}`; 94 } 95 } 96 const detail = compactAction(args); 97 return detail && detail !== "{}" ? `${toolName}: ${detail}` : toolName; 98} 99 100function emptyUsage(): Usage { 101 return { 102 input: 0, 103 output: 0, 104 cacheRead: 0, 105 cacheWrite: 0, 106 totalTokens: 0, 107 cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, 108 }; 109} 110 111function addUsage(total: Usage, next: Usage): void { 112 total.input += next.input; 113 total.output += next.output; 114 total.cacheRead += next.cacheRead; 115 total.cacheWrite += next.cacheWrite; 116 total.totalTokens += next.totalTokens; 117 total.cost.input += next.cost.input; 118 total.cost.output += next.cost.output; 119 total.cost.cacheRead += next.cost.cacheRead; 120 total.cost.cacheWrite += next.cost.cacheWrite; 121 total.cost.total += next.cost.total; 122 if (next.cacheWrite1h !== undefined) { 123 total.cacheWrite1h = (total.cacheWrite1h ?? 0) + next.cacheWrite1h; 124 } 125 if (next.reasoning !== undefined) { 126 total.reasoning = (total.reasoning ?? 0) + next.reasoning; 127 } 128} 129 130function finalText(messages: AssistantMessage[]): string { 131 for (let index = messages.length - 1; index >= 0; index--) { 132 const text = messages[index]?.content 133 .filter( 134 (part): part is { type: "text"; text: string } => part.type === "text", 135 ) 136 .map((part) => part.text) 137 .join("\n") 138 .trim(); 139 if (text) return text; 140 } 141 return ""; 142} 143 144async function runAdversary( 145 task: string, 146 cwd: string, 147 signal?: AbortSignal, 148 onProgress?: ProgressUpdate, 149) { 150 const config = await loadAdversaryConfig(); 151 const client = new RpcClient({ 152 cliPath: path.join(getPackageDir(), "dist", "cli.js"), 153 cwd, 154 model: config.model, 155 args: [ 156 "--no-session", 157 "--no-skills", 158 "--no-prompt-templates", 159 "--no-extensions", 160 "--tools", 161 "read,grep,find,ls,bash", 162 "--thinking", 163 config.thinking, 164 "--append-system-prompt", 165 config.systemPrompt, 166 ], 167 }); 168 const abort = () => void client.abort().catch(() => undefined); 169 let unsubscribe: (() => void) | undefined; 170 171 try { 172 if (signal?.aborted) throw new Error("Adversarial review aborted"); 173 onProgress?.("Adversary is starting…", { 174 model: config.model, 175 thinking: config.thinking, 176 status: "starting", 177 }); 178 await client.start(); 179 signal?.addEventListener("abort", abort, { once: true }); 180 unsubscribe = client.onEvent((event) => { 181 if (event.type === "tool_execution_start") { 182 const action = describeToolAction(event.toolName, event.args); 183 onProgress?.(`Reviewer: ${action}`, { 184 model: config.model, 185 thinking: config.thinking, 186 status: "running", 187 action, 188 }); 189 } else if ( 190 event.type === "message_update" && 191 event.assistantMessageEvent.type === "text_start" 192 ) { 193 onProgress?.("Adversary is writing findings…", { 194 model: config.model, 195 thinking: config.thinking, 196 status: "writing", 197 }); 198 } 199 }); 200 const events = await client.promptAndWait( 201 task, 202 undefined, 203 TIMEOUT_MS, 204 ); 205 206 const messages: AssistantMessage[] = []; 207 const usage = emptyUsage(); 208 for (const event of events) { 209 if (event.type !== "message_end" || event.message.role !== "assistant") 210 continue; 211 messages.push(event.message); 212 addUsage(usage, event.message.usage); 213 } 214 215 const last = messages[messages.length - 1]; 216 if (!last) throw new Error("Adversary returned no response"); 217 if (last.stopReason === "error" || last.stopReason === "aborted") { 218 throw new Error( 219 last.errorMessage || `Adversary stopped: ${last.stopReason}`, 220 ); 221 } 222 223 return { 224 text: finalText(messages) || "No findings.", 225 usage, 226 turns: messages.length, 227 model: config.model, 228 thinking: config.thinking, 229 }; 230 } finally { 231 unsubscribe?.(); 232 signal?.removeEventListener("abort", abort); 233 await client.stop(); 234 } 235} 236 237export default function adversaryExtension(pi: ExtensionAPI): void { 238 pi.registerTool({ 239 name: "adversary_review", 240 label: "Adversary Review", 241 description: 242 "Delegate a focused, read-only adversarial review.", 243 promptSnippet: 244 "Run a read-only adversarial review in an isolated reviewer context", 245 promptGuidelines: [ 246 "Use adversary_review only when the user asks for an adversarial or independent review; include the intended behavior and constraints in its task.", 247 ], 248 parameters: AdversaryParams, 249 async execute(_toolCallId, params, signal, onUpdate, ctx) { 250 const result = await runAdversary( 251 params.task, 252 ctx.cwd, 253 signal, 254 (text, details) => { 255 onUpdate?.({ content: [{ type: "text", text }], details }); 256 }, 257 ); 258 return { 259 content: [{ type: "text", text: result.text }], 260 details: { 261 model: result.model, 262 thinking: result.thinking, 263 turns: result.turns, 264 }, 265 usage: result.usage, 266 }; 267 }, 268 }); 269}