@mariozechner/pi-coding-agent 0.48.0 → 0.49.0

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +9 -1
  3. package/dist/core/agent-session.d.ts +2 -1
  4. package/dist/core/agent-session.d.ts.map +1 -1
  5. package/dist/core/agent-session.js +19 -1
  6. package/dist/core/agent-session.js.map +1 -1
  7. package/dist/core/compaction/compaction.d.ts +11 -0
  8. package/dist/core/compaction/compaction.d.ts.map +1 -1
  9. package/dist/core/compaction/compaction.js +50 -3
  10. package/dist/core/compaction/compaction.js.map +1 -1
  11. package/dist/core/extensions/index.d.ts +1 -1
  12. package/dist/core/extensions/index.d.ts.map +1 -1
  13. package/dist/core/extensions/index.js.map +1 -1
  14. package/dist/core/extensions/loader.d.ts.map +1 -1
  15. package/dist/core/extensions/loader.js +4 -0
  16. package/dist/core/extensions/loader.js.map +1 -1
  17. package/dist/core/extensions/runner.d.ts +2 -0
  18. package/dist/core/extensions/runner.d.ts.map +1 -1
  19. package/dist/core/extensions/runner.js +7 -0
  20. package/dist/core/extensions/runner.js.map +1 -1
  21. package/dist/core/extensions/types.d.ts +23 -0
  22. package/dist/core/extensions/types.d.ts.map +1 -1
  23. package/dist/core/extensions/types.js.map +1 -1
  24. package/dist/core/settings-manager.d.ts +3 -0
  25. package/dist/core/settings-manager.d.ts.map +1 -1
  26. package/dist/core/settings-manager.js +7 -0
  27. package/dist/core/settings-manager.js.map +1 -1
  28. package/dist/core/system-prompt.d.ts.map +1 -1
  29. package/dist/core/system-prompt.js +16 -21
  30. package/dist/core/system-prompt.js.map +1 -1
  31. package/dist/core/tools/path-utils.d.ts +0 -1
  32. package/dist/core/tools/path-utils.d.ts.map +1 -1
  33. package/dist/core/tools/path-utils.js +0 -7
  34. package/dist/core/tools/path-utils.js.map +1 -1
  35. package/dist/core/tools/read.d.ts.map +1 -1
  36. package/dist/core/tools/read.js +2 -12
  37. package/dist/core/tools/read.js.map +1 -1
  38. package/dist/index.d.ts +3 -3
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +2 -2
  41. package/dist/index.js.map +1 -1
  42. package/dist/modes/interactive/components/settings-selector.d.ts +2 -0
  43. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  44. package/dist/modes/interactive/components/settings-selector.js +13 -1
  45. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  46. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  47. package/dist/modes/interactive/interactive-mode.js +48 -2
  48. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  49. package/dist/modes/print-mode.d.ts.map +1 -1
  50. package/dist/modes/print-mode.js +16 -0
  51. package/dist/modes/print-mode.js.map +1 -1
  52. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  53. package/dist/modes/rpc/rpc-mode.js +16 -0
  54. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  55. package/dist/utils/image-convert.d.ts.map +1 -1
  56. package/dist/utils/image-convert.js +4 -3
  57. package/dist/utils/image-convert.js.map +1 -1
  58. package/dist/utils/image-resize.d.ts.map +1 -1
  59. package/dist/utils/image-resize.js +2 -2
  60. package/dist/utils/image-resize.js.map +1 -1
  61. package/dist/utils/photon.d.ts +5 -13
  62. package/dist/utils/photon.d.ts.map +1 -1
  63. package/dist/utils/photon.js +19 -31
  64. package/dist/utils/photon.js.map +1 -1
  65. package/docs/extensions.md +67 -1
  66. package/examples/extensions/README.md +1 -0
  67. package/examples/extensions/custom-header.ts +2 -1
  68. package/examples/extensions/trigger-compact.ts +40 -0
  69. package/examples/extensions/with-deps/package-lock.json +2 -2
  70. package/examples/extensions/with-deps/package.json +1 -1
  71. package/package.json +4 -4
