pi-openai-toolkit 0.23.1 → 0.23.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/LICENSE +21 -21
- package/NOTICE +83 -83
- package/README.md +6 -2
- package/README.zh.md +6 -2
- package/config.schema.json +278 -278
- package/docs/configuration.md +306 -298
- package/docs/internals.md +184 -181
- package/package.json +136 -135
- package/src/compact-client.ts +412 -412
- package/src/compaction-projection.ts +35 -35
- package/src/config/migration.ts +103 -103
- package/src/config.ts +310 -310
- package/src/context-management/codex-provider.ts +270 -270
- package/src/context-management/history-notes.ts +357 -357
- package/src/context-management/manual-compact.ts +247 -599
- package/src/context-management/scoped-compaction.ts +89 -0
- package/src/context-management/tools.ts +371 -371
- package/src/context-management/types.ts +168 -168
- package/src/context-management/window-manager.ts +912 -905
- package/src/debug.ts +238 -238
- package/src/deferred-tool-carryover.ts +282 -282
- package/src/details-store.ts +151 -151
- package/src/extension-runtime.ts +1919 -1986
- package/src/image-generation/extension.ts +189 -189
- package/src/image-generation/protocol.ts +579 -579
- package/src/image-generation/types.ts +300 -300
- package/src/native-fallback.ts +32 -29
- package/src/payload-rewrite.ts +515 -515
- package/src/pi-context-hook.ts +117 -117
- package/src/remote-v2-client.ts +811 -811
- package/src/request-context-cache.ts +155 -155
- package/src/responses-headers.ts +180 -180
- package/src/runtime.ts +865 -865
- package/src/serializer.ts +882 -882
- package/src/supported-environment.ts +27 -27
- package/src/types.ts +565 -565
- package/src/web-search/alpha-client.ts +876 -876
- package/src/web-search/extension.ts +530 -530
- package/src/web-search/payload.ts +299 -299
- package/src/web-search/prompt.ts +60 -60
- package/src/web-search/types.ts +166 -166
- package/tsconfig.check.json +50 -50
package/src/native-fallback.ts
CHANGED
|
@@ -20,7 +20,9 @@ export type NativeFallbackFailureReason =
|
|
|
20
20
|
| "auth-failed"
|
|
21
21
|
| "aborted"
|
|
22
22
|
| "empty-summary"
|
|
23
|
-
| "compact-failed"
|
|
23
|
+
| "compact-failed"
|
|
24
|
+
| "public-runtime-unavailable"
|
|
25
|
+
| "missing-model";
|
|
24
26
|
|
|
25
27
|
export type NativeFallbackResult =
|
|
26
28
|
| {
|
|
@@ -39,7 +41,7 @@ export type NativeFallbackResult =
|
|
|
39
41
|
export type NativeCompactFn = typeof compact;
|
|
40
42
|
|
|
41
43
|
type ResolvedAuth =
|
|
42
|
-
| { ok: true; apiKey?: string; headers?: ProviderHeaders; env?: Record<string, string> }
|
|
44
|
+
| { ok: true; apiKey?: string; headers?: ProviderHeaders; baseUrl?: string; env?: Record<string, string> }
|
|
43
45
|
| { ok: false; error: string };
|
|
44
46
|
|
|
45
47
|
function isAbortError(error: unknown): boolean {
|
|
@@ -61,10 +63,10 @@ function toErrorMessage(error: unknown): string {
|
|
|
61
63
|
* active model cannot use remote v2 at all. This function never inspects `remoteCompactModel`
|
|
62
64
|
* itself, so a model that cannot compact remotely can never inherit the remote producer.
|
|
63
65
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
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.
|
|
68
70
|
*/
|
|
69
71
|
export async function runNativeFallbackCompaction(args: {
|
|
70
72
|
ctx: ExtensionContext;
|
|
@@ -73,33 +75,33 @@ export async function runNativeFallbackCompaction(args: {
|
|
|
73
75
|
/** Model spec supplied by the caller for this specific fallback reason. */
|
|
74
76
|
modelSpec?: string;
|
|
75
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;
|
|
76
83
|
}): Promise<NativeFallbackResult> {
|
|
77
84
|
const { ctx, event, config } = args;
|
|
78
85
|
const compactFn = args.compactFn ?? compact;
|
|
79
86
|
const nativeFallback = config.nativeFallback;
|
|
87
|
+
const isCurrent = () => !event.signal.aborted && (args.isCurrent?.() ?? true);
|
|
88
|
+
if (!isCurrent()) return { ok: false, reason: "aborted" };
|
|
80
89
|
|
|
81
|
-
if (!nativeFallback.enabled) {
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const spec = args.modelSpec?.trim() ?? "";
|
|
86
|
-
if (!spec) {
|
|
87
|
-
return { ok: false, reason: "no-model-configured" };
|
|
88
|
-
}
|
|
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" };
|
|
90
93
|
const parsed = parseModelSpec(spec);
|
|
91
|
-
if (!parsed) {
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const model = ctx.modelRegistry.find(parsed.provider, parsed.modelId);
|
|
96
|
-
if (!model) {
|
|
97
|
-
return { ok: false, reason: "model-not-found", modelSpec: spec };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (ctx.model && ctx.model.provider === model.provider && ctx.model.id === model.id) {
|
|
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) {
|
|
101
98
|
return { ok: false, reason: "same-as-current-model", modelSpec: spec };
|
|
102
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
|
+
}
|
|
103
105
|
|
|
104
106
|
// Pi owns prompt construction, tool-result truncation and split-turn requests.
|
|
105
107
|
// A chars/4 estimate is not target-model token accounting and must not veto
|
|
@@ -115,20 +117,21 @@ export async function runNativeFallbackCompaction(args: {
|
|
|
115
117
|
return { ok: false, reason: "auth-failed", modelSpec: spec, errorMessage: auth.error };
|
|
116
118
|
}
|
|
117
119
|
|
|
120
|
+
if (!isCurrent()) return { ok: false, reason: "aborted", modelSpec: spec };
|
|
118
121
|
try {
|
|
119
122
|
const result = await compactFn(
|
|
120
123
|
event.preparation,
|
|
121
|
-
model,
|
|
124
|
+
auth.baseUrl ? { ...model, baseUrl: auth.baseUrl } : model,
|
|
122
125
|
auth.apiKey,
|
|
123
126
|
mergeProviderHeaders(auth.headers),
|
|
124
127
|
event.customInstructions,
|
|
125
128
|
event.signal,
|
|
126
|
-
nativeFallback.thinkingLevel,
|
|
127
|
-
undefined,
|
|
129
|
+
args.useCurrentModel ? ctx.thinkingLevel : nativeFallback.thinkingLevel,
|
|
130
|
+
args.scoped && typeof ctx.modelRegistry.streamSimple === "function" ? ctx.modelRegistry.streamSimple.bind(ctx.modelRegistry) : undefined,
|
|
128
131
|
auth.env,
|
|
129
132
|
);
|
|
130
133
|
|
|
131
|
-
if (
|
|
134
|
+
if (!isCurrent()) {
|
|
132
135
|
return { ok: false, reason: "aborted", modelSpec: spec };
|
|
133
136
|
}
|
|
134
137
|
if (!result.summary || result.summary.trim().length === 0) {
|