* Extract a mapping from operation id → tool id by scanning switch/case/return * patterns in a block file. Handles both simple returns and ternary returns * (for ternaries, takes the last quoted tool-like string, which is typically * the default/list variant). Also picks up named helper functions
(fileContent: string)
| 480 | * from tools.config.tool (e.g. selectGmailToolId). |
| 481 | */ |
| 482 | function extractSwitchCaseToolMapping(fileContent: string): Map<string, string> { |
| 483 | const mapping = new Map<string, string>() |
| 484 | const caseRegex = /\bcase\s+['"]([^'"]+)['"]\s*:/g |
| 485 | let caseMatch: RegExpExecArray | null |
| 486 | |
| 487 | while ((caseMatch = caseRegex.exec(fileContent)) !== null) { |
| 488 | const opId = caseMatch[1] |
| 489 | if (mapping.has(opId)) continue |
| 490 | |
| 491 | const searchStart = caseMatch.index + caseMatch[0].length |
| 492 | const searchEnd = Math.min(searchStart + 300, fileContent.length) |
| 493 | const segment = fileContent.substring(searchStart, searchEnd) |
| 494 | |
| 495 | const returnIdx = segment.search(/\breturn\b/) |
| 496 | if (returnIdx === -1) continue |
| 497 | |
| 498 | const afterReturn = segment.substring(returnIdx + 'return'.length) |
| 499 | // Limit scope to before the next case/default to avoid capturing sibling cases |
| 500 | const nextCaseIdx = afterReturn.search(/\bcase\b|\bdefault\b/) |
| 501 | const returnScope = nextCaseIdx > 0 ? afterReturn.substring(0, nextCaseIdx) : afterReturn |
| 502 | |
| 503 | const toolMatches = [...returnScope.matchAll(/['"]([a-z][a-z0-9_]+)['"]/g)] |
| 504 | // Take the last tool-like string (underscore = tool ID pattern); for ternaries this |
| 505 | // is the fallback/list variant |
| 506 | const toolId = toolMatches |
| 507 | .map((m) => m[1]) |
| 508 | .filter((id) => id.includes('_')) |
| 509 | .pop() |
| 510 | if (toolId) { |
| 511 | mapping.set(opId, toolId) |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return mapping |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Scan all tool files under apps/sim/tools/ and build a map from tool ID to description. |
no test coverage detected