feat(05-01): add messages table to schema

- Add messages table with sender/recipient pattern for agent-user communication
- Support message types: question, info, error, response
- Add requiresResponse flag and status tracking (pending, read, responded)
- Include parentMessageId for threading responses to original questions
- Add relations for sender/recipient agents and message threading
This commit is contained in:
Lukas May
2026-01-30 20:32:31 +01:00
parent 14f2e472f8
commit f873a32ff4

View File

@@ -197,3 +197,54 @@ export const agentsRelations = relations(agents, ({ one }) => ({
export type Agent = InferSelectModel<typeof agents>;
export type NewAgent = InferInsertModel<typeof agents>;
// ============================================================================
// MESSAGES
// ============================================================================
export const messages = sqliteTable('messages', {
id: text('id').primaryKey(),
senderType: text('sender_type', { enum: ['agent', 'user'] }).notNull(),
senderId: text('sender_id').references(() => agents.id, { onDelete: 'set null' }), // Agent ID if senderType='agent', null for user
recipientType: text('recipient_type', { enum: ['agent', 'user'] }).notNull(),
recipientId: text('recipient_id').references(() => agents.id, { onDelete: 'set null' }), // Agent ID if recipientType='agent', null for user
type: text('type', { enum: ['question', 'info', 'error', 'response'] })
.notNull()
.default('info'),
content: text('content').notNull(),
requiresResponse: integer('requires_response', { mode: 'boolean' })
.notNull()
.default(false),
status: text('status', { enum: ['pending', 'read', 'responded'] })
.notNull()
.default('pending'),
parentMessageId: text('parent_message_id').references((): ReturnType<typeof text> => messages.id, { onDelete: 'set null' }), // Links response to original question
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
export const messagesRelations = relations(messages, ({ one, many }) => ({
// Sender agent (optional - null for user senders)
senderAgent: one(agents, {
fields: [messages.senderId],
references: [agents.id],
relationName: 'senderAgent',
}),
// Recipient agent (optional - null for user recipients)
recipientAgent: one(agents, {
fields: [messages.recipientId],
references: [agents.id],
relationName: 'recipientAgent',
}),
// Parent message (for threading responses)
parentMessage: one(messages, {
fields: [messages.parentMessageId],
references: [messages.id],
relationName: 'parentMessage',
}),
// Child messages (replies to this message)
childMessages: many(messages, { relationName: 'parentMessage' }),
}));
export type Message = InferSelectModel<typeof messages>;
export type NewMessage = InferInsertModel<typeof messages>;