@sesamespace/hivemind 0.5.17 → 0.6.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.
@@ -0,0 +1,173 @@
1
+ # Hivemind Tool Use — Architecture Design
2
+
3
+ ## Current State
4
+
5
+ The LLM client does simple chat completions: `messages[] → response.content`. No tool/function calling.
6
+
7
+ ## Goal
8
+
9
+ Full agentic tool-use loop matching OpenClaw capabilities, with Hivemind's memory system as a differentiator.
10
+
11
+ ## Architecture
12
+
13
+ ### 1. Tool Calling Protocol (OpenAI-compatible, works with OpenRouter)
14
+
15
+ The OpenAI chat completions API supports `tools` (function definitions) and `tool_choice`. When the model wants to use a tool, it returns a `tool_calls` array instead of (or alongside) content. We then execute the tool, append the result as a `tool` role message, and call the model again.
16
+
17
+ ```
18
+ User message
19
+
20
+ LLM (with tools defined)
21
+
22
+ If tool_calls → execute tools → append results → call LLM again (loop)
23
+ If content only → return response
24
+ ```
25
+
26
+ This is a **while loop**, not a single call. The model may chain multiple tool calls before producing a final text response.
27
+
28
+ ### 2. Key Data Structures
29
+
30
+ ```typescript
31
+ interface ToolDefinition {
32
+ name: string;
33
+ description: string;
34
+ parameters: JSONSchema; // JSON Schema for function params
35
+ }
36
+
37
+ interface ToolCall {
38
+ id: string;
39
+ type: "function";
40
+ function: { name: string; arguments: string }; // arguments is JSON string
41
+ }
42
+
43
+ interface ToolResult {
44
+ tool_call_id: string;
45
+ role: "tool";
46
+ content: string; // result as string
47
+ }
48
+
49
+ // Extended message types
50
+ interface AssistantMessage {
51
+ role: "assistant";
52
+ content: string | null;
53
+ tool_calls?: ToolCall[];
54
+ }
55
+
56
+ interface ToolMessage {
57
+ role: "tool";
58
+ tool_call_id: string;
59
+ content: string;
60
+ }
61
+ ```
62
+
63
+ ### 3. Tool Registry
64
+
65
+ A simple registry where tools are registered with:
66
+ - Name
67
+ - Description (for the LLM)
68
+ - JSON Schema for parameters
69
+ - Executor function: `(params: any) => Promise<string>`
70
+
71
+ ```typescript
72
+ class ToolRegistry {
73
+ private tools: Map<string, { def: ToolDefinition; exec: (params: any) => Promise<string> }>;
74
+
75
+ register(name, description, schema, executor): void;
76
+ getDefinitions(): ToolDefinition[]; // For LLM API call
77
+ execute(name: string, params: any): Promise<string>; // Run a tool
78
+ }
79
+ ```
80
+
81
+ ### 4. The Agentic Loop (in Agent.processMessage)
82
+
83
+ ```
84
+ 1. Build messages (system + history + user)
85
+ 2. Call LLM with tools
86
+ 3. While response has tool_calls:
87
+ a. For each tool_call: execute, collect result
88
+ b. Append assistant message (with tool_calls) to messages
89
+ c. Append tool result messages
90
+ d. Call LLM again with updated messages
91
+ 4. Return final text content
92
+ 5. Store in memory (include tool usage summary)
93
+ ```
94
+
95
+ **Safety limits:**
96
+ - Max iterations per turn (e.g., 25)
97
+ - Max total tokens per turn
98
+ - Tool execution timeout (per tool)
99
+ - Dangerous command confirmation (optional)
100
+
101
+ ### 5. Phase 1 Tools
102
+
103
+ #### `shell` (exec)
104
+ - Run a shell command, return stdout/stderr
105
+ - Working directory: `~/hivemind/workspace`
106
+ - Timeout: 30s default, configurable
107
+ - Safety: no `rm -rf /` etc.
108
+
109
+ #### `read_file`
110
+ - Read file contents (with optional offset/limit for large files)
111
+ - Returns text content or error
112
+
113
+ #### `write_file`
114
+ - Write content to a file (creates dirs if needed)
115
+ - Returns success/failure
116
+
117
+ #### `edit_file`
118
+ - Find and replace exact text in a file
119
+ - oldText → newText pattern (surgical edits)
120
+
121
+ #### `web_search`
122
+ - Search via Brave API
123
+ - Returns titles, URLs, snippets
124
+
125
+ #### `web_fetch`
126
+ - Fetch URL, extract markdown
127
+ - Returns readable content
128
+
129
+ ### 6. Memory Integration
130
+
131
+ Tool calls and results should be stored in memory, but summarized:
132
+ - Don't store full file contents in L2 episodes
133
+ - Store: "Used shell to run `git status`, found 3 modified files"
134
+ - L3 promotion can learn patterns: "For git operations, agent uses shell tool"
135
+
136
+ ### 7. Config
137
+
138
+ ```toml
139
+ [tools]
140
+ enabled = true
141
+ max_iterations = 25
142
+ shell_timeout_s = 30
143
+ workspace = "workspace"
144
+
145
+ [tools.web_search]
146
+ api_key = "" # or from vault
147
+ ```
148
+
149
+ ### 8. Implementation Order
150
+
151
+ 1. **ToolRegistry class** — registration, definitions, execution
152
+ 2. **LLMClient.chatWithTools()** — extended chat that handles tool_calls
153
+ 3. **Agentic loop in Agent** — the while loop with safety limits
154
+ 4. **shell tool** — most impactful, enables everything
155
+ 5. **File tools** — read/write/edit
156
+ 6. **Web tools** — search/fetch
157
+ 7. **Memory integration** — summarize tool usage in episodes
158
+
159
+ ### 9. OpenRouter Compatibility
160
+
161
+ OpenRouter passes through tool definitions to the underlying model. Most models support tools:
162
+ - Claude: Native tool_use
163
+ - GPT-4: Native function_calling
164
+ - Gemini: Native function declarations
165
+
166
+ The OpenAI-compatible format works for all of them through OpenRouter.
167
+
168
+ ### 10. Safety Considerations
169
+
170
+ - **Sandbox**: Tools run on the agent's machine. File access should be scoped to workspace.
171
+ - **Confirmation**: Optionally require human approval for destructive operations.
172
+ - **Logging**: All tool calls logged to request logger for debugging.
173
+ - **Rate limiting**: Prevent runaway tool loops.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  startPipeline
3
- } from "./chunk-OPOXV53N.js";
3
+ } from "./chunk-LQ7CYHSQ.js";
4
4
 
5
5
  // packages/cli/src/commands/start.ts
6
6
  import { resolve } from "path";
@@ -66,4 +66,4 @@ Options:
66
66
  export {
67
67
  runStartCommand
68
68
  };
69
- //# sourceMappingURL=chunk-Z2FXPFKE.js.map
69
+ //# sourceMappingURL=chunk-HPWW7KKB.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  FleetManager
3
- } from "./chunk-KTOAREXT.js";
3
+ } from "./chunk-OTIMWYTH.js";
4
4
 
5
5
  // packages/cli/src/commands/fleet.ts
6
6
  function formatUptime(seconds) {
@@ -183,4 +183,4 @@ Commands:
183
183
  export {
184
184
  runFleetCommand
185
185
  };
186
- //# sourceMappingURL=chunk-OTEMHDRU.js.map
186
+ //# sourceMappingURL=chunk-I5DY3ULS.js.map