



















@@ -5,6 +5,7 @@ import type {
55import {
66assertOkOrThrowHttpError,
77postTranscriptionRequest,
8+readProviderJsonObjectResponse,
89resolveProviderHttpRequestConfig,
910requireTranscriptionText,
1011} from "openclaw/plugin-sdk/provider-http";
@@ -17,15 +18,36 @@ function resolveModel(model?: string): string {
1718return trimmed || DEFAULT_DEEPGRAM_AUDIO_MODEL;
1819}
192020-type DeepgramTranscriptResponse = {
21-results?: {
22-channels?: Array<{
23-alternatives?: Array<{
24-transcript?: string;
25-}>;
26-}>;
27-};
28-};
21+function asRecord(value: unknown): Record<string, unknown> | undefined {
22+return typeof value === "object" && value !== null && !Array.isArray(value)
23+ ? (value as Record<string, unknown>)
24+ : undefined;
25+}
26+27+function readDeepgramTranscript(payload: Record<string, unknown>): string | undefined {
28+const results = asRecord(payload.results);
29+if (!results) {
30+return undefined;
31+}
32+if (!Array.isArray(results.channels)) {
33+throw new Error("Audio transcription failed: malformed JSON response");
34+}
35+const channel = asRecord(results.channels[0]);
36+if (!channel) {
37+return undefined;
38+}
39+if (!Array.isArray(channel.alternatives)) {
40+throw new Error("Audio transcription failed: malformed JSON response");
41+}
42+const alternative = asRecord(channel.alternatives[0]);
43+if (!alternative) {
44+return undefined;
45+}
46+if (alternative.transcript !== undefined && typeof alternative.transcript !== "string") {
47+throw new Error("Audio transcription failed: malformed JSON response");
48+}
49+return alternative.transcript;
50+}
29513052export async function transcribeDeepgramAudio(
3153params: AudioTranscriptionRequest,
@@ -75,9 +97,9 @@ export async function transcribeDeepgramAudio(
7597try {
7698await assertOkOrThrowHttpError(res, "Audio transcription failed");
779978-const payload = (await res.json()) as DeepgramTranscriptResponse;
100+const payload = await readProviderJsonObjectResponse(res, "Audio transcription failed");
79101const transcript = requireTranscriptionText(
80-payload.results?.channels?.[0]?.alternatives?.[0]?.transcript,
102+readDeepgramTranscript(payload),
81103"Audio transcription response missing transcript",
82104);
83105return { text: transcript, model };
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。