( filePath: string, baseDir: string, frontmatter: Record<string, unknown>, content: string, source: SettingSource, )
| 539 | * Parses agent definition from markdown file data |
| 540 | */ |
| 541 | export function parseAgentFromMarkdown( |
| 542 | filePath: string, |
| 543 | baseDir: string, |
| 544 | frontmatter: Record<string, unknown>, |
| 545 | content: string, |
| 546 | source: SettingSource, |
| 547 | ): CustomAgentDefinition | null { |
| 548 | try { |
| 549 | const agentType = frontmatter['name'] |
| 550 | let whenToUse = frontmatter['description'] as string |
| 551 | |
| 552 | // Validate required fields — silently skip files without any agent |
| 553 | // frontmatter (they're likely co-located reference documentation) |
| 554 | if (!agentType || typeof agentType !== 'string') { |
| 555 | return null |
| 556 | } |
| 557 | if (!whenToUse || typeof whenToUse !== 'string') { |
| 558 | logForDebugging( |
| 559 | `Agent file ${filePath} is missing required 'description' in frontmatter`, |
| 560 | ) |
| 561 | return null |
| 562 | } |
| 563 | |
| 564 | // Unescape newlines in whenToUse that were escaped for YAML parsing |
| 565 | whenToUse = whenToUse.replace(/\\n/g, '\n') |
| 566 | |
| 567 | const color = frontmatter['color'] as AgentColorName | undefined |
| 568 | const modelRaw = frontmatter['model'] |
| 569 | let model: string | undefined |
| 570 | if (typeof modelRaw === 'string' && modelRaw.trim().length > 0) { |
| 571 | const trimmed = modelRaw.trim() |
| 572 | model = trimmed.toLowerCase() === 'inherit' ? 'inherit' : trimmed |
| 573 | } |
| 574 | |
| 575 | // Parse background flag |
| 576 | const backgroundRaw = frontmatter['background'] |
| 577 | |
| 578 | if ( |
| 579 | backgroundRaw !== undefined && |
| 580 | backgroundRaw !== 'true' && |
| 581 | backgroundRaw !== 'false' && |
| 582 | backgroundRaw !== true && |
| 583 | backgroundRaw !== false |
| 584 | ) { |
| 585 | logForDebugging( |
| 586 | `Agent file ${filePath} has invalid background value '${backgroundRaw}'. Must be 'true', 'false', or omitted.`, |
| 587 | ) |
| 588 | } |
| 589 | |
| 590 | const background = |
| 591 | backgroundRaw === 'true' || backgroundRaw === true ? true : undefined |
| 592 | |
| 593 | // Parse memory scope |
| 594 | const VALID_MEMORY_SCOPES: AgentMemoryScope[] = ['user', 'project', 'local'] |
| 595 | const memoryRaw = frontmatter['memory'] as string | undefined |
| 596 | let memory: AgentMemoryScope | undefined |
| 597 | if (memoryRaw !== undefined) { |
| 598 | if (VALID_MEMORY_SCOPES.includes(memoryRaw as AgentMemoryScope)) { |
no test coverage detected