CODE · 2.7 KB
api/manage-settings.js
Workspace snapshot · 09/04 13:35
import { callRuntimeRpc, loadPromptSettings, RUNTIME_SERVICE_ID } from "./banner-core.js";
import { hasManageSession, isSameOrigin } from "./manage-auth.js";
const useCaseIds = ["download-cover", "blog", "youtube", "web-hero", "web-banner", "sns-square", "sns-portrait", "story", "display-ad"];
function value(input, max) {
return typeof input === "string" ? input.trim().slice(0, max) : "";
}
export function validateSettings(body = {}) {
const settings = {
strategyInstruction: value(body.strategyInstruction, 12_000),
imagePromptTemplate: value(body.imagePromptTemplate, 16_000),
directionPrompts: {
人物写真: value(body.directionPrompts?.人物写真, 4_000),
強コピー: value(body.directionPrompts?.強コピー, 4_000),
シンプル: value(body.directionPrompts?.シンプル, 4_000),
},
useCasePrompts: Object.fromEntries(useCaseIds.map((id) => [id, value(body.useCasePrompts?.[id], 4_000)])),
};
const requiredVariables = ["serviceName", "target", "angle", "directionPrompt", "main", "cta"];
if (!settings.strategyInstruction || !settings.imagePromptTemplate || Object.values(settings.directionPrompts).some((item) => !item) || Object.values(settings.useCasePrompts).some((item) => !item) || requiredVariables.some((name) => !settings.imagePromptTemplate.includes(`{{${name}}}`))) throw new Error("必須項目または共通プロンプトの変数が不足しています");
return settings;
}
export default async function handler(req, res) {
if (!(await hasManageSession(req))) return res.status(401).json({ error: "管理認証が必要です" });
try {
if (req.method === "GET") return res.status(200).json(await loadPromptSettings());
if (req.method !== "POST") return res.status(405).json({ error: "GETまたはPOSTのみ利用できます" });
if (!isSameOrigin(req)) return res.status(403).json({ error: "送信元を確認できません" });
const settings = validateSettings(req.body);
const version = await callRuntimeRpc({
rpc: "avs_llm_public_update_prompt_settings",
payload: {
p_service_id: RUNTIME_SERVICE_ID,
p_token: process.env.AVS_LLM_RUNTIME_SERVICE_TOKEN,
p_strategy_instruction: settings.strategyInstruction,
p_image_prompt_template: settings.imagePromptTemplate,
p_person_prompt: settings.directionPrompts.人物写真,
p_copy_prompt: settings.directionPrompts.強コピー,
p_simple_prompt: settings.directionPrompts.シンプル,
p_use_case_prompts: settings.useCasePrompts,
p_actor_id: "ai-banner-manage-panel",
},
});
return res.status(200).json({ ok: true, version });
} catch (error) {
return res.status(error.status || 400).json({ error: error.message });
}
}