@economic/agents 1.7.1 → 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 +21 -16
- package/dist/index.mjs +26 -11
- 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,21 +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
|
|
212
|
+
protected get userContext(): TUserContext;
|
|
210
213
|
/**
|
|
211
214
|
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
212
|
-
*
|
|
213
|
-
* @
|
|
215
|
+
* This method should not have side effects - return a single object from it.
|
|
216
|
+
* @returns The user context from the request.
|
|
217
|
+
* @param jwtToken - A valid JWT token following authentication.
|
|
214
218
|
*/
|
|
215
|
-
protected
|
|
219
|
+
protected getUserContext?(jwtToken: string): Promise<TUserContext>;
|
|
216
220
|
/**
|
|
217
221
|
* Returns the user ID from the durable object name.
|
|
218
222
|
*/
|
|
219
223
|
protected getUserId(): string;
|
|
220
224
|
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
225
|
+
protected _pendingUserContextRequest?: Promise<void>;
|
|
221
226
|
/**
|
|
222
227
|
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
223
228
|
* otherwise silently does nothing.
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
|
|
2
|
-
import { Agent as Agent$1, callable, routeAgentRequest as routeAgentRequest$1 } from "agents";
|
|
2
|
+
import { Agent as Agent$1, callable, getCurrentAgent, routeAgentRequest as routeAgentRequest$1 } from "agents";
|
|
3
3
|
import { AIChatAgent } from "@cloudflare/ai-chat";
|
|
4
4
|
import { createRemoteJWKSet, decodeJwt, errors, jwtVerify } from "jose";
|
|
5
5
|
//#region src/server/shared/features/skills/index.ts
|
|
@@ -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,10 +870,13 @@ 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
|
-
|
|
876
|
+
get userContext() {
|
|
877
|
+
const { connection } = getCurrentAgent();
|
|
878
|
+
return (connection?.state)?.userContext ?? {};
|
|
879
|
+
}
|
|
869
880
|
/**
|
|
870
881
|
* Returns the user ID from the durable object name.
|
|
871
882
|
*/
|
|
@@ -900,10 +911,13 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
900
911
|
}
|
|
901
912
|
}
|
|
902
913
|
const token = extractTokenFromConnectRequest(ctx.request);
|
|
903
|
-
if (token) this.
|
|
914
|
+
if (token) this._pendingUserContextRequest = this.getUserContext?.(token).then((userContext) => {
|
|
915
|
+
if (userContext) connection.setState({ userContext });
|
|
916
|
+
});
|
|
904
917
|
}
|
|
905
918
|
return super.onConnect(connection, ctx);
|
|
906
919
|
}
|
|
920
|
+
_pendingUserContextRequest;
|
|
907
921
|
/**
|
|
908
922
|
* Writes an audit event to D1 if `AGENT_DB` is bound on the environment,
|
|
909
923
|
* otherwise silently does nothing.
|
|
@@ -935,7 +949,7 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
935
949
|
const experimental_context = {
|
|
936
950
|
...config.experimental_context,
|
|
937
951
|
...config.options?.body,
|
|
938
|
-
|
|
952
|
+
_userContext: this.userContext,
|
|
939
953
|
_logEvent: this.logEvent.bind(this)
|
|
940
954
|
};
|
|
941
955
|
const messages = await convertToModelMessages(this.messages);
|
|
@@ -1047,6 +1061,7 @@ var ChatAgentHarness = class extends ChatAgent {
|
|
|
1047
1061
|
return [];
|
|
1048
1062
|
}
|
|
1049
1063
|
async onChatMessage(onFinish, options) {
|
|
1064
|
+
await this._pendingUserContextRequest;
|
|
1050
1065
|
const ctx = options?.body;
|
|
1051
1066
|
return streamText(await this.buildLLMParams({
|
|
1052
1067
|
options,
|