@apholdings/jensen-code 0.0.7 → 0.1.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.
- package/CHANGELOG.md +3087 -3062
- package/README.md +1 -1
- package/dist/cli.js +0 -0
- package/dist/utils/changelog.d.ts.map +1 -1
- package/dist/utils/changelog.js +4 -0
- package/dist/utils/changelog.js.map +1 -1
- package/docs/compaction.md +1 -1
- package/docs/custom-provider.md +593 -593
- package/docs/extensions.md +1 -1
- package/docs/packages.md +1 -1
- package/docs/rpc.md +1 -1
- package/docs/sdk.md +1 -1
- package/docs/session.md +413 -413
- package/docs/termux.md +1 -1
- package/docs/tui.md +1 -1
- package/package.json +5 -6
package/docs/session.md
CHANGED
|
@@ -1,413 +1,413 @@
|
|
|
1
|
-
# Session File Format
|
|
2
|
-
|
|
3
|
-
Sessions are stored as JSONL (JSON Lines) files. Each line is a JSON object with a `type` field. Session entries form a tree structure via `id`/`parentId` fields, enabling in-place branching without creating new files.
|
|
4
|
-
|
|
5
|
-
## File Location
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
~/.pi/agent/sessions/--<path>--/<timestamp>_<uuid>.jsonl
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Where `<path>` is the working directory with `/` replaced by `-`.
|
|
12
|
-
|
|
13
|
-
## Deleting Sessions
|
|
14
|
-
|
|
15
|
-
Sessions can be removed by deleting their `.jsonl` files under `~/.pi/agent/sessions/`.
|
|
16
|
-
|
|
17
|
-
Pi also supports deleting sessions interactively from `/resume` (select a session and press `Ctrl+D`, then confirm). When available, pi uses the `trash` CLI to avoid permanent deletion.
|
|
18
|
-
|
|
19
|
-
## Session Version
|
|
20
|
-
|
|
21
|
-
Sessions have a version field in the header:
|
|
22
|
-
|
|
23
|
-
- **Version 1**: Linear entry sequence (legacy, auto-migrated on load)
|
|
24
|
-
- **Version 2**: Tree structure with `id`/`parentId` linking
|
|
25
|
-
- **Version 3**: Renamed `hookMessage` role to `custom` (extensions unification)
|
|
26
|
-
|
|
27
|
-
Existing sessions are automatically migrated to the current version (v3) when loaded.
|
|
28
|
-
|
|
29
|
-
## Source Files
|
|
30
|
-
|
|
31
|
-
Source on GitHub ([pi-mono](https://github.com/badlogic/pi-mono)):
|
|
32
|
-
- [`packages/coding-agent/src/core/session-manager.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/session-manager.ts) - Session entry types and SessionManager
|
|
33
|
-
- [`packages/coding-agent/src/core/messages.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/messages.ts) - Extended message types (BashExecutionMessage, CustomMessage, etc.)
|
|
34
|
-
- [`packages/ai/src/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/types.ts) - Base message types (UserMessage, AssistantMessage, ToolResultMessage)
|
|
35
|
-
- [`packages/agent/src/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/agent/src/types.ts) - AgentMessage union type
|
|
36
|
-
|
|
37
|
-
For TypeScript definitions in your project, inspect `node_modules/@apholdings/jensen-code/dist/` and `node_modules/@apholdings/jensen-ai/dist/`.
|
|
38
|
-
|
|
39
|
-
## Message Types
|
|
40
|
-
|
|
41
|
-
Session entries contain `AgentMessage` objects. Understanding these types is essential for parsing sessions and writing extensions.
|
|
42
|
-
|
|
43
|
-
### Content Blocks
|
|
44
|
-
|
|
45
|
-
Messages contain arrays of typed content blocks:
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
interface TextContent {
|
|
49
|
-
type: "text";
|
|
50
|
-
text: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface ImageContent {
|
|
54
|
-
type: "image";
|
|
55
|
-
data: string; // base64 encoded
|
|
56
|
-
mimeType: string; // e.g., "image/jpeg", "image/png"
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
interface ThinkingContent {
|
|
60
|
-
type: "thinking";
|
|
61
|
-
thinking: string;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface ToolCall {
|
|
65
|
-
type: "toolCall";
|
|
66
|
-
id: string;
|
|
67
|
-
name: string;
|
|
68
|
-
arguments: Record<string, any>;
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Base Message Types (from jensen-ai)
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
interface UserMessage {
|
|
76
|
-
role: "user";
|
|
77
|
-
content: string | (TextContent | ImageContent)[];
|
|
78
|
-
timestamp: number; // Unix ms
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
interface AssistantMessage {
|
|
82
|
-
role: "assistant";
|
|
83
|
-
content: (TextContent | ThinkingContent | ToolCall)[];
|
|
84
|
-
api: string;
|
|
85
|
-
provider: string;
|
|
86
|
-
model: string;
|
|
87
|
-
usage: Usage;
|
|
88
|
-
stopReason: "stop" | "length" | "toolUse" | "error" | "aborted";
|
|
89
|
-
errorMessage?: string;
|
|
90
|
-
timestamp: number;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
interface ToolResultMessage {
|
|
94
|
-
role: "toolResult";
|
|
95
|
-
toolCallId: string;
|
|
96
|
-
toolName: string;
|
|
97
|
-
content: (TextContent | ImageContent)[];
|
|
98
|
-
details?: any; // Tool-specific metadata
|
|
99
|
-
isError: boolean;
|
|
100
|
-
timestamp: number;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
interface Usage {
|
|
104
|
-
input: number;
|
|
105
|
-
output: number;
|
|
106
|
-
cacheRead: number;
|
|
107
|
-
cacheWrite: number;
|
|
108
|
-
totalTokens: number;
|
|
109
|
-
cost: {
|
|
110
|
-
input: number;
|
|
111
|
-
output: number;
|
|
112
|
-
cacheRead: number;
|
|
113
|
-
cacheWrite: number;
|
|
114
|
-
total: number;
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### Extended Message Types (from pi-coding-agent)
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
interface BashExecutionMessage {
|
|
123
|
-
role: "bashExecution";
|
|
124
|
-
command: string;
|
|
125
|
-
output: string;
|
|
126
|
-
exitCode: number | undefined;
|
|
127
|
-
cancelled: boolean;
|
|
128
|
-
truncated: boolean;
|
|
129
|
-
fullOutputPath?: string;
|
|
130
|
-
excludeFromContext?: boolean; // true for !! prefix commands
|
|
131
|
-
timestamp: number;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
interface CustomMessage {
|
|
135
|
-
role: "custom";
|
|
136
|
-
customType: string; // Extension identifier
|
|
137
|
-
content: string | (TextContent | ImageContent)[];
|
|
138
|
-
display: boolean; // Show in TUI
|
|
139
|
-
details?: any; // Extension-specific metadata
|
|
140
|
-
timestamp: number;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
interface BranchSummaryMessage {
|
|
144
|
-
role: "branchSummary";
|
|
145
|
-
summary: string;
|
|
146
|
-
fromId: string; // Entry we branched from
|
|
147
|
-
timestamp: number;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
interface CompactionSummaryMessage {
|
|
151
|
-
role: "compactionSummary";
|
|
152
|
-
summary: string;
|
|
153
|
-
tokensBefore: number;
|
|
154
|
-
timestamp: number;
|
|
155
|
-
}
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### AgentMessage Union
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
type AgentMessage =
|
|
162
|
-
| UserMessage
|
|
163
|
-
| AssistantMessage
|
|
164
|
-
| ToolResultMessage
|
|
165
|
-
| BashExecutionMessage
|
|
166
|
-
| CustomMessage
|
|
167
|
-
| BranchSummaryMessage
|
|
168
|
-
| CompactionSummaryMessage;
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
## Entry Base
|
|
172
|
-
|
|
173
|
-
All entries (except `SessionHeader`) extend `SessionEntryBase`:
|
|
174
|
-
|
|
175
|
-
```typescript
|
|
176
|
-
interface SessionEntryBase {
|
|
177
|
-
type: string;
|
|
178
|
-
id: string; // 8-char hex ID
|
|
179
|
-
parentId: string | null; // Parent entry ID (null for first entry)
|
|
180
|
-
timestamp: string; // ISO timestamp
|
|
181
|
-
}
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
## Entry Types
|
|
185
|
-
|
|
186
|
-
### SessionHeader
|
|
187
|
-
|
|
188
|
-
First line of the file. Metadata only, not part of the tree (no `id`/`parentId`).
|
|
189
|
-
|
|
190
|
-
```json
|
|
191
|
-
{"type":"session","version":3,"id":"uuid","timestamp":"2024-12-03T14:00:00.000Z","cwd":"/path/to/project"}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
For sessions with a parent (created via `/fork` or `newSession({ parentSession })`):
|
|
195
|
-
|
|
196
|
-
```json
|
|
197
|
-
{"type":"session","version":3,"id":"uuid","timestamp":"2024-12-03T14:00:00.000Z","cwd":"/path/to/project","parentSession":"/path/to/original/session.jsonl"}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### SessionMessageEntry
|
|
201
|
-
|
|
202
|
-
A message in the conversation. The `message` field contains an `AgentMessage`.
|
|
203
|
-
|
|
204
|
-
```json
|
|
205
|
-
{"type":"message","id":"a1b2c3d4","parentId":"prev1234","timestamp":"2024-12-03T14:00:01.000Z","message":{"role":"user","content":"Hello"}}
|
|
206
|
-
{"type":"message","id":"b2c3d4e5","parentId":"a1b2c3d4","timestamp":"2024-12-03T14:00:02.000Z","message":{"role":"assistant","content":[{"type":"text","text":"Hi!"}],"provider":"anthropic","model":"claude-sonnet-4-5","usage":{...},"stopReason":"stop"}}
|
|
207
|
-
{"type":"message","id":"c3d4e5f6","parentId":"b2c3d4e5","timestamp":"2024-12-03T14:00:03.000Z","message":{"role":"toolResult","toolCallId":"call_123","toolName":"bash","content":[{"type":"text","text":"output"}],"isError":false}}
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### ModelChangeEntry
|
|
211
|
-
|
|
212
|
-
Emitted when the user switches models mid-session.
|
|
213
|
-
|
|
214
|
-
```json
|
|
215
|
-
{"type":"model_change","id":"d4e5f6g7","parentId":"c3d4e5f6","timestamp":"2024-12-03T14:05:00.000Z","provider":"openai","modelId":"gpt-4o"}
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### ThinkingLevelChangeEntry
|
|
219
|
-
|
|
220
|
-
Emitted when the user changes the thinking/reasoning level.
|
|
221
|
-
|
|
222
|
-
```json
|
|
223
|
-
{"type":"thinking_level_change","id":"e5f6g7h8","parentId":"d4e5f6g7","timestamp":"2024-12-03T14:06:00.000Z","thinkingLevel":"high"}
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
### CompactionEntry
|
|
227
|
-
|
|
228
|
-
Created when context is compacted. Stores a summary of earlier messages.
|
|
229
|
-
|
|
230
|
-
```json
|
|
231
|
-
{"type":"compaction","id":"f6g7h8i9","parentId":"e5f6g7h8","timestamp":"2024-12-03T14:10:00.000Z","summary":"User discussed X, Y, Z...","firstKeptEntryId":"c3d4e5f6","tokensBefore":50000}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
Optional fields:
|
|
235
|
-
- `details`: Implementation-specific data (e.g., `{ readFiles: string[], modifiedFiles: string[] }` for default, or custom data for extensions)
|
|
236
|
-
- `fromHook`: `true` if generated by an extension, `false`/`undefined` if pi-generated (legacy field name)
|
|
237
|
-
|
|
238
|
-
### BranchSummaryEntry
|
|
239
|
-
|
|
240
|
-
Created when switching branches via `/tree` with an LLM generated summary of the left branch up to the common ancestor. Captures context from the abandoned path.
|
|
241
|
-
|
|
242
|
-
```json
|
|
243
|
-
{"type":"branch_summary","id":"g7h8i9j0","parentId":"a1b2c3d4","timestamp":"2024-12-03T14:15:00.000Z","fromId":"f6g7h8i9","summary":"Branch explored approach A..."}
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
Optional fields:
|
|
247
|
-
- `details`: File tracking data (`{ readFiles: string[], modifiedFiles: string[] }`) for default, or custom data for extensions
|
|
248
|
-
- `fromHook`: `true` if generated by an extension, `false`/`undefined` if pi-generated (legacy field name)
|
|
249
|
-
|
|
250
|
-
### CustomEntry
|
|
251
|
-
|
|
252
|
-
Extension state persistence. Does NOT participate in LLM context.
|
|
253
|
-
|
|
254
|
-
```json
|
|
255
|
-
{"type":"custom","id":"h8i9j0k1","parentId":"g7h8i9j0","timestamp":"2024-12-03T14:20:00.000Z","customType":"my-extension","data":{"count":42}}
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
Use `customType` to identify your extension's entries on reload.
|
|
259
|
-
|
|
260
|
-
### CustomMessageEntry
|
|
261
|
-
|
|
262
|
-
Extension-injected messages that DO participate in LLM context.
|
|
263
|
-
|
|
264
|
-
```json
|
|
265
|
-
{"type":"custom_message","id":"i9j0k1l2","parentId":"h8i9j0k1","timestamp":"2024-12-03T14:25:00.000Z","customType":"my-extension","content":"Injected context...","display":true}
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
Fields:
|
|
269
|
-
- `content`: String or `(TextContent | ImageContent)[]` (same as UserMessage)
|
|
270
|
-
- `display`: `true` = show in TUI with distinct styling, `false` = hidden
|
|
271
|
-
- `details`: Optional extension-specific metadata (not sent to LLM)
|
|
272
|
-
|
|
273
|
-
### LabelEntry
|
|
274
|
-
|
|
275
|
-
User-defined bookmark/marker on an entry.
|
|
276
|
-
|
|
277
|
-
```json
|
|
278
|
-
{"type":"label","id":"j0k1l2m3","parentId":"i9j0k1l2","timestamp":"2024-12-03T14:30:00.000Z","targetId":"a1b2c3d4","label":"checkpoint-1"}
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
Set `label` to `undefined` to clear a label.
|
|
282
|
-
|
|
283
|
-
### SessionInfoEntry
|
|
284
|
-
|
|
285
|
-
Session metadata (e.g., user-defined display name). Set via `/name` command or `pi.setSessionName()` in extensions.
|
|
286
|
-
|
|
287
|
-
```json
|
|
288
|
-
{"type":"session_info","id":"k1l2m3n4","parentId":"j0k1l2m3","timestamp":"2024-12-03T14:35:00.000Z","name":"Refactor auth module"}
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
The session name is displayed in the session selector (`/resume`) instead of the first message when set.
|
|
292
|
-
|
|
293
|
-
## Tree Structure
|
|
294
|
-
|
|
295
|
-
Entries form a tree:
|
|
296
|
-
- First entry has `parentId: null`
|
|
297
|
-
- Each subsequent entry points to its parent via `parentId`
|
|
298
|
-
- Branching creates new children from an earlier entry
|
|
299
|
-
- The "leaf" is the current position in the tree
|
|
300
|
-
|
|
301
|
-
```
|
|
302
|
-
[user msg] ─── [assistant] ─── [user msg] ─── [assistant] ─┬─ [user msg] ← current leaf
|
|
303
|
-
│
|
|
304
|
-
└─ [branch_summary] ─── [user msg] ← alternate branch
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
## Context Building
|
|
308
|
-
|
|
309
|
-
`buildSessionContext()` walks from the current leaf to the root, producing the message list for the LLM:
|
|
310
|
-
|
|
311
|
-
1. Collects all entries on the path
|
|
312
|
-
2. Extracts current model and thinking level settings
|
|
313
|
-
3. If a `CompactionEntry` is on the path:
|
|
314
|
-
- Emits the summary first
|
|
315
|
-
- Then messages from `firstKeptEntryId` to compaction
|
|
316
|
-
- Then messages after compaction
|
|
317
|
-
4. Converts `BranchSummaryEntry` and `CustomMessageEntry` to appropriate message formats
|
|
318
|
-
|
|
319
|
-
## Parsing Example
|
|
320
|
-
|
|
321
|
-
```typescript
|
|
322
|
-
import { readFileSync } from "fs";
|
|
323
|
-
|
|
324
|
-
const lines = readFileSync("session.jsonl", "utf8").trim().split("\n");
|
|
325
|
-
|
|
326
|
-
for (const line of lines) {
|
|
327
|
-
const entry = JSON.parse(line);
|
|
328
|
-
|
|
329
|
-
switch (entry.type) {
|
|
330
|
-
case "session":
|
|
331
|
-
console.log(`Session v${entry.version ?? 1}: ${entry.id}`);
|
|
332
|
-
break;
|
|
333
|
-
case "message":
|
|
334
|
-
console.log(`[${entry.id}] ${entry.message.role}: ${JSON.stringify(entry.message.content)}`);
|
|
335
|
-
break;
|
|
336
|
-
case "compaction":
|
|
337
|
-
console.log(`[${entry.id}] Compaction: ${entry.tokensBefore} tokens summarized`);
|
|
338
|
-
break;
|
|
339
|
-
case "branch_summary":
|
|
340
|
-
console.log(`[${entry.id}] Branch from ${entry.fromId}`);
|
|
341
|
-
break;
|
|
342
|
-
case "custom":
|
|
343
|
-
console.log(`[${entry.id}] Custom (${entry.customType}): ${JSON.stringify(entry.data)}`);
|
|
344
|
-
break;
|
|
345
|
-
case "custom_message":
|
|
346
|
-
console.log(`[${entry.id}] Extension message (${entry.customType}): ${entry.content}`);
|
|
347
|
-
break;
|
|
348
|
-
case "label":
|
|
349
|
-
console.log(`[${entry.id}] Label "${entry.label}" on ${entry.targetId}`);
|
|
350
|
-
break;
|
|
351
|
-
case "model_change":
|
|
352
|
-
console.log(`[${entry.id}] Model: ${entry.provider}/${entry.modelId}`);
|
|
353
|
-
break;
|
|
354
|
-
case "thinking_level_change":
|
|
355
|
-
console.log(`[${entry.id}] Thinking: ${entry.thinkingLevel}`);
|
|
356
|
-
break;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
## SessionManager API
|
|
362
|
-
|
|
363
|
-
Key methods for working with sessions programmatically.
|
|
364
|
-
|
|
365
|
-
### Static Creation Methods
|
|
366
|
-
- `SessionManager.create(cwd, sessionDir?)` - New session
|
|
367
|
-
- `SessionManager.open(path, sessionDir?)` - Open existing session file
|
|
368
|
-
- `SessionManager.continueRecent(cwd, sessionDir?)` - Continue most recent or create new
|
|
369
|
-
- `SessionManager.inMemory(cwd?)` - No file persistence
|
|
370
|
-
- `SessionManager.forkFrom(sourcePath, targetCwd, sessionDir?)` - Fork session from another project
|
|
371
|
-
|
|
372
|
-
### Static Listing Methods
|
|
373
|
-
- `SessionManager.list(cwd, sessionDir?, onProgress?)` - List sessions for a directory
|
|
374
|
-
- `SessionManager.listAll(onProgress?)` - List all sessions across all projects
|
|
375
|
-
|
|
376
|
-
### Instance Methods - Session Management
|
|
377
|
-
- `newSession(options?)` - Start a new session (options: `{ parentSession?: string }`)
|
|
378
|
-
- `setSessionFile(path)` - Switch to a different session file
|
|
379
|
-
- `createBranchedSession(leafId)` - Extract branch to new session file
|
|
380
|
-
|
|
381
|
-
### Instance Methods - Appending (all return entry ID)
|
|
382
|
-
- `appendMessage(message)` - Add message
|
|
383
|
-
- `appendThinkingLevelChange(level)` - Record thinking change
|
|
384
|
-
- `appendModelChange(provider, modelId)` - Record model change
|
|
385
|
-
- `appendCompaction(summary, firstKeptEntryId, tokensBefore, details?, fromHook?)` - Add compaction
|
|
386
|
-
- `appendCustomEntry(customType, data?)` - Extension state (not in context)
|
|
387
|
-
- `appendSessionInfo(name)` - Set session display name
|
|
388
|
-
- `appendCustomMessageEntry(customType, content, display, details?)` - Extension message (in context)
|
|
389
|
-
- `appendLabelChange(targetId, label)` - Set/clear label
|
|
390
|
-
|
|
391
|
-
### Instance Methods - Tree Navigation
|
|
392
|
-
- `getLeafId()` - Current position
|
|
393
|
-
- `getLeafEntry()` - Get current leaf entry
|
|
394
|
-
- `getEntry(id)` - Get entry by ID
|
|
395
|
-
- `getBranch(fromId?)` - Walk from entry to root
|
|
396
|
-
- `getTree()` - Get full tree structure
|
|
397
|
-
- `getChildren(parentId)` - Get direct children
|
|
398
|
-
- `getLabel(id)` - Get label for entry
|
|
399
|
-
- `branch(entryId)` - Move leaf to earlier entry
|
|
400
|
-
- `resetLeaf()` - Reset leaf to null (before any entries)
|
|
401
|
-
- `branchWithSummary(entryId, summary, details?, fromHook?)` - Branch with context summary
|
|
402
|
-
|
|
403
|
-
### Instance Methods - Context & Info
|
|
404
|
-
- `buildSessionContext()` - Get messages, thinkingLevel, and model for LLM
|
|
405
|
-
- `getEntries()` - All entries (excluding header)
|
|
406
|
-
- `getHeader()` - Session header metadata
|
|
407
|
-
- `getSessionName()` - Get display name from latest session_info entry
|
|
408
|
-
- `getCwd()` - Working directory
|
|
409
|
-
- `getSessionDir()` - Session storage directory
|
|
410
|
-
- `getSessionId()` - Session UUID
|
|
411
|
-
- `getSessionFile()` - Session file path (undefined for in-memory)
|
|
412
|
-
- `isPersisted()` - Whether session is saved to disk
|
|
413
|
-
|
|
1
|
+
# Session File Format
|
|
2
|
+
|
|
3
|
+
Sessions are stored as JSONL (JSON Lines) files. Each line is a JSON object with a `type` field. Session entries form a tree structure via `id`/`parentId` fields, enabling in-place branching without creating new files.
|
|
4
|
+
|
|
5
|
+
## File Location
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
~/.pi/agent/sessions/--<path>--/<timestamp>_<uuid>.jsonl
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Where `<path>` is the working directory with `/` replaced by `-`.
|
|
12
|
+
|
|
13
|
+
## Deleting Sessions
|
|
14
|
+
|
|
15
|
+
Sessions can be removed by deleting their `.jsonl` files under `~/.pi/agent/sessions/`.
|
|
16
|
+
|
|
17
|
+
Pi also supports deleting sessions interactively from `/resume` (select a session and press `Ctrl+D`, then confirm). When available, pi uses the `trash` CLI to avoid permanent deletion.
|
|
18
|
+
|
|
19
|
+
## Session Version
|
|
20
|
+
|
|
21
|
+
Sessions have a version field in the header:
|
|
22
|
+
|
|
23
|
+
- **Version 1**: Linear entry sequence (legacy, auto-migrated on load)
|
|
24
|
+
- **Version 2**: Tree structure with `id`/`parentId` linking
|
|
25
|
+
- **Version 3**: Renamed `hookMessage` role to `custom` (extensions unification)
|
|
26
|
+
|
|
27
|
+
Existing sessions are automatically migrated to the current version (v3) when loaded.
|
|
28
|
+
|
|
29
|
+
## Source Files
|
|
30
|
+
|
|
31
|
+
Source on GitHub ([pi-mono](https://github.com/badlogic/pi-mono)):
|
|
32
|
+
- [`packages/coding-agent/src/core/session-manager.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/session-manager.ts) - Session entry types and SessionManager
|
|
33
|
+
- [`packages/coding-agent/src/core/messages.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/messages.ts) - Extended message types (BashExecutionMessage, CustomMessage, etc.)
|
|
34
|
+
- [`packages/ai/src/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/types.ts) - Base message types (UserMessage, AssistantMessage, ToolResultMessage)
|
|
35
|
+
- [`packages/agent/src/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/agent/src/types.ts) - AgentMessage union type
|
|
36
|
+
|
|
37
|
+
For TypeScript definitions in your project, inspect `node_modules/@apholdings/jensen-code/dist/` and `node_modules/@apholdings/jensen-ai/dist/`.
|
|
38
|
+
|
|
39
|
+
## Message Types
|
|
40
|
+
|
|
41
|
+
Session entries contain `AgentMessage` objects. Understanding these types is essential for parsing sessions and writing extensions.
|
|
42
|
+
|
|
43
|
+
### Content Blocks
|
|
44
|
+
|
|
45
|
+
Messages contain arrays of typed content blocks:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
interface TextContent {
|
|
49
|
+
type: "text";
|
|
50
|
+
text: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface ImageContent {
|
|
54
|
+
type: "image";
|
|
55
|
+
data: string; // base64 encoded
|
|
56
|
+
mimeType: string; // e.g., "image/jpeg", "image/png"
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface ThinkingContent {
|
|
60
|
+
type: "thinking";
|
|
61
|
+
thinking: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ToolCall {
|
|
65
|
+
type: "toolCall";
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
arguments: Record<string, any>;
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Base Message Types (from jensen-ai)
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
interface UserMessage {
|
|
76
|
+
role: "user";
|
|
77
|
+
content: string | (TextContent | ImageContent)[];
|
|
78
|
+
timestamp: number; // Unix ms
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface AssistantMessage {
|
|
82
|
+
role: "assistant";
|
|
83
|
+
content: (TextContent | ThinkingContent | ToolCall)[];
|
|
84
|
+
api: string;
|
|
85
|
+
provider: string;
|
|
86
|
+
model: string;
|
|
87
|
+
usage: Usage;
|
|
88
|
+
stopReason: "stop" | "length" | "toolUse" | "error" | "aborted";
|
|
89
|
+
errorMessage?: string;
|
|
90
|
+
timestamp: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface ToolResultMessage {
|
|
94
|
+
role: "toolResult";
|
|
95
|
+
toolCallId: string;
|
|
96
|
+
toolName: string;
|
|
97
|
+
content: (TextContent | ImageContent)[];
|
|
98
|
+
details?: any; // Tool-specific metadata
|
|
99
|
+
isError: boolean;
|
|
100
|
+
timestamp: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface Usage {
|
|
104
|
+
input: number;
|
|
105
|
+
output: number;
|
|
106
|
+
cacheRead: number;
|
|
107
|
+
cacheWrite: number;
|
|
108
|
+
totalTokens: number;
|
|
109
|
+
cost: {
|
|
110
|
+
input: number;
|
|
111
|
+
output: number;
|
|
112
|
+
cacheRead: number;
|
|
113
|
+
cacheWrite: number;
|
|
114
|
+
total: number;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Extended Message Types (from pi-coding-agent)
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
interface BashExecutionMessage {
|
|
123
|
+
role: "bashExecution";
|
|
124
|
+
command: string;
|
|
125
|
+
output: string;
|
|
126
|
+
exitCode: number | undefined;
|
|
127
|
+
cancelled: boolean;
|
|
128
|
+
truncated: boolean;
|
|
129
|
+
fullOutputPath?: string;
|
|
130
|
+
excludeFromContext?: boolean; // true for !! prefix commands
|
|
131
|
+
timestamp: number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface CustomMessage {
|
|
135
|
+
role: "custom";
|
|
136
|
+
customType: string; // Extension identifier
|
|
137
|
+
content: string | (TextContent | ImageContent)[];
|
|
138
|
+
display: boolean; // Show in TUI
|
|
139
|
+
details?: any; // Extension-specific metadata
|
|
140
|
+
timestamp: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
interface BranchSummaryMessage {
|
|
144
|
+
role: "branchSummary";
|
|
145
|
+
summary: string;
|
|
146
|
+
fromId: string; // Entry we branched from
|
|
147
|
+
timestamp: number;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
interface CompactionSummaryMessage {
|
|
151
|
+
role: "compactionSummary";
|
|
152
|
+
summary: string;
|
|
153
|
+
tokensBefore: number;
|
|
154
|
+
timestamp: number;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### AgentMessage Union
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
type AgentMessage =
|
|
162
|
+
| UserMessage
|
|
163
|
+
| AssistantMessage
|
|
164
|
+
| ToolResultMessage
|
|
165
|
+
| BashExecutionMessage
|
|
166
|
+
| CustomMessage
|
|
167
|
+
| BranchSummaryMessage
|
|
168
|
+
| CompactionSummaryMessage;
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Entry Base
|
|
172
|
+
|
|
173
|
+
All entries (except `SessionHeader`) extend `SessionEntryBase`:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
interface SessionEntryBase {
|
|
177
|
+
type: string;
|
|
178
|
+
id: string; // 8-char hex ID
|
|
179
|
+
parentId: string | null; // Parent entry ID (null for first entry)
|
|
180
|
+
timestamp: string; // ISO timestamp
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Entry Types
|
|
185
|
+
|
|
186
|
+
### SessionHeader
|
|
187
|
+
|
|
188
|
+
First line of the file. Metadata only, not part of the tree (no `id`/`parentId`).
|
|
189
|
+
|
|
190
|
+
```json
|
|
191
|
+
{"type":"session","version":3,"id":"uuid","timestamp":"2024-12-03T14:00:00.000Z","cwd":"/path/to/project"}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
For sessions with a parent (created via `/fork` or `newSession({ parentSession })`):
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
{"type":"session","version":3,"id":"uuid","timestamp":"2024-12-03T14:00:00.000Z","cwd":"/path/to/project","parentSession":"/path/to/original/session.jsonl"}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### SessionMessageEntry
|
|
201
|
+
|
|
202
|
+
A message in the conversation. The `message` field contains an `AgentMessage`.
|
|
203
|
+
|
|
204
|
+
```json
|
|
205
|
+
{"type":"message","id":"a1b2c3d4","parentId":"prev1234","timestamp":"2024-12-03T14:00:01.000Z","message":{"role":"user","content":"Hello"}}
|
|
206
|
+
{"type":"message","id":"b2c3d4e5","parentId":"a1b2c3d4","timestamp":"2024-12-03T14:00:02.000Z","message":{"role":"assistant","content":[{"type":"text","text":"Hi!"}],"provider":"anthropic","model":"claude-sonnet-4-5","usage":{...},"stopReason":"stop"}}
|
|
207
|
+
{"type":"message","id":"c3d4e5f6","parentId":"b2c3d4e5","timestamp":"2024-12-03T14:00:03.000Z","message":{"role":"toolResult","toolCallId":"call_123","toolName":"bash","content":[{"type":"text","text":"output"}],"isError":false}}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### ModelChangeEntry
|
|
211
|
+
|
|
212
|
+
Emitted when the user switches models mid-session.
|
|
213
|
+
|
|
214
|
+
```json
|
|
215
|
+
{"type":"model_change","id":"d4e5f6g7","parentId":"c3d4e5f6","timestamp":"2024-12-03T14:05:00.000Z","provider":"openai","modelId":"gpt-4o"}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### ThinkingLevelChangeEntry
|
|
219
|
+
|
|
220
|
+
Emitted when the user changes the thinking/reasoning level.
|
|
221
|
+
|
|
222
|
+
```json
|
|
223
|
+
{"type":"thinking_level_change","id":"e5f6g7h8","parentId":"d4e5f6g7","timestamp":"2024-12-03T14:06:00.000Z","thinkingLevel":"high"}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### CompactionEntry
|
|
227
|
+
|
|
228
|
+
Created when context is compacted. Stores a summary of earlier messages.
|
|
229
|
+
|
|
230
|
+
```json
|
|
231
|
+
{"type":"compaction","id":"f6g7h8i9","parentId":"e5f6g7h8","timestamp":"2024-12-03T14:10:00.000Z","summary":"User discussed X, Y, Z...","firstKeptEntryId":"c3d4e5f6","tokensBefore":50000}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Optional fields:
|
|
235
|
+
- `details`: Implementation-specific data (e.g., `{ readFiles: string[], modifiedFiles: string[] }` for default, or custom data for extensions)
|
|
236
|
+
- `fromHook`: `true` if generated by an extension, `false`/`undefined` if pi-generated (legacy field name)
|
|
237
|
+
|
|
238
|
+
### BranchSummaryEntry
|
|
239
|
+
|
|
240
|
+
Created when switching branches via `/tree` with an LLM generated summary of the left branch up to the common ancestor. Captures context from the abandoned path.
|
|
241
|
+
|
|
242
|
+
```json
|
|
243
|
+
{"type":"branch_summary","id":"g7h8i9j0","parentId":"a1b2c3d4","timestamp":"2024-12-03T14:15:00.000Z","fromId":"f6g7h8i9","summary":"Branch explored approach A..."}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Optional fields:
|
|
247
|
+
- `details`: File tracking data (`{ readFiles: string[], modifiedFiles: string[] }`) for default, or custom data for extensions
|
|
248
|
+
- `fromHook`: `true` if generated by an extension, `false`/`undefined` if pi-generated (legacy field name)
|
|
249
|
+
|
|
250
|
+
### CustomEntry
|
|
251
|
+
|
|
252
|
+
Extension state persistence. Does NOT participate in LLM context.
|
|
253
|
+
|
|
254
|
+
```json
|
|
255
|
+
{"type":"custom","id":"h8i9j0k1","parentId":"g7h8i9j0","timestamp":"2024-12-03T14:20:00.000Z","customType":"my-extension","data":{"count":42}}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Use `customType` to identify your extension's entries on reload.
|
|
259
|
+
|
|
260
|
+
### CustomMessageEntry
|
|
261
|
+
|
|
262
|
+
Extension-injected messages that DO participate in LLM context.
|
|
263
|
+
|
|
264
|
+
```json
|
|
265
|
+
{"type":"custom_message","id":"i9j0k1l2","parentId":"h8i9j0k1","timestamp":"2024-12-03T14:25:00.000Z","customType":"my-extension","content":"Injected context...","display":true}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Fields:
|
|
269
|
+
- `content`: String or `(TextContent | ImageContent)[]` (same as UserMessage)
|
|
270
|
+
- `display`: `true` = show in TUI with distinct styling, `false` = hidden
|
|
271
|
+
- `details`: Optional extension-specific metadata (not sent to LLM)
|
|
272
|
+
|
|
273
|
+
### LabelEntry
|
|
274
|
+
|
|
275
|
+
User-defined bookmark/marker on an entry.
|
|
276
|
+
|
|
277
|
+
```json
|
|
278
|
+
{"type":"label","id":"j0k1l2m3","parentId":"i9j0k1l2","timestamp":"2024-12-03T14:30:00.000Z","targetId":"a1b2c3d4","label":"checkpoint-1"}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Set `label` to `undefined` to clear a label.
|
|
282
|
+
|
|
283
|
+
### SessionInfoEntry
|
|
284
|
+
|
|
285
|
+
Session metadata (e.g., user-defined display name). Set via `/name` command or `pi.setSessionName()` in extensions.
|
|
286
|
+
|
|
287
|
+
```json
|
|
288
|
+
{"type":"session_info","id":"k1l2m3n4","parentId":"j0k1l2m3","timestamp":"2024-12-03T14:35:00.000Z","name":"Refactor auth module"}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
The session name is displayed in the session selector (`/resume`) instead of the first message when set.
|
|
292
|
+
|
|
293
|
+
## Tree Structure
|
|
294
|
+
|
|
295
|
+
Entries form a tree:
|
|
296
|
+
- First entry has `parentId: null`
|
|
297
|
+
- Each subsequent entry points to its parent via `parentId`
|
|
298
|
+
- Branching creates new children from an earlier entry
|
|
299
|
+
- The "leaf" is the current position in the tree
|
|
300
|
+
|
|
301
|
+
```
|
|
302
|
+
[user msg] ─── [assistant] ─── [user msg] ─── [assistant] ─┬─ [user msg] ← current leaf
|
|
303
|
+
│
|
|
304
|
+
└─ [branch_summary] ─── [user msg] ← alternate branch
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Context Building
|
|
308
|
+
|
|
309
|
+
`buildSessionContext()` walks from the current leaf to the root, producing the message list for the LLM:
|
|
310
|
+
|
|
311
|
+
1. Collects all entries on the path
|
|
312
|
+
2. Extracts current model and thinking level settings
|
|
313
|
+
3. If a `CompactionEntry` is on the path:
|
|
314
|
+
- Emits the summary first
|
|
315
|
+
- Then messages from `firstKeptEntryId` to compaction
|
|
316
|
+
- Then messages after compaction
|
|
317
|
+
4. Converts `BranchSummaryEntry` and `CustomMessageEntry` to appropriate message formats
|
|
318
|
+
|
|
319
|
+
## Parsing Example
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { readFileSync } from "fs";
|
|
323
|
+
|
|
324
|
+
const lines = readFileSync("session.jsonl", "utf8").trim().split("\n");
|
|
325
|
+
|
|
326
|
+
for (const line of lines) {
|
|
327
|
+
const entry = JSON.parse(line);
|
|
328
|
+
|
|
329
|
+
switch (entry.type) {
|
|
330
|
+
case "session":
|
|
331
|
+
console.log(`Session v${entry.version ?? 1}: ${entry.id}`);
|
|
332
|
+
break;
|
|
333
|
+
case "message":
|
|
334
|
+
console.log(`[${entry.id}] ${entry.message.role}: ${JSON.stringify(entry.message.content)}`);
|
|
335
|
+
break;
|
|
336
|
+
case "compaction":
|
|
337
|
+
console.log(`[${entry.id}] Compaction: ${entry.tokensBefore} tokens summarized`);
|
|
338
|
+
break;
|
|
339
|
+
case "branch_summary":
|
|
340
|
+
console.log(`[${entry.id}] Branch from ${entry.fromId}`);
|
|
341
|
+
break;
|
|
342
|
+
case "custom":
|
|
343
|
+
console.log(`[${entry.id}] Custom (${entry.customType}): ${JSON.stringify(entry.data)}`);
|
|
344
|
+
break;
|
|
345
|
+
case "custom_message":
|
|
346
|
+
console.log(`[${entry.id}] Extension message (${entry.customType}): ${entry.content}`);
|
|
347
|
+
break;
|
|
348
|
+
case "label":
|
|
349
|
+
console.log(`[${entry.id}] Label "${entry.label}" on ${entry.targetId}`);
|
|
350
|
+
break;
|
|
351
|
+
case "model_change":
|
|
352
|
+
console.log(`[${entry.id}] Model: ${entry.provider}/${entry.modelId}`);
|
|
353
|
+
break;
|
|
354
|
+
case "thinking_level_change":
|
|
355
|
+
console.log(`[${entry.id}] Thinking: ${entry.thinkingLevel}`);
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## SessionManager API
|
|
362
|
+
|
|
363
|
+
Key methods for working with sessions programmatically.
|
|
364
|
+
|
|
365
|
+
### Static Creation Methods
|
|
366
|
+
- `SessionManager.create(cwd, sessionDir?)` - New session
|
|
367
|
+
- `SessionManager.open(path, sessionDir?)` - Open existing session file
|
|
368
|
+
- `SessionManager.continueRecent(cwd, sessionDir?)` - Continue most recent or create new
|
|
369
|
+
- `SessionManager.inMemory(cwd?)` - No file persistence
|
|
370
|
+
- `SessionManager.forkFrom(sourcePath, targetCwd, sessionDir?)` - Fork session from another project
|
|
371
|
+
|
|
372
|
+
### Static Listing Methods
|
|
373
|
+
- `SessionManager.list(cwd, sessionDir?, onProgress?)` - List sessions for a directory
|
|
374
|
+
- `SessionManager.listAll(onProgress?)` - List all sessions across all projects
|
|
375
|
+
|
|
376
|
+
### Instance Methods - Session Management
|
|
377
|
+
- `newSession(options?)` - Start a new session (options: `{ parentSession?: string }`)
|
|
378
|
+
- `setSessionFile(path)` - Switch to a different session file
|
|
379
|
+
- `createBranchedSession(leafId)` - Extract branch to new session file
|
|
380
|
+
|
|
381
|
+
### Instance Methods - Appending (all return entry ID)
|
|
382
|
+
- `appendMessage(message)` - Add message
|
|
383
|
+
- `appendThinkingLevelChange(level)` - Record thinking change
|
|
384
|
+
- `appendModelChange(provider, modelId)` - Record model change
|
|
385
|
+
- `appendCompaction(summary, firstKeptEntryId, tokensBefore, details?, fromHook?)` - Add compaction
|
|
386
|
+
- `appendCustomEntry(customType, data?)` - Extension state (not in context)
|
|
387
|
+
- `appendSessionInfo(name)` - Set session display name
|
|
388
|
+
- `appendCustomMessageEntry(customType, content, display, details?)` - Extension message (in context)
|
|
389
|
+
- `appendLabelChange(targetId, label)` - Set/clear label
|
|
390
|
+
|
|
391
|
+
### Instance Methods - Tree Navigation
|
|
392
|
+
- `getLeafId()` - Current position
|
|
393
|
+
- `getLeafEntry()` - Get current leaf entry
|
|
394
|
+
- `getEntry(id)` - Get entry by ID
|
|
395
|
+
- `getBranch(fromId?)` - Walk from entry to root
|
|
396
|
+
- `getTree()` - Get full tree structure
|
|
397
|
+
- `getChildren(parentId)` - Get direct children
|
|
398
|
+
- `getLabel(id)` - Get label for entry
|
|
399
|
+
- `branch(entryId)` - Move leaf to earlier entry
|
|
400
|
+
- `resetLeaf()` - Reset leaf to null (before any entries)
|
|
401
|
+
- `branchWithSummary(entryId, summary, details?, fromHook?)` - Branch with context summary
|
|
402
|
+
|
|
403
|
+
### Instance Methods - Context & Info
|
|
404
|
+
- `buildSessionContext()` - Get messages, thinkingLevel, and model for LLM
|
|
405
|
+
- `getEntries()` - All entries (excluding header)
|
|
406
|
+
- `getHeader()` - Session header metadata
|
|
407
|
+
- `getSessionName()` - Get display name from latest session_info entry
|
|
408
|
+
- `getCwd()` - Working directory
|
|
409
|
+
- `getSessionDir()` - Session storage directory
|
|
410
|
+
- `getSessionId()` - Session UUID
|
|
411
|
+
- `getSessionFile()` - Session file path (undefined for in-memory)
|
|
412
|
+
- `isPersisted()` - Whether session is saved to disk
|
|
413
|
+
|