hermes-pilot 0.0.0-stage → 0.2.0

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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja.md +390 -0
  3. package/README.md +398 -2
  4. package/__init__.py +15 -0
  5. package/apps/bridge/src/api-errors.js +165 -0
  6. package/apps/bridge/src/architect.js +1298 -0
  7. package/apps/bridge/src/auth.js +202 -0
  8. package/apps/bridge/src/capabilities.js +256 -0
  9. package/apps/bridge/src/capture.js +145 -0
  10. package/apps/bridge/src/chat-profile.js +95 -0
  11. package/apps/bridge/src/config.js +73 -0
  12. package/apps/bridge/src/discord.js +89 -0
  13. package/apps/bridge/src/gitinfo.js +211 -0
  14. package/apps/bridge/src/hermes.js +97 -0
  15. package/apps/bridge/src/inbox.js +347 -0
  16. package/apps/bridge/src/kanban.js +361 -0
  17. package/apps/bridge/src/lang.js +60 -0
  18. package/apps/bridge/src/live.js +285 -0
  19. package/apps/bridge/src/memos.js +150 -0
  20. package/apps/bridge/src/ops.js +201 -0
  21. package/apps/bridge/src/pair-codes.js +125 -0
  22. package/apps/bridge/src/pipeline-optimizer.js +358 -0
  23. package/apps/bridge/src/pipeline-watcher.js +1018 -0
  24. package/apps/bridge/src/qr.js +808 -0
  25. package/apps/bridge/src/repos.js +544 -0
  26. package/apps/bridge/src/review-watcher.js +271 -0
  27. package/apps/bridge/src/review.js +1749 -0
  28. package/apps/bridge/src/search.js +162 -0
  29. package/apps/bridge/src/server.js +1738 -0
  30. package/apps/bridge/src/snippets.js +373 -0
  31. package/apps/bridge/src/specs.js +734 -0
  32. package/apps/bridge/src/tail.js +28 -0
  33. package/apps/bridge/src/telemetry.js +667 -0
  34. package/apps/bridge/src/term-snippets.js +228 -0
  35. package/apps/bridge/src/transcript.js +231 -0
  36. package/apps/bridge/src/usage.js +294 -0
  37. package/apps/bridge/src/workerFeed.js +72 -0
  38. package/apps/dashboard/index.js +2014 -0
  39. package/apps/dashboard/manifest.json +14 -0
  40. package/apps/dashboard/pilot_dashboard.py +1466 -0
  41. package/apps/dashboard/plugin_api.py +237 -0
  42. package/apps/dashboard/plugin_init.py +15 -0
  43. package/apps/dashboard/style.css +928 -0
  44. package/apps/web/dist/assets/TermPane-DYP7pi_n.css +32 -0
  45. package/apps/web/dist/assets/TermPane-U500JD2P.js +12 -0
  46. package/apps/web/dist/assets/index-6iFDsB0G.css +1 -0
  47. package/apps/web/dist/assets/index-X4Bi2FWB.js +94 -0
  48. package/apps/web/dist/favicon.ico +0 -0
  49. package/apps/web/dist/icon-180-v6.png +0 -0
  50. package/apps/web/dist/icon-192-v6.png +0 -0
  51. package/apps/web/dist/icon-512-v6.png +0 -0
  52. package/apps/web/dist/icon-maskable-192-v6.png +0 -0
  53. package/apps/web/dist/icon-maskable-512-v6.png +0 -0
  54. package/apps/web/dist/icon-v6.svg +15 -0
  55. package/apps/web/dist/index.html +21 -0
  56. package/apps/web/dist/manifest.webmanifest +49 -0
  57. package/apps/web/dist/sw.js +69 -0
  58. package/bin/hermes-pilot.mjs +964 -0
  59. package/dashboard/dist/index.js +2014 -0
  60. package/dashboard/dist/style.css +928 -0
  61. package/dashboard/manifest.json +14 -0
  62. package/dashboard/pilot_dashboard.py +1466 -0
  63. package/dashboard/plugin_api.py +237 -0
  64. package/package.json +75 -4
  65. package/plugin.json +70 -0
  66. package/plugin.yaml +4 -0
  67. package/scripts/deploy.mjs +58 -0
  68. package/scripts/migrate-to-hermes-pilot.mjs +472 -0
  69. package/scripts/postinstall.mjs +32 -0
  70. package/scripts/sync-dashboard.mjs +98 -0
  71. package/skills/annotation/SKILL.md +103 -0
  72. package/skills/quick-review/SKILL.md +170 -0
  73. package/skills/quick-review/assets/sheet-template.html +175 -0
  74. package/skills/spec-architect/SKILL.md +175 -0
