agent-capabilities.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. export const AGENT_BUILTIN_TOOL_IDS = [
  2. 'read',
  3. 'write',
  4. 'edit',
  5. 'bash',
  6. 'task_query',
  7. 'task_output',
  8. 'task_stop',
  9. 'grep',
  10. 'glob',
  11. 'todowrite',
  12. 'web_fetch',
  13. 'website_deploy',
  14. ] as const;
  15. export type AgentBuiltinToolId = (typeof AGENT_BUILTIN_TOOL_IDS)[number];
  16. /**
  17. * Standalone Builtin Matrix MCP tools that may be selected independently.
  18. * `web_search` is feature-owned because its Tool and Prompt are gated together
  19. * through `features.webSearch`.
  20. */
  21. export const AGENT_BUILTIN_MCP_TOOL_IDS = [
  22. 'images_understand',
  23. 'image_synthesize',
  24. 'images_search_and_download',
  25. 'image_reverse_search',
  26. 'submit_video_generation',
  27. 'query_video_generation',
  28. 'gen_videos',
  29. 'batch_text_to_video',
  30. 'batch_image_to_video',
  31. 'get_voice_list',
  32. 'batch_text_to_audio',
  33. 'batch_text_to_music',
  34. 'synthesize_speech',
  35. 'batch_synthesize_speech',
  36. 'audios_understand',
  37. 'videos_understand',
  38. 'transcribe_audio',
  39. ] as const;
  40. export type AgentBuiltinMcpToolId = (typeof AGENT_BUILTIN_MCP_TOOL_IDS)[number];
  41. /**
  42. * Standalone Builtin Skills that may be selected independently.
  43. * Skills owned by another capability are derived at runtime so they cannot
  44. * outlive the Tool or feature that makes their instructions executable.
  45. */
  46. export const AGENT_BUILTIN_SKILL_IDS = [
  47. 'code-review',
  48. 'deep-research',
  49. 'deploy-website',
  50. 'docx',
  51. 'edit-deployed-website',
  52. 'init',
  53. 'lark-tools',
  54. 'llm-call',
  55. 'mcode-tools-master',
  56. 'mavis-doctor',
  57. 'pdf',
  58. 'plugin-creator',
  59. 'pptx',
  60. 'skill-creator',
  61. 'skill-refiner',
  62. 'visual-page',
  63. 'x-link-reader',
  64. 'xlsx',
  65. ] as const;
  66. export type AgentBuiltinSkillId = (typeof AGENT_BUILTIN_SKILL_IDS)[number];
  67. export const AGENT_CAPABILITY_OWNED_SKILL_IDS = [
  68. 'control-in-app-browser',
  69. 'cu-desktop',
  70. 'resume-codex',
  71. 'mavis',
  72. 'create-agent',
  73. 'minimax-code-product',
  74. 'miniapp-creator',
  75. ] as const;
  76. export interface AgentCapabilityConfig {
  77. /** Saved model configuration used by delegated child agents by default. */
  78. modelConfigId?: string;
  79. persona?: { enabled?: boolean };
  80. /** Undefined keeps every currently available Builtin Tool; [] selects none. */
  81. tools?: AgentBuiltinToolId[];
  82. /** Undefined keeps every standalone Builtin Matrix MCP Tool; [] selects none. */
  83. builtinTools?: AgentBuiltinMcpToolId[];
  84. /** Undefined keeps every compatible standalone Builtin Skill; [] selects none. */
  85. skills?: AgentBuiltinSkillId[];
  86. features?: {
  87. mavis?: boolean;
  88. delegation?: boolean;
  89. webSearch?: boolean;
  90. };
  91. }
  92. export interface AgentsConfig {
  93. default: AgentCapabilityConfig;
  94. }
  95. export interface ResolvedAgentCapabilities {
  96. persona: { enabled: boolean };
  97. tools: AgentBuiltinToolId[] | undefined;
  98. builtinTools: AgentBuiltinMcpToolId[] | undefined;
  99. skills: AgentBuiltinSkillId[] | undefined;
  100. features: {
  101. mavis: boolean;
  102. delegation: boolean;
  103. webSearch: boolean;
  104. };
  105. }
  106. const ROOT_KEYS = new Set([
  107. 'modelConfigId',
  108. 'persona',
  109. 'tools',
  110. 'builtinTools',
  111. 'skills',
  112. 'features',
  113. ]);
  114. const PERSONA_KEYS = new Set(['enabled']);
  115. const FEATURE_KEYS = new Set(['mavis', 'delegation', 'webSearch']);
  116. const TOOL_IDS = new Set<string>(AGENT_BUILTIN_TOOL_IDS);
  117. const TASK_CONTROL_IDS: readonly AgentBuiltinToolId[] = ['task_query', 'task_output', 'task_stop'];
  118. const MCP_TOOL_IDS = new Set<string>(AGENT_BUILTIN_MCP_TOOL_IDS);
  119. const SKILL_IDS = new Set<string>(AGENT_BUILTIN_SKILL_IDS);
  120. const OWNED_SKILL_IDS = new Set<string>(AGENT_CAPABILITY_OWNED_SKILL_IDS);
  121. // Preserve existing user allowlists while removing a retired ID. The strict
  122. // bundled-agent parser intentionally continues to reject retired IDs.
  123. const RETIRED_TOLERANT_SKILL_IDS = new Set(['plan-mode']);
  124. export function parseAgentsConfig(raw: unknown): AgentsConfig {
  125. const root = asRecord(raw);
  126. const defaults = asRecord(root?.default);
  127. return { default: defaults ? parseTolerantAgentCapabilityConfig(defaults) : {} };
  128. }
  129. /** Strict parser for bundled built-in agent.md capability overrides. */
  130. export function parseAgentCapabilityConfig(raw: unknown, path = 'agent'): AgentCapabilityConfig {
  131. if (raw === undefined) return {};
  132. const obj = readObject(raw, path);
  133. rejectUnknownKeys(obj, ROOT_KEYS, path);
  134. const result: AgentCapabilityConfig = {};
  135. if (obj.modelConfigId !== undefined) {
  136. if (typeof obj.modelConfigId !== 'string' || !obj.modelConfigId.trim()) {
  137. invalid(`${path}.modelConfigId`, 'must be a non-empty string');
  138. }
  139. result.modelConfigId = obj.modelConfigId.trim();
  140. }
  141. if (obj.persona !== undefined) {
  142. const persona = readObject(obj.persona, `${path}.persona`);
  143. rejectUnknownKeys(persona, PERSONA_KEYS, `${path}.persona`);
  144. if (persona.enabled !== undefined && typeof persona.enabled !== 'boolean') {
  145. invalid(`${path}.persona.enabled`, 'must be a boolean');
  146. }
  147. result.persona = {
  148. ...(typeof persona.enabled === 'boolean' ? { enabled: persona.enabled } : {}),
  149. };
  150. }
  151. if (obj.tools !== undefined) {
  152. if (!Array.isArray(obj.tools)) invalid(`${path}.tools`, 'must be an array');
  153. result.tools = obj.tools.map((value, index) => {
  154. if (typeof value !== 'string' || !TOOL_IDS.has(value)) {
  155. invalid(`${path}.tools[${index}]`, `must be one of ${AGENT_BUILTIN_TOOL_IDS.join(', ')}`);
  156. }
  157. return value as AgentBuiltinToolId;
  158. });
  159. }
  160. if (obj.builtinTools !== undefined) {
  161. if (!Array.isArray(obj.builtinTools)) invalid(`${path}.builtinTools`, 'must be an array');
  162. result.builtinTools = obj.builtinTools.map((value, index) => {
  163. if (value === 'web_search') {
  164. invalid(
  165. `${path}.builtinTools[${index}]`,
  166. 'web_search is feature-owned; use features.webSearch',
  167. );
  168. }
  169. if (typeof value !== 'string' || !MCP_TOOL_IDS.has(value)) {
  170. invalid(
  171. `${path}.builtinTools[${index}]`,
  172. `must be one of ${AGENT_BUILTIN_MCP_TOOL_IDS.join(', ')}`,
  173. );
  174. }
  175. return value as AgentBuiltinMcpToolId;
  176. });
  177. }
  178. if (obj.skills !== undefined) {
  179. if (!Array.isArray(obj.skills)) invalid(`${path}.skills`, 'must be an array');
  180. result.skills = obj.skills.map((value, index) => {
  181. if (typeof value === 'string' && OWNED_SKILL_IDS.has(value.trim())) {
  182. const name = value.trim();
  183. invalid(
  184. `${path}.skills[${index}]`,
  185. `${name} is capability-owned; use its owning Tool or feature switch`,
  186. );
  187. }
  188. if (typeof value !== 'string' || !SKILL_IDS.has(value.trim())) {
  189. invalid(`${path}.skills[${index}]`, `must be one of ${AGENT_BUILTIN_SKILL_IDS.join(', ')}`);
  190. }
  191. return value.trim() as AgentBuiltinSkillId;
  192. });
  193. }
  194. if (obj.features !== undefined) {
  195. const features = readObject(obj.features, `${path}.features`);
  196. rejectUnknownKeys(features, FEATURE_KEYS, `${path}.features`);
  197. for (const key of FEATURE_KEYS) {
  198. if (features[key] !== undefined && typeof features[key] !== 'boolean') {
  199. invalid(`${path}.features.${key}`, 'must be a boolean');
  200. }
  201. }
  202. result.features = {
  203. ...(typeof features.mavis === 'boolean' ? { mavis: features.mavis } : {}),
  204. ...(typeof features.delegation === 'boolean' ? { delegation: features.delegation } : {}),
  205. ...(typeof features.webSearch === 'boolean' ? { webSearch: features.webSearch } : {}),
  206. };
  207. }
  208. return result;
  209. }
  210. function parseTolerantAgentCapabilityConfig(obj: Record<string, unknown>): AgentCapabilityConfig {
  211. const result: AgentCapabilityConfig = {};
  212. if (typeof obj.modelConfigId === 'string' && obj.modelConfigId.trim()) {
  213. result.modelConfigId = obj.modelConfigId.trim();
  214. }
  215. const persona = asRecord(obj.persona);
  216. if (typeof persona?.enabled === 'boolean') {
  217. result.persona = { enabled: persona.enabled };
  218. }
  219. if (isKnownIdArray(obj.tools, TOOL_IDS)) {
  220. const tools = [...obj.tools] as AgentBuiltinToolId[];
  221. // Older user allowlists predate configurable task controls: those tools
  222. // remained available even with tools: [] or delegation disabled. Preserve
  223. // that behavior unless the list explicitly selects the new control IDs.
  224. // Bundled agent.md uses the strict parser and keeps its exact ceiling.
  225. if (!TASK_CONTROL_IDS.some((id) => tools.includes(id))) {
  226. tools.push(...TASK_CONTROL_IDS);
  227. }
  228. result.tools = tools;
  229. }
  230. if (isKnownIdArray(obj.builtinTools, MCP_TOOL_IDS)) {
  231. result.builtinTools = [...obj.builtinTools] as AgentBuiltinMcpToolId[];
  232. }
  233. if (Array.isArray(obj.skills)) {
  234. const normalizedSkills = obj.skills.map((value) =>
  235. typeof value === 'string' ? value.trim() : value,
  236. );
  237. if (
  238. normalizedSkills.every(
  239. (value) =>
  240. typeof value === 'string' &&
  241. (SKILL_IDS.has(value) || RETIRED_TOLERANT_SKILL_IDS.has(value)) &&
  242. !OWNED_SKILL_IDS.has(value),
  243. )
  244. ) {
  245. result.skills = normalizedSkills.filter(
  246. (value): value is AgentBuiltinSkillId => typeof value === 'string' && SKILL_IDS.has(value),
  247. );
  248. }
  249. }
  250. const features = asRecord(obj.features);
  251. if (features) {
  252. const parsedFeatures: NonNullable<AgentCapabilityConfig['features']> = {};
  253. for (const key of FEATURE_KEYS as Set<keyof typeof parsedFeatures>) {
  254. if (typeof features[key] === 'boolean') parsedFeatures[key] = features[key];
  255. }
  256. if (Object.keys(parsedFeatures).length > 0) result.features = parsedFeatures;
  257. }
  258. return result;
  259. }
  260. function asRecord(value: unknown): Record<string, unknown> | undefined {
  261. return typeof value === 'object' && value !== null && !Array.isArray(value)
  262. ? (value as Record<string, unknown>)
  263. : undefined;
  264. }
  265. function isKnownIdArray(
  266. value: unknown,
  267. knownIds: ReadonlySet<string>,
  268. ): value is AgentBuiltinToolId[] | AgentBuiltinMcpToolId[] {
  269. return (
  270. Array.isArray(value) && value.every((item) => typeof item === 'string' && knownIds.has(item))
  271. );
  272. }
  273. export function resolveAgentCapabilities(
  274. defaults: AgentCapabilityConfig = {},
  275. override: AgentCapabilityConfig = {},
  276. ): ResolvedAgentCapabilities {
  277. return {
  278. persona: {
  279. enabled: override.persona?.enabled ?? defaults.persona?.enabled ?? true,
  280. },
  281. tools: cloneOptionalArray(override.tools ?? defaults.tools),
  282. builtinTools: cloneOptionalArray(override.builtinTools ?? defaults.builtinTools),
  283. skills: cloneOptionalArray(override.skills ?? defaults.skills),
  284. features: {
  285. mavis: override.features?.mavis ?? defaults.features?.mavis ?? true,
  286. delegation: override.features?.delegation ?? defaults.features?.delegation ?? true,
  287. webSearch: override.features?.webSearch ?? defaults.features?.webSearch ?? true,
  288. },
  289. };
  290. }
  291. export function isAgentBuiltinToolEnabled(
  292. capabilities: ResolvedAgentCapabilities,
  293. toolName: AgentBuiltinToolId,
  294. ): boolean {
  295. return capabilities.tools === undefined || capabilities.tools.includes(toolName);
  296. }
  297. export function isAgentBuiltinMcpToolEnabled(
  298. capabilities: ResolvedAgentCapabilities,
  299. toolName: AgentBuiltinMcpToolId,
  300. ): boolean {
  301. return capabilities.builtinTools === undefined || capabilities.builtinTools.includes(toolName);
  302. }
  303. export function isAgentBuiltinSkillEnabled(
  304. capabilities: ResolvedAgentCapabilities,
  305. skillName: AgentBuiltinSkillId,
  306. ): boolean {
  307. return capabilities.skills === undefined || capabilities.skills.includes(skillName);
  308. }
  309. function cloneOptionalArray<T>(value: readonly T[] | undefined): T[] | undefined {
  310. return value === undefined ? undefined : [...value];
  311. }
  312. function readObject(value: unknown, path: string): Record<string, unknown> {
  313. if (value == null || typeof value !== 'object' || Array.isArray(value)) {
  314. invalid(path, 'must be an object');
  315. }
  316. return value as Record<string, unknown>;
  317. }
  318. function rejectUnknownKeys(
  319. value: Record<string, unknown>,
  320. allowed: ReadonlySet<string>,
  321. path: string,
  322. ): void {
  323. for (const key of Object.keys(value)) {
  324. if (!allowed.has(key)) invalid(`${path}.${key}`, 'is not supported');
  325. }
  326. }
  327. function invalid(path: string, message: string): never {
  328. throw new Error(`Invalid configuration at ${path}: ${message}`);
  329. }