pi-cache-optimizer 2.0.0 → 2.0.2
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 +4 -0
- package/README.zh-CN.md +5 -1
- package/{extension.ts → index.ts} +35 -1
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Pi Cache Optimizer
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/pi-cache-optimizer)
|
|
4
|
+
[](https://www.npmjs.com/package/pi-cache-optimizer)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
3
7
|
[中文说明](./README.zh-CN.md)
|
|
4
8
|
|
|
5
9
|
> **Renamed from `pi-deepseek-cache-optimizer`.** If you previously installed the old name, migrate with:
|
package/README.zh-CN.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Pi Cache Optimizer
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/pi-cache-optimizer)
|
|
4
|
+
[](https://www.npmjs.com/package/pi-cache-optimizer)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
3
7
|
[English README](./README.md)
|
|
4
8
|
|
|
5
9
|
> **已从 `pi-deepseek-cache-optimizer` 重命名。** 如果你之前安装的是旧名称,请迁移:
|
|
@@ -10,7 +14,7 @@
|
|
|
10
14
|
>
|
|
11
15
|
> 持久化的底部计数器以及已有的 `~/.pi/agent/models.json` 都会被保留。
|
|
12
16
|
|
|
13
|
-
开箱即用的 Pi 扩展,用稳定 prompt 前缀提升 provider-side KV Cache / Prompt Cache 命中概率,并以保守的 provider-specific adapter
|
|
17
|
+
开箱即用的 Pi 扩展,用稳定 prompt 前缀提升 provider-side KV Cache / Prompt Cache 命中概率,并以保守的 provider-specific adapter 显示底部缓存统计。包名里虽带 DeepSeek,但从 1.x 开始实际上已同时支持 DeepSeek、OpenAI、Claude、Gemini 的统计 adapter;新名称反映这个事实。
|
|
14
18
|
|
|
15
19
|
> 重要:prompt/KV 缓存是 provider 侧、best-effort 行为。本扩展只能通过稳定前缀、在 Pi 支持时请求长保留、提醒明显 compat 缺口、以及展示 provider 暴露的轻量统计来提高命中概率,不能保证每次命中。第三方代理可能隐藏、丢失、重路由或重新解释缓存行为。
|
|
16
20
|
|
|
@@ -49,6 +49,29 @@ const OPENAI_PROMPT_CACHE_KEY_PREFIX = "pi-dsco-";
|
|
|
49
49
|
const NO_AUTO_CONFIG_ENV = "PI_CACHE_OPTIMIZER_NO_AUTO_CONFIG";
|
|
50
50
|
const DEEPSEEK_API_KEY_ENV = "DEEPSEEK_API_KEY";
|
|
51
51
|
|
|
52
|
+
// Minimum trimmed length for a candidate to qualify as a stable-prefix "part".
|
|
53
|
+
//
|
|
54
|
+
// `optimizeSystemPrompt` removes each accepted candidate from the dynamic
|
|
55
|
+
// remainder via `rest.replace(part, "")`. Short or character-class candidates
|
|
56
|
+
// (think: `S`, `- u`, `- (`, `- }`) match the FIRST occurrence of those bytes
|
|
57
|
+
// anywhere in `rest`, ripping unrelated text out of the prompt and yielding a
|
|
58
|
+
// non-deterministic dynamic remainder per request. Both behaviors poison the
|
|
59
|
+
// provider's prompt-prefix cache.
|
|
60
|
+
//
|
|
61
|
+
// The threshold also caps the upstream string-vs-array regression we saw with
|
|
62
|
+
// trellis 0.5.16 / 0.6.0-beta.17 (subagent tool registration passing
|
|
63
|
+
// `promptGuidelines: "<long string>"` instead of `["<long string>"]`, which
|
|
64
|
+
// pi then iterates char-by-char). Even if a similar bug recurs upstream, this
|
|
65
|
+
// extension will not lift its single-character byproducts into the stable
|
|
66
|
+
// prefix candidate list.
|
|
67
|
+
//
|
|
68
|
+
// 8 chars is comfortably above all single-bullet (`- X` = 3 chars) and
|
|
69
|
+
// short-token noise while leaving every legitimate guideline / tool snippet /
|
|
70
|
+
// context-file payload above the bar. If a real future guideline is shorter
|
|
71
|
+
// than 8 chars, the cost is that it is not lifted into the stable prefix; the
|
|
72
|
+
// dynamic-remainder path still includes it untouched.
|
|
73
|
+
const MIN_STABLE_CANDIDATE_LENGTH = 8;
|
|
74
|
+
|
|
52
75
|
const ASSISTANT_MESSAGE_MODEL_TOKEN_KEYS = ["model", "name"];
|
|
53
76
|
const OPENAI_REASONING_MODEL_PATTERN = /(^|[/\s:_-])o[1345]($|[-_.:/\s])/;
|
|
54
77
|
|
|
@@ -187,9 +210,11 @@ function optimizeSystemPrompt(
|
|
|
187
210
|
let rest = original;
|
|
188
211
|
|
|
189
212
|
// Stable layer: content likely to be identical across sessions/turns.
|
|
213
|
+
// Short / single-char candidates are dropped: see MIN_STABLE_CANDIDATE_LENGTH.
|
|
190
214
|
for (const candidate of buildStableCandidates(opts)) {
|
|
191
215
|
const part = candidate.trim();
|
|
192
|
-
if (!part ||
|
|
216
|
+
if (!part || part.length < MIN_STABLE_CANDIDATE_LENGTH) continue;
|
|
217
|
+
if (seen.has(part) || !rest.includes(part)) continue;
|
|
193
218
|
|
|
194
219
|
stableParts.push(part);
|
|
195
220
|
seen.add(part);
|
|
@@ -1005,6 +1030,15 @@ function emitDeepseekApiKeyHintIfNeeded(
|
|
|
1005
1030
|
);
|
|
1006
1031
|
}
|
|
1007
1032
|
|
|
1033
|
+
// Internal helpers exported only so the task verification script
|
|
1034
|
+
// (.trellis/tasks/.../verify.ts) can exercise them. They are not part of the
|
|
1035
|
+
// extension's public API; pi only invokes the default export below.
|
|
1036
|
+
export const __internals_for_tests = {
|
|
1037
|
+
buildStableCandidates,
|
|
1038
|
+
optimizeSystemPrompt,
|
|
1039
|
+
MIN_STABLE_CANDIDATE_LENGTH,
|
|
1040
|
+
};
|
|
1041
|
+
|
|
1008
1042
|
export default function (pi: ExtensionAPI) {
|
|
1009
1043
|
const warnedModels = new Set<string>();
|
|
1010
1044
|
let cacheStatsByProvider: Partial<Record<CacheProviderId, CacheStats>> = emptyAllCacheStats();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-cache-optimizer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Pi extension that improves provider-side KV/prompt cache hit rates (DeepSeek, OpenAI, Claude, Gemini) by reordering the system prompt, requesting long retention, and showing footer cache stats. Renamed from pi-deepseek-cache-optimizer.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -16,17 +16,19 @@
|
|
|
16
16
|
"author": "freescheme",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"files": [
|
|
19
|
-
"
|
|
19
|
+
"index.ts"
|
|
20
20
|
],
|
|
21
21
|
"pi": {
|
|
22
|
-
"extensions": [
|
|
23
|
-
|
|
22
|
+
"extensions": [
|
|
23
|
+
"./index.ts"
|
|
24
|
+
],
|
|
25
|
+
"image": "https://img.shields.io/badge/Pi-Cache%20Optimizer-4A90D9"
|
|
24
26
|
},
|
|
25
27
|
"peerDependencies": {
|
|
26
28
|
"@earendil-works/pi-coding-agent": "*"
|
|
27
29
|
},
|
|
28
30
|
"repository": {
|
|
29
31
|
"type": "git",
|
|
30
|
-
"url": "git+https://github.com/jiangge/pi-
|
|
32
|
+
"url": "git+https://github.com/jiangge/pi-cache-optimizer.git"
|
|
31
33
|
}
|
|
32
34
|
}
|