-
-
Save r1bilski/a3cb7c16868169b22dd6e787f53be067 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "opencode--repro", | |
| "version": "1.0.0", | |
| "type": "module", | |
| "dependencies": { | |
| "@agentclientprotocol/sdk": "^0.5.1" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| import { spawn } from "node:child_process"; | |
| import { Writable, Readable } from "node:stream"; | |
| import * as acp from "@agentclientprotocol/sdk"; | |
| function logMessage(direction, msg) { | |
| console.log(`\n[${new Date().toISOString()}] ${direction}:`); | |
| console.log(JSON.stringify(msg, null, 2)); | |
| } | |
| function createLoggingStream(baseStream) { | |
| return { | |
| writable: new WritableStream({ | |
| async write(msg) { | |
| logMessage("SEND", msg); | |
| const writer = baseStream.writable.getWriter(); | |
| await writer.write(msg); | |
| writer.releaseLock(); | |
| }, | |
| }), | |
| readable: new ReadableStream({ | |
| async start(controller) { | |
| const reader = baseStream.readable.getReader(); | |
| try { | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) { | |
| controller.close(); | |
| break; | |
| } | |
| logMessage("RECV", value); | |
| controller.enqueue(value); | |
| } | |
| } catch (e) { | |
| controller.error(e); | |
| } | |
| }, | |
| }), | |
| }; | |
| } | |
| const client = { | |
| async requestPermission(params) { | |
| const opt = | |
| params.options.find((o) => o.kind === "allow_once") || params.options[0]; | |
| return { outcome: { outcome: "selected", optionId: opt.optionId } }; | |
| }, | |
| async sessionUpdate(params) { | |
| const u = params.update; | |
| if ( | |
| u.sessionUpdate === "agent_message_chunk" && | |
| u.content.type === "text" | |
| ) { | |
| process.stdout.write(u.content.text); | |
| } | |
| }, | |
| }; | |
| function connect(proc) { | |
| const stream = acp.ndJsonStream( | |
| Writable.toWeb(proc.stdin), | |
| Readable.toWeb(proc.stdout), | |
| ); | |
| return new acp.ClientSideConnection( | |
| () => client, | |
| createLoggingStream(stream), | |
| ); | |
| } | |
| async function main() { | |
| // Step 1: Create session and send a message | |
| console.log("--- STEP 1: Create session, send initial message ---\n"); | |
| const proc = spawn("opencode", ["acp"], { | |
| stdio: ["pipe", "pipe", "inherit"], | |
| }); | |
| const conn = connect(proc); | |
| await conn.initialize({ | |
| protocolVersion: acp.PROTOCOL_VERSION, | |
| clientCapabilities: {}, | |
| }); | |
| const { sessionId } = await conn.newSession({ | |
| cwd: process.cwd(), | |
| mcpServers: [], | |
| }); | |
| console.log(`\n[Main] Session: ${sessionId}\n`); | |
| await conn.prompt({ | |
| sessionId, | |
| prompt: [{ type: "text", text: "Say hello in exactly 5 words." }], | |
| }); | |
| console.log("\n\n--- STEP 2: Send /compact command ---\n"); | |
| await conn.prompt({ | |
| sessionId, | |
| prompt: [{ type: "text", text: "/compact" }], | |
| }); | |
| console.log("\n\n[Main] /compact command completed."); | |
| proc.kill(); | |
| process.exit(0); | |
| } | |
| main().catch((e) => { | |
| console.error(e); | |
| process.exit(1); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment