CREATE TABLE `agent_message_feedback` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`message_id` text NOT NULL,
	`user_id` integer NOT NULL,
	`feedback` text(10) NOT NULL,
	`comment` text,
	`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	`updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	FOREIGN KEY (`message_id`) REFERENCES `agent_messages`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_feedback_message_user` ON `agent_message_feedback` (`message_id`,`user_id`);--> statement-breakpoint
CREATE TABLE `agent_messages` (
	`id` text PRIMARY KEY NOT NULL,
	`session_id` text NOT NULL,
	`role` text(20) NOT NULL,
	`content` text DEFAULT '' NOT NULL,
	`parts` text,
	`model_id` integer,
	`input_tokens` integer,
	`output_tokens` integer,
	`sort_order` integer NOT NULL,
	`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	FOREIGN KEY (`session_id`) REFERENCES `agent_sessions`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_agent_messages_session_sort` ON `agent_messages` (`session_id`,`sort_order`);--> statement-breakpoint
CREATE TABLE `agent_sessions` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` integer,
	`temp_token` text(64),
	`title` text(100) DEFAULT '新对话' NOT NULL,
	`model_id` integer,
	`enable_thinking` integer DEFAULT 1 NOT NULL,
	`enable_tools` integer DEFAULT 1 NOT NULL,
	`last_active_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	`expires_at` integer,
	`deleted_at` integer,
	`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	`updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_agent_sessions_user_deleted` ON `agent_sessions` (`user_id`,`deleted_at`);--> statement-breakpoint
CREATE INDEX `idx_agent_sessions_temp_token` ON `agent_sessions` (`temp_token`);--> statement-breakpoint
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_llm_providers` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` integer,
	`name` text(100) NOT NULL,
	`slug` text(100) NOT NULL,
	`base_url` text(500),
	`parse_mode` text DEFAULT 'openai' NOT NULL,
	`api_key` text,
	`status` text DEFAULT 'active' NOT NULL,
	`description` text,
	`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	`updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
INSERT INTO `__new_llm_providers`("id", "user_id", "name", "slug", "base_url", "parse_mode", "api_key", "status", "description", "created_at", "updated_at") SELECT "id", "user_id", "name", "slug", "base_url", "parse_mode", "api_key", "status", "description", "created_at", "updated_at" FROM `llm_providers`;--> statement-breakpoint
DROP TABLE `llm_providers`;--> statement-breakpoint
ALTER TABLE `__new_llm_providers` RENAME TO `llm_providers`;--> statement-breakpoint
PRAGMA foreign_keys=ON;--> statement-breakpoint
CREATE UNIQUE INDEX `idx_llm_provider_user_slug` ON `llm_providers` (`user_id`,`slug`);--> statement-breakpoint
CREATE INDEX `idx_llm_provider_user` ON `llm_providers` (`user_id`);--> statement-breakpoint
CREATE INDEX `idx_llm_provider_status` ON `llm_providers` (`status`);