mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-04-30 12:40:10 +00:00
refactor: format python backend code (#1132)
Co-authored-by: bytecii <bytecii@users.noreply.github.com> Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
This commit is contained in:
parent
649bdc6822
commit
42ce1d96be
45 changed files with 1639 additions and 826 deletions
|
|
@ -12,28 +12,29 @@
|
|||
# limitations under the License.
|
||||
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Literal
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import APIRouter, Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.component.environment import sanitize_env_path, set_user_env_path
|
||||
from app.model.chat import NewAgent, UpdateData
|
||||
from app.service.task import (
|
||||
Action,
|
||||
ActionNewAgent,
|
||||
ActionStartData,
|
||||
ActionStopData,
|
||||
ActionTakeControl,
|
||||
ActionStartData,
|
||||
ActionUpdateTaskData,
|
||||
get_task_lock,
|
||||
task_locks,
|
||||
)
|
||||
import asyncio
|
||||
from app.component.environment import set_user_env_path, sanitize_env_path
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger("task_controller")
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
|
|
@ -48,10 +49,26 @@ def start(id: str):
|
|||
|
||||
@router.put("/task/{id}", name="update task")
|
||||
def put(id: str, data: UpdateData):
|
||||
logger.info("Updating task", extra={"task_id": id, "task_items_count": len(data.task)})
|
||||
logger.debug("Update task data", extra={"task_id": id, "data": data.model_dump_json()})
|
||||
logger.info(
|
||||
"Updating task",
|
||||
extra={
|
||||
"task_id": id,
|
||||
"task_items_count": len(data.task)
|
||||
}
|
||||
)
|
||||
logger.debug(
|
||||
"Update task data",
|
||||
extra={
|
||||
"task_id": id,
|
||||
"data": data.model_dump_json()
|
||||
}
|
||||
)
|
||||
task_lock = get_task_lock(id)
|
||||
asyncio.run(task_lock.put_queue(ActionUpdateTaskData(action=Action.update_task, data=data)))
|
||||
asyncio.run(
|
||||
task_lock.put_queue(
|
||||
ActionUpdateTaskData(action=Action.update_task, data=data)
|
||||
)
|
||||
)
|
||||
logger.info("Task updated successfully", extra={"task_id": id})
|
||||
return Response(status_code=201)
|
||||
|
||||
|
|
@ -62,25 +79,55 @@ class TakeControl(BaseModel):
|
|||
|
||||
@router.put("/task/{id}/take-control", name="take control pause or resume")
|
||||
def take_control(id: str, data: TakeControl):
|
||||
logger.info("Task control action", extra={"task_id": id, "action": data.action})
|
||||
logger.info(
|
||||
"Task control action", extra={
|
||||
"task_id": id,
|
||||
"action": data.action
|
||||
}
|
||||
)
|
||||
task_lock = get_task_lock(id)
|
||||
asyncio.run(task_lock.put_queue(ActionTakeControl(action=data.action)))
|
||||
logger.info("Task control action completed", extra={"task_id": id, "action": data.action})
|
||||
logger.info(
|
||||
"Task control action completed",
|
||||
extra={
|
||||
"task_id": id,
|
||||
"action": data.action
|
||||
}
|
||||
)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.post("/task/{id}/add-agent", name="add new agent")
|
||||
def add_agent(id: str, data: NewAgent):
|
||||
logger.info("Adding new agent to task", extra={"task_id": id, "agent_name": data.name})
|
||||
logger.debug("New agent data", extra={"task_id": id, "agent_data": data.model_dump_json()})
|
||||
logger.info(
|
||||
"Adding new agent to task",
|
||||
extra={
|
||||
"task_id": id,
|
||||
"agent_name": data.name
|
||||
}
|
||||
)
|
||||
logger.debug(
|
||||
"New agent data",
|
||||
extra={
|
||||
"task_id": id,
|
||||
"agent_data": data.model_dump_json()
|
||||
}
|
||||
)
|
||||
# Set user-specific environment path for this thread
|
||||
set_user_env_path(data.env_path)
|
||||
# Load environment with validated path
|
||||
safe_env_path = sanitize_env_path(data.env_path)
|
||||
if safe_env_path:
|
||||
load_dotenv(dotenv_path=safe_env_path)
|
||||
asyncio.run(get_task_lock(id).put_queue(ActionNewAgent(**data.model_dump())))
|
||||
logger.info("Agent added to task", extra={"task_id": id, "agent_name": data.name})
|
||||
asyncio.run(
|
||||
get_task_lock(id).put_queue(ActionNewAgent(**data.model_dump()))
|
||||
)
|
||||
logger.info(
|
||||
"Agent added to task", extra={
|
||||
"task_id": id,
|
||||
"agent_name": data.name
|
||||
}
|
||||
)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue