diff options
| author | Adam Malczewski <[email protected]> | 2026-06-21 14:58:38 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-21 14:58:38 +0900 |
| commit | dfb3a61afa545b67b85dbefe6b217affd14c16a7 (patch) | |
| tree | fbe0d18323136cc19d971e18f0801428bcd2e4a7 /packages/tool-youtube-transcript/src/client.ts | |
| parent | d56fe9cf64719bb330c17b2daee58c0bafa057c9 (diff) | |
| download | dispatch-dfb3a61afa545b67b85dbefe6b217affd14c16a7.tar.gz dispatch-dfb3a61afa545b67b85dbefe6b217affd14c16a7.zip | |
feat(tool-youtube-transcript): YouTube transcription tool
New standard tool extension backed by a self-hosted transcriber service
(http://100.102.55.49:41090, Tailscale, no API key). One tool
youtube_transcript — fetches transcripts for YouTube videos. Returns
completed (full text + timestamped segments), queued/processing (position
+ ETA + .youtube_subtitles_pending retry convention), or failed (error).
Pure core: validateUrl + format* functions + truncateOutput. Injected
edge: TranscriptClient (injectable fetchFn, AbortSignal.any for
cancellation). concurrencySafe true, capabilities network. 30 tests.
Verified: tsc EXIT 0, 1152 vitest, biome clean (327 files). Boot smoke
clean.
Diffstat (limited to 'packages/tool-youtube-transcript/src/client.ts')
| -rw-r--r-- | packages/tool-youtube-transcript/src/client.ts | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/packages/tool-youtube-transcript/src/client.ts b/packages/tool-youtube-transcript/src/client.ts new file mode 100644 index 0000000..a088d7d --- /dev/null +++ b/packages/tool-youtube-transcript/src/client.ts @@ -0,0 +1,80 @@ +/** + * TranscriptClient — the injected outermost edge for the youtube_transcript + * tool. + * + * All effects (fetch, clock-via-abort-timeout) are injected so the pure + * decision logic remains testable without real I/O. The factory builds a + * single `getTranscript` method over a self-hosted transcriber instance (no + * API key). Mirrors the tool-web-search FirecrawlClient's request structure: + * per-request timeout combined with the caller's cancellation signal via + * `AbortSignal.any`. + */ + +import type { TranscriptResponse } from "./format.js"; + +export type FetchLike = typeof globalThis.fetch; + +export const DEFAULT_BASE_URL = "http://100.102.55.49:41090"; +export const DEFAULT_TIMEOUT_MS = 30_000; + +export interface TranscriptClient { + readonly getTranscript: (url: string, signal: AbortSignal) => Promise<TranscriptResponse>; +} + +export interface TranscriptClientDeps { + readonly baseUrl: string; + readonly fetchFn: FetchLike; + readonly timeoutMs?: number; +} + +/** + * Create a TranscriptClient. `getTranscript` builds the request URL + * (`${baseUrl}/api/transcript?url=${encodeURIComponent(url)}`), calls the + * injected `fetchFn`, and handles HTTP + JSON errors. The per-request timeout + * is combined with the caller's cancellation signal via `AbortSignal.any`. + */ +export function createTranscriptClient(deps: TranscriptClientDeps): TranscriptClient { + const baseUrl = deps.baseUrl; + const fetchFn = deps.fetchFn; + const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS; + + return { + async getTranscript(url: string, signal: AbortSignal): Promise<TranscriptResponse> { + const endpoint = `${baseUrl}/api/transcript?url=${encodeURIComponent(url)}`; + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + const combined = AbortSignal.any([signal, controller.signal]); + try { + let response: Response; + try { + response = await fetchFn(endpoint, { + method: "GET", + headers: { Accept: "application/json" }, + signal: combined, + }); + } catch (err) { + if (signal.aborted) { + throw new Error("Request aborted."); + } + if (controller.signal.aborted) { + throw new Error(`Transcriber request timed out after ${timeoutMs / 1000} seconds.`); + } + throw err; + } + if (!response.ok) { + const text = await response.text().catch(() => ""); + throw new Error( + `HTTP ${response.status} ${response.statusText}${text ? `: ${text}` : ""}`, + ); + } + try { + return (await response.json()) as TranscriptResponse; + } catch { + throw new Error("Failed to parse transcriber response as JSON"); + } + } finally { + clearTimeout(timeout); + } + }, + }; +} |
