f0632cce95
estimateNutrition() (lib/ai/features/estimate-nutrition.ts) is now a
hybrid: for each ingredient, estimateGrams() (lib/ingredient-grams.ts)
converts its quantity+unit to grams where possible (weight units
exactly; volume units -- cup/tbsp/tsp/ml/l/etc -- via a 1ml~=1g water-
density approximation, documented as a real simplification but far
better than skipping them). Convertible ingredients get looked up in
USDA FoodData Central (lib/usda.ts, SR Legacy + Survey (FNDDS)
datasets -- the two suited to generic/raw ingredients, not Branded
packaged products or narrower Foundation) and summed. Whatever's left
(count-based units like "2 cloves", no USDA match, or USDA_API_KEY
unset entirely) goes through one AI call asking for just that
subset's total contribution, which sums directly with the USDA
totals -- no whole-recipe AI estimate to reconcile against.
Same exported signature and return shape as before
(NutritionEstimate / {perServing: {...}}), so every existing caller
(the nutrition route, meal-plan generation) gets more accurate
results automatically once USDA_API_KEY is set in Admin -> Settings,
with zero behavior change when it isn't.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import { db, siteSettings, eq } from "@epicure/db";
|
|
import { encrypt, decrypt } from "@/lib/encrypt";
|
|
|
|
export type SiteSettingKey =
|
|
| "OPENAI_API_KEY"
|
|
| "ANTHROPIC_API_KEY"
|
|
| "OPENROUTER_API_KEY"
|
|
| "OPENROUTER_DEFAULT_MODEL"
|
|
| "OLLAMA_BASE_URL"
|
|
| "NEXT_PUBLIC_VAPID_PUBLIC_KEY"
|
|
| "VAPID_PRIVATE_KEY"
|
|
| "SIGNUPS_DISABLED"
|
|
| "DEFAULT_TEXT_PROVIDER"
|
|
| "DEFAULT_TEXT_MODEL"
|
|
| "DEFAULT_VISION_PROVIDER"
|
|
| "DEFAULT_VISION_MODEL"
|
|
| "DEFAULT_MEAL_PLAN_PROVIDER"
|
|
| "DEFAULT_MEAL_PLAN_MODEL"
|
|
| "DEFAULT_CHAT_PROVIDER"
|
|
| "DEFAULT_CHAT_MODEL"
|
|
| "AI_TOOL_CALLING_ENABLED"
|
|
| "GITEA_URL"
|
|
| "GITEA_TOKEN"
|
|
| "GITEA_REPO"
|
|
| "GITEA_WEBHOOK_SECRET"
|
|
| "USDA_API_KEY";
|
|
|
|
const SECRET_KEYS: SiteSettingKey[] = [
|
|
"OPENAI_API_KEY",
|
|
"ANTHROPIC_API_KEY",
|
|
"OPENROUTER_API_KEY",
|
|
"VAPID_PRIVATE_KEY",
|
|
"GITEA_TOKEN",
|
|
"GITEA_WEBHOOK_SECRET",
|
|
"USDA_API_KEY",
|
|
];
|
|
|
|
export function isSecretKey(key: SiteSettingKey): boolean {
|
|
return SECRET_KEYS.includes(key);
|
|
}
|
|
|
|
export async function getSiteSetting(key: SiteSettingKey): Promise<string | null> {
|
|
try {
|
|
const row = await db.query.siteSettings.findFirst({ where: eq(siteSettings.key, key) });
|
|
if (row?.value) {
|
|
return row.isSecret ? decrypt(row.value) : row.value;
|
|
}
|
|
} catch {
|
|
// fall through to env
|
|
}
|
|
return process.env[key] ?? null;
|
|
}
|
|
|
|
export async function getAllSiteSettings(): Promise<Record<string, { value: string | null; isSecret: boolean; fromDb: boolean }>> {
|
|
const rows = await db.query.siteSettings.findMany();
|
|
const dbMap = new Map(rows.map((r) => [r.key, r]));
|
|
|
|
const KNOWN_KEYS: SiteSettingKey[] = [
|
|
"OPENAI_API_KEY",
|
|
"ANTHROPIC_API_KEY",
|
|
"OPENROUTER_API_KEY",
|
|
"OPENROUTER_DEFAULT_MODEL",
|
|
"OLLAMA_BASE_URL",
|
|
"NEXT_PUBLIC_VAPID_PUBLIC_KEY",
|
|
"VAPID_PRIVATE_KEY",
|
|
"DEFAULT_TEXT_PROVIDER",
|
|
"DEFAULT_TEXT_MODEL",
|
|
"DEFAULT_VISION_PROVIDER",
|
|
"DEFAULT_VISION_MODEL",
|
|
"DEFAULT_MEAL_PLAN_PROVIDER",
|
|
"DEFAULT_MEAL_PLAN_MODEL",
|
|
"DEFAULT_CHAT_PROVIDER",
|
|
"DEFAULT_CHAT_MODEL",
|
|
"AI_TOOL_CALLING_ENABLED",
|
|
"GITEA_URL",
|
|
"GITEA_TOKEN",
|
|
"GITEA_REPO",
|
|
"GITEA_WEBHOOK_SECRET",
|
|
"USDA_API_KEY",
|
|
];
|
|
|
|
const result: Record<string, { value: string | null; isSecret: boolean; fromDb: boolean }> = {};
|
|
for (const k of KNOWN_KEYS) {
|
|
const row = dbMap.get(k);
|
|
const secret = isSecretKey(k);
|
|
if (row) {
|
|
result[k] = {
|
|
value: secret ? "••••••••••••" : (row.value ?? null),
|
|
isSecret: secret,
|
|
fromDb: true,
|
|
};
|
|
} else {
|
|
const envVal = process.env[k];
|
|
result[k] = {
|
|
value: envVal ? (secret ? "••••••••••••" : envVal) : null,
|
|
isSecret: secret,
|
|
fromDb: false,
|
|
};
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export async function isSignupsDisabled(): Promise<boolean> {
|
|
return (await getSiteSetting("SIGNUPS_DISABLED")) === "true";
|
|
}
|
|
|
|
/** Defaults to enabled — the chatbot's createRecipe/addToShoppingList tools
|
|
* only get turned off explicitly, e.g. for a local model that can't reliably
|
|
* call tools. */
|
|
export async function isAiToolCallingEnabled(): Promise<boolean> {
|
|
return (await getSiteSetting("AI_TOOL_CALLING_ENABLED")) !== "false";
|
|
}
|
|
|
|
export async function setSiteSetting(
|
|
key: SiteSettingKey,
|
|
value: string | null,
|
|
updatedById: string
|
|
): Promise<void> {
|
|
if (value === null || value === "") {
|
|
await db.delete(siteSettings).where(eq(siteSettings.key, key));
|
|
return;
|
|
}
|
|
const secret = isSecretKey(key);
|
|
const stored = secret ? encrypt(value) : value;
|
|
await db
|
|
.insert(siteSettings)
|
|
.values({ key, value: stored, isSecret: secret, updatedById, updatedAt: new Date() })
|
|
.onConflictDoUpdate({
|
|
target: siteSettings.key,
|
|
set: { value: stored, updatedAt: new Date(), updatedById },
|
|
});
|
|
}
|