@eum-agent/integration-acp 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  import { accessSync, constants, statSync } from "node:fs";
2
2
  import { isAbsolute } from "node:path";
3
3
  //#region src/codex-adapter.ts
4
- /** Loads the bundled ACP bridge only after an explicit executable user CLI is present. */
5
4
  const cli = process.env.CODEX_PATH;
6
5
  try {
7
6
  if (!cli || !isAbsolute(cli) || !statSync(cli).isFile()) throw new Error();
@@ -0,0 +1,166 @@
1
+ import { t as control } from "./control-_lHBoljq.js";
2
+ import { z } from "zod";
3
+ import { Readable, Transform, Writable } from "node:stream";
4
+ import { setImmediate } from "node:timers/promises";
5
+ import { ClientSideConnection, PROTOCOL_VERSION, ndJsonStream } from "@agentclientprotocol/sdk";
6
+ import protocolSchema from "@agentclientprotocol/sdk/schema/schema.json" with { type: "json" };
7
+ //#region src/client.ts
8
+ const Update = z.fromJSONSchema({
9
+ $ref: "#/$defs/SessionNotification",
10
+ $defs: protocolSchema.$defs
11
+ });
12
+ const Permission = z.fromJSONSchema({
13
+ $ref: "#/$defs/RequestPermissionRequest",
14
+ $defs: protocolSchema.$defs
15
+ });
16
+ async function connectAcp(process, callbacks) {
17
+ const messageLimit = 8388608;
18
+ const fragments = [];
19
+ let bufferedBytes = 0;
20
+ let tail;
21
+ let tailBytes = 0;
22
+ const append = (chunk) => {
23
+ let offset = 0;
24
+ while (offset < chunk.length) {
25
+ if (!tail || tailBytes === tail.length) {
26
+ tail = Buffer.allocUnsafe(Math.min(65536, messageLimit - bufferedBytes));
27
+ tailBytes = 0;
28
+ fragments.push(tail);
29
+ }
30
+ const length = Math.min(chunk.length - offset, tail.length - tailBytes);
31
+ chunk.copy(tail, tailBytes, offset, offset + length);
32
+ tailBytes += length;
33
+ bufferedBytes += length;
34
+ offset += length;
35
+ }
36
+ };
37
+ const pending = /* @__PURE__ */ new Set();
38
+ const bounded = new Transform({
39
+ transform(chunk, _encoding, done) {
40
+ (async () => {
41
+ let offset = 0;
42
+ let frames = 0;
43
+ let processedBytes = 0;
44
+ while (offset < chunk.length) {
45
+ if (frames >= 16 || processedBytes >= 65536) {
46
+ await setImmediate();
47
+ if (this.destroyed) {
48
+ done(/* @__PURE__ */ new Error("connection-closed"));
49
+ return;
50
+ }
51
+ frames = 0;
52
+ processedBytes = 0;
53
+ }
54
+ const newline = chunk.indexOf(10, offset);
55
+ const end = newline === -1 ? chunk.length : newline;
56
+ if (bufferedBytes + end - offset > messageLimit) {
57
+ done(/* @__PURE__ */ new Error("protocol-message-limit"));
58
+ return;
59
+ }
60
+ append(chunk.subarray(offset, end));
61
+ if (newline === -1) break;
62
+ if (tail) fragments[fragments.length - 1] = tail.subarray(0, tailBytes);
63
+ const framed = Buffer.concat([...fragments, Buffer.from("\n")], bufferedBytes + 1);
64
+ const line = framed.subarray(0, bufferedBytes);
65
+ frames += 1;
66
+ processedBytes += bufferedBytes;
67
+ fragments.length = 0;
68
+ bufferedBytes = 0;
69
+ tail = void 0;
70
+ tailBytes = 0;
71
+ offset = newline + 1;
72
+ if (!line.length) continue;
73
+ let value;
74
+ try {
75
+ const parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(line));
76
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error();
77
+ value = parsed;
78
+ if (value.jsonrpc !== "2.0") throw new Error();
79
+ if ("method" in value) {
80
+ if (typeof value.method !== "string") throw new Error();
81
+ if (value.method === "session/update" && !Update.safeParse(value.params).success) throw new Error();
82
+ if (value.method === "session/request_permission" && !Permission.safeParse(value.params).success) throw new Error();
83
+ if (!("id" in value) && value.method !== "session/update") continue;
84
+ } else {
85
+ if (typeof value.id !== "string" && typeof value.id !== "number") throw new Error();
86
+ if (!pending.delete(value.id)) continue;
87
+ if (!("result" in value) && !("error" in value)) throw new Error();
88
+ }
89
+ } catch {
90
+ done(/* @__PURE__ */ new Error("invalid-protocol-message"));
91
+ return;
92
+ }
93
+ this.push(framed);
94
+ }
95
+ done();
96
+ })().catch(done);
97
+ },
98
+ flush(done) {
99
+ done(bufferedBytes ? /* @__PURE__ */ new Error("truncated-protocol-message") : void 0);
100
+ }
101
+ });
102
+ process.stdout.pipe(bounded);
103
+ process.stdout.on("close", () => {
104
+ if (!process.stdout.readableEnded) bounded.destroy();
105
+ });
106
+ process.stdout.on("error", () => bounded.destroy(/* @__PURE__ */ new Error("connection-lost")));
107
+ bounded.on("error", () => {
108
+ process.stop().catch(() => {});
109
+ });
110
+ const raw = ndJsonStream(Writable.toWeb(process.stdin), Readable.toWeb(bounded));
111
+ const writer = raw.writable.getWriter();
112
+ const client = new ClientSideConnection(() => ({
113
+ async requestPermission(request) {
114
+ try {
115
+ return await callbacks.permission(request);
116
+ } catch {
117
+ process.stop().catch(() => {});
118
+ return { outcome: { outcome: "cancelled" } };
119
+ }
120
+ },
121
+ sessionUpdate(notification) {
122
+ try {
123
+ callbacks.update(notification);
124
+ } catch {
125
+ process.stop().catch(() => {});
126
+ }
127
+ }
128
+ }), {
129
+ readable: raw.readable,
130
+ writable: new WritableStream({
131
+ async write(value) {
132
+ if (Buffer.byteLength(JSON.stringify(value)) > 8388608) throw new Error("protocol-message-limit");
133
+ if ("method" in value && "id" in value && value.id !== null) pending.add(value.id);
134
+ try {
135
+ await control(writer.write(value));
136
+ } catch {
137
+ process.stop().catch(() => {});
138
+ throw new Error("protocol-write-failed");
139
+ }
140
+ },
141
+ close: () => writer.close(),
142
+ abort: () => writer.abort(/* @__PURE__ */ new Error("connection-closed"))
143
+ })
144
+ });
145
+ try {
146
+ const initialized = await control(client.initialize({
147
+ protocolVersion: PROTOCOL_VERSION,
148
+ clientCapabilities: {},
149
+ clientInfo: {
150
+ name: "yaoguang",
151
+ version: "1.0.0"
152
+ }
153
+ }));
154
+ if (initialized.protocolVersion !== PROTOCOL_VERSION) throw new Error("protocol-version-mismatch");
155
+ return {
156
+ client,
157
+ initialized,
158
+ process
159
+ };
160
+ } catch {
161
+ await process.stop();
162
+ throw new Error("initialize-failed");
163
+ }
164
+ }
165
+ //#endregion
166
+ export { connectAcp };
@@ -0,0 +1,13 @@
1
+ //#region src/control.ts
2
+ async function control(promise, ms = 3e4) {
3
+ let timer;
4
+ try {
5
+ return await Promise.race([promise, new Promise((_, reject) => {
6
+ timer = setTimeout(() => reject(/* @__PURE__ */ new Error("protocol-timeout")), ms);
7
+ })]);
8
+ } finally {
9
+ if (timer) clearTimeout(timer);
10
+ }
11
+ }
12
+ //#endregion
13
+ export { control as t };