pi-openai-toolkit 0.14.2 → 0.14.4
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/README.md +239 -246
- package/README.zh.md +243 -246
- package/docs/internals.md +73 -0
- package/extensions/auto-mode.ts +1 -1
- package/extensions/codex-astra.ts +1 -1
- package/extensions/compaction.ts +1 -1
- package/extensions/image-generation.ts +1 -1
- package/extensions/web-search.ts +1 -1
- package/package.json +2 -1
- package/src/config.ts +10 -0
- package/src/context-management/codex-provider.ts +12 -14
- package/src/context-management/history-notes.ts +8 -2
- package/src/context-management/tools.ts +5 -3
- package/src/context-management/types.ts +2 -1
- package/src/context-management/window-manager.ts +479 -421
- package/src/extension-runtime.ts +9 -5
- package/src/types.ts +7 -0
- package/src/web-search/prompt.ts +1 -1
package/README.zh.md
CHANGED
|
@@ -1,246 +1,243 @@
|
|
|
1
|
-
# pi-openai-toolkit
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/pi-openai-toolkit)
|
|
4
|
-
[](./LICENSE)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
| 键 | 类型 /
|
|
149
|
-
| --- | --- | --- |
|
|
150
|
-
| `enabled` | *boolean*, `
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
|
155
|
-
|
|
|
156
|
-
| `enabled` | *boolean*, `true` |
|
|
157
|
-
| `
|
|
158
|
-
| `
|
|
159
|
-
| `
|
|
160
|
-
| `
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
## 许可证
|
|
245
|
-
|
|
246
|
-
MIT © awoaCrim and contributors。见 [LICENSE](./LICENSE),第三方归属见 [NOTICE](./NOTICE)。
|
|
1
|
+
# pi-openai-toolkit
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/pi-openai-toolkit)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
面向 Pi 的 OpenAI 工具包:无损长会话上下文管理、托管联网搜索、图像生成与带审批门禁的 Auto 模式。
|
|
8
|
+
|
|
9
|
+
Pi 内置压缩把较早的轮次摘要为文本,细节永久丢失。OpenAI Codex 后端用一整套通用客户端从未获得的机制解决这个问题:上下文窗口体系(`new_context`、`history`、`notes`)、加密无损压缩、托管工具。本包通过对接真实后端协议,把这些 Codex 能力移植进 Pi;Auto 模式则是本包自有功能。需要说明:覆盖模型会话中出现的窗口工具是 Codex 的机制、由本包接通,并非 Pi 或本包独立发明的能力。
|
|
10
|
+
|
|
11
|
+
## 特性
|
|
12
|
+
|
|
13
|
+
- **上下文窗口**(Codex 机制移植)- `new_context` 切换新窗口,全程不重摘要
|
|
14
|
+
- **跨窗口回读** - `history` 搜索、阅读更早窗口的原始内容
|
|
15
|
+
- **工作记忆笔记** - `notes` 持久化存档,作为每次换窗的门禁
|
|
16
|
+
- **加密压缩 v2** - OpenAI 后端压缩能力,为 Responses 模型接通
|
|
17
|
+
- **托管联网搜索** - OpenAI Responses 原生 `web_search`,对列入的模型替换 Pi 本地工具
|
|
18
|
+
- **图像生成** - 托管 `image_generation`(gpt-image-2)的 Pi 封装,图像字节不入会话历史
|
|
19
|
+
- **Auto 模式** - 本包自有功能:副作用工具调用的审查模型门禁
|
|
20
|
+
- **安全承诺** - 覆盖模型上杜绝有损摘要,凭据不出本机
|
|
21
|
+
|
|
22
|
+
需要 Pi >= 0.85.1、Node.js >= 22.19.0。本包不注册任何 provider 或模型,全部经由 Pi 已有的目录、登录与会话存储工作。不要与 `pi-remote-compact`、`@lll9p/pi-better-compaction` 或独立的 `pi-openai-web-search` 同时运行:重复 hook 会导致竞态。
|
|
23
|
+
|
|
24
|
+
## 安装
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pi install npm:pi-openai-toolkit
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
仅安装到当前项目:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pi install npm:pi-openai-toolkit --local
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
从 git 安装(开发或锁定构建):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pi install git:github.com/awoaCrim/pi-openai-toolkit
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
后续用 `pi update --extensions` 更新。
|
|
43
|
+
|
|
44
|
+
## 快速开始
|
|
45
|
+
|
|
46
|
+
**1.** 创建 `~/.pi/agent/extensions/pi-openai-toolkit/config.json`(Windows:`C:\Users\<user>\.pi\agent\extensions\pi-openai-toolkit\config.json`):
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"compaction": {
|
|
51
|
+
"contextManagement": "remote",
|
|
52
|
+
"contextReminderThresholdPercent": 10
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**2.** 在覆盖的模型上启动 Pi:Pi 内置 Codex provider(`openai-codex`)上的任意会话,或已列入 `gatewayContextModels` 的网关模型:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pi --model uwoacrimson/gpt-6-astra
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**3.** 确认生效:会话内出现四个 Codex 上下文工具(`new_context`、`get_context_remaining`、`history`、`notes`)。
|
|
64
|
+
|
|
65
|
+
窗口余量低于提醒阈值时,模型收到一次提醒:存档笔记并换窗。更早的内容始终可通过 `history` 搜索。没有配置文件时,Pi 按默认策略运行,所有可选功能保持关闭。
|
|
66
|
+
|
|
67
|
+
网关覆盖是一份显式白名单:`compaction.gatewayContextModels` 逐条列出 `provider/model` 精确键,模型须走 `openai-responses` 线路,且你的网关确实透传 Codex 窗口标记。若 Pi 中尚未配置该网关,在 `~/.pi/agent/models.json` 添加:
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"providers": {
|
|
72
|
+
"uwoacrimson": {
|
|
73
|
+
"baseUrl": "https://<your-gateway>/v1",
|
|
74
|
+
"apiKeyRef": "uwoacrimson",
|
|
75
|
+
"api": "openai",
|
|
76
|
+
"models": [{
|
|
77
|
+
"id": "gpt-6-astra",
|
|
78
|
+
"name": "GPT-6 Astra",
|
|
79
|
+
"api": "openai-responses",
|
|
80
|
+
"reasoning": true,
|
|
81
|
+
"input": ["text"],
|
|
82
|
+
"contextWindow": 272000,
|
|
83
|
+
"maxTokens": 16384,
|
|
84
|
+
"compat": {"thinkingLevelMap": {"off": 0, "minimal": 0.1, "low": 0.2, "medium": 0.5, "high": 1, "xhigh": 2, "max": 2}}
|
|
85
|
+
}]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 用法
|
|
92
|
+
|
|
93
|
+
### Codex Remote Context
|
|
94
|
+
|
|
95
|
+
这是 Codex 自身的上下文窗口机制,由本包为 Pi 实现。覆盖模型的会话按 Codex 客户端的方式运行:模型在上下文窗口中工作,用 `notes` 存档状态,在窗口装满前调用 `new_context`。不发生任何摘要,更早窗口通过 `history` 完整可读。本窗口没有成功笔记存档时换窗会被拒绝;resume 与 fork 后规则不变。提醒与窗口耗尽消息只向模型建议,换窗始终由模型自己调用 `new_context` 完成。
|
|
96
|
+
|
|
97
|
+
覆盖模型的压缩路径由本包独占:Pi 原生压缩全部取消,有损摘要无法进入会话。Remote Context 无法激活时,以通知报告确切原因。
|
|
98
|
+
|
|
99
|
+
生命周期机制与协议契约:[docs/internals.md](docs/internals.md#rollover-lifecycle)。
|
|
100
|
+
|
|
101
|
+
### Remote Compaction v2
|
|
102
|
+
|
|
103
|
+
适用于其他 Responses 模型:压缩触发时,本包请求 OpenAI 后端的压缩能力为旧前缀生成加密检查点,而非由文本摘要顶替;检查点由服务端计算,客户端只负责存储与回放。后续请求将其回放到较近轮次之前。远端尝试失败时回退到 Pi 原生压缩(`nativeFallback`)。线路契约:[docs/internals.md](docs/internals.md#remote-compaction-v2-wire-contract)。
|
|
104
|
+
|
|
105
|
+
### 联网搜索
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"webSearch": { "enabled": true, "models": ["uwoacrimson/gpt-5.6-luna"] }
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
列入的模型获得 OpenAI 托管 `web_search`,其本地 `web_search` 工具从载荷移除。复用当前模型连接,无需额外 key。
|
|
114
|
+
|
|
115
|
+
### 图像生成
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"imageGeneration": { "enabled": true }
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
为所有 Responses 协议会话添加 `openai_generate_image`,封装 OpenAI 托管 `image_generation` 工具(`gpt-image-2`):文生图,以及显式传入本地参考文件的编辑。图像字节以 artifact 存储而非进入会话历史。每次成功请求都可能产生费用,默认关闭。
|
|
124
|
+
|
|
125
|
+
### Auto 模式
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"autoMode": {
|
|
130
|
+
"enabled": true,
|
|
131
|
+
"models": ["uwoacrimson/gpt-5.6-luna"],
|
|
132
|
+
"reviewerModel": "uwoacrimson/gpt-5.6-luna"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
会话内用 `/auto on` 启用。副作用工具调用由 `reviewerModel` 批准(固有风险 + 对话授权),不再阻塞等用户。审查员仅有只读工具,超时不视为批准,无头会话一律阻断而非猜测。每条决定落盘可审计。
|
|
138
|
+
|
|
139
|
+
## 配置
|
|
140
|
+
|
|
141
|
+
配置文件唯一:`~/.pi/agent/extensions/pi-openai-toolkit/config.json`。所有键可选,未知键告警忽略。`models` 键是精确 `provider/model-id` 白名单(不支持通配),列入前功能保持关闭。
|
|
142
|
+
|
|
143
|
+
<details>
|
|
144
|
+
<summary><b>完整选项参考</b>(compaction、webSearch、imageGeneration、autoMode)</summary>
|
|
145
|
+
|
|
146
|
+
### `compaction`
|
|
147
|
+
|
|
148
|
+
| 键 | 类型 / 默认值 | 含义 |
|
|
149
|
+
| --- | --- | --- |
|
|
150
|
+
| `enabled` | *boolean*, `true` | 压缩扩展总开关。 |
|
|
151
|
+
| `contextManagement` | `"off"` \| `"remote"`, `"off"` | 为覆盖模型开启 Codex Remote Context:`openai-codex-responses` API 上的任意 `openai-codex` 会话,加上下列白名单中的网关模型。 |
|
|
152
|
+
| `gatewayContextModels` | *string[]*, `[]` | 允许经 `openai-responses` 网关线路使用 Codex Remote Context 的 `provider/model` 精确键。覆盖范围由使用者配置;原生 Codex 路线不看此列表。 |
|
|
153
|
+
| `contextReminderThresholdPercent` | *integer* `0`-`100`, `5` | 当剩余量低于可用预算(上下文窗口减去固定 16384 token 输出预留)的该百分比时,注入每窗口一次的 checkpoint 提醒。`0` 会连同窗口耗尽兜底一起关闭(当前实现如此),请保持大于 0。该阈值度量本包自己的预算,与 Pi 原生预留为固定数值配合,并非运行时动态联动。 |
|
|
154
|
+
| `allowCompactionContinuityBreak` | *boolean*, `false` | v2:最近一次压缩来自其他策略(如安装前的文本摘要)时,重建全新密文链。 |
|
|
155
|
+
| `remoteCompactModel` | *string \| null*, `null` | v2:仅用于服务端压缩请求的模型,会话模型不切换。必须与当前模型同 base URL。 |
|
|
156
|
+
| `nativeFallback.enabled` | *boolean*, `true` | 仅在远端压缩确实失败后回退到原生压缩。 |
|
|
157
|
+
| `nativeFallback.model` | *string \| null*, `null` | 完全无法使用 v2 的模型的摘要模型。`null` 沿用当前模型。 |
|
|
158
|
+
| `nativeFallback.thinkingLevel` | *string*, `"off"` | 回退摘要的 thinking level(`off`...`max`)。 |
|
|
159
|
+
| `responsesApis` | *string[]*, 两个 API | 允许使用 v2 压缩的 Responses API 标识。v2 只作用于未被 Remote Context 覆盖的模型。 |
|
|
160
|
+
| `notifyOnLoad` | *boolean*, `false` | 启动横幅。 |
|
|
161
|
+
| `debug` | *boolean*, `false` | 向 `artifactRoot` 写生命周期与压缩 artifact,含每次 `session_start` 的激活原因。 |
|
|
162
|
+
| `logProviderPayloads` / `logCompactResponses` | *boolean*, `false` | 额外写原始载荷与 SSE 响应体,排查特定请求时再开。 |
|
|
163
|
+
| `redactSensitiveData` | *boolean*, `true` | artifact 脱敏;凭据、account ID、密文永远脱敏。 |
|
|
164
|
+
| `artifactRoot` | *string*, `~/.pi/agent/artifacts/pi-openai-toolkit/compaction` | artifact 根目录;相对路径按配置目录解析。 |
|
|
165
|
+
|
|
166
|
+
### `webSearch`
|
|
167
|
+
|
|
168
|
+
| 键 | 类型 / 默认值 | 含义 |
|
|
169
|
+
| --- | --- | --- |
|
|
170
|
+
| `enabled` | *boolean*, `true` | 总开关。 |
|
|
171
|
+
| `models` | *string[]*, `[]` | 接收 OpenAI 托管 `web_search` 的会话:本地 `web_search` 工具被移除,原生 Responses API 工具被注入。 |
|
|
172
|
+
|
|
173
|
+
### `imageGeneration`
|
|
174
|
+
|
|
175
|
+
| 键 | 类型 / 默认值 | 含义 |
|
|
176
|
+
| --- | --- | --- |
|
|
177
|
+
| `enabled` | *boolean*, `false` | 向所有 Responses 协议会话开放 `openai_generate_image`。有意不设模型白名单。 |
|
|
178
|
+
|
|
179
|
+
### `autoMode`
|
|
180
|
+
|
|
181
|
+
| 键 | 类型 / 默认值 | 含义 |
|
|
182
|
+
| --- | --- | --- |
|
|
183
|
+
| `enabled` | *boolean*, `true` | 总开关;启用模式仍需列入模型并设置 `reviewerModel`。 |
|
|
184
|
+
| `models` | *string[]*, `[]` | 允许运行 Auto 模式的会话模型。 |
|
|
185
|
+
| `reviewerModel` | *string \| null*, `null` | 一次性审查的审批模型,绝不成为对话模型。 |
|
|
186
|
+
| `gate` | `"side-effect"` \| `"all"`, `"side-effect"` | 审查范围;`/auto all` 按会话切换。 |
|
|
187
|
+
| `extraTools` | *string[]*, `[]` | 在 `bash`、`write`、`edit` 之外追加审查的工具。 |
|
|
188
|
+
| `timeoutMs` | *integer* 1000-120000, `30000` | 审查总时限。超时不视为批准。 |
|
|
189
|
+
| `transcript` | *boolean*, `true` | 提供有界对话视图,供审查员判断授权情况。 |
|
|
190
|
+
| `evidenceTools` | *boolean*, `true` | 审查员可用 `read`、`grep`、`find`、`ls`;永远没有 `bash`、写工具与网络。 |
|
|
191
|
+
| `maxEvidenceRounds` | *integer* 0-8, `3` | 出结论前的调查轮数;`0` 为单次补全。 |
|
|
192
|
+
| `classifier.*` | *object* | 可选非阻塞低风险预打分器,只放行、不否决。 |
|
|
193
|
+
| `circuitBreaker.*` | *object* | 连续否决结束回合(`consecutiveDenials` `3`,或 `windowSize` `50` 内 `recentDenials` `10`;`0` 关闭)。 |
|
|
194
|
+
|
|
195
|
+
### `codexAstra`
|
|
196
|
+
|
|
197
|
+
已移除的配置段。Astra 兼容层对所有 Responses 系 API 的 `gpt-6-astra` 会话静默启用;旧 `codexAstra` 配置按未知字段告警忽略。若其他宿主(如 Oh My Pi)实现了同款改写,只启用一边。
|
|
198
|
+
|
|
199
|
+
</details>
|
|
200
|
+
|
|
201
|
+
## 安全
|
|
202
|
+
|
|
203
|
+
- 覆盖模型被 Remote Context 接管期间,Pi 原生压缩全部路径(阈值、手动 `/compact`、溢出)取消,无法产生有损摘要。
|
|
204
|
+
- 网关模式只转发配置的数据面 API key。OAuth token、account cookie、account ID 绝不发给网关,artifact 中永远脱敏。
|
|
205
|
+
- `new_context` 无法丢弃未保存状态:必须有本窗口成功的 notes 存档,或用户显式 `force`。
|
|
206
|
+
- 图像生成(计费)与 Auto 模式均需显式开启;无人可批准的场景下 Auto 模式阻断。
|
|
207
|
+
|
|
208
|
+
需要宿主层保险时,在 Pi 的 `settings.json` 设置 `"compaction": { "enabled": false }`。
|
|
209
|
+
|
|
210
|
+
## 开发
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
npm run typecheck # tsc 项目检查
|
|
214
|
+
bun test # 单元测试
|
|
215
|
+
npm run test:pi # Pi 集成冒烟测试
|
|
216
|
+
npm pack --dry-run # 检查发布包内容
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## 文档
|
|
220
|
+
|
|
221
|
+
- [docs/internals.md](docs/internals.md) - 协议端点、换窗生命周期、v2 线路契约、artifact、来源归属
|
|
222
|
+
- [README.md](README.md) - 英文原版(canonical)
|
|
223
|
+
- [NOTICE](./NOTICE) - 第三方归属
|
|
224
|
+
|
|
225
|
+
## 贡献
|
|
226
|
+
|
|
227
|
+
欢迎在 [issue 区](https://github.com/awoaCrim/pi-openai-toolkit/issues)提问和提交 PR。行为变更前先开 issue 讨论协议假设;提交前运行 `bun test` 与 `npm run typecheck`,线路变更需要 fixture 测试。
|
|
228
|
+
|
|
229
|
+
## 维护者
|
|
230
|
+
|
|
231
|
+
[awoaCrim](https://github.com/awoaCrim)
|
|
232
|
+
|
|
233
|
+
## 许可证
|
|
234
|
+
|
|
235
|
+
MIT © awoaCrim and contributors。见 [LICENSE](./LICENSE) 与 [NOTICE](./NOTICE)。
|
|
236
|
+
|
|
237
|
+
## 致谢
|
|
238
|
+
|
|
239
|
+
感谢 [Linux.do](https://linux.do/) 社区的支持与讨论。
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
*本文译自 [README.md](README.md)。若两版内容冲突,以英文版为准。*
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Toolkit Internals
|
|
2
|
+
|
|
3
|
+
Developer-facing reference for [pi-openai-toolkit](../README.md). This is the protocol and implementation detail that the README deliberately keeps out of the user's way. If you are only installing the toolkit, you never need this page; if you are changing wire behavior, fixtures, or the compaction strategy code, start here.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Remote Context protocol](#remote-context-protocol)
|
|
8
|
+
- [Rollover lifecycle](#rollover-lifecycle)
|
|
9
|
+
- [Remote Compaction v2 wire contract](#remote-compaction-v2-wire-contract)
|
|
10
|
+
- [Artifacts and debugging](#artifacts-and-debugging)
|
|
11
|
+
- [Provenance](#provenance)
|
|
12
|
+
|
|
13
|
+
## Remote Context protocol
|
|
14
|
+
|
|
15
|
+
Internal alpha endpoints, adapted from `@howaboua/pi-codex-conversion`:
|
|
16
|
+
|
|
17
|
+
```text
|
|
18
|
+
POST {base}/alpha/history/v2/list_windows | list_items | read_item | search_contents
|
|
19
|
+
POST {base}/alpha/notes/v2/list_files_by_prefix | read_file | search_contents | append_to_file | write_file
|
|
20
|
+
GET {base}/alpha/notes/v2/thread_hint
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Native route uses `{base} = <backend-api>/codex` with `Authorization: Bearer <oauth token>`, `ChatGPT-Account-ID`, and Codex CLI headers. Gateway route uses the configured `/v1` base with the data-plane API key, `originator: codex_cli_rs`, `X-Codex-Affinity-Scope`, and `X-Codex-Model` carrying the bare model id, never local credentials. Both set `Session-Id` / `X-Client-Request-Id` to the bounded Pi session id, `x-openai-tool-output-truncation-policy`, and `x-openai-encrypted-tool-arguments` for endpoints with encrypted parameters; encrypted results come back as `encrypted_output` and are re-encoded into later requests. These are alpha contracts: the upstream backend may change them at any time.
|
|
24
|
+
|
|
25
|
+
Search text and note bodies are sent under the encrypted-argument policy. Live model requests carry `x-codex-window-id` and `x-codex-turn-metadata` request headers derived from the active window identity.
|
|
26
|
+
|
|
27
|
+
Coverage is never inferred from model names. A native session is `provider: "openai-codex"` on api `openai-codex-responses` (any model id). A gateway session must be on api `openai-responses` and carry an exact `provider/model` key listed in `compaction.gatewayContextModels`; the list ships empty and there is no built-in gateway SKU ([src/context-management/codex-provider.ts](../src/context-management/codex-provider.ts)). Unlisted gateway models, listed or not, keep running remote compaction v2 when Remote Context declines them. The toolkit registers no provider or model with Pi; it resolves credentials and base URLs through Pi's `ModelRegistry`.
|
|
28
|
+
|
|
29
|
+
## Rollover lifecycle
|
|
30
|
+
|
|
31
|
+
The `new_context` happy path, end to end:
|
|
32
|
+
|
|
33
|
+
```mermaid
|
|
34
|
+
sequenceDiagram
|
|
35
|
+
participant Model
|
|
36
|
+
participant Toolkit
|
|
37
|
+
participant Pi
|
|
38
|
+
participant Backend
|
|
39
|
+
Model->>Toolkit: notes append_to_file (checkpoint)
|
|
40
|
+
Toolkit->>Backend: alpha notes request (encrypted args)
|
|
41
|
+
Backend-->>Toolkit: encrypted_output
|
|
42
|
+
Model->>Toolkit: new_context
|
|
43
|
+
Toolkit->>Toolkit: verify successful notes write in this window
|
|
44
|
+
Toolkit-->>Pi: window marker appended, previous window trim scheduled
|
|
45
|
+
Model->>Backend: next request with the new x-codex-window-id
|
|
46
|
+
Pi->>Toolkit: session_before_compact
|
|
47
|
+
Toolkit-->>Pi: no-summary boundary consuming the scheduled trim
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
State invariants the lifecycle code must keep honest:
|
|
51
|
+
|
|
52
|
+
- The checkpoint gate verifies a *successful* `notes` `append_to_file` / `write_file` against the persisted session branch after the latest window boundary, so it survives restarts and forks.
|
|
53
|
+
- Budget checks are skipped until the current window has produced its own assistant usage; acting on the previous window's usage anchor burns the once-per-window reminder on a false alarm.
|
|
54
|
+
- The scheduled trim is consumed exactly once, synchronously, by the first compaction attempt whose boundary window id matches. Every other compaction path (threshold without a scheduled rollover, manual `/compact`, overflow) is cancelled.
|
|
55
|
+
- A window boundary is persisted as a `codex-context-window` custom message; `session_start` replays boundaries from the branch and rebuilds identity after forks.
|
|
56
|
+
|
|
57
|
+
## Remote Compaction v2 wire contract
|
|
58
|
+
|
|
59
|
+
A `compaction_trigger` item appended to the live streaming request yields one output item of `type: "compaction"` with non-empty `encrypted_content`, stored in `CompactionEntry.details.compactedWindow`. On later requests the opaque checkpoint is replayed ahead of live turns: zero-loss, no text summary. Replay fails closed: if the summary anchor cannot be located, the request is aborted with a notification and a content-free failure artifact; the sentinel-only payload is never sent. A v2 response with a missing or empty checkpoint is never stored, and the `nativeFallback` tier is skipped: Pi's own threshold drives the next attempt, which may use `remoteCompactModel` when configured. `remoteCompactModel` must resolve to the same effective base URL as the active model.
|
|
60
|
+
|
|
61
|
+
## Artifacts and debugging
|
|
62
|
+
|
|
63
|
+
With `debug: true` the toolkit writes lifecycle and compaction artifacts under `artifactRoot` (default `~/.pi/agent/artifacts/pi-openai-toolkit/compaction`):
|
|
64
|
+
|
|
65
|
+
- each `session_start` writes a lifecycle artifact whose `activation` field records `active`, or the exact inactive reason (`tool-name-conflict`, `missing-api-key`, `auth-resolution-failed`, `model-not-covered`, ...);
|
|
66
|
+
- `logProviderPayloads` additionally writes raw provider request payloads, `logCompactResponses` the compact SSE bodies; keep both off unless inspecting a specific request;
|
|
67
|
+
- artifacts always redact Authorization credentials, API keys/tokens, Codex account ids, and opaque `encrypted_content` / `encrypted_output`, even with `redactSensitiveData: false`.
|
|
68
|
+
|
|
69
|
+
## Provenance
|
|
70
|
+
|
|
71
|
+
The Remote Context protocol was reverse-engineered from `@howaboua/pi-codex-conversion@3.0.29` (source commit `7021ae48e8efe36a3becc5830d529696ff798e5e`). The Astra compatibility layer is adapted from Oh My Pi 18.1.8 (MIT). Web Search behavior was adapted from [`pi-openai-web-search`](https://github.com/code-yeongyu/pi-openai-web-search) (commit `3964338`). Attribution details are in [NOTICE](../NOTICE).
|
|
72
|
+
|
|
73
|
+
Protocol fixtures live in [src/context-management/](../src/context-management/) and must be updated together with any upstream alpha endpoint change; fixture-based tests are required for new wire behavior.
|
package/extensions/auto-mode.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from "../src/auto-mode/extension.ts";
|
|
1
|
+
export { default } from "../src/auto-mode/extension.ts";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from "../src/codex-astra/extension.ts";
|
|
1
|
+
export { default } from "../src/codex-astra/extension.ts";
|
package/extensions/compaction.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from "../src/extension-runtime.ts";
|
|
1
|
+
export { default } from "../src/extension-runtime.ts";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from "../src/image-generation/extension.ts";
|
|
1
|
+
export { default } from "../src/image-generation/extension.ts";
|
package/extensions/web-search.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from "../src/web-search/extension.ts";
|
|
1
|
+
export { default } from "../src/web-search/extension.ts";
|