queued-message-source.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * queued-message-source — aggregate the routing source of a drained
  3. * batch of queued user messages into a single "merged" source that
  4. * downstream dispatch / reply routing can rely on.
  5. *
  6. * Why this lives here (shared): local-runtime compatibility imports and
  7. * cloud-runtime both drain the same kind of queued user input, and the new
  8. * injection design (see
  9. * `queued-message-injection-redesign.html`) demands one identical rule
  10. * for "what source does the MERGED user message carry?":
  11. *
  12. * 1. If every drained item is `'api'` (Web) → merged source is `'api'`.
  13. * 2. If ANY item is from an IM channel (`'channel:*'`) → merged
  14. * source is IM. When multiple IM items are present, take the
  15. * **last** IM in FIFO order — that is the most recent IM
  16. * participant and the one most likely to be waiting for a reply,
  17. * so we route the merged user message (and any reply receipt)
  18. * back to that channel.
  19. * 3. Empty input falls back to a caller-supplied default (`'api'` by
  20. * default) — empty drain should not actually call this, but the
  21. * defensive default avoids `undefined` propagating into
  22. * `MessageRequest.source`.
  23. *
  24. * The function is intentionally pure / structural — it returns a fresh
  25. * `{ source, origin?, inboundContext? }` triple. Callers decide what to
  26. * do with it (local-runtime fills `MessageRequest`; cloud-runtime uses it
  27. * for log attribution).
  28. */
  29. /** Minimum shape we need from a queued item to compute the merged
  30. * source. Both daemon `SessionQueueItem` and the
  31. * `DrainedQueueItem` shape returned over HTTP satisfy this. */
  32. export interface QueuedItemSourceLike {
  33. source?: string;
  34. origin?: unknown;
  35. inboundContext?: unknown;
  36. }
  37. /** Aggregated routing source for a merged user message. `origin` /
  38. * `inboundContext` are only populated when the winning item is an IM
  39. * channel item that carried them. */
  40. export interface AggregatedQueuedSource {
  41. source: string;
  42. origin?: unknown;
  43. inboundContext?: unknown;
  44. }
  45. /** Channel-source predicate — anything starting with `channel:` is
  46. * treated as IM (matches daemon's `isChannelSource` and the
  47. * `MessageSource` union in `packages/daemon/src/common/types.ts`). */
  48. export function isChannelSource(source: string | undefined): boolean {
  49. return typeof source === 'string' && source.startsWith('channel:');
  50. }
  51. /**
  52. * Compute the merged source for a drained batch.
  53. *
  54. * Rules (see file header for full rationale):
  55. * - All API / no IM → returns `{ source: fallbackSource }`.
  56. * - Any IM present → returns the LAST IM item's
  57. * `{ source, origin, inboundContext }` in FIFO order.
  58. * - Empty input → returns `{ source: fallbackSource }`.
  59. *
  60. * Pure: never throws, never mutates input. Items missing a `source`
  61. * are treated as non-IM (they cannot be the IM winner).
  62. */
  63. export function aggregateQueuedSource(
  64. items: ReadonlyArray<QueuedItemSourceLike>,
  65. fallbackSource = 'api',
  66. ): AggregatedQueuedSource {
  67. let lastIm: QueuedItemSourceLike | undefined;
  68. for (const item of items) {
  69. if (isChannelSource(item.source)) {
  70. lastIm = item;
  71. }
  72. }
  73. if (lastIm && typeof lastIm.source === 'string') {
  74. const aggregated: AggregatedQueuedSource = { source: lastIm.source };
  75. if (lastIm.origin !== undefined) aggregated.origin = lastIm.origin;
  76. if (lastIm.inboundContext !== undefined) aggregated.inboundContext = lastIm.inboundContext;
  77. return aggregated;
  78. }
  79. return { source: fallbackSource };
  80. }