-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
128 lines (108 loc) · 3.53 KB
/
index.js
File metadata and controls
128 lines (108 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { program } from "commander";
import chalk from "chalk";
import ora from "ora";
import dotenv from "dotenv";
import {
listAIs,
createAI,
createChat,
sendMessage,
queryAI,
} from "./api-handler.js";
import xhr from "xhr2";
// This is not required in the browser
global.XMLHttpRequest = xhr;
dotenv.config();
program.version("1.0.0").description("AI CLI Tool");
program
.command("list")
.description("List all my AIs")
.action(async () => {
const spinner = ora("Fetching available AIs...").start();
try {
const models = await listAIs();
spinner.succeed("Your AIs:");
models.forEach((model) => {
console.log(chalk.white(`- ${model.name}`));
console.log(chalk.white(` ${model.id}`));
console.log(chalk.gray(` ${model.description}`));
});
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
});
program
.command("create-ai")
.description("Create a new AI")
.requiredOption("-n, --name <name>", "name of the AI")
.requiredOption("-d, --description <description>", "description of the AI")
.option("-i, --instructions <instructions>", "instructions for the AI")
.option("-a, --avatar <src>", "URL for the avatar of the AI")
.action(async (options) => {
const spinner = ora("Creating a new AI...").start();
try {
const result = await createAI(
options.name,
options.description,
options.instructions,
options.src
);
spinner.succeed("AI created successfully:");
console.log(chalk.white(`- ${result.name}`));
console.log(chalk.white(` ${result.id}`));
console.log(chalk.gray(` ${result.description}`));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
});
program
.command("create-chat")
.description("Create a new chat session")
.requiredOption("-i, --id <id>", "id of the AI")
.action(async (options) => {
const spinner = ora("Creating a new chat thread...").start();
try {
const result = await createChat(options.id);
spinner.succeed("Chat created successfully:");
console.log(chalk.white(`- ${result.id}`));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
});
program
.command("msg")
.requiredOption("-i, --id <id>", "id of the chat")
.argument("<message>", "message to send to AI")
.action(async (message, options) => {
const spinner = ora("Processing your message...").start();
try {
const eventSource = await sendMessage(options.id, message);
spinner.succeed("AI is responding");
eventSource.addEventListener("message.delta", (e) => {
const data = JSON.parse(e.data);
process.stdout.write(data.content.text);
});
eventSource.addEventListener("message.complete", () => {
console.log(chalk.green("\nResponse completed"));
});
eventSource.stream();
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
});
program
.command("query-ai")
.description("Query an AI directly")
.requiredOption("-i, --id <id>", "id of the AI")
.argument("<message>", "message to send to AI")
.action(async (message, options) => {
const spinner = ora("Processing your query...").start();
try {
const response = await queryAI(options.id, message);
spinner.succeed("AI response:");
console.log(chalk.white(response.choices[0].message.content));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
});
program.parse();