Files
ai-novel-studio/server/api/projects/[id]/chapters.post.ts
T
jocay 3c9b65d982 AI 小说工坊:主编 + 作家多模型协作写作应用
- Nuxt 4 + Nuxt UI 4 + AI SDK 7 + Drizzle/SQLite
- 主编规划大纲与章节简报、选派作家;作家流式写入章节文件并实时推送前端
- 逐章确认模式(默认)与自动模式;摘要与故事记忆
- 酒馆角色卡(PNG/JSON,V1-V3)与世界书导入
- 自定义最高优先级系统提示词、审稿/修改/多版本、任务日志与提示词检视
- 模拟 LLM 服务与端到端测试脚本

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 02:04:40 +08:00

28 lines
1.1 KiB
TypeScript

import { and, eq, gte, sql } from 'drizzle-orm'
import { z } from 'zod'
const schema = z.object({
title: z.string().default(''),
synopsis: z.string().default(''),
/** 插入位置;不传则追加到末尾 */
index: z.number().int().positive().optional()
})
export default defineEventHandler(async (event) => {
const projectId = param(event)
getProject(projectId)
const body = await readJson(event, schema)
const db = useDb()
const max = db.select({ m: sql<number>`coalesce(max(${tables.chapters.index}), 0)` }).from(tables.chapters).where(eq(tables.chapters.projectId, projectId)).get()?.m ?? 0
const index = body.index && body.index <= max ? body.index : max + 1
const row = db.transaction((tx) => {
if (index <= max) {
tx.update(tables.chapters).set({ index: sql`${tables.chapters.index} + 1` })
.where(and(eq(tables.chapters.projectId, projectId), gte(tables.chapters.index, index))).run()
}
return tx.insert(tables.chapters).values({ id: newId(), projectId, index, title: body.title, synopsis: body.synopsis }).returning().get()
})
emitProject(projectId, { type: 'chapters' })
return row
})