ids.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * Branded types for session and agent IDs.
  3. * These prevent accidentally mixing up session IDs and agent IDs at compile time.
  4. */
  5. /**
  6. * A session ID uniquely identifies a Claude Code session.
  7. * Returned by getSessionId().
  8. */
  9. export type SessionId = string & { readonly __brand: 'SessionId' }
  10. /**
  11. * An agent ID uniquely identifies a subagent within a session.
  12. * Returned by createAgentId().
  13. * When present, indicates the context is a subagent (not the main session).
  14. */
  15. export type AgentId = string & { readonly __brand: 'AgentId' }
  16. /**
  17. * Cast a raw string to SessionId.
  18. * Use sparingly - prefer getSessionId() when possible.
  19. */
  20. export function asSessionId(id: string): SessionId {
  21. return id as SessionId
  22. }
  23. /**
  24. * Cast a raw string to AgentId.
  25. * Use sparingly - prefer createAgentId() when possible.
  26. */
  27. export function asAgentId(id: string): AgentId {
  28. return id as AgentId
  29. }
  30. const AGENT_ID_PATTERN = /^a(?:.+-)?[0-9a-f]{16}$/
  31. /**
  32. * Validate and brand a string as AgentId.
  33. * Matches the format produced by createAgentId(): `a` + optional `<label>-` + 16 hex chars.
  34. * Returns null if the string doesn't match (e.g. teammate names, team-addressing).
  35. */
  36. export function toAgentId(s: string): AgentId | null {
  37. return AGENT_ID_PATTERN.test(s) ? (s as AgentId) : null
  38. }