@@ -0,0 +1,162 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { DatabaseSync } from 'node:sqlite'
4
+
5
+ /**
6
+ * 全セッションの横断検索。
7
+ *
8
+ * `~/.hermes/state.db` の `messages` を **読み取り専用** で開き、
9
+ * FTS5 の `messages_fts_trigram`(日本語の部分一致が効く)で引く。
10
+ * トリグラム索引は 3 文字未満の語を拾えないので、その場合は LIKE に落とす。
11
+ *
12
+ * 返すのは**抜粋だけ**(本文全体は返さない)。会話を開くのはクライアントの役目。
13
+ */
14
+
15
+ const MAX_LIMIT = 50
16
+ const SNIPPET_PAD = 60
17
+
18
+ /**
19
+ * FTS5 のクエリ文字列を組み立てる。
20
+ * 演算子(AND/OR/NOT/NEAR)や `-` `*` `(` を語として扱うため、全体をフレーズ引用する。
21
+ * @param {string} raw 入力
22
+ * @returns {string} MATCH に渡す文字列
23
+ */
24
+ export function buildMatch(raw) {
25
+ const q = String(raw ?? '')
26
+ .replace(/[\u0000-\u001f]/g, ' ')
27
+ .trim()
28
+ if (!q) return ''
29
+ // 二重引用符は FTS のフレーズ区切りなのでエスケープ("" にする)
30
+ return `"${q.replace(/"/g, '""')}"`
31
+ }
32
+
33
+ /**
34
+ * 抜粋を作る(ヒット位置の前後を切り出す)。
35
+ * @param {string} text 本文
36
+ * @param {string} q 検索語
37
+ * @param {number} [pad] 前後の文字数
38
+ * @returns {string} 抜粋
39
+ */
40
+ export function makeSnippet(text, q, pad = SNIPPET_PAD) {
41
+ const body = String(text ?? '').replace(/\s+/g, ' ').trim()
42
+ if (!body) return ''
43
+ const needle = String(q ?? '').trim()
44
+ const at = needle ? body.toLowerCase().indexOf(needle.toLowerCase()) : -1
45
+ if (at < 0) return body.slice(0, pad * 2) + (body.length > pad * 2 ? '…' : '')
46
+ const start = Math.max(0, at - pad)
47
+ const end = Math.min(body.length, at + needle.length + pad)
48
+ return `${start > 0 ? '…' : ''}${body.slice(start, end)}${end < body.length ? '…' : ''}`
49
+ }
50
+
51
+ /**
52
+ * 検索を作る。
53
+ * @param {object} cfg 設定(home / stateDb)
54
+ * @returns {object} 検索 API
55
+ */
56
+ export function createSearch(cfg) {
57
+ const dbPath = cfg.stateDb ?? path.join(cfg.home ?? '', '.hermes', 'state.db')
58
+
59
+ /** 3文字以上なら trigram、短ければ LIKE(trigram は 2 文字を索引できない)。 */
60
+ function useTrigram(q) {
61
+ return [...q].length >= 3
62
+ }
63
+
64
+ /**
65
+ * 1つの DB から検索する。
66
+ * @param {{profile: string, path: string}} target DB情報
67
+ * @param {string} q 検索語
68
+ * @param {number} since 開始秒
69
+ * @param {number} limit 件数上限
70
+ * @returns {Array<object>} ヒット一覧
71
+ */
72
+ function searchOne(target, q, since, limit) {
73
+ let db
74
+ try {
75
+ db = new DatabaseSync(target.path, { readOnly: true })
76
+ const rows = useTrigram(q)
77
+ ? db
78
+ .prepare(
79
+ `SELECT m.id, m.session_id, m.role, m.content, m.timestamp,
80
+ s.title, s.source, s.chat_type, s.display_name
81
+ FROM messages_fts_trigram f
82
+ JOIN messages m ON m.id = f.rowid
83
+ LEFT JOIN sessions s ON s.id = m.session_id
84
+ WHERE messages_fts_trigram MATCH ?
85
+ AND m.timestamp >= ?
86
+ ORDER BY m.timestamp DESC
87
+ LIMIT ?`,
88
+ )
89
+ .all(buildMatch(q), since, limit)
90
+ : db
91
+ .prepare(
92
+ `SELECT m.id, m.session_id, m.role, m.content, m.timestamp,
93
+ s.title, s.source, s.chat_type, s.display_name
94
+ FROM messages m
95
+ LEFT JOIN sessions s ON s.id = m.session_id
96
+ WHERE m.content LIKE ?
97
+ AND m.timestamp >= ?
98
+ ORDER BY m.timestamp DESC
99
+ LIMIT ?`,
100
+ )
101
+ .all(`%${q}%`, since, limit)
102
+ return rows.map((r) => ({
103
+ profile: target.profile || 'default',
104
+ messageId: Number(r.id),
105
+ sessionId: r.session_id,
106
+ sessionTitle: r.title || r.display_name || r.session_id,
107
+ source: r.source ?? '',
108
+ chatType: r.chat_type ?? '',
109
+ role: r.role,
110
+ snippet: makeSnippet(r.content, q),
111
+ at: Math.round(Number(r.timestamp ?? 0) * 1000),
112
+ }))
113
+ } finally {
114
+ try {
115
+ db?.close()
116
+ } catch {
117
+ /* noop */
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * 検索する。
124
+ * @param {{q: string, limit?: number, hours?: number, dbs?: Array<{profile: string, path: string}>}} opts 条件
125
+ * @returns {{ok: boolean, q: string, tookMs: number, total: number, hits: Array<object>, reason?: string}} 結果
126
+ */
127
+ function search(opts = {}) {
128
+ const started = process.hrtime.bigint()
129
+ const q = String(opts.q ?? '').trim().slice(0, 100)
130
+ const limit = Math.min(Math.max(Number(opts.limit ?? 30), 1), MAX_LIMIT)
131
+ const hours = Number(opts.hours ?? 0)
132
+ const out = { ok: true, q, tookMs: 0, total: 0, hits: [] }
133
+ if (!q) return { ...out, ok: false, reason: '検索語がありません' }
134
+
135
+ const rawDbs = Array.isArray(opts.dbs) && opts.dbs.length ? opts.dbs : [{ profile: 'default', path: dbPath }]
136
+ const targets = rawDbs.filter((d) => d?.path && fs.existsSync(d.path))
137
+ if (!targets.length) return { ...out, ok: false, reason: 'state.db が見つかりません' }
138
+
139
+ const since = hours > 0 ? Date.now() / 1000 - hours * 3600 : 0
140
+ const merged = []
141
+ let firstErr = null
142
+ let succeeded = 0
143
+ for (const target of targets) {
144
+ try {
145
+ merged.push(...searchOne(target, q, since, limit))
146
+ succeeded++
147
+ } catch (e) {
148
+ if (!firstErr) firstErr = e
149
+ }
150
+ }
151
+ if (!succeeded && firstErr) {
152
+ return { ...out, ok: false, reason: `検索に失敗しました: ${firstErr.message}` }
153
+ }
154
+ merged.sort((a, b) => b.at - a.at)
155
+ out.hits = merged.slice(0, limit)
156
+ out.total = out.hits.length
157
+ out.tookMs = Number(process.hrtime.bigint() - started) / 1e6
158
+ return out
159
+ }
160
+
161
+ return { search, buildMatch, makeSnippet }
162
+ }