π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(database): repair Zod 4 + drizzle-zod insert schema type inference (#17072)
AmAzing-a6e57eaef4ba
diff --git a/apps/server/src/routers/lambda/thread.ts b/apps/server/src/routers/lambda/thread.tsindex 1b48b53cf60..bec7ca68f88 100644--- a/apps/server/src/routers/lambda/thread.ts+++ b/apps/server/src/routers/lambda/thread.ts@@ -5,7 +5,7 @@ import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPerimport { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';import { MessageModel } from '@/database/models/message';import { ThreadModel } from '@/database/models/thread';-import { insertThreadSchema } from '@/database/schemas';+import { updateThreadSchema } from '@/database/schemas';import { router } from '@/libs/trpc/lambda';import { serverDatabase } from '@/libs/trpc/lambda/middleware';import { type ThreadItem } from '@/types/topic/thread';@@ -120,7 +120,7 @@ export const threadRouter = router({.input(z.object({id: z.string(),- value: insertThreadSchema.partial(),+ value: updateThreadSchema,}),).mutation(async ({ input, ctx }) => {diff --git a/packages/database/package.json b/packages/database/package.jsonindex c3e3078d1fc..58e1d0c47d8 100644--- a/packages/database/package.json+++ b/packages/database/package.json@@ -34,7 +34,7 @@"peerDependencies": {"dayjs": "*","drizzle-orm": "*",- "drizzle-zod": "*",+ "drizzle-zod": "^0.8.3","nanoid": "*","pg": "*","zod": "^4.4.3"diff --git a/packages/database/src/schemas/agent.ts b/packages/database/src/schemas/agent.tsindex 7f6e3c898f6..886df2ec5fa 100644--- a/packages/database/src/schemas/agent.ts+++ b/packages/database/src/schemas/agent.ts@@ -29,6 +29,9 @@ import { workspaces } from './workspace';// agent is a model that represents the assistant that is created by the user// agent can have its own knowledge base and files+/** Agent visibility β shared by column def and insert schema. */+export const AGENT_VISIBILITY = ['private', 'public'] as const;+export const agents = pgTable('agents',{@@ -80,9 +83,7 @@ export const agents = pgTable(* the creator (`user_id`). Ignored in personal mode where the row is* implicitly private to its owner.*/- visibility: text('visibility', { enum: ['private', 'public'] })- .default('public')- .notNull(),+ visibility: text('visibility', { enum: AGENT_VISIBILITY }).default('public').notNull(),...timestamps,},@@ -106,6 +107,10 @@ export const insertAgentSchema = createInsertSchema(agents, {agencyConfig: z.custom<LobeAgentAgencyConfig>().nullish(),// Override chatConfig type to use the proper schemachatConfig: AgentChatConfigSchema.nullish(),+ // See insertSessionGroupSchema: Zod 4 + drizzle-zod text-enum inference pollution.+ // `.optional()` preserves defaulted-column omit semantics at runtime.+ // Enum values from AGENT_VISIBILITY so column def and schema stay in sync.+ visibility: z.enum(AGENT_VISIBILITY).optional(),});export type NewAgent = typeof agents.$inferInsert;diff --git a/packages/database/src/schemas/file.ts b/packages/database/src/schemas/file.tsindex 709380a3efe..29f3d9ac7bb 100644--- a/packages/database/src/schemas/file.ts+++ b/packages/database/src/schemas/file.ts@@ -13,6 +13,7 @@ import {varchar,} from 'drizzle-orm/pg-core';import { createInsertSchema } from 'drizzle-zod';+import { z } from 'zod';import type { LobeDocumentPage } from '@/types/document';import type { FileSource } from '@/types/files';@@ -243,6 +244,9 @@ export const files = pgTable(export type NewFile = typeof files.$inferInsert;export type FileItem = typeof files.$inferSelect;+/** Knowledge-base visibility β shared by column def and insert schema. */+export const KNOWLEDGE_BASE_VISIBILITY = ['private', 'public'] as const;+export const knowledgeBases = pgTable('knowledge_bases',{@@ -278,9 +282,7 @@ export const knowledgeBases = pgTable(* (file-level workspace visibility). This column only gates *KB list** enumeration; retrieval via a known KB id still goes through `ownership()`.*/- visibility: text('visibility', { enum: ['private', 'public'] })- .default('public')- .notNull(),+ visibility: text('visibility', { enum: KNOWLEDGE_BASE_VISIBILITY }).default('public').notNull(),...timestamps,},@@ -292,7 +294,12 @@ export const knowledgeBases = pgTable(],);-export const insertKnowledgeBasesSchema = createInsertSchema(knowledgeBases);+// See insertSessionGroupSchema: Zod 4 + drizzle-zod text-enum inference pollution.+// `.optional()` preserves defaulted-column omit semantics at runtime.+// Enum values from KNOWLEDGE_BASE_VISIBILITY so column def and schema stay in sync.+export const insertKnowledgeBasesSchema = createInsertSchema(knowledgeBases, {+ visibility: z.enum(KNOWLEDGE_BASE_VISIBILITY).optional(),+});export type NewKnowledgeBase = typeof knowledgeBases.$inferInsert;export type KnowledgeBaseItem = typeof knowledgeBases.$inferSelect;diff --git a/packages/database/src/schemas/session.ts b/packages/database/src/schemas/session.tsindex d9868598047..5dba88912aa 100644--- a/packages/database/src/schemas/session.ts+++ b/packages/database/src/schemas/session.ts@@ -1,12 +1,19 @@import { isNotNull, isNull } from 'drizzle-orm';import { boolean, index, integer, pgTable, text, uniqueIndex, varchar } from 'drizzle-orm/pg-core';import { createInsertSchema } from 'drizzle-zod';+import { z } from 'zod';import { idGenerator, randomSlug } from '../utils/idGenerator';import { timestamps } from './_helpers';import { users } from './user';import { workspaces } from './workspace';+/** Session-group visibility β shared by column def and insert schema. */+export const SESSION_GROUP_VISIBILITY = ['private', 'public'] as const;++/** Session type β shared by column def and insert schema. */+export const SESSION_TYPE = ['agent', 'group'] as const;+// ======= sessionGroups ======= //export const sessionGroups = pgTable(@@ -30,9 +37,7 @@ export const sessionGroups = pgTable(* workspace member can see the folder; `private` constrains it to the* creator (`user_id`). Ignored in personal mode.*/- visibility: text('visibility', { enum: ['private', 'public'] })- .default('public')- .notNull(),+ visibility: text('visibility', { enum: SESSION_GROUP_VISIBILITY }).default('public').notNull(),...timestamps,},@@ -51,7 +56,16 @@ export const sessionGroups = pgTable(}),);-export const insertSessionGroupSchema = createInsertSchema(sessionGroups);+// Explicit z.enum overrides: drizzle-zod + Zod 4 maps text(..., { enum }) to a+// ZodEnum whose inferred output pollutes with String prototype members+// (e.g. `"private" | "public" | 2 | (() => string) | ...`), which breaks+// assignability to Drizzle `$inferSelect` partials used by model.update.+// Keep `.optional()` so defaulted columns stay omit-able (bare z.enum would+// make them required and change runtime insert validation).+// Enum values from SESSION_GROUP_VISIBILITY so column def and schema stay in sync.+export const insertSessionGroupSchema = createInsertSchema(sessionGroups, {+ visibility: z.enum(SESSION_GROUP_VISIBILITY).optional(),+});export type NewSessionGroup = typeof sessionGroups.$inferInsert;export type SessionGroupItem = typeof sessionGroups.$inferSelect;@@ -72,7 +86,7 @@ export const sessions = pgTable(avatar: text('avatar'),backgroundColor: text('background_color'),- type: text('type', { enum: ['agent', 'group'] }).default('agent'),+ type: text('type', { enum: SESSION_TYPE }).default('agent'),userId: text('user_id').references(() => users.id, { onDelete: 'cascade' })@@ -99,7 +113,10 @@ export const sessions = pgTable(],);-export const insertSessionSchema = createInsertSchema(sessions);+export const insertSessionSchema = createInsertSchema(sessions, {+ // column is nullable + default β match drizzle-zod insert optionality+ type: z.enum(SESSION_TYPE).nullish(),+});// export const selectSessionSchema = createSelectSchema(sessions);export type NewSession = typeof sessions.$inferInsert;diff --git a/packages/database/src/schemas/topic.ts b/packages/database/src/schemas/topic.tsindex a51fd5adada..26f47f587af 100644--- a/packages/database/src/schemas/topic.ts+++ b/packages/database/src/schemas/topic.ts@@ -10,7 +10,8 @@ import {text,uniqueIndex,} from 'drizzle-orm/pg-core';-import { createInsertSchema } from 'drizzle-zod';+import { createInsertSchema, createUpdateSchema } from 'drizzle-zod';+import { z } from 'zod';import { createNanoId, idGenerator } from '../utils/idGenerator';import { amountNumeric, createdAt, timestamps, timestamptz } from './_helpers';@@ -106,6 +107,21 @@ export const topics = pgTable(export type NewTopic = typeof topics.$inferInsert;export type TopicItem = typeof topics.$inferSelect;+/** Single source of truth for thread `type` β column def + zod schemas share this. */+export const THREAD_TYPE = ['continuation', 'standalone', 'isolation', 'eval'] as const;++/** Single source of truth for thread `status` β column def + zod schemas share this. */+export const THREAD_STATUS = [+ 'active',+ 'processing',+ 'pending',+ 'inReview',+ 'todo',+ 'cancel',+ 'completed',+ 'failed',+] as const;+// @ts-ignoreexport const threads = pgTable('threads',@@ -117,19 +133,8 @@ export const threads = pgTable(title: text('title'),content: text('content'),editor_data: jsonb('editor_data'),- type: text('type', { enum: ['continuation', 'standalone', 'isolation', 'eval'] }).notNull(),- status: text('status', {- enum: [- 'active',- 'processing',- 'pending',- 'inReview',- 'todo',- 'cancel',- 'completed',- 'failed',- ],- }),+ type: text('type', { enum: THREAD_TYPE }).notNull(),+ status: text('status', { enum: THREAD_STATUS }),topicId: text('topic_id').references(() => topics.id, { onDelete: 'cascade' })@@ -165,7 +170,26 @@ export const threads = pgTable(export type NewThread = typeof threads.$inferInsert;export type ThreadItem = typeof threads.$inferSelect;-export const insertThreadSchema = createInsertSchema(threads);+// Explicit enum/jsonb overrides: plain createInsertSchema(threads) hits TS2589+// (excessively deep instantiation) under Zod 4 because of self-ref parentThreadId+// plus multi-value text enums; overrides keep inference shallow and correct.+// Preserve insert nullability: `type` is notNull (required), `status` is nullable.+// Enum values come from THREAD_TYPE / THREAD_STATUS so column def and schema can't drift.+export const insertThreadSchema = createInsertSchema(threads, {+ metadata: z.custom<ThreadMetadata>().optional(),+ status: z.enum(THREAD_STATUS).nullish(),+ type: z.enum(THREAD_TYPE),+});++// Prefer createUpdateSchema over insertThreadSchema.partial() β .partial() on the+// insert schema still blows the instantiation depth limit (TS2589) under Zod 4.+// All refined fields must be optional: bare z.enum refinements are NOT auto-wrapped+// by createUpdateSchema and would require type/status on every partial update.+export const updateThreadSchema = createUpdateSchema(threads, {+ metadata: z.custom<ThreadMetadata>().optional(),+ status: z.enum(THREAD_STATUS).nullish(),+ type: z.enum(THREAD_TYPE).optional(),+});/*** Document-Topic association table - Implements many-to-many relationship between documents and topics