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