@zhushanwen/pi-smart-context 0.4.4 → 0.5.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/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  - cross-model:调用 pi 原生 `compact()` 仅换模型与凭证——split-turn / fileOps / previousSummary 原生组装
11
11
  - **3 档阈值提醒**(默认 400K/500K/600K):`agent_settled` 越档检查,每档一次、多档合并、压缩后重置,nextTurn 投递(随下一次 prompt 注入,不自起 run);已提醒档位经 `pi.appendEntry` 落在 session entries(`smart-context:fired`),reload / 进程重启后从 entries 重建,同一档不会重复提醒;措辞是数据投递不是指令
12
12
  - **排除模型**(精准 `provider/modelId` 匹配):工具拒绝 + 不提醒 + 回落 pi 原生生成;切换跨界时注入一条可用性通知,downshift(切小窗模型将触线)时建议先压缩
13
- - **通知投递路径**:压缩结果 / 阈值提醒 / 模型切换四类通知统一走 nextTurn(custom role 随下次 prompt 注入,不自起 run)——工具压缩掐断的 turn 由 taiji runtime 在 `compaction_end` 判定后经投递内核追加续跑投递
13
+ - **通知投递路径**:压缩结果 / 阈值提醒 / 模型切换四类通知统一走 nextTurn(custom role 随下次 prompt 注入,不自起 run);手动触发失败回执(`__taiji_compact__` 命令的启动失败 / 进行中幂等回执)走即时投递(空闲即达,不进 pending 队列)。后台压缩全程不掐 turn(手动压缩不经 pi 同步 compact 链),无需续跑兜底
14
14
  - **健壮性**:摘要收缩校验、max-tokens 截断 fail-closed、接管失败 3 次熔断(本 session 内停止接管)、transcript 回查指针、压缩后最近文件内容重注入(≤5 文件/50K)、多轮压缩降智提示(累计压缩 ≥2 次后附加)
15
15
  - **subagent 进程**自动静默(`TAIJI_AGENT_SUBAGENT` 标记)
16
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhushanwen/pi-smart-context",
3
- "version": "0.4.4",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "taiji": {
@@ -19,7 +19,8 @@
19
19
  500000,
20
20
  600000
21
21
  ],
22
- "excludedModels": []
22
+ "excludedModels": [],
23
+ "autoCompactThresholdRatio": 0.9
23
24
  }
24
25
  }
25
26
  ]
@@ -36,10 +37,10 @@
36
37
  "pi-package"
37
38
  ],
38
39
  "dependencies": {
39
- "@zhushanwen/pi-ext-guards": "0.5.0",
40
- "@zhushanwen/extension-protocol": "0.26.0",
40
+ "@zhushanwen/extension-protocol": "0.27.0",
41
+ "@zhushanwen/pi-ext-guards": "0.5.1",
41
42
  "@zhushanwen/pi-extension-logger": "0.6.2",
42
- "@zhushanwen/pi-llm-shared": "0.12.0"
43
+ "@zhushanwen/pi-llm-shared": "0.12.1"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@vitest/coverage-v8": "^4.1.9",
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: smart-context-ext-config
3
- description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agent 自决 compact_context 工具 + 双模式摘要生成 + 3 档阈值提醒)时加载。含配置文件路径、schema、默认值、双模式说明、排障。触发词:smart-context 配置、压缩配置、compact 配置、上下文压缩、压缩模型、提醒阈值、排除模型。"
3
+ description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agent 自决 compact_context 工具 + agent_settled 后台自动压缩 + 双模式摘要生成 + 3 档阈值提醒)时加载。含配置文件路径、schema、默认值、双模式说明、排障。触发词:smart-context 配置、压缩配置、compact 配置、上下文压缩、压缩模型、提醒阈值、排除模型、自动压缩、后台压缩。"
4
4
  ---
5
5
 
6
6
  # smart-context 配置指南
@@ -8,6 +8,7 @@ description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agen
8
8
  ## 功能概览
