agentxjs 0.0.1
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/LICENSE +21 -0
- package/README.md +602 -0
- package/dist/index.cjs +1292 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +88 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +1283 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/sse/index.cjs +633 -0
- package/dist/runtime/sse/index.cjs.map +1 -0
- package/dist/runtime/sse/index.d.cts +159 -0
- package/dist/runtime/sse/index.d.ts +159 -0
- package/dist/runtime/sse/index.js +621 -0
- package/dist/runtime/sse/index.js.map +1 -0
- package/dist/server/adapters/express.cjs +81 -0
- package/dist/server/adapters/express.cjs.map +1 -0
- package/dist/server/adapters/express.d.cts +75 -0
- package/dist/server/adapters/express.d.ts +75 -0
- package/dist/server/adapters/express.js +78 -0
- package/dist/server/adapters/express.js.map +1 -0
- package/dist/server/adapters/hono.cjs +32 -0
- package/dist/server/adapters/hono.cjs.map +1 -0
- package/dist/server/adapters/hono.d.cts +96 -0
- package/dist/server/adapters/hono.d.ts +96 -0
- package/dist/server/adapters/hono.js +28 -0
- package/dist/server/adapters/hono.js.map +1 -0
- package/dist/server/adapters/index.cjs +135 -0
- package/dist/server/adapters/index.cjs.map +1 -0
- package/dist/server/adapters/index.d.cts +5 -0
- package/dist/server/adapters/index.d.ts +5 -0
- package/dist/server/adapters/index.js +125 -0
- package/dist/server/adapters/index.js.map +1 -0
- package/dist/server/adapters/next.cjs +30 -0
- package/dist/server/adapters/next.cjs.map +1 -0
- package/dist/server/adapters/next.d.cts +107 -0
- package/dist/server/adapters/next.d.ts +107 -0
- package/dist/server/adapters/next.js +25 -0
- package/dist/server/adapters/next.js.map +1 -0
- package/dist/server/index.cjs +1093 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +131 -0
- package/dist/server/index.d.ts +131 -0
- package/dist/server/index.js +1080 -0
- package/dist/server/index.js.map +1 -0
- package/dist/types-Cgfcw91r.d.cts +282 -0
- package/dist/types-Cgfcw91r.d.ts +282 -0
- package/dist/types-OVKV6qpE.d.cts +118 -0
- package/dist/types-OVKV6qpE.d.ts +118 -0
- package/package.json +78 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { A as AgentXHandler } from '../../types-Cgfcw91r.js';
|
|
2
|
+
import '@agentxjs/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hono Adapter
|
|
6
|
+
*
|
|
7
|
+
* Adapts AgentXHandler to Hono middleware.
|
|
8
|
+
* Hono already uses Web Standard Request/Response, so this is mostly a convenience wrapper.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Hono } from "hono";
|
|
13
|
+
* import { agentx } from "agentxjs";
|
|
14
|
+
* import { createAgentXHandler } from "agentxjs/server";
|
|
15
|
+
* import { toHonoHandler, createHonoRoutes } from "agentxjs/server/adapters/hono";
|
|
16
|
+
*
|
|
17
|
+
* const app = new Hono();
|
|
18
|
+
* const handler = createAgentXHandler(agentx);
|
|
19
|
+
*
|
|
20
|
+
* // Option 1: Simple catch-all
|
|
21
|
+
* app.all("/agentx/*", toHonoHandler(handler));
|
|
22
|
+
*
|
|
23
|
+
* // Option 2: Mount as sub-app
|
|
24
|
+
* app.route("/agentx", createHonoRoutes(handler));
|
|
25
|
+
*
|
|
26
|
+
* export default app;
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Hono Context interface (minimal)
|
|
32
|
+
*/
|
|
33
|
+
interface HonoContext {
|
|
34
|
+
req: {
|
|
35
|
+
raw: Request;
|
|
36
|
+
url: string;
|
|
37
|
+
method: string;
|
|
38
|
+
path: string;
|
|
39
|
+
};
|
|
40
|
+
body: (data: ReadableStream | string | ArrayBuffer | null, init?: ResponseInit) => Response;
|
|
41
|
+
json: (data: unknown, init?: ResponseInit) => Response;
|
|
42
|
+
text: (data: string, init?: ResponseInit) => Response;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Hono handler type
|
|
46
|
+
*/
|
|
47
|
+
type HonoHandler = (c: HonoContext) => Response | Promise<Response>;
|
|
48
|
+
/**
|
|
49
|
+
* Convert AgentXHandler to Hono handler
|
|
50
|
+
*
|
|
51
|
+
* Since Hono uses Web Standard Request/Response natively,
|
|
52
|
+
* this adapter is straightforward.
|
|
53
|
+
*
|
|
54
|
+
* @param handler - AgentX handler
|
|
55
|
+
* @returns Hono handler function
|
|
56
|
+
*/
|
|
57
|
+
declare function toHonoHandler(handler: AgentXHandler): HonoHandler;
|
|
58
|
+
/**
|
|
59
|
+
* Hono-like app interface (minimal for sub-routing)
|
|
60
|
+
*/
|
|
61
|
+
interface HonoLike {
|
|
62
|
+
all(path: string, handler: HonoHandler): HonoLike;
|
|
63
|
+
get(path: string, handler: HonoHandler): HonoLike;
|
|
64
|
+
post(path: string, handler: HonoHandler): HonoLike;
|
|
65
|
+
delete(path: string, handler: HonoHandler): HonoLike;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create a Hono sub-app with all AgentX routes
|
|
69
|
+
*
|
|
70
|
+
* This creates explicit routes for better type safety and documentation.
|
|
71
|
+
*
|
|
72
|
+
* @param handler - AgentX handler
|
|
73
|
+
* @param HonoClass - Hono class constructor (optional, for tree-shaking)
|
|
74
|
+
* @returns Hono app with all routes configured
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* import { Hono } from "hono";
|
|
79
|
+
* import { createHonoRoutes } from "agentxjs/server/adapters/hono";
|
|
80
|
+
*
|
|
81
|
+
* const app = new Hono();
|
|
82
|
+
* app.route("/agentx", createHonoRoutes(handler, Hono));
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function createHonoRoutes<T extends HonoLike>(handler: AgentXHandler, HonoClass: new () => T): T;
|
|
86
|
+
/**
|
|
87
|
+
* Simple Hono middleware that forwards all requests to handler
|
|
88
|
+
*
|
|
89
|
+
* Use this when you want a simple catch-all without explicit routing.
|
|
90
|
+
*
|
|
91
|
+
* @param handler - AgentX handler
|
|
92
|
+
* @returns Hono handler
|
|
93
|
+
*/
|
|
94
|
+
declare function createHonoAdapter(handler: AgentXHandler): HonoHandler;
|
|
95
|
+
|
|
96
|
+
export { type HonoHandler, createHonoAdapter, createHonoRoutes, toHonoHandler };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/server/adapters/hono.ts
|
|
2
|
+
function toHonoHandler(handler) {
|
|
3
|
+
return async (c) => {
|
|
4
|
+
return handler(c.req.raw);
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
function createHonoRoutes(handler, HonoClass) {
|
|
8
|
+
const app = new HonoClass();
|
|
9
|
+
const honoHandler = toHonoHandler(handler);
|
|
10
|
+
app.get("/info", honoHandler);
|
|
11
|
+
app.get("/health", honoHandler);
|
|
12
|
+
app.get("/agents", honoHandler);
|
|
13
|
+
app.post("/agents", honoHandler);
|
|
14
|
+
app.get("/agents/:agentId", honoHandler);
|
|
15
|
+
app.delete("/agents/:agentId", honoHandler);
|
|
16
|
+
app.get("/agents/:agentId/sse", honoHandler);
|
|
17
|
+
app.post("/agents/:agentId/messages", honoHandler);
|
|
18
|
+
app.post("/agents/:agentId/interrupt", honoHandler);
|
|
19
|
+
app.all("/*", honoHandler);
|
|
20
|
+
return app;
|
|
21
|
+
}
|
|
22
|
+
function createHonoAdapter(handler) {
|
|
23
|
+
return toHonoHandler(handler);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { createHonoAdapter, createHonoRoutes, toHonoHandler };
|
|
27
|
+
//# sourceMappingURL=hono.js.map
|
|
28
|
+
//# sourceMappingURL=hono.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/adapters/hono.ts"],"names":[],"mappings":";AA0DO,SAAS,cAAc,OAAA,EAAqC;AACjE,EAAA,OAAO,OAAO,CAAA,KAAM;AAElB,IAAA,OAAO,OAAA,CAAQ,CAAA,CAAE,GAAA,CAAI,GAAG,CAAA;AAAA,EAC1B,CAAA;AACF;AA8BO,SAAS,gBAAA,CACd,SACA,SAAA,EACG;AACH,EAAA,MAAM,GAAA,GAAM,IAAI,SAAA,EAAU;AAC1B,EAAA,MAAM,WAAA,GAAc,cAAc,OAAO,CAAA;AAGzC,EAAA,GAAA,CAAI,GAAA,CAAI,SAAS,WAAW,CAAA;AAC5B,EAAA,GAAA,CAAI,GAAA,CAAI,WAAW,WAAW,CAAA;AAG9B,EAAA,GAAA,CAAI,GAAA,CAAI,WAAW,WAAW,CAAA;AAC9B,EAAA,GAAA,CAAI,IAAA,CAAK,WAAW,WAAW,CAAA;AAG/B,EAAA,GAAA,CAAI,GAAA,CAAI,oBAAoB,WAAW,CAAA;AACvC,EAAA,GAAA,CAAI,MAAA,CAAO,oBAAoB,WAAW,CAAA;AAC1C,EAAA,GAAA,CAAI,GAAA,CAAI,wBAAwB,WAAW,CAAA;AAC3C,EAAA,GAAA,CAAI,IAAA,CAAK,6BAA6B,WAAW,CAAA;AACjD,EAAA,GAAA,CAAI,IAAA,CAAK,8BAA8B,WAAW,CAAA;AAGlD,EAAA,GAAA,CAAI,GAAA,CAAI,MAAM,WAAW,CAAA;AAEzB,EAAA,OAAO,GAAA;AACT;AAUO,SAAS,kBAAkB,OAAA,EAAqC;AACrE,EAAA,OAAO,cAAc,OAAO,CAAA;AAC9B","file":"hono.js","sourcesContent":["/**\n * Hono Adapter\n *\n * Adapts AgentXHandler to Hono middleware.\n * Hono already uses Web Standard Request/Response, so this is mostly a convenience wrapper.\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { toHonoHandler, createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * const handler = createAgentXHandler(agentx);\n *\n * // Option 1: Simple catch-all\n * app.all(\"/agentx/*\", toHonoHandler(handler));\n *\n * // Option 2: Mount as sub-app\n * app.route(\"/agentx\", createHonoRoutes(handler));\n *\n * export default app;\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Hono Context interface (minimal)\n */\ninterface HonoContext {\n req: {\n raw: Request;\n url: string;\n method: string;\n path: string;\n };\n // Return response\n body: (data: ReadableStream | string | ArrayBuffer | null, init?: ResponseInit) => Response;\n json: (data: unknown, init?: ResponseInit) => Response;\n text: (data: string, init?: ResponseInit) => Response;\n}\n\n/**\n * Hono handler type\n */\nexport type HonoHandler = (c: HonoContext) => Response | Promise<Response>;\n\n/**\n * Convert AgentXHandler to Hono handler\n *\n * Since Hono uses Web Standard Request/Response natively,\n * this adapter is straightforward.\n *\n * @param handler - AgentX handler\n * @returns Hono handler function\n */\nexport function toHonoHandler(handler: AgentXHandler): HonoHandler {\n return async (c) => {\n // Hono provides the raw Web Request directly\n return handler(c.req.raw);\n };\n}\n\n/**\n * Hono-like app interface (minimal for sub-routing)\n */\ninterface HonoLike {\n all(path: string, handler: HonoHandler): HonoLike;\n get(path: string, handler: HonoHandler): HonoLike;\n post(path: string, handler: HonoHandler): HonoLike;\n delete(path: string, handler: HonoHandler): HonoLike;\n}\n\n/**\n * Create a Hono sub-app with all AgentX routes\n *\n * This creates explicit routes for better type safety and documentation.\n *\n * @param handler - AgentX handler\n * @param HonoClass - Hono class constructor (optional, for tree-shaking)\n * @returns Hono app with all routes configured\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * app.route(\"/agentx\", createHonoRoutes(handler, Hono));\n * ```\n */\nexport function createHonoRoutes<T extends HonoLike>(\n handler: AgentXHandler,\n HonoClass: new () => T\n): T {\n const app = new HonoClass();\n const honoHandler = toHonoHandler(handler);\n\n // Platform routes\n app.get(\"/info\", honoHandler);\n app.get(\"/health\", honoHandler);\n\n // Agent collection routes\n app.get(\"/agents\", honoHandler);\n app.post(\"/agents\", honoHandler);\n\n // Agent instance routes\n app.get(\"/agents/:agentId\", honoHandler);\n app.delete(\"/agents/:agentId\", honoHandler);\n app.get(\"/agents/:agentId/sse\", honoHandler);\n app.post(\"/agents/:agentId/messages\", honoHandler);\n app.post(\"/agents/:agentId/interrupt\", honoHandler);\n\n // Catch-all for any missed routes\n app.all(\"/*\", honoHandler);\n\n return app;\n}\n\n/**\n * Simple Hono middleware that forwards all requests to handler\n *\n * Use this when you want a simple catch-all without explicit routing.\n *\n * @param handler - AgentX handler\n * @returns Hono handler\n */\nexport function createHonoAdapter(handler: AgentXHandler): HonoHandler {\n return toHonoHandler(handler);\n}\n"]}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/server/adapters/express.ts
|
|
4
|
+
function toWebRequest(req) {
|
|
5
|
+
const protocol = req.protocol || "http";
|
|
6
|
+
const host = req.hostname || req.headers.host || "localhost";
|
|
7
|
+
const url = `${protocol}://${host}${req.originalUrl || req.url}`;
|
|
8
|
+
const headers = new Headers();
|
|
9
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
10
|
+
if (value !== void 0) {
|
|
11
|
+
if (Array.isArray(value)) {
|
|
12
|
+
value.forEach((v) => headers.append(key, v));
|
|
13
|
+
} else {
|
|
14
|
+
headers.set(key, value);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const init = {
|
|
19
|
+
method: req.method,
|
|
20
|
+
headers
|
|
21
|
+
};
|
|
22
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
23
|
+
if (req.body !== void 0) {
|
|
24
|
+
init.body = JSON.stringify(req.body);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return new Request(url, init);
|
|
28
|
+
}
|
|
29
|
+
async function sendWebResponse(res, webResponse) {
|
|
30
|
+
res.status(webResponse.status);
|
|
31
|
+
webResponse.headers.forEach((value, key) => {
|
|
32
|
+
if (key.toLowerCase() !== "content-encoding") {
|
|
33
|
+
res.setHeader(key, value);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const contentType = webResponse.headers.get("content-type");
|
|
37
|
+
if (contentType?.includes("text/event-stream")) {
|
|
38
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
39
|
+
res.setHeader("Connection", "keep-alive");
|
|
40
|
+
if (res.flushHeaders) {
|
|
41
|
+
res.flushHeaders();
|
|
42
|
+
}
|
|
43
|
+
const reader = webResponse.body?.getReader();
|
|
44
|
+
if (reader) {
|
|
45
|
+
const decoder = new TextDecoder();
|
|
46
|
+
try {
|
|
47
|
+
while (true) {
|
|
48
|
+
const { done, value } = await reader.read();
|
|
49
|
+
if (done) break;
|
|
50
|
+
res.write(decoder.decode(value, { stream: true }));
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
} finally {
|
|
54
|
+
reader.releaseLock();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
res.end();
|
|
58
|
+
} else {
|
|
59
|
+
const body = await webResponse.text();
|
|
60
|
+
res.end(body);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function toExpressHandler(handler) {
|
|
64
|
+
return async (req, res, next) => {
|
|
65
|
+
try {
|
|
66
|
+
const webRequest = toWebRequest(req);
|
|
67
|
+
const webResponse = await handler(webRequest);
|
|
68
|
+
await sendWebResponse(res, webResponse);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
next(error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function createExpressAdapter(handler) {
|
|
75
|
+
return toExpressHandler(handler);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/server/adapters/hono.ts
|
|
79
|
+
function toHonoHandler(handler) {
|
|
80
|
+
return async (c) => {
|
|
81
|
+
return handler(c.req.raw);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function createHonoRoutes(handler, HonoClass) {
|
|
85
|
+
const app = new HonoClass();
|
|
86
|
+
const honoHandler = toHonoHandler(handler);
|
|
87
|
+
app.get("/info", honoHandler);
|
|
88
|
+
app.get("/health", honoHandler);
|
|
89
|
+
app.get("/agents", honoHandler);
|
|
90
|
+
app.post("/agents", honoHandler);
|
|
91
|
+
app.get("/agents/:agentId", honoHandler);
|
|
92
|
+
app.delete("/agents/:agentId", honoHandler);
|
|
93
|
+
app.get("/agents/:agentId/sse", honoHandler);
|
|
94
|
+
app.post("/agents/:agentId/messages", honoHandler);
|
|
95
|
+
app.post("/agents/:agentId/interrupt", honoHandler);
|
|
96
|
+
app.all("/*", honoHandler);
|
|
97
|
+
return app;
|
|
98
|
+
}
|
|
99
|
+
function createHonoAdapter(handler) {
|
|
100
|
+
return toHonoHandler(handler);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/server/adapters/next.ts
|
|
104
|
+
function createNextHandler(handler) {
|
|
105
|
+
const routeHandler = async (request) => {
|
|
106
|
+
return handler(request);
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
GET: routeHandler,
|
|
110
|
+
POST: routeHandler,
|
|
111
|
+
DELETE: routeHandler,
|
|
112
|
+
PUT: routeHandler,
|
|
113
|
+
PATCH: routeHandler,
|
|
114
|
+
HEAD: routeHandler,
|
|
115
|
+
OPTIONS: routeHandler
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function toNextHandler(handler) {
|
|
119
|
+
return async (request) => handler(request);
|
|
120
|
+
}
|
|
121
|
+
function createNextEdgeHandler(handler) {
|
|
122
|
+
return createNextHandler(handler);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
exports.createAgentXRoutes = createNextHandler;
|
|
126
|
+
exports.createExpressAdapter = createExpressAdapter;
|
|
127
|
+
exports.createHonoAdapter = createHonoAdapter;
|
|
128
|
+
exports.createHonoRoutes = createHonoRoutes;
|
|
129
|
+
exports.createNextEdgeHandler = createNextEdgeHandler;
|
|
130
|
+
exports.createNextHandler = createNextHandler;
|
|
131
|
+
exports.toExpressHandler = toExpressHandler;
|
|
132
|
+
exports.toHonoHandler = toHonoHandler;
|
|
133
|
+
exports.toNextHandler = toNextHandler;
|
|
134
|
+
//# sourceMappingURL=index.cjs.map
|
|
135
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/adapters/express.ts","../../../src/server/adapters/hono.ts","../../../src/server/adapters/next.ts"],"names":[],"mappings":";;;AAmEA,SAAS,aAAa,GAAA,EAA8B;AAElD,EAAA,MAAM,QAAA,GAAW,IAAI,QAAA,IAAY,MAAA;AACjC,EAAA,MAAM,IAAA,GAAO,GAAA,CAAI,QAAA,IAAY,GAAA,CAAI,QAAQ,IAAA,IAAQ,WAAA;AACjD,EAAA,MAAM,GAAA,GAAM,GAAG,QAAQ,CAAA,GAAA,EAAM,IAAI,CAAA,EAAG,GAAA,CAAI,WAAA,IAAe,GAAA,CAAI,GAAG,CAAA,CAAA;AAG9D,EAAA,MAAM,OAAA,GAAU,IAAI,OAAA,EAAQ;AAC5B,EAAA,KAAA,MAAW,CAAC,KAAK,KAAK,CAAA,IAAK,OAAO,OAAA,CAAQ,GAAA,CAAI,OAAO,CAAA,EAAG;AACtD,IAAA,IAAI,UAAU,MAAA,EAAW;AACvB,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxB,QAAA,KAAA,CAAM,QAAQ,CAAC,CAAA,KAAM,QAAQ,MAAA,CAAO,GAAA,EAAK,CAAC,CAAC,CAAA;AAAA,MAC7C,CAAA,MAAO;AACL,QAAA,OAAA,CAAQ,GAAA,CAAI,KAAK,KAAK,CAAA;AAAA,MACxB;AAAA,IACF;AAAA,EACF;AAGA,EAAA,MAAM,IAAA,GAAoB;AAAA,IACxB,QAAQ,GAAA,CAAI,MAAA;AAAA,IACZ;AAAA,GACF;AAGA,EAAA,IAAI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n * Adapts AgentXHandler to Express middleware.\n *\n * @example\n * ```typescript\n * import express from \"express\";\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { toExpressHandler } from \"agentxjs/server/adapters/express\";\n *\n * const app = express();\n * const handler = createAgentXHandler(agentx);\n *\n * app.use(\"/agentx\", toExpressHandler(handler));\n * app.listen(3000);\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Express-like request interface (minimal)\n */\ninterface ExpressRequest {\n method: string;\n url: string;\n headers: Record<string, string | string[] | undefined>;\n body?: unknown;\n protocol?: string;\n hostname?: string;\n originalUrl?: string;\n // For reading body as stream\n on?: (event: string, handler: (chunk: unknown) => void) => void;\n}\n\n/**\n * Express-like response interface (minimal)\n */\ninterface ExpressResponse {\n status(code: number): ExpressResponse;\n set(headers: Record<string, string>): ExpressResponse;\n setHeader(name: string, value: string): void;\n write(chunk: string | Uint8Array): boolean;\n end(data?: string | Buffer): void;\n headersSent: boolean;\n flushHeaders?(): void;\n}\n\n/**\n * Express-like next function\n */\ntype ExpressNext = (error?: unknown) => void;\n\n/**\n * Express request handler type\n */\nexport type ExpressHandler = (\n req: ExpressRequest,\n res: ExpressResponse,\n next: ExpressNext\n) => void | Promise<void>;\n\n/**\n * Convert Express request to Web Request\n */\nfunction toWebRequest(req: ExpressRequest): Request {\n // Build URL\n const protocol = req.protocol || \"http\";\n const host = req.hostname || req.headers.host || \"localhost\";\n const url = `${protocol}://${host}${req.originalUrl || req.url}`;\n\n // Build headers\n const headers = new Headers();\n for (const [key, value] of Object.entries(req.headers)) {\n if (value !== undefined) {\n if (Array.isArray(value)) {\n value.forEach((v) => headers.append(key, v));\n } else {\n headers.set(key, value);\n }\n }\n }\n\n // Build request options\n const init: RequestInit = {\n method: req.method,\n headers,\n };\n\n // Add body for non-GET/HEAD requests\n if (req.method !== \"GET\" && req.method !== \"HEAD\") {\n if (req.body !== undefined) {\n // Body already parsed (e.g., by express.json())\n init.body = JSON.stringify(req.body);\n }\n }\n\n return new Request(url, init);\n}\n\n/**\n * Send Web Response to Express response\n */\nasync function sendWebResponse(res: ExpressResponse, webResponse: Response): Promise<void> {\n // Set status\n res.status(webResponse.status);\n\n // Set headers\n webResponse.headers.forEach((value, key) => {\n // Skip content-encoding for SSE (we handle it differently)\n if (key.toLowerCase() !== \"content-encoding\") {\n res.setHeader(key, value);\n }\n });\n\n // Check if SSE response\n const contentType = webResponse.headers.get(\"content-type\");\n if (contentType?.includes(\"text/event-stream\")) {\n // SSE: Stream the response\n res.setHeader(\"Cache-Control\", \"no-cache\");\n res.setHeader(\"Connection\", \"keep-alive\");\n\n // Flush headers immediately for SSE\n if (res.flushHeaders) {\n res.flushHeaders();\n }\n\n const reader = webResponse.body?.getReader();\n if (reader) {\n const decoder = new TextDecoder();\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n res.write(decoder.decode(value, { stream: true }));\n }\n } catch {\n // Connection closed\n } finally {\n reader.releaseLock();\n }\n }\n res.end();\n } else {\n // Regular response: Send body as text\n const body = await webResponse.text();\n res.end(body);\n }\n}\n\n/**\n * Convert AgentXHandler to Express middleware\n *\n * @param handler - AgentX handler (Web Standard based)\n * @returns Express middleware function\n */\nexport function toExpressHandler(handler: AgentXHandler): ExpressHandler {\n return async (req, res, next) => {\n try {\n const webRequest = toWebRequest(req);\n const webResponse = await handler(webRequest);\n await sendWebResponse(res, webResponse);\n } catch (error) {\n next(error);\n }\n };\n}\n\n/**\n * Create Express router with AgentX handler\n *\n * This is a convenience function that wraps toExpressHandler\n * and can be used directly with app.use()\n *\n * @param handler - AgentX handler\n * @returns Express handler\n */\nexport function createExpressAdapter(handler: AgentXHandler): ExpressHandler {\n return toExpressHandler(handler);\n}\n","/**\n * Hono Adapter\n *\n * Adapts AgentXHandler to Hono middleware.\n * Hono already uses Web Standard Request/Response, so this is mostly a convenience wrapper.\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { toHonoHandler, createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * const handler = createAgentXHandler(agentx);\n *\n * // Option 1: Simple catch-all\n * app.all(\"/agentx/*\", toHonoHandler(handler));\n *\n * // Option 2: Mount as sub-app\n * app.route(\"/agentx\", createHonoRoutes(handler));\n *\n * export default app;\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Hono Context interface (minimal)\n */\ninterface HonoContext {\n req: {\n raw: Request;\n url: string;\n method: string;\n path: string;\n };\n // Return response\n body: (data: ReadableStream | string | ArrayBuffer | null, init?: ResponseInit) => Response;\n json: (data: unknown, init?: ResponseInit) => Response;\n text: (data: string, init?: ResponseInit) => Response;\n}\n\n/**\n * Hono handler type\n */\nexport type HonoHandler = (c: HonoContext) => Response | Promise<Response>;\n\n/**\n * Convert AgentXHandler to Hono handler\n *\n * Since Hono uses Web Standard Request/Response natively,\n * this adapter is straightforward.\n *\n * @param handler - AgentX handler\n * @returns Hono handler function\n */\nexport function toHonoHandler(handler: AgentXHandler): HonoHandler {\n return async (c) => {\n // Hono provides the raw Web Request directly\n return handler(c.req.raw);\n };\n}\n\n/**\n * Hono-like app interface (minimal for sub-routing)\n */\ninterface HonoLike {\n all(path: string, handler: HonoHandler): HonoLike;\n get(path: string, handler: HonoHandler): HonoLike;\n post(path: string, handler: HonoHandler): HonoLike;\n delete(path: string, handler: HonoHandler): HonoLike;\n}\n\n/**\n * Create a Hono sub-app with all AgentX routes\n *\n * This creates explicit routes for better type safety and documentation.\n *\n * @param handler - AgentX handler\n * @param HonoClass - Hono class constructor (optional, for tree-shaking)\n * @returns Hono app with all routes configured\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * app.route(\"/agentx\", createHonoRoutes(handler, Hono));\n * ```\n */\nexport function createHonoRoutes<T extends HonoLike>(\n handler: AgentXHandler,\n HonoClass: new () => T\n): T {\n const app = new HonoClass();\n const honoHandler = toHonoHandler(handler);\n\n // Platform routes\n app.get(\"/info\", honoHandler);\n app.get(\"/health\", honoHandler);\n\n // Agent collection routes\n app.get(\"/agents\", honoHandler);\n app.post(\"/agents\", honoHandler);\n\n // Agent instance routes\n app.get(\"/agents/:agentId\", honoHandler);\n app.delete(\"/agents/:agentId\", honoHandler);\n app.get(\"/agents/:agentId/sse\", honoHandler);\n app.post(\"/agents/:agentId/messages\", honoHandler);\n app.post(\"/agents/:agentId/interrupt\", honoHandler);\n\n // Catch-all for any missed routes\n app.all(\"/*\", honoHandler);\n\n return app;\n}\n\n/**\n * Simple Hono middleware that forwards all requests to handler\n *\n * Use this when you want a simple catch-all without explicit routing.\n *\n * @param handler - AgentX handler\n * @returns Hono handler\n */\nexport function createHonoAdapter(handler: AgentXHandler): HonoHandler {\n return toHonoHandler(handler);\n}\n","/**\n * Next.js App Router Adapter\n *\n * Adapts AgentXHandler to Next.js App Router route handlers.\n * Next.js App Router uses Web Standard Request/Response natively.\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n *\n * // Enable streaming for SSE\n * export const dynamic = \"force-dynamic\";\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Next.js route context\n */\ninterface NextRouteContext {\n params: Record<string, string | string[]>;\n}\n\n/**\n * Next.js route handler type\n */\nexport type NextRouteHandler = (\n request: Request,\n context?: NextRouteContext\n) => Response | Promise<Response>;\n\n/**\n * Next.js route handlers object\n */\nexport interface NextRouteHandlers {\n GET: NextRouteHandler;\n POST: NextRouteHandler;\n DELETE: NextRouteHandler;\n PUT: NextRouteHandler;\n PATCH: NextRouteHandler;\n HEAD: NextRouteHandler;\n OPTIONS: NextRouteHandler;\n}\n\n/**\n * Create Next.js App Router route handlers\n *\n * @param handler - AgentX handler\n * @returns Object with GET, POST, DELETE, etc. handlers\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n * ```\n */\nexport function createNextHandler(handler: AgentXHandler): NextRouteHandlers {\n // All methods use the same handler since routing is done by AgentXHandler\n const routeHandler: NextRouteHandler = async (request) => {\n return handler(request);\n };\n\n return {\n GET: routeHandler,\n POST: routeHandler,\n DELETE: routeHandler,\n PUT: routeHandler,\n PATCH: routeHandler,\n HEAD: routeHandler,\n OPTIONS: routeHandler,\n };\n}\n\n/**\n * Create a single Next.js handler for all methods\n *\n * Use this if you prefer to handle all methods with one export.\n *\n * @param handler - AgentX handler\n * @returns Single route handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { toNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = toNextHandler(createAgentXHandler(agentx));\n *\n * export { handler as GET, handler as POST, handler as DELETE };\n * ```\n */\nexport function toNextHandler(handler: AgentXHandler): NextRouteHandler {\n return async (request) => handler(request);\n}\n\n/**\n * Next.js Edge Runtime compatible handler\n *\n * Use this for edge functions deployment.\n *\n * @param handler - AgentX handler\n * @returns Edge-compatible handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextEdgeHandler } from \"agentxjs/server/adapters/next\";\n *\n * export const runtime = \"edge\";\n * export const { GET, POST, DELETE } = createNextEdgeHandler(handler);\n * ```\n */\nexport function createNextEdgeHandler(handler: AgentXHandler): NextRouteHandlers {\n // Edge runtime also uses Web Standard Request/Response\n return createNextHandler(handler);\n}\n\n/**\n * Convenience export for route.ts files\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createAgentXRoutes } from \"agentxjs/server/adapters/next\";\n *\n * const { GET, POST, DELETE } = createAgentXRoutes(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export { GET, POST, DELETE };\n * export const dynamic = \"force-dynamic\";\n * ```\n */\nexport { createNextHandler as createAgentXRoutes };\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ExpressHandler, createExpressAdapter, toExpressHandler } from './express.cjs';
|
|
2
|
+
export { HonoHandler, createHonoAdapter, createHonoRoutes, toHonoHandler } from './hono.cjs';
|
|
3
|
+
export { NextRouteHandler, NextRouteHandlers, createAgentXRoutes, createNextEdgeHandler, createAgentXRoutes as createNextHandler, toNextHandler } from './next.cjs';
|
|
4
|
+
import '../../types-Cgfcw91r.cjs';
|
|
5
|
+
import '@agentxjs/types';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ExpressHandler, createExpressAdapter, toExpressHandler } from './express.js';
|
|
2
|
+
export { HonoHandler, createHonoAdapter, createHonoRoutes, toHonoHandler } from './hono.js';
|
|
3
|
+
export { NextRouteHandler, NextRouteHandlers, createAgentXRoutes, createNextEdgeHandler, createAgentXRoutes as createNextHandler, toNextHandler } from './next.js';
|
|
4
|
+
import '../../types-Cgfcw91r.js';
|
|
5
|
+
import '@agentxjs/types';
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// src/server/adapters/express.ts
|
|
2
|
+
function toWebRequest(req) {
|
|
3
|
+
const protocol = req.protocol || "http";
|
|
4
|
+
const host = req.hostname || req.headers.host || "localhost";
|
|
5
|
+
const url = `${protocol}://${host}${req.originalUrl || req.url}`;
|
|
6
|
+
const headers = new Headers();
|
|
7
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
8
|
+
if (value !== void 0) {
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
value.forEach((v) => headers.append(key, v));
|
|
11
|
+
} else {
|
|
12
|
+
headers.set(key, value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const init = {
|
|
17
|
+
method: req.method,
|
|
18
|
+
headers
|
|
19
|
+
};
|
|
20
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
21
|
+
if (req.body !== void 0) {
|
|
22
|
+
init.body = JSON.stringify(req.body);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return new Request(url, init);
|
|
26
|
+
}
|
|
27
|
+
async function sendWebResponse(res, webResponse) {
|
|
28
|
+
res.status(webResponse.status);
|
|
29
|
+
webResponse.headers.forEach((value, key) => {
|
|
30
|
+
if (key.toLowerCase() !== "content-encoding") {
|
|
31
|
+
res.setHeader(key, value);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const contentType = webResponse.headers.get("content-type");
|
|
35
|
+
if (contentType?.includes("text/event-stream")) {
|
|
36
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
37
|
+
res.setHeader("Connection", "keep-alive");
|
|
38
|
+
if (res.flushHeaders) {
|
|
39
|
+
res.flushHeaders();
|
|
40
|
+
}
|
|
41
|
+
const reader = webResponse.body?.getReader();
|
|
42
|
+
if (reader) {
|
|
43
|
+
const decoder = new TextDecoder();
|
|
44
|
+
try {
|
|
45
|
+
while (true) {
|
|
46
|
+
const { done, value } = await reader.read();
|
|
47
|
+
if (done) break;
|
|
48
|
+
res.write(decoder.decode(value, { stream: true }));
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
} finally {
|
|
52
|
+
reader.releaseLock();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
res.end();
|
|
56
|
+
} else {
|
|
57
|
+
const body = await webResponse.text();
|
|
58
|
+
res.end(body);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function toExpressHandler(handler) {
|
|
62
|
+
return async (req, res, next) => {
|
|
63
|
+
try {
|
|
64
|
+
const webRequest = toWebRequest(req);
|
|
65
|
+
const webResponse = await handler(webRequest);
|
|
66
|
+
await sendWebResponse(res, webResponse);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
next(error);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function createExpressAdapter(handler) {
|
|
73
|
+
return toExpressHandler(handler);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/server/adapters/hono.ts
|
|
77
|
+
function toHonoHandler(handler) {
|
|
78
|
+
return async (c) => {
|
|
79
|
+
return handler(c.req.raw);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function createHonoRoutes(handler, HonoClass) {
|
|
83
|
+
const app = new HonoClass();
|
|
84
|
+
const honoHandler = toHonoHandler(handler);
|
|
85
|
+
app.get("/info", honoHandler);
|
|
86
|
+
app.get("/health", honoHandler);
|
|
87
|
+
app.get("/agents", honoHandler);
|
|
88
|
+
app.post("/agents", honoHandler);
|
|
89
|
+
app.get("/agents/:agentId", honoHandler);
|
|
90
|
+
app.delete("/agents/:agentId", honoHandler);
|
|
91
|
+
app.get("/agents/:agentId/sse", honoHandler);
|
|
92
|
+
app.post("/agents/:agentId/messages", honoHandler);
|
|
93
|
+
app.post("/agents/:agentId/interrupt", honoHandler);
|
|
94
|
+
app.all("/*", honoHandler);
|
|
95
|
+
return app;
|
|
96
|
+
}
|
|
97
|
+
function createHonoAdapter(handler) {
|
|
98
|
+
return toHonoHandler(handler);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/server/adapters/next.ts
|
|
102
|
+
function createNextHandler(handler) {
|
|
103
|
+
const routeHandler = async (request) => {
|
|
104
|
+
return handler(request);
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
GET: routeHandler,
|
|
108
|
+
POST: routeHandler,
|
|
109
|
+
DELETE: routeHandler,
|
|
110
|
+
PUT: routeHandler,
|
|
111
|
+
PATCH: routeHandler,
|
|
112
|
+
HEAD: routeHandler,
|
|
113
|
+
OPTIONS: routeHandler
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function toNextHandler(handler) {
|
|
117
|
+
return async (request) => handler(request);
|
|
118
|
+
}
|
|
119
|
+
function createNextEdgeHandler(handler) {
|
|
120
|
+
return createNextHandler(handler);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export { createNextHandler as createAgentXRoutes, createExpressAdapter, createHonoAdapter, createHonoRoutes, createNextEdgeHandler, createNextHandler, toExpressHandler, toHonoHandler, toNextHandler };
|
|
124
|
+
//# sourceMappingURL=index.js.map
|
|
125
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/adapters/express.ts","../../../src/server/adapters/hono.ts","../../../src/server/adapters/next.ts"],"names":[],"mappings":";AAmEA,SAAS,aAAa,GAAA,EAA8B;AAElD,EAAA,MAAM,QAAA,GAAW,IAAI,QAAA,IAAY,MAAA;AACjC,EAAA,MAAM,IAAA,GAAO,GAAA,CAAI,QAAA,IAAY,GAAA,CAAI,QAAQ,IAAA,IAAQ,WAAA;AACjD,EAAA,MAAM,GAAA,GAAM,GAAG,QAAQ,CAAA,GAAA,EAAM,IAAI,CAAA,EAAG,GAAA,CAAI,WAAA,IAAe,GAAA,CAAI,GAAG,CAAA,CAAA;AAG9D,EAAA,MAAM,OAAA,GAAU,IAAI,OAAA,EAAQ;AAC5B,EAAA,KAAA,MAAW,CAAC,KAAK,KAAK,CAAA,IAAK,OAAO,OAAA,CAAQ,GAAA,CAAI,OAAO,CAAA,EAAG;AACtD,IAAA,IAAI,UAAU,MAAA,EAAW;AACvB,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxB,QAAA,KAAA,CAAM,QAAQ,CAAC,CAAA,KAAM,QAAQ,MAAA,CAAO,GAAA,EAAK,CAAC,CAAC,CAAA;AAAA,MAC7C,CAAA,MAAO;AACL,QAAA,OAAA,CAAQ,GAAA,CAAI,KAAK,KAAK,CAAA;AAAA,MACxB;AAAA,IACF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;AAmBO,SAAS,sBAAsB,OAAA,EAA2C;AAE/E,EAAA,OAAO,kBAAkB,OAAO,CAAA;AAClC","file":"index.js","sourcesContent":["/**\n * Express Adapter\n *\n * Adapts AgentXHandler to Express middleware.\n *\n * @example\n * ```typescript\n * import express from \"express\";\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { toExpressHandler } from \"agentxjs/server/adapters/express\";\n *\n * const app = express();\n * const handler = createAgentXHandler(agentx);\n *\n * app.use(\"/agentx\", toExpressHandler(handler));\n * app.listen(3000);\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Express-like request interface (minimal)\n */\ninterface ExpressRequest {\n method: string;\n url: string;\n headers: Record<string, string | string[] | undefined>;\n body?: unknown;\n protocol?: string;\n hostname?: string;\n originalUrl?: string;\n // For reading body as stream\n on?: (event: string, handler: (chunk: unknown) => void) => void;\n}\n\n/**\n * Express-like response interface (minimal)\n */\ninterface ExpressResponse {\n status(code: number): ExpressResponse;\n set(headers: Record<string, string>): ExpressResponse;\n setHeader(name: string, value: string): void;\n write(chunk: string | Uint8Array): boolean;\n end(data?: string | Buffer): void;\n headersSent: boolean;\n flushHeaders?(): void;\n}\n\n/**\n * Express-like next function\n */\ntype ExpressNext = (error?: unknown) => void;\n\n/**\n * Express request handler type\n */\nexport type ExpressHandler = (\n req: ExpressRequest,\n res: ExpressResponse,\n next: ExpressNext\n) => void | Promise<void>;\n\n/**\n * Convert Express request to Web Request\n */\nfunction toWebRequest(req: ExpressRequest): Request {\n // Build URL\n const protocol = req.protocol || \"http\";\n const host = req.hostname || req.headers.host || \"localhost\";\n const url = `${protocol}://${host}${req.originalUrl || req.url}`;\n\n // Build headers\n const headers = new Headers();\n for (const [key, value] of Object.entries(req.headers)) {\n if (value !== undefined) {\n if (Array.isArray(value)) {\n value.forEach((v) => headers.append(key, v));\n } else {\n headers.set(key, value);\n }\n }\n }\n\n // Build request options\n const init: RequestInit = {\n method: req.method,\n headers,\n };\n\n // Add body for non-GET/HEAD requests\n if (req.method !== \"GET\" && req.method !== \"HEAD\") {\n if (req.body !== undefined) {\n // Body already parsed (e.g., by express.json())\n init.body = JSON.stringify(req.body);\n }\n }\n\n return new Request(url, init);\n}\n\n/**\n * Send Web Response to Express response\n */\nasync function sendWebResponse(res: ExpressResponse, webResponse: Response): Promise<void> {\n // Set status\n res.status(webResponse.status);\n\n // Set headers\n webResponse.headers.forEach((value, key) => {\n // Skip content-encoding for SSE (we handle it differently)\n if (key.toLowerCase() !== \"content-encoding\") {\n res.setHeader(key, value);\n }\n });\n\n // Check if SSE response\n const contentType = webResponse.headers.get(\"content-type\");\n if (contentType?.includes(\"text/event-stream\")) {\n // SSE: Stream the response\n res.setHeader(\"Cache-Control\", \"no-cache\");\n res.setHeader(\"Connection\", \"keep-alive\");\n\n // Flush headers immediately for SSE\n if (res.flushHeaders) {\n res.flushHeaders();\n }\n\n const reader = webResponse.body?.getReader();\n if (reader) {\n const decoder = new TextDecoder();\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n res.write(decoder.decode(value, { stream: true }));\n }\n } catch {\n // Connection closed\n } finally {\n reader.releaseLock();\n }\n }\n res.end();\n } else {\n // Regular response: Send body as text\n const body = await webResponse.text();\n res.end(body);\n }\n}\n\n/**\n * Convert AgentXHandler to Express middleware\n *\n * @param handler - AgentX handler (Web Standard based)\n * @returns Express middleware function\n */\nexport function toExpressHandler(handler: AgentXHandler): ExpressHandler {\n return async (req, res, next) => {\n try {\n const webRequest = toWebRequest(req);\n const webResponse = await handler(webRequest);\n await sendWebResponse(res, webResponse);\n } catch (error) {\n next(error);\n }\n };\n}\n\n/**\n * Create Express router with AgentX handler\n *\n * This is a convenience function that wraps toExpressHandler\n * and can be used directly with app.use()\n *\n * @param handler - AgentX handler\n * @returns Express handler\n */\nexport function createExpressAdapter(handler: AgentXHandler): ExpressHandler {\n return toExpressHandler(handler);\n}\n","/**\n * Hono Adapter\n *\n * Adapts AgentXHandler to Hono middleware.\n * Hono already uses Web Standard Request/Response, so this is mostly a convenience wrapper.\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { toHonoHandler, createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * const handler = createAgentXHandler(agentx);\n *\n * // Option 1: Simple catch-all\n * app.all(\"/agentx/*\", toHonoHandler(handler));\n *\n * // Option 2: Mount as sub-app\n * app.route(\"/agentx\", createHonoRoutes(handler));\n *\n * export default app;\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Hono Context interface (minimal)\n */\ninterface HonoContext {\n req: {\n raw: Request;\n url: string;\n method: string;\n path: string;\n };\n // Return response\n body: (data: ReadableStream | string | ArrayBuffer | null, init?: ResponseInit) => Response;\n json: (data: unknown, init?: ResponseInit) => Response;\n text: (data: string, init?: ResponseInit) => Response;\n}\n\n/**\n * Hono handler type\n */\nexport type HonoHandler = (c: HonoContext) => Response | Promise<Response>;\n\n/**\n * Convert AgentXHandler to Hono handler\n *\n * Since Hono uses Web Standard Request/Response natively,\n * this adapter is straightforward.\n *\n * @param handler - AgentX handler\n * @returns Hono handler function\n */\nexport function toHonoHandler(handler: AgentXHandler): HonoHandler {\n return async (c) => {\n // Hono provides the raw Web Request directly\n return handler(c.req.raw);\n };\n}\n\n/**\n * Hono-like app interface (minimal for sub-routing)\n */\ninterface HonoLike {\n all(path: string, handler: HonoHandler): HonoLike;\n get(path: string, handler: HonoHandler): HonoLike;\n post(path: string, handler: HonoHandler): HonoLike;\n delete(path: string, handler: HonoHandler): HonoLike;\n}\n\n/**\n * Create a Hono sub-app with all AgentX routes\n *\n * This creates explicit routes for better type safety and documentation.\n *\n * @param handler - AgentX handler\n * @param HonoClass - Hono class constructor (optional, for tree-shaking)\n * @returns Hono app with all routes configured\n *\n * @example\n * ```typescript\n * import { Hono } from \"hono\";\n * import { createHonoRoutes } from \"agentxjs/server/adapters/hono\";\n *\n * const app = new Hono();\n * app.route(\"/agentx\", createHonoRoutes(handler, Hono));\n * ```\n */\nexport function createHonoRoutes<T extends HonoLike>(\n handler: AgentXHandler,\n HonoClass: new () => T\n): T {\n const app = new HonoClass();\n const honoHandler = toHonoHandler(handler);\n\n // Platform routes\n app.get(\"/info\", honoHandler);\n app.get(\"/health\", honoHandler);\n\n // Agent collection routes\n app.get(\"/agents\", honoHandler);\n app.post(\"/agents\", honoHandler);\n\n // Agent instance routes\n app.get(\"/agents/:agentId\", honoHandler);\n app.delete(\"/agents/:agentId\", honoHandler);\n app.get(\"/agents/:agentId/sse\", honoHandler);\n app.post(\"/agents/:agentId/messages\", honoHandler);\n app.post(\"/agents/:agentId/interrupt\", honoHandler);\n\n // Catch-all for any missed routes\n app.all(\"/*\", honoHandler);\n\n return app;\n}\n\n/**\n * Simple Hono middleware that forwards all requests to handler\n *\n * Use this when you want a simple catch-all without explicit routing.\n *\n * @param handler - AgentX handler\n * @returns Hono handler\n */\nexport function createHonoAdapter(handler: AgentXHandler): HonoHandler {\n return toHonoHandler(handler);\n}\n","/**\n * Next.js App Router Adapter\n *\n * Adapts AgentXHandler to Next.js App Router route handlers.\n * Next.js App Router uses Web Standard Request/Response natively.\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n *\n * // Enable streaming for SSE\n * export const dynamic = \"force-dynamic\";\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Next.js route context\n */\ninterface NextRouteContext {\n params: Record<string, string | string[]>;\n}\n\n/**\n * Next.js route handler type\n */\nexport type NextRouteHandler = (\n request: Request,\n context?: NextRouteContext\n) => Response | Promise<Response>;\n\n/**\n * Next.js route handlers object\n */\nexport interface NextRouteHandlers {\n GET: NextRouteHandler;\n POST: NextRouteHandler;\n DELETE: NextRouteHandler;\n PUT: NextRouteHandler;\n PATCH: NextRouteHandler;\n HEAD: NextRouteHandler;\n OPTIONS: NextRouteHandler;\n}\n\n/**\n * Create Next.js App Router route handlers\n *\n * @param handler - AgentX handler\n * @returns Object with GET, POST, DELETE, etc. handlers\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n * ```\n */\nexport function createNextHandler(handler: AgentXHandler): NextRouteHandlers {\n // All methods use the same handler since routing is done by AgentXHandler\n const routeHandler: NextRouteHandler = async (request) => {\n return handler(request);\n };\n\n return {\n GET: routeHandler,\n POST: routeHandler,\n DELETE: routeHandler,\n PUT: routeHandler,\n PATCH: routeHandler,\n HEAD: routeHandler,\n OPTIONS: routeHandler,\n };\n}\n\n/**\n * Create a single Next.js handler for all methods\n *\n * Use this if you prefer to handle all methods with one export.\n *\n * @param handler - AgentX handler\n * @returns Single route handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { toNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = toNextHandler(createAgentXHandler(agentx));\n *\n * export { handler as GET, handler as POST, handler as DELETE };\n * ```\n */\nexport function toNextHandler(handler: AgentXHandler): NextRouteHandler {\n return async (request) => handler(request);\n}\n\n/**\n * Next.js Edge Runtime compatible handler\n *\n * Use this for edge functions deployment.\n *\n * @param handler - AgentX handler\n * @returns Edge-compatible handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextEdgeHandler } from \"agentxjs/server/adapters/next\";\n *\n * export const runtime = \"edge\";\n * export const { GET, POST, DELETE } = createNextEdgeHandler(handler);\n * ```\n */\nexport function createNextEdgeHandler(handler: AgentXHandler): NextRouteHandlers {\n // Edge runtime also uses Web Standard Request/Response\n return createNextHandler(handler);\n}\n\n/**\n * Convenience export for route.ts files\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createAgentXRoutes } from \"agentxjs/server/adapters/next\";\n *\n * const { GET, POST, DELETE } = createAgentXRoutes(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export { GET, POST, DELETE };\n * export const dynamic = \"force-dynamic\";\n * ```\n */\nexport { createNextHandler as createAgentXRoutes };\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/server/adapters/next.ts
|
|
4
|
+
function createNextHandler(handler) {
|
|
5
|
+
const routeHandler = async (request) => {
|
|
6
|
+
return handler(request);
|
|
7
|
+
};
|
|
8
|
+
return {
|
|
9
|
+
GET: routeHandler,
|
|
10
|
+
POST: routeHandler,
|
|
11
|
+
DELETE: routeHandler,
|
|
12
|
+
PUT: routeHandler,
|
|
13
|
+
PATCH: routeHandler,
|
|
14
|
+
HEAD: routeHandler,
|
|
15
|
+
OPTIONS: routeHandler
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function toNextHandler(handler) {
|
|
19
|
+
return async (request) => handler(request);
|
|
20
|
+
}
|
|
21
|
+
function createNextEdgeHandler(handler) {
|
|
22
|
+
return createNextHandler(handler);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.createAgentXRoutes = createNextHandler;
|
|
26
|
+
exports.createNextEdgeHandler = createNextEdgeHandler;
|
|
27
|
+
exports.createNextHandler = createNextHandler;
|
|
28
|
+
exports.toNextHandler = toNextHandler;
|
|
29
|
+
//# sourceMappingURL=next.cjs.map
|
|
30
|
+
//# sourceMappingURL=next.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/server/adapters/next.ts"],"names":[],"mappings":";;;AAwEO,SAAS,kBAAkB,OAAA,EAA2C;AAE3E,EAAA,MAAM,YAAA,GAAiC,OAAO,OAAA,KAAY;AACxD,IAAA,OAAO,QAAQ,OAAO,CAAA;AAAA,EACxB,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,YAAA;AAAA,IACL,IAAA,EAAM,YAAA;AAAA,IACN,MAAA,EAAQ,YAAA;AAAA,IACR,GAAA,EAAK,YAAA;AAAA,IACL,KAAA,EAAO,YAAA;AAAA,IACP,IAAA,EAAM,YAAA;AAAA,IACN,OAAA,EAAS;AAAA,GACX;AACF;AAoBO,SAAS,cAAc,OAAA,EAA0C;AACtE,EAAA,OAAO,OAAO,OAAA,KAAY,OAAA,CAAQ,OAAO,CAAA;AAC3C;AAmBO,SAAS,sBAAsB,OAAA,EAA2C;AAE/E,EAAA,OAAO,kBAAkB,OAAO,CAAA;AAClC","file":"next.cjs","sourcesContent":["/**\n * Next.js App Router Adapter\n *\n * Adapts AgentXHandler to Next.js App Router route handlers.\n * Next.js App Router uses Web Standard Request/Response natively.\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { agentx } from \"agentxjs\";\n * import { createAgentXHandler } from \"agentxjs/server\";\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n *\n * // Enable streaming for SSE\n * export const dynamic = \"force-dynamic\";\n * ```\n */\n\nimport type { AgentXHandler } from \"../types\";\n\n/**\n * Next.js route context\n */\ninterface NextRouteContext {\n params: Record<string, string | string[]>;\n}\n\n/**\n * Next.js route handler type\n */\nexport type NextRouteHandler = (\n request: Request,\n context?: NextRouteContext\n) => Response | Promise<Response>;\n\n/**\n * Next.js route handlers object\n */\nexport interface NextRouteHandlers {\n GET: NextRouteHandler;\n POST: NextRouteHandler;\n DELETE: NextRouteHandler;\n PUT: NextRouteHandler;\n PATCH: NextRouteHandler;\n HEAD: NextRouteHandler;\n OPTIONS: NextRouteHandler;\n}\n\n/**\n * Create Next.js App Router route handlers\n *\n * @param handler - AgentX handler\n * @returns Object with GET, POST, DELETE, etc. handlers\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = createAgentXHandler(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export const { GET, POST, DELETE } = createNextHandler(handler);\n * ```\n */\nexport function createNextHandler(handler: AgentXHandler): NextRouteHandlers {\n // All methods use the same handler since routing is done by AgentXHandler\n const routeHandler: NextRouteHandler = async (request) => {\n return handler(request);\n };\n\n return {\n GET: routeHandler,\n POST: routeHandler,\n DELETE: routeHandler,\n PUT: routeHandler,\n PATCH: routeHandler,\n HEAD: routeHandler,\n OPTIONS: routeHandler,\n };\n}\n\n/**\n * Create a single Next.js handler for all methods\n *\n * Use this if you prefer to handle all methods with one export.\n *\n * @param handler - AgentX handler\n * @returns Single route handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { toNextHandler } from \"agentxjs/server/adapters/next\";\n *\n * const handler = toNextHandler(createAgentXHandler(agentx));\n *\n * export { handler as GET, handler as POST, handler as DELETE };\n * ```\n */\nexport function toNextHandler(handler: AgentXHandler): NextRouteHandler {\n return async (request) => handler(request);\n}\n\n/**\n * Next.js Edge Runtime compatible handler\n *\n * Use this for edge functions deployment.\n *\n * @param handler - AgentX handler\n * @returns Edge-compatible handler\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createNextEdgeHandler } from \"agentxjs/server/adapters/next\";\n *\n * export const runtime = \"edge\";\n * export const { GET, POST, DELETE } = createNextEdgeHandler(handler);\n * ```\n */\nexport function createNextEdgeHandler(handler: AgentXHandler): NextRouteHandlers {\n // Edge runtime also uses Web Standard Request/Response\n return createNextHandler(handler);\n}\n\n/**\n * Convenience export for route.ts files\n *\n * @example\n * ```typescript\n * // app/api/agentx/[...path]/route.ts\n * import { createAgentXRoutes } from \"agentxjs/server/adapters/next\";\n *\n * const { GET, POST, DELETE } = createAgentXRoutes(agentx, {\n * basePath: \"/api/agentx\",\n * });\n *\n * export { GET, POST, DELETE };\n * export const dynamic = \"force-dynamic\";\n * ```\n */\nexport { createNextHandler as createAgentXRoutes };\n"]}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { A as AgentXHandler } from '../../types-Cgfcw91r.cjs';
|
|
2
|
+
import '@agentxjs/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Next.js App Router Adapter
|
|
6
|
+
*
|
|
7
|
+
* Adapts AgentXHandler to Next.js App Router route handlers.
|
|
8
|
+
* Next.js App Router uses Web Standard Request/Response natively.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // app/api/agentx/[...path]/route.ts
|
|
13
|
+
* import { agentx } from "agentxjs";
|
|
14
|
+
* import { createAgentXHandler } from "agentxjs/server";
|
|
15
|
+
* import { createNextHandler } from "agentxjs/server/adapters/next";
|
|
16
|
+
*
|
|
17
|
+
* const handler = createAgentXHandler(agentx, {
|
|
18
|
+
* basePath: "/api/agentx",
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* export const { GET, POST, DELETE } = createNextHandler(handler);
|
|
22
|
+
*
|
|
23
|
+
* // Enable streaming for SSE
|
|
24
|
+
* export const dynamic = "force-dynamic";
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Next.js route context
|
|
30
|
+
*/
|
|
31
|
+
interface NextRouteContext {
|
|
32
|
+
params: Record<string, string | string[]>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Next.js route handler type
|
|
36
|
+
*/
|
|
37
|
+
type NextRouteHandler = (request: Request, context?: NextRouteContext) => Response | Promise<Response>;
|
|
38
|
+
/**
|
|
39
|
+
* Next.js route handlers object
|
|
40
|
+
*/
|
|
41
|
+
interface NextRouteHandlers {
|
|
42
|
+
GET: NextRouteHandler;
|
|
43
|
+
POST: NextRouteHandler;
|
|
44
|
+
DELETE: NextRouteHandler;
|
|
45
|
+
PUT: NextRouteHandler;
|
|
46
|
+
PATCH: NextRouteHandler;
|
|
47
|
+
HEAD: NextRouteHandler;
|
|
48
|
+
OPTIONS: NextRouteHandler;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create Next.js App Router route handlers
|
|
52
|
+
*
|
|
53
|
+
* @param handler - AgentX handler
|
|
54
|
+
* @returns Object with GET, POST, DELETE, etc. handlers
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // app/api/agentx/[...path]/route.ts
|
|
59
|
+
* import { createNextHandler } from "agentxjs/server/adapters/next";
|
|
60
|
+
*
|
|
61
|
+
* const handler = createAgentXHandler(agentx, {
|
|
62
|
+
* basePath: "/api/agentx",
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* export const { GET, POST, DELETE } = createNextHandler(handler);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function createNextHandler(handler: AgentXHandler): NextRouteHandlers;
|
|
69
|
+
/**
|
|
70
|
+
* Create a single Next.js handler for all methods
|
|
71
|
+
*
|
|
72
|
+
* Use this if you prefer to handle all methods with one export.
|
|
73
|
+
*
|
|
74
|
+
* @param handler - AgentX handler
|
|
75
|
+
* @returns Single route handler
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // app/api/agentx/[...path]/route.ts
|
|
80
|
+
* import { toNextHandler } from "agentxjs/server/adapters/next";
|
|
81
|
+
*
|
|
82
|
+
* const handler = toNextHandler(createAgentXHandler(agentx));
|
|
83
|
+
*
|
|
84
|
+
* export { handler as GET, handler as POST, handler as DELETE };
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function toNextHandler(handler: AgentXHandler): NextRouteHandler;
|
|
88
|
+
/**
|
|
89
|
+
* Next.js Edge Runtime compatible handler
|
|
90
|
+
*
|
|
91
|
+
* Use this for edge functions deployment.
|
|
92
|
+
*
|
|
93
|
+
* @param handler - AgentX handler
|
|
94
|
+
* @returns Edge-compatible handler
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* // app/api/agentx/[...path]/route.ts
|
|
99
|
+
* import { createNextEdgeHandler } from "agentxjs/server/adapters/next";
|
|
100
|
+
*
|
|
101
|
+
* export const runtime = "edge";
|
|
102
|
+
* export const { GET, POST, DELETE } = createNextEdgeHandler(handler);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function createNextEdgeHandler(handler: AgentXHandler): NextRouteHandlers;
|
|
106
|
+
|
|
107
|
+
export { type NextRouteHandler, type NextRouteHandlers, createNextHandler as createAgentXRoutes, createNextEdgeHandler, createNextHandler, toNextHandler };
|