mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:22:47 +00:00
refactor(media): share http error handling
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
|
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
|
||||||
import { fetchWithTimeoutGuarded, normalizeBaseUrl, readErrorResponse } from "../shared.js";
|
import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js";
|
||||||
|
|
||||||
export const DEFAULT_DEEPGRAM_AUDIO_BASE_URL = "https://api.deepgram.com/v1";
|
export const DEFAULT_DEEPGRAM_AUDIO_BASE_URL = "https://api.deepgram.com/v1";
|
||||||
export const DEFAULT_DEEPGRAM_AUDIO_MODEL = "nova-3";
|
export const DEFAULT_DEEPGRAM_AUDIO_MODEL = "nova-3";
|
||||||
@@ -63,11 +63,7 @@ export async function transcribeDeepgramAudio(
|
|||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!res.ok) {
|
await assertOkOrThrowHttpError(res, "Audio transcription failed");
|
||||||
const detail = await readErrorResponse(res);
|
|
||||||
const suffix = detail ? `: ${detail}` : "";
|
|
||||||
throw new Error(`Audio transcription failed (HTTP ${res.status})${suffix}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = (await res.json()) as DeepgramTranscriptResponse;
|
const payload = (await res.json()) as DeepgramTranscriptResponse;
|
||||||
const transcript = payload.results?.channels?.[0]?.alternatives?.[0]?.transcript?.trim();
|
const transcript = payload.results?.channels?.[0]?.alternatives?.[0]?.transcript?.trim();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js";
|
import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js";
|
||||||
import { fetchWithTimeoutGuarded, normalizeBaseUrl, readErrorResponse } from "../shared.js";
|
import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js";
|
||||||
|
|
||||||
export async function generateGeminiInlineDataText(params: {
|
export async function generateGeminiInlineDataText(params: {
|
||||||
buffer: Buffer;
|
buffer: Buffer;
|
||||||
@@ -73,11 +73,7 @@ export async function generateGeminiInlineDataText(params: {
|
|||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!res.ok) {
|
await assertOkOrThrowHttpError(res, params.httpErrorLabel);
|
||||||
const detail = await readErrorResponse(res);
|
|
||||||
const suffix = detail ? `: ${detail}` : "";
|
|
||||||
throw new Error(`${params.httpErrorLabel} (HTTP ${res.status})${suffix}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = (await res.json()) as {
|
const payload = (await res.json()) as {
|
||||||
candidates?: Array<{
|
candidates?: Array<{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
|
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
|
||||||
import { fetchWithTimeoutGuarded, normalizeBaseUrl, readErrorResponse } from "../shared.js";
|
import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js";
|
||||||
|
|
||||||
export const DEFAULT_OPENAI_AUDIO_BASE_URL = "https://api.openai.com/v1";
|
export const DEFAULT_OPENAI_AUDIO_BASE_URL = "https://api.openai.com/v1";
|
||||||
const DEFAULT_OPENAI_AUDIO_MODEL = "gpt-4o-mini-transcribe";
|
const DEFAULT_OPENAI_AUDIO_MODEL = "gpt-4o-mini-transcribe";
|
||||||
@@ -52,11 +52,7 @@ export async function transcribeOpenAiCompatibleAudio(
|
|||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!res.ok) {
|
await assertOkOrThrowHttpError(res, "Audio transcription failed");
|
||||||
const detail = await readErrorResponse(res);
|
|
||||||
const suffix = detail ? `: ${detail}` : "";
|
|
||||||
throw new Error(`Audio transcription failed (HTTP ${res.status})${suffix}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = (await res.json()) as { text?: string };
|
const payload = (await res.json()) as { text?: string };
|
||||||
const text = payload.text?.trim();
|
const text = payload.text?.trim();
|
||||||
|
|||||||
@@ -47,3 +47,12 @@ export async function readErrorResponse(res: Response): Promise<string | undefin
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function assertOkOrThrowHttpError(res: Response, label: string): Promise<void> {
|
||||||
|
if (res.ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const detail = await readErrorResponse(res);
|
||||||
|
const suffix = detail ? `: ${detail}` : "";
|
||||||
|
throw new Error(`${label} (HTTP ${res.status})${suffix}`);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user