@johpaz/hive 1.1.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 (156) hide show
  1. package/CONTRIBUTING.md +44 -0
  2. package/README.md +310 -0
  3. package/package.json +96 -0
  4. package/packages/cli/package.json +28 -0
  5. package/packages/cli/src/commands/agent-run.ts +168 -0
  6. package/packages/cli/src/commands/agents.ts +398 -0
  7. package/packages/cli/src/commands/chat.ts +142 -0
  8. package/packages/cli/src/commands/config.ts +50 -0
  9. package/packages/cli/src/commands/cron.ts +161 -0
  10. package/packages/cli/src/commands/dev.ts +95 -0
  11. package/packages/cli/src/commands/doctor.ts +133 -0
  12. package/packages/cli/src/commands/gateway.ts +443 -0
  13. package/packages/cli/src/commands/logs.ts +57 -0
  14. package/packages/cli/src/commands/mcp.ts +175 -0
  15. package/packages/cli/src/commands/message.ts +77 -0
  16. package/packages/cli/src/commands/onboard.ts +1868 -0
  17. package/packages/cli/src/commands/security.ts +144 -0
  18. package/packages/cli/src/commands/service.ts +50 -0
  19. package/packages/cli/src/commands/sessions.ts +116 -0
  20. package/packages/cli/src/commands/skills.ts +187 -0
  21. package/packages/cli/src/commands/update.ts +25 -0
  22. package/packages/cli/src/index.ts +185 -0
  23. package/packages/cli/src/utils/token.ts +6 -0
  24. package/packages/code-bridge/README.md +78 -0
  25. package/packages/code-bridge/package.json +18 -0
  26. package/packages/code-bridge/src/index.ts +95 -0
  27. package/packages/code-bridge/src/process-manager.ts +212 -0
  28. package/packages/code-bridge/src/schemas.ts +133 -0
  29. package/packages/core/package.json +46 -0
  30. package/packages/core/src/agent/agent-loop.ts +369 -0
  31. package/packages/core/src/agent/compaction.ts +140 -0
  32. package/packages/core/src/agent/context-compiler.ts +378 -0
  33. package/packages/core/src/agent/context-guard.ts +91 -0
  34. package/packages/core/src/agent/context.ts +138 -0
  35. package/packages/core/src/agent/conversation-store.ts +198 -0
  36. package/packages/core/src/agent/curator.ts +158 -0
  37. package/packages/core/src/agent/hooks.ts +166 -0
  38. package/packages/core/src/agent/index.ts +116 -0
  39. package/packages/core/src/agent/llm-client.ts +503 -0
  40. package/packages/core/src/agent/native-tools.ts +505 -0
  41. package/packages/core/src/agent/prompt-builder.ts +532 -0
  42. package/packages/core/src/agent/providers/index.ts +167 -0
  43. package/packages/core/src/agent/providers.ts +1 -0
  44. package/packages/core/src/agent/reflector.ts +170 -0
  45. package/packages/core/src/agent/service.ts +64 -0
  46. package/packages/core/src/agent/stuck-loop.ts +133 -0
  47. package/packages/core/src/agent/supervisor.ts +39 -0
  48. package/packages/core/src/agent/tracer.ts +102 -0
  49. package/packages/core/src/agent/workspace.ts +110 -0
  50. package/packages/core/src/canvas/canvas-manager.test.ts +161 -0
  51. package/packages/core/src/canvas/canvas-manager.ts +319 -0
  52. package/packages/core/src/canvas/canvas-tools.ts +420 -0
  53. package/packages/core/src/canvas/emitter.ts +115 -0
  54. package/packages/core/src/canvas/index.ts +2 -0
  55. package/packages/core/src/channels/base.ts +138 -0
  56. package/packages/core/src/channels/discord.ts +260 -0
  57. package/packages/core/src/channels/index.ts +7 -0
  58. package/packages/core/src/channels/manager.ts +383 -0
  59. package/packages/core/src/channels/slack.ts +287 -0
  60. package/packages/core/src/channels/telegram.ts +502 -0
  61. package/packages/core/src/channels/webchat.ts +128 -0
  62. package/packages/core/src/channels/whatsapp.ts +375 -0
  63. package/packages/core/src/config/index.ts +12 -0
  64. package/packages/core/src/config/loader.ts +529 -0
  65. package/packages/core/src/events/event-bus.ts +169 -0
  66. package/packages/core/src/gateway/index.ts +5 -0
  67. package/packages/core/src/gateway/initializer.ts +290 -0
  68. package/packages/core/src/gateway/lane-queue.ts +169 -0
  69. package/packages/core/src/gateway/resolver.ts +108 -0
  70. package/packages/core/src/gateway/router.ts +124 -0
  71. package/packages/core/src/gateway/server.ts +3317 -0
  72. package/packages/core/src/gateway/session.ts +95 -0
  73. package/packages/core/src/gateway/slash-commands.ts +192 -0
  74. package/packages/core/src/heartbeat/index.ts +157 -0
  75. package/packages/core/src/index.ts +19 -0
  76. package/packages/core/src/integrations/catalog.ts +286 -0
  77. package/packages/core/src/integrations/env.ts +64 -0
  78. package/packages/core/src/integrations/index.ts +2 -0
  79. package/packages/core/src/memory/index.ts +1 -0
  80. package/packages/core/src/memory/notes.ts +68 -0
  81. package/packages/core/src/plugins/api.ts +128 -0
  82. package/packages/core/src/plugins/index.ts +2 -0
  83. package/packages/core/src/plugins/loader.ts +365 -0
  84. package/packages/core/src/resilience/circuit-breaker.ts +225 -0
  85. package/packages/core/src/security/google-chat.ts +269 -0
  86. package/packages/core/src/security/index.ts +192 -0
  87. package/packages/core/src/security/pairing.ts +250 -0
  88. package/packages/core/src/security/rate-limit.ts +270 -0
  89. package/packages/core/src/security/signal.ts +321 -0
  90. package/packages/core/src/state/store.ts +312 -0
  91. package/packages/core/src/storage/bun-sqlite-store.ts +188 -0
  92. package/packages/core/src/storage/crypto.ts +101 -0
  93. package/packages/core/src/storage/db-context.ts +333 -0
  94. package/packages/core/src/storage/onboarding.ts +1087 -0
  95. package/packages/core/src/storage/schema.ts +541 -0
  96. package/packages/core/src/storage/seed.ts +571 -0
  97. package/packages/core/src/storage/sqlite.ts +387 -0
  98. package/packages/core/src/storage/usage.ts +212 -0
  99. package/packages/core/src/tools/bridge-events.ts +74 -0
  100. package/packages/core/src/tools/browser.ts +275 -0
  101. package/packages/core/src/tools/codebridge.ts +421 -0
  102. package/packages/core/src/tools/coordinator-tools.ts +179 -0
  103. package/packages/core/src/tools/cron.ts +611 -0
  104. package/packages/core/src/tools/exec.ts +140 -0
  105. package/packages/core/src/tools/fs.ts +364 -0
  106. package/packages/core/src/tools/index.ts +12 -0
  107. package/packages/core/src/tools/memory.ts +176 -0
  108. package/packages/core/src/tools/notify.ts +113 -0
  109. package/packages/core/src/tools/project-management.ts +376 -0
  110. package/packages/core/src/tools/project.ts +375 -0
  111. package/packages/core/src/tools/read.ts +158 -0
  112. package/packages/core/src/tools/web.ts +436 -0
  113. package/packages/core/src/tools/workspace.ts +171 -0
  114. package/packages/core/src/utils/benchmark.ts +80 -0
  115. package/packages/core/src/utils/crypto.ts +73 -0
  116. package/packages/core/src/utils/date.ts +42 -0
  117. package/packages/core/src/utils/index.ts +4 -0
  118. package/packages/core/src/utils/logger.ts +388 -0
  119. package/packages/core/src/utils/retry.ts +70 -0
  120. package/packages/core/src/voice/index.ts +583 -0
  121. package/packages/core/tsconfig.json +9 -0
  122. package/packages/mcp/package.json +26 -0
  123. package/packages/mcp/src/config.ts +13 -0
  124. package/packages/mcp/src/index.ts +1 -0
  125. package/packages/mcp/src/logger.ts +42 -0
  126. package/packages/mcp/src/manager.ts +434 -0
  127. package/packages/mcp/src/transports/index.ts +67 -0
  128. package/packages/mcp/src/transports/sse.ts +241 -0
  129. package/packages/mcp/src/transports/websocket.ts +159 -0
  130. package/packages/skills/package.json +21 -0
  131. package/packages/skills/src/bundled/agent_management/SKILL.md +24 -0
  132. package/packages/skills/src/bundled/browser_automation/SKILL.md +30 -0
  133. package/packages/skills/src/bundled/context_compact/SKILL.md +35 -0
  134. package/packages/skills/src/bundled/cron_manager/SKILL.md +52 -0
  135. package/packages/skills/src/bundled/file_manager/SKILL.md +76 -0
  136. package/packages/skills/src/bundled/http_client/SKILL.md +24 -0
  137. package/packages/skills/src/bundled/memory/SKILL.md +42 -0
  138. package/packages/skills/src/bundled/project_management/SKILL.md +26 -0
  139. package/packages/skills/src/bundled/shell/SKILL.md +43 -0
  140. package/packages/skills/src/bundled/system_notify/SKILL.md +52 -0
  141. package/packages/skills/src/bundled/voice/SKILL.md +25 -0
  142. package/packages/skills/src/bundled/web_search/SKILL.md +29 -0
  143. package/packages/skills/src/index.ts +1 -0
  144. package/packages/skills/src/loader.ts +282 -0
  145. package/packages/tools/package.json +43 -0
  146. package/packages/tools/src/browser/browser.test.ts +111 -0
  147. package/packages/tools/src/browser/index.ts +272 -0
  148. package/packages/tools/src/canvas/index.ts +220 -0
  149. package/packages/tools/src/cron/cron.test.ts +164 -0
  150. package/packages/tools/src/cron/index.ts +304 -0
  151. package/packages/tools/src/filesystem/filesystem.test.ts +240 -0
  152. package/packages/tools/src/filesystem/index.ts +379 -0
  153. package/packages/tools/src/git/index.ts +239 -0
  154. package/packages/tools/src/index.ts +4 -0
  155. package/packages/tools/src/shell/detect-env.ts +70 -0
  156. package/packages/tools/tsconfig.json +9 -0
