@mariozechner/pi-coding-agent 0.27.5 → 0.27.7

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 (43) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/core/agent-session.d.ts +1 -1
  3. package/dist/core/agent-session.d.ts.map +1 -1
  4. package/dist/core/agent-session.js +49 -30
  5. package/dist/core/agent-session.js.map +1 -1
  6. package/dist/core/compaction.d.ts +3 -0
  7. package/dist/core/compaction.d.ts.map +1 -1
  8. package/dist/core/compaction.js +10 -1
  9. package/dist/core/compaction.js.map +1 -1
  10. package/dist/core/hooks/runner.d.ts.map +1 -1
  11. package/dist/core/hooks/runner.js +11 -3
  12. package/dist/core/hooks/runner.js.map +1 -1
  13. package/dist/core/hooks/types.d.ts +9 -1
  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/model-config.d.ts +7 -2
  17. package/dist/core/model-config.d.ts.map +1 -1
  18. package/dist/core/model-config.js +7 -2
  19. package/dist/core/model-config.js.map +1 -1
  20. package/dist/core/sdk.d.ts.map +1 -1
  21. package/dist/core/sdk.js +1 -1
  22. package/dist/core/sdk.js.map +1 -1
  23. package/dist/core/session-manager.d.ts +24 -11
  24. package/dist/core/session-manager.d.ts.map +1 -1
  25. package/dist/core/session-manager.js +25 -21
  26. package/dist/core/session-manager.js.map +1 -1
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +1 -1
  30. package/dist/index.js.map +1 -1
  31. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  32. package/dist/modes/interactive/interactive-mode.js +5 -5
  33. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  34. package/dist/modes/print-mode.d.ts.map +1 -1
  35. package/dist/modes/print-mode.js +1 -1
  36. package/dist/modes/print-mode.js.map +1 -1
  37. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  38. package/dist/modes/rpc/rpc-mode.js +1 -1
  39. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  40. package/docs/hooks.md +109 -48
  41. package/examples/hooks/README.md +3 -0
  42. package/examples/hooks/custom-compaction.ts +115 -0
  43. package/package.json +4 -4
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Custom Compaction Hook
3
+ *
4
+ * Replaces the default compaction behavior with a full summary of the entire context.
5
+ * Instead of keeping the last 20k tokens of conversation turns, this hook:
6
+ * 1. Summarizes ALL messages (both messagesToSummarize and messagesToKeep and previousSummary)
7
+ * 2. Discards all old turns completely, keeping only the summary
8
+ *
9
+ * This example also demonstrates using a different model (Gemini Flash) for summarization,
10
+ * which can be cheaper/faster than the main conversation model.
11
+ *
12
+ * Usage:
13
+ * pi --hook examples/hooks/custom-compaction.ts
14
+ */
15
+
16
+ import { complete } from "@mariozechner/pi-ai";
17
+ import { findModel, messageTransformer } from "@mariozechner/pi-coding-agent";
18
+ import type { HookAPI } from "@mariozechner/pi-coding-agent/hooks";
19
+
20
+ export default function (pi: HookAPI) {
21
+ pi.on("session", async (event, ctx) => {
22
+ if (event.reason !== "before_compact") return;
23
+
24
+ ctx.ui.notify("Custom compaction hook triggered", "info");
25
+
26
+ const { messagesToSummarize, messagesToKeep, previousSummary, tokensBefore, resolveApiKey, entries, signal } = event;
27
+
28
+ // Use Gemini Flash for summarization (cheaper/faster than most conversation models)
29
+ // findModel searches both built-in models and custom models from models.json
30
+ const { model, error } = findModel("google", "gemini-2.5-flash");
31
+ if (error || !model) {
32
+ ctx.ui.notify(`Could not find Gemini Flash model: ${error}, using default compaction`, "warning");
33
+ return;
34
+ }
35
+
36
+ // Resolve API key for the summarization model
37
+ const apiKey = await resolveApiKey(model);
38
+ if (!apiKey) {
39
+ ctx.ui.notify(`No API key for ${model.provider}, using default compaction`, "warning");
40
+ return;
41
+ }
42
+
43
+ // Combine all messages for full summary
44
+ const allMessages = [...messagesToSummarize, ...messagesToKeep];
45
+
46
+ ctx.ui.notify(
47
+ `Custom compaction: summarizing ${allMessages.length} messages (${tokensBefore.toLocaleString()} tokens) with ${model.id}...`,
48
+ "info",
49
+ );
50
+
51
+ // Transform app messages to pi-ai package format
52
+ const transformedMessages = messageTransformer(allMessages);
53
+
54
+ // Include previous summary context if available
55
+ const previousContext = previousSummary ? `\n\nPrevious session summary for context:\n${previousSummary}` : "";
56
+
57
+ // Build messages that ask for a comprehensive summary
58
+ const summaryMessages = [
59
+ ...transformedMessages,
60
+ {
61
+ role: "user" as const,
62
+ content: [
63
+ {
64
+ type: "text" as const,
65
+ text: `You are a conversation summarizer. Create a comprehensive summary of this entire conversation that captures:${previousContext}
66
+
67
+ 1. The main goals and objectives discussed
68
+ 2. Key decisions made and their rationale
69
+ 3. Important code changes, file modifications, or technical details
70
+ 4. Current state of any ongoing work
71
+ 5. Any blockers, issues, or open questions
72
+ 6. Next steps that were planned or suggested
73
+
74
+ Be thorough but concise. The summary will replace the ENTIRE conversation history, so include all information needed to continue the work effectively.
75
+
76
+ Format the summary as structured markdown with clear sections.`,
77
+ },
78
+ ],
79
+ timestamp: Date.now(),
80
+ },
81
+ ];
82
+
83
+ try {
84
+ // Pass signal to honor abort requests (e.g., user cancels compaction)
85
+ const response = await complete(model, { messages: summaryMessages }, { apiKey, maxTokens: 8192, signal });
86
+
87
+ const summary = response.content
88
+ .filter((c): c is { type: "text"; text: string } => c.type === "text")
89
+ .map((c) => c.text)
90
+ .join("\n");
91
+
92
+ if (!summary.trim()) {
93
+ if (!signal.aborted) ctx.ui.notify("Compaction summary was empty, using default compaction", "warning");
94
+ return;
95
+ }
96
+
97
+ // Return a compaction entry that discards ALL messages
98
+ // firstKeptEntryIndex points past all current entries
99
+ return {
100
+ compactionEntry: {
101
+ type: "compaction" as const,
102
+ timestamp: new Date().toISOString(),
103
+ summary,
104
+ firstKeptEntryIndex: entries.length,
105
+ tokensBefore,
106
+ },
107
+ };
108
+ } catch (error) {
109
+ const message = error instanceof Error ? error.message : String(error);
110
+ ctx.ui.notify(`Compaction failed: ${message}`, "error");
111
+ // Fall back to default compaction on error
112
+ return;
113
+ }
114
+ });
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mariozechner/pi-coding-agent",
3
- "version": "0.27.5",
3
+ "version": "0.27.7",
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.27.5",
43
- "@mariozechner/pi-ai": "^0.27.5",
44
- "@mariozechner/pi-tui": "^0.27.5",
42
+ "@mariozechner/pi-agent-core": "^0.27.7",
43
+ "@mariozechner/pi-ai": "^0.27.7",
44
+ "@mariozechner/pi-tui": "^0.27.7",
45
45
  "chalk": "^5.5.0",
46
46
  "cli-highlight": "^2.1.11",
47
47
  "diff": "^8.0.2",