@economic/agents 1.6.3 → 1.6.4
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 +19 -18
- package/dist/index.mjs +12 -25
- 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>,
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
type AgentToolContext<TBody = Record<string, unknown>, TJwtIdentity = Record<string, unknown> | undefined> = TBody & {
|
|
61
|
+
_logEvent: (message: string, payload?: Record<string, unknown>) => void | Promise<void>;
|
|
62
|
+
_jwtIdentity?: TJwtIdentity;
|
|
63
63
|
};
|
|
64
64
|
interface AgentEnv {
|
|
65
65
|
AGENT_DB: D1Database;
|
|
@@ -95,12 +95,7 @@ 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> extends Agent$1<Env & AgentEnv> {
|
|
99
|
-
/**
|
|
100
|
-
* Verified session claims from JWT authentication.
|
|
101
|
-
* Set in `onConnect` when `getJwtAuthConfig` is implemented.
|
|
102
|
-
*/
|
|
103
|
-
private session?;
|
|
98
|
+
declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env, TJwtIdentity extends Record<string, unknown> = Record<string, unknown>> extends Agent$1<Env & AgentEnv> {
|
|
104
99
|
/**
|
|
105
100
|
* Override to enable JWT authentication on WebSocket connections.
|
|
106
101
|
* Return the auth config based on the incoming request, or undefined to skip auth.
|
|
@@ -109,6 +104,17 @@ declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env> extend
|
|
|
109
104
|
* @returns JWT auth config or undefined to skip authentication
|
|
110
105
|
*/
|
|
111
106
|
protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* The identity associated with the session.
|
|
109
|
+
* Define getIdentity to return the identity from the request.
|
|
110
|
+
*/
|
|
111
|
+
protected jwtIdentity: TJwtIdentity;
|
|
112
|
+
/**
|
|
113
|
+
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
114
|
+
* @returns The identity from the request.
|
|
115
|
+
* @param token - The token from the request.
|
|
116
|
+
*/
|
|
117
|
+
protected getJwtIdentity?(token: string): Promise<TJwtIdentity>;
|
|
112
118
|
/**
|
|
113
119
|
* Returns the user ID from the durable object name.
|
|
114
120
|
*/
|
|
@@ -152,7 +158,7 @@ interface MessageRating {
|
|
|
152
158
|
* Skill loading, compaction, and LLM calls use `buildLLMParams` from
|
|
153
159
|
* `@economic/agents` inside `onChatMessage`.
|
|
154
160
|
*/
|
|
155
|
-
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env,
|
|
161
|
+
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TJwtIdentity extends Record<string, unknown> = Record<string, unknown>> extends AIChatAgent<Env & ChatAgentEnv> {
|
|
156
162
|
/**
|
|
157
163
|
* The binding of the Durable Object instance for this agent.
|
|
158
164
|
*/
|
|
@@ -188,11 +194,6 @@ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TS
|
|
|
188
194
|
* Default is 15.
|
|
189
195
|
*/
|
|
190
196
|
protected maxMessagesBeforeCompaction?: number | undefined;
|
|
191
|
-
/**
|
|
192
|
-
* Verified session claims from JWT authentication.
|
|
193
|
-
* Set in `onConnect` when `getJwtAuthConfig` is implemented.
|
|
194
|
-
*/
|
|
195
|
-
private session?;
|
|
196
197
|
/**
|
|
197
198
|
* Override to enable JWT authentication on WebSocket connections.
|
|
198
199
|
* Return the auth config based on the incoming request, or undefined to skip auth.
|
|
@@ -205,13 +206,13 @@ declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TS
|
|
|
205
206
|
* The identity associated with the session.
|
|
206
207
|
* Define getIdentity to return the identity from the request.
|
|
207
208
|
*/
|
|
208
|
-
protected
|
|
209
|
+
protected jwtIdentity: TJwtIdentity;
|
|
209
210
|
/**
|
|
210
|
-
* Returns the identity
|
|
211
|
+
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
211
212
|
* @returns The identity from the request.
|
|
212
213
|
* @param token - The token from the request.
|
|
213
214
|
*/
|
|
214
|
-
protected
|
|
215
|
+
protected getJwtIdentity?(token: string): Promise<TJwtIdentity>;
|
|
215
216
|
/**
|
|
216
217
|
* Returns the user ID from the durable object name.
|
|
217
218
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -474,10 +474,10 @@ async function verifyJwt(request, config) {
|
|
|
474
474
|
*/
|
|
475
475
|
var Agent = class extends Agent$1 {
|
|
476
476
|
/**
|
|
477
|
-
*
|
|
478
|
-
*
|
|
477
|
+
* The identity associated with the session.
|
|
478
|
+
* Define getIdentity to return the identity from the request.
|
|
479
479
|
*/
|
|
480
|
-
|
|
480
|
+
jwtIdentity = {};
|
|
481
481
|
/**
|
|
482
482
|
* Returns the user ID from the durable object name.
|
|
483
483
|
*/
|
|
@@ -510,7 +510,8 @@ var Agent = class extends Agent$1 {
|
|
|
510
510
|
connection.close(result.status === 401 ? 4001 : 4003, result.message);
|
|
511
511
|
return;
|
|
512
512
|
}
|
|
513
|
-
|
|
513
|
+
const token = extractTokenFromConnectRequest(ctx.request);
|
|
514
|
+
if (token) this.jwtIdentity = await this.getJwtIdentity?.(token) ?? {};
|
|
514
515
|
}
|
|
515
516
|
}
|
|
516
517
|
return super.onConnect(connection, ctx);
|
|
@@ -541,8 +542,8 @@ var Agent = class extends Agent$1 {
|
|
|
541
542
|
const experimental_context = {
|
|
542
543
|
...config.experimental_context,
|
|
543
544
|
...config.options?.body,
|
|
544
|
-
|
|
545
|
-
|
|
545
|
+
_jwtIdentity: this.jwtIdentity,
|
|
546
|
+
_logEvent: this.logEvent.bind(this)
|
|
546
547
|
};
|
|
547
548
|
const onFinish = async (event) => {
|
|
548
549
|
this.logEvent("Turn completed", buildTurnLogPayload(event));
|
|
@@ -844,23 +845,10 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
844
845
|
*/
|
|
845
846
|
maxMessagesBeforeCompaction = 15;
|
|
846
847
|
/**
|
|
847
|
-
* Verified session claims from JWT authentication.
|
|
848
|
-
* Set in `onConnect` when `getJwtAuthConfig` is implemented.
|
|
849
|
-
*/
|
|
850
|
-
session;
|
|
851
|
-
/**
|
|
852
848
|
* The identity associated with the session.
|
|
853
849
|
* Define getIdentity to return the identity from the request.
|
|
854
850
|
*/
|
|
855
|
-
|
|
856
|
-
/**
|
|
857
|
-
* Returns the identity from the request.
|
|
858
|
-
* @returns The identity from the request.
|
|
859
|
-
* @param token - The token from the request.
|
|
860
|
-
*/
|
|
861
|
-
async getIdentity(_token) {
|
|
862
|
-
return Promise.resolve({});
|
|
863
|
-
}
|
|
851
|
+
jwtIdentity = {};
|
|
864
852
|
/**
|
|
865
853
|
* Returns the user ID from the durable object name.
|
|
866
854
|
*/
|
|
@@ -893,11 +881,10 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
893
881
|
connection.close(result.status === 401 ? 4001 : 4003, result.message);
|
|
894
882
|
return;
|
|
895
883
|
}
|
|
896
|
-
this.session = result.claims;
|
|
897
884
|
}
|
|
885
|
+
const token = extractTokenFromConnectRequest(ctx.request);
|
|
886
|
+
if (token) this.jwtIdentity = await this.getJwtIdentity?.(token) ?? {};
|
|
898
887
|
}
|
|
899
|
-
const token = extractTokenFromConnectRequest(ctx.request);
|
|
900
|
-
if (token) this.identity = await this.getIdentity(token);
|
|
901
888
|
return super.onConnect(connection, ctx);
|
|
902
889
|
}
|
|
903
890
|
/**
|
|
@@ -931,8 +918,8 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
931
918
|
const experimental_context = {
|
|
932
919
|
...config.experimental_context,
|
|
933
920
|
...config.options?.body,
|
|
934
|
-
|
|
935
|
-
|
|
921
|
+
_jwtIdentity: this.jwtIdentity,
|
|
922
|
+
_logEvent: this.logEvent.bind(this)
|
|
936
923
|
};
|
|
937
924
|
const messages = await convertToModelMessages(this.messages);
|
|
938
925
|
const fastModel = this.getFastModel();
|