@@ -0,0 +1,198 @@
1
+ /**
2
+ * Conversation Store — persists message history in the `conversations` table.
3
+ * Replaces the LangGraph BunSqliteSaver + lg_checkpoints approach.
4
+ *
5
+ * Also manages: summaries, scratchpad.
6
+ */
7
+
8
+ import { getDb } from "../storage/sqlite"
9
+ import { logger } from "../utils/logger"
10
+ import type { LLMMessage } from "./llm-client"
11
+
12
+ const log = logger.child("conv-store")
13
+
14
+ // ─── Types ────────────────────────────────────────────────────────────────────
15
+
16
+ export interface StoredMessage {
17
+ id: number
18
+ thread_id: string
19
+ channel: string
20
+ role: "user" | "assistant" | "tool" | "system"
21
+ content: string
22
+ tool_calls_json: string | null
23
+ tool_call_id: string | null
24
+ token_count: number
25
+ created_at: number
26
+ }
27
+
28
+ // ─── Token estimation ─────────────────────────────────────────────────────────
29
+
30
+ export function estimateTokens(text: string): number {
31
+ return Math.ceil((text ?? "").length / 4)
32
+ }
33
+
34
+ // ─── Message operations ───────────────────────────────────────────────────────
35
+
36
+ export function addMessage(
37
+ threadId: string,
38
+ role: StoredMessage["role"],
39
+ content: string,
40
+ opts?: {
41
+ channel?: string
42
+ tool_calls?: LLMMessage["tool_calls"]
43
+ tool_call_id?: string
44
+ }
45
+ ): number {
46
+ const db = getDb()
47
+ const tool_calls_json = opts?.tool_calls?.length
48
+ ? JSON.stringify(opts.tool_calls)
49
+ : null
50
+
51
+ const result = db.query(`
52
+ INSERT INTO conversations (thread_id, channel, role, content, tool_calls_json, tool_call_id, token_count)
53
+ VALUES (?, ?, ?, ?, ?, ?, ?)
54
+ RETURNING id
55
+ `).get(
56
+ threadId,
57
+ opts?.channel ?? "webchat",
58
+ role,
59
+ content,
60
+ tool_calls_json,
61
+ opts?.tool_call_id ?? null,
62
+ estimateTokens(content) + estimateTokens(tool_calls_json ?? ""),
63
+ ) as { id: number }
64
+
65
+ return result.id
66
+ }
67
+
68
+ /**
69
+ * Returns all messages for the thread ordered oldest → newest.
70
+ */
71
+ export function getHistory(threadId: string, limit = 200): StoredMessage[] {
72
+ const db = getDb()
73
+ return db.query(`
74
+ SELECT * FROM conversations
75
+ WHERE thread_id = ?
76
+ ORDER BY id ASC
77
+ LIMIT ?
78
+ `).all(threadId, limit) as StoredMessage[]
79
+ }
80
+
81
+ /**
82
+ * Returns only the last N messages (oldest → newest order).
83
+ */
84
+ export function getRecentMessages(threadId: string, n: number): StoredMessage[] {
85
+ const db = getDb()
86
+ const rows = db.query(`
87
+ SELECT * FROM conversations
88
+ WHERE thread_id = ?
89
+ ORDER BY id DESC
90
+ LIMIT ?
91
+ `).all(threadId, n) as StoredMessage[]
92
+ return rows.reverse()
93
+ }
94
+
95
+ export function getMessageCount(threadId: string): number {
96
+ const db = getDb()
97
+ const row = db.query(
98
+ "SELECT COUNT(*) as cnt FROM conversations WHERE thread_id = ?"
99
+ ).get(threadId) as { cnt: number }
100
+ return row.cnt
101
+ }
102
+
103
+ export function getTotalTokens(threadId: string): number {
104
+ const db = getDb()
105
+ const row = db.query(
106
+ "SELECT COALESCE(SUM(token_count), 0) as total FROM conversations WHERE thread_id = ?"
107
+ ).get(threadId) as { total: number }
108
+ return row.total
109
+ }
110
+
111
+ /**
112
+ * Messages after a given message ID (for incremental summary updates).
113
+ */
114
+ export function getMessagesAfter(threadId: string, afterId: number): StoredMessage[] {
115
+ const db = getDb()
116
+ return db.query(`
117
+ SELECT * FROM conversations
118
+ WHERE thread_id = ? AND id > ?
119
+ ORDER BY id ASC
120
+ `).all(threadId, afterId) as StoredMessage[]
121
+ }
122
+
123
+ // ─── Convert stored messages → LLMMessage array ───────────────────────────────
124
+
125
+ export function toAPIMessages(rows: StoredMessage[]): LLMMessage[] {
126
+ return rows.map((r) => {
127
+ const msg: LLMMessage = { role: r.role, content: r.content }
128
+ if (r.tool_calls_json) {
129
+ try { msg.tool_calls = JSON.parse(r.tool_calls_json) } catch { /* ignore */ }
130
+ }
131
+ if (r.tool_call_id) msg.tool_call_id = r.tool_call_id
132
+ return msg
133
+ })
134
+ }
135
+
136
+ // ─── Summaries ────────────────────────────────────────────────────────────────
137
+
138
+ export interface Summary {
139
+ summary: string
140
+ last_message_id: number
141
+ messages_covered: number
142
+ }
143
+
144
+ export function getSummary(threadId: string): Summary | null {
145
+ const db = getDb()
146
+ return db.query(
147
+ "SELECT summary, last_message_id, messages_covered FROM summaries WHERE thread_id = ?"
148
+ ).get(threadId) as Summary | null
149
+ }
150
+
151
+ export function saveSummary(
152
+ threadId: string,
153
+ summary: string,
154
+ messagesCovered: number,
155
+ lastMessageId: number
156
+ ): void {
157
+ const db = getDb()
158
+ db.query(`
159
+ INSERT INTO summaries (thread_id, summary, messages_covered, last_message_id)
160
+ VALUES (?, ?, ?, ?)
161
+ ON CONFLICT(thread_id) DO UPDATE SET
162
+ summary = excluded.summary,
163
+ messages_covered = excluded.messages_covered,
164
+ last_message_id = excluded.last_message_id,
165
+ updated_at = unixepoch()
166
+ `).run(threadId, summary, messagesCovered, lastMessageId)
167
+ }
168
+
169
+ // ─── Scratchpad ───────────────────────────────────────────────────────────────
170
+
171
+ export function saveScratchpadNote(
172
+ threadId: string,
173
+ key: string,
174
+ value: string,
175
+ source?: string
176
+ ): void {
177
+ const db = getDb()
178
+ db.query(`
179
+ INSERT INTO scratchpad (thread_id, key, value, source)
180
+ VALUES (?, ?, ?, ?)
181
+ ON CONFLICT(thread_id, key) DO UPDATE SET
182
+ value = excluded.value,
183
+ source = excluded.source,
184
+ updated_at = unixepoch()
185
+ `).run(threadId, key, value, source ?? null)
186
+ }
187
+
188
+ export function getScratchpad(threadId: string): Array<{ key: string; value: string }> {
189
+ const db = getDb()
190
+ return db.query(
191
+ "SELECT key, value FROM scratchpad WHERE thread_id = ? ORDER BY updated_at DESC"
192
+ ).all(threadId) as Array<{ key: string; value: string }>
193
+ }
194
+
195
+ export function deleteScratchpadNote(threadId: string, key: string): void {
196
+ const db = getDb()
197
+ db.query("DELETE FROM scratchpad WHERE thread_id = ? AND key = ?").run(threadId, key)
198
+ }
@@ -0,0 +1,158 @@
1
+ /**
2
+ * ACE Curator — converts reflections into playbook rules.
3
+ *
4
+ * Runs after the Reflector. Performs incremental edits to the playbook:
5
+ * - New insights → new rules
6
+ * - Repeated patterns → increment helpful_count
7
+ * - Contradicted rules → increment harmful_count or deactivate
8
+ * - Deactivate rules where harmful_count > helpful_count
9
+ * - Archive unused workers
10
+ *
11
+ * Never rewrites the whole playbook — only incremental edits.
12
+ */
13
+
14
+ import { logger } from "../utils/logger"
15
+
16
+ const log = logger.child("curator")
17
+
18
+ const DAYS_BEFORE_ARCHIVE = 14 // archive workers not used in N days
19
+ const MAX_HARMFUL_BEFORE_PRUNE = 3
20
+
21
+ /** Entry point — called by reflector.ts after it inserts new reflections */
22
+ export async function runCurator(): Promise<void> {
23
+ try {
24
+ const { getDb } = await import("../storage/sqlite")
25
+ const db = getDb()
26
+
27
+ // Process unprocessed reflections (those newer than last run)
28
+ const lastProcessed = (db.query<any, []>(
29
+ "SELECT COALESCE(MAX(source_reflection_id), 0) as mid FROM playbook"
30
+ ).get() as any)?.mid ?? 0
31
+
32
+ const reflections = (db.query as any)(
33
+ "SELECT * FROM reflections WHERE id > ? ORDER BY id ASC"
34
+ ).all(lastProcessed)
35
+
36
+ if (reflections.length === 0) {
37
+ log.debug("[curator] No new reflections to process")
38
+ } else {
39
+ log.info(`[curator] Processing ${reflections.length} new reflections`)
40
+ for (const reflection of reflections) {
41
+ processReflection(db, reflection)
42
+ }
43
+ }
44
+
45
+ // Prune rules where harmful > helpful (consistently bad rules)
46
+ db.query(`
47
+ UPDATE playbook
48
+ SET active = 0, updated_at = unixepoch()
49
+ WHERE active = 1
50
+ AND harmful_count > helpful_count
51
+ AND harmful_count >= ?
52
+ `).run(MAX_HARMFUL_BEFORE_PRUNE)
53
+
54
+ // Archive unused workers
55
+ const cutoff = Math.floor(Date.now() / 1000) - (DAYS_BEFORE_ARCHIVE * 86400)
56
+ const staleworkers = (db.query as any)(`
57
+ SELECT a.id, a.name
58
+ FROM agents a
59
+ WHERE a.role = 'worker'
60
+ AND a.status != 'archived'
61
+ AND a.enabled = 1
62
+ AND (
63
+ SELECT MAX(t.created_at) FROM traces t WHERE t.agent_id = a.id
64
+ ) < ?
65
+ `).all(cutoff)
66
+
67
+ for (const worker of staleworkers) {
68
+ db.query(
69
+ "UPDATE agents SET status = 'archived', updated_at = unixepoch() WHERE id = ?"
70
+ ).run(worker.id)
71
+
72
+ // Add playbook note about archival
73
+ addOrUpdateRule(db, {
74
+ rule: `Worker '${worker.name}' was archived due to inactivity (>${DAYS_BEFORE_ARCHIVE} days unused).`,
75
+ category: "agent_creation",
76
+ applicable_to: null,
77
+ sourceReflectionId: null,
78
+ })
79
+
80
+ log.info(`[curator] Archived inactive worker: ${worker.name} (${worker.id})`)
81
+ }
82
+
83
+ log.info("[curator] Playbook updated")
84
+ } catch (err) {
85
+ log.warn("[curator] Error:", err)
86
+ }
87
+ }
88
+
89
+ // ─── Process a single reflection ─────────────────────────────────────────────
90
+
91
+ function processReflection(db: any, reflection: any): void {
92
+ const category = mapInsightTypeToCategory(reflection.insight_type)
93
+
94
+ // Check if a similar rule already exists (fuzzy check by first 60 chars)
95
+ const prefix = reflection.description.substring(0, 60)
96
+ const existing = (db.query as any)(
97
+ "SELECT id, helpful_count FROM playbook WHERE rule LIKE ? AND active = 1 LIMIT 1"
98
+ ).get(`${prefix}%`)
99
+
100
+ if (existing) {
101
+ // Reinforce existing rule
102
+ db.query(
103
+ "UPDATE playbook SET helpful_count = helpful_count + 1, updated_at = unixepoch() WHERE id = ?"
104
+ ).run(existing.id)
105
+ return
106
+ }
107
+
108
+ // Insert new rule
109
+ db.query(`
110
+ INSERT INTO playbook (rule, category, applicable_to, helpful_count, source_reflection_id)
111
+ VALUES (?, ?, ?, 1, ?)
112
+ `).run(
113
+ reflection.description,
114
+ category,
115
+ reflection.affected_tools
116
+ ? JSON.stringify(JSON.parse(reflection.affected_tools))
117
+ : null,
118
+ reflection.id,
119
+ )
120
+ }
121
+
122
+ function mapInsightTypeToCategory(
123
+ type: string
124
+ ): "tool_selection" | "response_quality" | "error_avoidance" | "optimization" | "agent_creation" {
125
+ const map: Record<string, any> = {
126
+ success_pattern: "tool_selection",
127
+ failure_pattern: "error_avoidance",
128
+ optimization: "optimization",
129
+ ethics_violation: "error_avoidance",
130
+ }
131
+ return map[type] ?? "optimization"
132
+ }
133
+
134
+ function addOrUpdateRule(
135
+ db: any,
136
+ opts: {
137
+ rule: string
138
+ category: string
139
+ applicable_to: string | null
140
+ sourceReflectionId: number | null
141
+ }
142
+ ): void {
143
+ const prefix = opts.rule.substring(0, 60)
144
+ const existing = (db.query as any)(
145
+ "SELECT id FROM playbook WHERE rule LIKE ? LIMIT 1"
146
+ ).get(`${prefix}%`)
147
+
148
+ if (existing) {
149
+ db.query(
150
+ "UPDATE playbook SET helpful_count = helpful_count + 1, updated_at = unixepoch() WHERE id = ?"
151
+ ).run(existing.id)
152
+ } else {
153
+ db.query(`
154
+ INSERT INTO playbook (rule, category, applicable_to, helpful_count, source_reflection_id)
155
+ VALUES (?, ?, ?, 1, ?)
156
+ `).run(opts.rule, opts.category, opts.applicable_to, opts.sourceReflectionId)
157
+ }
158
+ }
@@ -0,0 +1,166 @@
1
+ import type { Config } from "../config/loader.ts";
2
+ import { logger } from "../utils/logger.ts";
3
+ import * as childProcess from "node:child_process";
4
+
5
+ export type HookName =
6
+ | "before_model_resolve"
7
+ | "before_prompt_build"
8
+ | "before_tool_call"
9
+ | "after_tool_call"
10
+ | "tool_result_persist"
11
+ | "before_compaction"
12
+ | "after_compaction"
13
+ | "message_received"
14
+ | "message_sending"
15
+ | "message_sent"
16
+ | "session_start"
17
+ | "session_end"
18
+ | "gateway_start"
19
+ | "gateway_stop";
20
+
21
+ export interface HookContext {
22
+ sessionId?: string;
23
+ agentId?: string;
24
+ data?: Record<string, unknown>;
25
+ timestamp: Date;
26
+ }
27
+
28
+ export type HookHandler = (context: HookContext) => Promise<Record<string, unknown> | void>;
29
+
30
+ export class HookPipeline {
31
+ private config: Config;
32
+ private log = logger.child("hooks");
33
+ private handlers: Map<HookName, HookHandler[]> = new Map();
34
+ private scriptCache: Map<HookName, string> = new Map();
35
+
36
+ constructor(config: Config) {
37
+ this.config = config;
38
+ this.loadScripts();
39
+ }
40
+
41
+ private loadScripts(): void {
42
+ const scripts = this.config.hooks?.scripts;
43
+ if (!scripts) return;
44
+
45
+ const hookNames: HookName[] = [
46
+ "before_model_resolve",
47
+ "before_prompt_build",
48
+ "before_tool_call",
49
+ "after_tool_call",
50
+ "tool_result_persist",
51
+ "before_compaction",
52
+ "after_compaction",
53
+ "message_received",
54
+ "message_sending",
55
+ "message_sent",
56
+ "session_start",
57
+ "session_end",
58
+ "gateway_start",
59
+ "gateway_stop",
60
+ ];
61
+
62
+ for (const name of hookNames) {
63
+ const script = scripts[name];
64
+ if (script) {
65
+ this.scriptCache.set(name, script);
66
+ this.log.debug(`Loaded script for hook: ${name}`);
67
+ }
68
+ }
69
+ }
70
+
71
+ registerHandler(name: HookName, handler: HookHandler): void {
72
+ const handlers = this.handlers.get(name) ?? [];
73
+ handlers.push(handler);
74
+ this.handlers.set(name, handlers);
75
+ this.log.debug(`Registered handler for hook: ${name}`);
76
+ }
77
+
78
+ unregisterHandler(name: HookName, handler: HookHandler): boolean {
79
+ const handlers = this.handlers.get(name);
80
+ if (!handlers) return false;
81
+
82
+ const index = handlers.indexOf(handler);
83
+ if (index >= 0) {
84
+ handlers.splice(index, 1);
85
+ return true;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ async execute(name: HookName, context: HookContext): Promise<Record<string, unknown> | void> {
91
+ this.log.debug(`Executing hook: ${name}`, { sessionId: context.sessionId });
92
+
93
+ const handlers = this.handlers.get(name) ?? [];
94
+ for (const handler of handlers) {
95
+ try {
96
+ await handler(context);
97
+ } catch (error) {
98
+ this.log.error(`Handler failed for ${name}: ${(error as Error).message}`);
99
+ }
100
+ }
101
+
102
+ const script = this.scriptCache.get(name);
103
+ if (script) {
104
+ try {
105
+ const result = await this.executeScript(script, context);
106
+ return result;
107
+ } catch (error) {
108
+ this.log.error(`Script failed for ${name}: ${(error as Error).message}`);
109
+ }
110
+ }
111
+ }
112
+
113
+ private async executeScript(
114
+ scriptPath: string,
115
+ context: HookContext
116
+ ): Promise<Record<string, unknown> | void> {
117
+ return new Promise((resolve, reject) => {
118
+ const payload = JSON.stringify(context);
119
+
120
+ const proc = childProcess.spawn(scriptPath, [], {
121
+ stdio: ["pipe", "pipe", "pipe"],
122
+ shell: true,
123
+ }) as any;
124
+
125
+ let stdout = "";
126
+ let stderr = "";
127
+
128
+ proc.stdout?.on("data", (data) => {
129
+ stdout += data.toString();
130
+ });
131
+
132
+ proc.stderr?.on("data", (data) => {
133
+ stderr += data.toString();
134
+ });
135
+
136
+ proc.on("close", (code) => {
137
+ if (code === 0 && stdout) {
138
+ try {
139
+ resolve(JSON.parse(stdout));
140
+ } catch {
141
+ resolve();
142
+ }
143
+ } else if (stderr) {
144
+ reject(new Error(stderr));
145
+ } else {
146
+ resolve();
147
+ }
148
+ });
149
+
150
+ proc.on("error", (error) => {
151
+ reject(error);
152
+ });
153
+
154
+ proc.stdin?.write(payload);
155
+ proc.stdin?.end();
156
+ });
157
+ }
158
+
159
+ hasHandlers(name: HookName): boolean {
160
+ return (this.handlers.get(name)?.length ?? 0) > 0 || this.scriptCache.has(name);
161
+ }
162
+ }
163
+
164
+ export function createHookPipeline(config: Config): HookPipeline {
165
+ return new HookPipeline(config);
166
+ }
@@ -0,0 +1,116 @@
1
+ import type { Config } from "../config/loader.ts"
2
+ import { logger } from "../utils/logger.ts"
3
+ import { MCPClientManager, createMCPManager } from "@johpaz/hive-mcp"
4
+ import { getDb } from "../storage/sqlite.ts"
5
+ import { buildSystemPromptWithProjects } from "./prompt-builder.ts"
6
+ import { EventEmitter } from "node:events"
7
+
8
+ export interface AgentOptions {
9
+ agentId: string
10
+ config: Config
11
+ workspacePath: string
12
+ }
13
+
14
+ /**
15
+ * Agent — minimal wrapper kept only to manage the MCP connection lifecycle.
16
+ * Tool execution is handled entirely by native-tools.ts + agent-loop.ts.
17
+ */
18
+ export class Agent extends EventEmitter {
19
+ readonly agentId: string
20
+ readonly workspacePath: string
21
+ private config: Config
22
+ private mcpManager: MCPClientManager | null = null
23
+ private log = logger.child("agent")
24
+
25
+ constructor(options: AgentOptions) {
26
+ super()
27
+ this.agentId = options.agentId
28
+ this.workspacePath = options.workspacePath
29
+ this.config = options.config
30
+ }
31
+
32
+ async initialize(): Promise<void> {
33
+ this.log.info(`Initializing agent: ${this.agentId}`)
34
+
35
+ if (this.config.mcp?.enabled !== false) {
36
+ const db = getDb()
37
+ const dbServers = db.query<any, []>("SELECT * FROM mcp_servers").all()
38
+ const mcpConfig = { ...this.config.mcp }
39
+ mcpConfig.servers = { ...(mcpConfig.servers || {}) }
40
+
41
+ for (const s of dbServers) {
42
+ if (s.enabled) {
43
+ let headers: Record<string, string> | undefined
44
+ if (s.headers_encrypted && s.headers_iv) {
45
+ try {
46
+ const { decryptConfig } = await import("../storage/crypto.ts")
47
+ headers = decryptConfig(s.headers_encrypted, s.headers_iv)
48
+ } catch (e) {
49
+ this.log.error(`Failed to decrypt headers for ${s.name}: ${(e as Error).message}`)
50
+ }
51
+ }
52
+ mcpConfig.servers[s.name] = {
53
+ transport: s.transport,
54
+ command: s.command,
55
+ args: s.args ? JSON.parse(s.args) : [],
56
+ url: s.url,
57
+ headers,
58
+ enabled: true,
59
+ ...mcpConfig.servers[s.name],
60
+ }
61
+ if (headers) mcpConfig.servers[s.name].headers = headers
62
+ }
63
+ }
64
+
65
+ this.mcpManager = createMCPManager(mcpConfig)
66
+
67
+ const mcpLog = this.log.child("mcp")
68
+ this.mcpManager.setLogHandler((level, context, message, meta) => {
69
+ const ctx = context ? `[${context}] ` : ""
70
+ switch (level) {
71
+ case "debug": mcpLog.debug(`${ctx}${message}`, meta); break
72
+ case "info": mcpLog.info(`${ctx}${message}`, meta); break
73
+ case "warn": mcpLog.warn(`${ctx}${message}`, meta); break
74
+ case "error": mcpLog.error(`${ctx}${message}`, meta); break
75
+ }
76
+ })
77
+
78
+ await this.mcpManager.initialize()
79
+ try {
80
+ await this.mcpManager.connectAll()
81
+ } catch (error) {
82
+ this.log.warn(`MCP connection errors: ${(error as Error).message}`)
83
+ }
84
+ }
85
+
86
+ this.log.info(`Agent initialized: ${this.agentId}`)
87
+ }
88
+
89
+ async updateConfig(config: Config): Promise<void> {
90
+ this.config = config
91
+ if (this.mcpManager) {
92
+ await this.mcpManager.updateConfig(this.config.mcp || {})
93
+ }
94
+ }
95
+
96
+ getConfig(): Config { return this.config }
97
+ getWorkspacePath(): string { return this.workspacePath }
98
+ getMCPManager(): MCPClientManager | null { return this.mcpManager }
99
+
100
+ // ── Compatibility stubs ──────────────────────────────────────────────────────
101
+ // The native loop (context-compiler.ts) reads all data from SQLite on every
102
+ // call, so explicit reload methods are no-ops. buildPrompt() delegates to the
103
+ // shared prompt builder used by the loop.
104
+
105
+ async buildPrompt(): Promise<string> {
106
+ const userId = process.env.HIVE_USER_ID || ""
107
+ return buildSystemPromptWithProjects({ agentId: this.agentId, userId })
108
+ }
109
+
110
+ setEthics(_content: string): void { /* ethics loaded from DB by context-compiler */ }
111
+ async reload(): Promise<void> { /* no-op: context-compiler reads DB on every call */ }
112
+ async reloadSoul(): Promise<void> { /* no-op */ }
113
+ async reloadUser(): Promise<void> { /* no-op */ }
114
+ async reloadEthics(): Promise<void> { /* no-op */ }
115
+ reloadSkills(): void { /* no-op */ }
116
+ }