"""Agent 可调用的专业软件任务工具。""" from __future__ import annotations import json from uuid import UUID, uuid4 from core.software_jobs import ( SoftwareJobError, create_job, get_job, list_jobs, request_job_cancel, ) from core.software_nodes import SUPPORTED_CAPABILITIES, list_nodes from .base import Tool class _SoftwareJobTool(Tool): def __init__(self, user_id: UUID, task_id: UUID, **kwargs) -> None: super().__init__(**kwargs) self.user_id = user_id self.task_id = task_id class SoftwareCapabilityListTool(_SoftwareJobTool): name = "software_capability_list" description = "List professional software capabilities available through managed Windows nodes." parameters = {"type": "object", "properties": {}, "additionalProperties": False} def execute(self) -> str: nodes = list_nodes() items = [{ "capability": item, "display_name": "Origin 科研绘图" if item == "origin.plot@v1" else item, "available_nodes": sum( 1 for node in nodes if node["status"] == "online" and item in (node.get("capabilities") or []) and (node.get("runtime") or {}).get("available_slots", 0) > 0 ), } for item in sorted(SUPPORTED_CAPABILITIES)] return json.dumps({"capabilities": items}, ensure_ascii=False) class SoftwareJobSubmitTool(_SoftwareJobTool): name = "software_job_submit" description = ( "Submit a persistent professional software job for the current task. " "Return immediately with job_id; do not poll continuously or wait for completion." ) parameters = { "type": "object", "properties": { "capability": {"type": "string", "enum": sorted(SUPPORTED_CAPABILITIES)}, "request": {"type": "object"}, "idempotency_key": { "type": "string", "description": "Stable unique key for this exact submission; omit to generate one.", }, }, "required": ["capability", "request"], "additionalProperties": False, } def execute(self, capability: str, request: dict, idempotency_key: str = "") -> str: try: job, created = create_job( self.user_id, self.task_id, idempotency_key=idempotency_key.strip() or str(uuid4()), capability=capability, request=request, ) return json.dumps({**job, "created": created}, ensure_ascii=False) except SoftwareJobError as exc: return f"[Error] {exc}" class SoftwareJobStatusTool(_SoftwareJobTool): name = "software_job_status" description = "Check one software job, or list recent jobs in the current task when job_id is omitted." parameters = { "type": "object", "properties": {"job_id": {"type": "string"}}, "additionalProperties": False, } def execute(self, job_id: str = "") -> str: try: if job_id.strip(): item = get_job(self.user_id, UUID(job_id.strip())) if item is None or item["task_id"] != str(self.task_id): return "[Error] software job not found" return json.dumps(item, ensure_ascii=False) return json.dumps( {"results": list_jobs(self.user_id, task_id=self.task_id, limit=20)}, ensure_ascii=False, ) except ValueError: return "[Error] invalid job_id" class SoftwareJobCancelTool(_SoftwareJobTool): name = "software_job_cancel" description = "Request cancellation of a software job in the current task after the user asks to stop it." parameters = { "type": "object", "properties": {"job_id": {"type": "string"}}, "required": ["job_id"], "additionalProperties": False, } def execute(self, job_id: str) -> str: try: item = get_job(self.user_id, UUID(job_id.strip())) if item is None or item["task_id"] != str(self.task_id): return "[Error] software job not found" job, _ = request_job_cancel(self.user_id, UUID(job_id.strip())) return json.dumps(job, ensure_ascii=False) except (ValueError, SoftwareJobError) as exc: return f"[Error] {exc}"