sophhub 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agents/ai-cs-admin/.config.json +6 -1
- package/agents/ai-cs-qa/.config.json +1 -1
- package/agents/ai-cs-qa/AGENTS.md +43 -15
- package/package.json +1 -1
- package/skills/agent-install/skill.json +27 -0
- package/skills/agent-install/src/SKILL.md +238 -0
- package/skills/agent-install/src/pyproject.toml +6 -0
- package/skills/agent-install/src/scripts/backup_agent.py +120 -0
- package/skills/agent-install/src/scripts/check_installed.py +479 -0
- package/skills/agent-install/src/scripts/common.py +487 -0
- package/skills/agent-install/src/scripts/copy_agent_files.py +59 -0
- package/skills/agent-install/src/scripts/list_agents.py +285 -0
- package/skills/agent-install/src/scripts/resolve_install_params.py +90 -0
- package/skills/agent-install/src/scripts/update_agent_md.py +76 -0
- package/skills/agent-install/src/scripts/update_openclaw.py +183 -0
- package/skills/agent-install/src/scripts/verify_download.py +148 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import secrets
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def repo_root() -> Path:
|
|
14
|
+
return Path(__file__).resolve().parents[4]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def agents_root() -> Path:
|
|
18
|
+
return repo_root() / "agents"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def default_openclaw_config_path() -> Path:
|
|
22
|
+
raw = os.environ.get("OPENCLAW_CONFIG_PATH", "~/.openclaw/openclaw.json")
|
|
23
|
+
return Path(os.path.expanduser(raw)).resolve()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def openclaw_root(config_path: Path) -> Path:
|
|
27
|
+
return config_path.expanduser().resolve().parent
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def load_json(path: Path) -> dict[str, Any]:
|
|
31
|
+
with path.open("r", encoding="utf-8") as f:
|
|
32
|
+
return json.load(f)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def dump_json(path: Path, data: dict[str, Any]) -> None:
|
|
36
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
with path.open("w", encoding="utf-8") as f:
|
|
38
|
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
39
|
+
f.write("\n")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def get_agent_config_path(agent_id: str) -> Path:
|
|
43
|
+
return agents_root() / agent_id / ".config.json"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def candidate_agent_directories(source_path: Path, agent_id: str) -> list[Path]:
|
|
47
|
+
candidates = [source_path / agent_id, source_path]
|
|
48
|
+
unique: list[Path] = []
|
|
49
|
+
seen: set[str] = set()
|
|
50
|
+
for candidate in candidates:
|
|
51
|
+
resolved = str(candidate)
|
|
52
|
+
if resolved not in seen:
|
|
53
|
+
unique.append(candidate)
|
|
54
|
+
seen.add(resolved)
|
|
55
|
+
return unique
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def resolve_agent_directory(agent_id: str, source_path: Path | None = None) -> Path:
|
|
59
|
+
if source_path is None:
|
|
60
|
+
return get_agent_config_path(agent_id).parent
|
|
61
|
+
|
|
62
|
+
resolved_source = source_path.expanduser().resolve()
|
|
63
|
+
for candidate in candidate_agent_directories(resolved_source, agent_id):
|
|
64
|
+
config_path = candidate / ".config.json"
|
|
65
|
+
if not config_path.is_file():
|
|
66
|
+
continue
|
|
67
|
+
data = load_json(config_path)
|
|
68
|
+
if data.get("agent_id") == agent_id:
|
|
69
|
+
return candidate
|
|
70
|
+
|
|
71
|
+
raise FileNotFoundError(f"未找到已下载的 Agent 配置: {resolved_source}")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def get_agent_config_path_from_source(agent_id: str, source_path: Path | None = None) -> Path:
|
|
75
|
+
if source_path is None:
|
|
76
|
+
return get_agent_config_path(agent_id)
|
|
77
|
+
return resolve_agent_directory(agent_id, source_path) / ".config.json"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def load_agent_definition(agent_id: str, source_path: Path | None = None) -> dict[str, Any]:
|
|
81
|
+
path = get_agent_config_path_from_source(agent_id, source_path)
|
|
82
|
+
if not path.is_file():
|
|
83
|
+
raise FileNotFoundError(f"未找到 Agent 配置: {path}")
|
|
84
|
+
data = load_json(path)
|
|
85
|
+
if data.get("agent_id") != agent_id:
|
|
86
|
+
raise ValueError(f".config.json 中的 agent_id 与目录不一致: {path}")
|
|
87
|
+
if not data.get("version"):
|
|
88
|
+
raise ValueError(f"Agent 配置缺少 version: {path}")
|
|
89
|
+
return data
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def extract_placeholders(text: str) -> list[str]:
|
|
93
|
+
return list(dict.fromkeys(re.findall(r"\{\{[^{}]+\}\}", text)))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def markdown_files_in_dir(directory: Path) -> list[Path]:
|
|
97
|
+
if not directory.is_dir():
|
|
98
|
+
return []
|
|
99
|
+
return sorted(path for path in directory.iterdir() if path.is_file() and path.suffix == ".md")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def read_agent_placeholders(agent_id: str, source_path: Path | None = None) -> list[str]:
|
|
103
|
+
agent_dir = resolve_agent_directory(agent_id, source_path)
|
|
104
|
+
placeholders: list[str] = []
|
|
105
|
+
for md_path in markdown_files_in_dir(agent_dir):
|
|
106
|
+
try:
|
|
107
|
+
text = md_path.read_text(encoding="utf-8")
|
|
108
|
+
except OSError:
|
|
109
|
+
continue
|
|
110
|
+
placeholders.extend(extract_placeholders(text))
|
|
111
|
+
return list(dict.fromkeys(placeholders))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def normalize_placeholder_catalog(raw: Any) -> list[dict[str, str]]:
|
|
115
|
+
"""解析 .config.json 中的 placeholder_catalog,约定见 AGENT-CONFIG.md。"""
|
|
116
|
+
if not isinstance(raw, list):
|
|
117
|
+
return []
|
|
118
|
+
result: list[dict[str, str]] = []
|
|
119
|
+
for item in raw:
|
|
120
|
+
if not isinstance(item, dict):
|
|
121
|
+
continue
|
|
122
|
+
token = item.get("token")
|
|
123
|
+
if not isinstance(token, str) or not token.strip():
|
|
124
|
+
continue
|
|
125
|
+
label = item.get("label") if isinstance(item.get("label"), str) else ""
|
|
126
|
+
description = item.get("description") if isinstance(item.get("description"), str) else ""
|
|
127
|
+
result.append({"token": token.strip(), "label": label.strip(), "description": description.strip()})
|
|
128
|
+
return result
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def merge_placeholder_catalog_with_detected(
|
|
132
|
+
catalog: list[dict[str, str]],
|
|
133
|
+
detected: list[str],
|
|
134
|
+
) -> list[dict[str, Any]]:
|
|
135
|
+
"""合并 placeholder_catalog 与各条 label/description,以及 .md 中扫描到的 {{...}}:便于安装流程展示与校验。"""
|
|
136
|
+
by_token: dict[str, dict[str, str]] = {e["token"]: e for e in catalog}
|
|
137
|
+
out: list[dict[str, Any]] = []
|
|
138
|
+
seen: set[str] = set()
|
|
139
|
+
for entry in catalog:
|
|
140
|
+
tok = entry["token"]
|
|
141
|
+
seen.add(tok)
|
|
142
|
+
out.append(
|
|
143
|
+
{
|
|
144
|
+
**entry,
|
|
145
|
+
"present_in_markdown": tok in detected,
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
for tok in detected:
|
|
149
|
+
if tok not in seen:
|
|
150
|
+
out.append(
|
|
151
|
+
{
|
|
152
|
+
"token": tok,
|
|
153
|
+
"label": "",
|
|
154
|
+
"description": "",
|
|
155
|
+
"present_in_markdown": True,
|
|
156
|
+
"undeclared_in_config": True,
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
seen.add(tok)
|
|
160
|
+
return out
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def parse_version(version: str | None) -> tuple[int, ...]:
|
|
164
|
+
if not version:
|
|
165
|
+
return ()
|
|
166
|
+
try:
|
|
167
|
+
return tuple(int(part) for part in version.split("."))
|
|
168
|
+
except ValueError:
|
|
169
|
+
return ()
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def normalize_skills(skills: Any) -> list[str]:
|
|
173
|
+
if not isinstance(skills, list):
|
|
174
|
+
return []
|
|
175
|
+
|
|
176
|
+
result: list[str] = []
|
|
177
|
+
for item in skills:
|
|
178
|
+
if isinstance(item, str) and item:
|
|
179
|
+
result.append(item)
|
|
180
|
+
elif isinstance(item, dict):
|
|
181
|
+
name = item.get("name")
|
|
182
|
+
if isinstance(name, str) and name:
|
|
183
|
+
result.append(name)
|
|
184
|
+
|
|
185
|
+
return list(dict.fromkeys(result))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def normalize_dependencies(dependencies: Any) -> list[str]:
|
|
189
|
+
if not isinstance(dependencies, list):
|
|
190
|
+
return []
|
|
191
|
+
result: list[str] = []
|
|
192
|
+
for item in dependencies:
|
|
193
|
+
if isinstance(item, str) and item:
|
|
194
|
+
result.append(item)
|
|
195
|
+
return list(dict.fromkeys(result))
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def desired_workspace(agent_def: dict[str, Any], workspace_override: str | None = None) -> str:
|
|
199
|
+
if isinstance(workspace_override, str) and workspace_override.strip():
|
|
200
|
+
return workspace_override.strip()
|
|
201
|
+
|
|
202
|
+
install = agent_def.get("install", {})
|
|
203
|
+
workspace = install.get("workspace") or agent_def.get("workspace")
|
|
204
|
+
if isinstance(workspace, str) and workspace:
|
|
205
|
+
return workspace
|
|
206
|
+
return f"/home/node/.openclaw/workspace-{agent_def['agent_id']}"
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def get_agent_dependencies(agent_def: dict[str, Any]) -> list[str]:
|
|
210
|
+
return normalize_dependencies(agent_def.get("agent_dependencies"))
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def normalize_llm_primary(llm: Any) -> str | None:
|
|
214
|
+
"""将 .config.json 顶层 llm 转为 openclaw Agent model.primary;无 provider 时补 sophnet/。"""
|
|
215
|
+
if not isinstance(llm, str):
|
|
216
|
+
return None
|
|
217
|
+
stripped = llm.strip()
|
|
218
|
+
if not stripped:
|
|
219
|
+
return None
|
|
220
|
+
if "/" in stripped:
|
|
221
|
+
return stripped
|
|
222
|
+
return f"sophnet/{stripped}"
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def build_agent_entry(
|
|
226
|
+
agent_def: dict[str, Any],
|
|
227
|
+
*,
|
|
228
|
+
target_id_override: str | None = None,
|
|
229
|
+
workspace_override: str | None = None,
|
|
230
|
+
name_override: str | None = None,
|
|
231
|
+
) -> dict[str, Any]:
|
|
232
|
+
source_agent_id = agent_def["agent_id"]
|
|
233
|
+
agent_id = target_id_override.strip() if isinstance(target_id_override, str) and target_id_override.strip() else source_agent_id
|
|
234
|
+
install = agent_def.get("install", {})
|
|
235
|
+
identity = install.get("identity", {})
|
|
236
|
+
resolved_name = name_override.strip() if isinstance(name_override, str) and name_override.strip() else None
|
|
237
|
+
|
|
238
|
+
display_name = resolved_name or install.get("name") or identity.get("name") or source_agent_id
|
|
239
|
+
entry: dict[str, Any] = {
|
|
240
|
+
"id": agent_id,
|
|
241
|
+
"workspace": desired_workspace(agent_def, workspace_override),
|
|
242
|
+
"identity": {
|
|
243
|
+
"name": resolved_name or identity.get("name", display_name),
|
|
244
|
+
"theme": identity.get("theme", display_name),
|
|
245
|
+
"emoji": identity.get("emoji", "🤖"),
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if resolved_name:
|
|
250
|
+
entry["name"] = resolved_name
|
|
251
|
+
elif install.get("name"):
|
|
252
|
+
entry["name"] = install["name"]
|
|
253
|
+
|
|
254
|
+
if install.get("agent_dir"):
|
|
255
|
+
entry["agentDir"] = install["agent_dir"]
|
|
256
|
+
|
|
257
|
+
install_model = install.get("model")
|
|
258
|
+
if isinstance(install_model, dict) and install_model:
|
|
259
|
+
entry["model"] = install_model
|
|
260
|
+
else:
|
|
261
|
+
primary = normalize_llm_primary(agent_def.get("llm"))
|
|
262
|
+
if primary is not None:
|
|
263
|
+
entry["model"] = {"primary": primary}
|
|
264
|
+
|
|
265
|
+
if isinstance(agent_def.get("tools"), dict):
|
|
266
|
+
entry["tools"] = agent_def["tools"]
|
|
267
|
+
|
|
268
|
+
skills = normalize_skills(agent_def.get("skills"))
|
|
269
|
+
if skills:
|
|
270
|
+
entry["skills"] = skills
|
|
271
|
+
|
|
272
|
+
if install.get("subagents"):
|
|
273
|
+
entry["subagents"] = install["subagents"]
|
|
274
|
+
|
|
275
|
+
return entry
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def build_bot_api_account(
|
|
279
|
+
agent_def: dict[str, Any],
|
|
280
|
+
*,
|
|
281
|
+
agent_id_override: str | None = None,
|
|
282
|
+
existing_secret: str | None = None,
|
|
283
|
+
name_override: str | None = None,
|
|
284
|
+
) -> tuple[str, dict[str, Any]] | None:
|
|
285
|
+
if not agent_def.get("bot_api_enabled"):
|
|
286
|
+
return None
|
|
287
|
+
|
|
288
|
+
install = agent_def.get("install", {})
|
|
289
|
+
identity = install.get("identity", {})
|
|
290
|
+
bot_api = install.get("bot_api", {})
|
|
291
|
+
resolved_name = name_override.strip() if isinstance(name_override, str) and name_override.strip() else None
|
|
292
|
+
|
|
293
|
+
source_agent_id = agent_def["agent_id"]
|
|
294
|
+
agent_id = agent_id_override.strip() if isinstance(agent_id_override, str) and agent_id_override.strip() else source_agent_id
|
|
295
|
+
account_id = bot_api.get("account_id", agent_id)
|
|
296
|
+
account_name = bot_api.get("account_name") or resolved_name or identity.get("name", source_agent_id)
|
|
297
|
+
enabled = bot_api.get("enabled", True)
|
|
298
|
+
|
|
299
|
+
return account_id, {
|
|
300
|
+
"agentId": agent_id,
|
|
301
|
+
"name": account_name,
|
|
302
|
+
"apiSecret": existing_secret or secrets.token_hex(32),
|
|
303
|
+
"enabled": enabled,
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def load_openclaw_config(config_path: Path) -> dict[str, Any]:
|
|
308
|
+
if not config_path.is_file():
|
|
309
|
+
raise FileNotFoundError(f"未找到 openclaw.json: {config_path}")
|
|
310
|
+
return load_json(config_path)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def agent_install_state_path(config_path: Path, agent_id: str) -> Path:
|
|
314
|
+
return openclaw_root(config_path) / "agents" / agent_id / "install-state.json"
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def load_install_state(config_path: Path, agent_id: str) -> dict[str, Any] | None:
|
|
318
|
+
path = agent_install_state_path(config_path, agent_id)
|
|
319
|
+
if not path.is_file():
|
|
320
|
+
return None
|
|
321
|
+
return load_json(path)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def write_install_state(config_path: Path, agent_id: str, data: dict[str, Any]) -> Path:
|
|
325
|
+
path = agent_install_state_path(config_path, agent_id)
|
|
326
|
+
payload = dict(data)
|
|
327
|
+
payload["updatedAt"] = datetime.now(timezone.utc).isoformat()
|
|
328
|
+
dump_json(path, payload)
|
|
329
|
+
return path
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def find_agent_entry(config: dict[str, Any], agent_id: str) -> dict[str, Any] | None:
|
|
333
|
+
agents = config.get("agents", {}).get("list", [])
|
|
334
|
+
for agent in agents:
|
|
335
|
+
if isinstance(agent, dict) and agent.get("id") == agent_id:
|
|
336
|
+
return agent
|
|
337
|
+
return None
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def agent_id_matches(entry_id: object, agent_id: str) -> bool:
|
|
341
|
+
return isinstance(entry_id, str) and (entry_id == agent_id or entry_id.startswith(f"{agent_id}-"))
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def read_source_agent_id_from_agent_dir(agent_entry: dict[str, Any]) -> str | None:
|
|
345
|
+
agent_dir = agent_entry.get("agentDir")
|
|
346
|
+
if not isinstance(agent_dir, str) or not agent_dir:
|
|
347
|
+
return None
|
|
348
|
+
|
|
349
|
+
config_path = Path(agent_dir) / ".config.json"
|
|
350
|
+
if not config_path.is_file():
|
|
351
|
+
return None
|
|
352
|
+
|
|
353
|
+
try:
|
|
354
|
+
data = load_json(config_path)
|
|
355
|
+
except (OSError, json.JSONDecodeError):
|
|
356
|
+
return None
|
|
357
|
+
source_agent_id = data.get("agent_id")
|
|
358
|
+
return source_agent_id if isinstance(source_agent_id, str) else None
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def read_source_agent_id_from_install_state(config_path: Path, openclaw_id: str) -> str | None:
|
|
362
|
+
state = load_install_state(config_path, openclaw_id)
|
|
363
|
+
if not state:
|
|
364
|
+
return None
|
|
365
|
+
source_agent_id = state.get("agent_id")
|
|
366
|
+
return source_agent_id if isinstance(source_agent_id, str) else None
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def entry_matches_source_agent(config_path: Path, agent_entry: dict[str, Any], agent_id: str) -> bool:
|
|
370
|
+
entry_id = agent_entry.get("id")
|
|
371
|
+
if agent_id_matches(entry_id, agent_id):
|
|
372
|
+
return True
|
|
373
|
+
|
|
374
|
+
if isinstance(entry_id, str):
|
|
375
|
+
source_agent_id = read_source_agent_id_from_install_state(config_path, entry_id)
|
|
376
|
+
if source_agent_id == agent_id:
|
|
377
|
+
return True
|
|
378
|
+
|
|
379
|
+
source_agent_id = read_source_agent_id_from_agent_dir(agent_entry)
|
|
380
|
+
return source_agent_id == agent_id
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def is_agent_installed(config_path: Path, config: dict[str, Any], agent_id: str) -> bool:
|
|
384
|
+
agents = config.get("agents", {}).get("list", [])
|
|
385
|
+
for agent_entry in agents:
|
|
386
|
+
if isinstance(agent_entry, dict) and entry_matches_source_agent(config_path, agent_entry, agent_id):
|
|
387
|
+
return True
|
|
388
|
+
return False
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def resolve_existing_agent_entry(config_path: Path, config: dict[str, Any], agent_id: str, workspace: str) -> dict[str, Any] | None:
|
|
392
|
+
entry_by_id = find_agent_entry(config, agent_id)
|
|
393
|
+
if entry_by_id is not None:
|
|
394
|
+
return entry_by_id
|
|
395
|
+
entry_by_workspace = find_agent_by_workspace(config, workspace)
|
|
396
|
+
if entry_by_workspace is not None and entry_matches_source_agent(config_path, entry_by_workspace, agent_id):
|
|
397
|
+
return entry_by_workspace
|
|
398
|
+
return None
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def find_agent_by_workspace(config: dict[str, Any], workspace: str) -> dict[str, Any] | None:
|
|
402
|
+
agents = config.get("agents", {}).get("list", [])
|
|
403
|
+
for agent in agents:
|
|
404
|
+
if isinstance(agent, dict) and agent.get("workspace") == workspace:
|
|
405
|
+
return agent
|
|
406
|
+
return None
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def find_bot_api_accounts_by_agent_id(config: dict[str, Any], agent_id: str) -> dict[str, dict[str, Any]]:
|
|
410
|
+
accounts = config.get("channels", {}).get("bot-api", {}).get("accounts", {})
|
|
411
|
+
result: dict[str, dict[str, Any]] = {}
|
|
412
|
+
for account_id, account in accounts.items():
|
|
413
|
+
if isinstance(account, dict) and account.get("agentId") == agent_id:
|
|
414
|
+
result[account_id] = account
|
|
415
|
+
return result
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def read_version_from_agent_dir(agent_entry: dict[str, Any]) -> str | None:
|
|
419
|
+
agent_dir = agent_entry.get("agentDir")
|
|
420
|
+
if not isinstance(agent_dir, str) or not agent_dir:
|
|
421
|
+
return None
|
|
422
|
+
|
|
423
|
+
config_path = Path(agent_dir) / ".config.json"
|
|
424
|
+
if not config_path.is_file():
|
|
425
|
+
return None
|
|
426
|
+
|
|
427
|
+
try:
|
|
428
|
+
data = load_json(config_path)
|
|
429
|
+
except (OSError, json.JSONDecodeError):
|
|
430
|
+
return None
|
|
431
|
+
version = data.get("version")
|
|
432
|
+
return version if isinstance(version, str) else None
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def read_version_from_workspace_git_file(agent_entry: dict[str, Any]) -> str | None:
|
|
436
|
+
workspace = agent_entry.get("workspace")
|
|
437
|
+
if not isinstance(workspace, str) or not workspace:
|
|
438
|
+
return None
|
|
439
|
+
|
|
440
|
+
version_file = Path(workspace) / "git_version"
|
|
441
|
+
if not version_file.is_file():
|
|
442
|
+
return None
|
|
443
|
+
|
|
444
|
+
try:
|
|
445
|
+
return version_file.read_text(encoding="utf-8").strip() or None
|
|
446
|
+
except OSError:
|
|
447
|
+
return None
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def detect_installed_version(config_path: Path, agent_id: str, agent_entry: dict[str, Any]) -> tuple[str | None, str | None]:
|
|
451
|
+
version = read_version_from_agent_dir(agent_entry)
|
|
452
|
+
if version:
|
|
453
|
+
return version, "agent_dir_config"
|
|
454
|
+
|
|
455
|
+
current_openclaw_id = agent_entry.get("id")
|
|
456
|
+
state = None
|
|
457
|
+
if isinstance(current_openclaw_id, str) and current_openclaw_id:
|
|
458
|
+
state = load_install_state(config_path, current_openclaw_id)
|
|
459
|
+
if state:
|
|
460
|
+
state_version = state.get("version")
|
|
461
|
+
if isinstance(state_version, str) and state_version:
|
|
462
|
+
return state_version, "install_state_openclaw_id"
|
|
463
|
+
|
|
464
|
+
if current_openclaw_id != agent_id:
|
|
465
|
+
state = load_install_state(config_path, agent_id)
|
|
466
|
+
else:
|
|
467
|
+
state = load_install_state(config_path, agent_id)
|
|
468
|
+
if state:
|
|
469
|
+
state_version = state.get("version")
|
|
470
|
+
if isinstance(state_version, str) and state_version:
|
|
471
|
+
return state_version, "install_state_agent_id"
|
|
472
|
+
|
|
473
|
+
version = read_version_from_workspace_git_file(agent_entry)
|
|
474
|
+
if version:
|
|
475
|
+
return version, "workspace_git_version"
|
|
476
|
+
|
|
477
|
+
return None, None
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def deep_merge_dict(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
|
|
481
|
+
result = deepcopy(base)
|
|
482
|
+
for key, value in override.items():
|
|
483
|
+
if isinstance(value, dict) and isinstance(result.get(key), dict):
|
|
484
|
+
result[key] = deep_merge_dict(result[key], value)
|
|
485
|
+
else:
|
|
486
|
+
result[key] = deepcopy(value)
|
|
487
|
+
return result
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import shutil
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from common import resolve_agent_directory
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def copy_entry(source: Path, target: Path) -> None:
|
|
14
|
+
if source.is_dir():
|
|
15
|
+
shutil.copytree(source, target, dirs_exist_ok=True)
|
|
16
|
+
else:
|
|
17
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
18
|
+
shutil.copy2(source, target)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def copy_agent_files(agent_id: str, source_path: Path, workspace: Path) -> dict[str, object]:
|
|
22
|
+
agent_dir = resolve_agent_directory(agent_id, source_path)
|
|
23
|
+
workspace.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
|
|
25
|
+
copied_entries: list[str] = []
|
|
26
|
+
for source in sorted(agent_dir.iterdir()):
|
|
27
|
+
target = workspace / source.name
|
|
28
|
+
copy_entry(source, target)
|
|
29
|
+
copied_entries.append(source.name)
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
"ok": True,
|
|
33
|
+
"agent_id": agent_id,
|
|
34
|
+
"agent_directory": str(agent_dir),
|
|
35
|
+
"workspace": str(workspace),
|
|
36
|
+
"copied_entries": copied_entries,
|
|
37
|
+
"message": "已将下载的 Agent 文件拷贝到目标 workspace(不包含最外层 Agent 文件夹)。",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def main() -> int:
|
|
42
|
+
parser = argparse.ArgumentParser(description="拷贝下载的 Agent 文件到 workspace")
|
|
43
|
+
parser.add_argument("--agent-id", required=True, help="Agent ID")
|
|
44
|
+
parser.add_argument("--path", required=True, help="Agent 下载目录(sophhub agent download 的 --path)")
|
|
45
|
+
parser.add_argument("--workspace", required=True, help="目标 workspace 路径")
|
|
46
|
+
args = parser.parse_args()
|
|
47
|
+
|
|
48
|
+
result = copy_agent_files(
|
|
49
|
+
args.agent_id,
|
|
50
|
+
Path(args.path).expanduser().resolve(),
|
|
51
|
+
Path(args.workspace).expanduser().resolve(),
|
|
52
|
+
)
|
|
53
|
+
json.dump(result, sys.stdout, indent=2, ensure_ascii=False)
|
|
54
|
+
sys.stdout.write("\n")
|
|
55
|
+
return 0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
raise SystemExit(main())
|