@agentmark-ai/shared-utils 0.1.0 → 0.2.0

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 CHANGED
@@ -1274,6 +1274,218 @@ var MastraTransformer = class {
1274
1274
  }
1275
1275
  };
1276
1276
 
1277
+ // src/normalizer/extractors/agentmark-parser.ts
1278
+ function parseAgentMarkAttributes(attributes, prefix = "agentmark.") {
1279
+ const result = {};
1280
+ const get = (key) => attributes[`${prefix}${key}`];
1281
+ if (get("session_id")) result.sessionId = String(get("session_id"));
1282
+ if (get("session_name")) result.sessionName = String(get("session_name"));
1283
+ if (get("user_id")) result.userId = String(get("user_id"));
1284
+ if (get("trace_name")) result.traceName = String(get("trace_name"));
1285
+ if (get("prompt_name")) result.promptName = String(get("prompt_name"));
1286
+ if (get("props")) result.props = String(get("props"));
1287
+ if (get("dataset_run_id")) result.datasetRunId = String(get("dataset_run_id"));
1288
+ if (get("dataset_run_name")) result.datasetRunName = String(get("dataset_run_name"));
1289
+ if (get("dataset_item_name")) result.datasetItemName = String(get("dataset_item_name"));
1290
+ if (get("dataset_expected_output")) result.datasetExpectedOutput = String(get("dataset_expected_output"));
1291
+ if (get("dataset_path")) result.datasetPath = String(get("dataset_path"));
1292
+ return result;
1293
+ }
1294
+
1295
+ // src/normalizer/transformers/agentmark/index.ts
1296
+ var GenAIAttributes = {
1297
+ SYSTEM: "gen_ai.system",
1298
+ OPERATION_NAME: "gen_ai.operation.name",
1299
+ REQUEST_MODEL: "gen_ai.request.model",
1300
+ REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
1301
+ REQUEST_TEMPERATURE: "gen_ai.request.temperature",
1302
+ REQUEST_INPUT: "gen_ai.request.input",
1303
+ RESPONSE_ID: "gen_ai.response.id",
1304
+ RESPONSE_MODEL: "gen_ai.response.model",
1305
+ RESPONSE_OUTPUT: "gen_ai.response.output",
1306
+ RESPONSE_FINISH_REASONS: "gen_ai.response.finish_reasons",
1307
+ USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
1308
+ USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
1309
+ TOOL_NAME: "gen_ai.tool.name",
1310
+ TOOL_CALL_ID: "gen_ai.tool.call.id",
1311
+ TOOL_INPUT: "gen_ai.tool.input",
1312
+ TOOL_OUTPUT: "gen_ai.tool.output"
1313
+ };
1314
+ var SpanNames = {
1315
+ // OTEL GenAI standard operation names (these can be followed by model/tool/agent name)
1316
+ CHAT: "chat",
1317
+ EXECUTE_TOOL: "execute_tool",
1318
+ INVOKE_AGENT: "invoke_agent",
1319
+ // Legacy span names (for backwards compatibility)
1320
+ SESSION: "gen_ai.session",
1321
+ TOOL_CALL: "gen_ai.tool.call",
1322
+ SUBAGENT: "gen_ai.subagent",
1323
+ CONVERSATION: "gen_ai.conversation",
1324
+ LLM_TURN: "gen_ai.llm.turn"
1325
+ };
1326
+ var OperationNames = {
1327
+ CHAT: "chat",
1328
+ EMBEDDINGS: "embeddings",
1329
+ TEXT_COMPLETION: "text_completion",
1330
+ GENERATE_CONTENT: "generate_content",
1331
+ EXECUTE_TOOL: "execute_tool",
1332
+ CREATE_AGENT: "create_agent",
1333
+ INVOKE_AGENT: "invoke_agent"
1334
+ };
1335
+ var AgentMarkTransformer = class {
1336
+ /**
1337
+ * Classify the span type based on span name and attributes.
1338
+ *
1339
+ * GENERATION type (actual LLM calls):
1340
+ * - Spans starting with "chat " (OTEL convention)
1341
+ * - gen_ai.llm.turn spans (legacy traced module)
1342
+ * - gen_ai.session spans (legacy hooks) - these wrap LLM calls
1343
+ * - Spans with gen_ai.operation.name = "chat" or "text_completion"
1344
+ *
1345
+ * SPAN type (everything else):
1346
+ * - gen_ai.conversation (grouping span)
1347
+ * - execute_tool / gen_ai.tool.call (tool execution)
1348
+ * - invoke_agent / gen_ai.subagent (agent invocation)
1349
+ */
1350
+ classify(span, attributes) {
1351
+ const operationName = attributes[GenAIAttributes.OPERATION_NAME];
1352
+ if (operationName === OperationNames.CHAT || operationName === OperationNames.TEXT_COMPLETION) {
1353
+ return "GENERATION" /* GENERATION */;
1354
+ }
1355
+ if (span.name === SpanNames.CHAT || span.name.startsWith(SpanNames.CHAT + " ")) {
1356
+ return "GENERATION" /* GENERATION */;
1357
+ }
1358
+ if (span.name === SpanNames.LLM_TURN) {
1359
+ return "GENERATION" /* GENERATION */;
1360
+ }
1361
+ if (span.name === SpanNames.SESSION) {
1362
+ return "GENERATION" /* GENERATION */;
1363
+ }
1364
+ if (span.name === SpanNames.EXECUTE_TOOL || span.name.startsWith(SpanNames.EXECUTE_TOOL + " ") || span.name.startsWith(SpanNames.TOOL_CALL)) {
1365
+ return "SPAN" /* SPAN */;
1366
+ }
1367
+ if (span.name === SpanNames.INVOKE_AGENT || span.name.startsWith(SpanNames.INVOKE_AGENT + " ")) {
1368
+ return "SPAN" /* SPAN */;
1369
+ }
1370
+ if (span.name === SpanNames.CONVERSATION || span.name === SpanNames.SUBAGENT) {
1371
+ return "SPAN" /* SPAN */;
1372
+ }
1373
+ if (attributes[GenAIAttributes.SYSTEM] === "anthropic") {
1374
+ if (attributes[GenAIAttributes.USAGE_INPUT_TOKENS] !== void 0 && attributes[GenAIAttributes.RESPONSE_OUTPUT] !== void 0) {
1375
+ return "GENERATION" /* GENERATION */;
1376
+ }
1377
+ }
1378
+ return "SPAN" /* SPAN */;
1379
+ }
1380
+ /**
1381
+ * Transform the span and extract normalized fields from GenAI attributes.
1382
+ */
1383
+ transform(_span, attributes) {
1384
+ const result = {};
1385
+ const responseModel = attributes[GenAIAttributes.RESPONSE_MODEL];
1386
+ const requestModel = attributes[GenAIAttributes.REQUEST_MODEL];
1387
+ if (responseModel) {
1388
+ result.model = String(responseModel);
1389
+ } else if (requestModel) {
1390
+ result.model = String(requestModel);
1391
+ }
1392
+ const inputTokens = attributes[GenAIAttributes.USAGE_INPUT_TOKENS];
1393
+ const outputTokens = attributes[GenAIAttributes.USAGE_OUTPUT_TOKENS];
1394
+ if (typeof inputTokens === "number") {
1395
+ result.inputTokens = inputTokens;
1396
+ }
1397
+ if (typeof outputTokens === "number") {
1398
+ result.outputTokens = outputTokens;
1399
+ }
1400
+ if (result.inputTokens !== void 0 && result.outputTokens !== void 0) {
1401
+ result.totalTokens = result.inputTokens + result.outputTokens;
1402
+ }
1403
+ const finishReasons = attributes[GenAIAttributes.RESPONSE_FINISH_REASONS];
1404
+ if (finishReasons) {
1405
+ try {
1406
+ const reasons = JSON.parse(finishReasons);
1407
+ if (Array.isArray(reasons) && reasons.length > 0) {
1408
+ result.finishReason = String(reasons[0]);
1409
+ }
1410
+ } catch {
1411
+ result.finishReason = String(finishReasons);
1412
+ }
1413
+ }
1414
+ const maxTokens = attributes[GenAIAttributes.REQUEST_MAX_TOKENS];
1415
+ const temperature = attributes[GenAIAttributes.REQUEST_TEMPERATURE];
1416
+ if (maxTokens !== void 0 || temperature !== void 0) {
1417
+ result.settings = {};
1418
+ if (typeof maxTokens === "number") {
1419
+ result.settings.maxTokens = maxTokens;
1420
+ }
1421
+ if (typeof temperature === "number") {
1422
+ result.settings.temperature = temperature;
1423
+ }
1424
+ }
1425
+ const requestInput = attributes[GenAIAttributes.REQUEST_INPUT];
1426
+ if (requestInput && typeof requestInput === "string") {
1427
+ try {
1428
+ const parsed = JSON.parse(requestInput);
1429
+ if (Array.isArray(parsed) && parsed.length > 0) {
1430
+ const isMessagesArray = parsed.every(
1431
+ (item) => item && typeof item === "object" && "role" in item && "content" in item
1432
+ );
1433
+ if (isMessagesArray) {
1434
+ result.input = parsed;
1435
+ } else {
1436
+ result.input = [{ role: "user", content: requestInput }];
1437
+ }
1438
+ } else {
1439
+ result.input = [{ role: "user", content: requestInput }];
1440
+ }
1441
+ } catch {
1442
+ result.input = [{ role: "user", content: requestInput }];
1443
+ }
1444
+ }
1445
+ const responseOutput = attributes[GenAIAttributes.RESPONSE_OUTPUT];
1446
+ if (responseOutput && typeof responseOutput === "string") {
1447
+ result.output = responseOutput;
1448
+ }
1449
+ const toolName = attributes[GenAIAttributes.TOOL_NAME];
1450
+ const toolCallId = attributes[GenAIAttributes.TOOL_CALL_ID];
1451
+ const toolInput = attributes[GenAIAttributes.TOOL_INPUT];
1452
+ const toolOutput = attributes[GenAIAttributes.TOOL_OUTPUT];
1453
+ if (toolName && typeof toolName === "string") {
1454
+ result.name = toolName;
1455
+ const toolCall = {
1456
+ type: "tool-call",
1457
+ toolCallId: typeof toolCallId === "string" ? toolCallId : "",
1458
+ toolName,
1459
+ args: {}
1460
+ };
1461
+ if (toolInput && typeof toolInput === "string") {
1462
+ try {
1463
+ toolCall.args = JSON.parse(toolInput);
1464
+ } catch {
1465
+ toolCall.args = { raw: toolInput };
1466
+ }
1467
+ }
1468
+ if (toolOutput && typeof toolOutput === "string") {
1469
+ toolCall.result = toolOutput;
1470
+ }
1471
+ result.toolCalls = [toolCall];
1472
+ }
1473
+ const agentmarkAttrs = parseAgentMarkAttributes(attributes);
1474
+ const metadataAttrs = parseMetadata(attributes, "agentmark.metadata.");
1475
+ const customMetadata = extractCustomMetadata(attributes, "agentmark.metadata.");
1476
+ return {
1477
+ ...result,
1478
+ ...metadataAttrs,
1479
+ // agentmark.metadata.* values (lower priority)
1480
+ ...agentmarkAttrs,
1481
+ // agentmark.* values (higher priority)
1482
+ // Only include metadata field if there are custom metadata keys
1483
+ ...Object.keys(customMetadata).length > 0 ? { metadata: customMetadata } : {}
1484
+ };
1485
+ }
1486
+ };
1487
+ var AGENTMARK_SCOPE_NAME = "agentmark";
1488
+
1277
1489
  // src/normalizer/converters/otlp-converter.ts
