@j-o-r/hello-dave 0.1.0 → 0.1.4

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 (173) hide show
  1. package/CHANGELOG.md +42 -25
  2. package/README.md +81 -221
  3. package/TODO.md +173 -35
  4. package/agents/agent_creator.js +105 -0
  5. package/agents/agent_creator.prompt.md +371 -0
  6. package/agents/ask_agent.js +64 -127
  7. package/agents/claude_agent.js +68 -0
  8. package/agents/code_agent.js +55 -135
  9. package/agents/code_agent.prompt.md +50 -0
  10. package/agents/echo_agent.js +76 -0
  11. package/agents/financial_expert.js +75 -0
  12. package/agents/gpt_agent.js +52 -103
  13. package/agents/gpt_code.js +81 -0
  14. package/agents/grok_agent.js +58 -114
  15. package/agents/minimax_agent.js +92 -0
  16. package/agents/mureka_agent.js +77 -0
  17. package/agents/planner_agent.js +172 -0
  18. package/agents/stability_agent.js +87 -0
  19. package/agents/test_agent.js +75 -157
  20. package/agents/weather_agent.js +73 -0
  21. package/agents/workflow_agent.js +189 -0
  22. package/bin/dave.js +436 -184
  23. package/docs/bin-dave.md +85 -35
  24. package/docs/cdn-ssh.md +100 -0
  25. package/docs/creating-agents.md +301 -0
  26. package/docs/creating-toolsets.md +336 -0
  27. package/docs/docs-organization.md +48 -0
  28. package/docs/project-overview.md +86 -51
  29. package/lib/API/elevenlabs.io/music.compose.md +441 -0
  30. package/lib/API/elevenlabs.io/music.create-composition-plan.md +370 -0
  31. package/lib/API/elevenlabs.io/music.stream.md +425 -0
  32. package/lib/API/lalal.ai/lalal.js +445 -0
  33. package/lib/API/lalal.ai/openapi.json +2614 -0
  34. package/lib/API/minimax/ImageToolset.js +82 -37
  35. package/lib/API/minimax/MusicToolset.js +125 -79
  36. package/lib/API/minimax/VideoToolset.js +170 -167
  37. package/lib/API/minimax/image.js +5 -1
  38. package/lib/API/minimax/music.js +210 -23
  39. package/lib/API/minimax/video.js +242 -53
  40. package/lib/API/mureka/MusicToolset.js +646 -0
  41. package/lib/API/mureka/README.md +41 -0
  42. package/lib/API/mureka/index.js +7 -0
  43. package/lib/API/mureka/music.js +658 -0
  44. package/lib/API/openai.com/index.js +7 -0
  45. package/lib/API/openai.com/{reponses/text.js → responses.js} +64 -18
  46. package/lib/API/openai.com/video.create.character.md +40 -0
  47. package/lib/API/openai.com/video.create.md +219 -0
  48. package/lib/API/openai.com/video.delete.md +44 -0
  49. package/lib/API/openai.com/video.download.md +31 -0
  50. package/lib/API/openai.com/video.edit.md +155 -0
  51. package/lib/API/openai.com/video.extend.md +166 -0
  52. package/lib/API/openai.com/video.fetch.character.md +43 -0
  53. package/lib/API/openai.com/video.js +784 -0
  54. package/lib/API/openai.com/video.list.md +201 -0
  55. package/lib/API/openai.com/video.remix.md +175 -0
  56. package/lib/API/openai.com/video.retrieve.md +139 -0
  57. package/lib/API/openai.com/videoToolset.js +616 -0
  58. package/lib/API/stability.ai/ImageToolset.js +131 -40
  59. package/lib/API/stability.ai/MusicToolset.js +79 -47
  60. package/lib/API/stability.ai/audio.js +63 -131
  61. package/lib/API/x.ai/chat.responses.md +1040 -0
  62. package/lib/API/x.ai/image.js +229 -59
  63. package/lib/API/x.ai/imageToolset.js +376 -0
  64. package/lib/API/x.ai/index.js +1 -3
  65. package/lib/API/x.ai/responses.js +9 -18
  66. package/lib/Agent.js +271 -0
  67. package/lib/Agent.js.old +284 -0
  68. package/lib/AgentLauncher.js +562 -0
  69. package/lib/Cli.js +87 -13
  70. package/lib/Prompt.js +23 -1
  71. package/lib/Session.js +5 -4
  72. package/lib/ToolSet.js +102 -6
  73. package/lib/agentLoader.js +369 -0
  74. package/lib/cdn.js +67 -231
  75. package/lib/{CdnToolset.js → cdnToolset.js} +47 -64
  76. package/lib/defaultToolsets.js +43 -0
  77. package/lib/fafs.js +1 -1
  78. package/lib/genericToolset.js +442 -119
  79. package/lib/handOffToolset.js +179 -0
  80. package/lib/index.js +34 -27
  81. package/lib/toolsetLoader.js +248 -0
  82. package/package.json +11 -5
  83. package/types/API/lalal.ai/lalal.d.ts +116 -0
  84. package/types/API/minimax/image.d.ts +2 -1
  85. package/types/API/minimax/music.d.ts +189 -26
  86. package/types/API/minimax/video.d.ts +100 -31
  87. package/types/API/mureka/index.d.ts +7 -0
  88. package/types/API/mureka/music.d.ts +472 -0
  89. package/types/API/openai.com/index.d.ts +7 -0
  90. package/types/API/openai.com/{reponses/text.d.ts → responses.d.ts} +11 -11
  91. package/types/API/openai.com/video.d.ts +409 -0
  92. package/types/API/openai.com/videoToolset.d.ts +24 -0
  93. package/types/API/stability.ai/audio.d.ts +14 -103
  94. package/types/API/stability.ai/image.d.ts +2 -2
  95. package/types/API/x.ai/image.d.ts +138 -26
  96. package/types/API/x.ai/imageToolset.d.ts +3 -0
  97. package/types/API/x.ai/index.d.ts +1 -3
  98. package/types/API/x.ai/responses.d.ts +4 -4
  99. package/types/Agent.d.ts +123 -0
  100. package/types/AgentLauncher.d.ts +222 -0
  101. package/types/Cli.d.ts +28 -8
  102. package/types/Prompt.d.ts +23 -5
  103. package/types/Session.d.ts +1 -1
  104. package/types/ToolSet.d.ts +10 -0
  105. package/types/agentLoader.d.ts +78 -0
  106. package/types/cdn.d.ts +15 -90
  107. package/types/defaultToolsets.d.ts +9 -0
  108. package/types/fafs.d.ts +1 -1
  109. package/types/genericToolset.d.ts +1 -1
  110. package/types/handOffToolset.d.ts +28 -0
  111. package/types/index.d.ts +19 -16
  112. package/types/toolsetLoader.d.ts +114 -0
  113. package/utils/format_log.js +101 -23
  114. package/utils/launch_agent.js +18 -0
  115. package/utils/list_sessions.sh +13 -5
  116. package/utils/search_sessions.sh +65 -29
  117. package/utils/toolsets.js +33 -0
  118. package/README.md.bak.1779452127 +0 -240
  119. package/agents/codeserver.sh +0 -47
  120. package/agents/daisy_agent.js +0 -173
  121. package/agents/docs_agent.js +0 -148
  122. package/agents/memory_agent.js +0 -263
  123. package/agents/minimax.js +0 -173
  124. package/agents/npm_agent.js +0 -202
  125. package/agents/prompt_agent.js +0 -133
  126. package/agents/readme_agent.js +0 -148
  127. package/agents/spawn_agent.js +0 -160
  128. package/agents/stability.js +0 -173
  129. package/agents/todo_agent.js +0 -175
  130. package/bin/codeDave +0 -58
  131. package/docs/agent-dave-websocket-protocol.md +0 -180
  132. package/docs/agent-manager.md +0 -244
  133. package/docs/codeserver-pattern.md +0 -191
  134. package/docs/generic-toolset.md +0 -326
  135. package/docs/howtos/agent-networking.md +0 -253
  136. package/docs/howtos/spawn-agents.md.bak +0 -200
  137. package/docs/howtos/spawn-agents.md.bak_new +0 -200
  138. package/docs/multi-agent-clusters.md +0 -265
  139. package/docs/music-toolsets.md +0 -137
  140. package/docs/path-resolution-best-practices.md +0 -104
  141. package/docs/plans/minimax-music-generation.md +0 -80
  142. package/docs/plans/unified-agent-architecture.md +0 -146
  143. package/docs/plans/websocket-streaming-plan.md.bak +0 -317
  144. package/docs/prompt/spawn_agent.md +0 -175
  145. package/docs/prompt/spawn_agent.md.bak +0 -201
  146. package/docs/prompt/task_clarification_and_documentation.md +0 -35
  147. package/docs/prompt-class.md +0 -141
  148. package/docs/todo-archive-infra-2026-04-21.md +0 -15
  149. package/docs/todo-archive-v0.0.8.md +0 -1
  150. package/docs/todo-archive-v0.1.0.md +0 -32
  151. package/docs/todo-archive.md +0 -44
  152. package/docs/tools-syntax-validation.md +0 -121
  153. package/docs/toolset.md +0 -164
  154. package/docs/xai-responses.md +0 -111
  155. package/docs/xai_collections.md +0 -106
  156. package/lib/API/x.ai/ImageToolset.js +0 -165
  157. package/lib/API/x.ai/text.js +0 -415
  158. package/lib/AgentClient.js +0 -248
  159. package/lib/AgentManager.js +0 -245
  160. package/lib/AgentServer.js +0 -404
  161. package/lib/wsCli.js +0 -287
  162. package/lib/wsIO.js +0 -90
  163. package/types/API/x.ai/text.d.ts +0 -286
  164. package/types/AgentClient.d.ts +0 -109
  165. package/types/AgentManager.d.ts +0 -100
  166. package/types/AgentServer.d.ts +0 -89
  167. package/types/wsCli.d.ts +0 -17
  168. package/types/wsIO.d.ts +0 -30
  169. package/utils/test.sh +0 -46
  170. /package/docs/{suggestions.md → _notes/token-counts.md} +0 -0
  171. /package/lib/API/openai.com/{reponses/MESSAGES.md → MESSAGES.md} +0 -0
  172. /package/types/API/{x.ai/ImageToolset.d.ts → mureka/MusicToolset.d.ts} +0 -0
  173. /package/types/{CdnToolset.d.ts → cdnToolset.d.ts} +0 -0
