pi-openai-toolkit 0.20.7 → 0.21.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.
Files changed (55) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +8 -26
  4. package/README.zh.md +8 -26
  5. package/config.schema.json +278 -366
  6. package/docs/configuration.md +294 -310
  7. package/docs/internals.md +166 -175
  8. package/package.json +3 -13
  9. package/src/compact-client.ts +412 -389
  10. package/src/compaction-errors.ts +96 -0
  11. package/src/compaction-projection.ts +35 -35
  12. package/src/config/legacy.ts +519 -717
  13. package/src/config/migration.ts +103 -100
  14. package/src/config/notifications.ts +36 -28
  15. package/src/config/policy.ts +10 -18
  16. package/src/config/v2.ts +1167 -1380
  17. package/src/config.ts +310 -319
  18. package/src/context-management/codex-provider.ts +270 -270
  19. package/src/context-management/history-notes.ts +357 -357
  20. package/src/context-management/manual-compact.ts +0 -11
  21. package/src/context-management/tools.ts +371 -371
  22. package/src/context-management/types.ts +168 -168
  23. package/src/context-management/window-manager.ts +801 -801
  24. package/src/debug.ts +238 -237
  25. package/src/deferred-tool-carryover.ts +282 -282
  26. package/src/details-store.ts +151 -151
  27. package/src/extension-runtime.ts +1662 -1623
  28. package/src/image-generation/extension.ts +189 -189
  29. package/src/image-generation/protocol.ts +579 -579
  30. package/src/image-generation/types.ts +300 -300
  31. package/src/native-fallback.ts +4 -132
  32. package/src/payload-rewrite.ts +515 -518
  33. package/src/pi-context-hook.ts +117 -117
  34. package/src/remote-v2-client.ts +811 -814
  35. package/src/request-context-cache.ts +155 -155
  36. package/src/responses-headers.ts +180 -180
  37. package/src/runtime.ts +865 -832
  38. package/src/serializer.ts +882 -882
  39. package/src/supported-environment.ts +27 -27
  40. package/src/types.ts +565 -648
  41. package/src/web-search/alpha-client.ts +876 -876
  42. package/src/web-search/extension.ts +530 -530
  43. package/src/web-search/payload.ts +299 -299
  44. package/src/web-search/prompt.ts +60 -60
  45. package/src/web-search/types.ts +166 -166
  46. package/tsconfig.check.json +50 -54
  47. package/src/auto-mode/classifier.ts +0 -161
  48. package/src/auto-mode/extension.ts +0 -691
  49. package/src/auto-mode/model-selection-guard.ts +0 -52
  50. package/src/auto-mode/policy.ts +0 -146
  51. package/src/auto-mode/prompt.ts +0 -279
  52. package/src/auto-mode/reviewer.ts +0 -325
  53. package/src/auto-mode/tool-review-tui.ts +0 -211
  54. package/src/auto-mode/transcript.ts +0 -135
  55. package/src/auto-mode/types.ts +0 -137
