fix: split transcription input errors
This commit is contained in:
1 parent
b5aa8b65cc
commit
bf3ecf0e40
2 files changed
+32
-10
No files matched your search
@@ -45,7 +45,9 @@ import {
|
||||
MAX_TRANSCRIPTION_AUDIO_BYTES,
|
||||
readTranscriptionAudio,
|
||||
resolveTranscriptionProvider,
|
||||
TranscriptionInputError,
|
||||
TranscriptionApiKeyMissingError,
|
||||
TranscriptionEmptyAudioError,
|
||||
TranscriptionProviderUnsupportedError,
|
||||
} from "./transcription.ts";
|
||||
|
||||
const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
|
||||
@@ -273,7 +275,7 @@ export const transcriptionRouteLayer = HttpRouter.add(
|
||||
request.headers["x-t3-transcription-provider"] ?? "",
|
||||
);
|
||||
if (!provider) {
|
||||
return yield* new TranscriptionInputError({ reason: "invalid_provider" });
|
||||
return yield* new TranscriptionProviderUnsupportedError();
|
||||
}
|
||||
|
||||
const audio = yield* readTranscriptionAudio(request.stream);
|
||||
@@ -291,12 +293,16 @@ export const transcriptionRouteLayer = HttpRouter.add(
|
||||
EnvironmentScopeRequiredError: HttpServerRespondable.toResponse,
|
||||
TranscriptionAudioTooLargeError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 413 })),
|
||||
TranscriptionApiKeyMissingError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 400 })),
|
||||
TranscriptionBodyReadError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 400 })),
|
||||
TranscriptionInputError: (error) =>
|
||||
TranscriptionEmptyAudioError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 400 })),
|
||||
TranscriptionProviderError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 502 })),
|
||||
TranscriptionProviderUnsupportedError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 400 })),
|
||||
TranscriptionRequestError: (error) =>
|
||||
Effect.succeed(HttpServerResponse.jsonUnsafe({ error: error.message }, { status: 502 })),
|
||||
TranscriptionResponseError: (error) =>
|
||||
|
||||
@@ -42,13 +42,29 @@ export class TranscriptionBodyReadError extends Schema.TaggedErrorClass<Transcri
|
||||
}
|
||||
}
|
||||
|
||||
export class TranscriptionInputError extends Schema.TaggedErrorClass<TranscriptionInputError>()(
|
||||
"TranscriptionInputError",
|
||||
{ reason: Schema.Literals(["empty_audio", "invalid_provider", "missing_api_key"]) },
|
||||
export class TranscriptionEmptyAudioError extends Schema.TaggedErrorClass<TranscriptionEmptyAudioError>()(
|
||||
"TranscriptionEmptyAudioError",
|
||||
{},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "The recording was empty.";
|
||||
}
|
||||
}
|
||||
|
||||
export class TranscriptionProviderUnsupportedError extends Schema.TaggedErrorClass<TranscriptionProviderUnsupportedError>()(
|
||||
"TranscriptionProviderUnsupportedError",
|
||||
{},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Select OpenAI or Groq for transcription.";
|
||||
}
|
||||
}
|
||||
|
||||
export class TranscriptionApiKeyMissingError extends Schema.TaggedErrorClass<TranscriptionApiKeyMissingError>()(
|
||||
"TranscriptionApiKeyMissingError",
|
||||
{},
|
||||
) {
|
||||
override get message(): string {
|
||||
if (this.reason === "empty_audio") return "The recording was empty.";
|
||||
if (this.reason === "invalid_provider") return "Select OpenAI or Groq for transcription.";
|
||||
return "Add an API key for the selected transcription provider.";
|
||||
}
|
||||
}
|
||||
@@ -133,7 +149,7 @@ export const forwardVoiceTranscription = Effect.fn("voiceTranscription.forward")
|
||||
input: VoiceTranscriptionInput,
|
||||
) {
|
||||
if (input.audio.byteLength === 0) {
|
||||
return yield* new TranscriptionInputError({ reason: "empty_audio" });
|
||||
return yield* new TranscriptionEmptyAudioError();
|
||||
}
|
||||
if (input.audio.byteLength > MAX_TRANSCRIPTION_AUDIO_BYTES) {
|
||||
return yield* new TranscriptionAudioTooLargeError({
|
||||
@@ -142,7 +158,7 @@ export const forwardVoiceTranscription = Effect.fn("voiceTranscription.forward")
|
||||
}
|
||||
const apiKey = input.apiKey.trim();
|
||||
if (!apiKey) {
|
||||
return yield* new TranscriptionInputError({ reason: "missing_api_key" });
|
||||
return yield* new TranscriptionApiKeyMissingError();
|
||||
}
|
||||
|
||||
const providerConfig = transcriptionProviderConfig(input.provider);
|
||||
|
||||
Reference in new issue
Block a user