72 lines
2.5 KiB
JavaScript
72 lines
2.5 KiB
JavaScript
const PURPOSES = new Set(['inline_image', 'hero', 'card_cover', 'feed_cover']);
|
|
|
|
async function handleGenerate(req, res, { service, userId, consumerPrefix }) {
|
|
if (!service) {
|
|
return res.status(503).json({
|
|
ok: false,
|
|
fallback: true,
|
|
code: 'runtime_unavailable',
|
|
message: '图片生成服务未配置',
|
|
});
|
|
}
|
|
const purpose = String(req.body?.purpose ?? '').trim().toLowerCase();
|
|
const prompt = String(req.body?.prompt ?? '').trim();
|
|
if (!PURPOSES.has(purpose)) {
|
|
return res.status(400).json({ message: '不支持的图片生成用途' });
|
|
}
|
|
if (!prompt || prompt.length > 1600) {
|
|
return res.status(400).json({ message: '图片描述不能为空且不能超过 1600 字符' });
|
|
}
|
|
const headerIdempotencyKey = String(req.get('idempotency-key') ?? '').trim();
|
|
const bodyIdempotencyKey = String(req.body?.idempotency_key ?? '').trim();
|
|
const idempotencyKey = headerIdempotencyKey || bodyIdempotencyKey || undefined;
|
|
if (idempotencyKey && idempotencyKey.length > 128) {
|
|
return res.status(400).json({ message: '幂等键不能超过 128 字符' });
|
|
}
|
|
const result = await service.generate({
|
|
userId,
|
|
purpose,
|
|
prompt,
|
|
negativePrompt: req.body?.negative_prompt,
|
|
consumerRef: `${consumerPrefix}:${idempotencyKey || req.requestId || 'request'}:${purpose}`,
|
|
idempotencyKey,
|
|
});
|
|
if (!result.ok) {
|
|
const unavailable = new Set([
|
|
'runtime_unavailable',
|
|
'image_make_unavailable',
|
|
'IMAGE_MAKE_HTTP_ERROR',
|
|
'IMAGE_MAKE_TIMEOUT',
|
|
]).has(result.code);
|
|
return res.status(unavailable ? 503 : 409).json(result);
|
|
}
|
|
return res.status(201).json({ data: result });
|
|
}
|
|
|
|
export function attachMindSpaceImageGenerationRoutes(
|
|
router,
|
|
{ getService, requireInternal = null } = {},
|
|
) {
|
|
router.post('/mindspace/v1/images/generate', async (req, res) => {
|
|
const service = getService?.();
|
|
return handleGenerate(req, res, {
|
|
service,
|
|
userId: req.currentUser.id,
|
|
consumerPrefix: 'mindspace',
|
|
});
|
|
});
|
|
|
|
if (requireInternal) {
|
|
router.post('/agent/mindspace_image_generate', async (req, res) => {
|
|
if (!requireInternal(req, res)) return;
|
|
const userId = String(req.body?.user_id ?? req.body?.userId ?? '').trim();
|
|
if (!userId) return res.status(400).json({ message: '缺少 user_id' });
|
|
return handleGenerate(req, res, {
|
|
service: getService?.(),
|
|
userId,
|
|
consumerPrefix: 'mindspace-agent',
|
|
});
|
|
});
|
|
}
|
|
}
|