zcbot/tools/register_artifact.py

71 lines
2.3 KiB
Python

"""Register a task file as a stable artifact without publishing it to chat."""
from __future__ import annotations
import json
from pathlib import Path
from uuid import UUID
from core.artifact_lifecycle import register_workspace_artifact
from core.artifacts import ArtifactPathError, resolve_artifact_path
from .base import Tool
class RegisterArtifactTool(Tool):
name = "register_artifact"
description = (
"Register one existing file in the current task as a stable artifact and return "
"its artifact_id. Use this before a tool that requires an artifact input. "
"Registration does not publish the file as a final chat deliverable."
)
parameters = {
"type": "object",
"properties": {
"path": {
"type": "string",
"minLength": 1,
"maxLength": 1000,
"description": "File path relative to the current task working directory.",
}
},
"required": ["path"],
"additionalProperties": False,
}
def __init__(
self,
user_id: UUID,
task_id: UUID,
*,
working_dir: Path,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.user_id = user_id
self.task_id = task_id
self.working_dir = Path(working_dir)
def execute(self, path: str) -> str:
if self.user_root is None:
return "[Error] artifact registration requires a user workspace"
if len(str(path or "")) > 1000:
return "[Error] artifact path is too long"
try:
_, relative_path = resolve_artifact_path(
path,
working_dir=self.working_dir,
user_root=self.user_root,
require_file=True,
allow_legacy_user_relative=False,
)
artifact = register_workspace_artifact(
user_id=self.user_id,
task_id=self.task_id,
user_root=self.user_root,
working_dir=self.working_dir,
path=relative_path,
)
except (ArtifactPathError, ValueError) as exc:
return f"[Error] cannot register artifact: {exc}"
return json.dumps(artifact, ensure_ascii=False)