-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathcli.ts
More file actions
145 lines (134 loc) · 3.92 KB
/
cli.ts
File metadata and controls
145 lines (134 loc) · 3.92 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
import * as readline from "node:readline/promises";
import { stdin, stdout } from "node:process";
import { join } from "node:path";
import { homedir } from "node:os";
import type { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
import type { InferenceProvider } from "@huggingface/inference";
import { ANSI } from "./src/utils";
import { Agent } from "./src";
import { version as packageVersion } from "./package.json";
const MODEL_ID = process.env.MODEL_ID ?? "Qwen/Qwen2.5-72B-Instruct";
const PROVIDER = (process.env.PROVIDER as InferenceProvider) ?? "nebius";
const ENDPOINT_URL = process.env.ENDPOINT_URL ?? process.env.BASE_URL;
const SERVERS: StdioServerParameters[] = [
{
// Filesystem "official" mcp-server with access to your Desktop
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", join(homedir(), "Desktop")],
},
{
// Playwright MCP
command: "npx",
args: ["@playwright/mcp@latest"],
},
];
if (process.env.EXPERIMENTAL_HF_MCP_SERVER) {
SERVERS.push({
// Early version of a HF-MCP server
// you can download it from gist.github.com/julien-c/0500ba922e1b38f2dc30447fb81f7dc6
command: "node",
args: ["--disable-warning=ExperimentalWarning", join(homedir(), "Desktop/hf-mcp/index.ts")],
env: {
HF_TOKEN: process.env.HF_TOKEN ?? "",
},
});
}
async function main() {
if (process.argv.includes("--version")) {
console.log(packageVersion);
process.exit(0);
}
if (!process.env.HF_TOKEN) {
console.error(`a valid HF_TOKEN must be present in the env`);
process.exit(1);
}
const agent = new Agent(
ENDPOINT_URL
? {
endpointUrl: ENDPOINT_URL,
model: MODEL_ID,
apiKey: process.env.HF_TOKEN,
servers: SERVERS,
}
: {
provider: PROVIDER,
model: MODEL_ID,
apiKey: process.env.HF_TOKEN,
servers: SERVERS,
}
);
const rl = readline.createInterface({ input: stdin, output: stdout });
let abortController = new AbortController();
let waitingForInput = false;
async function waitForInput() {
waitingForInput = true;
const input = await rl.question("> ");
waitingForInput = false;
return input;
}
rl.on("SIGINT", async () => {
if (waitingForInput) {
// close the whole process
await agent.cleanup();
stdout.write("\n");
rl.close();
} else {
// otherwise, it means a request is underway
abortController.abort();
abortController = new AbortController();
stdout.write("\n");
stdout.write(ANSI.GRAY);
stdout.write("Ctrl+C a second time to exit");
stdout.write(ANSI.RESET);
stdout.write("\n");
}
});
process.on("uncaughtException", (err) => {
stdout.write("\n");
rl.close();
throw err;
});
await agent.loadTools();
stdout.write(ANSI.BLUE);
stdout.write(`Agent loaded with ${agent.availableTools.length} tools:\n`);
stdout.write(agent.availableTools.map((t) => `- ${t.function.name}`).join("\n"));
stdout.write(ANSI.RESET);
stdout.write("\n");
while (true) {
const input = await waitForInput();
for await (const chunk of agent.run(input, { abortSignal: abortController.signal })) {
if ("choices" in chunk) {
const delta = chunk.choices[0]?.delta;
if (delta.content) {
stdout.write(delta.content);
}
if (delta.tool_calls) {
stdout.write(ANSI.GRAY);
for (const deltaToolCall of delta.tool_calls) {
if (deltaToolCall.id) {
stdout.write(`<Tool ${deltaToolCall.id}>\n`);
}
if (deltaToolCall.function.name) {
stdout.write(deltaToolCall.function.name + " ");
}
if (deltaToolCall.function.arguments) {
stdout.write(deltaToolCall.function.arguments);
}
}
stdout.write(ANSI.RESET);
}
} else {
/// Tool call info
stdout.write("\n\n");
stdout.write(ANSI.GREEN);
stdout.write(`Tool[${chunk.name}] ${chunk.tool_call_id}\n`);
stdout.write(chunk.content);
stdout.write(ANSI.RESET);
stdout.write("\n\n");
}
}
stdout.write("\n");
}
}
main();