Repository navigation
Expand file tree
/
Copy pathpolicy.ts
More file actions
96 lines (90 loc) · 3.55 KB
/
Copy pathpolicy.ts
File metadata and controls
96 lines (90 loc) · 3.55 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
import { z } from 'zod';
export type ResearchModuleSlot = 'M1' | 'M3' | 'M4' | 'M5';
export type CustomResearchModule = {
id: string;
name: string;
slot: ResearchModuleSlot;
mode: 'append' | 'replace';
enabled: boolean;
instructions: string;
};
export type ResearchModulePlan = {
scheduler: 'plan' | 'critical-path' | 'continuation';
context: 'full' | 'essential';
custom: CustomResearchModule[];
};
const encoder = new TextEncoder();
const textBytes = (value: string) => encoder.encode(value).byteLength;
const noNul = (value: string) => !value.includes('\0');
const customResearchModuleSchema = z
.object({
id: z
.string()
.regex(/^[A-Za-z0-9_-]{1,64}$/u, '模块 ID 只能包含 1–64 个字母、数字、下划线或连字符。')
.refine(value => !['__proto__', 'prototype', 'constructor'].includes(value), '请更换模块 ID。'),
name: z.string().max(40, '模块名称最多 40 个字符。').refine(noNul, '模块名称不能包含空字符。'),
slot: z.enum(['M1', 'M3', 'M4', 'M5']),
mode: z.enum(['append', 'replace']),
enabled: z.boolean(),
instructions: z
.string()
.max(4_000, '单个模块的规则最多 4,000 字节。')
.refine(noNul, '模块规则不能包含空字符。')
.refine(value => textBytes(value) <= 4_000, '单个模块的规则最多 4,000 UTF-8 字节。'),
})
.strict();
export const researchModulePlanDraftSchema = z
.object({
scheduler: z.enum(['plan', 'critical-path', 'continuation']),
context: z.enum(['full', 'essential']),
custom: z.array(customResearchModuleSchema).max(8, '最多添加 8 个自定义模块。'),
})
.strict()
.superRefine((plan, context) => {
const ids = new Set<string>();
for (const [index, module] of plan.custom.entries()) {
if (ids.has(module.id))
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['custom', index, 'id'],
message: '自定义模块 ID 不能重复。',
});
ids.add(module.id);
}
if (plan.custom.reduce((bytes, module) => bytes + textBytes(module.instructions), 0) > 16_000)
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['custom'],
message: '全部自定义规则最多 16,000 UTF-8 字节。',
});
});
export const researchModulePlanSchema = researchModulePlanDraftSchema.superRefine((plan, context) => {
const replaced = new Set<ResearchModuleSlot>();
for (const [index, module] of plan.custom.entries()) {
if (!module.name.trim())
context.addIssue({ code: z.ZodIssueCode.custom, path: ['custom', index, 'name'], message: '请填写模块名称。' });
if (!module.enabled) continue;
if (!module.instructions.trim())
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['custom', index, 'instructions'],
message: '启用模块前请填写规则。',
});
if (module.mode === 'replace') {
if (replaced.has(module.slot))
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['custom', index, 'mode'],
message: '每个组件最多启用一个替换模块。',
});
replaced.add(module.slot);
}
}
});
export function defaultResearchModulePlan(): ResearchModulePlan {
return { scheduler: 'plan', context: 'full', custom: [] };
}
/** Old presets remain readable. All new runs use the one implemented scheduler. */
export function currentResearchModulePlan(plan: ResearchModulePlan): ResearchModulePlan {
return plan.scheduler === 'plan' ? plan : { ...plan, scheduler: 'plan' };
}