pi-cache-optimizer 2.4.1 → 2.4.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 CHANGED
@@ -82,6 +82,7 @@ After installation, `PI_CACHE_RETENTION=long` is applied automatically, the syst
82
82
 
83
83
  | Env var | Effect |
84
84
  |---------|--------|
85
+ | `PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE=1` | Skip all `before_agent_start` prompt mutations (churn strip, skill compression, stable-prefix reorder); footer stats and `prompt_cache_key` fallback remain active |
85
86
  | `PI_CACHE_OPTIMIZER_NO_SKILL_COMPRESSION=1` | Keep pi's verbose `<available_skills>` XML (opt out of one-line index) |
86
87
  | `PI_CACHE_OPTIMIZER_OPENAI_CACHE_KEY=0` | Disable the OpenAI-family `prompt_cache_key` fallback (default is enabled) |
87
88
  | `PI_CACHE_OPTIMIZER_NO_OPENAI_CACHE_KEY=1` | Disable the OpenAI-family `prompt_cache_key` fallback |
package/README.zh-CN.md CHANGED
@@ -85,6 +85,7 @@ pi install npm:pi-cache-optimizer
85
85
 
86
86
  | 环境变量 | 作用 |
87
87
  |---------|------|
88
+ | `PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE=1` | 跳过所有 `before_agent_start` prompt 修改(session-overview 字段剥离、skills 压缩、稳定前缀重排);底部统计和 `prompt_cache_key` 兜底仍然生效 |
88
89
  | `PI_CACHE_OPTIMIZER_NO_SKILL_COMPRESSION=1` | 保留 pi 的 verbose `<available_skills>` XML(退出一行索引模式) |
89
90
  | `PI_CACHE_OPTIMIZER_OPENAI_CACHE_KEY=0` | 禁用 OpenAI-family `prompt_cache_key` 兜底(默认启用) |
90
91
  | `PI_CACHE_OPTIMIZER_NO_OPENAI_CACHE_KEY=1` | 禁用 OpenAI-family `prompt_cache_key` 兜底 |
package/index.ts CHANGED
@@ -37,6 +37,7 @@ const OPENAI_CACHE_KEY_ENV = "PI_CACHE_OPTIMIZER_OPENAI_CACHE_KEY";
37
37
  const NO_OPENAI_CACHE_KEY_ENV = "PI_CACHE_OPTIMIZER_NO_OPENAI_CACHE_KEY";
38
38
  const OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH = 64;
39
39
  const NO_SKILL_COMPRESSION_ENV = "PI_CACHE_OPTIMIZER_NO_SKILL_COMPRESSION";
40
+ const NO_PROMPT_REWRITE_ENV = "PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE";
40
41
 
41
42
  // WORM-flag: if optimizeSystemPrompt ever detects that its blind-replace
42
43
  // logic has accidentally truncated a structural marker (any XML tag or
@@ -856,6 +857,43 @@ function describeMissingOpenAIFamilyProxyCompat(model: PiModel): string[] {
856
857
  return missing;
857
858
  }
858
859
 
