@agentmark-ai/shared-utils 0.4.0 → 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.d.mts +9 -44
- package/dist/index.d.ts +9 -44
- package/dist/index.js +85 -81
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -79
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
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
|
-
|
|
925
|
-
if (
|
|
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
|
|
943
|
-
if (typeof
|
|
928
|
+
let value = raw;
|
|
929
|
+
if (typeof value === "string") {
|
|
944
930
|
try {
|
|
945
|
-
|
|
931
|
+
value = JSON.parse(value);
|
|
946
932
|
} catch {
|
|
947
|
-
return void 0;
|
|
948
933
|
}
|
|
949
|
-
} else {
|
|
950
|
-
messages = messagesValue;
|
|
951
934
|
}
|
|
952
|
-
|
|
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
|
-
|
|
1221
|
-
if (
|
|
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
|
|
1239
|
-
if (typeof
|
|
1235
|
+
let value = raw;
|
|
1236
|
+
if (typeof value === "string") {
|
|
1240
1237
|
try {
|
|
1241
|
-
|
|
1238
|
+
value = JSON.parse(value);
|
|
1242
1239
|
} catch {
|
|
1243
|
-
return void 0;
|
|
1244
1240
|
}
|
|
1245
|
-
} else {
|
|
1246
|
-
messages = messagesValue;
|
|
1247
1241
|
}
|
|
1248
|
-
|
|
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
|
-
"
|
|
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
|
}
|
|
@@ -2255,36 +2293,6 @@ function normalizeOtlpSpans(resourceSpans) {
|
|
|
2255
2293
|
}
|
|
2256
2294
|
return normalizedSpans;
|
|
2257
2295
|
}
|
|
2258
|
-
|
|
2259
|
-
// src/dataset-item-name.ts
|
|
2260
|
-
import { createHash } from "crypto";
|
|
2261
|
-
function computeDatasetItemName(input, fallbackIndex) {
|
|
2262
|
-
if (input === void 0 || input === null) {
|
|
2263
|
-
return String(fallbackIndex);
|
|
2264
|
-
}
|
|
2265
|
-
const canonical = canonicalJsonStringify(input);
|
|
2266
|
-
return createHash("md5").update(canonical).digest("hex").slice(0, 12);
|
|
2267
|
-
}
|
|
2268
|
-
function canonicalJsonStringify(value) {
|
|
2269
|
-
if (value === null) return "null";
|
|
2270
|
-
if (value === void 0) return JSON.stringify(String(void 0));
|
|
2271
|
-
const t = typeof value;
|
|
2272
|
-
if (t === "string" || t === "number" || t === "boolean") {
|
|
2273
|
-
return JSON.stringify(value);
|
|
2274
|
-
}
|
|
2275
|
-
if (t === "bigint" || t === "function" || t === "symbol") {
|
|
2276
|
-
return JSON.stringify(String(value));
|
|
2277
|
-
}
|
|
2278
|
-
if (Array.isArray(value)) {
|
|
2279
|
-
return "[" + value.map(canonicalJsonStringify).join(",") + "]";
|
|
2280
|
-
}
|
|
2281
|
-
const obj = value;
|
|
2282
|
-
const keys = Object.keys(obj).sort();
|
|
2283
|
-
const parts = keys.map((k) => {
|
|
2284
|
-
return JSON.stringify(k) + ":" + canonicalJsonStringify(obj[k]);
|
|
2285
|
-
});
|
|
2286
|
-
return "{" + parts.join(",") + "}";
|
|
2287
|
-
}
|
|
2288
2296
|
export {
|
|
2289
2297
|
AGENTMARK_SCOPE_NAME,
|
|
2290
2298
|
AgentMarkTransformer,
|
|
@@ -2296,8 +2304,6 @@ export {
|
|
|
2296
2304
|
SpanType,
|
|
2297
2305
|
TransformerRegistry,
|
|
2298
2306
|
TypeClassifier,
|
|
2299
|
-
canonicalJsonStringify,
|
|
2300
|
-
computeDatasetItemName,
|
|
2301
2307
|
convertOtlpAttributes,
|
|
2302
2308
|
createSignature,
|
|
2303
2309
|
detectVersion,
|