@johpaz/hive-sdk 0.0.12 → 0.0.15
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/.github/CODEOWNERS +9 -0
- package/.github/workflows/publish.yml +89 -0
- package/.github/workflows/version-bump.yml +102 -0
- package/CHANGELOG.md +38 -0
- package/README.md +158 -0
- package/bun.lock +543 -0
- package/bunfig.toml +7 -0
- package/docs/API-AGENTS.md +316 -0
- package/docs/API-CONTEXT-COMPILER.md +252 -0
- package/docs/API-DAG-SCHEDULER.md +273 -0
- package/docs/API-TOOLS-SKILLS-CHANNELS.md +293 -0
- package/docs/API-WORKERS-EVENTS.md +152 -0
- package/docs/INDEX.md +141 -0
- package/docs/README.md +68 -0
- package/package.json +54 -105
- package/packages/cli/package.json +17 -0
- package/packages/cli/src/commands/init.ts +56 -0
- package/packages/cli/src/commands/run.ts +45 -0
- package/packages/cli/src/commands/test.ts +42 -0
- package/packages/cli/src/commands/trace.ts +55 -0
- package/packages/cli/src/index.ts +43 -0
- package/packages/core/package.json +58 -0
- package/packages/core/src/ace/Curator.ts +158 -0
- package/packages/core/src/ace/Reflector.ts +200 -0
- package/packages/core/src/ace/Tracer.ts +100 -0
- package/packages/core/src/ace/index.ts +4 -0
- package/packages/core/src/agent/AgentRunner.ts +699 -0
- package/packages/core/src/agent/Compaction.ts +221 -0
- package/packages/core/src/agent/ContextCompiler.ts +567 -0
- package/packages/core/src/agent/ContextGuard.ts +91 -0
- package/packages/core/src/agent/ConversationStore.ts +244 -0
- package/packages/core/src/agent/Hooks.ts +166 -0
- package/packages/core/src/agent/NativeTools.ts +31 -0
- package/packages/core/src/agent/PromptBuilder.ts +169 -0
- package/packages/core/src/agent/Service.ts +267 -0
- package/packages/core/src/agent/StuckLoop.ts +133 -0
- package/packages/core/src/agent/index.ts +12 -0
- package/packages/core/src/agent/providers/LLMClient.ts +149 -0
- package/packages/core/src/agent/providers/anthropic.ts +212 -0
- package/packages/core/src/agent/providers/gemini.ts +215 -0
- package/packages/core/src/agent/providers/index.ts +199 -0
- package/packages/core/src/agent/providers/interface.ts +195 -0
- package/packages/core/src/agent/providers/ollama.ts +175 -0
- package/packages/core/src/agent/providers/openai-compat.ts +231 -0
- package/packages/core/src/agent/providers.ts +1 -0
- package/packages/core/src/agent/selectors/PlaybookSelector.ts +147 -0
- package/packages/core/src/agent/selectors/SkillSelector.ts +478 -0
- package/packages/core/src/agent/selectors/ToolSelector.ts +577 -0
- package/packages/core/src/agent/selectors/index.ts +6 -0
- package/packages/core/src/api/createAgent.test.ts +48 -0
- package/packages/core/src/api/createAgent.ts +122 -0
- package/packages/core/src/api/index.ts +2 -0
- package/packages/core/src/canvas/CanvasManager.ts +390 -0
- package/packages/core/src/canvas/a2ui-tools.ts +255 -0
- package/packages/core/src/canvas/canvas-tools.ts +448 -0
- package/packages/core/src/canvas/emitter.ts +149 -0
- package/packages/core/src/canvas/index.ts +6 -0
- package/packages/core/src/config/index.ts +2 -0
- package/packages/core/src/config/loader.ts +554 -0
- package/packages/core/src/ethics/EthicsGuard.test.ts +54 -0
- package/packages/core/src/ethics/EthicsGuard.ts +66 -0
- package/packages/core/src/ethics/index.ts +2 -0
- package/packages/core/src/gateway/channel-notify.test.ts +14 -0
- package/packages/core/src/gateway/channel-notify.ts +12 -0
- package/packages/core/src/gateway/index.ts +1 -0
- package/packages/core/src/index.ts +37 -0
- package/packages/core/src/mcp/MCPClient.ts +439 -0
- package/packages/core/src/mcp/MCPToolAdapter.ts +176 -0
- package/packages/core/src/mcp/config.ts +13 -0
- package/packages/core/src/mcp/hot-reload.ts +147 -0
- package/packages/core/src/mcp/index.ts +11 -0
- package/packages/core/src/mcp/logger.ts +42 -0
- package/packages/core/src/mcp/singleton.ts +21 -0
- package/packages/core/src/mcp/transports/index.ts +67 -0
- package/packages/core/src/mcp/transports/sse.ts +241 -0
- package/packages/core/src/mcp/transports/websocket.ts +159 -0
- package/packages/core/src/memory/Scratchpad.test.ts +47 -0
- package/packages/core/src/memory/Scratchpad.ts +37 -0
- package/packages/core/src/memory/Storage.ts +6 -0
- package/packages/core/src/memory/index.ts +2 -0
- package/packages/core/src/multimodal/VisionService.ts +293 -0
- package/packages/core/src/multimodal/index.ts +2 -0
- package/packages/core/src/multimodal/types.ts +28 -0
- package/packages/core/src/security/Pairing.ts +250 -0
- package/packages/core/src/security/RateLimit.ts +270 -0
- package/packages/core/src/security/index.ts +4 -0
- package/packages/core/src/skills/SkillLoader.ts +388 -0
- package/packages/core/src/skills/bundled-data.generated.ts +3332 -0
- package/packages/core/src/skills/defineSkill.ts +18 -0
- package/packages/core/src/skills/index.ts +4 -0
- package/packages/core/src/state/index.ts +2 -0
- package/packages/core/src/state/store.ts +312 -0
- package/packages/core/src/storage/SQLiteStorage.ts +407 -0
- package/packages/core/src/storage/crypto.ts +101 -0
- package/packages/core/src/storage/index.ts +10 -0
- package/packages/core/src/storage/onboarding.ts +1603 -0
- package/packages/core/src/storage/schema.ts +689 -0
- package/packages/core/src/storage/seed.ts +740 -0
- package/packages/core/src/storage/usage.ts +374 -0
- package/packages/core/src/swarm/AgentBus.ts +460 -0
- package/packages/core/src/swarm/AgentExecutor.ts +53 -0
- package/packages/core/src/swarm/Coordinator.ts +251 -0
- package/packages/core/src/swarm/EventBridge.ts +122 -0
- package/packages/core/src/swarm/EventBus.ts +169 -0
- package/packages/core/src/swarm/TaskGraph.ts +192 -0
- package/packages/core/src/swarm/TaskNode.ts +97 -0
- package/packages/core/src/swarm/TaskResult.ts +22 -0
- package/packages/core/src/swarm/WorkerPool.ts +236 -0
- package/packages/core/src/swarm/errors.ts +37 -0
- package/packages/core/src/swarm/index.ts +30 -0
- package/packages/core/src/swarm/presets/HiveLearnPreset.ts +99 -0
- package/packages/core/src/swarm/presets/ResearchPreset.ts +97 -0
- package/packages/core/src/swarm/presets/index.ts +4 -0
- package/packages/core/src/swarm/strategies/ParallelStrategy.ts +21 -0
- package/packages/core/src/swarm/strategies/PriorityStrategy.ts +46 -0
- package/packages/core/src/swarm/strategies/index.ts +3 -0
- package/packages/core/src/swarm/types.ts +164 -0
- package/packages/core/src/tools/ToolExecutor.ts +58 -0
- package/packages/core/src/tools/ToolRegistry.test.ts +98 -0
- package/packages/core/src/tools/ToolRegistry.ts +61 -0
- package/packages/core/src/tools/agents/get-available-models.ts +118 -0
- package/packages/core/src/tools/agents/index.ts +715 -0
- package/packages/core/src/tools/bridge-events.ts +26 -0
- package/packages/core/src/tools/canvas/index.ts +375 -0
- package/packages/core/src/tools/cli/index.ts +142 -0
- package/packages/core/src/tools/codebridge/index.ts +342 -0
- package/packages/core/src/tools/core/index.ts +476 -0
- package/packages/core/src/tools/cron/index.ts +626 -0
- package/packages/core/src/tools/filesystem/fs-delete.ts +78 -0
- package/packages/core/src/tools/filesystem/fs-edit.ts +106 -0
- package/packages/core/src/tools/filesystem/fs-exists.ts +63 -0
- package/packages/core/src/tools/filesystem/fs-glob.ts +108 -0
- package/packages/core/src/tools/filesystem/fs-list.ts +129 -0
- package/packages/core/src/tools/filesystem/fs-read.ts +72 -0
- package/packages/core/src/tools/filesystem/fs-write.ts +67 -0
- package/packages/core/src/tools/filesystem/index.ts +34 -0
- package/packages/core/src/tools/filesystem/workspace-guard.ts +62 -0
- package/packages/core/src/tools/index.ts +231 -0
- package/packages/core/src/tools/meeting/index.ts +363 -0
- package/packages/core/src/tools/office/index.ts +47 -0
- package/packages/core/src/tools/office/office-escribir-docx.ts +192 -0
- package/packages/core/src/tools/office/office-escribir-pdf.ts +172 -0
- package/packages/core/src/tools/office/office-escribir-pptx.ts +174 -0
- package/packages/core/src/tools/office/office-escribir-xlsx.ts +116 -0
- package/packages/core/src/tools/office/office-leer-docx.ts +93 -0
- package/packages/core/src/tools/office/office-leer-pdf.ts +114 -0
- package/packages/core/src/tools/office/office-leer-pptx.ts +136 -0
- package/packages/core/src/tools/office/office-leer-xlsx.ts +124 -0
- package/packages/core/src/tools/projects/index.ts +37 -0
- package/packages/core/src/tools/projects/project-create.ts +94 -0
- package/packages/core/src/tools/projects/project-done.ts +66 -0
- package/packages/core/src/tools/projects/project-fail.ts +66 -0
- package/packages/core/src/tools/projects/project-list.ts +96 -0
- package/packages/core/src/tools/projects/project-update.ts +72 -0
- package/packages/core/src/tools/projects/task-create.ts +68 -0
- package/packages/core/src/tools/projects/task-evaluate.ts +93 -0
- package/packages/core/src/tools/projects/task-update.ts +93 -0
- package/packages/core/src/tools/types.ts +39 -0
- package/packages/core/src/tools/voice/index.ts +104 -0
- package/packages/core/src/tools/web/browser-click.ts +78 -0
- package/packages/core/src/tools/web/browser-extract.ts +139 -0
- package/packages/core/src/tools/web/browser-navigate.ts +106 -0
- package/packages/core/src/tools/web/browser-screenshot.ts +87 -0
- package/packages/core/src/tools/web/browser-script.ts +88 -0
- package/packages/core/src/tools/web/browser-service.ts +554 -0
- package/packages/core/src/tools/web/browser-type.ts +101 -0
- package/packages/core/src/tools/web/browser-wait.ts +136 -0
- package/packages/core/src/tools/web/index.ts +41 -0
- package/packages/core/src/tools/web/web-fetch.ts +78 -0
- package/packages/core/src/tools/web/web-search.ts +123 -0
- package/packages/core/src/utils/benchmark.ts +80 -0
- package/packages/core/src/utils/crypto.ts +73 -0
- package/packages/core/src/utils/date.ts +42 -0
- package/packages/core/src/utils/index.ts +10 -0
- package/packages/core/src/utils/logger.ts +389 -0
- package/packages/core/src/utils/retry.ts +70 -0
- package/packages/core/src/utils/toon.ts +253 -0
- package/packages/core/src/voice/index.ts +656 -0
- package/test/setup-db.ts +216 -0
- package/tsconfig.json +39 -0
- package/src/agents.ts +0 -1
- package/src/canvas.ts +0 -1
- package/src/channels.ts +0 -1
- package/src/config.ts +0 -1
- package/src/events.ts +0 -1
- package/src/gateway.ts +0 -1
- package/src/index.ts +0 -304
- package/src/mcp.ts +0 -1
- package/src/multimodal.ts +0 -1
- package/src/scheduler.ts +0 -1
- package/src/security.ts +0 -1
- package/src/skills.ts +0 -1
- package/src/state.ts +0 -1
- package/src/storage.ts +0 -1
- package/src/tools.ts +0 -1
- package/src/tts.ts +0 -1
- package/src/types.ts +0 -82
- package/src/utils.ts +0 -1
- package/src/voice.ts +0 -1
|
@@ -0,0 +1,244 @@
|
|
|
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/SQLiteStorage.ts"
|
|
9
|
+
import { logger } from "../utils/logger.ts"
|
|
10
|
+
import type { LLMMessage, ContentPart } from "./providers/LLMClient"
|
|
11
|
+
import { estimateTokens } from "../utils/toon.ts"
|
|
12
|
+
|
|
13
|
+
const log = logger.child("conv-store")
|
|
14
|
+
|
|
15
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
export interface StoredMessage {
|
|
18
|
+
id: number
|
|
19
|
+
thread_id: string
|
|
20
|
+
channel: string
|
|
21
|
+
role: "user" | "assistant" | "tool" | "system"
|
|
22
|
+
content: string
|
|
23
|
+
tool_calls_json: string | null
|
|
24
|
+
tool_call_id: string | null
|
|
25
|
+
reasoning_content: string | null // Kimi K2 thinking — must be round-tripped
|
|
26
|
+
content_multimodal: string | null // JSON array of ContentPart[]
|
|
27
|
+
token_count: number
|
|
28
|
+
created_at: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ─── Message operations ───────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
export function addMessage(
|
|
34
|
+
threadId: string,
|
|
35
|
+
role: StoredMessage["role"],
|
|
36
|
+
content: string | ContentPart[],
|
|
37
|
+
opts?: {
|
|
38
|
+
channel?: string
|
|
39
|
+
tool_calls?: LLMMessage["tool_calls"]
|
|
40
|
+
tool_call_id?: string
|
|
41
|
+
reasoning_content?: string
|
|
42
|
+
}
|
|
43
|
+
): number {
|
|
44
|
+
const db = getDb()
|
|
45
|
+
// Handle multimodal content by extracting text for the content column
|
|
46
|
+
const textContent = typeof content === "string"
|
|
47
|
+
? content
|
|
48
|
+
: Array.isArray(content)
|
|
49
|
+
? content.filter(p => p.type === "text").map(p => (p as any).text).join("\n")
|
|
50
|
+
: String(content)
|
|
51
|
+
|
|
52
|
+
const content_multimodal = Array.isArray(content) ? JSON.stringify(content) : null
|
|
53
|
+
const tool_calls_json = opts?.tool_calls ? JSON.stringify(opts.tool_calls) : null
|
|
54
|
+
|
|
55
|
+
const result = db.query(`
|
|
56
|
+
INSERT INTO conversations (thread_id, channel, role, content, content_multimodal, tool_calls_json, tool_call_id, reasoning_content, token_count, updated_at)
|
|
57
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, unixepoch())
|
|
58
|
+
RETURNING id
|
|
59
|
+
`).get(
|
|
60
|
+
threadId,
|
|
61
|
+
opts?.channel ?? "webchat",
|
|
62
|
+
role,
|
|
63
|
+
textContent,
|
|
64
|
+
content_multimodal,
|
|
65
|
+
tool_calls_json,
|
|
66
|
+
opts?.tool_call_id ?? null,
|
|
67
|
+
opts?.reasoning_content ?? null,
|
|
68
|
+
// Estimate tokens: content + tool_calls JSON
|
|
69
|
+
Math.max(1, estimateTokens(textContent) + estimateTokens(tool_calls_json ?? "")),
|
|
70
|
+
) as { id: number }
|
|
71
|
+
|
|
72
|
+
return result.id
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns all messages for the thread ordered oldest → newest.
|
|
77
|
+
*/
|
|
78
|
+
export function getHistory(threadId: string, limit = 200): StoredMessage[] {
|
|
79
|
+
const db = getDb()
|
|
80
|
+
return db.query(`
|
|
81
|
+
SELECT * FROM conversations
|
|
82
|
+
WHERE thread_id = ?
|
|
83
|
+
ORDER BY id ASC
|
|
84
|
+
LIMIT ?
|
|
85
|
+
`).all(threadId, limit) as StoredMessage[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns only the last N messages (oldest → newest order),
|
|
90
|
+
* with leading orphaned tool messages stripped from the window start.
|
|
91
|
+
*
|
|
92
|
+
* A tool message is "orphaned" when the assistant message that issued its
|
|
93
|
+
* tool_call_id is not present in the loaded window (it was compacted away).
|
|
94
|
+
* Sending orphaned tool messages to the LLM causes provider errors.
|
|
95
|
+
*/
|
|
96
|
+
export function getRecentMessages(threadId: string, n: number): StoredMessage[] {
|
|
97
|
+
const db = getDb()
|
|
98
|
+
const rows = db.query(`
|
|
99
|
+
SELECT * FROM conversations
|
|
100
|
+
WHERE thread_id = ?
|
|
101
|
+
ORDER BY id DESC
|
|
102
|
+
LIMIT ?
|
|
103
|
+
`).all(threadId, n) as StoredMessage[]
|
|
104
|
+
return stripLeadingOrphanedTools(rows.reverse())
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function stripLeadingOrphanedTools(rows: StoredMessage[]): StoredMessage[] {
|
|
108
|
+
// Collect all tool_call_ids referenced by assistant messages in this window
|
|
109
|
+
const knownIds = new Set<string>()
|
|
110
|
+
for (const r of rows) {
|
|
111
|
+
if (r.role === "assistant" && r.tool_calls_json) {
|
|
112
|
+
try {
|
|
113
|
+
const tcs = JSON.parse(r.tool_calls_json) as Array<{ id: string }>
|
|
114
|
+
for (const tc of tcs) knownIds.add(tc.id)
|
|
115
|
+
} catch { /* ignore malformed JSON */ }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Drop tool messages at the start of the window whose assistant is missing
|
|
120
|
+
let start = 0
|
|
121
|
+
while (
|
|
122
|
+
start < rows.length &&
|
|
123
|
+
rows[start].role === "tool" &&
|
|
124
|
+
rows[start].tool_call_id !== null &&
|
|
125
|
+
!knownIds.has(rows[start].tool_call_id!)
|
|
126
|
+
) {
|
|
127
|
+
start++
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (start > 0) {
|
|
131
|
+
log.warn(`[conv-store] Stripped ${start} leading orphaned tool message(s) from window (tool_call_ids outside window)`)
|
|
132
|
+
}
|
|
133
|
+
return start > 0 ? rows.slice(start) : rows
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function getMessageCount(threadId: string): number {
|
|
137
|
+
const db = getDb()
|
|
138
|
+
const row = db.query(
|
|
139
|
+
"SELECT COUNT(*) as cnt FROM conversations WHERE thread_id = ?"
|
|
140
|
+
).get(threadId) as { cnt: number }
|
|
141
|
+
return row.cnt
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function getTotalTokens(threadId: string): number {
|
|
145
|
+
const db = getDb()
|
|
146
|
+
const row = db.query(
|
|
147
|
+
"SELECT COALESCE(SUM(token_count), 0) as total FROM conversations WHERE thread_id = ?"
|
|
148
|
+
).get(threadId) as { total: number }
|
|
149
|
+
return row.total
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Messages after a given message ID (for incremental summary updates).
|
|
154
|
+
*/
|
|
155
|
+
export function getMessagesAfter(threadId: string, afterId: number): StoredMessage[] {
|
|
156
|
+
const db = getDb()
|
|
157
|
+
return db.query(`
|
|
158
|
+
SELECT * FROM conversations
|
|
159
|
+
WHERE thread_id = ? AND id > ?
|
|
160
|
+
ORDER BY id ASC
|
|
161
|
+
`).all(threadId, afterId) as StoredMessage[]
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ─── Convert stored messages → LLMMessage array ───────────────────────────────
|
|
165
|
+
|
|
166
|
+
export function toAPIMessages(rows: StoredMessage[]): LLMMessage[] {
|
|
167
|
+
return rows.map((r) => {
|
|
168
|
+
let content: string | ContentPart[] = r.content
|
|
169
|
+
if (r.content_multimodal) {
|
|
170
|
+
try { content = JSON.parse(r.content_multimodal) } catch { /* ignore */ }
|
|
171
|
+
}
|
|
172
|
+
const msg: LLMMessage = { role: r.role, content }
|
|
173
|
+
if (r.tool_calls_json) {
|
|
174
|
+
try { msg.tool_calls = JSON.parse(r.tool_calls_json) } catch { /* ignore */ }
|
|
175
|
+
}
|
|
176
|
+
if (r.tool_call_id) msg.tool_call_id = r.tool_call_id
|
|
177
|
+
if (r.reasoning_content) msg.reasoning_content = r.reasoning_content
|
|
178
|
+
return msg
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ─── Summaries ────────────────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
export interface Summary {
|
|
185
|
+
summary: string
|
|
186
|
+
last_message_id: number
|
|
187
|
+
messages_covered: number
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function getSummary(threadId: string): Summary | null {
|
|
191
|
+
const db = getDb()
|
|
192
|
+
return db.query(
|
|
193
|
+
"SELECT summary, last_message_id, messages_covered FROM summaries WHERE thread_id = ?"
|
|
194
|
+
).get(threadId) as Summary | null
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function saveSummary(
|
|
198
|
+
threadId: string,
|
|
199
|
+
summary: string,
|
|
200
|
+
messagesCovered: number,
|
|
201
|
+
lastMessageId: number
|
|
202
|
+
): void {
|
|
203
|
+
const db = getDb()
|
|
204
|
+
db.query(`
|
|
205
|
+
INSERT INTO summaries (thread_id, summary, messages_covered, last_message_id)
|
|
206
|
+
VALUES (?, ?, ?, ?)
|
|
207
|
+
ON CONFLICT(thread_id) DO UPDATE SET
|
|
208
|
+
summary = excluded.summary,
|
|
209
|
+
messages_covered = excluded.messages_covered,
|
|
210
|
+
last_message_id = excluded.last_message_id,
|
|
211
|
+
updated_at = unixepoch()
|
|
212
|
+
`).run(threadId, summary, messagesCovered, lastMessageId)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ─── Scratchpad ───────────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
export function saveScratchpadNote(
|
|
218
|
+
threadId: string,
|
|
219
|
+
key: string,
|
|
220
|
+
value: string,
|
|
221
|
+
source?: string
|
|
222
|
+
): void {
|
|
223
|
+
const db = getDb()
|
|
224
|
+
db.query(`
|
|
225
|
+
INSERT INTO scratchpad (thread_id, key, value, source)
|
|
226
|
+
VALUES (?, ?, ?, ?)
|
|
227
|
+
ON CONFLICT(thread_id, key) DO UPDATE SET
|
|
228
|
+
value = excluded.value,
|
|
229
|
+
source = excluded.source,
|
|
230
|
+
updated_at = unixepoch()
|
|
231
|
+
`).run(threadId, key, value, source ?? null)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function getScratchpad(threadId: string): Array<{ key: string; value: string }> {
|
|
235
|
+
const db = getDb()
|
|
236
|
+
return db.query(
|
|
237
|
+
"SELECT key, value FROM scratchpad WHERE thread_id = ? ORDER BY updated_at DESC"
|
|
238
|
+
).all(threadId) as Array<{ key: string; value: string }>
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function deleteScratchpadNote(threadId: string, key: string): void {
|
|
242
|
+
const db = getDb()
|
|
243
|
+
db.query("DELETE FROM scratchpad WHERE thread_id = ? AND key = ?").run(threadId, key)
|
|
244
|
+
}
|
|
@@ -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,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Tools Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Tool interface for native Hive tools (no LangChain)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface Tool {
|
|
8
|
+
name: string
|
|
9
|
+
description: string
|
|
10
|
+
parameters: {
|
|
11
|
+
type: "object"
|
|
12
|
+
properties: Record<string, ToolParameter>
|
|
13
|
+
required?: string[]
|
|
14
|
+
}
|
|
15
|
+
execute: (params: Record<string, unknown>, config?: any) => Promise<string | object>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ToolParameter {
|
|
19
|
+
type: string
|
|
20
|
+
description?: string
|
|
21
|
+
enum?: string[]
|
|
22
|
+
items?: ToolParameter
|
|
23
|
+
properties?: Record<string, ToolParameter>
|
|
24
|
+
required?: string[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ToolResult {
|
|
28
|
+
success: boolean
|
|
29
|
+
result?: any
|
|
30
|
+
error?: string
|
|
31
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt Builder — Construye el system prompt con la jerarquía constitucional.
|
|
3
|
+
*
|
|
4
|
+
* Orden de ensamblaje:
|
|
5
|
+
* 1. ÉTICA (capa constitucional, siempre completa, inmutable)
|
|
6
|
+
* 2. IDENTIDAD DEL AGENTE (de la tabla agents)
|
|
7
|
+
* 3. HIVE ECOSYSTEM (system prompt directo para el coordinador)
|
|
8
|
+
* 4. IDENTIDAD DEL USUARIO (de la tabla users)
|
|
9
|
+
*
|
|
10
|
+
* El Context Compiler agrega después:
|
|
11
|
+
* - Playbook rules (ACE)
|
|
12
|
+
* - Scratchpad notes
|
|
13
|
+
* - Skills activos
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { getDb } from "../storage/SQLiteStorage.ts"
|
|
17
|
+
import { logger } from "../utils/logger.ts"
|
|
18
|
+
import { formatContext } from "../utils/toon.ts"
|
|
19
|
+
import { resolveUserId } from "../storage/onboarding.ts"
|
|
20
|
+
|
|
21
|
+
const log = logger.child("prompt-builder")
|
|
22
|
+
|
|
23
|
+
export interface BuildSystemPromptOpts {
|
|
24
|
+
agentId: string
|
|
25
|
+
userId: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Construye el system prompt completo para un agente.
|
|
30
|
+
*
|
|
31
|
+
* Jerarquía:
|
|
32
|
+
* 1. Ética (siempre completa, no se filtra, no se comprime)
|
|
33
|
+
* 2. Identidad del agente (role, description, system_prompt)
|
|
34
|
+
* 3. Hive Ecosystem (ya incluido en agents.system_prompt desde onboarding.ts)
|
|
35
|
+
* 4. Identidad del usuario (nombre, preferencias, contexto)
|
|
36
|
+
*/
|
|
37
|
+
export async function buildSystemPrompt(opts: BuildSystemPromptOpts): Promise<string> {
|
|
38
|
+
const db = getDb()
|
|
39
|
+
const { agentId, userId } = opts
|
|
40
|
+
|
|
41
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
42
|
+
// 1. ÉTICA — Capa constitucional (siempre completa)
|
|
43
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
44
|
+
const ethicsRules = db.query<any, []>(`
|
|
45
|
+
SELECT name, content, description
|
|
46
|
+
FROM ethics
|
|
47
|
+
WHERE enabled = 1 AND active = 1
|
|
48
|
+
ORDER BY is_default DESC, id ASC
|
|
49
|
+
`).all()
|
|
50
|
+
|
|
51
|
+
let ethicsSection = ""
|
|
52
|
+
if (ethicsRules.length > 0) {
|
|
53
|
+
const ethicsContent = ethicsRules.map((rule: any) => {
|
|
54
|
+
return `## ${rule.name}\n${rule.content}`
|
|
55
|
+
}).join("\n\n")
|
|
56
|
+
|
|
57
|
+
ethicsSection = `# ÉTICA Y REGLAS CONSTITUCIONALES\n\n${ethicsContent}\n\n`
|
|
58
|
+
log.info(`[prompt-builder] Loaded ${ethicsRules.length} ethics rules`)
|
|
59
|
+
} else {
|
|
60
|
+
// Ética por defecto si no hay reglas configuradas
|
|
61
|
+
ethicsSection = `# ÉTICA Y REGLAS CONSTITUCIONALES\n\n` +
|
|
62
|
+
`- Sé útil, inofensivo y honesto\n` +
|
|
63
|
+
`- No generes contenido dañino, ilegal o peligroso\n` +
|
|
64
|
+
`- Respeta la privacidad y seguridad del usuario\n` +
|
|
65
|
+
`- Si no sabes algo, admítelo\n\n`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
69
|
+
// 2. IDENTIDAD DEL AGENTE
|
|
70
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
71
|
+
const agent = db.query<any, [string]>(`
|
|
72
|
+
SELECT id, name, role, description, system_prompt, tone, max_iterations, workspace
|
|
73
|
+
FROM agents
|
|
74
|
+
WHERE id = ?
|
|
75
|
+
`).get(agentId)
|
|
76
|
+
|
|
77
|
+
if (!agent) {
|
|
78
|
+
throw new Error(`Agent not found: ${agentId}`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let agentSection = `# IDENTIDAD DEL AGENTE\n\n`
|
|
82
|
+
agentSection += `**Nombre**: ${agent.name}\n`
|
|
83
|
+
agentSection += `**Rol**: ${agent.role}\n`
|
|
84
|
+
|
|
85
|
+
if (agent.description) {
|
|
86
|
+
agentSection += `**Descripción**: ${agent.description}\n`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (agent.tone) {
|
|
90
|
+
agentSection += `**Tono**: ${agent.tone}\n`
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
agentSection += `**Iteraciones máximas**: ${agent.max_iterations}\n\n`
|
|
94
|
+
|
|
95
|
+
// Workspace configuration
|
|
96
|
+
const workspacePath = agent.workspace || null
|
|
97
|
+
if (workspacePath) {
|
|
98
|
+
agentSection += `# WORKSPACE — ESPACIO DE TRABAJO EXCLUSIVO\n\n`
|
|
99
|
+
agentSection += `**Tu directorio de trabajo es**: \`${workspacePath}\`\n\n`
|
|
100
|
+
agentSection += `## REGLAS OBLIGATORIAS (no negociables)\n\n`
|
|
101
|
+
agentSection += `1. **TODAS** tus operaciones de archivos y comandos ocurren DENTRO de \`${workspacePath}\`. Sin excepciones.\n`
|
|
102
|
+
agentSection += `2. Cuando el sistema te pida listar archivos, explorar, leer o escribir — hazlo SIEMPRE dentro de \`${workspacePath}\`.\n`
|
|
103
|
+
agentSection += `3. Nunca uses \`ls\`, \`find\`, \`cat\` u otras herramientas apuntando a directorios del sistema (\`/\`, \`~\`, \`/home\`, \`/etc\`, etc.).\n`
|
|
104
|
+
agentSection += `4. Cuando uses \`cli_exec\`, el directorio de trabajo ya es \`${workspacePath}\` por defecto — NO necesitas especificar \`cwd\`.\n`
|
|
105
|
+
agentSection += `5. Para rutas relativas, son relativas a \`${workspacePath}\` — no al directorio del proceso.\n`
|
|
106
|
+
agentSection += `6. Si el usuario pide explorar "el proyecto" o "los archivos", asume que se refiere a \`${workspacePath}\`.\n`
|
|
107
|
+
agentSection += `7. Las tools de filesystem (\`fs_read\`, \`fs_write\`, \`fs_list\`, etc.) ya tienen tu workspace configurado — úsalas directamente con rutas relativas.\n\n`
|
|
108
|
+
agentSection += `> IMPORTANTE: Cualquier intento de acceder fuera de \`${workspacePath}\` será bloqueado automáticamente por el sistema.\n\n`
|
|
109
|
+
agentSection += `## CONSULTAS EN MCP, para llamar datos puedes buscar las tools por get o list recors\n\n`
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// System prompt específico del agente (su "personalidad" especializada)
|
|
114
|
+
if (agent.system_prompt) {
|
|
115
|
+
agentSection += `## System Prompt\n\n${agent.system_prompt}\n\n`
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
120
|
+
// 3. IDENTIDAD DEL USUARIO
|
|
121
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
122
|
+
const user = db.query<any, [string]>(`
|
|
123
|
+
SELECT id, name, language, timezone, occupation, notes
|
|
124
|
+
FROM users
|
|
125
|
+
WHERE id = ?
|
|
126
|
+
`).get(userId)
|
|
127
|
+
|
|
128
|
+
let userSection = `# IDENTIDAD DEL USUARIO\n\n`
|
|
129
|
+
|
|
130
|
+
if (user) {
|
|
131
|
+
const userData: Record<string, string | null> = {}
|
|
132
|
+
|
|
133
|
+
if (user.name) userData.Nombre = user.name
|
|
134
|
+
if (user.language) userData.Idioma = user.language
|
|
135
|
+
if (user.timezone) userData.ZonaHoraria = user.timezone
|
|
136
|
+
if (user.occupation) userData.Ocupación = user.occupation
|
|
137
|
+
if (user.notes) userData.Notes = user.notes
|
|
138
|
+
|
|
139
|
+
// Usar TOON para comprimir datos del usuario
|
|
140
|
+
if (Object.keys(userData).length > 0) {
|
|
141
|
+
userSection += formatContext(userData) + "\n\n"
|
|
142
|
+
} else {
|
|
143
|
+
userSection += `Usuario ID: ${userId}\n\n`
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
userSection += `Usuario ID: ${userId}\n\n`
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
150
|
+
// Ensamblar secciones en orden
|
|
151
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
152
|
+
const systemPrompt = `${ethicsSection}${agentSection}${userSection}`.trim()
|
|
153
|
+
|
|
154
|
+
log.info(`[prompt-builder] Built system prompt for agent=${agent.name} role=${agent.role}`)
|
|
155
|
+
|
|
156
|
+
return systemPrompt
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Versión simplificada para cuando solo se necesita el agentId
|
|
161
|
+
* (usa userId por defecto de env o threadId)
|
|
162
|
+
*/
|
|
163
|
+
export async function buildSystemPromptWithProjects(opts: {
|
|
164
|
+
agentId: string
|
|
165
|
+
userId?: string
|
|
166
|
+
}): Promise<string> {
|
|
167
|
+
const userId = opts.userId || resolveUserId({}) || "default"
|
|
168
|
+
return buildSystemPrompt({ agentId: opts.agentId, userId })
|
|
169
|
+
}
|