import assert from "node:assert/strict"; import { PassThrough } from "node:stream"; import { setTimeout } from "node:timers/promises"; import { endpoint } from "./fake-provider.mjs"; import { fork } from "node:child_process"; export const profileId = "35000000-0000-4000-8000-000000000001"; export const hostSessionId = "35000000-0000-4000-8000-000000000002"; export const config = { model: "gpt-4.1-mini", endpoint }; export async function wire({ fetch, storageRoot, projectRoot, childText }) { const { runAdapter } = await import("../../src/adapter.mjs"); assert.equal(typeof runAdapter, "function"); const child = childText === undefined ? undefined : fork(new URL("./adapter-child.mjs", import.meta.url), [storageRoot, childText], { stdio: ["pipe", "pipe", "pipe", "ipc"] }); const input = child?.stdin ?? new PassThrough(); const output = child?.stdout ?? new PassThrough(); const providerCalls = []; child?.on("message", (message) => { if (message.kind === "providerCall") providerCalls.push(message.call); }); const frames = []; let buffer = ""; let failure; let id = 0; output.setEncoding("utf8"); output.on("data", (chunk) => { buffer += chunk; while (buffer.includes("\n")) { const end = buffer.indexOf("\n"); const line = buffer.slice(0, end); buffer = buffer.slice(end + 1); try { frames.push(JSON.parse(line)); } catch { failure = new Error("adapter emitted non-JSON stdout"); } } }); const done = child ? new Promise((resolve) => { child.on("error", (error) => { failure = error; resolve(); }); child.on("exit", (code) => { if (code !== 0) failure = new Error(`child adapter exit ${code}`); resolve(); }); }) : runAdapter({ input, output, fetch, storageRoot }).catch((error) => { failure = error; }); const write = (value) => input.write(`${JSON.stringify(value)}\n`); const wait = async (predicate) => { const expires = Date.now() + 8_000; while (Date.now() < expires) { if (failure) throw failure; const found = frames.find(predicate); if (found) return found; const error = frames.find((frame) => frame.type === "event" && frame.name === "error"); if (error) throw new Error(`adapter error: ${JSON.stringify(error.data)}`); await setTimeout(2); } throw new Error("adapter did not produce the expected public protocol frame"); }; const request = async (method, params) => { const requestId = `qa-${++id}`; write({ v: 1, type: "request", id: requestId, method, params }); return await wait((frame) => frame.type === "response" && frame.id === requestId); }; return { frames, done, write, raw: (chunk) => input.write(chunk), wait, request, providerCalls, async initialize(version = 1) { return await request("initialize", { protocolVersion: version, adapterRef: { pluginId: "idea.cline-demo", adapterId: "cline-sdk", pluginVersion: "0.1.0" }, profileRef: { profileId, presetId: "cline-openai" } }); }, async open(sessionPlan = { kind: "none" }, tools = [], secrets = {}) { return await request("open", { sessionId: hostSessionId, sessionPlan, config, secrets, context: { content: "# Host context", relativePath: "AGENTS.md", projectRoot }, tools }); }, async close(sessionId = hostSessionId) { const reply = await request("close", { sessionId }); input.end(); await Promise.race([done, setTimeout(5_000, undefined, { ref: false }).then(() => { throw new Error("close did not await SDK disposal"); })]); if (failure) throw failure; return reply; }, async cleanup() { input.end(); await Promise.race([done, setTimeout(5_000, undefined, { ref: false })]); }, }; }