@economic/agents 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/hono.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { MiddlewareHandler } from "hono";
1
+ import { Context, MiddlewareHandler } from "hono";
2
2
 
3
3
  //#region src/hono/jwt-auth.d.ts
4
4
  interface JwtAuthConfig {
@@ -8,13 +8,20 @@ interface JwtAuthConfig {
8
8
  audience: string;
9
9
  /** Required OAuth scopes; token `scope` must include all (empty = no scope check). */
10
10
  requiredScopes?: readonly string[];
11
+ /**
12
+ * Custom token extraction function.
13
+ * If provided, replaces the default extraction entirely.
14
+ * Default checks Authorization header, then Sec-WebSocket-Protocol.
15
+ */
16
+ getToken?: (c: Context) => string | null | Promise<string | null>;
11
17
  }
12
18
  /**
13
19
  * Hono middleware: verify JWT via JWKS derived from the token `iss` claim,
14
20
  * after `iss` passes `allowedIssuers`.
15
21
  *
16
- * Reads the token from `Authorization: Bearer` only. Browser WebSocket clients
17
- * cannot set that header; use `routeAgentRequest` `onBeforeConnect` for those.
22
+ * By default, reads the token from `Authorization: Bearer` header first,
23
+ * then falls back to `Sec-WebSocket-Protocol: bearer, <token>` for WebSocket connections.
24
+ * Provide a custom `getToken` function to replace this behavior entirely.
18
25
  */
19
26
  declare function jwtAuth(config: JwtAuthConfig): MiddlewareHandler;
20
27
  //#endregion
package/dist/hono.mjs CHANGED
@@ -5,7 +5,9 @@ function getJwksForIssuer(issuer) {
5
5
  const normalized = issuer.replace(/\/$/, "");
6
6
  let jwks = jwksByIssuer.get(normalized);
7
7
  if (!jwks) {
8
- jwks = createRemoteJWKSet(new URL(`${normalized}/.well-known/openid-configuration/jwks`));
8
+ const jwksUrl = new URL(`${normalized}/.well-known/openid-configuration/jwks`);
9
+ console.log("jwksUrl", jwksUrl);
10
+ jwks = createRemoteJWKSet(jwksUrl);
9
11
  jwksByIssuer.set(normalized, jwks);
10
12
  }
11
13
  return jwks;
@@ -17,6 +19,17 @@ function bearerTokenFromAuthorizationHeader(authorization) {
17
19
  if (!authorization) return null;
18
20
  return authorization.match(/^Bearer\s+(.+)$/i)?.[1]?.trim() ?? null;
19
21
  }
22
+ function tokenFromWebSocketProtocol(header) {
23
+ if (!header) return null;
24
+ const parts = header.split(",").map((p) => p.trim());
25
+ const idx = parts.indexOf("bearer");
26
+ return idx !== -1 && parts[idx + 1] ? parts[idx + 1] : null;
27
+ }
28
+ function defaultGetToken(c) {
29
+ const authToken = bearerTokenFromAuthorizationHeader(c.req.header("Authorization"));
30
+ if (authToken) return authToken;
31
+ return tokenFromWebSocketProtocol(c.req.header("Sec-WebSocket-Protocol"));
32
+ }
20
33
  function hasRequiredScopes(tokenScope, required) {
21
34
  if (required.length === 0) return true;
22
35
  if (tokenScope === void 0) return false;
@@ -31,13 +44,15 @@ function authErrorResponse(status, message) {
31
44
  * Hono middleware: verify JWT via JWKS derived from the token `iss` claim,
32
45
  * after `iss` passes `allowedIssuers`.
33
46
  *
34
- * Reads the token from `Authorization: Bearer` only. Browser WebSocket clients
35
- * cannot set that header; use `routeAgentRequest` `onBeforeConnect` for those.
47
+ * By default, reads the token from `Authorization: Bearer` header first,
48
+ * then falls back to `Sec-WebSocket-Protocol: bearer, <token>` for WebSocket connections.
49
+ * Provide a custom `getToken` function to replace this behavior entirely.
36
50
  */
37
51
  function jwtAuth(config) {
38
52
  const requiredScopes = config.requiredScopes ?? [];
53
+ const getToken = config.getToken ?? defaultGetToken;
39
54
  return async (c, next) => {
40
- const token = bearerTokenFromAuthorizationHeader(c.req.header("Authorization"));
55
+ const token = await getToken(c);
41
56
  if (!token) return authErrorResponse(401, "Unauthorized: Missing authentication token");
42
57
  let unverifiedPayload;
43
58
  try {
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { Agent as Agent$1, Connection, ConnectionContext } from "agents";
1
+ import { Agent as Agent$1, AgentOptions, Connection, ConnectionContext } from "agents";
2
2
  import { AIChatAgent, OnChatMessageOptions } from "@cloudflare/ai-chat";
3
3
  import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
4
4
 
@@ -244,4 +244,7 @@ declare abstract class ChatAgentHarness<Env extends Cloudflare.Env, RequestBody
244
244
  onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, options?: OnChatMessageOptions): Promise<Response>;
245
245
  }
246
246
  //#endregion
247
- export { Agent, type AgentToolContext, type BuildLLMParamsConfig, ChatAgent, ChatAgentHarness, type Skill, buildLLMParams };
247
+ //#region src/server/route-agent-request.d.ts
248
+ declare function routeAgentRequest<Env>(request: Request, env: Env, options?: AgentOptions<Env>): Promise<Response | null>;
249
+ //#endregion
250
+ export { Agent, type AgentToolContext, type BuildLLMParamsConfig, ChatAgent, ChatAgentHarness, type Skill, buildLLMParams, routeAgentRequest };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Agent as Agent$1, callable } from "agents";
1
+ import { Agent as Agent$1, callable, routeAgentRequest as routeAgentRequest$1 } from "agents";
2
2
  import { AIChatAgent } from "@cloudflare/ai-chat";
3
3
  import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
4
4
  //#region src/server/features/skills/index.ts
@@ -880,4 +880,17 @@ var ChatAgentHarness = class extends ChatAgent {
880
880
  }
881
881
  };
882
882
  //#endregion
883
- export { Agent, ChatAgent, ChatAgentHarness, buildLLMParams };
883
+ //#region src/server/route-agent-request.ts
884
+ async function routeAgentRequest(request, env, options) {
885
+ const response = await routeAgentRequest$1(request, env, options);
886
+ if (!response) return null;
887
+ const protocol = request.headers.get("sec-websocket-protocol");
888
+ if (response.status === 101 && protocol) {
889
+ const newResponse = new Response(null, response);
890
+ newResponse.headers.set("Sec-WebSocket-Protocol", protocol.split(",")[0].trim());
891
+ return newResponse;
892
+ }
893
+ return response;
894
+ }
895
+ //#endregion
896
+ export { Agent, ChatAgent, ChatAgentHarness, buildLLMParams, routeAgentRequest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "bin": {