@economic/agents 1.6.1 → 1.6.3

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/index.d.mts CHANGED
@@ -152,7 +152,7 @@ interface MessageRating {
152
152
  * Skill loading, compaction, and LLM calls use `buildLLMParams` from
153
153
  * `@economic/agents` inside `onChatMessage`.
154
154
  */
155
- declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> extends AIChatAgent<Env & ChatAgentEnv> {
155
+ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TSessionIdentity extends Record<string, unknown> = Record<string, unknown>> extends AIChatAgent<Env & ChatAgentEnv> {
156
156
  /**
157
157
  * The binding of the Durable Object instance for this agent.
158
158
  */
@@ -201,6 +201,17 @@ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> ex
201
201
  * @returns JWT auth config or undefined to skip authentication
202
202
  */
203
203
  protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
204
+ /**
205
+ * The identity associated with the session.
206
+ * Define getIdentity to return the identity from the request.
207
+ */
208
+ protected identity: TSessionIdentity;
209
+ /**
210
+ * Returns the identity from the request.
211
+ * @returns The identity from the request.
212
+ * @param token - The token from the request.
213
+ */
214
+ protected getIdentity(_token: string): Promise<TSessionIdentity>;
204
215
  /**
205
216
  * Returns the user ID from the durable object name.
206
217
  */
@@ -243,7 +254,7 @@ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env> ex
243
254
  }
244
255
  //#endregion
245
256
  //#region src/server/agent-chat/ChatAgentHarness.d.ts
246
- declare abstract class ChatAgentHarness<Env extends Cloudflare.Env, RequestBody extends Record<string, unknown> = Record<string, unknown>> extends ChatAgent<Env> {
257
+ declare abstract class ChatAgentHarness<Env extends Cloudflare.Env, RequestBody extends Record<string, unknown> = Record<string, unknown>, SessionIdentity extends Record<string, unknown> = Record<string, unknown>> extends ChatAgent<Env, SessionIdentity> {
247
258
  get binding(): {
248
259
  getByName(name: string): {
249
260
  destroy(): Promise<void>;
package/dist/index.mjs CHANGED
@@ -375,7 +375,7 @@ function getJwksForIssuer(issuer) {
375
375
  function isIssuerAllowed(iss, allowed) {
376
376
  return allowed.some((rule) => typeof rule === "string" ? rule === iss : rule.test(iss));
377
377
  }
378
- function extractTokenFromRequest(request) {
378
+ function extractTokenFromConnectRequest(request) {
379
379
  const authorization = request.headers.get("Authorization");
380
380
  if (authorization) {
381
381
  const match = authorization.match(/^Bearer\s+(.+)$/i);
@@ -412,7 +412,7 @@ function hasRequiredScopes(tokenScope, required) {
412
412
  * @throws Error for unexpected failures that should be logged
413
413
  */
414
414
  async function verifyJwt(request, config) {
415
- const token = extractTokenFromRequest(request);
415
+ const token = extractTokenFromConnectRequest(request);
416
416
  if (!token) return {
417
417
  success: false,
418
418
  status: 401,
@@ -849,6 +849,19 @@ var ChatAgent = class extends AIChatAgent {
849
849
  */
850
850
  session;
851
851
  /**
852
+ * The identity associated with the session.
853
+ * Define getIdentity to return the identity from the request.
854
+ */
855
+ identity = {};
856
+ /**
857
+ * Returns the identity from the request.
858
+ * @returns The identity from the request.
859
+ * @param token - The token from the request.
860
+ */
861
+ async getIdentity(_token) {
862
+ return Promise.resolve({});
863
+ }
864
+ /**
852
865
  * Returns the user ID from the durable object name.
853
866
  */
854
867
  getUserId() {
@@ -883,6 +896,8 @@ var ChatAgent = class extends AIChatAgent {
883
896
  this.session = result.claims;
884
897
  }
885
898
  }
899
+ const token = extractTokenFromConnectRequest(ctx.request);
900
+ if (token) this.identity = await this.getIdentity(token);
886
901
  return super.onConnect(connection, ctx);
887
902
  }
888
903
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "bin": {