@@ -1 +1 @@
1
- {"version":3,"file":"image-resize.js","sourceRoot":"","sources":["../../src/utils/image-resize.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AA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CAAC;QACF,CAAC;QAED,mDAAmD;QACnD,OAAO;YACN,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;YACjD,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,aAAa;YACb,cAAc;YACd,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,WAAW;YACnB,UAAU,EAAE,IAAI;SAChB,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACR,uBAAuB;QACvB,OAAO;YACN,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,aAAa,EAAE,CAAC;YAChB,cAAc,EAAE,CAAC;YACjB,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,CAAC;YACT,UAAU,EAAE,KAAK;SACjB,CAAC;IACH,CAAC;YAAS,CAAC;QACV,IAAI,KAAK,EAAE,CAAC;YACX,KAAK,CAAC,IAAI,EAAE,CAAC;QACd,CAAC;IACF,CAAC;AAAA,CACD;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAoB,EAAsB;IAC7E,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC;IAClD,OAAO,oBAAoB,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,cAAc,kBAAkB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,6BAA6B,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC;AAAA,CAClM","sourcesContent":["import type { ImageContent } from \"@mariozechner/pi-ai\";\nimport { getPhoton } from \"./photon.js\";\n\nexport interface ImageResizeOptions {\n\tmaxWidth?: number; // Default: 2000\n\tmaxHeight?: number; // Default: 2000\n\tmaxBytes?: number; // Default: 4.5MB (below Anthropic's 5MB limit)\n\tjpegQuality?: number; // Default: 80\n}\n\nexport interface ResizedImage {\n\tdata: string; // base64\n\tmimeType: string;\n\toriginalWidth: number;\n\toriginalHeight: number;\n\twidth: number;\n\theight: number;\n\twasResized: boolean;\n}\n\n// 4.5MB - provides headroom below Anthropic's 5MB limit\nconst DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;\n\nconst DEFAULT_OPTIONS: Required<ImageResizeOptions> = {\n\tmaxWidth: 2000,\n\tmaxHeight: 2000,\n\tmaxBytes: DEFAULT_MAX_BYTES,\n\tjpegQuality: 80,\n};\n\n/** Helper to pick the smaller of two buffers */\nfunction pickSmaller(\n\ta: { buffer: Uint8Array; mimeType: string },\n\tb: { buffer: Uint8Array; mimeType: string },\n): { buffer: Uint8Array; mimeType: string } {\n\treturn a.buffer.length <= b.buffer.length ? a : b;\n}\n\n/**\n * Resize an image to fit within the specified max dimensions and file size.\n * Returns the original image if it already fits within the limits.\n *\n * Uses Photon (Rust/WASM) for image processing. If Photon is not available,\n * returns the original image unchanged.\n *\n * Strategy for staying under maxBytes:\n * 1. First resize to maxWidth/maxHeight\n * 2. Try both PNG and JPEG formats, pick the smaller one\n * 3. If still too large, try JPEG with decreasing quality\n * 4. If still too large, progressively reduce dimensions\n */\nexport async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage> {\n\tconst opts = { ...DEFAULT_OPTIONS, ...options };\n\tconst inputBuffer = Buffer.from(img.data, \"base64\");\n\n\tconst photon = getPhoton();\n\tif (!photon) {\n\t\t// Photon not available, return original image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t}\n\n\tlet image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;\n\ttry {\n\t\timage = photon.PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));\n\n\t\tconst originalWidth = image.get_width();\n\t\tconst originalHeight = image.get_height();\n\t\tconst format = img.mimeType?.split(\"/\")[1] ?? \"png\";\n\n\t\t// Check if already within all limits (dimensions AND size)\n\t\tconst originalSize = inputBuffer.length;\n\t\tif (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: img.data,\n\t\t\t\tmimeType: img.mimeType ?? `image/${format}`,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: originalWidth,\n\t\t\t\theight: originalHeight,\n\t\t\t\twasResized: false,\n\t\t\t};\n\t\t}\n\n\t\t// Calculate initial dimensions respecting max limits\n\t\tlet targetWidth = originalWidth;\n\t\tlet targetHeight = originalHeight;\n\n\t\tif (targetWidth > opts.maxWidth) {\n\t\t\ttargetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);\n\t\t\ttargetWidth = opts.maxWidth;\n\t\t}\n\t\tif (targetHeight > opts.maxHeight) {\n\t\t\ttargetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);\n\t\t\ttargetHeight = opts.maxHeight;\n\t\t}\n\n\t\t// Helper to resize and encode in both formats, returning the smaller one\n\t\tfunction tryBothFormats(\n\t\t\twidth: number,\n\t\t\theight: number,\n\t\t\tjpegQuality: number,\n\t\t): { buffer: Uint8Array; mimeType: string } {\n\t\t\tconst resized = photon!.resize(image!, width, height, photon!.SamplingFilter.Lanczos3);\n\n\t\t\ttry {\n\t\t\t\tconst pngBuffer = resized.get_bytes();\n\t\t\t\tconst jpegBuffer = resized.get_bytes_jpeg(jpegQuality);\n\n\t\t\t\treturn pickSmaller(\n\t\t\t\t\t{ buffer: pngBuffer, mimeType: \"image/png\" },\n\t\t\t\t\t{ buffer: jpegBuffer, mimeType: \"image/jpeg\" },\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tresized.free();\n\t\t\t}\n\t\t}\n\n\t\t// Try to produce an image under maxBytes\n\t\tconst qualitySteps = [85, 70, 55, 40];\n\t\tconst scaleSteps = [1.0, 0.75, 0.5, 0.35, 0.25];\n\n\t\tlet best: { buffer: Uint8Array; mimeType: string };\n\t\tlet finalWidth = targetWidth;\n\t\tlet finalHeight = targetHeight;\n\n\t\t// First attempt: resize to target dimensions, try both formats\n\t\tbest = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);\n\n\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\tmimeType: best.mimeType,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: finalWidth,\n\t\t\t\theight: finalHeight,\n\t\t\t\twasResized: true,\n\t\t\t};\n\t\t}\n\n\t\t// Still too large - try JPEG with decreasing quality\n\t\tfor (const quality of qualitySteps) {\n\t\t\tbest = tryBothFormats(targetWidth, targetHeight, quality);\n\n\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\treturn {\n\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\toriginalWidth,\n\t\t\t\t\toriginalHeight,\n\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\theight: finalHeight,\n\t\t\t\t\twasResized: true,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t// Still too large - reduce dimensions progressively\n\t\tfor (const scale of scaleSteps) {\n\t\t\tfinalWidth = Math.round(targetWidth * scale);\n\t\t\tfinalHeight = Math.round(targetHeight * scale);\n\n\t\t\tif (finalWidth < 100 || finalHeight < 100) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tfor (const quality of qualitySteps) {\n\t\t\t\tbest = tryBothFormats(finalWidth, finalHeight, quality);\n\n\t\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\t\toriginalWidth,\n\t\t\t\t\t\toriginalHeight,\n\t\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\t\theight: finalHeight,\n\t\t\t\t\t\twasResized: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Last resort: return smallest version we produced\n\t\treturn {\n\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\tmimeType: best.mimeType,\n\t\t\toriginalWidth,\n\t\t\toriginalHeight,\n\t\t\twidth: finalWidth,\n\t\t\theight: finalHeight,\n\t\t\twasResized: true,\n\t\t};\n\t} catch {\n\t\t// Failed to load image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t} finally {\n\t\tif (image) {\n\t\t\timage.free();\n\t\t}\n\t}\n}\n\n/**\n * Format a dimension note for resized images.\n * This helps the model understand the coordinate mapping.\n */\nexport function formatDimensionNote(result: ResizedImage): string | undefined {\n\tif (!result.wasResized) {\n\t\treturn undefined;\n\t}\n\n\tconst scale = result.originalWidth / result.width;\n\treturn `[Image: original ${result.originalWidth}x${result.originalHeight}, displayed at ${result.width}x${result.height}. Multiply coordinates by ${scale.toFixed(2)} to map to original image.]`;\n}\n"]}
1
+ {"version":3,"file":"image-resize.js","sourceRoot":"","sources":["../../src/utils/image-resize.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAmBzC,wDAAwD;AACxD,MAAM,iBAAiB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE5C,MAAM,eAAe,GAAiC;IACrD,QAAQ,EAAE,IAAI;IACd,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,iBAAiB;IAC3B,WAAW,EAAE,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b,cAAc;YACd,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,WAAW;YACnB,UAAU,EAAE,IAAI;SAChB,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACR,uBAAuB;QACvB,OAAO;YACN,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,aAAa,EAAE,CAAC;YAChB,cAAc,EAAE,CAAC;YACjB,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,CAAC;YACT,UAAU,EAAE,KAAK;SACjB,CAAC;IACH,CAAC;YAAS,CAAC;QACV,IAAI,KAAK,EAAE,CAAC;YACX,KAAK,CAAC,IAAI,EAAE,CAAC;QACd,CAAC;IACF,CAAC;AAAA,CACD;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAoB,EAAsB;IAC7E,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC;IAClD,OAAO,oBAAoB,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,cAAc,kBAAkB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,6BAA6B,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC;AAAA,CAClM","sourcesContent":["import type { ImageContent } from \"@mariozechner/pi-ai\";\nimport { loadPhoton } from \"./photon.js\";\n\nexport interface ImageResizeOptions {\n\tmaxWidth?: number; // Default: 2000\n\tmaxHeight?: number; // Default: 2000\n\tmaxBytes?: number; // Default: 4.5MB (below Anthropic's 5MB limit)\n\tjpegQuality?: number; // Default: 80\n}\n\nexport interface ResizedImage {\n\tdata: string; // base64\n\tmimeType: string;\n\toriginalWidth: number;\n\toriginalHeight: number;\n\twidth: number;\n\theight: number;\n\twasResized: boolean;\n}\n\n// 4.5MB - provides headroom below Anthropic's 5MB limit\nconst DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;\n\nconst DEFAULT_OPTIONS: Required<ImageResizeOptions> = {\n\tmaxWidth: 2000,\n\tmaxHeight: 2000,\n\tmaxBytes: DEFAULT_MAX_BYTES,\n\tjpegQuality: 80,\n};\n\n/** Helper to pick the smaller of two buffers */\nfunction pickSmaller(\n\ta: { buffer: Uint8Array; mimeType: string },\n\tb: { buffer: Uint8Array; mimeType: string },\n): { buffer: Uint8Array; mimeType: string } {\n\treturn a.buffer.length <= b.buffer.length ? a : b;\n}\n\n/**\n * Resize an image to fit within the specified max dimensions and file size.\n * Returns the original image if it already fits within the limits.\n *\n * Uses Photon (Rust/WASM) for image processing. If Photon is not available,\n * returns the original image unchanged.\n *\n * Strategy for staying under maxBytes:\n * 1. First resize to maxWidth/maxHeight\n * 2. Try both PNG and JPEG formats, pick the smaller one\n * 3. If still too large, try JPEG with decreasing quality\n * 4. If still too large, progressively reduce dimensions\n */\nexport async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage> {\n\tconst opts = { ...DEFAULT_OPTIONS, ...options };\n\tconst inputBuffer = Buffer.from(img.data, \"base64\");\n\n\tconst photon = await loadPhoton();\n\tif (!photon) {\n\t\t// Photon not available, return original image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t}\n\n\tlet image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;\n\ttry {\n\t\timage = photon.PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));\n\n\t\tconst originalWidth = image.get_width();\n\t\tconst originalHeight = image.get_height();\n\t\tconst format = img.mimeType?.split(\"/\")[1] ?? \"png\";\n\n\t\t// Check if already within all limits (dimensions AND size)\n\t\tconst originalSize = inputBuffer.length;\n\t\tif (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: img.data,\n\t\t\t\tmimeType: img.mimeType ?? `image/${format}`,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: originalWidth,\n\t\t\t\theight: originalHeight,\n\t\t\t\twasResized: false,\n\t\t\t};\n\t\t}\n\n\t\t// Calculate initial dimensions respecting max limits\n\t\tlet targetWidth = originalWidth;\n\t\tlet targetHeight = originalHeight;\n\n\t\tif (targetWidth > opts.maxWidth) {\n\t\t\ttargetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);\n\t\t\ttargetWidth = opts.maxWidth;\n\t\t}\n\t\tif (targetHeight > opts.maxHeight) {\n\t\t\ttargetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);\n\t\t\ttargetHeight = opts.maxHeight;\n\t\t}\n\n\t\t// Helper to resize and encode in both formats, returning the smaller one\n\t\tfunction tryBothFormats(\n\t\t\twidth: number,\n\t\t\theight: number,\n\t\t\tjpegQuality: number,\n\t\t): { buffer: Uint8Array; mimeType: string } {\n\t\t\tconst resized = photon!.resize(image!, width, height, photon!.SamplingFilter.Lanczos3);\n\n\t\t\ttry {\n\t\t\t\tconst pngBuffer = resized.get_bytes();\n\t\t\t\tconst jpegBuffer = resized.get_bytes_jpeg(jpegQuality);\n\n\t\t\t\treturn pickSmaller(\n\t\t\t\t\t{ buffer: pngBuffer, mimeType: \"image/png\" },\n\t\t\t\t\t{ buffer: jpegBuffer, mimeType: \"image/jpeg\" },\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tresized.free();\n\t\t\t}\n\t\t}\n\n\t\t// Try to produce an image under maxBytes\n\t\tconst qualitySteps = [85, 70, 55, 40];\n\t\tconst scaleSteps = [1.0, 0.75, 0.5, 0.35, 0.25];\n\n\t\tlet best: { buffer: Uint8Array; mimeType: string };\n\t\tlet finalWidth = targetWidth;\n\t\tlet finalHeight = targetHeight;\n\n\t\t// First attempt: resize to target dimensions, try both formats\n\t\tbest = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);\n\n\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\tmimeType: best.mimeType,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: finalWidth,\n\t\t\t\theight: finalHeight,\n\t\t\t\twasResized: true,\n\t\t\t};\n\t\t}\n\n\t\t// Still too large - try JPEG with decreasing quality\n\t\tfor (const quality of qualitySteps) {\n\t\t\tbest = tryBothFormats(targetWidth, targetHeight, quality);\n\n\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\treturn {\n\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\toriginalWidth,\n\t\t\t\t\toriginalHeight,\n\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\theight: finalHeight,\n\t\t\t\t\twasResized: true,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t// Still too large - reduce dimensions progressively\n\t\tfor (const scale of scaleSteps) {\n\t\t\tfinalWidth = Math.round(targetWidth * scale);\n\t\t\tfinalHeight = Math.round(targetHeight * scale);\n\n\t\t\tif (finalWidth < 100 || finalHeight < 100) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tfor (const quality of qualitySteps) {\n\t\t\t\tbest = tryBothFormats(finalWidth, finalHeight, quality);\n\n\t\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\t\toriginalWidth,\n\t\t\t\t\t\toriginalHeight,\n\t\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\t\theight: finalHeight,\n\t\t\t\t\t\twasResized: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Last resort: return smallest version we produced\n\t\treturn {\n\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\tmimeType: best.mimeType,\n\t\t\toriginalWidth,\n\t\t\toriginalHeight,\n\t\t\twidth: finalWidth,\n\t\t\theight: finalHeight,\n\t\t\twasResized: true,\n\t\t};\n\t} catch {\n\t\t// Failed to load image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t} finally {\n\t\tif (image) {\n\t\t\timage.free();\n\t\t}\n\t}\n}\n\n/**\n * Format a dimension note for resized images.\n * This helps the model understand the coordinate mapping.\n */\nexport function formatDimensionNote(result: ResizedImage): string | undefined {\n\tif (!result.wasResized) {\n\t\treturn undefined;\n\t}\n\n\tconst scale = result.originalWidth / result.width;\n\treturn `[Image: original ${result.originalWidth}x${result.originalHeight}, displayed at ${result.width}x${result.height}. Multiply coordinates by ${scale.toFixed(2)} to map to original image.]`;\n}\n"]}
@@ -8,21 +8,13 @@
8
8
  * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')
9
9
  * which bakes the build machine's absolute path into Bun compiled binaries.
10
10
  *
11
- * Solution: Lazy-load photon and gracefully handle failures. Image processing functions
12
- * already have fallbacks that return original images when photon isn't available.
11
+ * Solution: Lazy-load photon via dynamic import and gracefully handle failures.
12
+ * Image processing functions have fallbacks that return original images when photon isn't available.
13
13
  */
14
14
  export type { PhotonImage as PhotonImageType } from "@silvia-odwyer/photon-node";
15
15
  /**
16
- * Get the photon module, loading it lazily on first access.
17
- * Returns null if loading fails (e.g., in broken Bun binary).
16
+ * Load the photon module asynchronously.
17
+ * Returns cached module on subsequent calls.
18
18
  */
19
- export declare function getPhoton(): typeof import("@silvia-odwyer/photon-node") | null;
20
- /**
21
- * Check if photon is available and working.
22
- */
23
- export declare function isPhotonAvailable(): boolean;
24
- /**
25
- * Get the error that occurred during photon loading, if any.
26
- */
27
- export declare function getPhotonLoadError(): Error | null;
19
+ export declare function loadPhoton(): Promise<typeof import("@silvia-odwyer/photon-node") | null>;
28
20
  //# sourceMappingURL=photon.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"photon.d.ts","sourceRoot":"","sources":["../../src/utils/photon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,YAAY,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAOjF;;;GAGG;AACH,wBAAgB,SAAS,IAAI,cAAc,4BAA4B,CAAC,GAAG,IAAI,CAiB9E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,KAAK,GAAG,IAAI,CAGjD","sourcesContent":["/**\n * Photon image processing wrapper.\n *\n * This module provides a unified interface to @silvia-odwyer/photon-node that works in:\n * 1. Node.js (development, npm run build)\n * 2. Bun compiled binaries (standalone distribution)\n *\n * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')\n * which bakes the build machine's absolute path into Bun compiled binaries.\n *\n * Solution: Lazy-load photon and gracefully handle failures. Image processing functions\n * already have fallbacks that return original images when photon isn't available.\n */\n\n// Re-export types from the main package\nexport type { PhotonImage as PhotonImageType } from \"@silvia-odwyer/photon-node\";\n\n// Lazy-loaded photon module\nlet photonModule: typeof import(\"@silvia-odwyer/photon-node\") | null = null;\nlet loadAttempted = false;\nlet loadError: Error | null = null;\n\n/**\n * Get the photon module, loading it lazily on first access.\n * Returns null if loading fails (e.g., in broken Bun binary).\n */\nexport function getPhoton(): typeof import(\"@silvia-odwyer/photon-node\") | null {\n\tif (loadAttempted) {\n\t\treturn photonModule;\n\t}\n\n\tloadAttempted = true;\n\n\ttry {\n\t\t// Dynamic require to defer loading until actually needed\n\t\t// This also allows the error to be caught gracefully\n\t\tphotonModule = require(\"@silvia-odwyer/photon-node\");\n\t} catch (e) {\n\t\tloadError = e as Error;\n\t\tphotonModule = null;\n\t}\n\n\treturn photonModule;\n}\n\n/**\n * Check if photon is available and working.\n */\nexport function isPhotonAvailable(): boolean {\n\treturn getPhoton() !== null;\n}\n\n/**\n * Get the error that occurred during photon loading, if any.\n */\nexport function getPhotonLoadError(): Error | null {\n\tgetPhoton(); // Ensure load was attempted\n\treturn loadError;\n}\n"]}
1
+ {"version":3,"file":"photon.d.ts","sourceRoot":"","sources":["../../src/utils/photon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,YAAY,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAMjF;;;GAGG;AACH,wBAAsB,UAAU,IAAI,OAAO,CAAC,cAAc,4BAA4B,CAAC,GAAG,IAAI,CAAC,CAmB9F","sourcesContent":["/**\n * Photon image processing wrapper.\n *\n * This module provides a unified interface to @silvia-odwyer/photon-node that works in:\n * 1. Node.js (development, npm run build)\n * 2. Bun compiled binaries (standalone distribution)\n *\n * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')\n * which bakes the build machine's absolute path into Bun compiled binaries.\n *\n * Solution: Lazy-load photon via dynamic import and gracefully handle failures.\n * Image processing functions have fallbacks that return original images when photon isn't available.\n */\n\n// Re-export types from the main package\nexport type { PhotonImage as PhotonImageType } from \"@silvia-odwyer/photon-node\";\n\n// Lazy-loaded photon module\nlet photonModule: typeof import(\"@silvia-odwyer/photon-node\") | null = null;\nlet loadPromise: Promise<typeof import(\"@silvia-odwyer/photon-node\") | null> | null = null;\n\n/**\n * Load the photon module asynchronously.\n * Returns cached module on subsequent calls.\n */\nexport async function loadPhoton(): Promise<typeof import(\"@silvia-odwyer/photon-node\") | null> {\n\tif (photonModule) {\n\t\treturn photonModule;\n\t}\n\n\tif (loadPromise) {\n\t\treturn loadPromise;\n\t}\n\n\tloadPromise = (async () => {\n\t\ttry {\n\t\t\tphotonModule = await import(\"@silvia-odwyer/photon-node\");\n\t\t} catch {\n\t\t\tphotonModule = null;\n\t\t}\n\t\treturn photonModule;\n\t})();\n\n\treturn loadPromise;\n}\n"]}
@@ -8,44 +8,32 @@
8
8
  * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')
9
9
  * which bakes the build machine's absolute path into Bun compiled binaries.
10
10
  *
11
- * Solution: Lazy-load photon and gracefully handle failures. Image processing functions
12
- * already have fallbacks that return original images when photon isn't available.
11
+ * Solution: Lazy-load photon via dynamic import and gracefully handle failures.
12
+ * Image processing functions have fallbacks that return original images when photon isn't available.
13
13
  */
14
14
  // Lazy-loaded photon module
15
15
  let photonModule = null;
16
- let loadAttempted = false;
17
- let loadError = null;
16
+ let loadPromise = null;
18
17
  /**
19
- * Get the photon module, loading it lazily on first access.
20
- * Returns null if loading fails (e.g., in broken Bun binary).
18
+ * Load the photon module asynchronously.
19
+ * Returns cached module on subsequent calls.
21
20
  */
22
- export function getPhoton() {
23
- if (loadAttempted) {
21
+ export async function loadPhoton() {
22
+ if (photonModule) {
24
23
  return photonModule;
25
24
  }
26
- loadAttempted = true;
27
- try {
28
- // Dynamic require to defer loading until actually needed
29
- // This also allows the error to be caught gracefully
30
- photonModule = require("@silvia-odwyer/photon-node");
25
+ if (loadPromise) {
26
+ return loadPromise;
31
27
  }
32
- catch (e) {
33
- loadError = e;
34
- photonModule = null;
35
- }
36
- return photonModule;
37
- }
38
- /**
39
- * Check if photon is available and working.
40
- */
41
- export function isPhotonAvailable() {
42
- return getPhoton() !== null;
43
- }
44
- /**
45
- * Get the error that occurred during photon loading, if any.
46
- */
47
- export function getPhotonLoadError() {
48
- getPhoton(); // Ensure load was attempted
49
- return loadError;
28
+ loadPromise = (async () => {
29
+ try {
30
+ photonModule = await import("@silvia-odwyer/photon-node");
31
+ }
32
+ catch {
33
+ photonModule = null;
34
+ }
35
+ return photonModule;
36
+ })();
37
+ return loadPromise;
50
38
  }
51
39
  //# sourceMappingURL=photon.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"photon.js","sourceRoot":"","sources":["../../src/utils/photon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,4BAA4B;AAC5B,IAAI,YAAY,GAAuD,IAAI,CAAC;AAC5E,IAAI,aAAa,GAAG,KAAK,CAAC;AAC1B,IAAI,SAAS,GAAiB,IAAI,CAAC;AAEnC;;;GAGG;AACH,MAAM,UAAU,SAAS,GAAuD;IAC/E,IAAI,aAAa,EAAE,CAAC;QACnB,OAAO,YAAY,CAAC;IACrB,CAAC;IAED,aAAa,GAAG,IAAI,CAAC;IAErB,IAAI,CAAC;QACJ,yDAAyD;QACzD,qDAAqD;QACrD,YAAY,GAAG,OAAO,CAAC,4BAA4B,CAAC,CAAC;IACtD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACZ,SAAS,GAAG,CAAU,CAAC;QACvB,YAAY,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,OAAO,YAAY,CAAC;AAAA,CACpB;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,GAAY;IAC5C,OAAO,SAAS,EAAE,KAAK,IAAI,CAAC;AAAA,CAC5B;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,GAAiB;IAClD,SAAS,EAAE,CAAC,CAAC,4BAA4B;IACzC,OAAO,SAAS,CAAC;AAAA,CACjB","sourcesContent":["/**\n * Photon image processing wrapper.\n *\n * This module provides a unified interface to @silvia-odwyer/photon-node that works in:\n * 1. Node.js (development, npm run build)\n * 2. Bun compiled binaries (standalone distribution)\n *\n * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')\n * which bakes the build machine's absolute path into Bun compiled binaries.\n *\n * Solution: Lazy-load photon and gracefully handle failures. Image processing functions\n * already have fallbacks that return original images when photon isn't available.\n */\n\n// Re-export types from the main package\nexport type { PhotonImage as PhotonImageType } from \"@silvia-odwyer/photon-node\";\n\n// Lazy-loaded photon module\nlet photonModule: typeof import(\"@silvia-odwyer/photon-node\") | null = null;\nlet loadAttempted = false;\nlet loadError: Error | null = null;\n\n/**\n * Get the photon module, loading it lazily on first access.\n * Returns null if loading fails (e.g., in broken Bun binary).\n */\nexport function getPhoton(): typeof import(\"@silvia-odwyer/photon-node\") | null {\n\tif (loadAttempted) {\n\t\treturn photonModule;\n\t}\n\n\tloadAttempted = true;\n\n\ttry {\n\t\t// Dynamic require to defer loading until actually needed\n\t\t// This also allows the error to be caught gracefully\n\t\tphotonModule = require(\"@silvia-odwyer/photon-node\");\n\t} catch (e) {\n\t\tloadError = e as Error;\n\t\tphotonModule = null;\n\t}\n\n\treturn photonModule;\n}\n\n/**\n * Check if photon is available and working.\n */\nexport function isPhotonAvailable(): boolean {\n\treturn getPhoton() !== null;\n}\n\n/**\n * Get the error that occurred during photon loading, if any.\n */\nexport function getPhotonLoadError(): Error | null {\n\tgetPhoton(); // Ensure load was attempted\n\treturn loadError;\n}\n"]}
1
+ {"version":3,"file":"photon.js","sourceRoot":"","sources":["../../src/utils/photon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,4BAA4B;AAC5B,IAAI,YAAY,GAAuD,IAAI,CAAC;AAC5E,IAAI,WAAW,GAAuE,IAAI,CAAC;AAE3F;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,GAAgE;IAC/F,IAAI,YAAY,EAAE,CAAC;QAClB,OAAO,YAAY,CAAC;IACrB,CAAC;IAED,IAAI,WAAW,EAAE,CAAC;QACjB,OAAO,WAAW,CAAC;IACpB,CAAC;IAED,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC;QAC1B,IAAI,CAAC;YACJ,YAAY,GAAG,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAC;QAC3D,CAAC;QAAC,MAAM,CAAC;YACR,YAAY,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,OAAO,YAAY,CAAC;IAAA,CACpB,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AAAA,CACnB","sourcesContent":["/**\n * Photon image processing wrapper.\n *\n * This module provides a unified interface to @silvia-odwyer/photon-node that works in:\n * 1. Node.js (development, npm run build)\n * 2. Bun compiled binaries (standalone distribution)\n *\n * The challenge: photon-node's CJS entry uses fs.readFileSync(__dirname + '/photon_rs_bg.wasm')\n * which bakes the build machine's absolute path into Bun compiled binaries.\n *\n * Solution: Lazy-load photon via dynamic import and gracefully handle failures.\n * Image processing functions have fallbacks that return original images when photon isn't available.\n */\n\n// Re-export types from the main package\nexport type { PhotonImage as PhotonImageType } from \"@silvia-odwyer/photon-node\";\n\n// Lazy-loaded photon module\nlet photonModule: typeof import(\"@silvia-odwyer/photon-node\") | null = null;\nlet loadPromise: Promise<typeof import(\"@silvia-odwyer/photon-node\") | null> | null = null;\n\n/**\n * Load the photon module asynchronously.\n * Returns cached module on subsequent calls.\n */\nexport async function loadPhoton(): Promise<typeof import(\"@silvia-odwyer/photon-node\") | null> {\n\tif (photonModule) {\n\t\treturn photonModule;\n\t}\n\n\tif (loadPromise) {\n\t\treturn loadPromise;\n\t}\n\n\tloadPromise = (async () => {\n\t\ttry {\n\t\t\tphotonModule = await import(\"@silvia-odwyer/photon-node\");\n\t\t} catch {\n\t\t\tphotonModule = null;\n\t\t}\n\t\treturn photonModule;\n\t})();\n\n\treturn loadPromise;\n}\n"]}
@@ -384,7 +384,7 @@ pi.on("session_compact", async (event, ctx) => {
384
384
  });
385
385
  ```
386
386
 
387
- **Examples:** [custom-compaction.ts](../examples/extensions/custom-compaction.ts)
387
+ **Examples:** [custom-compaction.ts](../examples/extensions/custom-compaction.ts), [trigger-compact.ts](../examples/extensions/trigger-compact.ts)
388
388
 
389
389
  #### session_before_tree / session_tree
390
390
 
@@ -677,6 +677,33 @@ pi.on("tool_call", (event, ctx) => {
677
677
  });
678
678
  ```
679
679
 
680
+ ### ctx.getContextUsage()
681
+
682
+ Returns current context usage for the active model. Uses last assistant usage when available, then estimates tokens for trailing messages.
683
+
684
+ ```typescript
685
+ const usage = ctx.getContextUsage();
686
+ if (usage && usage.tokens > 100_000) {
687
+ // ...
688
+ }
689
+ ```
690
+
691
+ ### ctx.compact()
692
+
693
+ Trigger compaction without awaiting completion. Use `onComplete` and `onError` for follow-up actions.
694
+
695
+ ```typescript
696
+ ctx.compact({
697
+ customInstructions: "Focus on recent changes",
698
+ onComplete: (result) => {
699
+ ctx.ui.notify("Compaction completed", "info");
700
+ },
701
+ onError: (error) => {
702
+ ctx.ui.notify(`Compaction failed: ${error.message}`, "error");
703
+ },
704
+ });
705
+ ```
706
+
680
707
  ## ExtensionCommandContext
681
708
 
682
709
  Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.
@@ -877,6 +904,23 @@ if (name) {
877
904
  }
878
905
  ```
879
906
 
907
+ ### pi.setLabel(entryId, label)
908
+
909
+ Set or clear a label on an entry. Labels are user-defined markers for bookmarking and navigation (shown in `/tree` selector).
910
+
911
+ ```typescript
912
+ // Set a label
913
+ pi.setLabel(entryId, "checkpoint-before-refactor");
914
+
915
+ // Clear a label
916
+ pi.setLabel(entryId, undefined);
917
+
918
+ // Read labels via sessionManager
919
+ const label = ctx.sessionManager.getLabel(entryId);
920
+ ```
921
+
922
+ Labels persist in the session and survive restarts. Use them to mark important points (turns, checkpoints) in the conversation tree.
923
+
880
924
  ### pi.registerCommand(name, options)
881
925
 
882
926
  Register a command.
@@ -1276,6 +1320,28 @@ renderResult(result, { expanded, isPartial }, theme) {
1276
1320
  }
1277
1321
  ```
1278
1322
 
1323
+ #### Keybinding Hints
1324
+
1325
+ Use `keyHint()` to display keybinding hints that respect user's keybinding configuration:
1326
+
1327
+ ```typescript
1328
+ import { keyHint } from "@mariozechner/pi-coding-agent";
1329
+
1330
+ renderResult(result, { expanded }, theme) {
1331
+ let text = theme.fg("success", "✓ Done");
1332
+ if (!expanded) {
1333
+ text += ` (${keyHint("expandTools", "to expand")})`;
1334
+ }
1335
+ return new Text(text, 0, 0);
1336
+ }
1337
+ ```
1338
+
1339
+ Available functions:
1340
+ - `keyHint(action, description)` - Editor actions (e.g., `"expandTools"`, `"selectConfirm"`)
1341
+ - `appKeyHint(keybindings, action, description)` - App actions (requires `KeybindingsManager`)
1342
+ - `editorKey(action)` - Get raw key string for editor action
1343
+ - `rawKeyHint(key, description)` - Format a raw key string
1344
+
1279
1345
  #### Best Practices
1280
1346
 
1281
1347
  - Use `Text` with padding `(0, 0)` - the Box handles padding
@@ -77,6 +77,7 @@ cp permission-gate.ts ~/.pi/agent/extensions/
77
77
  | `pirate.ts` | Demonstrates `systemPromptAppend` to dynamically modify system prompt |
78
78
  | `claude-rules.ts` | Scans `.claude/rules/` folder and lists rules in system prompt |
79
79
  | `custom-compaction.ts` | Custom compaction that summarizes entire conversation |
80
+ | `trigger-compact.ts` | Triggers compaction when context usage exceeds 100k tokens and adds `/trigger-compact` command |
80
81
 
81
82
  ### System Integration
82
83
 
@@ -6,6 +6,7 @@
6
6
  */
7
7
 
8
8
  import type { ExtensionAPI, Theme } from "@mariozechner/pi-coding-agent";
9
+ import { VERSION } from "@mariozechner/pi-coding-agent";
9
10
 
10
11
  // --- PI MASCOT ---
11
12
  // Based on pi_mascot.ts - the pi agent character
@@ -52,7 +53,7 @@ export default function (pi: ExtensionAPI) {
52
53
  render(_width: number): string[] {
53
54
  const mascotLines = getPiMascot(theme);
54
55
  // Add a subtitle with hint
55
- const subtitle = theme.fg("muted", " shitty coding agent");
56
+ const subtitle = `${theme.fg("muted", " shitty coding agent")}${theme.fg("dim", ` v${VERSION}`)}`;
56
57
  return [...mascotLines, subtitle];
57
58
  },
58
59
  invalidate() {},
@@ -0,0 +1,40 @@
1
+ import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
2
+
3
+ const COMPACT_THRESHOLD_TOKENS = 100_000;
4
+
5
+ export default function (pi: ExtensionAPI) {
6
+ const triggerCompaction = (ctx: ExtensionContext, customInstructions?: string) => {
7
+ if (ctx.hasUI) {
8
+ ctx.ui.notify("Compaction started", "info");
9
+ }
10
+ ctx.compact({
11
+ customInstructions,
12
+ onComplete: () => {
13
+ if (ctx.hasUI) {
14
+ ctx.ui.notify("Compaction completed", "info");
15
+ }
16
+ },
17
+ onError: (error) => {
18
+ if (ctx.hasUI) {
19
+ ctx.ui.notify(`Compaction failed: ${error.message}`, "error");
20
+ }
21
+ },
22
+ });
23
+ };
24
+
25
+ pi.on("turn_end", (_event, ctx) => {
26
+ const usage = ctx.getContextUsage();
27
+ if (!usage || usage.tokens <= COMPACT_THRESHOLD_TOKENS) {
28
+ return;
29
+ }
30
+ triggerCompaction(ctx);
31
+ });
32
+
33
+ pi.registerCommand("trigger-compact", {
34
+ description: "Trigger compaction immediately",
35
+ handler: async (args, ctx) => {
36
+ const instructions = args.trim() || undefined;
37
+ triggerCompaction(ctx, instructions);
38
+ },
39
+ });
40
+ }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "pi-extension-with-deps",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "pi-extension-with-deps",
9
- "version": "1.12.0",
9
+ "version": "1.13.0",
10
10
  "dependencies": {
11
11
  "ms": "^2.1.3"
12
12
  },
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pi-extension-with-deps",
3
3
  "private": true,
4
- "version": "1.12.0",
4
+ "version": "1.13.0",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "clean": "echo 'nothing to clean'",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mariozechner/pi-coding-agent",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "Coding agent CLI with read, bash, edit, write tools and session management",
5
5
  "type": "module",
6
6
  "piConfig": {
@@ -40,9 +40,9 @@
40
40
  "dependencies": {
41
41
  "@mariozechner/clipboard": "^0.3.0",
42
42
  "@mariozechner/jiti": "^2.6.2",
43
- "@mariozechner/pi-agent-core": "^0.48.0",
44
- "@mariozechner/pi-ai": "^0.48.0",
45
- "@mariozechner/pi-tui": "^0.48.0",
43
+ "@mariozechner/pi-agent-core": "^0.49.0",
44
+ "@mariozechner/pi-ai": "^0.49.0",
45
+ "@mariozechner/pi-tui": "^0.49.0",
46
46
  "@silvia-odwyer/photon-node": "^0.3.4",
47
47
  "chalk": "^5.5.0",
48
48
  "cli-highlight": "^2.1.11",