mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 08:03:39 +00:00
Update RPC example to use RpcClient
This commit is contained in:
parent
3c8ab0280e
commit
70b84532bb
1 changed files with 64 additions and 63 deletions
|
|
@ -1,30 +1,25 @@
|
|||
import { spawn } from "node:child_process";
|
||||
import { dirname, join } from "path";
|
||||
import * as readline from "readline";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
/**
|
||||
* Interactive example of using coding-agent in RPC mode
|
||||
* Usage: npx tsx test/rpc-example.ts
|
||||
*/
|
||||
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.js";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// Spawn agent in RPC mode
|
||||
const agent = spawn("node", ["dist/cli.js", "--mode", "rpc", "--no-session"], {
|
||||
cwd: join(__dirname, ".."),
|
||||
env: process.env,
|
||||
});
|
||||
/**
|
||||
* Interactive example of using coding-agent via RpcClient.
|
||||
* Usage: npx tsx test/rpc-example.ts
|
||||
*/
|
||||
|
||||
let isWaiting = false;
|
||||
|
||||
// Parse agent events
|
||||
readline.createInterface({ input: agent.stdout, terminal: false }).on("line", (line: string) => {
|
||||
try {
|
||||
const event = JSON.parse(line);
|
||||
|
||||
if (event.type === "agent_start") isWaiting = true;
|
||||
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") {
|
||||
|
|
@ -37,49 +32,55 @@ readline.createInterface({ input: agent.stdout, terminal: false }).on("line", (l
|
|||
}
|
||||
|
||||
if (event.type === "tool_execution_end") {
|
||||
console.log(`[Result: ${JSON.stringify(event.result, null, 2)}]\n`);
|
||||
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);
|
||||
}
|
||||
|
||||
if (event.type === "agent_end") {
|
||||
console.log("\n");
|
||||
isWaiting = false;
|
||||
process.stdout.write("You: ");
|
||||
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);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Parse error:", line);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Handle user input
|
||||
const stdinReader = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: true,
|
||||
});
|
||||
console.log("Interactive RPC example. Type 'exit' to quit.\n");
|
||||
prompt();
|
||||
}
|
||||
|
||||
stdinReader.on("line", (line: string) => {
|
||||
if (isWaiting) return;
|
||||
isWaiting = true;
|
||||
agent.stdin.write(JSON.stringify({ type: "prompt", message: line }) + "\n");
|
||||
});
|
||||
|
||||
// Capture readline's SIGINT and handle it ourselves
|
||||
stdinReader.on("SIGINT", () => {
|
||||
process.emit("SIGINT", "SIGINT");
|
||||
});
|
||||
|
||||
// Handle Ctrl+C
|
||||
process.on("SIGINT", () => {
|
||||
if (isWaiting) {
|
||||
console.log("\n[Aborting...]");
|
||||
agent.stdin.write(JSON.stringify({ type: "abort" }) + "\n");
|
||||
} else {
|
||||
agent.kill();
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
agent.stderr.on("data", (data) => console.error("Error:", data.toString()));
|
||||
|
||||
console.log("Interactive RPC mode example. Type 'exit' to quit.\n");
|
||||
process.stdout.write("You: ");
|
||||
main().catch(console.error);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue