Skip to content

An AI-powered search engine with a generative UI

feat: implement Vercel AI SDK v5 beta persistence pattern

Yoshiki Miurae6ba840bce9d
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 46c3384f..93a68426 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -19,16 +19,25 @@ const DEFAULT_MODEL: Model = {
export async function POST(req: Request) {
try {
const body = await req.json()
- const { messages, chatId } = body
+ const { messages, chatId, trigger, messageId } = body
- // Get the last message from the messages array (the new user message)
- const message = messages?.[messages.length - 1]
-
- if (!message) {
- return new Response('No message provided', {
- status: 400,
- statusText: 'Bad Request'
- })
+ // Handle different triggers
+ let message
+
+ if (trigger === 'regenerate-assistant-message') {
+ // For regeneration, we'll fetch the messages from DB and use the last user message
+ // This will be handled in createChatStreamResponse
+ message = null
+ } else {
+ // Get the last message from the messages array (the new user message)
+ message = messages?.[messages.length - 1]
+
+ if (!message) {
+ return new Response('No message provided', {
+ status: 400,
+ statusText: 'Bad Request'
+ })
+ }
}
const referer = req.headers.get('referer')
@@ -74,7 +83,9 @@ export async function POST(req: Request) {
model: selectedModel,
chatId,
searchMode,
- userId: userId!
+ userId: userId!,
+ trigger,
+ messageId
})
} catch (error) {
console.error('API route error:', error)
diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsx
index bee02fdc..29072957 100644
--- a/app/search/[id]/page.tsx
+++ b/app/search/[id]/page.tsx
@@ -2,7 +2,7 @@ import { notFound, redirect } from 'next/navigation'
import { UIMessage } from 'ai'
-import { getChat } from '@/lib/actions/chat-db'
+import { getChat } from '@/lib/actions/chat'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
import { getModels } from '@/lib/config/models'
@@ -43,14 +43,7 @@ export default async function SearchPage(props: {
redirect('/auth/login')
}
- const messages: UIMessage[] = chat.messages.map(message => ({
- id: message.id,
- parts: message.parts as UIMessage['parts'],
- role: message.role as UIMessage['role'],
- metadata: {
- createdAt: new Date(message.createdAt)
- }
- }))
+ const messages: UIMessage[] = chat.messages
const models = await getModels()
return <Chat id={id} savedMessages={messages} models={models} />
diff --git a/bun.lock b/bun.lock
index bd574a00..5449c5e8 100644
--- a/bun.lock
+++ b/bun.lock
@@ -13,6 +13,7 @@
"@ai-sdk/openai": "^2.0.0-beta.11",
"@ai-sdk/react": "^2.0.0-beta.25",
"@ai-sdk/xai": "^2.0.0-beta.10",
+ "@paralleldrive/cuid2": "^2.2.2",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.1.9",
"@radix-ui/react-checkbox": "^1.0.4",
@@ -41,7 +42,7 @@
"clsx": "^2.1.0",
"cmdk": "1.0.0",
"dotenv": "^16.5.0",
- "drizzle-kit": "^0.31.1",
+ "drizzle-kit": "^0.31.4",
"drizzle-orm": "^0.43.1",
"embla-carousel-react": "^8.0.0",
"exa-js": "^1.0.12",
@@ -331,6 +332,8 @@
"@next/swc-win32-x64-msvc": ["@next/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-3PMvF2zRJAifcRNni9uMk/gulWfWS+qVI/pagd+4yLF5bcXPZPPH2xlYRYOsUjmCJOXSTAC2PjRzbhsRzR2fDQ=="],
+ "@noble/hashes": ["@noble/[email protected]", "", {}, "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A=="],
+
"@nodelib/fs.scandir": ["@nodelib/[email protected]", "", { "dependencies": { "@nodelib/fs.stat": "2.0.5", "run-parallel": "^1.1.9" } }, "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g=="],
"@nodelib/fs.stat": ["@nodelib/[email protected]", "", {}, "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A=="],
@@ -341,6 +344,8 @@
"@opentelemetry/api": ["@opentelemetry/[email protected]", "", {}, "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg=="],
+ "@paralleldrive/cuid2": ["@paralleldrive/[email protected]", "", { "dependencies": { "@noble/hashes": "^1.1.5" } }, "sha512-ZOBkgDwEdoYVlSeRbYYXs0S9MejQofiVYoTbKzy/6GQa39/q5tQU2IX46+shYnUkpEl3wc+J6wRlar7r2EK2xA=="],
+
"@pkgjs/parseargs": ["@pkgjs/[email protected]", "", {}, "sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg=="],
"@radix-ui/number": ["@radix-ui/[email protected]", "", {}, "sha512-MkKCwxlXTgz6CFoJx3pCwn07GKp36+aZyu/u2Ln2VrA5DcdyCZkASEDBTd8x5whTQQL5CiYf4prXKLcgQdv29g=="],
@@ -841,7 +846,7 @@
"dotenv": ["[email protected]", "", {}, "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg=="],
- "drizzle-kit": ["[email protected]", "", { "dependencies": { "@drizzle-team/brocli": "^0.10.2", "@esbuild-kit/esm-loader": "^2.5.5", "esbuild": "^0.25.2", "esbuild-register": "^3.5.0" }, "bin": { "drizzle-kit": "bin.cjs" } }, "sha512-PUjYKWtzOzPtdtQlTHQG3qfv4Y0XT8+Eas6UbxCmxTj7qgMf+39dDujf1BP1I+qqZtw9uzwTh8jYtkMuCq+B0Q=="],
+ "drizzle-kit": ["[email protected]", "", { "dependencies": { "@drizzle-team/brocli": "^0.10.2", "@esbuild-kit/esm-loader": "^2.5.5", "esbuild": "^0.25.4", "esbuild-register": "^3.5.0" }, "bin": { "drizzle-kit": "bin.cjs" } }, "sha512-tCPWVZWZqWVx2XUsVpJRnH9Mx0ClVOf5YUHerZ5so1OKSlqww4zy1R5ksEdGRcO3tM3zj0PYN6V48TbQCL1RfA=="],
"drizzle-orm": ["[email protected]", "", { "peerDependencies": { "@aws-sdk/client-rds-data": ">=3", "@cloudflare/workers-types": ">=4", "@electric-sql/pglite": ">=0.2.0", "@libsql/client": ">=0.10.0", "@libsql/client-wasm": ">=0.10.0", "@neondatabase/serverless": ">=0.10.0", "@op-engineering/op-sqlite": ">=2", "@opentelemetry/api": "^1.4.1", "@planetscale/database": ">=1.13", "@prisma/client": "*", "@tidbcloud/serverless": "*", "@types/better-sqlite3": "*", "@types/pg": "*", "@types/sql.js": "*", "@vercel/postgres": ">=0.8.0", "@xata.io/client": "*", "better-sqlite3": ">=7", "bun-types": "*", "expo-sqlite": ">=14.0.0", "gel": ">=2", "knex": "*", "kysely": "*", "mysql2": ">=2", "pg": ">=8", "postgres": ">=3", "sql.js": ">=1", "sqlite3": ">=5" }, "optionalPeers": ["@aws-sdk/client-rds-data", "@cloudflare/workers-types", "@electric-sql/pglite", "@libsql/client", "@libsql/client-wasm", "@neondatabase/serverless", "@op-engineering/op-sqlite", "@opentelemetry/api", "@planetscale/database", "@prisma/client", "@tidbcloud/serverless", "@types/better-sqlite3", "@types/pg", "@types/sql.js", "@vercel/postgres", "@xata.io/client", "better-sqlite3", "bun-types", "expo-sqlite", "gel", "knex", "kysely", "mysql2", "pg", "postgres", "sql.js", "sqlite3"] }, "sha512-dUcDaZtE/zN4RV/xqGrVSMpnEczxd5cIaoDeor7Zst9wOe/HzC/7eAaulywWGYXdDEc9oBPMjayVEDg0ziTLJA=="],
diff --git a/components/chat.tsx b/components/chat.tsx
index abcc92f6..0c10ab94 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -53,8 +53,30 @@ export function Chat({
} = useChat({
transport: new DefaultChatTransport({
api: '/api/chat',
- body: {
- chatId: id
+ prepareSendMessagesRequest: ({ id, messages, trigger, messageId }) => {
+ switch (trigger) {
+ case 'regenerate-assistant-message':
+ // Only send messageId, not message data
+ return {
+ body: {
+ trigger: 'regenerate-assistant-message',
+ chatId: id,
+ messageId,
+ },
+ }
+
+ case 'submit-user-message':
+ default:
+ // Only send the last message
+ return {
+ body: {
+ trigger: 'submit-user-message',
+ chatId: id,
+ messages: [messages[messages.length - 1]],
+ messageId,
+ },
+ }
+ }
}
}),
messages: savedMessages,
diff --git a/drizzle/0001_busy_katie_power.sql b/drizzle/0001_busy_katie_power.sql
new file mode 100644
index 00000000..a7797928
--- /dev/null
+++ b/drizzle/0001_busy_katie_power.sql
@@ -0,0 +1,58 @@
+CREATE TABLE "parts" (
+ "id" varchar PRIMARY KEY NOT NULL,
+ "message_id" varchar NOT NULL,
+ "order" integer NOT NULL,
+ "type" varchar(256) NOT NULL,
+ "text_text" text,
+ "reasoning_text" text,
+ "file_media_type" varchar(256),
+ "file_filename" varchar(1024),
+ "file_url" text,
+ "source_url_source_id" varchar(256),
+ "source_url_url" text,
+ "source_url_title" text,
+ "source_document_source_id" varchar(256),
+ "source_document_media_type" varchar(256),
+ "source_document_title" text,
+ "source_document_filename" varchar(1024),
+ "source_document_url" text,
+ "source_document_snippet" text,
+ "tool_tool_call_id" varchar(256),
+ "tool_state" varchar(256),
+ "tool_error_text" text,
+ "tool_search_input" json,
+ "tool_search_output" json,
+ "tool_retrieve_input" json,
+ "tool_retrieve_output" json,
+ "tool_question_input" json,
+ "tool_question_output" json,
+ "tool_video_search_input" json,
+ "tool_video_search_output" json,
+ "tool_mcp_input" json,
+ "tool_mcp_output" json,
+ "data_prefix" varchar(256),
+ "data_content" json,
+ "data_id" varchar(256),
+ "provider_metadata" json,
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
+ CONSTRAINT "text_text_required" CHECK ((type != 'text' OR text_text IS NOT NULL)),
+ CONSTRAINT "reasoning_text_required" CHECK ((type != 'reasoning' OR reasoning_text IS NOT NULL)),
+ CONSTRAINT "file_fields_required" CHECK ((type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL))),
+ CONSTRAINT "tool_state_valid" CHECK ((tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'))),
+ CONSTRAINT "tool_fields_required" CHECK ((type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL)))
+);
+--> statement-breakpoint
+ALTER TABLE "chats" ALTER COLUMN "id" SET DATA TYPE varchar;--> statement-breakpoint
+ALTER TABLE "chats" ALTER COLUMN "id" DROP DEFAULT;--> statement-breakpoint
+ALTER TABLE "chats" ALTER COLUMN "user_id" SET DATA TYPE varchar;--> statement-breakpoint
+ALTER TABLE "messages" ALTER COLUMN "id" SET DATA TYPE varchar;--> statement-breakpoint
+ALTER TABLE "messages" ALTER COLUMN "id" DROP DEFAULT;--> statement-breakpoint
+ALTER TABLE "messages" ALTER COLUMN "chat_id" SET DATA TYPE varchar;--> statement-breakpoint
+ALTER TABLE "parts" ADD CONSTRAINT "parts_message_id_messages_id_fk" FOREIGN KEY ("message_id") REFERENCES "public"."messages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
+CREATE INDEX "parts_message_id_idx" ON "parts" USING btree ("message_id");--> statement-breakpoint
+CREATE INDEX "parts_message_id_order_idx" ON "parts" USING btree ("message_id","order");--> statement-breakpoint
+CREATE INDEX "messages_chat_id_idx" ON "messages" USING btree ("chat_id");--> statement-breakpoint
+CREATE INDEX "messages_chat_id_created_at_idx" ON "messages" USING btree ("chat_id","created_at");--> statement-breakpoint
+ALTER TABLE "chats" DROP COLUMN "updated_at";--> statement-breakpoint
+ALTER TABLE "messages" DROP COLUMN "parts";--> statement-breakpoint
+ALTER TABLE "messages" DROP COLUMN "updated_at";
\ No newline at end of file
diff --git a/drizzle/meta/0001_snapshot.json b/drizzle/meta/0001_snapshot.json
new file mode 100644
index 00000000..487a1769
--- /dev/null
+++ b/drizzle/meta/0001_snapshot.json
@@ -0,0 +1,455 @@
+{
+ "id": "d4c5910e-59c9-4189-b010-22838368df2b",
+ "prevId": "93a8bb81-9754-43af-8fb2-abbedc59c89b",
+ "version": "7",
+ "dialect": "postgresql",
+ "tables": {
+ "public.chats": {
+ "name": "chats",
+ "schema": "",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "varchar",
+ "primaryKey": true,
+ "notNull": true
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "timestamp with time zone",
+ "primaryKey": false,
+ "notNull": true,
+ "default": "now()"
+ },
+ "title": {
+ "name": "title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "user_id": {
+ "name": "user_id",
+ "type": "varchar",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "visibility": {
+ "name": "visibility",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": true,
+ "default": "'private'"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "policies": {},
+ "checkConstraints": {},
+ "isRLSEnabled": false
+ },
+ "public.messages": {
+ "name": "messages",
+ "schema": "",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "varchar",
+ "primaryKey": true,
+ "notNull": true
+ },
+ "chat_id": {
+ "name": "chat_id",
+ "type": "varchar",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "role": {
+ "name": "role",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "timestamp with time zone",
+ "primaryKey": false,
+ "notNull": true,
+ "default": "now()"
+ }
+ },
+ "indexes": {
+ "messages_chat_id_idx": {
+ "name": "messages_chat_id_idx",
+ "columns": [
+ {
+ "expression": "chat_id",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ }
+ ],
+ "isUnique": false,
+ "concurrently": false,
+ "method": "btree",
+ "with": {}
+ },
+ "messages_chat_id_created_at_idx": {
+ "name": "messages_chat_id_created_at_idx",
+ "columns": [
+ {
+ "expression": "chat_id",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ },
+ {
+ "expression": "created_at",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ }
+ ],
+ "isUnique": false,
+ "concurrently": false,
+ "method": "btree",
+ "with": {}
+ }
+ },
+ "foreignKeys": {
+ "messages_chat_id_chats_id_fk": {
+ "name": "messages_chat_id_chats_id_fk",
+ "tableFrom": "messages",
+ "tableTo": "chats",
+ "columnsFrom": [
+ "chat_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "policies": {},
+ "checkConstraints": {},
+ "isRLSEnabled": false
+ },
+ "public.parts": {
+ "name": "parts",
+ "schema": "",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "varchar",
+ "primaryKey": true,
+ "notNull": true
+ },
+ "message_id": {
+ "name": "message_id",
+ "type": "varchar",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "order": {
+ "name": "order",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "type": {
+ "name": "type",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": true
+ },
+ "text_text": {
+ "name": "text_text",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "reasoning_text": {
+ "name": "reasoning_text",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "file_media_type": {
+ "name": "file_media_type",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "file_filename": {
+ "name": "file_filename",
+ "type": "varchar(1024)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "file_url": {
+ "name": "file_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_url_source_id": {
+ "name": "source_url_source_id",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_url_url": {
+ "name": "source_url_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_url_title": {
+ "name": "source_url_title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_source_id": {
+ "name": "source_document_source_id",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_media_type": {
+ "name": "source_document_media_type",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_title": {
+ "name": "source_document_title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_filename": {
+ "name": "source_document_filename",
+ "type": "varchar(1024)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_url": {
+ "name": "source_document_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "source_document_snippet": {
+ "name": "source_document_snippet",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_tool_call_id": {
+ "name": "tool_tool_call_id",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_state": {
+ "name": "tool_state",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_error_text": {
+ "name": "tool_error_text",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_search_input": {
+ "name": "tool_search_input",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_search_output": {
+ "name": "tool_search_output",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_retrieve_input": {
+ "name": "tool_retrieve_input",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_retrieve_output": {
+ "name": "tool_retrieve_output",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_question_input": {
+ "name": "tool_question_input",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_question_output": {
+ "name": "tool_question_output",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_video_search_input": {
+ "name": "tool_video_search_input",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_video_search_output": {
+ "name": "tool_video_search_output",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_mcp_input": {
+ "name": "tool_mcp_input",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "tool_mcp_output": {
+ "name": "tool_mcp_output",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "data_prefix": {
+ "name": "data_prefix",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "data_content": {
+ "name": "data_content",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "data_id": {
+ "name": "data_id",
+ "type": "varchar(256)",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "provider_metadata": {
+ "name": "provider_metadata",
+ "type": "json",
+ "primaryKey": false,
+ "notNull": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "timestamp with time zone",
+ "primaryKey": false,
+ "notNull": true,
+ "default": "now()"
+ }
+ },
+ "indexes": {
+ "parts_message_id_idx": {
+ "name": "parts_message_id_idx",
+ "columns": [
+ {
+ "expression": "message_id",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ }
+ ],
+ "isUnique": false,
+ "concurrently": false,
+ "method": "btree",
+ "with": {}
+ },
+ "parts_message_id_order_idx": {
+ "name": "parts_message_id_order_idx",
+ "columns": [
+ {
+ "expression": "message_id",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ },
+ {
+ "expression": "order",
+ "isExpression": false,
+ "asc": true,
+ "nulls": "last"
+ }
+ ],
+ "isUnique": false,
+ "concurrently": false,
+ "method": "btree",
+ "with": {}
+ }
+ },
+ "foreignKeys": {
+ "parts_message_id_messages_id_fk": {
+ "name": "parts_message_id_messages_id_fk",
+ "tableFrom": "parts",
+ "tableTo": "messages",
+ "columnsFrom": [
+ "message_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "policies": {},
+ "checkConstraints": {
+ "text_text_required": {
+ "name": "text_text_required",
+ "value": "(type != 'text' OR text_text IS NOT NULL)"
+ },
+ "reasoning_text_required": {
+ "name": "reasoning_text_required",
+ "value": "(type != 'reasoning' OR reasoning_text IS NOT NULL)"
+ },
+ "file_fields_required": {
+ "name": "file_fields_required",
+ "value": "(type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL))"
+ },
+ "tool_state_valid": {
+ "name": "tool_state_valid",
+ "value": "(tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'))"
+ },
+ "tool_fields_required": {
+ "name": "tool_fields_required",
+ "value": "(type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL))"
+ }
+ },
+ "isRLSEnabled": false
+ }
+ },
+ "enums": {},
+ "schemas": {},
+ "sequences": {},
+ "roles": {},
+ "policies": {},
+ "views": {},
+ "_meta": {
+ "columns": {},
+ "schemas": {},
+ "tables": {}
+ }
+}
\ No newline at end of file
diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json
index 5c6a10c5..9e010182 100644
--- a/drizzle/meta/_journal.json
+++ b/drizzle/meta/_journal.json
@@ -8,6 +8,13 @@
"when": 1747030742056,
"tag": "0000_melodic_unicorn",
"breakpoints": true
+ },
+ {
+ "idx": 1,
+ "version": "7",
+ "when": 1753524626533,
+ "tag": "0001_busy_katie_power",
+ "breakpoints": true
}
]
-}
+}
\ No newline at end of file
diff --git a/lib/actions/__tests__/chat-db.test.ts b/lib/actions/__tests__/chat-db.test.ts
index 80e8eaa3..d98335f8 100644
--- a/lib/actions/__tests__/chat-db.test.ts
+++ b/lib/actions/__tests__/chat-db.test.ts
@@ -63,8 +63,6 @@ const testMessageData: DBMessage = {
id: commonMessageId,
chatId: commonChatId,
role: 'user',
- parts: [{ type: 'text', content: 'Hello' }],
- attachments: [], // Added attachments as it is non-nullable in schema
createdAt: now
}
@@ -348,8 +346,7 @@ describe('Chat Actions - saveSingleMessage', () => {
...testMessageData,
id: messageId,
chatId: chatId,
- role: messageInput.role,
- parts: messageInput.parts
+ role: messageInput.role
}
})
@@ -388,7 +385,7 @@ describe('Chat Actions - saveSingleMessage', () => {
describe('Chat Actions - saveChatMessage', () => {
let userId: string
let chatId: string
- let userMessageInput: { id: string; role: string; parts: any[] }
+ let userMessageInput: any
let newChatData: DBChat
let newMessageData: DBMessage
@@ -412,7 +409,6 @@ describe('Chat Actions - saveChatMessage', () => {
...testMessageData,
id: userMessageInput.id,
chatId: chatId,
- parts: userMessageInput.parts,
role: userMessageInput.role
}
})
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts
index 8b325558..affc4ca2 100644
--- a/lib/actions/chat-db.ts
+++ b/lib/actions/chat-db.ts
@@ -169,8 +169,8 @@ interface ClientChatInput {
// Interface for new message input from the client
interface ClientNewMessageInput {
- role: DBMessage['role']
- parts: DBMessage['parts']
+ role: UIMessage['role']
+ parts: UIMessage['parts']
}
// Save a single message
diff --git a/lib/actions/chat.ts b/lib/actions/chat.ts
new file mode 100644
index 00000000..e7b4fd29
--- /dev/null
+++ b/lib/actions/chat.ts
@@ -0,0 +1,235 @@
+'use server'
+
+import { revalidateTag, unstable_cache } from 'next/cache'
+
+import { getCurrentUserId } from '@/lib/auth/get-current-user'
+import * as dbActions from '@/lib/db/actions'
+import type { Chat, Message } from '@/lib/db/schema'
+import { generateId } from '@/lib/db/schema'
+import type { UIMessage } from '@/lib/types/ai'
+import { getTextFromParts } from '@/lib/utils/message-utils'
+
+/**
+ * Get all chats for the current user
+ */
+export async function getChats() {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return []
+ }
+ return dbActions.getChats(userId)
+}
+
+/**
+ * Get a single chat with caching
+ */
+const getChatUncached = async function (
+ chatId: string,
+ requestingUserId?: string
+): Promise<(Chat & { messages: UIMessage[] }) | null> {
+ const chat = await dbActions.getChat(chatId, requestingUserId)
+ if (!chat) {
+ return null
+ }
+
+ const messages = await dbActions.loadChat(chatId)
+ return { ...chat, messages }
+}
+
+// Create a cached version for each chatId
+const createCachedGetChat = (chatId: string) =>
+ unstable_cache(
+ (id: string, userId?: string) => getChatUncached(id, userId),
+ [`chat-${chatId}`],
+ {
+ revalidate: 300, // 5 minutes cache
+ tags: [`chat-${chatId}`, 'chat']
+ }
+ )
+
+/**
+ * Get a chat with messages (cached)
+ */
+export async function getChat(
+ chatId: string,
+ requestingUserId?: string
+): Promise<(Chat & { messages: UIMessage[] }) | null> {
+ const cachedGetChat = createCachedGetChat(chatId)
+ return cachedGetChat(chatId, requestingUserId)
+}
+
+/**
+ * Create a new chat and save the first message
+ */
+export async function createChatAndSaveMessage(
+ message: UIMessage,
+ title?: string
+): Promise<{ chat: Chat; message: Message }> {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ throw new Error('User not authenticated')
+ }
+
+ const chatId = generateId()
+ const messageId = message.id || generateId()
+
+ // Extract title from message if not provided
+ const chatTitle = title || getTextFromParts(message.parts as any[]) || 'New Chat'
+
+ // Create chat
+ const chat = await dbActions.createChat({
+ id: chatId,
+ title: chatTitle.substring(0, 255),
+ userId,
+ visibility: 'private'
+ })
+
+ // Save message
+ const dbMessage = await dbActions.upsertMessage({
+ ...message,
+ id: messageId,
+ chatId
+ })
+
+ // Revalidate cache
+ revalidateTag(`chat-${chatId}`)
+
+ return { chat, message: dbMessage }
+}
+
+/**
+ * Save a message to an existing chat
+ */
+export async function saveMessage(
+ chatId: string,
+ message: UIMessage
+): Promise<Message> {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ throw new Error('User not authenticated')
+ }
+
+ // Verify access
+ const chat = await dbActions.getChat(chatId, userId)
+ if (!chat) {
+ throw new Error('Chat not found or unauthorized')
+ }
+
+ const messageId = message.id || generateId()
+ const dbMessage = await dbActions.upsertMessage({
+ ...message,
+ id: messageId,
+ chatId
+ })
+
+ // Revalidate cache
+ revalidateTag(`chat-${chatId}`)
+
+ return dbMessage
+}
+
+/**
+ * Delete a chat
+ */
+export async function deleteChat(chatId: string) {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return { error: 'User not authenticated' }
+ }
+
+ const result = await dbActions.deleteChat(chatId, userId)
+
+ if (result.success) {
+ revalidateTag(`chat-${chatId}`)
+ revalidateTag('chat')
+ }
+
+ return result
+}
+
+/**
+ * Clear all chats for the current user
+ */
+export async function clearChats() {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return { error: 'User not authenticated' }
+ }
+
+ const chats = await dbActions.getChats(userId)
+
+ for (const chat of chats) {
+ await dbActions.deleteChat(chat.id, userId)
+ }
+
+ revalidateTag('chat')
+ return { success: true }
+}
+
+/**
+ * Delete messages after a specific message
+ */
+export async function deleteMessagesAfter(
+ chatId: string,
+ messageId: string
+) {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return { error: 'User not authenticated' }
+ }
+
+ // Verify access
+ const chat = await dbActions.getChat(chatId, userId)
+ if (!chat || chat.userId !== userId) {
+ return { error: 'Unauthorized' }
+ }
+
+ const result = await dbActions.deleteMessagesAfter(chatId, messageId)
+
+ revalidateTag(`chat-${chatId}`)
+
+ return { success: true, count: result.count }
+}
+
+/**
+ * Share a chat (make it public)
+ */
+export async function shareChat(chatId: string) {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return null
+ }
+
+ const updatedChat = await dbActions.updateChatVisibility(chatId, userId, 'public')
+
+ if (updatedChat) {
+ revalidateTag(`chat-${chatId}`)
+ }
+
+ return updatedChat
+}
+
+/**
+ * Delete messages from a specific message index
+ */
+export async function deleteMessagesFromIndex(
+ chatId: string,
+ messageId: string
+) {
+ const userId = await getCurrentUserId()
+ if (!userId) {
+ return { error: 'User not authenticated' }
+ }
+
+ // Verify access
+ const chat = await dbActions.getChat(chatId, userId)
+ if (!chat || chat.userId !== userId) {
+ return { error: 'Unauthorized' }
+ }
+
+ const result = await dbActions.deleteMessagesFromIndex(chatId, messageId)
+
+ revalidateTag(`chat-${chatId}`)
+
+ return { success: true, count: result.count }
+}
\ No newline at end of file
diff --git a/lib/db/actions.ts b/lib/db/actions.ts
new file mode 100644
index 00000000..e1ddf151
--- /dev/null
+++ b/lib/db/actions.ts
@@ -0,0 +1,250 @@
+'use server'
+
+import { and, asc, desc,eq, gt, inArray } from 'drizzle-orm'
+
+import type { UIMessage } from '@/lib/types/ai'
+import type { PersistableUIMessage } from '@/lib/types/message-persistence'
+import {
+ buildUIMessageFromDB,
+ mapUIMessagePartsToDBParts,
+ mapUIMessageToDBMessage} from '@/lib/utils/message-mapping'
+
+import type { Chat, Message } from './schema'
+import { chats, generateId,messages, parts } from './schema'
+import { db } from '.'
+
+/**
+ * Create a new chat
+ */
+export async function createChat({
+ id = generateId(),
+ title,
+ userId,
+ visibility = 'private'
+}: {
+ id?: string
+ title: string
+ userId: string
+ visibility?: 'public' | 'private'
+}): Promise<Chat> {
+ const [chat] = await db
+ .insert(chats)
+ .values({
+ id,
+ title,
+ userId,
+ visibility
+ })
+ .returning()
+
+ return chat
+}
+
+/**
+ * Get chat by ID with permission check
+ */
+export async function getChat(
+ chatId: string,
+ userId?: string
+): Promise<Chat | null> {
+ const [chat] = await db
+ .select()
+ .from(chats)
+ .where(eq(chats.id, chatId))
+ .limit(1)
+
+ if (!chat) {
+ return null
+ }
+
+ // Permission check
+ if (chat.visibility === 'public') {
+ return chat
+ }
+
+ if (chat.visibility === 'private' && userId && chat.userId === userId) {
+ return chat
+ }
+
+ return null
+}
+
+/**
+ * Upsert a message with its parts
+ */
+export async function upsertMessage(
+ message: PersistableUIMessage & { chatId: string }
+): Promise<Message> {
+ return await db.transaction(async (tx) => {
+ // 1. Insert or update the message
+ const messageData = mapUIMessageToDBMessage(message)
+ const [dbMessage] = await tx
+ .insert(messages)
+ .values(messageData)
+ .onConflictDoUpdate({
+ target: messages.id,
+ set: { role: messageData.role },
+ })
+ .returning()
+
+ // 2. Delete existing parts
+ await tx.delete(parts).where(eq(parts.messageId, message.id))
+
+ // 3. Insert new parts
+ if (message.parts && message.parts.length > 0) {
+ const dbParts = mapUIMessagePartsToDBParts(message.parts, message.id)
+ if (dbParts.length > 0) {
+ await tx.insert(parts).values(dbParts)
+ }
+ }
+
+ return dbMessage
+ })
+}
+
+/**
+ * Load chat messages with parts
+ */
+export async function loadChat(chatId: string): Promise<UIMessage[]> {
+ // Use Drizzle's query API with relations
+ const result = await db.query.messages.findMany({
+ where: eq(messages.chatId, chatId),
+ with: {
+ parts: {
+ orderBy: [asc(parts.order)],
+ },
+ },
+ orderBy: [asc(messages.createdAt)],
+ })
+
+ // Convert to UI format
+ return result.map(msg => buildUIMessageFromDB(msg, msg.parts))
+}
+
+/**
+ * Delete messages after a specific message
+ */
+export async function deleteMessagesAfter(
+ chatId: string,
+ messageId: string
+): Promise<{ count: number }> {
+ // Get the message's timestamp
+ const [targetMessage] = await db
+ .select({ createdAt: messages.createdAt })
+ .from(messages)
+ .where(eq(messages.id, messageId))
+ .limit(1)
+
+ if (!targetMessage) {
+ return { count: 0 }
+ }
+
+ // Find messages to delete
+ const messagesToDelete = await db
+ .select({ id: messages.id })
+ .from(messages)
+ .where(
+ and(
+ eq(messages.chatId, chatId),
+ gt(messages.createdAt, targetMessage.createdAt)
+ )
+ )
+
+ const messageIds = messagesToDelete.map(m => m.id)
+
+ if (messageIds.length > 0) {
+ // Delete messages (parts will be cascade deleted)
+ await db.delete(messages).where(inArray(messages.id, messageIds))
+ }
+
+ return { count: messageIds.length }
+}
+
+/**
+ * Delete messages from a specific index
+ */
+export async function deleteMessagesFromIndex(
+ chatId: string,
+ messageId: string
+): Promise<{ count: number }> {
+ // Get all messages for the chat
+ const allMessages = await db
+ .select({ id: messages.id, createdAt: messages.createdAt })
+ .from(messages)
+ .where(eq(messages.chatId, chatId))
+ .orderBy(asc(messages.createdAt))
+
+ // Find the index of the target message
+ const messageIndex = allMessages.findIndex(m => m.id === messageId)
+
+ if (messageIndex === -1) {
+ return { count: 0 }
+ }
+
+ // Get messages to delete (from index onwards)
+ const messagesToDelete = allMessages.slice(messageIndex)
+ const messageIds = messagesToDelete.map(m => m.id)
+
+ if (messageIds.length > 0) {
+ await db.delete(messages).where(inArray(messages.id, messageIds))
+ }
+
+ return { count: messageIds.length }
+}
+
+/**
+ * Get all chats for a user
+ */
+export async function getChats(userId: string): Promise<Chat[]> {
+ return db
+ .select()
+ .from(chats)
+ .where(eq(chats.userId, userId))
+ .orderBy(desc(chats.createdAt))
+}
+
+/**
+ * Delete a chat
+ */
+export async function deleteChat(
+ chatId: string,
+ userId: string
+): Promise<{ success: boolean; error?: string }> {
+ try {
+ // Verify ownership
+ const chat = await getChat(chatId, userId)
+ if (!chat || chat.userId !== userId) {
+ return { success: false, error: 'Unauthorized' }
+ }
+
+ // Delete the chat (messages and parts will cascade)
+ await db.delete(chats).where(eq(chats.id, chatId))
+
+ return { success: true }
+ } catch (error) {
+ console.error('Error deleting chat:', error)
+ return { success: false, error: 'Failed to delete chat' }
+ }
+}
+
+/**
+ * Update chat visibility
+ */
+export async function updateChatVisibility(
+ chatId: string,
+ userId: string,
+ visibility: 'public' | 'private'
+): Promise<Chat | null> {
+ const chat = await getChat(chatId, userId)
+ if (!chat || chat.userId !== userId) {
+ return null
+ }
+
+ const [updatedChat] = await db
+ .update(chats)
+ .set({ visibility })
+ .where(eq(chats.id, chatId))
+ .returning()
+
+ return updatedChat
+}
\ No newline at end of file
diff --git a/lib/db/index.ts b/lib/db/index.ts
index b81415c0..dc27af9e 100644
--- a/lib/db/index.ts
+++ b/lib/db/index.ts
@@ -1,6 +1,7 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
+import * as relations from './relations'
import * as schema from './schema'
// For server-side usage only
@@ -16,7 +17,9 @@ const client = postgres(connectionString, {
max: 20 // Max 20 connections
})
-export const db = drizzle(client, { schema })
+export const db = drizzle(client, {
+ schema: { ...schema, ...relations }
+})
// Helper type for all tables
export type Schema = typeof schema
diff --git a/lib/db/migrations/0001_persistence_v5.sql b/lib/db/migrations/0001_persistence_v5.sql
new file mode 100644
index 00000000..515c13d4
--- /dev/null
+++ b/lib/db/migrations/0001_persistence_v5.sql
@@ -0,0 +1,108 @@
+-- Drop existing tables if they exist (clean slate approach)
+DROP TABLE IF EXISTS messages CASCADE;
+DROP TABLE IF EXISTS chats CASCADE;
+
+-- Create new chats table with generateId
+CREATE TABLE chats (
+ id VARCHAR PRIMARY KEY,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
+ title TEXT NOT NULL,
+ user_id VARCHAR NOT NULL,
+ visibility VARCHAR(256) DEFAULT 'private' NOT NULL CHECK (visibility IN ('public', 'private'))
+);
+
+-- Create new messages table (simplified)
+CREATE TABLE messages (
+ id VARCHAR PRIMARY KEY,
+ chat_id VARCHAR NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
+ role VARCHAR(256) NOT NULL,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
+);
+
+-- Create indexes for messages
+CREATE INDEX messages_chat_id_idx ON messages(chat_id);
+CREATE INDEX messages_chat_id_created_at_idx ON messages(chat_id, created_at);
+
+-- Create parts table
+CREATE TABLE parts (
+ id VARCHAR PRIMARY KEY,
+ message_id VARCHAR NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
+ "order" INTEGER NOT NULL,
+ type VARCHAR(256) NOT NULL,
+
+ -- Text parts
+ text_text TEXT,
+
+ -- Reasoning parts
+ reasoning_text TEXT,
+
+ -- File parts
+ file_media_type VARCHAR(256),
+ file_filename VARCHAR(1024),
+ file_url TEXT,
+
+ -- Source URL parts
+ source_url_source_id VARCHAR(256),
+ source_url_url TEXT,
+ source_url_title TEXT,
+
+ -- Source document parts
+ source_document_source_id VARCHAR(256),
+ source_document_media_type VARCHAR(256),
+ source_document_title TEXT,
+ source_document_filename VARCHAR(1024),
+ source_document_url TEXT,
+ source_document_snippet TEXT,
+
+ -- Tool parts (generic)
+ tool_tool_call_id VARCHAR(256),
+ tool_state VARCHAR(256),
+ tool_error_text TEXT,
+
+ -- Tool-specific columns
+ tool_search_input JSONB,
+ tool_search_output JSONB,
+ tool_retrieve_input JSONB,
+ tool_retrieve_output JSONB,
+ tool_question_input JSONB,
+ tool_question_output JSONB,
+ tool_video_search_input JSONB,
+ tool_video_search_output JSONB,
+ tool_mcp_input JSONB,
+ tool_mcp_output JSONB,
+
+ -- Data parts
+ data_prefix VARCHAR(256),
+ data_content JSONB,
+ data_id VARCHAR(256),
+
+ -- Provider metadata
+ provider_metadata JSONB,
+
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
+);
+
+-- Create indexes for parts
+CREATE INDEX parts_message_id_idx ON parts(message_id);
+CREATE INDEX parts_message_id_order_idx ON parts(message_id, "order");
+
+-- Add constraints
+ALTER TABLE parts ADD CONSTRAINT text_text_required
+ CHECK (type != 'text' OR text_text IS NOT NULL);
+
+ALTER TABLE parts ADD CONSTRAINT reasoning_text_required
+ CHECK (type != 'reasoning' OR reasoning_text IS NOT NULL);
+
+ALTER TABLE parts ADD CONSTRAINT file_fields_required
+ CHECK (type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL));
+
+ALTER TABLE parts ADD CONSTRAINT tool_state_valid
+ CHECK (tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'));
+
+ALTER TABLE parts ADD CONSTRAINT tool_fields_required
+ CHECK (type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL));
+
+-- Enable Row Level Security (if needed)
+-- ALTER TABLE chats ENABLE ROW LEVEL SECURITY;
+-- ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
+-- ALTER TABLE parts ENABLE ROW LEVEL SECURITY;
\ No newline at end of file
diff --git a/lib/db/relations.ts b/lib/db/relations.ts
new file mode 100644
index 00000000..67a8705a
--- /dev/null
+++ b/lib/db/relations.ts
@@ -0,0 +1,22 @@
+import { relations } from 'drizzle-orm'
+
+import { chats, messages, parts } from './schema'
+
+export const chatsRelations = relations(chats, ({ many }) => ({
+ messages: many(messages),
+}))
+
+export const messagesRelations = relations(messages, ({ one, many }) => ({
+ chat: one(chats, {
+ fields: [messages.chatId],
+ references: [chats.id],
+ }),
+ parts: many(parts),
+}))
+
+export const partsRelations = relations(parts, ({ one }) => ({
+ message: one(messages, {
+ fields: [parts.messageId],
+ references: [messages.id],
+ }),
+}))
\ No newline at end of file
diff --git a/lib/db/schema.ts b/lib/db/schema.ts
index ca8aa26a..2f48b555 100644
--- a/lib/db/schema.ts
+++ b/lib/db/schema.ts
@@ -1,22 +1,28 @@
-import { InferSelectModel } from 'drizzle-orm'
+import { createId } from '@paralleldrive/cuid2'
+import { InferSelectModel, sql } from 'drizzle-orm'
import {
+ check,
+ index,
+ integer,
json,
pgTable,
text,
timestamp,
- uuid,
- varchar
-} from 'drizzle-orm/pg-core'
+ varchar} from 'drizzle-orm/pg-core'
+// ID generation function
+export const generateId = () => createId()
+
+// Chats table
export const chats = pgTable(
'chats',
{
- id: uuid('id').primaryKey().notNull().defaultRandom(),
+ id: varchar('id').primaryKey().$defaultFn(() => generateId()),
createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
.notNull()
.defaultNow(),
title: text('title').notNull(),
- userId: uuid('user_id').notNull(), // User ID from the authentication system
+ userId: varchar('user_id').notNull(),
visibility: varchar('visibility', {
length: 256,
enum: ['public', 'private']
@@ -33,25 +39,106 @@ export const chats = pgTable(
export type Chat = InferSelectModel<typeof chats>
+// Messages table (simplified)
export const messages = pgTable(
'messages',
{
- id: uuid('id').primaryKey().notNull().defaultRandom(),
- chatId: uuid('chat_id')
+ id: varchar('id').primaryKey().$defaultFn(() => generateId()),
+ chatId: varchar('chat_id')
.notNull()
.references(() => chats.id, { onDelete: 'cascade' }),
role: varchar('role', { length: 256 }).notNull(),
- parts: json('parts').notNull(), // Stores structured message content (e.g., Vercel AI SDK Message parts)
- attachments: json('attachments').notNull(),
createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
.notNull()
.defaultNow()
},
- table => {
- return {
- enableRls: true
- }
- }
+ table => ({
+ chatIdIdx: index('messages_chat_id_idx').on(table.chatId),
+ chatIdCreatedAtIdx: index('messages_chat_id_created_at_idx').on(table.chatId, table.createdAt),
+ enableRls: true
+ })
)
export type Message = InferSelectModel<typeof messages>
+
+// Parts table
+export const parts = pgTable(
+ 'parts',
+ {
+ id: varchar('id').primaryKey().$defaultFn(() => generateId()),
+ messageId: varchar('message_id')
+ .notNull()
+ .references(() => messages.id, { onDelete: 'cascade' }),
+ order: integer('order').notNull(),
+ type: varchar('type', { length: 256 }).notNull(),
+
+ // Text parts
+ text_text: text('text_text'),
+
+ // Reasoning parts
+ reasoning_text: text('reasoning_text'),
+
+ // File parts
+ file_mediaType: varchar('file_media_type', { length: 256 }),
+ file_filename: varchar('file_filename', { length: 1024 }),
+ file_url: text('file_url'),
+
+ // Source URL parts
+ source_url_sourceId: varchar('source_url_source_id', { length: 256 }),
+ source_url_url: text('source_url_url'),
+ source_url_title: text('source_url_title'),
+
+ // Source document parts
+ source_document_sourceId: varchar('source_document_source_id', { length: 256 }),
+ source_document_mediaType: varchar('source_document_media_type', { length: 256 }),
+ source_document_title: text('source_document_title'),
+ source_document_filename: varchar('source_document_filename', { length: 1024 }),
+ source_document_url: text('source_document_url'),
+ source_document_snippet: text('source_document_snippet'),
+
+ // Tool parts (generic)
+ tool_toolCallId: varchar('tool_tool_call_id', { length: 256 }),
+ tool_state: varchar('tool_state', { length: 256 }),
+ tool_errorText: text('tool_error_text'),
+
+ // Tool-specific columns (all Morphic tools)
+ tool_search_input: json('tool_search_input').$type<any>(),
+ tool_search_output: json('tool_search_output').$type<any>(),
+ tool_retrieve_input: json('tool_retrieve_input').$type<any>(),
+ tool_retrieve_output: json('tool_retrieve_output').$type<any>(),
+ tool_question_input: json('tool_question_input').$type<any>(),
+ tool_question_output: json('tool_question_output').$type<any>(),
+ tool_videoSearch_input: json('tool_video_search_input').$type<any>(),
+ tool_videoSearch_output: json('tool_video_search_output').$type<any>(),
+ tool_mcp_input: json('tool_mcp_input').$type<any>(),
+ tool_mcp_output: json('tool_mcp_output').$type<any>(),
+
+ // Data parts (generic support)
+ data_prefix: varchar('data_prefix', { length: 256 }),
+ data_content: json('data_content').$type<any>(),
+ data_id: varchar('data_id', { length: 256 }),
+
+ // Provider metadata
+ providerMetadata: json('provider_metadata').$type<Record<string, any>>(),
+
+ createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ .notNull()
+ .defaultNow()
+ },
+ table => ({
+ // Indexes
+ messageIdIdx: index('parts_message_id_idx').on(table.messageId),
+ messageIdOrderIdx: index('parts_message_id_order_idx').on(table.messageId, table.order),
+ enableRls: true,
+
+ // Constraints
+ textTextRequired: check('text_text_required', sql`(type != 'text' OR text_text IS NOT NULL)`),
+ reasoningTextRequired: check('reasoning_text_required', sql`(type != 'reasoning' OR reasoning_text IS NOT NULL)`),
+ fileFieldsRequired: check('file_fields_required', sql`(type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL))`),
+ toolStateValid: check('tool_state_valid', sql`(tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'))`),
+ toolFieldsRequired: check('tool_fields_required', sql`(type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL))`)
+ })
+)
+
+export type Part = InferSelectModel<typeof parts>
+export type NewPart = typeof parts.$inferInsert
\ No newline at end of file
diff --git a/lib/db/schema.ts.backup b/lib/db/schema.ts.backup
new file mode 100644
index 00000000..ca8aa26a
--- /dev/null
+++ b/lib/db/schema.ts.backup
@@ -0,0 +1,57 @@
+import { InferSelectModel } from 'drizzle-orm'
+import {
+ json,
+ pgTable,
+ text,
+ timestamp,
+ uuid,
+ varchar
+} from 'drizzle-orm/pg-core'
+
+export const chats = pgTable(
+ 'chats',
+ {
+ id: uuid('id').primaryKey().notNull().defaultRandom(),
+ createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ .notNull()
+ .defaultNow(),
+ title: text('title').notNull(),
+ userId: uuid('user_id').notNull(), // User ID from the authentication system
+ visibility: varchar('visibility', {
+ length: 256,
+ enum: ['public', 'private']
+ })
+ .notNull()
+ .default('private')
+ },
+ table => {
+ return {
+ enableRls: true
+ }
+ }
+)
+
+export type Chat = InferSelectModel<typeof chats>
+
+export const messages = pgTable(
+ 'messages',
+ {
+ id: uuid('id').primaryKey().notNull().defaultRandom(),
+ chatId: uuid('chat_id')
+ .notNull()
+ .references(() => chats.id, { onDelete: 'cascade' }),
+ role: varchar('role', { length: 256 }).notNull(),
+ parts: json('parts').notNull(), // Stores structured message content (e.g., Vercel AI SDK Message parts)
+ attachments: json('attachments').notNull(),
+ createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ .notNull()
+ .defaultNow()
+ },
+ table => {
+ return {
+ enableRls: true
+ }
+ }
+)
+
+export type Message = InferSelectModel<typeof messages>
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 191cd7f7..a4ea9277 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -8,10 +8,10 @@ import {
import { researcher } from '@/lib/agents/researcher'
-import { saveChatMessage, saveSingleMessage } from '../actions/chat-db'
+import { createChatAndSaveMessage, deleteMessagesFromIndex,getChat as getChatAction, saveMessage } from '../actions/chat'
import { generateRelatedQuestions } from '../agents/generate-related-questions'
import { generateChatTitle } from '../agents/title-generator'
-import { getChat, getChatMessages } from '../db/chat'
+import { generateId } from '../db/schema'
import { getTextFromParts, mergeUIMessages } from '../utils/message-utils'
import { BaseStreamConfig } from './types'
@@ -19,11 +19,11 @@ import { BaseStreamConfig } from './types'
export async function createChatStreamResponse(
config: BaseStreamConfig
): Promise<Response> {
- const { message, model, chatId, searchMode, userId } = config
+ const { message, model, chatId, searchMode, userId, trigger, messageId } = config
const modelId = `${model.providerId}:${model.id}`
// Fetch chat data for authorization check before stream creation
- const chatForAuth = await getChat(chatId, userId)
+ const chatForAuth = await getChatAction(chatId, userId)
// Authorization check: if chat exists and does not belong to the user
if (chatForAuth && chatForAuth.userId !== userId) {
@@ -37,31 +37,55 @@ export async function createChatStreamResponse(
const stream = createUIMessageStream({
execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {
try {
- // Use chatForAuth (from outer scope) to decide new/existing path
- if (!chatForAuth) {
- // New chat
- const userContent = getTextFromParts(message.parts)
- const title = await generateChatTitle({
- userMessageContent: userContent,
- modelId
- })
- await saveChatMessage(chatId, message, userId, title)
+ let messagesToModel: UIMessage[]
+
+ if (trigger === 'regenerate-assistant-message' && messageId) {
+ // Handle regeneration
+ if (!chatForAuth) {
+ throw new Error('Chat not found for regeneration')
+ }
+
+ // Delete messages from the specified messageId onwards
+ await deleteMessagesFromIndex(chatId, messageId)
+
+ // Get updated messages
+ const updatedChat = await getChatAction(chatId, userId)
+ if (!updatedChat || !updatedChat.messages.length) {
+ throw new Error('No messages to regenerate')
+ }
+
+ messagesToModel = updatedChat.messages
} else {
- // Existing chat, and user is authorized (auth check done outside)
- await saveSingleMessage(chatId, message)
+ // Handle normal message submission
+ if (!message) {
+ throw new Error('No message provided')
+ }
+
+ // Ensure message has an ID
+ const messageWithId = {
+ ...message,
+ id: message.id || generateId()
+ }
+
+ // Use chatForAuth (from outer scope) to decide new/existing path
+ if (!chatForAuth) {
+ // New chat
+ const userContent = getTextFromParts(message.parts)
+ const title = await generateChatTitle({
+ userMessageContent: userContent,
+ modelId
+ })
+ await createChatAndSaveMessage(messageWithId, title)
+ } else {
+ // Existing chat, and user is authorized (auth check done outside)
+ await saveMessage(chatId, messageWithId)
+ }
+
+ // Get all messages including the one just saved
+ const updatedChat = await getChatAction(chatId, userId)
+ messagesToModel = updatedChat?.messages || [messageWithId]
}
- const previousMessages = await getChatMessages(chatId)
-
- // Convert database messages to UIMessage format
- const previousUIMessages: UIMessage[] = previousMessages.map(msg => ({
- id: msg.id,
- role: msg.role as 'user' | 'assistant',
- parts: msg.parts as UIMessage['parts']
- }))
-
- const messagesToModel = [...previousUIMessages, message]
-
// Get the researcher agent
const researchAgent = researcher({
model: modelId,
@@ -127,17 +151,17 @@ export async function createChatStreamResponse(
researchMessage,
relatedQuestionsMessage
)
- await saveSingleMessage(chatId, mergedMessage)
+ await saveMessage(chatId, mergedMessage)
} else if (researchMessage) {
// Save research message only if related questions failed
- await saveSingleMessage(chatId, researchMessage)
+ await saveMessage(chatId, researchMessage)
}
})
.catch(async error => {
console.error('Related questions error:', error)
// Save research message even if related questions fail
if (researchMessage) {
- await saveSingleMessage(chatId, researchMessage)
+ await saveMessage(chatId, researchMessage)
}
})
} catch (error) {
diff --git a/lib/streaming/types.ts b/lib/streaming/types.ts
index abb40b04..ff6f6a55 100644
--- a/lib/streaming/types.ts
+++ b/lib/streaming/types.ts
@@ -3,9 +3,11 @@ import { UIMessage } from '@ai-sdk/react'
import { Model } from '../types/models'
export interface BaseStreamConfig {
- message: UIMessage
+ message: UIMessage | null
model: Model
chatId: string
searchMode: boolean
userId: string
+ trigger?: 'submit-user-message' | 'regenerate-assistant-message'
+ messageId?: string
}
diff --git a/lib/types/message-persistence.ts b/lib/types/message-persistence.ts
new file mode 100644
index 00000000..fc32e3ff
--- /dev/null
+++ b/lib/types/message-persistence.ts
@@ -0,0 +1,55 @@
+import { z } from 'zod'
+
+import type { parts } from '@/lib/db/schema'
+import type { UIMessage } from '@/lib/types/ai'
+
+// Metadata schema
+export const metadataSchema = z.object({})
+export type Metadata = z.infer<typeof metadataSchema>
+
+// Data part definition (extensible)
+export const dataPartSchema = z.object({}).passthrough()
+export type DataPart = z.infer<typeof dataPartSchema>
+
+// Provider metadata
+export type ProviderMetadata = Record<string, any>
+
+// DB type definitions
+export type DBMessagePart = typeof parts.$inferInsert
+export type DBMessagePartSelect = typeof parts.$inferSelect
+
+// Tool states
+export type ToolState = 'input-streaming' | 'input-available' | 'output-available' | 'output-error'
+
+// MCP tool type definitions
+export type MCPToolInput = {
+ toolName: string
+ params: unknown
+}
+
+export type MCPToolOutput = unknown
+
+// Common MCP tool type definition examples
+export type MCPGitHubInput = {
+ toolName: 'mcp__github__create_issue'
+ params: {
+ owner: string
+ repo: string
+ title: string
+ body?: string
+ }
+}
+
+// DB message type
+export type DBMessage = {
+ id: string
+ chatId: string
+ role: string
+ createdAt: string
+}
+
+// Extended UIMessage type (persistence support)
+export type PersistableUIMessage = UIMessage & {
+ id: string
+ chatId?: string
+}
\ No newline at end of file
diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.ts
new file mode 100644
index 00000000..ec2c4e6c
--- /dev/null
+++ b/lib/utils/message-mapping.ts
@@ -0,0 +1,302 @@
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
+import type { DBMessagePart, DBMessagePartSelect, ToolState } from '@/lib/types/message-persistence'
+
+// Define local types for message parts that are compatible with the AI SDK
+type TextUIPart = { type: 'text'; text: string; providerMetadata?: any }
+type ReasoningUIPart = { type: 'reasoning'; text: string; providerMetadata?: any }
+type FileUIPart = { type: 'file'; mediaType: string; filename?: string; url: string }
+type SourceUrlUIPart = { type: 'source-url'; sourceId: string; url: string; title: string } // title is required
+type SourceDocumentUIPart = { type: 'source-document'; sourceId: string; mediaType: string; title: string; filename: string; url: string; snippet: string } // all fields required
+type ToolCallPart = { type: 'tool-call'; toolCallId: string; toolName: string; args: any }
+type ToolResultPart = { type: 'tool-result'; toolCallId: string; result: any; isError?: boolean }
+type DataPart = { type: string; [key: string]: any }
+
+type UIMessagePart =
+ | TextUIPart
+ | ReasoningUIPart
+ | FileUIPart
+ | SourceUrlUIPart
+ | SourceDocumentUIPart
+ | ToolCallPart
+ | ToolResultPart
+ | DataPart
+
+/**
+ * Convert UI message parts to DB format
+ */
+export function mapUIMessagePartsToDBParts(
+ messageParts: UIMessagePart[],
+ messageId: string
+): DBMessagePart[] {
+ return messageParts.map((part, index) => {
+ const basePart = {
+ messageId,
+ order: index,
+ type: part.type,
+ }
+
+ switch (part.type) {
+ case 'text':
+ return {
+ ...basePart,
+ text_text: part.text,
+ }
+
+ case 'reasoning':
+ return {
+ ...basePart,
+ reasoning_text: part.text,
+ providerMetadata: part.providerMetadata,
+ }
+
+ case 'file':
+ return {
+ ...basePart,
+ file_mediaType: part.mediaType,
+ file_filename: part.filename,
+ file_url: part.url,
+ }
+
+ case 'source-url':
+ return {
+ ...basePart,
+ source_url_sourceId: part.sourceId,
+ source_url_url: part.url,
+ source_url_title: part.title,
+ }
+
+ case 'source-document':
+ return {
+ ...basePart,
+ source_document_sourceId: part.sourceId,
+ source_document_mediaType: part.mediaType,
+ source_document_title: part.title,
+ source_document_filename: part.filename,
+ source_document_url: part.url,
+ source_document_snippet: part.snippet,
+ }
+
+ // Tool parts
+ case 'tool-call':
+ const toolName = getToolNameFromType(part.toolName)
+ const toolInputColumn = `tool_${toolName}_input` as keyof DBMessagePart
+
+ return {
+ ...basePart,
+ type: `tool-${toolName}`,
+ tool_toolCallId: part.toolCallId,
+ tool_state: 'input-available' as ToolState,
+ [toolInputColumn]: part.args,
+ }
+
+ case 'tool-result':
+ const resultToolName = getToolNameFromCallId(part.toolCallId, messageParts)
+ const toolOutputColumn = `tool_${resultToolName}_output` as keyof DBMessagePart
+
+ return {
+ ...basePart,
+ type: `tool-${resultToolName}`,
+ tool_toolCallId: part.toolCallId,
+ tool_state: part.isError ? 'output-error' : 'output-available' as ToolState,
+ tool_errorText: part.isError ? String(part.result) : undefined,
+ [toolOutputColumn]: !part.isError ? part.result : undefined,
+ }
+
+ // Data parts
+ default:
+ if (part.type.startsWith('data-')) {
+ const dataType = part.type.substring(5) // Remove 'data-' prefix
+ return {
+ ...basePart,
+ data_prefix: dataType,
+ data_content: (part as any).data || part,
+ data_id: (part as any).id,
+ }
+ }
+
+ // Unknown part type - store as data
+ return {
+ ...basePart,
+ data_prefix: part.type,
+ data_content: part,
+ }
+ }
+ })
+}
+
+/**
+ * Convert DB message parts to UI format
+ */
+export function mapDBPartToUIMessagePart(part: DBMessagePartSelect): UIMessagePart {
+ switch (part.type) {
+ case 'text':
+ return {
+ type: 'text',
+ text: part.text_text!,
+ }
+
+ case 'reasoning':
+ return {
+ type: 'reasoning',
+ text: part.reasoning_text!,
+ providerMetadata: part.providerMetadata,
+ }
+
+ case 'file':
+ return {
+ type: 'file',
+ mediaType: part.file_mediaType!,
+ filename: part.file_filename!,
+ url: part.file_url!,
+ }
+
+ case 'source-url':
+ return {
+ type: 'source-url',
+ sourceId: part.source_url_sourceId!,
+ url: part.source_url_url!,
+ title: part.source_url_title || '', // Provide default empty string
+ }
+
+ case 'source-document':
+ return {
+ type: 'source-document',
+ sourceId: part.source_document_sourceId!,
+ mediaType: part.source_document_mediaType!,
+ title: part.source_document_title || '', // Provide defaults
+ filename: part.source_document_filename || '',
+ url: part.source_document_url || '',
+ snippet: part.source_document_snippet || '',
+ }
+
+ default:
+ // Tool parts
+ if (part.type.startsWith('tool-')) {
+ const toolName = part.type.substring(5) // Remove 'tool-' prefix
+ const inputColumn = `tool_${toolName}_input` as keyof DBMessagePartSelect
+ const outputColumn = `tool_${toolName}_output` as keyof DBMessagePartSelect
+
+ // Determine if this is a tool-call or tool-result based on state
+ if (part.tool_state === 'input-available' || part.tool_state === 'input-streaming') {
+ return {
+ type: 'tool-call',
+ toolCallId: part.tool_toolCallId!,
+ toolName: getOriginalToolName(toolName),
+ args: part[inputColumn] as any,
+ }
+ } else {
+ // output-available or output-error
+ return {
+ type: 'tool-result',
+ toolCallId: part.tool_toolCallId!,
+ isError: part.tool_state === 'output-error',
+ result: part.tool_state === 'output-error'
+ ? part.tool_errorText
+ : part[outputColumn],
+ }
+ }
+ }
+
+ // Data parts
+ if (part.data_prefix) {
+ return {
+ type: `data-${part.data_prefix}`,
+ ...(part.data_content as any),
+ ...(part.data_id ? { id: part.data_id } : {}),
+ }
+ }
+
+ // Fallback - should not happen
+ throw new Error(`Unknown part type: ${part.type}`)
+ }
+}
+
+/**
+ * Normalize tool name (from tool-call's toolName)
+ */
+function getToolNameFromType(toolName: string): string {
+ // Map original tool names to DB column names
+ const toolNameMap: Record<string, string> = {
+ 'search': 'search',
+ 'retrieve': 'retrieve',
+ 'askQuestion': 'question',
+ 'question': 'question',
+ 'videoSearch': 'videoSearch',
+ 'video-search': 'videoSearch',
+ }
+
+ // For MCP tools
+ if (toolName.startsWith('mcp__')) {
+ return 'mcp'
+ }
+
+ return toolNameMap[toolName] || toolName
+}
+
+/**
+ * Get tool name from tool-result
+ */
+function getToolNameFromCallId(toolCallId: string, allParts: UIMessagePart[]): string {
+ // Find tool-call part with the same toolCallId
+ const toolCallPart = allParts.find(
+ part => part.type === 'tool-call' && part.toolCallId === toolCallId
+ ) as any
+
+ if (toolCallPart) {
+ return getToolNameFromType(toolCallPart.toolName)
+ }
+
+ // Fallback - should not happen
+ return 'unknown'
+}
+
+/**
+ * Convert DB column name back to original tool name
+ */
+function getOriginalToolName(dbToolName: string): string {
+ const reverseMap: Record<string, string> = {
+ 'search': 'search',
+ 'retrieve': 'retrieve',
+ 'question': 'askQuestion',
+ 'videoSearch': 'videoSearch',
+ 'mcp': 'mcp', // For MCP, the actual tool name is included in the input
+ }
+
+ return reverseMap[dbToolName] || dbToolName
+}
+
+/**
+ * Convert UI message to DB message (excluding parts)
+ */
+export function mapUIMessageToDBMessage(
+ message: UIMessage & { id: string; chatId: string }
+): {
+ id: string
+ chatId: string
+ role: string
+} {
+ return {
+ id: message.id,
+ chatId: message.chatId,
+ role: message.role,
+ }
+}
+
+/**
+ * Build UI message from DB message and parts
+ */
+export function buildUIMessageFromDB(
+ dbMessage: {
+ id: string
+ role: string
+ createdAt?: string
+ },
+ dbParts: DBMessagePartSelect[]
+): UIMessage {
+ return {
+ id: dbMessage.id,
+ role: dbMessage.role as 'user' | 'assistant',
+ parts: dbParts.map(mapDBPartToUIMessagePart) as UIMessage['parts'],
+ metadata: dbMessage.createdAt ? { createdAt: new Date(dbMessage.createdAt) } : undefined,
+ }
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 6dc7fa6c..8a0203f9 100644
--- a/package.json
+++ b/package.json
@@ -25,6 +25,7 @@
"@ai-sdk/openai": "^2.0.0-beta.11",
"@ai-sdk/react": "^2.0.0-beta.25",
"@ai-sdk/xai": "^2.0.0-beta.10",
+ "@paralleldrive/cuid2": "^2.2.2",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.1.9",
"@radix-ui/react-checkbox": "^1.0.4",
@@ -53,7 +54,7 @@
"clsx": "^2.1.0",
"cmdk": "1.0.0",
"dotenv": "^16.5.0",
- "drizzle-kit": "^0.31.1",
+ "drizzle-kit": "^0.31.4",
"drizzle-orm": "^0.43.1",
"embla-carousel-react": "^8.0.0",
"exa-js": "^1.0.12",