从远程 URL 为 Agent 添加 skill
(agent_id: str, name: str, source_url: str, description: str = '')
| 71 | |
| 72 | |
| 73 | def add_remote(agent_id: str, name: str, source_url: str, description: str = '') -> bool: |
| 74 | """从远程 URL 为 Agent 添加 skill""" |
| 75 | if not safe_name(agent_id) or not safe_name(name): |
| 76 | print(f'❌ 错误:agent_id 或 skill 名称含非法字符') |
| 77 | return False |
| 78 | |
| 79 | # 设置 workspace |
| 80 | workspace = OCLAW_HOME / f'workspace-{agent_id}' / 'skills' / name |
| 81 | workspace.mkdir(parents=True, exist_ok=True) |
| 82 | skill_md = workspace / 'SKILL.md' |
| 83 | |
| 84 | # 下载文件 |
| 85 | print(f'⏳ 正在从 {source_url} 下载...') |
| 86 | try: |
| 87 | content = _download_file(source_url) |
| 88 | except Exception as e: |
| 89 | print(f'❌ 下载失败:{e}') |
| 90 | print(f' URL: {source_url}') |
| 91 | return False |
| 92 | |
| 93 | # 基础验证(放宽检查:有些 skill 不以 --- 开头) |
| 94 | if len(content.strip()) < 10: |
| 95 | print(f'❌ 文件内容过短或为空') |
| 96 | return False |
| 97 | |
| 98 | # 保存 SKILL.md |
| 99 | skill_md.write_text(content) |
| 100 | |
| 101 | # 保存源信息 |
| 102 | source_info = { |
| 103 | 'skillName': name, |
| 104 | 'sourceUrl': source_url, |
| 105 | 'description': description, |
| 106 | 'addedAt': now_iso(), |
| 107 | 'lastUpdated': now_iso(), |
| 108 | 'checksum': _compute_checksum(content), |
| 109 | 'status': 'valid', |
| 110 | } |
| 111 | source_json = workspace / '.source.json' |
| 112 | source_json.write_text(json.dumps(source_info, ensure_ascii=False, indent=2)) |
| 113 | |
| 114 | print(f'✅ 技能 {name} 已添加到 {agent_id}') |
| 115 | print(f' 路径: {skill_md}') |
| 116 | print(f' 大小: {len(content)} 字节') |
| 117 | return True |
| 118 | |
| 119 | |
| 120 | def list_remote() -> bool: |
no test coverage detected