@j-o-r/hello-dave 0.0.3 → 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 (119) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +216 -56
  3. package/README.md.bak +481 -0
  4. package/README.md.bak.1774780058 +338 -0
  5. package/README.md.bak2 +455 -0
  6. package/docs.bak.1774780058/agent-manager.md +167 -0
  7. package/docs.bak.1774780058/agent-manager.md.bak +137 -0
  8. package/docs.bak.1774780058/agent-manager.md.bak2 +157 -0
  9. package/docs.bak.1774780058/codeserver-pattern.md +191 -0
  10. package/docs.bak.1774780058/path-resolution-best-practices.md +104 -0
  11. package/docs.bak.1774780058/project-overview.md +67 -0
  12. package/docs.bak.1774780058/project-overview.md.bak +67 -0
  13. package/docs.bak.1774780058/prompt-class.md +141 -0
  14. package/docs.bak.1774780058/prompt-class.md.bak +142 -0
  15. package/docs.bak.1774780058/tools-syntax-validation.md +121 -0
  16. package/docs.bak.1774780058/tools-syntax-validation.md.bak2 +125 -0
  17. package/docs.bak.1774780058/tools-syntax-validation.md.bak3 +125 -0
  18. package/docs.bak.1774780058/tools-syntax-validation.md.bak4 +106 -0
  19. package/docs.bak.1774780058/tools-syntax-validation.md.bak_path +106 -0
  20. package/docs.bak.1774780058/toolset.md +164 -0
  21. package/docs.bak.1774780058/toolset.md.bak +94 -0
  22. package/docs.bak.1774780058/toolset.md.bak3 +161 -0
  23. package/docs.bak.1774780058/toolset.md.bak4 +161 -0
  24. package/docs.bak.1774780058/toolset.md.bak5 +161 -0
  25. package/docs.bak.1774780058/toolset.md.bak6 +163 -0
  26. package/docs.bak.1774780058/toolset.md.bak_path +163 -0
  27. package/docs.bak.1774780058/toolset.md.bak_syntax +161 -0
  28. package/docs.bak.1774780058/xai-responses.md +111 -0
  29. package/docs.bak.1774780058/xai-responses.md.bak +107 -0
  30. package/docs.bak.1774780058/xai-responses.md.bak2 +107 -0
  31. package/docs.bak.1774780058/xai_collections.md +106 -0
  32. package/examples/ask_agent.js +137 -0
  33. package/examples/code_agent.js +149 -0
  34. package/examples/coderev_agent.js +136 -0
  35. package/examples/codeserver.sh +47 -0
  36. package/examples/daisy_agent.js +170 -0
  37. package/examples/docs_agent.js +148 -0
  38. package/examples/gpt_agent.js +125 -0
  39. package/examples/grok_agent.js +132 -0
  40. package/examples/grok_agent.js.bak +98 -0
  41. package/examples/grok_agent.js.bak.2 +99 -0
  42. package/examples/grok_agent.js.bak.3 +1 -0
  43. package/examples/grok_agent.js.bak.4 +124 -0
  44. package/examples/grok_agent.js.bak.5 +1 -0
  45. package/examples/grok_agent.js.bak.6 +1 -0
  46. package/examples/memory_agent.js +152 -0
  47. package/examples/npm_agent.js +202 -0
  48. package/examples/npm_agent.js.bak.3 +2 -0
  49. package/examples/npm_agent.js.bak.4 +205 -0
  50. package/examples/npm_agent.js.bak.5 +1 -0
  51. package/examples/npm_agent.js.bak.6 +1 -0
  52. package/examples/prompt_agent.js +133 -0
  53. package/examples/readme_agent.js +148 -0
  54. package/examples/spawn_agent.js +293 -0
  55. package/examples/test_agent.js +187 -0
  56. package/examples/todo_agent.js +175 -0
  57. package/{examples/codeDave.js → examples.bak.1774780058/code_agent.js} +40 -6
  58. package/{examples/CodeServer → examples.bak.1774780058/codeserver.sh} +9 -5
  59. package/examples.bak.1774780058/memory_agent.js +112 -0
  60. package/{examples/spawndave.js → examples.bak.1774780058/spawn_agent.js} +29 -6
  61. package/examples.bak.1774780058/test_agent.js +162 -0
  62. package/{examples/todoDave.js → examples.bak.1774780058/todo_agent.js} +8 -2
  63. package/lib/API/x.ai/responses.js +7 -9
  64. package/lib/AgentManager.js +3 -2
  65. package/lib/AgentServer.js +23 -19
  66. package/lib/genericToolset.js +159 -26
  67. package/lib/genericToolset.js.bak_syntax +402 -0
  68. package/lib/wsCli.js +0 -1
  69. package/package.json +4 -5
  70. package/scenarios.bak.1774780058/data/eval_node_message.json +9 -0
  71. package/scenarios.bak.1774780058/data/hist_oa.json +66 -0
  72. package/scenarios.bak.1774780058/data/o3_response1.json +96 -0
  73. package/scenarios.bak.1774780058/data/oa_reasoning_parse.json +112 -0
  74. package/scenarios.bak.1774780058/data/tool_oa.json +96 -0
  75. package/scenarios.bak.1774780058/data/tool_xai.json +59 -0
  76. package/scenarios.bak.1774780058/data/tool_xai2.json +40 -0
  77. package/scenarios.bak.1774780058/data/xai-response-1.json +59 -0
  78. package/scenarios.bak.1774780058/data/xai-response-2.json +10 -0
  79. package/scenarios.bak.1774780058/data/xai_reasoning_tools_resp.json +59 -0
  80. package/scenarios.bak.1774780058/data/xai_search_response.json +58 -0
  81. package/scenarios.bak.1774780058/environment.js +10 -0
  82. package/scenarios.bak.1774780058/example.js +17 -0
  83. package/scenarios.bak.1774780058/genericToolset.test.js +182 -0
  84. package/scenarios.bak.1774780058/grok.js +113 -0
  85. package/scenarios.bak.1774780058/memory-tools.js +51 -0
  86. package/scenarios.bak.1774780058/openai-o3.js +137 -0
  87. package/scenarios.bak.1774780058/openai-prompt.js +155 -0
  88. package/scenarios.bak.1774780058/openai-session.js +148 -0
  89. package/scenarios.bak.1774780058/openai.js +102 -0
  90. package/scenarios.bak.1774780058/prompt.js +118 -0
  91. package/scenarios.bak.1774780058/promptFishbowl.js +76 -0
  92. package/scenarios.bak.1774780058/search.brave.com.js +25 -0
  93. package/scenarios.bak.1774780058/sh.js +15 -0
  94. package/scenarios.bak.1774780058/test-wsio.js +26 -0
  95. package/scenarios.bak.1774780058/testToolset.js +42 -0
  96. package/scenarios.bak.1774780058/toolset.js +16 -0
  97. package/scenarios.bak.1774780058/toolset.test.js +141 -0
  98. package/scenarios.bak.1774780058/write_file_syntax.test.js +145 -0
  99. package/scenarios.bak.1774780058/write_file_validation/README.md +30 -0
  100. package/scenarios.bak.1774780058/write_file_validation/bad.js +3 -0
  101. package/scenarios.bak.1774780058/write_file_validation/good.js +4 -0
  102. package/scenarios.bak.1774780058/write_file_validation/test.sh +43 -0
  103. package/scenarios.bak.1774780058/wsClient.js +69 -0
  104. package/scenarios.bak.1774780058/xai_responses.integration.test.js +57 -0
  105. package/scenarios.bak.1774780058/xai_responses.test.js +154 -0
  106. package/scenarios.bak.1774780058/xaicoll.js +50 -0
  107. package/scenarios.bak.1774780058/xaifiles.js +48 -0
  108. package/types/AgentManager.d.ts +4 -3
  109. package/utils/syntax_check.sh +61 -0
  110. package/utils/test.sh +46 -0
  111. /package/{examples/askDave.js → examples.bak.1774780058/ask_agent.js} +0 -0
  112. /package/{examples/coderev.js → examples.bak.1774780058/coderev_agent.js} +0 -0
  113. /package/{examples/daisy.js → examples.bak.1774780058/daisy_agent.js} +0 -0
  114. /package/{examples/docsDave.js → examples.bak.1774780058/docs_agent.js} +0 -0
  115. /package/{examples/gpt.js → examples.bak.1774780058/gpt_agent.js} +0 -0
  116. /package/{examples/grok.js → examples.bak.1774780058/grok_agent.js} +0 -0
  117. /package/{examples/npmDave.js → examples.bak.1774780058/npm_agent.js} +0 -0
  118. /package/{examples/promptDave.js → examples.bak.1774780058/prompt_agent.js} +0 -0
  119. /package/{examples/readmeDave.js → examples.bak.1774780058/readme_agent.js} +0 -0
