|
| 1 | +import type { |
| 2 | + ChatAttachment, |
| 3 | + OrchestrationClientCapabilities, |
| 4 | + OrchestrationEvent, |
| 5 | + OrchestrationMessage, |
| 6 | + OrchestrationReadModel, |
| 7 | + OrchestrationThread, |
| 8 | + OrchestrationThreadDetailSnapshot, |
| 9 | + OrchestrationThreadStreamItem, |
| 10 | + ThreadMessageSentPayload, |
| 11 | +} from "@t3tools/contracts"; |
| 12 | + |
| 13 | +const LEGACY_VOICE_NOTE_PLACEHOLDER = "[Voice note]"; |
| 14 | + |
| 15 | +function supportsAudioAttachments( |
| 16 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 17 | +): boolean { |
| 18 | + return capabilities?.audioAttachments === true; |
| 19 | +} |
| 20 | + |
| 21 | +function legacyVoiceNoteText(attachment: Extract<ChatAttachment, { type: "audio" }>): string { |
| 22 | + const transcript = attachment.transcript?.trim(); |
| 23 | + return transcript ? `[Voice note transcript]\n${transcript}` : LEGACY_VOICE_NOTE_PLACEHOLDER; |
| 24 | +} |
| 25 | + |
| 26 | +function projectMessageFields(input: { |
| 27 | + readonly text: string; |
| 28 | + readonly attachments?: ReadonlyArray<ChatAttachment> | undefined; |
| 29 | +}): { |
| 30 | + readonly changed: boolean; |
| 31 | + readonly text: string; |
| 32 | + readonly attachments?: ReadonlyArray<ChatAttachment>; |
| 33 | +} { |
| 34 | + const attachments = input.attachments ?? []; |
| 35 | + const audioAttachments = attachments.filter( |
| 36 | + (attachment): attachment is Extract<ChatAttachment, { type: "audio" }> => |
| 37 | + attachment.type === "audio", |
| 38 | + ); |
| 39 | + if (audioAttachments.length === 0) { |
| 40 | + return { |
| 41 | + changed: false, |
| 42 | + text: input.text, |
| 43 | + ...(input.attachments ? { attachments: input.attachments } : {}), |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + const supportedAttachments = attachments.filter((attachment) => attachment.type !== "audio"); |
| 48 | + const voiceNoteText = audioAttachments.map(legacyVoiceNoteText).join("\n\n"); |
| 49 | + const text = input.text.trim().length > 0 ? `${input.text}\n\n${voiceNoteText}` : voiceNoteText; |
| 50 | + return { |
| 51 | + changed: true, |
| 52 | + text, |
| 53 | + ...(supportedAttachments.length > 0 ? { attachments: supportedAttachments } : {}), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +export function projectOrchestrationMessageForClient( |
| 58 | + message: OrchestrationMessage, |
| 59 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 60 | +): OrchestrationMessage { |
| 61 | + if (supportsAudioAttachments(capabilities)) { |
| 62 | + return message; |
| 63 | + } |
| 64 | + const projected = projectMessageFields(message); |
| 65 | + if (!projected.changed) { |
| 66 | + return message; |
| 67 | + } |
| 68 | + const { attachments: _attachments, ...messageWithoutAttachments } = message; |
| 69 | + return { |
| 70 | + ...messageWithoutAttachments, |
| 71 | + text: projected.text, |
| 72 | + ...(projected.attachments ? { attachments: [...projected.attachments] } : {}), |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +function projectThreadMessageEventForClient( |
| 77 | + payload: ThreadMessageSentPayload, |
| 78 | +): ThreadMessageSentPayload { |
| 79 | + const projected = projectMessageFields(payload); |
| 80 | + if (!projected.changed) { |
| 81 | + return payload; |
| 82 | + } |
| 83 | + const { attachments: _attachments, ...payloadWithoutAttachments } = payload; |
| 84 | + return { |
| 85 | + ...payloadWithoutAttachments, |
| 86 | + text: projected.text, |
| 87 | + ...(projected.attachments ? { attachments: [...projected.attachments] } : {}), |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +export function projectOrchestrationThreadForClient( |
| 92 | + thread: OrchestrationThread, |
| 93 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 94 | +): OrchestrationThread { |
| 95 | + if (supportsAudioAttachments(capabilities)) { |
| 96 | + return thread; |
| 97 | + } |
| 98 | + return { |
| 99 | + ...thread, |
| 100 | + messages: thread.messages.map((message) => |
| 101 | + projectOrchestrationMessageForClient(message, capabilities), |
| 102 | + ), |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +export function projectOrchestrationThreadSnapshotForClient( |
| 107 | + snapshot: OrchestrationThreadDetailSnapshot, |
| 108 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 109 | +): OrchestrationThreadDetailSnapshot { |
| 110 | + if (supportsAudioAttachments(capabilities)) { |
| 111 | + return snapshot; |
| 112 | + } |
| 113 | + return { |
| 114 | + ...snapshot, |
| 115 | + thread: projectOrchestrationThreadForClient(snapshot.thread, capabilities), |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +export function projectOrchestrationReadModelForClient( |
| 120 | + snapshot: OrchestrationReadModel, |
| 121 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 122 | +): OrchestrationReadModel { |
| 123 | + if (supportsAudioAttachments(capabilities)) { |
| 124 | + return snapshot; |
| 125 | + } |
| 126 | + return { |
| 127 | + ...snapshot, |
| 128 | + threads: snapshot.threads.map((thread) => |
| 129 | + projectOrchestrationThreadForClient(thread, capabilities), |
| 130 | + ), |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +export function projectOrchestrationEventForClient( |
| 135 | + event: OrchestrationEvent, |
| 136 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 137 | +): OrchestrationEvent { |
| 138 | + if (supportsAudioAttachments(capabilities) || event.type !== "thread.message-sent") { |
| 139 | + return event; |
| 140 | + } |
| 141 | + return { |
| 142 | + ...event, |
| 143 | + payload: projectThreadMessageEventForClient(event.payload), |
| 144 | + }; |
| 145 | +} |
| 146 | + |
| 147 | +export function projectOrchestrationThreadStreamItemForClient( |
| 148 | + item: OrchestrationThreadStreamItem, |
| 149 | + capabilities: OrchestrationClientCapabilities | undefined, |
| 150 | +): OrchestrationThreadStreamItem { |
| 151 | + switch (item.kind) { |
| 152 | + case "synchronized": |
| 153 | + return item; |
| 154 | + case "snapshot": |
| 155 | + return { |
| 156 | + ...item, |
| 157 | + snapshot: projectOrchestrationThreadSnapshotForClient(item.snapshot, capabilities), |
| 158 | + }; |
| 159 | + case "event": |
| 160 | + return { |
| 161 | + ...item, |
| 162 | + event: projectOrchestrationEventForClient(item.event, capabilities), |
| 163 | + }; |
| 164 | + default: |
| 165 | + item satisfies never; |
| 166 | + return item; |
| 167 | + } |
| 168 | +} |
0 commit comments