860
+ /**
861
+ * Build the warning text displayed to users when an OpenAI-family third-party
862
+ * proxy is missing one or more cache/session-affinity compat flags.
863
+ *
864
+ * The returned string contains a parseable JSON object (via JSON.stringify)
865
+ * listing only the missing flags with recommended value `true`. Inline
866
+ * explanations for each flag follow the JSON snippet as separate prose lines,
867
+ * so the JSON remains valid and copyable.
868
+ *
869
+ * Expected use: the openai adapter's warningText calls this function; tests
870
+ * exercise it via __internals_for_tests.
871
+ */
872
+ function buildOpenAIProxyCompatWarningText(key: string, missing: string[]): string {
873
+ const suggestion: Record<string, boolean> = {};
874
+ for (const flag of missing) {
875
+ suggestion[flag] = true;
876
+ }
877
+
878
+ const lines: string[] = [
879
+ `💡 pi-cache-optimizer: ${key} is a third-party GPT/OpenAI-compatible proxy but merged compat lacks ${missing.join(" and ")}.`,
880
+ `Add under the model's compat in ~/.pi/agent/models.json (only if the endpoint supports them):`,
881
+ ``,
882
+ JSON.stringify(suggestion, null, 2),
883
+ ``,
884
+ ];
885
+
886
+ for (const flag of missing) {
887
+ if (flag === "supportsLongCacheRetention") {
888
+ lines.push("- supportsLongCacheRetention: confirm your endpoint or proxy supports long prompt cache retention.");
889
+ } else if (flag === "sendSessionAffinityHeaders") {
890
+ lines.push("- sendSessionAffinityHeaders: keeps requests on the same backend for proxy cache locality (session affinity).");
891
+ }
892
+ }
893
+
894
+ return lines.join("\n");
895
+ }
896
+
859
897
  function describeMissingDeepSeekCompat(model: PiModel): string[] {
860
898
  const compat = getCompat(model);
861
899
  const missing: string[] = [];
@@ -935,11 +973,7 @@ const CACHE_PROVIDER_ADAPTERS: CacheProviderAdapter[] = [
935
973
  warningText(model) {
936
974
  const missing = describeMissingOpenAIFamilyProxyCompat(model);
937
975
  if (missing.length === 0) return undefined;
938
-
939
- return (
940
- `💡 pi-cache-optimizer: ${modelKey(model)} looks like a third-party GPT/OpenAI-compatible proxy but merged compat lacks ${missing.join(" and ")}. ` +
941
- `For better cache locality, add compat: { "supportsLongCacheRetention": true, "sendSessionAffinityHeaders": true } in ~/.pi/agent/models.json when the endpoint supports these fields.`
942
- );
976
+ return buildOpenAIProxyCompatWarningText(modelKey(model), missing);
943
977
  },
944
978
  },
945
979
  {
@@ -1190,6 +1224,8 @@ export const __internals_for_tests = {
1190
1224
  compressSkillsInSystemPrompt,
1191
1225
  MIN_STABLE_CANDIDATE_LENGTH,
1192
1226
  SKILL_COMPRESSION_MIN_COUNT,
1227
+ NO_PROMPT_REWRITE_ENV,
1228
+ isEnabledEnv,
1193
1229
  // OpenAI-family cache-key helpers
1194
1230
  addOpenAIPromptCacheKey,
1195
1231
  clampPromptCacheKey,
@@ -1202,6 +1238,7 @@ export const __internals_for_tests = {
1202
1238
  isOpenAIFamilyToken,
1203
1239
  describeMissingOpenAIFamilyProxyCompat,
1204
1240
  isOfficialOpenAIBaseUrl,
1241
+ buildOpenAIProxyCompatWarningText,
1205
1242
  getModelIdNameTokenValues,
1206
1243
  getAssistantMessageModelTokenValues,
1207
1244
  getCompat,
@@ -1421,6 +1458,14 @@ export default function (pi: ExtensionAPI) {
1421
1458
  }
1422
1459
  }
1423
1460
 
1461
+ // Global opt-out: PI_CACHE_OPTIMIZER_NO_PROMPT_REWRITE=1 bypasses all
1462
+ // prompt mutations below (session-overview churn strip, skill compression,
1463
+ // and stable-prefix reordering). Footer stats and the OpenAI
1464
+ // prompt_cache_key fallback remain active.
1465
+ if (isEnabledEnv(process.env[NO_PROMPT_REWRITE_ENV])) {
1466
+ return {};
1467
+ }
1468
+
1424
1469
  // Step 1: strip per-turn churn from <session-overview>.
1425
1470
  // Removing RECENT COMMITS, Working directory status, and
1426
1471
  // Journal line count makes more of the session-overview stable
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cache-optimizer",
3
- "version": "2.4.1",
3
+ "version": "2.4.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",