Skip to content

Commit ce21678

Browse files
wyf7107copybara-github
authored andcommitted
chore: Adding builder endpoints, WIP
PiperOrigin-RevId: 813489379
1 parent 8c73d29 commit ce21678

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

src/google/adk/cli/fast_api.py

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,40 +252,92 @@ def tear_down_observer(observer: Observer, _: AdkWebServer):
252252

253253
@working_in_progress("builder_save is not ready for use.")
254254
@app.post("/builder/save", response_model_exclude_none=True)
255-
async def builder_build(files: list[UploadFile]) -> bool:
255+
async def builder_build(
256+
files: list[UploadFile], tmp: Optional[bool] = False
257+
) -> bool:
256258
base_path = Path.cwd() / agents_dir
257-
258259
for file in files:
260+
if not file.filename:
261+
logger.exception("Agent name is missing in the input files")
262+
return False
263+
agent_name, filename = file.filename.split("/")
264+
agent_dir = os.path.join(base_path, agent_name)
259265
try:
260266
# File name format: {app_name}/{agent_name}.yaml
261-
if not file.filename:
262-
logger.exception("Agent name is missing in the input files")
263-
return False
264-
265-
agent_name, filename = file.filename.split("/")
266-
267-
agent_dir = os.path.join(base_path, agent_name)
268-
os.makedirs(agent_dir, exist_ok=True)
269-
file_path = os.path.join(agent_dir, filename)
270-
271-
with open(file_path, "wb") as buffer:
272-
shutil.copyfileobj(file.file, buffer)
273-
267+
if tmp:
268+
agent_dir = os.path.join(agent_dir, "tmp/" + agent_name)
269+
os.makedirs(agent_dir, exist_ok=True)
270+
file_path = os.path.join(agent_dir, filename)
271+
with open(file_path, "wb") as buffer:
272+
shutil.copyfileobj(file.file, buffer)
273+
274+
else:
275+
source_dir = os.path.join(agent_dir, "tmp/" + agent_name)
276+
destination_dir = agent_dir
277+
for item in os.listdir(source_dir):
278+
source_item = os.path.join(source_dir, item)
279+
destination_item = os.path.join(destination_dir, item)
280+
if os.path.isdir(source_item):
281+
shutil.copytree(source_item, destination_item, dirs_exist_ok=True)
282+
# Check if the item is a file
283+
elif os.path.isfile(source_item):
284+
shutil.copy2(source_item, destination_item)
274285
except Exception as e:
275286
logger.exception("Error in builder_build: %s", e)
276287
return False
277288

278289
return True
279290

291+
@working_in_progress("builder_save is not ready for use.")
292+
@app.post("/builder/app/{app_name}/cancel", response_model_exclude_none=True)
293+
async def builder_cancel(app_name: str) -> bool:
294+
base_path = Path.cwd() / agents_dir
295+
agent_dir = os.path.join(base_path, app_name)
296+
destination_dir = os.path.join(agent_dir, "tmp/" + app_name)
297+
source_dir = agent_dir
298+
source_items = set(os.listdir(source_dir))
299+
try:
300+
for item in os.listdir(destination_dir):
301+
if item in source_items:
302+
continue
303+
# If it doesn't exist in the source, delete it from the destination
304+
item_path = os.path.join(destination_dir, item)
305+
if os.path.isdir(item_path):
306+
shutil.rmtree(item_path)
307+
elif os.path.isfile(item_path):
308+
os.remove(item_path)
309+
310+
for item in os.listdir(source_dir):
311+
source_item = os.path.join(source_dir, item)
312+
destination_item = os.path.join(destination_dir, item)
313+
if item == "tmp" and os.path.isdir(source_item):
314+
continue
315+
if os.path.isdir(source_item):
316+
shutil.copytree(source_item, destination_item, dirs_exist_ok=True)
317+
# Check if the item is a file
318+
elif os.path.isfile(source_item):
319+
shutil.copy2(source_item, destination_item)
320+
except Exception as e:
321+
logger.exception("Error in builder_build: %s", e)
322+
return False
323+
return True
324+
280325
@working_in_progress("builder_get is not ready for use.")
281326
@app.get(
282327
"/builder/app/{app_name}",
283328
response_model_exclude_none=True,
284329
response_class=PlainTextResponse,
285330
)
286-
async def get_agent_builder(app_name: str, file_path: Optional[str] = None):
331+
async def get_agent_builder(
332+
app_name: str,
333+
file_path: Optional[str] = None,
334+
tmp: Optional[bool] = False,
335+
):
287336
base_path = Path.cwd() / agents_dir
288337
agent_dir = base_path / app_name
338+
if tmp:
339+
agent_dir = agent_dir / "tmp"
340+
agent_dir = agent_dir / app_name
289341
if not file_path:
290342
file_name = "root_agent.yaml"
291343
root_file_path = agent_dir / file_name

0 commit comments

Comments
 (0)