@agentmark-ai/shared-utils 0.3.3 → 0.5.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
@@ -921,39 +921,50 @@ var AiSdkV4Strategy = class {
921
921
  return attributes["gen_ai.request.model"] || attributes["ai.model.id"];
922
922
  }
923
923
  extractInput(attributes) {
924
- let messagesValue;
925
- if (attributes["ai.prompt.messages"] !== void 0) {
926
- messagesValue = attributes["ai.prompt.messages"];
927
- } else if (attributes["ai.prompt"] !== void 0) {
928
- const promptValue = attributes["ai.prompt"];
929
- if (typeof promptValue === "string") {
930
- try {
931
- const parsed = JSON.parse(promptValue);
932
- messagesValue = parsed.messages || parsed;
933
- } catch {
934
- return void 0;
935
- }
936
- } else {
937
- messagesValue = promptValue.messages || promptValue;
938
- }
939
- } else {
924
+ const raw = attributes["ai.prompt.messages"] !== void 0 ? attributes["ai.prompt.messages"] : attributes["ai.prompt"];
925
+ if (raw === void 0) {
940
926
  return void 0;
941
927
  }
942
- let messages;
943
- if (typeof messagesValue === "string") {
928
+ let value = raw;
929
+ if (typeof value === "string") {
944
930
  try {
945
- messages = JSON.parse(messagesValue);
931
+ value = JSON.parse(value);
946
932
  } catch {
947
- return void 0;
948
933
  }
949
- } else {
950
- messages = messagesValue;
951
934
  }
952
- if (!Array.isArray(messages)) {
935
+ const messages = this.coerceToMessages(value);
936
+ if (!messages) {
953
937
  return void 0;
954
938
  }
955
939
  return this.normalizeMessages(messages);
956
940
  }
941
+ /**
942
+ * Coerce the shapes `ai.prompt` / `ai.prompt.messages` can take into a
943
+ * messages array: a raw array, `{ messages, system? }`,
944
+ * `{ prompt, system? }` (string prompt), or a bare string.
945
+ */
946
+ coerceToMessages(value) {
947
+ if (typeof value === "string") {
948
+ return [{ role: "user", content: value }];
949
+ }
950
+ if (Array.isArray(value)) {
951
+ return value;
952
+ }
953
+ if (value && typeof value === "object") {
954
+ if (Array.isArray(value.messages)) {
955
+ return typeof value.system === "string" ? [{ role: "system", content: value.system }, ...value.messages] : value.messages;
956
+ }
957
+ if (typeof value.prompt === "string") {
958
+ const messages = [];
959
+ if (typeof value.system === "string") {
960
+ messages.push({ role: "system", content: value.system });
961
+ }
962
+ messages.push({ role: "user", content: value.prompt });
963
+ return messages;
964
+ }
965
+ }
966
+ return void 0;
967
+ }
957
968
  extractOutput(attributes) {
958
969
  if (attributes["ai.result.text"] !== void 0) return attributes["ai.result.text"];
959
970
  if (attributes["ai.response.text"] !== void 0) return attributes["ai.response.text"];
@@ -1217,39 +1228,54 @@ var AiSdkV5Strategy = class {
1217
1228
  return attributes["gen_ai.request.model"] || attributes["ai.model.id"];
1218
1229
  }
1219
1230
  extractInput(attributes) {
1220
- let messagesValue;
1221
- if (attributes["ai.prompt.messages"] !== void 0) {
1222
- messagesValue = attributes["ai.prompt.messages"];
1223
- } else if (attributes["ai.prompt"] !== void 0) {
1224
- const promptValue = attributes["ai.prompt"];
1225
- if (typeof promptValue === "string") {
1226
- try {
1227
- const parsed = JSON.parse(promptValue);
1228
- messagesValue = parsed.messages || parsed;
1229
- } catch {
1230
- return void 0;
1231
- }
1232
- } else {
1233
- messagesValue = promptValue.messages || promptValue;
1234
- }
1235
- } else {
1231
+ const raw = attributes["ai.prompt.messages"] !== void 0 ? attributes["ai.prompt.messages"] : attributes["ai.prompt"];
1232
+ if (raw === void 0) {
1236
1233
  return void 0;
1237
1234
  }
1238
- let messages;
1239
- if (typeof messagesValue === "string") {
1235
+ let value = raw;
1236
+ if (typeof value === "string") {
1240
1237
  try {
1241
- messages = JSON.parse(messagesValue);
1238
+ value = JSON.parse(value);
1242
1239
  } catch {
1243
- return void 0;
1244
1240
  }
1245
- } else {
1246
- messages = messagesValue;
1247
1241
  }
1248
- if (!Array.isArray(messages)) {
1242
+ const messages = this.coerceToMessages(value);
1243
+ if (!messages) {
1249
1244
  return void 0;
1250
1245
  }
1251
1246
  return this.normalizeMessages(messages);
1252
1247
  }
1248
+ /**
1249
+ * Coerce the shapes `ai.prompt` / `ai.prompt.messages` can take into a
1250
+ * messages array:
1251
+ * - `[ ...messages ]` — ai.prompt.messages (leaf spans)
1252
+ * - `{ messages: [...], system? }` — ai.prompt with messages
1253
+ * - `{ prompt: "text", system? }` — ai.prompt for a string prompt
1254
+ * (the parent wrapper spans)
1255
+ * - `"text"` — a bare (non-JSON) prompt string
1256
+ */
1257
+ coerceToMessages(value) {
1258
+ if (typeof value === "string") {
1259
+ return [{ role: "user", content: value }];
1260
+ }
1261
+ if (Array.isArray(value)) {
1262
+ return value;
1263
+ }
1264
+ if (value && typeof value === "object") {
1265
+ if (Array.isArray(value.messages)) {
1266
+ return typeof value.system === "string" ? [{ role: "system", content: value.system }, ...value.messages] : value.messages;
1267
+ }
1268
+ if (typeof value.prompt === "string") {
1269
+ const messages = [];
1270
+ if (typeof value.system === "string") {
1271
+ messages.push({ role: "system", content: value.system });
1272
+ }
1273
+ messages.push({ role: "user", content: value.prompt });
1274
+ return messages;
1275
+ }
1276
+ }
1277
+ return void 0;
1278
+ }
1253
1279
  extractOutput(attributes) {
1254
1280
  if (attributes["ai.response.text"] !== void 0) return attributes["ai.response.text"];
1255
1281
  return void 0;
@@ -1590,6 +1616,8 @@ function parseAgentMarkAttributes(attributes, prefix = "agentmark.") {
1590
1616
  if (get("dataset_expected_output")) result.datasetExpectedOutput = String(get("dataset_expected_output"));
1591
1617
  if (get("dataset_input")) result.datasetInput = String(get("dataset_input"));
1592
1618
  if (get("dataset_path")) result.datasetPath = String(get("dataset_path"));
1619
+ if (get("experiment_key")) result.experimentKey = String(get("experiment_key"));
1620
+ if (get("source_tree_hash")) result.sourceTreeHash = String(get("source_tree_hash"));
1593
1621
  return result;
1594
1622
  }
1595
1623
 
@@ -2104,13 +2132,20 @@ var FRAMEWORK_MAPPINGS = [
2104
2132
  {
2105
2133
  key: "ai.operationId",
2106
2134
  // Vercel AI SDK
2135
+ // The AI SDK emits ai.operationId WITH the "ai." prefix (e.g.
2136
+ // "ai.generateText"); accept both prefixed and unprefixed so generation
2137
+ // wrappers resolve to "llm" instead of falling through to "function".
2107
2138
  map: {
2108
2139
  "embed": "embedding",
2109
2140
  "ai.embed": "embedding",
2110
2141
  "generateText": "llm",
2142
+ "ai.generateText": "llm",
2111
2143
  "streamText": "llm",
2144
+ "ai.streamText": "llm",
2112
2145
  "generateObject": "llm",
2113
- "streamObject": "llm"
2146
+ "ai.generateObject": "llm",
2147
+ "streamObject": "llm",
2148
+ "ai.streamObject": "llm"
2114
2149
  }
2115
2150
  },
2116
2151
  {
@@ -2154,6 +2189,9 @@ function resolveSemanticKind(normalized, allAttributes) {
2154
2189
  if (normalized.type === "GENERATION" /* GENERATION */) {
2155
2190
  return "llm";
2156
2191
  }
2192
+ if (normalized.model) {
2193
+ return "llm";
2194
+ }
2157
2195
  if (normalized.toolCalls && normalized.toolCalls.length > 0) {
2158
2196
  return "tool";
2159
2197
  }