-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.ts
More file actions
299 lines (270 loc) · 9.43 KB
/
Copy pathmodels.ts
File metadata and controls
299 lines (270 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Typed response models, matching the TranscriptFetch API schemas.
*
* The API mixes snake_case (transcript/list envelopes) and camelCase (video
* list items). The `normalize*` helpers accept either wire shape and return
* clean camelCase objects, so consumers never see the inconsistency.
*/
/** Credit + byte accounting returned alongside every successful response. */
export interface Usage {
creditsSpent: number;
/** null for unlimited (admin) accounts. */
balance: number | null;
bytes: number;
}
/** A single timestamped caption cue. */
export interface Segment {
start: number;
duration: number;
text: string;
}
/**
* The show and episode behind a podcast transcript.
*
* Present only when the input resolved to a podcast episode. A job polled back
* later carries the short form (show + episode); the rest is filled in on the
* response that did the resolving, so treat every field as optional.
*/
export interface PodcastMeta {
show: string | null;
episode: string | null;
publishedAt: string | null;
feedUrl: string | null;
audioUrl: string | null;
/** How the link was matched to a feed, e.g. "rss" or "itunes_search". */
resolvedVia: string | null;
}
/**
* A single video's transcript, from any supported source.
*
* This also covers the "not ready yet" case. A source with no captions is
* transcribed from its audio, and the API answers 202 with `kind:
* "transcript_job"` and no text. `kind` is therefore a plain string rather than
* a literal: pinning it would make a perfectly good 202 look like a wrong
* shape. When `status` is "processing", pass `jobId` to `transcripts.job()`
* until it reports "completed".
*/
export interface Transcript {
/** transcript | transcript_job */
kind: string;
videoId: string;
/** Source platform: youtube | tiktok | instagram | podcast | file. */
platform: string | null;
title: string | null;
text: string | null;
segments: Segment[];
/** Set only when the input resolved to a podcast episode. */
podcast: PodcastMeta | null;
usage: Usage | null;
// Envelope-level fields, lifted onto the model so an async job round-trips as
// one object (the API returns them beside `data`, not inside it).
/** processing | completed | failed. Only set on async transcription jobs. */
status: string | null;
jobId: string | null;
/** Path to poll for the finished transcript. */
pollUrl: string | null;
}
/** A video reference from a channel/playlist/search list (metadata only). */
export interface Video {
videoId: string;
title: string | null;
thumbnailUrl: string | null;
duration: number | null;
channel: string | null;
}
/** A paginated list of videos. */
export interface VideoList {
kind: "video_list";
source: string;
videos: Video[];
nextCursor: string | null;
usage: Usage | null;
}
/** One video's result inside a batch response. */
export interface BatchResult {
videoId: string;
/** ok | no_transcript | blocked | error | null */
outcome: string | null;
title: string | null;
text: string | null;
segments: Segment[] | null;
cached: boolean;
bytes: number;
}
/** Result of a batch fetch. */
export interface BatchResponse {
kind: "transcript_batch";
results: BatchResult[];
usage: Usage | null;
}
/** The account behind the API key, from `/me`. */
export interface Me {
kind: "me";
userId: string;
/** Remaining credit balance. */
credits: number;
usage: Usage | null;
}
/**
* A polled transcription job: a {@link Transcript} plus why it failed, if it did.
*
* It extends Transcript rather than wrapping one so the poll loop reads the
* same whether the transcript arrived synchronously or via a job, and so the
* completed job can be handed to anything that takes a Transcript.
*
* "failed" is a normal outcome here rather than an HTTP error: the endpoint
* answers 200 in all three states and `status` carries the meaning.
*/
export interface TranscriptJob extends Transcript {
/** Why the job failed. Only present when `status` is "failed". */
error: { code: string | null; message: string | null } | null;
}
/** The public health-check body. */
export interface Health {
status: string;
service: string;
version: string;
time: string;
}
// ── Wire helpers ──────────────────────────────────────────────────────────────
type Wire = Record<string, unknown>;
function obj(value: unknown): Wire {
return value && typeof value === "object" ? (value as Wire) : {};
}
/** Read the first present key (supports both snake_case and camelCase wire names). */
function pick(source: Wire, ...keys: string[]): unknown {
for (const key of keys) {
if (source[key] !== undefined && source[key] !== null) return source[key];
}
return undefined;
}
function str(value: unknown): string {
return typeof value === "string" ? value : "";
}
function strOrNull(value: unknown): string | null {
return typeof value === "string" ? value : null;
}
function numOrNull(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function num(value: unknown, fallback = 0): number {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function normalizeSegment(raw: unknown): Segment {
const s = obj(raw);
return { start: num(s.start), duration: num(s.duration), text: str(s.text) };
}
function normalizeSegments(raw: unknown): Segment[] {
return Array.isArray(raw) ? raw.map(normalizeSegment) : [];
}
function normalizeUsage(env: Wire): Usage | null {
const raw = env.usage;
if (!raw || typeof raw !== "object") return null;
const u = raw as Wire;
return {
creditsSpent: num(pick(u, "creditsSpent", "credits_spent")),
balance: numOrNull(pick(u, "balance")),
bytes: num(pick(u, "bytes")),
};
}
function normalizeVideo(raw: unknown): Video {
const v = obj(raw);
return {
videoId: str(pick(v, "videoId", "video_id")),
title: strOrNull(pick(v, "title")),
thumbnailUrl: strOrNull(pick(v, "thumbnailUrl", "thumbnail_url")),
duration: numOrNull(pick(v, "duration")),
channel: strOrNull(pick(v, "channel")),
};
}
function normalizePodcast(raw: unknown): PodcastMeta | null {
if (!raw || typeof raw !== "object") return null;
const p = raw as Wire;
return {
show: strOrNull(pick(p, "show")),
episode: strOrNull(pick(p, "episode")),
publishedAt: strOrNull(pick(p, "publishedAt", "published_at")),
feedUrl: strOrNull(pick(p, "feedUrl", "feed_url")),
audioUrl: strOrNull(pick(p, "audioUrl", "audio_url")),
resolvedVia: strOrNull(pick(p, "resolvedVia", "resolved_via")),
};
}
/**
* Parse a `{ data, usage }` transcript envelope.
*
* `data` is null while an async job is still processing, which `obj()` already
* absorbs, so a 202 parses into a Transcript carrying only its job fields.
*/
export function normalizeTranscript(env: Wire): Transcript {
const d = obj(env.data);
return {
// Trust the wire kind: a 202 says "transcript_job", a finished one says
// "transcript".
kind: str(pick(d, "kind")) || "transcript",
videoId: str(pick(d, "videoId", "video_id")),
platform: strOrNull(pick(d, "platform")),
title: strOrNull(pick(d, "title")),
text: strOrNull(pick(d, "text")),
segments: normalizeSegments(d.segments),
podcast: normalizePodcast(d.podcast),
usage: normalizeUsage(env),
status: strOrNull(pick(env, "status")),
jobId: strOrNull(pick(env, "jobId", "job_id")),
pollUrl: strOrNull(pick(env, "pollUrl", "poll_url")),
};
}
/** Parse a `{ data, usage }` video-list envelope. */
export function normalizeVideoList(env: Wire): VideoList {
const d = obj(env.data);
const videos = Array.isArray(d.videos) ? d.videos.map(normalizeVideo) : [];
return {
kind: "video_list",
source: str(pick(d, "source")),
videos,
nextCursor: strOrNull(pick(d, "nextCursor", "next_cursor")),
usage: normalizeUsage(env),
};
}
/** Parse a `{ data, usage }` account envelope. */
export function normalizeMe(env: Wire): Me {
const d = obj(env.data);
return {
kind: "me",
userId: str(pick(d, "userId", "user_id")),
credits: num(pick(d, "credits")),
usage: normalizeUsage(env),
};
}
/**
* Parse a job-poll envelope: a transcript envelope that may also carry an
* `error` block, which a failed job delivers at HTTP 200 alongside `ok: false`.
*/
export function normalizeJob(env: Wire): TranscriptJob {
const rawError = obj(env.error);
const hasError = env.error != null && typeof env.error === "object";
return {
...normalizeTranscript(env),
error: hasError
? { code: strOrNull(pick(rawError, "code")), message: strOrNull(pick(rawError, "message")) }
: null,
};
}
/** Parse a `{ data, usage }` batch envelope. */
export function normalizeBatch(env: Wire): BatchResponse {
const d = obj(env.data);
const rawResults = Array.isArray(d.results) ? d.results : [];
const results: BatchResult[] = rawResults.map((raw) => {
const r = obj(raw);
const segments = r.segments;
return {
videoId: str(pick(r, "videoId", "video_id")),
outcome: strOrNull(pick(r, "outcome")),
title: strOrNull(pick(r, "title")),
text: strOrNull(pick(r, "text")),
segments: Array.isArray(segments) ? normalizeSegments(segments) : null,
cached: pick(r, "cached") === true,
bytes: num(pick(r, "bytes")),
};
});
return { kind: "transcript_batch", results, usage: normalizeUsage(env) };
}