@j-o-r/hello-dave 0.0.0

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 (53) hide show
  1. package/LICENSE +73 -0
  2. package/README.md +207 -0
  3. package/bin/hdAsk.js +103 -0
  4. package/bin/hdClear.js +13 -0
  5. package/bin/hdCode.js +110 -0
  6. package/bin/hdConnect.js +230 -0
  7. package/bin/hdInspect.js +28 -0
  8. package/bin/hdNpm.js +114 -0
  9. package/bin/hdPrompt.js +108 -0
  10. package/examples/claude-test.js +89 -0
  11. package/examples/claude.js +143 -0
  12. package/examples/gpt.js +127 -0
  13. package/examples/gpt_code.js +125 -0
  14. package/examples/gpt_note_keeping.js +117 -0
  15. package/examples/grok.js +119 -0
  16. package/examples/grok_code.js +114 -0
  17. package/examples/grok_note_keeping.js +111 -0
  18. package/lib/API/anthropic.com/text.js +402 -0
  19. package/lib/API/brave.com/search.js +239 -0
  20. package/lib/API/openai.com/README.md +1 -0
  21. package/lib/API/openai.com/reponses/MESSAGES.md +69 -0
  22. package/lib/API/openai.com/reponses/text.js +416 -0
  23. package/lib/API/x.ai/text.js +415 -0
  24. package/lib/AgentClient.js +197 -0
  25. package/lib/AgentManager.js +144 -0
  26. package/lib/AgentServer.js +336 -0
  27. package/lib/Cli.js +256 -0
  28. package/lib/Prompt.js +728 -0
  29. package/lib/Session.js +231 -0
  30. package/lib/ToolSet.js +186 -0
  31. package/lib/fafs.js +93 -0
  32. package/lib/genericToolset.js +170 -0
  33. package/lib/index.js +34 -0
  34. package/lib/promptHelpers.js +132 -0
  35. package/lib/testToolset.js +42 -0
  36. package/module.md +189 -0
  37. package/package.json +49 -0
  38. package/types/API/anthropic.com/text.d.ts +207 -0
  39. package/types/API/brave.com/search.d.ts +156 -0
  40. package/types/API/openai.com/reponses/text.d.ts +225 -0
  41. package/types/API/x.ai/text.d.ts +286 -0
  42. package/types/AgentClient.d.ts +70 -0
  43. package/types/AgentManager.d.ts +112 -0
  44. package/types/AgentServer.d.ts +38 -0
  45. package/types/Cli.d.ts +52 -0
  46. package/types/Prompt.d.ts +298 -0
  47. package/types/Session.d.ts +31 -0
  48. package/types/ToolSet.d.ts +95 -0
  49. package/types/fafs.d.ts +47 -0
  50. package/types/genericToolset.d.ts +3 -0
  51. package/types/index.d.ts +23 -0
  52. package/types/promptHelpers.d.ts +1 -0
  53. package/types/testToolset.d.ts +3 -0
package/lib/Cli.js ADDED
@@ -0,0 +1,256 @@
1
+ import cli from '@j-o-r/cli';
2
+ import { SH } from '@j-o-r/sh';
3
+ // import audio2text from './tts/openai.com/generate.js';
4
+ /**
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
+ */
17
+ /**
18
+ * @typedef {Object} CLIOptions
19
+ * @property {Prompt} prompt - The prompt session
20
+ * @property {Session} session - Session Cache
21
+ * @property {ToolSet} [toolset] - The toolset
22
+ * @property {string} [description] - Custom introduction message.
23
+ * @property {string} [help] - Custom help message.
24
+ */
25
+
26
+
27
+ const INTRO = `
28
+ Use:
29
+ - "ALT + d" to exit.
30
+ - "ALT + k" Show shortcuts /help
31
+ `;
32
+
33
+ const ERROR = `
34
+ `
35
+
36
+ const HELP = `
37
+
38
+ Use:
39
+ - "ALT + d" to exit.
40
+ - "ALT + r" to reset and start a new conversation.
41
+ - "ALT + s" to load a previous session.
42
+ - "ALT + k" Show shortcuts keys /help
43
+ - "ALT + i" Show info about the current session and prompt
44
+
45
+ prompt:
46
+
47
+ Special prompt commands for editing your prompt:
48
+ Commands are placed between ">" and "<" characters and are only parsed when on a single line.
49
+
50
+ e.g. >edit< , >paste< , >#!bash [command]<
51
+
52
+ > "Edit this text >edit<" : Opens an editor with the text before submitting
53
+ > ">edit<" : Opens an editor to formulate your question
54
+ > "Text with .. >paste< more text" : Opens an editor with the clipboard as content for further editing
55
+ > ">paste<" : Opens an editor with the clipboard as content for further editing
56
+ > "Question >#!bash ls -Fla< remark" : Executes a bash command and fill the editor with the response
57
+ `;
58
+
59
+ /**
60
+ * Copy text to the clipboard
61
+ * @param {string} text
62
+ * @retruns {Promise<string>}
63
+ */
64
+ const copyToClipboard = async (text) => {
65
+ if (typeof text !== 'string') return;
66
+ if (text === '') text = ' ';
67
+ const prams = [
68
+ '-selection',
69
+ 'clipboard'
70
+ ]
71
+ return SH`xclip ${prams}`.options({ stdio: 'inherit' }).run(text);
72
+ }
73
+
74
+
75
+ /**
76
+ * CLILoader class for handling CLI-based interactions with AI models.
77
+ */
78
+ class Cli {
79
+ #HELP = HELP;
80
+ #ERROR = ERROR;
81
+ /** @type {Session} */
82
+ #session;
83
+ /** @type {Prompt} */
84
+ #prompt;
85
+ #description = '';
86
+ #lastMessage = '';
87
+ /**
88
+ * Creates an instance of CLILoader.
89
+ * @param {CLIOptions} options - The options to configure the CLILoader.
90
+ */
91
+ constructor(options) {
92
+ this.#prompt = options.prompt;
93
+ this.#session = options.session;
94
+ this.#description = (options.description) ? options.description + INTRO : INTRO;
95
+ this.#HELP = (options.help) ? options.help : HELP;
96
+ this.#setupCLI();
97
+ }
98
+
99
+ #setupCLI = () => {
100
+ cli.setRole('assistant', `${this.#session.name}: > `, 'brightGreen');
101
+ cli.setRole('util', ``, 'brightYellow');
102
+ cli.setRole('error', ``, 'red');
103
+ // @ts-ignore
104
+ cli.inputHandler = async (s, role) => await this.#handleInput(s, role);
105
+ cli.registerKeyMappings([
106
+ { // reset the session, empty the context from the prompt
107
+ name: 'r', ctrl: false, meta: true, shift: false, handler: async () => {
108
+ cli.clear();
109
+ await this.reset();
110
+ }
111
+ },
112
+ { // load a previous ssession
113
+ name: 's', ctrl: false, meta: true, shift: false, handler: async () => {
114
+ await this.loadSession();
115
+ }
116
+ },
117
+ { // load a previous ssession
118
+ name: 'i', ctrl: false, meta: true, shift: false, handler: async () => {
119
+ this.printInfo();
120
+ // cli.focus('user', true);
121
+ }
122
+ },
123
+ { // Copy last message to clipboard
124
+ name: 'm', ctrl: false, meta: true, shift: false, handler: async () => {
125
+ await copyToClipboard(this.#lastMessage);
126
+ cli.focus('util');
127
+ cli.write('copied');
128
+ // cli.focus('user', true);
129
+ }
130
+ },
131
+ { // Show help /keys
132
+ name: 'k', ctrl: false, meta: true, shift: false, handler: async () => {
133
+ cli.focus('util')
134
+ cli.write(HELP)
135
+ // cli.focus('user', true);
136
+ }
137
+ }
138
+ ]);
139
+ // Write messages
140
+ this.#prompt.on('message', (msg) => {
141
+ if (msg.role === 'reasoning') {
142
+ const content = this.#prompt.contentToString(msg.content);
143
+ cli.focus('util');
144
+ cli.write(`REASONING: ---------\n${content}\n---------\n`);
145
+ } else if (msg.role === 'log') {
146
+ const content = this.#prompt.contentToString(msg.content);
147
+ cli.focus('log');
148
+ cli.write(content);
149
+ } else if (msg.role === 'assistant') {
150
+ if (msg.content[0].type === 'text') {
151
+ const content = this.#prompt.contentToString(msg.content);
152
+ if (content.trim() !== '') {
153
+ cli.focus('assistant');
154
+ cli.write(content);
155
+ }
156
+ }
157
+ }
158
+ });
159
+ this.#prompt.on('truncated', () => {
160
+ cli.focus('log');
161
+ cli.write(`--- prompt truncated ---`);
162
+ // cli.startSpinner();
163
+ });
164
+ this.#prompt.on('finished', () => {
165
+ cli.focus('user', true);
166
+ });
167
+ // console.log(this.#prompt);
168
+ }
169
+
170
+ /**
171
+ * @param {string} s
172
+ * @param {string} role
173
+ */
174
+ async #handleInput(s, role) {
175
+ if (!s || s === '') {
176
+ return;
177
+ }
178
+ // Only accept a users input
179
+ if (role === 'user') {
180
+ cli.startSpinner();
181
+ try {
182
+ this.#lastMessage = await this.#prompt.call(s)
183
+ } catch (error) {
184
+ // @ts-ignore
185
+ await this.#handleError(error);
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * @param {Error} error
192
+ */
193
+ async #handleError(error) {
194
+ cli.error(error);
195
+ cli.focus('util');
196
+ cli.write(this.#ERROR);
197
+ // this.reset();
198
+ cli.focus('user', true);
199
+ }
200
+
201
+ printInfo() {
202
+ const sessionInfo = this.#session.info();
203
+ const promptInfo = this.#prompt.info();
204
+ const info = `\nSESSION\n${sessionInfo}\nPROMPT\n${promptInfo}\n${this.#description}`;
205
+ cli.focus('util');
206
+ cli.write(info);
207
+ }
208
+
209
+ /**
210
+ * Starts the CLI interaction.
211
+ * @param {string} [s] - optional user string/question
212
+ * @returns {Promise<void>}
213
+ */
214
+ async start(s) {
215
+ cli.focus('util');
216
+ cli.write(this.#description);
217
+ cli.focus('user', true);
218
+ if (s && s.trim() !== '') {
219
+ await this.#handleInput(s, 'user');
220
+ }
221
+ }
222
+ async loadSession() {
223
+ // Last session last
224
+ const sess = this.#session.sessionList().reverse();
225
+ sess.push('NONE');
226
+ const selected = await cli.select('Select your session: ', sess);
227
+ if (!selected || selected === 'NONE') {
228
+ cli.focus('user', true);
229
+ return;
230
+ }
231
+ cli.clear();
232
+ const messages = this.#session.set(selected);
233
+ this.#prompt.messages = messages;
234
+ messages.forEach((msg) => {
235
+ if (!msg.sticky) {
236
+ if (['user', 'assistant', 'reasoning', 'log'].includes(msg.role)) {
237
+ const content = this.#prompt.contentToString(msg.content);
238
+ // @ts-ignore
239
+ cli.focus(msg.role);
240
+ cli.write(content);
241
+ }
242
+ }
243
+ });
244
+ cli.startSpinner();
245
+ await this.#prompt.triggerRequest();
246
+ // cli.focus('user', true);
247
+ }
248
+ async reset() {
249
+ this.#prompt.reset();
250
+ // this.start();
251
+ this.printInfo();
252
+ // cli.focus('user', true);
253
+ }
254
+ }
255
+
256
+ export default Cli;