henergyqueai 0.2.0 → 0.3.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 +54 -22
- package/bin/henergyqueai.js +163 -24
- package/package.json +1 -1
- package/src/butterfly.js +140 -10
- package/src/confiance.js +50 -0
- package/src/reglages.js +122 -0
- package/src/sessions.js +39 -0
package/README.md
CHANGED
|
@@ -18,42 +18,74 @@ 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
22
|
|
|
23
23
|
## Utiliser
|
|
24
24
|
|
|
25
25
|
```bash
|
|
26
|
-
henergyqueai
|
|
26
|
+
henergyqueai ouvre une session
|
|
27
27
|
henergyqueai "corrige le bug de date dans facture.js"
|
|
28
28
|
cat erreur.log | henergyqueai "d'où vient cette erreur ?"
|
|
29
|
-
henergyqueai
|
|
29
|
+
henergyqueai -c reprend la conversation de ce dossier
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
Il n’a accès qu’au dossier depuis lequel vous l’avez lancé.
|
|
33
33
|
|
|
34
34
|
## Options
|
|
35
35
|
|
|
36
|
-
| Option | Effet
|
|
37
|
-
| -------------------------------- |
|
|
38
|
-
| `--modele <aelyr\|oryne\|velys>` | Choisit l’assistant
|
|
39
|
-
| `--
|
|
40
|
-
| `--
|
|
36
|
+
| Option | Effet |
|
|
37
|
+
| -------------------------------- | ------------------------------------- |
|
|
38
|
+
| `--modele <aelyr\|oryne\|velys>` | Choisit l’assistant |
|
|
39
|
+
| `--mode <mode>` | Voir les modes ci-dessous |
|
|
40
|
+
| `--continuer`, `-c` | Reprend la conversation du dossier |
|
|
41
|
+
| `--direct`, `-p` | Répond sans rien enregistrer |
|
|
42
|
+
| `--lecture-seule` | Raccourci pour `--mode lecture-seule` |
|
|
43
|
+
| `--oui` | Raccourci pour `--mode sans-demander` |
|
|
41
44
|
|
|
42
|
-
##
|
|
45
|
+
## Modes
|
|
46
|
+
|
|
47
|
+
| Mode | Ce qu’il peut faire |
|
|
48
|
+
| ------------------------ | ------------------------------------------------- |
|
|
49
|
+
| `demander` | Demande avant d’écrire ou d’exécuter. Par défaut. |
|
|
50
|
+
| `accepter-modifications` | Écrit sans demander, demande pour les commandes |
|
|
51
|
+
| `lecture-seule` | Ne peut ni écrire ni exécuter |
|
|
52
|
+
| `sans-demander` | Ne demande jamais rien |
|
|
43
53
|
|
|
44
|
-
|
|
45
|
-
| ------------------------------- | ---------------------- |
|
|
46
|
-
| `/aide` | Liste les commandes |
|
|
47
|
-
| `/modele <aelyr\|oryne\|velys>` | Change d’assistant |
|
|
48
|
-
| `/effacer` | Oublie la conversation |
|
|
49
|
-
| `/connexion` | Change de compte |
|
|
50
|
-
| `/quitter` | Sort |
|
|
54
|
+
## En session
|
|
51
55
|
|
|
52
|
-
|
|
56
|
+
| Commande | Effet |
|
|
57
|
+
| ------------------------------- | ---------------------------------------------- |
|
|
58
|
+
| `/aide` | Liste les commandes |
|
|
59
|
+
| `/modele <aelyr\|oryne\|velys>` | Change d’assistant |
|
|
60
|
+
| `/mode <mode>` | Change ce qu’il peut faire |
|
|
61
|
+
| `/statut` | Ce qui est chargé : réglages, règles, échanges |
|
|
62
|
+
| `/effacer` | Oublie la conversation |
|
|
63
|
+
| `/connexion` | Change de compte |
|
|
64
|
+
| `/quitter` | Sort |
|
|
65
|
+
|
|
66
|
+
## Réglages
|
|
67
|
+
|
|
68
|
+
Deux fichiers, le plus proche l’emporte :
|
|
69
|
+
|
|
70
|
+
| Fichier | Portée |
|
|
71
|
+
| -------------------------------------- | ------------- |
|
|
72
|
+
| `~/.henergyqueai/settings.json` | Vous, partout |
|
|
73
|
+
| `<projet>/.henergyqueai/settings.json` | Ce projet |
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{
|
|
77
|
+
"modele": "oryne",
|
|
78
|
+
"mode": "demander",
|
|
79
|
+
"permissions": {
|
|
80
|
+
"autoriser": ["Executer(npm run test *)", "Lire"],
|
|
81
|
+
"refuser": ["Executer(rm *)", "Ecrire(.env*)"]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
53
85
|
|
|
54
|
-
|
|
86
|
+
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
87
|
|
|
56
|
-
|
|
88
|
+
Une règle autorisée dispense de la question. Une règle refusée arrête l’action avant qu’elle ne parte.
|
|
57
89
|
|
|
58
90
|
## Variables d’environnement
|
|
59
91
|
|
|
@@ -63,9 +95,9 @@ Lire un fichier, explorer le projet et chercher un texte se font librement.
|
|
|
63
95
|
| `HENERGYQUEAI_URL` | Adresse du service, pour un déploiement privé |
|
|
64
96
|
| `NO_COLOR` | Désactive les couleurs |
|
|
65
97
|
|
|
66
|
-
## Votre
|
|
98
|
+
## Votre accès
|
|
67
99
|
|
|
68
|
-
|
|
100
|
+
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
101
|
|
|
70
102
|
## Licence
|
|
71
103
|
|
package/bin/henergyqueai.js
CHANGED
|
@@ -14,9 +14,17 @@ import {
|
|
|
14
14
|
style,
|
|
15
15
|
write,
|
|
16
16
|
} from "../src/ui.js";
|
|
17
|
-
import { brand,
|
|
17
|
+
import { brand, flutter, still } from "../src/butterfly.js";
|
|
18
|
+
import { choose, clearScreen } from "../src/confiance.js";
|
|
19
|
+
import {
|
|
20
|
+
MODES,
|
|
21
|
+
loadSettings,
|
|
22
|
+
ruling,
|
|
23
|
+
saveUserSetting,
|
|
24
|
+
} from "../src/reglages.js";
|
|
25
|
+
import { lastSession, saveSession } from "../src/sessions.js";
|
|
18
26
|
|
|
19
|
-
const VERSION = "0.
|
|
27
|
+
const VERSION = "0.3.0";
|
|
20
28
|
const CONFIG = path.join(homedir(), ".henergyqueai", "config.json");
|
|
21
29
|
const DEFAULT_BASE = process.env.HENERGYQUEAI_URL || "https://henergyqueai.fr";
|
|
22
30
|
const MODELS = { aelyr: "Aelyr", oryne: "Oryne", velys: "Velys" };
|
|
@@ -40,23 +48,28 @@ async function saveConfig(config) {
|
|
|
40
48
|
|
|
41
49
|
function usage() {
|
|
42
50
|
say(`
|
|
43
|
-
${
|
|
51
|
+
${brand("henergyqueai")} ${style.dim(`v${VERSION}`)}
|
|
44
52
|
|
|
45
53
|
${style.bold("henergyqueai")} ouvre une session
|
|
46
54
|
${style.bold('henergyqueai "votre demande"')} répond puis rend la main
|
|
47
55
|
${style.dim('cat fichier | henergyqueai "…"')} lit l’entrée standard
|
|
48
56
|
|
|
49
57
|
${style.dim("--modele aelyr|oryne|velys")} choisit l’assistant
|
|
50
|
-
${style.dim("--
|
|
51
|
-
${style.dim("
|
|
58
|
+
${style.dim("--mode <mode>")} demander · accepter-modifications
|
|
59
|
+
${style.dim("")} lecture-seule · sans-demander
|
|
60
|
+
${style.dim("--continuer, -c")} reprend la conversation du dossier
|
|
61
|
+
${style.dim("--direct, -p")} répond sans rien enregistrer
|
|
52
62
|
|
|
53
|
-
${style.dim("En session : /aide /modele /
|
|
63
|
+
${style.dim("En session : /aide /modele /mode /statut /effacer /quitter")}
|
|
64
|
+
${style.dim("Réglages : ~/.henergyqueai/settings.json ou .henergyqueai/settings.json")}
|
|
54
65
|
`);
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
// First run opens the browser, the way a sign-in should. Nothing to copy.
|
|
58
69
|
async function connect() {
|
|
59
|
-
say(
|
|
70
|
+
say("");
|
|
71
|
+
still().forEach((line) => say(` ${line}`));
|
|
72
|
+
say("");
|
|
60
73
|
say(style.dim(" Ouverture de votre navigateur pour autoriser ce terminal…"));
|
|
61
74
|
try {
|
|
62
75
|
const key = await awaitAuthorization({
|
|
@@ -79,6 +92,7 @@ async function connect() {
|
|
|
79
92
|
async function turn(session, messages) {
|
|
80
93
|
for (let round = 0; round < 24; round += 1) {
|
|
81
94
|
let printed = false;
|
|
95
|
+
let settle = flutter(round === 0 ? "" : "");
|
|
82
96
|
const answer = await ask({
|
|
83
97
|
base: session.base,
|
|
84
98
|
key: session.key,
|
|
@@ -86,11 +100,17 @@ async function turn(session, messages) {
|
|
|
86
100
|
tools: session.tools,
|
|
87
101
|
modelId: session.modelId,
|
|
88
102
|
onDelta: (text) => {
|
|
103
|
+
if (settle) {
|
|
104
|
+
settle();
|
|
105
|
+
settle = null;
|
|
106
|
+
}
|
|
89
107
|
printed = true;
|
|
90
108
|
write(text);
|
|
91
109
|
},
|
|
92
|
-
});
|
|
93
|
-
|
|
110
|
+
}).finally(() => settle?.());
|
|
111
|
+
// A short answer can arrive whole, without a single streamed fragment.
|
|
112
|
+
if (!printed && answer.content) write(answer.content);
|
|
113
|
+
if (printed || answer.content) say();
|
|
94
114
|
|
|
95
115
|
if (!answer.toolCalls?.length) {
|
|
96
116
|
messages.push({ role: "assistant", content: answer.content });
|
|
@@ -117,8 +137,18 @@ async function turn(session, messages) {
|
|
|
117
137
|
activity(call.name, args);
|
|
118
138
|
|
|
119
139
|
let approved = true;
|
|
120
|
-
|
|
121
|
-
|
|
140
|
+
const verdict = ruling(session.permissions, call.name, args);
|
|
141
|
+
if (verdict === "refuse") {
|
|
142
|
+
messages.push({
|
|
143
|
+
role: "tool",
|
|
144
|
+
tool_call_id: call.id,
|
|
145
|
+
content: "Interdit par les règles du projet.",
|
|
146
|
+
});
|
|
147
|
+
say(style.dim(" refusé par une règle"));
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (needsApproval(call.name) && verdict !== "autorise") {
|
|
151
|
+
if (session.mode === "lecture-seule") {
|
|
122
152
|
messages.push({
|
|
123
153
|
role: "tool",
|
|
124
154
|
tool_call_id: call.id,
|
|
@@ -132,8 +162,12 @@ async function turn(session, messages) {
|
|
|
132
162
|
await preview(session.root, args.chemin, String(args.contenu ?? ""));
|
|
133
163
|
if (call.name === "executer" && args.raison)
|
|
134
164
|
say(style.dim(` ${args.raison}`));
|
|
165
|
+
const silent =
|
|
166
|
+
session.mode === "sans-demander" ||
|
|
167
|
+
(session.mode === "accepter-modifications" &&
|
|
168
|
+
call.name === "ecrire_fichier");
|
|
135
169
|
approved =
|
|
136
|
-
|
|
170
|
+
silent ||
|
|
137
171
|
(await confirm(
|
|
138
172
|
call.name === "ecrire_fichier"
|
|
139
173
|
? ` Écrire ${args.chemin} ?`
|
|
@@ -156,15 +190,57 @@ async function turn(session, messages) {
|
|
|
156
190
|
say(style.dim(" (trop d’étapes, arrêt)"));
|
|
157
191
|
}
|
|
158
192
|
|
|
159
|
-
function
|
|
160
|
-
const
|
|
161
|
-
|
|
193
|
+
async function trusted(root) {
|
|
194
|
+
const config = await loadConfig();
|
|
195
|
+
const known = Array.isArray(config.dossiers) ? config.dossiers : [];
|
|
196
|
+
if (known.includes(root)) return true;
|
|
197
|
+
if (!process.stdin.isTTY) return true;
|
|
198
|
+
|
|
199
|
+
say("");
|
|
200
|
+
say(` ${style.bold("Dossier de travail")}`);
|
|
201
|
+
say(` ${brand(root)}`);
|
|
202
|
+
say("");
|
|
162
203
|
say(
|
|
163
204
|
style.dim(
|
|
164
|
-
|
|
205
|
+
" Est-ce un projet que vous avez créé, ou en qui vous avez confiance ?",
|
|
165
206
|
),
|
|
166
207
|
);
|
|
167
|
-
say(
|
|
208
|
+
say(
|
|
209
|
+
style.dim(
|
|
210
|
+
" L’assistant pourra y lire, y écrire et y exécuter des commandes.",
|
|
211
|
+
),
|
|
212
|
+
);
|
|
213
|
+
say("");
|
|
214
|
+
const answer = await choose(
|
|
215
|
+
["Non, quitter", "Oui, je fais confiance à ce dossier"],
|
|
216
|
+
{
|
|
217
|
+
paint: (option, active) =>
|
|
218
|
+
active
|
|
219
|
+
? ` ${brand("›")} ${style.bold(option)}`
|
|
220
|
+
: ` ${style.dim(option)}`,
|
|
221
|
+
},
|
|
222
|
+
);
|
|
223
|
+
say("");
|
|
224
|
+
say(style.dim(" Entrée pour confirmer · Échap pour annuler"));
|
|
225
|
+
if (!answer || answer.startsWith("Non")) return false;
|
|
226
|
+
await saveConfig({ ...config, dossiers: [...known, root].slice(-50) });
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function header(session) {
|
|
231
|
+
const place = session.root.replace(homedir(), "~");
|
|
232
|
+
const facts = [
|
|
233
|
+
"",
|
|
234
|
+
`${brand("HenergyqueAI")} ${style.dim(`v${VERSION}`)}`,
|
|
235
|
+
style.dim(
|
|
236
|
+
`${MODELS[session.modelId] || "Velys"}${session.mode === "demander" ? "" : ` · ${session.mode}`}`,
|
|
237
|
+
),
|
|
238
|
+
style.dim(place),
|
|
239
|
+
style.dim("/aide pour les commandes"),
|
|
240
|
+
];
|
|
241
|
+
say("");
|
|
242
|
+
still().forEach((line, index) => say(` ${line} ${facts[index] ?? ""}`));
|
|
243
|
+
say("");
|
|
168
244
|
}
|
|
169
245
|
|
|
170
246
|
// Slash commands, handled here so a session never needs to be restarted.
|
|
@@ -173,6 +249,8 @@ async function slash(line, session, messages) {
|
|
|
173
249
|
if (["quitter", "exit", "q"].includes(name)) return "fin";
|
|
174
250
|
if (name === "aide") {
|
|
175
251
|
say(style.dim(" /modele <aelyr|oryne|velys> change d’assistant"));
|
|
252
|
+
say(style.dim(" /mode <…> change ce qu’il peut faire"));
|
|
253
|
+
say(style.dim(" /statut ce qui est chargé"));
|
|
176
254
|
say(style.dim(" /effacer oublie la conversation"));
|
|
177
255
|
say(style.dim(" /connexion change de compte"));
|
|
178
256
|
say(style.dim(" /quitter sort"));
|
|
@@ -198,6 +276,39 @@ async function slash(line, session, messages) {
|
|
|
198
276
|
if (key) session.key = key;
|
|
199
277
|
return "fait";
|
|
200
278
|
}
|
|
279
|
+
if (name === "mode") {
|
|
280
|
+
if (!MODES[value]) {
|
|
281
|
+
for (const [nom, quoi] of Object.entries(MODES))
|
|
282
|
+
say(style.dim(` ${nom.padEnd(24)} ${quoi}`));
|
|
283
|
+
say(style.dim(` actuel : ${session.mode}`));
|
|
284
|
+
return "fait";
|
|
285
|
+
}
|
|
286
|
+
session.mode = value;
|
|
287
|
+
say(style.dim(` Mode : ${value} — ${MODES[value]}`));
|
|
288
|
+
return "fait";
|
|
289
|
+
}
|
|
290
|
+
if (name === "statut") {
|
|
291
|
+
say(
|
|
292
|
+
style.dim(` assistant ${MODELS[session.modelId] || session.modelId}`),
|
|
293
|
+
);
|
|
294
|
+
say(style.dim(` mode ${session.mode} — ${MODES[session.mode]}`));
|
|
295
|
+
say(style.dim(` dossier ${session.root}`));
|
|
296
|
+
say(
|
|
297
|
+
style.dim(
|
|
298
|
+
` réglages ${session.reglages.sources.length ? session.reglages.sources.join(", ") : "aucun fichier"}`,
|
|
299
|
+
),
|
|
300
|
+
);
|
|
301
|
+
const { autoriser, refuser } = session.permissions || {};
|
|
302
|
+
if (autoriser?.length)
|
|
303
|
+
say(style.dim(` autorisé ${autoriser.join(", ")}`));
|
|
304
|
+
if (refuser?.length) say(style.dim(` refusé ${refuser.join(", ")}`));
|
|
305
|
+
say(
|
|
306
|
+
style.dim(
|
|
307
|
+
` échanges ${messages.filter((m) => m.role === "user").length}`,
|
|
308
|
+
),
|
|
309
|
+
);
|
|
310
|
+
return "fait";
|
|
311
|
+
}
|
|
201
312
|
say(style.dim(` Commande inconnue. /aide pour la liste.`));
|
|
202
313
|
return "fait";
|
|
203
314
|
}
|
|
@@ -224,20 +335,33 @@ async function main() {
|
|
|
224
335
|
return value ?? null;
|
|
225
336
|
};
|
|
226
337
|
const chosen = take("--modele") || take("--model");
|
|
227
|
-
const
|
|
228
|
-
const
|
|
229
|
-
const
|
|
338
|
+
const askedMode = take("--mode");
|
|
339
|
+
const resume = argv.includes("--continuer") || argv.includes("-c");
|
|
340
|
+
const once = argv.includes("--direct") || argv.includes("-p");
|
|
341
|
+
const words = argv.filter(
|
|
342
|
+
(item) => !item.startsWith("--") && item !== "-c" && item !== "-p",
|
|
343
|
+
);
|
|
230
344
|
|
|
231
345
|
const config = await loadConfig();
|
|
346
|
+
const root = process.cwd();
|
|
347
|
+
const reglages = await loadSettings(root);
|
|
232
348
|
const session = {
|
|
233
349
|
base: config.base || DEFAULT_BASE,
|
|
234
350
|
key: process.env.HENERGYQUEAI_KEY || config.key,
|
|
235
|
-
modelId: chosen || config.modelId || "velys",
|
|
236
|
-
root
|
|
351
|
+
modelId: chosen || config.modelId || reglages.modele || "velys",
|
|
352
|
+
root,
|
|
237
353
|
tools: definitions,
|
|
238
|
-
|
|
239
|
-
|
|
354
|
+
mode:
|
|
355
|
+
(argv.includes("--lecture-seule") && "lecture-seule") ||
|
|
356
|
+
(argv.includes("--oui") && "sans-demander") ||
|
|
357
|
+
(MODES[askedMode] ? askedMode : null) ||
|
|
358
|
+
reglages.mode ||
|
|
359
|
+
"demander",
|
|
360
|
+
permissions: reglages.permissions,
|
|
361
|
+
reglages,
|
|
240
362
|
};
|
|
363
|
+
for (const file of reglages.casses)
|
|
364
|
+
say(style.red(`Réglages illisibles, ignorés : ${file}`));
|
|
241
365
|
|
|
242
366
|
const piped = !process.stdin.isTTY
|
|
243
367
|
? await new Promise((resolve) => {
|
|
@@ -262,12 +386,26 @@ async function main() {
|
|
|
262
386
|
}
|
|
263
387
|
|
|
264
388
|
const messages = [];
|
|
389
|
+
if (resume) {
|
|
390
|
+
const saved = await lastSession(session.root);
|
|
391
|
+
if (saved) {
|
|
392
|
+
messages.push(...saved.messages);
|
|
393
|
+
say(
|
|
394
|
+
style.dim(
|
|
395
|
+
` ${saved.messages.filter((m) => m.role === "user").length} échange(s) repris.`,
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
} else say(style.dim(" Aucune conversation à reprendre ici."));
|
|
399
|
+
}
|
|
265
400
|
try {
|
|
266
401
|
if (first) {
|
|
267
402
|
messages.push({ role: "user", content: first });
|
|
268
403
|
await turn(session, messages);
|
|
404
|
+
if (!once) await saveSession(session.root, messages);
|
|
269
405
|
return;
|
|
270
406
|
}
|
|
407
|
+
if (!(await trusted(session.root))) return;
|
|
408
|
+
clearScreen();
|
|
271
409
|
header(session);
|
|
272
410
|
for (;;) {
|
|
273
411
|
const line = await prompt(`${brand("›")} `);
|
|
@@ -279,6 +417,7 @@ async function main() {
|
|
|
279
417
|
}
|
|
280
418
|
messages.push({ role: "user", content: line });
|
|
281
419
|
await turn(session, messages);
|
|
420
|
+
await saveSession(session.root, messages);
|
|
282
421
|
say();
|
|
283
422
|
}
|
|
284
423
|
} catch (error) {
|
package/package.json
CHANGED
package/src/butterfly.js
CHANGED
|
@@ -1,15 +1,145 @@
|
|
|
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 place while something is being waited for. Returns the function
|
|
111
|
+
// that stops it and wipes the drawing away.
|
|
112
|
+
export function flutter(label = "") {
|
|
113
|
+
if (plain || !process.stdout.isTTY) {
|
|
114
|
+
if (label) process.stdout.write(`${label}\n`);
|
|
115
|
+
return () => {};
|
|
116
|
+
}
|
|
117
|
+
let at = 0;
|
|
118
|
+
let drawn = false;
|
|
119
|
+
const show = () => {
|
|
120
|
+
if (drawn) process.stdout.write(`\u001b[${HEIGHT}A`);
|
|
121
|
+
const lines = paint(FRAMES[at % FRAMES.length]);
|
|
122
|
+
process.stdout.write(
|
|
123
|
+
lines
|
|
124
|
+
.map(
|
|
125
|
+
(line, index) =>
|
|
126
|
+
`\u001b[2K${line}${index === 2 && label ? ` \u001b[2m${label}\u001b[0m` : ""}\n`,
|
|
127
|
+
)
|
|
128
|
+
.join(""),
|
|
129
|
+
);
|
|
130
|
+
drawn = true;
|
|
131
|
+
at += 1;
|
|
132
|
+
};
|
|
133
|
+
process.stdout.write("\u001b[?25l");
|
|
134
|
+
show();
|
|
135
|
+
const timer = setInterval(show, 140);
|
|
136
|
+
return () => {
|
|
137
|
+
clearInterval(timer);
|
|
138
|
+
if (drawn) {
|
|
139
|
+
process.stdout.write(`\u001b[${HEIGHT}A`);
|
|
140
|
+
process.stdout.write(`\u001b[2K\n`.repeat(HEIGHT));
|
|
141
|
+
process.stdout.write(`\u001b[${HEIGHT}A`);
|
|
142
|
+
}
|
|
143
|
+
process.stdout.write("\u001b[?25h");
|
|
144
|
+
};
|
|
145
|
+
}
|
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
|
+
}
|