@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/Cli.js
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
import cli from '@j-o-r/cli';
|
|
2
2
|
import { SH } from '@j-o-r/sh';
|
|
3
3
|
// import audio2text from './tts/openai.com/generate.js';
|
|
4
|
+
|
|
4
5
|
/**
|
|
5
|
-
* @typedef {import('./API/openai.com/reponses/text.js').request} OARequest
|
|
6
|
-
* @typedef {import('./API/x.ai/text.js').request} XRequest
|
|
7
|
-
* @typedef {import('./API/anthropic.com/text.js').request} ANTHRequest
|
|
8
|
-
*
|
|
9
|
-
* @typedef {import('./API/x.ai/text.js').XOptions} XOptions
|
|
10
|
-
* @typedef {import('./API/openai.com/reponses/text.js').OAOptions} OAOptions
|
|
11
|
-
* @typedef {import('./API/anthropic.com/text.js').ANTHOptions} ANTHOptions
|
|
12
|
-
*
|
|
13
|
-
* @typedef {import('./Prompt.js').default} Prompt
|
|
14
|
-
* @typedef {import('./Session.js').default} Session
|
|
15
|
-
* @typedef {import('./ToolSet.js').default} ToolSet
|
|
16
|
-
*/
|
|
6
|
+
* @typedef {import('./API/openai.com/reponses/text.js').request} OARequest
|
|
7
|
+
* @typedef {import('./API/x.ai/text.js').request} XRequest
|
|
8
|
+
* @typedef {import('./API/anthropic.com/text.js').request} ANTHRequest
|
|
9
|
+
*
|
|
10
|
+
* @typedef {import('./API/x.ai/text.js').XOptions} XOptions
|
|
11
|
+
* @typedef {import('./API/openai.com/reponses/text.js').OAOptions} OAOptions
|
|
12
|
+
* @typedef {import('./API/anthropic.com/text.js').ANTHOptions} ANTHOptions
|
|
13
|
+
*
|
|
14
|
+
* @typedef {import('./Prompt.js').default} Prompt
|
|
15
|
+
* @typedef {import('./Session.js').default} Session
|
|
16
|
+
* @typedef {import('./ToolSet.js').default} ToolSet
|
|
17
|
+
*/
|
|
17
18
|
/**
|
|
19
|
+
* Configuration options for initializing the Cli class.
|
|
18
20
|
* @typedef {Object} CLIOptions
|
|
19
|
-
* @property {Prompt} prompt - The
|
|
20
|
-
* @property {Session} session - Session
|
|
21
|
-
* @property {ToolSet} [toolset] -
|
|
22
|
-
* @property {string} [description] - Custom introduction message.
|
|
23
|
-
* @property {string} [help] - Custom help message.
|
|
21
|
+
* @property {Prompt} prompt - The Prompt instance for handling AI interactions.
|
|
22
|
+
* @property {Session} session - The Session instance for managing conversation history.
|
|
23
|
+
* @property {ToolSet} [toolset] - Optional ToolSet for tool integrations.
|
|
24
|
+
* @property {string} [description] - Custom introduction message (appended to default INTRO).
|
|
25
|
+
* @property {string} [help] - Custom help message (overrides default HELP).
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
|
-
|
|
28
|
+
/** @const {string} Short introduction with basic shortcuts. */
|
|
27
29
|
const INTRO = `
|
|
28
30
|
Use:
|
|
29
31
|
- "ALT + d" to exit.
|
|
30
32
|
- "ALT + k" Show shortcuts /help
|
|
31
33
|
`;
|
|
32
34
|
|
|
35
|
+
/** @const {string} Error message template (customizable via options.help). */
|
|
33
36
|
const ERROR = `
|
|
34
|
-
|
|
37
|
+
`;
|
|
35
38
|
|
|
39
|
+
/** @const {string} Full help text with shortcuts and prompt commands. */
|
|
36
40
|
const HELP = `
|
|
37
41
|
|
|
38
42
|
Use:
|
|
@@ -57,23 +61,24 @@ e.g. >edit< , >paste< , >#!bash [command]<
|
|
|
57
61
|
`;
|
|
58
62
|
|
|
59
63
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
64
|
+
* CLI Interface for interactive AI conversations.
|
|
65
|
+
*
|
|
66
|
+
* Integrates with {@link Prompt}, {@link Session}, and optionally {@link ToolSet} to provide a terminal-based chat interface.
|
|
67
|
+
* Supports keyboard shortcuts (Alt + key), session management, error handling, and real-time message streaming.
|
|
68
|
+
*
|
|
69
|
+
* Key features:
|
|
70
|
+
* - Rich text output with roles (user/assistant/util/error/log).
|
|
71
|
+
* - Event-driven updates from Prompt (messages, truncated, finished).
|
|
72
|
+
* - Built-in help, reset, load session, copy last message (Alt+m).
|
|
73
|
+
* - Customizable intro/help via options.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const cli = new Cli({ prompt: myPrompt, session: mySession });
|
|
77
|
+
* await cli.start('Hello, AI!');
|
|
78
|
+
*
|
|
79
|
+
* @fires Prompt#message
|
|
80
|
+
* @fires Prompt#truncated
|
|
81
|
+
* @fires Prompt#finished
|
|
77
82
|
*/
|
|
78
83
|
class Cli {
|
|
79
84
|
#HELP = HELP;
|
|
@@ -84,22 +89,30 @@ class Cli {
|
|
|
84
89
|
#prompt;
|
|
85
90
|
#description = '';
|
|
86
91
|
#lastMessage = '';
|
|
92
|
+
|
|
87
93
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
94
|
+
* Initializes the Cli instance with required Prompt and Session.
|
|
95
|
+
* Sets up CLI roles, key mappings, and event listeners on Prompt.
|
|
96
|
+
* @param {CLIOptions} options - Configuration options.
|
|
97
|
+
* @throws {Error} If required prompt or session is missing.
|
|
90
98
|
*/
|
|
91
99
|
constructor(options) {
|
|
100
|
+
if (!options.prompt || !options.session) {
|
|
101
|
+
throw new Error('Cli requires valid prompt and session options.');
|
|
102
|
+
}
|
|
92
103
|
this.#prompt = options.prompt;
|
|
93
104
|
this.#session = options.session;
|
|
94
|
-
this.#description =
|
|
95
|
-
this.#HELP =
|
|
105
|
+
this.#description = options.description ? options.description + '\n\n' + INTRO : INTRO;
|
|
106
|
+
this.#HELP = options.help || HELP;
|
|
96
107
|
this.#setupCLI();
|
|
97
108
|
}
|
|
98
109
|
|
|
99
|
-
|
|
110
|
+
/** @private */
|
|
111
|
+
#setupCLI() {
|
|
100
112
|
cli.setRole('assistant', `${this.#session.name}: > `, 'brightGreen');
|
|
101
113
|
cli.setRole('util', ``, 'brightYellow');
|
|
102
114
|
cli.setRole('error', ``, 'red');
|
|
115
|
+
cli.setRole('log', '', 'brightBlue');
|
|
103
116
|
// @ts-ignore
|
|
104
117
|
cli.inputHandler = async (s, role) => await this.#handleInput(s, role);
|
|
105
118
|
cli.registerKeyMappings([
|
|
@@ -109,34 +122,31 @@ class Cli {
|
|
|
109
122
|
await this.reset();
|
|
110
123
|
}
|
|
111
124
|
},
|
|
112
|
-
{ // load a previous
|
|
125
|
+
{ // load a previous session
|
|
113
126
|
name: 's', ctrl: false, meta: true, shift: false, handler: async () => {
|
|
114
127
|
await this.loadSession();
|
|
115
128
|
}
|
|
116
129
|
},
|
|
117
|
-
{ //
|
|
130
|
+
{ // show session/prompt info
|
|
118
131
|
name: 'i', ctrl: false, meta: true, shift: false, handler: async () => {
|
|
119
132
|
this.printInfo();
|
|
120
|
-
// cli.focus('user', true);
|
|
121
133
|
}
|
|
122
134
|
},
|
|
123
135
|
{ // Copy last message to clipboard
|
|
124
136
|
name: 'm', ctrl: false, meta: true, shift: false, handler: async () => {
|
|
125
137
|
await copyToClipboard(this.#lastMessage);
|
|
126
138
|
cli.focus('util');
|
|
127
|
-
cli.write('copied');
|
|
128
|
-
// cli.focus('user', true);
|
|
139
|
+
cli.write('Last message copied to clipboard.');
|
|
129
140
|
}
|
|
130
141
|
},
|
|
131
142
|
{ // Show help /keys
|
|
132
143
|
name: 'k', ctrl: false, meta: true, shift: false, handler: async () => {
|
|
133
|
-
cli.focus('util')
|
|
134
|
-
cli.write(HELP)
|
|
135
|
-
// cli.focus('user', true);
|
|
144
|
+
cli.focus('util');
|
|
145
|
+
cli.write(this.#HELP);
|
|
136
146
|
}
|
|
137
147
|
}
|
|
138
148
|
]);
|
|
139
|
-
//
|
|
149
|
+
// Event listeners for Prompt updates
|
|
140
150
|
this.#prompt.on('message', (msg) => {
|
|
141
151
|
let write = false;
|
|
142
152
|
if (msg.role === 'reasoning') {
|
|
@@ -150,7 +160,7 @@ class Cli {
|
|
|
150
160
|
cli.write(content);
|
|
151
161
|
write = true;
|
|
152
162
|
} else if (msg.role === 'assistant') {
|
|
153
|
-
if (msg.content[0]
|
|
163
|
+
if (msg.content[0]?.type === 'text') {
|
|
154
164
|
const content = this.#prompt.contentToString(msg.content);
|
|
155
165
|
if (content.trim() !== '') {
|
|
156
166
|
cli.focus('assistant');
|
|
@@ -160,63 +170,67 @@ class Cli {
|
|
|
160
170
|
}
|
|
161
171
|
}
|
|
162
172
|
if (write) {
|
|
163
|
-
|
|
173
|
+
cli.startSpinner();
|
|
164
174
|
}
|
|
165
175
|
});
|
|
166
176
|
this.#prompt.on('truncated', () => {
|
|
167
177
|
cli.focus('log');
|
|
168
|
-
cli.write(
|
|
169
|
-
// cli.startSpinner();
|
|
178
|
+
cli.write('--- prompt truncated ---');
|
|
170
179
|
});
|
|
171
180
|
this.#prompt.on('finished', () => {
|
|
172
181
|
cli.focus('user', true);
|
|
173
182
|
});
|
|
174
|
-
// console.log(this.#prompt);
|
|
175
183
|
}
|
|
176
184
|
|
|
177
185
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
186
|
+
* Handles user input, processes via Prompt.call(), captures last message, and manages errors.
|
|
187
|
+
* Only processes 'user' role inputs.
|
|
188
|
+
* @private
|
|
189
|
+
* @param {string} s - Input string.
|
|
190
|
+
* @param {string} role - Input role (e.g., 'user').
|
|
180
191
|
*/
|
|
181
192
|
async #handleInput(s, role) {
|
|
182
|
-
if (!s || s === '')
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
this.#lastMessage = await this.#prompt.call(s)
|
|
190
|
-
} catch (error) {
|
|
191
|
-
// @ts-ignore
|
|
192
|
-
await this.#handleError(error);
|
|
193
|
-
}
|
|
193
|
+
if (!s || s.trim() === '') return;
|
|
194
|
+
if (role !== 'user') return;
|
|
195
|
+
cli.startSpinner();
|
|
196
|
+
try {
|
|
197
|
+
this.#lastMessage = await this.#prompt.call(s);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
await this.#handleError(error);
|
|
194
200
|
}
|
|
195
201
|
}
|
|
196
202
|
|
|
197
203
|
/**
|
|
198
|
-
*
|
|
204
|
+
* Displays error message and focuses back to user input.
|
|
205
|
+
* @private
|
|
206
|
+
* @param {Error} error - The error to handle.
|
|
199
207
|
*/
|
|
200
208
|
async #handleError(error) {
|
|
201
|
-
cli.error(error);
|
|
209
|
+
cli.error(error.message || error);
|
|
202
210
|
cli.focus('util');
|
|
203
|
-
cli.write(this.#ERROR);
|
|
204
|
-
// this.reset();
|
|
211
|
+
cli.write(this.#ERROR || 'An error occurred. Try Alt+r to reset.');
|
|
205
212
|
cli.focus('user', true);
|
|
206
213
|
}
|
|
207
214
|
|
|
208
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Prints detailed info about current session and prompt, plus description.
|
|
217
|
+
* Triggered by Alt+i.
|
|
218
|
+
*/
|
|
219
|
+
printInfo() {
|
|
209
220
|
const sessionInfo = this.#session.info();
|
|
210
221
|
const promptInfo = this.#prompt.info();
|
|
211
|
-
const info = `\nSESSION\n${sessionInfo}\nPROMPT\n${promptInfo}\n${this.#description}`;
|
|
222
|
+
const info = `\nSESSION\n${sessionInfo}\n\nPROMPT\n${promptInfo}\n\n${this.#description}`;
|
|
212
223
|
cli.focus('util');
|
|
213
224
|
cli.write(info);
|
|
214
|
-
|
|
225
|
+
}
|
|
215
226
|
|
|
216
227
|
/**
|
|
217
|
-
* Starts the CLI
|
|
218
|
-
* @param {string} [s] -
|
|
228
|
+
* Starts the CLI: Shows intro/description, focuses user input, optionally processes initial message.
|
|
229
|
+
* @param {string} [s] - Optional initial user message to process.
|
|
219
230
|
* @returns {Promise<void>}
|
|
231
|
+
* @example
|
|
232
|
+
* await cli.start(); // Interactive mode
|
|
233
|
+
* await cli.start('What is the weather?'); // Start with query
|
|
220
234
|
*/
|
|
221
235
|
async start(s) {
|
|
222
236
|
cli.focus('util');
|
|
@@ -226,11 +240,17 @@ class Cli {
|
|
|
226
240
|
await this.#handleInput(s, 'user');
|
|
227
241
|
}
|
|
228
242
|
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Loads a previous session by user selection, replays non-sticky messages, and triggers request.
|
|
246
|
+
* Lists sessions in reverse chronological order + 'NONE'.
|
|
247
|
+
* Triggered by Alt+s.
|
|
248
|
+
* @returns {Promise<void>}
|
|
249
|
+
*/
|
|
229
250
|
async loadSession() {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const selected = await cli.select('Select your session: ', sess);
|
|
251
|
+
const sessions = this.#session.sessionList().reverse();
|
|
252
|
+
sessions.push('NONE');
|
|
253
|
+
const selected = await cli.select('Select session: ', sessions);
|
|
234
254
|
if (!selected || selected === 'NONE') {
|
|
235
255
|
cli.focus('user', true);
|
|
236
256
|
return;
|
|
@@ -239,25 +259,38 @@ class Cli {
|
|
|
239
259
|
const messages = this.#session.set(selected);
|
|
240
260
|
this.#prompt.messages = messages;
|
|
241
261
|
messages.forEach((msg) => {
|
|
242
|
-
if (!msg.sticky) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
cli.focus(msg.role);
|
|
247
|
-
cli.write(content);
|
|
248
|
-
}
|
|
262
|
+
if (!msg.sticky && ['user', 'assistant', 'reasoning', 'log'].includes(msg.role)) {
|
|
263
|
+
const content = this.#prompt.contentToString(msg.content);
|
|
264
|
+
cli.focus(msg.role);
|
|
265
|
+
cli.write(content);
|
|
249
266
|
}
|
|
250
267
|
});
|
|
251
268
|
cli.startSpinner();
|
|
252
269
|
await this.#prompt.triggerRequest();
|
|
253
|
-
// cli.focus('user', true);
|
|
254
270
|
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Resets the prompt (clears context), reprints info, triggered by Alt+r.
|
|
274
|
+
* @returns {Promise<void>}
|
|
275
|
+
*/
|
|
255
276
|
async reset() {
|
|
256
|
-
this.#prompt.reset();
|
|
257
|
-
// this.start();
|
|
277
|
+
await this.#prompt.reset();
|
|
258
278
|
this.printInfo();
|
|
259
|
-
|
|
279
|
+
cli.focus('user', true);
|
|
260
280
|
}
|
|
261
281
|
}
|
|
262
282
|
|
|
283
|
+
/**
|
|
284
|
+
* Copies text to the system clipboard using xclip.
|
|
285
|
+
* @param {string} text - Text to copy (non-empty string).
|
|
286
|
+
* @returns {Promise<void>}
|
|
287
|
+
* @throws {Error} If xclip fails or invalid input.
|
|
288
|
+
*/
|
|
289
|
+
const copyToClipboard = async (text) => {
|
|
290
|
+
if (typeof text !== 'string' || text === '') return;
|
|
291
|
+
const params = ['-selection', 'clipboard'];
|
|
292
|
+
await SH`xclip ${params}`.options({ stdio: 'inherit' }).run(text);
|
|
293
|
+
};
|
|
294
|
+
|
|
263
295
|
export default Cli;
|
|
296
|
+
export { copyToClipboard };
|
package/lib/Prompt.js
CHANGED
|
@@ -290,13 +290,46 @@ const isValidFunctionResponse = (msg) => {
|
|
|
290
290
|
}
|
|
291
291
|
return false
|
|
292
292
|
}
|
|
293
|
+
|
|
293
294
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
295
|
+
* Manages a conversation history of standardized AI messages (text, image, audio, tool calls/responses)
|
|
296
|
+
* across different LLM providers (OpenAI, xAI, Anthropic). Inspired by OpenAI's chat format but extended.
|
|
297
|
+
*
|
|
298
|
+
* Key features:
|
|
299
|
+
* - Message validation and ordering (roles: system, user, assistant, tool, reasoning, log)
|
|
300
|
+
* - Context window management with automatic truncation (preserves sticky base prompts)
|
|
301
|
+
* - ToolSet integration via adapters for function calls
|
|
302
|
+
* - Token counting, records for billing/optimization
|
|
303
|
+
* - Event-driven lifecycle (e.g., 'message', 'start', 'finished', tool events)
|
|
304
|
+
*
|
|
305
|
+
* Typical usage:
|
|
306
|
+
* const prompt = new Prompt(4096);
|
|
307
|
+
* prompt.setAdaptor(openaiHandler, toolset, options);
|
|
308
|
+
* prompt.add('system', 'You are helpful.', true);
|
|
309
|
+
* const response = await prompt.call('Hello!');
|
|
310
|
+
*
|
|
311
|
+
* @extends {EventEmitter}
|
|
312
|
+
* @property {number} contextLength - Maximum token limit (getter)
|
|
313
|
+
* @property {Message[]} messages - Copy of conversation history (getter/setter)
|
|
314
|
+
* @property {ToolSet|null} toolset - Current toolset (getter)
|
|
315
|
+
* @property {boolean} hasSystemprompt - Whether system prompt exists (getter)
|
|
316
|
+
* @property {string} system_prompt - System prompt text (getter)
|
|
297
317
|
*
|
|
298
|
-
* @
|
|
299
|
-
* @
|
|
318
|
+
* @fires Prompt#start - Emitted when a request starts
|
|
319
|
+
* @fires Prompt#retrigger - Retrigger after message import
|
|
320
|
+
* @fires Prompt#finished - Request completed
|
|
321
|
+
* @fires Prompt#message - New message added to history
|
|
322
|
+
* @fires Prompt#record - Token usage record added
|
|
323
|
+
* @fires Prompt#truncated - Messages truncated due to context limit
|
|
324
|
+
* @fires Prompt#reset - Non-sticky messages cleared
|
|
325
|
+
* @fires Prompt#ready - Call/trigger ready
|
|
326
|
+
* @fires Prompt#error - Error occurred
|
|
327
|
+
* @fires Prompt#warning - Warning issued (e.g., token overflow)
|
|
328
|
+
* @fires Prompt#tool_request - Tool call initiated
|
|
329
|
+
* @fires Prompt#tool_response - Tool call completed
|
|
330
|
+
* @fires Prompt#tool_error - Tool call failed
|
|
331
|
+
* @fires Prompt#http_request - HTTP tool request starts
|
|
332
|
+
* @fires Prompt#http_response - HTTP tool response received
|
|
300
333
|
*/
|
|
301
334
|
class Prompt extends EventEmitter {
|
|
302
335
|
#tokenCount = 0;
|