UNPKG

histoire

Version:

A fast story book powered by Vite

26 lines (25 loc) • 1.86 kB
import { Readable, Writable } from 'node:stream'; import { client, methods, ndJsonStream, PROTOCOL_VERSION } from '@agentclientprotocol/sdk'; /** Connects official ACP SDK v1 with only supported, client-owned callbacks. */ export function connectAgentClient(process, handlers) { // The SDK walks handlers in order with one await each; session updates come first so // reply chunks are routed before the prompt response that follows them settles. const connection = client({ name: 'histoire' }) .onNotification(methods.client.session.update, context => handlers.update(context.params)) .onRequest(methods.client.session.requestPermission, context => handlers.permission(context.params, context.signal)) .onRequest(methods.client.elicitation.create, context => handlers.elicitation(context.params, context.signal)) .onNotification(methods.client.elicitation.complete, context => handlers.elicitationComplete(context.params)) .connect(ndJsonStream(Writable.toWeb(process.child.stdin), Readable.toWeb(process.child.stdout), { maxMessageBytes: 1024 * 1024 })); /** Initial negotiation never advertises source writing or terminal hosting; forms and links render in threads. */ async function initialize() { const result = await connection.agent.request(methods.agent.initialize, { protocolVersion: PROTOCOL_VERSION, clientInfo: { name: 'histoire', version: '1.0.0' }, clientCapabilities: { fs: { readTextFile: false, writeTextFile: false }, terminal: false, elicitation: { form: {}, url: {} } }, }); if (result.protocolVersion !== PROTOCOL_VERSION) throw new Error(`ACP protocol version mismatch: expected ${PROTOCOL_VERSION}, received ${result.protocolVersion}`); return result; } return { connection, initialize }; }