hono-agents 0.0.0-fe9e8d3 → 0.0.0-feef082

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.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 {
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<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":";AAAA,SAAS,WAAW;AACpB,SAAS,wBAAwB;AACjC,SAAS,yBAAyB;AAmB3B,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":[]}
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-fe9e8d3",
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-feef082",
9
+ "hono": "^4.9.6"
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-fe9e8d3",
34
+ "agents": "0.0.0-feef082",
39
35
  "hono": "^4.6.17"
40
36
  },
41
- "devDependencies": {
42
- "agents": "0.0.0-fe9e8d3",
43
- "hono": "^4.7.7"
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-feef082"
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
@@ -69,7 +68,7 @@ async function handleWebSocketUpgrade<E extends Env>(
69
68
 
70
69
  return new Response(null, {
71
70
  status: 101,
72
- webSocket: response.webSocket,
71
+ webSocket: response.webSocket
73
72
  });
74
73
  }
75
74