Skip to content

An AI-powered search engine with a generative UI

chore: format

Yoshiki Miurab59d5ee24c96
diff --git a/drizzle/meta/0010_snapshot.json b/drizzle/meta/0010_snapshot.json
index 1e8772be..b11a13ad 100644
--- a/drizzle/meta/0010_snapshot.json
+++ b/drizzle/meta/0010_snapshot.json
@@ -123,9 +123,7 @@
"name": "users_manage_own_chats",
"as": "PERMISSIVE",
"for": "ALL",
- "to": [
- "public"
- ],
+ "to": ["public"],
"using": "user_id = current_setting('app.current_user_id', true)",
"withCheck": "user_id = current_setting('app.current_user_id', true)"
},
@@ -133,9 +131,7 @@
"name": "public_chats_readable",
"as": "PERMISSIVE",
"for": "SELECT",
- "to": [
- "public"
- ],
+ "to": ["public"],
"using": "visibility = 'public'"
}
},
@@ -230,18 +226,14 @@
"name": "feedback_select_policy",
"as": "PERMISSIVE",
"for": "SELECT",
- "to": [
- "public"
- ],
+ "to": ["public"],
"using": "true"
},
"anyone_can_insert_feedback": {
"name": "anyone_can_insert_feedback",
"as": "PERMISSIVE",
"for": "INSERT",
- "to": [
- "public"
- ],
+ "to": ["public"],
"withCheck": "true"
}
},
@@ -333,12 +325,8 @@
"name": "messages_chat_id_chats_id_fk",
"tableFrom": "messages",
"tableTo": "chats",
- "columnsFrom": [
- "chat_id"
- ],
- "columnsTo": [
- "id"
- ],
+ "columnsFrom": ["chat_id"],
+ "columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
@@ -350,9 +338,7 @@
"name": "users_manage_chat_messages",
"as": "PERMISSIVE",
"for": "ALL",
- "to": [
- "public"
- ],
+ "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 )"
},
@@ -360,9 +346,7 @@
"name": "public_chat_messages_readable",
"as": "PERMISSIVE",
"for": "SELECT",
- "to": [
- "public"
- ],
+ "to": ["public"],
"using": "EXISTS (\n SELECT 1 FROM \"chats\"\n WHERE \"chats\".id = chat_id\n AND \"chats\".visibility = 'public'\n )"
}
},
@@ -658,12 +642,8 @@
"name": "parts_message_id_messages_id_fk",
"tableFrom": "parts",
"tableTo": "messages",
- "columnsFrom": [
- "message_id"
- ],
- "columnsTo": [
- "id"
- ],
+ "columnsFrom": ["message_id"],
+ "columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
@@ -675,9 +655,7 @@
"name": "users_manage_message_parts",
"as": "PERMISSIVE",
"for": "ALL",
- "to": [
- "public"
- ],
+ "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 )"
},
@@ -685,9 +663,7 @@
"name": "public_chat_parts_readable",
"as": "PERMISSIVE",
"for": "SELECT",
- "to": [
- "public"
- ],
+ "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 )"
}
},
@@ -727,4 +703,4 @@
"schemas": {},
"tables": {}
}
-}
\ No newline at end of file
+}
diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json
index 5669c3b4..8ff5843c 100644
--- a/drizzle/meta/_journal.json
+++ b/drizzle/meta/_journal.json
@@ -80,4 +80,4 @@
"breakpoints": true
}
]
-}
\ No newline at end of file
+}
diff --git a/drizzle/relations.ts b/drizzle/relations.ts
index e7360289..a213d54e 100644
--- a/drizzle/relations.ts
+++ b/drizzle/relations.ts
@@ -1,21 +1,21 @@
-import { relations } from "drizzle-orm/relations";
-import { chats, messages, parts } from "./schema";
+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 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 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 file
+export const partsRelations = relations(parts, ({ one }) => ({
+ message: one(messages, {
+ fields: [parts.messageId],
+ references: [messages.id]
+ })
+}))
diff --git a/drizzle/schema.ts b/drizzle/schema.ts
index 2aacd55e..14ca94a0 100644
--- a/drizzle/schema.ts
+++ b/drizzle/schema.ts
@@ -1,116 +1,241 @@
-import { pgTable, index, pgPolicy, varchar, timestamp, text, foreignKey, jsonb, check, integer, json } from "drizzle-orm/pg-core"
-import { sql } from "drizzle-orm"
+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 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
+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"] }),
-]);
+ 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
+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))`),
-]);
+ 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"] }),
-]);
+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.ts
index 7743439d..ae7157dd 100644
--- a/lib/actions/site-feedback.ts
+++ b/lib/actions/site-feedback.ts
@@ -36,16 +36,14 @@ export async function submitFeedback(data: {
// 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
- })
+ await tx.insert(feedback).values({
+ id,
+ userId,
+ sentiment: data.sentiment,
+ message: data.message,
+ pageUrl: data.pageUrl,
+ userAgent
+ })
})
// Send to Slack if webhook URL is configured