@@ -1,8 +1,5 @@
1
1
  import {
2
2
  compact,
3
- convertToLlm,
4
- DEFAULT_COMPACTION_SETTINGS,
5
- serializeConversation,
6
3
  type CompactionResult,
7
4
  type ExtensionContext,
8
5
  type SessionBeforeCompactEvent,
@@ -23,9 +20,7 @@ export type NativeFallbackFailureReason =
23
20
  | "auth-failed"
24
21
  | "aborted"
25
22
  | "empty-summary"
26
- | "compact-failed"
27
- /** The configured summary model cannot fit the request it would have to send. */
28
- | "model-window-too-small";
23
+ | "compact-failed";
29
24
 
30
25
  export type NativeFallbackResult =
31
26
  | {
@@ -38,8 +33,6 @@ export type NativeFallbackResult =
38
33
  reason: NativeFallbackFailureReason;
39
34
  modelSpec?: string;
40
35
  errorMessage?: string;
41
- estimatedTokens?: number;
42
- contextWindow?: number;
43
36
  };
44
37
 
45
38
  /** pi's exported native compact(); injectable for tests. */
@@ -60,116 +53,6 @@ function toErrorMessage(error: unknown): string {
60
53
  return error instanceof Error ? error.message : String(error);
61
54
  }
62
55
 
63
- /** Summarization instructions plus the framing around the serialized transcript. */
64
- const SUMMARIZATION_TEMPLATE_TOKENS = 1024;
65
- const HISTORY_OUTPUT_RESERVE_RATIO = 0.8;
66
- const TURN_PREFIX_OUTPUT_RESERVE_RATIO = 0.5;
67
-
68
- export type SummarizationRequestSize = {
69
- inputTokens: number;
70
- outputTokens: number;
71
- totalTokens: number;
72
- };
73
-
74
- function estimateTextTokens(text: string | undefined): number {
75
- return text ? Math.ceil(String(text).length / 4) : 0;
76
- }
77
-
78
- function estimateSerializedConversationTokens(
79
- messages: SessionBeforeCompactEvent["preparation"]["messagesToSummarize"],
80
- ): number {
81
- const serialized = serializeConversation(convertToLlm(messages));
82
- return estimateTextTokens(serialized);
83
- }
84
-
85
- function getEffectiveReserveTokens(preparation: SessionBeforeCompactEvent["preparation"]): number {
86
- const reserveTokens = preparation.settings?.reserveTokens;
87
- return typeof reserveTokens === "number" && Number.isFinite(reserveTokens) && reserveTokens >= 0
88
- ? reserveTokens
89
- : DEFAULT_COMPACTION_SETTINGS.reserveTokens;
90
- }
91
-
92
- function getOutputReserveTokens(
93
- reserveTokens: number,
94
- model: { maxTokens?: number },
95
- ratio: number,
96
- ): number {
97
- const outputTokens = Math.floor(ratio * reserveTokens);
98
- return typeof model.maxTokens === "number" && model.maxTokens > 0
99
- ? Math.min(outputTokens, model.maxTokens)
100
- : outputTokens;
101
- }
102
-
103
- function estimateSummaryRequest(
104
- messages: SessionBeforeCompactEvent["preparation"]["messagesToSummarize"],
105
- outputTokens: number,
106
- previousSummary?: string,
107
- customInstructions?: string,
108
- ): SummarizationRequestSize {
109
- const inputTokens =
110
- SUMMARIZATION_TEMPLATE_TOKENS
111
- + estimateSerializedConversationTokens(messages)
112
- + estimateTextTokens(previousSummary)
113
- + estimateTextTokens(customInstructions);
114
- return { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens };
115
- }
116
-
117
- /**
118
- * Size of the individual request(s) Pi's `compact()` is about to send.
119
- *
120
- * Pi first converts messages with `convertToLlm()` and then applies
121
- * `serializeConversation()`, which truncates tool results before they enter the
122
- * summarization prompt. Split-turn compaction sends history and turn-prefix
123
- * summaries as separate requests, so this returns the largest individual request
124
- * rather than adding their inputs together. The fixed allowance remains
125
- * deliberately conservative for Pi's system prompt, tags, and instructions.
126
- *
127
- * A session driven by a wide-window model (400k) can produce a preparation that a
128
- * narrower summary model (272k) cannot accept. Callers must not attempt that request;
129
- * a missing `contextWindow` skips the check instead of guessing.
130
- */
131
- export function estimateSummarizationRequest(
132
- preparation: SessionBeforeCompactEvent["preparation"],
133
- model: { contextWindow?: number; maxTokens?: number },
134
- customInstructions?: string,
135
- ): SummarizationRequestSize {
136
- const reserveTokens = getEffectiveReserveTokens(preparation);
137
- const historyMessages = preparation.messagesToSummarize ?? [];
138
- const turnPrefixMessages = preparation.turnPrefixMessages ?? [];
139
- const historyOutputTokens = getOutputReserveTokens(reserveTokens, model, HISTORY_OUTPUT_RESERVE_RATIO);
140
- const historyRequest = estimateSummaryRequest(
141
- historyMessages,
142
- historyOutputTokens,
143
- preparation.previousSummary,
144
- customInstructions,
145
- );
146
-
147
- if (!preparation.isSplitTurn || turnPrefixMessages.length === 0) {
148
- return historyRequest;
149
- }
150
-
151
- const requests = [
152
- ...(historyMessages.length > 0 ? [historyRequest] : []),
153
- estimateSummaryRequest(
154
- turnPrefixMessages,
155
- getOutputReserveTokens(reserveTokens, model, TURN_PREFIX_OUTPUT_RESERVE_RATIO),
156
- ),
157
- ];
158
- return requests.reduce((largest, request) =>
159
- request.totalTokens > largest.totalTokens ? request : largest,
160
- );
161
- }
162
-
163
- /** Whether the summary model can accept this request at all; unknown windows always pass. */
164
- export function fitsSummarizationRequest(
165
- size: SummarizationRequestSize,
166
- model: { contextWindow?: number },
167
- ): boolean {
168
- const contextWindow = model.contextWindow;
169
- if (typeof contextWindow !== "number" || contextWindow <= 0) return true;
170
- return size.totalTokens <= contextWindow;
171
- }
172
-
173
56
  /**
174
57
  * Run pi's native compaction method with a caller-resolved summary model.
175
58
  *
@@ -218,20 +101,9 @@ export async function runNativeFallbackCompaction(args: {
218
101
  return { ok: false, reason: "same-as-current-model", modelSpec: spec };
219
102
  }
220
103
 
221
- const size = estimateSummarizationRequest(
222
- event.preparation,
223
- model as { contextWindow?: number; maxTokens?: number },
224
- event.customInstructions,
225
- );
226
- if (!fitsSummarizationRequest(size, model as { contextWindow?: number })) {
227
- return {
228
- ok: false,
229
- reason: "model-window-too-small",
230
- modelSpec: spec,
231
- estimatedTokens: size.totalTokens,
232
- contextWindow: (model as { contextWindow?: number }).contextWindow,
233
- };
234
- }
104
+ // Pi owns prompt construction, tool-result truncation and split-turn requests.
105
+ // A chars/4 estimate is not target-model token accounting and must not veto
106
+ // the user's summary model. Surface actual provider failures to the caller.
235
107
 
236
108
  let auth: ResolvedAuth;
237
109
  try {