@economic/agents 2.1.7 → 2.2.1
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/README.md +10 -8
- package/dist/index.d.mts +198 -220
- package/dist/index.mjs +575 -785
- package/dist/{telemetry-BDxmv_R7.mjs → route-agent-request-DKDvDYnR.mjs} +20 -6
- package/dist/{index-DzOC3mNl.d.mts → route-agent-request-DmwIOBJS.d.mts} +6 -2
- package/dist/v1.d.mts +275 -0
- package/dist/v1.mjs +918 -0
- package/package.json +2 -2
- package/dist/v2.d.mts +0 -256
- package/dist/v2.mjs +0 -721
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { routeAgentRequest } from "agents";
|
|
1
2
|
import { createRemoteJWKSet, decodeJwt, errors, jwtVerify } from "jose";
|
|
2
3
|
import { BasicTracerProvider, SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
3
|
-
//#region src/server/
|
|
4
|
+
//#region src/server/features/auth/index.ts
|
|
4
5
|
const jwksByIssuer = /* @__PURE__ */ new Map();
|
|
5
6
|
function getJwksForIssuer(issuer) {
|
|
6
7
|
const normalized = issuer.replace(/\/$/, "");
|
|
@@ -100,7 +101,7 @@ async function verifyJwt(request, config) {
|
|
|
100
101
|
};
|
|
101
102
|
}
|
|
102
103
|
//#endregion
|
|
103
|
-
//#region src/server/
|
|
104
|
+
//#region src/server/features/telemetry/utils.ts
|
|
104
105
|
function durationMs(duration) {
|
|
105
106
|
return duration[0] * 1e3 + duration[1] / 1e6;
|
|
106
107
|
}
|
|
@@ -121,7 +122,7 @@ function parseJson(value) {
|
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
//#endregion
|
|
124
|
-
//#region src/server/
|
|
125
|
+
//#region src/server/features/telemetry/audit-logs.ts
|
|
125
126
|
function safePathSegment(value, fallback) {
|
|
126
127
|
return (value || fallback).replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
127
128
|
}
|
|
@@ -266,7 +267,7 @@ async function handleAuditSpan(span, auditLogs, context) {
|
|
|
266
267
|
console.log("[AuditLog] Created", auditLog.id);
|
|
267
268
|
}
|
|
268
269
|
//#endregion
|
|
269
|
-
//#region src/server/
|
|
270
|
+
//#region src/server/features/telemetry/analytics.ts
|
|
270
271
|
function writeAnalyticsDatapoint(analytics, dataPoint) {
|
|
271
272
|
if (!analytics) return;
|
|
272
273
|
try {
|
|
@@ -332,7 +333,7 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
332
333
|
}
|
|
333
334
|
}
|
|
334
335
|
//#endregion
|
|
335
|
-
//#region src/server/
|
|
336
|
+
//#region src/server/features/telemetry/index.ts
|
|
336
337
|
var AgentSpanExporter = class {
|
|
337
338
|
auditLogs;
|
|
338
339
|
analytics;
|
|
@@ -369,4 +370,17 @@ function createAgentTracer(auditLogs, analytics, context) {
|
|
|
369
370
|
return new BasicTracerProvider({ spanProcessors: [new SimpleSpanProcessor(new AgentSpanExporter(auditLogs, analytics, context))] }).getTracer("@economic/agents");
|
|
370
371
|
}
|
|
371
372
|
//#endregion
|
|
372
|
-
|
|
373
|
+
//#region src/server/route-agent-request.ts
|
|
374
|
+
async function routeAgentRequest$1(request, env, options) {
|
|
375
|
+
const response = await routeAgentRequest(request, env, options);
|
|
376
|
+
if (!response) return null;
|
|
377
|
+
const protocol = request.headers.get("Sec-WebSocket-Protocol");
|
|
378
|
+
if (response.status === 101 && protocol) {
|
|
379
|
+
const newResponse = new Response(null, response);
|
|
380
|
+
newResponse.headers.set("Sec-WebSocket-Protocol", protocol.split(",")[0].trim());
|
|
381
|
+
return newResponse;
|
|
382
|
+
}
|
|
383
|
+
return response;
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
export { verifyJwt as i, createAgentTracer as n, extractTokenFromConnectRequest as r, routeAgentRequest$1 as t };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { AgentOptions } from "agents";
|
|
1
2
|
import { JWTPayload } from "jose";
|
|
2
3
|
|
|
3
|
-
//#region src/server/
|
|
4
|
+
//#region src/server/features/auth/index.d.ts
|
|
4
5
|
interface JwtAuthConfig<TClaims extends Record<string, unknown> = Record<string, unknown>> {
|
|
5
6
|
/** Issuers whose tokens are accepted (exact string or RegExp). */
|
|
6
7
|
allowedIssuers: readonly (string | RegExp)[];
|
|
@@ -15,4 +16,7 @@ interface JwtAuthConfig<TClaims extends Record<string, unknown> = Record<string,
|
|
|
15
16
|
getClaims: (payload: JWTPayload) => TClaims;
|
|
16
17
|
}
|
|
17
18
|
//#endregion
|
|
18
|
-
|
|
19
|
+
//#region src/server/route-agent-request.d.ts
|
|
20
|
+
declare function routeAgentRequest$1<Env>(request: Request, env: Env, options?: AgentOptions<Env>): Promise<Response | null>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { JwtAuthConfig as n, routeAgentRequest$1 as t };
|
package/dist/v1.d.mts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { n as JwtAuthConfig, t as routeAgentRequest } from "./route-agent-request-DmwIOBJS.mjs";
|
|
2
|
+
import { LanguageModel, StreamTextOnFinishCallback, ToolSet, UIMessage, generateText, streamText } from "ai";
|
|
3
|
+
import { Agent as Agent$1, Connection, ConnectionContext } from "agents";
|
|
4
|
+
import { AIChatAgent, ChatResponseResult, OnChatMessageOptions } from "@cloudflare/ai-chat";
|
|
5
|
+
|
|
6
|
+
//#region src/server/features/skills/index.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* A named group of related tools that can be loaded together on demand.
|
|
9
|
+
*
|
|
10
|
+
* The agent starts with only its always-on tools active. When the LLM calls
|
|
11
|
+
* activate_skill with a skill name, that skill's tools become available for
|
|
12
|
+
* the rest of the conversation.
|
|
13
|
+
*/
|
|
14
|
+
interface Skill {
|
|
15
|
+
name: string;
|
|
16
|
+
/** One-line description shown in the activate_skill tool schema */
|
|
17
|
+
description: string;
|
|
18
|
+
/**
|
|
19
|
+
* Guidance text for this skill — e.g. rate limits, preferred patterns,
|
|
20
|
+
* when to use each tool. Injected into the `## Tools` section of the
|
|
21
|
+
* system prompt via `buildSystemPrompt` in `llm.ts` whenever this skill
|
|
22
|
+
* is loaded.
|
|
23
|
+
*/
|
|
24
|
+
guidance?: string;
|
|
25
|
+
tools: ToolSet;
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/server/util/llm.d.ts
|
|
29
|
+
type LLMParams = Parameters<typeof streamText>[0] & Parameters<typeof generateText>[0];
|
|
30
|
+
type BuildLLMParamsConfig = Omit<LLMParams, "prompt"> & {
|
|
31
|
+
/** Skill names loaded in previous turns. Pass `await this.getLoadedSkills()`. */activeSkills?: string[]; /** Skills available for on-demand loading this turn. */
|
|
32
|
+
skills?: Skill[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Builds the parameter object for a Vercel AI SDK `streamText` or `generateText` call.
|
|
36
|
+
*
|
|
37
|
+
* Handles skill wiring (`activate_skill`, `list_capabilities`, `prepareStep`).
|
|
38
|
+
*
|
|
39
|
+
* The returned object can be spread directly into `streamText` or `generateText`:
|
|
40
|
+
*
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const params = buildLLMParams({ ... });
|
|
43
|
+
* return streamText(params).toUIMessageStreamResponse();
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function buildLLMParams(config: BuildLLMParamsConfig): LLMParams;
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/server/v1/types.d.ts
|
|
49
|
+
/**
|
|
50
|
+
* The context object available throughout an agent's lifetime — passed via
|
|
51
|
+
* `experimental_context` to tool `execute` functions.
|
|
52
|
+
*
|
|
53
|
+
* Define your own body shape and compose:
|
|
54
|
+
* ```typescript
|
|
55
|
+
* interface MyBody { userTier: "free" | "pro" }
|
|
56
|
+
* type MyContext = AgentToolContext<MyBody>;
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
type AgentToolContext<TBody = Record<string, unknown>, TUserContext = Record<string, unknown> | undefined> = TBody & {
|
|
60
|
+
_userContext?: TUserContext;
|
|
61
|
+
};
|
|
62
|
+
interface AgentEnv {
|
|
63
|
+
AGENT_DB: D1Database;
|
|
64
|
+
AGENTS_AUDIT_LOGS: R2Bucket;
|
|
65
|
+
AGENTS_ANALYTICS: AnalyticsEngineDataset;
|
|
66
|
+
}
|
|
67
|
+
interface ChatAgentEnv extends AgentEnv {}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/server/v1/agent/Agent.d.ts
|
|
70
|
+
/**
|
|
71
|
+
* Base agent for Cloudflare Agents SDK Durable Objects with lazy skill loading
|
|
72
|
+
* and `buildLLMParams` wiring.
|
|
73
|
+
*
|
|
74
|
+
* Handles CF infrastructure concerns: DO SQLite persistence for loaded skill state.
|
|
75
|
+
*
|
|
76
|
+
* For chat agents with message history, compaction, and conversation recording,
|
|
77
|
+
* extend {@link ChatAgent} instead.
|
|
78
|
+
*/
|
|
79
|
+
declare abstract class Agent<Env extends Cloudflare.Env = Cloudflare.Env, TUserContext extends Record<string, unknown> = Record<string, unknown>> extends Agent$1<Env & AgentEnv> {
|
|
80
|
+
protected clientIp?: string;
|
|
81
|
+
protected forwardedFor?: string;
|
|
82
|
+
/**
|
|
83
|
+
* Override to enable JWT authentication on WebSocket connections.
|
|
84
|
+
* Return the auth config based on the incoming request, or undefined to skip auth.
|
|
85
|
+
*
|
|
86
|
+
* @param request - The WebSocket upgrade request
|
|
87
|
+
* @returns JWT auth config or undefined to skip authentication
|
|
88
|
+
*/
|
|
89
|
+
protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* The user context for the session.
|
|
92
|
+
* Define getUserContext to set a user context.
|
|
93
|
+
*/
|
|
94
|
+
protected get userContext(): TUserContext;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
97
|
+
* This method should not have side effects - return a single object from it.
|
|
98
|
+
* @returns The user context from the request.
|
|
99
|
+
* @param jwtToken - A valid JWT token following authentication.
|
|
100
|
+
*/
|
|
101
|
+
protected getUserContext?(jwtToken: string): Promise<TUserContext>;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the user ID from the durable object name.
|
|
104
|
+
*/
|
|
105
|
+
protected getUserId(): string;
|
|
106
|
+
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Builds the parameter object for a `streamText` or `generateText` call,
|
|
109
|
+
* pre-filling `activeSkills` from this agent instance.
|
|
110
|
+
*/
|
|
111
|
+
protected buildLLMParams<TBody = Record<string, unknown>>(config: Omit<BuildLLMParamsConfig, "messages"> & {
|
|
112
|
+
options?: OnChatMessageOptions;
|
|
113
|
+
}): Promise<LLMParams>;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/server/v1/agent-chat/features/conversations/rating.d.ts
|
|
117
|
+
interface MessageRating {
|
|
118
|
+
rating: number;
|
|
119
|
+
comment?: string;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/server/v1/agent-chat/ChatAgent.d.ts
|
|
123
|
+
/**
|
|
124
|
+
* Chat agent for Cloudflare Agents SDK: lazy skill loading, message persistence,
|
|
125
|
+
* compaction, and conversation metadata in D1.
|
|
126
|
+
*
|
|
127
|
+
* Handles CF infrastructure concerns: DO SQLite for loaded skill state,
|
|
128
|
+
* stripping skill meta-tool messages before persistence, and history replay to
|
|
129
|
+
* newly connected clients.
|
|
130
|
+
*
|
|
131
|
+
* Skill loading, compaction, and LLM calls use `buildLLMParams` from
|
|
132
|
+
* `@economic/agents` inside `onChatMessage`.
|
|
133
|
+
*/
|
|
134
|
+
declare abstract class ChatAgent<Env extends Cloudflare.Env = Cloudflare.Env, TUserContext extends Record<string, unknown> = Record<string, unknown>> extends AIChatAgent<Env & ChatAgentEnv> {
|
|
135
|
+
initialState: {
|
|
136
|
+
status: string;
|
|
137
|
+
type: string;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The binding of the Durable Object instance for this agent.
|
|
141
|
+
*/
|
|
142
|
+
protected abstract get binding(): {
|
|
143
|
+
getByName(name: string): {
|
|
144
|
+
destroy(): Promise<void>;
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Fast/cheap language model used for background tasks: compaction and conversation summarization.
|
|
149
|
+
*
|
|
150
|
+
* Declare this on every subclass:
|
|
151
|
+
*
|
|
152
|
+
* ```typescript
|
|
153
|
+
* protected fastModel = google("gemini-2.0-flash");
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* To disable compaction for a specific call, pass `maxMessagesBeforeCompaction: undefined`
|
|
157
|
+
* to `buildLLMParams` rather than omitting or nulling out `fastModel`.
|
|
158
|
+
*/
|
|
159
|
+
protected abstract getFastModel(): LanguageModel;
|
|
160
|
+
/**
|
|
161
|
+
* Number of days of inactivity before the full conversation is deleted.
|
|
162
|
+
*
|
|
163
|
+
* Leave `undefined` to disable automatic retention cleanup.
|
|
164
|
+
*/
|
|
165
|
+
protected conversationRetentionDays?: number;
|
|
166
|
+
/**
|
|
167
|
+
* Number of recent messages to keep verbatim when compaction runs.
|
|
168
|
+
* Older messages beyond this count are summarised into a single system message.
|
|
169
|
+
* Used as the default when `maxMessagesBeforeCompaction` is not provided to `buildLLMParams`.
|
|
170
|
+
*
|
|
171
|
+
* Default is 15.
|
|
172
|
+
*/
|
|
173
|
+
protected maxMessagesBeforeCompaction?: number | undefined;
|
|
174
|
+
protected clientIp?: string;
|
|
175
|
+
protected forwardedFor?: string;
|
|
176
|
+
/**
|
|
177
|
+
* Override to enable JWT authentication on WebSocket connections.
|
|
178
|
+
* Return the auth config based on the incoming request, or undefined to skip auth.
|
|
179
|
+
*
|
|
180
|
+
* @param request - The WebSocket upgrade request
|
|
181
|
+
* @returns JWT auth config or undefined to skip authentication
|
|
182
|
+
*/
|
|
183
|
+
protected getJwtAuthConfig?(request: Request): JwtAuthConfig<Record<string, unknown>> | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* The user context for the session.
|
|
186
|
+
* Define getUserContext to set a user context.
|
|
187
|
+
*/
|
|
188
|
+
protected get userContext(): TUserContext;
|
|
189
|
+
/**
|
|
190
|
+
* Returns the identity following verification of the JWT token - getJwtAuthConfig is required.
|
|
191
|
+
* This method should not have side effects - return a single object from it.
|
|
192
|
+
* @returns The user context from the request.
|
|
193
|
+
* @param jwtToken - A valid JWT token following authentication.
|
|
194
|
+
*/
|
|
195
|
+
protected getUserContext?(jwtToken: string): Promise<TUserContext>;
|
|
196
|
+
/**
|
|
197
|
+
* Returns the user ID from the durable object name.
|
|
198
|
+
*/
|
|
199
|
+
protected getUserId(): string;
|
|
200
|
+
onStart(): void;
|
|
201
|
+
onClose(): Promise<void>;
|
|
202
|
+
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
203
|
+
protected _pendingUserContextRequest?: Promise<void>;
|
|
204
|
+
/**
|
|
205
|
+
* Builds the parameter object for a `streamText` or `generateText` call,
|
|
206
|
+
* pre-filling `messages`, `activeSkills`, and `fastModel` from this agent instance.
|
|
207
|
+
*
|
|
208
|
+
* **Compaction** runs automatically when `fastModel` is set on the class, using
|
|
209
|
+
* `DEFAULT_MAX_MESSAGES_BEFORE_COMPACTION` (30) as the threshold. Override the
|
|
210
|
+
* threshold by setting `maxMessagesBeforeCompaction` on the class. Disable compaction
|
|
211
|
+
* entirely by setting `maxMessagesBeforeCompaction = undefined` explicitly.
|
|
212
|
+
*/
|
|
213
|
+
protected buildLLMParams<TBody = Record<string, unknown>>(config: BuildLLMParamsConfig & {
|
|
214
|
+
options?: OnChatMessageOptions;
|
|
215
|
+
}): Promise<LLMParams>;
|
|
216
|
+
persistMessages(messages: UIMessage[], excludeBroadcastIds?: string[], options?: {
|
|
217
|
+
_deleteStaleRows?: boolean;
|
|
218
|
+
}): Promise<void>;
|
|
219
|
+
protected onChatResponse(result: ChatResponseResult): Promise<void>;
|
|
220
|
+
rateMessage(messageId: string, rating: number, comment?: string): Promise<void>;
|
|
221
|
+
getMessageRatings(): Promise<Record<string, MessageRating>>;
|
|
222
|
+
getConversations(): Promise<Record<string, unknown>[]>;
|
|
223
|
+
/**
|
|
224
|
+
* Exports this conversation's persisted message history for the v1 -> v2
|
|
225
|
+
* migration. Called over DO RPC by the v2 `Assistant` while migrating a
|
|
226
|
+
* user's chats into facets. `this.messages` is loaded from the
|
|
227
|
+
* `cf_ai_chat_agent_messages` table when the DO wakes.
|
|
228
|
+
*
|
|
229
|
+
* Read-only: does not mutate or delete any state.
|
|
230
|
+
*/
|
|
231
|
+
exportForMigration(): Promise<{
|
|
232
|
+
messages: UIMessage[];
|
|
233
|
+
}>;
|
|
234
|
+
deleteConversation(id: string): Promise<boolean>;
|
|
235
|
+
destroy(): Promise<void>;
|
|
236
|
+
private deleteConversationCallback;
|
|
237
|
+
private scheduleConversationForAutoDeletion;
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/server/v1/agent-chat/ChatAgentHarness.d.ts
|
|
241
|
+
declare abstract class ChatAgentHarness<Env extends Cloudflare.Env, RequestBody extends Record<string, unknown> = Record<string, unknown>, SessionIdentity extends Record<string, unknown> = Record<string, unknown>> extends ChatAgent<Env, SessionIdentity> {
|
|
242
|
+
get binding(): {
|
|
243
|
+
getByName(name: string): {
|
|
244
|
+
destroy(): Promise<void>;
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
conversationRetentionDays: number;
|
|
248
|
+
/**
|
|
249
|
+
* Returns the model for the agent.
|
|
250
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
251
|
+
* @returns The model for the agent.
|
|
252
|
+
*/
|
|
253
|
+
abstract getModel(ctx: AgentToolContext<RequestBody>): LanguageModel;
|
|
254
|
+
/**
|
|
255
|
+
* Returns the system prompt for the agent.
|
|
256
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
257
|
+
* @returns The system prompt for the agent.
|
|
258
|
+
*/
|
|
259
|
+
abstract getSystemPrompt(ctx: AgentToolContext<RequestBody>): string;
|
|
260
|
+
/**
|
|
261
|
+
* Returns the tools for the agent.
|
|
262
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
263
|
+
* @returns The tools for the agent.
|
|
264
|
+
*/
|
|
265
|
+
getTools(_ctx: AgentToolContext<RequestBody>): ToolSet;
|
|
266
|
+
/**
|
|
267
|
+
* Returns the skills for the agent.
|
|
268
|
+
* @param ctx - The context object for the agent built from the request body.
|
|
269
|
+
* @returns The skills for the agent.
|
|
270
|
+
*/
|
|
271
|
+
getSkills(_ctx: AgentToolContext<RequestBody>): Skill[];
|
|
272
|
+
onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>, options?: OnChatMessageOptions): Promise<Response>;
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
export { Agent, type AgentToolContext, type BuildLLMParamsConfig, ChatAgent, ChatAgentHarness, type Skill, buildLLMParams, routeAgentRequest };
|