9
9
 
10
10
  - `compact_context` 工具:agent 在「任务阶段性完成 && 压缩不影响后续 && 上下文超阈值」时自决调用
11
+ - 后台自动压缩:`agent_settled`(agent 回合结束)时若 token 用量越过 `autoCompactThresholdRatio` × 当前模型窗口即自动触发,无需 agent 调用工具。与 `compact_context` 工具共享 session 级互斥(同一时刻只有一个压缩任务在跑);连续失败 3 次后熔断——只停自动触发,`compact_context` 工具路径不受影响
11
12
  - 双模式摘要生成:压缩模型 = 当前模型 → same-model 模式(KV 缓存命中,成本最低质量最高);不同 → cross-model 模式(廉价模型 + 最小输入)
12
13
  - 3 档阈值提醒:越过档位时 agent 收到一次性提示(自行判断,不强制)。投递为静默注入——只进下一轮 LLM 上下文,不触发新 turn、不进对话流;同一档位不会重复提醒(已提醒档位随 session 持久化,reload / 重启后不丢)
13
14
  - 排除模型:命中列表的会话模型整体关闭本功能(回落 pi 原生压缩)
@@ -25,6 +26,7 @@ description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agen
25
26
  | `compactModel` | `{type:"ref", ref:string}` | `{type:"ref", ref:""}` | 压缩模型(完整 `provider/modelId`)。`ref` 为空 = 跟随当前会话模型(same-model 模式);等于当前模型同样进 same-model 模式 |
26
27
  | `reminderThresholds` | `number[]` | `[400000, 500000, 600000]` | 3 档提醒阈值(token 绝对数,升序) |
27
28
  | `excludedModels` | `string[]` | `[]` | 排除模型列表,完整 `provider/modelId` 精准等值匹配(如 `"deepseek/deepseek-chat"`),不做前缀匹配 |
29
+ | `autoCompactThresholdRatio` | `number` | `0.9` | 后台自动压缩触发线:相对当前模型窗口的用量比例,`tokens ≥ ratio × 窗口` 即触发(越过含等于)。合法区间 `[0, 1]`,非有限数或越界值钳制到区间内。与 `reminderThresholds` 三档提醒是两个独立行为,各自配置 |
28
30
 
29
31
  ### 配置示例
30
32
 
@@ -33,7 +35,8 @@ description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agen
33
35
  "enabled": true,
34
36
  "compactModel": { "type": "ref", "ref": "xiaomi-token-plan-cn/mimo-v2.6-flash" },
35
37
  "reminderThresholds": [400000, 500000, 600000],
36
- "excludedModels": ["deepseek/deepseek-chat"]
38
+ "excludedModels": ["deepseek/deepseek-chat"],
39
+ "autoCompactThresholdRatio": 0.9
37
40
  }
38
41
  ```
39
42
 
@@ -53,4 +56,5 @@ description: "配置 @zhushanwen/pi-smart-context(智能上下文压缩:agen
53
56
  - 工具调用被拒「已禁用」/「已排除」:检查 `enabled` 与 `excludedModels`
54
57
  - 提醒未出现:`getContextUsage().tokens` 在压缩后首个响应前为 null,属正常(下轮恢复)
55
58
  - 连续接管失败 3 次后不再接管:本会话熔断保护,查看 `TAIJI_AGENT_DEBUG=1` 日志定位失败原因
59
+ - 后台自动压缩未触发:`getContextUsage().tokens` 在压缩后首个响应前为 null 时跳过本轮;同一 session 有压缩任务在跑时互斥等待;后台连续失败 3 次后熔断只停自动触发(`compact_context` 工具仍可用),看 `TAIJI_AGENT_DEBUG=1` 日志中 `background compaction circuit breaker open` 定位
56
60
  - 调试:`TAIJI_AGENT_DEBUG=1` 后看 `[smart-context]` 前缀日志(`~/.pi/agent/logs/`)