Skip to content

Commit

Permalink
Merge pull request #290 from crestalnetwork/feat/multi-chats
Browse files Browse the repository at this point in the history
Feat: multi chats
  • Loading branch information
taiyangc authored Feb 27, 2025
2 parents d3b5fbf + 13c5b6c commit f513d91
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 135 deletions.
104 changes: 51 additions & 53 deletions app/admin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,16 @@ async def create_agent(
3. Reinitializes agent if already in cache
4. Masks sensitive data in response
Args:
- agent: Agent configuration
- db: Database session
**Request Body:**
* `agent` - Agent configuration
Returns:
- AgentResponse: Updated agent configuration with additional processed data
**Returns:**
* `AgentResponse` - Updated agent configuration with additional processed data
Raises:
- HTTPException:
- 400: Invalid agent ID format
- 500: Database error
**Raises:**
* `HTTPException`:
- 400: Invalid agent ID format
- 500: Database error
"""
latest_agent, agent_data = await _process_agent(agent, subject)
return AgentResponse.from_agent(latest_agent, agent_data)
Expand All @@ -226,11 +225,8 @@ async def create_agent(
async def get_agents(db: AsyncSession = Depends(get_db)) -> list[AgentResponse]:
"""Get all agents with their quota information.
Args:
db: Database session
Returns:
list[AgentResponse]: List of agents with their quota information and additional processed data
**Returns:**
* `list[AgentResponse]` - List of agents with their quota information and additional processed data
"""
# Query all agents first
agents = (await db.exec(select(Agent))).all()
Expand All @@ -255,19 +251,21 @@ async def get_agents(db: AsyncSession = Depends(get_db)) -> list[AgentResponse]:
dependencies=[Depends(verify_jwt)],
operation_id="get_agent",
)
async def get_agent(agent_id: str, db: AsyncSession = Depends(get_db)) -> AgentResponse:
async def get_agent(
agent_id: str = Path(..., description="ID of the agent to retrieve"),
db: AsyncSession = Depends(get_db),
) -> AgentResponse:
"""Get a single agent by ID.
Args:
agent_id: ID of the agent to retrieve
db: Database session
**Path Parameters:**
* `agent_id` - ID of the agent to retrieve
Returns:
AgentResponse: Agent configuration with additional processed data
**Returns:**
* `AgentResponse` - Agent configuration with additional processed data
Raises:
HTTPException:
- 404: Agent not found
**Raises:**
* `HTTPException`:
- 404: Agent not found
"""
agent = (await db.exec(select(Agent).where(Agent.id == agent_id))).first()
if not agent:
Expand Down Expand Up @@ -309,23 +307,23 @@ class MemCleanRequest(BaseModel):
)
async def clean_memory(
request: MemCleanRequest = Body(
MemCleanRequest, description="Agent memory cleanup requestd"
MemCleanRequest, description="Agent memory cleanup request"
),
db: AsyncSession = Depends(get_db),
) -> str:
"""Clear an agent memory.
Args:
request (MemCleanRequest): The execution request containing agent ID, message, and thread ID
**Request Body:**
* `request` - The execution request containing agent ID, message, and thread ID
Returns:
str: Formatted response lines from agent memory cleanup
**Returns:**
* `str` - Formatted response lines from agent memory cleanup
Raises:
HTTPException:
- 400: If input parameters are invalid (empty agent_id, thread_id, or message text)
- 404: If agent not found
- 500: For other server-side errors
**Raises:**
* `HTTPException`:
- 400: If input parameters are invalid (empty agent_id, thread_id, or message text)
- 404: If agent not found
- 500: For other server-side errors
"""
# Validate input parameters
if not request.agent_id or not request.agent_id.strip():
Expand Down Expand Up @@ -366,19 +364,19 @@ async def clean_memory(
dependencies=[Depends(verify_jwt)],
)
async def export_agent(
agent_id: str,
agent_id: str = Path(..., description="ID of the agent to export"),
) -> str:
"""Export agent configuration as YAML.
Args:
agent_id: ID of the agent to export
**Path Parameters:**
* `agent_id` - ID of the agent to export
Returns:
str: YAML configuration of the agent
**Returns:**
* `str` - YAML configuration of the agent
Raises:
HTTPException:
- 404: Agent not found
**Raises:**
* `HTTPException`:
- 404: Agent not found
"""
try:
agent = await Agent.get(agent_id)
Expand Down Expand Up @@ -414,20 +412,20 @@ async def import_agent(
"""Import agent configuration from YAML file.
Only updates existing agents, will not create new ones.
Args:
agent_id: ID of the agent to update
file: YAML file containing agent configuration
subject: JWT subject for authorization
db: Database session
**Path Parameters:**
* `agent_id` - ID of the agent to update
Returns:
str: Success message
**Request Body:**
* `file` - YAML file containing agent configuration
**Returns:**
* `str` - Success message
Raises:
HTTPException:
- 400: Invalid YAML or agent configuration
- 404: Agent not found
- 500: Server error
**Raises:**
* `HTTPException`:
- 400: Invalid YAML or agent configuration
- 404: Agent not found
- 500: Server error
"""
try:
# First check if agent exists
Expand Down
26 changes: 12 additions & 14 deletions app/admin/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
async def get_agent_schema() -> JSONResponse:
"""Get the JSON schema for Agent model with all $ref references resolved.
Returns:
JSONResponse: The complete JSON schema for the Agent model with application/json content type
"""
**Returns:**
* `JSONResponse` - The complete JSON schema for the Agent model with application/json content type
"""
base_uri = f"file://{AGENT_SCHEMA_PATH}"
with open(AGENT_SCHEMA_PATH) as f:
schema = jsonref.load(f, base_uri=base_uri, proxies=False, lazy_load=False)
Expand All @@ -49,22 +49,20 @@ async def get_agent_schema() -> JSONResponse:
)
async def get_skill_schema(
skill: str = PathParam(
...,
description="Skill name (alphanumeric and dash only)",
regex="^[a-zA-Z0-9-]+$",
..., description="Skill name", regex="^[a-zA-Z0-9_-]+$"
),
) -> JSONResponse:
"""Get the JSON schema for a skill.
"""Get the JSON schema for a specific skill.
Args:
skill: The name of the skill to get the schema for (alphanumeric and dash only)
**Path Parameters:**
* `skill` - Skill name
Returns:
JSONResponse: The JSON schema for the skill with application/json content type
**Returns:**
* `JSONResponse` - The complete JSON schema for the skill with application/json content type
Raises:
HTTPException: If the skill is not found or name is invalid
"""
**Raises:**
* `HTTPException` - If the skill is not found or name is invalid
"""
base_path = PROJECT_ROOT / "skills"
schema_path = base_path / skill / "schema.json"
normalized_path = schema_path.resolve()
Expand Down
18 changes: 9 additions & 9 deletions app/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ async def execute(
) -> list[ChatMessage]:
"""Execute an agent with the given input and return response lines.
Args:
message (ChatMessage): The chat message containing agent_id, chat_id and message content
**Request Body:**
* `message` - The chat message containing agent_id, chat_id and message content
Returns:
list[str]: Formatted response lines from agent execution
**Returns:**
* `list[ChatMessage]` - Formatted response lines from agent execution
Raises:
HTTPException:
- 400: If input parameters are invalid
- 404: If agent not found
- 500: For other server-side errors
**Raises:**
* `HTTPException`:
- 400: If input parameters are invalid
- 404: If agent not found
- 500: For other server-side errors
"""
message.created_at = None
# Validate input parameters
Expand Down
Loading

0 comments on commit f513d91

Please sign in to comment.