using System; using System.Text.Json; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Codex.Dal.Migrations { /// public partial class InitialAgentSchema : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "Agents", columns: table => new { Id = table.Column(type: "uuid", nullable: false), Name = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Description = table.Column(type: "character varying(1000)", maxLength: 1000, nullable: false), Type = table.Column(type: "integer", nullable: false), ModelProvider = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ModelName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ProviderType = table.Column(type: "integer", nullable: false), ModelEndpoint = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), ApiKeyEncrypted = table.Column(type: "text", nullable: true), Temperature = table.Column(type: "double precision", nullable: false), MaxTokens = table.Column(type: "integer", nullable: false), SystemPrompt = table.Column(type: "text", nullable: false), EnableMemory = table.Column(type: "boolean", nullable: false), ConversationWindowSize = table.Column(type: "integer", nullable: false), Status = table.Column(type: "integer", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: false), IsDeleted = table.Column(type: "boolean", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Agents", x => x.Id); }); migrationBuilder.CreateTable( name: "Conversations", columns: table => new { Id = table.Column(type: "uuid", nullable: false), Title = table.Column(type: "character varying(500)", maxLength: 500, nullable: false), Summary = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: true), StartedAt = table.Column(type: "timestamp with time zone", nullable: false), LastMessageAt = table.Column(type: "timestamp with time zone", nullable: false), IsActive = table.Column(type: "boolean", nullable: false), MessageCount = table.Column(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Conversations", x => x.Id); }); migrationBuilder.CreateTable( name: "AgentTools", columns: table => new { Id = table.Column(type: "uuid", nullable: false), AgentId = table.Column(type: "uuid", nullable: false), ToolName = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Type = table.Column(type: "integer", nullable: false), Configuration = table.Column(type: "jsonb", nullable: true), McpServerUrl = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), McpAuthTokenEncrypted = table.Column(type: "text", nullable: true), ApiBaseUrl = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), ApiKeyEncrypted = table.Column(type: "text", nullable: true), IsEnabled = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_AgentTools", x => x.Id); table.ForeignKey( name: "FK_AgentTools_Agents_AgentId", column: x => x.AgentId, principalTable: "Agents", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "AgentExecutions", columns: table => new { Id = table.Column(type: "uuid", nullable: false), AgentId = table.Column(type: "uuid", nullable: false), ConversationId = table.Column(type: "uuid", nullable: true), UserPrompt = table.Column(type: "text", nullable: false), Input = table.Column(type: "text", nullable: true), Output = table.Column(type: "text", nullable: false, defaultValue: ""), StartedAt = table.Column(type: "timestamp with time zone", nullable: false), CompletedAt = table.Column(type: "timestamp with time zone", nullable: true), ExecutionTimeMs = table.Column(type: "bigint", nullable: true), InputTokens = table.Column(type: "integer", nullable: true), OutputTokens = table.Column(type: "integer", nullable: true), TotalTokens = table.Column(type: "integer", nullable: true), EstimatedCost = table.Column(type: "numeric(18,6)", precision: 18, scale: 6, nullable: true), ToolCalls = table.Column(type: "text", nullable: true), ToolCallResults = table.Column(type: "text", nullable: true), Status = table.Column(type: "integer", nullable: false), ErrorMessage = table.Column(type: "text", nullable: true) }, constraints: table => { table.PrimaryKey("PK_AgentExecutions", x => x.Id); table.ForeignKey( name: "FK_AgentExecutions_Agents_AgentId", column: x => x.AgentId, principalTable: "Agents", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_AgentExecutions_Conversations_ConversationId", column: x => x.ConversationId, principalTable: "Conversations", principalColumn: "Id", onDelete: ReferentialAction.SetNull); }); migrationBuilder.CreateTable( name: "ConversationMessages", columns: table => new { Id = table.Column(type: "uuid", nullable: false), ConversationId = table.Column(type: "uuid", nullable: false), Role = table.Column(type: "integer", nullable: false), Content = table.Column(type: "text", nullable: false), ToolCalls = table.Column(type: "text", nullable: true), ToolResults = table.Column(type: "text", nullable: true), MessageIndex = table.Column(type: "integer", nullable: false), IsInActiveWindow = table.Column(type: "boolean", nullable: false), TokenCount = table.Column(type: "integer", nullable: true), ExecutionId = table.Column(type: "uuid", nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_ConversationMessages", x => x.Id); table.ForeignKey( name: "FK_ConversationMessages_AgentExecutions_ExecutionId", column: x => x.ExecutionId, principalTable: "AgentExecutions", principalColumn: "Id", onDelete: ReferentialAction.SetNull); table.ForeignKey( name: "FK_ConversationMessages_Conversations_ConversationId", column: x => x.ConversationId, principalTable: "Conversations", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "IX_AgentExecutions_AgentId_StartedAt", table: "AgentExecutions", columns: new[] { "AgentId", "StartedAt" }, descending: new[] { false, true }); migrationBuilder.CreateIndex( name: "IX_AgentExecutions_ConversationId", table: "AgentExecutions", column: "ConversationId"); migrationBuilder.CreateIndex( name: "IX_AgentExecutions_Status", table: "AgentExecutions", column: "Status"); migrationBuilder.CreateIndex( name: "IX_Agents_Status_IsDeleted", table: "Agents", columns: new[] { "Status", "IsDeleted" }); migrationBuilder.CreateIndex( name: "IX_Agents_Type", table: "Agents", column: "Type"); migrationBuilder.CreateIndex( name: "IX_AgentTools_AgentId_IsEnabled", table: "AgentTools", columns: new[] { "AgentId", "IsEnabled" }); migrationBuilder.CreateIndex( name: "IX_AgentTools_Type", table: "AgentTools", column: "Type"); migrationBuilder.CreateIndex( name: "IX_ConversationMessages_ConversationId_IsInActiveWindow_Messag~", table: "ConversationMessages", columns: new[] { "ConversationId", "IsInActiveWindow", "MessageIndex" }); migrationBuilder.CreateIndex( name: "IX_ConversationMessages_ConversationId_MessageIndex", table: "ConversationMessages", columns: new[] { "ConversationId", "MessageIndex" }); migrationBuilder.CreateIndex( name: "IX_ConversationMessages_ExecutionId", table: "ConversationMessages", column: "ExecutionId"); migrationBuilder.CreateIndex( name: "IX_ConversationMessages_Role", table: "ConversationMessages", column: "Role"); migrationBuilder.CreateIndex( name: "IX_Conversations_IsActive_LastMessageAt", table: "Conversations", columns: new[] { "IsActive", "LastMessageAt" }, descending: new[] { false, true }); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "AgentTools"); migrationBuilder.DropTable( name: "ConversationMessages"); migrationBuilder.DropTable( name: "AgentExecutions"); migrationBuilder.DropTable( name: "Agents"); migrationBuilder.DropTable( name: "Conversations"); } } }