native_skills.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import base64
  2. import sys
  3. """Run the bundled native skill tools against an admitted, isolated working copy.
  4. Yaovi owns immutable versions and Bot authorization. All skill semantics (frontmatter,
  5. batch edits, supporting files, read-before-write and learning) stay in the native tools.
  6. Only a successful native change set is committed to the workspace.
  7. """
  8. import json
  9. from copy import deepcopy
  10. import shutil
  11. import tempfile
  12. import threading
  13. from pathlib import Path
  14. _root = None
  15. _entries = {}
  16. _dispatch = None
  17. _bot = None
  18. _managed = False
  19. _compact_index = False
  20. _lock = threading.RLock()
  21. def root():
  22. return _root
  23. def compact_index_enabled():
  24. return _compact_index
  25. def compact_description(text):
  26. """Bound only the discovery summary; the approved skill body stays intact."""
  27. return ' '.join(str(text or '').split()).encode('utf-16-le')[:320].decode('utf-16-le', errors='ignore')
  28. def install(request, dispatch):
  29. global _root, _entries, _dispatch, _bot, _managed, _compact_index
  30. _dispatch, _bot = dispatch, request["botId"]
  31. _managed = request.get('botPolicy', {}).get('enabled') is True
  32. discovery = request.get('toolDiscovery') or {}
  33. _compact_index = discovery.get('version') == 1 and discovery.get('mode') == 'on_demand'
  34. parent = Path(request["dataRoot"]) / "skill-views"
  35. parent.mkdir(parents=True, exist_ok=True)
  36. _root = Path(tempfile.mkdtemp(prefix="run-", dir=parent)).resolve()
  37. _entries = {e["nativePath"]: dict(e) for e in request.get("skills", [])}
  38. restore()
  39. def restore(entries=None, *, missing_only=False):
  40. # This is a disposable admission copy, never the authoritative skill library.
  41. if not missing_only:
  42. for p in _root.iterdir():
  43. if p.is_dir() and not p.is_symlink():
  44. shutil.rmtree(p)
  45. else:
  46. p.unlink()
  47. for path, entry in (entries if entries is not None else _entries).items():
  48. if not entry.get("enabled", True):
  49. continue
  50. for file, content in {"SKILL.md": entry["content"], **entry.get("resources", {})}.items():
  51. target = _root / path / file
  52. if target.is_absolute() and not target.resolve().is_relative_to(_root):
  53. raise ValueError("Invalid admitted skill path")
  54. if missing_only and target.exists():
  55. continue
  56. target.parent.mkdir(parents=True, exist_ok=True)
  57. target.write_bytes(base64.b64decode(content,validate=True)) if entry.get('resourceEncodings',{}).get(file)=='base64' else target.write_text(content)
  58. def load_missing(name, args, **kwargs):
  59. """Fetch approved frozen bundles without replacing this run's working copy."""
  60. if not _managed:
  61. return None
  62. # The control plane rechecks exactly the same operation fingerprint that
  63. # the registry guard admitted (including supporting-file/category options).
  64. request = {'action': 'list' if name == 'skills_list' else 'view', 'toolArgs': dict(args)}
  65. if name == 'skill_view' and isinstance(args.get('name'), str):
  66. request['name'] = args['name']
  67. ids = {key: kwargs[key] for key in ('tool_call_id', 'task_id', 'session_id') if kwargs.get(key) is not None}
  68. result = _dispatch('yaovi_skills_load', request, **ids)
  69. result = json.loads(result) if isinstance(result, str) else result
  70. if not isinstance(result, dict) or not isinstance(result.get('entries'), list):
  71. return json.dumps({'success': False, 'error': result.get('error', 'Skill loading was not authorized') if isinstance(result, dict) else 'Skill loading was not authorized'})
  72. additions = {}
  73. for entry in result['entries']:
  74. path = entry.get('nativePath') if isinstance(entry, dict) else None
  75. if not isinstance(path, str) or not path or not isinstance(entry.get('content'), str):
  76. return json.dumps({'success': False, 'error': 'Invalid admitted skill bundle'})
  77. known = _entries.get(path)
  78. if known and known['id'] != entry.get('id'):
  79. return json.dumps({'success': False, 'error': 'Skill identity changed; restart the task'})
  80. # A previously committed revision and local edits are authoritative for
  81. # this run. Lazy lookup must not put the initial snapshot back over them.
  82. if known and (_root / path).exists():
  83. continue
  84. additions[path] = known if known is not None else dict(entry)
  85. restore(additions, missing_only=True)
  86. _entries.update(additions)
  87. return None
  88. def cleanup():
  89. if _root:
  90. shutil.rmtree(_root, ignore_errors=True)
  91. def documents():
  92. from agent.skill_utils import parse_frontmatter
  93. result = {}
  94. for md in _root.rglob("SKILL.md"):
  95. rel = md.parent.relative_to(_root).as_posix()
  96. if any(part.startswith(".") for part in Path(rel).parts):
  97. continue
  98. content = md.read_text()
  99. meta, _ = parse_frontmatter(content)
  100. resources = {}
  101. encodings = {}
  102. for file in md.parent.rglob("*"):
  103. if file.is_symlink():
  104. raise ValueError("Skill resources cannot contain symbolic links")
  105. if file.is_file() and file != md:
  106. name=file.relative_to(md.parent).as_posix()
  107. if _entries.get(rel,{}).get('resourceEncodings',{}).get(name)=='base64':
  108. resources[name]=base64.b64encode(file.read_bytes()).decode();encodings[name]='base64'
  109. else:
  110. try: resources[name]=file.read_text()
  111. except UnicodeDecodeError: resources[name]=base64.b64encode(file.read_bytes()).decode();encodings[name]='base64'
  112. result[rel] = {"nativePath": rel, "name": str(meta.get("name") or md.parent.name),
  113. "description": str(meta.get("description") or md.parent.name),
  114. "content": content, "resources": resources, "resourceEncodings": encodings}
  115. return result
  116. def register(agent, writable=True):
  117. from tools import skills_tool, skill_manager_tool
  118. from tools.registry import registry
  119. skills_tool.SKILLS_DIR = skill_manager_tool.SKILLS_DIR = _root
  120. schemas = {"skills_list": skills_tool.SKILLS_LIST_SCHEMA, "skill_view": skills_tool.SKILL_VIEW_SCHEMA}
  121. if writable:
  122. schemas["skill_manage"] = skill_manager_tool.SKILL_MANAGE_SCHEMA
  123. def invoke(name, args, **kwargs):
  124. with _lock:
  125. # Revocation remains live even though the admitted content is frozen.
  126. admitted = json.loads(_dispatch("yaovi_skills_authorize", {}))
  127. if not isinstance(admitted, list):
  128. return json.dumps({"success": False, "error": admitted.get("error", "Skill access revoked")})
  129. allowed = set(admitted)
  130. for path, entry in _entries.items():
  131. if entry["id"] not in allowed and (_root / path).exists():
  132. shutil.rmtree(_root / path)
  133. if name in ('skills_list', 'skill_view'):
  134. failure = load_missing(name, args, **kwargs)
  135. if failure is not None:
  136. return failure
  137. skills_tool.clear_skills_cache()
  138. if name == "skills_list":
  139. result = skills_tool.skills_list(category=args.get("category"))
  140. if _compact_index:
  141. parsed = json.loads(result)
  142. def shorten(value):
  143. if isinstance(value, dict):
  144. return {key: compact_description(item) if key == 'description' and isinstance(item, str) else shorten(item) for key, item in value.items()}
  145. return [shorten(item) for item in value] if isinstance(value, list) else value
  146. return json.dumps(shorten(parsed), ensure_ascii=False)
  147. return result
  148. if name == "skill_view":
  149. if not skill_manager_tool._find_skill(args.get("name", "")):
  150. return json.dumps({"success": False, "error": "Skill is not admitted for this Bot/session."})
  151. found=skill_manager_tool._find_skill(args.get("name", ""))
  152. resource=args.get('file_path')
  153. entry=_entries.get(found['path'].relative_to(_root).as_posix(),{}) if found else {}
  154. if resource and entry.get('resourceEncodings',{}).get(resource)=='base64':
  155. return json.dumps({'success':True,'name':args['name'],'file_path':resource,'encoding':'binary','resource_root':str(found['path']),'note':'Binary resource preserved; transfer or read it using authorized file tools.'})
  156. result=skills_tool._skill_view_with_bump(args, **kwargs)
  157. parsed=json.loads(result)
  158. if found:
  159. parsed['execution_context']={'resource_root':str(found['path']),'python':sys.executable,'environment':'Host','note':'Resolve scripts/templates relative to this frozen resource_root, not HERMES_HOME/skills. Transfer through authorized file tools before executing in another environment.'}
  160. return json.dumps(parsed,ensure_ascii=False)
  161. before = documents()
  162. try:
  163. # Existing skills may have localized display names. Resolve those
  164. # to their admitted stable path before native mutation validation.
  165. # Creation still uses the native filesystem-name rules.
  166. native_args = deepcopy(args)
  167. ops = native_args.get("operations") or [native_args]
  168. for op in ops:
  169. found = skill_manager_tool._find_skill(op.get("name", native_args.get("name", "")))
  170. if found:
  171. key = found["path"].relative_to(_root).as_posix()
  172. if _entries.get(key, {}).get("botId") != _bot:
  173. return json.dumps({"success": False, "error": "Shared skills are read-only during tasks. Create a separate Bot skill or edit the shared skill in Yaovi settings."})
  174. if op.get("action") != "create":
  175. op["name"] = key
  176. result = skill_manager_tool._skill_manage_from(
  177. native_args, task_id=kwargs.get("task_id"), session_id=kwargs.get("session_id"))
  178. parsed = json.loads(result)
  179. if not parsed.get("success") or parsed.get("staged"):
  180. restore()
  181. return result
  182. after = documents()
  183. changes = []
  184. for path in sorted(before.keys() | after.keys()):
  185. if before.get(path) == after.get(path):
  186. continue
  187. old = _entries.get(path)
  188. doc = after.get(path) or {**before[path], "content": None, "resources": {}}
  189. changes.append({**doc, **({"id": old["id"]} if old else {}),
  190. "expectedRevision": old["revision"] if old else None})
  191. if changes:
  192. committed = json.loads(_dispatch("yaovi_skills_commit", {"changes": changes}))
  193. if committed.get("isError") or committed.get("error"):
  194. restore()
  195. return json.dumps({"success": False, "error": committed.get("error", "Skill commit failed")})
  196. for entry in committed["entries"]:
  197. path = entry["nativePath"]
  198. _entries[path] = {**entry, **(after.get(path) or before[path])}
  199. parsed["versions"] = [{"id": e["id"], "revision": e["revision"]} for e in committed["entries"]]
  200. return json.dumps(parsed, ensure_ascii=False)
  201. except Exception:
  202. restore()
  203. raise
  204. for name, schema in schemas.items():
  205. registry.register(name=name, toolset="skills", schema=schema,
  206. handler=lambda args, _name=name, **kw: invoke(_name, args, **kw))
  207. agent.tools = [t for t in agent.tools if t["function"]["name"] not in (*schemas, "skill_read", "skill_propose", "skill_feedback", "skill_run_workflow")]
  208. agent.tools.extend({"type": "function", "function": schema} for schema in schemas.values())