henergyqueai 0.1.0 → 0.2.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 +15 -3
- package/bin/henergyqueai.js +105 -46
- package/package.json +1 -1
- package/src/butterfly.js +5 -20
- package/src/connexion.js +78 -0
package/README.md
CHANGED
|
@@ -10,13 +10,15 @@ npm install -g henergyqueai
|
|
|
10
10
|
|
|
11
11
|
## Se connecter
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Rien à faire. Lancez-le :
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
henergyqueai
|
|
16
|
+
henergyqueai
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
Votre navigateur s’ouvre, vous cliquez sur **Autoriser**, c’est fini. Aucune clé à copier.
|
|
20
|
+
|
|
21
|
+
L’accès est enregistré dans `~/.henergyqueai/config.json`, lisible par vous seul, et révocable à tout moment depuis **Paramètres → Clés d’API**.
|
|
20
22
|
|
|
21
23
|
## Utiliser
|
|
22
24
|
|
|
@@ -37,6 +39,16 @@ L’assistant n’a accès qu’au dossier depuis lequel vous l’avez lancé.
|
|
|
37
39
|
| `--lecture-seule` | Interdit toute écriture et toute commande |
|
|
38
40
|
| `--oui` | Accepte les modifications sans demander |
|
|
39
41
|
|
|
42
|
+
## En session
|
|
43
|
+
|
|
44
|
+
| Commande | Effet |
|
|
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 |
|
|
51
|
+
|
|
40
52
|
## Ce qu’il fait sans demander, et ce qu’il vous soumet
|
|
41
53
|
|
|
42
54
|
Lire un fichier, explorer le projet et chercher un texte se font librement.
|
package/bin/henergyqueai.js
CHANGED
|
@@ -3,6 +3,7 @@ import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
|
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { ask } from "../src/client.js";
|
|
6
|
+
import { awaitAuthorization } from "../src/connexion.js";
|
|
6
7
|
import { definitions, execute, needsApproval } from "../src/tools.js";
|
|
7
8
|
import {
|
|
8
9
|
activity,
|
|
@@ -13,10 +14,12 @@ import {
|
|
|
13
14
|
style,
|
|
14
15
|
write,
|
|
15
16
|
} from "../src/ui.js";
|
|
16
|
-
import { brand,
|
|
17
|
+
import { brand, mark } from "../src/butterfly.js";
|
|
17
18
|
|
|
19
|
+
const VERSION = "0.2.0";
|
|
18
20
|
const CONFIG = path.join(homedir(), ".henergyqueai", "config.json");
|
|
19
21
|
const DEFAULT_BASE = process.env.HENERGYQUEAI_URL || "https://henergyqueai.fr";
|
|
22
|
+
const MODELS = { aelyr: "Aelyr", oryne: "Oryne", velys: "Velys" };
|
|
20
23
|
|
|
21
24
|
async function loadConfig() {
|
|
22
25
|
const raw = await readFile(CONFIG, "utf8").catch(() => null);
|
|
@@ -31,46 +34,48 @@ async function loadConfig() {
|
|
|
31
34
|
async function saveConfig(config) {
|
|
32
35
|
await mkdir(path.dirname(CONFIG), { recursive: true });
|
|
33
36
|
await writeFile(CONFIG, JSON.stringify(config, null, 2), "utf8");
|
|
34
|
-
// The key is a credential:
|
|
37
|
+
// The key is a credential: readable by its owner only.
|
|
35
38
|
await chmod(CONFIG, 0o600).catch(() => {});
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
function usage() {
|
|
39
42
|
say(`
|
|
40
|
-
${
|
|
43
|
+
${mark()} ${brand("henergyqueai")} ${style.dim(`v${VERSION}`)}
|
|
41
44
|
|
|
42
|
-
${
|
|
45
|
+
${style.bold("henergyqueai")} ouvre une session
|
|
46
|
+
${style.bold('henergyqueai "votre demande"')} répond puis rend la main
|
|
47
|
+
${style.dim('cat fichier | henergyqueai "…"')} lit l’entrée standard
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
henergyqueai deconnexion efface la clé enregistrée
|
|
49
|
+
${style.dim("--modele aelyr|oryne|velys")} choisit l’assistant
|
|
50
|
+
${style.dim("--lecture-seule")} interdit écriture et commandes
|
|
51
|
+
${style.dim("--oui")} accepte sans demander
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
--lecture-seule interdit toute écriture et commande
|
|
52
|
-
--oui accepte les modifications sans demander
|
|
53
|
-
|
|
54
|
-
Créez une clé depuis ${DEFAULT_BASE}/parametres`);
|
|
53
|
+
${style.dim("En session : /aide /modele /effacer /connexion /quitter")}
|
|
54
|
+
`);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
say(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
// First run opens the browser, the way a sign-in should. Nothing to copy.
|
|
58
|
+
async function connect() {
|
|
59
|
+
say(`\n ${mark()} ${brand("HenergyqueAI")}\n`);
|
|
60
|
+
say(style.dim(" Ouverture de votre navigateur pour autoriser ce terminal…"));
|
|
61
|
+
try {
|
|
62
|
+
const key = await awaitAuthorization({
|
|
63
|
+
base: DEFAULT_BASE,
|
|
64
|
+
onUrl: (url, opened) => {
|
|
65
|
+
if (!opened) say(style.dim(" Ouvrez cette adresse :"));
|
|
66
|
+
say(style.dim(` ${url}\n`));
|
|
67
|
+
say(style.dim(" En attente de votre accord…"));
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
await saveConfig({ ...(await loadConfig()), key });
|
|
71
|
+
say(style.green("\n Connecté.\n"));
|
|
72
|
+
return key;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
say(style.red(`\n ${error.message}\n`));
|
|
75
|
+
return null;
|
|
66
76
|
}
|
|
67
|
-
const config = await loadConfig();
|
|
68
|
-
await saveConfig({ ...config, key });
|
|
69
|
-
say(style.green(`Clé enregistrée dans ${CONFIG}.`));
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
// One exchange: the model answers, may ask for tools, and we loop until it
|
|
73
|
-
// stops asking. Every write or command passes through the user first.
|
|
74
79
|
async function turn(session, messages) {
|
|
75
80
|
for (let round = 0; round < 24; round += 1) {
|
|
76
81
|
let printed = false;
|
|
@@ -151,11 +156,61 @@ async function turn(session, messages) {
|
|
|
151
156
|
say(style.dim(" (trop d’étapes, arrêt)"));
|
|
152
157
|
}
|
|
153
158
|
|
|
159
|
+
function header(session) {
|
|
160
|
+
const place = session.root.replace(homedir(), "~");
|
|
161
|
+
say(`\n ${mark()} ${brand("HenergyqueAI")} ${style.dim(`v${VERSION}`)}`);
|
|
162
|
+
say(
|
|
163
|
+
style.dim(
|
|
164
|
+
` ${MODELS[session.modelId] || "Velys"} · ${place}${session.readOnly ? " · lecture seule" : ""}`,
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
say(style.dim(" /aide pour les commandes\n"));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Slash commands, handled here so a session never needs to be restarted.
|
|
171
|
+
async function slash(line, session, messages) {
|
|
172
|
+
const [name, value] = line.slice(1).trim().split(/\s+/);
|
|
173
|
+
if (["quitter", "exit", "q"].includes(name)) return "fin";
|
|
174
|
+
if (name === "aide") {
|
|
175
|
+
say(style.dim(" /modele <aelyr|oryne|velys> change d’assistant"));
|
|
176
|
+
say(style.dim(" /effacer oublie la conversation"));
|
|
177
|
+
say(style.dim(" /connexion change de compte"));
|
|
178
|
+
say(style.dim(" /quitter sort"));
|
|
179
|
+
return "fait";
|
|
180
|
+
}
|
|
181
|
+
if (name === "effacer") {
|
|
182
|
+
messages.length = 0;
|
|
183
|
+
say(style.dim(" Conversation oubliée."));
|
|
184
|
+
return "fait";
|
|
185
|
+
}
|
|
186
|
+
if (name === "modele") {
|
|
187
|
+
if (!MODELS[value]) {
|
|
188
|
+
say(style.dim(` Assistants : ${Object.keys(MODELS).join(", ")}`));
|
|
189
|
+
return "fait";
|
|
190
|
+
}
|
|
191
|
+
session.modelId = value;
|
|
192
|
+
await saveConfig({ ...(await loadConfig()), modelId: value });
|
|
193
|
+
say(style.dim(` Assistant : ${MODELS[value]}`));
|
|
194
|
+
return "fait";
|
|
195
|
+
}
|
|
196
|
+
if (name === "connexion") {
|
|
197
|
+
const key = await connect();
|
|
198
|
+
if (key) session.key = key;
|
|
199
|
+
return "fait";
|
|
200
|
+
}
|
|
201
|
+
say(style.dim(` Commande inconnue. /aide pour la liste.`));
|
|
202
|
+
return "fait";
|
|
203
|
+
}
|
|
204
|
+
|
|
154
205
|
async function main() {
|
|
155
206
|
const argv = process.argv.slice(2);
|
|
156
207
|
if (argv.includes("--aide") || argv.includes("-h") || argv.includes("--help"))
|
|
157
208
|
return usage();
|
|
158
|
-
if (argv
|
|
209
|
+
if (argv.includes("--version") || argv.includes("-v")) return say(VERSION);
|
|
210
|
+
if (argv[0] === "connexion" || argv[0] === "login") {
|
|
211
|
+
await connect();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
159
214
|
if (argv[0] === "deconnexion" || argv[0] === "logout") {
|
|
160
215
|
await saveConfig({ ...(await loadConfig()), key: undefined });
|
|
161
216
|
return say("Clé effacée.");
|
|
@@ -168,30 +223,22 @@ async function main() {
|
|
|
168
223
|
argv.splice(at, value ? 2 : 1);
|
|
169
224
|
return value ?? null;
|
|
170
225
|
};
|
|
171
|
-
const
|
|
226
|
+
const chosen = take("--modele") || take("--model");
|
|
172
227
|
const readOnly = argv.includes("--lecture-seule");
|
|
173
228
|
const yes = argv.includes("--oui");
|
|
174
229
|
const words = argv.filter((item) => !item.startsWith("--"));
|
|
175
230
|
|
|
176
231
|
const config = await loadConfig();
|
|
177
|
-
const key = process.env.HENERGYQUEAI_KEY || config.key;
|
|
178
|
-
if (!key) {
|
|
179
|
-
say(style.red("Aucune clé enregistrée."));
|
|
180
|
-
say(`Lancez ${style.bold("henergyqueai connexion")} pour en ajouter une.`);
|
|
181
|
-
process.exit(1);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
232
|
const session = {
|
|
185
233
|
base: config.base || DEFAULT_BASE,
|
|
186
|
-
key,
|
|
187
|
-
modelId:
|
|
234
|
+
key: process.env.HENERGYQUEAI_KEY || config.key,
|
|
235
|
+
modelId: chosen || config.modelId || "velys",
|
|
188
236
|
root: process.cwd(),
|
|
189
237
|
tools: definitions,
|
|
190
238
|
readOnly,
|
|
191
239
|
yes,
|
|
192
240
|
};
|
|
193
241
|
|
|
194
|
-
const messages = [];
|
|
195
242
|
const piped = !process.stdin.isTTY
|
|
196
243
|
? await new Promise((resolve) => {
|
|
197
244
|
let raw = "";
|
|
@@ -200,24 +247,36 @@ async function main() {
|
|
|
200
247
|
process.stdin.on("end", () => resolve(raw));
|
|
201
248
|
})
|
|
202
249
|
: "";
|
|
203
|
-
|
|
204
250
|
const first = [words.join(" "), piped && `\n\n${piped.slice(0, 100000)}`]
|
|
205
251
|
.filter(Boolean)
|
|
206
252
|
.join("");
|
|
207
253
|
|
|
254
|
+
// No key yet: connect instead of telling the user what to go and do.
|
|
255
|
+
if (!session.key) {
|
|
256
|
+
if (!process.stdin.isTTY) {
|
|
257
|
+
say(style.red("Aucune clé. Lancez henergyqueai connexion."));
|
|
258
|
+
process.exit(1);
|
|
259
|
+
}
|
|
260
|
+
session.key = await connect();
|
|
261
|
+
if (!session.key) process.exit(1);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const messages = [];
|
|
208
265
|
try {
|
|
209
266
|
if (first) {
|
|
210
267
|
messages.push({ role: "user", content: first });
|
|
211
268
|
await turn(session, messages);
|
|
212
269
|
return;
|
|
213
270
|
}
|
|
214
|
-
|
|
215
|
-
say(` ${brand("HenergyqueAI")}${style.dim(` — ${session.root}`)}`);
|
|
216
|
-
say(style.dim(" Écrivez votre demande. « quitter » pour sortir.\n"));
|
|
271
|
+
header(session);
|
|
217
272
|
for (;;) {
|
|
218
|
-
const line = await prompt("›
|
|
273
|
+
const line = await prompt(`${brand("›")} `);
|
|
219
274
|
if (!line) continue;
|
|
220
|
-
if (
|
|
275
|
+
if (line.startsWith("/")) {
|
|
276
|
+
if ((await slash(line, session, messages)) === "fin") return;
|
|
277
|
+
say();
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
221
280
|
messages.push({ role: "user", content: line });
|
|
222
281
|
await turn(session, messages);
|
|
223
282
|
say();
|
package/package.json
CHANGED
package/src/butterfly.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
1
|
+
// A small mark rather than a drawing: a terminal banner should sit quietly at
|
|
2
|
+
// the top, not fill the screen. Wings in the brand's light tone, body in its
|
|
3
|
+
// deepest one.
|
|
3
4
|
const plain = !process.stdout.isTTY || process.env.NO_COLOR;
|
|
4
5
|
|
|
5
6
|
const rgb = (hex, text) => {
|
|
@@ -8,23 +9,7 @@ const rgb = (hex, text) => {
|
|
|
8
9
|
return `\u001b[38;2;${(value >> 16) & 255};${(value >> 8) & 255};${value & 255}m${text}\u001b[0m`;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const BODY = "#5d3b2f";
|
|
14
|
-
|
|
15
|
-
const shape = [
|
|
16
|
-
" ▗▄▄▖ ▗▄▄▖ ",
|
|
17
|
-
" ▟███▙▓▟███▙ ",
|
|
18
|
-
"▐█████▓█████▌",
|
|
19
|
-
" ▜███▛▓▜███▛ ",
|
|
20
|
-
" ▝▀▘ ▀ ▝▀▘",
|
|
21
|
-
];
|
|
22
|
-
|
|
23
|
-
// The body runs down the middle, so column 6 is tinted apart from the wings.
|
|
24
|
-
const tinted = (line, wing) =>
|
|
25
|
-
rgb(wing, line.slice(0, 6)) + rgb(BODY, line[6]) + rgb(wing, line.slice(7));
|
|
26
|
-
|
|
27
|
-
export const butterfly = () =>
|
|
28
|
-
shape.map((line, index) => tinted(line, WINGS[index])).join("\n");
|
|
12
|
+
export const mark = () =>
|
|
13
|
+
`${rgb("#edac88", "◗")}${rgb("#784e3e", "▮")}${rgb("#edac88", "◖")}`;
|
|
29
14
|
|
|
30
15
|
export const brand = (text) => rgb("#c46c4f", text);
|
package/src/connexion.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { hostname } from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
|
|
6
|
+
const openBrowser = (url) => {
|
|
7
|
+
const [command, args] =
|
|
8
|
+
process.platform === "win32"
|
|
9
|
+
? ["cmd", ["/c", "start", "", url]]
|
|
10
|
+
: process.platform === "darwin"
|
|
11
|
+
? ["open", [url]]
|
|
12
|
+
: ["xdg-open", [url]];
|
|
13
|
+
try {
|
|
14
|
+
spawn(command, args, { detached: true, stdio: "ignore" }).unref();
|
|
15
|
+
return true;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const page = (title, body) => `<!doctype html><html lang="fr"><head>
|
|
22
|
+
<meta charset="utf-8"><title>${title}</title><style>
|
|
23
|
+
body{font-family:system-ui,sans-serif;background:#faf9f6;color:#3a4132;
|
|
24
|
+
display:flex;align-items:center;justify-content:center;height:100vh;margin:0}
|
|
25
|
+
div{text-align:center;max-width:420px;padding:0 24px}
|
|
26
|
+
h1{font-size:22px;margin:0 0 10px}p{color:#626c56;line-height:1.7;font-size:14px}
|
|
27
|
+
</style></head><body><div><h1>${title}</h1><p>${body}</p></div></body></html>`;
|
|
28
|
+
|
|
29
|
+
// Waits for the browser to hand the key back, the way a loopback sign-in does.
|
|
30
|
+
// Nothing is typed, nothing is pasted.
|
|
31
|
+
export function awaitAuthorization({ base, onUrl, timeout = 180000 }) {
|
|
32
|
+
const code = randomBytes(24).toString("base64url");
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const server = createServer((req, res) => {
|
|
35
|
+
const url = new URL(req.url, "http://127.0.0.1");
|
|
36
|
+
// The browser also asks for a favicon; only the landing path matters.
|
|
37
|
+
if (url.pathname !== "/") {
|
|
38
|
+
res.writeHead(204).end();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const key = url.searchParams.get("cle");
|
|
42
|
+
const given = url.searchParams.get("code");
|
|
43
|
+
const good = key && given === code;
|
|
44
|
+
res.writeHead(good ? 200 : 400, {
|
|
45
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
46
|
+
});
|
|
47
|
+
res.end(
|
|
48
|
+
good
|
|
49
|
+
? page(
|
|
50
|
+
"C’est bon.",
|
|
51
|
+
"Vous pouvez fermer cet onglet et retourner à votre terminal.",
|
|
52
|
+
)
|
|
53
|
+
: page(
|
|
54
|
+
"Demande invalide.",
|
|
55
|
+
"Relancez henergyqueai depuis votre terminal.",
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
if (!good) return;
|
|
59
|
+
clearTimeout(limit);
|
|
60
|
+
server.close();
|
|
61
|
+
resolve(key);
|
|
62
|
+
});
|
|
63
|
+
const limit = setTimeout(() => {
|
|
64
|
+
server.close();
|
|
65
|
+
reject(new Error("Délai dépassé : personne n’a autorisé la connexion."));
|
|
66
|
+
}, timeout);
|
|
67
|
+
server.on("error", (error) => {
|
|
68
|
+
clearTimeout(limit);
|
|
69
|
+
reject(error);
|
|
70
|
+
});
|
|
71
|
+
// Port 0 lets the system choose a free one, bound to this machine only.
|
|
72
|
+
server.listen(0, "127.0.0.1", () => {
|
|
73
|
+
const { port } = server.address();
|
|
74
|
+
const target = `${base}/terminal?port=${port}&code=${encodeURIComponent(code)}&machine=${encodeURIComponent(hostname().slice(0, 40))}`;
|
|
75
|
+
onUrl(target, openBrowser(target));
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|