@economic/agents 2.3.14 → 2.3.15

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
@@ -46,10 +46,11 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
46
46
  abstract getModel(ctx?: ToolContext<RequestContext, UserContext>): LanguageModel;
47
47
  abstract getSystemPrompt(ctx?: ToolContext<RequestContext, UserContext>): string;
48
48
  onStart(): Promise<void>;
49
- onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
50
49
  configureSession(session: Session): Session;
50
+ onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
51
51
  private _toolsForCurrentTurn;
52
52
  private _toolContextForCurrentTurn?;
53
+ private getToolContextForCurrentTurn;
53
54
  /**
54
55
  * Merges the client request `body` into `experimental_context` for tools
55
56
  * returned from {@link getTools} only (Think-internal tools are unchanged).
@@ -91,8 +92,6 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
91
92
  * @returns JWT auth config or undefined to skip authentication
92
93
  */
93
94
  static getJwtAuthConfig?(env: Cloudflare.Env): JwtAuthConfig<Record<string, unknown>> | undefined;
94
- /** Store the pending user context request to defer awaiting it until after the connection is established */
95
- private _pendingUserContextRequest?;
96
95
  /**
97
96
  * The user context for the session.
98
97
  * Define getUserContext to set a user context.
package/dist/index.mjs CHANGED
@@ -88,6 +88,9 @@ var Agent = class extends Think {
88
88
  if (!hasCorrectBindings) throw new Error("Could not connect to agent, bindings not found");
89
89
  this.registerInstance();
90
90
  }
91
+ configureSession(session) {
92
+ return session.withContext("soul", { provider: { get: async () => this.getSystemPrompt(this._toolContextForCurrentTurn) } }).withContext("critical-rules", { provider: { get: async () => SECURITY_RULES_PROMPT } }).withContext("turn-protocol-rules", { provider: { get: async () => TURN_PROTOCOL_RULES_PROMPT } }).withCachedPrompt();
93
+ }
91
94
  async onConnect(connection, ctx) {
92
95
  sendAgentType(connection, this.agentType);
93
96
  sendConnectionStatus(connection, "connecting");
@@ -113,20 +116,23 @@ var Agent = class extends Think {
113
116
  return;
114
117
  }
115
118
  const token = extractTokenFromConnectRequest(ctx.request);
116
- if (token && this.getUserContext) this._pendingUserContextRequest = this.getUserContext(token).then((userContext) => {
117
- this.userContext = userContext;
118
- });
119
+ if (token && this.getUserContext) {
120
+ this.userContext = await this.getUserContext(token);
121
+ this._toolContextForCurrentTurn = this.getToolContextForCurrentTurn();
122
+ await this.session.refreshSystemPrompt();
123
+ }
119
124
  }
120
125
  }
121
126
  sendConnectionStatus(connection, "connected");
122
127
  }
123
- configureSession(session) {
124
- return session.withContext("soul", { provider: { get: async () => {
125
- return this.getSystemPrompt(this._toolContextForCurrentTurn !== void 0 ? this._toolContextForCurrentTurn : void 0);
126
- } } }).withContext("critical-rules", { provider: { get: async () => SECURITY_RULES_PROMPT } }).withContext("turn-protocol-rules", { provider: { get: async () => TURN_PROTOCOL_RULES_PROMPT } }).withCachedPrompt();
127
- }
128
128
  _toolsForCurrentTurn = {};
129
129
  _toolContextForCurrentTurn;
130
+ getToolContextForCurrentTurn(requestContext = {}) {
131
+ return {
132
+ ...requestContext,
133
+ _userContext: this.userContext
134
+ };
135
+ }
130
136
  /**
131
137
  * Merges the client request `body` into `experimental_context` for tools
132
138
  * returned from {@link getTools} only (Think-internal tools are unchanged).
@@ -135,11 +141,7 @@ var Agent = class extends Think {
135
141
  * `returned.tools` into your own `TurnConfig.tools` if you return tools.
136
142
  */
137
143
  async beforeTurn(ctx) {
138
- if (this._pendingUserContextRequest) await this._pendingUserContextRequest;
139
- this._toolContextForCurrentTurn = {
140
- ...ctx.body ?? {},
141
- _userContext: this.userContext
142
- };
144
+ this._toolContextForCurrentTurn = this.getToolContextForCurrentTurn(ctx.body);
143
145
  await this.session.refreshSystemPrompt();
144
146
  this._toolsForCurrentTurn = ctx.tools;
145
147
  const activeSkillName = this._getLastActivatedSkillName(ctx.messages);
@@ -251,8 +253,6 @@ var Agent = class extends Think {
251
253
  }
252
254
  }
253
255
  }
254
- /** Store the pending user context request to defer awaiting it until after the connection is established */
255
- _pendingUserContextRequest;
256
256
  /**
257
257
  * The user context for the session.
258
258
  * Define getUserContext to set a user context.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.3.14",
3
+ "version": "2.3.15",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [