@agentmark-ai/shared-utils 0.1.1 → 0.3.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
@@ -111,7 +111,15 @@ ${jsonToFrontMatter(content)}---
111
111
  // src/generate-types.ts
112
112
  import * as fs from "fs-extra";
113
113
  import path from "path";
114
- import fm from "front-matter";
114
+ import yaml from "js-yaml";
115
+ function extractFrontmatter(content) {
116
+ const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
117
+ if (!match) {
118
+ return { attributes: {} };
119
+ }
120
+ const attributes = yaml.load(match[1]);
121
+ return { attributes: attributes || {} };
122
+ }
115
123
  var _compile = null;
116
124
  async function getCompile() {
117
125
  var _a;
@@ -355,7 +363,7 @@ async function fetchPromptsFrontmatter(options) {
355
363
  return Promise.all(
356
364
  promptFiles.map(async (file) => {
357
365
  const content = await fs.readFile(file, "utf-8");
358
- const { attributes } = fm(content);
366
+ const { attributes } = extractFrontmatter(content);
359
367
  if (isNewFormat(attributes)) {
360
368
  return {
361
369
  path: path.relative(options.rootDir, file),
@@ -1274,6 +1282,218 @@ var MastraTransformer = class {
1274
1282
  }
1275
1283
  };
1276
1284
 
1285
+ // src/normalizer/extractors/agentmark-parser.ts
1286
+ function parseAgentMarkAttributes(attributes, prefix = "agentmark.") {
1287
+ const result = {};
1288
+ const get = (key) => attributes[`${prefix}${key}`];
1289
+ if (get("session_id")) result.sessionId = String(get("session_id"));
1290
+ if (get("session_name")) result.sessionName = String(get("session_name"));
1291
+ if (get("user_id")) result.userId = String(get("user_id"));
1292
+ if (get("trace_name")) result.traceName = String(get("trace_name"));
1293
+ if (get("prompt_name")) result.promptName = String(get("prompt_name"));
1294
+ if (get("props")) result.props = String(get("props"));
1295
+ if (get("dataset_run_id")) result.datasetRunId = String(get("dataset_run_id"));
1296
+ if (get("dataset_run_name")) result.datasetRunName = String(get("dataset_run_name"));
1297
+ if (get("dataset_item_name")) result.datasetItemName = String(get("dataset_item_name"));
1298
+ if (get("dataset_expected_output")) result.datasetExpectedOutput = String(get("dataset_expected_output"));
1299
+ if (get("dataset_path")) result.datasetPath = String(get("dataset_path"));
1300
+ return result;
1301
+ }
1302
+
1303
+ // src/normalizer/transformers/agentmark/index.ts
1304
+ var GenAIAttributes = {
1305
+ SYSTEM: "gen_ai.system",
1306
+ OPERATION_NAME: "gen_ai.operation.name",
1307
+ REQUEST_MODEL: "gen_ai.request.model",
1308
+ REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
1309
+ REQUEST_TEMPERATURE: "gen_ai.request.temperature",
1310
+ REQUEST_INPUT: "gen_ai.request.input",
1311
+ RESPONSE_ID: "gen_ai.response.id",
1312
+ RESPONSE_MODEL: "gen_ai.response.model",
1313
+ RESPONSE_OUTPUT: "gen_ai.response.output",
1314
+ RESPONSE_FINISH_REASONS: "gen_ai.response.finish_reasons",
1315
+ USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
1316
+ USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
1317
+ TOOL_NAME: "gen_ai.tool.name",
1318
+ TOOL_CALL_ID: "gen_ai.tool.call.id",
1319
+ TOOL_INPUT: "gen_ai.tool.input",
1320
+ TOOL_OUTPUT: "gen_ai.tool.output"
1321
+ };
1322
+ var SpanNames = {
1323
+ // OTEL GenAI standard operation names (these can be followed by model/tool/agent name)
1324
+ CHAT: "chat",
1325
+ EXECUTE_TOOL: "execute_tool",
1326
+ INVOKE_AGENT: "invoke_agent",
1327
+ // Legacy span names (for backwards compatibility)
1328
+ SESSION: "gen_ai.session",
1329
+ TOOL_CALL: "gen_ai.tool.call",
1330
+ SUBAGENT: "gen_ai.subagent",
1331
+ CONVERSATION: "gen_ai.conversation",
1332
+ LLM_TURN: "gen_ai.llm.turn"
1333
+ };
1334
+ var OperationNames = {
1335
+ CHAT: "chat",
1336
+ EMBEDDINGS: "embeddings",
1337
+ TEXT_COMPLETION: "text_completion",
1338
+ GENERATE_CONTENT: "generate_content",
1339
+ EXECUTE_TOOL: "execute_tool",
1340
+ CREATE_AGENT: "create_agent",
1341
+ INVOKE_AGENT: "invoke_agent"
1342
+ };
1343
+ var AgentMarkTransformer = class {
1344
+ /**
1345
+ * Classify the span type based on span name and attributes.
1346
+ *
1347
+ * GENERATION type (actual LLM calls):
1348
+ * - Spans starting with "chat " (OTEL convention)
1349
+ * - gen_ai.llm.turn spans (legacy traced module)
1350
+ * - gen_ai.session spans (legacy hooks) - these wrap LLM calls
1351
+ * - Spans with gen_ai.operation.name = "chat" or "text_completion"
1352
+ *
1353
+ * SPAN type (everything else):
1354
+ * - gen_ai.conversation (grouping span)
1355
+ * - execute_tool / gen_ai.tool.call (tool execution)
1356
+ * - invoke_agent / gen_ai.subagent (agent invocation)
1357
+ */
1358
+ classify(span, attributes) {
1359
+ const operationName = attributes[GenAIAttributes.OPERATION_NAME];
1360
+ if (operationName === OperationNames.CHAT || operationName === OperationNames.TEXT_COMPLETION) {
1361
+ return "GENERATION" /* GENERATION */;
1362
+ }
1363
+ if (span.name === SpanNames.CHAT || span.name.startsWith(SpanNames.CHAT + " ")) {
1364
+ return "GENERATION" /* GENERATION */;
1365
+ }
1366
+ if (span.name === SpanNames.LLM_TURN) {
1367
+ return "GENERATION" /* GENERATION */;
1368
+ }
1369
+ if (span.name === SpanNames.SESSION) {
1370
+ return "GENERATION" /* GENERATION */;
1371
+ }
1372
+ if (span.name === SpanNames.EXECUTE_TOOL || span.name.startsWith(SpanNames.EXECUTE_TOOL + " ") || span.name.startsWith(SpanNames.TOOL_CALL)) {
1373
+ return "SPAN" /* SPAN */;
1374
+ }
1375
+ if (span.name === SpanNames.INVOKE_AGENT || span.name.startsWith(SpanNames.INVOKE_AGENT + " ")) {
1376
+ return "SPAN" /* SPAN */;
1377
+ }
1378
+ if (span.name === SpanNames.CONVERSATION || span.name === SpanNames.SUBAGENT) {
1379
+ return "SPAN" /* SPAN */;
1380
+ }
1381
+ if (attributes[GenAIAttributes.SYSTEM] === "anthropic") {
1382
+ if (attributes[GenAIAttributes.USAGE_INPUT_TOKENS] !== void 0 && attributes[GenAIAttributes.RESPONSE_OUTPUT] !== void 0) {
1383
+ return "GENERATION" /* GENERATION */;
1384
+ }
1385
+ }
1386
+ return "SPAN" /* SPAN */;
1387
+ }
1388
+ /**
1389
+ * Transform the span and extract normalized fields from GenAI attributes.
1390
+ */
1391
+ transform(_span, attributes) {
1392
+ const result = {};
1393
+ const responseModel = attributes[GenAIAttributes.RESPONSE_MODEL];
1394
+ const requestModel = attributes[GenAIAttributes.REQUEST_MODEL];
1395
+ if (responseModel) {
1396
+ result.model = String(responseModel);
1397
+ } else if (requestModel) {
1398
+ result.model = String(requestModel);
1399
+ }
1400
+ const inputTokens = attributes[GenAIAttributes.USAGE_INPUT_TOKENS];
1401
+ const outputTokens = attributes[GenAIAttributes.USAGE_OUTPUT_TOKENS];
1402
+ if (typeof inputTokens === "number") {
1403
+ result.inputTokens = inputTokens;
1404
+ }
1405
+ if (typeof outputTokens === "number") {
1406
+ result.outputTokens = outputTokens;
1407
+ }
1408
+ if (result.inputTokens !== void 0 && result.outputTokens !== void 0) {
1409
+ result.totalTokens = result.inputTokens + result.outputTokens;
1410
+ }
1411
+ const finishReasons = attributes[GenAIAttributes.RESPONSE_FINISH_REASONS];
1412
+ if (finishReasons) {
1413
+ try {
1414
+ const reasons = JSON.parse(finishReasons);
1415
+ if (Array.isArray(reasons) && reasons.length > 0) {
1416
+ result.finishReason = String(reasons[0]);
1417
+ }
1418
+ } catch {
1419
+ result.finishReason = String(finishReasons);
1420
+ }
1421
+ }
1422
+ const maxTokens = attributes[GenAIAttributes.REQUEST_MAX_TOKENS];
1423
+ const temperature = attributes[GenAIAttributes.REQUEST_TEMPERATURE];
1424
+ if (maxTokens !== void 0 || temperature !== void 0) {
1425
+ result.settings = {};
1426
+ if (typeof maxTokens === "number") {
1427
+ result.settings.maxTokens = maxTokens;
1428
+ }
1429
+ if (typeof temperature === "number") {
1430
+ result.settings.temperature = temperature;
1431
+ }
1432
+ }
1433
+ const requestInput = attributes[GenAIAttributes.REQUEST_INPUT];
1434
+ if (requestInput && typeof requestInput === "string") {
1435
+ try {
1436
+ const parsed = JSON.parse(requestInput);
1437
+ if (Array.isArray(parsed) && parsed.length > 0) {
1438
+ const isMessagesArray = parsed.every(
1439
+ (item) => item && typeof item === "object" && "role" in item && "content" in item
1440
+ );
1441
+ if (isMessagesArray) {
1442
+ result.input = parsed;
1443
+ } else {
1444
+ result.input = [{ role: "user", content: requestInput }];
1445
+ }
1446
+ } else {
1447
+ result.input = [{ role: "user", content: requestInput }];
1448
+ }
1449
+ } catch {
1450
+ result.input = [{ role: "user", content: requestInput }];
1451
+ }
1452
+ }
1453
+ const responseOutput = attributes[GenAIAttributes.RESPONSE_OUTPUT];
1454
+ if (responseOutput && typeof responseOutput === "string") {
1455
+ result.output = responseOutput;
1456
+ }
1457
+ const toolName = attributes[GenAIAttributes.TOOL_NAME];
1458
+ const toolCallId = attributes[GenAIAttributes.TOOL_CALL_ID];
1459
+ const toolInput = attributes[GenAIAttributes.TOOL_INPUT];
1460
+ const toolOutput = attributes[GenAIAttributes.TOOL_OUTPUT];
1461
+ if (toolName && typeof toolName === "string") {
1462
+ result.name = toolName;
1463
+ const toolCall = {
1464
+ type: "tool-call",
1465
+ toolCallId: typeof toolCallId === "string" ? toolCallId : "",
1466
+ toolName,
1467
+ args: {}
1468
+ };
1469
+ if (toolInput && typeof toolInput === "string") {
1470
+ try {
1471
+ toolCall.args = JSON.parse(toolInput);
1472
+ } catch {
1473
+ toolCall.args = { raw: toolInput };
1474
+ }
1475
+ }
1476
+ if (toolOutput && typeof toolOutput === "string") {
1477
+ toolCall.result = toolOutput;
1478
+ }
1479
+ result.toolCalls = [toolCall];
1480
+ }
1481
+ const agentmarkAttrs = parseAgentMarkAttributes(attributes);
1482
+ const metadataAttrs = parseMetadata(attributes, "agentmark.metadata.");
1483
+ const customMetadata = extractCustomMetadata(attributes, "agentmark.metadata.");
1484
+ return {
1485
+ ...result,
1486
+ ...metadataAttrs,
1487
+ // agentmark.metadata.* values (lower priority)
1488
+ ...agentmarkAttrs,
1489
+ // agentmark.* values (higher priority)
1490
+ // Only include metadata field if there are custom metadata keys
1491
+ ...Object.keys(customMetadata).length > 0 ? { metadata: customMetadata } : {}
1492
+ };
1493
+ }
1494
+ };
1495
+ var AGENTMARK_SCOPE_NAME = "agentmark";
1496
+
1277
1497
  // src/normalizer/converters/otlp-converter.ts
1278
1498
  function convertOtlpValue(value) {
1279
1499
  var _a;
@@ -1354,22 +1574,6 @@ function extractResourceScopeSpan(resourceSpans) {
1354
1574
  return result;
1355
1575
  }
1356
1576
 
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
- if (get("dataset_path")) result.datasetPath = String(get("dataset_path"));
1370
- return result;
1371
- }
1372
-
1373
1577
  // src/normalizer/type-classifier.ts
1374
1578
  var TypeClassifier = class {
1375
1579
  classify(span, attributes) {
@@ -1390,6 +1594,7 @@ var typeClassifier = new TypeClassifier();
1390
1594
  // src/normalizer/index.ts
1391
1595
  registry.register("ai", new AiSdkTransformer());
1392
1596
  registry.register("default-tracer", new MastraTransformer());
1597
+ registry.register("agentmark", new AgentMarkTransformer());
1393
1598
  registry.setDefault(new AiSdkTransformer());
1394
1599
  function normalizeSpan(resource, scope, span) {
1395
1600
  var _a, _b, _c;
@@ -1459,7 +1664,10 @@ function normalizeOtlpSpans(resourceSpans) {
1459
1664
  return normalizedSpans;
1460
1665
  }
1461
1666
  export {
1667
+ AGENTMARK_SCOPE_NAME,
1668
+ AgentMarkTransformer,
1462
1669
  AiSdkTransformer,
1670
+ AgentMarkTransformer as ClaudeAgentTransformer,
1463
1671
  MastraTransformer,
1464
1672
  SpanType,
1465
1673
  TransformerRegistry,