@economic/agents 2.1.1 → 2.1.2
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.mjs +9 -9
- package/dist/{telemetry-BKgBMVF0.mjs → telemetry-BDxmv_R7.mjs} +23 -23
- package/dist/v2.d.mts +2 -2
- package/dist/v2.mjs +45 -15
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as extractTokenFromConnectRequest, r as verifyJwt, t as createAgentTracer } from "./telemetry-
|
|
1
|
+
import { n as extractTokenFromConnectRequest, r as verifyJwt, t as createAgentTracer } from "./telemetry-BDxmv_R7.mjs";
|
|
2
2
|
import { Output, convertToModelMessages, generateText, jsonSchema, stepCountIs, streamText, tool } from "ai";
|
|
3
3
|
import { Agent as Agent$1, callable, getCurrentAgent, routeAgentRequest as routeAgentRequest$1 } from "agents";
|
|
4
4
|
import { AIChatAgent } from "@cloudflare/ai-chat";
|
|
@@ -359,16 +359,16 @@ var Agent = class extends Agent$1 {
|
|
|
359
359
|
isEnabled: true,
|
|
360
360
|
tracer: createAgentTracer(this.env.AGENTS_AUDIT_LOGS, this.env.AGENTS_ANALYTICS, {
|
|
361
361
|
agentName: this.constructor.name,
|
|
362
|
-
|
|
363
|
-
|
|
362
|
+
durableObjectName: this.name,
|
|
363
|
+
actorId: this.getUserId(),
|
|
364
364
|
...this.clientIp ? { clientIp: this.clientIp } : {},
|
|
365
365
|
...this.forwardedFor ? { forwardedFor: this.forwardedFor } : {}
|
|
366
366
|
}),
|
|
367
367
|
metadata: {
|
|
368
368
|
agentName: this.constructor.name,
|
|
369
369
|
version: "v1",
|
|
370
|
-
|
|
371
|
-
|
|
370
|
+
durableObjectName: this.name,
|
|
371
|
+
actorId: this.getUserId(),
|
|
372
372
|
...this.clientIp ? { clientIp: this.clientIp } : {},
|
|
373
373
|
...this.forwardedFor ? { forwardedFor: this.forwardedFor } : {},
|
|
374
374
|
...config.experimental_telemetry?.metadata
|
|
@@ -769,16 +769,16 @@ var ChatAgent = class extends AIChatAgent {
|
|
|
769
769
|
isEnabled: true,
|
|
770
770
|
tracer: createAgentTracer(this.env.AGENTS_AUDIT_LOGS, this.env.AGENTS_ANALYTICS, {
|
|
771
771
|
agentName: this.constructor.name,
|
|
772
|
-
|
|
773
|
-
|
|
772
|
+
durableObjectName: this.name,
|
|
773
|
+
actorId: this.getUserId(),
|
|
774
774
|
...this.clientIp ? { clientIp: this.clientIp } : {},
|
|
775
775
|
...this.forwardedFor ? { forwardedFor: this.forwardedFor } : {}
|
|
776
776
|
}),
|
|
777
777
|
metadata: {
|
|
778
778
|
agentName: this.constructor.name,
|
|
779
779
|
version: "v1",
|
|
780
|
-
|
|
781
|
-
|
|
780
|
+
durableObjectName: this.name,
|
|
781
|
+
actorId: this.getUserId(),
|
|
782
782
|
...this.clientIp ? { clientIp: this.clientIp } : {},
|
|
783
783
|
...this.forwardedFor ? { forwardedFor: this.forwardedFor } : {},
|
|
784
784
|
...config.experimental_telemetry?.metadata
|
|
@@ -125,12 +125,12 @@ function parseJson(value) {
|
|
|
125
125
|
function safePathSegment(value, fallback) {
|
|
126
126
|
return (value || fallback).replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
127
127
|
}
|
|
128
|
-
function createAuditLogKey(agentName,
|
|
128
|
+
function createAuditLogKey(agentName, durableObjectName) {
|
|
129
129
|
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replaceAll(":", "-");
|
|
130
130
|
const id = crypto.randomUUID().slice(0, 8);
|
|
131
131
|
return [
|
|
132
132
|
safePathSegment(agentName, "unknown-agent"),
|
|
133
|
-
safePathSegment(
|
|
133
|
+
safePathSegment(durableObjectName, "unknown-chat"),
|
|
134
134
|
`${timestamp}-${id}.json`
|
|
135
135
|
].join("/");
|
|
136
136
|
}
|
|
@@ -208,34 +208,34 @@ function rememberToolCall(span) {
|
|
|
208
208
|
if (parentSpanId) pushToolCall(toolCallsByParentSpan, parentSpanId, toolCall);
|
|
209
209
|
pendingToolCalls.push(toolCall);
|
|
210
210
|
}
|
|
211
|
-
function attachToolCallsToChat(span,
|
|
211
|
+
function attachToolCallsToChat(span, durableObjectName) {
|
|
212
212
|
const spanId = span.spanContext().spanId;
|
|
213
213
|
const toolCalls = toolCallsByParentSpan.get(spanId) ?? pendingToolCalls.splice(0);
|
|
214
214
|
if (!toolCalls.length) return;
|
|
215
215
|
toolCallsByParentSpan.delete(spanId);
|
|
216
|
-
const currentSkill = currentSkillByChat.get(
|
|
216
|
+
const currentSkill = currentSkillByChat.get(durableObjectName);
|
|
217
217
|
const attributedToolCalls = toolCalls.map((toolCall) => {
|
|
218
218
|
const skillName = toolCall.skillName ?? currentSkill;
|
|
219
|
-
if (toolCall.skillName) currentSkillByChat.set(
|
|
219
|
+
if (toolCall.skillName) currentSkillByChat.set(durableObjectName, toolCall.skillName);
|
|
220
220
|
return {
|
|
221
221
|
...toolCall,
|
|
222
222
|
...skillName ? { skillName } : {}
|
|
223
223
|
};
|
|
224
224
|
});
|
|
225
|
-
toolCallsByChat.set(
|
|
225
|
+
toolCallsByChat.set(durableObjectName, [...toolCallsByChat.get(durableObjectName) ?? [], ...attributedToolCalls]);
|
|
226
226
|
}
|
|
227
227
|
function buildAuditLog(span, context) {
|
|
228
|
-
const lastDoStream = lastDoStreamByChat.get(context.
|
|
229
|
-
lastDoStreamByChat.delete(context.
|
|
228
|
+
const lastDoStream = lastDoStreamByChat.get(context.durableObjectName);
|
|
229
|
+
lastDoStreamByChat.delete(context.durableObjectName);
|
|
230
230
|
const promptMessages = parseJson(stringAttribute(lastDoStream ?? span, "ai.prompt.messages"));
|
|
231
231
|
const fallbackPrompt = parseJson(stringAttribute(span, "ai.prompt"));
|
|
232
232
|
const inputMessages = promptMessages ?? fallbackPrompt?.messages ?? [];
|
|
233
|
-
const spanToolCalls = toolCallsByChat.get(context.
|
|
234
|
-
toolCallsByChat.delete(context.
|
|
233
|
+
const spanToolCalls = toolCallsByChat.get(context.durableObjectName) ?? [];
|
|
234
|
+
toolCallsByChat.delete(context.durableObjectName);
|
|
235
235
|
return {
|
|
236
|
-
id: createAuditLogKey(context.agentName, context.
|
|
236
|
+
id: createAuditLogKey(context.agentName, context.durableObjectName),
|
|
237
237
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
238
|
-
|
|
238
|
+
durableObjectName: context.durableObjectName,
|
|
239
239
|
agent: {
|
|
240
240
|
name: context.agentName,
|
|
241
241
|
llm: {
|
|
@@ -244,7 +244,7 @@ function buildAuditLog(span, context) {
|
|
|
244
244
|
}
|
|
245
245
|
},
|
|
246
246
|
actor: {
|
|
247
|
-
|
|
247
|
+
id: context.actorId,
|
|
248
248
|
ip: {
|
|
249
249
|
client: context.clientIp ?? context.forwardedFor?.split(",").map((ip) => ip.trim()).filter(Boolean)[0],
|
|
250
250
|
forwardedFor: context.forwardedFor?.split(",").map((ip) => ip.trim()).filter(Boolean) ?? []
|
|
@@ -256,8 +256,8 @@ function buildAuditLog(span, context) {
|
|
|
256
256
|
tools: [...extractTools(inputMessages), ...spanToolCalls]
|
|
257
257
|
};
|
|
258
258
|
}
|
|
259
|
-
function rememberDoStream(span,
|
|
260
|
-
lastDoStreamByChat.set(
|
|
259
|
+
function rememberDoStream(span, durableObjectName) {
|
|
260
|
+
lastDoStreamByChat.set(durableObjectName, span);
|
|
261
261
|
}
|
|
262
262
|
async function handleAuditSpan(span, auditLogs, context) {
|
|
263
263
|
const auditLog = buildAuditLog(span, context);
|
|
@@ -280,11 +280,11 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
280
280
|
const promptMessages = parseJson(stringAttribute(span, "ai.prompt.messages"));
|
|
281
281
|
const responseText = stringAttribute(span, "ai.response.text") ?? "";
|
|
282
282
|
writeAnalyticsDatapoint(analytics, {
|
|
283
|
-
indexes: [stringAttribute(span, "ai.telemetry.metadata.
|
|
283
|
+
indexes: [stringAttribute(span, "ai.telemetry.metadata.actorId") ?? ""],
|
|
284
284
|
blobs: [
|
|
285
285
|
"llm_call",
|
|
286
286
|
stringAttribute(span, "ai.telemetry.metadata.agentName") ?? "",
|
|
287
|
-
stringAttribute(span, "ai.telemetry.metadata.
|
|
287
|
+
stringAttribute(span, "ai.telemetry.metadata.durableObjectName") ?? "",
|
|
288
288
|
stringAttribute(span, "ai.model.id") ?? "",
|
|
289
289
|
stringAttribute(span, "ai.model.provider") ?? "",
|
|
290
290
|
stringAttribute(span, "ai.response.finishReason") ?? ""
|
|
@@ -305,15 +305,15 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
305
305
|
if (span.name === "ai.toolCall") {
|
|
306
306
|
const toolName = stringAttribute(span, "ai.toolCall.name");
|
|
307
307
|
const toolInput = parseJson(stringAttribute(span, "ai.toolCall.args"));
|
|
308
|
-
const
|
|
309
|
-
const skillName = extractSkillName(toolName, toolInput) ?? currentSkillByChat.get(
|
|
308
|
+
const durableObjectName = stringAttribute(span, "ai.telemetry.metadata.durableObjectName") ?? "";
|
|
309
|
+
const skillName = extractSkillName(toolName, toolInput) ?? currentSkillByChat.get(durableObjectName) ?? "";
|
|
310
310
|
const success = span.status.code === 0;
|
|
311
311
|
writeAnalyticsDatapoint(analytics, {
|
|
312
|
-
indexes: [stringAttribute(span, "ai.telemetry.metadata.
|
|
312
|
+
indexes: [stringAttribute(span, "ai.telemetry.metadata.actorId") ?? ""],
|
|
313
313
|
blobs: [
|
|
314
314
|
"tool_call",
|
|
315
315
|
stringAttribute(span, "ai.telemetry.metadata.agentName") ?? "",
|
|
316
|
-
|
|
316
|
+
durableObjectName,
|
|
317
317
|
toolName ?? "",
|
|
318
318
|
skillName,
|
|
319
319
|
success ? "success" : "error"
|
|
@@ -346,8 +346,8 @@ var AgentSpanExporter = class {
|
|
|
346
346
|
(async () => {
|
|
347
347
|
try {
|
|
348
348
|
for (const span of spans) if (span.name === "ai.streamText.doStream") {
|
|
349
|
-
rememberDoStream(span, this.context.
|
|
350
|
-
attachToolCallsToChat(span, this.context.
|
|
349
|
+
rememberDoStream(span, this.context.durableObjectName);
|
|
350
|
+
attachToolCallsToChat(span, this.context.durableObjectName);
|
|
351
351
|
handleAnalyticsSpan(span, this.analytics);
|
|
352
352
|
} else if (span.name === "ai.streamText") await handleAuditSpan(span, this.auditLogs, this.context);
|
|
353
353
|
else if (span.name === "ai.toolCall") {
|
package/dist/v2.d.mts
CHANGED
|
@@ -53,7 +53,7 @@ declare abstract class Agent<RequestContext extends Record<string, unknown> = Re
|
|
|
53
53
|
/**
|
|
54
54
|
* Returns the user ID from the durable object name.
|
|
55
55
|
*/
|
|
56
|
-
protected
|
|
56
|
+
protected getActorIdFromDurableObjectName(): string;
|
|
57
57
|
protected getParentAgent<T extends Agent$1>(): T | undefined;
|
|
58
58
|
abstract getModel(ctx?: ToolContext<RequestContext, UserContext>): LanguageModel;
|
|
59
59
|
abstract getSystemPrompt(ctx?: ToolContext<RequestContext, UserContext>): string;
|
|
@@ -155,7 +155,7 @@ declare abstract class Assistant extends Agent$1<Cloudflare.Env, AgentConnection
|
|
|
155
155
|
protected abstract fastModel: LanguageModel;
|
|
156
156
|
onStart(): void;
|
|
157
157
|
onClose(): Promise<void>;
|
|
158
|
-
onConnect(): Promise<void>;
|
|
158
|
+
onConnect(connection: Connection, ctx: ConnectionContext): Promise<void>;
|
|
159
159
|
createChat(): Promise<string>;
|
|
160
160
|
deleteChat(id: string): Promise<void>;
|
|
161
161
|
getChats(): Promise<Chat[]>;
|
package/dist/v2.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as extractTokenFromConnectRequest, r as verifyJwt, t as createAgentTracer } from "./telemetry-
|
|
1
|
+
import { n as extractTokenFromConnectRequest, r as verifyJwt, t as createAgentTracer } from "./telemetry-BDxmv_R7.mjs";
|
|
2
2
|
import { Output, convertToModelMessages, generateText, jsonSchema, pruneMessages, tool as tool$1 } from "ai";
|
|
3
3
|
import { Agent as Agent$1, callable, getCurrentAgent } from "agents";
|
|
4
4
|
import { Think } from "@cloudflare/think";
|
|
@@ -56,8 +56,9 @@ var Agent = class extends Think {
|
|
|
56
56
|
/**
|
|
57
57
|
* Returns the user ID from the durable object name.
|
|
58
58
|
*/
|
|
59
|
-
|
|
60
|
-
return this.name.split(":")[0];
|
|
59
|
+
getActorIdFromDurableObjectName() {
|
|
60
|
+
if (this.name.includes(":")) return this.name.split(":")[0];
|
|
61
|
+
return "system";
|
|
61
62
|
}
|
|
62
63
|
getParentAgent() {
|
|
63
64
|
if (this.parentPath.length) {
|
|
@@ -94,10 +95,6 @@ var Agent = class extends Think {
|
|
|
94
95
|
console.error("[Agent] Connection rejected: no AGENTS_AUDIT_LOGS bound. Audit logs are required.");
|
|
95
96
|
}
|
|
96
97
|
if (!this.env.AGENTS_ANALYTICS) console.warn("[Agent] No AGENTS_ANALYTICS bound. Analytics will not be collected.");
|
|
97
|
-
if (!this.getUserIdFromDurableObjectName()) {
|
|
98
|
-
hasCorrectBindings = false;
|
|
99
|
-
console.error("[Agent] Connection rejected: name must be in the format userId:uniqueChatId");
|
|
100
|
-
}
|
|
101
98
|
if (!hasCorrectBindings) {
|
|
102
99
|
this.setState({
|
|
103
100
|
...this.initialState,
|
|
@@ -185,16 +182,16 @@ var Agent = class extends Think {
|
|
|
185
182
|
isEnabled: true,
|
|
186
183
|
tracer: createAgentTracer(this.env.AGENTS_AUDIT_LOGS, this.env.AGENTS_ANALYTICS, {
|
|
187
184
|
agentName: this.constructor.name,
|
|
188
|
-
|
|
189
|
-
|
|
185
|
+
durableObjectName: this.name,
|
|
186
|
+
actorId: this.getActorIdFromDurableObjectName(),
|
|
190
187
|
clientIp: this.clientIp,
|
|
191
188
|
forwardedFor: this.forwardedFor
|
|
192
189
|
}),
|
|
193
190
|
metadata: {
|
|
194
191
|
agentName: this.constructor.name,
|
|
195
192
|
version: "v2",
|
|
196
|
-
|
|
197
|
-
|
|
193
|
+
durableObjectName: this.name,
|
|
194
|
+
actorId: this.getActorIdFromDurableObjectName()
|
|
198
195
|
}
|
|
199
196
|
}
|
|
200
197
|
};
|
|
@@ -379,7 +376,7 @@ var Assistant = class extends Agent$1 {
|
|
|
379
376
|
};
|
|
380
377
|
onStart() {
|
|
381
378
|
this.setState({
|
|
382
|
-
|
|
379
|
+
...this.initialState,
|
|
383
380
|
status: "connecting",
|
|
384
381
|
subAgentName: this.agent.name
|
|
385
382
|
});
|
|
@@ -387,14 +384,47 @@ var Assistant = class extends Agent$1 {
|
|
|
387
384
|
}
|
|
388
385
|
async onClose() {
|
|
389
386
|
this.setState({
|
|
390
|
-
|
|
387
|
+
...this.initialState,
|
|
391
388
|
status: "disconnected",
|
|
392
389
|
subAgentName: this.agent.name
|
|
393
390
|
});
|
|
394
391
|
}
|
|
395
|
-
async onConnect() {
|
|
392
|
+
async onConnect(connection, ctx) {
|
|
393
|
+
const getJwtAuthConfig = this.agent.getJwtAuthConfig;
|
|
394
|
+
if (getJwtAuthConfig) {
|
|
395
|
+
const config = getJwtAuthConfig(this.env);
|
|
396
|
+
if (config) {
|
|
397
|
+
let result;
|
|
398
|
+
try {
|
|
399
|
+
result = await verifyJwt(ctx.request, config);
|
|
400
|
+
} catch (error) {
|
|
401
|
+
this.setState({
|
|
402
|
+
...this.initialState,
|
|
403
|
+
status: "unauthorized",
|
|
404
|
+
subAgentName: this.agent.name
|
|
405
|
+
});
|
|
406
|
+
console.error(`[Assistant] JWT verification error - ${error}`);
|
|
407
|
+
connection.close(4001, "Unauthorized");
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (!result.success) {
|
|
411
|
+
this.setState({
|
|
412
|
+
...this.initialState,
|
|
413
|
+
status: "unauthorized",
|
|
414
|
+
subAgentName: this.agent.name
|
|
415
|
+
});
|
|
416
|
+
console.error(`[Assistant] JWT verification error - ${result.message}`);
|
|
417
|
+
connection.close(result.status === 401 ? 4001 : 4003, result.message);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
connection.setState({
|
|
421
|
+
authenticated: true,
|
|
422
|
+
claims: result.claims
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
396
426
|
this.setState({
|
|
397
|
-
|
|
427
|
+
...this.initialState,
|
|
398
428
|
status: "connected",
|
|
399
429
|
subAgentName: this.agent.name
|
|
400
430
|
});
|