b17984fbef
Outbound (already existed one-way: ticket create -> Gitea issue) now also mirrors status changes: closing/reopening a ticket in the admin UI closes/reopens the linked Gitea issue, and replies posted from Epicure (by the ticket owner or an admin) post as a comment on the issue. Captures and stores the Gitea issue number at creation time to address these follow-up calls without re-parsing the issue URL. Inbound: new webhook receiver at /api/webhooks/gitea, verified via HMAC-SHA256 signature (X-Gitea-Signature) with a configurable GITEA_WEBHOOK_SECRET site setting, deduped by delivery id the same way the existing Stripe receiver dedupes events. Handles "issues" (closed/reopened -> ticket status) and "issue_comment" (created -> appends to the ticket's comment thread), skipping comments Epicura already posted itself (matched by Gitea comment id) to avoid loops. New support_ticket_comments table backs a lightweight conversation thread on both the user-facing support page and the admin support manager, each comment tagged by author (user/admin/gitea). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
131 lines
3.7 KiB
TypeScript
131 lines
3.7 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";
|
|
|
|
const SECRET_KEYS: SiteSettingKey[] = [
|
|
"OPENAI_API_KEY",
|
|
"ANTHROPIC_API_KEY",
|
|
"OPENROUTER_API_KEY",
|
|
"VAPID_PRIVATE_KEY",
|
|
"GITEA_TOKEN",
|
|
"GITEA_WEBHOOK_SECRET",
|
|
];
|
|
|
|
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",
|
|
];
|
|
|
|
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 },
|
|
});
|
|
}
|