pi-openai-toolkit 0.22.2 → 0.22.3

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 (39) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +1 -1
  4. package/README.zh.md +1 -1
  5. package/config.schema.json +278 -278
  6. package/docs/configuration.md +298 -298
  7. package/docs/internals.md +181 -181
  8. package/package.json +134 -134
  9. package/src/compact-client.ts +412 -412
  10. package/src/compaction-projection.ts +35 -35
  11. package/src/config/migration.ts +103 -103
  12. package/src/config.ts +310 -310
  13. package/src/context-management/codex-provider.ts +270 -270
  14. package/src/context-management/history-notes.ts +357 -357
  15. package/src/context-management/tools.ts +371 -371
  16. package/src/context-management/types.ts +168 -168
  17. package/src/context-management/window-manager.ts +811 -801
  18. package/src/debug.ts +238 -238
  19. package/src/deferred-tool-carryover.ts +282 -282
  20. package/src/details-store.ts +151 -151
  21. package/src/extension-runtime.ts +1661 -1645
  22. package/src/image-generation/extension.ts +189 -189
  23. package/src/image-generation/protocol.ts +579 -579
  24. package/src/image-generation/types.ts +300 -300
  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/runtime.ts +865 -865
  31. package/src/serializer.ts +882 -882
  32. package/src/supported-environment.ts +27 -27
  33. package/src/types.ts +565 -565
  34. package/src/web-search/alpha-client.ts +876 -876
  35. package/src/web-search/extension.ts +530 -530
  36. package/src/web-search/payload.ts +299 -299
  37. package/src/web-search/prompt.ts +60 -60
  38. package/src/web-search/types.ts +166 -166
  39. package/tsconfig.check.json +50 -50
