2026-02-04 03:46:46 +05:30
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
2026-02-04 15:16:28 +05:30
|
|
|
from jinja2 import Environment, FileSystemLoader, StrictUndefined, select_autoescape
|
2026-02-04 13:03:18 +05:30
|
|
|
|
2026-02-04 03:46:46 +05:30
|
|
|
from app.core.config import settings
|
2026-02-04 16:04:52 +05:30
|
|
|
from app.integrations.openclaw_gateway import GatewayConfig, ensure_session, send_message
|
2026-02-04 03:46:46 +05:30
|
|
|
from app.models.agents import Agent
|
2026-02-04 16:04:52 +05:30
|
|
|
from app.models.boards import Board
|
2026-02-04 03:46:46 +05:30
|
|
|
|
|
|
|
|
TEMPLATE_FILES = [
|
|
|
|
|
"AGENTS.md",
|
|
|
|
|
"BOOT.md",
|
|
|
|
|
"BOOTSTRAP.md",
|
|
|
|
|
"HEARTBEAT.md",
|
|
|
|
|
"IDENTITY.md",
|
|
|
|
|
"SOUL.md",
|
|
|
|
|
"TOOLS.md",
|
|
|
|
|
"USER.md",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _repo_root() -> Path:
|
|
|
|
|
return Path(__file__).resolve().parents[3]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _templates_root() -> Path:
|
|
|
|
|
return _repo_root() / "templates"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _slugify(value: str) -> str:
|
|
|
|
|
slug = re.sub(r"[^a-z0-9]+", "-", value.lower()).strip("-")
|
|
|
|
|
return slug or uuid4().hex
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 13:03:18 +05:30
|
|
|
def _template_env() -> Environment:
|
|
|
|
|
return Environment(
|
|
|
|
|
loader=FileSystemLoader(_templates_root()),
|
2026-02-04 15:16:28 +05:30
|
|
|
autoescape=select_autoescape(default=True),
|
2026-02-04 13:03:18 +05:30
|
|
|
undefined=StrictUndefined,
|
|
|
|
|
keep_trailing_newline=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _read_templates(context: dict[str, str]) -> dict[str, str]:
|
|
|
|
|
env = _template_env()
|
2026-02-04 03:46:46 +05:30
|
|
|
templates: dict[str, str] = {}
|
|
|
|
|
for name in TEMPLATE_FILES:
|
2026-02-04 13:03:18 +05:30
|
|
|
path = _templates_root() / name
|
|
|
|
|
if not path.exists():
|
2026-02-04 03:46:46 +05:30
|
|
|
templates[name] = ""
|
2026-02-04 13:03:18 +05:30
|
|
|
continue
|
|
|
|
|
template = env.get_template(name)
|
|
|
|
|
templates[name] = template.render(**context).strip()
|
2026-02-04 03:46:46 +05:30
|
|
|
return templates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _render_file_block(name: str, content: str) -> str:
|
|
|
|
|
body = content if content else f"# {name}\n\nTODO: add content\n"
|
|
|
|
|
return f"\n{name}\n```md\n{body}\n```\n"
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
def _workspace_path(agent_name: str, workspace_root: str) -> str:
|
|
|
|
|
root = workspace_root or "~/.openclaw/workspaces"
|
2026-02-04 03:46:46 +05:30
|
|
|
root = root.rstrip("/")
|
|
|
|
|
return f"{root}/{_slugify(agent_name)}"
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
def build_provisioning_message(agent: Agent, board: Board, auth_token: str) -> str:
|
2026-02-04 13:03:18 +05:30
|
|
|
agent_id = str(agent.id)
|
2026-02-04 16:04:52 +05:30
|
|
|
workspace_root = board.gateway_workspace_root or "~/.openclaw/workspaces"
|
|
|
|
|
workspace_path = _workspace_path(agent.name, workspace_root)
|
2026-02-04 03:46:46 +05:30
|
|
|
session_key = agent.openclaw_session_id or ""
|
2026-02-04 13:03:18 +05:30
|
|
|
base_url = settings.base_url or "REPLACE_WITH_BASE_URL"
|
2026-02-04 16:04:52 +05:30
|
|
|
main_session_key = board.gateway_main_session_key or "agent:main:main"
|
2026-02-04 13:03:18 +05:30
|
|
|
|
|
|
|
|
context = {
|
|
|
|
|
"agent_name": agent.name,
|
|
|
|
|
"agent_id": agent_id,
|
2026-02-04 16:04:52 +05:30
|
|
|
"board_id": str(board.id),
|
2026-02-04 13:03:18 +05:30
|
|
|
"session_key": session_key,
|
|
|
|
|
"workspace_path": workspace_path,
|
|
|
|
|
"base_url": base_url,
|
|
|
|
|
"auth_token": auth_token,
|
2026-02-04 16:04:52 +05:30
|
|
|
"main_session_key": main_session_key,
|
|
|
|
|
"workspace_root": workspace_root,
|
2026-02-04 13:03:18 +05:30
|
|
|
"user_name": "Unset",
|
|
|
|
|
"user_preferred_name": "Unset",
|
|
|
|
|
"user_timezone": "Unset",
|
|
|
|
|
"user_notes": "Fill in user context.",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
templates = _read_templates(context)
|
2026-02-04 03:46:46 +05:30
|
|
|
|
|
|
|
|
file_blocks = "".join(
|
|
|
|
|
_render_file_block(name, templates.get(name, "")) for name in TEMPLATE_FILES
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
"Provision a new OpenClaw agent workspace.\n\n"
|
|
|
|
|
f"Agent name: {agent.name}\n"
|
|
|
|
|
f"Agent id: {agent_id}\n"
|
|
|
|
|
f"Session key: {session_key}\n"
|
|
|
|
|
f"Workspace path: {workspace_path}\n\n"
|
2026-02-04 13:03:18 +05:30
|
|
|
f"Base URL: {base_url}\n"
|
|
|
|
|
f"Auth token: {auth_token}\n\n"
|
2026-02-04 03:46:46 +05:30
|
|
|
"Steps:\n"
|
2026-02-04 16:08:28 +05:30
|
|
|
"0) IMPORTANT: Do NOT replace or repurpose the main agent. Keep "
|
|
|
|
|
f"{context['main_session_key']} unchanged and its workspace intact.\n"
|
2026-02-04 03:46:46 +05:30
|
|
|
"1) Create the workspace directory.\n"
|
|
|
|
|
"2) Write the files below with the exact contents.\n"
|
2026-02-04 13:03:18 +05:30
|
|
|
"3) Update TOOLS.md if BASE_URL/AUTH_TOKEN must change.\n"
|
|
|
|
|
"4) Leave BOOTSTRAP.md in place; the agent should run it on first start and delete it.\n"
|
2026-02-04 16:08:28 +05:30
|
|
|
"5) Register agent id in OpenClaw so it uses this workspace path "
|
|
|
|
|
"(never overwrite the main agent session).\n\n"
|
2026-02-04 03:46:46 +05:30
|
|
|
"Files:" + file_blocks
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 16:04:52 +05:30
|
|
|
async def send_provisioning_message(
|
|
|
|
|
agent: Agent,
|
|
|
|
|
board: Board,
|
|
|
|
|
auth_token: str,
|
|
|
|
|
) -> None:
|
|
|
|
|
main_session = board.gateway_main_session_key or "agent:main:main"
|
|
|
|
|
if not board.gateway_url:
|
2026-02-04 03:46:46 +05:30
|
|
|
return
|
2026-02-04 16:04:52 +05:30
|
|
|
config = GatewayConfig(url=board.gateway_url, token=board.gateway_token)
|
|
|
|
|
await ensure_session(main_session, config=config, label="Main Agent")
|
|
|
|
|
message = build_provisioning_message(agent, board, auth_token)
|
|
|
|
|
await send_message(message, session_key=main_session, config=config, deliver=False)
|