|
| 1 | +import { commands as fsSyncCommands } from "@hypr/plugin-fs-sync"; |
| 2 | +import type { SessionContentData } from "@hypr/plugin-fs-sync"; |
| 3 | +import type { SessionContext, Transcript } from "@hypr/plugin-template"; |
| 4 | +import type { SpeakerHintStorage } from "@hypr/store"; |
| 5 | +import { isValidTiptapContent, json2md } from "@hypr/tiptap/shared"; |
| 6 | + |
| 7 | +import type * as main from "../store/tinybase/store/main"; |
| 8 | +import { buildSegments, SegmentKey, type WordLike } from "../utils/segment"; |
| 9 | +import { |
| 10 | + defaultRenderLabelContext, |
| 11 | + SpeakerLabelManager, |
| 12 | +} from "../utils/segment/shared"; |
| 13 | +import { convertStorageHintsToRuntime } from "../utils/speaker-hints"; |
| 14 | + |
| 15 | +function toMarkdownFromTiptap(value: unknown): string | null { |
| 16 | + if (!value || typeof value !== "object") { |
| 17 | + return null; |
| 18 | + } |
| 19 | + if (!isValidTiptapContent(value)) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + const md = json2md(value); |
| 24 | + const trimmed = md.trim(); |
| 25 | + return trimmed ? trimmed : null; |
| 26 | +} |
| 27 | + |
| 28 | +function extractEventName(event: unknown): string | null { |
| 29 | + if (!event || typeof event !== "object") { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + const record = event as Record<string, unknown>; |
| 34 | + if (typeof record.name === "string" && record.name) { |
| 35 | + return record.name; |
| 36 | + } |
| 37 | + if (typeof record.title === "string" && record.title) { |
| 38 | + return record.title; |
| 39 | + } |
| 40 | + |
| 41 | + return null; |
| 42 | +} |
| 43 | + |
| 44 | +function buildTranscript( |
| 45 | + transcriptData: SessionContentData["transcript"], |
| 46 | + store: ReturnType<typeof main.UI.useStore>, |
| 47 | +): Transcript | null { |
| 48 | + if (!transcriptData || transcriptData.transcripts.length === 0) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + const indexedWords = transcriptData.transcripts |
| 53 | + .flatMap((transcript) => |
| 54 | + transcript.words.map((word) => ({ |
| 55 | + id: word.id ?? null, |
| 56 | + text: word.text, |
| 57 | + start_ms: word.startMs, |
| 58 | + end_ms: word.endMs, |
| 59 | + channel: word.channel as WordLike["channel"], |
| 60 | + })), |
| 61 | + ) |
| 62 | + .sort((a, b) => a.start_ms - b.start_ms); |
| 63 | + |
| 64 | + const words: WordLike[] = indexedWords.map((word) => ({ |
| 65 | + text: word.text, |
| 66 | + start_ms: word.start_ms, |
| 67 | + end_ms: word.end_ms, |
| 68 | + channel: word.channel, |
| 69 | + })); |
| 70 | + |
| 71 | + if (words.length === 0) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const wordIdToIndex = new Map<string, number>(); |
| 76 | + indexedWords.forEach((word, index) => { |
| 77 | + if (typeof word.id === "string" && word.id) { |
| 78 | + wordIdToIndex.set(word.id, index); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + const storageHints: SpeakerHintStorage[] = transcriptData.transcripts.flatMap( |
| 83 | + (transcript) => |
| 84 | + transcript.speakerHints.flatMap((hint) => { |
| 85 | + const start = wordIdToIndex.get(hint.startWordId); |
| 86 | + const end = wordIdToIndex.get(hint.endWordId); |
| 87 | + if (typeof start !== "number" || typeof end !== "number") { |
| 88 | + return []; |
| 89 | + } |
| 90 | + |
| 91 | + const from = Math.min(start, end); |
| 92 | + const to = Math.max(start, end); |
| 93 | + |
| 94 | + const speakerId = hint.speakerId; |
| 95 | + const speakerIndex = |
| 96 | + typeof speakerId === "string" |
| 97 | + ? Number.parseInt(speakerId.replace(/[^\d-]/g, ""), 10) |
| 98 | + : Number.NaN; |
| 99 | + |
| 100 | + const isHumanAssignment = |
| 101 | + !!store && |
| 102 | + typeof speakerId === "string" && |
| 103 | + Boolean(store.getRow("humans", speakerId)); |
| 104 | + |
| 105 | + const type = isHumanAssignment |
| 106 | + ? "user_speaker_assignment" |
| 107 | + : "provider_speaker_index"; |
| 108 | + const value = JSON.stringify( |
| 109 | + isHumanAssignment |
| 110 | + ? { human_id: speakerId } |
| 111 | + : { |
| 112 | + speaker_index: Number.isFinite(speakerIndex) ? speakerIndex : 0, |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + return indexedWords.slice(from, to + 1).flatMap((word) => { |
| 117 | + if (typeof word.id !== "string" || !word.id) { |
| 118 | + return []; |
| 119 | + } |
| 120 | + return [ |
| 121 | + { |
| 122 | + word_id: word.id, |
| 123 | + type, |
| 124 | + value, |
| 125 | + }, |
| 126 | + ]; |
| 127 | + }); |
| 128 | + }), |
| 129 | + ); |
| 130 | + |
| 131 | + const runtimeHints = convertStorageHintsToRuntime( |
| 132 | + storageHints, |
| 133 | + wordIdToIndex, |
| 134 | + ); |
| 135 | + |
| 136 | + const segments = buildSegments(words, [], runtimeHints); |
| 137 | + const ctx = store ? defaultRenderLabelContext(store) : undefined; |
| 138 | + const manager = SpeakerLabelManager.fromSegments(segments, ctx); |
| 139 | + |
| 140 | + const startedAtCandidates = transcriptData.transcripts |
| 141 | + .map((t) => t.startedAt) |
| 142 | + .filter((v): v is number => typeof v === "number"); |
| 143 | + const endedAtCandidates = transcriptData.transcripts |
| 144 | + .map((t) => t.endedAt) |
| 145 | + .filter((v): v is number => typeof v === "number"); |
| 146 | + |
| 147 | + return { |
| 148 | + segments: segments.map((segment) => ({ |
| 149 | + speaker: SegmentKey.renderLabel(segment.key, ctx, manager), |
| 150 | + text: segment.words.map((word) => word.text).join(" "), |
| 151 | + })), |
| 152 | + startedAt: |
| 153 | + startedAtCandidates.length > 0 ? Math.min(...startedAtCandidates) : null, |
| 154 | + endedAt: |
| 155 | + endedAtCandidates.length > 0 ? Math.max(...endedAtCandidates) : null, |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +export async function hydrateSessionContextFromFs( |
| 160 | + store: ReturnType<typeof main.UI.useStore>, |
| 161 | + sessionId: string, |
| 162 | +): Promise<SessionContext | null> { |
| 163 | + const result = await fsSyncCommands.loadSessionContent(sessionId); |
| 164 | + if (result.status === "error") { |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + const payload = result.data; |
| 169 | + const participants = |
| 170 | + payload.meta?.participants |
| 171 | + .map((participant) => { |
| 172 | + const row = store?.getRow("humans", participant.humanId); |
| 173 | + if (!row || typeof row.name !== "string" || !row.name) { |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + return { |
| 178 | + name: row.name, |
| 179 | + jobTitle: |
| 180 | + typeof row.job_title === "string" && row.job_title |
| 181 | + ? row.job_title |
| 182 | + : null, |
| 183 | + }; |
| 184 | + }) |
| 185 | + .filter( |
| 186 | + ( |
| 187 | + participant, |
| 188 | + ): participant is { name: string; jobTitle: string | null } => |
| 189 | + Boolean(participant), |
| 190 | + ) ?? []; |
| 191 | + |
| 192 | + const enhancedContent = payload.notes |
| 193 | + .slice() |
| 194 | + .sort((a, b) => (a.position ?? 0) - (b.position ?? 0)) |
| 195 | + .map((note) => toMarkdownFromTiptap(note.tiptapJson)) |
| 196 | + .filter((note): note is string => Boolean(note)) |
| 197 | + .join("\n\n---\n\n"); |
| 198 | + |
| 199 | + const transcript = buildTranscript(payload.transcript, store); |
| 200 | + const eventName = extractEventName(payload.meta?.event); |
| 201 | + |
| 202 | + return { |
| 203 | + title: payload.meta?.title ?? null, |
| 204 | + date: payload.meta?.createdAt ?? null, |
| 205 | + rawContent: toMarkdownFromTiptap(payload.rawMemoTiptapJson), |
| 206 | + enhancedContent: enhancedContent || null, |
| 207 | + transcript, |
| 208 | + participants, |
| 209 | + event: eventName ? { name: eventName } : null, |
| 210 | + }; |
| 211 | +} |
0 commit comments