Repository navigation
Expand file tree
/
Copy pathschema.ts
More file actions
611 lines (547 loc) · 20.8 KB
/
Copy pathschema.ts
File metadata and controls
611 lines (547 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* eslint-disable max-lines -- zcode-cli 配置 schema 需要集中维护文件解析和 provider 继承,拆散会让配置语义更难对齐。 */
import { z } from "zod";
import type { RuntimeConfigPatch } from "@zcode/contracts";
const stringRecordSchema = z.record(z.string(), z.string());
const unknownRecordSchema = z.record(z.string(), z.unknown());
const positiveNumberSchema = z.number().finite().positive();
const positiveIntegerSchema = z.number().int().positive();
const modelStreamSchema = z.object({
idleTimeoutMs: positiveNumberSchema.optional(),
});
const permissionSchema = z.object({
mode: z.enum(["plan", "build", "edit", "yolo", "guarded", "auto"]).optional(),
allowedTools: z.array(z.string()).optional(),
disallowedTools: z.array(z.string()).optional(),
autoApproveHighRisk: z.boolean().optional(),
allowMediumRiskInAuto: z.boolean().optional(),
});
const storageSchema = z.object({
dir: z.string().min(1).optional(),
sessionDbPath: z.string().min(1).optional(),
});
const networkSchema = z.object({
httpProxy: z.string().min(1).optional(),
noProxy: z.string().min(1).optional(),
caCertFile: z.string().min(1).optional(),
timeout: positiveNumberSchema.optional(),
});
const featuresSchema = z.object({
compact: z.boolean().optional(),
rewind: z.boolean().optional(),
subagent: z.boolean().optional(),
memory: z.boolean().optional(),
skill: z.boolean().optional(),
mcp: z.boolean().optional(),
});
const memorySchema = z.object({
use: z.boolean().optional(),
});
const mcpServerBaseSchema = {
alwaysLoad: z.boolean().optional(),
// Host DTO 和连接池已有 isolation;文件解析不能丢弃整条已支持的声明。
isolation: z.enum(["session", "workspace"]).optional(),
// 设置页和 MCP adapter 已支持协议选择;配置入口漏掉该字段会因 strict 校验丢弃整个 server。
protocolVersion: z.enum(["auto", "legacy", "2026-07-28"]).optional(),
enabled: z.boolean().optional(),
timeoutMs: positiveNumberSchema.optional(),
};
const mcpOAuthSchema = z.union([
z
.object({
type: z.literal("client_credentials"),
clientId: z.string().min(1),
clientSecret: z.string().min(1),
clientName: z.string().min(1).optional(),
scope: z.string().optional(),
})
.strict(),
z
.object({
type: z.literal("authorization_code"),
clientId: z.string().min(1).optional(),
clientSecret: z.string().min(1).optional(),
clientName: z.string().min(1).optional(),
redirectPath: z.string().min(1).optional(),
scope: z.string().optional(),
})
.strict(),
]);
const mcpStdioServerSchema = z
.object({
...mcpServerBaseSchema,
type: z.literal("stdio"),
command: z.string().min(1),
args: z.array(z.string()).optional(),
cwd: z.string().min(1).optional(),
env: stringRecordSchema.optional(),
})
.strict();
const mcpHttpServerSchema = z
.object({
...mcpServerBaseSchema,
type: z.literal("http"),
url: z.string().min(1),
headers: stringRecordSchema.optional(),
oauth: mcpOAuthSchema.optional(),
})
.strict();
const mcpSseServerSchema = z
.object({
...mcpServerBaseSchema,
type: z.literal("sse"),
url: z.string().min(1),
headers: stringRecordSchema.optional(),
oauth: mcpOAuthSchema.optional(),
})
.strict();
const mcpServerSchema = z.preprocess(
normalizeMcpServerConfigInput,
z.discriminatedUnion("type", [mcpStdioServerSchema, mcpHttpServerSchema, mcpSseServerSchema]),
);
const mcpSchema = z.object({
servers: z.record(z.string(), mcpServerSchema).optional(),
});
const pluginOptionValueSchema = z.union([z.string(), z.number(), z.boolean()]);
const pluginMarketplaceSourceSchema = z.discriminatedUnion("source", [
z
.object({
source: z.literal("url"),
url: z.string().min(1),
headers: stringRecordSchema.optional(),
})
.strict(),
z
.object({
source: z.literal("github"),
repo: z.string().min(1),
ref: z.string().min(1).optional(),
path: z.string().min(1).optional(),
sparsePaths: z.array(z.string().min(1)).optional(),
})
.strict(),
z
.object({
source: z.literal("git"),
url: z.string().min(1),
ref: z.string().min(1).optional(),
path: z.string().min(1).optional(),
sparsePaths: z.array(z.string().min(1)).optional(),
})
.strict(),
z
.object({
source: z.literal("npm"),
package: z.string().min(1),
})
.strict(),
z
.object({
source: z.literal("file"),
path: z.string().min(1),
})
.strict(),
z
.object({
source: z.literal("directory"),
path: z.string().min(1),
})
.strict(),
]);
const pluginsSchema = z.object({
enabled: z.boolean().optional(),
dirs: z.array(z.string().min(1)).optional(),
enabledPlugins: z.record(z.string(), z.boolean()).optional(),
extraKnownMarketplaces: z
.record(z.string().min(1), z.object({ source: pluginMarketplaceSourceSchema }).strict())
.optional(),
options: z.record(z.string(), z.record(z.string(), pluginOptionValueSchema)).optional(),
suppressedBuiltins: z.array(z.string().min(1)).optional(),
});
export const LEGACY_CUA_PLUGIN_ID = "zcode-cua@zcode-plugins-official";
export const CANONICAL_CUA_PLUGIN_ID = "computer-use@zcode-plugins-official";
export function canonicalizePluginId(pluginId: string): string {
return pluginId === LEGACY_CUA_PLUGIN_ID ? CANONICAL_CUA_PLUGIN_ID : pluginId;
}
export function pluginIdAliases(pluginId: string): readonly string[] {
return canonicalizePluginId(pluginId) === CANONICAL_CUA_PLUGIN_ID
? [CANONICAL_CUA_PLUGIN_ID, LEGACY_CUA_PLUGIN_ID]
: [pluginId];
}
const skillToggleSchema = z.object({
enable: z.boolean().optional(),
});
// skill / command 可用性覆盖:key 为对应 .md 的绝对路径,enable:false 表示禁用
const skillCommandOverrideSchema = z
.object({
enable: z.boolean().optional(),
})
.passthrough();
const skillCommandOverridesSchema = z.record(z.string(), skillCommandOverrideSchema);
const skillsSchema = z
.object({
enabled: z.boolean().optional(),
includeInstructions: z.boolean().optional(),
metadataBudget: positiveNumberSchema.optional(),
roots: z.array(z.string()).optional(),
})
.catchall(skillToggleSchema);
const loggingSchema = z.object({
level: z.enum(["debug", "info", "warn", "error"]).optional(),
format: z.enum(["text", "json"]).optional(),
});
const uiSchema = z.object({
locale: z.enum(["auto", "en-US", "zh-CN"]).optional(),
theme: z.enum(["auto", "dark", "light"]).optional(),
});
const toolConcurrencySchema = z.object({
maxConcurrency: positiveNumberSchema.optional(),
});
const modelAnomalyGuardSchema = z.object({
toolCallWarningThreshold: positiveIntegerSchema.optional(),
repeatedToolCallWarningThreshold: positiveIntegerSchema.optional(),
maxBudgetWarningsPerTurn: z.number().int().nonnegative().optional(),
});
// Hooks schema:
// 理想态是 re-export shared/workspace-hook-config,但两个 pnpm workspace 解析出物理
// 不同的 zod 实例(adapters 4.4.3 / shared 4.3.6):shared schema 嵌入本包组合 schema
// 会让 dts 引用 foreign zod 内部类型(TS2742),typeof/ZodType 注解都会落入类型循环。
// 因此本副本按原样保留(本包 zod 构造),并保持与 shared 的校验语义等价;
// 运行时校验语义仍以 shared 为准(discovery/trust 装配入口都走 shared schema——
// 本 schema 只负责配置文件装载诊断)。若未来统一 zod 实例,应删除本副本改 re-export。
export const hookProcessSchema = z
.object({
type: z.literal("process"),
command: z.string().min(1),
enabled: z.boolean().optional(),
args: z.array(z.string()).optional(),
timeoutMs: positiveNumberSchema.optional(),
statusMessage: z.string().min(1).optional(),
onError: z.enum(["allow", "deny"]).optional(),
})
.passthrough();
export const hookCommandSchema = z
.object({
type: z.literal("command"),
command: z.string().min(1),
enabled: z.boolean().optional(),
async: z.boolean().optional(),
shell: z.union([z.literal(true), z.string().min(1)]).optional(),
timeout: positiveNumberSchema.optional(),
timeoutMs: positiveNumberSchema.optional(),
statusMessage: z.string().min(1).optional(),
onError: z.enum(["allow", "deny"]).optional(),
})
.passthrough();
export const hookMatcherSchema = z
.object({
matcher: z.string().min(1).optional(),
hooks: z.array(z.discriminatedUnion("type", [hookProcessSchema, hookCommandSchema])).min(1),
})
.strict();
export const hooksSchema = z
.object({
enabled: z.boolean().optional(),
timeoutMs: positiveNumberSchema.optional(),
maxOutputBytes: positiveNumberSchema.optional(),
events: z
.object({
SessionStart: z.array(hookMatcherSchema).optional(),
UserPromptSubmit: z.array(hookMatcherSchema).optional(),
PreToolUse: z.array(hookMatcherSchema).optional(),
PermissionRequest: z.array(hookMatcherSchema).optional(),
PostToolUse: z.array(hookMatcherSchema).optional(),
PostToolUseFailure: z.array(hookMatcherSchema).optional(),
Stop: z.array(hookMatcherSchema).optional(),
})
.strict()
.optional(),
})
.strict();
export const ZCodeConfigFileSchema = z
.object({
$schema: z.string().optional(),
modelStream: modelStreamSchema.optional(),
permission: permissionSchema.optional(),
storage: storageSchema.optional(),
network: networkSchema.optional(),
features: featuresSchema.optional(),
memory: memorySchema.optional(),
mcp: mcpSchema.optional(),
plugins: pluginsSchema.optional(),
skills: skillsSchema.optional(),
skill: skillCommandOverridesSchema.optional(),
command: skillCommandOverridesSchema.optional(),
logging: loggingSchema.optional(),
ui: uiSchema.optional(),
toolConcurrency: toolConcurrencySchema.optional(),
modelAnomalyGuard: modelAnomalyGuardSchema.optional(),
hooks: hooksSchema.optional(),
})
.passthrough();
// 项目曾复用用户配置 schema,可改写代理、CA 和插件存储根目录。只开放扩展声明,
// 在校验前剔除运行参数,防止新增用户字段自动向项目开放,也避免无效的禁用字段拖垮 MCP/Hook。
const projectConfigFileSchema = ZCodeConfigFileSchema.pick({
mcp: true,
hooks: true,
skill: true,
command: true,
})
.extend({
plugins: pluginsSchema.pick({ enabledPlugins: true, options: true }).optional(),
skills: z
.preprocess(
(value) =>
isPlainRecord(value)
? Object.fromEntries(
Object.entries(value).filter(([path]) => isAbsoluteConfigPath(path)),
)
: value,
z.record(z.string(), skillToggleSchema),
)
.optional(),
})
.strip();
export type ZCodeConfigFile = z.infer<typeof ZCodeConfigFileSchema>;
type SkillCommandOverrideMap = Record<string, { enable?: boolean }>;
export type ConfigDiagnosticSeverity = "warning" | "error";
export type ConfigDiagnosticCode =
| "config_file_invalid"
| "config_mcp_server_invalid"
| "config_project_hooks_pending_trust";
export interface ConfigDiagnostic {
code: ConfigDiagnosticCode;
filePath?: string;
message: string;
path?: string;
severity: ConfigDiagnosticSeverity;
}
export interface ParseConfigFileResult {
config: RuntimeConfigPatch;
diagnostics: ConfigDiagnostic[];
}
function normalizeMcpServerConfigInput(value: unknown): unknown {
if (typeof value !== "object" || value === null || Array.isArray(value)) {
return value;
}
const server = { ...(value as Record<string, unknown>) };
if (!("env" in server) && "environment" in server) {
// 旧配置和部分外部导入使用 environment;运行态只消费 env。
// 在解析入口归一化,避免一个 legacy MCP server 拖垮整份 config。
server.env = server.environment;
}
delete server.environment;
if (typeof server.enable === "boolean" || typeof server.enabled === "boolean") {
// 历史遗留兜底:桌面端早期把停用状态写成 enable,而契约字段一直是 enabled。
// 桌面端与 mcp-sync 现在只写 enabled,并在加载配置时把存量 enable 就地迁移落盘;
// CLI 是只读解析、不能写盘,这里只覆盖「用户先跑 CLI、还没开过桌面端」的窗口。
// 冲突时以「停用」为准:桌面端写 enable:false 时不会清理外部导入残留的 enabled:true,
// 按 enabled 取值会把用户关掉的 server 重新拉起(停用了还在被调用)。
// 存量配置迁移完毕后,这段连同下面的 delete 可以整块删除。
server.enabled = server.enable === false ? false : (server.enabled ?? true);
}
delete server.enable;
// External MCP configs may carry provider-specific timeout fields. ZCode does not migrate
// those values, but the strict runtime schema should still accept old imported entries.
delete server.timeout;
delete server.startup_timeout_sec;
if (server.type === "remote") {
// 外部 Agent 配置常把 HTTP MCP 标记为 remote;ZCode 运行态协议类型是 http。
server.type = "http";
} else if (typeof server.type !== "string") {
// app 管理层把 command 形态视为默认 stdio;CLI 也需要同样推断,
// 否则历史 app 配置缺少 type 时会阻断模型配置加载。
if (typeof server.command === "string" && server.command.trim().length > 0) {
server.type = "stdio";
} else if (typeof server.url === "string" && server.url.trim().length > 0) {
server.type = "http";
}
}
if (
(server.type === "http" || server.type === "sse") &&
server.headers === undefined &&
server.http_headers !== undefined
) {
// 兼容历史 BigModel MCP 配置:旧字段名是 http_headers,agent runtime 只消费 headers。
server.headers = server.http_headers;
}
delete server.http_headers;
return server;
}
export function parseConfigFileToRuntimePatchWithDiagnostics(
value: unknown,
scope: "user" | "project" = "user",
): ParseConfigFileResult {
const diagnostics: ConfigDiagnostic[] = [];
const normalized = normalizeConfigFileInput(value, diagnostics);
const parsed = (scope === "project" ? projectConfigFileSchema : ZCodeConfigFileSchema).parse(
normalized,
);
return {
config: parsedConfigFileToRuntimePatch(parsed),
diagnostics,
};
}
function parsedConfigFileToRuntimePatch(parsed: ZCodeConfigFile): RuntimeConfigPatch {
const config: RuntimeConfigPatch = {};
if (parsed.modelStream) config.modelStream = parsed.modelStream;
if (parsed.permission) config.permission = parsed.permission;
if (parsed.storage) config.storage = parsed.storage;
if (parsed.network) config.network = parsed.network;
if (parsed.features) config.features = parsed.features;
if (parsed.memory) config.memory = parsed.memory;
if (parsed.mcp) config.mcp = parsed.mcp;
if (parsed.plugins) config.plugins = normalizePluginConfig(parsed.plugins);
const skillsConfig = parseSkillsRuntimeConfig(parsed.skills);
if (skillsConfig) config.skills = skillsConfig;
const skillOverrides = mergeSkillCommandOverrides(
parsed.skill,
parseSkillOverridesFromPluralSkills(parsed.skills),
);
if (skillOverrides) config.skillOverrides = skillOverrides;
if (parsed.command) config.commandOverrides = parsed.command;
if (parsed.logging) config.logging = parsed.logging;
if (parsed.ui) config.ui = parsed.ui;
if (parsed.toolConcurrency) config.toolConcurrency = parsed.toolConcurrency;
if (parsed.modelAnomalyGuard) config.modelAnomalyGuard = parsed.modelAnomalyGuard;
if (parsed.hooks) config.hooks = parsed.hooks;
return config;
}
function normalizePluginConfig(
plugins: ZCodeConfigFile["plugins"],
): NonNullable<RuntimeConfigPatch["plugins"]> {
if (!plugins) return {};
const enabledPlugins = plugins.enabledPlugins ? { ...plugins.enabledPlugins } : undefined;
if (enabledPlugins?.[LEGACY_CUA_PLUGIN_ID] !== undefined) {
if (enabledPlugins[CANONICAL_CUA_PLUGIN_ID] === undefined) {
enabledPlugins[CANONICAL_CUA_PLUGIN_ID] = enabledPlugins[LEGACY_CUA_PLUGIN_ID];
}
delete enabledPlugins[LEGACY_CUA_PLUGIN_ID];
}
const suppressedBuiltins = plugins.suppressedBuiltins
? plugins.suppressedBuiltins.reduce<string[]>((ids, id) => {
const canonicalId = id === LEGACY_CUA_PLUGIN_ID ? CANONICAL_CUA_PLUGIN_ID : id;
if (canonicalId === CANONICAL_CUA_PLUGIN_ID && ids.includes(CANONICAL_CUA_PLUGIN_ID)) {
return ids;
}
ids.push(canonicalId);
return ids;
}, [])
: undefined;
const options = plugins.options ? { ...plugins.options } : undefined;
if (options?.[LEGACY_CUA_PLUGIN_ID] !== undefined) {
if (options[CANONICAL_CUA_PLUGIN_ID] === undefined) {
options[CANONICAL_CUA_PLUGIN_ID] = options[LEGACY_CUA_PLUGIN_ID];
}
delete options[LEGACY_CUA_PLUGIN_ID];
}
return {
...plugins,
...(enabledPlugins ? { enabledPlugins } : {}),
...(suppressedBuiltins ? { suppressedBuiltins } : {}),
...(options ? { options } : {}),
};
}
function normalizeConfigFileInput(value: unknown, diagnostics: ConfigDiagnostic[]): unknown {
if (!isPlainRecord(value)) return value;
const root = { ...value };
const mcp = root.mcp;
if (!isPlainRecord(mcp)) return root;
const servers = mcp.servers;
if (servers === undefined) return root;
if (!isPlainRecord(servers)) {
diagnostics.push({
code: "config_mcp_server_invalid",
message: "mcp.servers must be a JSON object; ignoring all MCP servers.",
path: "mcp.servers",
severity: "warning",
});
root.mcp = {
...mcp,
servers: {},
};
return root;
}
const parsedServers: Record<string, unknown> = {};
for (const [name, server] of Object.entries(servers)) {
const parsed = mcpServerSchema.safeParse(server);
if (parsed.success) {
parsedServers[name] = parsed.data;
continue;
}
// MCP server 是可选工具配置,单个 server 错误不应导致 provider/plugin 等配置丢失。
diagnostics.push({
code: "config_mcp_server_invalid",
message: formatMcpServerError(parsed.error),
path: `mcp.servers.${name}`,
severity: "warning",
});
}
root.mcp = {
...mcp,
servers: parsedServers,
};
return root;
}
function formatMcpServerError(error: z.ZodError): string {
return error.issues
.map((issue) => {
const path = issue.path.length > 0 ? issue.path.join(".") : "<server>";
return `${path}: ${issue.message}`;
})
.join("; ");
}
function isPlainRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function readPositiveNumber(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
}
function parseSkillsRuntimeConfig(
skills: ZCodeConfigFile["skills"],
): RuntimeConfigPatch["skills"] | undefined {
if (!skills) return undefined;
const next: NonNullable<RuntimeConfigPatch["skills"]> = {};
if (skills.enabled !== undefined) next.enabled = skills.enabled;
if (skills.includeInstructions !== undefined) {
next.includeInstructions = skills.includeInstructions;
}
if (skills.metadataBudget !== undefined) next.metadataBudget = skills.metadataBudget;
if (skills.roots !== undefined) next.roots = skills.roots;
return Object.keys(next).length > 0 ? next : undefined;
}
function parseSkillOverridesFromPluralSkills(
skills: ZCodeConfigFile["skills"],
): SkillCommandOverrideMap | undefined {
if (!skills) return undefined;
const overrides: SkillCommandOverrideMap = {};
for (const [path, value] of Object.entries(skills)) {
if (!isAbsoluteConfigPath(path) || !isSkillCommandOverrideValue(value)) {
continue;
}
// 设置页当前把单个 skill 开关写到 skills[SKILL.md 绝对路径]。
// agent 运行态只消费 skillOverrides,所以这里在配置解析阶段统一映射,避免 UI 状态和 agent 实际加载分叉。
overrides[path] = { enable: value.enable };
}
return Object.keys(overrides).length > 0 ? overrides : undefined;
}
function mergeSkillCommandOverrides(
...overridesList: Array<SkillCommandOverrideMap | undefined>
): SkillCommandOverrideMap | undefined {
const merged: SkillCommandOverrideMap = {};
for (const overrides of overridesList) {
if (!overrides) continue;
Object.assign(merged, overrides);
}
return Object.keys(merged).length > 0 ? merged : undefined;
}
function isSkillCommandOverrideValue(value: unknown): value is { enable?: boolean } {
return (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
("enable" in value ? typeof (value as { enable?: unknown }).enable === "boolean" : true)
);
}
function isAbsoluteConfigPath(path: string): boolean {
return path.startsWith("/") || path.startsWith("\\") || /^[A-Za-z]:[\\/]/.test(path);
}