feat: show backend optimization progress
This commit is contained in:
@@ -142,6 +142,31 @@ describe("generateValidatedJson", () => {
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not duplicate start and response logs when using the default JSON generator", async () => {
|
||||
process.env.LLM_PROVIDER = "deepseek";
|
||||
process.env.DEEPSEEK_API_KEY = "test-key";
|
||||
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
|
||||
client.setChatCompletionForTesting(async () => ({
|
||||
choices: [{ message: { content: '{"value":"from-llm"}' } }],
|
||||
}));
|
||||
|
||||
const result = await client.generateValidatedJson({
|
||||
schema: z.object({ value: z.string() }),
|
||||
prompt: "Return JSON.",
|
||||
task: "article_optimizer",
|
||||
});
|
||||
|
||||
expect(result).toEqual({ value: "from-llm" });
|
||||
const startLogs = infoSpy.mock.calls
|
||||
.map((call) => call[0])
|
||||
.filter((line) => line.startsWith("[llm:start]"));
|
||||
const responseLogs = infoSpy.mock.calls
|
||||
.map((call) => call[0])
|
||||
.filter((line) => line.startsWith("[llm:response]"));
|
||||
expect(startLogs).toHaveLength(1);
|
||||
expect(responseLogs).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("logs validation failure without leaking provider secrets", async () => {
|
||||
process.env.LLM_PROVIDER = "deepseek";
|
||||
process.env.DEEPSEEK_API_KEY = "super-secret-key";
|
||||
|
||||
+14
-7
@@ -224,17 +224,22 @@ export async function generateValidatedJson<T>({
|
||||
);
|
||||
}
|
||||
|
||||
const usesDefaultGenerator = generateJsonForValidation === generateJson;
|
||||
const status = getLlmProviderStatus();
|
||||
const startedAt = Date.now();
|
||||
console.info(
|
||||
`[llm:start] provider=${status.provider} model=${input.model ?? status.model} task=${task}`,
|
||||
);
|
||||
if (!usesDefaultGenerator) {
|
||||
console.info(
|
||||
`[llm:start] provider=${status.provider} model=${input.model ?? status.model} task=${task}`,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const generated = await generateJsonForValidation<unknown>(input);
|
||||
console.info(
|
||||
`[llm:response] task=${task} duration_ms=${Date.now() - startedAt} raw=${truncateRaw(stringifyForLog(generated))}`,
|
||||
);
|
||||
if (!usesDefaultGenerator) {
|
||||
console.info(
|
||||
`[llm:response] task=${task} duration_ms=${Date.now() - startedAt} raw=${truncateRaw(stringifyForLog(generated))}`,
|
||||
);
|
||||
}
|
||||
const parsed = schema.safeParse(generated);
|
||||
if (parsed.success) {
|
||||
console.info(`[llm:validated] task=${task} ok=true`);
|
||||
@@ -254,7 +259,9 @@ export async function generateValidatedJson<T>({
|
||||
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)}`,
|
||||
usesDefaultGenerator
|
||||
? `[llm:error] task=${task} message=${quoteLogValue(message)}`
|
||||
: `[llm:error] task=${task} duration_ms=${Date.now() - startedAt} message=${quoteLogValue(message)}`,
|
||||
);
|
||||
throw error instanceof Error ? error : new Error(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user