@cocorograph/hub-agent 0.5.4 → 0.5.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocorograph/hub-agent",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Hub Hosted Cockpit のローカル常駐 agent。Hub と outbound WSS で接続し、ローカルの tmux/pty を中継する。",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
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
@@ -20,10 +20,12 @@ import { readConfig } from "./config.mjs"
20
20
  import { loadPlugins, runHookBroadcast, runHookChain } from "./plugin-loader.mjs"
21
21
  import { WsClient } from "./ws-client.mjs"
22
22
  import { PtyBridge } from "./pty-bridge.mjs"
23
+ import { listAgents } from "./agents.mjs"
23
24
  import { listSkills } from "./skills.mjs"
24
25
  import { listSessionStates } from "./state.mjs"
25
26
  import {
26
27
  createSession as createTmuxSession,
28
+ createWorktreeDir,
27
29
  execTmux,
28
30
  killManySessions,
29
31
  killSession as killTmuxSession,
@@ -313,6 +315,53 @@ async function dispatch(msg, ctx) {
313
315
  }
314
316
  return
315
317
  }
318
+ case "worktree.create": {
319
+ // body: { request_id, dir, branch, claude_cmd? }
320
+ const dir = (msg.dir || "").trim()
321
+ const branch = (msg.branch || "").trim()
322
+ if (!dir || !branch) {
323
+ ctx.client.send({
324
+ type: "worktree.create.result",
325
+ request_id: msg.request_id,
326
+ ok: false,
327
+ error: "dir and branch required",
328
+ })
329
+ return
330
+ }
331
+ if (!/^[A-Za-z0-9._\-/]+$/.test(branch) || branch.startsWith("-")) {
332
+ ctx.client.send({
333
+ type: "worktree.create.result",
334
+ request_id: msg.request_id,
335
+ ok: false,
336
+ error: "invalid branch name",
337
+ })
338
+ return
339
+ }
340
+ try {
341
+ const { wtName, wtPath, createdBranch } = await createWorktreeDir(dir, branch)
342
+ // worktree dir で tmux session を作成 (claude_cmd は createSession の default)
343
+ await createTmuxSession(wtName, wtPath, {
344
+ claudeCmd: typeof msg.claude_cmd === "string" ? msg.claude_cmd : undefined,
345
+ })
346
+ ctx.client.send({
347
+ type: "worktree.create.result",
348
+ request_id: msg.request_id,
349
+ ok: true,
350
+ wt_name: wtName,
351
+ wt_path: wtPath,
352
+ branch,
353
+ created_branch: createdBranch,
354
+ })
355
+ } catch (err) {
356
+ ctx.client.send({
357
+ type: "worktree.create.result",
358
+ request_id: msg.request_id,
359
+ ok: false,
360
+ error: err.message,
361
+ })
362
+ }
363
+ return
364
+ }
316
365
  case "tmux.kill_session": {
317
366
  const names = Array.isArray(msg.session_names)
318
367
  ? msg.session_names
@@ -357,6 +406,24 @@ async function dispatch(msg, ctx) {
357
406
  }
358
407
  return
359
408
  }
409
+ case "agents.request": {
410
+ try {
411
+ const agents = await listAgents()
412
+ ctx.client.send({
413
+ type: "agents.response",
414
+ request_id: msg.request_id,
415
+ agents,
416
+ })
417
+ } catch (err) {
418
+ ctx.client.send({
419
+ type: "agents.response",
420
+ request_id: msg.request_id,
421
+ agents: [],
422
+ error: err.message,
423
+ })
424
+ }
425
+ return
426
+ }
360
427
  case "usage.request": {
361
428
  try {
362
429
  const [usage, sessions] = await Promise.all([
package/src/tmux.mjs CHANGED
@@ -33,6 +33,72 @@ function sanitizeTmuxName(s) {
33
33
  return s.replace(/[.:]/g, "-")
34
34
  }
35
35
 
36
+ /**
37
+ * git branch 名から worktree dir 名 (= tmux session 名) を導出する。
38
+ * 例: "feature/multi-area" -> "feature-multi-area"
39
+ * "fix.typo:v2" -> "fix-typo-v2"
40
+ *
41
+ * 移植元: D00000_cockpit/webapp/lib/worktrees.ts (sanitizeBranchToWtName)
42
+ */
43
+ export function sanitizeBranchToWtName(branch) {
44
+ return branch
45
+ .replace(/[\s/:.]+/g, "-")
46
+ .replace(/[^A-Za-z0-9_-]/g, "")
47
+ .replace(/^-+|-+$/g, "")
48
+ }
49
+
50
+ async function branchExists(repoDir, branch) {
51
+ try {
52
+ await execFileP("git", [
53
+ "-C",
54
+ repoDir,
55
+ "show-ref",
56
+ "--verify",
57
+ "--quiet",
58
+ `refs/heads/${branch}`,
59
+ ])
60
+ return true
61
+ } catch {
62
+ return false
63
+ }
64
+ }
65
+
66
+ /**
67
+ * `<repoDir>/.claude/worktrees/<wtName>` に worktree を作成する。
68
+ * branch が既存ならそれを checkout、無ければ `-b` で新規作成。
69
+ *
70
+ * 移植元: D00000_cockpit/webapp/lib/worktrees.ts (createWorktree)
71
+ */
72
+ export async function createWorktreeDir(parentDir, branch) {
73
+ const repoDir = path.resolve(HUB_PROJECTS_BASE, parentDir)
74
+ if (repoDir !== HUB_PROJECTS_BASE && !repoDir.startsWith(HUB_PROJECTS_BASE + path.sep)) {
75
+ throw new Error("dir outside projects base")
76
+ }
77
+ // .git の存在チェック (file or dir)
78
+ try {
79
+ await fs.stat(path.join(repoDir, ".git"))
80
+ } catch {
81
+ throw new Error("not a git repository")
82
+ }
83
+ const wtName = sanitizeBranchToWtName(branch)
84
+ if (!wtName) throw new Error("invalid branch name")
85
+ const wtPath = path.join(repoDir, ".claude", "worktrees", wtName)
86
+ try {
87
+ await fs.stat(wtPath)
88
+ throw new Error("worktree already exists")
89
+ } catch (err) {
90
+ if (err.message === "worktree already exists") throw err
91
+ // ENOENT は期待動作
92
+ }
93
+ const exists = await branchExists(repoDir, branch)
94
+ if (exists) {
95
+ await execFileP("git", ["-C", repoDir, "worktree", "add", wtPath, branch])
96
+ } else {
97
+ await execFileP("git", ["-C", repoDir, "worktree", "add", "-b", branch, wtPath])
98
+ }
99
+ return { wtName, wtPath, createdBranch: !exists }
100
+ }
101
+
36
102
  /**
37
103
  * `~/hub/projects/<project>/.claude/worktrees/<wt>` を走査し、
38
104
  * worktree_session_name → parent_session_name の Map を返す。