@lll9p/pi-better-compaction 0.2.0 → 0.4.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 +83 -124
- package/README.zh-CN.md +142 -0
- package/package.json +8 -3
- package/src/compact-client-v2.ts +428 -0
- package/src/compact-client.ts +1 -63
- package/src/config.ts +18 -0
- package/src/extension-runtime.ts +204 -10
- package/src/native-fallback.ts +21 -3
- package/src/retained-messages.ts +98 -0
- package/src/runtime.ts +41 -2
- package/src/shared-headers.ts +103 -0
- package/src/types.ts +26 -4
package/src/types.ts
CHANGED
|
@@ -10,6 +10,7 @@ export const REDACTED_VALUE = "[REDACTED]";
|
|
|
10
10
|
*/
|
|
11
11
|
export const RESPONSES_COMPACT_CAPABLE_APIS = ["openai-responses", "openai-codex-responses"] as const;
|
|
12
12
|
export const NATIVE_COMPACTION_STRATEGY = "openai-native-compact-v1";
|
|
13
|
+
export const NATIVE_COMPACTION_STRATEGY_V2 = "openai-native-compact-v2";
|
|
13
14
|
/** Used as CompactionEntry.summary only when no summary text could be extracted from the compact response. */
|
|
14
15
|
export const NATIVE_COMPACTION_FALLBACK_SUMMARY = "[OpenAI native compaction checkpoint]";
|
|
15
16
|
|
|
@@ -23,6 +24,9 @@ export const THINKING_LEVELS: readonly ThinkingLevel[] = [
|
|
|
23
24
|
"max",
|
|
24
25
|
];
|
|
25
26
|
|
|
27
|
+
export type CompactionVersion = "v1" | "v2";
|
|
28
|
+
export const COMPACTION_VERSIONS: readonly CompactionVersion[] = ["v1", "v2"];
|
|
29
|
+
|
|
26
30
|
export type DebugArtifactKind =
|
|
27
31
|
| "provider-request"
|
|
28
32
|
| "compact-response"
|
|
@@ -31,6 +35,11 @@ export type DebugArtifactKind =
|
|
|
31
35
|
|
|
32
36
|
export type ExtensionConfig = {
|
|
33
37
|
enabled: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Allow a Responses session whose latest compaction was not created by this extension
|
|
40
|
+
* to restart native compaction from Pi's current serialized session context.
|
|
41
|
+
*/
|
|
42
|
+
allowCompactionContinuityBreak: boolean;
|
|
34
43
|
/**
|
|
35
44
|
* "provider/model-id" used for native-method fallback compaction (non-Responses APIs,
|
|
36
45
|
* or when the compact endpoint fails). Unset = current model via pi's default path.
|
|
@@ -40,6 +49,13 @@ export type ExtensionConfig = {
|
|
|
40
49
|
compactionThinkingLevel: ThinkingLevel;
|
|
41
50
|
/** Subset of RESPONSES_COMPACT_CAPABLE_APIS that should use the compact endpoint. */
|
|
42
51
|
responsesCompactApis: string[];
|
|
52
|
+
/**
|
|
53
|
+
* Which compaction protocol to use for Responses-family APIs.
|
|
54
|
+
* - "v2" (default): streaming CompactionTrigger via /responses endpoint.
|
|
55
|
+
* - "v1": POST /responses/compact endpoint.
|
|
56
|
+
* V2 failures automatically fall back to V1.
|
|
57
|
+
*/
|
|
58
|
+
compactionVersion: CompactionVersion;
|
|
43
59
|
notifyOnLoad: boolean;
|
|
44
60
|
debug: boolean;
|
|
45
61
|
logProviderPayloads: boolean;
|
|
@@ -92,6 +108,7 @@ export type RedactOptions = {
|
|
|
92
108
|
};
|
|
93
109
|
|
|
94
110
|
export type NativeCompactionStrategy = typeof NATIVE_COMPACTION_STRATEGY;
|
|
111
|
+
export type NativeCompactionStrategyV2 = typeof NATIVE_COMPACTION_STRATEGY_V2;
|
|
95
112
|
|
|
96
113
|
export type NativeCompactionRequestMeta = {
|
|
97
114
|
tokensBefore?: number;
|
|
@@ -106,7 +123,7 @@ export type NativeCompactionIdentity = {
|
|
|
106
123
|
};
|
|
107
124
|
|
|
108
125
|
export type NativeCompactionDetails = NativeCompactionIdentity & {
|
|
109
|
-
strategy: NativeCompactionStrategy;
|
|
126
|
+
strategy: NativeCompactionStrategy | NativeCompactionStrategyV2;
|
|
110
127
|
compactedWindow: unknown[];
|
|
111
128
|
compactResponseId?: string;
|
|
112
129
|
createdAt: string;
|
|
@@ -235,7 +252,7 @@ export function isNativeCompactionDetails(value: unknown): value is NativeCompac
|
|
|
235
252
|
}
|
|
236
253
|
|
|
237
254
|
return (
|
|
238
|
-
value.strategy === NATIVE_COMPACTION_STRATEGY &&
|
|
255
|
+
(value.strategy === NATIVE_COMPACTION_STRATEGY || value.strategy === NATIVE_COMPACTION_STRATEGY_V2) &&
|
|
239
256
|
isNativeCompactionIdentity(value) &&
|
|
240
257
|
Array.isArray(value.compactedWindow) &&
|
|
241
258
|
value.compactedWindow.every(isCompactedWindowItem) &&
|
|
@@ -249,9 +266,12 @@ export function isNativeCompactionEntry(value: unknown): value is NativeCompacti
|
|
|
249
266
|
return isRecord(value) && value.type === "compaction" && isNativeCompactionDetails(value.details);
|
|
250
267
|
}
|
|
251
268
|
|
|
252
|
-
export function createNativeCompactionDetails(
|
|
269
|
+
export function createNativeCompactionDetails(
|
|
270
|
+
input: CreateNativeCompactionDetailsInput,
|
|
271
|
+
strategy: NativeCompactionStrategy | NativeCompactionStrategyV2 = NATIVE_COMPACTION_STRATEGY,
|
|
272
|
+
): NativeCompactionDetails {
|
|
253
273
|
return {
|
|
254
|
-
strategy
|
|
274
|
+
strategy,
|
|
255
275
|
provider: normalizeString(input.provider),
|
|
256
276
|
api: normalizeString(input.api),
|
|
257
277
|
model: normalizeString(input.model),
|
|
@@ -284,9 +304,11 @@ export function createNativeCompactionResult(
|
|
|
284
304
|
|
|
285
305
|
export const DEFAULT_EXTENSION_CONFIG: ExtensionConfig = {
|
|
286
306
|
enabled: true,
|
|
307
|
+
allowCompactionContinuityBreak: false,
|
|
287
308
|
compactionModel: undefined,
|
|
288
309
|
compactionThinkingLevel: "off",
|
|
289
310
|
responsesCompactApis: [...RESPONSES_COMPACT_CAPABLE_APIS],
|
|
311
|
+
compactionVersion: "v2",
|
|
290
312
|
notifyOnLoad: false,
|
|
291
313
|
debug: false,
|
|
292
314
|
logProviderPayloads: false,
|