@cocorograph/hub-agent 0.5.5 → 0.5.7
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/package.json +1 -1
- package/src/agents.mjs +79 -0
- package/src/main.mjs +41 -1
- package/src/ws-client.mjs +121 -1
package/package.json
CHANGED
package/src/agents.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code の subagent (`~/.claude/agents/<category>/<name>.md`) を集計する。
|
|
3
|
+
*
|
|
4
|
+
* - 各 .md の先頭 H1 から title / description を抽出 ("Title — description")
|
|
5
|
+
* - category=ディレクトリ名、name=basename
|
|
6
|
+
*
|
|
7
|
+
* 移植元: D00000_cockpit/webapp/lib/agents.ts
|
|
8
|
+
*/
|
|
9
|
+
import { promises as fs } from "node:fs"
|
|
10
|
+
import os from "node:os"
|
|
11
|
+
import path from "node:path"
|
|
12
|
+
|
|
13
|
+
const AGENTS_DIR = process.env.CLAUDE_AGENTS_DIR || path.join(os.homedir(), ".claude", "agents")
|
|
14
|
+
|
|
15
|
+
function parseH1(text) {
|
|
16
|
+
for (const line of text.split("\n")) {
|
|
17
|
+
const m = line.match(/^#\s+(.+)$/)
|
|
18
|
+
if (!m) continue
|
|
19
|
+
const heading = m[1].trim()
|
|
20
|
+
const dashIdx = heading.search(/\s*[-—–]\s*/)
|
|
21
|
+
if (dashIdx > 0) {
|
|
22
|
+
const title = heading.slice(0, dashIdx).trim()
|
|
23
|
+
const description = heading.slice(dashIdx).replace(/^[\s\-—–]+/, "").trim()
|
|
24
|
+
return { title, description }
|
|
25
|
+
}
|
|
26
|
+
return { title: heading, description: "" }
|
|
27
|
+
}
|
|
28
|
+
return { title: "", description: "" }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function listAgents() {
|
|
32
|
+
const agents = []
|
|
33
|
+
let categories
|
|
34
|
+
try {
|
|
35
|
+
categories = await fs.readdir(AGENTS_DIR)
|
|
36
|
+
} catch {
|
|
37
|
+
return []
|
|
38
|
+
}
|
|
39
|
+
for (const cat of categories) {
|
|
40
|
+
if (cat.startsWith(".") || cat.startsWith("_")) continue
|
|
41
|
+
const catPath = path.join(AGENTS_DIR, cat)
|
|
42
|
+
let st
|
|
43
|
+
try {
|
|
44
|
+
st = await fs.stat(catPath)
|
|
45
|
+
} catch {
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
if (!st.isDirectory()) continue
|
|
49
|
+
let files
|
|
50
|
+
try {
|
|
51
|
+
files = await fs.readdir(catPath)
|
|
52
|
+
} catch {
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
for (const f of files) {
|
|
56
|
+
if (!f.endsWith(".md")) continue
|
|
57
|
+
if (f.startsWith("_")) continue
|
|
58
|
+
const fp = path.join(catPath, f)
|
|
59
|
+
try {
|
|
60
|
+
const text = await fs.readFile(fp, "utf-8")
|
|
61
|
+
const { title, description } = parseH1(text)
|
|
62
|
+
const baseName = f.replace(/\.md$/, "")
|
|
63
|
+
agents.push({
|
|
64
|
+
name: baseName,
|
|
65
|
+
category: cat,
|
|
66
|
+
title: title || baseName,
|
|
67
|
+
description: description || "",
|
|
68
|
+
path: `${cat}/${baseName}`,
|
|
69
|
+
})
|
|
70
|
+
} catch {
|
|
71
|
+
/* skip */
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
agents.sort(
|
|
76
|
+
(a, b) => a.category.localeCompare(b.category) || a.name.localeCompare(b.name),
|
|
77
|
+
)
|
|
78
|
+
return agents
|
|
79
|
+
}
|
package/src/main.mjs
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*
|
|
11
11
|
* 仕様書: ナレッジ/インフラ/cockpit-hub-hosted-integration-spec (id=6080)
|
|
12
12
|
*/
|
|
13
|
+
import { readFileSync } from "node:fs"
|
|
13
14
|
import { readFile } from "node:fs/promises"
|
|
14
15
|
import os from "node:os"
|
|
15
16
|
import path from "node:path"
|
|
@@ -20,6 +21,7 @@ import { readConfig } from "./config.mjs"
|
|
|
20
21
|
import { loadPlugins, runHookBroadcast, runHookChain } from "./plugin-loader.mjs"
|
|
21
22
|
import { WsClient } from "./ws-client.mjs"
|
|
22
23
|
import { PtyBridge } from "./pty-bridge.mjs"
|
|
24
|
+
import { listAgents } from "./agents.mjs"
|
|
23
25
|
import { listSkills } from "./skills.mjs"
|
|
24
26
|
import { listSessionStates } from "./state.mjs"
|
|
25
27
|
import {
|
|
@@ -52,6 +54,20 @@ async function readBundleVersion() {
|
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
/**
|
|
58
|
+
* WsClient の runtime refresh / fs.watch コールバックから同期で呼ばれる版。
|
|
59
|
+
* 失敗時は null を返す (silent fail)。
|
|
60
|
+
*/
|
|
61
|
+
function readBundleVersionSync() {
|
|
62
|
+
try {
|
|
63
|
+
const text = readFileSync(BUNDLE_MANIFEST_PATH, "utf-8")
|
|
64
|
+
const data = JSON.parse(text)
|
|
65
|
+
return typeof data?.version === "string" ? data.version : null
|
|
66
|
+
} catch {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
export async function startDaemon({ version, ptyModule } = {}) {
|
|
56
72
|
const config = await readConfig()
|
|
57
73
|
if (!config) {
|
|
@@ -73,7 +89,13 @@ export async function startDaemon({ version, ptyModule } = {}) {
|
|
|
73
89
|
logger.info({ bundleVersion }, "hub bundle version detected")
|
|
74
90
|
}
|
|
75
91
|
|
|
76
|
-
const client = new WsClient(config, {
|
|
92
|
+
const client = new WsClient(config, {
|
|
93
|
+
logger,
|
|
94
|
+
version,
|
|
95
|
+
bundleVersion,
|
|
96
|
+
bundleVersionProvider: readBundleVersionSync,
|
|
97
|
+
bundleManifestPath: BUNDLE_MANIFEST_PATH,
|
|
98
|
+
})
|
|
77
99
|
|
|
78
100
|
// EventEmitter の 'error' は listener が無いと process が落ちる。
|
|
79
101
|
// ws-client は close 側で reconnect を予約しているので、ここでは log だけ。
|
|
@@ -405,6 +427,24 @@ async function dispatch(msg, ctx) {
|
|
|
405
427
|
}
|
|
406
428
|
return
|
|
407
429
|
}
|
|
430
|
+
case "agents.request": {
|
|
431
|
+
try {
|
|
432
|
+
const agents = await listAgents()
|
|
433
|
+
ctx.client.send({
|
|
434
|
+
type: "agents.response",
|
|
435
|
+
request_id: msg.request_id,
|
|
436
|
+
agents,
|
|
437
|
+
})
|
|
438
|
+
} catch (err) {
|
|
439
|
+
ctx.client.send({
|
|
440
|
+
type: "agents.response",
|
|
441
|
+
request_id: msg.request_id,
|
|
442
|
+
agents: [],
|
|
443
|
+
error: err.message,
|
|
444
|
+
})
|
|
445
|
+
}
|
|
446
|
+
return
|
|
447
|
+
}
|
|
408
448
|
case "usage.request": {
|
|
409
449
|
try {
|
|
410
450
|
const [usage, sessions] = await Promise.all([
|
package/src/ws-client.mjs
CHANGED
|
@@ -5,10 +5,14 @@
|
|
|
5
5
|
* - 起動時に `hello`、30s おきに `heartbeat` を送信
|
|
6
6
|
* - 切断時は exponential backoff (1s, 2s, 4s, ..., max 30s) で再接続
|
|
7
7
|
* - サーバから受け取った JSON は `onMessage` callback に渡す
|
|
8
|
+
* - `bundleVersionProvider` + `bundleManifestPath` を渡すと、manifest.json の
|
|
9
|
+
* 変更を fs.watch で検知して即時 heartbeat を送信し Cockpit UI に最新版を
|
|
10
|
+
* リアルタイム反映する (heartbeat 30s 待ちを回避)
|
|
8
11
|
*
|
|
9
12
|
* 仕様書: ナレッジ/インフラ/cockpit-hub-hosted-integration-spec (id=6080)
|
|
10
13
|
*/
|
|
11
14
|
import { EventEmitter } from "node:events"
|
|
15
|
+
import fs from "node:fs"
|
|
12
16
|
import os from "node:os"
|
|
13
17
|
|
|
14
18
|
import WebSocket from "ws"
|
|
@@ -16,11 +20,19 @@ import WebSocket from "ws"
|
|
|
16
20
|
const HEARTBEAT_INTERVAL_MS = 30_000
|
|
17
21
|
const MIN_BACKOFF_MS = 1_000
|
|
18
22
|
const MAX_BACKOFF_MS = 30_000
|
|
23
|
+
const BUNDLE_WATCH_DEBOUNCE_MS = 500
|
|
19
24
|
|
|
20
25
|
export class WsClient extends EventEmitter {
|
|
21
26
|
/**
|
|
22
27
|
* @param {{ hub_url: string, agent_id: string, agent_token: string }} config
|
|
23
|
-
* @param {{
|
|
28
|
+
* @param {{
|
|
29
|
+
* logger?: import('pino').Logger,
|
|
30
|
+
* version?: string,
|
|
31
|
+
* bundleVersion?: string|null,
|
|
32
|
+
* bundleVersionProvider?: () => (string|null),
|
|
33
|
+
* bundleManifestPath?: string|null,
|
|
34
|
+
* hostname?: string,
|
|
35
|
+
* }} opts
|
|
24
36
|
*/
|
|
25
37
|
constructor(config, opts = {}) {
|
|
26
38
|
super()
|
|
@@ -28,6 +40,13 @@ export class WsClient extends EventEmitter {
|
|
|
28
40
|
this.logger = opts.logger
|
|
29
41
|
this.version = opts.version || "0.1.0"
|
|
30
42
|
this.bundleVersion = opts.bundleVersion || null
|
|
43
|
+
this.bundleVersionProvider =
|
|
44
|
+
typeof opts.bundleVersionProvider === "function"
|
|
45
|
+
? opts.bundleVersionProvider
|
|
46
|
+
: null
|
|
47
|
+
this.bundleManifestPath = opts.bundleManifestPath || null
|
|
48
|
+
this.bundleWatcher = null
|
|
49
|
+
this.bundleWatchDebounceTimer = null
|
|
31
50
|
this.hostname = opts.hostname || os.hostname()
|
|
32
51
|
this.ws = null
|
|
33
52
|
this.heartbeatTimer = null
|
|
@@ -52,6 +71,8 @@ export class WsClient extends EventEmitter {
|
|
|
52
71
|
ws.on("open", () => {
|
|
53
72
|
this.backoff = MIN_BACKOFF_MS
|
|
54
73
|
this.logger?.info("ws open")
|
|
74
|
+
// hello 前に最新の bundle version を読み直す (再接続時の鮮度確保)
|
|
75
|
+
this._refreshBundleVersion()
|
|
55
76
|
this._sendJson({
|
|
56
77
|
type: "hello",
|
|
57
78
|
agent_id: this.config.agent_id,
|
|
@@ -60,6 +81,7 @@ export class WsClient extends EventEmitter {
|
|
|
60
81
|
bundle_version: this.bundleVersion,
|
|
61
82
|
})
|
|
62
83
|
this._startHeartbeat()
|
|
84
|
+
this._startBundleWatcher()
|
|
63
85
|
this.emit("open")
|
|
64
86
|
})
|
|
65
87
|
|
|
@@ -76,6 +98,7 @@ export class WsClient extends EventEmitter {
|
|
|
76
98
|
|
|
77
99
|
ws.on("close", (code, reason) => {
|
|
78
100
|
this._stopHeartbeat()
|
|
101
|
+
this._stopBundleWatcher()
|
|
79
102
|
this.logger?.info({ code, reason: reason?.toString() }, "ws close")
|
|
80
103
|
this.emit("close", { code, reason })
|
|
81
104
|
if (!this.stopped) this._scheduleReconnect()
|
|
@@ -101,6 +124,7 @@ export class WsClient extends EventEmitter {
|
|
|
101
124
|
stop() {
|
|
102
125
|
this.stopped = true
|
|
103
126
|
this._stopHeartbeat()
|
|
127
|
+
this._stopBundleWatcher()
|
|
104
128
|
if (this.reconnectTimer) {
|
|
105
129
|
clearTimeout(this.reconnectTimer)
|
|
106
130
|
this.reconnectTimer = null
|
|
@@ -138,6 +162,9 @@ export class WsClient extends EventEmitter {
|
|
|
138
162
|
_startHeartbeat() {
|
|
139
163
|
this._stopHeartbeat()
|
|
140
164
|
this.heartbeatTimer = setInterval(() => {
|
|
165
|
+
// heartbeat 都度 provider 経由で bundle version を最新化する。
|
|
166
|
+
// fs.watch を取り逃した変更 (atomic rename / 別マウント越し等) への保険。
|
|
167
|
+
this._refreshBundleVersion()
|
|
141
168
|
// 送信失敗 (= ws not OPEN) を検知したら即時 close + reconnect 発火。
|
|
142
169
|
// setInterval を放置すると死んだコネクションに heartbeat を投げ続けて
|
|
143
170
|
// 30s 間気付かないので、close を強制トリガーする。
|
|
@@ -162,6 +189,99 @@ export class WsClient extends EventEmitter {
|
|
|
162
189
|
}
|
|
163
190
|
}
|
|
164
191
|
|
|
192
|
+
/**
|
|
193
|
+
* bundleVersionProvider を呼んで `this.bundleVersion` を最新化する。
|
|
194
|
+
* 値が変わったら true を返す。provider が未設定 / 失敗時は false。
|
|
195
|
+
*/
|
|
196
|
+
_refreshBundleVersion() {
|
|
197
|
+
if (!this.bundleVersionProvider) return false
|
|
198
|
+
try {
|
|
199
|
+
const next = this.bundleVersionProvider()
|
|
200
|
+
const normalized = typeof next === "string" && next.length > 0 ? next : null
|
|
201
|
+
if (normalized === this.bundleVersion) return false
|
|
202
|
+
this.logger?.info(
|
|
203
|
+
{ from: this.bundleVersion, to: normalized },
|
|
204
|
+
"bundle version refreshed",
|
|
205
|
+
)
|
|
206
|
+
this.bundleVersion = normalized
|
|
207
|
+
return true
|
|
208
|
+
} catch (err) {
|
|
209
|
+
this.logger?.warn({ err: err.message }, "bundle version refresh failed")
|
|
210
|
+
return false
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* `bundleManifestPath` を fs.watch で監視し、変更時に即時 heartbeat を送る。
|
|
216
|
+
* Cockpit UI への反映ラグを 30s heartbeat 周期から ~15s (UI poll) に短縮する。
|
|
217
|
+
* fs.watch の起動に失敗しても heartbeat 都度 refresh が保険として動くので no-op。
|
|
218
|
+
*/
|
|
219
|
+
_startBundleWatcher() {
|
|
220
|
+
if (!this.bundleManifestPath || this.bundleWatcher) return
|
|
221
|
+
try {
|
|
222
|
+
this.bundleWatcher = fs.watch(
|
|
223
|
+
this.bundleManifestPath,
|
|
224
|
+
{ persistent: false },
|
|
225
|
+
() => this._onBundleManifestChange(),
|
|
226
|
+
)
|
|
227
|
+
this.bundleWatcher.on("error", (err) => {
|
|
228
|
+
this.logger?.warn(
|
|
229
|
+
{ err: err.message, path: this.bundleManifestPath },
|
|
230
|
+
"bundle manifest watcher errored",
|
|
231
|
+
)
|
|
232
|
+
})
|
|
233
|
+
this.logger?.debug(
|
|
234
|
+
{ path: this.bundleManifestPath },
|
|
235
|
+
"bundle manifest watcher started",
|
|
236
|
+
)
|
|
237
|
+
} catch (err) {
|
|
238
|
+
this.logger?.warn(
|
|
239
|
+
{ err: err.message, path: this.bundleManifestPath },
|
|
240
|
+
"bundle manifest watcher failed to start",
|
|
241
|
+
)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
_stopBundleWatcher() {
|
|
246
|
+
if (this.bundleWatchDebounceTimer) {
|
|
247
|
+
clearTimeout(this.bundleWatchDebounceTimer)
|
|
248
|
+
this.bundleWatchDebounceTimer = null
|
|
249
|
+
}
|
|
250
|
+
if (this.bundleWatcher) {
|
|
251
|
+
try {
|
|
252
|
+
this.bundleWatcher.close()
|
|
253
|
+
} catch {
|
|
254
|
+
/* ignore */
|
|
255
|
+
}
|
|
256
|
+
this.bundleWatcher = null
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* fs.watch のコールバック。同一書き込みで多重 fire することがある (macOS) ため
|
|
262
|
+
* デバウンスしてから refresh + 即時 heartbeat する。
|
|
263
|
+
*/
|
|
264
|
+
_onBundleManifestChange() {
|
|
265
|
+
if (this.bundleWatchDebounceTimer) {
|
|
266
|
+
clearTimeout(this.bundleWatchDebounceTimer)
|
|
267
|
+
}
|
|
268
|
+
this.bundleWatchDebounceTimer = setTimeout(() => {
|
|
269
|
+
this.bundleWatchDebounceTimer = null
|
|
270
|
+
const changed = this._refreshBundleVersion()
|
|
271
|
+
if (!changed) return
|
|
272
|
+
const ok = this._sendJson({
|
|
273
|
+
type: "heartbeat",
|
|
274
|
+
uptime_sec: Math.floor((Date.now() - this.startedAt) / 1000),
|
|
275
|
+
bundle_version: this.bundleVersion,
|
|
276
|
+
})
|
|
277
|
+
if (!ok) {
|
|
278
|
+
this.logger?.warn("bundle-change heartbeat send failed, forcing reconnect")
|
|
279
|
+
this._forceReconnect()
|
|
280
|
+
}
|
|
281
|
+
}, BUNDLE_WATCH_DEBOUNCE_MS)
|
|
282
|
+
this.bundleWatchDebounceTimer.unref?.()
|
|
283
|
+
}
|
|
284
|
+
|
|
165
285
|
_forceReconnect() {
|
|
166
286
|
// 現在の ws を強制 close。close ハンドラが _scheduleReconnect を呼ぶので
|
|
167
287
|
// それ以上の処理は不要。stopped 中は no-op。
|