fix: surface llm workflow errors
This commit is contained in:
+25
-6
@@ -28,6 +28,16 @@ export interface LlmProviderStatus {
|
||||
reason?: string;
|
||||
}
|
||||
|
||||
export class LlmValidationError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly task: LlmTaskName,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "LlmValidationError";
|
||||
}
|
||||
}
|
||||
|
||||
function getProvider() {
|
||||
return (process.env.LLM_PROVIDER || "deepseek").toLowerCase();
|
||||
}
|
||||
@@ -80,8 +90,8 @@ export function setChatCompletionForTesting(
|
||||
chatCompletionForTesting = handler;
|
||||
}
|
||||
|
||||
function getTask(input: GenerateInput) {
|
||||
return input.task?.trim() || "unknown";
|
||||
function getTask(input: GenerateInput): LlmTaskName {
|
||||
return input.task || "unknown";
|
||||
}
|
||||
|
||||
function getRawLogLimit() {
|
||||
@@ -204,11 +214,14 @@ export function setGenerateJsonForValidation(
|
||||
export async function generateValidatedJson<T>({
|
||||
schema,
|
||||
...input
|
||||
}: GenerateValidatedJsonInput<T>): Promise<T | null> {
|
||||
}: GenerateValidatedJsonInput<T>): Promise<T> {
|
||||
const task = getTask(input);
|
||||
if (!isLlmConfigured()) {
|
||||
console.info(`[llm:validated] task=${task} ok=false reason=not_configured`);
|
||||
return null;
|
||||
throw new LlmValidationError(
|
||||
getLlmProviderStatus().reason ?? "LLM provider is not configured",
|
||||
task,
|
||||
);
|
||||
}
|
||||
|
||||
const status = getLlmProviderStatus();
|
||||
@@ -230,14 +243,20 @@ export async function generateValidatedJson<T>({
|
||||
console.warn(
|
||||
`[llm:validated] task=${task} ok=false zod_error=${quoteLogValue(summarizeZodError(parsed.error))}`,
|
||||
);
|
||||
return null;
|
||||
throw new LlmValidationError(
|
||||
`LLM response failed schema validation: ${summarizeZodError(parsed.error)}`,
|
||||
task,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof LlmValidationError) {
|
||||
throw error;
|
||||
}
|
||||
console.info(`[llm:validated] task=${task} ok=false reason=provider_error`);
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.warn(
|
||||
`[llm:error] task=${task} duration_ms=${Date.now() - startedAt} message=${quoteLogValue(message)}`,
|
||||
);
|
||||
return null;
|
||||
throw error instanceof Error ? error : new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user