|
| 1 | +import { D1Orm } from 'd1-orm'; |
| 2 | +import { userAgentsModel, userTasksModel, UserAgentsTable } from '../models'; |
| 3 | + |
| 4 | +/** AGENT MANAGEMENT */ |
| 5 | + |
| 6 | +interface AgentResult { |
| 7 | + success: boolean; |
| 8 | + error?: string; |
| 9 | + agent?: UserAgentsTable; |
| 10 | + agents?: UserAgentsTable[]; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Get all agents for a specific crew |
| 15 | + * @param orm The D1Orm instance from durable object class |
| 16 | + * @param crewId The ID of the crew to get agents for |
| 17 | + * @returns Promise containing array of agents and metadata |
| 18 | + * @throws Error if database query fails |
| 19 | + */ |
| 20 | +export async function getAgents(orm: D1Orm, crewId: number): Promise<AgentResult> { |
| 21 | + try { |
| 22 | + userAgentsModel.SetOrm(orm); |
| 23 | + const result = await userAgentsModel.All({ |
| 24 | + where: { |
| 25 | + crew_id: crewId |
| 26 | + }, |
| 27 | + orderBy: [ |
| 28 | + { |
| 29 | + column: 'created_at', |
| 30 | + descending: true, |
| 31 | + }, |
| 32 | + ], |
| 33 | + }); |
| 34 | + return { |
| 35 | + agents: result.results as unknown as UserAgentsTable[], |
| 36 | + success: true |
| 37 | + }; |
| 38 | + } catch (error) { |
| 39 | + console.error(`Error in getAgents: ${error instanceof Error ? error.message : String(error)}`); |
| 40 | + return { |
| 41 | + success: false, |
| 42 | + error: `Failed to get agents: ${error instanceof Error ? error.message : String(error)}` |
| 43 | + }; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Create a new agent |
| 49 | + * @param orm The D1Orm instance from durable object class |
| 50 | + * @param agentData The agent configuration data |
| 51 | + * @returns Promise containing the created agent or error details |
| 52 | + * @throws Error if database insertion fails |
| 53 | + */ |
| 54 | +export async function createAgent( |
| 55 | + orm: D1Orm, |
| 56 | + agentData: Omit<UserAgentsTable, 'id' | 'created_at' | 'updated_at'> |
| 57 | +): Promise<AgentResult> { |
| 58 | + try { |
| 59 | + userAgentsModel.SetOrm(orm); |
| 60 | + const agent = await userAgentsModel.InsertOne(agentData); |
| 61 | + return { |
| 62 | + agent: agent as unknown as UserAgentsTable, |
| 63 | + success: true |
| 64 | + }; |
| 65 | + } catch (error) { |
| 66 | + console.error(`Error in createAgent: ${error instanceof Error ? error.message : String(error)}`); |
| 67 | + return { |
| 68 | + success: false, |
| 69 | + error: `Failed to create agent: ${error instanceof Error ? error.message : String(error)}` |
| 70 | + }; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Update an existing agent's configuration |
| 76 | + * @param orm The D1Orm instance from durable object class |
| 77 | + * @param agentId The ID of the agent to update |
| 78 | + * @param updates The fields to update on the agent |
| 79 | + * @returns Promise containing the update result or error details |
| 80 | + * @throws Error if database update fails |
| 81 | + */ |
| 82 | +export async function updateAgent( |
| 83 | + orm: D1Orm, |
| 84 | + agentId: number, |
| 85 | + updates: Partial<Omit<UserAgentsTable, 'id' | 'created_at' | 'updated_at' | 'profile_id' | 'crew_id'>> |
| 86 | +): Promise<AgentResult> { |
| 87 | + try { |
| 88 | + userAgentsModel.SetOrm(orm); |
| 89 | + await userAgentsModel.Update({ |
| 90 | + where: { |
| 91 | + id: agentId |
| 92 | + }, |
| 93 | + data: updates |
| 94 | + }); |
| 95 | + return { |
| 96 | + success: true |
| 97 | + }; |
| 98 | + } catch (error) { |
| 99 | + console.error(`Error in updateAgent: ${error instanceof Error ? error.message : String(error)}`); |
| 100 | + return { |
| 101 | + success: false, |
| 102 | + error: `Failed to update agent: ${error instanceof Error ? error.message : String(error)}` |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Delete an agent and all associated tasks |
| 109 | + * @param orm The D1Orm instance from durable object class |
| 110 | + * @param agentId The ID of the agent to delete |
| 111 | + * @returns Promise containing the deletion result or error details |
| 112 | + * @throws Error if database deletion fails |
| 113 | + */ |
| 114 | +export async function deleteAgent(orm: D1Orm, agentId: number): Promise<{ |
| 115 | + success: boolean; |
| 116 | + error?: string; |
| 117 | +}> { |
| 118 | + try { |
| 119 | + userTasksModel.SetOrm(orm); |
| 120 | + userAgentsModel.SetOrm(orm); |
| 121 | + |
| 122 | + // First delete all tasks associated with this agent |
| 123 | + await userTasksModel.Delete({ |
| 124 | + where: { |
| 125 | + agent_id: agentId |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + // Then delete the agent |
| 130 | + await userAgentsModel.Delete({ |
| 131 | + where: { |
| 132 | + id: agentId |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + return { |
| 137 | + success: true |
| 138 | + }; |
| 139 | + } catch (error) { |
| 140 | + console.error(`Error in deleteAgent: ${error instanceof Error ? error.message : String(error)}`); |
| 141 | + return { |
| 142 | + success: false, |
| 143 | + error: `Failed to delete agent: ${error instanceof Error ? error.message : String(error)}` |
| 144 | + }; |
| 145 | + } |
| 146 | +} |
0 commit comments