-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathchat.ts
More file actions
35 lines (28 loc) · 1.18 KB
/
chat.ts
File metadata and controls
35 lines (28 loc) · 1.18 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
import { CopilotClient, approveAll, type SessionEvent } from "@github/copilot-sdk";
import * as readline from "node:readline";
async function main() {
const client = new CopilotClient();
const session = await client.createSession({
onPermissionRequest: approveAll,
});
session.on((event: SessionEvent) => {
let output: string | null = null;
if (event.type === "assistant.reasoning") {
output = `[reasoning: ${event.data.content}]`;
} else if (event.type === "tool.execution_start") {
output = `[tool: ${event.data.toolName}]`;
}
if (output) console.log(`\x1b[34m${output}\x1b[0m`);
});
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const prompt = (q: string) => new Promise<string>((r) => rl.question(q, r));
console.log("Chat with Copilot (Ctrl+C to exit)\n");
while (true) {
const input = await prompt("You: ");
if (!input.trim()) continue;
console.log();
const reply = await session.sendAndWait({ prompt: input });
console.log(`\nAssistant: ${reply?.data.content}\n`);
}
}
main().catch(console.error);