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,519 +1,469 @@
1
- import * as os from "node:os";
2
- import * as path from "node:path";
3
- import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
4
- import {
5
- DEFAULT_COMPACTION_CONFIG,
6
- DEFAULT_TOOLKIT_CONFIG,
7
- DEFAULT_IMAGE_GENERATION_CONFIG,
8
- DEFAULT_IMAGE_GENERATION_MODEL,
9
- DEFAULT_NATIVE_FALLBACK_CONFIG,
10
- DEFAULT_WEB_SEARCH_CONFIG,
11
- RESPONSES_COMPACT_CAPABLE_APIS,
12
- THINKING_LEVELS,
13
- TOOLKIT_ID,
14
- type CompactionConfig,
15
- type ContextManagementMode,
16
- type LeaveManagedModePolicy,
17
- type ImageGenerationConfig,
18
- type RemoteV2ContextSource,
19
- type LoadedToolkitConfig,
20
- type NativeFallbackConfig,
21
- type ToolkitConfig,
22
- type WebSearchConfig,
23
- type WebSearchRoute,
24
- } from "../types";
25
- import { RETIRED_AUTO_MODE_MESSAGE } from "./policy";
26
- import { MAX_IMAGE_MODEL_ID_CHARS } from "../image-generation/types";
27
-
28
- export const CONFIG_DIR = path.join(os.homedir(), ".pi", "agent", "extensions", TOOLKIT_ID);
29
- export const CONFIG_PATH = path.join(CONFIG_DIR, "config.json");
30
-
31
- const TOP_LEVEL_FIELDS = new Set(["compaction", "webSearch", "imageGeneration", "autoMode"]);
32
- const COMPACTION_FIELDS = new Set([
33
- "enabled",
34
- "leaveManagedMode",
35
- "contextManagement",
36
- "allowCompactionContinuityBreak",
37
- "remoteCompactModel",
38
- "remoteV2ContextSource",
39
- "nativeFallback",
40
- "responsesApis",
41
- "gatewayContextModels",
42
- "contextReminderThresholdPercent",
43
- "notifyOnLoad",
44
- "debug",
45
- "logProviderPayloads",
46
- "logCompactResponses",
47
- "redactSensitiveData",
48
- "artifactRoot",
49
- ]);
50
- const NATIVE_FALLBACK_FIELDS = new Set(["enabled", "model", "thinkingLevel"]);
51
- const WEB_SEARCH_FIELDS = new Set(["enabled", "models", "defaultRoute", "routes"]);
52
- const IMAGE_GENERATION_FIELDS = new Set(["enabled", "models"]);
53
-
54
- function isRecord(value: unknown): value is Record<string, unknown> {
55
- return !!value && typeof value === "object" && !Array.isArray(value);
56
- }
57
-
58
- function resolveConfiguredPath(rawPath: string, baseDir: string): string {
59
- if (rawPath.startsWith("~/")) {
60
- return path.join(os.homedir(), rawPath.slice(2));
61
- }
62
- if (path.isAbsolute(rawPath)) {
63
- return path.resolve(rawPath);
64
- }
65
- return path.resolve(baseDir, rawPath);
66
- }
67
-
68
- function warnUnknownFields(
69
- value: Record<string, unknown>,
70
- knownFields: ReadonlySet<string>,
71
- fieldPath: string,
72
- warnings: string[],
73
- ): void {
74
- for (const key of Object.keys(value)) {
75
- if (!knownFields.has(key)) {
76
- warnings.push(`Ignoring ${fieldPath ? `${fieldPath}.` : ""}${key}: unknown field.`);
77
- }
78
- }
79
- }
80
-
81
- function toBoolean(value: unknown, fieldPath: string, warnings: string[]): boolean | undefined {
82
- if (value === undefined) return undefined;
83
- if (typeof value === "boolean") return value;
84
- warnings.push(`Ignoring ${fieldPath}: expected a boolean.`);
85
- return undefined;
86
- }
87
-
88
- function toLeaveManagedMode(
89
- value: unknown,
90
- fieldPath: string,
91
- warnings: string[],
92
- ): LeaveManagedModePolicy | undefined {
93
- if (value === undefined) return undefined;
94
- if (typeof value === "string") {
95
- const normalized = value.trim();
96
- if (normalized === "warn" || normalized === "compact") return normalized;
97
- }
98
- warnings.push(`Ignoring ${fieldPath}: expected one of warn, compact.`);
99
- return undefined;
100
- }
101
-
102
- function toContextManagementMode(
103
- value: unknown,
104
- fieldPath: string,
105
- warnings: string[],
106
- ): ContextManagementMode | undefined {
107
- if (value === undefined) return undefined;
108
- if (typeof value === "string") {
109
- const normalized = value.trim();
110
- if (normalized === "off" || normalized === "remote") return normalized;
111
- }
112
- warnings.push(`Ignoring ${fieldPath}: expected one of off, remote.`);
113
- return undefined;
114
- }
115
-
116
- function toRemoteV2ContextSource(
117
- value: unknown,
118
- fieldPath: string,
119
- warnings: string[],
120
- ): RemoteV2ContextSource | undefined {
121
- if (value === undefined) return undefined;
122
- if (typeof value === "string") {
123
- const normalized = value.trim();
124
- if (normalized === "pi-context-hook" || normalized === "legacy") return normalized;
125
- }
126
- warnings.push(`Ignoring ${fieldPath}: expected one of pi-context-hook, legacy.`);
127
- return undefined;
128
- }
129
-
130
- function toModelSpec(value: unknown, fieldPath: string, warnings: string[]): string | null | undefined {
131
- if (value === undefined) return undefined;
132
- if (value === null) return null;
133
- if (typeof value === "string" && value.trim().length > 0) {
134
- return value.trim();
135
- }
136
- warnings.push(`Ignoring ${fieldPath}: expected "provider/model-id" or null.`);
137
- return undefined;
138
- }
139
-
140
- function toThinkingLevel(value: unknown, fieldPath: string, warnings: string[]): ThinkingLevel | undefined {
141
- if (value === undefined) return undefined;
142
- if (typeof value === "string" && (THINKING_LEVELS as readonly string[]).includes(value)) {
143
- return value as ThinkingLevel;
144
- }
145
- warnings.push(`Ignoring ${fieldPath}: expected one of ${THINKING_LEVELS.join(", ")}.`);
146
- return undefined;
147
- }
148
-
149
- function toBoundedInteger(
150
- value: unknown,
151
- fieldPath: string,
152
- warnings: string[],
153
- min: number,
154
- max: number,
155
- ): number | undefined {
156
- if (value === undefined) return undefined;
157
- if (typeof value === "number" && Number.isInteger(value) && Number.isFinite(value) && value >= min && value <= max) {
158
- return value;
159
- }
160
- warnings.push(`Ignoring ${fieldPath}: expected an integer between ${min} and ${max}.`);
161
- return undefined;
162
- }
163
-
164
- function toSupportedApis(
165
- value: unknown,
166
- fieldPath: string,
167
- capableApis: readonly string[],
168
- warnings: string[],
169
- ): string[] | undefined {
170
- if (value === undefined) return undefined;
171
- if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
172
- warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
173
- return undefined;
174
- }
175
-
176
- const capable = new Set(capableApis);
177
- const accepted: string[] = [];
178
- for (const item of new Set(value.map((entry) => entry.trim()).filter(Boolean))) {
179
- if (capable.has(item)) {
180
- accepted.push(item);
181
- } else {
182
- warnings.push(
183
- `Ignoring ${fieldPath} entry "${item}": only ${capableApis.join(", ")} are supported.`,
184
- );
185
- }
186
- }
187
-
188
- return accepted;
189
- }
190
-
191
- function toStringList(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
192
- if (value === undefined) return undefined;
193
- if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
194
- warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
195
- return undefined;
196
- }
197
-
198
- return [...new Set(value.map((entry) => entry.trim()).filter(Boolean))];
199
- }
200
-
201
- function toImageGenerationModels(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
202
- const models = toStringList(value, fieldPath, warnings);
203
- if (models === undefined) return undefined;
204
- if (models.some((model) => model.length > MAX_IMAGE_MODEL_ID_CHARS)) {
205
- warnings.push(
206
- `Ignoring ${fieldPath}: each model id must be at most ${MAX_IMAGE_MODEL_ID_CHARS} characters.`,
207
- );
208
- return undefined;
209
- }
210
- return models;
211
- }
212
-
213
- function toWebSearchRoute(
214
- value: unknown,
215
- fieldPath: string,
216
- warnings: string[],
217
- ): WebSearchRoute | undefined {
218
- if (value === undefined) return undefined;
219
- if (value === "local" || value === "hosted" || value === "standalone-alpha") {
220
- return value;
221
- }
222
- warnings.push(`Ignoring ${fieldPath}: expected one of local, hosted, standalone-alpha.`);
223
- return undefined;
224
- }
225
-
226
- function isExactWebSearchModelKey(value: string): boolean {
227
- const trimmed = value.trim();
228
- const separatorIndex = trimmed.indexOf("/");
229
- if (separatorIndex <= 0 || separatorIndex >= trimmed.length - 1) return false;
230
- if (/[\s*?\[\]{}]/.test(trimmed)) return false;
231
- const provider = trimmed.slice(0, separatorIndex);
232
- const modelId = trimmed.slice(separatorIndex + 1);
233
- return provider.length > 0 && modelId.length > 0;
234
- }
235
-
236
- function toWebSearchRoutes(
237
- value: unknown,
238
- fieldPath: string,
239
- warnings: string[],
240
- ): Record<string, WebSearchRoute> | undefined {
241
- if (value === undefined) return undefined;
242
- if (!isRecord(value)) {
243
- warnings.push(`Ignoring ${fieldPath}: expected a JSON object.`);
244
- return undefined;
245
- }
246
-
247
- const routes: Record<string, WebSearchRoute> = {};
248
- for (const [rawKey, rawRoute] of Object.entries(value)) {
249
- const key = rawKey.trim();
250
- if (!isExactWebSearchModelKey(key)) {
251
- warnings.push(`Ignoring ${fieldPath}.${rawKey}: expected an exact "provider/model-id" key.`);
252
- continue;
253
- }
254
- const route = toWebSearchRoute(rawRoute, `${fieldPath}.${rawKey}`, warnings);
255
- if (route !== undefined) routes[key] = route;
256
- }
257
- return routes;
258
- }
259
-
260
- function cloneDefaults(): ToolkitConfig {
261
- return {
262
- compaction: {
263
- ...DEFAULT_COMPACTION_CONFIG,
264
- nativeFallback: { ...DEFAULT_NATIVE_FALLBACK_CONFIG },
265
- responsesApis: [...DEFAULT_COMPACTION_CONFIG.responsesApis],
266
- gatewayContextModels: [...DEFAULT_COMPACTION_CONFIG.gatewayContextModels],
267
- },
268
- webSearch: {
269
- ...DEFAULT_WEB_SEARCH_CONFIG,
270
- models: [...DEFAULT_WEB_SEARCH_CONFIG.models],
271
- ...(DEFAULT_WEB_SEARCH_CONFIG.defaultRoute
272
- ? { defaultRoute: DEFAULT_WEB_SEARCH_CONFIG.defaultRoute }
273
- : {}),
274
- ...(DEFAULT_WEB_SEARCH_CONFIG.routes
275
- ? { routes: { ...DEFAULT_WEB_SEARCH_CONFIG.routes } }
276
- : {}),
277
- },
278
- imageGeneration: {
279
- ...DEFAULT_IMAGE_GENERATION_CONFIG,
280
- models: [...DEFAULT_IMAGE_GENERATION_CONFIG.models],
281
- },
282
- };
283
- }
284
-
285
- function applyNativeFallbackConfig(
286
- raw: Record<string, unknown>,
287
- resolved: NativeFallbackConfig,
288
- warnings: string[],
289
- ): void {
290
- warnUnknownFields(raw, NATIVE_FALLBACK_FIELDS, "compaction.nativeFallback", warnings);
291
- resolved.enabled =
292
- toBoolean(raw.enabled, "compaction.nativeFallback.enabled", warnings) ?? resolved.enabled;
293
-
294
- const modelSpec = toModelSpec(raw.model, "compaction.nativeFallback.model", warnings);
295
- if (modelSpec !== undefined) {
296
- resolved.model = modelSpec === null ? undefined : modelSpec;
297
- }
298
-
299
- resolved.thinkingLevel =
300
- toThinkingLevel(raw.thinkingLevel, "compaction.nativeFallback.thinkingLevel", warnings) ??
301
- resolved.thinkingLevel;
302
- }
303
-
304
- function applyCompactionConfig(
305
- raw: Record<string, unknown>,
306
- resolved: CompactionConfig,
307
- warnings: string[],
308
- ): void {
309
- warnUnknownFields(raw, COMPACTION_FIELDS, "compaction", warnings);
310
-
311
- resolved.enabled = toBoolean(raw.enabled, "compaction.enabled", warnings) ?? resolved.enabled;
312
- resolved.contextManagement =
313
- toContextManagementMode(raw.contextManagement, "compaction.contextManagement", warnings) ??
314
- resolved.contextManagement;
315
- resolved.leaveManagedMode =
316
- toLeaveManagedMode(raw.leaveManagedMode, "compaction.leaveManagedMode", warnings) ??
317
- resolved.leaveManagedMode;
318
- resolved.allowCompactionContinuityBreak =
319
- toBoolean(
320
- raw.allowCompactionContinuityBreak,
321
- "compaction.allowCompactionContinuityBreak",
322
- warnings,
323
- ) ?? resolved.allowCompactionContinuityBreak;
324
- resolved.notifyOnLoad =
325
- toBoolean(raw.notifyOnLoad, "compaction.notifyOnLoad", warnings) ?? resolved.notifyOnLoad;
326
- const reminderPercent = toBoundedInteger(
327
- raw.contextReminderThresholdPercent,
328
- "compaction.contextReminderThresholdPercent",
329
- warnings,
330
- 0,
331
- 100,
332
- );
333
- if (reminderPercent !== undefined) {
334
- resolved.contextReminderThresholdPercent = reminderPercent;
335
- }
336
- resolved.debug = toBoolean(raw.debug, "compaction.debug", warnings) ?? resolved.debug;
337
- resolved.logProviderPayloads =
338
- toBoolean(raw.logProviderPayloads, "compaction.logProviderPayloads", warnings) ??
339
- resolved.logProviderPayloads;
340
- resolved.logCompactResponses =
341
- toBoolean(raw.logCompactResponses, "compaction.logCompactResponses", warnings) ??
342
- resolved.logCompactResponses;
343
- resolved.redactSensitiveData =
344
- toBoolean(raw.redactSensitiveData, "compaction.redactSensitiveData", warnings) ??
345
- resolved.redactSensitiveData;
346
-
347
- const remoteCompactModelSpec = toModelSpec(
348
- raw.remoteCompactModel,
349
- "compaction.remoteCompactModel",
350
- warnings,
351
- );
352
- if (remoteCompactModelSpec !== undefined) {
353
- resolved.remoteCompactModel = remoteCompactModelSpec === null ? undefined : remoteCompactModelSpec;
354
- }
355
-
356
- resolved.remoteV2ContextSource =
357
- toRemoteV2ContextSource(raw.remoteV2ContextSource, "compaction.remoteV2ContextSource", warnings) ??
358
- resolved.remoteV2ContextSource;
359
-
360
- if (raw.nativeFallback !== undefined) {
361
- if (isRecord(raw.nativeFallback)) {
362
- applyNativeFallbackConfig(raw.nativeFallback, resolved.nativeFallback, warnings);
363
- } else {
364
- warnings.push("Ignoring compaction.nativeFallback: expected a JSON object.");
365
- }
366
- }
367
-
368
- const apis = toSupportedApis(
369
- raw.responsesApis,
370
- "compaction.responsesApis",
371
- RESPONSES_COMPACT_CAPABLE_APIS,
372
- warnings,
373
- );
374
- if (apis !== undefined) {
375
- resolved.responsesApis = apis;
376
- }
377
-
378
- const gatewayContextModels = toStringList(
379
- raw.gatewayContextModels,
380
- "compaction.gatewayContextModels",
381
- warnings,
382
- );
383
- if (gatewayContextModels !== undefined) {
384
- resolved.gatewayContextModels = gatewayContextModels;
385
- }
386
-
387
- if (typeof raw.artifactRoot === "string" && raw.artifactRoot.trim().length > 0) {
388
- resolved.artifactRoot = raw.artifactRoot.trim();
389
- } else if (raw.artifactRoot !== undefined) {
390
- warnings.push("Ignoring compaction.artifactRoot: expected a non-empty string.");
391
- }
392
- }
393
-
394
- function applyWebSearchConfig(
395
- raw: Record<string, unknown>,
396
- resolved: WebSearchConfig,
397
- warnings: string[],
398
- ): void {
399
- warnUnknownFields(raw, WEB_SEARCH_FIELDS, "webSearch", warnings);
400
- resolved.enabled = toBoolean(raw.enabled, "webSearch.enabled", warnings) ?? resolved.enabled;
401
-
402
- const models = toStringList(raw.models, "webSearch.models", warnings);
403
- if (models !== undefined) {
404
- resolved.models = models;
405
- }
406
-
407
- const defaultRoute = toWebSearchRoute(raw.defaultRoute, "webSearch.defaultRoute", warnings);
408
- if (defaultRoute !== undefined) {
409
- resolved.defaultRoute = defaultRoute;
410
- }
411
-
412
- const routes = toWebSearchRoutes(raw.routes, "webSearch.routes", warnings);
413
- if (routes !== undefined) {
414
- resolved.routes = routes;
415
- }
416
-
417
- if (defaultRoute !== undefined && resolved.models.length > 0) {
418
- warnings.push(
419
- "webSearch.defaultRoute overrides legacy webSearch.models for models without an exact webSearch.routes entry.",
420
- );
421
- }
422
- if (routes !== undefined) {
423
- for (const key of Object.keys(routes)) {
424
- if (resolved.models.includes(key)) {
425
- warnings.push(
426
- `webSearch.routes.${key} overrides the legacy webSearch.models entry for the same exact model.`,
427
- );
428
- }
429
- }
430
- }
431
- }
432
-
433
- function applyImageGenerationConfig(
434
- raw: Record<string, unknown>,
435
- resolved: ImageGenerationConfig,
436
- warnings: string[],
437
- ): void {
438
- warnUnknownFields(raw, IMAGE_GENERATION_FIELDS, "imageGeneration", warnings);
439
- resolved.enabled =
440
- toBoolean(raw.enabled, "imageGeneration.enabled", warnings) ?? resolved.enabled;
441
-
442
- const models = toImageGenerationModels(raw.models, "imageGeneration.models", warnings);
443
- if (models !== undefined) {
444
- // An empty or blank-only list cannot express a default, so keep the shipped model.
445
- if (models.length === 0) {
446
- warnings.push(
447
- `Ignoring imageGeneration.models: expected at least one model id; using ${DEFAULT_IMAGE_GENERATION_MODEL}.`,
448
- );
449
- } else {
450
- resolved.models = models;
451
- }
452
- }
453
- }
454
-
455
- /**
456
- * A retired `autoMode` section is ignored in any shape: an object, `null`, a scalar or a malformed
457
- * nested value. Nothing inside it is parsed, so its old validation can never invalidate another
458
- * feature, and its contents are never echoed back to the user.
459
- */
460
- function warnRetiredAutoMode(warnings: string[]): void {
461
- warnings.push(`Ignoring autoMode: ${RETIRED_AUTO_MODE_MESSAGE}`);
462
- }
463
-
464
- /**
465
- * Normalize the supported unversioned document without I/O or mutation.
466
- * Versioned decoding and operation resolution are owned by ../config.ts.
467
- */
468
- export function normalizeLegacyConfig(
469
- raw: Record<string, unknown> | undefined,
470
- configPath: string = CONFIG_PATH,
471
- warnings: string[] = [],
472
- ): LoadedToolkitConfig {
473
- const resolved = cloneDefaults();
474
- let source: string | undefined;
475
-
476
- if (raw) {
477
- source = configPath;
478
- warnUnknownFields(raw, TOP_LEVEL_FIELDS, "", warnings);
479
-
480
- if (raw.compaction !== undefined) {
481
- if (isRecord(raw.compaction)) {
482
- applyCompactionConfig(raw.compaction, resolved.compaction, warnings);
483
- } else {
484
- warnings.push("Ignoring compaction: expected a JSON object.");
485
- }
486
- }
487
-
488
- if (raw.webSearch !== undefined) {
489
- if (isRecord(raw.webSearch)) {
490
- applyWebSearchConfig(raw.webSearch, resolved.webSearch, warnings);
491
- } else {
492
- warnings.push("Ignoring webSearch: expected a JSON object.");
493
- }
494
- }
495
-
496
- if (raw.imageGeneration !== undefined) {
497
- if (isRecord(raw.imageGeneration)) {
498
- applyImageGenerationConfig(raw.imageGeneration, resolved.imageGeneration, warnings);
499
- } else {
500
- warnings.push("Ignoring imageGeneration: expected a JSON object.");
501
- }
502
- }
503
-
504
- if (Object.hasOwn(raw, "autoMode")) warnRetiredAutoMode(warnings);
505
- }
506
-
507
- resolved.compaction.artifactRoot = resolveConfiguredPath(
508
- resolved.compaction.artifactRoot,
509
- path.dirname(configPath),
510
- );
511
-
512
- return {
513
- config: resolved,
514
- source,
515
- warnings,
516
- };
517
- }
518
-
519
- export { DEFAULT_TOOLKIT_CONFIG };
1
+ import * as os from "node:os";
2
+ import * as path from "node:path";
3
+ import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
4
+ import {
5
+ DEFAULT_COMPACTION_CONFIG,
6
+ DEFAULT_TOOLKIT_CONFIG,
7
+ DEFAULT_IMAGE_GENERATION_CONFIG,
8
+ DEFAULT_IMAGE_GENERATION_MODEL,
9
+ DEFAULT_NATIVE_FALLBACK_CONFIG,
10
+ DEFAULT_WEB_SEARCH_CONFIG,
11
+ RESPONSES_COMPACT_CAPABLE_APIS,
12
+ THINKING_LEVELS,
13
+ TOOLKIT_ID,
14
+ type CompactionConfig,
15
+ type ImageGenerationConfig,
16
+ type RemoteV2ContextSource,
17
+ type LoadedToolkitConfig,
18
+ type NativeFallbackConfig,
19
+ type ToolkitConfig,
20
+ type WebSearchConfig,
21
+ type WebSearchRoute,
22
+ } from "../types";
23
+ import { RETIRED_AUTO_MODE_MESSAGE, RETIRED_REMOTE_WINDOWS_CONFIG_MESSAGE, RETIRED_REMOTE_WINDOWS_MODE_MESSAGE } from "./policy";
24
+ import { MAX_IMAGE_MODEL_ID_CHARS } from "../image-generation/types";
25
+
26
+ export const CONFIG_DIR = path.join(os.homedir(), ".pi", "agent", "extensions", TOOLKIT_ID);
27
+ export const CONFIG_PATH = path.join(CONFIG_DIR, "config.json");
28
+
29
+ const TOP_LEVEL_FIELDS = new Set(["compaction", "webSearch", "imageGeneration", "autoMode"]);
30
+ const COMPACTION_FIELDS = new Set([
31
+ "enabled",
32
+ "leaveManagedMode",
33
+ "contextManagement",
34
+ "allowCompactionContinuityBreak",
35
+ "remoteCompactModel",
36
+ "remoteV2ContextSource",
37
+ "nativeFallback",
38
+ "responsesApis",
39
+ "gatewayContextModels",
40
+ "contextReminderThresholdPercent",
41
+ "notifyOnLoad",
42
+ "debug",
43
+ "logProviderPayloads",
44
+ "logCompactResponses",
45
+ "redactSensitiveData",
46
+ "artifactRoot",
47
+ ]);
48
+ const NATIVE_FALLBACK_FIELDS = new Set(["enabled", "model", "thinkingLevel"]);
49
+ const WEB_SEARCH_FIELDS = new Set(["enabled", "models", "defaultRoute", "routes"]);
50
+ const IMAGE_GENERATION_FIELDS = new Set(["enabled", "models"]);
51
+
52
+ function isRecord(value: unknown): value is Record<string, unknown> {
53
+ return !!value && typeof value === "object" && !Array.isArray(value);
54
+ }
55
+
56
+ function resolveConfiguredPath(rawPath: string, baseDir: string): string {
57
+ if (rawPath.startsWith("~/")) {
58
+ return path.join(os.homedir(), rawPath.slice(2));
59
+ }
60
+ if (path.isAbsolute(rawPath)) {
61
+ return path.resolve(rawPath);
62
+ }
63
+ return path.resolve(baseDir, rawPath);
64
+ }
65
+
66
+ function warnUnknownFields(
67
+ value: Record<string, unknown>,
68
+ knownFields: ReadonlySet<string>,
69
+ fieldPath: string,
70
+ warnings: string[],
71
+ ): void {
72
+ for (const key of Object.keys(value)) {
73
+ if (!knownFields.has(key)) {
74
+ warnings.push(`Ignoring ${fieldPath ? `${fieldPath}.` : ""}${key}: unknown field.`);
75
+ }
76
+ }
77
+ }
78
+
79
+ function toBoolean(value: unknown, fieldPath: string, warnings: string[]): boolean | undefined {
80
+ if (value === undefined) return undefined;
81
+ if (typeof value === "boolean") return value;
82
+ warnings.push(`Ignoring ${fieldPath}: expected a boolean.`);
83
+ return undefined;
84
+ }
85
+
86
+ function toRemoteV2ContextSource(
87
+ value: unknown,
88
+ fieldPath: string,
89
+ warnings: string[],
90
+ ): RemoteV2ContextSource | undefined {
91
+ if (value === undefined) return undefined;
92
+ if (typeof value === "string") {
93
+ const normalized = value.trim();
94
+ if (normalized === "pi-context-hook" || normalized === "legacy") return normalized;
95
+ }
96
+ warnings.push(`Ignoring ${fieldPath}: expected one of pi-context-hook, legacy.`);
97
+ return undefined;
98
+ }
99
+
100
+ function toModelSpec(value: unknown, fieldPath: string, warnings: string[]): string | null | undefined {
101
+ if (value === undefined) return undefined;
102
+ if (value === null) return null;
103
+ if (typeof value === "string" && value.trim().length > 0) {
104
+ return value.trim();
105
+ }
106
+ warnings.push(`Ignoring ${fieldPath}: expected "provider/model-id" or null.`);
107
+ return undefined;
108
+ }
109
+
110
+ function toThinkingLevel(value: unknown, fieldPath: string, warnings: string[]): ThinkingLevel | undefined {
111
+ if (value === undefined) return undefined;
112
+ if (typeof value === "string" && (THINKING_LEVELS as readonly string[]).includes(value)) {
113
+ return value as ThinkingLevel;
114
+ }
115
+ warnings.push(`Ignoring ${fieldPath}: expected one of ${THINKING_LEVELS.join(", ")}.`);
116
+ return undefined;
117
+ }
118
+
119
+ function toSupportedApis(
120
+ value: unknown,
121
+ fieldPath: string,
122
+ capableApis: readonly string[],
123
+ warnings: string[],
124
+ ): string[] | undefined {
125
+ if (value === undefined) return undefined;
126
+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
127
+ warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
128
+ return undefined;
129
+ }
130
+
131
+ const capable = new Set(capableApis);
132
+ const accepted: string[] = [];
133
+ for (const item of new Set(value.map((entry) => entry.trim()).filter(Boolean))) {
134
+ if (capable.has(item)) {
135
+ accepted.push(item);
136
+ } else {
137
+ warnings.push(
138
+ `Ignoring ${fieldPath} entry "${item}": only ${capableApis.join(", ")} are supported.`,
139
+ );
140
+ }
141
+ }
142
+
143
+ return accepted;
144
+ }
145
+
146
+ function toStringList(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
147
+ if (value === undefined) return undefined;
148
+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
149
+ warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
150
+ return undefined;
151
+ }
152
+
153
+ return [...new Set(value.map((entry) => entry.trim()).filter(Boolean))];
154
+ }
155
+
156
+ function toImageGenerationModels(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
157
+ const models = toStringList(value, fieldPath, warnings);
158
+ if (models === undefined) return undefined;
159
+ if (models.some((model) => model.length > MAX_IMAGE_MODEL_ID_CHARS)) {
160
+ warnings.push(
161
+ `Ignoring ${fieldPath}: each model id must be at most ${MAX_IMAGE_MODEL_ID_CHARS} characters.`,
162
+ );
163
+ return undefined;
164
+ }
165
+ return models;
166
+ }
167
+
168
+ function toWebSearchRoute(
169
+ value: unknown,
170
+ fieldPath: string,
171
+ warnings: string[],
172
+ ): WebSearchRoute | undefined {
173
+ if (value === undefined) return undefined;
174
+ if (value === "local" || value === "hosted" || value === "standalone-alpha") {
175
+ return value;
176
+ }
177
+ warnings.push(`Ignoring ${fieldPath}: expected one of local, hosted, standalone-alpha.`);
178
+ return undefined;
179
+ }
180
+
181
+ function isExactWebSearchModelKey(value: string): boolean {
182
+ const trimmed = value.trim();
183
+ const separatorIndex = trimmed.indexOf("/");
184
+ if (separatorIndex <= 0 || separatorIndex >= trimmed.length - 1) return false;
185
+ if (/[\s*?\[\]{}]/.test(trimmed)) return false;
186
+ const provider = trimmed.slice(0, separatorIndex);
187
+ const modelId = trimmed.slice(separatorIndex + 1);
188
+ return provider.length > 0 && modelId.length > 0;
189
+ }
190
+
191
+ function toWebSearchRoutes(
192
+ value: unknown,
193
+ fieldPath: string,
194
+ warnings: string[],
195
+ ): Record<string, WebSearchRoute> | undefined {
196
+ if (value === undefined) return undefined;
197
+ if (!isRecord(value)) {
198
+ warnings.push(`Ignoring ${fieldPath}: expected a JSON object.`);
199
+ return undefined;
200
+ }
201
+
202
+ const routes: Record<string, WebSearchRoute> = {};
203
+ for (const [rawKey, rawRoute] of Object.entries(value)) {
204
+ const key = rawKey.trim();
205
+ if (!isExactWebSearchModelKey(key)) {
206
+ warnings.push(`Ignoring ${fieldPath}.${rawKey}: expected an exact "provider/model-id" key.`);
207
+ continue;
208
+ }
209
+ const route = toWebSearchRoute(rawRoute, `${fieldPath}.${rawKey}`, warnings);
210
+ if (route !== undefined) routes[key] = route;
211
+ }
212
+ return routes;
213
+ }
214
+
215
+ function cloneDefaults(): ToolkitConfig {
216
+ return {
217
+ compaction: {
218
+ ...DEFAULT_COMPACTION_CONFIG,
219
+ nativeFallback: { ...DEFAULT_NATIVE_FALLBACK_CONFIG },
220
+ responsesApis: [...DEFAULT_COMPACTION_CONFIG.responsesApis],
221
+ gatewayContextModels: [...DEFAULT_COMPACTION_CONFIG.gatewayContextModels],
222
+ },
223
+ webSearch: {
224
+ ...DEFAULT_WEB_SEARCH_CONFIG,
225
+ models: [...DEFAULT_WEB_SEARCH_CONFIG.models],
226
+ ...(DEFAULT_WEB_SEARCH_CONFIG.defaultRoute
227
+ ? { defaultRoute: DEFAULT_WEB_SEARCH_CONFIG.defaultRoute }
228
+ : {}),
229
+ ...(DEFAULT_WEB_SEARCH_CONFIG.routes
230
+ ? { routes: { ...DEFAULT_WEB_SEARCH_CONFIG.routes } }
231
+ : {}),
232
+ },
233
+ imageGeneration: {
234
+ ...DEFAULT_IMAGE_GENERATION_CONFIG,
235
+ models: [...DEFAULT_IMAGE_GENERATION_CONFIG.models],
236
+ },
237
+ };
238
+ }
239
+
240
+ function applyNativeFallbackConfig(
241
+ raw: Record<string, unknown>,
242
+ resolved: NativeFallbackConfig,
243
+ warnings: string[],
244
+ ): void {
245
+ warnUnknownFields(raw, NATIVE_FALLBACK_FIELDS, "compaction.nativeFallback", warnings);
246
+ resolved.enabled =
247
+ toBoolean(raw.enabled, "compaction.nativeFallback.enabled", warnings) ?? resolved.enabled;
248
+
249
+ const modelSpec = toModelSpec(raw.model, "compaction.nativeFallback.model", warnings);
250
+ if (modelSpec !== undefined) {
251
+ resolved.model = modelSpec === null ? undefined : modelSpec;
252
+ }
253
+
254
+ resolved.thinkingLevel =
255
+ toThinkingLevel(raw.thinkingLevel, "compaction.nativeFallback.thinkingLevel", warnings) ??
256
+ resolved.thinkingLevel;
257
+ }
258
+
259
+ function applyCompactionConfig(
260
+ raw: Record<string, unknown>,
261
+ resolved: CompactionConfig,
262
+ warnings: string[],
263
+ ): void {
264
+ warnUnknownFields(raw, COMPACTION_FIELDS, "compaction", warnings);
265
+
266
+ resolved.enabled = toBoolean(raw.enabled, "compaction.enabled", warnings) ?? resolved.enabled;
267
+ for (const key of ["leaveManagedMode", "contextReminderThresholdPercent"]) {
268
+ if (Object.hasOwn(raw, key)) warnings.push(`Ignoring compaction.${key}: ${RETIRED_REMOTE_WINDOWS_CONFIG_MESSAGE}`);
269
+ }
270
+ if (Object.hasOwn(raw, "contextManagement")) {
271
+ const mode = typeof raw.contextManagement === "string" ? raw.contextManagement.trim() : undefined;
272
+ warnings.push(mode === "remote"
273
+ ? `Ignoring compaction.contextManagement: ${RETIRED_REMOTE_WINDOWS_MODE_MESSAGE}`
274
+ : mode === "off"
275
+ ? `Ignoring compaction.contextManagement: ${RETIRED_REMOTE_WINDOWS_CONFIG_MESSAGE}`
276
+ : "Ignoring compaction.contextManagement: expected off; remote has been removed.");
277
+ }
278
+ resolved.allowCompactionContinuityBreak =
279
+ toBoolean(
280
+ raw.allowCompactionContinuityBreak,
281
+ "compaction.allowCompactionContinuityBreak",
282
+ warnings,
283
+ ) ?? resolved.allowCompactionContinuityBreak;
284
+ resolved.notifyOnLoad =
285
+ toBoolean(raw.notifyOnLoad, "compaction.notifyOnLoad", warnings) ?? resolved.notifyOnLoad;
286
+ resolved.debug = toBoolean(raw.debug, "compaction.debug", warnings) ?? resolved.debug;
287
+ resolved.logProviderPayloads =
288
+ toBoolean(raw.logProviderPayloads, "compaction.logProviderPayloads", warnings) ??
289
+ resolved.logProviderPayloads;
290
+ resolved.logCompactResponses =
291
+ toBoolean(raw.logCompactResponses, "compaction.logCompactResponses", warnings) ??
292
+ resolved.logCompactResponses;
293
+ resolved.redactSensitiveData =
294
+ toBoolean(raw.redactSensitiveData, "compaction.redactSensitiveData", warnings) ??
295
+ resolved.redactSensitiveData;
296
+
297
+ const remoteCompactModelSpec = toModelSpec(
298
+ raw.remoteCompactModel,
299
+ "compaction.remoteCompactModel",
300
+ warnings,
301
+ );
302
+ if (remoteCompactModelSpec !== undefined) {
303
+ resolved.remoteCompactModel = remoteCompactModelSpec === null ? undefined : remoteCompactModelSpec;
304
+ }
305
+
306
+ resolved.remoteV2ContextSource =
307
+ toRemoteV2ContextSource(raw.remoteV2ContextSource, "compaction.remoteV2ContextSource", warnings) ??
308
+ resolved.remoteV2ContextSource;
309
+
310
+ if (raw.nativeFallback !== undefined) {
311
+ if (isRecord(raw.nativeFallback)) {
312
+ applyNativeFallbackConfig(raw.nativeFallback, resolved.nativeFallback, warnings);
313
+ } else {
314
+ warnings.push("Ignoring compaction.nativeFallback: expected a JSON object.");
315
+ }
316
+ }
317
+
318
+ const apis = toSupportedApis(
319
+ raw.responsesApis,
320
+ "compaction.responsesApis",
321
+ RESPONSES_COMPACT_CAPABLE_APIS,
322
+ warnings,
323
+ );
324
+ if (apis !== undefined) {
325
+ resolved.responsesApis = apis;
326
+ }
327
+
328
+ const gatewayContextModels = toStringList(
329
+ raw.gatewayContextModels,
330
+ "compaction.gatewayContextModels",
331
+ warnings,
332
+ );
333
+ if (gatewayContextModels !== undefined) {
334
+ resolved.gatewayContextModels = gatewayContextModels;
335
+ }
336
+
337
+ if (typeof raw.artifactRoot === "string" && raw.artifactRoot.trim().length > 0) {
338
+ resolved.artifactRoot = raw.artifactRoot.trim();
339
+ } else if (raw.artifactRoot !== undefined) {
340
+ warnings.push("Ignoring compaction.artifactRoot: expected a non-empty string.");
341
+ }
342
+ }
343
+
344
+ function applyWebSearchConfig(
345
+ raw: Record<string, unknown>,
346
+ resolved: WebSearchConfig,
347
+ warnings: string[],
348
+ ): void {
349
+ warnUnknownFields(raw, WEB_SEARCH_FIELDS, "webSearch", warnings);
350
+ resolved.enabled = toBoolean(raw.enabled, "webSearch.enabled", warnings) ?? resolved.enabled;
351
+
352
+ const models = toStringList(raw.models, "webSearch.models", warnings);
353
+ if (models !== undefined) {
354
+ resolved.models = models;
355
+ }
356
+
357
+ const defaultRoute = toWebSearchRoute(raw.defaultRoute, "webSearch.defaultRoute", warnings);
358
+ if (defaultRoute !== undefined) {
359
+ resolved.defaultRoute = defaultRoute;
360
+ }
361
+
362
+ const routes = toWebSearchRoutes(raw.routes, "webSearch.routes", warnings);
363
+ if (routes !== undefined) {
364
+ resolved.routes = routes;
365
+ }
366
+
367
+ if (defaultRoute !== undefined && resolved.models.length > 0) {
368
+ warnings.push(
369
+ "webSearch.defaultRoute overrides legacy webSearch.models for models without an exact webSearch.routes entry.",
370
+ );
371
+ }
372
+ if (routes !== undefined) {
373
+ for (const key of Object.keys(routes)) {
374
+ if (resolved.models.includes(key)) {
375
+ warnings.push(
376
+ `webSearch.routes.${key} overrides the legacy webSearch.models entry for the same exact model.`,
377
+ );
378
+ }
379
+ }
380
+ }
381
+ }
382
+
383
+ function applyImageGenerationConfig(
384
+ raw: Record<string, unknown>,
385
+ resolved: ImageGenerationConfig,
386
+ warnings: string[],
387
+ ): void {
388
+ warnUnknownFields(raw, IMAGE_GENERATION_FIELDS, "imageGeneration", warnings);
389
+ resolved.enabled =
390
+ toBoolean(raw.enabled, "imageGeneration.enabled", warnings) ?? resolved.enabled;
391
+
392
+ const models = toImageGenerationModels(raw.models, "imageGeneration.models", warnings);
393
+ if (models !== undefined) {
394
+ // An empty or blank-only list cannot express a default, so keep the shipped model.
395
+ if (models.length === 0) {
396
+ warnings.push(
397
+ `Ignoring imageGeneration.models: expected at least one model id; using ${DEFAULT_IMAGE_GENERATION_MODEL}.`,
398
+ );
399
+ } else {
400
+ resolved.models = models;
401
+ }
402
+ }
403
+ }
404
+
405
+ /**
406
+ * A retired `autoMode` section is ignored in any shape: an object, `null`, a scalar or a malformed
407
+ * nested value. Nothing inside it is parsed, so its old validation can never invalidate another
408
+ * feature, and its contents are never echoed back to the user.
409
+ */
410
+ function warnRetiredAutoMode(warnings: string[]): void {
411
+ warnings.push(`Ignoring autoMode: ${RETIRED_AUTO_MODE_MESSAGE}`);
412
+ }
413
+
414
+ /**
415
+ * Normalize the supported unversioned document without I/O or mutation.
416
+ * Versioned decoding and operation resolution are owned by ../config.ts.
417
+ */
418
+ export function normalizeLegacyConfig(
419
+ raw: Record<string, unknown> | undefined,
420
+ configPath: string = CONFIG_PATH,
421
+ warnings: string[] = [],
422
+ ): LoadedToolkitConfig {
423
+ const resolved = cloneDefaults();
424
+ let source: string | undefined;
425
+
426
+ if (raw) {
427
+ source = configPath;
428
+ warnUnknownFields(raw, TOP_LEVEL_FIELDS, "", warnings);
429
+
430
+ if (raw.compaction !== undefined) {
431
+ if (isRecord(raw.compaction)) {
432
+ applyCompactionConfig(raw.compaction, resolved.compaction, warnings);
433
+ } else {
434
+ warnings.push("Ignoring compaction: expected a JSON object.");
435
+ }
436
+ }
437
+
438
+ if (raw.webSearch !== undefined) {
439
+ if (isRecord(raw.webSearch)) {
440
+ applyWebSearchConfig(raw.webSearch, resolved.webSearch, warnings);
441
+ } else {
442
+ warnings.push("Ignoring webSearch: expected a JSON object.");
443
+ }
444
+ }
445
+
446
+ if (raw.imageGeneration !== undefined) {
447
+ if (isRecord(raw.imageGeneration)) {
448
+ applyImageGenerationConfig(raw.imageGeneration, resolved.imageGeneration, warnings);
449
+ } else {
450
+ warnings.push("Ignoring imageGeneration: expected a JSON object.");
451
+ }
452
+ }
453
+
454
+ if (Object.hasOwn(raw, "autoMode")) warnRetiredAutoMode(warnings);
455
+ }
456
+
457
+ resolved.compaction.artifactRoot = resolveConfiguredPath(
458
+ resolved.compaction.artifactRoot,
459
+ path.dirname(configPath),
460
+ );
461
+
462
+ return {
463
+ config: resolved,
464
+ source,
465
+ warnings,
466
+ };
467
+ }
468
+
469
+ export { DEFAULT_TOOLKIT_CONFIG };