netic 0.1.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 +1 -0
- package/bin/netic +2 -0
- package/package.json +23 -0
- package/src/cli.js +43 -0
- package/src/client.js +27 -0
- package/src/config.js +17 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/bin/netic
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "netic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI + package Node.js pour discuter avec Netic API",
|
|
5
|
+
"main": "src/client.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"netic": "./bin/netic"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node ./test.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["cli", "chat", "netic", "api"],
|
|
14
|
+
"author": "qqln",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"axios": "^1.6.0",
|
|
18
|
+
"commander": "^11.0.0"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { program } from "commander";
|
|
3
|
+
import readline from "readline";
|
|
4
|
+
import { setApiKey } from "./config.js";
|
|
5
|
+
import { chat } from "./client.js";
|
|
6
|
+
|
|
7
|
+
program
|
|
8
|
+
.command("setkey <key>")
|
|
9
|
+
.description("Set your API key")
|
|
10
|
+
.action((key) => {
|
|
11
|
+
setApiKey(key);
|
|
12
|
+
console.log("API key saved!");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
program
|
|
16
|
+
.command("chat")
|
|
17
|
+
.description("Start interactive chat")
|
|
18
|
+
.action(() => {
|
|
19
|
+
console.log("Netic chat lancé (Ctrl+C pour quitter)");
|
|
20
|
+
const rl = readline.createInterface({
|
|
21
|
+
input: process.stdin,
|
|
22
|
+
output: process.stdout,
|
|
23
|
+
prompt: "> ",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
rl.prompt();
|
|
27
|
+
|
|
28
|
+
rl.on("line", async (line) => {
|
|
29
|
+
if (!line.trim()) return rl.prompt();
|
|
30
|
+
try {
|
|
31
|
+
const res = await chat(line.trim());
|
|
32
|
+
console.log(res);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
console.error(err.message);
|
|
35
|
+
}
|
|
36
|
+
rl.prompt();
|
|
37
|
+
}).on("SIGINT", () => {
|
|
38
|
+
console.log("\nBye!");
|
|
39
|
+
process.exit(0);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
program.parse(process.argv);
|
package/src/client.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { getApiKey } from "./config.js";
|
|
3
|
+
|
|
4
|
+
const API_URL = "https://netic.jtheberg.cloud/api/v1/chat";
|
|
5
|
+
|
|
6
|
+
export async function chat(message) {
|
|
7
|
+
const key = getApiKey();
|
|
8
|
+
if (!key) throw new Error("API key not set. Use netic setkey <key>");
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const res = await axios.post(API_URL, { message }, {
|
|
12
|
+
headers: {
|
|
13
|
+
Authorization: `Bearer ${key}`,
|
|
14
|
+
"Content-Type": "application/json",
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// đ„ Retourne uniquement la rĂ©ponse textuelle
|
|
19
|
+
return res.data.response;
|
|
20
|
+
|
|
21
|
+
} catch (err) {
|
|
22
|
+
if (err.response) {
|
|
23
|
+
throw new Error(`HTTP ${err.response.status}: ${err.response.data?.error || err.response.statusText}`);
|
|
24
|
+
}
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/config.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
|
|
5
|
+
const configDir = path.join(os.homedir(), ".netic");
|
|
6
|
+
const configFile = path.join(configDir, "config.json");
|
|
7
|
+
|
|
8
|
+
export function setApiKey(key) {
|
|
9
|
+
if (!fs.existsSync(configDir)) fs.mkdirSync(configDir);
|
|
10
|
+
fs.writeFileSync(configFile, JSON.stringify({ key }));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getApiKey() {
|
|
14
|
+
if (!fs.existsSync(configFile)) return null;
|
|
15
|
+
const data = JSON.parse(fs.readFileSync(configFile, "utf8"));
|
|
16
|
+
return data.key;
|
|
17
|
+
}
|