feat(chat): 支持角色继续生成并保留前文

- 新增 continue 模式,不插入用户台词,沿用生成租约、限流和停止逻辑

- 重新生成仅替换末段,并补充真实路由回归和 README 接口说明
This commit is contained in:
湛兮
2026-10-27 18:15:31 +08:00
parent a094dcd22c
commit 17b0668557
5 changed files with 297 additions and 17 deletions
+36 -16
View File
@@ -4,7 +4,11 @@ import { randomUUID } from "node:crypto";
import type { FastifyReply } from "fastify";
import type { PoolConnection } from "mysql2/promise";
import { rows, write, json, transaction } from "./digital.repository";
import { assemblePrompt, type PromptMessage } from "./digital.prompt";
import {
assemblePrompt,
continuationMessages,
type PromptMessage,
} from "./digital.prompt";
import {
contentSchema,
settingsSchema,
@@ -200,7 +204,7 @@ export async function streamChat(
input: {
requestId: string;
content: string;
mode: "send" | "regenerate";
mode: "send" | "regenerate" | "continue";
uiDecisionId?: string;
},
reply: FastifyReply,
@@ -271,9 +275,9 @@ export async function streamChat(
[randomUUID(), id, input.content, input.requestId],
c,
);
} else {
const [last] = await rows<{ role: string }>(
"SELECT role FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT 1",
} else if (input.mode === "regenerate") {
const [last] = await rows<{ role: string; sequence_id: number }>(
"SELECT role,sequence_id FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT 1",
[id],
c,
);
@@ -284,16 +288,30 @@ export async function streamChat(
c,
);
if (!users.length) throw badRequest("请先发送一条消息");
const [anchor] = await rows<{ sequence_id: number }>(
"SELECT sequence_id FROM dh_messages WHERE conversation_id=? AND role='user' ORDER BY sequence_id DESC LIMIT 1",
// 只替换最近一条回答;角色连续说话时,不抹掉此前已经说过的段落。
if (last.role === "assistant")
await write(
"UPDATE dh_messages SET status='superseded' WHERE conversation_id=? AND role='assistant' AND sequence_id=? AND status IN ('complete','stopped','failed')",
[id, last.sequence_id],
c,
);
} else {
const [last] = await rows<{
role: string;
content: string;
status: string;
}>(
"SELECT role,content,status FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT 1",
[id],
c,
);
await write(
"UPDATE dh_messages SET status='superseded' WHERE conversation_id=? AND role='assistant' AND sequence_id>? AND status IN ('complete','stopped','failed')",
[id, anchor.sequence_id],
c,
);
if (
!last ||
last.role !== "assistant" ||
!["complete", "stopped"].includes(last.status) ||
!last.content.trim()
)
throw badRequest("角色还没有可继续的内容,请先开始一段对话");
}
await write(
"INSERT INTO dh_messages(id,conversation_id,role,content,status,request_id) VALUES (?,?,'assistant','','generating',?)",
@@ -352,9 +370,6 @@ export async function streamChat(
[id, assistantId],
);
history.reverse();
if (input.mode === "regenerate") {
while (history.length && history.at(-1)?.role !== "user") history.pop();
}
const pinned = await rows<{ content: string }>(
"SELECT content FROM dh_messages WHERE conversation_id=? AND pinned=TRUE AND content<>'' ORDER BY sequence_id DESC LIMIT 20",
[id],
@@ -375,7 +390,12 @@ export async function streamChat(
},
body: JSON.stringify({
model: "deepseek-flash",
messages: prompt.messages,
// 控制指令仅用于模型请求;数据库中“继续”只新增 assistant 消息。
messages:
input.mode === "continue" ||
(input.mode === "regenerate" && history.at(-1)?.role === "assistant")
? continuationMessages(prompt.messages)
: prompt.messages,
stream: true,
thinking: { type: "disabled" },
temperature: preset.temperature,