|
| 1 | +"""Service functions for agent draft operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from epyxid import XID |
| 6 | +from fastapi import status |
| 7 | +from sqlalchemy import desc, select |
| 8 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 9 | + |
| 10 | +from intentkit.models.agent import Agent, AgentUserInput |
| 11 | +from intentkit.models.draft import AgentDraft, AgentDraftTable |
| 12 | +from intentkit.utils.error import IntentKitAPIError |
| 13 | + |
| 14 | + |
| 15 | +async def update_agent_draft( |
| 16 | + *, |
| 17 | + agent_id: str, |
| 18 | + user_id: str, |
| 19 | + input: AgentUserInput, |
| 20 | + db: AsyncSession, |
| 21 | +) -> AgentDraft: |
| 22 | + """Update the latest draft for the specified agent with partial field updates. |
| 23 | +
|
| 24 | + This function only updates fields that are explicitly provided in the input, |
| 25 | + leaving other fields unchanged. This is more efficient than override as it |
| 26 | + reduces context usage and minimizes the risk of accidentally changing fields. |
| 27 | + """ |
| 28 | + query = ( |
| 29 | + select(AgentDraftTable) |
| 30 | + .where(AgentDraftTable.agent_id == agent_id, AgentDraftTable.owner == user_id) |
| 31 | + .order_by(desc(AgentDraftTable.created_at)) |
| 32 | + .limit(1) |
| 33 | + ) |
| 34 | + |
| 35 | + result = await db.execute(query) |
| 36 | + latest_draft = result.scalar_one_or_none() |
| 37 | + |
| 38 | + if not latest_draft: |
| 39 | + raise IntentKitAPIError( |
| 40 | + status.HTTP_404_NOT_FOUND, |
| 41 | + "DraftNotFound", |
| 42 | + "No drafts found for this agent", |
| 43 | + ) |
| 44 | + |
| 45 | + # Get only the fields that are explicitly provided (exclude_unset=True) |
| 46 | + update_data = input.model_dump(exclude_unset=True) |
| 47 | + |
| 48 | + if latest_draft.deployed_at is not None: |
| 49 | + # Create new draft version if current one is deployed |
| 50 | + draft_id = str(XID()) |
| 51 | + |
| 52 | + # Start with existing draft data and merge updates |
| 53 | + draft_data = AgentUserInput.model_validate(latest_draft).model_dump() |
| 54 | + draft_data.update(update_data) |
| 55 | + |
| 56 | + updated_input = AgentUserInput.model_validate(draft_data) |
| 57 | + |
| 58 | + draft_table = AgentDraftTable( |
| 59 | + id=draft_id, |
| 60 | + agent_id=agent_id, |
| 61 | + owner=user_id, |
| 62 | + version=updated_input.hash(), |
| 63 | + last_draft_id=latest_draft.id, |
| 64 | + project_id=latest_draft.project_id, |
| 65 | + **draft_data, |
| 66 | + ) |
| 67 | + |
| 68 | + db.add(draft_table) |
| 69 | + await db.commit() |
| 70 | + await db.refresh(draft_table) |
| 71 | + |
| 72 | + return AgentDraft.model_validate(draft_table) |
| 73 | + |
| 74 | + # Update existing draft in-place |
| 75 | + for key, value in update_data.items(): |
| 76 | + setattr(latest_draft, key, value) |
| 77 | + |
| 78 | + # Update version hash based on updated data |
| 79 | + updated_input = AgentUserInput.model_validate(latest_draft) |
| 80 | + latest_draft.version = updated_input.hash() |
| 81 | + |
| 82 | + await db.commit() |
| 83 | + await db.refresh(latest_draft) |
| 84 | + |
| 85 | + return AgentDraft.model_validate(latest_draft) |
| 86 | + |
| 87 | + |
| 88 | +async def override_agent_draft( |
| 89 | + *, |
| 90 | + agent_id: str, |
| 91 | + user_id: str, |
| 92 | + input: AgentUserInput, |
| 93 | + db: AsyncSession, |
| 94 | +) -> AgentDraft: |
| 95 | + """Override the latest draft for the specified agent.""" |
| 96 | + query = ( |
| 97 | + select(AgentDraftTable) |
| 98 | + .where(AgentDraftTable.agent_id == agent_id, AgentDraftTable.owner == user_id) |
| 99 | + .order_by(desc(AgentDraftTable.created_at)) |
| 100 | + .limit(1) |
| 101 | + ) |
| 102 | + |
| 103 | + result = await db.execute(query) |
| 104 | + latest_draft = result.scalar_one_or_none() |
| 105 | + |
| 106 | + if not latest_draft: |
| 107 | + raise IntentKitAPIError( |
| 108 | + status.HTTP_404_NOT_FOUND, |
| 109 | + "DraftNotFound", |
| 110 | + "No drafts found for this agent", |
| 111 | + ) |
| 112 | + |
| 113 | + if latest_draft.deployed_at is not None: |
| 114 | + draft_id = str(XID()) |
| 115 | + |
| 116 | + draft_table = AgentDraftTable( |
| 117 | + id=draft_id, |
| 118 | + agent_id=agent_id, |
| 119 | + owner=user_id, |
| 120 | + version=input.hash(), |
| 121 | + last_draft_id=latest_draft.id, |
| 122 | + project_id=latest_draft.project_id, |
| 123 | + **input.model_dump(), |
| 124 | + ) |
| 125 | + |
| 126 | + db.add(draft_table) |
| 127 | + await db.commit() |
| 128 | + await db.refresh(draft_table) |
| 129 | + |
| 130 | + return AgentDraft.model_validate(draft_table) |
| 131 | + |
| 132 | + for key, value in input.model_dump().items(): |
| 133 | + setattr(latest_draft, key, value) |
| 134 | + |
| 135 | + latest_draft.version = input.hash() |
| 136 | + |
| 137 | + await db.commit() |
| 138 | + await db.refresh(latest_draft) |
| 139 | + |
| 140 | + return AgentDraft.model_validate(latest_draft) |
| 141 | + |
| 142 | + |
| 143 | +async def get_agent_latest_draft( |
| 144 | + *, |
| 145 | + agent_id: str, |
| 146 | + user_id: str, |
| 147 | + db: AsyncSession, |
| 148 | +) -> AgentDraft: |
| 149 | + """Return the latest draft for the specified agent.""" |
| 150 | + query = ( |
| 151 | + select(AgentDraftTable) |
| 152 | + .where(AgentDraftTable.agent_id == agent_id, AgentDraftTable.owner == user_id) |
| 153 | + .order_by(desc(AgentDraftTable.created_at)) |
| 154 | + .limit(1) |
| 155 | + ) |
| 156 | + |
| 157 | + result = await db.execute(query) |
| 158 | + latest_draft = result.scalar_one_or_none() |
| 159 | + |
| 160 | + if latest_draft: |
| 161 | + return AgentDraft.model_validate(latest_draft) |
| 162 | + |
| 163 | + agent = await Agent.get(agent_id) |
| 164 | + |
| 165 | + if not agent: |
| 166 | + raise IntentKitAPIError( |
| 167 | + status.HTTP_404_NOT_FOUND, |
| 168 | + "AgentNotFound", |
| 169 | + "No drafts found for this agent", |
| 170 | + ) |
| 171 | + |
| 172 | + if agent.owner != user_id: |
| 173 | + raise IntentKitAPIError( |
| 174 | + status.HTTP_403_FORBIDDEN, |
| 175 | + "Forbidden", |
| 176 | + "Not your agent", |
| 177 | + ) |
| 178 | + |
| 179 | + draft_id = str(XID()) |
| 180 | + |
| 181 | + agent_dict = agent.model_dump() |
| 182 | + input_dict: dict[str, object] = {} |
| 183 | + for key in AgentUserInput.model_fields: |
| 184 | + if key in agent_dict: |
| 185 | + input_dict[key] = agent_dict[key] |
| 186 | + input = AgentUserInput.model_validate(input_dict) |
| 187 | + |
| 188 | + draft_table = AgentDraftTable( |
| 189 | + id=draft_id, |
| 190 | + agent_id=agent_id, |
| 191 | + owner=user_id, |
| 192 | + version=input.hash(), |
| 193 | + deployed_at=agent.updated_at, |
| 194 | + **input.model_dump(), |
| 195 | + ) |
| 196 | + |
| 197 | + db.add(draft_table) |
| 198 | + await db.commit() |
| 199 | + await db.refresh(draft_table) |
| 200 | + |
| 201 | + return AgentDraft.model_validate(draft_table) |
0 commit comments