@economic/agents 1.7.2 → 1.7.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 +20 -16
- package/dist/index.mjs +23 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -57,9 +57,9 @@ declare function buildLLMParams(config: BuildLLMParamsConfig): LLMParams;
|
|
|
57
57
|
* type MyContext = AgentToolContext<MyBody>;
|
|
58
58
|
* ```
|
|
59
59
|
*/
|
|
60
|
-
type AgentToolContext<TBody = Record<string, unknown>,
|
|
60
|
+
type AgentToolContext<TBody = Record<string, unknown>, TUserContext = Record<string, unknown> | undefined> = TBody & {
|
|
61
61
|
_logEvent: (message: string, payload?: Record<string, unknown>) => void | Promise<void>;
|
|
62
|
-
|
|
62
|
+
_userContext?: TUserContext;
|
|
63
63
|
};
|
|
64
64
|
interface AgentEnv {
|
|
65
65
|
AGENT_DB: D1Database;
|
|
@@ -95,7 +95,9 @@ interface JwtAuthConfig<TClaims extends Record<string, unknown> = Record<string,
|
|
|
95
95
|
* For chat agents with message history, compaction, and conversation recording,
|
|
96
96
|
* extend {@link ChatAgent} instead.
|
|
97
97
|
*/
|
|
98
|
-
declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env,
|
|
98
|
+
declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env, TUserContext extends Record<string, unknown> = Record<string, unknown>> extends Agent$1<Env & AgentEnv> {
|
|
99
|
+
protected clientIp?: string;
|
|
100
|
+
protected forwardedFor?: string;
|
|
99
101
|
/**
|
|
100
102
|
* Override to enable JWT authentication on WebSocket connections.
|
|
101
103
|
* Return the auth config based on the incoming request, or undefined to skip auth.
|
|
@@ -105,16 +107,17 @@ declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env, TJwtId
|
|
|
105
107
|
*/
|
|
106
108
|
protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
|
|
107
109
|
/**
|
|
108
|
-
* The
|
|
109
|
-
* Define
|
|
110
|
+
* The user context for the session.
|
|
111
|
+
* Define getUserContext to set a user context.
|
|
110
112
|
*/
|
|
111
|
-
protected
|
|
113
|
+
protected get userContext(): TUserContext;
|
|
112
114
|
/**
|
|
113
115
|
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
114
|
-
*
|
|
115
|
-
* @
|
|
116
|
+
* This method should not have side effects - return a single object from it.
|
|
117
|
+
* @returns The user context from the request.
|
|
118
|
+
* @param jwtToken - A valid JWT token following authentication.
|
|
116
119
|
*/
|
|
117
|
-
protected
|
|
120
|
+
protected getUserContext?(jwtToken: string): Promise<TUserContext>;
|
|
118
121
|
/**
|
|
119
122
|
* Returns the user ID from the durable object name.
|
|
120
123
|
*/
|
|
@@ -158,7 +161,7 @@ interface MessageRating {
|
|
|
158
161
|
* Skill loading, compaction, and LLM calls use `buildLLMParams` from
|
|
159
162
|
* `@economic/agents` inside `onChatMessage`.
|
|
160
163
|
*/
|
|
161
|
-
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env,
|
|
164
|
+
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TUserContext extends Record<string, unknown> = Record<string, unknown>> extends AIChatAgent<Env & ChatAgentEnv> {
|
|
162
165
|
/**
|
|
163
166
|
* The binding of the Durable Object instance for this agent.
|
|
164
167
|
*/
|
|
@@ -203,22 +206,23 @@ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TJ
|
|
|
203
206
|
*/
|
|
204
207
|
protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
|
|
205
208
|
/**
|
|
206
|
-
* The
|
|
207
|
-
* Define
|
|
209
|
+
* The user context for the session.
|
|
210
|
+
* Define getUserContext to set a user context.
|
|
208
211
|
*/
|
|
209
|
-
protected get
|
|
212
|
+
protected get userContext(): TUserContext;
|
|
210
213
|
/**
|
|
211
214
|
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
212
215
|
* This method should not have side effects - return a single object from it.
|
|
213
|
-
* @returns The
|
|
214
|
-
* @param
|
|
216
|
+
* @returns The user context from the request.
|
|
217
|
+
* @param jwtToken - A valid JWT token following authentication.
|
|
215
218
|
*/
|
|
216
|
-
protected
|
|
219
|
+
protected getUserContext?(jwtToken: string): Promise<TUserContext>;
|
|
217
220
|
/**
|
|
218
221
|
* Returns the user ID from the durable object name.
|
|
219
222
|
*/
|
|
220
223
|
protected getUserId(): string;
|
|
221
224
|
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
225
|
+
protected _pendingUserContextRequest?: Promise<void>;
|
|
222
226
|
/**
|
|
223
227
|
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
224
228
|
* otherwise silently does nothing.
|
package/dist/index.mjs
CHANGED
|
@@ -473,11 +473,16 @@ async function verifyJwt(request, config) {
|
|
|
473
473
|
* extend {@link ChatAgent} instead.
|
|
474
474
|
*/
|
|
475
475
|
var Agent = class extends Agent$1 {
|
|
476
|
+
clientIp;
|
|
477
|
+
forwardedFor;
|
|
476
478
|
/**
|
|
477
|
-
* The
|
|
478
|
-
* Define
|
|
479
|
+
* The user context for the session.
|
|
480
|
+
* Define getUserContext to set a user context.
|
|
479
481
|
*/
|
|
480
|
-
|
|
482
|
+
get userContext() {
|
|
483
|
+
const { connection } = getCurrentAgent();
|
|
484
|
+
return (connection?.state)?.userContext ?? {};
|
|
485
|
+
}
|
|
481
486
|
/**
|
|
482
487
|
* Returns the user ID from the durable object name.
|
|
483
488
|
*/
|
|
@@ -511,7 +516,10 @@ var Agent = class extends Agent$1 {
|
|
|
511
516
|
return;
|
|
512
517
|
}
|
|
513
518
|
const token = extractTokenFromConnectRequest(ctx.request);
|
|
514
|
-
if (token)
|
|
519
|
+
if (token) {
|
|
520
|
+
const userContext = await this.getUserContext?.(token);
|
|
521
|
+
if (userContext) connection.setState({ userContext });
|
|
522
|
+
}
|
|
515
523
|
}
|
|
516
524
|
}
|
|
517
525
|
return super.onConnect(connection, ctx);
|
|
@@ -542,7 +550,7 @@ var Agent = class extends Agent$1 {
|
|
|
542
550
|
const experimental_context = {
|
|
543
551
|
...config.experimental_context,
|
|
544
552
|
...config.options?.body,
|
|
545
|
-
|
|
553
|
+
_userContext: this.userContext,
|
|
546
554
|
_logEvent: this.logEvent.bind(this)
|
|
547
555
|
};
|
|
548
556
|
const onFinish = async (event) => {
|
|
@@ -862,12 +870,12 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
862
870
|
*/
|
|
863
871
|
maxMessagesBeforeCompaction = 15;
|
|
864
872
|
/**
|
|
865
|
-
* The
|
|
866
|
-
* Define
|
|
873
|
+
* The user context for the session.
|
|
874
|
+
* Define getUserContext to set a user context.
|
|
867
875
|
*/
|
|
868
|
-
get
|
|
876
|
+
get userContext() {
|
|
869
877
|
const { connection } = getCurrentAgent();
|
|
870
|
-
return (connection?.state)?.
|
|
878
|
+
return (connection?.state)?.userContext ?? {};
|
|
871
879
|
}
|
|
872
880
|
/**
|
|
873
881
|
* Returns the user ID from the durable object name.
|
|
@@ -903,13 +911,13 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
903
911
|
}
|
|
904
912
|
}
|
|
905
913
|
const token = extractTokenFromConnectRequest(ctx.request);
|
|
906
|
-
if (token) {
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
}
|
|
914
|
+
if (token) this._pendingUserContextRequest = this.getUserContext?.(token).then((userContext) => {
|
|
915
|
+
if (userContext) connection.setState({ userContext });
|
|
916
|
+
});
|
|
910
917
|
}
|
|
911
918
|
return super.onConnect(connection, ctx);
|
|
912
919
|
}
|
|
920
|
+
_pendingUserContextRequest;
|
|
913
921
|
/**
|
|
914
922
|
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
915
923
|
* otherwise silently does nothing.
|
|
@@ -941,7 +949,7 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
941
949
|
const experimental_context = {
|
|
942
950
|
...config.experimental_context,
|
|
943
951
|
...config.options?.body,
|
|
944
|
-
|
|
952
|
+
_userContext: this.userContext,
|
|
945
953
|
_logEvent: this.logEvent.bind(this)
|
|
946
954
|
};
|
|
947
955
|
const messages = await convertToModelMessages(this.messages);
|
|
@@ -1053,6 +1061,7 @@ var ChatAgentHarness = class extends ChatAgent {
|
|
|
1053
1061
|
return [];
|
|
1054
1062
|
}
|
|
1055
1063
|
async onChatMessage(onFinish, options) {
|
|
1064
|
+
await this._pendingUserContextRequest;
|
|
1056
1065
|
const ctx = options?.body;
|
|
1057
1066
|
return streamText(await this.buildLLMParams({
|
|
1058
1067
|
options,
|