forked from cft0808/edict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
308 lines (272 loc) · 12.1 KB
/
install.ps1
File metadata and controls
308 lines (272 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# ══════════════════════════════════════════════════════════════
# 三省六部 · OpenClaw Multi-Agent System 一键安装脚本 (Windows)
# PowerShell 版本 — 对应 install.sh
# ══════════════════════════════════════════════════════════════
#Requires -Version 5.1
$ErrorActionPreference = "Stop"
$REPO_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
$OC_HOME = Join-Path $env:USERPROFILE ".openclaw"
$OC_CFG = Join-Path $OC_HOME "openclaw.json"
function Write-Banner {
Write-Host ""
Write-Host "╔══════════════════════════════════════════╗" -ForegroundColor Blue
Write-Host "║ 🏛️ 三省六部 · OpenClaw Multi-Agent ║" -ForegroundColor Blue
Write-Host "║ 安装向导 (Windows) ║" -ForegroundColor Blue
Write-Host "╚══════════════════════════════════════════╝" -ForegroundColor Blue
Write-Host ""
}
function Log { param($msg) Write-Host "✅ $msg" -ForegroundColor Green }
function Warn { param($msg) Write-Host "⚠️ $msg" -ForegroundColor Yellow }
function Error { param($msg) Write-Host "❌ $msg" -ForegroundColor Red }
function Info { param($msg) Write-Host "ℹ️ $msg" -ForegroundColor Blue }
# ── Step 0: 依赖检查 ──
function Check-Deps {
Info "检查依赖..."
$oc = Get-Command openclaw -ErrorAction SilentlyContinue
if (-not $oc) {
Error "未找到 openclaw CLI。请先安装 OpenClaw: https://openclaw.ai"
exit 1
}
Log "OpenClaw CLI: OK"
$py = Get-Command python3 -ErrorAction SilentlyContinue
if (-not $py) {
$py = Get-Command python -ErrorAction SilentlyContinue
}
if (-not $py) {
Error "未找到 python3 或 python"
exit 1
}
$global:PYTHON = $py.Source
Log "Python: $($global:PYTHON)"
if (-not (Test-Path $OC_CFG)) {
Error "未找到 openclaw.json。请先运行 openclaw 完成初始化。"
exit 1
}
Log "openclaw.json: $OC_CFG"
}
# ── Step 0.5: 备份已有 Agent 数据 ──
function Backup-Existing {
$hasExisting = Get-ChildItem -Path $OC_HOME -Directory -Filter "workspace-*" -ErrorAction SilentlyContinue
if ($hasExisting) {
Info "检测到已有 Agent Workspace,自动备份中..."
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
$backupDir = Join-Path $OC_HOME "backups\pre-install-$ts"
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
Get-ChildItem -Path $OC_HOME -Directory -Filter "workspace-*" | ForEach-Object {
Copy-Item -Path $_.FullName -Destination (Join-Path $backupDir $_.Name) -Recurse
}
if (Test-Path $OC_CFG) {
Copy-Item $OC_CFG (Join-Path $backupDir "openclaw.json")
}
Log "已备份到: $backupDir"
}
}
# ── Step 1: 创建 Workspace ──
function Create-Workspaces {
Info "创建 Agent Workspace..."
$agents = @("taizi","zhongshu","menxia","shangshu","hubu","libu","bingbu","xingbu","gongbu","libu_hr","zaochao")
foreach ($agent in $agents) {
$ws = Join-Path $OC_HOME "workspace-$agent"
New-Item -ItemType Directory -Path (Join-Path $ws "skills") -Force | Out-Null
$soulSrc = Join-Path $REPO_DIR "agents\$agent\SOUL.md"
$soulDst = Join-Path $ws "SOUL.md"
if (Test-Path $soulSrc) {
if (Test-Path $soulDst) {
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
Copy-Item $soulDst "$soulDst.bak.$ts"
Warn "已备份旧 SOUL.md → $soulDst.bak.$ts"
}
$content = (Get-Content $soulSrc -Raw) -replace "__REPO_DIR__", $REPO_DIR
Set-Content -Path $soulDst -Value $content -Encoding UTF8
}
Log "Workspace 已创建: $ws"
# AGENTS.md
$agentsMd = @"
# AGENTS.md · 工作协议
1. 接到任务先回复"已接旨"。
2. 输出必须包含:任务ID、结果、证据/文件路径、阻塞项。
3. 需要协作时,回复尚书省请求转派,不跨部直连。
4. 涉及删除/外发动作必须明确标注并等待批准。
"@
Set-Content -Path (Join-Path $ws "AGENTS.md") -Value $agentsMd -Encoding UTF8
}
}
# ── Step 2: 注册 Agents ──
function Register-Agents {
Info "注册三省六部 Agents..."
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
Copy-Item $OC_CFG "$OC_CFG.bak.sansheng-$ts"
Log "已备份配置: $OC_CFG.bak.*"
$pyScript = @"
import json, pathlib, sys, os
cfg_path = pathlib.Path(os.environ['USERPROFILE']) / '.openclaw' / 'openclaw.json'
cfg = json.loads(cfg_path.read_text(encoding='utf-8'))
AGENTS = [
{"id": "taizi", "subagents": {"allowAgents": ["zhongshu"]}},
{"id": "zhongshu", "subagents": {"allowAgents": ["menxia", "shangshu"]}},
{"id": "menxia", "subagents": {"allowAgents": ["shangshu", "zhongshu"]}},
{"id": "shangshu", "subagents": {"allowAgents": ["zhongshu", "menxia", "hubu", "libu", "bingbu", "xingbu", "gongbu", "libu_hr"]}},
{"id": "hubu", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "libu", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "bingbu", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "xingbu", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "gongbu", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "libu_hr", "subagents": {"allowAgents": ["shangshu"]}},
{"id": "zaochao", "subagents": {"allowAgents": []}},
]
agents_cfg = cfg.setdefault('agents', {})
agents_list = agents_cfg.get('list', [])
existing_ids = {a['id'] for a in agents_list}
added = 0
for ag in AGENTS:
ag_id = ag['id']
ws = str(pathlib.Path(os.environ['USERPROFILE']) / f'.openclaw/workspace-{ag_id}')
if ag_id not in existing_ids:
entry = {'id': ag_id, 'workspace': ws, **{k:v for k,v in ag.items() if k!='id'}}
agents_list.append(entry)
added += 1
print(f' + added: {ag_id}')
else:
print(f' ~ exists: {ag_id} (skipped)')
agents_cfg['list'] = agents_list
# Fix #142: clean invalid binding pattern
bindings = cfg.get('bindings', [])
for b in bindings:
match = b.get('match', {})
if isinstance(match, dict) and 'pattern' in match:
del match['pattern']
print(f' cleaned invalid pattern from binding: {b.get("agentId", "?")}')
cfg_path.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding='utf-8')
print(f'Done: {added} agents added')
"@
& $global:PYTHON -c $pyScript
Log "Agents 注册完成"
}
# ── Step 3: 初始化 Data ──
function Init-Data {
Info "初始化数据目录..."
$dataDir = Join-Path $REPO_DIR "data"
New-Item -ItemType Directory -Path $dataDir -Force | Out-Null
foreach ($f in @("live_status.json","agent_config.json","model_change_log.json")) {
$fp = Join-Path $dataDir $f
if (-not (Test-Path $fp)) { Set-Content $fp "{}" -Encoding UTF8 }
}
Set-Content (Join-Path $dataDir "pending_model_changes.json") "[]" -Encoding UTF8
Log "数据目录初始化完成"
}
# ── Step 3.3: 创建 data/scripts 目录连接 (Junction) ──
function Link-Resources {
Info "创建 data/scripts 目录连接..."
$linked = 0
$agents = @("taizi","zhongshu","menxia","shangshu","hubu","libu","bingbu","xingbu","gongbu","libu_hr","zaochao")
foreach ($agent in $agents) {
$ws = Join-Path $OC_HOME "workspace-$agent"
New-Item -ItemType Directory -Path $ws -Force | Out-Null
# data 目录
$wsData = Join-Path $ws "data"
$srcData = Join-Path $REPO_DIR "data"
if (-not (Test-Path $wsData)) {
cmd /c mklink /J "$wsData" "$srcData" | Out-Null
$linked++
} elseif (-not ((Get-Item $wsData).Attributes -band [IO.FileAttributes]::ReparsePoint)) {
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
Rename-Item $wsData "$wsData.bak.$ts"
cmd /c mklink /J "$wsData" "$srcData" | Out-Null
$linked++
}
# scripts 目录
$wsScripts = Join-Path $ws "scripts"
$srcScripts = Join-Path $REPO_DIR "scripts"
if (-not (Test-Path $wsScripts)) {
cmd /c mklink /J "$wsScripts" "$srcScripts" | Out-Null
$linked++
} elseif (-not ((Get-Item $wsScripts).Attributes -band [IO.FileAttributes]::ReparsePoint)) {
$ts = Get-Date -Format "yyyyMMdd-HHmmss"
Rename-Item $wsScripts "$wsScripts.bak.$ts"
cmd /c mklink /J "$wsScripts" "$srcScripts" | Out-Null
$linked++
}
}
Log "已创建 $linked 个目录连接 (data/scripts → 项目目录)"
}
# ── Step 3.5: 设置 Agent 间通信可见性 ──
function Setup-Visibility {
Info "配置 Agent 间消息可见性..."
try {
openclaw config set tools.sessions.visibility all 2>$null
Log "已设置 tools.sessions.visibility=all"
} catch {
Warn "设置 visibility 失败,请手动执行: openclaw config set tools.sessions.visibility all"
}
}
# ── Step 4: 构建前端 ──
function Build-Frontend {
Info "构建 React 前端..."
$node = Get-Command node -ErrorAction SilentlyContinue
if (-not $node) {
Warn "未找到 node,跳过前端构建。"
Warn "请安装 Node.js 18+ 后运行: cd edict\frontend && npm install && npm run build"
return
}
$pkgJson = Join-Path $REPO_DIR "edict\frontend\package.json"
if (Test-Path $pkgJson) {
Push-Location (Join-Path $REPO_DIR "edict\frontend")
npm install --silent 2>$null
npm run build 2>$null
Pop-Location
$indexHtml = Join-Path $REPO_DIR "dashboard\dist\index.html"
if (Test-Path $indexHtml) {
Log "前端构建完成: dashboard\dist\"
} else {
Warn "前端构建可能失败,请手动检查"
}
}
}
# ── Step 5: 首次数据同步 ──
function First-Sync {
Info "执行首次数据同步..."
Push-Location $REPO_DIR
$env:REPO_DIR = $REPO_DIR
try { & $global:PYTHON scripts/sync_agent_config.py } catch { Warn "sync_agent_config 有警告" }
try { & $global:PYTHON scripts/sync_officials_stats.py } catch { Warn "sync_officials_stats 有警告" }
try { & $global:PYTHON scripts/refresh_live_data.py } catch { Warn "refresh_live_data 有警告" }
Pop-Location
Log "首次同步完成"
}
# ── Step 6: 重启 Gateway ──
function Restart-Gateway {
Info "重启 OpenClaw Gateway..."
try {
openclaw gateway restart 2>$null
Log "Gateway 重启成功"
} catch {
Warn "Gateway 重启失败,请手动重启: openclaw gateway restart"
}
}
# ── Main ──
Write-Banner
Check-Deps
Backup-Existing
Create-Workspaces
Register-Agents
Init-Data
Link-Resources
Setup-Visibility
Build-Frontend
First-Sync
Restart-Gateway
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "║ 🎉 三省六部安装完成! ║" -ForegroundColor Green
Write-Host "╚══════════════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""
Write-Host "下一步:"
Write-Host " 1. 配置 API Key(如尚未配置):"
Write-Host " openclaw agents add taizi # 按提示输入 Anthropic API Key"
Write-Host " .\install.ps1 # 重新运行以同步到所有 Agent"
Write-Host " 2. 启动数据刷新循环: bash scripts/run_loop.sh"
Write-Host " 3. 启动看板服务器: python dashboard/server.py"
Write-Host " 4. 打开看板: http://127.0.0.1:7891"
Write-Host ""
Warn "首次安装必须配置 API Key,否则 Agent 会报错"
Info "文档: docs/getting-started.md"