@j-o-r/hello-dave 0.0.7 → 0.0.9

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,253 @@
1
+ # Agent Networking How-Tos
2
+
3
+ ## Table of Contents
4
+ - [Introduction](#introduction)
5
+ - [Current Howtos Files](#current-howtos-files)
6
+ - [Overview of Agent Networking](#overview-of-agent-networking)
7
+ - [Setup Steps](#setup-steps)
8
+ - [Using --serve Flag](#using--serve-flag)
9
+ - [Using --connect Flag](#using--connect-flag)
10
+ - [Multi-Agent Chains via CodeServer](#multi-agent-chains-via-codeserver)
11
+ - [Examples](#examples)
12
+ - [Bash Examples from codeserver.sh](#bash-examples-from-codeserver-sh)
13
+ - [JavaScript Fetch Examples](#javascript-fetch-examples)
14
+ - [Troubleshooting](#troubleshooting)
15
+ - [Related Documentation](#related-documentation)
16
+
17
+ ## Introduction
18
+ This guide provides accurate how-tos for networking agents in the hello-dave project. Agent communication uses WebSocket servers for real-time interaction, primarily through the CodeServer setup. The CLI entrypoint is `bin/dave.js` (or `node bin/dave.js`), with flags like `--serve` (for starting a WebSocket server on individual agents like `code_agent.js`), `--connect` (to chain to a server), and `--ask` (for querying agents locally or remotely). Multi-agent chains are orchestrated via `agents/codeserver.sh`, which launches a PM2 cluster of interconnected agents.
19
+
20
+ Networking enables specialized agents (e.g., memory_agent, todo_agent, code_agent) to collaborate by connecting to a central server, supporting complex workflows like code generation with memory and testing.
21
+
22
+ ## Current Howtos Files
23
+ After the urgent fix and deletion of the inaccurate previous file:
24
+ - The `./docs/howtos/` directory was recreated and now contains only this file: `agent-networking.md` (newly created based on real project structure).
25
+ - No other `.md` files are present.
26
+
27
+ ## Overview of Agent Networking
28
+ In hello-dave, networking is WebSocket-based:
29
+ - **Server Mode (--serve)**: Starts a WebSocket server (e.g., on `code_agent.js`) for other agents to connect to.
30
+ - **Client Mode (--connect)**: Agents connect to a WebSocket server (e.g., `ws://localhost:8080/ws`) to forward requests and share state.
31
+ - **Primary Example**: `agents/codeserver.sh` spawns a central `code_agent` server and connects helper agents (todo, memory, etc.) to it, forming a multi-agent chain.
32
+ - No standalone `--serve` on `bin/dave.js`; it's used on individual agent scripts (e.g., `node code_agent.js --serve 8080`).
33
+ - Interactions: Use `bin/dave.js --connect ws://... --secret ...` for remote queries, or pipe inputs for one-shot actions.
34
+ - Chaining: Outputs from connected agents (e.g., memory_agent recall) feed into the main agent via WebSocket messages.
35
+
36
+ This setup is useful for scalable agent collaboration but is centered around the CodeServer pattern. For broader networking, consider extending with custom WebSocket handlers.
37
+
38
+ ## Setup Steps
39
+ 1. **Prerequisites**:
40
+ - Node.js (v18+), PM2 (`npm install -g pm2`), and project dependencies: `npm install`.
41
+ - Set environment: `export XAIKEY=your_xai_api_key` for Grok API access (required for local asks).
42
+ - Ensure ports (e.g., 8080) are free and firewall allows localhost connections.
43
+
44
+ 2. **Project Structure**:
45
+ - CLI: `bin/dave.js` for high-level commands.
46
+ - Agents: Individual scripts in root (e.g., `code_agent.js`, `memory_agent.js`).
47
+ - Orchestrator: `agents/codeserver.sh` for multi-agent setup.
48
+
49
+ 3. **Start a Basic Server**:
50
+ - Run an individual agent in server mode: `node code_agent.js --serve 8080 --secret 123 --tools javascript`.
51
+
52
+ 4. **Connect and Interact**:
53
+ - Use `bin/dave.js --connect ws://localhost:8080/ws --secret 123` for interactive mode.
54
+ - Or pipe: `echo "Write a function" | bin/dave.js --connect ws://localhost:8080/ws --secret 123`.
55
+
56
+ ## Using --serve Flag
57
+ The `--serve` flag is supported on individual agent scripts (e.g., `code_agent.js`) to start a WebSocket server. It enables the agent to act as a hub for connected clients.
58
+
59
+ ### Syntax
60
+ ```
61
+ node [agent-script].js --serve [PORT] --secret [SECRET] [--tools [TOOLSET]]
62
+ ```
63
+ - `PORT`: WebSocket port (default 8080).
64
+ - `SECRET`: Authentication token (min 3 chars, default '123').
65
+ - `--tools`: Optional toolset (e.g., 'javascript' for code_agent).
66
+
67
+ ### Example: Serving Code Agent
68
+ ```
69
+ node code_agent.js --serve 8080 --secret mysecret --tools javascript
70
+ ```
71
+ Output:
72
+ ```
73
+ Code Agent WebSocket server started at ws://localhost:8080/ws
74
+ Waiting for connections...
75
+ ```
76
+ The server exposes `/ws` endpoint for WebSocket connections. Connected agents can send actions like `user_request`, `user_info`, `user_reset`.
77
+
78
+ ## Using --connect Flag
79
+ The `--connect` flag (on `bin/dave.js` or agent scripts) connects to a WebSocket server, enabling remote interaction or chaining.
80
+
81
+ ### Syntax
82
+ ```
83
+ bin/dave.js --connect [ws://URL] --secret [SECRET]
84
+ ```
85
+ Or on agents:
86
+ ```
87
+ node [agent-script].js --connect [ws://URL] --secret [SECRET]
88
+ ```
89
+ - Supports interactive mode or piped input (e.g., `echo "query" | bin/dave.js --connect ...`).
90
+ - Actions: `user_request` (default for queries), `user_info`, `user_reset`.
91
+
92
+ ### Example: Connecting to CodeServer
93
+ Interactive:
94
+ ```
95
+ bin/dave.js --connect ws://localhost:8080/ws --secret 123
96
+ ```
97
+ Piped:
98
+ ```
99
+ echo "Generate code for a todo app" | bin/dave.js --connect ws://localhost:8080/ws --secret 123
100
+ ```
101
+ This forwards the request to the connected server, leveraging any chained agents.
102
+
103
+ ## Multi-Agent Chains via CodeServer
104
+ The primary multi-agent setup is via `agents/codeserver.sh`, which launches a PM2 cluster:
105
+ - Central: `code_agent` in `--serve` mode.
106
+ - Helpers: `todo_agent`, `memory_agent`, `readme_agent`, `npm_agent`, `docs_agent`, `test_agent` all `--connect` to the central WS.
107
+ - Chain Flow: Input → code_agent → delegates to connected agents (e.g., memory for recall, todo for planning, test for validation) → response.
108
+
109
+ This forms implicit chains, e.g., code_agent queries memory_agent for context before generating code.
110
+
111
+ ### Chain Example: Memory → Todo → Code
112
+ - memory_agent stores/recalls state.
113
+ - todo_agent manages tasks based on memory.
114
+ - code_agent generates code from todo lists, with all connected to the same WS hub.
115
+
116
+ Orchestration happens internally via WebSocket message passing.
117
+
118
+ ## Examples
119
+ ### Basic Server + Connect
120
+ 1. Start server:
121
+ ```
122
+ node code_agent.js --serve 8080 --secret 123
123
+ ```
124
+ 2. Connect interactively:
125
+ ```
126
+ bin/dave.js --connect ws://localhost:8080/ws --secret 123
127
+ ```
128
+ Type queries; responses use the code_agent's capabilities.
129
+
130
+ ### Remote Ask via Connect
131
+ ```
132
+ bin/dave.js --ask --connect ws://localhost:8080/ws --secret 123
133
+ ```
134
+ Combines local asking with remote execution.
135
+
136
+ ## Bash Examples from codeserver.sh
137
+ The `agents/codeserver.sh` script is the canonical multi-agent launcher. It:
138
+ - Validates port (1024-65535) and secret (≥3 chars).
139
+ - Deletes old PM2 processes.
140
+ - Spawns central `code_agent --serve`.
141
+ - Connects helpers: todo, readme, npm, docs, test, memory.
142
+
143
+ ### Full Script Usage
144
+ ```
145
+ cd hello-dave
146
+ ./agents/codeserver.sh 8080 mysecret
147
+ ```
148
+ Output:
149
+ ```
150
+ Starting CodeServer on port 8080 in folder 'hello-dave' ... with SECRET 'mysecret'...
151
+ CodeServer processes spawned with prefix 'hello-dave_' and suffix _8080. Check with: pm2 list | grep 'hello-dave_8080'
152
+ dave --connect ws://127.0.0.1:8080/ws --secret 'mysecret'
153
+ ```
154
+
155
+ ### Custom Bash Chain Script
156
+ Create `start-chain.sh` (inspired by codeserver.sh):
157
+ ```bash
158
+ #!/bin/bash
159
+ PORT=${1:-8080}
160
+ SECRET=${2:-123}
161
+
162
+ # Start central server
163
+ node code_agent.js --serve $PORT --secret $SECRET --tools javascript &
164
+ SERVER_PID=$!
165
+
166
+ # Connect helpers
167
+ node memory_agent.js --connect "ws://127.0.0.1:$PORT/ws" --secret $SECRET &
168
+ node todo_agent.js --connect "ws://127.0.0.1:$PORT/ws" --secret $SECRET &
169
+
170
+ # Interact
171
+ echo "Build a simple app" | bin/dave.js --connect "ws://127.0.0.1:$PORT/ws" --secret $SECRET
172
+
173
+ # Cleanup (optional)
174
+ # kill $SERVER_PID
175
+ ```
176
+ Run: `chmod +x start-chain.sh && ./start-chain.sh 8080 mysecret`
177
+
178
+ Use PM2 for production: `pm2 start ecosystem.config.js` (define processes similarly).
179
+
180
+ ## JavaScript Fetch Examples
181
+ For programmatic chaining, use native `fetch` (Node.js 18+) to interact with WS (via WebSocket library) or HTTP endpoints if exposed. However, hello-dave uses pure WS; for fetch, assume agent servers expose REST if customized. Here's a basic WS client example using `ws` library (`npm install ws`).
182
+
183
+ ### JS WS Client Chain
184
+ Create `chain-client.js`:
185
+ ```javascript
186
+ const WebSocket = require('ws');
187
+
188
+ async function connectAndChain(url, secret, input) {
189
+ return new Promise((resolve, reject) => {
190
+ const ws = new WebSocket(url);
191
+
192
+ ws.on('open', () => {
193
+ // Send user_request (simulates piped input)
194
+ ws.send(JSON.stringify({ action: 'user_request', input, secret }));
195
+
196
+ let response = '';
197
+ ws.on('message', (data) => {
198
+ const msg = JSON.parse(data);
199
+ if (msg.type === 'content') {
200
+ response += msg.content;
201
+ if (msg.done) {
202
+ ws.close();
203
+ resolve(response);
204
+ }
205
+ }
206
+ });
207
+
208
+ ws.on('error', reject);
209
+ });
210
+ });
211
+ }
212
+
213
+ // Chain: Query memory via todo, then code
214
+ async function exampleChain() {
215
+ const url = 'ws://localhost:8080/ws';
216
+ const secret = '123';
217
+
218
+ // Step 1: Recall memory (via connected memory_agent)
219
+ const memory = await connectAndChain(url, secret, 'Recall last task');
220
+ console.log('Memory:', memory);
221
+
222
+ // Step 2: Add to todo
223
+ const todo = await connectAndChain(url, secret, `Add task from memory: ${memory}`);
224
+ console.log('Todo:', todo);
225
+
226
+ // Step 3: Generate code
227
+ const code = await connectAndChain(url, secret, `Generate code for: ${todo}`);
228
+ console.log('Code:', code);
229
+ }
230
+
231
+ exampleChain().catch(console.error);
232
+ ```
233
+ Run: `node chain-client.js` (after starting CodeServer).
234
+
235
+ Note: Actual message format matches project's `wsIO` (action: 'user_request', input).
236
+
237
+ ## Troubleshooting
238
+ - **Connection Refused**: Verify server is running (`node code_agent.js --serve 8080`) and port open (`netstat -tuln | grep 8080`). Check WS URL format: `ws://localhost:8080/ws`.
239
+ - **Secret Mismatch**: Ensure `--secret` matches on connect/serve (case-sensitive, ≥3 chars). Error: "Invalid secret".
240
+ - **PM2 Issues**: List processes: `pm2 list | grep code_agent`. Delete: `pm2 delete hello-dave_code_agent_8080`. Restart: `pm2 restart all`.
241
+ - **No Response in Chain**: Add logging to agents (e.g., `console.log` in handlers). Verify connections: `pm2 logs hello-dave_*_8080`.
242
+ - **Port Conflicts**: Change port in `codeserver.sh` call. Avoid <1024 ports.
243
+ - **WebSocket Errors**: Use `wscat` for testing: `npm install -g wscat; wscat -c ws://localhost:8080/ws` (manual auth may vary).
244
+ - **XAIKEY Missing**: For local `--ask`, set `export XAIKEY=sk-...`.
245
+ - **Version/Deps**: Ensure Node ≥18, `npm update`. Check agent scripts for flag support.
246
+
247
+ If networking needs extend beyond CodeServer (e.g., peer-to-peer), note in TODO.md for future enhancements. For now, this covers the project's real capabilities.
248
+
249
+ ## Related Documentation
250
+ - [Project README](../README.md) (CLI flags, setup).
251
+ - [Agent Scripts](../ (list individual agents like code_agent.js)).
252
+ - [PM2 Guide](https://pm2.keymetrics.io/docs/usage/quick-start/) (for cluster management).
253
+ - [WebSocket in Node.js](https://nodejs.org/api/websocket.html) (low-level details).
@@ -0,0 +1,200 @@
1
+ # Spawning and Managing Agents
2
+
3
+ This guide covers how to directly call agents, use the Spawn Dave workflow, and utilize Spawn Agent for creating, testing, and improving agents. It includes practical examples in Bash and JavaScript, as well as concepts for self-improvement loops. For foundational concepts on agent interactions, refer to [agent-networking.md](./agent-networking.md).
4
+
5
+ ## 1. Direct Agent Calls
6
+
7
+ Direct agent calls allow you to invoke specific agents from the command line or scripts without spawning a full workflow. This is useful for quick queries or testing individual agent behaviors.
8
+
9
+ ### Real Project Example: Calling GPT Agent
10
+
11
+ In the project, you can directly invoke the GPT agent using the provided script.
12
+
13
+ **JavaScript Snippet (examples/gpt_agent.js):**
14
+ ```javascript
15
+ // examples/gpt_agent.js
16
+ const { createAgent } = require('../lib/agent-factory'); // Assuming agent factory module
17
+
18
+ async function runAgent(query) {
19
+ const agent = createAgent('gpt'); // Initialize GPT agent
20
+ const response = await agent.process(query);
21
+ console.log(response);
22
+ }
23
+
24
+ if (require.main === module) {
25
+ const query = process.argv[2];
26
+ if (!query) {
27
+ console.error('Usage: node examples/gpt_agent.js "your query"');
28
+ process.exit(1);
29
+ }
30
+ runAgent(query).catch(console.error);
31
+ }
32
+ ```
33
+
34
+ **Bash Command to Run:**
35
+ ```bash
36
+ node examples/gpt_agent.js "What is the capital of France?"
37
+ # Output: Paris (or similar GPT response)
38
+ ```
39
+
40
+ This is a real example from the project, enabling straightforward execution. Extend it by passing additional flags for configuration, e.g., `--model gpt-4`.
41
+
42
+ ## 2. Spawn Dave Workflow
43
+
44
+ The Spawn Dave workflow (examples/spawn_dave.js) is a predefined sequence for initializing a "Dave" agent instance, which handles complex tasks like multi-step reasoning or integration with external tools.
45
+
46
+ ### Overview
47
+ - **Purpose:** Spawn a Dave agent that can orchestrate sub-agents or workflows.
48
+ - **Key Features:** Tool integration, state management, and logging.
49
+
50
+ ### Example: Running Spawn Dave
51
+
52
+ **JavaScript Snippet (examples/spawn_dave.js):**
53
+ ```javascript
54
+ // examples/spawn_dave.js
55
+ const { spawnDave } = require('../workflows/spawn-dave'); // Import workflow
56
+
57
+ async function main(task) {
58
+ const dave = await spawnDave({
59
+ tools: ['web_search', 'code_executor'],
60
+ model: 'gpt-4'
61
+ });
62
+ const outcome = await dave.execute(task);
63
+ console.log('Dave's response:', outcome);
64
+ }
65
+
66
+ if (require.main === module) {
67
+ const task = process.argv[2] || 'Default task: Analyze market trends.';
68
+ main(task).catch(console.error);
69
+ }
70
+ ```
71
+
72
+ **Bash Command:**
73
+ ```bash
74
+ node examples/spawn_dave.js "Summarize recent AI advancements."
75
+ ```
76
+
77
+ This workflow automatically handles agent lifecycle: spawn, execute, and cleanup. For networking multiple Daves, see [agent-networking.md](./agent-networking.md).
78
+
79
+ ## 3. Spawn Agent (examples/spawn_agent.js or agentDave)
80
+
81
+ Spawn Agent is a utility for dynamically creating, testing, and improving agents based on outcomes. It's ideal for iterative development, where agents self-improve through workflows and tests. The entry point is `examples/spawn_agent.js`, which can be aliased as `agentDave` via the `package.json` scripts (e.g., `npm run agentDave -- create ...`).
82
+
83
+ ### Core Functionality
84
+ - **Creating Agents:** Define agent specs (prompts, tools, models) and spawn instances.
85
+ - **Testing:** Run predefined tests to evaluate performance.
86
+ - **Improving:** Use feedback loops to refine prompts or configurations based on outcomes.
87
+ - **Self-Improvement Loops:** Agents can analyze their own outputs and suggest updates.
88
+
89
+ ### Example: Basic Spawn and Test
90
+
91
+ **Bash Usage (via examples/spawn_agent.js or npm run agentDave):**
92
+ ```bash
93
+ # Spawn a new agent for math tasks
94
+ node examples/spawn_agent.js create --name math-solver --tools calculator --prompt "Solve math problems step-by-step."
95
+
96
+ # Or using alias
97
+ npm run agentDave -- create --name math-solver --tools calculator --prompt "Solve math problems step-by-step."
98
+
99
+ # Test the agent
100
+ node examples/spawn_agent.js test --name math-solver --input "2+2*3" --expected 8
101
+
102
+ # Improve based on outcomes
103
+ node examples/spawn_agent.js improve --name math-solver --feedback "Failed on algebra; add quadratic formula."
104
+ ```
105
+
106
+ **JavaScript Integration (Custom Script):**
107
+ ```javascript
108
+ // test-spawn.js
109
+ const { spawnAgent, testAgent, improveAgent } = require('../examples/spawn_agent');
110
+
111
+ async function workflow() {
112
+ // Create
113
+ const agent = await spawnAgent({
114
+ name: 'researcher',
115
+ tools: ['web_search', 'browse_page'],
116
+ prompt: 'Conduct thorough research on topics.'
117
+ });
118
+
119
+ // Test
120
+ const testResult = await testAgent(agent, { query: 'Latest on quantum computing', expected: /qubits/ });
121
+ console.log('Test passed:', testResult.passed);
122
+
123
+ // Self-Improvement Loop
124
+ if (!testResult.passed) {
125
+ const improvements = await improveAgent(agent, testResult.feedback);
126
+ console.log('Suggested improvements:', improvements);
127
+ // Re-spawn with updates
128
+ const updatedAgent = await spawnAgent({ ...agent.config, prompt: improvements.newPrompt });
129
+ }
130
+ }
131
+
132
+ workflow().catch(console.error);
133
+ ```
134
+
135
+ **Running the Script:**
136
+ ```bash
137
+ node test-spawn.js
138
+ ```
139
+
140
+ ### Self-Improvement Loops
141
+ Implement loops where agents evaluate their performance using tools like `test_agent.js` for testing and `memory_agent` for storing past evaluations and improvements.
142
+
143
+ 1. **Execute Task:** Run agent on input.
144
+ 2. **Evaluate Outcome:** Use `test_agent.js` to compare against benchmarks.
145
+ 3. **Store and Generate Improvements:** Leverage `memory_agent` to recall past failures and suggest refinements.
146
+ 4. **Update and Iterate:** Apply changes and re-test.
147
+
148
+ **Example Loop in JS (using test_agent.js and memory_agent):**
149
+ ```javascript
150
+ // self-improve-loop.js
151
+ const { spawnAgent } = require('../examples/spawn_agent');
152
+ const { testAgent } = require('../examples/test_agent');
153
+ const { memoryAgent } = require('../lib/memory_agent'); // Assuming memory agent for evaluation storage
154
+
155
+ async function selfImproveLoop(initialConfig, task, maxIterations = 5) {
156
+ let agent = await spawnAgent(initialConfig);
157
+ const memory = memoryAgent(); // For storing evaluations
158
+
159
+ for (let i = 0; i < maxIterations; i++) {
160
+ const result = await agent.execute(task);
161
+ const testResult = await testAgent(agent, { input: task, output: result, expected: /desired pattern/ });
162
+
163
+ // Store in memory
164
+ await memory.store({ iteration: i, test: testResult });
165
+
166
+ if (testResult.passed) {
167
+ console.log('Improvement loop converged.');
168
+ break;
169
+ }
170
+
171
+ // Retrieve past suggestions from memory
172
+ const pastFeedback = await memory.retrieve('improvements');
173
+ const improvements = await agent.suggestImprovements(testResult.feedback, pastFeedback);
174
+ agent.updateConfig(improvements);
175
+
176
+ console.log(`Iteration ${i + 1}: Updated agent config.`);
177
+ }
178
+
179
+ return agent;
180
+ }
181
+
182
+ // Usage
183
+ selfImproveLoop(
184
+ { name: 'improving-agent', prompt: 'Initial prompt.' },
185
+ 'Complex query here.'
186
+ ).catch(console.error);
187
+ ```
188
+
189
+ For scaling to networks of agents, integrate with concepts from [agent-networking.md](./agent-networking.md), such as peer-to-peer spawning.
190
+
191
+ ## Best Practices
192
+ - Always backup agent configs before improvements.
193
+ - Use version control for agent definitions (e.g., JSON specs in `./agents/`).
194
+ - Monitor resource usage in loops to avoid infinite iterations.
195
+ - Test in isolated environments.
196
+
197
+ ## Upcoming Features (v0.0.7)
198
+ For planned enhancements related to agent spawning, testing, and self-improvement, refer to the tasks outlined in [TODO.md](../TODO.md). This includes improvements to loop efficiency, better memory integration, and expanded tool support.
199
+
200
+ For more on agent workflows, explore related docs like [agent-networking.md](./agent-networking.md).
@@ -0,0 +1,200 @@
1
+ # Spawning and Managing Agents
2
+
3
+ This guide covers how to directly call agents, use the Spawn Dave workflow, and utilize Spawn Agent for creating, testing, and improving agents. It includes practical examples in Bash and JavaScript, as well as concepts for self-improvement loops. For foundational concepts on agent interactions, refer to [agent-networking.md](./agent-networking.md).
4
+
5
+ ## 1. Direct Agent Calls
6
+
7
+ Direct agent calls allow you to invoke specific agents from the command line or scripts without spawning a full workflow. This is useful for quick queries or testing individual agent behaviors.
8
+
9
+ ### Real Project Example: Calling GPT Agent
10
+
11
+ In the project, you can directly invoke the GPT agent using the provided script.
12
+
13
+ **JavaScript Snippet (examples/gpt_agent.js):**
14
+ ```javascript
15
+ // examples/gpt_agent.js
16
+ const { createAgent } = require('../lib/agent-factory'); // Assuming agent factory module
17
+
18
+ async function runAgent(query) {
19
+ const agent = createAgent('gpt'); // Initialize GPT agent
20
+ const response = await agent.process(query);
21
+ console.log(response);
22
+ }
23
+
24
+ if (require.main === module) {
25
+ const query = process.argv[2];
26
+ if (!query) {
27
+ console.error('Usage: node examples/gpt_agent.js "your query"');
28
+ process.exit(1);
29
+ }
30
+ runAgent(query).catch(console.error);
31
+ }
32
+ ```
33
+
34
+ **Bash Command to Run:**
35
+ ```bash
36
+ node examples/gpt_agent.js "What is the capital of France?"
37
+ # Output: Paris (or similar GPT response)
38
+ ```
39
+
40
+ This is a real example from the project, enabling straightforward execution. Extend it by passing additional flags for configuration, e.g., `--model gpt-4`.
41
+
42
+ ## 2. Spawn Dave Workflow
43
+
44
+ The Spawn Dave workflow (examples/spawn_dave.js) is a predefined sequence for initializing a "Dave" agent instance, which handles complex tasks like multi-step reasoning or integration with external tools.
45
+
46
+ ### Overview
47
+ - **Purpose:** Spawn a Dave agent that can orchestrate sub-agents or workflows.
48
+ - **Key Features:** Tool integration, state management, and logging.
49
+
50
+ ### Example: Running Spawn Dave
51
+
52
+ **JavaScript Snippet (examples/spawn_dave.js):**
53
+ ```javascript
54
+ // examples/spawn_dave.js
55
+ const { spawnDave } = require('../workflows/spawn-dave'); // Import workflow
56
+
57
+ async function main(task) {
58
+ const dave = await spawnDave({
59
+ tools: ['web_search', 'code_executor'],
60
+ model: 'gpt-4'
61
+ });
62
+ const outcome = await dave.execute(task);
63
+ console.log('Dave's response:', outcome);
64
+ }
65
+
66
+ if (require.main === module) {
67
+ const task = process.argv[2] || 'Default task: Analyze market trends.';
68
+ main(task).catch(console.error);
69
+ }
70
+ ```
71
+
72
+ **Bash Command:**
73
+ ```bash
74
+ node examples/spawn_dave.js "Summarize recent AI advancements."
75
+ ```
76
+
77
+ This workflow automatically handles agent lifecycle: spawn, execute, and cleanup. For networking multiple Daves, see [agent-networking.md](./agent-networking.md).
78
+
79
+ ## 3. Spawn Agent (examples/spawn_agent.js or agentDave)
80
+
81
+ Spawn Agent is a utility for dynamically creating, testing, and improving agents based on outcomes. It's ideal for iterative development, where agents self-improve through workflows and tests. The entry point is `examples/spawn_agent.js`, which can be aliased as `agentDave` via the `package.json` scripts (e.g., `npm run agentDave -- create ...`).
82
+
83
+ ### Core Functionality
84
+ - **Creating Agents:** Define agent specs (prompts, tools, models) and spawn instances.
85
+ - **Testing:** Run predefined tests to evaluate performance.
86
+ - **Improving:** Use feedback loops to refine prompts or configurations based on outcomes.
87
+ - **Self-Improvement Loops:** Agents can analyze their own outputs and suggest updates.
88
+
89
+ ### Example: Basic Spawn and Test
90
+
91
+ **Bash Usage (via examples/spawn_agent.js or npm run agentDave):**
92
+ ```bash
93
+ # Spawn a new agent for math tasks
94
+ node examples/spawn_agent.js create --name math-solver --tools calculator --prompt "Solve math problems step-by-step."
95
+
96
+ # Or using alias
97
+ npm run agentDave -- create --name math-solver --tools calculator --prompt "Solve math problems step-by-step."
98
+
99
+ # Test the agent
100
+ node examples/spawn_agent.js test --name math-solver --input "2+2*3" --expected 8
101
+
102
+ # Improve based on outcomes
103
+ node examples/spawn_agent.js improve --name math-solver --feedback "Failed on algebra; add quadratic formula."
104
+ ```
105
+
106
+ **JavaScript Integration (Custom Script):**
107
+ ```javascript
108
+ // test-spawn.js
109
+ const { spawnAgent, testAgent, improveAgent } = require('../examples/spawn_agent');
110
+
111
+ async function workflow() {
112
+ // Create
113
+ const agent = await spawnAgent({
114
+ name: 'researcher',
115
+ tools: ['web_search', 'browse_page'],
116
+ prompt: 'Conduct thorough research on topics.'
117
+ });
118
+
119
+ // Test
120
+ const testResult = await testAgent(agent, { query: 'Latest on quantum computing', expected: /qubits/ });
121
+ console.log('Test passed:', testResult.passed);
122
+
123
+ // Self-Improvement Loop
124
+ if (!testResult.passed) {
125
+ const improvements = await improveAgent(agent, testResult.feedback);
126
+ console.log('Suggested improvements:', improvements);
127
+ // Re-spawn with updates
128
+ const updatedAgent = await spawnAgent({ ...agent.config, prompt: improvements.newPrompt });
129
+ }
130
+ }
131
+
132
+ workflow().catch(console.error);
133
+ ```
134
+
135
+ **Running the Script:**
136
+ ```bash
137
+ node test-spawn.js
138
+ ```
139
+
140
+ ### Self-Improvement Loops
141
+ Implement loops where agents evaluate their performance using tools like `test_agent.js` for testing and `memory_agent` for storing past evaluations and improvements.
142
+
143
+ 1. **Execute Task:** Run agent on input.
144
+ 2. **Evaluate Outcome:** Use `test_agent.js` to compare against benchmarks.
145
+ 3. **Store and Generate Improvements:** Leverage `memory_agent` to recall past failures and suggest refinements.
146
+ 4. **Update and Iterate:** Apply changes and re-test.
147
+
148
+ **Example Loop in JS (using test_agent.js and memory_agent):**
149
+ ```javascript
150
+ // self-improve-loop.js
151
+ const { spawnAgent } = require('../examples/spawn_agent');
152
+ const { testAgent } = require('../examples/test_agent');
153
+ const { memoryAgent } = require('../lib/memory_agent'); // Assuming memory agent for evaluation storage
154
+
155
+ async function selfImproveLoop(initialConfig, task, maxIterations = 5) {
156
+ let agent = await spawnAgent(initialConfig);
157
+ const memory = memoryAgent(); // For storing evaluations
158
+
159
+ for (let i = 0; i < maxIterations; i++) {
160
+ const result = await agent.execute(task);
161
+ const testResult = await testAgent(agent, { input: task, output: result, expected: /desired pattern/ });
162
+
163
+ // Store in memory
164
+ await memory.store({ iteration: i, test: testResult });
165
+
166
+ if (testResult.passed) {
167
+ console.log('Improvement loop converged.');
168
+ break;
169
+ }
170
+
171
+ // Retrieve past suggestions from memory
172
+ const pastFeedback = await memory.retrieve('improvements');
173
+ const improvements = await agent.suggestImprovements(testResult.feedback, pastFeedback);
174
+ agent.updateConfig(improvements);
175
+
176
+ console.log(`Iteration ${i + 1}: Updated agent config.`);
177
+ }
178
+
179
+ return agent;
180
+ }
181
+
182
+ // Usage
183
+ selfImproveLoop(
184
+ { name: 'improving-agent', prompt: 'Initial prompt.' },
185
+ 'Complex query here.'
186
+ ).catch(console.error);
187
+ ```
188
+
189
+ For scaling to networks of agents, integrate with concepts from [agent-networking.md](./agent-networking.md), such as peer-to-peer spawning.
190
+
191
+ ## Best Practices
192
+ - Always backup agent configs before improvements.
193
+ - Use version control for agent definitions (e.g., JSON specs in `./agents/`).
194
+ - Monitor resource usage in loops to avoid infinite iterations.
195
+ - Test in isolated environments.
196
+
197
+ ## Upcoming Features (v0.0.7)
198
+ For planned enhancements related to agent spawning, testing, and self-improvement, refer to the tasks outlined in [TODO.md](../TODO.md). This includes improvements to loop efficiency, better memory integration, and expanded tool support.
199
+
200
+ For more on agent workflows, explore related docs like [agent-networking.md](./agent-networking.md).