@sesamespace/hivemind 0.5.17 → 0.6.1
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/TOOL-USE-DESIGN.md +173 -0
- package/dist/{chunk-OPOXV53N.js → chunk-7YHRRM5B.js} +622 -218
- package/dist/chunk-7YHRRM5B.js.map +1 -0
- package/dist/{chunk-KTOAREXT.js → chunk-D3P3TJX4.js} +2 -2
- package/dist/{chunk-OTEMHDRU.js → chunk-EL4FSJBK.js} +2 -2
- package/dist/{chunk-Z2FXPFKE.js → chunk-F4C7TIEX.js} +2 -2
- package/dist/{chunk-2PCF2ADI.js → chunk-HCA2NYWH.js} +3 -3
- package/dist/commands/fleet.js +3 -3
- package/dist/commands/start.js +3 -3
- package/dist/commands/watchdog.js +3 -3
- package/dist/index.js +2 -2
- package/dist/main.js +5 -5
- package/dist/start.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-OPOXV53N.js.map +0 -1
- /package/dist/{chunk-KTOAREXT.js.map → chunk-D3P3TJX4.js.map} +0 -0
- /package/dist/{chunk-OTEMHDRU.js.map → chunk-EL4FSJBK.js.map} +0 -0
- /package/dist/{chunk-Z2FXPFKE.js.map → chunk-F4C7TIEX.js.map} +0 -0
- /package/dist/{chunk-2PCF2ADI.js.map → chunk-HCA2NYWH.js.map} +0 -0
|
@@ -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.
|