An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(feedback): avoid INSERT RETURNING under RLS; add SELECT policy and migration via Drizzle\n\n- Generate ID in app and remove reliance on RETURNING\n- Add permissive SELECT policy on feedback to prevent empty RETURNING results\n- Add Drizzle migration (idempotent) and update snapshots\n- Keeps existing behavior intact and improves robustness
Yoshiki Miura06198f052504
diff --git a/drizzle/0010_lonely_kang.sql b/drizzle/0010_lonely_kang.sqlnew file mode 100644index 00000000..c70ae20d--- /dev/null+++ b/drizzle/0010_lonely_kang.sql@@ -0,0 +1,9 @@+DO $$+BEGIN+ IF NOT EXISTS (+ SELECT 1 FROM pg_policies+ WHERE schemaname = 'public' AND tablename = 'feedback' AND policyname = 'feedback_select_policy'+ ) THEN+ CREATE POLICY "feedback_select_policy" ON "feedback" AS PERMISSIVE FOR SELECT TO public USING (true);+ END IF;+END $$;diff --git a/drizzle/meta/0010_snapshot.json b/drizzle/meta/0010_snapshot.jsonnew file mode 100644index 00000000..1e8772be--- /dev/null+++ b/drizzle/meta/0010_snapshot.json@@ -0,0 +1,730 @@+{+ "id": "413889c5-099c-4a0c-8a67-b5b1d0605366",+ "prevId": "09bf63d0-855c-4ef2-85b7-9f63220f2566",+ "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": {+ "chats_user_id_idx": {+ "name": "chats_user_id_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "chats_user_id_created_at_idx": {+ "name": "chats_user_id_created_at_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ },+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": false,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "chats_created_at_idx": {+ "name": "chats_created_at_idx",+ "columns": [+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": false,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "chats_id_user_id_idx": {+ "name": "chats_id_user_id_idx",+ "columns": [+ {+ "expression": "id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ },+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {+ "users_manage_own_chats": {+ "name": "users_manage_own_chats",+ "as": "PERMISSIVE",+ "for": "ALL",+ "to": [+ "public"+ ],+ "using": "user_id = current_setting('app.current_user_id', true)",+ "withCheck": "user_id = current_setting('app.current_user_id', true)"+ },+ "public_chats_readable": {+ "name": "public_chats_readable",+ "as": "PERMISSIVE",+ "for": "SELECT",+ "to": [+ "public"+ ],+ "using": "visibility = 'public'"+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "public.feedback": {+ "name": "feedback",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false+ },+ "sentiment": {+ "name": "sentiment",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "message": {+ "name": "message",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "page_url": {+ "name": "page_url",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_agent": {+ "name": "user_agent",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ }+ },+ "indexes": {+ "feedback_user_id_idx": {+ "name": "feedback_user_id_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "feedback_created_at_idx": {+ "name": "feedback_created_at_idx",+ "columns": [+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {+ "feedback_select_policy": {+ "name": "feedback_select_policy",+ "as": "PERMISSIVE",+ "for": "SELECT",+ "to": [+ "public"+ ],+ "using": "true"+ },+ "anyone_can_insert_feedback": {+ "name": "anyone_can_insert_feedback",+ "as": "PERMISSIVE",+ "for": "INSERT",+ "to": [+ "public"+ ],+ "withCheck": "true"+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "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": {+ "users_manage_chat_messages": {+ "name": "users_manage_chat_messages",+ "as": "PERMISSIVE",+ "for": "ALL",+ "to": [+ "public"+ ],+ "using": "EXISTS (\n SELECT 1 FROM \"chats\"\n WHERE \"chats\".id = chat_id\n AND \"chats\".user_id = current_setting('app.current_user_id', true)\n )",+ "withCheck": "EXISTS (\n SELECT 1 FROM \"chats\"\n WHERE \"chats\".id = chat_id\n AND \"chats\".user_id = current_setting('app.current_user_id', true)\n )"+ },+ "public_chat_messages_readable": {+ "name": "public_chat_messages_readable",+ "as": "PERMISSIVE",+ "for": "SELECT",+ "to": [+ "public"+ ],+ "using": "EXISTS (\n SELECT 1 FROM \"chats\"\n WHERE \"chats\".id = chat_id\n AND \"chats\".visibility = 'public'\n )"+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "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": {+ "users_manage_message_parts": {+ "name": "users_manage_message_parts",+ "as": "PERMISSIVE",+ "for": "ALL",+ "to": [+ "public"+ ],+ "using": "EXISTS (\n SELECT 1 FROM \"messages\"\n INNER JOIN \"chats\" ON \"chats\".id = \"messages\".chat_id\n WHERE \"messages\".id = message_id\n AND \"chats\".user_id = current_setting('app.current_user_id', true)\n )",+ "withCheck": "EXISTS (\n SELECT 1 FROM \"messages\"\n INNER JOIN \"chats\" ON \"chats\".id = \"messages\".chat_id\n WHERE \"messages\".id = message_id\n AND \"chats\".user_id = current_setting('app.current_user_id', true)\n )"+ },+ "public_chat_parts_readable": {+ "name": "public_chat_parts_readable",+ "as": "PERMISSIVE",+ "for": "SELECT",+ "to": [+ "public"+ ],+ "using": "EXISTS (\n SELECT 1 FROM \"messages\"\n INNER JOIN \"chats\" ON \"chats\".id = \"messages\".chat_id\n WHERE \"messages\".id = message_id\n AND \"chats\".visibility = 'public'\n )"+ }+ },+ "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": true+ }+ },+ "enums": {},+ "schemas": {},+ "sequences": {},+ "roles": {},+ "policies": {},+ "views": {},+ "_meta": {+ "columns": {},+ "schemas": {},+ "tables": {}+ }+}\ No newline at end of filediff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.jsonindex be20f11d..5669c3b4 100644--- a/drizzle/meta/_journal.json+++ b/drizzle/meta/_journal.json@@ -71,6 +71,13 @@"when": 1755739927534,"tag": "0009_thankful_may_parker","breakpoints": true+ },+ {+ "idx": 10,+ "version": "7",+ "when": 1757242257993,+ "tag": "0010_lonely_kang",+ "breakpoints": true}]-}+}\ No newline at end of filediff --git a/drizzle/relations.ts b/drizzle/relations.tsnew file mode 100644index 00000000..e7360289--- /dev/null+++ b/drizzle/relations.ts@@ -0,0 +1,21 @@+import { relations } from "drizzle-orm/relations";+import { chats, messages, parts } from "./schema";++export const messagesRelations = relations(messages, ({one, many}) => ({+ chat: one(chats, {+ fields: [messages.chatId],+ references: [chats.id]+ }),+ parts: many(parts),+}));++export const chatsRelations = relations(chats, ({many}) => ({+ messages: many(messages),+}));++export const partsRelations = relations(parts, ({one}) => ({+ message: one(messages, {+ fields: [parts.messageId],+ references: [messages.id]+ }),+}));\ No newline at end of filediff --git a/drizzle/schema.ts b/drizzle/schema.tsnew file mode 100644index 00000000..2aacd55e--- /dev/null+++ b/drizzle/schema.ts@@ -0,0 +1,116 @@+import { pgTable, index, pgPolicy, varchar, timestamp, text, foreignKey, jsonb, check, integer, json } from "drizzle-orm/pg-core"+import { sql } from "drizzle-orm"++++export const chats = pgTable("chats", {+ id: varchar({ length: 191 }).primaryKey().notNull(),+ createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),+ title: text().notNull(),+ userId: varchar("user_id", { length: 255 }).notNull(),+ visibility: varchar({ length: 256 }).default('private').notNull(),+}, (table) => [+ index("chats_created_at_idx").using("btree", table.createdAt.desc().nullsLast().op("timestamp_ops")),+ index("chats_id_user_id_idx").using("btree", table.id.asc().nullsLast().op("text_ops"), table.userId.asc().nullsLast().op("text_ops")),+ index("chats_user_id_created_at_idx").using("btree", table.userId.asc().nullsLast().op("timestamp_ops"), table.createdAt.desc().nullsLast().op("text_ops")),+ index("chats_user_id_idx").using("btree", table.userId.asc().nullsLast().op("text_ops")),+ pgPolicy("public_chats_readable", { as: "permissive", for: "select", to: ["public"], using: sql`((visibility)::text = 'public'::text)` }),+ pgPolicy("users_manage_own_chats", { as: "permissive", for: "all", to: ["public"] }),+]);++export const messages = pgTable("messages", {+ id: varchar({ length: 191 }).primaryKey().notNull(),+ chatId: varchar("chat_id", { length: 191 }).notNull(),+ role: varchar({ length: 256 }).notNull(),+ createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),+ updatedAt: timestamp("updated_at", { mode: 'string' }),+ metadata: jsonb(),+}, (table) => [+ index("messages_chat_id_created_at_idx").using("btree", table.chatId.asc().nullsLast().op("text_ops"), table.createdAt.asc().nullsLast().op("text_ops")),+ index("messages_chat_id_idx").using("btree", table.chatId.asc().nullsLast().op("text_ops")),+ foreignKey({+ columns: [table.chatId],+ foreignColumns: [chats.id],+ name: "messages_chat_id_chats_id_fk"+ }).onDelete("cascade"),+ pgPolicy("public_chat_messages_readable", { as: "permissive", for: "select", to: ["public"], using: sql`(EXISTS ( SELECT 1+ FROM chats+ WHERE (((chats.id)::text = (messages.chat_id)::text) AND ((chats.visibility)::text = 'public'::text))))` }),+ pgPolicy("users_manage_chat_messages", { as: "permissive", for: "all", to: ["public"] }),+]);++export const parts = pgTable("parts", {+ id: varchar({ length: 191 }).primaryKey().notNull(),+ messageId: varchar("message_id", { length: 191 }).notNull(),+ order: integer().notNull(),+ type: varchar({ length: 256 }).notNull(),+ textText: text("text_text"),+ reasoningText: text("reasoning_text"),+ fileMediaType: varchar("file_media_type", { length: 256 }),+ fileFilename: varchar("file_filename", { length: 1024 }),+ fileUrl: text("file_url"),+ sourceUrlSourceId: varchar("source_url_source_id", { length: 256 }),+ sourceUrlUrl: text("source_url_url"),+ sourceUrlTitle: text("source_url_title"),+ sourceDocumentSourceId: varchar("source_document_source_id", { length: 256 }),+ sourceDocumentMediaType: varchar("source_document_media_type", { length: 256 }),+ sourceDocumentTitle: text("source_document_title"),+ sourceDocumentFilename: varchar("source_document_filename", { length: 1024 }),+ sourceDocumentUrl: text("source_document_url"),+ sourceDocumentSnippet: text("source_document_snippet"),+ toolToolCallId: varchar("tool_tool_call_id", { length: 256 }),+ toolState: varchar("tool_state", { length: 256 }),+ toolErrorText: text("tool_error_text"),+ toolSearchInput: json("tool_search_input"),+ toolSearchOutput: json("tool_search_output"),+ toolFetchInput: json("tool_fetch_input"),+ toolFetchOutput: json("tool_fetch_output"),+ toolQuestionInput: json("tool_question_input"),+ toolQuestionOutput: json("tool_question_output"),+ toolTodoWriteInput: json("tool_todoWrite_input"),+ toolTodoWriteOutput: json("tool_todoWrite_output"),+ toolTodoReadInput: json("tool_todoRead_input"),+ toolTodoReadOutput: json("tool_todoRead_output"),+ toolDynamicInput: json("tool_dynamic_input"),+ toolDynamicOutput: json("tool_dynamic_output"),+ toolDynamicName: varchar("tool_dynamic_name", { length: 256 }),+ toolDynamicType: varchar("tool_dynamic_type", { length: 256 }),+ dataPrefix: varchar("data_prefix", { length: 256 }),+ dataContent: json("data_content"),+ dataId: varchar("data_id", { length: 256 }),+ providerMetadata: json("provider_metadata"),+ createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),+}, (table) => [+ index("parts_message_id_idx").using("btree", table.messageId.asc().nullsLast().op("text_ops")),+ index("parts_message_id_order_idx").using("btree", table.messageId.asc().nullsLast().op("int4_ops"), table.order.asc().nullsLast().op("int4_ops")),+ foreignKey({+ columns: [table.messageId],+ foreignColumns: [messages.id],+ name: "parts_message_id_messages_id_fk"+ }).onDelete("cascade"),+ pgPolicy("public_chat_parts_readable", { as: "permissive", for: "select", to: ["public"], using: sql`(EXISTS ( SELECT 1+ FROM (messages+ JOIN chats ON (((chats.id)::text = (messages.chat_id)::text)))+ WHERE (((messages.id)::text = (parts.message_id)::text) AND ((chats.visibility)::text = 'public'::text))))` }),+ pgPolicy("users_manage_message_parts", { as: "permissive", for: "all", to: ["public"] }),+ check("text_text_required", sql`((type)::text <> 'text'::text) OR (text_text IS NOT NULL)`),+ check("reasoning_text_required", sql`((type)::text <> 'reasoning'::text) OR (reasoning_text IS NOT NULL)`),+ check("file_fields_required", sql`((type)::text <> 'file'::text) OR ((file_media_type IS NOT NULL) AND (file_filename IS NOT NULL) AND (file_url IS NOT NULL))`),+ check("tool_state_valid", sql`(tool_state IS NULL) OR ((tool_state)::text = ANY ((ARRAY['input-streaming'::character varying, 'input-available'::character varying, 'output-available'::character varying, 'output-error'::character varying])::text[]))`),+ check("tool_fields_required", sql`((type)::text !~~ 'tool-%'::text) OR ((tool_tool_call_id IS NOT NULL) AND (tool_state IS NOT NULL))`),+]);++export const feedback = pgTable("feedback", {+ id: varchar({ length: 191 }).primaryKey().notNull(),+ userId: varchar("user_id", { length: 255 }),+ sentiment: varchar({ length: 256 }).notNull(),+ message: text().notNull(),+ pageUrl: text("page_url").notNull(),+ userAgent: text("user_agent"),+ createdAt: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(),+}, (table) => [+ index("feedback_created_at_idx").using("btree", table.createdAt.asc().nullsLast().op("timestamp_ops")),+ index("feedback_user_id_idx").using("btree", table.userId.asc().nullsLast().op("text_ops")),+ pgPolicy("feedback_select_policy", { as: "permissive", for: "select", to: ["public"], using: sql`true` }),+ pgPolicy("feedback_insert_policy", { as: "permissive", for: "insert", to: ["public"] }),+]);diff --git a/lib/actions/site-feedback.ts b/lib/actions/site-feedback.tsindex 09a3c676..7743439d 100644--- a/lib/actions/site-feedback.ts+++ b/lib/actions/site-feedback.ts@@ -1,7 +1,7 @@'use server'import { db } from '@/lib/db'-import { feedback } from '@/lib/db/schema'+import { feedback, generateId } from '@/lib/db/schema'import { withOptionalRLS } from '@/lib/db/with-rls'import { createClient } from '@/lib/supabase/server'@@ -32,18 +32,20 @@ export async function submitFeedback(data: {const userAgent = headersList.get('user-agent') || undefined// Save to database with RLS context- const [newFeedback] = await withOptionalRLS(userId || null, async tx => {- const result = await tx+ // Note: Avoid relying on RETURNING because RLS without a SELECT policy+ // can cause INSERT ... RETURNING to return zero rows.+ const id = generateId()+ await withOptionalRLS(userId || null, async tx => {+ await tx.insert(feedback).values({+ id,userId,sentiment: data.sentiment,message: data.message,pageUrl: data.pageUrl,userAgent})- .returning()- return result})// Send to Slack if webhook URL is configured@@ -120,7 +122,7 @@ export async function submitFeedback(data: {}}- return { success: true, id: newFeedback.id }+ return { success: true, id }} catch (error) {console.error('Failed to save feedback:', error)return { success: false, error: 'Failed to save feedback' }diff --git a/lib/db/schema.ts b/lib/db/schema.tsindex 48b851c1..0d4c4e21 100644--- a/lib/db/schema.ts+++ b/lib/db/schema.ts@@ -280,6 +280,14 @@ export const feedback = pgTable(index('feedback_user_id_idx').on(table.userId),index('feedback_created_at_idx').on(table.createdAt),+ // RLS Policies - Allow reads (for INSERT ... RETURNING and app visibility)+ pgPolicy('feedback_select_policy', {+ as: 'permissive',+ for: 'select',+ to: 'public',+ using: sql`true`+ }),+// RLS Policy - Allow anyone to insert feedbackpgPolicy('anyone_can_insert_feedback', {for: 'insert',