Skip to content

Commit 4da859a

Browse files
committed
refactor: public info
1 parent f850d70 commit 4da859a

File tree

7 files changed

+109
-122
lines changed

7 files changed

+109
-122
lines changed

intentkit/core/agent/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from .management import create_agent, deploy_agent, override_agent, patch_agent
99
from .notifications import send_agent_notification
10+
from .public_info import override_public_info, update_public_info
1011
from .queries import get_agent, get_agent_by_id_or_slug, iterate_agent_id_batches
1112
from .wallet import process_agent_wallet
1213

@@ -25,4 +26,6 @@
2526
"update_agents_account_snapshot",
2627
"update_agents_assets",
2728
"update_agents_statistics",
29+
"update_public_info",
30+
"override_public_info",
2831
]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from sqlalchemy import func, select
6+
7+
from intentkit.config.db import get_session
8+
from intentkit.models.agent import AgentPublicInfo, AgentTable
9+
from intentkit.utils.error import IntentKitAPIError
10+
11+
if TYPE_CHECKING:
12+
from intentkit.models.agent import Agent
13+
14+
15+
async def update_public_info(*, agent_id: str, public_info: AgentPublicInfo) -> Agent:
16+
"""Update agent public info with only the fields that are explicitly provided."""
17+
from intentkit.models.agent import Agent
18+
19+
async with get_session() as session:
20+
result = await session.execute(
21+
select(AgentTable).where(AgentTable.id == agent_id)
22+
)
23+
db_agent = result.scalar_one_or_none()
24+
25+
if not db_agent:
26+
raise IntentKitAPIError(404, "NotFound", f"Agent {agent_id} not found")
27+
28+
update_data = public_info.model_dump(exclude_unset=True)
29+
30+
for key, value in update_data.items():
31+
if hasattr(db_agent, key):
32+
setattr(db_agent, key, value)
33+
34+
db_agent.public_info_updated_at = func.now()
35+
36+
await session.commit()
37+
await session.refresh(db_agent)
38+
39+
return Agent.model_validate(db_agent)
40+
41+
42+
async def override_public_info(*, agent_id: str, public_info: AgentPublicInfo) -> Agent:
43+
"""Override agent public info with all fields from this instance."""
44+
from intentkit.models.agent import Agent
45+
46+
async with get_session() as session:
47+
result = await session.execute(
48+
select(AgentTable).where(AgentTable.id == agent_id)
49+
)
50+
db_agent = result.scalar_one_or_none()
51+
52+
if not db_agent:
53+
raise IntentKitAPIError(404, "NotFound", f"Agent {agent_id} not found")
54+
55+
update_data = public_info.model_dump()
56+
for key, value in update_data.items():
57+
if hasattr(db_agent, key):
58+
setattr(db_agent, key, value)
59+
60+
db_agent.public_info_updated_at = func.now()
61+
62+
await session.commit()
63+
await session.refresh(db_agent)
64+
65+
return Agent.model_validate(db_agent)

intentkit/core/manager/skills/public_info.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from langchain_core.tools import ArgsSchema
99
from pydantic import BaseModel
1010

11+
from intentkit.core.agent.public_info import update_public_info
1112
from intentkit.core.manager.service import get_latest_public_info
1213
from intentkit.core.manager.skills.base import ManagerSkill
1314
from intentkit.models.agent import AgentPublicInfo
@@ -88,7 +89,10 @@ async def _arun(self, **kwargs: Any) -> str:
8889
)
8990

9091
public_info = AgentPublicInfo.model_validate(kwargs["public_info_update"])
91-
updated_agent = await public_info.update(context.agent_id)
92+
updated_agent = await update_public_info(
93+
agent_id=context.agent_id,
94+
public_info=public_info,
95+
)
9296
updated_public_info = AgentPublicInfo.model_validate(updated_agent)
9397

9498
return json.dumps(updated_public_info.model_dump(mode="json"), indent=2)

intentkit/models/agent/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from .autonomous import AgentAutonomous, AgentAutonomousStatus
44
from .core import AgentCore, AgentVisibility
55
from .db import AgentTable, AgentUserInputColumns
6-
from .example import AgentExample
7-
from .public_info import AgentPublicInfo
6+
from .public_info import AgentExample, AgentPublicInfo
87
from .response import AgentResponse
98
from .user_input import AgentCreate, AgentUpdate, AgentUserInput
109

