@economic/agents 0.0.1-alpha.5 → 0.0.1-alpha.7

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
@@ -136,16 +136,17 @@ interface SkillsConfig {
136
136
  /**
137
137
  * Skill context injected by the @withSkills decorator.
138
138
  *
139
- * Spread the context fields directly into streamText messages already has
140
- * guidance injected at the correct position:
139
+ * Spread the skill fields into streamText and pass headers via
140
+ * experimental_context so tools can read them from their second execute arg:
141
141
  *
142
142
  * ```typescript
143
- * const { messages, ...skillArgs } = ctx;
143
+ * const { messages, headers, guidance, ...skillArgs } = ctx;
144
144
  * return streamText({
145
145
  * model: this.getModel(),
146
146
  * system: "Your base prompt — static, never includes guidance",
147
147
  * messages,
148
148
  * ...skillArgs,
149
+ * experimental_context: { headers },
149
150
  * onFinish,
150
151
  * stopWhen: stepCountIs(20),
151
152
  * }).toUIMessageStreamResponse();
@@ -170,6 +171,12 @@ interface SkillContext {
170
171
  * Pass directly as the `messages` param of streamText.
171
172
  */
172
173
  messages: ai.ModelMessage[];
174
+ /**
175
+ * Headers captured from the WebSocket upgrade request, filtered to the
176
+ * names returned by getTrackedHeaders(). Pass to tools via
177
+ * experimental_context: `streamText({ experimental_context: { headers }, ... })`
178
+ */
179
+ headers: Record<string, string>;
173
180
  }
174
181
  /**
175
182
  * The object returned by createSkills().
@@ -244,6 +251,21 @@ declare abstract class AIChatAgentBase<Env extends Cloudflare.Env = Cloudflare.E
244
251
  * tail is maxPersistedMessages - 1 recent messages. Raise or lower per agent.
245
252
  */
246
253
  maxPersistedMessages: number;
254
+ /**
255
+ * Query parameter names to read from the WebSocket connection URL and
256
+ * forward to tools via experimental_context.
257
+ *
258
+ * Browsers cannot set custom headers on WebSocket upgrade requests, so
259
+ * auth tokens and other metadata must be passed as query parameters instead.
260
+ *
261
+ * ```typescript
262
+ * passthroughRequestHeaders = ['authorization', 'x-user-id'];
263
+ * ```
264
+ *
265
+ * Values are read from the URL at connect time and stored in _requestHeaders
266
+ * for the lifetime of the Durable Object instance.
267
+ */
268
+ passthroughRequestHeaders: string[];
247
269
  /** Tools that are always active regardless of loaded skills */
248
270
  abstract getTools(): ToolSet;
249
271
  /** All skills available for on-demand loading */
@@ -276,6 +298,8 @@ declare abstract class AIChatAgentBase<Env extends Cloudflare.Env = Cloudflare.E
276
298
  * skill when activate_skill is called. Defaults to allow-all.
277
299
  */
278
300
  protected filterSkill(_skillName: string): Promise<boolean>;
301
+ /** @internal Captured header values, keyed by lowercase name. */
302
+ protected _requestHeaders: Record<string, string>;
279
303
  /**
280
304
  * Buffered skill state from the current turn.
281
305
  *
@@ -409,13 +433,30 @@ declare function withSkills(fn: WithSkillsFn, _context: ClassMethodDecoratorCont
409
433
  * ```typescript
410
434
  * export class MyAgent extends AIChatAgent {
411
435
  * getModel() { return openai("gpt-4o"); }
412
- * getTools() { return []; }
436
+ * getTools() { return tools; }
413
437
  * getSkills() { return [searchSkill, codeSkill]; }
414
438
  * getSystemPrompt() { return "You are a helpful assistant."; }
415
439
  * getDB() { return this.env.AGENT_DB; }
416
440
  * }
417
441
  * ```
418
442
  *
443
+ * ## Passing auth headers to tools
444
+ *
445
+ * Set `passthroughRequestHeaders` to capture headers from the WebSocket upgrade
446
+ * request. They are forwarded automatically to every tool via `experimental_context`:
447
+ *
448
+ * ```typescript
449
+ * passthroughRequestHeaders = ['authorization', 'x-user-id'];
450
+ * ```
451
+ *
452
+ * Tools receive them as the second `execute` argument:
453
+ *
454
+ * ```typescript
455
+ * execute: async (args, { experimental_context }) => {
456
+ * const { authorization } = experimental_context?.headers ?? {};
457
+ * }
458
+ * ```
459
+ *
419
460
  * If you need full control over the `streamText` call (custom model options,
420
461
  * streaming transforms, varying the model per request, etc.) use
421
462
  * `AIChatAgentBase` with the `@withSkills` decorator instead.
@@ -446,6 +487,24 @@ declare abstract class AIChatAgent<Env extends Cloudflare.Env = Cloudflare.Env>
446
487
  onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, options?: OnChatMessageOptions): Promise<Response | undefined>;
447
488
  }
448
489
  //#endregion
490
+ //#region src/agents/chat/types.d.ts
491
+ /**
492
+ * The shape of experimental_context passed to tool execute functions.
493
+ *
494
+ * Cast experimental_context to this type to access forwarded request headers:
495
+ *
496
+ * ```typescript
497
+ * import type { AgentToolContext } from '@economic/agents';
498
+ *
499
+ * execute: async (args, { experimental_context }) => {
500
+ * const { authorization } = experimental_context as AgentToolContext;
501
+ * }
502
+ * ```
503
+ */
504
+ interface AgentToolContext {
505
+ headers: Record<string, string>;
506
+ }
507
+ //#endregion
449
508
  //#region src/features/skills/index.d.ts
