@j-o-r/hello-dave 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -26
- package/README.md +161 -522
- package/README.md.bak +144 -449
- package/{examples → agents}/ask_agent.js +5 -5
- package/{examples → agents}/codeserver.sh +14 -14
- package/{examples → agents}/daisy_agent.js +5 -5
- package/{examples → agents}/docs_agent.js +5 -5
- package/{examples → agents}/gpt_agent.js +5 -5
- package/{examples → agents}/grok_agent.js +5 -5
- package/agents/memory_agent.js +263 -0
- package/{examples → agents}/npm_agent.js +5 -5
- package/{examples → agents}/prompt_agent.js +5 -5
- package/agents/spawn_agent.js +137 -0
- package/{examples → agents}/test_agent.js +6 -8
- package/{examples → agents}/todo_agent.js +5 -5
- package/bin/codeDave +58 -0
- package/bin/dave.js +114 -96
- package/lib/AgentClient.js +111 -67
- package/lib/AgentManager.js +111 -80
- package/lib/AgentServer.js +144 -104
- package/lib/Cli.js +126 -93
- package/lib/Prompt.js +38 -5
- package/lib/Session.js +102 -79
- package/lib/ToolSet.js +79 -60
- package/lib/fafs.js +54 -19
- package/lib/genericToolset.js +109 -213
- package/lib/wsCli.js +50 -19
- package/lib/wsIO.js +11 -17
- package/package.json +2 -2
- package/types/AgentClient.d.ts +69 -35
- package/types/AgentManager.d.ts +50 -56
- package/types/AgentServer.d.ts +63 -16
- package/types/Cli.d.ts +56 -10
- package/types/Prompt.d.ts +36 -4
- package/types/Session.d.ts +23 -9
- package/types/ToolSet.d.ts +49 -32
- package/types/fafs.d.ts +68 -25
- package/types/wsCli.d.ts +14 -0
- package/types/wsIO.d.ts +9 -5
- package/utils/search_sessions.sh +100 -53
- package/README.md.backup +0 -269
- package/README.md.bak.1774780058 +0 -338
- package/README.md.bak2 +0 -531
- package/bin/spawn_agent.js +0 -293
- package/docs.bak.1774780058/agent-manager.md +0 -167
- package/docs.bak.1774780058/agent-manager.md.bak +0 -137
- package/docs.bak.1774780058/agent-manager.md.bak2 +0 -157
- package/docs.bak.1774780058/codeserver-pattern.md +0 -191
- package/docs.bak.1774780058/path-resolution-best-practices.md +0 -104
- package/docs.bak.1774780058/project-overview.md +0 -67
- package/docs.bak.1774780058/project-overview.md.bak +0 -67
- package/docs.bak.1774780058/prompt-class.md +0 -141
- package/docs.bak.1774780058/prompt-class.md.bak +0 -142
- package/docs.bak.1774780058/tools-syntax-validation.md +0 -121
- package/docs.bak.1774780058/tools-syntax-validation.md.bak2 +0 -125
- package/docs.bak.1774780058/tools-syntax-validation.md.bak3 +0 -125
- package/docs.bak.1774780058/tools-syntax-validation.md.bak4 +0 -106
- package/docs.bak.1774780058/tools-syntax-validation.md.bak_path +0 -106
- package/docs.bak.1774780058/toolset.md +0 -164
- package/docs.bak.1774780058/toolset.md.bak +0 -94
- package/docs.bak.1774780058/toolset.md.bak3 +0 -161
- package/docs.bak.1774780058/toolset.md.bak4 +0 -161
- package/docs.bak.1774780058/toolset.md.bak5 +0 -161
- package/docs.bak.1774780058/toolset.md.bak6 +0 -163
- package/docs.bak.1774780058/toolset.md.bak_path +0 -163
- package/docs.bak.1774780058/toolset.md.bak_syntax +0 -161
- package/docs.bak.1774780058/xai-responses.md +0 -111
- package/docs.bak.1774780058/xai-responses.md.bak +0 -107
- package/docs.bak.1774780058/xai-responses.md.bak2 +0 -107
- package/docs.bak.1774780058/xai_collections.md +0 -106
- package/examples/memory_agent.js +0 -152
- package/examples.bak.1774780058/ask_agent.js +0 -114
- package/examples.bak.1774780058/code_agent.js +0 -149
- package/examples.bak.1774780058/coderev_agent.js +0 -72
- package/examples.bak.1774780058/codeserver.sh +0 -47
- package/examples.bak.1774780058/daisy_agent.js +0 -177
- package/examples.bak.1774780058/docs_agent.js +0 -119
- package/examples.bak.1774780058/gpt_agent.js +0 -109
- package/examples.bak.1774780058/grok_agent.js +0 -98
- package/examples.bak.1774780058/memory_agent.js +0 -112
- package/examples.bak.1774780058/npm_agent.js +0 -175
- package/examples.bak.1774780058/prompt_agent.js +0 -112
- package/examples.bak.1774780058/readme_agent.js +0 -144
- package/examples.bak.1774780058/spawn_agent.js +0 -263
- package/examples.bak.1774780058/test_agent.js +0 -162
- package/examples.bak.1774780058/todo_agent.js +0 -138
- package/lib/genericToolset.js.bak_syntax +0 -402
- package/scenarios.bak.1774780058/data/eval_node_message.json +0 -9
- package/scenarios.bak.1774780058/data/hist_oa.json +0 -66
- package/scenarios.bak.1774780058/data/o3_response1.json +0 -96
- package/scenarios.bak.1774780058/data/oa_reasoning_parse.json +0 -112
- package/scenarios.bak.1774780058/data/tool_oa.json +0 -96
- package/scenarios.bak.1774780058/data/tool_xai.json +0 -59
- package/scenarios.bak.1774780058/data/tool_xai2.json +0 -40
- package/scenarios.bak.1774780058/data/xai-response-1.json +0 -59
- package/scenarios.bak.1774780058/data/xai-response-2.json +0 -10
- package/scenarios.bak.1774780058/data/xai_reasoning_tools_resp.json +0 -59
- package/scenarios.bak.1774780058/data/xai_search_response.json +0 -58
- package/scenarios.bak.1774780058/environment.js +0 -10
- package/scenarios.bak.1774780058/example.js +0 -17
- package/scenarios.bak.1774780058/genericToolset.test.js +0 -182
- package/scenarios.bak.1774780058/grok.js +0 -113
- package/scenarios.bak.1774780058/memory-tools.js +0 -51
- package/scenarios.bak.1774780058/openai-o3.js +0 -137
- package/scenarios.bak.1774780058/openai-prompt.js +0 -155
- package/scenarios.bak.1774780058/openai-session.js +0 -148
- package/scenarios.bak.1774780058/openai.js +0 -102
- package/scenarios.bak.1774780058/prompt.js +0 -118
- package/scenarios.bak.1774780058/promptFishbowl.js +0 -76
- package/scenarios.bak.1774780058/search.brave.com.js +0 -25
- package/scenarios.bak.1774780058/sh.js +0 -15
- package/scenarios.bak.1774780058/test-wsio.js +0 -26
- package/scenarios.bak.1774780058/testToolset.js +0 -42
- package/scenarios.bak.1774780058/toolset.js +0 -16
- package/scenarios.bak.1774780058/toolset.test.js +0 -141
- package/scenarios.bak.1774780058/write_file_syntax.test.js +0 -145
- package/scenarios.bak.1774780058/write_file_validation/README.md +0 -30
- package/scenarios.bak.1774780058/write_file_validation/bad.js +0 -3
- package/scenarios.bak.1774780058/write_file_validation/good.js +0 -4
- package/scenarios.bak.1774780058/write_file_validation/test.sh +0 -43
- package/scenarios.bak.1774780058/wsClient.js +0 -69
- package/scenarios.bak.1774780058/xai_responses.integration.test.js +0 -57
- package/scenarios.bak.1774780058/xai_responses.test.js +0 -154
- package/scenarios.bak.1774780058/xaicoll.js +0 -50
- package/scenarios.bak.1774780058/xaifiles.js +0 -48
- /package/{examples → agents}/code_agent.js +0 -0
- /package/{examples → agents}/readme_agent.js +0 -0
package/lib/AgentServer.js
CHANGED
|
@@ -4,65 +4,76 @@ const TIMEOUT = 30000;
|
|
|
4
4
|
const MAX_INTERVALS = 20; // 10 minutes (30 sec * 20)
|
|
5
5
|
|
|
6
6
|
let ws;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @module lib/AgentServer
|
|
10
|
+
* @exports default AgentServer
|
|
11
|
+
* Dynamic WebSocket server for AI agents. Registers connected clients as dynamic tool calls in the Prompt's ToolSet.
|
|
12
|
+
* Handles authentication, user/agent messages, sessions, resets, and broadcasts.
|
|
13
|
+
*/
|
|
14
|
+
|
|
7
15
|
/**
|
|
8
|
-
@typedef
|
|
9
|
-
@typedef {import('./Prompt.js').default} Prompt
|
|
10
|
-
@typedef {import('./ToolSet.js').default} ToolSet
|
|
11
|
-
@typedef {import('./Session.js').default} Session
|
|
12
|
-
*/
|
|
16
|
+
* @typedef {import('@j-o-r/apiserver/types/ClientWrapper.d.ts').ClientWrapper} Client
|
|
17
|
+
* @typedef {import('./Prompt.js').default} Prompt
|
|
18
|
+
* @typedef {import('./ToolSet.js').default} ToolSet
|
|
19
|
+
* @typedef {import('./Session.js').default} Session
|
|
20
|
+
*/
|
|
13
21
|
/**
|
|
14
|
-
* @
|
|
15
|
-
*/
|
|
22
|
+
* @typedef {string|object} MessageContent
|
|
23
|
+
*/
|
|
16
24
|
/**
|
|
17
|
-
* @
|
|
18
|
-
* @property {string} action
|
|
19
|
-
* @property {
|
|
20
|
-
|
|
25
|
+
* @typedef {Object} Message
|
|
26
|
+
* @property {string} action - Action type (must be in ACTIONS).
|
|
27
|
+
* @property {MessageContent} [content]
|
|
28
|
+
* @property {string} [id]
|
|
29
|
+
*/
|
|
21
30
|
/**
|
|
22
|
-
* @typedef {(conn: import('@j-o-r/apiserver/types/WebSocketServer.js').WebSocketConnection, req: any) => Promise
|
|
23
|
-
*/
|
|
31
|
+
* @typedef {(conn: import('@j-o-r/apiserver/types/WebSocketServer.js').WebSocketConnection, req: any) => Promise<boolean>} AuthFunction
|
|
32
|
+
*/
|
|
24
33
|
/**
|
|
25
|
-
* @typedef {Object} AgentServerOptions
|
|
26
|
-
* @property {string} name
|
|
27
|
-
* @property {string} secret -
|
|
28
|
-
* @property {string} description
|
|
29
|
-
* @property {Prompt} prompt
|
|
30
|
-
* @property {Session} session
|
|
31
|
-
* @property {AuthFunction} [auth]
|
|
32
|
-
* @property {number} [port] -
|
|
33
|
-
* @property {boolean} [debug
|
|
34
|
-
|
|
34
|
+
* @typedef {Object} AgentServerOptions
|
|
35
|
+
* @property {string} name - Server/agent name.
|
|
36
|
+
* @property {string} secret - Access secret (matches client secrets).
|
|
37
|
+
* @property {string} description - Server description.
|
|
38
|
+
* @property {Prompt} prompt - Prompt instance.
|
|
39
|
+
* @property {Session} session - Session manager.
|
|
40
|
+
* @property {AuthFunction} [auth] - Custom auth function.
|
|
41
|
+
* @property {number} [port=8000] - Listen port (>=1024).
|
|
42
|
+
* @property {boolean} [debug=false] - Verbose logging.
|
|
43
|
+
* @example { name: 'main-agent', prompt: myPrompt, session: mySession, secret: 'abc', port: 8001 }
|
|
44
|
+
*/
|
|
35
45
|
|
|
46
|
+
/** @const @type {readonly string[]} Supported WebSocket message actions. Use .includes() for validation. */
|
|
36
47
|
const ACTIONS = [
|
|
37
48
|
// Agent / Toolset function calls
|
|
38
|
-
'agent_introduction', //
|
|
39
|
-
'agent_error', //
|
|
40
|
-
'agent_query', //
|
|
41
|
-
'agent_response', //
|
|
42
|
-
//
|
|
43
|
-
'user_introduction', //
|
|
44
|
-
'user_request', //
|
|
45
|
-
'server_response', //
|
|
46
|
-
'server_error',
|
|
47
|
-
'user_reset', //
|
|
48
|
-
'user_sessionlist', //
|
|
49
|
-
'user_loadsession', //
|
|
50
|
-
'user_info', //
|
|
49
|
+
'agent_introduction', // Client introduces as tool
|
|
50
|
+
'agent_error', // Agent error response
|
|
51
|
+
'agent_query', // Query to agent
|
|
52
|
+
'agent_response', // Agent response
|
|
53
|
+
// User interactions
|
|
54
|
+
'user_introduction', // Client connect
|
|
55
|
+
'user_request', // User query
|
|
56
|
+
'server_response', // Server response to user
|
|
57
|
+
'server_error', // Server error to user
|
|
58
|
+
'user_reset', // Reset session
|
|
59
|
+
'user_sessionlist', // List sessions
|
|
60
|
+
'user_loadsession', // Load session
|
|
61
|
+
'user_info', // Server info
|
|
51
62
|
];
|
|
52
63
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// WS clients
|
|
64
|
+
// WS clients map: conn_id → agent name
|
|
57
65
|
const clients = new Map();
|
|
58
|
-
// Pending
|
|
59
|
-
const pendingResponses = new Map();
|
|
66
|
+
// Pending responses: `${conn_id}:${msg_id}` → {resolve, reject, timer}
|
|
67
|
+
const pendingResponses = new Map();
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
*
|
|
65
|
-
|
|
70
|
+
* @function deleteClient
|
|
71
|
+
* @private
|
|
72
|
+
* Removes a client from the toolset and clients map.
|
|
73
|
+
* @param {ToolSet|null} toolset - Prompt's toolset.
|
|
74
|
+
* @param {string} conn_id - Connection ID.
|
|
75
|
+
* @returns {void}
|
|
76
|
+
*/
|
|
66
77
|
const deleteClient = (toolset, conn_id) => {
|
|
67
78
|
if (!toolset) return;
|
|
68
79
|
const name = clients.get(conn_id);
|
|
@@ -72,13 +83,19 @@ const deleteClient = (toolset, conn_id) => {
|
|
|
72
83
|
toolset.delete(name);
|
|
73
84
|
}
|
|
74
85
|
}
|
|
75
|
-
}
|
|
86
|
+
};
|
|
87
|
+
|
|
76
88
|
/**
|
|
77
|
-
* @
|
|
78
|
-
* @
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
89
|
+
* @function addClient
|
|
90
|
+
* @private
|
|
91
|
+
* Registers a WS client as a dynamic tool in the toolset.
|
|
92
|
+
* Sends query via WS, awaits response with timeout.
|
|
93
|
+
* @param {ToolSet|null} toolset - Prompt's toolset.
|
|
94
|
+
* @param {string} conn_id - Connection ID.
|
|
95
|
+
* @param {string} name - Tool name.
|
|
96
|
+
* @param {string} description - Tool description.
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
82
99
|
const addClient = (toolset, conn_id, name, description) => {
|
|
83
100
|
if (!toolset) return;
|
|
84
101
|
toolset.add(
|
|
@@ -89,7 +106,7 @@ const addClient = (toolset, conn_id, name, description) => {
|
|
|
89
106
|
properties: {
|
|
90
107
|
query: {
|
|
91
108
|
type: 'string',
|
|
92
|
-
description: `Command, question or query for this
|
|
109
|
+
description: `Command, question or query for this particular expert well formed in natural language`
|
|
93
110
|
}
|
|
94
111
|
},
|
|
95
112
|
required: ['query']
|
|
@@ -101,7 +118,6 @@ const addClient = (toolset, conn_id, name, description) => {
|
|
|
101
118
|
const key = `${conn_id}:${msg_id}`;
|
|
102
119
|
clients.set(conn_id, name);
|
|
103
120
|
|
|
104
|
-
// Start timeout/interval and keep its handle so we can clear it on response
|
|
105
121
|
let intervals = 0;
|
|
106
122
|
const timer = setInterval(() => {
|
|
107
123
|
intervals++;
|
|
@@ -114,7 +130,6 @@ const addClient = (toolset, conn_id, name, description) => {
|
|
|
114
130
|
}
|
|
115
131
|
}, TIMEOUT);
|
|
116
132
|
|
|
117
|
-
// Store handlers and timer so onMessage can resolve/reject and clear the timer
|
|
118
133
|
pendingResponses.set(key, { resolve, reject, timer });
|
|
119
134
|
|
|
120
135
|
ws.sendToConnection(conn_id, JSON.stringify({
|
|
@@ -122,34 +137,66 @@ const addClient = (toolset, conn_id, name, description) => {
|
|
|
122
137
|
id: msg_id,
|
|
123
138
|
content: params.query
|
|
124
139
|
}));
|
|
125
|
-
})
|
|
126
|
-
}
|
|
127
|
-
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
};
|
|
128
144
|
|
|
129
|
-
|
|
145
|
+
/**
|
|
146
|
+
* AgentServer: Core WebSocket server class.
|
|
147
|
+
* Exposes connected agents as tools; handles user CLI/WS interactions, auth, sessions.
|
|
148
|
+
* Starts on localhost; broadcasts resets.
|
|
149
|
+
* Logs events via console; emits via Prompt.
|
|
150
|
+
* @emits tool_request - Tool calls (via #prompt).
|
|
151
|
+
* @emits tool_error - Tool failures (via #prompt).
|
|
152
|
+
* @example
|
|
153
|
+
* const server = new AgentServer({
|
|
154
|
+
* name: 'main',
|
|
155
|
+
* prompt: myPrompt,
|
|
156
|
+
* session: mySession,
|
|
157
|
+
* secret: 'abc',
|
|
158
|
+
* port: 8000,
|
|
159
|
+
* debug: true
|
|
160
|
+
* });
|
|
161
|
+
*/
|
|
130
162
|
class AgentServer {
|
|
163
|
+
/** @private @type {boolean} */
|
|
131
164
|
#debug = false;
|
|
165
|
+
/** @private @type {number} */
|
|
132
166
|
#port = 8000;
|
|
167
|
+
/** @private @type {string} */
|
|
133
168
|
#name = '';
|
|
134
|
-
|
|
169
|
+
/** @private @type {string} */
|
|
170
|
+
#secret = '';
|
|
171
|
+
/** @private @type {string} */
|
|
135
172
|
#description = '';
|
|
136
|
-
/** @type {Prompt} */
|
|
137
|
-
#prompt
|
|
138
|
-
/** @type {Session} */
|
|
139
|
-
#session
|
|
140
|
-
/**
|
|
173
|
+
/** @private @type {Prompt} */
|
|
174
|
+
#prompt;
|
|
175
|
+
/** @private @type {Session} */
|
|
176
|
+
#session;
|
|
177
|
+
/**
|
|
178
|
+
* @private
|
|
179
|
+
* Default auth: Checks 'wssrc_id' query param against #secret.
|
|
180
|
+
* @param {Object} conn - Connection object.
|
|
181
|
+
* @returns {Promise<boolean>}
|
|
182
|
+
*/
|
|
141
183
|
#auth = async (conn) => {
|
|
142
184
|
const q = conn.client.query;
|
|
143
185
|
const key = q.get('wssrc_id');
|
|
144
186
|
return key === this.#secret;
|
|
145
|
-
}
|
|
187
|
+
};
|
|
188
|
+
|
|
146
189
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
190
|
+
* Initializes and starts the AgentServer.
|
|
191
|
+
* Validates port, sets up Prompt event logging, launches WS server.
|
|
192
|
+
* @param {AgentServerOptions} options - Server configuration.
|
|
193
|
+
* @throws {Error} If port < 1024.
|
|
194
|
+
* @example See AgentServerOptions.
|
|
195
|
+
*/
|
|
149
196
|
constructor(options) {
|
|
150
197
|
if (options.port) {
|
|
151
198
|
if (options.port < 1024) {
|
|
152
|
-
throw new Error('Port must be higher
|
|
199
|
+
throw new Error('Port must be higher than 1024');
|
|
153
200
|
}
|
|
154
201
|
this.#port = options.port;
|
|
155
202
|
}
|
|
@@ -161,12 +208,12 @@ class AgentServer {
|
|
|
161
208
|
this.#description = options.description;
|
|
162
209
|
this.#session = options.session;
|
|
163
210
|
this.#prompt = options.prompt;
|
|
164
|
-
this.#debug = options.debug;
|
|
165
|
-
|
|
211
|
+
this.#debug = options.debug ?? false;
|
|
212
|
+
|
|
213
|
+
// Log Prompt events
|
|
166
214
|
const events = Object.keys(this.#prompt.EVENTS);
|
|
167
215
|
events.forEach((evt) => {
|
|
168
216
|
this.#prompt.on(evt, (_msg) => {
|
|
169
|
-
// log events
|
|
170
217
|
console.log(`** ${this.#name} e:${evt}**`);
|
|
171
218
|
if (evt === 'tool_request') {
|
|
172
219
|
console.log(`tool execute: ${this.#name} ${_msg.name} ${_msg.call_id}`);
|
|
@@ -176,18 +223,19 @@ class AgentServer {
|
|
|
176
223
|
}
|
|
177
224
|
});
|
|
178
225
|
});
|
|
179
|
-
|
|
226
|
+
|
|
227
|
+
// Async start
|
|
180
228
|
(async () => {
|
|
181
229
|
await this._start();
|
|
182
230
|
})();
|
|
183
231
|
}
|
|
232
|
+
|
|
184
233
|
/**
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
*/
|
|
234
|
+
* @private
|
|
235
|
+
* Launches the WS server with handlers for messages, connections, closes.
|
|
236
|
+
* Defines HTTP / API index endpoint.
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
191
239
|
async _start() {
|
|
192
240
|
const name = this.#name;
|
|
193
241
|
const description = this.#description;
|
|
@@ -195,8 +243,8 @@ class AgentServer {
|
|
|
195
243
|
const DEBUG = this.#debug;
|
|
196
244
|
const API = {
|
|
197
245
|
/**
|
|
198
|
-
|
|
199
|
-
|
|
246
|
+
* @param {Client} client
|
|
247
|
+
*/
|
|
200
248
|
index: async (client) => {
|
|
201
249
|
await client.serve(200, { name, description });
|
|
202
250
|
}
|
|
@@ -205,18 +253,16 @@ class AgentServer {
|
|
|
205
253
|
verbose: this.#debug,
|
|
206
254
|
onMessage: async (conn, msg) => {
|
|
207
255
|
if (!msg.action || !ACTIONS.includes(msg.action)) {
|
|
208
|
-
console.log('
|
|
209
|
-
deleteClient(this.#prompt.toolset, conn.id)
|
|
256
|
+
console.log('Unknown action: ' + msg.action);
|
|
257
|
+
deleteClient(this.#prompt.toolset, conn.id);
|
|
210
258
|
ws.closeConnection(conn.id);
|
|
211
259
|
return;
|
|
212
260
|
}
|
|
213
261
|
if (msg.action === 'agent_introduction') {
|
|
214
|
-
// Register Toolset Agent
|
|
215
|
-
// CLient is added as a Toolset function call
|
|
216
262
|
const name = msg.name;
|
|
217
263
|
const content = (typeof msg.content === 'string') ? msg.content : JSON.stringify(msg.content);
|
|
218
264
|
if (!content) {
|
|
219
|
-
deleteClient(this.#prompt.toolset, conn.id)
|
|
265
|
+
deleteClient(this.#prompt.toolset, conn.id);
|
|
220
266
|
ws.closeConnection(conn.id);
|
|
221
267
|
return;
|
|
222
268
|
}
|
|
@@ -224,7 +270,7 @@ class AgentServer {
|
|
|
224
270
|
return;
|
|
225
271
|
}
|
|
226
272
|
if (msg.action === 'agent_response') {
|
|
227
|
-
const key = `${conn.id}:${msg.id}`;
|
|
273
|
+
const key = `${conn.id}:${msg.id}`;
|
|
228
274
|
const pending = pendingResponses.get(key);
|
|
229
275
|
if (pending) {
|
|
230
276
|
try { clearInterval(pending.timer); } catch { }
|
|
@@ -234,7 +280,7 @@ class AgentServer {
|
|
|
234
280
|
return;
|
|
235
281
|
}
|
|
236
282
|
if (msg.action === 'agent_error') {
|
|
237
|
-
const key = `${conn.id}:${msg.id}`;
|
|
283
|
+
const key = `${conn.id}:${msg.id}`;
|
|
238
284
|
const pending = pendingResponses.get(key);
|
|
239
285
|
if (pending) {
|
|
240
286
|
try { clearInterval(pending.timer); } catch { }
|
|
@@ -243,21 +289,17 @@ class AgentServer {
|
|
|
243
289
|
}
|
|
244
290
|
return;
|
|
245
291
|
}
|
|
246
|
-
//
|
|
247
|
-
// CLI / WS interaction to MAIN
|
|
292
|
+
// User interactions
|
|
248
293
|
if (msg.action === 'user_request') {
|
|
249
|
-
const action = `user_reponse`;
|
|
250
294
|
const id = msg.id;
|
|
251
295
|
if (!msg.content || msg.content.trim() === '') {
|
|
252
|
-
// Empty message, empty reponse
|
|
253
296
|
ws.sendToConnection(conn.id, JSON.stringify({
|
|
254
|
-
action: '
|
|
297
|
+
action: 'server_response',
|
|
255
298
|
id,
|
|
256
299
|
content: ''
|
|
257
300
|
}));
|
|
258
301
|
return;
|
|
259
302
|
}
|
|
260
|
-
// Bind to THIS prompt
|
|
261
303
|
console.log(`user_request:>>\n${msg.content}\n`);
|
|
262
304
|
try {
|
|
263
305
|
const content = await this.#prompt.call(msg.content);
|
|
@@ -266,14 +308,14 @@ class AgentServer {
|
|
|
266
308
|
id,
|
|
267
309
|
content
|
|
268
310
|
}));
|
|
269
|
-
console.log(`
|
|
311
|
+
console.log(`server_response:>>\n${content}\n`);
|
|
270
312
|
} catch (e) {
|
|
271
313
|
console.log(`server_error:>>\n`);
|
|
272
314
|
console.error(e);
|
|
273
315
|
ws.sendToConnection(conn.id, JSON.stringify({
|
|
274
316
|
action: 'server_error',
|
|
275
317
|
id,
|
|
276
|
-
e
|
|
318
|
+
e: String(e)
|
|
277
319
|
}));
|
|
278
320
|
}
|
|
279
321
|
}
|
|
@@ -296,7 +338,6 @@ class AgentServer {
|
|
|
296
338
|
content: sess
|
|
297
339
|
}));
|
|
298
340
|
}
|
|
299
|
-
|
|
300
341
|
if (msg.action === 'user_loadsession') {
|
|
301
342
|
const id = msg.id;
|
|
302
343
|
const selected = msg.content;
|
|
@@ -315,8 +356,7 @@ class AgentServer {
|
|
|
315
356
|
}
|
|
316
357
|
}
|
|
317
358
|
}
|
|
318
|
-
}
|
|
319
|
-
// Resume execution
|
|
359
|
+
}
|
|
320
360
|
await this.#prompt.triggerRequest();
|
|
321
361
|
ws.sendToConnection(conn.id, JSON.stringify({
|
|
322
362
|
action: 'server_sessionloaded',
|
|
@@ -341,10 +381,8 @@ class AgentServer {
|
|
|
341
381
|
},
|
|
342
382
|
onClose: async (conn, reason) => {
|
|
343
383
|
deleteClient(this.#prompt.toolset, conn.id);
|
|
344
|
-
|
|
345
384
|
},
|
|
346
385
|
onError: async (conn, error) => {
|
|
347
|
-
// console.error(error);
|
|
348
386
|
deleteClient(this.#prompt.toolset, conn.id);
|
|
349
387
|
ws.closeConnection(conn.id);
|
|
350
388
|
}
|
|
@@ -352,10 +390,12 @@ class AgentServer {
|
|
|
352
390
|
await server.create('v1', { port, host: '127.0.0.1', ws, strict: DEBUG, verbose: DEBUG }, API);
|
|
353
391
|
console.log(`Websocket server: ws://127.0.0.1:${port}/ws`);
|
|
354
392
|
}
|
|
393
|
+
|
|
355
394
|
/**
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
395
|
+
* Broadcasts a reset message to all connected clients (starts new sessions).
|
|
396
|
+
* @returns {void}
|
|
397
|
+
* @fires reset - Via underlying prompt/clients.
|
|
398
|
+
*/
|
|
359
399
|
resetAll() {
|
|
360
400
|
ws.broadcast({ action: 'reset' });
|
|
361
401
|
}
|