@townco/agent 0.1.19 → 0.1.21
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.
- package/dist/acp-server/adapter.js +77 -73
- package/dist/acp-server/http.js +412 -173
- package/dist/definition/index.d.ts +3 -0
- package/dist/definition/index.js +11 -1
- package/dist/dev-agent/index.d.ts +2 -0
- package/dist/dev-agent/index.js +18 -0
- package/dist/index.js +14 -14
- package/dist/runner/agent-runner.d.ts +20 -3
- package/dist/runner/agent-runner.js +4 -4
- package/dist/runner/langchain/index.d.ts +6 -5
- package/dist/runner/langchain/index.js +58 -17
- package/dist/runner/langchain/tools/filesystem.d.ts +66 -0
- package/dist/runner/langchain/tools/filesystem.js +261 -0
- package/dist/runner/tools.d.ts +5 -2
- package/dist/runner/tools.js +11 -1
- package/dist/templates/index.d.ts +3 -0
- package/dist/templates/index.js +11 -2
- package/dist/test-script.js +12 -12
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/logger.d.ts +39 -0
- package/dist/utils/logger.js +175 -0
- package/index.ts +7 -6
- package/package.json +10 -5
- package/templates/index.ts +14 -3
package/dist/acp-server/http.js
CHANGED
|
@@ -1,188 +1,427 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
+
import { gzipSync } from "node:zlib";
|
|
2
3
|
import * as acp from "@agentclientprotocol/sdk";
|
|
3
4
|
import { PGlite } from "@electric-sql/pglite";
|
|
4
5
|
import { Hono } from "hono";
|
|
5
6
|
import { cors } from "hono/cors";
|
|
6
7
|
import { streamSSE } from "hono/streaming";
|
|
7
8
|
import { makeRunnerFromDefinition } from "../runner";
|
|
9
|
+
import { createLogger } from "../utils/logger.js";
|
|
8
10
|
import { AgentAcpAdapter } from "./adapter";
|
|
9
|
-
|
|
11
|
+
const logger = createLogger("agent");
|
|
12
|
+
/**
|
|
13
|
+
* Compress a payload using gzip if it's too large for PostgreSQL NOTIFY
|
|
14
|
+
* Returns an object with the payload and metadata about compression
|
|
15
|
+
*/
|
|
16
|
+
function compressIfNeeded(rawMsg) {
|
|
17
|
+
const jsonStr = JSON.stringify(rawMsg);
|
|
18
|
+
const originalSize = jsonStr.length;
|
|
19
|
+
// If it fits without compression, send as-is
|
|
20
|
+
if (originalSize <= 7500) {
|
|
21
|
+
return {
|
|
22
|
+
payload: jsonStr,
|
|
23
|
+
isCompressed: false,
|
|
24
|
+
originalSize,
|
|
25
|
+
compressedSize: originalSize,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Compress and encode as base64
|
|
29
|
+
const compressed = gzipSync(jsonStr);
|
|
30
|
+
const base64 = compressed.toString("base64");
|
|
31
|
+
// Wrap in a compression envelope
|
|
32
|
+
const envelope = JSON.stringify({
|
|
33
|
+
_compressed: true,
|
|
34
|
+
data: base64,
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
payload: envelope,
|
|
38
|
+
isCompressed: true,
|
|
39
|
+
originalSize,
|
|
40
|
+
compressedSize: envelope.length,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
10
43
|
// Use PGlite in-memory database for LISTEN/NOTIFY
|
|
11
44
|
const pg = new PGlite();
|
|
12
45
|
// Helper to create safe channel names from untrusted IDs
|
|
13
46
|
function safeChannelName(prefix, id) {
|
|
14
|
-
|
|
15
|
-
|
|
47
|
+
const hash = createHash("sha256").update(id).digest("hex").slice(0, 16);
|
|
48
|
+
return `${prefix}_${hash}`;
|
|
16
49
|
}
|
|
17
50
|
export function makeHttpTransport(agent) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
51
|
+
const inbound = new TransformStream();
|
|
52
|
+
const outbound = new TransformStream();
|
|
53
|
+
const bridge = acp.ndJsonStream(outbound.writable, inbound.readable);
|
|
54
|
+
const agentRunner = "definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
55
|
+
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn), bridge);
|
|
56
|
+
const app = new Hono();
|
|
57
|
+
// Track active SSE streams by sessionId for direct output delivery
|
|
58
|
+
const sseStreams = new Map();
|
|
59
|
+
const decoder = new TextDecoder();
|
|
60
|
+
const encoder = new TextEncoder();
|
|
61
|
+
(async () => {
|
|
62
|
+
const reader = outbound.readable.getReader();
|
|
63
|
+
let buf = "";
|
|
64
|
+
for (;;) {
|
|
65
|
+
const { value, done } = await reader.read();
|
|
66
|
+
if (done)
|
|
67
|
+
break;
|
|
68
|
+
buf += decoder.decode(value, { stream: true });
|
|
69
|
+
for (let nl = buf.indexOf("\n"); nl !== -1; nl = buf.indexOf("\n")) {
|
|
70
|
+
const line = buf.slice(0, nl).trim();
|
|
71
|
+
buf = buf.slice(nl + 1);
|
|
72
|
+
if (!line)
|
|
73
|
+
continue;
|
|
74
|
+
let rawMsg;
|
|
75
|
+
try {
|
|
76
|
+
rawMsg = JSON.parse(line);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// ignore malformed lines
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// Validate the agent message with Zod schema
|
|
83
|
+
const parseResult = acp.agentOutgoingMessageSchema.safeParse(rawMsg);
|
|
84
|
+
if (!parseResult.success) {
|
|
85
|
+
logger.error("Invalid agent message", {
|
|
86
|
+
issues: parseResult.error.issues,
|
|
87
|
+
});
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
//const msg = parseResult.data;
|
|
91
|
+
if (rawMsg == null || typeof rawMsg !== "object") {
|
|
92
|
+
logger.warn("Malformed message, cannot route", { message: rawMsg });
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if ("id" in rawMsg &&
|
|
96
|
+
typeof rawMsg.id === "string" &&
|
|
97
|
+
rawMsg.id != null) {
|
|
98
|
+
// This is a response to a request - send to response-specific channel
|
|
99
|
+
const channel = safeChannelName("response", rawMsg.id);
|
|
100
|
+
const { payload, isCompressed, originalSize, compressedSize } = compressIfNeeded(rawMsg);
|
|
101
|
+
if (isCompressed) {
|
|
102
|
+
logger.info("Compressed response payload", {
|
|
103
|
+
requestId: rawMsg.id,
|
|
104
|
+
originalSize,
|
|
105
|
+
compressedSize,
|
|
106
|
+
compressionRatio: ((1 - compressedSize / originalSize) * 100).toFixed(1) + "%",
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// Escape single quotes for PostgreSQL
|
|
110
|
+
const escapedPayload = payload.replace(/'/g, "''");
|
|
111
|
+
// Check if even compressed payload is too large
|
|
112
|
+
if (compressedSize > 7500) {
|
|
113
|
+
logger.error("Response payload too large even after compression", {
|
|
114
|
+
requestId: rawMsg.id,
|
|
115
|
+
originalSize,
|
|
116
|
+
compressedSize,
|
|
117
|
+
});
|
|
118
|
+
// Send error response
|
|
119
|
+
const errorResponse = {
|
|
120
|
+
jsonrpc: "2.0",
|
|
121
|
+
id: rawMsg.id,
|
|
122
|
+
error: {
|
|
123
|
+
code: -32603,
|
|
124
|
+
message: "Response payload too large even after compression",
|
|
125
|
+
data: {
|
|
126
|
+
originalSize,
|
|
127
|
+
compressedSize,
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
const errorPayload = JSON.stringify(errorResponse).replace(/'/g, "''");
|
|
132
|
+
await pg.query(`NOTIFY ${channel}, '${errorPayload}'`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
await pg.query(`NOTIFY ${channel}, '${escapedPayload}'`);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
logger.error("Failed to send response", {
|
|
140
|
+
error,
|
|
141
|
+
requestId: rawMsg.id,
|
|
142
|
+
originalSize,
|
|
143
|
+
compressedSize,
|
|
144
|
+
});
|
|
145
|
+
// For responses, we still need to send something to unblock the client
|
|
146
|
+
const errorResponse = {
|
|
147
|
+
jsonrpc: "2.0",
|
|
148
|
+
id: rawMsg.id,
|
|
149
|
+
error: {
|
|
150
|
+
code: -32603,
|
|
151
|
+
message: "Failed to send response",
|
|
152
|
+
data: {
|
|
153
|
+
originalSize,
|
|
154
|
+
compressedSize,
|
|
155
|
+
error: error instanceof Error ? error.message : String(error),
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
const errorPayload = JSON.stringify(errorResponse).replace(/'/g, "''");
|
|
160
|
+
await pg.query(`NOTIFY ${channel}, '${errorPayload}'`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if ("params" in rawMsg &&
|
|
164
|
+
rawMsg.params != null &&
|
|
165
|
+
typeof rawMsg.params === "object" &&
|
|
166
|
+
"sessionId" in rawMsg.params &&
|
|
167
|
+
typeof rawMsg.params.sessionId === "string") {
|
|
168
|
+
const sessionId = rawMsg.params.sessionId;
|
|
169
|
+
const messageType = "method" in rawMsg && typeof rawMsg.method === "string"
|
|
170
|
+
? rawMsg.method
|
|
171
|
+
: undefined;
|
|
172
|
+
// Check if this is a tool_output update - send directly via SSE
|
|
173
|
+
if (messageType === "session/update" &&
|
|
174
|
+
"params" in rawMsg &&
|
|
175
|
+
rawMsg.params != null &&
|
|
176
|
+
typeof rawMsg.params === "object" &&
|
|
177
|
+
"update" in rawMsg.params &&
|
|
178
|
+
rawMsg.params.update != null &&
|
|
179
|
+
typeof rawMsg.params.update === "object" &&
|
|
180
|
+
"sessionUpdate" in rawMsg.params.update &&
|
|
181
|
+
rawMsg.params.update.sessionUpdate === "tool_output") {
|
|
182
|
+
// Send tool output directly via SSE, bypassing PostgreSQL NOTIFY
|
|
183
|
+
const stream = sseStreams.get(sessionId);
|
|
184
|
+
if (stream) {
|
|
185
|
+
try {
|
|
186
|
+
await stream.writeSSE({
|
|
187
|
+
event: "message",
|
|
188
|
+
data: JSON.stringify(rawMsg),
|
|
189
|
+
});
|
|
190
|
+
logger.debug("Sent tool output", {
|
|
191
|
+
sessionId,
|
|
192
|
+
payloadSize: JSON.stringify(rawMsg).length,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
logger.error("Failed to send tool output", {
|
|
197
|
+
error,
|
|
198
|
+
sessionId,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
logger.warn("No SSE stream found for tool output", { sessionId });
|
|
204
|
+
}
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
// Other messages (notifications, requests from agent) go to
|
|
208
|
+
// session-specific channel via PostgreSQL NOTIFY
|
|
209
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
210
|
+
const { payload, isCompressed, originalSize, compressedSize } = compressIfNeeded(rawMsg);
|
|
211
|
+
if (isCompressed) {
|
|
212
|
+
logger.info("Compressed notification payload", {
|
|
213
|
+
sessionId,
|
|
214
|
+
messageType,
|
|
215
|
+
originalSize,
|
|
216
|
+
compressedSize,
|
|
217
|
+
compressionRatio: ((1 - compressedSize / originalSize) * 100).toFixed(1) + "%",
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
// Escape single quotes for PostgreSQL
|
|
221
|
+
const escapedPayload = payload.replace(/'/g, "''");
|
|
222
|
+
// Check if even compressed payload is too large
|
|
223
|
+
if (compressedSize > 7500) {
|
|
224
|
+
logger.error("Notification payload too large even after compression, skipping", {
|
|
225
|
+
sessionId,
|
|
226
|
+
messageType,
|
|
227
|
+
originalSize,
|
|
228
|
+
compressedSize,
|
|
229
|
+
});
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
await pg.query(`NOTIFY ${channel}, '${escapedPayload}'`);
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
logger.error("Failed to send notification", {
|
|
237
|
+
error,
|
|
238
|
+
sessionId,
|
|
239
|
+
messageType,
|
|
240
|
+
originalSize,
|
|
241
|
+
compressedSize,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
logger.warn("Message without sessionId, cannot route", {
|
|
247
|
+
message: rawMsg,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
})();
|
|
253
|
+
// TODO: fix this for production
|
|
254
|
+
app.use("*", cors({
|
|
255
|
+
origin: "*",
|
|
256
|
+
allowHeaders: ["content-type", "authorization", "x-session-id"],
|
|
257
|
+
allowMethods: ["GET", "POST", "OPTIONS"],
|
|
258
|
+
}));
|
|
259
|
+
app.get("/health", (c) => c.json({ ok: true }));
|
|
260
|
+
app.get("/events", (c) => {
|
|
261
|
+
const sessionId = c.req.header("X-Session-ID");
|
|
262
|
+
if (!sessionId) {
|
|
263
|
+
logger.warn("GET /events - Missing X-Session-ID header");
|
|
264
|
+
return c.json({ error: "X-Session-ID header required" }, 401);
|
|
265
|
+
}
|
|
266
|
+
logger.debug("GET /events - SSE connection opened", { sessionId });
|
|
267
|
+
return streamSSE(c, async (stream) => {
|
|
268
|
+
// Register this stream for direct tool output delivery
|
|
269
|
+
sseStreams.set(sessionId, stream);
|
|
270
|
+
await stream.writeSSE({ event: "ping", data: "{}" });
|
|
271
|
+
const hb = setInterval(() => {
|
|
272
|
+
// Heartbeat to keep proxies from terminating idle connections
|
|
273
|
+
void stream.writeSSE({ event: "ping", data: "{}" });
|
|
274
|
+
}, 1000);
|
|
275
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
276
|
+
const unsub = await pg.listen(channel, async (payload) => {
|
|
277
|
+
let json = JSON.parse(payload);
|
|
278
|
+
// Check if the message is compressed
|
|
279
|
+
if (json &&
|
|
280
|
+
typeof json === "object" &&
|
|
281
|
+
"_compressed" in json &&
|
|
282
|
+
json._compressed === true &&
|
|
283
|
+
"data" in json &&
|
|
284
|
+
typeof json.data === "string") {
|
|
285
|
+
// This is a compressed message - decompress it
|
|
286
|
+
try {
|
|
287
|
+
const { gunzipSync } = await import("node:zlib");
|
|
288
|
+
const compressed = Buffer.from(json.data, "base64");
|
|
289
|
+
const decompressed = gunzipSync(compressed);
|
|
290
|
+
json = JSON.parse(decompressed.toString());
|
|
291
|
+
logger.trace("Decompressed SSE message", { sessionId, channel });
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
logger.error("Failed to decompress message", {
|
|
295
|
+
error,
|
|
296
|
+
sessionId,
|
|
297
|
+
channel,
|
|
298
|
+
});
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
logger.trace("Sending SSE message", { sessionId, channel });
|
|
303
|
+
await stream.writeSSE({
|
|
304
|
+
event: "message",
|
|
305
|
+
data: JSON.stringify(json),
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
// Clean up when the client disconnects
|
|
309
|
+
stream.onAbort(() => {
|
|
310
|
+
logger.debug("GET /events - SSE connection closed", { sessionId });
|
|
311
|
+
clearInterval(hb);
|
|
312
|
+
unsub();
|
|
313
|
+
sseStreams.delete(sessionId);
|
|
314
|
+
});
|
|
315
|
+
// Keep the connection open indefinitely
|
|
316
|
+
await stream.sleep(1000 * 60 * 60 * 24);
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
app.post("/rpc", async (c) => {
|
|
320
|
+
// Get and validate the request body
|
|
321
|
+
const rawBody = await c.req.json();
|
|
322
|
+
// Validate using Zod schema
|
|
323
|
+
const parseResult = acp.clientOutgoingMessageSchema.safeParse(rawBody);
|
|
324
|
+
if (!parseResult.success) {
|
|
325
|
+
logger.warn("POST /rpc - Invalid ACP message", {
|
|
326
|
+
issues: parseResult.error.issues,
|
|
327
|
+
});
|
|
328
|
+
return c.json({
|
|
329
|
+
error: "Invalid ACP message",
|
|
330
|
+
details: parseResult.error.issues,
|
|
331
|
+
}, 400);
|
|
332
|
+
}
|
|
333
|
+
const body = parseResult.data;
|
|
334
|
+
// Ensure the request has an id
|
|
335
|
+
const id = "id" in body ? body.id : null;
|
|
336
|
+
const isRequest = id != null;
|
|
337
|
+
const method = "method" in body ? body.method : "notification";
|
|
338
|
+
logger.debug("POST /rpc - Received request", { method, id, isRequest });
|
|
339
|
+
if (isRequest) {
|
|
340
|
+
// For requests, set up a listener before sending the request
|
|
341
|
+
const responseChannel = safeChannelName("response", String(id));
|
|
342
|
+
let responseResolver;
|
|
343
|
+
const responsePromise = new Promise((resolve) => {
|
|
344
|
+
responseResolver = resolve;
|
|
345
|
+
});
|
|
346
|
+
const unsub = await pg.listen(responseChannel, async (payload) => {
|
|
347
|
+
let rawResponse = JSON.parse(payload);
|
|
348
|
+
// Check if the response is compressed
|
|
349
|
+
if (rawResponse &&
|
|
350
|
+
typeof rawResponse === "object" &&
|
|
351
|
+
"_compressed" in rawResponse &&
|
|
352
|
+
rawResponse._compressed === true &&
|
|
353
|
+
"data" in rawResponse &&
|
|
354
|
+
typeof rawResponse.data === "string") {
|
|
355
|
+
// This is a compressed response - decompress it
|
|
356
|
+
try {
|
|
357
|
+
const { gunzipSync } = await import("node:zlib");
|
|
358
|
+
const compressed = Buffer.from(rawResponse.data, "base64");
|
|
359
|
+
const decompressed = gunzipSync(compressed);
|
|
360
|
+
rawResponse = JSON.parse(decompressed.toString());
|
|
361
|
+
logger.trace("Decompressed RPC response", { id });
|
|
362
|
+
}
|
|
363
|
+
catch (error) {
|
|
364
|
+
logger.error("Failed to decompress response", {
|
|
365
|
+
error,
|
|
366
|
+
requestId: id,
|
|
367
|
+
});
|
|
368
|
+
rawResponse = {
|
|
369
|
+
jsonrpc: "2.0",
|
|
370
|
+
id,
|
|
371
|
+
error: {
|
|
372
|
+
code: -32603,
|
|
373
|
+
message: "Failed to decompress response",
|
|
374
|
+
data: {
|
|
375
|
+
error: error instanceof Error ? error.message : String(error),
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
responseResolver(rawResponse);
|
|
382
|
+
});
|
|
383
|
+
// Write NDJSON line into the ACP inbound stream
|
|
384
|
+
const writer = inbound.writable.getWriter();
|
|
385
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
386
|
+
writer.releaseLock();
|
|
387
|
+
// Wait for response with 30 second timeout
|
|
388
|
+
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Request timeout")), 30000));
|
|
389
|
+
try {
|
|
390
|
+
const response = await Promise.race([responsePromise, timeoutPromise]);
|
|
391
|
+
logger.debug("POST /rpc - Response received", { id });
|
|
392
|
+
return c.json(response);
|
|
393
|
+
}
|
|
394
|
+
catch (error) {
|
|
395
|
+
logger.error("POST /rpc - Request timeout", {
|
|
396
|
+
id,
|
|
397
|
+
error: error instanceof Error ? error.message : String(error),
|
|
398
|
+
});
|
|
399
|
+
throw error;
|
|
400
|
+
}
|
|
401
|
+
finally {
|
|
402
|
+
// Clean up the listener whether we got a response or timed out
|
|
403
|
+
unsub();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
// For notifications, just send and return success
|
|
408
|
+
const writer = inbound.writable.getWriter();
|
|
409
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
410
|
+
writer.releaseLock();
|
|
411
|
+
return c.json({
|
|
412
|
+
success: true,
|
|
413
|
+
message: "Notification sent to agent",
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
const port = Number.parseInt(process.env.PORT || "3100", 10);
|
|
418
|
+
logger.info("Starting HTTP server", { port });
|
|
419
|
+
Bun.serve({
|
|
420
|
+
fetch: app.fetch,
|
|
421
|
+
port,
|
|
422
|
+
});
|
|
423
|
+
logger.info("HTTP server listening", {
|
|
424
|
+
url: `http://localhost:${port}`,
|
|
425
|
+
port,
|
|
426
|
+
});
|
|
188
427
|
}
|
|
@@ -19,6 +19,9 @@ export declare const AgentDefinitionSchema: z.ZodObject<{
|
|
|
19
19
|
tools: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
20
20
|
type: z.ZodLiteral<"custom">;
|
|
21
21
|
modulePath: z.ZodString;
|
|
22
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
23
|
+
type: z.ZodLiteral<"filesystem">;
|
|
24
|
+
working_directory: z.ZodOptional<z.ZodString>;
|
|
22
25
|
}, z.core.$strip>]>>>;
|
|
23
26
|
mcps: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
24
27
|
name: z.ZodString;
|
package/dist/definition/index.js
CHANGED
|
@@ -29,8 +29,18 @@ const CustomToolSchema = z.object({
|
|
|
29
29
|
type: z.literal("custom"),
|
|
30
30
|
modulePath: z.string(),
|
|
31
31
|
});
|
|
32
|
+
/** Filesystem tool configuration schema. */
|
|
33
|
+
const FilesystemToolSchema = z.object({
|
|
34
|
+
type: z.literal("filesystem"),
|
|
35
|
+
/** If omitted, defaults to process.cwd() at runtime */
|
|
36
|
+
working_directory: z.string().optional(),
|
|
37
|
+
});
|
|
32
38
|
/** Tool schema - can be a string (built-in tool) or custom tool object. */
|
|
33
|
-
const ToolSchema = z.union([
|
|
39
|
+
const ToolSchema = z.union([
|
|
40
|
+
z.string(),
|
|
41
|
+
CustomToolSchema,
|
|
42
|
+
FilesystemToolSchema,
|
|
43
|
+
]);
|
|
34
44
|
/** Agent definition schema. */
|
|
35
45
|
export const AgentDefinitionSchema = z.object({
|
|
36
46
|
systemPrompt: z.string().nullable(),
|