MCPcopy
hub / github.com/dataelement/Clawith / write_file

Function write_file

backend/app/api/files.py:596–638  ·  view source on GitHub ↗

Write content to a file (create or overwrite).

(
    agent_id: uuid.UUID,
    path: str,
    data: FileWrite,
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
)

Source from the content-addressed store, hash-verified

594
595@router.put("/content")
596async def write_file(
597 agent_id: uuid.UUID,
598 path: str,
599 data: FileWrite,
600 current_user: User = Depends(get_current_user),
601 db: AsyncSession = Depends(get_db),
602):
603 """Write content to a file (create or overwrite)."""
604 await check_agent_access(db, current_user, agent_id)
605 if is_focus_file_path(path):
606 raise HTTPException(
607 status_code=status.HTTP_410_GONE,
608 detail="Focus is stored in the system database. Use the Focus API.",
609 )
610 if path.startswith("enterprise_info"):
611 if current_user.role not in ("platform_admin", "org_admin"):
612 raise HTTPException(status_code=403, detail="Only admins can edit enterprise knowledge base")
613 if path.strip("/") == "enterprise_info":
614 raise HTTPException(status_code=400, detail="Cannot overwrite enterprise_info root")
615 target, _, _ = _visible_path(agent_id, path, current_user.tenant_id)
616 target.parent.mkdir(parents=True, exist_ok=True)
617 async with aiofiles.open(target, "w", encoding="utf-8") as f:
618 await f.write(data.content)
619 return {"status": "ok", "path": path, "revision_id": None}
620
621 result = await write_workspace_file(
622 db,
623 agent_id=agent_id,
624 base_dir=_agent_base_dir(agent_id),
625 path=path,
626 content=data.content,
627 actor_type="user",
628 actor_id=current_user.id,
629 operation="autosave" if data.autosave else "write",
630 session_id=data.session_id,
631 enforce_human_lock=False,
632 merge_user_autosave=data.autosave,
633 expected_version_token=data.expected_version_token,
634 )
635 if not result.ok:
636 raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=result.message)
637 await db.commit()
638 return {"status": "ok", "path": result.path, "revision_id": result.revision_id}
639
640
641@router.post("/locks")

Callers

nothing calls this directly

Calls 6

check_agent_accessFunction · 0.90
is_focus_file_pathFunction · 0.90
write_workspace_fileFunction · 0.90
_visible_pathFunction · 0.85
_agent_base_dirFunction · 0.85
commitMethod · 0.45

Tested by

no test coverage detected