@economic/agents 1.8.1 → 1.8.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 +23 -17
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -370,7 +370,7 @@ async function verifyJwt(request, config) {
|
|
|
370
370
|
};
|
|
371
371
|
}
|
|
372
372
|
//#endregion
|
|
373
|
-
//#region src/server/shared/features/telemetry/
|
|
373
|
+
//#region src/server/shared/features/telemetry/utils.ts
|
|
374
374
|
function durationMs(duration) {
|
|
375
375
|
return duration[0] * 1e3 + duration[1] / 1e6;
|
|
376
376
|
}
|
|
@@ -390,6 +390,8 @@ function parseJson(value) {
|
|
|
390
390
|
return;
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/server/shared/features/telemetry/audit-logs.ts
|
|
393
395
|
function safePathSegment(value, fallback) {
|
|
394
396
|
return (value || fallback).replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
395
397
|
}
|
|
@@ -436,19 +438,6 @@ function extractTools(messages) {
|
|
|
436
438
|
}
|
|
437
439
|
return tools;
|
|
438
440
|
}
|
|
439
|
-
/**
|
|
440
|
-
* Stores the last ai.streamText.doStream span per conversation.
|
|
441
|
-
* Its ai.prompt.messages contains the full conversation history including all
|
|
442
|
-
* intermediate tool_use + tool_result turns from the agentic loop.
|
|
443
|
-
*/
|
|
444
|
-
const lastDoStreamByConversation = /* @__PURE__ */ new Map();
|
|
445
|
-
const toolCallsByParentSpan = /* @__PURE__ */ new Map();
|
|
446
|
-
const toolCallsByConversation = /* @__PURE__ */ new Map();
|
|
447
|
-
const pendingToolCalls = [];
|
|
448
|
-
const currentSkillByConversation = /* @__PURE__ */ new Map();
|
|
449
|
-
function userIndex(userId) {
|
|
450
|
-
return userId;
|
|
451
|
-
}
|
|
452
441
|
function extractSkillName(toolName, input) {
|
|
453
442
|
if (!input || typeof input !== "object") return;
|
|
454
443
|
const record = input;
|
|
@@ -466,6 +455,16 @@ function pushToolCall(map, key, toolCall) {
|
|
|
466
455
|
if (existing) existing.push(toolCall);
|
|
467
456
|
else map.set(key, [toolCall]);
|
|
468
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Stores the last ai.streamText.doStream span per conversation.
|
|
460
|
+
* Its ai.prompt.messages contains the full conversation history including all
|
|
461
|
+
* intermediate tool_use + tool_result turns from the agentic loop.
|
|
462
|
+
*/
|
|
463
|
+
const lastDoStreamByConversation = /* @__PURE__ */ new Map();
|
|
464
|
+
const toolCallsByParentSpan = /* @__PURE__ */ new Map();
|
|
465
|
+
const toolCallsByConversation = /* @__PURE__ */ new Map();
|
|
466
|
+
const pendingToolCalls = [];
|
|
467
|
+
const currentSkillByConversation = /* @__PURE__ */ new Map();
|
|
469
468
|
function rememberToolCall(span) {
|
|
470
469
|
const parentSpanId = span.parentSpanContext?.spanId;
|
|
471
470
|
const toolName = stringAttribute(span, "ai.toolCall.name");
|
|
@@ -527,12 +526,17 @@ function buildAuditLog(span, context) {
|
|
|
527
526
|
tools: [...extractTools(inputMessages), ...spanToolCalls]
|
|
528
527
|
};
|
|
529
528
|
}
|
|
529
|
+
function rememberDoStream(span, conversationId) {
|
|
530
|
+
lastDoStreamByConversation.set(conversationId, span);
|
|
531
|
+
}
|
|
530
532
|
async function handleAuditSpan(span, auditLogs, context) {
|
|
531
533
|
const auditLog = buildAuditLog(span, context);
|
|
532
534
|
if (!auditLogs) return;
|
|
533
535
|
await auditLogs.put(auditLog.id, JSON.stringify(auditLog, null, 2), { httpMetadata: { contentType: "application/json" } });
|
|
534
536
|
console.log("[AuditLog] Created", auditLog.id);
|
|
535
537
|
}
|
|
538
|
+
//#endregion
|
|
539
|
+
//#region src/server/shared/features/telemetry/analytics.ts
|
|
536
540
|
function writeAnalyticsDatapoint(analytics, dataPoint) {
|
|
537
541
|
if (!analytics) return;
|
|
538
542
|
try {
|
|
@@ -546,7 +550,7 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
546
550
|
const promptMessages = parseJson(stringAttribute(span, "ai.prompt.messages"));
|
|
547
551
|
const responseText = stringAttribute(span, "ai.response.text") ?? "";
|
|
548
552
|
writeAnalyticsDatapoint(analytics, {
|
|
549
|
-
indexes: [
|
|
553
|
+
indexes: [stringAttribute(span, "ai.telemetry.metadata.userId") ?? ""],
|
|
550
554
|
blobs: [
|
|
551
555
|
"llm_call",
|
|
552
556
|
stringAttribute(span, "ai.telemetry.metadata.agentName") ?? "",
|
|
@@ -575,7 +579,7 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
575
579
|
const skillName = extractSkillName(toolName, toolInput) ?? currentSkillByConversation.get(conversationId) ?? "";
|
|
576
580
|
const success = span.status.code === 0;
|
|
577
581
|
writeAnalyticsDatapoint(analytics, {
|
|
578
|
-
indexes: [
|
|
582
|
+
indexes: [stringAttribute(span, "ai.telemetry.metadata.userId") ?? ""],
|
|
579
583
|
blobs: [
|
|
580
584
|
"tool_call",
|
|
581
585
|
stringAttribute(span, "ai.telemetry.metadata.agentName") ?? "",
|
|
@@ -597,6 +601,8 @@ function handleAnalyticsSpan(span, analytics) {
|
|
|
597
601
|
});
|
|
598
602
|
}
|
|
599
603
|
}
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/server/shared/features/telemetry/index.ts
|
|
600
606
|
var AgentSpanExporter = class {
|
|
601
607
|
auditLogs;
|
|
602
608
|
analytics;
|
|
@@ -610,7 +616,7 @@ var AgentSpanExporter = class {
|
|
|
610
616
|
(async () => {
|
|
611
617
|
try {
|
|
612
618
|
for (const span of spans) if (span.name === "ai.streamText.doStream") {
|
|
613
|
-
|
|
619
|
+
rememberDoStream(span, this.context.conversationId);
|
|
614
620
|
attachToolCallsToConversation(span, this.context.conversationId);
|
|
615
621
|
handleAnalyticsSpan(span, this.analytics);
|
|
616
622
|
} else if (span.name === "ai.streamText") await handleAuditSpan(span, this.auditLogs, this.context);
|