@@ -1,180 +0,0 @@
1
- # Agent Dave WebSocket Protocol
2
-
3
- **Version**: 0.0.8 (as of April 2026)
4
- **Repository**: https://codeberg.org/duin/hello-dave
5
- **Core Files**:
6
- - `lib/AgentServer.js` — Central hub, registers agents as dynamic tools, handles user interactions and sessions.
7
- - `lib/AgentClient.js` — Agent-side wrapper (Prompt + ToolSet), queue-based message processor, auto-reconnect, epoch-based reset handling.
8
- - `lib/wsIO.js` — One-shot WS client for CLI tools (`dave --connect`, `wsio()` helper).
9
-
10
- ## Overview
11
-
12
- The protocol enables **multi-agent collaboration** over WebSocket.
13
-
14
- - A central **AgentServer** listens on `ws://127.0.0.1:<port>/ws`.
15
- - **AgentClient** instances connect, introduce themselves, and get registered as **dynamic tools** in the server's `ToolSet`.
16
- - The server exposes these agents to the main Prompt, allowing the LLM to call them like any other tool (`agent_query` / `agent_response`).
17
- - **User/CLI interaction** uses `user_*` actions (via `wsIO.js` or `dave` CLI).
18
- - All messages are **JSON** objects with an `action` field validated against a fixed list.
19
- - **Authentication**: `?wssrc_id=<secret>` query param (plain in AgentServer, base64 in wsIO.js).
20
- - **Reset handling**: Broadcast `reset`, clients use an `#epoch` counter to invalidate in-flight work.
21
- - **Response matching**: Uses `id` (timestamp-based) + pending map on server.
22
-
23
- This design turns specialized agents (code, todo, memory, readme, etc.) into callable tools while supporting direct CLI, interactive REPL, and one-shot queries.
24
-
25
- ## Message Format
26
-
27
- Every message is a JSON object:
28
-
29
- ```json
30
- {
31
- "action": "one_of_the_actions_below",
32
- "content": "string or object (varies)",
33
- "id": "unique_timestamp_string_or_number",
34
- "name"?: "agent_name_on_introduction"
35
- }
36
- ```
37
-
38
- ### Supported Actions (ACTIONS array in AgentServer.js)
39
-
40
- **Agent ↔ Server (Tool Calls)**
41
- - `agent_introduction` — Agent registers itself (sends `name` + `description`).
42
- - `agent_query` — Server calls an agent tool with natural language `content` (query).
43
- - `agent_response` — Agent returns result.
44
- - `agent_error` — Agent reports failure.
45
-
46
- **User/CLI ↔ Server**
47
- - `user_introduction` — CLI connects (sent by wsIO.js).
48
- - `user_request` — User query to main prompt.
49
- - `server_response` — Final answer from main prompt.
50
- - `server_error` — Error from main prompt.
51
- - `user_reset` — Reset current session.
52
- - `user_sessionlist` — Request list of saved sessions.
53
- - `user_loadsession` — Load a previous session.
54
- - `user_info` — Get server/prompt/session info.
55
-
56
- **Server → Clients (broadcasts/responses)**
57
- - `reset` — Broadcast to all agents on reset.
58
- - `server_reset`, `server_sessionlist`, `server_sessionloaded`, `server_info` — Responses to user actions.
59
-
60
- Unknown actions cause immediate client disconnection.
61
-
62
- ## Key Interactions & Sequence Diagrams
63
-
64
- ### 1. Agent Registration & Tool Call
65
-
66
- ```mermaid
67
- sequenceDiagram
68
- participant AgentClient
69
- participant AgentServer
70
- participant Prompt/ToolSet
71
-
72
- AgentClient->>AgentServer: WS connect + ?wssrc_id=secret
73
- AgentClient->>AgentServer: {action: "agent_introduction", name: "code", content: "Code execution agent..."}
74
- AgentServer->>ToolSet: add("code", description, schema, handler)
75
- Note over ToolSet,AgentServer: Tool now available to LLM
76
-
77
- Prompt->>ToolSet: LLM decides to call "code" with query
78
- ToolSet->>AgentServer: handler(query) → Promise
79
- AgentServer->>AgentClient: {action: "agent_query", id: "1745...", content: "Write a bash script..."}
80
- AgentClient->>AgentClient: #queue.push(), #processOne()
81
- AgentClient->>Prompt: prompt.call(query)
82
- AgentClient->>AgentServer: {action: "agent_response", id: "1745...", content: "```bash ...```"}
83
- AgentServer->>Prompt: resolve(promise)
84
- Prompt->>LLM: Continue with tool result
85
- ```
86
-
87
- ### 2. User One-Shot Query (via wsIO.js or dave --connect)
88
-
89
- ```mermaid
90
- sequenceDiagram
91
- participant CLI (wsIO)
92
- participant AgentServer
93
- participant Prompt
94
-
95
- CLI->>AgentServer: WS connect + wssrc_id
96
- CLI->>AgentServer: {action: "user_introduction", id: X}
97
- CLI->>AgentServer: {action: "user_request", id: Y, content: "Hello Dave"}
98
- AgentServer->>Prompt: prompt.call("Hello Dave")
99
- Prompt->>LLM: Full reasoning + tool calls (may call registered agents)
100
- Prompt-->>AgentServer: Final content
101
- AgentServer->>CLI: {action: "server_response", id: Y, content: "..."}
102
- CLI->>CLI: resolve promise, close WS
103
- ```
104
-
105
- ### 3. Reset Handling
106
-
107
- ```mermaid
108
- sequenceDiagram
109
- participant User
110
- participant AgentServer
111
- participant AgentClient
112
-
113
- User->>AgentServer: {action: "user_reset"}
114
- AgentServer->>Prompt: prompt.reset()
115
- AgentServer-->>All Agents: broadcast {action: "reset"}
116
- AgentClient->>AgentClient: #epoch++, #queue=[], prompt.reset()
117
- ```
118
-
119
- ## Implementation Details
120
-
121
- - **Server-side pending responses**: `pendingResponses` Map keyed by `${conn.id}:${msg.id}` with timeout via `setInterval` (30s intervals, max 20 → 10min).
122
- - **Client-side queue**: Strict sequential processing (`#processing` guard, 2s polling interval by default). Prevents race conditions in tool calls.
123
- - **Auto-reconnect**: AgentClient retries every 5s on close.
124
- - **Session Management**: Integrated with `Session.js` for `user_sessionlist` / `user_loadsession`.
125
- - **wsIO.js specifics**: Sends both `user_introduction` + action in one connection, matches response by `id`, then closes. Uses base64 secret.
126
-
127
- ## Optimizations & Suggestions
128
-
129
- **Current Strengths**
130
- - Simple JSON, no complex framing.
131
- - Epoch prevents stale responses after reset.
132
- - Dynamic tool registration — agents can be added at runtime.
133
-
134
- **Potential Improvements**
135
- 1. **Performance**:
136
- - Replace polling interval in AgentClient with event-driven queue (e.g. `setImmediate` or microtask after each message).
137
- - Use a proper request-response correlation library or WebSocket subprotocol.
138
- - Add backpressure handling for high-volume tool calls.
139
-
140
- 2. **Reliability**:
141
- - Heartbeat/ping-pong to detect dead connections faster than timeout.
142
- - Exponential backoff on reconnect.
143
- - Persistent message IDs (UUID instead of timestamp).
144
- - Schema validation for all incoming messages (e.g. Zod or JSON Schema).
145
-
146
- 3. **Observability**:
147
- - Add structured logging (instead of console.log).
148
- - Metrics: active clients, avg response time, error rate.
149
- - OpenTelemetry or Prometheus integration.
150
-
151
- 4. **Security**:
152
- - Current secret is weak (query param). Consider JWT or mTLS for production.
153
- - Rate limiting per connection.
154
- - Validate `content` size.
155
-
156
- 5. **Extensibility**:
157
- - Support binary messages for large data (e.g. images, files).
158
- - Add `agent_stream` for streaming responses from agents.
159
- - Formalize as a subprotocol (`Sec-WebSocket-Protocol: agent-dave-v1`).
160
-
161
- 6. **Documentation**:
162
- - Generate Mermaid diagrams automatically from code comments.
163
- - Add protocol test suite in `scenarios/`.
164
-
165
- **Next Steps** (from TODO):
166
- - Complete sequence diagrams (expand the Mermaid examples above).
167
- - Identify specific performance bottlenecks via profiling.
168
- - Create formal spec with error codes and versioning.
169
-
170
- ## References
171
-
172
- - `lib/AgentServer.js`
173
- - `lib/AgentClient.js`
174
- - `lib/wsIO.js`
175
- - `agents/spawn_agent.js` (uses this protocol for hybrid/server/client modes)
176
- - `docs/multi-agent-clusters.md`
177
- - `docs/prompt/spawn_agent.md`
178
-
179
- *Last updated: April 20, 2026*
180
- *This document was generated as part of the TODO task "Document and review the 'agent dave websocket protocol'."*
@@ -1,244 +0,0 @@
1
- # AgentManager (\`lib/AgentManager.js\`)
2
-
3
- ## Overview
4
- **AgentManager** is the central orchestrator class in the hello-dave framework. It manages agent lifecycle: setup (prompts, API, tools, sessions), execution modes (CLI, direct calls, WebSocket server/client), and tool integration. Designed for simplicity and extensibility, it abstracts LLM providers (\`xai\`, \`gpt\`, \`claude\`), \`Prompt.js\`, \`ToolSet.js\`, and \`Session.js\`.
5
-
6
- **Uses named exports from \`lib/index.js\` - NO default export.**
7
-
8
- Key Features:
9
- - **Modes**: Interactive CLI, one-shot \`directCall\`, WS **server** (expose agent as tool), **client** (attach to remote server), hybrid (server + client).
10
- - **Tool Handling**: Pre-configured (\`toolsetMode: 'auto'\`/\`'required'\`), generics (\`addGenericToolcall\`), custom \`ToolSet\`.
11
- - **Persistence**: Sessions in \`.cache/[name]/\` (NDJSON logs).
12
- - **Networking**: WS with optional \`secret\` auth (base64-encoded).
13
- - **Validation**: Strict name regex \`/^[a-z_0-9_]{2,}$/\`.
14
- - **Events**: Leverages \`Prompt\` events (e.g., \`reset\` → server resetAll).
15
-
16
- Exports: \`{ AgentManager }\` (named).
17
-
18
- ## Agent Naming Rules ⚠️
19
- **CRITICAL**: The \`name\` (constructor) and \`toolName\` (in \`start()\`) **MUST** match regex \`/^[a-z_0-9_]{2,}$/\`:
20
- - Lowercase \`a-z\`, digits \`0-9\`, \`_\` **only**.
21
- - **Minimum 2 characters**.
22
-
23
- **Valid ✅**:
24
- - \`myagent\`
25
- - \`chatbot\`
26
- - \`weather_agent\`
27
- - \`tool123\`
28
- - \`ws_client\`
29
-
30
- **Invalid ❌ (throws \`Error\`)**:
31
- - \`MyAgent\` (uppercase)
32
- - \`agent-v1\` (hyphen)
33
- - \`a\` (too short)
34
- - \`agent#tool\` (\`#\` invalid)
35
- - \`Agent_\` (uppercase)
36
-
37
- **Why?** Safe for folders, tool names, filesystems (no specials/caps/spaces).
38
-
39
- ## Quick Usage
40
- \`\`\`javascript
41
- import { AgentManager } from '@j-o-r/hello-dave';
42
-
43
- const agent = new AgentManager({ name: 'myagent', secret: 'mysecret' });
44
- agent.setup({
45
- prompt: 'You are a helpful coding assistant.',
46
- api: 'xai',
47
- options: { model: 'grok-4-fast-reasoning' },
48
- toolsetMode: 'auto',
49
- contextWindow: 128000
50
- });
51
- agent.addGenericToolcall('execute_bash_script');
52
-
53
- await agent.start(); // Interactive CLI mode
54
- // Or: await agent.directCall('Write a Bash script'); // One-shot (pair with readIn for piped CLI)
55
- // Or: await agent.start(8000); // WS server on port 8000
56
- \`\`\`
57
-
58
- ## Constructor Options
59
- | Property | Type | Default | Description |
60
- |----------|------|---------|-------------|
61
- | \`name\` | string | \`'agent'\` | **Agent ID (session folder, tool name). MUST match \`/^[a-z_0-9_]{2,}$/\` (lowercase a-z0-9_; ≥2 chars).** |
62
- | \`secret\` | string | \`''\` | WS auth (base64-encoded; optional, match server/client). |
63
- | \`cachePath\` | string | \`'.cache/hello-dave'\` | Session storage root. |
64
-
65
- ## setup(setup) → \`this\` (chainable)
66
- Configures the agent. Must call before \`start()\`/\`directCall()\`.
67
-
68
- | Property | Type | Default | Description |
69
- |----------|------|---------|-------------|
70
- | \`prompt\` | string | - | **Required**: System prompt. |
71
- | \`api\` | \`'xai'|'gpt'|'claude'\` | \`'gpt'\` | LLM provider (uses \`lib/API/[provider]/text.js\`). |
72
- | \`options\` | \`XAIOptions\`/\`OAOptions\`/etc. | \`{}\` | Provider-specific (model, temperature, tools, reasoning). |
73
- | \`contextWindow\` | number | \`300000\` | Token limit for \`Prompt\`. |
74
- | \`toolsetMode\` | \`'auto'|'required'\` | \`null\` | \`'auto'\`: Generic tools; \`'required'\`: Empty \`ToolSet\` (add manually). |
75
- | \`debug\` | boolean | \`false\` | Verbose logging. |
76
-
77
- Internals: Creates \`Prompt\`, \`Session\`, sets \`Prompt.setAdaptor(API.text[api], toolset, options)\`.
78
-
79
- ## Key Methods
80
- | Method | Args | Returns | Description |
81
- |--------|------|---------|-------------|
82
- | \`directCall(input)\` | \`string\` | \`Promise&lt;string&gt;\` | One-shot query (via \`prompt.call()\`). **Ideal for piped CLI scripts & cron jobs.** |
83
- | \`getPrompt()\` | - | \`Prompt\` | Access internal \`Prompt\`. |
84
- | \`getToolset()\` | - | \`ToolSet\\|void\` | Access tools. |
85
- | \`environment()\` | - | \`Promise&lt;EnvironmentInfo&gt;\` | System info (via \`fafs.js/env()\`). |
86
- | \`addGenericToolcall(name)\` | string (e.g., \`'read_file'\`) | - | Copy from \`genericToolset.js\` pool. Post-setup only. |
87
- | \`start(servePort?, connectUrl?, cliIntro?, toolName?, toolDesc?)\` | Mixed | \`Promise&lt;void&gt;\` | **Smart launcher**:<br>- \`servePort\` (num): WS **server** (\`toolName\` MUST match name regex or auto-gen).<br>- \`connectUrl\` (str, e.g. \`'ws://127.0.0.1:8000/ws'\`): WS **client** (toolName/desc for attachment).<br>- Neither: **Interactive CLI** mode.<br>- Both: Hybrid. |
88
-
89
- ## Workflow Diagram
90
- \`\`\`mermaid
91
- graph TD
92
- A[New AgentManager name, secret] --> B[setup prompt, api, options, toolsetMode]
93
- B --> C{start args?}
94
- C -->|servePort| D[enableServer port, name, desc<br/>+ optional attach client]
95
- C -->|connectUrl| E[attach client url, name, desc]
96
- C -->|none| F[startCli intro]
97
- D --> G[Session Loop: Prompt → API/Tools → Response]
98
- E --> G
99
- F --> G
100
- G --> H[directCall or persist]
101
- \`\`\`
102
-
103
- **Per-Endpoint Options**:
104
- - **Interactive CLI**: \`start()\` → chat loop until Ctrl+C.
105
- - **One-Shot Piped CLI**: Custom script w/ \`readIn()\` + \`directCall()\` (stdin detection; exits after 1 response). No loop.
106
- - **WS Server**: \`start(port)\` → \`AgentServer\` (localhost only). Exposes agent as tool (name/desc). \`secret\` required for clients. \`prompt.on('reset')\` → \`server.resetAll()\`.
107
- - **WS Client**: \`start(undefined, url)\` → \`AgentClient\`. Connects to remote server; agent callable as tool.
108
- - **Direct**: \`directCall()\` bypasses networking/CLI.
109
-
110
- ## Examples
111
-
112
- ### 1. Interactive CLI Agent
113
- \`\`\`javascript
114
- import { AgentManager } from '@j-o-r/hello-dave';
115
-
116
- const agent = new AgentManager({ name: 'chatbot' });
117
- agent.setup({
118
- prompt: 'You are a friendly chatbot.',
119
- api: 'xai',
120
- toolsetMode: 'auto'
121
- });
122
- await agent.start('Chatbot ready!'); // Interactive chat loop
123
- \`\`\`
124
-
125
- ### 2. WS Server (Expose as Tool)
126
- \`\`\`javascript
127
- import { AgentManager } from '@j-o-r/hello-dave';
128
-
129
- agent.setup({ ... });
130
- await agent.start(8000, undefined, undefined, 'weathertool', 'Get real-time weather');
131
- \`\`\`
132
- - Clients: \`npx @j-o-r/hello-dave dave --connect ws://localhost:8000/ws --secret mysecret\`
133
-
134
- ### 3. Hybrid + Direct Call
135
- \`\`\`javascript
136
- import { AgentManager } from '@j-o-r/hello-dave';
137
-
138
- await agent.start(8001, 'ws://other:8000/ws'); // Server on 8001 + client to other
139
- const resp = await agent.directCall('Hybrid query'); // Uses attached tools too
140
- \`\`\`
141
-
142
- ### 4. Custom Tools + Generics
143
- \`\`\`javascript
144
- import { AgentManager } from '@j-o-r/hello-dave';
145
-
146
- agent.setup({ toolsetMode: 'required' });
147
- const ts = agent.getToolset();
148
- ts.add('custommath', 'Add numbers', {
149
- type: 'object', properties: {a: {type: 'number'}, b: {type: 'number'}},
150
- required: ['a', 'b']
151
- }, async ({a, b}) => a + b);
152
- agent.addGenericToolcall('javascript_interpreter');
153
- \`\`\`
154
-
155
- ### 5. CLI One-Shot Piped Usage (Scripts/Cron)
156
- Uses \`@j-o-r/sh\` (\`readIn()\`, \`parseArgs()\`) + \`AgentManager.directCall()\` for fast, single-turn queries from stdin. **Detects piped input vs interactive** (empty stdin → help). Ideal for bash pipelines, cron jobs, automation.
157
-
158
- **Boilerplate Snippet** (see \`agents/grok_agent.js\`, \`agents/docs_agent.js\` etc.):
159
- \`\`\`javascript
160
- #!/usr/bin/env node
161
- import { AgentManager } from '@j-o-r/hello-dave';
162
- import { parseArgs, readIn } from '@j-o-r/sh';
163
-
164
- const name = 'grok_agent'; // Matches naming rules
165
- const api = 'xai';
166
- const secret = process.env.SECRET || '';
167
-
168
- const input = await readIn();
169
- const args = parseArgs();
170
- if (args.help || !input?.trim()) {
171
- console.log(\`
172
- Usage: echo "query" | \${name}.js [--model grok-4] [--temperature 0.7]
173
-
174
- Options:
175
- --model [str] e.g., 'grok-4', 'grok-beta'
176
- --temperature [float] 0.0 - 2.0
177
- --tokens [int] Max output tokens
178
- --top_p [float]
179
- --context [int] Context window (default: 1.9M)
180
- \`);
181
- process.exit(0);
182
- }
183
-
184
- const options = {
185
- tools: [{type: 'web_search'}, {type: 'x_search'}],
186
- model: args.model || 'grok-4.20-multi-agent-0309',
187
- temperature: args.temperature ? parseFloat(args.temperature) : undefined,
188
- max_output_tokens: args.tokens ? parseInt(args.tokens) : undefined,
189
- top_p: args.top_p ? parseFloat(args.top_p) : undefined,
190
- reasoning: { effort: 'medium', summary: 'auto' }
191
- };
192
-
193
- const agent = new AgentManager({ name, secret });
194
- agent.setup({
195
- prompt: 'Respond briefly and directly. Reason step-by-step first.',
196
- api,
197
- options,
198
- toolsetMode: 'auto',
199
- contextWindow: args.context ? parseInt(args.context) : 1900000
200
- });
201
-
202
- const res = await agent.directCall(input);
203
- console.log(res);
204
- \`\`\`
205
-
206
- **Bash Examples**:
207
- \`\`\`bash
208
- # Quick query
209
- echo "Explain async/await" | agents/grok_agent.js --model grok-4
210
-
211
- # Custom options
212
- echo "Daily summary" | agents/grok_agent.js --model grok-4 --temperature 0.1 --tokens 500
213
-
214
- # Cron job (append to log)
215
- 0 8 * * 1 echo "Weekly report: sales data" | /path/to/grok_agent.js >> reports.log
216
-
217
- # Pipe chain
218
- cat data.txt | agents/docs_agent.js --model grok-4 | jq .
219
- \`\`\`
220
-
221
- **Interactive vs Piped**:
222
- | Mode | Invocation | Behavior |
223
- |------|------------|----------|
224
- | **Interactive** | \`agent.start()\` | Chat loop (multi-turn, until Ctrl+C). |
225
- | **Piped** | \`readIn() + directCall()\` | Single response, fast exit. Stdin empty → help. |
226
-
227
- **Suitability**: Cron/CLI automation (lightweight, no server). Complements WS modes.
228
-
229
- **Cross-Ref**: [README.md#cli-one-shot-with-example-agents](../README.md#cli-one-shot-with-example-agents) (\`agents/*.js\` ready-to-run).
230
-
231
- ## Advanced: Networking & Secrets
232
- - **Server-Client Chain**: Server A attaches to Server B → A callable from B (and vice versa if mutual).
233
- - **Cron/CLI Messaging**: Pipe to one-shot scripts (above) or WS clients: \`echo 'query' | npx @j-o-r/hello-dave dave --connect ...\`
234
- - **Reset**: \`Prompt\` emits \`'reset'\` → propagates to attached servers/clients.
235
-
236
- ## Errors
237
- - No \`setup()\`: Throws.
238
- - **Invalid \`name\`/\`toolName\`: Regex \`/^[a-z_0-9_]{2,}$/\` fail → \`Error\`**.
239
- - No \`toolset\` on server: Throws.
240
-
241
- See source \`lib/AgentManager.js\` for full JSDoc/types. Cross-links:
242
- - [Prompt](./prompt-class.md)
243
- - [ToolSet](./toolset.md)
244
- - [Generic Tools](../lib/genericToolset.js)
@@ -1,191 +0,0 @@
1
- # CodeServer Pattern
2
-
3
- [![PM2](https://img.shields.io/badge/PM2-Managed-green.svg)](https://pm2.keymetrics.io/) [![WebSocket](https://img.shields.io/badge/WebSocket-Persistent-blue.svg)](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
4
-
5
- ## Table of Contents
6
-
7
- - [Introduction & Motivation](#introduction--motivation)
8
- - [Architecture](#architecture)
9
- - [Prerequisites](#prerequisites)
10
- - [Step-by-Step Setup](#step-by-step-setup)
11
- - [Interaction via \`bin/dave.js\`](#interaction-via-bindavejs)
12
- - [Monitoring & Stopping with PM2](#monitoring--stopping-with-pm2)
13
- - [Customization](#customization)
14
- - [Benefits](#benefits)
15
- - [Troubleshooting](#troubleshooting)
16
- - [Integration with AgentManager](#integration-with-agentmanager)
17
- - [References](#references)
18
-
19
- ## Introduction & Motivation
20
-
21
- The **CodeServer pattern** demonstrates a production-ready setup for running a **persistent, always-on agent server** managed by [PM2](https://pm2.keymetrics.io/). It launches:
22
-
23
- - **Main Server**: \`code_agent.js\` as a WebSocket server (code-focused agent with JavaScript tools).
24
- - **Sub-Agents (Clients)**: Specialized agents (\`todo_agent.js\`, \`readme_agent.js\`, \`npm_agent.js\`, \`docs_agent.js\`) that **attach as tools** to the main server via WebSocket.
25
-
26
- **Motivation**:
27
- - **Persistence**: Long-running sessions without restarts (state in \`.cache/hello-dave/[agent]/\`).
28
- - **Delegation**: Main agent delegates to specialists (e.g., "update docs" → \`docsDave\` tool).
29
- - **No Heartbeats**: PM2 handles restarts, monitoring—no custom pings.
30
- - **Scalability**: Easy to add sub-agents, deploy across machines.
31
- - **CLI-Friendly**: Interact via \`bin/dave.js --connect\`.
32
-
33
- Ideal for dev workflows: code execution, docs mgmt, npm inspection, todos—all in one persistent hub.
34
-
35
- See live example: [agents/CodeServer](../agents/CodeServer)
36
-
37
- ## Architecture
38
-
39
- ```mermaid
40
- graph TB
41
- PM2[PM2 Process Manager] --> Main[Main: code_agent.js<br/>--serve PORT --secret SECRET<br/>WS Server on /ws]
42
- PM2 --> Todo[todo_agent.js<br/>--connect ws://localhost:PORT/ws<br/>tool: todo_agent]
43
- PM2 --> Readme[readme_agent.js<br/>--connect ...<br/>tool: readme_agent]
44
- PM2 --> NPM[npm_agent.js<br/>--connect ...<br/>tool: npm_module_inspector]
45
- PM2 --> Docs[docs_agent.js<br/>--connect ...<br/>tool: agent_docs]
46
-
47
- User[bin/dave.js --connect] -.-> Main
48
- Main -->|Delegates via tools| Todo & Readme & NPM & Docs
49
- Todo -.->|File ops| ProjectFiles[(TODO.md, etc.)]
50
- Docs -.->|Docs mgmt| DocsFolder[./docs/*.md]
51
- ```
52
-
53
- **Key Flows**:
54
- 1. PM2 spawns all processes (named \`FOLDER_agent_PORT\`).
55
- 2. Clients connect to main server → auto-register as **tools** (e.g., \`todo_agent\`, \`readme_agent\`).
56
- 3. User queries main server → delegates (e.g., \`call todo_agent {add: "new task"}\`).
57
- 4. Sessions persist independently per agent.
58
-
59
- ## Prerequisites
60
-
61
- | Requirement | Install Command | Notes |
62
- |-------------|-----------------|-------|
63
- | **Node.js** | `node --version` (≥18) | ESM support required. |
64
- | **PM2** | `npm i -g pm2` | Process manager. Startup: `pm2 startup`. |
65
- | **Project** | `git clone <repo> && cd hello-dave` | Examples in \`./agents/\`. |
66
-
67
- Verify: `pm2 --version` & `node -e "import('@j-o-r/hello-dave')"`
68
-
69
- ## Step-by-Step Setup
70
-
71
- 1. **Navigate to project**:
72
- ```bash
73
- cd /path/to/hello-dave
74
- ```
75
-
76
- 2. **Launch CodeServer** (pick PORT 1024-65535, SECRET ≥3 chars):
77
- ```bash
78
- ./agents/CodeServer 8080 mysecret123
79
- ```
80
- Output:
81
- ```
82
- Starting CodeServer on port 8080 in folder 'hello-dave' ... with SECRET 'mysecret123'
83
- CodeServer processes spawned with prefix 'hello-dave_' and suffix _8080.
84
- dave --connect ws://127.0.0.1:8080/ws --secret 'mysecret123'
85
- ```
86
-
87
- 3. **Verify PM2**:
88
- ```bash
89
- pm2 list | grep 8080
90
- ```
91
- | Name | Status | CPU | Memory |
92
- |-----------------------|--------|-----|--------|
93
- | hello-dave_code_agent_8080 | online | ... | ... |
94
- | hello-dave_todo_agent_8080 | online | ... | ... |
95
- | ... (4 total) | ... | ... | ... |
96
-
97
- ## Interaction via \`bin/dave.js\`
98
-
99
- **Interactive**:
100
- ```bash
101
- ./bin/dave.js --connect 'ws://localhost:8080/ws' --secret 'mysecret123'
102
- ```
103
- - Chat with **code_agent** (main agent).
104
- - Delegates automatically: "Manage todos", "Inspect @j-o-r/cli", "Update docs/codeserver-pattern.md".
105
-
106
- **Piped (one-shot)**:
107
- ```bash
108
- echo "List pending todos" | ./bin/dave.js --connect 'ws://localhost:8080/ws' --secret 'mysecret123'
109
- echo "user_reset" | ./bin/dave.js --connect ... # Reset session
110
- ```
111
-
112
- **Delegation Examples**:
113
- - \`code_agent\`: "Use todo_agent to add 'Review PRs'"
114
- - \`code_agent\`: "Call agent_docs to create nodejs-guide.md"
115
-
116
- ## Monitoring & Stopping with PM2
117
-
118
- **Monitor**:
119
- - `pm2 list` / `pm2 monit` (dashboard)
120
- - `pm2 logs hello-dave_code_agent_8080` (agent logs)
121
- - `pm2 logs *8080 --lines 50`
122
-
123
- **Stop**:
124
- ```bash
125
- pm2 stop hello-dave_*_8080 # All related
126
- # Or delete:
127
- pm2 delete hello-dave_code_agent_8080 hello-dave_todo_agent_8080 ...
128
- ```
129
-
130
- **Startup** (auto-restart on boot): `pm2 save && pm2 startup`
131
-
132
- ## Customization
133
-
134
- ### Adding Sub-Agents
135
- Edit [agents/CodeServer](../agents/CodeServer):
136
- ```bash
137
- # Add new agent
138
- pm2 start "${SCRIPT_DIR}/newAgent.js" --name "${FOLDER}_new_agent_${PORT}" -- --connect "ws://127.0.0.1:${PORT}/ws" --secret "${SECRET}"
139
- ```
140
- - Create \`newAgent.js\` like [agents/todo_agent.js](../agents/todo_agent.js): Set \`tool_call_name\`, prompt.
141
-
142
- ### Changing Tools
143
- - Main server: Edit \`code_agent.js\`: \`--tools javascript,bash,ssh\`
144
- - Sub-agents: \`agent.addGenericToolcall('read_file')\` etc. See [toolset.md](../toolset.md).
145
-
146
- ### Prompts/Models
147
- - Per-agent: Edit JS files (e.g., \`options.model = 'grok-4-1-fast-reasoning'\`).
148
-
149
- ## Benefits
150
-
151
- | Feature | Advantage |
152
- |---------|-----------|
153
- | **Persistent Sessions** | State in \`.cache/hello-dave/[name]/sessions/\`—no re-explaining. |
154
- | **Delegation** | Main agent uses sub-agents as tools (parallel, specialized). |
155
- | **No Heartbeat** | PM2 restarts on crash; no custom logic. |
156
- | **Resource Efficient** | Shared context; subs only active on call. |
157
- | **Portable** | Script auto-prefixes PM2 names by folder/port. |
158
-
159
- ## Troubleshooting
160
-
161
- | Issue | Solution |
162
- |-------|----------|
163
- | **Port in use** | Change PORT; `lsof -i :8080` & `kill`. |
164
- | **Secret mismatch** | Ensure exact match (no extra spaces). |
165
- | **PM2 not found** | `npm i -g pm2`. |
166
- | **Connection refused** | Wait 5s for server startup; check `pm2 logs`. |
167
- | **No delegation** | Verify client tool names in JS (e.g., \`todo_agent\`). |
168
- | **High memory** | `pm2 del *` or tweak \`contextWindow\`. |
169
-
170
- Logs: `.cache/hello-dave/[agent]/sessions/*.ndjson` (use \`bin/dave.js --inspect log.ndjson\`).
171
-
172
- ## Integration with AgentManager
173
-
174
- Built on [AgentManager](../docs/agent-manager.md):
175
-
176
- - **Server**: `agent.start(PORT)` → WS server exposing \`code_agent\`.
177
- - **Clients**: `agent.start(undefined, 'ws://...', undefined, 'todo_agent', 'desc')` → Attaches as tool.
178
-
179
- **Tools**: Generic from [toolset.md](../docs/toolset.md) (e.g., \`execute_bash_script\`, \`javascript_interpreter\`).
180
-
181
- Cross-links:
182
- - [Prompt Class](../docs/prompt-class.md)
183
- - [Generic Tools](../lib/genericToolset.js)
184
-
185
- ## References
186
-
187
- - [PM2 Docs](https://pm2.keymetrics.io/docs/usage/quick-start/)
188
- - Examples: [CodeServer](../agents/CodeServer), [code_agent.js](../agents/code_agent.js), [todo_agent.js](../agents/todo_agent.js), etc.
189
- - Sessions: \`bin/dave.js --list\`
190
-
191
- **Updated**: March 24, 2026