shabti 1.2.0 → 1.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/package.json +3 -1
- package/src/commands/chat.js +84 -0
- package/src/index.js +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shabti",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Demo CLI tool — showcasing npm-publishable CLI structure",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -37,6 +37,8 @@
|
|
|
37
37
|
"chalk": "^5.6.2",
|
|
38
38
|
"cli-table3": "^0.6.5",
|
|
39
39
|
"commander": "^14.0.3",
|
|
40
|
+
"dotenv": "^17.3.1",
|
|
41
|
+
"openai": "^6.22.0",
|
|
40
42
|
"ora": "^9.3.0"
|
|
41
43
|
},
|
|
42
44
|
"lint-staged": {
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createInterface } from "readline";
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { error, info } from "../utils/style.js";
|
|
5
|
+
|
|
6
|
+
const SYSTEM_PROMPT = `You are Shabti, a helpful assistant accessed via CLI. Be concise and direct.`;
|
|
7
|
+
|
|
8
|
+
export function registerChat(program) {
|
|
9
|
+
program
|
|
10
|
+
.command("chat")
|
|
11
|
+
.description("Start an interactive chat session with GPT")
|
|
12
|
+
.option("-m, --model <model>", "OpenAI model to use", "gpt-4o-mini")
|
|
13
|
+
.option("-s, --system <prompt>", "Custom system prompt", SYSTEM_PROMPT)
|
|
14
|
+
.action(async (opts) => {
|
|
15
|
+
await import("dotenv/config");
|
|
16
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
17
|
+
if (!apiKey || apiKey === "your-api-key-here") {
|
|
18
|
+
error("OPENAI_API_KEY is not set. Add it to .env in the project root.");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const client = new OpenAI({ apiKey });
|
|
23
|
+
const messages = [{ role: "system", content: opts.system }];
|
|
24
|
+
|
|
25
|
+
console.log();
|
|
26
|
+
info(`shabti chat — model: ${chalk.cyan(opts.model)}`);
|
|
27
|
+
console.log(chalk.dim(" Type your message. Ctrl+C or 'exit' to quit.\n"));
|
|
28
|
+
|
|
29
|
+
const rl = createInterface({
|
|
30
|
+
input: process.stdin,
|
|
31
|
+
output: process.stdout,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const prompt = () => {
|
|
35
|
+
rl.question(chalk.green("You: "), async (input) => {
|
|
36
|
+
const trimmed = input.trim();
|
|
37
|
+
if (!trimmed) return prompt();
|
|
38
|
+
if (trimmed.toLowerCase() === "exit") {
|
|
39
|
+
console.log(chalk.dim("\nBye.\n"));
|
|
40
|
+
rl.close();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
messages.push({ role: "user", content: trimmed });
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
process.stdout.write(chalk.cyan("shabti: "));
|
|
48
|
+
|
|
49
|
+
const stream = await client.chat.completions.create({
|
|
50
|
+
model: opts.model,
|
|
51
|
+
messages,
|
|
52
|
+
stream: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
let reply = "";
|
|
56
|
+
for await (const chunk of stream) {
|
|
57
|
+
const delta = chunk.choices[0]?.delta?.content || "";
|
|
58
|
+
reply += delta;
|
|
59
|
+
process.stdout.write(delta);
|
|
60
|
+
}
|
|
61
|
+
console.log("\n");
|
|
62
|
+
|
|
63
|
+
messages.push({ role: "assistant", content: reply });
|
|
64
|
+
} catch (err) {
|
|
65
|
+
console.log();
|
|
66
|
+
if (err.status === 401) {
|
|
67
|
+
error("Invalid API key. Check your .env file.");
|
|
68
|
+
} else if (err.status === 429) {
|
|
69
|
+
error("Rate limited. Wait a moment and try again.");
|
|
70
|
+
} else {
|
|
71
|
+
error(`API error: ${err.message}`);
|
|
72
|
+
}
|
|
73
|
+
console.log();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
prompt();
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
prompt();
|
|
81
|
+
|
|
82
|
+
rl.on("close", () => process.exit(0));
|
|
83
|
+
});
|
|
84
|
+
}
|
package/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import chalk from "chalk";
|
|
4
4
|
import { Command } from "commander";
|
|
5
|
+
import { registerChat } from "./commands/chat.js";
|
|
5
6
|
import { registerHello } from "./commands/hello.js";
|
|
6
7
|
import { registerList } from "./commands/list.js";
|
|
7
8
|
import { registerSpin } from "./commands/spin.js";
|
|
@@ -41,6 +42,7 @@ program
|
|
|
41
42
|
.version("1.0.0");
|
|
42
43
|
|
|
43
44
|
// Register commands
|
|
45
|
+
registerChat(program);
|
|
44
46
|
registerHello(program);
|
|
45
47
|
registerList(program);
|
|
46
48
|
registerSpin(program);
|