@economic/agents 2.3.13 → 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
@@ -49,6 +49,12 @@ function sendAgentType(connection, agentType) {
49
49
  agentType
50
50
  }));
51
51
  }
52
+ function sendSubAgentName(connection, subAgentName) {
53
+ connection.send(JSON.stringify({
54
+ type: "sub_agent_name",
55
+ subAgentName
56
+ }));
57
+ }
52
58
  //#endregion
53
59
  //#region src/server/agents/Agent.ts
54
60
  var Agent = class extends Think {
@@ -82,6 +88,9 @@ var Agent = class extends Think {
82
88
  if (!hasCorrectBindings) throw new Error("Could not connect to agent, bindings not found");
83
89
  this.registerInstance();
84
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
+ }
85
94
  async onConnect(connection, ctx) {
86
95
  sendAgentType(connection, this.agentType);
87
96
  sendConnectionStatus(connection, "connecting");
@@ -107,20 +116,23 @@ var Agent = class extends Think {
107
116
  return;
108
117
  }
109
118
  const token = extractTokenFromConnectRequest(ctx.request);
110
- if (token && this.getUserContext) this._pendingUserContextRequest = this.getUserContext(token).then((userContext) => {
111
- this.userContext = userContext;
112
- });
119
+ if (token && this.getUserContext) {
120
+ this.userContext = await this.getUserContext(token);
121
+ this._toolContextForCurrentTurn = this.getToolContextForCurrentTurn();
122
+ await this.session.refreshSystemPrompt();
123
+ }
113
124
  }
114
125
  }
115
126
  sendConnectionStatus(connection, "connected");
116
127
  }
117
- configureSession(session) {
118
- return session.withContext("soul", { provider: { get: async () => {
119
- return this.getSystemPrompt(this._toolContextForCurrentTurn !== void 0 ? this._toolContextForCurrentTurn : void 0);
120
- } } }).withContext("critical-rules", { provider: { get: async () => SECURITY_RULES_PROMPT } }).withContext("turn-protocol-rules", { provider: { get: async () => TURN_PROTOCOL_RULES_PROMPT } }).withCachedPrompt();
121
- }
122
128
  _toolsForCurrentTurn = {};
123
129
  _toolContextForCurrentTurn;
130
+ getToolContextForCurrentTurn(requestContext = {}) {
131
+ return {
132
+ ...requestContext,
133
+ _userContext: this.userContext
134
+ };
135
+ }
124
136
  /**
125
137
  * Merges the client request `body` into `experimental_context` for tools
126
138
  * returned from {@link getTools} only (Think-internal tools are unchanged).
@@ -129,11 +141,7 @@ var Agent = class extends Think {
129
141
  * `returned.tools` into your own `TurnConfig.tools` if you return tools.
130
142
  */
131
143
  async beforeTurn(ctx) {
132
- if (this._pendingUserContextRequest) await this._pendingUserContextRequest;
133
- this._toolContextForCurrentTurn = {
134
- ...ctx.body ?? {},
135
- _userContext: this.userContext
136
- };
144
+ this._toolContextForCurrentTurn = this.getToolContextForCurrentTurn(ctx.body);
137
145
  await this.session.refreshSystemPrompt();
138
146
  this._toolsForCurrentTurn = ctx.tools;
139
147
  const activeSkillName = this._getLastActivatedSkillName(ctx.messages);
@@ -245,8 +253,6 @@ var Agent = class extends Think {
245
253
  }
246
254
  }
247
255
  }
248
- /** Store the pending user context request to defer awaiting it until after the connection is established */
249
- _pendingUserContextRequest;
250
256
  /**
251
257
  * The user context for the session.
252
258
  * Define getUserContext to set a user context.
@@ -634,6 +640,7 @@ var Assistant = class extends Agent$1 {
634
640
  }
635
641
  async onConnect(connection, ctx) {
636
642
  sendAgentType(connection, "assistant");
643
+ sendSubAgentName(connection, this.agent.name);
637
644
  sendConnectionStatus(connection, "connecting");
638
645
  const getJwtAuthConfig = this.agent.getJwtAuthConfig;
639
646
  if (getJwtAuthConfig) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents",
3
- "version": "2.3.13",
3
+ "version": "2.3.15",
4
4
  "description": "A starter for creating a TypeScript package.",
5
5
  "license": "MIT",
6
6
  "files": [