henergyqueai 0.2.0 → 0.4.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.
- package/README.md +58 -22
- package/bin/henergyqueai.js +299 -23
- package/package.json +1 -1
- package/src/butterfly.js +134 -10
- package/src/client.js +1 -1
- package/src/confiance.js +50 -0
- package/src/reglages.js +122 -0
- package/src/sessions.js +39 -0
- package/src/ui.js +31 -0
package/README.md
CHANGED
|
@@ -18,42 +18,78 @@ henergyqueai
|
|
|
18
18
|
|
|
19
19
|
Votre navigateur s’ouvre, vous cliquez sur **Autoriser**, c’est fini. Aucune clé à copier.
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Au premier lancement dans un dossier, il demande si vous lui faites confiance. Répondez, et l’écran se nettoie pour lui laisser la place.
|
|
22
|
+
|
|
23
|
+
Dans une session, écrivez normalement pour discuter. Appuyez sur **Entrée sur une ligne vide** pour ouvrir le menu visuel, puis utilisez **↑** et **↓**, **Entrée** pour choisir et **Échap** pour revenir. Vous pouvez ainsi changer d’assistant ou de mode, voir l’état de la session, recommencer une conversation, changer de compte ou quitter sans mémoriser de commandes.
|
|
22
24
|
|
|
23
25
|
## Utiliser
|
|
24
26
|
|
|
25
27
|
```bash
|
|
26
|
-
henergyqueai
|
|
28
|
+
henergyqueai ouvre une session
|
|
27
29
|
henergyqueai "corrige le bug de date dans facture.js"
|
|
28
30
|
cat erreur.log | henergyqueai "d'où vient cette erreur ?"
|
|
29
|
-
henergyqueai
|
|
31
|
+
henergyqueai -c reprend la conversation de ce dossier
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
Il n’a accès qu’au dossier depuis lequel vous l’avez lancé.
|
|
33
35
|
|
|
34
36
|
## Options
|
|
35
37
|
|
|
36
|
-
| Option | Effet
|
|
37
|
-
| -------------------------------- |
|
|
38
|
-
| `--modele <aelyr\|oryne\|velys>` | Choisit l’assistant
|
|
39
|
-
| `--
|
|
40
|
-
| `--
|
|
38
|
+
| Option | Effet |
|
|
39
|
+
| -------------------------------- | ------------------------------------- |
|
|
40
|
+
| `--modele <aelyr\|oryne\|velys>` | Choisit l’assistant |
|
|
41
|
+
| `--mode <mode>` | Voir les modes ci-dessous |
|
|
42
|
+
| `--continuer`, `-c` | Reprend la conversation du dossier |
|
|
43
|
+
| `--direct`, `-p` | Répond sans rien enregistrer |
|
|
44
|
+
| `--lecture-seule` | Raccourci pour `--mode lecture-seule` |
|
|
45
|
+
| `--oui` | Raccourci pour `--mode sans-demander` |
|
|
41
46
|
|
|
42
|
-
##
|
|
47
|
+
## Modes
|
|
43
48
|
|
|
44
|
-
|
|
|
45
|
-
|
|
|
46
|
-
|
|
|
47
|
-
|
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
| `/quitter` | Sort |
|
|
49
|
+
| Mode | Ce qu’il peut faire |
|
|
50
|
+
| ------------------------ | ------------------------------------------------- |
|
|
51
|
+
| `demander` | Demande avant d’écrire ou d’exécuter. Par défaut. |
|
|
52
|
+
| `accepter-modifications` | Écrit sans demander, demande pour les commandes |
|
|
53
|
+
| `lecture-seule` | Ne peut ni écrire ni exécuter |
|
|
54
|
+
| `sans-demander` | Ne demande jamais rien |
|
|
51
55
|
|
|
52
|
-
##
|
|
56
|
+
## En session
|
|
57
|
+
|
|
58
|
+
Le menu au clavier est la méthode principale. Les commandes ci-dessous restent disponibles comme raccourcis et pour les terminaux non interactifs.
|
|
59
|
+
|
|
60
|
+
| Commande | Effet |
|
|
61
|
+
| ------------------------------- | ---------------------------------------------- |
|
|
62
|
+
| `/aide` | Liste les commandes |
|
|
63
|
+
| `/modele <aelyr\|oryne\|velys>` | Change d’assistant |
|
|
64
|
+
| `/mode <mode>` | Change ce qu’il peut faire |
|
|
65
|
+
| `/statut` | Ce qui est chargé : réglages, règles, échanges |
|
|
66
|
+
| `/effacer` | Oublie la conversation |
|
|
67
|
+
| `/connexion` | Change de compte |
|
|
68
|
+
| `/quitter` | Sort |
|
|
69
|
+
|
|
70
|
+
## Réglages
|
|
71
|
+
|
|
72
|
+
Deux fichiers, le plus proche l’emporte :
|
|
73
|
+
|
|
74
|
+
| Fichier | Portée |
|
|
75
|
+
| -------------------------------------- | ------------- |
|
|
76
|
+
| `~/.henergyqueai/settings.json` | Vous, partout |
|
|
77
|
+
| `<projet>/.henergyqueai/settings.json` | Ce projet |
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"modele": "oryne",
|
|
82
|
+
"mode": "demander",
|
|
83
|
+
"permissions": {
|
|
84
|
+
"autoriser": ["Executer(npm run test *)", "Lire"],
|
|
85
|
+
"refuser": ["Executer(rm *)", "Ecrire(.env*)"]
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
53
89
|
|
|
54
|
-
|
|
90
|
+
Une règle nomme un outil — `Lire`, `Ecrire`, `Executer`, `Lister`, `Chercher` — avec un motif facultatif où `*` remplace n’importe quoi. **Un refus l’emporte toujours sur une autorisation.**
|
|
55
91
|
|
|
56
|
-
|
|
92
|
+
Une règle autorisée dispense de la question. Une règle refusée arrête l’action avant qu’elle ne parte.
|
|
57
93
|
|
|
58
94
|
## Variables d’environnement
|
|
59
95
|
|
|
@@ -63,9 +99,9 @@ Lire un fichier, explorer le projet et chercher un texte se font librement.
|
|
|
63
99
|
| `HENERGYQUEAI_URL` | Adresse du service, pour un déploiement privé |
|
|
64
100
|
| `NO_COLOR` | Désactive les couleurs |
|
|
65
101
|
|
|
66
|
-
## Votre
|
|
102
|
+
## Votre accès
|
|
67
103
|
|
|
68
|
-
|
|
104
|
+
Chaque terminal autorisé crée une clé au nom de votre machine, visible dans **Paramètres → Clés d’API** avec sa date de dernière utilisation. Révocable à tout moment, sans toucher à vos autres machines.
|
|
69
105
|
|
|
70
106
|
## Licence
|
|
71
107
|
|
package/bin/henergyqueai.js
CHANGED
|
@@ -12,11 +12,20 @@ import {
|
|
|
12
12
|
prompt,
|
|
13
13
|
say,
|
|
14
14
|
style,
|
|
15
|
+
waiting,
|
|
15
16
|
write,
|
|
16
17
|
} from "../src/ui.js";
|
|
17
|
-
import { brand,
|
|
18
|
+
import { brand, flutter, still } from "../src/butterfly.js";
|
|
19
|
+
import { choose, clearScreen } from "../src/confiance.js";
|
|
20
|
+
import {
|
|
21
|
+
MODES,
|
|
22
|
+
loadSettings,
|
|
23
|
+
ruling,
|
|
24
|
+
saveUserSetting,
|
|
25
|
+
} from "../src/reglages.js";
|
|
26
|
+
import { lastSession, saveSession } from "../src/sessions.js";
|
|
18
27
|
|
|
19
|
-
const VERSION = "0.
|
|
28
|
+
const VERSION = "0.4.0";
|
|
20
29
|
const CONFIG = path.join(homedir(), ".henergyqueai", "config.json");
|
|
21
30
|
const DEFAULT_BASE = process.env.HENERGYQUEAI_URL || "https://henergyqueai.fr";
|
|
22
31
|
const MODELS = { aelyr: "Aelyr", oryne: "Oryne", velys: "Velys" };
|
|
@@ -40,23 +49,29 @@ async function saveConfig(config) {
|
|
|
40
49
|
|
|
41
50
|
function usage() {
|
|
42
51
|
say(`
|
|
43
|
-
${
|
|
52
|
+
${brand("henergyqueai")} ${style.dim(`v${VERSION}`)}
|
|
44
53
|
|
|
45
54
|
${style.bold("henergyqueai")} ouvre une session
|
|
46
55
|
${style.bold('henergyqueai "votre demande"')} répond puis rend la main
|
|
47
56
|
${style.dim('cat fichier | henergyqueai "…"')} lit l’entrée standard
|
|
48
57
|
|
|
49
58
|
${style.dim("--modele aelyr|oryne|velys")} choisit l’assistant
|
|
50
|
-
${style.dim("--
|
|
51
|
-
${style.dim("
|
|
59
|
+
${style.dim("--mode <mode>")} demander · accepter-modifications
|
|
60
|
+
${style.dim("")} lecture-seule · sans-demander
|
|
61
|
+
${style.dim("--continuer, -c")} reprend la conversation du dossier
|
|
62
|
+
${style.dim("--direct, -p")} répond sans rien enregistrer
|
|
52
63
|
|
|
53
|
-
${style.dim("En session :
|
|
64
|
+
${style.dim("En session : Entrée à vide ouvre le menu piloté avec ↑ et ↓")}
|
|
65
|
+
${style.dim("Les commandes /aide, /modele, /mode… restent disponibles.")}
|
|
66
|
+
${style.dim("Réglages : ~/.henergyqueai/settings.json ou .henergyqueai/settings.json")}
|
|
54
67
|
`);
|
|
55
68
|
}
|
|
56
69
|
|
|
57
70
|
// First run opens the browser, the way a sign-in should. Nothing to copy.
|
|
58
71
|
async function connect() {
|
|
59
|
-
say(
|
|
72
|
+
say("");
|
|
73
|
+
still().forEach((line) => say(` ${line}`));
|
|
74
|
+
say("");
|
|
60
75
|
say(style.dim(" Ouverture de votre navigateur pour autoriser ce terminal…"));
|
|
61
76
|
try {
|
|
62
77
|
const key = await awaitAuthorization({
|
|
@@ -79,18 +94,41 @@ async function connect() {
|
|
|
79
94
|
async function turn(session, messages) {
|
|
80
95
|
for (let round = 0; round < 24; round += 1) {
|
|
81
96
|
let printed = false;
|
|
97
|
+
let settle = session.headerActive ? flutter() : () => {};
|
|
98
|
+
const wait = waiting(
|
|
99
|
+
round === 0
|
|
100
|
+
? `${MODELS[session.modelId] || "L’assistant"} réfléchit`
|
|
101
|
+
: "Suite de la réponse",
|
|
102
|
+
);
|
|
103
|
+
let waitingVisible = true;
|
|
104
|
+
const finishWaiting = () => {
|
|
105
|
+
if (!waitingVisible) return;
|
|
106
|
+
waitingVisible = false;
|
|
107
|
+
wait.stop();
|
|
108
|
+
};
|
|
82
109
|
const answer = await ask({
|
|
83
110
|
base: session.base,
|
|
84
111
|
key: session.key,
|
|
85
112
|
messages,
|
|
86
113
|
tools: session.tools,
|
|
87
114
|
modelId: session.modelId,
|
|
115
|
+
onThinking: ({ tokens } = {}) => wait.update(tokens),
|
|
88
116
|
onDelta: (text) => {
|
|
117
|
+
finishWaiting();
|
|
118
|
+
if (settle) {
|
|
119
|
+
settle();
|
|
120
|
+
settle = null;
|
|
121
|
+
}
|
|
89
122
|
printed = true;
|
|
90
123
|
write(text);
|
|
91
124
|
},
|
|
125
|
+
}).finally(() => {
|
|
126
|
+
finishWaiting();
|
|
127
|
+
settle?.();
|
|
92
128
|
});
|
|
93
|
-
|
|
129
|
+
// A short answer can arrive whole, without a single streamed fragment.
|
|
130
|
+
if (!printed && answer.content) write(answer.content);
|
|
131
|
+
if (printed || answer.content) say();
|
|
94
132
|
|
|
95
133
|
if (!answer.toolCalls?.length) {
|
|
96
134
|
messages.push({ role: "assistant", content: answer.content });
|
|
@@ -117,8 +155,18 @@ async function turn(session, messages) {
|
|
|
117
155
|
activity(call.name, args);
|
|
118
156
|
|
|
119
157
|
let approved = true;
|
|
120
|
-
|
|
121
|
-
|
|
158
|
+
const verdict = ruling(session.permissions, call.name, args);
|
|
159
|
+
if (verdict === "refuse") {
|
|
160
|
+
messages.push({
|
|
161
|
+
role: "tool",
|
|
162
|
+
tool_call_id: call.id,
|
|
163
|
+
content: "Interdit par les règles du projet.",
|
|
164
|
+
});
|
|
165
|
+
say(style.dim(" refusé par une règle"));
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (needsApproval(call.name) && verdict !== "autorise") {
|
|
169
|
+
if (session.mode === "lecture-seule") {
|
|
122
170
|
messages.push({
|
|
123
171
|
role: "tool",
|
|
124
172
|
tool_call_id: call.id,
|
|
@@ -132,8 +180,12 @@ async function turn(session, messages) {
|
|
|
132
180
|
await preview(session.root, args.chemin, String(args.contenu ?? ""));
|
|
133
181
|
if (call.name === "executer" && args.raison)
|
|
134
182
|
say(style.dim(` ${args.raison}`));
|
|
183
|
+
const silent =
|
|
184
|
+
session.mode === "sans-demander" ||
|
|
185
|
+
(session.mode === "accepter-modifications" &&
|
|
186
|
+
call.name === "ecrire_fichier");
|
|
135
187
|
approved =
|
|
136
|
-
|
|
188
|
+
silent ||
|
|
137
189
|
(await confirm(
|
|
138
190
|
call.name === "ecrire_fichier"
|
|
139
191
|
? ` Écrire ${args.chemin} ?`
|
|
@@ -156,15 +208,184 @@ async function turn(session, messages) {
|
|
|
156
208
|
say(style.dim(" (trop d’étapes, arrêt)"));
|
|
157
209
|
}
|
|
158
210
|
|
|
211
|
+
async function trusted(root) {
|
|
212
|
+
const config = await loadConfig();
|
|
213
|
+
const known = Array.isArray(config.dossiers) ? config.dossiers : [];
|
|
214
|
+
if (known.includes(root)) return true;
|
|
215
|
+
if (!process.stdin.isTTY) return true;
|
|
216
|
+
|
|
217
|
+
say("");
|
|
218
|
+
say(` ${style.bold("Dossier de travail")}`);
|
|
219
|
+
say(` ${brand(root)}`);
|
|
220
|
+
say("");
|
|
221
|
+
say(
|
|
222
|
+
style.dim(
|
|
223
|
+
" Est-ce un projet que vous avez créé, ou en qui vous avez confiance ?",
|
|
224
|
+
),
|
|
225
|
+
);
|
|
226
|
+
say(
|
|
227
|
+
style.dim(
|
|
228
|
+
" L’assistant pourra y lire, y écrire et y exécuter des commandes.",
|
|
229
|
+
),
|
|
230
|
+
);
|
|
231
|
+
say("");
|
|
232
|
+
const answer = await choose(
|
|
233
|
+
["Non, quitter", "Oui, je fais confiance à ce dossier"],
|
|
234
|
+
{
|
|
235
|
+
paint: (option, active) =>
|
|
236
|
+
active
|
|
237
|
+
? ` ${brand("›")} ${style.bold(option)}`
|
|
238
|
+
: ` ${style.dim(option)}`,
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
say("");
|
|
242
|
+
say(style.dim(" Entrée pour confirmer · Échap pour annuler"));
|
|
243
|
+
if (!answer || answer.startsWith("Non")) return false;
|
|
244
|
+
await saveConfig({ ...config, dossiers: [...known, root].slice(-50) });
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
159
248
|
function header(session) {
|
|
160
249
|
const place = session.root.replace(homedir(), "~");
|
|
161
|
-
|
|
250
|
+
const facts = [
|
|
251
|
+
`${brand("HenergyqueAI")} ${style.dim(`v${VERSION}`)}`,
|
|
252
|
+
`${style.bold(MODELS[session.modelId] || "Velys")} ${style.dim(`· ${session.mode}`)}`,
|
|
253
|
+
style.dim(place),
|
|
254
|
+
"",
|
|
255
|
+
style.dim("Entrée à vide · menu ↑↓"),
|
|
256
|
+
];
|
|
257
|
+
still().forEach((line, index) =>
|
|
258
|
+
say(` ${line} ${facts[index] ?? ""}`.trimEnd()),
|
|
259
|
+
);
|
|
260
|
+
say("");
|
|
261
|
+
say(
|
|
262
|
+
` ${style.dim("──────────────────────────────────────────────────────────")}`,
|
|
263
|
+
);
|
|
264
|
+
say(
|
|
265
|
+
` ${style.dim("Écrivez votre demande. Le papillon reste dans l’en-tête pendant la réponse.")}`,
|
|
266
|
+
);
|
|
267
|
+
say("");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const menuPaint = (option, active) => {
|
|
271
|
+
const [label, detail = ""] = option.split("\t");
|
|
272
|
+
const marker = active ? brand("◆") : style.dim("◇");
|
|
273
|
+
const name = active ? style.bold(label) : label;
|
|
274
|
+
return ` ${marker} ${name.padEnd(30)} ${style.dim(detail)}`;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
async function menuChoice(title, options, selected = 0) {
|
|
278
|
+
say("");
|
|
279
|
+
say(` ${style.bold(title)}`);
|
|
162
280
|
say(
|
|
163
281
|
style.dim(
|
|
164
|
-
|
|
282
|
+
" ↑ ↓ pour choisir · Entrée pour confirmer · Échap pour revenir",
|
|
165
283
|
),
|
|
166
284
|
);
|
|
167
|
-
say(
|
|
285
|
+
say("");
|
|
286
|
+
return choose(options, { selected, paint: menuPaint });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function showStatus(session, messages) {
|
|
290
|
+
say("");
|
|
291
|
+
say(` ${style.bold("État de la session")}`);
|
|
292
|
+
say(
|
|
293
|
+
style.dim(` Assistant ${MODELS[session.modelId] || session.modelId}`),
|
|
294
|
+
);
|
|
295
|
+
say(style.dim(` Mode ${session.mode} · ${MODES[session.mode]}`));
|
|
296
|
+
say(style.dim(` Dossier ${session.root}`));
|
|
297
|
+
say(
|
|
298
|
+
style.dim(
|
|
299
|
+
` Réglages ${session.reglages.sources.length ? session.reglages.sources.join(", ") : "aucun fichier"}`,
|
|
300
|
+
),
|
|
301
|
+
);
|
|
302
|
+
const { autoriser, refuser } = session.permissions || {};
|
|
303
|
+
if (autoriser?.length)
|
|
304
|
+
say(style.dim(` Autorisé ${autoriser.join(", ")}`));
|
|
305
|
+
if (refuser?.length) say(style.dim(` Refusé ${refuser.join(", ")}`));
|
|
306
|
+
say(
|
|
307
|
+
style.dim(
|
|
308
|
+
` Échanges ${messages.filter((message) => message.role === "user").length}`,
|
|
309
|
+
),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async function openMenu(session, messages) {
|
|
314
|
+
const entries = [
|
|
315
|
+
["assistant", "Choisir l’assistant", MODELS[session.modelId]],
|
|
316
|
+
["mode", "Changer le mode", session.mode],
|
|
317
|
+
["status", "Voir l’état de la session", "dossier, règles, échanges"],
|
|
318
|
+
["clear", "Nouvelle conversation", "oublier le contexte actuel"],
|
|
319
|
+
["account", "Changer de compte", "autoriser ce terminal"],
|
|
320
|
+
["quit", "Quitter", "fermer HenergyqueAI"],
|
|
321
|
+
["back", "Revenir à la discussion", "Échap fonctionne aussi"],
|
|
322
|
+
];
|
|
323
|
+
const picked = await menuChoice(
|
|
324
|
+
"Menu HenergyqueAI",
|
|
325
|
+
entries.map(([, label, detail]) => `${label}\t${detail}`),
|
|
326
|
+
);
|
|
327
|
+
if (!picked) return "fait";
|
|
328
|
+
const action = entries.find(([, label]) => picked.startsWith(label))?.[0];
|
|
329
|
+
if (action === "assistant") {
|
|
330
|
+
const models = Object.entries(MODELS);
|
|
331
|
+
const chosen = await menuChoice(
|
|
332
|
+
"Choisir l’assistant",
|
|
333
|
+
models.map(
|
|
334
|
+
([id, label]) =>
|
|
335
|
+
`${label}\t${id === session.modelId ? "actuel" : id === "aelyr" ? "modèle phare" : ""}`,
|
|
336
|
+
),
|
|
337
|
+
Math.max(
|
|
338
|
+
0,
|
|
339
|
+
models.findIndex(([id]) => id === session.modelId),
|
|
340
|
+
),
|
|
341
|
+
);
|
|
342
|
+
const model = models.find(([, label]) => chosen?.startsWith(label));
|
|
343
|
+
if (model) {
|
|
344
|
+
session.modelId = model[0];
|
|
345
|
+
await saveConfig({ ...(await loadConfig()), modelId: model[0] });
|
|
346
|
+
say(style.green(`\n Assistant sélectionné : ${model[1]}`));
|
|
347
|
+
}
|
|
348
|
+
return "fait";
|
|
349
|
+
}
|
|
350
|
+
if (action === "mode") {
|
|
351
|
+
const modes = Object.entries(MODES);
|
|
352
|
+
const chosen = await menuChoice(
|
|
353
|
+
"Choisir le mode",
|
|
354
|
+
modes.map(([id, detail]) => `${id}\t${detail}`),
|
|
355
|
+
Math.max(
|
|
356
|
+
0,
|
|
357
|
+
modes.findIndex(([id]) => id === session.mode),
|
|
358
|
+
),
|
|
359
|
+
);
|
|
360
|
+
const mode = modes.find(([id]) => chosen?.startsWith(id));
|
|
361
|
+
if (mode) {
|
|
362
|
+
session.mode = mode[0];
|
|
363
|
+
say(style.green(`\n Mode sélectionné : ${mode[0]}`));
|
|
364
|
+
}
|
|
365
|
+
return "fait";
|
|
366
|
+
}
|
|
367
|
+
if (action === "status") {
|
|
368
|
+
showStatus(session, messages);
|
|
369
|
+
return "fait";
|
|
370
|
+
}
|
|
371
|
+
if (action === "clear") {
|
|
372
|
+
const answer = await menuChoice("Commencer une nouvelle conversation ?", [
|
|
373
|
+
"Conserver la conversation\trevenir sans rien effacer",
|
|
374
|
+
"Nouvelle conversation\teffacer le contexte de cette session",
|
|
375
|
+
]);
|
|
376
|
+
if (answer?.startsWith("Nouvelle conversation")) {
|
|
377
|
+
messages.length = 0;
|
|
378
|
+
say(style.green("\n Nouvelle conversation prête."));
|
|
379
|
+
}
|
|
380
|
+
return "fait";
|
|
381
|
+
}
|
|
382
|
+
if (action === "account") {
|
|
383
|
+
const key = await connect();
|
|
384
|
+
if (key) session.key = key;
|
|
385
|
+
return "fait";
|
|
386
|
+
}
|
|
387
|
+
if (action === "quit") return "fin";
|
|
388
|
+
return "fait";
|
|
168
389
|
}
|
|
169
390
|
|
|
170
391
|
// Slash commands, handled here so a session never needs to be restarted.
|
|
@@ -172,7 +393,14 @@ async function slash(line, session, messages) {
|
|
|
172
393
|
const [name, value] = line.slice(1).trim().split(/\s+/);
|
|
173
394
|
if (["quitter", "exit", "q"].includes(name)) return "fin";
|
|
174
395
|
if (name === "aide") {
|
|
396
|
+
say(
|
|
397
|
+
style.bold(
|
|
398
|
+
" Appuyez simplement sur Entrée à vide pour ouvrir le menu ↑↓.",
|
|
399
|
+
),
|
|
400
|
+
);
|
|
175
401
|
say(style.dim(" /modele <aelyr|oryne|velys> change d’assistant"));
|
|
402
|
+
say(style.dim(" /mode <…> change ce qu’il peut faire"));
|
|
403
|
+
say(style.dim(" /statut ce qui est chargé"));
|
|
176
404
|
say(style.dim(" /effacer oublie la conversation"));
|
|
177
405
|
say(style.dim(" /connexion change de compte"));
|
|
178
406
|
say(style.dim(" /quitter sort"));
|
|
@@ -198,6 +426,21 @@ async function slash(line, session, messages) {
|
|
|
198
426
|
if (key) session.key = key;
|
|
199
427
|
return "fait";
|
|
200
428
|
}
|
|
429
|
+
if (name === "mode") {
|
|
430
|
+
if (!MODES[value]) {
|
|
431
|
+
for (const [nom, quoi] of Object.entries(MODES))
|
|
432
|
+
say(style.dim(` ${nom.padEnd(24)} ${quoi}`));
|
|
433
|
+
say(style.dim(` actuel : ${session.mode}`));
|
|
434
|
+
return "fait";
|
|
435
|
+
}
|
|
436
|
+
session.mode = value;
|
|
437
|
+
say(style.dim(` Mode : ${value} — ${MODES[value]}`));
|
|
438
|
+
return "fait";
|
|
439
|
+
}
|
|
440
|
+
if (name === "statut") {
|
|
441
|
+
showStatus(session, messages);
|
|
442
|
+
return "fait";
|
|
443
|
+
}
|
|
201
444
|
say(style.dim(` Commande inconnue. /aide pour la liste.`));
|
|
202
445
|
return "fait";
|
|
203
446
|
}
|
|
@@ -224,20 +467,33 @@ async function main() {
|
|
|
224
467
|
return value ?? null;
|
|
225
468
|
};
|
|
226
469
|
const chosen = take("--modele") || take("--model");
|
|
227
|
-
const
|
|
228
|
-
const
|
|
229
|
-
const
|
|
470
|
+
const askedMode = take("--mode");
|
|
471
|
+
const resume = argv.includes("--continuer") || argv.includes("-c");
|
|
472
|
+
const once = argv.includes("--direct") || argv.includes("-p");
|
|
473
|
+
const words = argv.filter(
|
|
474
|
+
(item) => !item.startsWith("--") && item !== "-c" && item !== "-p",
|
|
475
|
+
);
|
|
230
476
|
|
|
231
477
|
const config = await loadConfig();
|
|
478
|
+
const root = process.cwd();
|
|
479
|
+
const reglages = await loadSettings(root);
|
|
232
480
|
const session = {
|
|
233
481
|
base: config.base || DEFAULT_BASE,
|
|
234
482
|
key: process.env.HENERGYQUEAI_KEY || config.key,
|
|
235
|
-
modelId: chosen || config.modelId || "velys",
|
|
236
|
-
root
|
|
483
|
+
modelId: chosen || config.modelId || reglages.modele || "velys",
|
|
484
|
+
root,
|
|
237
485
|
tools: definitions,
|
|
238
|
-
|
|
239
|
-
|
|
486
|
+
mode:
|
|
487
|
+
(argv.includes("--lecture-seule") && "lecture-seule") ||
|
|
488
|
+
(argv.includes("--oui") && "sans-demander") ||
|
|
489
|
+
(MODES[askedMode] ? askedMode : null) ||
|
|
490
|
+
reglages.mode ||
|
|
491
|
+
"demander",
|
|
492
|
+
permissions: reglages.permissions,
|
|
493
|
+
reglages,
|
|
240
494
|
};
|
|
495
|
+
for (const file of reglages.casses)
|
|
496
|
+
say(style.red(`Réglages illisibles, ignorés : ${file}`));
|
|
241
497
|
|
|
242
498
|
const piped = !process.stdin.isTTY
|
|
243
499
|
? await new Promise((resolve) => {
|
|
@@ -262,16 +518,35 @@ async function main() {
|
|
|
262
518
|
}
|
|
263
519
|
|
|
264
520
|
const messages = [];
|
|
521
|
+
if (resume) {
|
|
522
|
+
const saved = await lastSession(session.root);
|
|
523
|
+
if (saved) {
|
|
524
|
+
messages.push(...saved.messages);
|
|
525
|
+
say(
|
|
526
|
+
style.dim(
|
|
527
|
+
` ${saved.messages.filter((m) => m.role === "user").length} échange(s) repris.`,
|
|
528
|
+
),
|
|
529
|
+
);
|
|
530
|
+
} else say(style.dim(" Aucune conversation à reprendre ici."));
|
|
531
|
+
}
|
|
265
532
|
try {
|
|
266
533
|
if (first) {
|
|
267
534
|
messages.push({ role: "user", content: first });
|
|
268
535
|
await turn(session, messages);
|
|
536
|
+
if (!once) await saveSession(session.root, messages);
|
|
269
537
|
return;
|
|
270
538
|
}
|
|
539
|
+
if (!(await trusted(session.root))) return;
|
|
540
|
+
clearScreen();
|
|
271
541
|
header(session);
|
|
542
|
+
session.headerActive = true;
|
|
272
543
|
for (;;) {
|
|
273
|
-
const line = await prompt(`${brand("›")} `);
|
|
274
|
-
if (!line)
|
|
544
|
+
const line = await prompt(`${brand("Vous")} ${style.dim("›")} `);
|
|
545
|
+
if (!line) {
|
|
546
|
+
if ((await openMenu(session, messages)) === "fin") return;
|
|
547
|
+
say();
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
275
550
|
if (line.startsWith("/")) {
|
|
276
551
|
if ((await slash(line, session, messages)) === "fin") return;
|
|
277
552
|
say();
|
|
@@ -279,6 +554,7 @@ async function main() {
|
|
|
279
554
|
}
|
|
280
555
|
messages.push({ role: "user", content: line });
|
|
281
556
|
await turn(session, messages);
|
|
557
|
+
await saveSession(session.root, messages);
|
|
282
558
|
say();
|
|
283
559
|
}
|
|
284
560
|
} catch (error) {
|
package/package.json
CHANGED
package/src/butterfly.js
CHANGED
|
@@ -1,15 +1,139 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// The butterfly is pixel art: each character cell carries two pixels, the upper
|
|
2
|
+
// one as the foreground of "▀" and the lower one as its background. Only the
|
|
3
|
+
// left half of the wings is written down and then mirrored, so the drawing can
|
|
4
|
+
// never drift out of symmetry.
|
|
4
5
|
const plain = !process.stdout.isTTY || process.env.NO_COLOR;
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const INK = {
|
|
8
|
+
a: "#f6c9a8",
|
|
9
|
+
b: "#edac88",
|
|
10
|
+
c: "#c46c4f",
|
|
11
|
+
d: "#95503f",
|
|
12
|
+
e: "#784e3e",
|
|
13
|
+
f: "#5d3b2f",
|
|
10
14
|
};
|
|
11
15
|
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
const fg = (hex) => {
|
|
17
|
+
const v = parseInt(hex.slice(1), 16);
|
|
18
|
+
return `38;2;${(v >> 16) & 255};${(v >> 8) & 255};${v & 255}`;
|
|
19
|
+
};
|
|
20
|
+
const bg = (hex) => {
|
|
21
|
+
const v = parseInt(hex.slice(1), 16);
|
|
22
|
+
return `48;2;${(v >> 16) & 255};${(v >> 8) & 255};${v & 255}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Antennae are written whole: mirroring them would undo the head turning.
|
|
26
|
+
const HEADS = {
|
|
27
|
+
gauche: ["·b..........b..", "..b........b..."],
|
|
28
|
+
face: ["..b.........b..", "...b.......b..."],
|
|
29
|
+
droite: ["..b..........b·", "...b........b.."],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const mirror = (rows) =>
|
|
33
|
+
rows.map(([half, body]) => half + body + [...half].reverse().join(""));
|
|
34
|
+
|
|
35
|
+
const OUVERT = mirror([
|
|
36
|
+
[".bbbb..", "e"],
|
|
37
|
+
["bbaabb.", "e"],
|
|
38
|
+
["bbaabbe", "f"],
|
|
39
|
+
[".bbbcce", "f"],
|
|
40
|
+
["..cccc.", "f"],
|
|
41
|
+
["..cddc.", "f"],
|
|
42
|
+
["...dd..", "e"],
|
|
43
|
+
[".......", "e"],
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
const MI = mirror([
|
|
47
|
+
["...bbb.", "e"],
|
|
48
|
+
["..bbaab", "e"],
|
|
49
|
+
["..bbaab", "f"],
|
|
50
|
+
["...bbcc", "f"],
|
|
51
|
+
["...cccc", "f"],
|
|
52
|
+
["...cddc", "f"],
|
|
53
|
+
["....dd.", "e"],
|
|
54
|
+
[".......", "e"],
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
const FERME = mirror([
|
|
58
|
+
["....bb.", "e"],
|
|
59
|
+
["....baa", "e"],
|
|
60
|
+
["....bba", "f"],
|
|
61
|
+
["....bbc", "f"],
|
|
62
|
+
[".....cc", "f"],
|
|
63
|
+
[".....cd", "f"],
|
|
64
|
+
["......d", "e"],
|
|
65
|
+
[".......", "e"],
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
// One full wingbeat, with the head glancing left then right between beats.
|
|
69
|
+
export const FRAMES = [
|
|
70
|
+
[...HEADS.face, ...OUVERT],
|
|
71
|
+
[...HEADS.gauche, ...MI],
|
|
72
|
+
[...HEADS.gauche, ...FERME],
|
|
73
|
+
[...HEADS.gauche, ...MI],
|
|
74
|
+
[...HEADS.face, ...OUVERT],
|
|
75
|
+
[...HEADS.droite, ...MI],
|
|
76
|
+
[...HEADS.droite, ...FERME],
|
|
77
|
+
[...HEADS.droite, ...MI],
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
export const WIDTH = 15;
|
|
81
|
+
export const HEIGHT = 5;
|
|
82
|
+
|
|
83
|
+
// Turns a pixel grid into five printable lines.
|
|
84
|
+
export function paint(grid) {
|
|
85
|
+
const lines = [];
|
|
86
|
+
for (let y = 0; y < grid.length; y += 2) {
|
|
87
|
+
let line = "";
|
|
88
|
+
for (let x = 0; x < WIDTH; x += 1) {
|
|
89
|
+
const top = INK[grid[y]?.[x]];
|
|
90
|
+
const bottom = INK[grid[y + 1]?.[x]];
|
|
91
|
+
if (plain) {
|
|
92
|
+
line += top || bottom ? "#" : " ";
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!top && !bottom) line += " ";
|
|
96
|
+
else if (top && !bottom) line += `\u001b[${fg(top)}m▀\u001b[0m`;
|
|
97
|
+
else if (!top && bottom) line += `\u001b[${fg(bottom)}m▄\u001b[0m`;
|
|
98
|
+
else line += `\u001b[${fg(top)};${bg(bottom)}m▀\u001b[0m`;
|
|
99
|
+
}
|
|
100
|
+
lines.push(line);
|
|
101
|
+
}
|
|
102
|
+
return lines;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const still = () => paint(FRAMES[0]);
|
|
106
|
+
|
|
107
|
+
export const brand = (text) =>
|
|
108
|
+
plain ? text : `\u001b[${fg("#c46c4f")}m${text}\u001b[0m`;
|
|
14
109
|
|
|
15
|
-
|
|
110
|
+
// Flaps in the reserved header area while something is being waited for. The
|
|
111
|
+
// cursor is saved and restored around every frame, so streamed answers keep
|
|
112
|
+
// writing at the bottom instead of inheriting five animation lines.
|
|
113
|
+
export function flutter(label = "", { row = 1, column = 3 } = {}) {
|
|
114
|
+
if (plain || !process.stdout.isTTY) {
|
|
115
|
+
if (label) process.stdout.write(`${label}\n`);
|
|
116
|
+
return () => {};
|
|
117
|
+
}
|
|
118
|
+
let at = 0;
|
|
119
|
+
const show = () => {
|
|
120
|
+
const lines = paint(FRAMES[at % FRAMES.length]);
|
|
121
|
+
const frame = lines
|
|
122
|
+
.map(
|
|
123
|
+
(line, index) =>
|
|
124
|
+
`\u001b[${row + index};${column}H${line}${index === 2 && label ? ` \u001b[2m${label}\u001b[0m` : ""}`,
|
|
125
|
+
)
|
|
126
|
+
.join("");
|
|
127
|
+
process.stdout.write(`\u001b7${frame}\u001b8`);
|
|
128
|
+
at += 1;
|
|
129
|
+
};
|
|
130
|
+
show();
|
|
131
|
+
const timer = setInterval(show, 140);
|
|
132
|
+
return () => {
|
|
133
|
+
clearInterval(timer);
|
|
134
|
+
const resting = still()
|
|
135
|
+
.map((line, index) => `\u001b[${row + index};${column}H${line}`)
|
|
136
|
+
.join("");
|
|
137
|
+
process.stdout.write(`\u001b7${resting}\u001b8`);
|
|
138
|
+
};
|
|
139
|
+
}
|
package/src/client.js
CHANGED
|
@@ -56,7 +56,7 @@ export async function ask({
|
|
|
56
56
|
continue;
|
|
57
57
|
}
|
|
58
58
|
if (name === "delta") onDelta?.(parsed.text || "");
|
|
59
|
-
else if (name === "thinking") onThinking?.();
|
|
59
|
+
else if (name === "thinking") onThinking?.(parsed);
|
|
60
60
|
else if (name === "done") result = parsed;
|
|
61
61
|
else if (name === "error") failure = parsed;
|
|
62
62
|
}
|
package/src/confiance.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { emitKeypressEvents } from "node:readline";
|
|
2
|
+
|
|
3
|
+
// An arrow-key choice, the way a terminal asks a real question: one line per
|
|
4
|
+
// option, Entrée to confirm, Échap to back out.
|
|
5
|
+
export function choose(options, { selected = 0, paint } = {}) {
|
|
6
|
+
if (!process.stdin.isTTY) return Promise.resolve(null);
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
let at = selected;
|
|
9
|
+
let drawn = false;
|
|
10
|
+
const show = () => {
|
|
11
|
+
if (drawn) process.stdout.write(`\u001b[${options.length}A`);
|
|
12
|
+
process.stdout.write(
|
|
13
|
+
options
|
|
14
|
+
.map((option, index) => `\u001b[2K${paint(option, index === at)}\n`)
|
|
15
|
+
.join(""),
|
|
16
|
+
);
|
|
17
|
+
drawn = true;
|
|
18
|
+
};
|
|
19
|
+
emitKeypressEvents(process.stdin);
|
|
20
|
+
process.stdin.setRawMode(true);
|
|
21
|
+
process.stdin.resume();
|
|
22
|
+
process.stdout.write("\u001b[?25l");
|
|
23
|
+
show();
|
|
24
|
+
const onKey = (_, key) => {
|
|
25
|
+
if (!key) return;
|
|
26
|
+
if (key.name === "up" || key.name === "k")
|
|
27
|
+
at = (at - 1 + options.length) % options.length;
|
|
28
|
+
else if (key.name === "down" || key.name === "j")
|
|
29
|
+
at = (at + 1) % options.length;
|
|
30
|
+
else if (key.name === "return" || key.name === "enter") return done(at);
|
|
31
|
+
else if (key.name === "escape" || (key.ctrl && key.name === "c"))
|
|
32
|
+
return done(null);
|
|
33
|
+
else return;
|
|
34
|
+
show();
|
|
35
|
+
};
|
|
36
|
+
const done = (value) => {
|
|
37
|
+
process.stdin.off("keypress", onKey);
|
|
38
|
+
process.stdin.setRawMode(false);
|
|
39
|
+
process.stdin.pause();
|
|
40
|
+
process.stdout.write("\u001b[?25h");
|
|
41
|
+
resolve(value === null ? null : options[value]);
|
|
42
|
+
};
|
|
43
|
+
process.stdin.on("keypress", onKey);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Wipes the screen and its scrollback, so the session starts on a clean page.
|
|
48
|
+
export const clearScreen = () => {
|
|
49
|
+
if (process.stdout.isTTY) process.stdout.write("\u001b[2J\u001b[3J\u001b[H");
|
|
50
|
+
};
|
package/src/reglages.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
// Settings come from two files, the nearer one winning, so a project can set
|
|
6
|
+
// its own rules without touching anyone's personal preferences.
|
|
7
|
+
// ~/.henergyqueai/settings.json you, everywhere
|
|
8
|
+
// <projet>/.henergyqueai/settings.json this project
|
|
9
|
+
export const USER_FILE = path.join(homedir(), ".henergyqueai", "settings.json");
|
|
10
|
+
export const projectFile = (root) =>
|
|
11
|
+
path.join(root, ".henergyqueai", "settings.json");
|
|
12
|
+
|
|
13
|
+
const readJson = async (file) => {
|
|
14
|
+
const raw = await readFile(file, "utf8").catch(() => null);
|
|
15
|
+
if (!raw) return null;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(raw);
|
|
18
|
+
} catch {
|
|
19
|
+
return { __casse: file };
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const DEFAULTS = {
|
|
24
|
+
modele: "velys",
|
|
25
|
+
mode: "demander",
|
|
26
|
+
permissions: { autoriser: [], refuser: [] },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export async function loadSettings(root) {
|
|
30
|
+
const user = await readJson(USER_FILE);
|
|
31
|
+
const project = await readJson(projectFile(root));
|
|
32
|
+
const broken = [user, project]
|
|
33
|
+
.filter((item) => item?.__casse)
|
|
34
|
+
.map((i) => i.__casse);
|
|
35
|
+
const merge = (a = {}, b = {}) => ({
|
|
36
|
+
...DEFAULTS,
|
|
37
|
+
...a,
|
|
38
|
+
...b,
|
|
39
|
+
permissions: {
|
|
40
|
+
autoriser: [
|
|
41
|
+
...(a.permissions?.autoriser || []),
|
|
42
|
+
...(b.permissions?.autoriser || []),
|
|
43
|
+
],
|
|
44
|
+
refuser: [
|
|
45
|
+
...(a.permissions?.refuser || []),
|
|
46
|
+
...(b.permissions?.refuser || []),
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
// A missing file reads as null, which would slip past a default
|
|
52
|
+
// parameter: it has to become an object here.
|
|
53
|
+
...merge(
|
|
54
|
+
user && !user.__casse ? user : {},
|
|
55
|
+
project && !project.__casse ? project : {},
|
|
56
|
+
),
|
|
57
|
+
sources: [
|
|
58
|
+
user && !user.__casse && USER_FILE,
|
|
59
|
+
project && !project.__casse && projectFile(root),
|
|
60
|
+
].filter(Boolean),
|
|
61
|
+
casses: broken,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function saveUserSetting(key, value) {
|
|
66
|
+
const current = (await readJson(USER_FILE)) || {};
|
|
67
|
+
const next = { ...(current.__casse ? {} : current), [key]: value };
|
|
68
|
+
await mkdir(path.dirname(USER_FILE), { recursive: true });
|
|
69
|
+
await writeFile(USER_FILE, JSON.stringify(next, null, 2), "utf8");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A rule is a tool name with an optional argument pattern, where * stands for
|
|
73
|
+
// any run of characters: Executer(npm run test *) Ecrire(src/**)
|
|
74
|
+
const compile = (rule) => {
|
|
75
|
+
const match = /^([A-Za-z]+)(?:\((.*)\))?$/.exec(String(rule).trim());
|
|
76
|
+
if (!match) return null;
|
|
77
|
+
const [, tool, pattern] = match;
|
|
78
|
+
return {
|
|
79
|
+
tool: tool.toLowerCase(),
|
|
80
|
+
test:
|
|
81
|
+
pattern === undefined
|
|
82
|
+
? () => true
|
|
83
|
+
: (value) =>
|
|
84
|
+
new RegExp(
|
|
85
|
+
`^${pattern
|
|
86
|
+
.replace(/[.+?^${}()|[\]\\]/g, "\\$&")
|
|
87
|
+
.replace(/\*/g, ".*")}$`,
|
|
88
|
+
).test(value ?? ""),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const SUBJECT = {
|
|
93
|
+
ecrire_fichier: ["ecrire", (args) => args.chemin],
|
|
94
|
+
executer: ["executer", (args) => args.commande],
|
|
95
|
+
lire_fichier: ["lire", (args) => args.chemin],
|
|
96
|
+
lister_fichiers: ["lister", (args) => args.dossier || "."],
|
|
97
|
+
chercher: ["chercher", (args) => args.motif],
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Returns "autorise", "refuse" or null when no rule has an opinion.
|
|
101
|
+
export function ruling(permissions, name, args) {
|
|
102
|
+
const known = SUBJECT[name];
|
|
103
|
+
if (!known) return null;
|
|
104
|
+
const [tool, pick] = known;
|
|
105
|
+
const value = String(pick(args) ?? "");
|
|
106
|
+
const matches = (list) =>
|
|
107
|
+
(list || [])
|
|
108
|
+
.map(compile)
|
|
109
|
+
.filter(Boolean)
|
|
110
|
+
.some((rule) => rule.tool === tool && rule.test(value));
|
|
111
|
+
if (matches(permissions?.refuser)) return "refuse";
|
|
112
|
+
if (matches(permissions?.autoriser)) return "autorise";
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const MODES = {
|
|
117
|
+
demander: "demande avant d’écrire ou d’exécuter",
|
|
118
|
+
"accepter-modifications":
|
|
119
|
+
"écrit sans demander, mais demande pour les commandes",
|
|
120
|
+
"lecture-seule": "ne peut ni écrire ni exécuter",
|
|
121
|
+
"sans-demander": "ne demande jamais rien",
|
|
122
|
+
};
|
package/src/sessions.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
// One saved conversation per project folder, so a session can be picked up
|
|
7
|
+
// where it stopped. Kept beside the settings, never inside the project.
|
|
8
|
+
const store = (root) =>
|
|
9
|
+
path.join(
|
|
10
|
+
homedir(),
|
|
11
|
+
".henergyqueai",
|
|
12
|
+
"sessions",
|
|
13
|
+
`${createHash("sha256").update(root).digest("hex").slice(0, 16)}.json`,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
export async function saveSession(root, messages) {
|
|
17
|
+
const file = store(root);
|
|
18
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
19
|
+
await writeFile(
|
|
20
|
+
file,
|
|
21
|
+
JSON.stringify(
|
|
22
|
+
{ root, date: Date.now(), messages: messages.slice(-60) },
|
|
23
|
+
null,
|
|
24
|
+
2,
|
|
25
|
+
),
|
|
26
|
+
"utf8",
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function lastSession(root) {
|
|
31
|
+
const raw = await readFile(store(root), "utf8").catch(() => null);
|
|
32
|
+
if (!raw) return null;
|
|
33
|
+
try {
|
|
34
|
+
const saved = JSON.parse(raw);
|
|
35
|
+
return Array.isArray(saved.messages) ? saved : null;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/ui.js
CHANGED
|
@@ -17,6 +17,37 @@ export const style = {
|
|
|
17
17
|
export const say = (text = "") => process.stdout.write(`${text}\n`);
|
|
18
18
|
export const write = (text) => process.stdout.write(text);
|
|
19
19
|
|
|
20
|
+
export function waiting(label = "Réflexion en cours") {
|
|
21
|
+
if (plain || !process.stdout.isTTY)
|
|
22
|
+
return { update: () => {}, stop: () => {} };
|
|
23
|
+
const frames = ["◜", "◠", "◝", "◞", "◡", "◟"];
|
|
24
|
+
let frame = 0;
|
|
25
|
+
let tokens = 0;
|
|
26
|
+
let stopped = false;
|
|
27
|
+
const render = () => {
|
|
28
|
+
if (stopped) return;
|
|
29
|
+
const count = tokens ? ` · ${tokens} token${tokens > 1 ? "s" : ""}` : "";
|
|
30
|
+
process.stdout.write(
|
|
31
|
+
`\r\u001b[2K ${style.olive(frames[frame % frames.length])} ${style.dim(label + count)}`,
|
|
32
|
+
);
|
|
33
|
+
frame += 1;
|
|
34
|
+
};
|
|
35
|
+
render();
|
|
36
|
+
const timer = setInterval(render, 110);
|
|
37
|
+
return {
|
|
38
|
+
update(next) {
|
|
39
|
+
tokens = Math.max(tokens, Number(next) || tokens);
|
|
40
|
+
render();
|
|
41
|
+
},
|
|
42
|
+
stop() {
|
|
43
|
+
if (stopped) return;
|
|
44
|
+
stopped = true;
|
|
45
|
+
clearInterval(timer);
|
|
46
|
+
process.stdout.write("\r\u001b[2K");
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
20
51
|
export function activity(name, args) {
|
|
21
52
|
const detail = {
|
|
22
53
|
lire_fichier: args.chemin,
|