feat: 工作台聊天室功能

This commit is contained in:
2026-05-31 02:37:23 +08:00
parent 83cf9eab94
commit f83f434863
33 changed files with 2520 additions and 265 deletions

View File

@@ -0,0 +1,178 @@
import type Database from "bun:sqlite";
import { desc, eq } from "drizzle-orm";
import type { Conversation, Message } from "../../shared/api";
import { paginateQuery, wrap } from "./connection";
import { conversations, messages, models } from "./schema";
export function createConversation(
raw: Database,
projectId: string,
defaultModelId?: string,
): { conversation: Conversation } | { error: string; status: number } {
const db = wrap(raw);
let modelId = defaultModelId;
if (!modelId) {
const firstModel = db.select().from(models).limit(1).get();
if (!firstModel) return { error: "没有可用的模型,请先配置模型", status: 400 };
modelId = firstModel.id;
} else {
const model = db.select().from(models).where(eq(models.id, modelId)).get();
if (!model) return { error: "模型不存在", status: 400 };
}
const id = crypto.randomUUID();
const now = new Date().toISOString();
db.insert(conversations)
.values({
createdAt: now,
id,
modelId,
projectId,
title: "新会话",
updatedAt: now,
})
.run();
const row = db.select().from(conversations).where(eq(conversations.id, id)).get();
return { conversation: toConversation(row!) };
}
export function createMessage(
raw: Database,
data: {
content: string;
conversationId: string;
parts?: string;
role: "assistant" | "system" | "user";
},
): Message {
const db = wrap(raw);
const id = crypto.randomUUID();
const now = new Date().toISOString();
db.insert(messages)
.values({
content: data.content,
conversationId: data.conversationId,
createdAt: now,
id,
parts: data.parts ?? null,
role: data.role,
})
.run();
const row = db.select().from(messages).where(eq(messages.id, id)).get();
return toMessage(row!);
}
export function createMessages(
raw: Database,
data: Array<{
content: string;
conversationId: string;
parts?: string;
role: "assistant" | "system" | "user";
}>,
): Message[] {
const db = wrap(raw);
const now = new Date().toISOString();
const results: Message[] = [];
for (const item of data) {
const id = crypto.randomUUID();
db.insert(messages)
.values({
content: item.content,
conversationId: item.conversationId,
createdAt: now,
id,
parts: item.parts ?? null,
role: item.role,
})
.run();
const row = db.select().from(messages).where(eq(messages.id, id)).get();
results.push(toMessage(row!));
}
return results;
}
export function deleteConversation(raw: Database, id: string): { error: string; status: number } | { success: true } {
const db = wrap(raw);
const existing = db.select().from(conversations).where(eq(conversations.id, id)).get();
if (!existing) return { error: "会话不存在", status: 404 };
db.delete(messages).where(eq(messages.conversationId, id)).run();
db.delete(conversations).where(eq(conversations.id, id)).run();
return { success: true };
}
export function getConversation(
raw: Database,
id: string,
): { conversation: Conversation } | { error: string; status: number } {
const db = wrap(raw);
const row = db.select().from(conversations).where(eq(conversations.id, id)).get();
if (!row) return { error: "会话不存在", status: 404 };
return { conversation: toConversation(row) };
}
export function listConversations(
raw: Database,
projectId: string,
options: { page: number; pageSize: number },
): { items: Conversation[]; page: number; pageSize: number; total: number } {
return paginateQuery(raw, conversations, {
conditions: [eq(conversations.projectId, projectId)],
mapRow: toConversation,
orderBy: () => desc(conversations.updatedAt),
page: options.page,
pageSize: options.pageSize,
});
}
export function listMessages(
raw: Database,
conversationId: string,
options: { page: number; pageSize: number },
): { items: Message[]; page: number; pageSize: number; total: number } {
return paginateQuery(raw, messages, {
conditions: [eq(messages.conversationId, conversationId)],
mapRow: toMessage,
orderBy: () => desc(messages.createdAt),
page: options.page,
pageSize: options.pageSize,
});
}
export function updateConversationTimestamp(raw: Database, id: string): void {
const db = wrap(raw);
db.update(conversations).set({ updatedAt: new Date().toISOString() }).where(eq(conversations.id, id)).run();
}
function toConversation(row: typeof conversations.$inferSelect): Conversation {
return {
createdAt: row.createdAt,
id: row.id,
modelId: row.modelId,
projectId: row.projectId,
title: row.title,
updatedAt: row.updatedAt,
};
}
function toMessage(row: typeof messages.$inferSelect): Message {
return {
content: row.content,
conversationId: row.conversationId,
createdAt: row.createdAt,
id: row.id,
parts: row.parts,
role: row.role,
};
}

View File

@@ -1,4 +1,14 @@
export { createDatabase } from "./connection";
export {
createConversation,
createMessage,
createMessages,
deleteConversation,
getConversation,
listConversations,
listMessages,
updateConversationTimestamp,
} from "./conversations";
export { loadMigrationsFromDir, type MigrationRecord } from "./load-migrations";
export { runMigrations } from "./migrate";
export { projects, schemaMigrations } from "./schema";
export { conversations, messages, projects, schemaMigrations } from "./schema";

View File

@@ -45,6 +45,38 @@ export const models = sqliteTable(
],
);
export const conversations = sqliteTable(
"conversations",
{
createdAt: text("created_at").notNull(),
id: text("id").primaryKey(),
modelId: text("model_id")
.notNull()
.references(() => models.id),
projectId: text("project_id")
.notNull()
.references(() => projects.id),
title: text("title").notNull().default("新会话"),
updatedAt: text("updated_at").notNull(),
},
(table) => [index("conversations_project_id_idx").on(table.projectId)],
);
export const messages = sqliteTable(
"messages",
{
content: text("content").notNull().default(""),
conversationId: text("conversation_id")
.notNull()
.references(() => conversations.id, { onDelete: "cascade" }),
createdAt: text("created_at").notNull(),
id: text("id").primaryKey(),
parts: text("parts"),
role: text("role", { enum: ["assistant", "system", "user"] }).notNull(),
},
(table) => [index("messages_conversation_id_idx").on(table.conversationId)],
);
export const schemaMigrations = sqliteTable("schema_migrations", {
appliedAt: text("applied_at").notNull(),
checksum: text("checksum").notNull(),