intentkit/models/agent/example.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

intentkit/models/agent/public_info.py

Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
from __future__ import annotations
22

33
from decimal import Decimal
4-
from typing import TYPE_CHECKING, Annotated, Any
4+
from typing import Annotated, Any
55

66
from pydantic import BaseModel, ConfigDict
77
from pydantic import Field as PydanticField
8-
from sqlalchemy import func, select
98

10-
from intentkit.config.db import get_session
11-
from intentkit.models.agent.db import AgentTable
12-
from intentkit.models.agent.example import AgentExample
13-
from intentkit.utils.error import IntentKitAPIError
149

15-
if TYPE_CHECKING:
16-
from intentkit.models.agent.agent import Agent
10+
class AgentExample(BaseModel):
11+
"""Agent example configuration."""
12+
13+
name: Annotated[
14+
str,
15+
PydanticField(
16+
description="Name of the example",
17+
max_length=50,
18+
json_schema_extra={
19+
"x-placeholder": "Add a name for the example",
20+
},
21+
),
22+
]
23+
description: Annotated[
24+
str,
25+
PydanticField(
26+
description="Description of the example",
27+
max_length=200,
28+
json_schema_extra={
29+
"x-placeholder": "Add a short description for the example",
30+
},
31+
),
32+
]
33+
prompt: Annotated[
34+
str,
35+
PydanticField(
36+
description="Example prompt",
37+
max_length=2000,
38+
json_schema_extra={
39+
"x-placeholder": "The prompt will be sent to the agent",
40+
},
41+
),
42+
]
1743

1844

1945
class AgentPublicInfo(BaseModel):
@@ -132,72 +158,3 @@ class AgentPublicInfo(BaseModel):
132158
description="Public extra data of the agent",
133159
),
134160
]
135-
136-
async def update(self, agent_id: str) -> "Agent":
137-
"""Update agent public info with only the fields that are explicitly provided.
138-
139-
This method only updates fields that are explicitly set in this instance,
140-
leaving other fields unchanged. This is more efficient than override as it
141-
reduces context usage and minimizes the risk of accidentally changing fields.
142-
143-
Args:
144-
agent_id: The ID of the agent to update
145-
146-
Returns:
147-
The updated Agent instance
148-
"""
149-
from intentkit.models.agent.agent import Agent
150-
151-
async with get_session() as session:
152-
result = await session.execute(
153-
select(AgentTable).where(AgentTable.id == agent_id)
154-
)
155-
db_agent = result.scalar_one_or_none()
156-
157-
if not db_agent:
158-
raise IntentKitAPIError(404, "NotFound", f"Agent {agent_id} not found")
159-
160-
update_data = self.model_dump(exclude_unset=True)
161-
162-
for key, value in update_data.items():
163-
if hasattr(db_agent, key):
164-
setattr(db_agent, key, value)
165-
166-
db_agent.public_info_updated_at = func.now()
167-
168-
await session.commit()
169-
await session.refresh(db_agent)
170-
171-
return Agent.model_validate(db_agent)
172-
173-
async def override(self, agent_id: str) -> "Agent":
174-
"""Override agent public info with all fields from this instance.
175-
176-
Args:
177-
agent_id: The ID of the agent to override
178-
179-
Returns:
180-
The updated Agent instance
181-
"""
182-
from intentkit.models.agent.agent import Agent
183-
184-
async with get_session() as session:
185-
result = await session.execute(
186-
select(AgentTable).where(AgentTable.id == agent_id)
187-
)
188-
db_agent = result.scalar_one_or_none()
189-
190-
if not db_agent:
191-
raise IntentKitAPIError(404, "NotFound", f"Agent {agent_id} not found")
192-
193-
update_data = self.model_dump()
194-
for key, value in update_data.items():
195-
if hasattr(db_agent, key):
196-
setattr(db_agent, key, value)
197-
198-
db_agent.public_info_updated_at = func.now()
199-
200-
await session.commit()
201-
await session.refresh(db_agent)
202-
203-
return Agent.model_validate(db_agent)

intentkit/models/agent/response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pydantic.main import IncEx
1313

1414
from intentkit.models.agent.agent import Agent
15-
from intentkit.models.agent.example import AgentExample
15+
from intentkit.models.agent.public_info import AgentExample
1616
from intentkit.models.agent_data import AgentData
1717

1818

0 commit comments

Comments
 (0)