Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

generation-reconstruct.ts

121 lines4.1 KB

1import type {
2 AudioGenerationResult,
3 ImageGenerationResult,
4 PersistedArtifactRef,
5 SummarizationResult,
6 TranscriptionResult,
7 TTSResult,
8} from '@tanstack/ai'
9import type { GenerationRestoredResult } from './generation-types'
10​
11/**
12 * Per-activity `reconstructResult` mappers. On mount restore the generic
13 * `GenerationClient` hands each specialized hook a {@link GenerationRestoredResult}
14 * (the metadata that survived persistence plus the durable artifact refs, each
15 * carrying its serve `url`); the mapper rebuilds the concrete typed result so
16 * `result` repaints as if the run had just finished, with media resolved to the
17 * durable serve route rather than the provider's expired link.
18 *
19 * A mapper returns `null` when the snapshot cannot rebuild a valid result; then
20 * `result` stays null while `status` / `error` / `resumeState` still repaint.
21 */
22​
23/** Output artifact refs of a given media type that carry a durable serve URL. */
24function mediaUrls(
25 restored: GenerationRestoredResult,
26 mediaType: PersistedArtifactRef['source']['mediaType'],
27): Array<string> {
28 return restored.artifacts
29 .filter(
30 (a) =>
31 a.role === 'output' &&
32 a.source.mediaType === mediaType &&
33 a.url != null,
34 )
35 .map((a) => a.url as string)
37​
38/** image → `{ id, model, images: [{ url }], artifacts }`. */
39export function reconstructImageResult(
40 restored: GenerationRestoredResult,
41): ImageGenerationResult | null {
42 const urls = mediaUrls(restored, 'image')
43 if (urls.length === 0) return null
44 return {
45 id: restored.id ?? '',
46 model: restored.model ?? '',
47 images: urls.map((url) => ({ url })),
48 ...(restored.artifacts.length > 0 ? { artifacts: restored.artifacts } : {}),
49 }
51​
52/**
53 * tts → `{ id, model, audio: '', format, contentType, artifacts }`.
54 *
55 * Unlike {@link reconstructAudioResult}, `TTSResult.audio` is a bare base64
56 * string with no URL slot, and server-driven persistence never stores the raw
57 * bytes — only the durable serve URL on the artifact ref. So the restored
58 * result surfaces the audio through `artifacts` (each carrying `url`); consumers
59 * play the restored clip from `result.artifacts[0].url` and fall back to the
60 * live base64 `audio` only for a just-finished (non-restored) run.
61 */
62export function reconstructSpeechResult(
63 restored: GenerationRestoredResult,
64): TTSResult | null {
65 const ref = restored.artifacts.find(
66 (a) =>
67 a.role === 'output' && a.source.mediaType === 'audio' && a.url != null,
68 )
69 if (!ref) return null
70 const contentType = ref.mimeType || undefined
71 return {
72 id: restored.id ?? '',
73 model: restored.model ?? '',
74 // Bytes live in the blob store, served at `ref.url`; the base64 field can't
75 // be rebuilt from the snapshot, so it stays empty on restore.
76 audio: '',
77 format: contentType?.split('/')[1] ?? '',
78 ...(contentType ? { contentType } : {}),
79 artifacts: restored.artifacts,
80 }
82​
83/** audio → `{ id, model, audio: { url }, artifacts }`. */
84export function reconstructAudioResult(
85 restored: GenerationRestoredResult,
86): AudioGenerationResult | null {
87 const [url] = mediaUrls(restored, 'audio')
88 if (!url) return null
89 return {
90 id: restored.id ?? '',
91 model: restored.model ?? '',
92 audio: { url },
93 ...(restored.artifacts.length > 0 ? { artifacts: restored.artifacts } : {}),
94 }
96​
97/** transcription → `{ id, model, text, artifacts }`. */
98export function reconstructTranscriptionResult(
99 restored: GenerationRestoredResult,
100): TranscriptionResult | null {
101 if (restored.text === undefined) return null
102 return {
103 id: restored.id ?? '',
104 model: restored.model ?? '',
105 text: restored.text,
106 ...(restored.artifacts.length > 0 ? { artifacts: restored.artifacts } : {}),
107 }
109​
110/** summarize → `{ id, model, summary, usage }` (needs persisted `usage`). */
111export function reconstructSummarizeResult(
112 restored: GenerationRestoredResult,
113): SummarizationResult | null {
114 if (restored.text === undefined || restored.usage === undefined) return null
115 return {
116 id: restored.id ?? '',
117 model: restored.model ?? '',
118 summary: restored.text,
119 usage: restored.usage,
120 }