hono-agents 0.0.0-ff0679f → 0.0.0-ff9329f

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
@@ -7,15 +7,15 @@ Add intelligent, stateful AI agents to your Hono app. Create persistent AI agent
7
7
  ## Installation
8
8
 
9
9
  ```bash
10
- npm install hono-agents hono agents-sdk
10
+ npm install agents hono hono-agents
11
11
  ```
12
12
 
13
13
  ## Usage
14
14
 
15
15
  ```ts
16
16
  import { Hono } from "hono";
17
+ import { Agent } from "agents";
17
18
  import { agentsMiddleware } from "hono-agents";
18
- import { Agent } from "agents-sdk";
19
19
 
20
20
  // Define your agent classes
21
21
  export class ChatAgent extends Agent {
@@ -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
@@ -99,6 +104,6 @@ Agents can:
99
104
 
100
105
  ## License
101
106
 
102
- Learn more about Cloudflare Agents at https://www.npmjs.com/package/agents-sdk
107
+ Learn more about Cloudflare Agents at https://www.npmjs.com/package/agents
103
108
 
104
109
  ISC
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as hono from "hono";
2
2
  import { Env } from "hono";
3
- import { AgentOptions } from "agents-sdk";
3
+ import { AgentOptions } from "agents";
4
4
 
5
5
  /**
6
6
  * Configuration options for the Cloudflare Agents middleware
@@ -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-sdk";
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,8 +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(req, env(c), options);
38
+ return routeAgentRequest(c.req.raw, env(c), options);
44
39
  }
45
40
  export {
46
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-sdk\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents-sdk\";\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 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 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(req, env(c), options);\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;AACJ,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;AACtC,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,kBAAkB,KAAK,IAAI,CAAC,GAAG,OAAO;AAC/C;","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-ff0679f",
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-ff9329f",
9
+ "hono": "^4.9.8"
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": "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-sdk": "^0.0.20",
34
+ "agents": "0.0.0-ff9329f",
39
35
  "hono": "^4.6.17"
40
36
  },
41
- "devDependencies": {
42
- "agents-sdk": "^0.0.20",
43
- "hono": "^4.6.17"
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-ff9329f"
45
49
  }
package/src/index.ts CHANGED
@@ -1,9 +1,8 @@
1
+ import type { AgentOptions } from "agents";
2
+ import { routeAgentRequest } from "agents";
3
+ import type { Context, Env } from "hono";
1
4
  import { env } from "hono/adapter";
2
5
  import { createMiddleware } from "hono/factory";
3
- import { routeAgentRequest } from "agents-sdk";
4
-
5
- import type { Context, Env } from "hono";
6
- import type { AgentOptions } from "agents-sdk";
7
6
 
8
7
  /**
9
8
  * Configuration options for the Cloudflare Agents middleware
@@ -22,11 +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;
29
+
30
30
  const response = await handler(c, ctx?.options);
31
31
 
32
32
  return response === null ? await next() : response;
@@ -48,18 +48,6 @@ function isWebSocketUpgrade(c: Context): boolean {
48
48
  return c.req.header("upgrade")?.toLowerCase() === "websocket";
49
49
  }
50
50
 
51
- /**
52
- * Creates a new Request object from the Hono context
53
- * Preserves the original request's URL, method, headers, and body
54
- */
55
- function createRequestFromContext(c: Context) {
56
- return new Request(c.req.url, {
57
- method: c.req.method,
58
- headers: c.req.header(),
59
- body: c.req.raw.body,
60
- });
61
- }
62
-
63
51
  /**
64
52
  * Handles WebSocket upgrade requests
65
53
  * Returns a WebSocket upgrade response if successful, null otherwise
@@ -68,8 +56,11 @@ async function handleWebSocketUpgrade<E extends Env>(
68
56
  c: Context<E>,
69
57
  options?: AgentOptions<E>
70
58
  ) {
71
- const req = createRequestFromContext(c);
72
- 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
+ );
73
64
 
74
65
  if (!response?.webSocket) {
75
66
  return null;
@@ -77,7 +68,7 @@ async function handleWebSocketUpgrade<E extends Env>(
77
68
 
78
69
  return new Response(null, {
79
70
  status: 101,
80
- webSocket: response.webSocket,
71
+ webSocket: response.webSocket
81
72
  });
82
73
  }
83
74
 
@@ -89,6 +80,5 @@ async function handleHttpRequest<E extends Env>(
89
80
  c: Context<E>,
90
81
  options?: AgentOptions<E>
91
82
  ) {
92
- const req = createRequestFromContext(c);
93
- return routeAgentRequest(req, env(c), options);
83
+ return routeAgentRequest(c.req.raw, env(c) satisfies Env, options);
94
84
  }