@j-o-r/hello-dave 0.0.2 → 0.0.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 (161) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +445 -160
  3. package/README.md.backup +269 -0
  4. package/README.md.bak +481 -0
  5. package/README.md.bak.1774780058 +338 -0
  6. package/README.md.bak2 +455 -0
  7. package/bin/dave.js +165 -0
  8. package/docs.bak.1774780058/agent-manager.md +167 -0
  9. package/docs.bak.1774780058/agent-manager.md.bak +137 -0
  10. package/docs.bak.1774780058/agent-manager.md.bak2 +157 -0
  11. package/docs.bak.1774780058/codeserver-pattern.md +191 -0
  12. package/docs.bak.1774780058/path-resolution-best-practices.md +104 -0
  13. package/docs.bak.1774780058/project-overview.md +67 -0
  14. package/docs.bak.1774780058/project-overview.md.bak +67 -0
  15. package/docs.bak.1774780058/prompt-class.md +141 -0
  16. package/docs.bak.1774780058/prompt-class.md.bak +142 -0
  17. package/docs.bak.1774780058/tools-syntax-validation.md +121 -0
  18. package/docs.bak.1774780058/tools-syntax-validation.md.bak2 +125 -0
  19. package/docs.bak.1774780058/tools-syntax-validation.md.bak3 +125 -0
  20. package/docs.bak.1774780058/tools-syntax-validation.md.bak4 +106 -0
  21. package/docs.bak.1774780058/tools-syntax-validation.md.bak_path +106 -0
  22. package/docs.bak.1774780058/toolset.md +164 -0
  23. package/docs.bak.1774780058/toolset.md.bak +94 -0
  24. package/docs.bak.1774780058/toolset.md.bak3 +161 -0
  25. package/docs.bak.1774780058/toolset.md.bak4 +161 -0
  26. package/docs.bak.1774780058/toolset.md.bak5 +161 -0
  27. package/docs.bak.1774780058/toolset.md.bak6 +163 -0
  28. package/docs.bak.1774780058/toolset.md.bak_path +163 -0
  29. package/docs.bak.1774780058/toolset.md.bak_syntax +161 -0
  30. package/docs.bak.1774780058/xai-responses.md +111 -0
  31. package/docs.bak.1774780058/xai-responses.md.bak +107 -0
  32. package/docs.bak.1774780058/xai-responses.md.bak2 +107 -0
  33. package/docs.bak.1774780058/xai_collections.md +106 -0
  34. package/examples/ask_agent.js +137 -0
  35. package/examples/code_agent.js +149 -0
  36. package/examples/coderev_agent.js +136 -0
  37. package/examples/codeserver.sh +47 -0
  38. package/examples/daisy_agent.js +170 -0
  39. package/examples/docs_agent.js +148 -0
  40. package/examples/gpt_agent.js +125 -0
  41. package/examples/grok_agent.js +132 -0
  42. package/examples/grok_agent.js.bak +98 -0
  43. package/examples/grok_agent.js.bak.2 +99 -0
  44. package/examples/grok_agent.js.bak.3 +1 -0
  45. package/examples/grok_agent.js.bak.4 +124 -0
  46. package/examples/grok_agent.js.bak.5 +1 -0
  47. package/examples/grok_agent.js.bak.6 +1 -0
  48. package/examples/memory_agent.js +152 -0
  49. package/examples/npm_agent.js +202 -0
  50. package/examples/npm_agent.js.bak.3 +2 -0
  51. package/examples/npm_agent.js.bak.4 +205 -0
  52. package/examples/npm_agent.js.bak.5 +1 -0
  53. package/examples/npm_agent.js.bak.6 +1 -0
  54. package/examples/prompt_agent.js +133 -0
  55. package/examples/readme_agent.js +148 -0
  56. package/examples/spawn_agent.js +293 -0
  57. package/examples/test_agent.js +187 -0
  58. package/examples/todo_agent.js +175 -0
  59. package/examples.bak.1774780058/ask_agent.js +114 -0
  60. package/examples.bak.1774780058/code_agent.js +149 -0
  61. package/examples.bak.1774780058/coderev_agent.js +72 -0
  62. package/examples.bak.1774780058/codeserver.sh +47 -0
  63. package/examples.bak.1774780058/daisy_agent.js +177 -0
  64. package/examples.bak.1774780058/docs_agent.js +119 -0
  65. package/{bin/hdAsk.js → examples.bak.1774780058/gpt_agent.js} +46 -40
  66. package/examples.bak.1774780058/grok_agent.js +98 -0
  67. package/examples.bak.1774780058/memory_agent.js +112 -0
  68. package/examples.bak.1774780058/npm_agent.js +175 -0
  69. package/examples.bak.1774780058/prompt_agent.js +112 -0
  70. package/examples.bak.1774780058/readme_agent.js +144 -0
  71. package/examples.bak.1774780058/spawn_agent.js +263 -0
  72. package/examples.bak.1774780058/test_agent.js +162 -0
  73. package/examples.bak.1774780058/todo_agent.js +138 -0
  74. package/lib/API/openai.com/reponses/text.js +12 -18
  75. package/lib/API/x.ai/collections.js +354 -0
  76. package/lib/API/x.ai/files.js +218 -0
  77. package/lib/API/x.ai/responses.js +492 -0
  78. package/lib/API/x.ai/text.js +1 -1
  79. package/lib/AgentClient.js +13 -6
  80. package/lib/AgentManager.js +80 -10
  81. package/lib/AgentServer.js +50 -22
  82. package/lib/Cli.js +7 -1
  83. package/lib/Prompt.js +4 -2
  84. package/lib/ToolSet.js +2 -1
  85. package/lib/genericToolset.js +258 -88
  86. package/lib/genericToolset.js.bak_syntax +402 -0
  87. package/lib/index.js +4 -2
  88. package/lib/wsCli.js +256 -0
  89. package/lib/wsIO.js +96 -0
  90. package/package.json +26 -21
  91. package/scenarios.bak.1774780058/data/eval_node_message.json +9 -0
  92. package/scenarios.bak.1774780058/data/hist_oa.json +66 -0
  93. package/scenarios.bak.1774780058/data/o3_response1.json +96 -0
  94. package/scenarios.bak.1774780058/data/oa_reasoning_parse.json +112 -0
  95. package/scenarios.bak.1774780058/data/tool_oa.json +96 -0
  96. package/scenarios.bak.1774780058/data/tool_xai.json +59 -0
  97. package/scenarios.bak.1774780058/data/tool_xai2.json +40 -0
  98. package/scenarios.bak.1774780058/data/xai-response-1.json +59 -0
  99. package/scenarios.bak.1774780058/data/xai-response-2.json +10 -0
  100. package/scenarios.bak.1774780058/data/xai_reasoning_tools_resp.json +59 -0
  101. package/scenarios.bak.1774780058/data/xai_search_response.json +58 -0
  102. package/scenarios.bak.1774780058/environment.js +10 -0
  103. package/scenarios.bak.1774780058/example.js +17 -0
  104. package/scenarios.bak.1774780058/genericToolset.test.js +182 -0
  105. package/scenarios.bak.1774780058/grok.js +113 -0
  106. package/scenarios.bak.1774780058/memory-tools.js +51 -0
  107. package/scenarios.bak.1774780058/openai-o3.js +137 -0
  108. package/scenarios.bak.1774780058/openai-prompt.js +155 -0
  109. package/scenarios.bak.1774780058/openai-session.js +148 -0
  110. package/scenarios.bak.1774780058/openai.js +102 -0
  111. package/scenarios.bak.1774780058/prompt.js +118 -0
  112. package/scenarios.bak.1774780058/promptFishbowl.js +76 -0
  113. package/scenarios.bak.1774780058/search.brave.com.js +25 -0
  114. package/scenarios.bak.1774780058/sh.js +15 -0
  115. package/scenarios.bak.1774780058/test-wsio.js +26 -0
  116. package/scenarios.bak.1774780058/testToolset.js +42 -0
  117. package/scenarios.bak.1774780058/toolset.js +16 -0
  118. package/scenarios.bak.1774780058/toolset.test.js +141 -0
  119. package/scenarios.bak.1774780058/write_file_syntax.test.js +145 -0
  120. package/scenarios.bak.1774780058/write_file_validation/README.md +30 -0
  121. package/scenarios.bak.1774780058/write_file_validation/bad.js +3 -0
  122. package/scenarios.bak.1774780058/write_file_validation/good.js +4 -0
  123. package/scenarios.bak.1774780058/write_file_validation/test.sh +43 -0
  124. package/scenarios.bak.1774780058/wsClient.js +69 -0
  125. package/scenarios.bak.1774780058/xai_responses.integration.test.js +57 -0
  126. package/scenarios.bak.1774780058/xai_responses.test.js +154 -0
  127. package/scenarios.bak.1774780058/xaicoll.js +50 -0
  128. package/scenarios.bak.1774780058/xaifiles.js +48 -0
  129. package/types/API/openai.com/reponses/text.d.ts +17 -3
  130. package/types/API/x.ai/collections.d.ts +167 -0
  131. package/types/API/x.ai/files.d.ts +84 -0
  132. package/types/API/x.ai/responses.d.ts +379 -0
  133. package/types/AgentClient.d.ts +5 -0
  134. package/types/AgentManager.d.ts +25 -31
  135. package/types/AgentServer.d.ts +5 -1
  136. package/types/Prompt.d.ts +4 -2
  137. package/types/ToolSet.d.ts +1 -0
  138. package/types/index.d.ts +4 -3
  139. package/types/wsCli.d.ts +3 -0
  140. package/types/wsIO.d.ts +26 -0
  141. package/utils/bars.js +40 -0
  142. package/utils/clear_sessions.sh +54 -0
  143. package/{bin/hdInspect.js → utils/format_log.js} +5 -0
  144. package/utils/list_sessions.sh +46 -0
  145. package/utils/search_sessions.sh +73 -0
  146. package/utils/syntax_check.sh +61 -0
  147. package/utils/test.sh +46 -0
  148. package/bin/hdClear.js +0 -13
  149. package/bin/hdCode.js +0 -115
  150. package/bin/hdConnect.js +0 -230
  151. package/bin/hdNpm.js +0 -114
  152. package/bin/hdPrompt.js +0 -108
  153. package/examples/claude-test.js +0 -89
  154. package/examples/claude.js +0 -143
  155. package/examples/gpt.js +0 -127
  156. package/examples/gpt_code.js +0 -125
  157. package/examples/gpt_note_keeping.js +0 -117
  158. package/examples/grok.js +0 -119
  159. package/examples/grok_code.js +0 -114
  160. package/examples/grok_note_keeping.js +0 -111
  161. package/module.md +0 -189
