Files
claude-code/src/utils/agentId.ts
claude-code-best d0915fc880 chore: 清理 src 下 33 项死代码和类型断言
删除未使用的文件/目录(mcp/adapter、cli/update.ts 等)、
未使用的重导出文件(design-system/color.ts 等 12 个)、
7 个零引用的导出函数、修复 5 处 as any 为精确类型。
净减少 ~1194 行代码,precheck 4077 测试全部通过。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-05 16:07:30 +08:00

69 lines
2.0 KiB
TypeScript

/**
* Deterministic Agent ID System
*
* This module provides helper functions for formatting and parsing deterministic
* agent IDs used in the swarm/teammate system.
*
* ## ID Formats
*
* **Agent IDs**: `agentName@teamName`
* - Example: `team-lead@my-project`, `researcher@my-project`
* - The @ symbol acts as a separator between agent name and team name
*
* **Request IDs**: `{requestType}-{timestamp}@{agentId}`
* - Example: `shutdown-1702500000000@researcher@my-project`
* - Used for shutdown requests, plan approvals, etc.
*
* ## Why Deterministic IDs?
*
* Deterministic IDs provide several benefits:
*
* 1. **Reproducibility**: The same agent spawned with the same name in the same team
* always gets the same ID, enabling reconnection after crashes/restarts.
*
* 2. **Human-readable**: IDs are meaningful and debuggable (e.g., `tester@my-project`).
*
* 3. **Predictable**: Team leads can compute a teammate's ID without looking it up,
* simplifying message routing and task assignment.
*
* ## Constraints
*
* - Agent names must NOT contain `@` (it's used as the separator)
* - Use `sanitizeAgentName()` from TeammateTool.ts to strip @ from names
*/
/**
* Formats an agent ID in the format `agentName@teamName`.
*/
export function formatAgentId(agentName: string, teamName: string): string {
return `${agentName}@${teamName}`
}
/**
* Parses an agent ID into its components.
* Returns null if the ID doesn't contain the @ separator.
*/
export function parseAgentId(
agentId: string,
): { agentName: string; teamName: string } | null {
const atIndex = agentId.indexOf('@')
if (atIndex === -1) {
return null
}
return {
agentName: agentId.slice(0, atIndex),
teamName: agentId.slice(atIndex + 1),
}
}
/**
* Formats a request ID in the format `{requestType}-{timestamp}@{agentId}`.
*/
export function generateRequestId(
requestType: string,
agentId: string,
): string {
const timestamp = Date.now()
return `${requestType}-${timestamp}@${agentId}`
}