-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc-example.ts
More file actions
86 lines (70 loc) · 2 KB
/
rpc-example.ts
File metadata and controls
86 lines (70 loc) · 2 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
import { dirname, join } from "node:path";
import * as readline from "node:readline";
import { fileURLToPath } from "node:url";
import { RpcClient } from "../src/modes/rpc/rpc-client.ts";
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Interactive example of using coding-agent via RpcClient.
* Usage: npx tsx test/rpc-example.ts
*/
async function main() {
const client = new RpcClient({
cliPath: join(__dirname, "../dist/cli.js"),
provider: "anthropic",
model: "claude-sonnet-4-20250514",
args: ["--no-session"],
});
// Stream events to console
client.onEvent((event) => {
if (event.type === "message_update") {
const { assistantMessageEvent } = event;
if (assistantMessageEvent.type === "text_delta" || assistantMessageEvent.type === "thinking_delta") {
process.stdout.write(assistantMessageEvent.delta);
}
}
if (event.type === "tool_execution_start") {
console.log(`\n[Tool: ${event.toolName}]`);
}
if (event.type === "tool_execution_end") {
console.log(`[Result: ${JSON.stringify(event.result).slice(0, 200)}...]\n`);
}
});
await client.start();
const state = await client.getState();
console.log(`Model: ${state.model?.provider}/${state.model?.id}`);
console.log(`Thinking: ${state.thinkingLevel ?? "off"}\n`);
// Handle user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
});
let isWaiting = false;
const prompt = () => {
if (!isWaiting) process.stdout.write("You: ");
};
rl.on("line", async (line) => {
if (isWaiting) return;
if (line.trim() === "exit") {
await client.stop();
process.exit(0);
}
isWaiting = true;
await client.promptAndWait(line);
console.log("\n");
isWaiting = false;
prompt();
});
rl.on("SIGINT", () => {
if (isWaiting) {
console.log("\n[Aborting...]");
client.abort();
} else {
client.stop();
process.exit(0);
}
});
console.log("Interactive RPC example. Type 'exit' to quit.\n");
prompt();
}
main().catch(console.error);