pi-trace-viewer 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/LICENSE +21 -0
- package/README.md +151 -0
- package/README.zh.md +151 -0
- package/assets/images/compaction-context-viewport.png +0 -0
- package/assets/images/pi-export-viewport.png +0 -0
- package/assets/images/realtime-session-viewport.png +0 -0
- package/package.json +62 -0
- package/src/collector.ts +218 -0
- package/src/index.ts +145 -0
- package/src/security.ts +20 -0
- package/src/server.ts +244 -0
- package/src/store.ts +200 -0
- package/src/types.ts +126 -0
- package/tsconfig.json +14 -0
- package/web/app.js +541 -0
- package/web/index.html +78 -0
- package/web/render-helpers.js +276 -0
- package/web/styles.css +318 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ZKiteLM
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Pi Trace Viewer
|
|
2
|
+
|
|
3
|
+
> Live local observability for Pi sessions, branches, compaction, and LLM context.
|
|
4
|
+
|
|
5
|
+
[中文 README](./README.zh.md)
|
|
6
|
+
|
|
7
|
+
Pi Trace Viewer answers a practical debugging question: **what did the model actually see?**
|
|
8
|
+
|
|
9
|
+
It runs as a Pi extension and opens a local browser UI where you can inspect the live session tree, model calls, tool activity, compaction inputs, and provider payloads without modifying Pi's native session files.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pi install npm:pi-trace-viewer
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Start a new Pi session and open the viewer:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
/trace-view
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The viewer starts at `http://127.0.0.1:7890` by default. If that port is busy, it automatically uses the next available port, such as `7891` or `7892`.
|
|
24
|
+
|
|
25
|
+
To try the extension for one Pi run without changing your settings:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pi -e npm:pi-trace-viewer
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To choose a custom starting port:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pi --pi-trace-port 8890
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## What You Can Inspect
|
|
38
|
+
|
|
39
|
+
- The live Pi session tree, including branches and active turns
|
|
40
|
+
- The normalized Pi context passed into `pi-ai`
|
|
41
|
+
- The provider-specific payload sent to the backend
|
|
42
|
+
- Streaming model output, tool calls, and tool results
|
|
43
|
+
- Compaction source messages, cut points, summaries, token metadata, and provider requests
|
|
44
|
+
- Custom messages and custom state created by other extensions
|
|
45
|
+
|
|
46
|
+
## Screenshots
|
|
47
|
+
|
|
48
|
+
### Realtime Session Viewer
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
### Session Viewer vs Pi Export
|
|
53
|
+
|
|
54
|
+
| Pi Trace Viewer | Pi `/export` |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
|  |  |
|
|
57
|
+
|
|
58
|
+
### Compaction Context
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
## Why Use It?
|
|
63
|
+
|
|
64
|
+
### Live Observation
|
|
65
|
+
|
|
66
|
+
Pi `/export` is useful after a conversation. Pi Trace Viewer updates while the session is running, so you can watch branches, tool calls, model output, and context changes as they happen.
|
|
67
|
+
|
|
68
|
+
### Pi Context vs Provider Payload
|
|
69
|
+
|
|
70
|
+
Every captured LLM call separates two views:
|
|
71
|
+
|
|
72
|
+
| View | What it shows |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| Pi Context | Normalized system prompt, messages, and tools handed to `pi-ai` |
|
|
75
|
+
| Provider Payload | Backend-specific request after provider mapping and templates |
|
|
76
|
+
|
|
77
|
+
Each message can be expanded independently, with rendered Markdown, raw Markdown, and full JSON available when you need them.
|
|
78
|
+
|
|
79
|
+
### Inspectable Compaction
|
|
80
|
+
|
|
81
|
+
Compaction events show the messages selected for summarization, split-turn prefixes, cut points, token counts, recent-message policy, previous summaries, extracted file operations, and the provider request used to generate the summary.
|
|
82
|
+
|
|
83
|
+
That makes context loss after compaction easier to trace.
|
|
84
|
+
|
|
85
|
+
### Long Sessions Stay Navigable
|
|
86
|
+
|
|
87
|
+
The session sidebar keeps single-child chains flat and only indents real branches. LLM calls are numbered chronologically and include call kind, turn, model, API, prompt excerpt, tool names, status, request count, and duration.
|
|
88
|
+
|
|
89
|
+
## How It Works
|
|
90
|
+
|
|
91
|
+
```mermaid
|
|
92
|
+
flowchart LR
|
|
93
|
+
A[Pi session] --> B[Pi extension hooks]
|
|
94
|
+
B --> C[In-memory viewer state]
|
|
95
|
+
C --> D[localhost browser UI]
|
|
96
|
+
B --> E[.pi-traces/session-id.jsonl]
|
|
97
|
+
D --> F[Session and LLM Calls]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The extension reads Pi's live snapshot and listens to public lifecycle events, including:
|
|
101
|
+
|
|
102
|
+
- `context`
|
|
103
|
+
- `before_provider_request`
|
|
104
|
+
- `after_provider_response`
|
|
105
|
+
- `message_update`
|
|
106
|
+
- `message_end`
|
|
107
|
+
- `session_before_compact`
|
|
108
|
+
- `session_compact`
|
|
109
|
+
- `session_tree`
|
|
110
|
+
|
|
111
|
+
## Data and Privacy
|
|
112
|
+
|
|
113
|
+
Pi Trace Viewer binds only to `127.0.0.1`.
|
|
114
|
+
|
|
115
|
+
It does not write to Pi's native session JSONL and does not call `appendCustomEntry` or `appendCustomMessageEntry`. Pi's native session remains in:
|
|
116
|
+
|
|
117
|
+
```text
|
|
118
|
+
~/.pi/agent/sessions/<encoded-cwd>/<session-id>.jsonl
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The extension writes sidecar traces to:
|
|
122
|
+
|
|
123
|
+
```text
|
|
124
|
+
<session-cwd>/.pi-traces/<session-id>.jsonl
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Trace directories use `0700` permissions and trace files use `0600` permissions. Credential-shaped fields and sensitive response headers are redacted, but prompts, system instructions, tool results, and model output can still contain sensitive data. Treat `.pi-traces` as private local debugging data.
|
|
128
|
+
|
|
129
|
+
To remove old traces:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
rm -rf "/path/to/session-cwd/.pi-traces"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Local Development
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npm install
|
|
139
|
+
npm run check
|
|
140
|
+
pi -e /path/to/pi-trace-viewer
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Publishing Checklist
|
|
144
|
+
|
|
145
|
+
Before publishing a new release:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
npm run check
|
|
149
|
+
npm pack --dry-run
|
|
150
|
+
npm publish --access public
|
|
151
|
+
```
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Pi Trace Viewer
|
|
2
|
+
|
|
3
|
+
> 面向 Pi 会话、分支、压缩和 LLM Context 的本地实时可观测 UI。
|
|
4
|
+
|
|
5
|
+
[English README](./README.md)
|
|
6
|
+
|
|
7
|
+
Pi Trace Viewer 解决一个很具体的问题:**模型到底看到了什么?**
|
|
8
|
+
|
|
9
|
+
它作为 Pi 扩展运行,并启动一个本地浏览器 UI。你可以在不修改 Pi 原生 session 文件的情况下,实时查看会话树、模型调用、工具活动、压缩输入,以及 provider 实际收到的 payload。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pi install npm:pi-trace-viewer
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
启动新的 Pi 会话后,打开 viewer:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
/trace-view
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Viewer 默认从 `http://127.0.0.1:7890` 启动。如果端口被占用,会自动递增寻找下一个可用端口,例如 `7891` 或 `7892`。
|
|
24
|
+
|
|
25
|
+
如果只想在单次 Pi 运行中临时启用,不修改设置:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pi -e npm:pi-trace-viewer
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
如需指定自定义起始端口:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pi --pi-trace-port 8890
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 可以查看什么
|
|
38
|
+
|
|
39
|
+
- 实时 Pi 会话树,包括分支和当前 turn
|
|
40
|
+
- 传给 `pi-ai` 的规范化 Pi Context
|
|
41
|
+
- 实际发送给后端的 provider-specific payload
|
|
42
|
+
- 模型流式输出、工具调用和工具结果
|
|
43
|
+
- Compaction 的源消息、截断点、summary、token 元数据和 provider 请求
|
|
44
|
+
- 其他扩展写入的 custom message 与 custom state
|
|
45
|
+
|
|
46
|
+
## 截图
|
|
47
|
+
|
|
48
|
+
### 实时 Session Viewer
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
### Session Viewer vs Pi Export
|
|
53
|
+
|
|
54
|
+
| Pi Trace Viewer | Pi `/export` |
|
|
55
|
+
| --- | --- |
|
|
56
|
+
|  |  |
|
|
57
|
+
|
|
58
|
+
### Compaction Context
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
## 为什么使用它?
|
|
63
|
+
|
|
64
|
+
### 实时观察
|
|
65
|
+
|
|
66
|
+
Pi `/export` 适合在对话结束后阅读完整历史。Pi Trace Viewer 会在会话运行时持续更新,让你边运行 Pi,边观察分支、工具调用、模型输出和 Context 变化。
|
|
67
|
+
|
|
68
|
+
### 区分 Pi Context 与 Provider Payload
|
|
69
|
+
|
|
70
|
+
每一次捕获到的 LLM 调用都会拆成两个视图:
|
|
71
|
+
|
|
72
|
+
| 视图 | 内容 |
|
|
73
|
+
| --- | --- |
|
|
74
|
+
| Pi Context | 传给 `pi-ai` 的规范化 system prompt、messages 和 tools |
|
|
75
|
+
| Provider Payload | 经过 provider mapping 和 templates 之后,实际发送给后端的请求 |
|
|
76
|
+
|
|
77
|
+
每条消息都可以独立展开,并支持渲染后的 Markdown、Markdown 原文和完整 JSON。
|
|
78
|
+
|
|
79
|
+
### 可解释的 Compaction
|
|
80
|
+
|
|
81
|
+
Compaction 事件会展示被选中用于总结的消息、split-turn prefix、截断点、token 数、最近消息保留策略、上一次 summary、提取到的文件操作信息,以及用于生成 summary 的 provider 请求。
|
|
82
|
+
|
|
83
|
+
这让“为什么压缩后模型忘了某件事”变成一个可以追溯的问题。
|
|
84
|
+
|
|
85
|
+
### 长会话也能保持可导航
|
|
86
|
+
|
|
87
|
+
Session 侧栏会把单子节点链保持扁平,只有真正发生分支时才缩进。LLM calls 按时间编号,并显示调用类型、turn、model、API、prompt 摘要、工具名、状态、请求次数和耗时。
|
|
88
|
+
|
|
89
|
+
## 工作方式
|
|
90
|
+
|
|
91
|
+
```mermaid
|
|
92
|
+
flowchart LR
|
|
93
|
+
A[Pi session] --> B[Pi extension hooks]
|
|
94
|
+
B --> C[In-memory viewer state]
|
|
95
|
+
C --> D[localhost browser UI]
|
|
96
|
+
B --> E[.pi-traces/session-id.jsonl]
|
|
97
|
+
D --> F[Session and LLM Calls]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
扩展读取 Pi 提供的 live snapshot,并监听公开生命周期事件,包括:
|
|
101
|
+
|
|
102
|
+
- `context`
|
|
103
|
+
- `before_provider_request`
|
|
104
|
+
- `after_provider_response`
|
|
105
|
+
- `message_update`
|
|
106
|
+
- `message_end`
|
|
107
|
+
- `session_before_compact`
|
|
108
|
+
- `session_compact`
|
|
109
|
+
- `session_tree`
|
|
110
|
+
|
|
111
|
+
## 数据与隐私
|
|
112
|
+
|
|
113
|
+
Pi Trace Viewer 只绑定 `127.0.0.1`。
|
|
114
|
+
|
|
115
|
+
它不会写入 Pi 原生 session JSONL,也不会调用 `appendCustomEntry` 或 `appendCustomMessageEntry`。Pi 原生 session 仍然位于:
|
|
116
|
+
|
|
117
|
+
```text
|
|
118
|
+
~/.pi/agent/sessions/<encoded-cwd>/<session-id>.jsonl
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
扩展会把旁车 trace 写入:
|
|
122
|
+
|
|
123
|
+
```text
|
|
124
|
+
<session-cwd>/.pi-traces/<session-id>.jsonl
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Trace 目录使用 `0700` 权限,trace 文件使用 `0600` 权限。credential-shaped 字段和敏感响应头会被脱敏,但 prompt、system instruction、工具结果和模型输出仍可能包含敏感信息。请把 `.pi-traces` 当作私有的本地调试数据。
|
|
128
|
+
|
|
129
|
+
清理旧 trace:
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
rm -rf "/path/to/session-cwd/.pi-traces"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 本地开发
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npm install
|
|
139
|
+
npm run check
|
|
140
|
+
pi -e /path/to/pi-trace-viewer
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## 发布检查
|
|
144
|
+
|
|
145
|
+
发布新版本前:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
npm run check
|
|
149
|
+
npm pack --dry-run
|
|
150
|
+
npm publish --access public
|
|
151
|
+
```
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-trace-viewer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Live local session and LLM context viewer for pi",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pi-package",
|
|
9
|
+
"pi-extension",
|
|
10
|
+
"llm-observability"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ZKiteLM/pi-trace-viewer.git"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/ZKiteLM/pi-trace-viewer#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/ZKiteLM/pi-trace-viewer/issues"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"assets/images/",
|
|
22
|
+
"src/",
|
|
23
|
+
"web/",
|
|
24
|
+
"README.zh.md",
|
|
25
|
+
"tsconfig.json"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"pi": {
|
|
31
|
+
"extensions": [
|
|
32
|
+
"./src/index.ts"
|
|
33
|
+
],
|
|
34
|
+
"video": "https://github.com/ZKiteLM/pi-trace-viewer/releases/download/v0.1.0/pi-trace-viewer-demo.mp4",
|
|
35
|
+
"image": "https://raw.githubusercontent.com/ZKiteLM/pi-trace-viewer/main/assets/images/realtime-session-viewport.png"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"check": "tsc --noEmit && vitest run",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@highlightjs/cdn-assets": "11.11.1",
|
|
44
|
+
"marked": "16.2.1"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@earendil-works/pi-agent-core": "*",
|
|
48
|
+
"@earendil-works/pi-ai": "*",
|
|
49
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@earendil-works/pi-agent-core": "0.84.3",
|
|
53
|
+
"@earendil-works/pi-ai": "0.84.3",
|
|
54
|
+
"@earendil-works/pi-coding-agent": "0.84.3",
|
|
55
|
+
"@types/node": "24.3.0",
|
|
56
|
+
"typescript": "5.9.2",
|
|
57
|
+
"vitest": "4.1.11"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=22.19.0"
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/collector.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
BeforeProviderRequestEvent,
|
|
4
|
+
ContextEvent,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
MessageEndEvent,
|
|
7
|
+
MessageUpdateEvent,
|
|
8
|
+
SessionCompactEvent,
|
|
9
|
+
SessionBeforeCompactEvent,
|
|
10
|
+
SessionTreeEvent,
|
|
11
|
+
ToolInfo,
|
|
12
|
+
} from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import type { AssistantMessageEvent, Tool } from "@earendil-works/pi-ai";
|
|
14
|
+
import { redactHeaders, redactSensitive } from "./security.ts";
|
|
15
|
+
import { TraceStore } from "./store.ts";
|
|
16
|
+
import type { CallKind, CompactAssistantEvent, SessionSnapshot } from "./types.ts";
|
|
17
|
+
import { toTraceModel } from "./types.ts";
|
|
18
|
+
|
|
19
|
+
interface ActiveCall {
|
|
20
|
+
id: string;
|
|
21
|
+
kind: CallKind;
|
|
22
|
+
attempt: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface AfterProviderResponseEvent {
|
|
26
|
+
type: "after_provider_response";
|
|
27
|
+
status: number;
|
|
28
|
+
headers: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class TraceCollector {
|
|
32
|
+
private activeCall: ActiveCall | undefined;
|
|
33
|
+
private nextKind: CallKind = "agent";
|
|
34
|
+
private turnIndex: number | undefined;
|
|
35
|
+
private systemPrompt = "";
|
|
36
|
+
private tools: ToolInfo[] = [];
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
private store: TraceStore,
|
|
40
|
+
private snapshot: () => SessionSnapshot,
|
|
41
|
+
) {}
|
|
42
|
+
|
|
43
|
+
setSystemPrompt(systemPrompt: string): void {
|
|
44
|
+
this.systemPrompt = systemPrompt;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setTools(tools: ToolInfo[]): void {
|
|
48
|
+
this.tools = structuredClone(tools);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
startTurn(turnIndex: number): void {
|
|
52
|
+
this.turnIndex = turnIndex;
|
|
53
|
+
this.nextKind = "agent";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
prepare(kind: Extract<CallKind, "compaction" | "branch_summary">, ctx?: ExtensionContext): void {
|
|
57
|
+
this.nextKind = kind;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
beginCompaction(event: SessionBeforeCompactEvent, ctx: ExtensionContext): void {
|
|
61
|
+
this.nextKind = "compaction";
|
|
62
|
+
if (this.activeCall) this.failActive("Superseded by context compaction");
|
|
63
|
+
const call = this.startCall("compaction", ctx);
|
|
64
|
+
const preparation = event.preparation;
|
|
65
|
+
this.store.append({
|
|
66
|
+
type: "compaction_context",
|
|
67
|
+
callId: call.id,
|
|
68
|
+
context: {
|
|
69
|
+
reason: event.reason,
|
|
70
|
+
willRetry: event.willRetry,
|
|
71
|
+
customInstructions: event.customInstructions,
|
|
72
|
+
firstKeptEntryId: preparation.firstKeptEntryId,
|
|
73
|
+
isSplitTurn: preparation.isSplitTurn,
|
|
74
|
+
tokensBefore: preparation.tokensBefore,
|
|
75
|
+
previousSummary: preparation.previousSummary,
|
|
76
|
+
messagesToSummarize: structuredClone(preparation.messagesToSummarize),
|
|
77
|
+
turnPrefixMessages: structuredClone(preparation.turnPrefixMessages),
|
|
78
|
+
settings: structuredClone(preparation.settings),
|
|
79
|
+
fileOps: {
|
|
80
|
+
read: [...preparation.fileOps.read],
|
|
81
|
+
written: [...preparation.fileOps.written],
|
|
82
|
+
edited: [...preparation.fileOps.edited],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
onContext(event: ContextEvent, ctx: ExtensionContext): void {
|
|
89
|
+
if (this.activeCall) this.failActive("Superseded by the next LLM context");
|
|
90
|
+
this.nextKind = "agent";
|
|
91
|
+
const call = this.startCall("agent", ctx);
|
|
92
|
+
const activeNames = new Set(this.tools.map((tool) => tool.name));
|
|
93
|
+
const tools: Tool[] = this.tools
|
|
94
|
+
.filter((tool) => activeNames.has(tool.name))
|
|
95
|
+
.map((tool) => ({ name: tool.name, description: tool.description, parameters: tool.parameters }));
|
|
96
|
+
this.store.append({
|
|
97
|
+
type: "generic_context",
|
|
98
|
+
callId: call.id,
|
|
99
|
+
context: {
|
|
100
|
+
systemPrompt: ctx.getSystemPrompt() || this.systemPrompt,
|
|
101
|
+
messages: structuredClone(event.messages),
|
|
102
|
+
tools,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
onProviderRequest(event: BeforeProviderRequestEvent, ctx: ExtensionContext): void {
|
|
108
|
+
const call = this.activeCall ?? this.startCall(this.nextKind === "agent" ? "unknown" : this.nextKind, ctx);
|
|
109
|
+
call.attempt += 1;
|
|
110
|
+
this.store.append({
|
|
111
|
+
type: "provider_request",
|
|
112
|
+
callId: call.id,
|
|
113
|
+
attempt: call.attempt,
|
|
114
|
+
payload: redactSensitive(structuredClone(event.payload)),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
onProviderResponse(event: AfterProviderResponseEvent, ctx: ExtensionContext): void {
|
|
119
|
+
const call = this.activeCall ?? this.startCall(this.nextKind === "agent" ? "unknown" : this.nextKind, ctx);
|
|
120
|
+
this.store.append({
|
|
121
|
+
type: "provider_response",
|
|
122
|
+
callId: call.id,
|
|
123
|
+
attempt: Math.max(call.attempt, 1),
|
|
124
|
+
status: event.status,
|
|
125
|
+
headers: redactHeaders(event.headers),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
onMessageUpdate(event: MessageUpdateEvent): void {
|
|
130
|
+
if (!this.activeCall || this.activeCall.kind !== "agent") return;
|
|
131
|
+
this.store.append({
|
|
132
|
+
type: "output_event",
|
|
133
|
+
callId: this.activeCall.id,
|
|
134
|
+
event: compactEvent(event.assistantMessageEvent),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
onMessageEnd(event: MessageEndEvent): void {
|
|
139
|
+
if (event.message.role !== "assistant" || !this.activeCall || this.activeCall.kind !== "agent") return;
|
|
140
|
+
const snapshot = this.snapshot();
|
|
141
|
+
if (event.message.stopReason === "error" || event.message.stopReason === "aborted") {
|
|
142
|
+
this.store.append({
|
|
143
|
+
type: "call_failed",
|
|
144
|
+
callId: this.activeCall.id,
|
|
145
|
+
error: event.message.errorMessage ?? event.message.stopReason,
|
|
146
|
+
});
|
|
147
|
+
} else {
|
|
148
|
+
this.store.append({
|
|
149
|
+
type: "call_completed",
|
|
150
|
+
callId: this.activeCall.id,
|
|
151
|
+
message: structuredClone(event.message),
|
|
152
|
+
leafId: snapshot.leafId,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
this.activeCall = undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
onCompaction(event: SessionCompactEvent): void {
|
|
159
|
+
if (!this.activeCall || this.activeCall.kind !== "compaction") return;
|
|
160
|
+
this.store.append({
|
|
161
|
+
type: "compaction_completed",
|
|
162
|
+
callId: this.activeCall.id,
|
|
163
|
+
summary: event.compactionEntry.summary,
|
|
164
|
+
tokensBefore: event.compactionEntry.tokensBefore,
|
|
165
|
+
sourceEntryId: event.compactionEntry.id,
|
|
166
|
+
});
|
|
167
|
+
this.activeCall = undefined;
|
|
168
|
+
this.nextKind = "agent";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
onTree(event: SessionTreeEvent): void {
|
|
172
|
+
this.nextKind = "agent";
|
|
173
|
+
if (!this.activeCall || this.activeCall.kind !== "branch_summary") return;
|
|
174
|
+
if (!event.summaryEntry) {
|
|
175
|
+
this.failActive("Tree navigation completed without a generated summary");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this.store.append({
|
|
179
|
+
type: "branch_summary_completed",
|
|
180
|
+
callId: this.activeCall.id,
|
|
181
|
+
summary: event.summaryEntry.summary,
|
|
182
|
+
leafId: event.newLeafId,
|
|
183
|
+
});
|
|
184
|
+
this.activeCall = undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
onCompactionFailed(error: string): void {
|
|
188
|
+
this.nextKind = "agent";
|
|
189
|
+
if (this.activeCall?.kind === "compaction") this.failActive(error);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private startCall(kind: CallKind, ctx: ExtensionContext): ActiveCall {
|
|
193
|
+
const snapshot = this.snapshot();
|
|
194
|
+
const call: ActiveCall = { id: randomUUID(), kind, attempt: 0 };
|
|
195
|
+
this.activeCall = call;
|
|
196
|
+
this.store.append({
|
|
197
|
+
type: "call_started",
|
|
198
|
+
callId: call.id,
|
|
199
|
+
kind,
|
|
200
|
+
turnIndex: this.turnIndex,
|
|
201
|
+
leafId: snapshot.leafId,
|
|
202
|
+
model: toTraceModel(ctx.model),
|
|
203
|
+
});
|
|
204
|
+
return call;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private failActive(error: string): void {
|
|
208
|
+
if (!this.activeCall) return;
|
|
209
|
+
this.store.append({ type: "call_failed", callId: this.activeCall.id, error });
|
|
210
|
+
this.activeCall = undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function compactEvent(event: AssistantMessageEvent): CompactAssistantEvent {
|
|
215
|
+
const copy = structuredClone(event) as AssistantMessageEvent & { partial?: unknown };
|
|
216
|
+
delete copy.partial;
|
|
217
|
+
return copy;
|
|
218
|
+
}
|