directMemberMessage.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import type { AppState } from '../state/AppState.js'
  2. /**
  3. * Parse `@agent-name message` syntax for direct team member messaging.
  4. */
  5. export function parseDirectMemberMessage(input: string): {
  6. recipientName: string
  7. message: string
  8. } | null {
  9. const match = input.match(/^@([\w-]+)\s+(.+)$/s)
  10. if (!match) return null
  11. const [, recipientName, message] = match
  12. if (!recipientName || !message) return null
  13. const trimmedMessage = message.trim()
  14. if (!trimmedMessage) return null
  15. return { recipientName, message: trimmedMessage }
  16. }
  17. export type DirectMessageResult =
  18. | { success: true; recipientName: string }
  19. | {
  20. success: false
  21. error: 'no_team_context' | 'unknown_recipient'
  22. recipientName?: string
  23. }
  24. type WriteToMailboxFn = (
  25. recipientName: string,
  26. message: { from: string; text: string; timestamp: string },
  27. teamName: string,
  28. ) => Promise<void>
  29. /**
  30. * Send a direct message to a team member, bypassing the model.
  31. */
  32. export async function sendDirectMemberMessage(
  33. recipientName: string,
  34. message: string,
  35. teamContext: AppState['teamContext'],
  36. writeToMailbox?: WriteToMailboxFn,
  37. ): Promise<DirectMessageResult> {
  38. if (!teamContext || !writeToMailbox) {
  39. return { success: false, error: 'no_team_context' }
  40. }
  41. // Find team member by name
  42. const member = Object.values(teamContext.teammates ?? {}).find(
  43. t => t.name === recipientName,
  44. )
  45. if (!member) {
  46. return { success: false, error: 'unknown_recipient', recipientName }
  47. }
  48. await writeToMailbox(
  49. recipientName,
  50. {
  51. from: 'user',
  52. text: message,
  53. timestamp: new Date().toISOString(),
  54. },
  55. teamContext.teamName,
  56. )
  57. return { success: true, recipientName }
  58. }