@mariozechner/pi-coding-agent 0.26.1 → 0.27.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 (54) hide show
  1. package/CHANGELOG.md +15 -1
  2. package/dist/core/agent-session.d.ts +7 -5
  3. package/dist/core/agent-session.d.ts.map +1 -1
  4. package/dist/core/agent-session.js +49 -15
  5. package/dist/core/agent-session.js.map +1 -1
  6. package/dist/core/hooks/index.d.ts +1 -1
  7. package/dist/core/hooks/index.d.ts.map +1 -1
  8. package/dist/core/hooks/index.js.map +1 -1
  9. package/dist/core/hooks/runner.d.ts +3 -3
  10. package/dist/core/hooks/runner.d.ts.map +1 -1
  11. package/dist/core/hooks/runner.js +7 -3
  12. package/dist/core/hooks/runner.js.map +1 -1
  13. package/dist/core/hooks/types.d.ts +30 -24
  14. package/dist/core/hooks/types.d.ts.map +1 -1
  15. package/dist/core/hooks/types.js.map +1 -1
  16. package/dist/core/system-prompt.d.ts.map +1 -1
  17. package/dist/core/system-prompt.js +1 -1
  18. package/dist/core/system-prompt.js.map +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  23. package/dist/modes/interactive/components/bash-execution.js +5 -5
  24. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  25. package/dist/modes/interactive/components/tool-execution.d.ts +8 -3
  26. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  27. package/dist/modes/interactive/components/tool-execution.js +73 -47
  28. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  29. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  30. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  31. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  32. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  33. package/dist/modes/interactive/interactive-mode.d.ts +1 -0
  34. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  35. package/dist/modes/interactive/interactive-mode.js +26 -9
  36. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  37. package/dist/modes/rpc/rpc-client.d.ts +10 -2
  38. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  39. package/dist/modes/rpc/rpc-client.js +7 -2
  40. package/dist/modes/rpc/rpc-client.js.map +1 -1
  41. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  42. package/dist/modes/rpc/rpc-mode.js +5 -5
  43. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  44. package/dist/modes/rpc/rpc-types.d.ts +7 -0
  45. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  46. package/dist/modes/rpc/rpc-types.js.map +1 -1
  47. package/docs/hooks.md +42 -28
  48. package/docs/rpc.md +26 -6
  49. package/examples/hooks/README.md +19 -3
  50. package/examples/hooks/auto-commit-on-exit.ts +50 -0
  51. package/examples/hooks/confirm-destructive.ts +62 -0
  52. package/examples/hooks/dirty-repo-guard.ts +59 -0
  53. package/examples/hooks/git-checkpoint.ts +6 -5
  54. package/package.json +4 -4
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-types.d.ts","sourceRoot":"","sources":["../../../src/modes/rpc/rpc-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AACzF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAClF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAM9D,MAAM,MAAM,UAAU,GAEnB;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,WAA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MAAM,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC1F;IAAE,IAAI,EAAE,iBAAiB,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GAC7F;IACA,IAAI,EAAE,iBAAiB,CAAC;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,QAAQ,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC;CACzC,CAAC;AAML,oCAAoC;AACpC,MAAM,MAAM,iBAAiB,GAC1B;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,GAC5D;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE,CAAC;AAM7D,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC","sourcesContent":["/**\n * RPC protocol types for headless operation.\n *\n * Commands are sent as JSON lines on stdin.\n * Responses and events are emitted as JSON lines on stdout.\n */\n\nimport type { AppMessage, Attachment, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { Model } from \"@mariozechner/pi-ai\";\nimport type { CompactionResult, SessionStats } from \"../../core/agent-session.js\";\nimport type { BashResult } from \"../../core/bash-executor.js\";\n\n// ============================================================================\n// RPC Commands (stdin)\n// ============================================================================\n\nexport type RpcCommand =\n\t// Prompting\n\t| { id?: string; type: \"prompt\"; message: string; attachments?: Attachment[] }\n\t| { id?: string; type: \"queue_message\"; message: string }\n\t| { id?: string; type: \"abort\" }\n\t| { id?: string; type: \"reset\" }\n\n\t// State\n\t| { id?: string; type: \"get_state\" }\n\n\t// Model\n\t| { id?: string; type: \"set_model\"; provider: string; modelId: string }\n\t| { id?: string; type: \"cycle_model\" }\n\t| { id?: string; type: \"get_available_models\" }\n\n\t// Thinking\n\t| { id?: string; type: \"set_thinking_level\"; level: ThinkingLevel }\n\t| { id?: string; type: \"cycle_thinking_level\" }\n\n\t// Queue mode\n\t| { id?: string; type: \"set_queue_mode\"; mode: \"all\" | \"one-at-a-time\" }\n\n\t// Compaction\n\t| { id?: string; type: \"compact\"; customInstructions?: string }\n\t| { id?: string; type: \"set_auto_compaction\"; enabled: boolean }\n\n\t// Retry\n\t| { id?: string; type: \"set_auto_retry\"; enabled: boolean }\n\t| { id?: string; type: \"abort_retry\" }\n\n\t// Bash\n\t| { id?: string; type: \"bash\"; command: string }\n\t| { id?: string; type: \"abort_bash\" }\n\n\t// Session\n\t| { id?: string; type: \"get_session_stats\" }\n\t| { id?: string; type: \"export_html\"; outputPath?: string }\n\t| { id?: string; type: \"switch_session\"; sessionPath: string }\n\t| { id?: string; type: \"branch\"; entryIndex: number }\n\t| { id?: string; type: \"get_branch_messages\" }\n\t| { id?: string; type: \"get_last_assistant_text\" }\n\n\t// Messages\n\t| { id?: string; type: \"get_messages\" };\n\n// ============================================================================\n// RPC State\n// ============================================================================\n\nexport interface RpcSessionState {\n\tmodel: Model<any> | null;\n\tthinkingLevel: ThinkingLevel;\n\tisStreaming: boolean;\n\tisCompacting: boolean;\n\tqueueMode: \"all\" | \"one-at-a-time\";\n\tsessionFile: string | null;\n\tsessionId: string;\n\tautoCompactionEnabled: boolean;\n\tmessageCount: number;\n\tqueuedMessageCount: number;\n}\n\n// ============================================================================\n// RPC Responses (stdout)\n// ============================================================================\n\n// Success responses with data\nexport type RpcResponse =\n\t// Prompting (async - events follow)\n\t| { id?: string; type: \"response\"; command: \"prompt\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"queue_message\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"reset\"; success: true }\n\n\t// State\n\t| { id?: string; type: \"response\"; command: \"get_state\"; success: true; data: RpcSessionState }\n\n\t// Model\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"set_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: Model<any>;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: { model: Model<any>; thinkingLevel: ThinkingLevel; isScoped: boolean } | null;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_available_models\";\n\t\t\tsuccess: true;\n\t\t\tdata: { models: Model<any>[] };\n\t }\n\n\t// Thinking\n\t| { id?: string; type: \"response\"; command: \"set_thinking_level\"; success: true }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_thinking_level\";\n\t\t\tsuccess: true;\n\t\t\tdata: { level: ThinkingLevel } | null;\n\t }\n\n\t// Queue mode\n\t| { id?: string; type: \"response\"; command: \"set_queue_mode\"; success: true }\n\n\t// Compaction\n\t| { id?: string; type: \"response\"; command: \"compact\"; success: true; data: CompactionResult }\n\t| { id?: string; type: \"response\"; command: \"set_auto_compaction\"; success: true }\n\n\t// Retry\n\t| { id?: string; type: \"response\"; command: \"set_auto_retry\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort_retry\"; success: true }\n\n\t// Bash\n\t| { id?: string; type: \"response\"; command: \"bash\"; success: true; data: BashResult }\n\t| { id?: string; type: \"response\"; command: \"abort_bash\"; success: true }\n\n\t// Session\n\t| { id?: string; type: \"response\"; command: \"get_session_stats\"; success: true; data: SessionStats }\n\t| { id?: string; type: \"response\"; command: \"export_html\"; success: true; data: { path: string } }\n\t| { id?: string; type: \"response\"; command: \"switch_session\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"branch\"; success: true; data: { text: string } }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_branch_messages\";\n\t\t\tsuccess: true;\n\t\t\tdata: { messages: Array<{ entryIndex: number; text: string }> };\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_last_assistant_text\";\n\t\t\tsuccess: true;\n\t\t\tdata: { text: string | null };\n\t }\n\n\t// Messages\n\t| { id?: string; type: \"response\"; command: \"get_messages\"; success: true; data: { messages: AppMessage[] } }\n\n\t// Error response (any command can fail)\n\t| { id?: string; type: \"response\"; command: string; success: false; error: string };\n\n// ============================================================================\n// Hook UI Events (stdout)\n// ============================================================================\n\n/** Emitted when a hook needs user input */\nexport type RpcHookUIRequest =\n\t| { type: \"hook_ui_request\"; id: string; method: \"select\"; title: string; options: string[] }\n\t| { type: \"hook_ui_request\"; id: string; method: \"confirm\"; title: string; message: string }\n\t| { type: \"hook_ui_request\"; id: string; method: \"input\"; title: string; placeholder?: string }\n\t| {\n\t\t\ttype: \"hook_ui_request\";\n\t\t\tid: string;\n\t\t\tmethod: \"notify\";\n\t\t\tmessage: string;\n\t\t\tnotifyType?: \"info\" | \"warning\" | \"error\";\n\t };\n\n// ============================================================================\n// Hook UI Commands (stdin)\n// ============================================================================\n\n/** Response to a hook UI request */\nexport type RpcHookUIResponse =\n\t| { type: \"hook_ui_response\"; id: string; value: string }\n\t| { type: \"hook_ui_response\"; id: string; confirmed: boolean }\n\t| { type: \"hook_ui_response\"; id: string; cancelled: true };\n\n// ============================================================================\n// Helper type for extracting command types\n// ============================================================================\n\nexport type RpcCommandType = RpcCommand[\"type\"];\n"]}
1
+ {"version":3,"file":"rpc-types.d.ts","sourceRoot":"","sources":["../../../src/modes/rpc/rpc-types.ts"],"names":[],"mappings":"AAA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MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,GAC5D;IAAE,IAAI,EAAE,kBAAkB,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE,CAAC;AAM7D,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC","sourcesContent":["/**\n * RPC protocol types for headless operation.\n *\n * Commands are sent as JSON lines on stdin.\n * Responses and events are emitted as JSON lines on stdout.\n */\n\nimport type { AppMessage, Attachment, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { Model } from \"@mariozechner/pi-ai\";\nimport type { CompactionResult, SessionStats } from \"../../core/agent-session.js\";\nimport type { BashResult } from \"../../core/bash-executor.js\";\n\n// ============================================================================\n// RPC Commands (stdin)\n// ============================================================================\n\nexport type RpcCommand =\n\t// Prompting\n\t| { id?: string; type: \"prompt\"; message: string; attachments?: Attachment[] }\n\t| { id?: string; type: \"queue_message\"; message: string }\n\t| { id?: string; type: \"abort\" }\n\t| { id?: string; type: \"reset\" }\n\n\t// State\n\t| { id?: string; type: \"get_state\" }\n\n\t// Model\n\t| { id?: string; type: \"set_model\"; provider: string; modelId: string }\n\t| { id?: string; type: \"cycle_model\" }\n\t| { id?: string; type: \"get_available_models\" }\n\n\t// Thinking\n\t| { id?: string; type: \"set_thinking_level\"; level: ThinkingLevel }\n\t| { id?: string; type: \"cycle_thinking_level\" }\n\n\t// Queue mode\n\t| { id?: string; type: \"set_queue_mode\"; mode: \"all\" | \"one-at-a-time\" }\n\n\t// Compaction\n\t| { id?: string; type: \"compact\"; customInstructions?: string }\n\t| { id?: string; type: \"set_auto_compaction\"; enabled: boolean }\n\n\t// Retry\n\t| { id?: string; type: \"set_auto_retry\"; enabled: boolean }\n\t| { id?: string; type: \"abort_retry\" }\n\n\t// Bash\n\t| { id?: string; type: \"bash\"; command: string }\n\t| { id?: string; type: \"abort_bash\" }\n\n\t// Session\n\t| { id?: string; type: \"get_session_stats\" }\n\t| { id?: string; type: \"export_html\"; outputPath?: string }\n\t| { id?: string; type: \"switch_session\"; sessionPath: string }\n\t| { id?: string; type: \"branch\"; entryIndex: number }\n\t| { id?: string; type: \"get_branch_messages\" }\n\t| { id?: string; type: \"get_last_assistant_text\" }\n\n\t// Messages\n\t| { id?: string; type: \"get_messages\" };\n\n// ============================================================================\n// RPC State\n// ============================================================================\n\nexport interface RpcSessionState {\n\tmodel: Model<any> | null;\n\tthinkingLevel: ThinkingLevel;\n\tisStreaming: boolean;\n\tisCompacting: boolean;\n\tqueueMode: \"all\" | \"one-at-a-time\";\n\tsessionFile: string | null;\n\tsessionId: string;\n\tautoCompactionEnabled: boolean;\n\tmessageCount: number;\n\tqueuedMessageCount: number;\n}\n\n// ============================================================================\n// RPC Responses (stdout)\n// ============================================================================\n\n// Success responses with data\nexport type RpcResponse =\n\t// Prompting (async - events follow)\n\t| { id?: string; type: \"response\"; command: \"prompt\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"queue_message\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"reset\"; success: true; data: { cancelled: boolean } }\n\n\t// State\n\t| { id?: string; type: \"response\"; command: \"get_state\"; success: true; data: RpcSessionState }\n\n\t// Model\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"set_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: Model<any>;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: { model: Model<any>; thinkingLevel: ThinkingLevel; isScoped: boolean } | null;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_available_models\";\n\t\t\tsuccess: true;\n\t\t\tdata: { models: Model<any>[] };\n\t }\n\n\t// Thinking\n\t| { id?: string; type: \"response\"; command: \"set_thinking_level\"; success: true }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_thinking_level\";\n\t\t\tsuccess: true;\n\t\t\tdata: { level: ThinkingLevel } | null;\n\t }\n\n\t// Queue mode\n\t| { id?: string; type: \"response\"; command: \"set_queue_mode\"; success: true }\n\n\t// Compaction\n\t| { id?: string; type: \"response\"; command: \"compact\"; success: true; data: CompactionResult }\n\t| { id?: string; type: \"response\"; command: \"set_auto_compaction\"; success: true }\n\n\t// Retry\n\t| { id?: string; type: \"response\"; command: \"set_auto_retry\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort_retry\"; success: true }\n\n\t// Bash\n\t| { id?: string; type: \"response\"; command: \"bash\"; success: true; data: BashResult }\n\t| { id?: string; type: \"response\"; command: \"abort_bash\"; success: true }\n\n\t// Session\n\t| { id?: string; type: \"response\"; command: \"get_session_stats\"; success: true; data: SessionStats }\n\t| { id?: string; type: \"response\"; command: \"export_html\"; success: true; data: { path: string } }\n\t| { id?: string; type: \"response\"; command: \"switch_session\"; success: true; data: { cancelled: boolean } }\n\t| { id?: string; type: \"response\"; command: \"branch\"; success: true; data: { text: string; cancelled: boolean } }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_branch_messages\";\n\t\t\tsuccess: true;\n\t\t\tdata: { messages: Array<{ entryIndex: number; text: string }> };\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_last_assistant_text\";\n\t\t\tsuccess: true;\n\t\t\tdata: { text: string | null };\n\t }\n\n\t// Messages\n\t| { id?: string; type: \"response\"; command: \"get_messages\"; success: true; data: { messages: AppMessage[] } }\n\n\t// Error response (any command can fail)\n\t| { id?: string; type: \"response\"; command: string; success: false; error: string };\n\n// ============================================================================\n// Hook UI Events (stdout)\n// ============================================================================\n\n/** Emitted when a hook needs user input */\nexport type RpcHookUIRequest =\n\t| { type: \"hook_ui_request\"; id: string; method: \"select\"; title: string; options: string[] }\n\t| { type: \"hook_ui_request\"; id: string; method: \"confirm\"; title: string; message: string }\n\t| { type: \"hook_ui_request\"; id: string; method: \"input\"; title: string; placeholder?: string }\n\t| {\n\t\t\ttype: \"hook_ui_request\";\n\t\t\tid: string;\n\t\t\tmethod: \"notify\";\n\t\t\tmessage: string;\n\t\t\tnotifyType?: \"info\" | \"warning\" | \"error\";\n\t };\n\n// ============================================================================\n// Hook UI Commands (stdin)\n// ============================================================================\n\n/** Response to a hook UI request */\nexport type RpcHookUIResponse =\n\t| { type: \"hook_ui_response\"; id: string; value: string }\n\t| { type: \"hook_ui_response\"; id: string; confirmed: boolean }\n\t| { type: \"hook_ui_response\"; id: string; cancelled: true };\n\n// ============================================================================\n// Helper type for extracting command types\n// ============================================================================\n\nexport type RpcCommandType = RpcCommand[\"type\"];\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-types.js","sourceRoot":"","sources":["../../../src/modes/rpc/rpc-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG","sourcesContent":["/**\n * RPC protocol types for headless operation.\n *\n * Commands are sent as JSON lines on stdin.\n * Responses and events are emitted as JSON lines on stdout.\n */\n\nimport type { AppMessage, Attachment, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { Model } from \"@mariozechner/pi-ai\";\nimport type { CompactionResult, SessionStats } from \"../../core/agent-session.js\";\nimport type { BashResult } from \"../../core/bash-executor.js\";\n\n// ============================================================================\n// RPC Commands (stdin)\n// ============================================================================\n\nexport type RpcCommand =\n\t// Prompting\n\t| { id?: string; type: \"prompt\"; message: string; attachments?: Attachment[] }\n\t| { id?: string; type: \"queue_message\"; message: string }\n\t| { id?: string; type: \"abort\" }\n\t| { id?: string; type: \"reset\" }\n\n\t// State\n\t| { id?: string; type: \"get_state\" }\n\n\t// Model\n\t| { id?: string; type: \"set_model\"; provider: string; modelId: string }\n\t| { id?: string; type: \"cycle_model\" }\n\t| { id?: string; type: \"get_available_models\" }\n\n\t// Thinking\n\t| { id?: string; type: \"set_thinking_level\"; level: ThinkingLevel }\n\t| { id?: string; type: \"cycle_thinking_level\" }\n\n\t// Queue mode\n\t| { id?: string; type: \"set_queue_mode\"; mode: \"all\" | \"one-at-a-time\" }\n\n\t// Compaction\n\t| { id?: string; type: \"compact\"; customInstructions?: string }\n\t| { id?: string; type: \"set_auto_compaction\"; enabled: boolean }\n\n\t// Retry\n\t| { id?: string; type: \"set_auto_retry\"; enabled: boolean }\n\t| { id?: string; type: \"abort_retry\" }\n\n\t// Bash\n\t| { id?: string; type: \"bash\"; command: string }\n\t| { id?: string; type: \"abort_bash\" }\n\n\t// Session\n\t| { id?: string; type: \"get_session_stats\" }\n\t| { id?: string; type: \"export_html\"; outputPath?: string }\n\t| { id?: string; type: \"switch_session\"; sessionPath: string }\n\t| { id?: string; type: \"branch\"; entryIndex: number }\n\t| { id?: string; type: \"get_branch_messages\" }\n\t| { id?: string; type: \"get_last_assistant_text\" }\n\n\t// Messages\n\t| { id?: string; type: \"get_messages\" };\n\n// ============================================================================\n// RPC State\n// ============================================================================\n\nexport interface RpcSessionState {\n\tmodel: Model<any> | null;\n\tthinkingLevel: ThinkingLevel;\n\tisStreaming: boolean;\n\tisCompacting: boolean;\n\tqueueMode: \"all\" | \"one-at-a-time\";\n\tsessionFile: string | null;\n\tsessionId: string;\n\tautoCompactionEnabled: boolean;\n\tmessageCount: number;\n\tqueuedMessageCount: number;\n}\n\n// ============================================================================\n// RPC Responses (stdout)\n// ============================================================================\n\n// Success responses with data\nexport type RpcResponse =\n\t// Prompting (async - events follow)\n\t| { id?: string; type: \"response\"; command: \"prompt\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"queue_message\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"reset\"; success: true }\n\n\t// State\n\t| { id?: string; type: \"response\"; command: \"get_state\"; success: true; data: RpcSessionState }\n\n\t// Model\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"set_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: Model<any>;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: { model: Model<any>; thinkingLevel: ThinkingLevel; isScoped: boolean } | null;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_available_models\";\n\t\t\tsuccess: true;\n\t\t\tdata: { models: Model<any>[] };\n\t }\n\n\t// Thinking\n\t| { id?: string; type: \"response\"; command: \"set_thinking_level\"; success: true }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_thinking_level\";\n\t\t\tsuccess: true;\n\t\t\tdata: { level: ThinkingLevel } | null;\n\t }\n\n\t// Queue mode\n\t| { id?: string; type: \"response\"; command: \"set_queue_mode\"; success: true }\n\n\t// Compaction\n\t| { id?: string; type: \"response\"; command: \"compact\"; success: true; data: CompactionResult }\n\t| { id?: string; type: \"response\"; command: \"set_auto_compaction\"; success: true }\n\n\t// Retry\n\t| { id?: string; type: \"response\"; command: \"set_auto_retry\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort_retry\"; success: true }\n\n\t// Bash\n\t| { id?: string; type: \"response\"; command: \"bash\"; success: true; data: BashResult }\n\t| { id?: string; type: \"response\"; command: \"abort_bash\"; success: true }\n\n\t// Session\n\t| { id?: string; type: \"response\"; command: \"get_session_stats\"; success: true; data: SessionStats }\n\t| { id?: string; type: \"response\"; command: \"export_html\"; success: true; data: { path: string } }\n\t| { id?: string; type: \"response\"; command: \"switch_session\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"branch\"; success: true; data: { text: string } }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_branch_messages\";\n\t\t\tsuccess: true;\n\t\t\tdata: { messages: Array<{ entryIndex: number; text: string }> };\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_last_assistant_text\";\n\t\t\tsuccess: true;\n\t\t\tdata: { text: string | null };\n\t }\n\n\t// Messages\n\t| { id?: string; type: \"response\"; command: \"get_messages\"; success: true; data: { messages: AppMessage[] } }\n\n\t// Error response (any command can fail)\n\t| { id?: string; type: \"response\"; command: string; success: false; error: string };\n\n// ============================================================================\n// Hook UI Events (stdout)\n// ============================================================================\n\n/** Emitted when a hook needs user input */\nexport type RpcHookUIRequest =\n\t| { type: \"hook_ui_request\"; id: string; method: \"select\"; title: string; options: string[] }\n\t| { type: \"hook_ui_request\"; id: string; method: \"confirm\"; title: string; message: string }\n\t| { type: \"hook_ui_request\"; id: string; method: \"input\"; title: string; placeholder?: string }\n\t| {\n\t\t\ttype: \"hook_ui_request\";\n\t\t\tid: string;\n\t\t\tmethod: \"notify\";\n\t\t\tmessage: string;\n\t\t\tnotifyType?: \"info\" | \"warning\" | \"error\";\n\t };\n\n// ============================================================================\n// Hook UI Commands (stdin)\n// ============================================================================\n\n/** Response to a hook UI request */\nexport type RpcHookUIResponse =\n\t| { type: \"hook_ui_response\"; id: string; value: string }\n\t| { type: \"hook_ui_response\"; id: string; confirmed: boolean }\n\t| { type: \"hook_ui_response\"; id: string; cancelled: true };\n\n// ============================================================================\n// Helper type for extracting command types\n// ============================================================================\n\nexport type RpcCommandType = RpcCommand[\"type\"];\n"]}
1
+ {"version":3,"file":"rpc-types.js","sourceRoot":"","sources":["../../../src/modes/rpc/rpc-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG","sourcesContent":["/**\n * RPC protocol types for headless operation.\n *\n * Commands are sent as JSON lines on stdin.\n * Responses and events are emitted as JSON lines on stdout.\n */\n\nimport type { AppMessage, Attachment, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { Model } from \"@mariozechner/pi-ai\";\nimport type { CompactionResult, SessionStats } from \"../../core/agent-session.js\";\nimport type { BashResult } from \"../../core/bash-executor.js\";\n\n// ============================================================================\n// RPC Commands (stdin)\n// ============================================================================\n\nexport type RpcCommand =\n\t// Prompting\n\t| { id?: string; type: \"prompt\"; message: string; attachments?: Attachment[] }\n\t| { id?: string; type: \"queue_message\"; message: string }\n\t| { id?: string; type: \"abort\" }\n\t| { id?: string; type: \"reset\" }\n\n\t// State\n\t| { id?: string; type: \"get_state\" }\n\n\t// Model\n\t| { id?: string; type: \"set_model\"; provider: string; modelId: string }\n\t| { id?: string; type: \"cycle_model\" }\n\t| { id?: string; type: \"get_available_models\" }\n\n\t// Thinking\n\t| { id?: string; type: \"set_thinking_level\"; level: ThinkingLevel }\n\t| { id?: string; type: \"cycle_thinking_level\" }\n\n\t// Queue mode\n\t| { id?: string; type: \"set_queue_mode\"; mode: \"all\" | \"one-at-a-time\" }\n\n\t// Compaction\n\t| { id?: string; type: \"compact\"; customInstructions?: string }\n\t| { id?: string; type: \"set_auto_compaction\"; enabled: boolean }\n\n\t// Retry\n\t| { id?: string; type: \"set_auto_retry\"; enabled: boolean }\n\t| { id?: string; type: \"abort_retry\" }\n\n\t// Bash\n\t| { id?: string; type: \"bash\"; command: string }\n\t| { id?: string; type: \"abort_bash\" }\n\n\t// Session\n\t| { id?: string; type: \"get_session_stats\" }\n\t| { id?: string; type: \"export_html\"; outputPath?: string }\n\t| { id?: string; type: \"switch_session\"; sessionPath: string }\n\t| { id?: string; type: \"branch\"; entryIndex: number }\n\t| { id?: string; type: \"get_branch_messages\" }\n\t| { id?: string; type: \"get_last_assistant_text\" }\n\n\t// Messages\n\t| { id?: string; type: \"get_messages\" };\n\n// ============================================================================\n// RPC State\n// ============================================================================\n\nexport interface RpcSessionState {\n\tmodel: Model<any> | null;\n\tthinkingLevel: ThinkingLevel;\n\tisStreaming: boolean;\n\tisCompacting: boolean;\n\tqueueMode: \"all\" | \"one-at-a-time\";\n\tsessionFile: string | null;\n\tsessionId: string;\n\tautoCompactionEnabled: boolean;\n\tmessageCount: number;\n\tqueuedMessageCount: number;\n}\n\n// ============================================================================\n// RPC Responses (stdout)\n// ============================================================================\n\n// Success responses with data\nexport type RpcResponse =\n\t// Prompting (async - events follow)\n\t| { id?: string; type: \"response\"; command: \"prompt\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"queue_message\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"reset\"; success: true; data: { cancelled: boolean } }\n\n\t// State\n\t| { id?: string; type: \"response\"; command: \"get_state\"; success: true; data: RpcSessionState }\n\n\t// Model\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"set_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: Model<any>;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_model\";\n\t\t\tsuccess: true;\n\t\t\tdata: { model: Model<any>; thinkingLevel: ThinkingLevel; isScoped: boolean } | null;\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_available_models\";\n\t\t\tsuccess: true;\n\t\t\tdata: { models: Model<any>[] };\n\t }\n\n\t// Thinking\n\t| { id?: string; type: \"response\"; command: \"set_thinking_level\"; success: true }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"cycle_thinking_level\";\n\t\t\tsuccess: true;\n\t\t\tdata: { level: ThinkingLevel } | null;\n\t }\n\n\t// Queue mode\n\t| { id?: string; type: \"response\"; command: \"set_queue_mode\"; success: true }\n\n\t// Compaction\n\t| { id?: string; type: \"response\"; command: \"compact\"; success: true; data: CompactionResult }\n\t| { id?: string; type: \"response\"; command: \"set_auto_compaction\"; success: true }\n\n\t// Retry\n\t| { id?: string; type: \"response\"; command: \"set_auto_retry\"; success: true }\n\t| { id?: string; type: \"response\"; command: \"abort_retry\"; success: true }\n\n\t// Bash\n\t| { id?: string; type: \"response\"; command: \"bash\"; success: true; data: BashResult }\n\t| { id?: string; type: \"response\"; command: \"abort_bash\"; success: true }\n\n\t// Session\n\t| { id?: string; type: \"response\"; command: \"get_session_stats\"; success: true; data: SessionStats }\n\t| { id?: string; type: \"response\"; command: \"export_html\"; success: true; data: { path: string } }\n\t| { id?: string; type: \"response\"; command: \"switch_session\"; success: true; data: { cancelled: boolean } }\n\t| { id?: string; type: \"response\"; command: \"branch\"; success: true; data: { text: string; cancelled: boolean } }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_branch_messages\";\n\t\t\tsuccess: true;\n\t\t\tdata: { messages: Array<{ entryIndex: number; text: string }> };\n\t }\n\t| {\n\t\t\tid?: string;\n\t\t\ttype: \"response\";\n\t\t\tcommand: \"get_last_assistant_text\";\n\t\t\tsuccess: true;\n\t\t\tdata: { text: string | null };\n\t }\n\n\t// Messages\n\t| { id?: string; type: \"response\"; command: \"get_messages\"; success: true; data: { messages: AppMessage[] } }\n\n\t// Error response (any command can fail)\n\t| { id?: string; type: \"response\"; command: string; success: false; error: string };\n\n// ============================================================================\n// Hook UI Events (stdout)\n// ============================================================================\n\n/** Emitted when a hook needs user input */\nexport type RpcHookUIRequest =\n\t| { type: \"hook_ui_request\"; id: string; method: \"select\"; title: string; options: string[] }\n\t| { type: \"hook_ui_request\"; id: string; method: \"confirm\"; title: string; message: string }\n\t| { type: \"hook_ui_request\"; id: string; method: \"input\"; title: string; placeholder?: string }\n\t| {\n\t\t\ttype: \"hook_ui_request\";\n\t\t\tid: string;\n\t\t\tmethod: \"notify\";\n\t\t\tmessage: string;\n\t\t\tnotifyType?: \"info\" | \"warning\" | \"error\";\n\t };\n\n// ============================================================================\n// Hook UI Commands (stdin)\n// ============================================================================\n\n/** Response to a hook UI request */\nexport type RpcHookUIResponse =\n\t| { type: \"hook_ui_response\"; id: string; value: string }\n\t| { type: \"hook_ui_response\"; id: string; confirmed: boolean }\n\t| { type: \"hook_ui_response\"; id: string; cancelled: true };\n\n// ============================================================================\n// Helper type for extracting command types\n// ============================================================================\n\nexport type RpcCommandType = RpcCommand[\"type\"];\n"]}
package/docs/hooks.md CHANGED
@@ -117,51 +117,55 @@ user sends another prompt ◄─────────────────
117
117
 
118
118
  user branches (/branch)
119
119
 
120
- ├─► branch (BEFORE branch, can control)
121
- └─► session (reason: "switch", AFTER branch)
120
+ ├─► session (reason: "before_branch", can cancel)
121
+ └─► session (reason: "branch", AFTER branch)
122
122
 
123
123
  user switches session (/session)
124
124
 
125
- └─► session (reason: "switch")
125
+ ├─► session (reason: "before_switch", can cancel)
126
+ └─► session (reason: "switch", AFTER switch)
126
127
 
127
128
  user clears session (/clear)
128
129
 
129
- └─► session (reason: "clear")
130
+ ├─► session (reason: "before_clear", can cancel)
131
+ └─► session (reason: "clear", AFTER clear)
132
+
133
+ user exits (double Ctrl+C or Ctrl+D)
134
+
135
+ └─► session (reason: "shutdown")
130
136
  ```
131
137
 
132
138
  A **turn** is one LLM response plus any tool calls. Complex tasks loop through multiple turns until the LLM responds without calling tools.
133
139
 
134
140
  ### session
135
141
 
136
- Fired on startup and when session changes.
142
+ Fired on session lifecycle events. The `before_*` variants fire before the action and can be cancelled by returning `{ cancel: true }`.
137
143
 
138
144
  ```typescript
139
145
  pi.on("session", async (event, ctx) => {
140
146
  // event.entries: SessionEntry[] - all session entries
141
147
  // event.sessionFile: string | null - current session file (null with --no-session)
142
148
  // event.previousSessionFile: string | null - previous session file
143
- // event.reason: "start" | "switch" | "clear"
149
+ // event.reason: "start" | "before_switch" | "switch" | "before_clear" | "clear" |
150
+ // "before_branch" | "branch" | "shutdown"
151
+ // event.targetTurnIndex: number - only for "before_branch" and "branch"
152
+
153
+ // Cancel a before_* action:
154
+ if (event.reason === "before_clear") {
155
+ return { cancel: true };
156
+ }
157
+ // No return needed if not cancelling
144
158
  });
145
159
  ```
146
160
 
147
161
  **Reasons:**
148
162
  - `start`: Initial session load on startup
149
- - `switch`: User switched sessions (`/session`) or branched (`/branch`)
150
- - `clear`: User cleared the session (`/clear`)
151
-
152
- ### branch
153
-
154
- Fired BEFORE a branch happens. Can control branch behavior.
163
+ - `before_switch` / `switch`: User switched sessions (`/session`)
164
+ - `before_clear` / `clear`: User cleared the session (`/clear`)
165
+ - `before_branch` / `branch`: User branched the session (`/branch`)
166
+ - `shutdown`: Process is exiting (double Ctrl+C, Ctrl+D, or SIGTERM)
155
167
 
156
- ```typescript
157
- pi.on("branch", async (event, ctx) => {
158
- // event.targetTurnIndex: number
159
- // event.entries: SessionEntry[]
160
- return { skipConversationRestore: true }; // or undefined
161
- });
162
- ```
163
-
164
- Note: After branch completes, a `session` event fires with `reason: "switch"`.
168
+ For `before_branch` and `branch` events, `event.targetTurnIndex` contains the entry index being branched from.
165
169
 
166
170
  ### agent_start / agent_end
167
171
 
@@ -544,9 +548,12 @@ export default function (pi: HookAPI) {
544
548
  }
545
549
  });
546
550
 
547
- pi.on("branch", async (event, ctx) => {
551
+ pi.on("session", async (event, ctx) => {
552
+ // Only handle before_branch events
553
+ if (event.reason !== "before_branch") return;
554
+
548
555
  const ref = checkpoints.get(event.targetTurnIndex);
549
- if (!ref) return undefined;
556
+ if (!ref) return;
550
557
 
551
558
  const choice = await ctx.ui.select("Restore code state?", [
552
559
  "Yes, restore code to that point",
@@ -557,8 +564,6 @@ export default function (pi: HookAPI) {
557
564
  await ctx.exec("git", ["stash", "apply", ref]);
558
565
  ctx.ui.notify("Code restored to checkpoint", "info");
559
566
  }
560
-
561
- return undefined;
562
567
  });
563
568
 
564
569
  pi.on("agent_end", async () => {
@@ -712,17 +717,26 @@ User sends prompt:
712
717
 
713
718
  Branch:
714
719
  -> AgentSession.branch()
715
- -> hookRunner.emit({ type: "branch", ... }) # BEFORE branch
716
- -> [branch happens]
717
- -> hookRunner.emit({ type: "session", reason: "switch", ... }) # AFTER
720
+ -> hookRunner.emit({ type: "session", reason: "before_branch", ... }) # can cancel
721
+ -> [if not cancelled: branch happens]
722
+ -> hookRunner.emit({ type: "session", reason: "branch", ... })
718
723
 
719
724
  Session switch:
720
725
  -> AgentSession.switchSession()
726
+ -> hookRunner.emit({ type: "session", reason: "before_switch", ... }) # can cancel
727
+ -> [if not cancelled: switch happens]
721
728
  -> hookRunner.emit({ type: "session", reason: "switch", ... })
722
729
 
723
730
  Clear:
724
731
  -> AgentSession.reset()
732
+ -> hookRunner.emit({ type: "session", reason: "before_clear", ... }) # can cancel
733
+ -> [if not cancelled: clear happens]
725
734
  -> hookRunner.emit({ type: "session", reason: "clear", ... })
735
+
736
+ Shutdown (interactive mode):
737
+ -> handleCtrlC() or handleCtrlD()
738
+ -> hookRunner.emit({ type: "session", reason: "shutdown", ... })
739
+ -> process.exit(0)
726
740
  ```
727
741
 
728
742
  ## UI Context by Mode
package/docs/rpc.md CHANGED
@@ -78,7 +78,7 @@ Response:
78
78
 
79
79
  #### reset
80
80
 
81
- Clear context and start a fresh session.
81
+ Clear context and start a fresh session. Can be cancelled by a `before_clear` hook.
82
82
 
83
83
  ```json
84
84
  {"type": "reset"}
@@ -86,7 +86,12 @@ Clear context and start a fresh session.
86
86
 
87
87
  Response:
88
88
  ```json
89
- {"type": "response", "command": "reset", "success": true}
89
+ {"type": "response", "command": "reset", "success": true, "data": {"cancelled": false}}
90
+ ```
91
+
92
+ If a hook cancelled the reset:
93
+ ```json
94
+ {"type": "response", "command": "reset", "success": true, "data": {"cancelled": true}}
90
95
  ```
91
96
 
92
97
  ### State
@@ -465,7 +470,7 @@ Response:
465
470
 
466
471
  #### switch_session
467
472
 
468
- Load a different session file.
473
+ Load a different session file. Can be cancelled by a `before_switch` hook.
469
474
 
470
475
  ```json
471
476
  {"type": "switch_session", "sessionPath": "/path/to/session.jsonl"}
@@ -473,12 +478,17 @@ Load a different session file.
473
478
 
474
479
  Response:
475
480
  ```json
476
- {"type": "response", "command": "switch_session", "success": true}
481
+ {"type": "response", "command": "switch_session", "success": true, "data": {"cancelled": false}}
482
+ ```
483
+
484
+ If a hook cancelled the switch:
485
+ ```json
486
+ {"type": "response", "command": "switch_session", "success": true, "data": {"cancelled": true}}
477
487
  ```
478
488
 
479
489
  #### branch
480
490
 
481
- Create a new branch from a previous user message. Returns the text of the message being branched from.
491
+ Create a new branch from a previous user message. Can be cancelled by a `before_branch` hook. Returns the text of the message being branched from.
482
492
 
483
493
  ```json
484
494
  {"type": "branch", "entryIndex": 2}
@@ -490,7 +500,17 @@ Response:
490
500
  "type": "response",
491
501
  "command": "branch",
492
502
  "success": true,
493
- "data": {"text": "The original prompt text..."}
503
+ "data": {"text": "The original prompt text...", "cancelled": false}
504
+ }
505
+ ```
506
+
507
+ If a hook cancelled the branch:
508
+ ```json
509
+ {
510
+ "type": "response",
511
+ "command": "branch",
512
+ "success": true,
513
+ "data": {"text": "The original prompt text...", "cancelled": true}
494
514
  }
495
515
  ```
496
516
 
@@ -16,6 +16,15 @@ Blocks writes to protected paths (.env, .git/, node_modules/).
16
16
  ### file-trigger.ts
17
17
  Watches a trigger file and injects its contents into the conversation. Useful for external systems (CI, file watchers, webhooks) to send messages to the agent.
18
18
 
19
+ ### confirm-destructive.ts
20
+ Prompts for confirmation before destructive session actions (clear, switch, branch). Demonstrates how to cancel `before_*` session events.
21
+
22
+ ### dirty-repo-guard.ts
23
+ Prevents session changes when there are uncommitted git changes. Blocks clear/switch/branch until you commit.
24
+
25
+ ### auto-commit-on-exit.ts
26
+ Automatically commits changes when the agent exits (shutdown event). Uses the last assistant message to generate a commit message.
27
+
19
28
  ## Usage
20
29
 
21
30
  ```bash
@@ -38,8 +47,16 @@ import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
38
47
 
39
48
  export default function (pi: HookAPI) {
40
49
  pi.on("session", async (event, ctx) => {
41
- // event.reason: "start" | "switch" | "clear"
50
+ // event.reason: "start" | "before_switch" | "switch" | "before_clear" | "clear" |
51
+ // "before_branch" | "branch" | "shutdown"
52
+ // event.targetTurnIndex: number (only for before_branch/branch)
42
53
  // ctx.ui, ctx.exec, ctx.cwd, ctx.sessionFile, ctx.hasUI
54
+
55
+ // Cancel before_* actions:
56
+ if (event.reason === "before_clear") {
57
+ return { cancel: true };
58
+ }
59
+ return undefined;
43
60
  });
44
61
 
45
62
  pi.on("tool_call", async (event, ctx) => {
@@ -58,8 +75,7 @@ export default function (pi: HookAPI) {
58
75
  ```
59
76
 
60
77
  **Available events:**
61
- - `session` - startup, session switch, clear
62
- - `branch` - before branching (can skip conversation restore)
78
+ - `session` - lifecycle events with before/after variants (can cancel before_* actions)
63
79
  - `agent_start` / `agent_end` - per user prompt
64
80
  - `turn_start` / `turn_end` - per LLM turn
65
81
  - `tool_call` - before tool execution (can block)
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Auto-Commit on Exit Hook
3
+ *
4
+ * Automatically commits changes when the agent exits.
5
+ * Uses the last assistant message to generate a commit message.
6
+ */
7
+
8
+ import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
9
+
10
+ export default function (pi: HookAPI) {
11
+ pi.on("session", async (event, ctx) => {
12
+ if (event.reason !== "shutdown") return;
13
+
14
+ // Check for uncommitted changes
15
+ const { stdout: status, code } = await ctx.exec("git", ["status", "--porcelain"]);
16
+
17
+ if (code !== 0 || status.trim().length === 0) {
18
+ // Not a git repo or no changes
19
+ return;
20
+ }
21
+
22
+ // Find the last assistant message for commit context
23
+ let lastAssistantText = "";
24
+ for (let i = event.entries.length - 1; i >= 0; i--) {
25
+ const entry = event.entries[i];
26
+ if (entry.type === "message" && entry.message.role === "assistant") {
27
+ const content = entry.message.content;
28
+ if (Array.isArray(content)) {
29
+ lastAssistantText = content
30
+ .filter((c): c is { type: "text"; text: string } => c.type === "text")
31
+ .map((c) => c.text)
32
+ .join("\n");
33
+ }
34
+ break;
35
+ }
36
+ }
37
+
38
+ // Generate a simple commit message
39
+ const firstLine = lastAssistantText.split("\n")[0] || "Work in progress";
40
+ const commitMessage = `[pi] ${firstLine.slice(0, 50)}${firstLine.length > 50 ? "..." : ""}`;
41
+
42
+ // Stage and commit
43
+ await ctx.exec("git", ["add", "-A"]);
44
+ const { code: commitCode } = await ctx.exec("git", ["commit", "-m", commitMessage]);
45
+
46
+ if (commitCode === 0 && ctx.hasUI) {
47
+ ctx.ui.notify(`Auto-committed: ${commitMessage}`, "info");
48
+ }
49
+ });
50
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Confirm Destructive Actions Hook
3
+ *
4
+ * Prompts for confirmation before destructive session actions (clear, switch, branch).
5
+ * Demonstrates how to cancel session events using the before_* variants.
6
+ */
7
+
8
+ import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
9
+
10
+ export default function (pi: HookAPI) {
11
+ pi.on("session", async (event, ctx) => {
12
+ // Only handle before_* events (the ones that can be cancelled)
13
+ if (event.reason === "before_clear") {
14
+ if (!ctx.hasUI) return;
15
+
16
+ const confirmed = await ctx.ui.confirm(
17
+ "Clear session?",
18
+ "This will delete all messages in the current session.",
19
+ );
20
+
21
+ if (!confirmed) {
22
+ ctx.ui.notify("Clear cancelled", "info");
23
+ return { cancel: true };
24
+ }
25
+ }
26
+
27
+ if (event.reason === "before_switch") {
28
+ if (!ctx.hasUI) return;
29
+
30
+ // Check if there are unsaved changes (messages since last assistant response)
31
+ const hasUnsavedWork = event.entries.some(
32
+ (e) => e.type === "message" && e.message.role === "user",
33
+ );
34
+
35
+ if (hasUnsavedWork) {
36
+ const confirmed = await ctx.ui.confirm(
37
+ "Switch session?",
38
+ "You have messages in the current session. Switch anyway?",
39
+ );
40
+
41
+ if (!confirmed) {
42
+ ctx.ui.notify("Switch cancelled", "info");
43
+ return { cancel: true };
44
+ }
45
+ }
46
+ }
47
+
48
+ if (event.reason === "before_branch") {
49
+ if (!ctx.hasUI) return;
50
+
51
+ const choice = await ctx.ui.select(
52
+ `Branch from turn ${event.targetTurnIndex}?`,
53
+ ["Yes, create branch", "No, stay in current session"],
54
+ );
55
+
56
+ if (choice !== "Yes, create branch") {
57
+ ctx.ui.notify("Branch cancelled", "info");
58
+ return { cancel: true };
59
+ }
60
+ }
61
+ });
62
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Dirty Repo Guard Hook
3
+ *
4
+ * Prevents session changes when there are uncommitted git changes.
5
+ * Useful to ensure work is committed before switching context.
6
+ */
7
+
8
+ import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
9
+
10
+ export default function (pi: HookAPI) {
11
+ pi.on("session", async (event, ctx) => {
12
+ // Only guard destructive actions
13
+ if (
14
+ event.reason !== "before_clear" &&
15
+ event.reason !== "before_switch" &&
16
+ event.reason !== "before_branch"
17
+ ) {
18
+ return;
19
+ }
20
+
21
+ // Check for uncommitted changes
22
+ const { stdout, code } = await ctx.exec("git", ["status", "--porcelain"]);
23
+
24
+ if (code !== 0) {
25
+ // Not a git repo, allow the action
26
+ return;
27
+ }
28
+
29
+ const hasChanges = stdout.trim().length > 0;
30
+ if (!hasChanges) {
31
+ return;
32
+ }
33
+
34
+ if (!ctx.hasUI) {
35
+ // In non-interactive mode, block by default
36
+ return { cancel: true };
37
+ }
38
+
39
+ // Count changed files
40
+ const changedFiles = stdout.trim().split("\n").filter(Boolean).length;
41
+
42
+ const action =
43
+ event.reason === "before_clear"
44
+ ? "clear session"
45
+ : event.reason === "before_switch"
46
+ ? "switch session"
47
+ : "branch";
48
+
49
+ const choice = await ctx.ui.select(
50
+ `You have ${changedFiles} uncommitted file(s). ${action} anyway?`,
51
+ ["Yes, proceed anyway", "No, let me commit first"],
52
+ );
53
+
54
+ if (choice !== "Yes, proceed anyway") {
55
+ ctx.ui.notify("Commit your changes first", "warning");
56
+ return { cancel: true };
57
+ }
58
+ });
59
+ }
@@ -19,13 +19,16 @@ export default function (pi: HookAPI) {
19
19
  }
20
20
  });
21
21
 
22
- pi.on("branch", async (event, ctx) => {
22
+ pi.on("session", async (event, ctx) => {
23
+ // Only handle before_branch events
24
+ if (event.reason !== "before_branch") return;
25
+
23
26
  const ref = checkpoints.get(event.targetTurnIndex);
24
- if (!ref) return undefined;
27
+ if (!ref) return;
25
28
 
26
29
  if (!ctx.hasUI) {
27
30
  // In non-interactive mode, don't restore automatically
28
- return undefined;
31
+ return;
29
32
  }
30
33
 
31
34
  const choice = await ctx.ui.select("Restore code state?", [
@@ -37,8 +40,6 @@ export default function (pi: HookAPI) {
37
40
  await ctx.exec("git", ["stash", "apply", ref]);
38
41
  ctx.ui.notify("Code restored to checkpoint", "info");
39
42
  }
40
-
41
- return undefined;
42
43
  });
43
44
 
44
45
  pi.on("agent_end", async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mariozechner/pi-coding-agent",
3
- "version": "0.26.1",
3
+ "version": "0.27.0",
4
4
  "description": "Coding agent CLI with read, bash, edit, write tools and session management",
5
5
  "type": "module",
6
6
  "piConfig": {
@@ -39,9 +39,9 @@
39
39
  "prepublishOnly": "npm run clean && npm run build"
40
40
  },
41
41
  "dependencies": {
42
- "@mariozechner/pi-agent-core": "^0.26.1",
43
- "@mariozechner/pi-ai": "^0.26.1",
44
- "@mariozechner/pi-tui": "^0.26.1",
42
+ "@mariozechner/pi-agent-core": "^0.27.0",
43
+ "@mariozechner/pi-ai": "^0.27.0",
44
+ "@mariozechner/pi-tui": "^0.27.0",
45
45
  "chalk": "^5.5.0",
46
46
  "cli-highlight": "^2.1.11",
47
47
  "diff": "^8.0.2",