pi-openai-toolkit 0.14.13 → 0.15.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.
- package/README.md +250 -241
- package/README.zh.md +250 -241
- package/config.schema.json +352 -0
- package/docs/configuration.md +176 -0
- package/docs/internals.md +139 -128
- package/package.json +135 -127
- package/src/auto-mode/extension.ts +35 -13
- package/src/config/legacy.ts +717 -0
- package/src/config/migration.ts +100 -0
- package/src/config/notifications.ts +28 -0
- package/src/config/policy.ts +122 -0
- package/src/config/v2.ts +1347 -0
- package/src/config-command.ts +73 -0
- package/src/config.ts +318 -739
- package/src/context-management/tools.ts +353 -347
- package/src/context-management/window-manager.ts +696 -693
- package/src/debug.ts +5 -5
- package/src/deferred-tool-carryover.ts +3 -1
- package/src/extension-runtime.ts +1375 -1329
- package/src/image-generation/extension.ts +14 -18
- package/src/image-generation/protocol.ts +10 -4
- package/src/image-generation/service.ts +9 -3
- package/src/request-context-cache.ts +2 -1
- package/src/types.ts +641 -628
- package/src/web-search/alpha-client.ts +876 -875
- package/src/web-search/extension.ts +529 -523
- package/src/web-search/payload.ts +299 -295
- package/src/web-search/prompt.ts +60 -59
- package/src/web-search/types.ts +144 -144
|
@@ -0,0 +1,717 @@
|
|
|
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_AUTO_MODE_CONFIG,
|
|
8
|
+
DEFAULT_IMAGE_GENERATION_CONFIG,
|
|
9
|
+
DEFAULT_IMAGE_GENERATION_MODEL,
|
|
10
|
+
DEFAULT_NATIVE_FALLBACK_CONFIG,
|
|
11
|
+
DEFAULT_WEB_SEARCH_CONFIG,
|
|
12
|
+
RESPONSES_COMPACT_CAPABLE_APIS,
|
|
13
|
+
BREAKER_LIMIT_MAX,
|
|
14
|
+
BREAKER_LIMIT_MIN,
|
|
15
|
+
BREAKER_WINDOW_MAX,
|
|
16
|
+
BREAKER_WINDOW_MIN,
|
|
17
|
+
CLASSIFIER_MAX_LAG_MAX,
|
|
18
|
+
CLASSIFIER_MAX_LAG_MIN,
|
|
19
|
+
DEFAULT_CLASSIFIER_TIMEOUT_MS,
|
|
20
|
+
EVIDENCE_ROUNDS_MAX,
|
|
21
|
+
EVIDENCE_ROUNDS_MIN,
|
|
22
|
+
REVIEWER_TIMEOUT_MAX_MS,
|
|
23
|
+
REVIEWER_TIMEOUT_MIN_MS,
|
|
24
|
+
THINKING_LEVELS,
|
|
25
|
+
TOOLKIT_ID,
|
|
26
|
+
type AutoModeCircuitBreakerConfig,
|
|
27
|
+
type AutoModeClassifierConfig,
|
|
28
|
+
type AutoModeConfig,
|
|
29
|
+
type AutoModeGate,
|
|
30
|
+
type CompactionConfig,
|
|
31
|
+
type ContextManagementMode,
|
|
32
|
+
type LeaveManagedModePolicy,
|
|
33
|
+
type ImageGenerationConfig,
|
|
34
|
+
type RemoteV2ContextSource,
|
|
35
|
+
type LoadedToolkitConfig,
|
|
36
|
+
type NativeFallbackConfig,
|
|
37
|
+
type ToolkitConfig,
|
|
38
|
+
type WebSearchConfig,
|
|
39
|
+
type WebSearchRoute,
|
|
40
|
+
} from "../types";
|
|
41
|
+
import { MAX_IMAGE_MODEL_ID_CHARS } from "../image-generation/types";
|
|
42
|
+
|
|
43
|
+
export const CONFIG_DIR = path.join(os.homedir(), ".pi", "agent", "extensions", TOOLKIT_ID);
|
|
44
|
+
export const CONFIG_PATH = path.join(CONFIG_DIR, "config.json");
|
|
45
|
+
|
|
46
|
+
const TOP_LEVEL_FIELDS = new Set(["compaction", "webSearch", "imageGeneration", "autoMode"]);
|
|
47
|
+
const COMPACTION_FIELDS = new Set([
|
|
48
|
+
"enabled",
|
|
49
|
+
"leaveManagedMode",
|
|
50
|
+
"contextManagement",
|
|
51
|
+
"allowCompactionContinuityBreak",
|
|
52
|
+
"remoteCompactModel",
|
|
53
|
+
"remoteV2ContextSource",
|
|
54
|
+
"nativeFallback",
|
|
55
|
+
"responsesApis",
|
|
56
|
+
"gatewayContextModels",
|
|
57
|
+
"contextReminderThresholdPercent",
|
|
58
|
+
"notifyOnLoad",
|
|
59
|
+
"debug",
|
|
60
|
+
"logProviderPayloads",
|
|
61
|
+
"logCompactResponses",
|
|
62
|
+
"redactSensitiveData",
|
|
63
|
+
"artifactRoot",
|
|
64
|
+
]);
|
|
65
|
+
const NATIVE_FALLBACK_FIELDS = new Set(["enabled", "model", "thinkingLevel"]);
|
|
66
|
+
const WEB_SEARCH_FIELDS = new Set(["enabled", "models", "defaultRoute", "routes"]);
|
|
67
|
+
const IMAGE_GENERATION_FIELDS = new Set(["enabled", "models"]);
|
|
68
|
+
const AUTO_MODE_FIELDS = new Set([
|
|
69
|
+
"enabled",
|
|
70
|
+
"models",
|
|
71
|
+
"reviewerModel",
|
|
72
|
+
"gate",
|
|
73
|
+
"extraTools",
|
|
74
|
+
"timeoutMs",
|
|
75
|
+
"transcript",
|
|
76
|
+
"evidenceTools",
|
|
77
|
+
"maxEvidenceRounds",
|
|
78
|
+
"classifier",
|
|
79
|
+
"circuitBreaker",
|
|
80
|
+
]);
|
|
81
|
+
const AUTO_MODE_CLASSIFIER_FIELDS = new Set(["enabled", "model", "timeoutMs", "maxLag"]);
|
|
82
|
+
const AUTO_MODE_BREAKER_FIELDS = new Set(["consecutiveDenials", "recentDenials", "windowSize"]);
|
|
83
|
+
|
|
84
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
85
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function resolveConfiguredPath(rawPath: string, baseDir: string): string {
|
|
89
|
+
if (rawPath.startsWith("~/")) {
|
|
90
|
+
return path.join(os.homedir(), rawPath.slice(2));
|
|
91
|
+
}
|
|
92
|
+
if (path.isAbsolute(rawPath)) {
|
|
93
|
+
return path.resolve(rawPath);
|
|
94
|
+
}
|
|
95
|
+
return path.resolve(baseDir, rawPath);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function warnUnknownFields(
|
|
99
|
+
value: Record<string, unknown>,
|
|
100
|
+
knownFields: ReadonlySet<string>,
|
|
101
|
+
fieldPath: string,
|
|
102
|
+
warnings: string[],
|
|
103
|
+
): void {
|
|
104
|
+
for (const key of Object.keys(value)) {
|
|
105
|
+
if (!knownFields.has(key)) {
|
|
106
|
+
warnings.push(`Ignoring ${fieldPath ? `${fieldPath}.` : ""}${key}: unknown field.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function toBoolean(value: unknown, fieldPath: string, warnings: string[]): boolean | undefined {
|
|
112
|
+
if (value === undefined) return undefined;
|
|
113
|
+
if (typeof value === "boolean") return value;
|
|
114
|
+
warnings.push(`Ignoring ${fieldPath}: expected a boolean.`);
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function toLeaveManagedMode(
|
|
119
|
+
value: unknown,
|
|
120
|
+
fieldPath: string,
|
|
121
|
+
warnings: string[],
|
|
122
|
+
): LeaveManagedModePolicy | undefined {
|
|
123
|
+
if (value === undefined) return undefined;
|
|
124
|
+
if (typeof value === "string") {
|
|
125
|
+
const normalized = value.trim();
|
|
126
|
+
if (normalized === "warn" || normalized === "compact") return normalized;
|
|
127
|
+
}
|
|
128
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of warn, compact.`);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function toContextManagementMode(
|
|
133
|
+
value: unknown,
|
|
134
|
+
fieldPath: string,
|
|
135
|
+
warnings: string[],
|
|
136
|
+
): ContextManagementMode | undefined {
|
|
137
|
+
if (value === undefined) return undefined;
|
|
138
|
+
if (typeof value === "string") {
|
|
139
|
+
const normalized = value.trim();
|
|
140
|
+
if (normalized === "off" || normalized === "remote") return normalized;
|
|
141
|
+
}
|
|
142
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of off, remote.`);
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function toRemoteV2ContextSource(
|
|
147
|
+
value: unknown,
|
|
148
|
+
fieldPath: string,
|
|
149
|
+
warnings: string[],
|
|
150
|
+
): RemoteV2ContextSource | undefined {
|
|
151
|
+
if (value === undefined) return undefined;
|
|
152
|
+
if (typeof value === "string") {
|
|
153
|
+
const normalized = value.trim();
|
|
154
|
+
if (normalized === "pi-context-hook" || normalized === "legacy") return normalized;
|
|
155
|
+
}
|
|
156
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of pi-context-hook, legacy.`);
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function toModelSpec(value: unknown, fieldPath: string, warnings: string[]): string | null | undefined {
|
|
161
|
+
if (value === undefined) return undefined;
|
|
162
|
+
if (value === null) return null;
|
|
163
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
164
|
+
return value.trim();
|
|
165
|
+
}
|
|
166
|
+
warnings.push(`Ignoring ${fieldPath}: expected "provider/model-id" or null.`);
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function toThinkingLevel(value: unknown, fieldPath: string, warnings: string[]): ThinkingLevel | undefined {
|
|
171
|
+
if (value === undefined) return undefined;
|
|
172
|
+
if (typeof value === "string" && (THINKING_LEVELS as readonly string[]).includes(value)) {
|
|
173
|
+
return value as ThinkingLevel;
|
|
174
|
+
}
|
|
175
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of ${THINKING_LEVELS.join(", ")}.`);
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function toBoundedInteger(
|
|
180
|
+
value: unknown,
|
|
181
|
+
fieldPath: string,
|
|
182
|
+
warnings: string[],
|
|
183
|
+
min: number,
|
|
184
|
+
max: number,
|
|
185
|
+
): number | undefined {
|
|
186
|
+
if (value === undefined) return undefined;
|
|
187
|
+
if (typeof value === "number" && Number.isInteger(value) && Number.isFinite(value) && value >= min && value <= max) {
|
|
188
|
+
return value;
|
|
189
|
+
}
|
|
190
|
+
warnings.push(`Ignoring ${fieldPath}: expected an integer between ${min} and ${max}.`);
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function toSupportedApis(
|
|
195
|
+
value: unknown,
|
|
196
|
+
fieldPath: string,
|
|
197
|
+
capableApis: readonly string[],
|
|
198
|
+
warnings: string[],
|
|
199
|
+
): string[] | undefined {
|
|
200
|
+
if (value === undefined) return undefined;
|
|
201
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
|
|
202
|
+
warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const capable = new Set(capableApis);
|
|
207
|
+
const accepted: string[] = [];
|
|
208
|
+
for (const item of new Set(value.map((entry) => entry.trim()).filter(Boolean))) {
|
|
209
|
+
if (capable.has(item)) {
|
|
210
|
+
accepted.push(item);
|
|
211
|
+
} else {
|
|
212
|
+
warnings.push(
|
|
213
|
+
`Ignoring ${fieldPath} entry "${item}": only ${capableApis.join(", ")} are supported.`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return accepted;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function toStringList(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
|
|
222
|
+
if (value === undefined) return undefined;
|
|
223
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
|
|
224
|
+
warnings.push(`Ignoring ${fieldPath}: expected a string array.`);
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return [...new Set(value.map((entry) => entry.trim()).filter(Boolean))];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function toImageGenerationModels(value: unknown, fieldPath: string, warnings: string[]): string[] | undefined {
|
|
232
|
+
const models = toStringList(value, fieldPath, warnings);
|
|
233
|
+
if (models === undefined) return undefined;
|
|
234
|
+
if (models.some((model) => model.length > MAX_IMAGE_MODEL_ID_CHARS)) {
|
|
235
|
+
warnings.push(
|
|
236
|
+
`Ignoring ${fieldPath}: each model id must be at most ${MAX_IMAGE_MODEL_ID_CHARS} characters.`,
|
|
237
|
+
);
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
return models;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function toWebSearchRoute(
|
|
244
|
+
value: unknown,
|
|
245
|
+
fieldPath: string,
|
|
246
|
+
warnings: string[],
|
|
247
|
+
): WebSearchRoute | undefined {
|
|
248
|
+
if (value === undefined) return undefined;
|
|
249
|
+
if (value === "local" || value === "hosted" || value === "standalone-alpha") {
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of local, hosted, standalone-alpha.`);
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function isExactWebSearchModelKey(value: string): boolean {
|
|
257
|
+
const trimmed = value.trim();
|
|
258
|
+
const separatorIndex = trimmed.indexOf("/");
|
|
259
|
+
if (separatorIndex <= 0 || separatorIndex >= trimmed.length - 1) return false;
|
|
260
|
+
if (/[\s*?\[\]{}]/.test(trimmed)) return false;
|
|
261
|
+
const provider = trimmed.slice(0, separatorIndex);
|
|
262
|
+
const modelId = trimmed.slice(separatorIndex + 1);
|
|
263
|
+
return provider.length > 0 && modelId.length > 0;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function toWebSearchRoutes(
|
|
267
|
+
value: unknown,
|
|
268
|
+
fieldPath: string,
|
|
269
|
+
warnings: string[],
|
|
270
|
+
): Record<string, WebSearchRoute> | undefined {
|
|
271
|
+
if (value === undefined) return undefined;
|
|
272
|
+
if (!isRecord(value)) {
|
|
273
|
+
warnings.push(`Ignoring ${fieldPath}: expected a JSON object.`);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const routes: Record<string, WebSearchRoute> = {};
|
|
278
|
+
for (const [rawKey, rawRoute] of Object.entries(value)) {
|
|
279
|
+
const key = rawKey.trim();
|
|
280
|
+
if (!isExactWebSearchModelKey(key)) {
|
|
281
|
+
warnings.push(`Ignoring ${fieldPath}.${rawKey}: expected an exact "provider/model-id" key.`);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const route = toWebSearchRoute(rawRoute, `${fieldPath}.${rawKey}`, warnings);
|
|
285
|
+
if (route !== undefined) routes[key] = route;
|
|
286
|
+
}
|
|
287
|
+
return routes;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function toAutoModeGate(value: unknown, fieldPath: string, warnings: string[]): AutoModeGate | undefined {
|
|
291
|
+
if (value === undefined) return undefined;
|
|
292
|
+
if (value === "side-effect" || value === "all") return value;
|
|
293
|
+
warnings.push(`Ignoring ${fieldPath}: expected one of side-effect, all.`);
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function toReviewerTimeoutMs(value: unknown, fieldPath: string, warnings: string[]): number | undefined {
|
|
298
|
+
if (value === undefined) return undefined;
|
|
299
|
+
if (
|
|
300
|
+
typeof value === "number" &&
|
|
301
|
+
Number.isInteger(value) &&
|
|
302
|
+
value >= REVIEWER_TIMEOUT_MIN_MS &&
|
|
303
|
+
value <= REVIEWER_TIMEOUT_MAX_MS
|
|
304
|
+
) {
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
warnings.push(
|
|
308
|
+
`Ignoring ${fieldPath}: expected an integer between ${REVIEWER_TIMEOUT_MIN_MS} and ${REVIEWER_TIMEOUT_MAX_MS} ms.`,
|
|
309
|
+
);
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function cloneDefaults(): ToolkitConfig {
|
|
314
|
+
return {
|
|
315
|
+
compaction: {
|
|
316
|
+
...DEFAULT_COMPACTION_CONFIG,
|
|
317
|
+
nativeFallback: { ...DEFAULT_NATIVE_FALLBACK_CONFIG },
|
|
318
|
+
responsesApis: [...DEFAULT_COMPACTION_CONFIG.responsesApis],
|
|
319
|
+
gatewayContextModels: [...DEFAULT_COMPACTION_CONFIG.gatewayContextModels],
|
|
320
|
+
},
|
|
321
|
+
webSearch: {
|
|
322
|
+
...DEFAULT_WEB_SEARCH_CONFIG,
|
|
323
|
+
models: [...DEFAULT_WEB_SEARCH_CONFIG.models],
|
|
324
|
+
...(DEFAULT_WEB_SEARCH_CONFIG.defaultRoute
|
|
325
|
+
? { defaultRoute: DEFAULT_WEB_SEARCH_CONFIG.defaultRoute }
|
|
326
|
+
: {}),
|
|
327
|
+
...(DEFAULT_WEB_SEARCH_CONFIG.routes
|
|
328
|
+
? { routes: { ...DEFAULT_WEB_SEARCH_CONFIG.routes } }
|
|
329
|
+
: {}),
|
|
330
|
+
},
|
|
331
|
+
imageGeneration: {
|
|
332
|
+
...DEFAULT_IMAGE_GENERATION_CONFIG,
|
|
333
|
+
models: [...DEFAULT_IMAGE_GENERATION_CONFIG.models],
|
|
334
|
+
},
|
|
335
|
+
autoMode: {
|
|
336
|
+
...DEFAULT_AUTO_MODE_CONFIG,
|
|
337
|
+
models: [...DEFAULT_AUTO_MODE_CONFIG.models],
|
|
338
|
+
extraTools: [...DEFAULT_AUTO_MODE_CONFIG.extraTools],
|
|
339
|
+
classifier: { ...DEFAULT_AUTO_MODE_CONFIG.classifier },
|
|
340
|
+
circuitBreaker: { ...DEFAULT_AUTO_MODE_CONFIG.circuitBreaker },
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function applyNativeFallbackConfig(
|
|
346
|
+
raw: Record<string, unknown>,
|
|
347
|
+
resolved: NativeFallbackConfig,
|
|
348
|
+
warnings: string[],
|
|
349
|
+
): void {
|
|
350
|
+
warnUnknownFields(raw, NATIVE_FALLBACK_FIELDS, "compaction.nativeFallback", warnings);
|
|
351
|
+
resolved.enabled =
|
|
352
|
+
toBoolean(raw.enabled, "compaction.nativeFallback.enabled", warnings) ?? resolved.enabled;
|
|
353
|
+
|
|
354
|
+
const modelSpec = toModelSpec(raw.model, "compaction.nativeFallback.model", warnings);
|
|
355
|
+
if (modelSpec !== undefined) {
|
|
356
|
+
resolved.model = modelSpec === null ? undefined : modelSpec;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
resolved.thinkingLevel =
|
|
360
|
+
toThinkingLevel(raw.thinkingLevel, "compaction.nativeFallback.thinkingLevel", warnings) ??
|
|
361
|
+
resolved.thinkingLevel;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function applyCompactionConfig(
|
|
365
|
+
raw: Record<string, unknown>,
|
|
366
|
+
resolved: CompactionConfig,
|
|
367
|
+
warnings: string[],
|
|
368
|
+
): void {
|
|
369
|
+
warnUnknownFields(raw, COMPACTION_FIELDS, "compaction", warnings);
|
|
370
|
+
|
|
371
|
+
resolved.enabled = toBoolean(raw.enabled, "compaction.enabled", warnings) ?? resolved.enabled;
|
|
372
|
+
resolved.contextManagement =
|
|
373
|
+
toContextManagementMode(raw.contextManagement, "compaction.contextManagement", warnings) ??
|
|
374
|
+
resolved.contextManagement;
|
|
375
|
+
resolved.leaveManagedMode =
|
|
376
|
+
toLeaveManagedMode(raw.leaveManagedMode, "compaction.leaveManagedMode", warnings) ??
|
|
377
|
+
resolved.leaveManagedMode;
|
|
378
|
+
resolved.allowCompactionContinuityBreak =
|
|
379
|
+
toBoolean(
|
|
380
|
+
raw.allowCompactionContinuityBreak,
|
|
381
|
+
"compaction.allowCompactionContinuityBreak",
|
|
382
|
+
warnings,
|
|
383
|
+
) ?? resolved.allowCompactionContinuityBreak;
|
|
384
|
+
resolved.notifyOnLoad =
|
|
385
|
+
toBoolean(raw.notifyOnLoad, "compaction.notifyOnLoad", warnings) ?? resolved.notifyOnLoad;
|
|
386
|
+
const reminderPercent = toBoundedInteger(
|
|
387
|
+
raw.contextReminderThresholdPercent,
|
|
388
|
+
"compaction.contextReminderThresholdPercent",
|
|
389
|
+
warnings,
|
|
390
|
+
0,
|
|
391
|
+
100,
|
|
392
|
+
);
|
|
393
|
+
if (reminderPercent !== undefined) {
|
|
394
|
+
resolved.contextReminderThresholdPercent = reminderPercent;
|
|
395
|
+
}
|
|
396
|
+
resolved.debug = toBoolean(raw.debug, "compaction.debug", warnings) ?? resolved.debug;
|
|
397
|
+
resolved.logProviderPayloads =
|
|
398
|
+
toBoolean(raw.logProviderPayloads, "compaction.logProviderPayloads", warnings) ??
|
|
399
|
+
resolved.logProviderPayloads;
|
|
400
|
+
resolved.logCompactResponses =
|
|
401
|
+
toBoolean(raw.logCompactResponses, "compaction.logCompactResponses", warnings) ??
|
|
402
|
+
resolved.logCompactResponses;
|
|
403
|
+
resolved.redactSensitiveData =
|
|
404
|
+
toBoolean(raw.redactSensitiveData, "compaction.redactSensitiveData", warnings) ??
|
|
405
|
+
resolved.redactSensitiveData;
|
|
406
|
+
|
|
407
|
+
const remoteCompactModelSpec = toModelSpec(
|
|
408
|
+
raw.remoteCompactModel,
|
|
409
|
+
"compaction.remoteCompactModel",
|
|
410
|
+
warnings,
|
|
411
|
+
);
|
|
412
|
+
if (remoteCompactModelSpec !== undefined) {
|
|
413
|
+
resolved.remoteCompactModel = remoteCompactModelSpec === null ? undefined : remoteCompactModelSpec;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
resolved.remoteV2ContextSource =
|
|
417
|
+
toRemoteV2ContextSource(raw.remoteV2ContextSource, "compaction.remoteV2ContextSource", warnings) ??
|
|
418
|
+
resolved.remoteV2ContextSource;
|
|
419
|
+
|
|
420
|
+
if (raw.nativeFallback !== undefined) {
|
|
421
|
+
if (isRecord(raw.nativeFallback)) {
|
|
422
|
+
applyNativeFallbackConfig(raw.nativeFallback, resolved.nativeFallback, warnings);
|
|
423
|
+
} else {
|
|
424
|
+
warnings.push("Ignoring compaction.nativeFallback: expected a JSON object.");
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const apis = toSupportedApis(
|
|
429
|
+
raw.responsesApis,
|
|
430
|
+
"compaction.responsesApis",
|
|
431
|
+
RESPONSES_COMPACT_CAPABLE_APIS,
|
|
432
|
+
warnings,
|
|
433
|
+
);
|
|
434
|
+
if (apis !== undefined) {
|
|
435
|
+
resolved.responsesApis = apis;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const gatewayContextModels = toStringList(
|
|
439
|
+
raw.gatewayContextModels,
|
|
440
|
+
"compaction.gatewayContextModels",
|
|
441
|
+
warnings,
|
|
442
|
+
);
|
|
443
|
+
if (gatewayContextModels !== undefined) {
|
|
444
|
+
resolved.gatewayContextModels = gatewayContextModels;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (typeof raw.artifactRoot === "string" && raw.artifactRoot.trim().length > 0) {
|
|
448
|
+
resolved.artifactRoot = raw.artifactRoot.trim();
|
|
449
|
+
} else if (raw.artifactRoot !== undefined) {
|
|
450
|
+
warnings.push("Ignoring compaction.artifactRoot: expected a non-empty string.");
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function applyWebSearchConfig(
|
|
455
|
+
raw: Record<string, unknown>,
|
|
456
|
+
resolved: WebSearchConfig,
|
|
457
|
+
warnings: string[],
|
|
458
|
+
): void {
|
|
459
|
+
warnUnknownFields(raw, WEB_SEARCH_FIELDS, "webSearch", warnings);
|
|
460
|
+
resolved.enabled = toBoolean(raw.enabled, "webSearch.enabled", warnings) ?? resolved.enabled;
|
|
461
|
+
|
|
462
|
+
const models = toStringList(raw.models, "webSearch.models", warnings);
|
|
463
|
+
if (models !== undefined) {
|
|
464
|
+
resolved.models = models;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const defaultRoute = toWebSearchRoute(raw.defaultRoute, "webSearch.defaultRoute", warnings);
|
|
468
|
+
if (defaultRoute !== undefined) {
|
|
469
|
+
resolved.defaultRoute = defaultRoute;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const routes = toWebSearchRoutes(raw.routes, "webSearch.routes", warnings);
|
|
473
|
+
if (routes !== undefined) {
|
|
474
|
+
resolved.routes = routes;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (defaultRoute !== undefined && resolved.models.length > 0) {
|
|
478
|
+
warnings.push(
|
|
479
|
+
"webSearch.defaultRoute overrides legacy webSearch.models for models without an exact webSearch.routes entry.",
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
if (routes !== undefined) {
|
|
483
|
+
for (const key of Object.keys(routes)) {
|
|
484
|
+
if (resolved.models.includes(key)) {
|
|
485
|
+
warnings.push(
|
|
486
|
+
`webSearch.routes.${key} overrides the legacy webSearch.models entry for the same exact model.`,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function applyImageGenerationConfig(
|
|
494
|
+
raw: Record<string, unknown>,
|
|
495
|
+
resolved: ImageGenerationConfig,
|
|
496
|
+
warnings: string[],
|
|
497
|
+
): void {
|
|
498
|
+
warnUnknownFields(raw, IMAGE_GENERATION_FIELDS, "imageGeneration", warnings);
|
|
499
|
+
resolved.enabled =
|
|
500
|
+
toBoolean(raw.enabled, "imageGeneration.enabled", warnings) ?? resolved.enabled;
|
|
501
|
+
|
|
502
|
+
const models = toImageGenerationModels(raw.models, "imageGeneration.models", warnings);
|
|
503
|
+
if (models !== undefined) {
|
|
504
|
+
// An empty or blank-only list cannot express a default, so keep the shipped model.
|
|
505
|
+
if (models.length === 0) {
|
|
506
|
+
warnings.push(
|
|
507
|
+
`Ignoring imageGeneration.models: expected at least one model id; using ${DEFAULT_IMAGE_GENERATION_MODEL}.`,
|
|
508
|
+
);
|
|
509
|
+
} else {
|
|
510
|
+
resolved.models = models;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function applyAutoModeConfig(
|
|
516
|
+
raw: Record<string, unknown>,
|
|
517
|
+
resolved: AutoModeConfig,
|
|
518
|
+
warnings: string[],
|
|
519
|
+
): void {
|
|
520
|
+
warnUnknownFields(raw, AUTO_MODE_FIELDS, "autoMode", warnings);
|
|
521
|
+
resolved.enabled = toBoolean(raw.enabled, "autoMode.enabled", warnings) ?? resolved.enabled;
|
|
522
|
+
|
|
523
|
+
const models = toStringList(raw.models, "autoMode.models", warnings);
|
|
524
|
+
if (models !== undefined) {
|
|
525
|
+
resolved.models = models;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const reviewerModel = toModelSpec(raw.reviewerModel, "autoMode.reviewerModel", warnings);
|
|
529
|
+
if (reviewerModel !== undefined) {
|
|
530
|
+
resolved.reviewerModel = reviewerModel === null ? undefined : reviewerModel;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
resolved.gate = toAutoModeGate(raw.gate, "autoMode.gate", warnings) ?? resolved.gate;
|
|
534
|
+
|
|
535
|
+
const extraTools = toStringList(raw.extraTools, "autoMode.extraTools", warnings);
|
|
536
|
+
if (extraTools !== undefined) {
|
|
537
|
+
resolved.extraTools = extraTools;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const timeoutMs = toReviewerTimeoutMs(raw.timeoutMs, "autoMode.timeoutMs", warnings);
|
|
541
|
+
if (timeoutMs !== undefined) {
|
|
542
|
+
resolved.timeoutMs = timeoutMs;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
resolved.transcript = toBoolean(raw.transcript, "autoMode.transcript", warnings) ?? resolved.transcript;
|
|
546
|
+
resolved.evidenceTools =
|
|
547
|
+
toBoolean(raw.evidenceTools, "autoMode.evidenceTools", warnings) ?? resolved.evidenceTools;
|
|
548
|
+
|
|
549
|
+
const maxEvidenceRounds = toBoundedInteger(
|
|
550
|
+
raw.maxEvidenceRounds,
|
|
551
|
+
"autoMode.maxEvidenceRounds",
|
|
552
|
+
warnings,
|
|
553
|
+
EVIDENCE_ROUNDS_MIN,
|
|
554
|
+
EVIDENCE_ROUNDS_MAX,
|
|
555
|
+
);
|
|
556
|
+
if (maxEvidenceRounds !== undefined) {
|
|
557
|
+
resolved.maxEvidenceRounds = maxEvidenceRounds;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
if (raw.classifier !== undefined) {
|
|
561
|
+
if (isRecord(raw.classifier)) {
|
|
562
|
+
applyAutoModeClassifierConfig(raw.classifier, resolved.classifier, warnings);
|
|
563
|
+
} else {
|
|
564
|
+
warnings.push("Ignoring autoMode.classifier: expected a JSON object.");
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (raw.circuitBreaker !== undefined) {
|
|
569
|
+
if (isRecord(raw.circuitBreaker)) {
|
|
570
|
+
applyAutoModeBreakerConfig(raw.circuitBreaker, resolved.circuitBreaker, warnings);
|
|
571
|
+
} else {
|
|
572
|
+
warnings.push("Ignoring autoMode.circuitBreaker: expected a JSON object.");
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function applyAutoModeClassifierConfig(
|
|
578
|
+
raw: Record<string, unknown>,
|
|
579
|
+
resolved: AutoModeClassifierConfig,
|
|
580
|
+
warnings: string[],
|
|
581
|
+
): void {
|
|
582
|
+
warnUnknownFields(raw, AUTO_MODE_CLASSIFIER_FIELDS, "autoMode.classifier", warnings);
|
|
583
|
+
resolved.enabled =
|
|
584
|
+
toBoolean(raw.enabled, "autoMode.classifier.enabled", warnings) ?? resolved.enabled;
|
|
585
|
+
|
|
586
|
+
const model = toModelSpec(raw.model, "autoMode.classifier.model", warnings);
|
|
587
|
+
if (model !== undefined) {
|
|
588
|
+
resolved.model = model === null ? undefined : model;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const timeoutMs =
|
|
592
|
+
toBoundedInteger(
|
|
593
|
+
raw.timeoutMs,
|
|
594
|
+
"autoMode.classifier.timeoutMs",
|
|
595
|
+
warnings,
|
|
596
|
+
REVIEWER_TIMEOUT_MIN_MS,
|
|
597
|
+
REVIEWER_TIMEOUT_MAX_MS,
|
|
598
|
+
) ?? DEFAULT_CLASSIFIER_TIMEOUT_MS;
|
|
599
|
+
if (raw.timeoutMs !== undefined) {
|
|
600
|
+
resolved.timeoutMs = timeoutMs;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const maxLag = toBoundedInteger(
|
|
604
|
+
raw.maxLag,
|
|
605
|
+
"autoMode.classifier.maxLag",
|
|
606
|
+
warnings,
|
|
607
|
+
CLASSIFIER_MAX_LAG_MIN,
|
|
608
|
+
CLASSIFIER_MAX_LAG_MAX,
|
|
609
|
+
);
|
|
610
|
+
if (maxLag !== undefined) {
|
|
611
|
+
resolved.maxLag = maxLag;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function applyAutoModeBreakerConfig(
|
|
616
|
+
raw: Record<string, unknown>,
|
|
617
|
+
resolved: AutoModeCircuitBreakerConfig,
|
|
618
|
+
warnings: string[],
|
|
619
|
+
): void {
|
|
620
|
+
warnUnknownFields(raw, AUTO_MODE_BREAKER_FIELDS, "autoMode.circuitBreaker", warnings);
|
|
621
|
+
|
|
622
|
+
const consecutiveDenials = toBoundedInteger(
|
|
623
|
+
raw.consecutiveDenials,
|
|
624
|
+
"autoMode.circuitBreaker.consecutiveDenials",
|
|
625
|
+
warnings,
|
|
626
|
+
BREAKER_LIMIT_MIN,
|
|
627
|
+
BREAKER_LIMIT_MAX,
|
|
628
|
+
);
|
|
629
|
+
if (consecutiveDenials !== undefined) {
|
|
630
|
+
resolved.consecutiveDenials = consecutiveDenials;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const recentDenials = toBoundedInteger(
|
|
634
|
+
raw.recentDenials,
|
|
635
|
+
"autoMode.circuitBreaker.recentDenials",
|
|
636
|
+
warnings,
|
|
637
|
+
BREAKER_LIMIT_MIN,
|
|
638
|
+
BREAKER_LIMIT_MAX,
|
|
639
|
+
);
|
|
640
|
+
if (recentDenials !== undefined) {
|
|
641
|
+
resolved.recentDenials = recentDenials;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
const windowSize = toBoundedInteger(
|
|
645
|
+
raw.windowSize,
|
|
646
|
+
"autoMode.circuitBreaker.windowSize",
|
|
647
|
+
warnings,
|
|
648
|
+
BREAKER_WINDOW_MIN,
|
|
649
|
+
BREAKER_WINDOW_MAX,
|
|
650
|
+
);
|
|
651
|
+
if (windowSize !== undefined) {
|
|
652
|
+
resolved.windowSize = windowSize;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Normalize the supported unversioned document without I/O or mutation.
|
|
658
|
+
* Versioned decoding and operation resolution are owned by ../config.ts.
|
|
659
|
+
*/
|
|
660
|
+
export function normalizeLegacyConfig(
|
|
661
|
+
raw: Record<string, unknown> | undefined,
|
|
662
|
+
configPath: string = CONFIG_PATH,
|
|
663
|
+
warnings: string[] = [],
|
|
664
|
+
): LoadedToolkitConfig {
|
|
665
|
+
const resolved = cloneDefaults();
|
|
666
|
+
let source: string | undefined;
|
|
667
|
+
|
|
668
|
+
if (raw) {
|
|
669
|
+
source = configPath;
|
|
670
|
+
warnUnknownFields(raw, TOP_LEVEL_FIELDS, "", warnings);
|
|
671
|
+
|
|
672
|
+
if (raw.compaction !== undefined) {
|
|
673
|
+
if (isRecord(raw.compaction)) {
|
|
674
|
+
applyCompactionConfig(raw.compaction, resolved.compaction, warnings);
|
|
675
|
+
} else {
|
|
676
|
+
warnings.push("Ignoring compaction: expected a JSON object.");
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
if (raw.webSearch !== undefined) {
|
|
681
|
+
if (isRecord(raw.webSearch)) {
|
|
682
|
+
applyWebSearchConfig(raw.webSearch, resolved.webSearch, warnings);
|
|
683
|
+
} else {
|
|
684
|
+
warnings.push("Ignoring webSearch: expected a JSON object.");
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (raw.imageGeneration !== undefined) {
|
|
689
|
+
if (isRecord(raw.imageGeneration)) {
|
|
690
|
+
applyImageGenerationConfig(raw.imageGeneration, resolved.imageGeneration, warnings);
|
|
691
|
+
} else {
|
|
692
|
+
warnings.push("Ignoring imageGeneration: expected a JSON object.");
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (raw.autoMode !== undefined) {
|
|
697
|
+
if (isRecord(raw.autoMode)) {
|
|
698
|
+
applyAutoModeConfig(raw.autoMode, resolved.autoMode, warnings);
|
|
699
|
+
} else {
|
|
700
|
+
warnings.push("Ignoring autoMode: expected a JSON object.");
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
resolved.compaction.artifactRoot = resolveConfiguredPath(
|
|
706
|
+
resolved.compaction.artifactRoot,
|
|
707
|
+
path.dirname(configPath),
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
return {
|
|
711
|
+
config: resolved,
|
|
712
|
+
source,
|
|
713
|
+
warnings,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export { DEFAULT_TOOLKIT_CONFIG };
|