diff --git a/components/ResultCard.tsx b/components/ResultCard.tsx index 3e1d804..71f6394 100644 --- a/components/ResultCard.tsx +++ b/components/ResultCard.tsx @@ -100,8 +100,8 @@ export const ResultCard: React.FC = ({ {[ { icon: 'water' as const, label: t.water, value: t.waterEveryXDays.replace('{0}', result.careInfo.waterIntervalDays.toString()), color: colors.info, bg: colors.infoSoft }, - { icon: 'sunny' as const, label: t.light, value: result.careInfo.light || t.unknown, color: colors.warning, bg: colors.warningSoft }, - { icon: 'thermometer' as const, label: t.temp, value: result.careInfo.temp || t.unknown, color: colors.danger, bg: colors.dangerSoft }, + { icon: 'sunny' as const, label: t.light, value: (result.careInfo.light && result.careInfo.light !== 'Unknown') ? result.careInfo.light : t.unknown, color: colors.warning, bg: colors.warningSoft }, + { icon: 'thermometer' as const, label: t.temp, value: (result.careInfo.temp && result.careInfo.temp !== 'Unknown') ? result.careInfo.temp : t.unknown, color: colors.danger, bg: colors.dangerSoft }, ].map((item) => ( @@ -118,8 +118,8 @@ export const ResultCard: React.FC = ({ {t.detailedCare} {[ { text: t.careTextWater.replace('{0}', result.careInfo.waterIntervalDays.toString()), color: colors.success }, - { text: t.careTextLight.replace('{0}', result.careInfo.light || t.unknown), color: colors.warning }, - { text: t.careTextTemp.replace('{0}', result.careInfo.temp || t.unknown), color: colors.danger }, + { text: t.careTextLight.replace('{0}', (result.careInfo.light && result.careInfo.light !== 'Unknown') ? result.careInfo.light : t.unknown), color: colors.warning }, + { text: t.careTextTemp.replace('{0}', (result.careInfo.temp && result.careInfo.temp !== 'Unknown') ? result.careInfo.temp : t.unknown), color: colors.danger }, ].map((item, i) => ( diff --git a/server/index.js b/server/index.js index 06c1dfd..a9d064c 100644 --- a/server/index.js +++ b/server/index.js @@ -69,6 +69,14 @@ const SEMANTIC_SEARCH_COST = 2; const HEALTH_CHECK_COST = 2; const LOW_CONFIDENCE_REVIEW_THRESHOLD = 0.8; +let catalogCache = null; + +const getCachedCatalogEntries = async (db) => { + if (catalogCache) return catalogCache; + catalogCache = await getPlants(db, { limit: 500 }); + return catalogCache; +}; + const DEFAULT_BOOTSTRAP_PLANTS = [ { id: '1', @@ -627,18 +635,16 @@ app.post('/v1/scan', async (request, response) => { let modelUsed = null; let modelFallbackCount = 0; - if (!isGuest(userId)) { - creditsCharged += await consumeCreditsWithIdempotency( - db, - userId, - chargeKey('scan-primary', userId, idempotencyKey), - SCAN_PRIMARY_COST, - ); - } + const [creditResult, accountSnapshot, catalogEntries] = await Promise.all([ + isGuest(userId) + ? Promise.resolve(0) + : consumeCreditsWithIdempotency(db, userId, chargeKey('scan-primary', userId, idempotencyKey), SCAN_PRIMARY_COST), + getAccountSnapshot(db, userId), + getCachedCatalogEntries(db), + ]); + creditsCharged += creditResult; - const accountSnapshot = await getAccountSnapshot(db, userId); const scanPlan = accountSnapshot.plan === 'pro' ? 'pro' : 'free'; - const catalogEntries = await getPlants(db, { limit: 500 }); let result = pickCatalogFallback(catalogEntries, imageUri, false, { silent: true }); let usedOpenAi = false; diff --git a/server/lib/scanGrounding.js b/server/lib/scanGrounding.js index 87cab0e..ba4abfa 100644 --- a/server/lib/scanGrounding.js +++ b/server/lib/scanGrounding.js @@ -115,8 +115,8 @@ const applyCatalogGrounding = (aiResult, catalogEntries, language = 'en') => { description: aiResult.description || matchedEntry.description || '', careInfo: { waterIntervalDays: Math.max(1, Number(matchedEntry.careInfo?.waterIntervalDays) || Number(aiResult.careInfo?.waterIntervalDays) || 7), - light: matchedEntry.careInfo?.light || aiResult.careInfo?.light || 'Unknown', - temp: matchedEntry.careInfo?.temp || aiResult.careInfo?.temp || 'Unknown', + light: (matchedEntry.careInfo?.light && matchedEntry.careInfo.light !== 'Unknown') ? matchedEntry.careInfo.light : (aiResult.careInfo?.light || 'Unknown'), + temp: (matchedEntry.careInfo?.temp && matchedEntry.careInfo.temp !== 'Unknown') ? matchedEntry.careInfo.temp : (aiResult.careInfo?.temp || 'Unknown'), }, }, };