Skip to content

Commit c71a655

Browse files
Jianfeng Zengcopybara-github
authored andcommitted
feat: Update builder/save endpoint to accept files as input. Write the agent yaml file to the agent folder
PiperOrigin-RevId: 783484854
1 parent b705b35 commit c71a655

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ test = [
105105
"pytest-mock>=3.14.0",
106106
"pytest-xdist>=3.6.1",
107107
"pytest>=8.3.4",
108+
"python-multipart>=0.0.9",
108109
# go/keep-sorted end
109110
]
110111

src/google/adk/cli/fast_api.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
import os
2222
from pathlib import Path
23+
import shutil
2324
import time
2425
import traceback
2526
import typing
@@ -32,6 +33,7 @@
3233
from fastapi import FastAPI
3334
from fastapi import HTTPException
3435
from fastapi import Query
36+
from fastapi import UploadFile
3537
from fastapi.middleware.cors import CORSMiddleware
3638
from fastapi.responses import RedirectResponse
3739
from fastapi.responses import StreamingResponse
@@ -51,7 +53,6 @@
5153
from typing_extensions import override
5254
from watchdog.events import FileSystemEventHandler
5355
from watchdog.observers import Observer
54-
import yaml
5556

5657
from ..agents import RunConfig
5758
from ..agents.live_request_queue import LiveRequest
@@ -212,14 +213,6 @@ class GetEventGraphResult(common.BaseModel):
212213
dot_src: str
213214

214215

215-
class AgentBuildRequest(common.BaseModel):
216-
agent_name: str
217-
agent_type: str
218-
model: str
219-
description: str
220-
instruction: str
221-
222-
223216
def get_fast_api_app(
224217
*,
225218
agents_dir: str,
@@ -820,26 +813,30 @@ async def delete_artifact(
820813

821814
@working_in_progress("builder_save is not ready for use.")
822815
@app.post("/builder/save", response_model_exclude_none=True)
823-
async def builder_build(req: AgentBuildRequest):
816+
async def builder_build(files: list[UploadFile]) -> bool:
824817
base_path = Path.cwd() / agents_dir
825-
agent = {
826-
"agent_class": req.agent_type,
827-
"name": req.agent_name,
828-
"model": req.model,
829-
"description": req.description,
830-
"instruction": f"""{req.instruction}""",
831-
}
832-
try:
833-
agent_dir = os.path.join(base_path, req.agent_name)
834-
os.makedirs(agent_dir, exist_ok=True)
835-
file_path = os.path.join(agent_dir, "root_agent.yaml")
836-
with open(file_path, "w") as file:
837-
yaml.dump(agent, file, default_flow_style=False)
838-
agent_loader.load_agent(agent_name=req.agent_name)
839-
return True
840-
except Exception as e:
841-
logger.exception("Error in builder_build: %s", e)
842-
return False
818+
819+
for file in files:
820+
try:
821+
# File name format: {app_name}/{agent_name}.yaml
822+
if not file.filename:
823+
logger.exception("Agent name is missing in the input files")
824+
return False
825+
826+
agent_name, filename = file.filename.split("/")
827+
828+
agent_dir = os.path.join(base_path, agent_name)
829+
os.makedirs(agent_dir, exist_ok=True)
830+
file_path = os.path.join(agent_dir, filename)
831+
832+
with open(file_path, "w") as buffer:
833+
shutil.copyfileobj(file.file, buffer)
834+
835+
except Exception as e:
836+
logger.exception("Error in builder_build: %s", e)
837+
return False
838+
839+
return True
843840

844841
@app.post("/run", response_model_exclude_none=True)
845842
async def agent_run(req: AgentRunRequest) -> list[Event]:

0 commit comments

Comments
 (0)