@juanbenjumea/pi-agy 0.7.14 → 0.7.16

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.
@@ -1,4 +1,4 @@
1
- import { randomBytes } from "node:crypto";
1
+ import { createHash, randomBytes } from "node:crypto";
2
2
  import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
3
  import * as os from "node:os";
4
4
  import * as path from "node:path";
@@ -83,15 +83,20 @@ export function imageExtension(mimeType: string): string {
83
83
  return IMAGE_EXTENSIONS[mimeType.toLowerCase()] ?? "bin";
84
84
  }
85
85
 
86
- /** Collect image blocks in transcript order, optionally only one tool result. */
86
+ /** Collect image blocks in transcript order, optionally only matching tool result(s). */
87
87
  export function collectImageBlocks(
88
88
  messages: ProviderTranscriptMessage[],
89
- onlyToolCallId?: string,
89
+ onlyToolCallId?: string | ReadonlySet<string>,
90
90
  ): PromptImageBlock[] {
91
91
  const blocks: PromptImageBlock[] = [];
92
92
  for (const message of messages) {
93
93
  if (onlyToolCallId !== undefined) {
94
- if (message.role !== "toolResult" || message.toolCallId !== onlyToolCallId) continue;
94
+ if (message.role !== "toolResult") continue;
95
+ if (typeof onlyToolCallId === "string") {
96
+ if (message.toolCallId !== onlyToolCallId) continue;
97
+ } else if (!message.toolCallId || !onlyToolCallId.has(message.toolCallId)) {
98
+ continue;
99
+ }
95
100
  }
96
101
  const content = message.content;
97
102
  if (!Array.isArray(content)) continue;
@@ -209,42 +214,115 @@ export async function clearImageStaging(): Promise<void> {
209
214
  }
210
215
  }
211
216
 
217
+ function formatHistoryEntry(
218
+ message: ProviderTranscriptMessage,
219
+ imageRef: (block: object) => string | undefined,
220
+ ): string | undefined {
221
+ const text = clip(textOfContent(message.content, imageRef).trim(), MAX_MESSAGE_CHARS);
222
+ if (!text) return undefined;
223
+ let label: string;
224
+ if (message.role === "user") label = "User";
225
+ else if (message.role === "assistant") label = "Assistant (previous turn)";
226
+ else if (message.role === "toolResult") {
227
+ label = `Tool result (${message.toolName ?? "tool"}${message.isError ? ", error" : ""})`;
228
+ } else if (message.role === "system") label = "System";
229
+ else label = message.role;
230
+ return `${label}: ${text}`;
231
+ }
232
+
233
+ function makeImageRef(imagePaths?: Map<object, string>): (block: object) => string | undefined {
234
+ return (block: object): string | undefined => {
235
+ const file = imagePaths?.get(block);
236
+ return file
237
+ ? `[attached image saved at ${file} — open it with your file tools to view it]`
238
+ : undefined;
239
+ };
240
+ }
241
+
212
242
  /**
213
- * Render Pi transcript messages into a single agy `-p` prompt. The latest
214
- * user message is authoritative; earlier history is bounded reference. Pi
215
- * tool definitions are intentionally NOT translated — agy runs its own tool
216
- * loop (see the module wall note).
243
+ * Deterministic fingerprint of a transcript prefix so resumed turns can
244
+ * detect compaction, /tree rewinds, replaced replies, or edited history
245
+ * (including image payloads and tool-call arguments) and fall back to a
246
+ * full prompt instead of sending a delta.
247
+ */
248
+ export function transcriptFingerprint(messages: readonly ProviderTranscriptMessage[]): string {
249
+ const hash = createHash("sha256");
250
+ const mix = (value: string): void => {
251
+ hash.update(value);
252
+ hash.update("\u001f");
253
+ };
254
+ for (const message of messages) {
255
+ mix(message.role);
256
+ if (message.toolCallId) mix(`id:${message.toolCallId}`);
257
+ if (message.toolName) mix(`tool:${message.toolName}`);
258
+ if (message.isError) mix("err:1");
259
+ const content = message.content;
260
+ if (typeof content === "string") {
261
+ mix(`t:${content}`);
262
+ } else if (Array.isArray(content)) {
263
+ for (const block of content) {
264
+ if (typeof block === "string") {
265
+ mix(`t:${block}`);
266
+ } else if (block && typeof block === "object") {
267
+ const record = block as Record<string, unknown>;
268
+ if (record.type === "text" && typeof record.text === "string") {
269
+ mix(`t:${record.text}`);
270
+ } else if (record.type === "thinking" && typeof record.thinking === "string") {
271
+ mix(`th:${record.thinking}`);
272
+ } else if (record.type === "image") {
273
+ mix(`img:${String(record.mimeType ?? "")}:${String(record.data ?? "")}`);
274
+ } else if (record.type === "toolCall") {
275
+ let serializedArgs = "";
276
+ try {
277
+ serializedArgs = JSON.stringify(record.arguments ?? {});
278
+ } catch {
279
+ serializedArgs = "[unserializable]";
280
+ }
281
+ mix(`tc:${String(record.id ?? "")}:${String(record.name ?? "")}:${serializedArgs}`);
282
+ }
283
+ }
284
+ }
285
+ }
286
+ hash.update("\u001e");
287
+ }
288
+ return `m${messages.length}-${hash.digest("hex").slice(0, 16)}`;
289
+ }
290
+
291
+ export interface RenderProviderPromptOptions {
292
+ /** Active bridged Pi tool names exposed via the pi-agy-tools MCP server. */
293
+ bridgedToolNames?: readonly string[];
294
+ }
295
+
296
+ /**
297
+ * Render Pi transcript messages into a full agy turn prompt (used for fresh
298
+ * conversations, model switches, compaction, or /tree rewinds). The latest
299
+ * message is authoritative; earlier history is bounded reference. Active Pi
300
+ * tools are bridged over the localhost `pi-agy-tools` MCP server.
217
301
  */
218
302
  export function renderProviderPrompt(
219
303
  messages: ProviderTranscriptMessage[],
220
304
  systemPrompt?: string,
221
305
  imagePaths?: Map<object, string>,
306
+ options?: RenderProviderPromptOptions,
222
307
  ): string {
223
- const imageRef = (block: object): string | undefined => {
224
- const file = imagePaths?.get(block);
225
- return file
226
- ? `[attached image saved at ${file} — open it with your file tools to view it]`
227
- : undefined;
228
- };
308
+ const imageRef = makeImageRef(imagePaths);
229
309
  const lines: string[] = [];
230
310
  if (systemPrompt?.trim()) {
231
311
  lines.push("System instructions:", clip(systemPrompt.trim(), MAX_SYSTEM_CHARS), "");
232
312
  }
313
+ if (options?.bridgedToolNames && options.bridgedToolNames.length > 0) {
314
+ lines.push(
315
+ `Pi tool bridge (pi-agy-tools MCP server): ${options.bridgedToolNames.join(", ")}.`,
316
+ "Prefer these pi-agy-tools MCP tools over built-in workspace file/shell tools so Pi can render native tool cards, run approval gates, and track touched files.",
317
+ "",
318
+ );
319
+ }
233
320
  const history = messages.slice(0, -1);
234
321
  const rendered: string[] = [];
235
322
  let budget = MAX_HISTORY_CHARS;
236
323
  for (let i = history.length - 1; i >= 0 && budget > 0; i--) {
237
- const message = history[i];
238
- const text = clip(textOfContent(message.content, imageRef).trim(), MAX_MESSAGE_CHARS);
239
- if (!text) continue;
240
- let label: string;
241
- if (message.role === "user") label = "User";
242
- else if (message.role === "assistant") label = "Assistant (previous turn)";
243
- else if (message.role === "toolResult") {
244
- label = `Tool result (${message.toolName ?? "tool"}${message.isError ? ", error" : ""})`;
245
- } else if (message.role === "system") label = "System";
246
- else label = message.role;
247
- const entry = `${label}: ${text}`;
324
+ const entry = formatHistoryEntry(history[i], imageRef);
325
+ if (!entry) continue;
248
326
  if (entry.length > budget) break;
249
327
  budget -= entry.length;
250
328
  rendered.unshift(entry);
@@ -258,3 +336,37 @@ export function renderProviderPrompt(
258
336
  lines.push(currentLabel, current || "(empty — respond with a brief acknowledgement)");
259
337
  return lines.join("\n");
260
338
  }
339
+
340
+ /**
341
+ * Render only the new delta messages for an already-synced resumed agy
342
+ * conversation, avoiding redundant system prompt and transcript replay.
343
+ */
344
+ export function renderProviderDeltaPrompt(
345
+ deltaMessages: ProviderTranscriptMessage[],
346
+ imagePaths?: Map<object, string>,
347
+ ): string {
348
+ const imageRef = makeImageRef(imagePaths);
349
+ if (deltaMessages.length === 1 && deltaMessages[0].role === "user") {
350
+ const current = textOfContent(deltaMessages[0].content, imageRef).trim();
351
+ return current || "(empty — respond with a brief acknowledgement)";
352
+ }
353
+ const lines: string[] = [];
354
+ const earlier = deltaMessages.slice(0, -1);
355
+ const rendered: string[] = [];
356
+ let budget = MAX_HISTORY_CHARS;
357
+ for (let i = earlier.length - 1; i >= 0 && budget > 0; i--) {
358
+ const entry = formatHistoryEntry(earlier[i], imageRef);
359
+ if (!entry) continue;
360
+ if (entry.length > budget) break;
361
+ budget -= entry.length;
362
+ rendered.unshift(entry);
363
+ }
364
+ if (rendered.length > 0) {
365
+ lines.push("Additional context since last turn:", ...rendered, "");
366
+ }
367
+ const last = deltaMessages[deltaMessages.length - 1];
368
+ const current = last ? textOfContent(last.content, imageRef).trim() : "";
369
+ const currentLabel = last && last.role !== "user" ? `Current message (${last.role}):` : "Current message:";
370
+ lines.push(currentLabel, current || "(empty — respond with a brief acknowledgement)");
371
+ return lines.join("\n");
372
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Native token usage validation, session-cumulative differencing, and
3
+ * Pi `Usage` normalization for Antigravity provider turns.
4
+ *
5
+ * Verified against live `agy --output-format stream-json`:
6
+ * - `input_tokens` excludes `cache_read_tokens`.
7
+ * - `output_tokens` includes `thinking_tokens` (matching Pi's `reasoning` subset).
8
+ * - In persistent `--input-format stream-json` mode, `result.usage` is
9
+ * cumulative across turns in the process session, so consecutive turns
10
+ * subtract the previous committed baseline to avoid double-counting.
11
+ */
12
+
13
+ export interface ValidatedAgyUsage {
14
+ input: number;
15
+ output: number;
16
+ cacheRead: number;
17
+ thinking?: number;
18
+ }
19
+
20
+ export interface NormalizedProviderUsage {
21
+ input: number;
22
+ output: number;
23
+ cacheRead: number;
24
+ cacheWrite: number;
25
+ reasoning?: number;
26
+ totalTokens: number;
27
+ }
28
+
29
+ export const ZERO_USAGE_BASELINE: Readonly<ValidatedAgyUsage> = Object.freeze({
30
+ input: 0,
31
+ output: 0,
32
+ cacheRead: 0,
33
+ thinking: 0,
34
+ });
35
+
36
+ function parseNonNegSafeInt(value: unknown): number | undefined {
37
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
38
+ return undefined;
39
+ }
40
+ return value === 0 ? 0 : value;
41
+ }
42
+
43
+ /**
44
+ * Validate a raw `agy` usage object without coercion, rounding, or clamping.
45
+ * Returns `undefined` when absent or when any recognized own field is invalid.
46
+ */
47
+ export function validateAgyUsage(raw: unknown): ValidatedAgyUsage | undefined {
48
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return undefined;
49
+ const record = raw as Record<string, unknown>;
50
+
51
+ if (!Object.hasOwn(record, "input_tokens") || !Object.hasOwn(record, "output_tokens")) {
52
+ return undefined;
53
+ }
54
+ const input = parseNonNegSafeInt(record.input_tokens);
55
+ const output = parseNonNegSafeInt(record.output_tokens);
56
+ if (input === undefined || output === undefined) return undefined;
57
+
58
+ let cacheRead = 0;
59
+ if (Object.hasOwn(record, "cache_read_tokens") && record.cache_read_tokens !== undefined) {
60
+ const parsed = parseNonNegSafeInt(record.cache_read_tokens);
61
+ if (parsed === undefined) return undefined;
62
+ cacheRead = parsed;
63
+ }
64
+
65
+ let thinking: number | undefined;
66
+ if (Object.hasOwn(record, "thinking_tokens") && record.thinking_tokens !== undefined) {
67
+ const parsed = parseNonNegSafeInt(record.thinking_tokens);
68
+ if (parsed === undefined) return undefined;
69
+ thinking = parsed;
70
+ }
71
+
72
+ if (Object.hasOwn(record, "total_tokens") && record.total_tokens !== undefined) {
73
+ if (parseNonNegSafeInt(record.total_tokens) === undefined) return undefined;
74
+ }
75
+
76
+ return thinking !== undefined
77
+ ? { input, output, cacheRead, thinking }
78
+ : { input, output, cacheRead };
79
+ }
80
+
81
+ /**
82
+ * Difference a cumulative session usage snapshot against a known same-generation
83
+ * baseline. Returns `undefined` on counter regression or safe-integer overflow.
84
+ */
85
+ export function diffAgyUsage(
86
+ current: ValidatedAgyUsage,
87
+ baseline: ValidatedAgyUsage,
88
+ ): NormalizedProviderUsage | undefined {
89
+ const input = current.input - baseline.input;
90
+ const output = current.output - baseline.output;
91
+ const cacheRead = current.cacheRead - baseline.cacheRead;
92
+ if (input < 0 || output < 0 || cacheRead < 0) return undefined;
93
+
94
+ let reasoning: number | undefined;
95
+ if (current.thinking !== undefined) {
96
+ const baseThinking = baseline.thinking ?? 0;
97
+ const deltaThinking = current.thinking - baseThinking;
98
+ if (deltaThinking < 0) return undefined;
99
+ if (deltaThinking <= output) {
100
+ reasoning = deltaThinking === 0 ? 0 : deltaThinking;
101
+ }
102
+ }
103
+
104
+ const totalTokens = input + output + cacheRead;
105
+ if (!Number.isSafeInteger(totalTokens)) return undefined;
106
+
107
+ return {
108
+ input: input === 0 ? 0 : input,
109
+ output: output === 0 ? 0 : output,
110
+ cacheRead: cacheRead === 0 ? 0 : cacheRead,
111
+ cacheWrite: 0,
112
+ ...(reasoning !== undefined ? { reasoning } : {}),
113
+ totalTokens: totalTokens === 0 ? 0 : totalTokens,
114
+ };
115
+ }