diff --git a/lib/grading/grading.js b/lib/grading/grading.js index 974f8f5..3f397a9 100644 --- a/lib/grading/grading.js +++ b/lib/grading/grading.js @@ -641,14 +641,21 @@ export async function gradeDetailedLLM(frontUrl, backUrl, config, extraImages = const apiKey = process.env.ANTHROPIC_API_KEY; let detectTokens = { input: 0, output: 0 }; - const togetherKey = process.env.TOGETHER_API_KEY; + const rawTogetherKey = process.env.TOGETHER_API_KEY; + const togetherKey = rawTogetherKey && rawTogetherKey.length > 20 ? rawTogetherKey : null; const detectOpts = togetherKey ? { provider: "together", togetherKey } : {}; const detectModel = togetherKey ? null : "claude-sonnet-4-6"; - const detectJobs = [detectAndCropCard(frontUrl, apiKey, detectModel, detectOpts)]; - if (hasBack) detectJobs.push(detectAndCropCard(backUrl, apiKey, detectModel, detectOpts)); - const [frontDetect, backDetect] = await Promise.all(detectJobs); + let frontDetect = { imageUrl: frontUrl, cropped: false }; + let backDetect = hasBack ? { imageUrl: backUrl, cropped: false } : null; + try { + const detectJobs = [detectAndCropCard(frontUrl, apiKey, detectModel, detectOpts)]; + if (hasBack) detectJobs.push(detectAndCropCard(backUrl, apiKey, detectModel, detectOpts)); + [frontDetect, backDetect] = await Promise.all(detectJobs); + } catch (e) { + console.warn(`[grade] card detection failed, using original images: ${e.message || e}`); + } const frontBlock = frontDetect.cropped ? imageBlockFromBase64(frontDetect.base64, frontDetect.mediaType) @@ -801,8 +808,8 @@ export async function gradeImage(imageUrl, config, extraImages = [], centeringHi if (config.aiGrading.mode === "llm") { const backImg = extraImages[0]?.imageUrl || extraImages[0] || null; const remainingExtras = extraImages.slice(1); - if (backImg && config.aiGrading.llm.provider === "claude") { - result = await gradeDetailedLLM(imageUrl, backImg, config, remainingExtras, centeringHint); + if (config.aiGrading.llm.provider === "claude") { + result = await gradeDetailedLLM(imageUrl, backImg || null, config, remainingExtras, centeringHint); } if (!result) { result = await gradeViaLLM(imageUrl, config, extraImages);