450
509
  /**
451
510
  * Creates a skill loading system for use with the Vercel AI SDK.
@@ -565,4 +624,4 @@ declare function compactMessages(messages: UIMessage[], model: LanguageModel, ta
565
624
  */
566
625
  declare function compactIfNeeded(messages: UIMessage[], model: LanguageModel | undefined, tailSize: number): Promise<UIMessage[]>;
567
626
  //#endregion
568
- export { AIChatAgent, AIChatAgentBase, COMPACT_TOKEN_THRESHOLD, type Skill, type SkillContext, type SkillsConfig, type SkillsResult, compactIfNeeded, compactMessages, createSkills, estimateMessagesTokens, filterEphemeralMessages, injectGuidance, withSkills };
627
+ export { AIChatAgent, AIChatAgentBase, type AgentToolContext, COMPACT_TOKEN_THRESHOLD, type Skill, type SkillContext, type SkillsConfig, type SkillsResult, compactIfNeeded, compactMessages, createSkills, estimateMessagesTokens, filterEphemeralMessages, injectGuidance, withSkills };
package/dist/index.mjs CHANGED
@@ -447,6 +447,21 @@ var AIChatAgentBase = class extends AIChatAgent$1 {
447
447
  */
448
448
  maxPersistedMessages = 50;
449
449
  /**
450
+ * Query parameter names to read from the WebSocket connection URL and
451
+ * forward to tools via experimental_context.
452
+ *
453
+ * Browsers cannot set custom headers on WebSocket upgrade requests, so
454
+ * auth tokens and other metadata must be passed as query parameters instead.
455
+ *
456
+ * ```typescript
457
+ * passthroughRequestHeaders = ['authorization', 'x-user-id'];
458
+ * ```
459
+ *
460
+ * Values are read from the URL at connect time and stored in _requestHeaders
461
+ * for the lifetime of the Durable Object instance.
462
+ */
463
+ passthroughRequestHeaders = [];
464
+ /**
450
465
  * Return a LanguageModel to use for compaction summarisation.
451
466
  *
452
467
  * Return undefined (default) to disable compaction — messages are kept up
@@ -476,6 +491,8 @@ var AIChatAgentBase = class extends AIChatAgent$1 {
476
491
  async filterSkill(_skillName) {
477
492
  return true;
478
493
  }
494
+ /** @internal Captured header values, keyed by lowercase name. */
495
+ _requestHeaders = {};
479
496
  /**
480
497
  * Buffered skill state from the current turn.
481
498
  *
@@ -519,6 +536,14 @@ var AIChatAgentBase = class extends AIChatAgent$1 {
519
536
  * that case and replays in-progress chunks via its own protocol.
520
537
  */
521
538
  async onConnect(connection, ctx) {
539
+ if (this.passthroughRequestHeaders.length > 0) {
540
+ this._requestHeaders = {};
541
+ const params = new URL(ctx.request.url).searchParams;
542
+ for (const name of this.passthroughRequestHeaders) {
543
+ const value = params.get(name);
544
+ if (value !== null) this._requestHeaders[name] = value;
545
+ }
546
+ }
522
547
  await super.onConnect(connection, ctx);
523
548
  if (!this._activeStreamId && this.messages.length > 0) connection.send(JSON.stringify({
524
549
  type: "cf_agent_chat_messages",
@@ -593,7 +618,8 @@ var AIChatAgentBase = class extends AIChatAgent$1 {
593
618
  activeTools: skills.activeTools,
594
619
  prepareStep: skills.prepareStep,
595
620
  guidance: skills.getLoadedGuidance(),
596
- messages: await convertToModelMessages(this.messages)
621
+ messages: await convertToModelMessages(this.messages),
622
+ headers: this._requestHeaders
597
623
  };
598
624
  }
599
625
  };
@@ -622,13 +648,30 @@ function withSkills(fn, _context) {
622
648
  * ```typescript
623
649
  * export class MyAgent extends AIChatAgent {
624
650
  * getModel() { return openai("gpt-4o"); }
625
- * getTools() { return []; }
651
+ * getTools() { return tools; }
626
652
  * getSkills() { return [searchSkill, codeSkill]; }
627
653
  * getSystemPrompt() { return "You are a helpful assistant."; }
628
654
  * getDB() { return this.env.AGENT_DB; }
629
655
  * }
630
656
  * ```
631
657
  *
658
+ * ## Passing auth headers to tools
659
+ *
660
+ * Set `passthroughRequestHeaders` to capture headers from the WebSocket upgrade
661
+ * request. They are forwarded automatically to every tool via `experimental_context`:
662
+ *
663
+ * ```typescript
664
+ * passthroughRequestHeaders = ['authorization', 'x-user-id'];
665
+ * ```
666
+ *
667
+ * Tools receive them as the second `execute` argument:
668
+ *
669
+ * ```typescript
670
+ * execute: async (args, { experimental_context }) => {
671
+ * const { authorization } = experimental_context?.headers ?? {};
672
+ * }
673
+ * ```
674
+ *
632
675
  * If you need full control over the `streamText` call (custom model options,
633
676
  * streaming transforms, varying the model per request, etc.) use
634
677
  * `AIChatAgentBase` with the `@withSkills` decorator instead.
@@ -665,6 +708,7 @@ var AIChatAgent = class extends AIChatAgentBase {
665
708
  tools: skills.tools,
666
709
  activeTools: skills.activeTools,
667
710
  prepareStep: skills.prepareStep,
711
+ experimental_context: { headers: this._requestHeaders },
668
712
  stopWhen: stepCountIs(20),
669
713
  abortSignal: options?.abortSignal,
670
714
  onFinish
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "0.0.1-alpha.5",
3
+ "version": "0.0.1-alpha.7",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "homepage": "https://github.com/author/library#readme",
6
6
  "bugs": {