@@ -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).
@@ -0,0 +1,125 @@
1
+ # Syntax Validation in Generic Tools
2
+
3
+ ## Overview
4
+ Recent enhancements to `lib/genericToolset.js` introduce **automatic syntax validation** for files written via the `write_file` tool. This prevents invalid code deployment, resolves common **escaping issues** in JavaScript, and supports shebang executables. Validation occurs **post-write**, throwing descriptive errors to prompt LLM retries/fixes.
5
+
6
+ **Key Benefits**:
7
+ - **JS Safety**: `node --check` catches syntax errors (e.g., unescaped backticks in template literals).
8
+ - **Executable Prep**: Auto `chmod +x` for shebangs.
9
+ - **Retry-Friendly**: Errors include code preview + fix hints, enabling agent self-correction.
10
+ - **No Overhead**: Only for JS/shebang files; fast native checks.
11
+
12
+ **Commit History** (relevant):
13
+ - `3c8e1ae`: Added `read_file`, `write_file` with initial path security.
14
+ - Recent refinements: Syntax check + escaping guidance.
15
+
16
+ ## write_file Tool Updates (`lib/genericToolset.js`)
17
+
18
+ ### Updated Description
19
+ ```
20
+ 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.
21
+ ```
22
+
23
+ ### Validation Logic
24
+ ```javascript
25
+ // After fs.writeFile(resolvedPath, content, 'utf8');
26
+ const ext = path.extname(file).toLowerCase();
27
+ const isJS = ext === '.js' || content.startsWith('#!/usr/bin/env node');
28
+ const hasShebang = content.startsWith('#!');
29
+
30
+ if (isJS) {
31
+ try {
32
+ await SH`node --check ${[resolvedPath]}`.run();
33
+ validationMsg = ' ✓ JS syntax OK';
34
+ } catch (e) {
35
+ const errPreview = content.slice(0, 1000).split('\\n').slice(0, 20).join('\\n');
36
+ throw new Error(`❌ JS SYNTAX ERROR in '${file}' (node --check failed):
37
+ ${e.message}
38
+
39
+ PREVIEW:
40
+ ${errPreview}
41
+
42
+ ... Fix escaping/backticks in template literals (use \\\\` for \` in JS strings) and retry.`);
43
+ }
44
+ }
45
+ if (hasShebang) {
46
+ await SH`chmod +x ${[resolvedPath]}`.run();
47
+ validationMsg += ' ✓ chmod +x';
48
+ }
49
+ ```
50
+
51
+ ### Escaping Issues Resolved
52
+ - **Backticks in JS Strings**: Common LLM error—generates `` `hi` `` inside strings without escaping. Hint: `use \\\\` for \` in JS strings`.
53
+ - **Template Literals**: Ensures `` \` ${var} \` `` is valid; catches mismatched quotes.
54
+ - **Raw Content**: Verbatim write (newlines, `$|<> & " '\\`` preserved), but syntax-enforced.
55
+ - **No False Positives**: Only triggers on `.js` ext or Node shebang; preview limits noise.
56
+
57
+ **Example Error (LLM Sees)**:
58
+ ```
59
+ ❌ JS SYNTAX ERROR in 'test.js' (node --check failed):
60
+ SyntaxError: Unexpected token `
61
+
62
+ PREVIEW:
63
+ const msg = `Hello ${name}
64
+ ...
65
+
66
+ ... Fix escaping/backticks in template literals (use \\\\` for \` in JS strings) and retry.
67
+ ```
68
+ - **LLM Fix**: Rewrites with `"Hello \${name}"` or proper `` \` `` nesting.
69
+
70
+ ### Usage in Agents
71
+ ```javascript
72
+ agent.addGenericToolcall('write_file'); // Auto-includes validation
73
+ await agent.directCall("Write test.js: const x = `hi`; console.log(x);"); // Passes
74
+ // Fails + retries: "Write test.js: const x = `hi; console.log(x);" → SyntaxError → Fix
75
+ ```
76
+
77
+ ## Multi-Language Syntax Validation (utils/syntax_check.sh)
78
+
79
+ **New Utility**: `./utils/syntax_check.sh` – Standalone script for **JS, Python, Bash, JSON**. Detects lang via ext/shebang, runs native checkers. Exit 0=OK, 1=error. Integrates as future `write_file` extension or new tool.
80
+
81
+ ### Features
82
+ | Lang | Checker | Optional | Notes |
83
+ |------|---------|----------|-------|
84
+ | **JS** | `node --check` | - | Syntax only (no runtime). |
85
+ | **Python** | `python3 -m py_compile` | - | Bytecode compile. |
86
+ | **Bash/Sh** | `bash -n` + `shellcheck` | Shellcheck | Static + linter. |
87
+ | **JSON** | `node -e JSON.parse(...)` | - | Parse validation. |
88
+ | **Unknown** | Skip | - | Non-fatal. |
89
+
90
+ ### Usage
91
+ ```bash
92
+ ./utils/syntax_check.sh path/to/file.js # 🔍 JS OK / ❌ error
93
+ ./utils/syntax_check.sh script.py --verbose # Extra output
94
+ # Future: --fix (auto-retry via agent?)
95
+ ```
96
+
97
+ ### Script Source (utils/syntax_check.sh)
98
+ ```bash
99
+ #!/bin/bash
100
+ set -euo pipefail
101
+ # Full script: Detects lang, validates, echoes status.
102
+ # Created: March 27, 2026
103
+ ```
104
+
105
+ ### Integration Proposal: New Tool `syntax_check`
106
+ Add to `ToolSet` / `genericToolset.js`:
107
+ ```javascript
108
+ tools.add('syntax_check', 'Validate file syntax (JS/Py/Bash/JSON).', {
109
+ type: 'object',
110
+ properties: { file: {type: 'string'} },
111
+ required: ['file']
112
+ }, async ({file}) => {
113
+ const script = path.resolve(__dirname, '..', 'utils', 'syntax_check.sh');
114
+ return await SH`${script} "${file}"`.run();
115
+ });
116
+ ```
117
+ - **Post-write Call**: `write_file` → `syntax_check` → LLM fix loop.
118
+ - **Extend write_file**: Run `./syntax_check.sh "$file"` after JS check.
119
+
120
+ **Cross-Links**:
121
+ - [ToolSet](../toolset.md#generic-tools)
122
+ - [AgentManager](../agent-manager.md#custom-tools--generics)
123
+ - Source: [lib/genericToolset.js](../lib/genericToolset.js)
124
+
125
+ **Updated**: March 27, 2026