pi-openai-toolkit 0.23.2 → 0.24.1

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 (54) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +4 -15
  4. package/README.zh.md +4 -15
  5. package/config.schema.json +264 -278
  6. package/docs/configuration.md +304 -306
  7. package/docs/internals.md +34 -120
  8. package/package.json +123 -136
  9. package/src/compact-client.ts +412 -412
  10. package/src/compaction-projection.ts +35 -35
  11. package/src/config/legacy.ts +469 -519
  12. package/src/config/migration.ts +115 -103
  13. package/src/config/notifications.ts +46 -36
  14. package/src/config/policy.ts +115 -116
  15. package/src/config/v2.ts +1128 -1167
  16. package/src/config.ts +320 -310
  17. package/src/debug.ts +238 -238
  18. package/src/deferred-tool-carryover.ts +282 -282
  19. package/src/details-store.ts +151 -151
  20. package/src/extension-runtime.ts +1095 -1919
  21. package/src/image-generation/extension.ts +189 -189
  22. package/src/image-generation/protocol.ts +579 -579
  23. package/src/image-generation/types.ts +300 -300
  24. package/src/native-fallback.ts +140 -152
  25. package/src/payload-rewrite.ts +515 -515
  26. package/src/pi-context-hook.ts +117 -117
  27. package/src/remote-v2-client.ts +811 -811
  28. package/src/request-context-cache.ts +155 -155
  29. package/src/responses-headers.ts +180 -180
  30. package/src/retired-remote-windows.ts +35 -0
  31. package/src/runtime.ts +875 -865
  32. package/src/serializer.ts +882 -882
  33. package/src/supported-environment.ts +27 -27
  34. package/src/types.ts +541 -565
  35. package/src/web-search/alpha-client.ts +876 -876
  36. package/src/web-search/extension.ts +530 -530
  37. package/src/web-search/payload.ts +299 -299
  38. package/src/web-search/prompt.ts +60 -60
  39. package/src/web-search/types.ts +166 -166
  40. package/tsconfig.check.json +50 -50
  41. package/src/context-management/codex-provider.ts +0 -270
  42. package/src/context-management/history-notes.ts +0 -357
  43. package/src/context-management/manual-compact.ts +0 -247
  44. package/src/context-management/messages.ts +0 -238
  45. package/src/context-management/namespace-tools.ts +0 -426
  46. package/src/context-management/responses-tools.ts +0 -238
  47. package/src/context-management/scoped-compaction.ts +0 -89
  48. package/src/context-management/tool-contract.ts +0 -62
  49. package/src/context-management/tools.ts +0 -371
  50. package/src/context-management/types.ts +0 -168
  51. package/src/context-management/window-budget.ts +0 -83
  52. package/src/context-management/window-bulk.ts +0 -155
  53. package/src/context-management/window-manager.ts +0 -912
  54. package/src/context-management/window-request.ts +0 -125
