| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- export const AGENT_BUILTIN_TOOL_IDS = [
- 'read',
- 'write',
- 'edit',
- 'bash',
- 'task_query',
- 'task_output',
- 'task_stop',
- 'grep',
- 'glob',
- 'todowrite',
- 'web_fetch',
- 'website_deploy',
- ] as const;
- export type AgentBuiltinToolId = (typeof AGENT_BUILTIN_TOOL_IDS)[number];
- /**
- * Standalone Builtin Matrix MCP tools that may be selected independently.
- * `web_search` is feature-owned because its Tool and Prompt are gated together
- * through `features.webSearch`.
- */
- export const AGENT_BUILTIN_MCP_TOOL_IDS = [
- 'images_understand',
- 'image_synthesize',
- 'images_search_and_download',
- 'image_reverse_search',
- 'submit_video_generation',
- 'query_video_generation',
- 'gen_videos',
- 'batch_text_to_video',
- 'batch_image_to_video',
- 'get_voice_list',
- 'batch_text_to_audio',
- 'batch_text_to_music',
- 'synthesize_speech',
- 'batch_synthesize_speech',
- 'audios_understand',
- 'videos_understand',
- 'transcribe_audio',
- ] as const;
- export type AgentBuiltinMcpToolId = (typeof AGENT_BUILTIN_MCP_TOOL_IDS)[number];
- /**
- * Standalone Builtin Skills that may be selected independently.
- * Skills owned by another capability are derived at runtime so they cannot
- * outlive the Tool or feature that makes their instructions executable.
- */
- export const AGENT_BUILTIN_SKILL_IDS = [
- 'code-review',
- 'deep-research',
- 'deploy-website',
- 'docx',
- 'edit-deployed-website',
- 'init',
- 'lark-tools',
- 'llm-call',
- 'mcode-tools-master',
- 'mavis-doctor',
- 'pdf',
- 'plugin-creator',
- 'pptx',
- 'skill-creator',
- 'skill-refiner',
- 'visual-page',
- 'x-link-reader',
- 'xlsx',
- ] as const;
- export type AgentBuiltinSkillId = (typeof AGENT_BUILTIN_SKILL_IDS)[number];
- export const AGENT_CAPABILITY_OWNED_SKILL_IDS = [
- 'control-in-app-browser',
- 'cu-desktop',
- 'resume-codex',
- 'mavis',
- 'create-agent',
- 'minimax-code-product',
- 'miniapp-creator',
- ] as const;
- export interface AgentCapabilityConfig {
- /** Saved model configuration used by delegated child agents by default. */
- modelConfigId?: string;
- persona?: { enabled?: boolean };
- /** Undefined keeps every currently available Builtin Tool; [] selects none. */
- tools?: AgentBuiltinToolId[];
- /** Undefined keeps every standalone Builtin Matrix MCP Tool; [] selects none. */
- builtinTools?: AgentBuiltinMcpToolId[];
- /** Undefined keeps every compatible standalone Builtin Skill; [] selects none. */
- skills?: AgentBuiltinSkillId[];
- features?: {
- mavis?: boolean;
- delegation?: boolean;
- webSearch?: boolean;
- };
- }
- export interface AgentsConfig {
- default: AgentCapabilityConfig;
- }
- export interface ResolvedAgentCapabilities {
- persona: { enabled: boolean };
- tools: AgentBuiltinToolId[] | undefined;
- builtinTools: AgentBuiltinMcpToolId[] | undefined;
- skills: AgentBuiltinSkillId[] | undefined;
- features: {
- mavis: boolean;
- delegation: boolean;
- webSearch: boolean;
- };
- }
- const ROOT_KEYS = new Set([
- 'modelConfigId',
- 'persona',
- 'tools',
- 'builtinTools',
- 'skills',
- 'features',
- ]);
- const PERSONA_KEYS = new Set(['enabled']);
- const FEATURE_KEYS = new Set(['mavis', 'delegation', 'webSearch']);
- const TOOL_IDS = new Set<string>(AGENT_BUILTIN_TOOL_IDS);
- const TASK_CONTROL_IDS: readonly AgentBuiltinToolId[] = ['task_query', 'task_output', 'task_stop'];
- const MCP_TOOL_IDS = new Set<string>(AGENT_BUILTIN_MCP_TOOL_IDS);
- const SKILL_IDS = new Set<string>(AGENT_BUILTIN_SKILL_IDS);
- const OWNED_SKILL_IDS = new Set<string>(AGENT_CAPABILITY_OWNED_SKILL_IDS);
- // Preserve existing user allowlists while removing a retired ID. The strict
- // bundled-agent parser intentionally continues to reject retired IDs.
- const RETIRED_TOLERANT_SKILL_IDS = new Set(['plan-mode']);
- export function parseAgentsConfig(raw: unknown): AgentsConfig {
- const root = asRecord(raw);
- const defaults = asRecord(root?.default);
- return { default: defaults ? parseTolerantAgentCapabilityConfig(defaults) : {} };
- }
- /** Strict parser for bundled built-in agent.md capability overrides. */
- export function parseAgentCapabilityConfig(raw: unknown, path = 'agent'): AgentCapabilityConfig {
- if (raw === undefined) return {};
- const obj = readObject(raw, path);
- rejectUnknownKeys(obj, ROOT_KEYS, path);
- const result: AgentCapabilityConfig = {};
- if (obj.modelConfigId !== undefined) {
- if (typeof obj.modelConfigId !== 'string' || !obj.modelConfigId.trim()) {
- invalid(`${path}.modelConfigId`, 'must be a non-empty string');
- }
- result.modelConfigId = obj.modelConfigId.trim();
- }
- if (obj.persona !== undefined) {
- const persona = readObject(obj.persona, `${path}.persona`);
- rejectUnknownKeys(persona, PERSONA_KEYS, `${path}.persona`);
- if (persona.enabled !== undefined && typeof persona.enabled !== 'boolean') {
- invalid(`${path}.persona.enabled`, 'must be a boolean');
- }
- result.persona = {
- ...(typeof persona.enabled === 'boolean' ? { enabled: persona.enabled } : {}),
- };
- }
- if (obj.tools !== undefined) {
- if (!Array.isArray(obj.tools)) invalid(`${path}.tools`, 'must be an array');
- result.tools = obj.tools.map((value, index) => {
- if (typeof value !== 'string' || !TOOL_IDS.has(value)) {
- invalid(`${path}.tools[${index}]`, `must be one of ${AGENT_BUILTIN_TOOL_IDS.join(', ')}`);
- }
- return value as AgentBuiltinToolId;
- });
- }
- if (obj.builtinTools !== undefined) {
- if (!Array.isArray(obj.builtinTools)) invalid(`${path}.builtinTools`, 'must be an array');
- result.builtinTools = obj.builtinTools.map((value, index) => {
- if (value === 'web_search') {
- invalid(
- `${path}.builtinTools[${index}]`,
- 'web_search is feature-owned; use features.webSearch',
- );
- }
- if (typeof value !== 'string' || !MCP_TOOL_IDS.has(value)) {
- invalid(
- `${path}.builtinTools[${index}]`,
- `must be one of ${AGENT_BUILTIN_MCP_TOOL_IDS.join(', ')}`,
- );
- }
- return value as AgentBuiltinMcpToolId;
- });
- }
- if (obj.skills !== undefined) {
- if (!Array.isArray(obj.skills)) invalid(`${path}.skills`, 'must be an array');
- result.skills = obj.skills.map((value, index) => {
- if (typeof value === 'string' && OWNED_SKILL_IDS.has(value.trim())) {
- const name = value.trim();
- invalid(
- `${path}.skills[${index}]`,
- `${name} is capability-owned; use its owning Tool or feature switch`,
- );
- }
- if (typeof value !== 'string' || !SKILL_IDS.has(value.trim())) {
- invalid(`${path}.skills[${index}]`, `must be one of ${AGENT_BUILTIN_SKILL_IDS.join(', ')}`);
- }
- return value.trim() as AgentBuiltinSkillId;
- });
- }
- if (obj.features !== undefined) {
- const features = readObject(obj.features, `${path}.features`);
- rejectUnknownKeys(features, FEATURE_KEYS, `${path}.features`);
- for (const key of FEATURE_KEYS) {
- if (features[key] !== undefined && typeof features[key] !== 'boolean') {
- invalid(`${path}.features.${key}`, 'must be a boolean');
- }
- }
- result.features = {
- ...(typeof features.mavis === 'boolean' ? { mavis: features.mavis } : {}),
- ...(typeof features.delegation === 'boolean' ? { delegation: features.delegation } : {}),
- ...(typeof features.webSearch === 'boolean' ? { webSearch: features.webSearch } : {}),
- };
- }
- return result;
- }
- function parseTolerantAgentCapabilityConfig(obj: Record<string, unknown>): AgentCapabilityConfig {
- const result: AgentCapabilityConfig = {};
- if (typeof obj.modelConfigId === 'string' && obj.modelConfigId.trim()) {
- result.modelConfigId = obj.modelConfigId.trim();
- }
- const persona = asRecord(obj.persona);
- if (typeof persona?.enabled === 'boolean') {
- result.persona = { enabled: persona.enabled };
- }
- if (isKnownIdArray(obj.tools, TOOL_IDS)) {
- const tools = [...obj.tools] as AgentBuiltinToolId[];
- // Older user allowlists predate configurable task controls: those tools
- // remained available even with tools: [] or delegation disabled. Preserve
- // that behavior unless the list explicitly selects the new control IDs.
- // Bundled agent.md uses the strict parser and keeps its exact ceiling.
- if (!TASK_CONTROL_IDS.some((id) => tools.includes(id))) {
- tools.push(...TASK_CONTROL_IDS);
- }
- result.tools = tools;
- }
- if (isKnownIdArray(obj.builtinTools, MCP_TOOL_IDS)) {
- result.builtinTools = [...obj.builtinTools] as AgentBuiltinMcpToolId[];
- }
- if (Array.isArray(obj.skills)) {
- const normalizedSkills = obj.skills.map((value) =>
- typeof value === 'string' ? value.trim() : value,
- );
- if (
- normalizedSkills.every(
- (value) =>
- typeof value === 'string' &&
- (SKILL_IDS.has(value) || RETIRED_TOLERANT_SKILL_IDS.has(value)) &&
- !OWNED_SKILL_IDS.has(value),
- )
- ) {
- result.skills = normalizedSkills.filter(
- (value): value is AgentBuiltinSkillId => typeof value === 'string' && SKILL_IDS.has(value),
- );
- }
- }
- const features = asRecord(obj.features);
- if (features) {
- const parsedFeatures: NonNullable<AgentCapabilityConfig['features']> = {};
- for (const key of FEATURE_KEYS as Set<keyof typeof parsedFeatures>) {
- if (typeof features[key] === 'boolean') parsedFeatures[key] = features[key];
- }
- if (Object.keys(parsedFeatures).length > 0) result.features = parsedFeatures;
- }
- return result;
- }
- function asRecord(value: unknown): Record<string, unknown> | undefined {
- return typeof value === 'object' && value !== null && !Array.isArray(value)
- ? (value as Record<string, unknown>)
- : undefined;
- }
- function isKnownIdArray(
- value: unknown,
- knownIds: ReadonlySet<string>,
- ): value is AgentBuiltinToolId[] | AgentBuiltinMcpToolId[] {
- return (
- Array.isArray(value) && value.every((item) => typeof item === 'string' && knownIds.has(item))
- );
- }
- export function resolveAgentCapabilities(
- defaults: AgentCapabilityConfig = {},
- override: AgentCapabilityConfig = {},
- ): ResolvedAgentCapabilities {
- return {
- persona: {
- enabled: override.persona?.enabled ?? defaults.persona?.enabled ?? true,
- },
- tools: cloneOptionalArray(override.tools ?? defaults.tools),
- builtinTools: cloneOptionalArray(override.builtinTools ?? defaults.builtinTools),
- skills: cloneOptionalArray(override.skills ?? defaults.skills),
- features: {
- mavis: override.features?.mavis ?? defaults.features?.mavis ?? true,
- delegation: override.features?.delegation ?? defaults.features?.delegation ?? true,
- webSearch: override.features?.webSearch ?? defaults.features?.webSearch ?? true,
- },
- };
- }
- export function isAgentBuiltinToolEnabled(
- capabilities: ResolvedAgentCapabilities,
- toolName: AgentBuiltinToolId,
- ): boolean {
- return capabilities.tools === undefined || capabilities.tools.includes(toolName);
- }
- export function isAgentBuiltinMcpToolEnabled(
- capabilities: ResolvedAgentCapabilities,
- toolName: AgentBuiltinMcpToolId,
- ): boolean {
- return capabilities.builtinTools === undefined || capabilities.builtinTools.includes(toolName);
- }
- export function isAgentBuiltinSkillEnabled(
- capabilities: ResolvedAgentCapabilities,
- skillName: AgentBuiltinSkillId,
- ): boolean {
- return capabilities.skills === undefined || capabilities.skills.includes(skillName);
- }
- function cloneOptionalArray<T>(value: readonly T[] | undefined): T[] | undefined {
- return value === undefined ? undefined : [...value];
- }
- function readObject(value: unknown, path: string): Record<string, unknown> {
- if (value == null || typeof value !== 'object' || Array.isArray(value)) {
- invalid(path, 'must be an object');
- }
- return value as Record<string, unknown>;
- }
- function rejectUnknownKeys(
- value: Record<string, unknown>,
- allowed: ReadonlySet<string>,
- path: string,
- ): void {
- for (const key of Object.keys(value)) {
- if (!allowed.has(key)) invalid(`${path}.${key}`, 'is not supported');
- }
- }
- function invalid(path: string, message: string): never {
- throw new Error(`Invalid configuration at ${path}: ${message}`);
- }
|