@@ -0,0 +1,104 @@
1
+ # Path Resolution Best Practices for Generic Tools
2
+
3
+ ## Overview
4
+ In `lib/genericToolset.js` and related tools (e.g., `syntax_check`, `write_file`), **path resolution** distinguishes between:
5
+ - **Module-relative paths** (utils/scripts): Use `__dirname` for portability.
6
+ - **User-provided file paths**: Resolve to `process.cwd()` (CWD sandbox) + strict validation.
7
+
8
+ This ensures **sandboxing** (no escapes), **portability** (works from any CWD/PM2), and **security**. All tools enforce: no `/`, `..`, `\\\\`; `resolvedPath.startsWith(process.cwd())`.
9
+
10
+ **ESM Setup** (genericToolset.js):
11
+ ```javascript
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Polyfill for ESM
13
+ ```
14
+
15
+ ## __dirname vs process.cwd(): When to Use What
16
+
17
+ | Context | Resolution | Example Code | Rationale |
18
+ |---------|------------|--------------|-----------|
19
+ | **Utils/Scripts** (e.g., syntax_check.sh, search_sessions.sh) | `__dirname` (module-rel) | `path.resolve(__dirname, '..', 'utils', 'syntax_check.sh')` | Portable: Locates utils/ from `lib/` regardless of CWD. Works in servers/clients. |
20
+ | **User Files** (read/write/syntax/memory) | `process.cwd()` | `path.resolve(process.cwd(), params.file)` | Sandbox: Pins to project CWD (`~/devpri/js/hello-dave`). Prevents jailbreaks. |
21
+ | **Cache/Memory** | `path.join(process.cwd(), '.cache/...')` | `.cache/hello-dave/memory.ndjson` | Per-project persistence. |
22
+ | **Shebangs/Chmod** | Absolute (resolved) | `SH`chmod +x ${[resolvedPath]}` | Post-write on validated path. |
23
+
24
+ **Universal Validation** (in read_file, write_file, syntax_check):
25
+ ```javascript
26
+ const file = params.file?.trim();
27
+ if (file.startsWith('/') || file.includes('..') || file.includes('\\\\')) {
28
+ throw new Error('Path must be relative within CWD only (no `/`, `..`, or `\\\\`).');
29
+ }
30
+ const resolvedPath = path.resolve(process.cwd(), file);
31
+ if (!resolvedPath.startsWith(process.cwd())) {
32
+ throw new Error(`Path '${file}' escapes CWD scope.`);
33
+ }
34
+ ```
35
+
36
+ ## syntax_check.sh Path Fix (User-Reported)
37
+
38
+ **Issue** (Early Versions):
39
+ - Relative paths passed to SH`` → Failed if CWD != lib/.
40
+ - E.g., `SH`syntax_check.sh "rel/file.js"`` → "No such file" in sub-processes (PM2/CodeServer).
41
+
42
+ **Fix** (Implemented):
43
+ 1. **Absolute Script Path**: `path.resolve(__dirname, '..', 'utils', 'syntax_check.sh')`.
44
+ 2. **Absolute File Arg**: `${syntaxChecker} "${resolvedPath}"` (resolvedPath = CWD-abs).
45
+ 3. **Script Handles Absolutes**: `FILE="$1"`; `head -n1 "$FILE"` works anywhere.
46
+
47
+ **Test from Any CWD**:
48
+ ```bash
49
+ cd /tmp
50
+ /full/path/to/hello-dave/utils/syntax_check.sh /full/path/to/hello-dave/test.js # ✅ JS OK
51
+ # Or via tool: agent.directCall("syntax_check('test.js')") # Resolves internally
52
+ ```
53
+
54
+ **SH Best Practice**: Use array args `${[resolvedPath]}` → Auto-quotes/escapes paths with spaces.
55
+
56
+ ## Best Practices & Pitfalls
57
+
58
+ ### ✅ Do
59
+ 1. **Utils**: Always `__dirname, '..', 'utils', 'script.sh'` → Portable across deployments.
60
+ 2. **Files**: `process.cwd()` + validation → Sandboxed writes/reads.
61
+ 3. **SH Calls**: `SH`${script} ${[path]}`.run()` → Safe quoting via @j-o-r/sh.
62
+ 4. **Join vs Resolve**: `path.join(process.cwd(), '.cache')` for dirs; `resolve` for files.
63
+ 5. **ESM __dirname**: Polyfill with `fileURLToPath(import.meta.url)`.
64
+
65
+ ### ❌ Avoid
66
+ 1. `path.resolve(file)` → Assumes CWD; skips validation.
67
+ 2. `__dirname` for user files → Escapes to lib/ (security hole).
68
+ 3. Relative SH args → Breaks in non-lib CWD (e.g., WS clients).
69
+ 4. `process.chdir()` → Mutates global; use per-tool cwd.
70
+
71
+ ### Real-World Examples (from genericToolset.js)
72
+ - **history_search**:
73
+ ```js
74
+ const history_search = path.resolve(__dirname, '..', 'utils', 'search_sessions.sh');
75
+ SH`${history_search} "${escapedQuery}"`.run();
76
+ ```
77
+ - **syntax_check**:
78
+ ```js
79
+ const syntaxChecker = path.resolve(__dirname, '..', 'utils', 'syntax_check.sh');
80
+ return await SH`${syntaxChecker} "${resolvedPath}"`.run();
81
+ ```
82
+ - **write_file** (AUTO-VALIDATE):
83
+ ```js
84
+ const syntaxChecker = path.resolve(__dirname, '..', 'utils', 'syntax_check.sh');
85
+ await SH`${syntaxChecker} ${[resolvedPath]}`.run(); // Throws on fail
86
+ ```
87
+
88
+ ## Troubleshooting
89
+ | Issue | Cause | Fix |
90
+ |-------|-------|-----|
91
+ | "No such file: utils/script.sh" | Wrong CWD | Use `__dirname` for utils. |
92
+ | "Path escapes CWD" | `..` in input | Input validation blocks. |
93
+ | SH arg unescaped | Spaces/specials | Array `${[path]}` auto-handles. |
94
+ | ESM no __dirname | Node <20 | Polyfill shown above. |
95
+
96
+ **Pro Tip**: For custom tools, copy pattern: `__dirname` (utils), `process.cwd()` (files).
97
+
98
+ ## Cross-Links
99
+ - [ToolSet](../toolset.md#generic-tools)
100
+ - [Syntax Validation](../tools-syntax-validation.md)
101
+ - [genericToolset.js](../lib/genericToolset.js) (grep "path.resolve")
102
+ - Utils: [syntax_check.sh](../utils/syntax_check.sh)
103
+
104
+ **Updated**: March 27, 2026 (syntax_check.sh path fix documented).
@@ -0,0 +1,67 @@
1
+ # Hello-Dave Project Overview
2
+
3
+ ## Root Directory
4
+ The root contains essential project configuration, documentation, and entry points:
5
+ - **README.md**: Main project documentation and setup instructions.
6
+ - **TODO.md**: Outstanding tasks and development notes.
7
+ - **package.json**: NPM package metadata, dependencies, and scripts.
8
+ - **LICENSE**: Project license (Apache 2.0).
9
+ - **jsconfig.json** / **tsc.json**: TypeScript/JavaScript configuration for editor and compiler support.
10
+ - **.gitignore** / **.npmignore**: Git and NPM ignore rules.
11
+ - **workspace.js**: Development or workspace management script.
12
+ - Directories: `bin/`, `docs/`, `examples/`, `lib/`, `scenarios/`, `types/`, `utils/`, `release/`, `node_modules/`, `.cache/` (for sessions).
13
+
14
+ ## bin/
15
+ Executable CLI scripts (npm bin entries) for spawning and interacting with specialized \"Dave\" agents. These are user-facing tools for quick tasks:
16
+ - `spawn_agent.js`: Generates and runs custom agent scripts.
17
+ - `ask_agent.js`, `chat2Dave.js`: General interactive chat/query.
18
+ - `code_agent.js`: Code-related agent.
19
+ - `prompt_agent.js`, `docs_agent.js`: Prompt engineering and docs management.
20
+ - `readme_agent.js`, `todo_agent.js`: Project maintenance (README/TODO).
21
+ - `npm_agent.js`: NPM module inspection/publishing.
22
+ - Others: `formatDave.js`, `clearDave.js`, `CodeServer`.
23
+
24
+ ## lib/
25
+ Core library code (ESM modules) **named exports only** from `lib/index.js` (NO default export):
26
+ - **index.js**: Main export barrel (`{ AgentManager, Prompt, ToolSet, ... }`).
27
+ - **AgentManager.js**: Central orchestrator for agents—handles setup, sessions, tools, networking (WS server/client), and `start()` for CLI/server modes.
28
+ - **Prompt.js**: Conversation manager (EventEmitter)—tracks messages, tools, truncation, token counting, provider adapters.
29
+ - **ToolSet.js**: Defines/extends tools (JSON Schema + handlers) like bash execution, web search, JS interpreter.
30
+ - **AgentServer.js**, **AgentClient.js**: WebSocket server/client for agent networking.
31
+ - **Session.js**: Persistent chat history/logs (NDJSON in `.cache/`).
32
+ - **Cli.js**: CLI input/output handling.
33
+ - **genericToolset.js**: Pre-configured toolset with common tools.
34
+ - **fafs.js**, **promptHelpers.js**: Utilities (env, file access, prompt ops).
35
+ - **API/**: Provider-specific wrappers (subdirs):
36
+ - `anthropic.com/`: Claude API.
37
+ - `brave.com/`: Search API.
38
+ - `openai.com/`: GPT API.
39
+ - `x.ai/`: Grok/xAI API (responses, files, collections).
40
+
41
+ ## Agent Workflow
42
+ An **Agent** (via `AgentManager`) is configured with:
43
+ 1. **System prompt** (via `Prompt.js`): Defines personality/task.
44
+ 2. **API provider** (from `lib/API/`): xAI/Grok, OpenAI/GPT, Anthropic/Claude.
45
+ 3. **ToolSet** (`ToolSet.js`): Custom/local functions (e.g., `execute_bash_script`, `web_search`); extensible.
46
+ 4. **Session**: Conversation history.
47
+
48
+ Modes: Standalone CLI, one-shot query, WS **server** (expose as tool), **client** (attach to server), hybrid. `bin/spawn_agent.js` generates custom agents leveraging this.
49
+
50
+ ## docs/
51
+ Project documentation (Markdown) ✅ **Complete** (reviewed Mar 24, 2026):
52
+ - `project-overview.md`: This file.
53
+ - `prompt-class.md`: Deep dive on `Prompt.js`.
54
+ - `toolset.md`: `ToolSet.js` details + 9 generics.
55
+ - `agent-manager.md`: **AgentManager** workflows, options, Mermaid diagrams.
56
+ - `xai-responses.md`: xAI Responses API integration/examples.
57
+ - `xai-collections.md`: xAI Collections (raw; Later priority).
58
+
59
+ ## Other Directories
60
+ - **examples/**: Usage demos (e.g., `daisy_agent.js` music agent).
61
+ - **scenarios/**: Tests/demos (e.g., `grok_agent.js`, `wsAgent.js`).
62
+ - **types/**: TypeScript `.d.ts` files.
63
+ - **utils/**: Miscellaneous utilities.
64
+ - **release/**: NPM pack artifacts.
65
+
66
+ ## Summary
67
+ `hello-dave` (@j-o-r/hello-dave NPM pkg) is a lightweight ESM Node.js framework for **tool-using AI agents** with unified LLM access (Grok/Claude/GPT), WS networking, and CLI tools. **Import via named exports: `import { AgentManager } from '@j-o-r/hello-dave';`**. Focus: Rapid prototyping of domain agents (code, docs, music) via `AgentManager` + `Prompt` + `ToolSet` + APIs. Sessions in `.cache/`. Node >=20.
@@ -0,0 +1,67 @@
1
+ # Hello-Dave Project Overview
2
+
3
+ ## Root Directory
4
+ The root contains essential project configuration, documentation, and entry points:
5
+ - **README.md**: Main project documentation and setup instructions.
6
+ - **TODO.md**: Outstanding tasks and development notes.
7
+ - **package.json**: NPM package metadata, dependencies, and scripts.
8
+ - **LICENSE**: Project license (Apache 2.0).
9
+ - **jsconfig.json** / **tsc.json**: TypeScript/JavaScript configuration for editor and compiler support.
10
+ - **.gitignore** / **.npmignore**: Git and NPM ignore rules.
11
+ - **workspace.js**: Development or workspace management script.
12
+ - Directories: `bin/`, `docs/`, `examples/`, `lib/`, `scenarios/`, `types/`, `utils/`, `release/`, `node_modules/`, `.cache/` (for sessions).
13
+
14
+ ## bin/
15
+ Executable CLI scripts (npm bin entries) for spawning and interacting with specialized \"Dave\" agents. These are user-facing tools for quick tasks:
16
+ - `spawnDave.js`: Generates and runs custom agent scripts.
17
+ - `ask_agent.js`, `chat2Dave.js`: General interactive chat/query.
18
+ - `codeDave.js`: Code-related agent.
19
+ - `prompt_agent.js`, `docsDave.js`: Prompt engineering and docs management.
20
+ - `readmeDave.js`, `todoDave.js`: Project maintenance (README/TODO).
21
+ - `npmDave.js`: NPM module inspection/publishing.
22
+ - Others: `formatDave.js`, `clearDave.js`, `CodeServer`.
23
+
24
+ ## lib/
25
+ Core library code (ESM modules) implementing the agent framework:
26
+ - **index.js**: Main export barrel (AgentManager, Prompt, ToolSet, API wrappers, etc.).
27
+ - **AgentManager.js**: Central orchestrator for agents—handles setup, sessions, tools, networking (WS server/client), and `start()` for CLI/server modes.
28
+ - **Prompt.js**: Conversation manager (EventEmitter)—tracks messages, tools, truncation, token counting, provider adapters.
29
+ - **ToolSet.js**: Defines/extends tools (JSON Schema + handlers) like bash execution, web search, JS interpreter.
30
+ - **AgentServer.js**, **AgentClient.js**: WebSocket server/client for agent networking.
31
+ - **Session.js**: Persistent chat history/logs (NDJSON in `.cache/`).
32
+ - **Cli.js**: CLI input/output handling.
33
+ - **genericToolset.js**: Pre-configured toolset with common tools.
34
+ - **fafs.js**, **promptHelpers.js**: Utilities (env, file access, prompt ops).
35
+ - **API/**: Provider-specific wrappers (subdirs):
36
+ - `anthropic.com/`: Claude API.
37
+ - `brave.com/`: Search API.
38
+ - `openai.com/`: GPT API.
39
+ - `x.ai/`: Grok/xAI API (responses, files, collections).
40
+
41
+ ## Agent Workflow
42
+ An **Agent** (via `AgentManager`) is configured with:
43
+ 1. **System prompt** (via `Prompt.js`): Defines personality/task.
44
+ 2. **API provider** (from `lib/API/`): xAI/Grok, OpenAI/GPT, Anthropic/Claude.
45
+ 3. **ToolSet** (`ToolSet.js`): Custom/local functions (e.g., `execute_bash_script`, `web_search`); extensible.
46
+ 4. **Session**: Conversation history.
47
+
48
+ Modes: Standalone CLI, one-shot query, WS **server** (expose as tool), **client** (attach to server), hybrid. `bin/spawnDave.js` generates custom agents leveraging this.
49
+
50
+ ## docs/
51
+ Project documentation (Markdown) ✅ **Complete** (reviewed Mar 24, 2026):
52
+ - `project-overview.md`: This file.
53
+ - `prompt-class.md`: Deep dive on `Prompt.js`.
54
+ - `toolset.md`: `ToolSet.js` details + 9 generics.
55
+ - `agent-manager.md`: **AgentManager** workflows, options, Mermaid diagrams.
56
+ - `xai-responses.md`: xAI Responses API integration/examples.
57
+ - `xai-collections.md`: xAI Collections (raw; Later priority).
58
+
59
+ ## Other Directories
60
+ - **examples/**: Usage demos (e.g., `daisy_agent.js` music agent).
61
+ - **scenarios/**: Tests/demos (e.g., `grok_agent.js`, `wsAgent.js`).
62
+ - **types/**: TypeScript `.d.ts` files.
63
+ - **utils/**: Miscellaneous utilities.
64
+ - **release/**: NPM pack artifacts.
65
+
66
+ ## Summary
67
+ `hello-dave` (@j-o-r/hello-dave NPM pkg) is a lightweight ESM Node.js framework for **tool-using AI agents** with unified LLM access (Grok/Claude/GPT), WS networking, and CLI tools. Focus: Rapid prototyping of domain agents (code, docs, music) via `AgentManager` + `Prompt` + `ToolSet` + APIs. Sessions in `.cache/`. Node >=20."
@@ -0,0 +1,141 @@
1
+ # Prompt Class
2
+
3
+ Source: \`lib/Prompt.js\`
4
+
5
+ The \`Prompt\` class is an EventEmitter-based manager for standardized AI conversation messages. It supports multi-modal content (text, images, audio), tool/function calls, token counting, automatic truncation, and adapters for different LLM providers (OpenAI, xAI, Anthropic).
6
+
7
+ ## Key Features
8
+ - Uniform message format across providers.
9
+ - Context window management with truncation.
10
+ - Toolset integration for function calling.
11
+ - Event-driven (add, start, finished, tool_request, etc.).
12
+ - Sticky system prompts preserved on reset/truncate.
13
+
14
+ ## Dependencies
15
+ - \`gpt-3-encoder\` for token counting.
16
+ - \`node:events\` EventEmitter.
17
+ - \`./promptHelpers.js\` for tool pruning.
18
+
19
+ ## Message Structure
20
+ ```js
21
+ {
22
+ role: "system" | "assistant" | "user" | "tool" | "log" | "reasoning",
23
+ content: Content[],
24
+ name?: string,
25
+ sticky?: boolean, // Preserve on reset
26
+ ts?: number // Timestamp
27
+ }
28
+ ```
29
+
30
+ ### Content Types
31
+ | Type | Role Constraints | Structure |
32
+ |------|------------------|-----------|
33
+ | `text` | system/assistant/user | `{ type: "text", text: string }` |
34
+ | `image_url` | user | `{ type: "image_url", url: string \| data:URL }` |
35
+ | `audio_url` | user | `{ type: "audio_url", url: string \| data:URL }` |
36
+ | `function_request` | assistant | `{ type: "function_request", function_request: { name: string, id: string, call_id?: string, parameters: string (JSON) } }` |
37
+ | `function_response` | tool | `{ type: "function_response", function_response: { name: string, id: string, call_id?: string, response: string (JSON) } }` |
38
+
39
+ ## Record Structure (API Logs)
40
+ ```js
41
+ {
42
+ id: string,
43
+ isoDate: string,
44
+ duration: number (ms),
45
+ environment: string,
46
+ model: string,
47
+ tokensIn: number,
48
+ tokensOut: number
49
+ }
50
+ ```
51
+
52
+ ## Constants
53
+ ```js
54
+ const MESSAGE_ROLES = { SYSTEM: "system", ASSISTANT: "assistant", ... };
55
+ const MESSAGE_TYPES = { TEXT: "text", IMAGE_URL: "image_url", ... };
56
+ const EVENTS = { start: "start", finished: "finished", tool_request: "tool_request", ... };
57
+ ```
58
+
59
+ ## Usage Example
60
+ ```js
61
+ import { Prompt, ToolSet } from '@j-o-r/hello-dave';
62
+ import { openaiText } from './API/openai.com/text.js'; // Adapter example
63
+
64
+ const prompt = new Prompt(128000); // Context window
65
+ prompt.setAdaptor(openaiText, new ToolSet(), { model: "gpt-4o" });
66
+
67
+ prompt.add("system", "You are a helpful assistant.", true);
68
+ const response = await prompt.call("Hello!");
69
+ console.log(response); // Assistant response
70
+ ```
71
+
72
+ ## Properties
73
+ | Name | Type | Description |
74
+ |------|------|-------------|
75
+ | `contextLength` | `number` | Max tokens (getter) |
76
+ | `messages` | `Message[]` | Copy of messages (getter/setter validates/imports) |
77
+ | `length` | `number` | Message count |
78
+ | `system_prompt` | `string` | System message text (throws if missing) |
79
+ | `hasSystemprompt` | `boolean` | System prompt present? |
80
+ | `toolset` | `ToolSet \| void` | Current toolset |
81
+ | `EVENTS` | `object` | Frozen event names |
82
+
83
+ ## Methods
84
+
85
+ ### Constructor
86
+ ```js
87
+ new Prompt(contextWindow?: number)
88
+ ```
89
+ - `contextWindow = 0`: One-shot (no history buildup).
90
+
91
+ ### Setup
92
+ - `setAdaptor(requestHandler, toolset?: ToolSet, options?)`: Bind AI provider.
93
+ - `toolsetFunctions()`: `string[]` of available tools.
94
+
95
+ ### Adding Messages
96
+ - `add(role: Roles, text: string, sticky = false)`: Text message.
97
+ - `addMultiModal(role: Roles, content: Content[], sticky = false)`: Multi-modal.
98
+ - Validates role/content, order (system first/sticky), emits `message`.
99
+
100
+ ### Conversation Flow
101
+ - `call(content: string \| Content[])`: Add user input, call adapter, return text response. Emits `start`, `finished`.
102
+ - `triggerRequest()`: Auto-trigger if last message is user/tool (for tools first).
103
+
104
+ ### Management
105
+ - `reset()`: Remove non-sticky, return removed. Emits `reset`.
106
+ - `truncate()`: Prune tools, remove old user-assistant blocks to fit context. Emits `truncated`.
107
+ - `countTokens(str?: string)`: Tokens in string or last total.
108
+ - `contentToString(content)`: Concat text from content.
109
+ - `getLastMessage()`: Last message.
110
+
111
+ ### Records & Info
112
+ - `addRecord(record)` / `addRecords(records[])`: Log API usage, update tokens, warn if over limit.
113
+ - `templateRecord()`: Empty record.
114
+ - `info()`: Debug string (cwd, tools, context, tokens).
115
+
116
+ ## Events
117
+ Emitted via `EventEmitter`:
118
+
119
+ | Event | Triggered When | Data |
120
+ |-------|----------------|------|
121
+ | `message` | Message added | `Message` |
122
+ | `start` / `retrigger` / `finished` | Request lifecycle | `true` |
123
+ | `record` | Record added | `Record` |
124
+ | `truncated` / `reset` | History altered | Event name |
125
+ | `tool_request` / `tool_response` / `tool_error` | Tool lifecycle | Tool data |
126
+ | `error` / `warning` | Issues | Error/Warning |
127
+
128
+ ## Validation
129
+ - Strict checks on message roles/types/content.
130
+ - Records must have all fields.
131
+ - Order: System first/sticky sequential.
132
+
133
+ ## Truncation Details
134
+ 1. Prune resolved tool calls (keep last per ID).
135
+ 2. Sum sticky tokens.
136
+ 3. From end: Remove full user-assistant blocks exceeding window.
137
+
138
+ ## Integration Notes
139
+ - Adapters handle provider-specific formatting (e.g., OpenAI, xAI).
140
+ - Tools execute automatically in `triggerRequest`.
141
+ - Supports reasoning role (xAI-specific).
@@ -0,0 +1,142 @@
1
+ # Prompt Class
2
+
3
+ Source: \`lib/Prompt.js\`
4
+
5
+ The \`Prompt\` class is an EventEmitter-based manager for standardized AI conversation messages. It supports multi-modal content (text, images, audio), tool/function calls, token counting, automatic truncation, and adapters for different LLM providers (OpenAI, xAI, Anthropic).
6
+
7
+ ## Key Features
8
+ - Uniform message format across providers.
9
+ - Context window management with truncation.
10
+ - Toolset integration for function calling.
11
+ - Event-driven (add, start, finished, tool_request, etc.).
12
+ - Sticky system prompts preserved on reset/truncate.
13
+
14
+ ## Dependencies
15
+ - \`gpt-3-encoder\` for token counting.
16
+ - \`node:events\` EventEmitter.
17
+ - \`./promptHelpers.js\` for tool pruning.
18
+
19
+ ## Message Structure
20
+ ```js
21
+ {
22
+ role: "system" | "assistant" | "user" | "tool" | "log" | "reasoning",
23
+ content: Content[],
24
+ name?: string,
25
+ sticky?: boolean, // Preserve on reset
26
+ ts?: number // Timestamp
27
+ }
28
+ ```
29
+
30
+ ### Content Types
31
+ | Type | Role Constraints | Structure |
32
+ |------|------------------|-----------|
33
+ | `text` | system/assistant/user | `{ type: "text", text: string }` |
34
+ | `image_url` | user | `{ type: "image_url", url: string \| data:URL }` |
35
+ | `audio_url` | user | `{ type: "audio_url", url: string \| data:URL }` |
36
+ | `function_request` | assistant | `{ type: "function_request", function_request: { name: string, id: string, call_id?: string, parameters: string (JSON) } }` |
37
+ | `function_response` | tool | `{ type: "function_response", function_response: { name: string, id: string, call_id?: string, response: string (JSON) } }` |
38
+
39
+ ## Record Structure (API Logs)
40
+ ```js
41
+ {
42
+ id: string,
43
+ isoDate: string,
44
+ duration: number (ms),
45
+ environment: string,
46
+ model: string,
47
+ tokensIn: number,
48
+ tokensOut: number
49
+ }
50
+ ```
51
+
52
+ ## Constants
53
+ ```js
54
+ const MESSAGE_ROLES = { SYSTEM: "system", ASSISTANT: "assistant", ... };
55
+ const MESSAGE_TYPES = { TEXT: "text", IMAGE_URL: "image_url", ... };
56
+ const EVENTS = { start: "start", finished: "finished", tool_request: "tool_request", ... };
57
+ ```
58
+
59
+ ## Usage Example
60
+ ```js
61
+ import Prompt from "./Prompt.js";
62
+ import { openaiText } from "./API/openai.com/text.js"; // Adapter example
63
+ import ToolSet from "./ToolSet.js";
64
+
65
+ const prompt = new Prompt(128000); // Context window
66
+ prompt.setAdaptor(openaiText, new ToolSet(), { model: "gpt-4o" });
67
+
68
+ prompt.add("system", "You are a helpful assistant.", true);
69
+ const response = await prompt.call("Hello!");
70
+ console.log(response); // Assistant response
71
+ ```
72
+
73
+ ## Properties
74
+ | Name | Type | Description |
75
+ |------|------|-------------|
76
+ | `contextLength` | `number` | Max tokens (getter) |
77
+ | `messages` | `Message[]` | Copy of messages (getter/setter validates/imports) |
78
+ | `length` | `number` | Message count |
79
+ | `system_prompt` | `string` | System message text (throws if missing) |
80
+ | `hasSystemprompt` | `boolean` | System prompt present? |
81
+ | `toolset` | `ToolSet \| void` | Current toolset |
82
+ | `EVENTS` | `object` | Frozen event names |
83
+
84
+ ## Methods
85
+
86
+ ### Constructor
87
+ ```js
88
+ new Prompt(contextWindow?: number)
89
+ ```
90
+ - `contextWindow = 0`: One-shot (no history buildup).
91
+
92
+ ### Setup
93
+ - `setAdaptor(requestHandler, toolset?: ToolSet, options?)`: Bind AI provider.
94
+ - `toolsetFunctions()`: `string[]` of available tools.
95
+
96
+ ### Adding Messages
97
+ - `add(role: Roles, text: string, sticky = false)`: Text message.
98
+ - `addMultiModal(role: Roles, content: Content[], sticky = false)`: Multi-modal.
99
+ - Validates role/content, order (system first/sticky), emits `message`.
100
+
101
+ ### Conversation Flow
102
+ - `call(content: string \| Content[])`: Add user input, call adapter, return text response. Emits `start`, `finished`.
103
+ - `triggerRequest()`: Auto-trigger if last message is user/tool (for tools first).
104
+
105
+ ### Management
106
+ - `reset()`: Remove non-sticky, return removed. Emits `reset`.
107
+ - `truncate()`: Prune tools, remove old user-assistant blocks to fit context. Emits `truncated`.
108
+ - `countTokens(str?: string)`: Tokens in string or last total.
109
+ - `contentToString(content)`: Concat text from content.
110
+ - `getLastMessage()`: Last message.
111
+
112
+ ### Records & Info
113
+ - `addRecord(record)` / `addRecords(records[])`: Log API usage, update tokens, warn if over limit.
114
+ - `templateRecord()`: Empty record.
115
+ - `info()`: Debug string (cwd, tools, context, tokens).
116
+
117
+ ## Events
118
+ Emitted via `EventEmitter`:
119
+
120
+ | Event | Triggered When | Data |
121
+ |-------|----------------|------|
122
+ | `message` | Message added | `Message` |
123
+ | `start` / `retrigger` / `finished` | Request lifecycle | `true` |
124
+ | `record` | Record added | `Record` |
125
+ | `truncated` / `reset` | History altered | Event name |
126
+ | `tool_request` / `tool_response` / `tool_error` | Tool lifecycle | Tool data |
127
+ | `error` / `warning` | Issues | Error/Warning |
128
+
129
+ ## Validation
130
+ - Strict checks on message roles/types/content.
131
+ - Records must have all fields.
132
+ - Order: System first/sticky sequential.
133
+
134
+ ## Truncation Details
135
+ 1. Prune resolved tool calls (keep last per ID).
136
+ 2. Sum sticky tokens.
137
+ 3. From end: Remove full user-assistant blocks exceeding window.
138
+
139
+ ## Integration Notes
140
+ - Adapters handle provider-specific formatting (e.g., OpenAI, xAI).
141
+ - Tools execute automatically in `triggerRequest`.
142
+ - Supports reasoning role (xAI-specific).
@@ -0,0 +1,121 @@
1
+ # Syntax Validation in Generic Tools
2
+
3
+ ## Overview
4
+ **Integration Complete**: `lib/genericToolset.js` now includes **automatic JS syntax validation** in `write_file` **and a new `syntax_check` tool** using `./utils/syntax_check.sh` for multi-language support (JS/Python/Bash/JSON). Prevents invalid code, resolves escaping issues, auto `chmod +x` shebangs. Post-write errors prompt LLM retries.
5
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
6
+
7
+ **Key Benefits**:
8
+ - **JS Safety**: `node --check` (in `write_file`); full multi-lang in `syntax_check`.
9
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
10
+ - **Executable Prep**: Auto `chmod +x`.
11
+ - **Retry-Friendly**: Descriptive errors + previews.
12
+ - **Generic Access**: `toolsetMode: 'auto'` loads both.
13
+
14
+ **Commit History** (relevant):
15
+ - `3c8e1ae`: Initial `read_file`/`write_file`.
16
+ - Recent: JS validation + `syntax_check` tool.
17
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
18
+
19
+ ## write_file Tool Updates
20
+
21
+ ### Description (Updated)
22
+ ```
23
+ Write raw content to a file strictly within the current working directory (CWD). Paths must be relative (no leading /, no ..). Content written as-is (no escaping). **AUTO-VALIDATES JS FILES** with `node --check`; chmod +x shebangs. Retries syntax errors force LLM fix.
24
+ ```
25
+
26
+ ### Examples
27
+ **Success** (CLI/Agent):
28
+ ```bash
29
+ echo 'const x = \`hi\`; console.log(x);' | agent.directCall("Use write_file on test.js with this content")
30
+ # Returns: "Successfully wrote to 'test.js' (42 bytes). ✓ JS syntax OK"
31
+ ```
32
+
33
+ **Failure + Retry**:
34
+ ```javascript
35
+ // LLM generates invalid:
36
+ const msg = `Hello ${name`; // Missing `
37
+ ```
38
+ **Error**:
39
+ ```
40
+ ❌ JS SYNTAX ERROR in 'test.js' (node --check failed):
41
+ SyntaxError: Unexpected token '}'
42
+
43
+ PREVIEW:
44
+ const msg = `Hello ${name
45
+ ...
46
+
47
+ ... Fix escaping/backticks in template literals (use \\\\` for \` in JS strings) and retry.
48
+ ```
49
+ **LLM Sees → Fixes**: Uses `"Hello \${name}"` or proper `` \` ``.
50
+
51
+ **Shebang**:
52
+ ```javascript
53
+ content: '#!/usr/bin/env node\nconst hello = () => console.log("Hi");'
54
+ # Returns: "... ✓ JS syntax OK ✓ chmod +x"
55
+ ```
56
+
57
+ ## New: syntax_check Tool
58
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
59
+
60
+ **Description**:
61
+ ```
62
+ Validate syntax of a file (JS, Python, Bash/Sh, JSON). Detects language from extension/shebang. Uses native checkers... Exit 1 on fail.
63
+ ```
64
+
65
+ ### Parameters
66
+ ```json
67
+ {
68
+ "type": "object",
69
+ "properties": { "file": { "type": "string" } },
70
+ "required": ["file"]
71
+ }
72
+ ```
73
+
74
+ ### Examples
75
+ **Agent Call**:
76
+ ```
77
+ agent.directCall("Use syntax_check on test.js") → "🔍 Validating test.js (lang: js)\n✅ JS OK\n✅ Syntax validation passed for test.js"
78
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
79
+ ```
80
+
81
+ **Multi-Lang**:
82
+ - **Python**: `syntax_check script.py` → `✅ Python OK`
83
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
84
+ - **Bash**: `syntax_check script.sh` → `✅ Bash OK` ( + Shellcheck if avail.)
85
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
86
+ - **JSON**: `syntax_check config.json` → `✅ JSON OK`
87
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
88
+ - **Fail**: Invalid JSON → `❌ JSON invalid`
89
+
90
+ **Integration**: Post-`write_file`, agent can call `syntax_check(file)` for non-JS.
91
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
92
+
93
+ ## utils/syntax_check.sh (Underlying Script)
94
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
95
+ [Full source in prior doc]. Detects lang, runs checkers. Standalone/executable.
96
+
97
+ ### Usage (Direct)
98
+ ```bash
99
+ ./utils/syntax_check.sh test.js # ✅ JS OK
100
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
101
+ ./utils/syntax_check.sh bad.py # ❌ Python syntax error
102
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
103
+ ```
104
+
105
+ ## Agent Workflow Example
106
+ ```
107
+ User: "Create & validate test.js: const add = (a,b) => a+b; export {add};"
108
+ Agent:
109
+ 1. write_file(test.js, content) → ✓ JS OK
110
+ 2. syntax_check(test.js) → ✅ (redundant but confirms)
111
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
112
+ ```
113
+
114
+ ## Cross-Links
115
+ - [ToolSet](../toolset.md#generic-tools) ← [Linked below](#update-to-toolsetmd)
116
+ - [AgentManager](../agent-manager.md)
117
+ - Source: [lib/genericToolset.js](../lib/genericToolset.js), [utils/syntax_check.sh](../utils/syntax_check.sh)
118
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).
119
+
120
+ **Updated**: March 27, 2026 (syntax_check integrated).
121
+ **Path Note**: Uses `__dirname` for utils/; resolves files to `process.cwd()`. See [path-resolution-best-practices.md](path-resolution-best-practices.md).