@@ -1,152 +1,140 @@
1
- import {
2
- compact,
3
- type CompactionResult,
4
- type ExtensionContext,
5
- type SessionBeforeCompactEvent,
6
- } from "@earendil-works/pi-coding-agent";
7
- import type { ProviderHeaders } from "@earendil-works/pi-ai";
8
- import { mergeProviderHeaders } from "./provider-headers";
9
- import { parseModelSpec } from "./runtime";
10
- import type { CompactionConfig } from "./types";
11
-
12
- export { parseModelSpec } from "./runtime";
13
-
14
- export type NativeFallbackFailureReason =
15
- | "disabled"
16
- | "no-model-configured"
17
- | "invalid-model-spec"
18
- | "model-not-found"
19
- | "same-as-current-model"
20
- | "auth-failed"
21
- | "aborted"
22
- | "empty-summary"
23
- | "compact-failed"
24
- | "public-runtime-unavailable"
25
- | "missing-model";
26
-
27
- export type NativeFallbackResult =
28
- | {
29
- ok: true;
30
- result: CompactionResult;
31
- model: { provider: string; id: string };
32
- }
33
- | {
34
- ok: false;
35
- reason: NativeFallbackFailureReason;
36
- modelSpec?: string;
37
- errorMessage?: string;
38
- };
39
-
40
- /** pi's exported native compact(); injectable for tests. */
41
- export type NativeCompactFn = typeof compact;
42
-
43
- type ResolvedAuth =
44
- | { ok: true; apiKey?: string; headers?: ProviderHeaders; baseUrl?: string; env?: Record<string, string> }
45
- | { ok: false; error: string };
46
-
47
- function isAbortError(error: unknown): boolean {
48
- return (
49
- (error instanceof DOMException && error.name === "AbortError") ||
50
- (error instanceof Error && (error.name === "AbortError" || error.name === "ABORT_ERR"))
51
- );
52
- }
53
-
54
- function toErrorMessage(error: unknown): string {
55
- return error instanceof Error ? error.message : String(error);
56
- }
57
-
58
- /**
59
- * Run pi's native compaction method with a caller-resolved summary model.
60
- *
61
- * The two compaction chains stay separate: the caller passes `remoteCompactModel` only when a
62
- * remote v2 request was actually attempted and failed, and `nativeFallback.model` only when the
63
- * active model cannot use remote v2 at all. This function never inspects `remoteCompactModel`
64
- * itself, so a model that cannot compact remotely can never inherit the remote producer.
65
- *
66
- * Ordinary no-boundary defaults stay with Pi. When a window constrains input,
67
- * useCurrentModel runs the same public native primitive with the public registry
68
- * stream, current thinking and auth, rather than reopening unscoped host history.
69
- * Host-private custom stream/retry callbacks are not exposed by ExtensionContext.
70
- */
71
- export async function runNativeFallbackCompaction(args: {
72
- ctx: ExtensionContext;
73
- event: SessionBeforeCompactEvent;
74
- config: CompactionConfig;
75
- /** Model spec supplied by the caller for this specific fallback reason. */
76
- modelSpec?: string;
77
- compactFn?: NativeCompactFn;
78
- /** Scoped input cannot be handed back to an unscoped host default. */
79
- scoped?: boolean;
80
- /** Run current-model native policy, independently of Toolkit's fallback config. */
81
- useCurrentModel?: boolean;
82
- isCurrent?: () => boolean;
83
- }): Promise<NativeFallbackResult> {
84
- const { ctx, event, config } = args;
85
- const compactFn = args.compactFn ?? compact;
86
- const nativeFallback = config.nativeFallback;
87
- const isCurrent = () => !event.signal.aborted && (args.isCurrent?.() ?? true);
88
- if (!isCurrent()) return { ok: false, reason: "aborted" };
89
-
90
- if (!args.useCurrentModel && !nativeFallback.enabled) return { ok: false, reason: "disabled" };
91
- const spec = args.useCurrentModel ? (ctx.model ? `${ctx.model.provider}/${ctx.model.id}` : "") : args.modelSpec?.trim() ?? "";
92
- if (!spec) return { ok: false, reason: args.useCurrentModel ? "missing-model" : "no-model-configured" };
93
- const parsed = parseModelSpec(spec);
94
- if (!parsed) return { ok: false, reason: "invalid-model-spec", modelSpec: spec };
95
- const model = args.useCurrentModel ? ctx.model : ctx.modelRegistry.find(parsed.provider, parsed.modelId);
96
- if (!model) return { ok: false, reason: "model-not-found", modelSpec: spec };
97
- if (!args.useCurrentModel && ctx.model && ctx.model.provider === model.provider && ctx.model.id === model.id) {
98
- return { ok: false, reason: "same-as-current-model", modelSpec: spec };
99
- }
100
- // The public registry preserves provider-native stream/auth wiring. Do not
101
- // silently use a compat-only transport for scoped current-model compression.
102
- if (args.scoped && typeof ctx.modelRegistry.streamSimple !== "function" && !args.compactFn) {
103
- return { ok: false, reason: "public-runtime-unavailable", modelSpec: spec };
104
- }
105
-
106
- // Pi owns prompt construction, tool-result truncation and split-turn requests.
107
- // A chars/4 estimate is not target-model token accounting and must not veto
108
- // the user's summary model. Surface actual provider failures to the caller.
109
-
110
- let auth: ResolvedAuth;
111
- try {
112
- auth = (await ctx.modelRegistry.getApiKeyAndHeaders(model)) as ResolvedAuth;
113
- } catch (error) {
114
- return { ok: false, reason: "auth-failed", modelSpec: spec, errorMessage: toErrorMessage(error) };
115
- }
116
- if (!auth.ok) {
117
- return { ok: false, reason: "auth-failed", modelSpec: spec, errorMessage: auth.error };
118
- }
119
-
120
- if (!isCurrent()) return { ok: false, reason: "aborted", modelSpec: spec };
121
- try {
122
- const result = await compactFn(
123
- event.preparation,
124
- auth.baseUrl ? { ...model, baseUrl: auth.baseUrl } : model,
125
- auth.apiKey,
126
- mergeProviderHeaders(auth.headers),
127
- event.customInstructions,
128
- event.signal,
129
- args.useCurrentModel ? ctx.thinkingLevel : nativeFallback.thinkingLevel,
130
- args.scoped && typeof ctx.modelRegistry.streamSimple === "function" ? ctx.modelRegistry.streamSimple.bind(ctx.modelRegistry) : undefined,
131
- auth.env,
132
- );
133
-
134
- if (!isCurrent()) {
135
- return { ok: false, reason: "aborted", modelSpec: spec };
136
- }
137
- if (!result.summary || result.summary.trim().length === 0) {
138
- return { ok: false, reason: "empty-summary", modelSpec: spec };
139
- }
140
-
141
- return {
142
- ok: true,
143
- result,
144
- model: { provider: model.provider, id: model.id },
145
- };
146
- } catch (error) {
147
- if (event.signal.aborted || isAbortError(error)) {
148
- return { ok: false, reason: "aborted", modelSpec: spec };
149
- }
150
- return { ok: false, reason: "compact-failed", modelSpec: spec, errorMessage: toErrorMessage(error) };
151
- }
152
- }
1
+ import {
2
+ compact,
3
+ type CompactionResult,
4
+ type ExtensionContext,
5
+ type SessionBeforeCompactEvent,
6
+ } from "@earendil-works/pi-coding-agent";
7
+ import type { ProviderHeaders } from "@earendil-works/pi-ai";
8
+ import { mergeProviderHeaders } from "./provider-headers";
9
+ import { parseModelSpec } from "./runtime";
10
+ import type { CompactionConfig } from "./types";
11
+
12
+ export { parseModelSpec } from "./runtime";
13
+
14
+ export type NativeFallbackFailureReason =
15
+ | "disabled"
16
+ | "no-model-configured"
17
+ | "invalid-model-spec"
18
+ | "model-not-found"
19
+ | "same-as-current-model"
20
+ | "auth-failed"
21
+ | "aborted"
22
+ | "empty-summary"
23
+ | "compact-failed";
24
+
25
+ export type NativeFallbackResult =
26
+ | {
27
+ ok: true;
28
+ result: CompactionResult;
29
+ model: { provider: string; id: string };
30
+ }
31
+ | {
32
+ ok: false;
33
+ reason: NativeFallbackFailureReason;
34
+ modelSpec?: string;
35
+ errorMessage?: string;
36
+ };
37
+
38
+ /** pi's exported native compact(); injectable for tests. */
39
+ export type NativeCompactFn = typeof compact;
40
+
41
+ type ResolvedAuth =
42
+ | { ok: true; apiKey?: string; headers?: ProviderHeaders; baseUrl?: string; env?: Record<string, string> }
43
+ | { ok: false; error: string };
44
+
45
+ function isAbortError(error: unknown): boolean {
46
+ return (
47
+ (error instanceof DOMException && error.name === "AbortError") ||
48
+ (error instanceof Error && (error.name === "AbortError" || error.name === "ABORT_ERR"))
49
+ );
50
+ }
51
+
52
+ function toErrorMessage(error: unknown): string {
53
+ return error instanceof Error ? error.message : String(error);
54
+ }
55
+
56
+ /**
57
+ * Run pi's native compaction method with a caller-resolved summary model.
58
+ *
59
+ * The two compaction chains stay separate: the caller passes `remoteCompactModel` only when a
60
+ * remote v2 request was actually attempted and failed, and `nativeFallback.model` only when the
61
+ * active model cannot use remote v2 at all. This function never inspects `remoteCompactModel`
62
+ * itself, so a model that cannot compact remotely can never inherit the remote producer.
63
+ *
64
+ * Unset or same-current-model defaults stay with Pi. This helper does not
65
+ * select a session model or take ownership of Pi's scheduler.
66
+ */
67
+ export async function runNativeFallbackCompaction(args: {
68
+ ctx: ExtensionContext;
69
+ event: SessionBeforeCompactEvent;
70
+ config: CompactionConfig;
71
+ /** Model spec supplied by the caller for this specific fallback reason. */
72
+ modelSpec?: string;
73
+ compactFn?: NativeCompactFn;
74
+ /** Reject results if the originating session/branch/selection changed. */
75
+ isCurrent?: () => boolean;
76
+ }): Promise<NativeFallbackResult> {
77
+ const { ctx, event, config } = args;
78
+ const compactFn = args.compactFn ?? compact;
79
+ const nativeFallback = config.nativeFallback;
80
+ const isCurrent = () => !event.signal.aborted && (args.isCurrent?.() ?? true);
81
+ if (!isCurrent()) return { ok: false, reason: "aborted" };
82
+
83
+ if (!nativeFallback.enabled) return { ok: false, reason: "disabled" };
84
+ const spec = args.modelSpec?.trim() ?? "";
85
+ if (!spec) return { ok: false, reason: "no-model-configured" };
86
+ const parsed = parseModelSpec(spec);
87
+ if (!parsed) return { ok: false, reason: "invalid-model-spec", modelSpec: spec };
88
+ const model = ctx.modelRegistry.find(parsed.provider, parsed.modelId);
89
+ if (!model) return { ok: false, reason: "model-not-found", modelSpec: spec };
90
+ if (ctx.model && ctx.model.provider === model.provider && ctx.model.id === model.id) {
91
+ return { ok: false, reason: "same-as-current-model", modelSpec: spec };
92
+ }
93
+
94
+ // Pi owns prompt construction, tool-result truncation and split-turn requests.
95
+ // A chars/4 estimate is not target-model token accounting and must not veto
96
+ // the user's summary model. Surface actual provider failures to the caller.
97
+
98
+ let auth: ResolvedAuth;
99
+ try {
100
+ auth = (await ctx.modelRegistry.getApiKeyAndHeaders(model)) as ResolvedAuth;
101
+ } catch (error) {
102
+ return { ok: false, reason: "auth-failed", modelSpec: spec, errorMessage: toErrorMessage(error) };
103
+ }
104
+ if (!auth.ok) {
105
+ return { ok: false, reason: "auth-failed", modelSpec: spec, errorMessage: auth.error };
106
+ }
107
+
108
+ if (!isCurrent()) return { ok: false, reason: "aborted", modelSpec: spec };
109
+ try {
110
+ const result = await compactFn(
111
+ event.preparation,
112
+ auth.baseUrl ? { ...model, baseUrl: auth.baseUrl } : model,
113
+ auth.apiKey,
114
+ mergeProviderHeaders(auth.headers),
115
+ event.customInstructions,
116
+ event.signal,
117
+ nativeFallback.thinkingLevel,
118
+ undefined,
119
+ auth.env,
120
+ );
121
+
122
+ if (!isCurrent()) {
123
+ return { ok: false, reason: "aborted", modelSpec: spec };
124
+ }
125
+ if (!result.summary || result.summary.trim().length === 0) {
126
+ return { ok: false, reason: "empty-summary", modelSpec: spec };
127
+ }
128
+
129
+ return {
130
+ ok: true,
131
+ result,
132
+ model: { provider: model.provider, id: model.id },
133
+ };
134
+ } catch (error) {
135
+ if (event.signal.aborted || isAbortError(error)) {
136
+ return { ok: false, reason: "aborted", modelSpec: spec };
137
+ }
138
+ return { ok: false, reason: "compact-failed", modelSpec: spec, errorMessage: toErrorMessage(error) };
139
+ }
140
+ }