pi-cache-optimizer 2.8.16 → 2.8.17

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.
@@ -0,0 +1,114 @@
1
+ import { type PersistedCacheOptimizerFeature } from "./config.ts";
2
+
3
+ export type CommandCompletionItem = {
4
+ value: string;
5
+ label: string;
6
+ description?: string;
7
+ };
8
+
9
+ export const CACHE_OPTIMIZER_COMMANDS = [
10
+ "enable",
11
+ "disable",
12
+ "doctor",
13
+ "stats",
14
+ "config",
15
+ "compat",
16
+ "reset",
17
+ "fix",
18
+ "rollback",
19
+ ] as const;
20
+
21
+ export const CACHE_OPTIMIZER_CONFIG_ARGUMENTS = ["footer-mode", "prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order", "reset"] as const;
22
+
23
+ export const CACHE_OPTIMIZER_FEATURE_COMMANDS = ["prompt-rewrite", "virtual-rewrite", "skill-compression", "openai-cache-key", "tool-order"] as const;
24
+
25
+ export const CACHE_OPTIMIZER_FEATURE_VALUES = ["on", "off"] as const;
26
+
27
+ export const CACHE_OPTIMIZER_FOOTER_MODES = ["total", "session", "process"] as const;
28
+
29
+ export const FEATURE_COMMAND_MAP: Record<string, PersistedCacheOptimizerFeature> = {
30
+ "prompt-rewrite": "promptRewrite",
31
+ "virtual-rewrite": "virtualRewrite",
32
+ "skill-compression": "skillCompression",
33
+ "openai-cache-key": "openAICacheKey",
34
+ "tool-order": "toolOrder",
35
+ };
36
+
37
+ export const CACHE_OPTIMIZER_STATS_ARGUMENTS = ["all", "contributors"] as const;
38
+
39
+ export const CACHE_OPTIMIZER_FIX_ARGUMENTS = ["prompt-cache-key"] as const;
40
+
41
+ export function filterCommandCompletionItems(
42
+ values: readonly string[],
43
+ prefix: string,
44
+ argumentPath = "",
45
+ ): CommandCompletionItem[] | null {
46
+ const normalizedPrefix = prefix.trim().toLowerCase();
47
+ const matches = values
48
+ .filter((value) => value.startsWith(normalizedPrefix))
49
+ .map((value) => ({
50
+ // Pi replaces the complete argumentPrefix when applying a command
51
+ // completion, so nested suggestions must include their full path.
52
+ value: argumentPath ? `${argumentPath} ${value}` : value,
53
+ label: value,
54
+ }));
55
+ return matches.length > 0 ? matches : null;
56
+ }
57
+
58
+ export function getCacheOptimizerArgumentCompletions(argumentPrefix: string): CommandCompletionItem[] | null {
59
+ if (typeof argumentPrefix !== "string") return null;
60
+ const trimmed = argumentPrefix.trim();
61
+ const parts = trimmed ? trimmed.split(/\s+/) : [];
62
+
63
+ if (parts.length === 0) {
64
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_COMMANDS, "");
65
+ }
66
+
67
+ if (parts.length === 1) {
68
+ const subcommandPrefix = parts[0].toLowerCase();
69
+ // `config` is the primary `c` completion; `compat` remains available
70
+ // through its more specific `co` prefix. Surrounding whitespace is
71
+ // ignored so ` c ` behaves the same as `c`.
72
+ if (subcommandPrefix === "c") {
73
+ return [{ value: "config", label: "config" }];
74
+ }
75
+ if (subcommandPrefix === "config") {
76
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_CONFIG_ARGUMENTS, "", "config");
77
+ }
78
+ if (subcommandPrefix === "stats") {
79
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_STATS_ARGUMENTS, "", "stats");
80
+ }
81
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_COMMANDS, parts[0]);
82
+ }
83
+
84
+ if (parts[0].toLowerCase() === "stats") {
85
+ return parts.length === 2
86
+ ? filterCommandCompletionItems(CACHE_OPTIMIZER_STATS_ARGUMENTS, parts[1], "stats")
87
+ : null;
88
+ }
89
+
90
+ if (parts[0].toLowerCase() === "fix") {
91
+ return parts.length === 2
92
+ ? filterCommandCompletionItems(CACHE_OPTIMIZER_FIX_ARGUMENTS, parts[1], "fix")
93
+ : null;
94
+ }
95
+
96
+ if (parts[0].toLowerCase() !== "config") return null;
97
+
98
+ if (parts.length === 2) {
99
+ const nestedPrefix = parts[1].toLowerCase();
100
+ if (nestedPrefix === "footer-mode") {
101
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_FOOTER_MODES, "", "config footer-mode");
102
+ }
103
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_CONFIG_ARGUMENTS, parts[1], "config");
104
+ }
105
+
106
+ if (parts.length === 3 && parts[1].toLowerCase() === "footer-mode") {
107
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_FOOTER_MODES, parts[2], "config footer-mode");
108
+ }
109
+ if (parts.length === 3 && CACHE_OPTIMIZER_FEATURE_COMMANDS.includes(parts[1] as typeof CACHE_OPTIMIZER_FEATURE_COMMANDS[number])) {
110
+ return filterCommandCompletionItems(CACHE_OPTIMIZER_FEATURE_VALUES, parts[2], `config ${parts[1]}`);
111
+ }
112
+
113
+ return null;
114
+ }