1278
1490
  function convertOtlpValue(value) {
1279
1491
  var _a;
@@ -1354,21 +1566,6 @@ function extractResourceScopeSpan(resourceSpans) {
1354
1566
  return result;
1355
1567
  }
1356
1568
 
1357
- // src/normalizer/extractors/agentmark-parser.ts
1358
- function parseAgentMarkAttributes(attributes, prefix = "agentmark.") {
1359
- const result = {};
1360
- const get = (key) => attributes[`${prefix}${key}`];
1361
- if (get("session_id")) result.sessionId = String(get("session_id"));
1362
- if (get("session_name")) result.sessionName = String(get("session_name"));
1363
- if (get("user_id")) result.userId = String(get("user_id"));
1364
- if (get("trace_name")) result.traceName = String(get("trace_name"));
1365
- if (get("dataset_run_id")) result.datasetRunId = String(get("dataset_run_id"));
1366
- if (get("dataset_run_name")) result.datasetRunName = String(get("dataset_run_name"));
1367
- if (get("dataset_item_name")) result.datasetItemName = String(get("dataset_item_name"));
1368
- if (get("dataset_expected_output")) result.datasetExpectedOutput = String(get("dataset_expected_output"));
1369
- return result;
1370
- }
1371
-
1372
1569
  // src/normalizer/type-classifier.ts
1373
1570
  var TypeClassifier = class {
1374
1571
  classify(span, attributes) {
@@ -1389,6 +1586,7 @@ var typeClassifier = new TypeClassifier();
1389
1586
  // src/normalizer/index.ts
1390
1587
  registry.register("ai", new AiSdkTransformer());
1391
1588
  registry.register("default-tracer", new MastraTransformer());
1589
+ registry.register("agentmark", new AgentMarkTransformer());
1392
1590
  registry.setDefault(new AiSdkTransformer());
1393
1591
  function normalizeSpan(resource, scope, span) {
1394
1592
  var _a, _b, _c;
@@ -1458,7 +1656,10 @@ function normalizeOtlpSpans(resourceSpans) {
1458
1656
  return normalizedSpans;
1459
1657
  }
1460
1658
  export {
1659
+ AGENTMARK_SCOPE_NAME,
1660
+ AgentMarkTransformer,
1461
1661
  AiSdkTransformer,
1662
+ AgentMarkTransformer as ClaudeAgentTransformer,
1462
1663
  MastraTransformer,
1463
1664
  SpanType,
1464
1665
  TransformerRegistry,