package/src/config.ts CHANGED
@@ -1,310 +1,310 @@
1
- import * as fs from "node:fs";
2
- import * as os from "node:os";
3
- import * as path from "node:path";
4
- import { getExactModelKey, type ExactModelScopeModel } from "./model-scope";
5
- import { resolveWebSearchRoute, type WebSearchModel } from "./web-search/types";
6
- import { CONFIG_PATH, normalizeLegacyConfig } from "./config/legacy";
7
- import { resolveV2Config } from "./config/v2";
8
- import {
9
- CONFIG_FEATURES,
10
- RETIRED_AUTO_MODE_ISSUE,
11
- createPolicyDefaults,
12
- type ConfigDocumentSnapshot,
13
- type ConfigFeature,
14
- type ConfigIssue,
15
- type ConfigOrigin,
16
- type EffectiveToolkitPolicy,
17
- type ResolvedToolkitPolicy,
18
- } from "./config/policy";
19
- import type { LoadedToolkitConfig, ToolkitConfig } from "./types";
20
-
21
- export { CONFIG_DIR, CONFIG_PATH } from "./config/legacy";
22
- export { DEFAULT_TOOLKIT_CONFIG } from "./types";
23
- export type { ConfigFeature, ConfigIssue, ConfigOrigin, EffectiveToolkitPolicy } from "./config/policy";
24
-
25
- export type ResolvedToolkitConfig = ResolvedToolkitPolicy & {
26
- /** Narrow adapters for existing feature engines, never raw document sections. */
27
- config: ToolkitConfig;
28
- format: ConfigDocumentSnapshot["format"];
29
- source?: string;
30
- modelKey?: string;
31
- /** Cross-feature protocol policy, independent of context enablement. */
32
- gatewayModelKeys: string[];
33
- /** Same decoded document for resolving another identity within this operation. */
34
- snapshot: LoadedToolkitConfig;
35
- };
36
-
37
- function isRecord(value: unknown): value is Record<string, unknown> {
38
- return !!value && typeof value === "object" && !Array.isArray(value);
39
- }
40
- function has(value: Record<string, unknown>, key: string): boolean {
41
- return Object.prototype.hasOwnProperty.call(value, key);
42
- }
43
- function freeze<T>(value: T): T {
44
- if (value && typeof value === "object" && !Object.isFrozen(value)) {
45
- Object.freeze(value);
46
- for (const item of Object.values(value)) freeze(item);
47
- }
48
- return value;
49
- }
50
- function documentError(code: string): ConfigIssue {
51
- return { severity: "error", code, path: "$", feature: "document" };
52
- }
53
-
54
- /** Read exactly one global file. No project discovery, cache, writes or auth/network work. */
55
- export function loadToolkitConfig(configPath: string = CONFIG_PATH): LoadedToolkitConfig {
56
- let raw: unknown;
57
- let format: ConfigDocumentSnapshot["format"] = "missing";
58
- const issues: ConfigIssue[] = [];
59
- const warnings: string[] = [];
60
- try {
61
- const text = fs.readFileSync(configPath, "utf8");
62
- try {
63
- raw = JSON.parse(text);
64
- if (!isRecord(raw)) {
65
- format = "invalid";
66
- issues.push(documentError("expected-object"));
67
- } else if (has(raw, "schemaVersion")) {
68
- format = raw.schemaVersion === 2 ? "v2" : "invalid";
69
- if (format === "invalid") issues.push(documentError("unsupported-schema-version"));
70
- } else if (has(raw, "defaults") || has(raw, "models") || has(raw, "diagnostics")) {
71
- format = "invalid";
72
- issues.push(documentError("missing-schema-version"));
73
- } else {
74
- format = "legacy";
75
- }
76
- } catch {
77
- format = "invalid";
78
- issues.push(documentError("invalid-json"));
79
- }
80
- } catch (error) {
81
- if (!isRecord(error) || error.code !== "ENOENT") {
82
- format = "invalid";
83
- issues.push(documentError("unreadable-config"));
84
- }
85
- }
86
- for (const issue of issues) warnings.push(`Ignoring ${configPath}: ${issue.code}.`);
87
- const legacy = normalizeLegacyConfig(format === "legacy" && isRecord(raw) ? raw : undefined, configPath, warnings);
88
- return {
89
- ...legacy,
90
- ...(format === "v2" ? { source: configPath } : {}),
91
- document: freeze({ format, configPath, raw, issues }),
92
- };
93
- }
94
-
95
- function configuredPath(value: string, configPath: string): string {
96
- if (value.startsWith("~/")) return path.join(os.homedir(), value.slice(2));
97
- return path.resolve(path.dirname(configPath), value);
98
- }
99
-
100
- function leafPaths(value: unknown, prefix = ""): string[] {
101
- if (!isRecord(value)) return [prefix];
102
- return Object.entries(value).flatMap(([key, child]) => leafPaths(child, prefix ? `${prefix}.${key}` : key));
103
- }
104
-
105
- const LEGACY_PATHS: Record<string, string> = {
106
- "context.mode": "compaction.contextManagement",
107
- "context.remoteCompaction.model": "compaction.remoteCompactModel",
108
- "context.remoteCompaction.inputSource": "compaction.remoteV2ContextSource",
109
- "context.remoteCompaction.allowContinuityBreak": "compaction.allowCompactionContinuityBreak",
110
- "context.remoteCompaction.apis": "compaction.responsesApis",
111
- "context.nativeFallback.enabled": "compaction.nativeFallback.enabled",
112
- "context.nativeFallback.model": "compaction.nativeFallback.model",
113
- "context.nativeFallback.thinkingLevel": "compaction.nativeFallback.thinkingLevel",
114
- "context.remoteWindows.leaveManagedMode": "compaction.leaveManagedMode",
115
- "context.remoteWindows.reminderThresholdPercent": "compaction.contextReminderThresholdPercent",
116
- "webSearch.route": "webSearch.models",
117
- "imageGeneration.enabled": "imageGeneration.enabled",
118
- "imageGeneration.defaultModel": "imageGeneration.models",
119
- "imageGeneration.allowedModels": "imageGeneration.models",
120
- "compatibility.transport": "compaction.gatewayContextModels",
121
- "diagnostics.level": "compaction.debug",
122
- "diagnostics.notifyOnLoad": "compaction.notifyOnLoad",
123
- "diagnostics.captureRequests": "compaction.logProviderPayloads",
124
- "diagnostics.captureResponses": "compaction.logCompactResponses",
125
- "diagnostics.redactSensitiveData": "compaction.redactSensitiveData",
126
- "diagnostics.artifactRoot": "compaction.artifactRoot",
127
- };
128
- function containsPath(value: unknown, fieldPath: string): boolean {
129
- for (const key of fieldPath.split(".")) {
130
- if (!isRecord(value) || !has(value, key)) return false;
131
- value = value[key];
132
- }
133
- return true;
134
- }
135
-
136
- function legacyRouteKey(raw: unknown, modelKey: string | undefined): string | undefined {
137
- if (!modelKey || !isRecord(raw) || !isRecord(raw.webSearch) || !isRecord(raw.webSearch.routes)) return undefined;
138
- return Object.keys(raw.webSearch.routes).find((key) => key.trim() === modelKey);
139
- }
140
-
141
- function legacyIssues(loaded: LoadedToolkitConfig, modelKey: string | undefined): ConfigIssue[] {
142
- const raw = loaded.document?.raw;
143
- const rawWeb = isRecord(raw) && isRecord(raw.webSearch) ? raw.webSearch : undefined;
144
- // Route keys may contain dots or colons. Never recover identity by splitting a warning.
145
- const routeIssues: ConfigIssue[] = [];
146
- if (isRecord(rawWeb?.routes)) {
147
- for (const [key, value] of Object.entries(rawWeb.routes)) {
148
- const normalized = key.trim();
149
- const separator = normalized.indexOf("/");
150
- const exact = separator > 0 && separator < normalized.length - 1 && !/[\s*?\[\]{}]/.test(normalized);
151
- if (exact && typeof value === "string" && ["local", "hosted", "standalone-alpha"].includes(value)) continue;
152
- routeIssues.push({ severity: "error", code: "invalid-legacy-value", feature: "webSearch",
153
- path: exact ? `webSearch.routes[${JSON.stringify(normalized).slice(0, 180)}]` : "webSearch.routes.[invalid-model-key]",
154
- modelKey: normalized });
155
- }
156
- }
157
- const issues = loaded.warnings.filter((warning) => !warning.startsWith("Ignoring webSearch.routes.")).map((warning): ConfigIssue => {
158
- // A removed feature is retired, not invalid: it never blocks another feature.
159
- if (warning.startsWith("Ignoring autoMode:")) {
160
- return { severity: "warning", code: RETIRED_AUTO_MODE_ISSUE, path: "autoMode", feature: "document" };
161
- }
162
- const match = /^Ignoring ([^:]+):/.exec(warning);
163
- const field = warning.startsWith("Ignoring compaction.responsesApis entry ") ? "compaction.responsesApis" : match?.[1] ?? "$";
164
- const root = field.split(".")[0];
165
- const diagnostic = Object.entries(LEGACY_PATHS).some(([leaf, input]) => leaf.startsWith("diagnostics.") && input === field);
166
- const feature: ConfigFeature | "document" = diagnostic ? "diagnostics"
167
- : field === "compaction.gatewayContextModels" ? "compatibility" : root === "compaction" ? "context"
168
- : root === "webSearch" || root === "imageGeneration" ? root : "document";
169
- const supersededDefault = field === "webSearch.defaultRoute" && legacyRouteKey(raw, modelKey) !== undefined;
170
- const warningOnly = warning.endsWith("unknown field.") || !match
171
- || warning.includes("expected at least one model id") || supersededDefault;
172
- return {
173
- severity: warningOnly ? "warning" : "error",
174
- code: warning.endsWith("unknown field.") ? "unknown-legacy-field" : warningOnly ? "legacy-normalization" : "invalid-legacy-value",
175
- path: warning.endsWith("unknown field.") ? `${feature}.[unknown-field]` : field.slice(0, 220),
176
- feature,
177
- };
178
- });
179
- return [...issues, ...routeIssues];
180
- }
181
-
182
- function selectedInvalidFeatures(issues: ConfigIssue[], modelKey?: string): ConfigFeature[] {
183
- const invalid = new Set<ConfigFeature>();
184
- for (const issue of issues) {
185
- if (issue.severity !== "error" || (issue.modelKey !== undefined && issue.modelKey !== modelKey)) continue;
186
- if (issue.feature === "document") for (const feature of CONFIG_FEATURES) invalid.add(feature);
187
- else invalid.add(issue.feature);
188
- }
189
- return [...invalid];
190
- }
191
-
192
- function fromLegacy(loaded: LoadedToolkitConfig, model: WebSearchModel | undefined): ResolvedToolkitPolicy {
193
- const config = loaded.config;
194
- const compaction = config.compaction;
195
- const modelKey = getExactModelKey(model);
196
- const route = resolveWebSearchRoute({ model, config: config.webSearch });
197
- const policy = createPolicyDefaults();
198
- policy.context = {
199
- mode: !compaction.enabled ? "pi" : compaction.contextManagement === "remote" ? "remote-windows" : "remote-compaction",
200
- remoteCompaction: {
201
- model: compaction.remoteCompactModel ?? null,
202
- inputSource: compaction.remoteV2ContextSource,
203
- allowContinuityBreak: compaction.allowCompactionContinuityBreak,
204
- apis: [...compaction.responsesApis],
205
- },
206
- nativeFallback: { ...compaction.nativeFallback, model: compaction.nativeFallback.model ?? null },
207
- remoteWindows: { leaveManagedMode: compaction.leaveManagedMode, reminderThresholdPercent: compaction.contextReminderThresholdPercent },
208
- };
209
- policy.webSearch.route = route.route === "none" ? "unmanaged" : route.route;
210
- policy.imageGeneration = {
211
- enabled: config.imageGeneration.enabled,
212
- defaultModel: config.imageGeneration.models[0],
213
- allowedModels: [...config.imageGeneration.models],
214
- };
215
- policy.responsesWebSocket = { enabled: false };
216
- policy.compatibility.transport = modelKey && compaction.gatewayContextModels.includes(modelKey) ? "codex-gateway" : "standard";
217
- policy.diagnostics = {
218
- level: compaction.debug ? "debug" : "info",
219
- notifyOnLoad: compaction.notifyOnLoad,
220
- captureRequests: compaction.logProviderPayloads,
221
- captureResponses: compaction.logCompactResponses,
222
- redactSensitiveData: compaction.redactSensitiveData,
223
- artifactRoot: compaction.artifactRoot,
224
- };
225
- const origins: Record<string, ConfigOrigin> = Object.create(null);
226
- const raw = loaded.document?.raw;
227
- for (const leaf of leafPaths(policy)) {
228
- let input = LEGACY_PATHS[leaf] ?? leaf;
229
- if (leaf === "context.mode" && !compaction.enabled) input = "compaction.enabled";
230
- if (leaf === "webSearch.route") {
231
- if (!config.webSearch.enabled) input = "webSearch.enabled";
232
- else if (route.source === "exact") {
233
- const rawKey = legacyRouteKey(raw, modelKey);
234
- if (rawKey !== undefined) {
235
- origins[leaf] = { kind: "legacy", path: `webSearch.routes[${JSON.stringify(rawKey)}]`, source: loaded.source };
236
- continue;
237
- }
238
- }
239
- else if (route.source === "default") input = "webSearch.defaultRoute";
240
- }
241
- const ignored = loaded.warnings.some((warning) => warning.startsWith(`Ignoring ${input}:`));
242
- origins[leaf] = !ignored && ((raw && containsPath(raw, input)) || (!loaded.document && loaded.source))
243
- ? { kind: "legacy", path: input, source: loaded.source } : { kind: "builtin" };
244
- }
245
- const issues = [...(loaded.document?.issues ?? []), ...legacyIssues(loaded, modelKey)];
246
- return { policy, origins, issues, invalidFeatures: selectedInvalidFeatures(issues, modelKey) };
247
- }
248
-
249
- /** Internal engine adapter. All interpretation of document names stays in this module. */
250
- function engineConfig(policy: EffectiveToolkitPolicy, gatewayKeys: string[]): ToolkitConfig {
251
- const { context, diagnostics, imageGeneration } = policy;
252
- return {
253
- compaction: {
254
- enabled: context.mode !== "pi",
255
- contextManagement: context.mode === "remote-windows" ? "remote" : "off",
256
- remoteCompactModel: context.remoteCompaction.model ?? undefined,
257
- remoteV2ContextSource: context.remoteCompaction.inputSource,
258
- allowCompactionContinuityBreak: context.remoteCompaction.allowContinuityBreak,
259
- responsesApis: [...context.remoteCompaction.apis],
260
- gatewayContextModels: [...gatewayKeys],
261
- nativeFallback: { ...context.nativeFallback, model: context.nativeFallback.model ?? undefined },
262
- leaveManagedMode: context.remoteWindows.leaveManagedMode,
263
- contextReminderThresholdPercent: context.remoteWindows.reminderThresholdPercent,
264
- debug: diagnostics.level === "debug",
265
- notifyOnLoad: diagnostics.notifyOnLoad,
266
- logProviderPayloads: diagnostics.captureRequests,
267
- logCompactResponses: diagnostics.captureResponses,
268
- redactSensitiveData: diagnostics.redactSensitiveData,
269
- artifactRoot: diagnostics.artifactRoot,
270
- },
271
- webSearch: policy.webSearch.route === "unmanaged" ? { enabled: false, models: [] }
272
- : { enabled: true, models: [], defaultRoute: policy.webSearch.route },
273
- imageGeneration: { enabled: imageGeneration.enabled, models: [...imageGeneration.allowedModels] },
274
- };
275
- }
276
-
277
- /** Resolve one immutable snapshot for one model. Never reads disk or a model registry. */
278
- export function resolveToolkitConfig(loaded: LoadedToolkitConfig, model?: ExactModelScopeModel & { api?: string }): ResolvedToolkitConfig {
279
- // Injected readers are also decoded at the module seam; they may omit optional feature sections.
280
- if (!loaded.document) {
281
- const configPath = loaded.source ?? CONFIG_PATH;
282
- const normalized = normalizeLegacyConfig(loaded.config, configPath);
283
- loaded = { ...normalized, source: loaded.source, warnings: [...loaded.warnings, ...normalized.warnings],
284
- document: { format: "legacy", configPath, raw: loaded.config, issues: [] } };
285
- }
286
- const modelKey = getExactModelKey(model);
287
- const format = loaded.document?.format ?? "legacy";
288
- const v2 = format === "v2" ? resolveV2Config(loaded.document?.raw, modelKey) : undefined;
289
- const resolution = v2 ?? fromLegacy(loaded, model);
290
- const source = loaded.source;
291
- if (format === "v2") {
292
- resolution.policy.diagnostics.artifactRoot = configuredPath(resolution.policy.diagnostics.artifactRoot, loaded.document?.configPath ?? CONFIG_PATH);
293
- for (const origin of Object.values(resolution.origins)) if (origin.kind !== "builtin") origin.source = source;
294
- }
295
- const gatewayModelKeys = v2 ? v2.gatewayModelKeys : [...loaded.config.compaction.gatewayContextModels];
296
- const config = format === "v2" ? engineConfig(resolution.policy, gatewayModelKeys) : structuredClone(loaded.config);
297
- return freeze({ ...resolution, config, format, source, modelKey, gatewayModelKeys, snapshot: structuredClone(loaded) });
298
- }
299
-
300
- export class ToolkitConfigurationError extends Error {
301
- constructor(readonly features: readonly ConfigFeature[]) {
302
- super(`Toolkit configuration is invalid for ${features.join(", ")}; use /toolkit-config validate. No fallback policy was selected.`);
303
- this.name = "ToolkitConfigurationError";
304
- }
305
- }
306
-
307
- export function assertConfigValid(resolved: ResolvedToolkitConfig, ...features: ConfigFeature[]): void {
308
- const invalid = features.filter((feature) => resolved.invalidFeatures.includes(feature));
309
- if (invalid.length > 0) throw new ToolkitConfigurationError(invalid);
310
- }
1
+ import * as fs from "node:fs";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import { getExactModelKey, type ExactModelScopeModel } from "./model-scope";
5
+ import { resolveWebSearchRoute, type WebSearchModel } from "./web-search/types";
6
+ import { CONFIG_PATH, normalizeLegacyConfig } from "./config/legacy";
7
+ import { resolveV2Config } from "./config/v2";
8
+ import {
9
+ CONFIG_FEATURES,
10
+ RETIRED_AUTO_MODE_ISSUE,
11
+ createPolicyDefaults,
12
+ type ConfigDocumentSnapshot,
13
+ type ConfigFeature,
14
+ type ConfigIssue,
15
+ type ConfigOrigin,
16
+ type EffectiveToolkitPolicy,
17
+ type ResolvedToolkitPolicy,
18
+ } from "./config/policy";
19
+ import type { LoadedToolkitConfig, ToolkitConfig } from "./types";
20
+
21
+ export { CONFIG_DIR, CONFIG_PATH } from "./config/legacy";
22
+ export { DEFAULT_TOOLKIT_CONFIG } from "./types";
23
+ export type { ConfigFeature, ConfigIssue, ConfigOrigin, EffectiveToolkitPolicy } from "./config/policy";
24
+
25
+ export type ResolvedToolkitConfig = ResolvedToolkitPolicy & {
26
+ /** Narrow adapters for existing feature engines, never raw document sections. */
27
+ config: ToolkitConfig;
28
+ format: ConfigDocumentSnapshot["format"];
29
+ source?: string;
30
+ modelKey?: string;
31
+ /** Cross-feature protocol policy, independent of context enablement. */
32
+ gatewayModelKeys: string[];
33
+ /** Same decoded document for resolving another identity within this operation. */
34
+ snapshot: LoadedToolkitConfig;
35
+ };
36
+
37
+ function isRecord(value: unknown): value is Record<string, unknown> {
38
+ return !!value && typeof value === "object" && !Array.isArray(value);
39
+ }
40
+ function has(value: Record<string, unknown>, key: string): boolean {
41
+ return Object.prototype.hasOwnProperty.call(value, key);
42
+ }
43
+ function freeze<T>(value: T): T {
44
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
45
+ Object.freeze(value);
46
+ for (const item of Object.values(value)) freeze(item);
47
+ }
48
+ return value;
49
+ }
50
+ function documentError(code: string): ConfigIssue {
51
+ return { severity: "error", code, path: "$", feature: "document" };
52
+ }
53
+
54
+ /** Read exactly one global file. No project discovery, cache, writes or auth/network work. */
55
+ export function loadToolkitConfig(configPath: string = CONFIG_PATH): LoadedToolkitConfig {
56
+ let raw: unknown;
57
+ let format: ConfigDocumentSnapshot["format"] = "missing";
58
+ const issues: ConfigIssue[] = [];
59
+ const warnings: string[] = [];
60
+ try {
61
+ const text = fs.readFileSync(configPath, "utf8");
62
+ try {
63
+ raw = JSON.parse(text);
64
+ if (!isRecord(raw)) {
65
+ format = "invalid";
66
+ issues.push(documentError("expected-object"));
67
+ } else if (has(raw, "schemaVersion")) {
68
+ format = raw.schemaVersion === 2 ? "v2" : "invalid";
69
+ if (format === "invalid") issues.push(documentError("unsupported-schema-version"));
70
+ } else if (has(raw, "defaults") || has(raw, "models") || has(raw, "diagnostics")) {
71
+ format = "invalid";
72
+ issues.push(documentError("missing-schema-version"));
73
+ } else {
74
+ format = "legacy";
75
+ }
76
+ } catch {
77
+ format = "invalid";
78
+ issues.push(documentError("invalid-json"));
79
+ }
80
+ } catch (error) {
81
+ if (!isRecord(error) || error.code !== "ENOENT") {
82
+ format = "invalid";
83
+ issues.push(documentError("unreadable-config"));
84
+ }
85
+ }
86
+ for (const issue of issues) warnings.push(`Ignoring ${configPath}: ${issue.code}.`);
87
+ const legacy = normalizeLegacyConfig(format === "legacy" && isRecord(raw) ? raw : undefined, configPath, warnings);
88
+ return {
89
+ ...legacy,
90
+ ...(format === "v2" ? { source: configPath } : {}),
91
+ document: freeze({ format, configPath, raw, issues }),
92
+ };
93
+ }
94
+
95
+ function configuredPath(value: string, configPath: string): string {
96
+ if (value.startsWith("~/")) return path.join(os.homedir(), value.slice(2));
97
+ return path.resolve(path.dirname(configPath), value);
98
+ }
99
+
100
+ function leafPaths(value: unknown, prefix = ""): string[] {
101
+ if (!isRecord(value)) return [prefix];
102
+ return Object.entries(value).flatMap(([key, child]) => leafPaths(child, prefix ? `${prefix}.${key}` : key));
103
+ }
104
+
105
+ const LEGACY_PATHS: Record<string, string> = {
106
+ "context.mode": "compaction.contextManagement",
107
+ "context.remoteCompaction.model": "compaction.remoteCompactModel",
108
+ "context.remoteCompaction.inputSource": "compaction.remoteV2ContextSource",
109
+ "context.remoteCompaction.allowContinuityBreak": "compaction.allowCompactionContinuityBreak",
110
+ "context.remoteCompaction.apis": "compaction.responsesApis",
111
+ "context.nativeFallback.enabled": "compaction.nativeFallback.enabled",
112
+ "context.nativeFallback.model": "compaction.nativeFallback.model",
113
+ "context.nativeFallback.thinkingLevel": "compaction.nativeFallback.thinkingLevel",
114
+ "context.remoteWindows.leaveManagedMode": "compaction.leaveManagedMode",
115
+ "context.remoteWindows.reminderThresholdPercent": "compaction.contextReminderThresholdPercent",
116
+ "webSearch.route": "webSearch.models",
117
+ "imageGeneration.enabled": "imageGeneration.enabled",
118
+ "imageGeneration.defaultModel": "imageGeneration.models",
119
+ "imageGeneration.allowedModels": "imageGeneration.models",
120
+ "compatibility.transport": "compaction.gatewayContextModels",
121
+ "diagnostics.level": "compaction.debug",
122
+ "diagnostics.notifyOnLoad": "compaction.notifyOnLoad",
123
+ "diagnostics.captureRequests": "compaction.logProviderPayloads",
124
+ "diagnostics.captureResponses": "compaction.logCompactResponses",
125
+ "diagnostics.redactSensitiveData": "compaction.redactSensitiveData",
126
+ "diagnostics.artifactRoot": "compaction.artifactRoot",
127
+ };
128
+ function containsPath(value: unknown, fieldPath: string): boolean {
129
+ for (const key of fieldPath.split(".")) {
130
+ if (!isRecord(value) || !has(value, key)) return false;
131
+ value = value[key];
132
+ }
133
+ return true;
134
+ }
135
+
136
+ function legacyRouteKey(raw: unknown, modelKey: string | undefined): string | undefined {
137
+ if (!modelKey || !isRecord(raw) || !isRecord(raw.webSearch) || !isRecord(raw.webSearch.routes)) return undefined;
138
+ return Object.keys(raw.webSearch.routes).find((key) => key.trim() === modelKey);
139
+ }
140
+
141
+ function legacyIssues(loaded: LoadedToolkitConfig, modelKey: string | undefined): ConfigIssue[] {
142
+ const raw = loaded.document?.raw;
143
+ const rawWeb = isRecord(raw) && isRecord(raw.webSearch) ? raw.webSearch : undefined;
144
+ // Route keys may contain dots or colons. Never recover identity by splitting a warning.
145
+ const routeIssues: ConfigIssue[] = [];
146
+ if (isRecord(rawWeb?.routes)) {
147
+ for (const [key, value] of Object.entries(rawWeb.routes)) {
148
+ const normalized = key.trim();
149
+ const separator = normalized.indexOf("/");
150
+ const exact = separator > 0 && separator < normalized.length - 1 && !/[\s*?\[\]{}]/.test(normalized);
151
+ if (exact && typeof value === "string" && ["local", "hosted", "standalone-alpha"].includes(value)) continue;
152
+ routeIssues.push({ severity: "error", code: "invalid-legacy-value", feature: "webSearch",
153
+ path: exact ? `webSearch.routes[${JSON.stringify(normalized).slice(0, 180)}]` : "webSearch.routes.[invalid-model-key]",
154
+ modelKey: normalized });
155
+ }
156
+ }
157
+ const issues = loaded.warnings.filter((warning) => !warning.startsWith("Ignoring webSearch.routes.")).map((warning): ConfigIssue => {
158
+ // A removed feature is retired, not invalid: it never blocks another feature.
159
+ if (warning.startsWith("Ignoring autoMode:")) {
160
+ return { severity: "warning", code: RETIRED_AUTO_MODE_ISSUE, path: "autoMode", feature: "document" };
161
+ }
162
+ const match = /^Ignoring ([^:]+):/.exec(warning);
163
+ const field = warning.startsWith("Ignoring compaction.responsesApis entry ") ? "compaction.responsesApis" : match?.[1] ?? "$";
164
+ const root = field.split(".")[0];
165
+ const diagnostic = Object.entries(LEGACY_PATHS).some(([leaf, input]) => leaf.startsWith("diagnostics.") && input === field);
166
+ const feature: ConfigFeature | "document" = diagnostic ? "diagnostics"
167
+ : field === "compaction.gatewayContextModels" ? "compatibility" : root === "compaction" ? "context"
168
+ : root === "webSearch" || root === "imageGeneration" ? root : "document";
169
+ const supersededDefault = field === "webSearch.defaultRoute" && legacyRouteKey(raw, modelKey) !== undefined;
170
+ const warningOnly = warning.endsWith("unknown field.") || !match
171
+ || warning.includes("expected at least one model id") || supersededDefault;
172
+ return {
173
+ severity: warningOnly ? "warning" : "error",
174
+ code: warning.endsWith("unknown field.") ? "unknown-legacy-field" : warningOnly ? "legacy-normalization" : "invalid-legacy-value",
175
+ path: warning.endsWith("unknown field.") ? `${feature}.[unknown-field]` : field.slice(0, 220),
176
+ feature,
177
+ };
178
+ });
179
+ return [...issues, ...routeIssues];
180
+ }
181
+
182
+ function selectedInvalidFeatures(issues: ConfigIssue[], modelKey?: string): ConfigFeature[] {
183
+ const invalid = new Set<ConfigFeature>();
184
+ for (const issue of issues) {
185
+ if (issue.severity !== "error" || (issue.modelKey !== undefined && issue.modelKey !== modelKey)) continue;
186
+ if (issue.feature === "document") for (const feature of CONFIG_FEATURES) invalid.add(feature);
187
+ else invalid.add(issue.feature);
188
+ }
189
+ return [...invalid];
190
+ }
191
+
192
+ function fromLegacy(loaded: LoadedToolkitConfig, model: WebSearchModel | undefined): ResolvedToolkitPolicy {
193
+ const config = loaded.config;
194
+ const compaction = config.compaction;
195
+ const modelKey = getExactModelKey(model);
196
+ const route = resolveWebSearchRoute({ model, config: config.webSearch });
197
+ const policy = createPolicyDefaults();
198
+ policy.context = {
199
+ mode: !compaction.enabled ? "pi" : compaction.contextManagement === "remote" ? "remote-windows" : "remote-compaction",
200
+ remoteCompaction: {
201
+ model: compaction.remoteCompactModel ?? null,
202
+ inputSource: compaction.remoteV2ContextSource,
203
+ allowContinuityBreak: compaction.allowCompactionContinuityBreak,
204
+ apis: [...compaction.responsesApis],
205
+ },
206
+ nativeFallback: { ...compaction.nativeFallback, model: compaction.nativeFallback.model ?? null },
207
+ remoteWindows: { leaveManagedMode: compaction.leaveManagedMode, reminderThresholdPercent: compaction.contextReminderThresholdPercent },
208
+ };
209
+ policy.webSearch.route = route.route === "none" ? "unmanaged" : route.route;
210
+ policy.imageGeneration = {
211
+ enabled: config.imageGeneration.enabled,
212
+ defaultModel: config.imageGeneration.models[0],
213
+ allowedModels: [...config.imageGeneration.models],
214
+ };
215
+ policy.responsesWebSocket = { enabled: false };
216
+ policy.compatibility.transport = modelKey && compaction.gatewayContextModels.includes(modelKey) ? "codex-gateway" : "standard";
217
+ policy.diagnostics = {
218
+ level: compaction.debug ? "debug" : "info",
219
+ notifyOnLoad: compaction.notifyOnLoad,
220
+ captureRequests: compaction.logProviderPayloads,
221
+ captureResponses: compaction.logCompactResponses,
222
+ redactSensitiveData: compaction.redactSensitiveData,
223
+ artifactRoot: compaction.artifactRoot,
224
+ };
225
+ const origins: Record<string, ConfigOrigin> = Object.create(null);
226
+ const raw = loaded.document?.raw;
227
+ for (const leaf of leafPaths(policy)) {
228
+ let input = LEGACY_PATHS[leaf] ?? leaf;
229
+ if (leaf === "context.mode" && !compaction.enabled) input = "compaction.enabled";
230
+ if (leaf === "webSearch.route") {
231
+ if (!config.webSearch.enabled) input = "webSearch.enabled";
232
+ else if (route.source === "exact") {
233
+ const rawKey = legacyRouteKey(raw, modelKey);
234
+ if (rawKey !== undefined) {
235
+ origins[leaf] = { kind: "legacy", path: `webSearch.routes[${JSON.stringify(rawKey)}]`, source: loaded.source };
236
+ continue;
237
+ }
238
+ }
239
+ else if (route.source === "default") input = "webSearch.defaultRoute";
240
+ }
241
+ const ignored = loaded.warnings.some((warning) => warning.startsWith(`Ignoring ${input}:`));
242
+ origins[leaf] = !ignored && ((raw && containsPath(raw, input)) || (!loaded.document && loaded.source))
243
+ ? { kind: "legacy", path: input, source: loaded.source } : { kind: "builtin" };
244
+ }
245
+ const issues = [...(loaded.document?.issues ?? []), ...legacyIssues(loaded, modelKey)];
246
+ return { policy, origins, issues, invalidFeatures: selectedInvalidFeatures(issues, modelKey) };
247
+ }
248
+
249
+ /** Internal engine adapter. All interpretation of document names stays in this module. */
250
+ function engineConfig(policy: EffectiveToolkitPolicy, gatewayKeys: string[]): ToolkitConfig {
251
+ const { context, diagnostics, imageGeneration } = policy;
252
+ return {
253
+ compaction: {
254
+ enabled: context.mode !== "pi",
255
+ contextManagement: context.mode === "remote-windows" ? "remote" : "off",
256
+ remoteCompactModel: context.remoteCompaction.model ?? undefined,
257
+ remoteV2ContextSource: context.remoteCompaction.inputSource,
258
+ allowCompactionContinuityBreak: context.remoteCompaction.allowContinuityBreak,
259
+ responsesApis: [...context.remoteCompaction.apis],
260
+ gatewayContextModels: [...gatewayKeys],
261
+ nativeFallback: { ...context.nativeFallback, model: context.nativeFallback.model ?? undefined },
262
+ leaveManagedMode: context.remoteWindows.leaveManagedMode,
263
+ contextReminderThresholdPercent: context.remoteWindows.reminderThresholdPercent,
264
+ debug: diagnostics.level === "debug",
265
+ notifyOnLoad: diagnostics.notifyOnLoad,
266
+ logProviderPayloads: diagnostics.captureRequests,
267
+ logCompactResponses: diagnostics.captureResponses,
268
+ redactSensitiveData: diagnostics.redactSensitiveData,
269
+ artifactRoot: diagnostics.artifactRoot,
270
+ },
271
+ webSearch: policy.webSearch.route === "unmanaged" ? { enabled: false, models: [] }
272
+ : { enabled: true, models: [], defaultRoute: policy.webSearch.route },
273
+ imageGeneration: { enabled: imageGeneration.enabled, models: [...imageGeneration.allowedModels] },
274
+ };
275
+ }
276
+
277
+ /** Resolve one immutable snapshot for one model. Never reads disk or a model registry. */
278
+ export function resolveToolkitConfig(loaded: LoadedToolkitConfig, model?: ExactModelScopeModel & { api?: string }): ResolvedToolkitConfig {
279
+ // Injected readers are also decoded at the module seam; they may omit optional feature sections.
280
+ if (!loaded.document) {
281
+ const configPath = loaded.source ?? CONFIG_PATH;
282
+ const normalized = normalizeLegacyConfig(loaded.config, configPath);
283
+ loaded = { ...normalized, source: loaded.source, warnings: [...loaded.warnings, ...normalized.warnings],
284
+ document: { format: "legacy", configPath, raw: loaded.config, issues: [] } };
285
+ }
286
+ const modelKey = getExactModelKey(model);
287
+ const format = loaded.document?.format ?? "legacy";
288
+ const v2 = format === "v2" ? resolveV2Config(loaded.document?.raw, modelKey) : undefined;
289
+ const resolution = v2 ?? fromLegacy(loaded, model);
290
+ const source = loaded.source;
291
+ if (format === "v2") {
292
+ resolution.policy.diagnostics.artifactRoot = configuredPath(resolution.policy.diagnostics.artifactRoot, loaded.document?.configPath ?? CONFIG_PATH);
293
+ for (const origin of Object.values(resolution.origins)) if (origin.kind !== "builtin") origin.source = source;
294
+ }
295
+ const gatewayModelKeys = v2 ? v2.gatewayModelKeys : [...loaded.config.compaction.gatewayContextModels];
296
+ const config = format === "v2" ? engineConfig(resolution.policy, gatewayModelKeys) : structuredClone(loaded.config);
297
+ return freeze({ ...resolution, config, format, source, modelKey, gatewayModelKeys, snapshot: structuredClone(loaded) });
298
+ }
299
+
300
+ export class ToolkitConfigurationError extends Error {
301
+ constructor(readonly features: readonly ConfigFeature[]) {
302
+ super(`Toolkit configuration is invalid for ${features.join(", ")}; use /toolkit-config validate. No fallback policy was selected.`);
303
+ this.name = "ToolkitConfigurationError";
304
+ }
305
+ }
306
+
307
+ export function assertConfigValid(resolved: ResolvedToolkitConfig, ...features: ConfigFeature[]): void {
308
+ const invalid = features.filter((feature) => resolved.invalidFeatures.includes(feature));
309
+ if (invalid.length > 0) throw new ToolkitConfigurationError(invalid);
310
+ }