subagent-roles.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. export type CanonicalSubagentRole = 'explore' | 'worker' | 'verifier';
  2. export interface LocalSubagentRoleDefinition {
  3. whenToUse: string;
  4. }
  5. /** Single source of truth for canonical desktop SubAgent role names. */
  6. export const SUBAGENT_ROLES = {
  7. explore: {
  8. whenToUse:
  9. 'Read-only mapping for unfamiliar, cross-file, or evidence-heavy questions; it can use Bash for read-only Git and code investigation, but cannot create or edit files.',
  10. },
  11. worker: {
  12. whenToUse:
  13. 'Bounded production work with explicit scope, ownership, deliverable, and acceptance.',
  14. },
  15. verifier: {
  16. whenToUse:
  17. 'Independently validate an existing deliverable and report findings; no project-file changes. Temporary validation artifacts require an explicitly designated temporary location.',
  18. },
  19. } as const satisfies Record<CanonicalSubagentRole, LocalSubagentRoleDefinition>;
  20. export const CANONICAL_SUBAGENT_ROLES = Object.keys(SUBAGENT_ROLES) as CanonicalSubagentRole[];
  21. /** Model-visible built-in targets. */
  22. const TASK_AGENT_TARGETS = ['mavis', ...CANONICAL_SUBAGENT_ROLES] as const;
  23. const TASK_AGENT_TARGET_DESCRIPTIONS: Readonly<
  24. Record<(typeof TASK_AGENT_TARGETS)[number], string>
  25. > = {
  26. mavis: 'Broad or mixed-scope work that does not fit a specialist role.',
  27. explore: SUBAGENT_ROLES.explore.whenToUse,
  28. worker: SUBAGENT_ROLES.worker.whenToUse,
  29. verifier: SUBAGENT_ROLES.verifier.whenToUse,
  30. };
  31. /** Names whose bare form is interpreted as a canonical role or primary alias. */
  32. export const RESERVED_SUBAGENT_NAMES = [
  33. ...CANONICAL_SUBAGENT_ROLES,
  34. 'main',
  35. 'mavis',
  36. ] as readonly string[];
  37. const RESERVED_SUBAGENT_NAME_SET = new Set(RESERVED_SUBAGENT_NAMES);
  38. /**
  39. * The desktop service currently returns the thrift enum value for built-ins,
  40. * while a few adapters expose the already-decoded string. Everything else is
  41. * intentionally treated as non-builtin so a manual/unknown row cannot be
  42. * selected through a role or primary alias by accident.
  43. */
  44. export function isTrustedBuiltinCreationSource(value: unknown): boolean {
  45. return value === 3 || value === 'builtin';
  46. }
  47. export function toAgentRequestRef(
  48. agent: { name?: unknown; creationSource?: unknown } | null | undefined,
  49. ): string | undefined {
  50. if (typeof agent?.name !== 'string') return undefined;
  51. const name = agent.name;
  52. if (name.toLowerCase().startsWith('agent:')) return name;
  53. if (
  54. RESERVED_SUBAGENT_NAME_SET.has(name.toLowerCase()) &&
  55. !isTrustedBuiltinCreationSource(agent.creationSource)
  56. ) {
  57. return `agent:${name}`;
  58. }
  59. return name;
  60. }
  61. export const AGENT_REQUEST_REF_DESCRIPTION =
  62. 'Use the `requestRef` returned by the native `mavis` tool with command "agent list". For built-in work use mavis, explore, worker, or verifier. Use `agent:<stable-name>` to select the exact manual/custom Agent when its name collides with a reserved role or primary alias; ordinary custom names use their raw stable name.';
  63. export const LOCAL_MAVIS_AGENT_NAME_DESCRIPTION = `${AGENT_REQUEST_REF_DESCRIPTION} "me" selects the current Agent.`;
  64. const TASK_AGENT_REQUEST_REF_DESCRIPTION =
  65. 'Built-in name or stable custom `requestRef`. Use `agent:<stable-name>` for a custom Agent whose name collides with a reserved role or primary alias; ordinary custom names use their raw stable name. Use the native `mavis` tool with command "agent list" for discovery only when needed and available.';
  66. const WITHOUT_MAVIS_AGENT_REQUEST_REF_DESCRIPTION =
  67. 'Use explore, worker, or verifier for built-in work. For a known custom Agent, use its stable `requestRef`. Use `agent:<stable-name>` to select the exact manual/custom Agent when its name collides with a reserved role or primary alias; ordinary custom names use their raw stable name.';
  68. export function isCanonicalSubagentRole(value: string): value is CanonicalSubagentRole {
  69. return Object.hasOwn(SUBAGENT_ROLES, value);
  70. }
  71. export function resolveCanonicalSubagentRole(
  72. requestedName: string,
  73. ): CanonicalSubagentRole | undefined {
  74. const normalized = requestedName.trim().toLowerCase();
  75. return isCanonicalSubagentRole(normalized) ? normalized : undefined;
  76. }
  77. export function roleDirectoryText(): string {
  78. return TASK_AGENT_TARGETS.map(
  79. (target) => `- ${target} — ${TASK_AGENT_TARGET_DESCRIPTIONS[target]}`,
  80. ).join('\n');
  81. }
  82. /**
  83. * Returns model-visible Task target guidance for the currently exposed
  84. * capability surface. Resolver compatibility stays unchanged.
  85. */
  86. export function agentNameDescription(options: { includeMavis?: boolean } = {}): string {
  87. return options.includeMavis === false
  88. ? WITHOUT_MAVIS_AGENT_REQUEST_REF_DESCRIPTION
  89. : TASK_AGENT_REQUEST_REF_DESCRIPTION;
  90. }