-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstart.ps1
More file actions
352 lines (313 loc) · 13.9 KB
/
Copy pathstart.ps1
File metadata and controls
352 lines (313 loc) · 13.9 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# start.ps1 — 一键启停全部 Shiki 服务
# Usage:
# .\start.ps1 启动全部服务
# .\start.ps1 -Stop 停止全部服务(等价于 shutdown_all.py)
#
# 启动顺序:
# 1. llama-server (ngl=41, batch=2048/1024)
# 2. Embedding Server (localhost:9999, all-MiniLM-L6-v2)
# 3. Live2D Bridge (localhost:19200)
# 4. OpenClaw Gateway (localhost:18789)
# 5. Cron Jobs (mem0-auto-sync + cleanup-orphans)
# 6. llama-watchdog (Task Scheduler)
#
# Stop 模式:
# 1. 禁用 llama-watchdog(防止自动重启)
# 2. 调用 shutdown_all.py 停止所有进程
param([switch]$Stop)
$ErrorActionPreference = "Stop"
# Gateway start 可能返回非零 exit(已运行时),不要因此中断
$global:ProgressPreference = 'SilentlyContinue'
[Console]::OutputEncoding = [Text.Encoding]::UTF8
$scriptRoot = $PSScriptRoot
# ========== Watchdog 计划任务管理 ==========
$watchdogTaskName = "AI_Girlfriend_llama_watchdog"
function Enable-Watchdog {
try {
$task = schtasks /query /tn $watchdogTaskName 2>$null
if ($LASTEXITCODE -eq 0) {
$state = schtasks /query /tn $watchdogTaskName /fo CSV | Select-Object -Skip 1 | ConvertFrom-Csv
if ($state.Status -eq "Ready") {
Write-Host " llama-watchdog task already enabled" -ForegroundColor Green
return
}
schtasks /change /tn $watchdogTaskName /enable | Out-Null
Write-Host " llama-watchdog task re-enabled" -ForegroundColor Green
} else {
Write-Host " WARNING: llama-watchdog task not found ($watchdogTaskName)" -ForegroundColor Yellow
Write-Host " Create it with: schtasks /create /tn $watchdogTaskName /tr ... /sc minute /mo 10" -ForegroundColor Yellow
}
} catch {
Write-Host " WARNING: Could not manage watchdog task: $_" -ForegroundColor Yellow
}
}
function Disable-Watchdog {
try {
$task = schtasks /query /tn $watchdogTaskName 2>$null
if ($LASTEXITCODE -eq 0) {
schtasks /change /tn $watchdogTaskName /disable | Out-Null
Write-Host " llama-watchdog task disabled (won't auto-restart)" -ForegroundColor Cyan
}
} catch {
# silently ignore — task might not exist
}
}
# ========== Stop mode ==========
if ($Stop) {
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " 四季夏目 — Shutdown" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "[1/2] Disabling llama-watchdog..." -ForegroundColor Yellow
Disable-Watchdog
Write-Host "[2/2] Stopping all processes..." -ForegroundColor Yellow
$shutdownScript = Join-Path $scriptRoot "shutdown_all.py"
if (-not (Test-Path $shutdownScript)) {
Write-Host "ERROR: shutdown_all.py not found at $shutdownScript" -ForegroundColor Red
exit 1
}
$pyExe = (Get-Command python -ErrorAction SilentlyContinue).Source
if (-not $pyExe) { $pyExe = (Get-Command py -ErrorAction SilentlyContinue).Source }
if (-not $pyExe) {
Write-Host "ERROR: python not found in PATH" -ForegroundColor Red
exit 1
}
& $pyExe $shutdownScript
exit $LASTEXITCODE
}
# ========== 读取 config.yaml ==========
$configPath = Join-Path $scriptRoot "config.yaml"
if (-not (Test-Path $configPath)) {
Write-Host "ERROR: config.yaml not found at $configPath" -ForegroundColor Red
Write-Host "Run quick_setup.ps1 first, or copy config.example.yaml → config.yaml" -ForegroundColor Yellow
exit 1
}
$configRaw = Get-Content $configPath -Raw -Encoding UTF8
function Get-YamlValue($raw, $key) {
$pattern = "(?m)^\s*${key}\s*:\s*`"?(.+?)`"?\s*$"
$m = [regex]::Match($raw, $pattern)
if ($m.Success) { return $m.Groups[1].Value.Trim('"').Trim() }
return $null
}
$llamaExe = Get-YamlValue $configRaw 'llama_exe'
$llamaModel = Get-YamlValue $configRaw 'llama_model'
$llamaPort = Get-YamlValue $configRaw 'llama_port'
if (-not $llamaPort) { $llamaPort = 8080 }
$llamaLogDir = Get-YamlValue $configRaw 'llama_log_dir'
$workspace = Get-YamlValue $configRaw 'workspace'
$mem0Bridge = Get-YamlValue $configRaw 'mem0_bridge'
$embedScript = Get-YamlValue $configRaw 'embedding_server'
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " 四季夏目 — Shiki Natsume Startup" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
function Test-Online($port, $label) {
try {
$null = Invoke-WebRequest -Uri "http://127.0.0.1:${port}/health" -TimeoutSec 2 -UseBasicParsing
Write-Host " ${label} already online (port ${port})" -ForegroundColor Green
return $true
} catch {
return $false
}
}
# ========== 1. Llama Server ==========
Write-Host "[1/6] llama-server" -ForegroundColor Yellow
if (Test-Online $llamaPort "llama-server") {
# already up — skip
} else {
if (-not (Test-Path $llamaExe)) {
Write-Host " ERROR: llama-server.exe not found: $llamaExe" -ForegroundColor Red
Write-Host " Check config.yaml → llama_exe" -ForegroundColor Yellow
exit 1
}
if (-not (Test-Path $llamaModel)) {
Write-Host " ERROR: Model not found: $llamaModel" -ForegroundColor Red
Write-Host " Check config.yaml → llama_model" -ForegroundColor Yellow
exit 1
}
Write-Host " Starting llama-server (ngl=41, batch=2048/1024)..."
Get-Process llama-server -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
$llamaArgs = @(
'-m', $llamaModel,
'-c', '120000',
'--flash-attn', 'on',
'-ctk', 'q8_0',
'-ctv', 'q8_0',
'--no-mmap',
'--cpu-moe',
'--batch-size', '2048',
'--ubatch-size', '1024',
'--threads', '24',
'-rea', 'off',
'--jinja',
'--cache-ram', '3000',
'--parallel', '1',
'--kv-unified',
'--no-mmap',
'--port', $llamaPort,
'--timeout', '600'
)
$llamaErrLog = Join-Path $llamaLogDir "llama-err.log"
$null = New-Item -ItemType Directory -Force -Path $llamaLogDir -ErrorAction SilentlyContinue
$null = Start-Process -FilePath $llamaExe -ArgumentList $llamaArgs -WindowStyle Hidden `
-RedirectStandardError $llamaErrLog
Write-Host " Waiting for llama-server to load model..."
$sw = [Diagnostics.Stopwatch]::StartNew()
$maxWait = 300
$ready = $false
while ($sw.Elapsed.TotalSeconds -lt $maxWait) {
try {
$r = Invoke-WebRequest -Uri "http://127.0.0.1:${llamaPort}/health" -TimeoutSec 3 -UseBasicParsing
if ($r.StatusCode -eq 200) {
$took = [math]::Round($sw.Elapsed.TotalSeconds, 1)
Write-Host " llama-server ready! (${took}s)" -ForegroundColor Green
$ready = $true
break
}
} catch {}
Start-Sleep -Seconds 2
}
if (-not $ready) {
Write-Host " WARNING: llama-server not responding after ${maxWait}s — continuing anyway" -ForegroundColor Yellow
}
}
# ========== 2. Embedding Server ==========
Write-Host "[2/6] Embedding Server" -ForegroundColor Yellow
$embedPort = 9999
if (-not $embedScript) {
$embedScript = Join-Path $scriptRoot "skills\shared\embedding_server.py"
}
if (Test-Online $embedPort "Embedding Server") {
# already up
} else {
if (-not (Test-Path $embedScript)) {
Write-Host " WARNING: embedding_server.py not found at $embedScript" -ForegroundColor Yellow
Write-Host " OpenClaw memory_search (semantic) will degrade to BM25-only" -ForegroundColor Yellow
} else {
Write-Host " Starting embedding server (all-MiniLM-L6-v2 on port ${embedPort})..."
$pyExe = (Get-Command python -ErrorAction SilentlyContinue).Source
if (-not $pyExe) { $pyExe = (Get-Command py -ErrorAction SilentlyContinue).Source }
if (-not $pyExe) {
Write-Host " WARNING: python not found — skip embedding server" -ForegroundColor Yellow
} else {
$null = Start-Process -FilePath $pyExe -ArgumentList $embedScript -WindowStyle Hidden
Start-Sleep -Seconds 3
$swEmb = [Diagnostics.Stopwatch]::StartNew()
$readyEmb = $false
while ($swEmb.Elapsed.TotalSeconds -lt 45) {
try {
$null = Invoke-WebRequest -Uri "http://127.0.0.1:${embedPort}/health" -TimeoutSec 3 -UseBasicParsing
Write-Host " Embedding server ready! (port ${embedPort})" -ForegroundColor Green
$readyEmb = $true
break
} catch {}
Start-Sleep -Seconds 2
}
if (-not $readyEmb) {
Write-Host " WARNING: Embedding server not responding after 45s" -ForegroundColor Yellow
}
}
}
}
# ========== 3. Live2D Bridge ==========
Write-Host "[3/6] Live2D Bridge" -ForegroundColor Yellow
if (Test-Online 19200 "Live2D Bridge") {
# already up — skip
} else {
$live2dDir = Join-Path $scriptRoot "live2d"
if (-not (Test-Path (Join-Path $live2dDir "live2d-bridge.mjs"))) {
Write-Host " WARNING: live2d-bridge.mjs not found in $live2dDir" -ForegroundColor Yellow
} else {
Write-Host " Starting Live2D Bridge..."
$null = Start-Process -FilePath node -ArgumentList "live2d-bridge.mjs" -WorkingDirectory $live2dDir -WindowStyle Hidden
Start-Sleep -Seconds 2
$sw2 = [Diagnostics.Stopwatch]::StartNew()
$ready2 = $false
while ($sw2.Elapsed.TotalSeconds -lt 10) {
try {
$null = Invoke-WebRequest -Uri "http://localhost:19200/api/status" -TimeoutSec 2 -UseBasicParsing
Write-Host " Live2D Bridge ready! (port 19200)" -ForegroundColor Green
$ready2 = $true
break
} catch {}
Start-Sleep -Seconds 1
}
if (-not $ready2) {
Write-Host " WARNING: Bridge not responding after 10s" -ForegroundColor Yellow
}
}
}
# ========== 4. OpenClaw Gateway ==========
Write-Host "[4/6] OpenClaw Gateway" -ForegroundColor Yellow
try {
$gwStatus = & openclaw gateway status 2>&1
if ($gwStatus -match 'running') {
Write-Host " Gateway already running" -ForegroundColor Green
} else {
Write-Host " Starting gateway..."
& openclaw gateway start 2>&1 | Out-Null
Start-Sleep -Seconds 2
# Verify it actually started
$gwVerify = & openclaw gateway status 2>&1
if ($gwVerify -match 'running') {
Write-Host " Gateway started" -ForegroundColor Green
} else {
Write-Host " WARNING: Gateway may not have started. Check: openclaw gateway status" -ForegroundColor Yellow
}
}
} catch {
Write-Host " WARNING: Could not check/start gateway: $($_.Exception.Message)" -ForegroundColor Yellow
}
Start-Sleep -Seconds 2
# ========== 5. Mem0 Auto-Sync Cron Job ==========
Write-Host "[5/6] Memory System (mem0 → OpenClaw cron)" -ForegroundColor Yellow
if (-not $mem0Bridge) {
$mem0Bridge = Join-Path $scriptRoot "skills\shared\mem0_bridge.py"
}
if (Test-Path $mem0Bridge) {
Write-Host " mem0_bridge.py found" -ForegroundColor Green
# 清理旧的冗余 cron job(单角色 sync)
$oldJobNames = @("mem0-to-markdown-sync", "mem0-sync-natsume", "mem0-sync-enola", "mem0-sync-atori")
foreach ($name in $oldJobNames) {
try {
$check = & openclaw cron list 2>$null | Select-String $name -SimpleMatch
if ($check) {
Write-Host " Removing deprecated cron job: $name" -ForegroundColor DarkGray
}
} catch {}
}
# 检查 mem0-auto-sync 是否存在(统一四角色同步)
try {
$check = & openclaw cron list 2>$null | Select-String "mem0-auto-sync" -SimpleMatch
if ($check) {
Write-Host " mem0-auto-sync cron job already registered (every 30 min)" -ForegroundColor Green
} else {
Write-Host " Registering mem0-auto-sync cron job..."
$mem0SyncCron = Join-Path $scriptRoot "skills\shared\mem0_sync_cron.py"
$syncMsg = "Run: python `"$mem0SyncCron`". Only exec, nothing else."
& openclaw cron add --% --name mem0-auto-sync --every-ms 1800000 --message "$syncMsg" --timeout 60 --light-context --no-deliver 2>&1 | Out-Null
Write-Host " mem0-auto-sync cron job registered! (every 30 min, all 4 characters)" -ForegroundColor Green
}
} catch {
Write-Host " WARNING: Could not register mem0 cron job. Gateway may not be ready yet." -ForegroundColor Yellow
Write-Host " Run manually later: openclaw cron add ..." -ForegroundColor Yellow
}
} else {
Write-Host " WARNING: mem0_bridge.py not found at $mem0Bridge" -ForegroundColor Yellow
Write-Host " Memory sync cron job skipped" -ForegroundColor Yellow
}
# ========== 6. Enable Watchdog ==========
Write-Host "[6/6] llama-watchdog" -ForegroundColor Yellow
Enable-Watchdog
# ========== Done ==========
Write-Host ""
Write-Host "============================================" -ForegroundColor Green
Write-Host " All services ready!" -ForegroundColor Green
Write-Host " llama-server : http://127.0.0.1:${llamaPort}" -ForegroundColor Green
Write-Host " Embedding : http://127.0.0.1:${embedPort} (hybrid memory search)" -ForegroundColor Green
Write-Host " Live2D Bridge : http://localhost:19200" -ForegroundColor Green
Write-Host " Gateway : http://127.0.0.1:18789" -ForegroundColor Green
Write-Host " Mem0 Sync Cron : every 30 min (Qdrant → _mem0_auto.md)" -ForegroundColor Green
Write-Host " Watchdog : enabled (auto-restart if crash)" -ForegroundColor Green
Write-Host "============================================" -ForegroundColor Green