mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 04:02:21 +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 "node:path";
|
||||||
import { dirname, join } from "path";
|
import * as readline from "node:readline";
|
||||||
import * as readline from "readline";
|
import { fileURLToPath } from "node:url";
|
||||||
import { fileURLToPath } from "url";
|
import { RpcClient } from "../src/modes/rpc/rpc-client.js";
|
||||||
|
|
||||||
/**
|
|
||||||
* Interactive example of using coding-agent in RPC mode
|
|
||||||
* Usage: npx tsx test/rpc-example.ts
|
|
||||||
*/
|
|
||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
// Spawn agent in RPC mode
|
/**
|
||||||
const agent = spawn("node", ["dist/cli.js", "--mode", "rpc", "--no-session"], {
|
* Interactive example of using coding-agent via RpcClient.
|
||||||
cwd: join(__dirname, ".."),
|
* Usage: npx tsx test/rpc-example.ts
|
||||||
env: process.env,
|
*/
|
||||||
});
|
|
||||||
|
|
||||||
let isWaiting = false;
|
async function main() {
|
||||||
|
const client = new RpcClient({
|
||||||
// Parse agent events
|
cliPath: join(__dirname, "../dist/cli.js"),
|
||||||
readline.createInterface({ input: agent.stdout, terminal: false }).on("line", (line: string) => {
|
provider: "anthropic",
|
||||||
try {
|
model: "claude-sonnet-4-20250514",
|
||||||
const event = JSON.parse(line);
|
args: ["--no-session"],
|
||||||
|
});
|
||||||
if (event.type === "agent_start") isWaiting = true;
|
|
||||||
|
|
||||||
|
// Stream events to console
|
||||||
|
client.onEvent((event) => {
|
||||||
if (event.type === "message_update") {
|
if (event.type === "message_update") {
|
||||||
const { assistantMessageEvent } = event;
|
const { assistantMessageEvent } = event;
|
||||||
if (assistantMessageEvent.type === "text_delta" || assistantMessageEvent.type === "thinking_delta") {
|
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") {
|
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") {
|
isWaiting = true;
|
||||||
console.log("\n");
|
await client.promptAndWait(line);
|
||||||
isWaiting = false;
|
console.log("\n");
|
||||||
process.stdout.write("You: ");
|
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
|
console.log("Interactive RPC example. Type 'exit' to quit.\n");
|
||||||
const stdinReader = readline.createInterface({
|
prompt();
|
||||||
input: process.stdin,
|
}
|
||||||
output: process.stdout,
|
|
||||||
terminal: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
stdinReader.on("line", (line: string) => {
|
main().catch(console.error);
|
||||||
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: ");
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue