Files
LingvAI/pages/api/admin/settings.ts
Malin 0be8b0b0f0 fix: resolve 422 from Replicate + add local Cog Docker mode
- replicate-translate: parse owner/model:hash correctly — extract only
  the hash portion for the version field, and use the model endpoint
  (POST /v1/models/{owner}/{model}/predictions) which avoids 422
  'Invalid version' errors when sending the full owner/model:hash string.

- Add local Cog mode: when replicateMode="local", calls the local Docker
  container directly (no Replicate API key needed), default endpoint
  http://localhost:5030/predictions (host port 5030 → container port 5000).

- settings-store: add replicateMode ("cloud"|"local") and localEndpoint
  fields with env var fallbacks REPLICATE_MODE and LOCAL_MODEL_ENDPOINT.

- admin panel: Radio selector for Cloud vs Local mode; shows docker run
  command snippet and local endpoint URL field when local is selected;
  hides Replicate API token field in local mode (not needed).

Local model startup:
  docker run -d -p 5030:5000 \
    r8.im/jigsawstack/text-translate@sha256:454df4c...

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-10 08:50:40 +01:00

47 lines
1.8 KiB
TypeScript

import { NextApiHandler } from "next";
import { requireAdmin } from "@utils/admin-auth";
import { readSettings, writeSettings } from "@utils/settings-store";
const handler: NextApiHandler = async (req, res) => {
if (!(await requireAdmin(req, res))) return;
if (req.method === "GET") {
const settings = readSettings();
// Never expose the adminPasswordHash
const { adminPasswordHash: _omit, ...safe } = settings;
return res.status(200).json(safe);
}
if (req.method === "POST") {
const {
replicateApiToken,
jigsawApiKey,
modelVersion,
replicateEnabled,
replicateMode,
localEndpoint,
newPassword
} = req.body ?? {};
const updates: Parameters<typeof writeSettings>[0] = {};
if (replicateApiToken !== undefined) updates.replicateApiToken = replicateApiToken;
if (jigsawApiKey !== undefined) updates.jigsawApiKey = jigsawApiKey;
if (modelVersion !== undefined) updates.modelVersion = modelVersion;
if (replicateEnabled !== undefined) updates.replicateEnabled = Boolean(replicateEnabled);
if (replicateMode === "cloud" || replicateMode === "local") updates.replicateMode = replicateMode;
if (localEndpoint !== undefined) updates.localEndpoint = localEndpoint;
if (newPassword && typeof newPassword === "string" && newPassword.length >= 6) {
updates.adminPasswordHash = newPassword;
}
const saved = writeSettings(updates);
const { adminPasswordHash: _omit, ...safe } = saved;
return res.status(200).json(safe);
}
res.setHeader("Allow", ["GET", "POST"]);
return res.status(405).json({ error: "Method Not Allowed" });
};
export default handler;