An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: implement Langfuse feedback integration with user rating system
Yoshiki Miurae1587a2949d8
diff --git a/app/api/feedback/route.ts b/app/api/feedback/route.tsnew file mode 100644index 00000000..5d6c9d3f--- /dev/null+++ b/app/api/feedback/route.ts@@ -0,0 +1,94 @@+import { eq } from 'drizzle-orm'+import { Langfuse } from 'langfuse'++import { chatCache } from '@/lib/cache/memory-cache'+import { db } from '@/lib/db'+import { messages } from '@/lib/db/schema'+import { isTracingEnabled } from '@/lib/utils/telemetry'++export async function POST(req: Request) {+ try {+ const body = await req.json()+ const { traceId, score, comment, messageId } = body++ if (!traceId) {+ return new Response('traceId is required', {+ status: 400,+ statusText: 'Bad Request'+ })+ }++ if (score === undefined || (score !== 1 && score !== -1)) {+ return new Response('score must be 1 (good) or -1 (bad)', {+ status: 400,+ statusText: 'Bad Request'+ })+ }++ // Check if tracing is enabled+ if (!isTracingEnabled()) {+ return new Response('Feedback tracking is not enabled', {+ status: 200+ })+ }++ // Initialize Langfuse client+ const langfuse = new Langfuse()++ // Send score to Langfuse+ langfuse.score({+ traceId,+ name: 'user_feedback',+ value: score,+ comment+ })++ // Flush to ensure the score is sent+ await langfuse.flushAsync()++ // Update the message metadata with the feedback score+ if (messageId) {+ try {+ // First get the current message to preserve existing metadata and get chatId+ const [currentMessage] = await db+ .select({+ metadata: messages.metadata,+ chatId: messages.chatId+ })+ .from(messages)+ .where(eq(messages.id, messageId))+ .limit(1)++ // Merge the feedback score with existing metadata+ const updatedMetadata = {+ ...(currentMessage?.metadata || {}),+ feedbackScore: score+ }++ // Update the message with the new metadata+ await db+ .update(messages)+ .set({ metadata: updatedMetadata })+ .where(eq(messages.id, messageId))++ // Invalidate cache for this chat to ensure fresh data is loaded+ if (currentMessage?.chatId) {+ chatCache.deletePattern(`${currentMessage.chatId}-`)+ }+ } catch (dbError) {+ console.error('Error updating message metadata:', dbError)+ // Continue even if database update fails+ }+ }++ return new Response('Feedback recorded successfully', {+ status: 200+ })+ } catch (error) {+ console.error('Error recording feedback:', error)+ return new Response('Error recording feedback', {+ status: 500,+ statusText: 'Internal Server Error'+ })+ }+}diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex 1db3bc1e..bd25d011 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -4,7 +4,12 @@ import { UseChatHelpers } from '@ai-sdk/react'import { ChatRequestOptions } from 'ai'import type { SearchResultItem } from '@/lib/types'-import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import type {+ UIDataTypes,+ UIMessage,+ UIMessageMetadata,+ UITools+} from '@/lib/types/ai'import { CollapsibleMessage } from './collapsible-message'import { DefaultSkeleton } from './default-skeleton'@@ -18,6 +23,7 @@ export type AnswerSectionProps = {chatId?: stringshowActions?: booleanmessageId: string+ metadata?: UIMessageMetadatastatus?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']reload?: (messageId: string,@@ -33,6 +39,7 @@ export function AnswerSection({chatId,showActions = true, // Default to true for backward compatibilitymessageId,+ metadata,status,reload,citationMaps@@ -53,6 +60,8 @@ export function AnswerSection({<MessageActionsmessage={content} // Keep original message content for copymessageId={messageId}+ traceId={metadata?.traceId}+ feedbackScore={metadata?.feedbackScore}chatId={chatId}enableShare={enableShare}reload={handleReload}diff --git a/components/message-actions.tsx b/components/message-actions.tsxindex 4f3a1050..5cd31bf5 100644--- a/components/message-actions.tsx+++ b/components/message-actions.tsx@@ -1,7 +1,9 @@'use client'+import { useState } from 'react'+import { UseChatHelpers } from '@ai-sdk/react'-import { Copy } from 'lucide-react'+import { Copy, ThumbsDown, ThumbsUp } from 'lucide-react'import { toast } from 'sonner'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'@@ -14,6 +16,8 @@ import { RetryButton } from './retry-button'interface MessageActionsProps {message: stringmessageId: string+ traceId?: string+ feedbackScore?: number | nullreload?: () => Promise<void | string | null | undefined>chatId?: stringenableShare?: boolean@@ -24,12 +28,18 @@ interface MessageActionsProps {export function MessageActions({message,messageId,+ traceId,+ feedbackScore: initialFeedbackScore,reload,chatId,enableShare,className,status}: MessageActionsProps) {+ const [feedbackScore, setFeedbackScore] = useState<number | null>(+ initialFeedbackScore ?? null+ )+ const [isSubmittingFeedback, setIsSubmittingFeedback] = useState(false)const isLoading = status === 'submitted' || status === 'streaming'async function handleCopy() {@@ -37,6 +47,40 @@ export function MessageActions({toast.success('Message copied to clipboard')}+ async function handleFeedback(score: number) {+ if (isSubmittingFeedback || !traceId) return++ setIsSubmittingFeedback(true)+ try {+ const response = await fetch('/api/feedback', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ traceId,+ score,+ messageId+ })+ })++ if (response.ok) {+ setFeedbackScore(score)+ toast.success(+ score === 1+ ? 'Thanks for the feedback!'+ : 'Thanks for letting us know!'+ )+ } else {+ console.error('Failed to submit feedback')+ toast.error('Failed to submit feedback')+ }+ } catch (error) {+ console.error('Error submitting feedback:', error)+ toast.error('Failed to submit feedback')+ } finally {+ setIsSubmittingFeedback(false)+ }+ }+return (<divclassName={cn(@@ -54,6 +98,38 @@ export function MessageActions({><Copy size={14} /></Button>+ {traceId && (+ <>+ {(feedbackScore === null || feedbackScore === 1) && (+ <Button+ variant="ghost"+ size="icon"+ onClick={() => handleFeedback(1)}+ disabled={isSubmittingFeedback || feedbackScore === 1}+ className="rounded-full"+ >+ <ThumbsUp+ size={14}+ className={feedbackScore === 1 ? 'fill-current' : ''}+ />+ </Button>+ )}+ {(feedbackScore === null || feedbackScore === -1) && (+ <Button+ variant="ghost"+ size="icon"+ onClick={() => handleFeedback(-1)}+ disabled={isSubmittingFeedback || feedbackScore === -1}+ className="rounded-full"+ >+ <ThumbsDown+ size={14}+ className={feedbackScore === -1 ? 'fill-current' : ''}+ />+ </Button>+ )}+ </>+ )}{enableShare && chatId && <ChatShare chatId={chatId} />}</div>)diff --git a/components/render-message.tsx b/components/render-message.tsxindex bdd6b29a..77031127 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -1,6 +1,11 @@import { UseChatHelpers } from '@ai-sdk/react'-import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import type {+ UIDataTypes,+ UIMessage,+ UIMessageMetadata,+ UITools+} from '@/lib/types/ai'import type { DynamicToolPart } from '@/lib/types/dynamic-tools'import { extractCitationMaps } from '@/lib/utils/citation'@@ -122,6 +127,7 @@ export function RenderMessage({chatId={chatId}showActions={shouldShowActions}messageId={messageId}+ metadata={message.metadata as UIMessageMetadata | undefined}reload={reload}status={status}citationMaps={citationMaps}diff --git a/drizzle/0001_thin_supreme_intelligence.sql b/drizzle/0001_thin_supreme_intelligence.sqlnew file mode 100644index 00000000..fe44e1d1--- /dev/null+++ b/drizzle/0001_thin_supreme_intelligence.sql@@ -0,0 +1,2 @@+ALTER TABLE "messages" ADD COLUMN "updated_at" timestamp;--> statement-breakpoint+ALTER TABLE "messages" ADD COLUMN "metadata" json;\ No newline at end of filediff --git a/drizzle/0002_material_crystal.sql b/drizzle/0002_material_crystal.sqlnew file mode 100644index 00000000..9bfb2e41--- /dev/null+++ b/drizzle/0002_material_crystal.sql@@ -0,0 +1 @@+ALTER TABLE "messages" ALTER COLUMN "metadata" SET DATA TYPE jsonb;\ No newline at end of filediff --git a/drizzle/meta/0001_snapshot.json b/drizzle/meta/0001_snapshot.jsonnew file mode 100644index 00000000..a54b9494--- /dev/null+++ b/drizzle/meta/0001_snapshot.json@@ -0,0 +1,483 @@+{+ "id": "90075b6a-dc27-4224-beb5-1ce34abc7fc5",+ "prevId": "bd2133a6-1281-44a1-8619-c1f0407dfb77",+ "version": "7",+ "dialect": "postgresql",+ "tables": {+ "public.chats": {+ "name": "chats",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "title": {+ "name": "title",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(255)",+ "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(191)",+ "primaryKey": true,+ "notNull": true+ },+ "chat_id": {+ "name": "chat_id",+ "type": "varchar(191)",+ "primaryKey": false,+ "notNull": true+ },+ "role": {+ "name": "role",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "updated_at": {+ "name": "updated_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": false+ },+ "metadata": {+ "name": "metadata",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ }+ },+ "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(191)",+ "primaryKey": true,+ "notNull": true+ },+ "message_id": {+ "name": "message_id",+ "type": "varchar(191)",+ "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_fetch_input": {+ "name": "tool_fetch_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_fetch_output": {+ "name": "tool_fetch_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_todoWrite_input": {+ "name": "tool_todoWrite_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoWrite_output": {+ "name": "tool_todoWrite_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_input": {+ "name": "tool_todoRead_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_output": {+ "name": "tool_todoRead_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_input": {+ "name": "tool_dynamic_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_output": {+ "name": "tool_dynamic_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_name": {+ "name": "tool_dynamic_name",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_type": {+ "name": "tool_dynamic_type",+ "type": "varchar(256)",+ "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",+ "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": {}+ }+}diff --git a/drizzle/meta/0002_snapshot.json b/drizzle/meta/0002_snapshot.jsonnew file mode 100644index 00000000..f9d8b824--- /dev/null+++ b/drizzle/meta/0002_snapshot.json@@ -0,0 +1,483 @@+{+ "id": "8e9de712-037f-4132-8f06-b3d2f57dd68a",+ "prevId": "90075b6a-dc27-4224-beb5-1ce34abc7fc5",+ "version": "7",+ "dialect": "postgresql",+ "tables": {+ "public.chats": {+ "name": "chats",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "title": {+ "name": "title",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(255)",+ "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(191)",+ "primaryKey": true,+ "notNull": true+ },+ "chat_id": {+ "name": "chat_id",+ "type": "varchar(191)",+ "primaryKey": false,+ "notNull": true+ },+ "role": {+ "name": "role",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "updated_at": {+ "name": "updated_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": false+ },+ "metadata": {+ "name": "metadata",+ "type": "jsonb",+ "primaryKey": false,+ "notNull": false+ }+ },+ "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(191)",+ "primaryKey": true,+ "notNull": true+ },+ "message_id": {+ "name": "message_id",+ "type": "varchar(191)",+ "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_fetch_input": {+ "name": "tool_fetch_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_fetch_output": {+ "name": "tool_fetch_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_todoWrite_input": {+ "name": "tool_todoWrite_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoWrite_output": {+ "name": "tool_todoWrite_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_input": {+ "name": "tool_todoRead_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_output": {+ "name": "tool_todoRead_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_input": {+ "name": "tool_dynamic_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_output": {+ "name": "tool_dynamic_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_name": {+ "name": "tool_dynamic_name",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_type": {+ "name": "tool_dynamic_type",+ "type": "varchar(256)",+ "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",+ "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": {}+ }+}diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.jsonindex 777539fb..bec9a626 100644--- a/drizzle/meta/_journal.json+++ b/drizzle/meta/_journal.json@@ -8,6 +8,20 @@"when": 1754303165189,"tag": "0000_black_lifeguard","breakpoints": true+ },+ {+ "idx": 1,+ "version": "7",+ "when": 1754819323396,+ "tag": "0001_thin_supreme_intelligence",+ "breakpoints": true+ },+ {+ "idx": 2,+ "version": "7",+ "when": 1754822051273,+ "tag": "0002_material_crystal",+ "breakpoints": true}]}diff --git a/lib/actions/__tests__/chat-db.test.ts b/lib/actions/__tests__/chat-db.test.tsindex dffeaafd..2a9c41c0 100644--- a/lib/actions/__tests__/chat-db.test.ts+++ b/lib/actions/__tests__/chat-db.test.ts@@ -65,7 +65,9 @@ const testMessageData: DBMessage = {id: commonMessageId,chatId: commonChatId,role: 'user',- createdAt: new Date(now)+ createdAt: new Date(now),+ updatedAt: null,+ metadata: null}describe('Chat Actions - clearChats', () => {diff --git a/lib/db/schema.ts b/lib/db/schema.tsindex 8e33e917..5ed8f216 100644--- a/lib/db/schema.ts+++ b/lib/db/schema.ts@@ -5,6 +5,7 @@ import {index,integer,json,+ jsonb,pgTable,text,timestamp,@@ -57,7 +58,9 @@ export const messages = pgTable(.notNull().references(() => chats.id, { onDelete: 'cascade' }),role: varchar('role', { length: VARCHAR_LENGTH }).notNull(),- createdAt: timestamp('created_at').notNull().defaultNow()+ createdAt: timestamp('created_at').notNull().defaultNow(),+ updatedAt: timestamp('updated_at'),+ metadata: jsonb('metadata').$type<Record<string, any>>()},table => ({chatIdIdx: index('messages_chat_id_idx').on(table.chatId),diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex f6f212b9..86c8004c 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -105,6 +105,14 @@ export async function createChatStreamResponse(parentTraceId})+ // Write metadata including traceId at the start of streaming+ if (parentTraceId) {+ writer.write({+ type: 'message-metadata',+ messageMetadata: { traceId: parentTraceId }+ })+ }+// Filter out reasoning parts from messages before converting to model messages// OpenAI API requires reasoning messages to be followed by assistant messagesconst filteredMessages = filterReasoningParts(messagesToModel)diff --git a/lib/streaming/helpers/handle-stream-finish.ts b/lib/streaming/helpers/handle-stream-finish.tsindex 8eda49a5..622e1e68 100644--- a/lib/streaming/helpers/handle-stream-finish.ts+++ b/lib/streaming/helpers/handle-stream-finish.ts@@ -20,6 +20,14 @@ export async function handleStreamFinish() {const { chatId, modelId, abortSignal, parentTraceId } = context+ // Attach metadata to the response message if we have a traceId+ if (parentTraceId) {+ responseMessage.metadata = {+ ...(responseMessage.metadata || {}),+ traceId: parentTraceId+ }+ }+// Generate related questions if there are tool callsif (hasToolCalls(responseMessage as UIMessage | null)) {const questionPartId = generateId()diff --git a/lib/types/ai.ts b/lib/types/ai.tsindex a1af34a8..8cc85e9b 100644--- a/lib/types/ai.ts+++ b/lib/types/ai.ts@@ -9,8 +9,15 @@ import { createTodoTools, type TodoItem } from '@/lib/tools/todo'// Re-export TodoItem for external useexport type { TodoItem }+// Define metadata type for messages+export interface UIMessageMetadata {+ traceId?: string+ feedbackScore?: number | null+ [key: string]: any+}+export type UIMessage<- TMetadata = unknown,+ TMetadata = UIMessageMetadata,TDataTypes = UIDataTypes,TTools = UITools> = AIMessagediff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.tsindex bd51eed4..967cc608 100644--- a/lib/utils/message-mapping.ts+++ b/lib/utils/message-mapping.ts@@ -727,11 +727,13 @@ export function mapUIMessageToDBMessage(id: stringchatId: stringrole: string+ metadata?: Record<string, any> | null} {return {id: message.id,chatId: message.chatId,- role: message.role+ role: message.role,+ metadata: message.metadata as Record<string, any> | undefined}}@@ -742,21 +744,26 @@ export function buildUIMessageFromDB(dbMessage: {id: stringrole: string+ metadata?: Record<string, any> | nullcreatedAt?: Date | string},dbParts: DBMessagePartSelect[]): UIMessage {+ // Merge metadata from DB with createdAt+ const metadata: Record<string, any> = {+ ...(dbMessage.metadata || {}),+ ...(dbMessage.createdAt && {+ createdAt:+ dbMessage.createdAt instanceof Date+ ? dbMessage.createdAt+ : new Date(dbMessage.createdAt)+ })+ }+return {id: dbMessage.id,role: dbMessage.role as 'user' | 'assistant',parts: dbParts.map(mapDBPartToUIMessagePart) as UIMessage['parts'],- metadata: dbMessage.createdAt- ? {- createdAt:- dbMessage.createdAt instanceof Date- ? dbMessage.createdAt- : new Date(dbMessage.createdAt)- }- : undefined+ metadata: Object.keys(metadata).length > 0 ? metadata : undefined}}