hono-agents 0.0.0-fd36bbc → 0.0.0-fd59ae2

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/README.md CHANGED
@@ -43,8 +43,8 @@ app.use(
43
43
  const token = req.headers.get("authorization");
44
44
  // validate token
45
45
  if (!token) return new Response("Unauthorized", { status: 401 });
46
- },
47
- },
46
+ }
47
+ }
48
48
  })
49
49
  );
50
50
 
@@ -56,8 +56,8 @@ app.use(
56
56
  "*",
57
57
  agentsMiddleware({
58
58
  options: {
59
- prefix: "agents", // Handles /agents/* routes only
60
- },
59
+ prefix: "agents" // Handles /agents/* routes only
60
+ }
61
61
  })
62
62
  );
63
63
 
@@ -66,18 +66,23 @@ export default app;
66
66
 
67
67
  ## Configuration
68
68
 
69
- To properly configure your Cloudflare Workers project to use agents, update your `wrangler.toml` file:
70
-
71
- ```toml
72
- [durable_objects]
73
- bindings = [
74
- { name = "ChatAgent", class_name = "ChatAgent" },
75
- { name = "AssistantAgent", class_name = "AssistantAgent" }
76
- ]
77
-
78
- [[migrations]]
79
- tag = "v1"
80
- new_sqlite_classes = ["ChatAgent", "AssistantAgent"]
69
+ To properly configure your Cloudflare Workers project to use agents, add bindings to your `wrangler.jsonc` file:
70
+
71
+ ```json
72
+ {
73
+ "durable_objects": {
74
+ "bindings": [
75
+ { "name": "ChatAgent", "class_name": "ChatAgent" },
76
+ { "name": "AssistantAgent", "class_name": "AssistantAgent" }
77
+ ]
78
+ },
79
+ "migrations": [
80
+ {
81
+ "tag": "v1",
82
+ "new_sqlite_classes": ["ChatAgent", "AssistantAgent"]
83
+ }
84
+ ]
85
+ }
81
86
  ```
82
87
 
83
88
  ## How It Works
package/dist/index.d.ts CHANGED
@@ -17,6 +17,6 @@ type AgentMiddlewareContext<E extends Env> = {
17
17
  */
18
18
  declare function agentsMiddleware<E extends Env = Env>(
19
19
  ctx?: AgentMiddlewareContext<E>
20
- ): hono.MiddlewareHandler<any, string, {}>;
20
+ ): hono.MiddlewareHandler<Env, string, {}>;
21
21
 
22
22
  export { agentsMiddleware };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/index.ts
2
+ import { routeAgentRequest } from "agents";
2
3
  import { env } from "hono/adapter";
3
4
  import { createMiddleware } from "hono/factory";
4
- import { routeAgentRequest } from "agents";
5
5
  function agentsMiddleware(ctx) {
6
6
  return createMiddleware(async (c, next) => {
7
7
  try {
@@ -20,16 +20,12 @@ function agentsMiddleware(ctx) {
20
20
  function isWebSocketUpgrade(c) {
21
21
  return c.req.header("upgrade")?.toLowerCase() === "websocket";
22
22
  }
23
- function createRequestFromContext(c) {
24
- return new Request(c.req.url, {
25
- method: c.req.method,
26
- headers: c.req.header(),
27
- body: c.req.raw.body
28
- });
29
- }
30
23
  async function handleWebSocketUpgrade(c, options) {
31
- const req = createRequestFromContext(c);
32
- const response = await routeAgentRequest(req, env(c), options);
24
+ const response = await routeAgentRequest(
25
+ c.req.raw,
26
+ env(c),
27
+ options
28
+ );
33
29
  if (!response?.webSocket) {
34
30
  return null;
35
31
  }
@@ -39,13 +35,7 @@ async function handleWebSocketUpgrade(c, options) {
39
35
  });
40
36
  }
41
37
  async function handleHttpRequest(c, options) {
42
- const req = createRequestFromContext(c);
43
- return routeAgentRequest(
44
- req,
45
- env(c),
46
- // @ts-expect-error - TODO: fix this, I'm just bad at TS
47
- options
48
- );
38
+ return routeAgentRequest(c.req.raw, env(c), options);
49
39
  }
50
40
  export {
51
41
  agentsMiddleware
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { env } from \"hono/adapter\";\nimport { createMiddleware } from \"hono/factory\";\nimport { routeAgentRequest } from \"agents\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents\";\n\n/**\n * Configuration options for the Cloudflare Agents middleware\n */\ntype AgentMiddlewareContext<E extends Env> = {\n /** Cloudflare Agents-specific configuration options */\n options?: AgentOptions<E>;\n /** Optional error handler for caught errors */\n onError?: (error: Error) => void;\n};\n\n/**\n * Creates a middleware for handling Cloudflare Agents WebSocket and HTTP requests\n * Processes both WebSocket upgrades and standard HTTP requests, delegating them to Cloudflare Agents\n */\nexport function agentsMiddleware<E extends Env = Env>(\n ctx?: AgentMiddlewareContext<E>\n) {\n return createMiddleware(async (c, next) => {\n try {\n const handler = isWebSocketUpgrade(c)\n ? handleWebSocketUpgrade\n : handleHttpRequest;\n // @ts-expect-error - TODO: fix this, I'm just bad at TS\n const response = await handler(c, ctx?.options);\n\n return response === null ? await next() : response;\n } catch (error) {\n if (ctx?.onError) {\n ctx.onError(error as Error);\n return next();\n }\n throw error;\n }\n });\n}\n\n/**\n * Checks if the incoming request is a WebSocket upgrade request\n * Looks for the 'upgrade' header with a value of 'websocket' (case-insensitive)\n */\nfunction isWebSocketUpgrade(c: Context): boolean {\n return c.req.header(\"upgrade\")?.toLowerCase() === \"websocket\";\n}\n\n/**\n * Creates a new Request object from the Hono context\n * Preserves the original request's URL, method, headers, and body\n */\nfunction createRequestFromContext(c: Context) {\n return new Request(c.req.url, {\n method: c.req.method,\n headers: c.req.header(),\n body: c.req.raw.body,\n });\n}\n\n/**\n * Handles WebSocket upgrade requests\n * Returns a WebSocket upgrade response if successful, null otherwise\n */\nasync function handleWebSocketUpgrade<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n const req = createRequestFromContext(c);\n // @ts-expect-error - TODO: fix this, I'm just bad at TS\n const response = await routeAgentRequest(req, env(c), options);\n\n if (!response?.webSocket) {\n return null;\n }\n\n return new Response(null, {\n status: 101,\n webSocket: response.webSocket,\n });\n}\n\n/**\n * Handles standard HTTP requests\n * Forwards the request to Cloudflare Agents and returns the response\n */\nasync function handleHttpRequest<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n const req = createRequestFromContext(c);\n return routeAgentRequest(\n req,\n env(c),\n // @ts-expect-error - TODO: fix this, I'm just bad at TS\n options\n );\n}\n"],"mappings":";AAAA,SAAS,WAAW;AACpB,SAAS,wBAAwB;AACjC,SAAS,yBAAyB;AAmB3B,SAAS,iBACd,KACA;AACA,SAAO,iBAAiB,OAAO,GAAG,SAAS;AACzC,QAAI;AACF,YAAM,UAAU,mBAAmB,CAAC,IAChC,yBACA;AAEJ,YAAM,WAAW,MAAM,QAAQ,GAAG,KAAK,OAAO;AAE9C,aAAO,aAAa,OAAO,MAAM,KAAK,IAAI;AAAA,IAC5C,SAAS,OAAO;AACd,UAAI,KAAK,SAAS;AAChB,YAAI,QAAQ,KAAc;AAC1B,eAAO,KAAK;AAAA,MACd;AACA,YAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;AAMA,SAAS,mBAAmB,GAAqB;AAC/C,SAAO,EAAE,IAAI,OAAO,SAAS,GAAG,YAAY,MAAM;AACpD;AAMA,SAAS,yBAAyB,GAAY;AAC5C,SAAO,IAAI,QAAQ,EAAE,IAAI,KAAK;AAAA,IAC5B,QAAQ,EAAE,IAAI;AAAA,IACd,SAAS,EAAE,IAAI,OAAO;AAAA,IACtB,MAAM,EAAE,IAAI,IAAI;AAAA,EAClB,CAAC;AACH;AAMA,eAAe,uBACb,GACA,SACA;AACA,QAAM,MAAM,yBAAyB,CAAC;AAEtC,QAAM,WAAW,MAAM,kBAAkB,KAAK,IAAI,CAAC,GAAG,OAAO;AAE7D,MAAI,CAAC,UAAU,WAAW;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,SAAS,MAAM;AAAA,IACxB,QAAQ;AAAA,IACR,WAAW,SAAS;AAAA,EACtB,CAAC;AACH;AAMA,eAAe,kBACb,GACA,SACA;AACA,QAAM,MAAM,yBAAyB,CAAC;AACtC,SAAO;AAAA,IACL;AAAA,IACA,IAAI,CAAC;AAAA;AAAA,IAEL;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { AgentOptions } from \"agents\";\nimport { routeAgentRequest } from \"agents\";\nimport type { Context, Env } from \"hono\";\nimport { env } from \"hono/adapter\";\nimport { createMiddleware } from \"hono/factory\";\n\n/**\n * Configuration options for the Cloudflare Agents middleware\n */\ntype AgentMiddlewareContext<E extends Env> = {\n /** Cloudflare Agents-specific configuration options */\n options?: AgentOptions<E>;\n /** Optional error handler for caught errors */\n onError?: (error: Error) => void;\n};\n\n/**\n * Creates a middleware for handling Cloudflare Agents WebSocket and HTTP requests\n * Processes both WebSocket upgrades and standard HTTP requests, delegating them to Cloudflare Agents\n */\nexport function agentsMiddleware<E extends Env = Env>(\n ctx?: AgentMiddlewareContext<E>\n) {\n return createMiddleware<Env>(async (c, next) => {\n try {\n const handler = isWebSocketUpgrade(c)\n ? handleWebSocketUpgrade\n : handleHttpRequest;\n\n const response = await handler(c, ctx?.options);\n\n return response === null ? await next() : response;\n } catch (error) {\n if (ctx?.onError) {\n ctx.onError(error as Error);\n return next();\n }\n throw error;\n }\n });\n}\n\n/**\n * Checks if the incoming request is a WebSocket upgrade request\n * Looks for the 'upgrade' header with a value of 'websocket' (case-insensitive)\n */\nfunction isWebSocketUpgrade(c: Context): boolean {\n return c.req.header(\"upgrade\")?.toLowerCase() === \"websocket\";\n}\n\n/**\n * Handles WebSocket upgrade requests\n * Returns a WebSocket upgrade response if successful, null otherwise\n */\nasync function handleWebSocketUpgrade<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n const response = await routeAgentRequest(\n c.req.raw,\n env(c) satisfies Env,\n options\n );\n\n if (!response?.webSocket) {\n return null;\n }\n\n return new Response(null, {\n status: 101,\n webSocket: response.webSocket\n });\n}\n\n/**\n * Handles standard HTTP requests\n * Forwards the request to Cloudflare Agents and returns the response\n */\nasync function handleHttpRequest<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n return routeAgentRequest(c.req.raw, env(c) satisfies Env, options);\n}\n"],"mappings":";AACA,SAAS,yBAAyB;AAElC,SAAS,WAAW;AACpB,SAAS,wBAAwB;AAgB1B,SAAS,iBACd,KACA;AACA,SAAO,iBAAsB,OAAO,GAAG,SAAS;AAC9C,QAAI;AACF,YAAM,UAAU,mBAAmB,CAAC,IAChC,yBACA;AAEJ,YAAM,WAAW,MAAM,QAAQ,GAAG,KAAK,OAAO;AAE9C,aAAO,aAAa,OAAO,MAAM,KAAK,IAAI;AAAA,IAC5C,SAAS,OAAO;AACd,UAAI,KAAK,SAAS;AAChB,YAAI,QAAQ,KAAc;AAC1B,eAAO,KAAK;AAAA,MACd;AACA,YAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;AAMA,SAAS,mBAAmB,GAAqB;AAC/C,SAAO,EAAE,IAAI,OAAO,SAAS,GAAG,YAAY,MAAM;AACpD;AAMA,eAAe,uBACb,GACA,SACA;AACA,QAAM,WAAW,MAAM;AAAA,IACrB,EAAE,IAAI;AAAA,IACN,IAAI,CAAC;AAAA,IACL;AAAA,EACF;AAEA,MAAI,CAAC,UAAU,WAAW;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,SAAS,MAAM;AAAA,IACxB,QAAQ;AAAA,IACR,WAAW,SAAS;AAAA,EACtB,CAAC;AACH;AAMA,eAAe,kBACb,GACA,SACA;AACA,SAAO,kBAAkB,EAAE,IAAI,KAAK,IAAI,CAAC,GAAiB,OAAO;AACnE;","names":[]}
package/package.json CHANGED
@@ -1,45 +1,49 @@
1
1
  {
2
- "name": "hono-agents",
3
- "version": "0.0.0-fd36bbc",
4
- "main": "src/index.ts",
5
- "type": "module",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
8
- "build": "tsx ./scripts/build.ts"
2
+ "author": "Cloudflare Inc.",
3
+ "bugs": {
4
+ "url": "https://github.com/cloudflare/agents/issues"
5
+ },
6
+ "description": "Add Cloudflare Agents to your Hono app",
7
+ "devDependencies": {
8
+ "agents": "0.0.0-fd59ae2",
9
+ "hono": "^4.9.1"
10
+ },
11
+ "publishConfig": {
12
+ "access": "public"
9
13
  },
10
- "files": [
11
- "dist",
12
- "README.md"
13
- ],
14
14
  "exports": {
15
15
  ".": {
16
16
  "types": "./dist/index.d.ts",
17
- "require": "./dist/index.js",
18
- "import": "./dist/index.js"
17
+ "import": "./dist/index.js",
18
+ "require": "./dist/index.js"
19
19
  }
20
20
  },
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
21
25
  "keywords": [
22
26
  "cloudflare",
23
27
  "agents",
24
28
  "hono"
25
29
  ],
26
- "repository": {
27
- "type": "git",
28
- "url": "git+https://github.com/cloudflare/agents.git",
29
- "directory": "packages/hono-agents"
30
- },
31
- "bugs": {
32
- "url": "https://github.com/cloudflare/agents/issues"
33
- },
34
- "author": "Cloudflare Inc.",
35
30
  "license": "MIT",
36
- "description": "Add Cloudflare Agents to your Hono app",
31
+ "main": "src/index.ts",
32
+ "name": "hono-agents",
37
33
  "peerDependencies": {
38
- "agents": "0.0.0-fd36bbc",
34
+ "agents": "0.0.0-fd59ae2",
39
35
  "hono": "^4.6.17"
40
36
  },
41
- "devDependencies": {
42
- "agents": "0.0.0-fd36bbc",
43
- "hono": "^4.7.4"
44
- }
37
+ "repository": {
38
+ "directory": "packages/hono-agents",
39
+ "type": "git",
40
+ "url": "git+https://github.com/cloudflare/agents.git"
41
+ },
42
+ "scripts": {
43
+ "build": "tsx ./scripts/build.ts",
44
+ "test": "echo \"Error: no test specified\" && exit 1"
45
+ },
46
+ "type": "module",
47
+ "types": "dist/index.d.ts",
48
+ "version": "0.0.0-fd59ae2"
45
49
  }
package/src/index.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { env } from "hono/adapter";
2
- import { createMiddleware } from "hono/factory";
1
+ import type { AgentOptions } from "agents";
3
2
  import { routeAgentRequest } from "agents";
4
-
5
3
  import type { Context, Env } from "hono";
6
- import type { AgentOptions } from "agents";
4
+ import { env } from "hono/adapter";
5
+ import { createMiddleware } from "hono/factory";
7
6
 
8
7
  /**
9
8
  * Configuration options for the Cloudflare Agents middleware
@@ -22,12 +21,12 @@ type AgentMiddlewareContext<E extends Env> = {
22
21
  export function agentsMiddleware<E extends Env = Env>(
23
22
  ctx?: AgentMiddlewareContext<E>
24
23
  ) {
25
- return createMiddleware(async (c, next) => {
24
+ return createMiddleware<Env>(async (c, next) => {
26
25
  try {
27
26
  const handler = isWebSocketUpgrade(c)
28
27
  ? handleWebSocketUpgrade
29
28
  : handleHttpRequest;
30
- // @ts-expect-error - TODO: fix this, I'm just bad at TS
29
+
31
30
  const response = await handler(c, ctx?.options);
32
31
 
33
32
  return response === null ? await next() : response;
@@ -49,18 +48,6 @@ function isWebSocketUpgrade(c: Context): boolean {
49
48
  return c.req.header("upgrade")?.toLowerCase() === "websocket";
50
49
  }
51
50
 
52
- /**
53
- * Creates a new Request object from the Hono context
54
- * Preserves the original request's URL, method, headers, and body
55
- */
56
- function createRequestFromContext(c: Context) {
57
- return new Request(c.req.url, {
58
- method: c.req.method,
59
- headers: c.req.header(),
60
- body: c.req.raw.body,
61
- });
62
- }
63
-
64
51
  /**
65
52
  * Handles WebSocket upgrade requests
66
53
  * Returns a WebSocket upgrade response if successful, null otherwise
@@ -69,9 +56,11 @@ async function handleWebSocketUpgrade<E extends Env>(
69
56
  c: Context<E>,
70
57
  options?: AgentOptions<E>
71
58
  ) {
72
- const req = createRequestFromContext(c);
73
- // @ts-expect-error - TODO: fix this, I'm just bad at TS
74
- const response = await routeAgentRequest(req, env(c), options);
59
+ const response = await routeAgentRequest(
60
+ c.req.raw,
61
+ env(c) satisfies Env,
62
+ options
63
+ );
75
64
 
76
65
  if (!response?.webSocket) {
77
66
  return null;
@@ -79,7 +68,7 @@ async function handleWebSocketUpgrade<E extends Env>(
79
68
 
80
69
  return new Response(null, {
81
70
  status: 101,
82
- webSocket: response.webSocket,
71
+ webSocket: response.webSocket
83
72
  });
84
73
  }
85
74
 
@@ -91,11 +80,5 @@ async function handleHttpRequest<E extends Env>(
91
80
  c: Context<E>,
92
81
  options?: AgentOptions<E>
93
82
  ) {
94
- const req = createRequestFromContext(c);
95
- return routeAgentRequest(
96
- req,
97
- env(c),
98
- // @ts-expect-error - TODO: fix this, I'm just bad at TS
99
- options
100
- );
83
+ return routeAgentRequest(c.req.raw, env(c) satisfies Env, options);
101
84
  }