@agentmark-ai/shared-utils 0.6.1 → 0.7.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 +163 -1
- package/dist/index.d.ts +163 -1
- package/dist/index.js +529 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +522 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2136,6 +2136,520 @@ var OtelGenAiTransformer = class {
|
|
|
2136
2136
|
};
|
|
2137
2137
|
OtelGenAiTransformer.SCOPE_NAME = "pydantic-ai";
|
|
2138
2138
|
|
|
2139
|
+
// src/normalizer/extractors/indexed-message-parser.ts
|
|
2140
|
+
function escapeRegExp(value) {
|
|
2141
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2142
|
+
}
|
|
2143
|
+
function collectIndices(attributes, prefix) {
|
|
2144
|
+
const re = new RegExp(`^${escapeRegExp(prefix)}\\.(\\d+)\\.`);
|
|
2145
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2146
|
+
for (const key of Object.keys(attributes)) {
|
|
2147
|
+
const match = re.exec(key);
|
|
2148
|
+
if (match) seen.add(Number(match[1]));
|
|
2149
|
+
}
|
|
2150
|
+
return [...seen].sort((a, b) => a - b);
|
|
2151
|
+
}
|
|
2152
|
+
function parseArgs(raw) {
|
|
2153
|
+
if (raw === void 0 || raw === null) return {};
|
|
2154
|
+
if (typeof raw === "object") return raw;
|
|
2155
|
+
if (typeof raw === "string") {
|
|
2156
|
+
try {
|
|
2157
|
+
const parsed = JSON.parse(raw);
|
|
2158
|
+
return parsed && typeof parsed === "object" ? parsed : { raw };
|
|
2159
|
+
} catch {
|
|
2160
|
+
return { raw };
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
return { raw };
|
|
2164
|
+
}
|
|
2165
|
+
function readMessageToolCalls(attributes, messageBase, config) {
|
|
2166
|
+
const tc = config.toolCalls;
|
|
2167
|
+
if (!tc) return [];
|
|
2168
|
+
const arrayPrefix = `${messageBase}${tc.arrayKey}`;
|
|
2169
|
+
const indices = collectIndices(attributes, arrayPrefix);
|
|
2170
|
+
const calls = [];
|
|
2171
|
+
for (const j of indices) {
|
|
2172
|
+
const base = `${arrayPrefix}.${j}.${tc.infix}`;
|
|
2173
|
+
const name = attributes[`${base}${tc.nameKey}`];
|
|
2174
|
+
if (name === void 0) continue;
|
|
2175
|
+
const id = attributes[`${base}${tc.idKey}`];
|
|
2176
|
+
const args = parseArgs(attributes[`${base}${tc.argsKey}`]);
|
|
2177
|
+
calls.push({
|
|
2178
|
+
type: "tool-call",
|
|
2179
|
+
toolCallId: id !== void 0 ? String(id) : "",
|
|
2180
|
+
toolName: String(name),
|
|
2181
|
+
args
|
|
2182
|
+
});
|
|
2183
|
+
}
|
|
2184
|
+
return calls;
|
|
2185
|
+
}
|
|
2186
|
+
function readContentsText(attributes, messageBase, config) {
|
|
2187
|
+
var _a;
|
|
2188
|
+
if (!config.contentsKey) return void 0;
|
|
2189
|
+
const contentsPrefix = `${messageBase}${config.contentsKey}`;
|
|
2190
|
+
const indices = collectIndices(attributes, contentsPrefix);
|
|
2191
|
+
if (indices.length === 0) return void 0;
|
|
2192
|
+
const partInfix = (_a = config.contentsPartInfix) != null ? _a : "";
|
|
2193
|
+
const parts = [];
|
|
2194
|
+
for (const j of indices) {
|
|
2195
|
+
const text = attributes[`${contentsPrefix}.${j}.${partInfix}text`];
|
|
2196
|
+
if (typeof text === "string" && text.length > 0) parts.push(text);
|
|
2197
|
+
}
|
|
2198
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
2199
|
+
}
|
|
2200
|
+
function parseIndexedMessages(attributes, config) {
|
|
2201
|
+
var _a, _b;
|
|
2202
|
+
const indices = collectIndices(attributes, config.prefix);
|
|
2203
|
+
if (indices.length === 0) return void 0;
|
|
2204
|
+
const roleKey = (_a = config.roleKey) != null ? _a : "role";
|
|
2205
|
+
const contentKey = (_b = config.contentKey) != null ? _b : "content";
|
|
2206
|
+
const messages = [];
|
|
2207
|
+
for (const i of indices) {
|
|
2208
|
+
const base = `${config.prefix}.${i}.${config.messageInfix}`;
|
|
2209
|
+
const role = attributes[`${base}${roleKey}`];
|
|
2210
|
+
const scalarContent = attributes[`${base}${contentKey}`];
|
|
2211
|
+
const text = typeof scalarContent === "string" ? scalarContent : scalarContent !== void 0 ? String(scalarContent) : readContentsText(attributes, base, config);
|
|
2212
|
+
const toolCalls = readMessageToolCalls(attributes, base, config);
|
|
2213
|
+
if (role === void 0 && text === void 0 && toolCalls.length === 0) continue;
|
|
2214
|
+
let content;
|
|
2215
|
+
if (toolCalls.length > 0) {
|
|
2216
|
+
const parts = [];
|
|
2217
|
+
if (text) parts.push({ type: "text", text });
|
|
2218
|
+
for (const call of toolCalls) {
|
|
2219
|
+
parts.push({
|
|
2220
|
+
type: "tool-call",
|
|
2221
|
+
toolCallId: call.toolCallId,
|
|
2222
|
+
toolName: call.toolName,
|
|
2223
|
+
args: call.args
|
|
2224
|
+
});
|
|
2225
|
+
}
|
|
2226
|
+
content = parts;
|
|
2227
|
+
} else {
|
|
2228
|
+
content = text != null ? text : "";
|
|
2229
|
+
}
|
|
2230
|
+
messages.push({ role: role !== void 0 ? String(role) : "user", content });
|
|
2231
|
+
}
|
|
2232
|
+
return messages.length > 0 ? messages : void 0;
|
|
2233
|
+
}
|
|
2234
|
+
function extractIndexedToolCalls(attributes, config) {
|
|
2235
|
+
if (!config.toolCalls) return void 0;
|
|
2236
|
+
const indices = collectIndices(attributes, config.prefix);
|
|
2237
|
+
const calls = [];
|
|
2238
|
+
for (const i of indices) {
|
|
2239
|
+
const base = `${config.prefix}.${i}.${config.messageInfix}`;
|
|
2240
|
+
calls.push(...readMessageToolCalls(attributes, base, config));
|
|
2241
|
+
}
|
|
2242
|
+
return calls.length > 0 ? calls : void 0;
|
|
2243
|
+
}
|
|
2244
|
+
function messagesToPlainText(messages) {
|
|
2245
|
+
if (!messages || messages.length === 0) return void 0;
|
|
2246
|
+
const chunks = [];
|
|
2247
|
+
for (const msg of messages) {
|
|
2248
|
+
if (typeof msg.content === "string") {
|
|
2249
|
+
if (msg.content.length > 0) chunks.push(msg.content);
|
|
2250
|
+
} else if (Array.isArray(msg.content)) {
|
|
2251
|
+
for (const part of msg.content) {
|
|
2252
|
+
if (typeof part === "string") {
|
|
2253
|
+
if (part.length > 0) chunks.push(part);
|
|
2254
|
+
} else if (part.type === "text" && part.text.length > 0) {
|
|
2255
|
+
chunks.push(part.text);
|
|
2256
|
+
}
|
|
2257
|
+
}
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
return chunks.length > 0 ? chunks.join("\n") : void 0;
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
// src/normalizer/utils/coerce.ts
|
|
2264
|
+
function toNumber(value) {
|
|
2265
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
2266
|
+
if (typeof value === "string") {
|
|
2267
|
+
const n = Number(value);
|
|
2268
|
+
return Number.isFinite(n) ? n : void 0;
|
|
2269
|
+
}
|
|
2270
|
+
return void 0;
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
// src/normalizer/transformers/openinference/index.ts
|
|
2274
|
+
var Attrs2 = {
|
|
2275
|
+
SPAN_KIND: "openinference.span.kind",
|
|
2276
|
+
MODEL: "llm.model_name",
|
|
2277
|
+
PROVIDER: "llm.provider",
|
|
2278
|
+
SYSTEM: "llm.system",
|
|
2279
|
+
INVOCATION_PARAMETERS: "llm.invocation_parameters",
|
|
2280
|
+
TOKEN_PROMPT: "llm.token_count.prompt",
|
|
2281
|
+
TOKEN_COMPLETION: "llm.token_count.completion",
|
|
2282
|
+
TOKEN_TOTAL: "llm.token_count.total",
|
|
2283
|
+
TOKEN_REASONING: "llm.token_count.completion_details.reasoning",
|
|
2284
|
+
INPUT_VALUE: "input.value",
|
|
2285
|
+
INPUT_MIME: "input.mime_type",
|
|
2286
|
+
OUTPUT_VALUE: "output.value",
|
|
2287
|
+
OUTPUT_MIME: "output.mime_type",
|
|
2288
|
+
TOOL_NAME: "tool.name",
|
|
2289
|
+
SESSION_ID: "session.id",
|
|
2290
|
+
USER_ID: "user.id",
|
|
2291
|
+
METADATA: "metadata"
|
|
2292
|
+
};
|
|
2293
|
+
var INPUT_MESSAGES = {
|
|
2294
|
+
prefix: "llm.input_messages",
|
|
2295
|
+
messageInfix: "message.",
|
|
2296
|
+
contentsKey: "contents",
|
|
2297
|
+
contentsPartInfix: "message_content.",
|
|
2298
|
+
toolCalls: {
|
|
2299
|
+
arrayKey: "tool_calls",
|
|
2300
|
+
infix: "tool_call.",
|
|
2301
|
+
idKey: "id",
|
|
2302
|
+
nameKey: "function.name",
|
|
2303
|
+
argsKey: "function.arguments"
|
|
2304
|
+
}
|
|
2305
|
+
};
|
|
2306
|
+
var OUTPUT_MESSAGES = {
|
|
2307
|
+
...INPUT_MESSAGES,
|
|
2308
|
+
prefix: "llm.output_messages"
|
|
2309
|
+
};
|
|
2310
|
+
var RETRIEVAL_PREFIX = "retrieval.documents";
|
|
2311
|
+
function extractSettings(attributes) {
|
|
2312
|
+
var _a, _b, _c, _d, _e;
|
|
2313
|
+
const raw = attributes[Attrs2.INVOCATION_PARAMETERS];
|
|
2314
|
+
if (raw === void 0) return void 0;
|
|
2315
|
+
let parsed;
|
|
2316
|
+
if (typeof raw === "string") {
|
|
2317
|
+
try {
|
|
2318
|
+
parsed = JSON.parse(raw);
|
|
2319
|
+
} catch {
|
|
2320
|
+
return void 0;
|
|
2321
|
+
}
|
|
2322
|
+
} else if (typeof raw === "object" && raw !== null) {
|
|
2323
|
+
parsed = raw;
|
|
2324
|
+
} else {
|
|
2325
|
+
return void 0;
|
|
2326
|
+
}
|
|
2327
|
+
if (!parsed || typeof parsed !== "object") return void 0;
|
|
2328
|
+
const settings = {};
|
|
2329
|
+
const temperature = toNumber(parsed.temperature);
|
|
2330
|
+
const maxTokens = toNumber((_b = (_a = parsed.max_tokens) != null ? _a : parsed.maxTokens) != null ? _b : parsed.max_completion_tokens);
|
|
2331
|
+
const topP = toNumber((_c = parsed.top_p) != null ? _c : parsed.topP);
|
|
2332
|
+
const presencePenalty = toNumber((_d = parsed.presence_penalty) != null ? _d : parsed.presencePenalty);
|
|
2333
|
+
const frequencyPenalty = toNumber((_e = parsed.frequency_penalty) != null ? _e : parsed.frequencyPenalty);
|
|
2334
|
+
if (temperature !== void 0) settings.temperature = temperature;
|
|
2335
|
+
if (maxTokens !== void 0) settings.maxTokens = maxTokens;
|
|
2336
|
+
if (topP !== void 0) settings.topP = topP;
|
|
2337
|
+
if (presencePenalty !== void 0) settings.presencePenalty = presencePenalty;
|
|
2338
|
+
if (frequencyPenalty !== void 0) settings.frequencyPenalty = frequencyPenalty;
|
|
2339
|
+
return Object.keys(settings).length > 0 ? settings : void 0;
|
|
2340
|
+
}
|
|
2341
|
+
function extractGenericInput(attributes) {
|
|
2342
|
+
const value = attributes[Attrs2.INPUT_VALUE];
|
|
2343
|
+
if (value === void 0) return {};
|
|
2344
|
+
const mime = attributes[Attrs2.INPUT_MIME];
|
|
2345
|
+
if (mime === "application/json" && typeof value === "string") {
|
|
2346
|
+
try {
|
|
2347
|
+
const parsed = JSON.parse(value);
|
|
2348
|
+
if (Array.isArray(parsed) && parsed.every((m) => m && typeof m === "object" && "role" in m)) {
|
|
2349
|
+
return { input: parsed };
|
|
2350
|
+
}
|
|
2351
|
+
return { input: [{ role: "user", content: JSON.stringify(parsed) }] };
|
|
2352
|
+
} catch {
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
return { input: [{ role: "user", content: String(value) }] };
|
|
2356
|
+
}
|
|
2357
|
+
function extractGenericOutput(attributes) {
|
|
2358
|
+
const value = attributes[Attrs2.OUTPUT_VALUE];
|
|
2359
|
+
if (value === void 0) return {};
|
|
2360
|
+
const mime = attributes[Attrs2.OUTPUT_MIME];
|
|
2361
|
+
if (mime === "application/json" && typeof value === "string") {
|
|
2362
|
+
try {
|
|
2363
|
+
const parsed = JSON.parse(value);
|
|
2364
|
+
return { output: JSON.stringify(parsed), outputObject: parsed };
|
|
2365
|
+
} catch {
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
return { output: String(value) };
|
|
2369
|
+
}
|
|
2370
|
+
function extractRetrievalDocuments(attributes) {
|
|
2371
|
+
const docs = [];
|
|
2372
|
+
for (const i of collectIndices(attributes, RETRIEVAL_PREFIX)) {
|
|
2373
|
+
const content = attributes[`${RETRIEVAL_PREFIX}.${i}.document.content`];
|
|
2374
|
+
if (typeof content === "string" && content.length > 0) docs.push(content);
|
|
2375
|
+
}
|
|
2376
|
+
return docs.length > 0 ? docs.join("\n\n") : void 0;
|
|
2377
|
+
}
|
|
2378
|
+
var OpenInferenceTransformer = class {
|
|
2379
|
+
classify(_span, attributes) {
|
|
2380
|
+
const kind = attributes[Attrs2.SPAN_KIND];
|
|
2381
|
+
if (typeof kind === "string" && kind.toUpperCase() === "LLM") return "GENERATION" /* GENERATION */;
|
|
2382
|
+
if (attributes[Attrs2.TOKEN_PROMPT] !== void 0 || attributes[Attrs2.TOKEN_COMPLETION] !== void 0) {
|
|
2383
|
+
return "GENERATION" /* GENERATION */;
|
|
2384
|
+
}
|
|
2385
|
+
return "SPAN" /* SPAN */;
|
|
2386
|
+
}
|
|
2387
|
+
transform(_span, attributes) {
|
|
2388
|
+
const result = {};
|
|
2389
|
+
const model = attributes[Attrs2.MODEL];
|
|
2390
|
+
if (typeof model === "string" && model.length > 0) result.model = model;
|
|
2391
|
+
const inputTokens = toNumber(attributes[Attrs2.TOKEN_PROMPT]);
|
|
2392
|
+
const outputTokens = toNumber(attributes[Attrs2.TOKEN_COMPLETION]);
|
|
2393
|
+
const totalTokens = toNumber(attributes[Attrs2.TOKEN_TOTAL]);
|
|
2394
|
+
const reasoningTokens = toNumber(attributes[Attrs2.TOKEN_REASONING]);
|
|
2395
|
+
if (inputTokens !== void 0) result.inputTokens = inputTokens;
|
|
2396
|
+
if (outputTokens !== void 0) result.outputTokens = outputTokens;
|
|
2397
|
+
if (totalTokens !== void 0) {
|
|
2398
|
+
result.totalTokens = totalTokens;
|
|
2399
|
+
} else if (inputTokens !== void 0 && outputTokens !== void 0) {
|
|
2400
|
+
result.totalTokens = inputTokens + outputTokens;
|
|
2401
|
+
}
|
|
2402
|
+
if (reasoningTokens !== void 0) result.reasoningTokens = reasoningTokens;
|
|
2403
|
+
const settings = extractSettings(attributes);
|
|
2404
|
+
if (settings) result.settings = settings;
|
|
2405
|
+
const inputMessages = parseIndexedMessages(attributes, INPUT_MESSAGES);
|
|
2406
|
+
if (inputMessages) {
|
|
2407
|
+
result.input = inputMessages;
|
|
2408
|
+
} else {
|
|
2409
|
+
Object.assign(result, extractGenericInput(attributes));
|
|
2410
|
+
}
|
|
2411
|
+
const outputMessages = parseIndexedMessages(attributes, OUTPUT_MESSAGES);
|
|
2412
|
+
const toolCalls = extractIndexedToolCalls(attributes, OUTPUT_MESSAGES);
|
|
2413
|
+
if (toolCalls) result.toolCalls = toolCalls;
|
|
2414
|
+
if (outputMessages) {
|
|
2415
|
+
const text = messagesToPlainText(outputMessages);
|
|
2416
|
+
if (text) result.output = text;
|
|
2417
|
+
} else {
|
|
2418
|
+
Object.assign(result, extractGenericOutput(attributes));
|
|
2419
|
+
}
|
|
2420
|
+
if (result.output === void 0) {
|
|
2421
|
+
const docs = extractRetrievalDocuments(attributes);
|
|
2422
|
+
if (docs) result.output = docs;
|
|
2423
|
+
}
|
|
2424
|
+
const toolName = attributes[Attrs2.TOOL_NAME];
|
|
2425
|
+
if (typeof toolName === "string" && toolName.length > 0) result.name = toolName;
|
|
2426
|
+
const sessionId = attributes[Attrs2.SESSION_ID];
|
|
2427
|
+
if (sessionId !== void 0) result.sessionId = String(sessionId);
|
|
2428
|
+
const userId = attributes[Attrs2.USER_ID];
|
|
2429
|
+
if (userId !== void 0) result.userId = String(userId);
|
|
2430
|
+
const metadataRaw = attributes[Attrs2.METADATA];
|
|
2431
|
+
if (metadataRaw !== void 0) {
|
|
2432
|
+
let parsed = metadataRaw;
|
|
2433
|
+
if (typeof metadataRaw === "string") {
|
|
2434
|
+
try {
|
|
2435
|
+
parsed = JSON.parse(metadataRaw);
|
|
2436
|
+
} catch {
|
|
2437
|
+
parsed = void 0;
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
2441
|
+
const metadata = {};
|
|
2442
|
+
for (const [k, v] of Object.entries(parsed)) {
|
|
2443
|
+
metadata[k] = typeof v === "string" ? v : JSON.stringify(v);
|
|
2444
|
+
}
|
|
2445
|
+
if (Object.keys(metadata).length > 0) result.metadata = metadata;
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
return result;
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
|
|
2452
|
+
// src/normalizer/transformers/openllmetry/index.ts
|
|
2453
|
+
var Attrs3 = {
|
|
2454
|
+
SPAN_KIND: "traceloop.span.kind",
|
|
2455
|
+
REQUEST_MODEL: "gen_ai.request.model",
|
|
2456
|
+
RESPONSE_MODEL: "gen_ai.response.model",
|
|
2457
|
+
REQUEST_TEMPERATURE: "gen_ai.request.temperature",
|
|
2458
|
+
REQUEST_MAX_TOKENS: "gen_ai.request.max_tokens",
|
|
2459
|
+
REQUEST_TOP_P: "gen_ai.request.top_p",
|
|
2460
|
+
REQUEST_PRESENCE_PENALTY: "gen_ai.request.presence_penalty",
|
|
2461
|
+
REQUEST_FREQUENCY_PENALTY: "gen_ai.request.frequency_penalty",
|
|
2462
|
+
RESPONSE_FINISH_REASON: "gen_ai.response.finish_reason",
|
|
2463
|
+
TOTAL_TOKENS_ALT: "llm.usage.total_tokens",
|
|
2464
|
+
ENTITY_INPUT: "traceloop.entity.input",
|
|
2465
|
+
ENTITY_OUTPUT: "traceloop.entity.output",
|
|
2466
|
+
ENTITY_NAME: "traceloop.entity.name",
|
|
2467
|
+
ASSOCIATION_PREFIX: "traceloop.association.properties."
|
|
2468
|
+
};
|
|
2469
|
+
var PROMPT_MESSAGES = {
|
|
2470
|
+
prefix: "gen_ai.prompt",
|
|
2471
|
+
messageInfix: "",
|
|
2472
|
+
toolCalls: { arrayKey: "tool_calls", infix: "", idKey: "id", nameKey: "name", argsKey: "arguments" }
|
|
2473
|
+
};
|
|
2474
|
+
var COMPLETION_MESSAGES = {
|
|
2475
|
+
...PROMPT_MESSAGES,
|
|
2476
|
+
prefix: "gen_ai.completion"
|
|
2477
|
+
};
|
|
2478
|
+
function extractSettings2(attributes) {
|
|
2479
|
+
const settings = {};
|
|
2480
|
+
const temperature = toNumber(attributes[Attrs3.REQUEST_TEMPERATURE]);
|
|
2481
|
+
const maxTokens = toNumber(attributes[Attrs3.REQUEST_MAX_TOKENS]);
|
|
2482
|
+
const topP = toNumber(attributes[Attrs3.REQUEST_TOP_P]);
|
|
2483
|
+
const presencePenalty = toNumber(attributes[Attrs3.REQUEST_PRESENCE_PENALTY]);
|
|
2484
|
+
const frequencyPenalty = toNumber(attributes[Attrs3.REQUEST_FREQUENCY_PENALTY]);
|
|
2485
|
+
if (temperature !== void 0) settings.temperature = temperature;
|
|
2486
|
+
if (maxTokens !== void 0) settings.maxTokens = maxTokens;
|
|
2487
|
+
if (topP !== void 0) settings.topP = topP;
|
|
2488
|
+
if (presencePenalty !== void 0) settings.presencePenalty = presencePenalty;
|
|
2489
|
+
if (frequencyPenalty !== void 0) settings.frequencyPenalty = frequencyPenalty;
|
|
2490
|
+
return Object.keys(settings).length > 0 ? settings : void 0;
|
|
2491
|
+
}
|
|
2492
|
+
function extractEntityInput(attributes) {
|
|
2493
|
+
const raw = attributes[Attrs3.ENTITY_INPUT];
|
|
2494
|
+
if (raw === void 0) return void 0;
|
|
2495
|
+
const text = typeof raw === "string" ? raw : JSON.stringify(raw);
|
|
2496
|
+
return [{ role: "user", content: text }];
|
|
2497
|
+
}
|
|
2498
|
+
function extractEntityOutput(attributes) {
|
|
2499
|
+
const raw = attributes[Attrs3.ENTITY_OUTPUT];
|
|
2500
|
+
if (raw === void 0) return {};
|
|
2501
|
+
if (typeof raw === "string") {
|
|
2502
|
+
try {
|
|
2503
|
+
const parsed = JSON.parse(raw);
|
|
2504
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
2505
|
+
return { output: raw, outputObject: parsed };
|
|
2506
|
+
}
|
|
2507
|
+
} catch {
|
|
2508
|
+
}
|
|
2509
|
+
return { output: raw };
|
|
2510
|
+
}
|
|
2511
|
+
const result = { output: JSON.stringify(raw) };
|
|
2512
|
+
if (typeof raw === "object" && raw !== null) result.outputObject = raw;
|
|
2513
|
+
return result;
|
|
2514
|
+
}
|
|
2515
|
+
function extractFromEvents(events, attrKey) {
|
|
2516
|
+
var _a, _b, _c;
|
|
2517
|
+
if (!events) return void 0;
|
|
2518
|
+
for (const event of events) {
|
|
2519
|
+
const value = (_c = (_a = event.attributes) == null ? void 0 : _a[attrKey]) != null ? _c : (_b = event.attributes) == null ? void 0 : _b["content"];
|
|
2520
|
+
if (typeof value === "string" && value.length > 0) return value;
|
|
2521
|
+
}
|
|
2522
|
+
return void 0;
|
|
2523
|
+
}
|
|
2524
|
+
function extractFinishReason(attributes) {
|
|
2525
|
+
const direct = attributes[Attrs3.RESPONSE_FINISH_REASON];
|
|
2526
|
+
if (direct !== void 0) {
|
|
2527
|
+
return Array.isArray(direct) ? String(direct[0]) : String(direct);
|
|
2528
|
+
}
|
|
2529
|
+
const indexed = attributes["gen_ai.completion.0.finish_reason"];
|
|
2530
|
+
if (indexed !== void 0) return String(indexed);
|
|
2531
|
+
return void 0;
|
|
2532
|
+
}
|
|
2533
|
+
var OpenLLMetryTransformer = class {
|
|
2534
|
+
classify(_span, attributes) {
|
|
2535
|
+
const kind = attributes[Attrs3.SPAN_KIND];
|
|
2536
|
+
if (typeof kind === "string" && kind.toUpperCase() === "LLM") return "GENERATION" /* GENERATION */;
|
|
2537
|
+
if (attributes["gen_ai.usage.prompt_tokens"] !== void 0 || attributes["gen_ai.usage.completion_tokens"] !== void 0 || attributes["gen_ai.usage.input_tokens"] !== void 0) {
|
|
2538
|
+
return "GENERATION" /* GENERATION */;
|
|
2539
|
+
}
|
|
2540
|
+
return "SPAN" /* SPAN */;
|
|
2541
|
+
}
|
|
2542
|
+
transform(span, attributes) {
|
|
2543
|
+
var _a;
|
|
2544
|
+
const result = {};
|
|
2545
|
+
const model = (_a = attributes[Attrs3.RESPONSE_MODEL]) != null ? _a : attributes[Attrs3.REQUEST_MODEL];
|
|
2546
|
+
if (typeof model === "string" && model.length > 0) result.model = model;
|
|
2547
|
+
const tokens = parseTokens(attributes, {
|
|
2548
|
+
inputKey: "gen_ai.usage.input_tokens",
|
|
2549
|
+
outputKey: "gen_ai.usage.output_tokens",
|
|
2550
|
+
totalKey: "gen_ai.usage.total_tokens",
|
|
2551
|
+
promptKey: "gen_ai.usage.prompt_tokens",
|
|
2552
|
+
completionKey: "gen_ai.usage.completion_tokens"
|
|
2553
|
+
});
|
|
2554
|
+
if (tokens.inputTokens !== void 0) result.inputTokens = tokens.inputTokens;
|
|
2555
|
+
if (tokens.outputTokens !== void 0) result.outputTokens = tokens.outputTokens;
|
|
2556
|
+
if (tokens.totalTokens !== void 0) {
|
|
2557
|
+
result.totalTokens = tokens.totalTokens;
|
|
2558
|
+
} else {
|
|
2559
|
+
const altTotal = toNumber(attributes[Attrs3.TOTAL_TOKENS_ALT]);
|
|
2560
|
+
if (altTotal !== void 0) result.totalTokens = altTotal;
|
|
2561
|
+
}
|
|
2562
|
+
const settings = extractSettings2(attributes);
|
|
2563
|
+
if (settings) result.settings = settings;
|
|
2564
|
+
const finishReason = extractFinishReason(attributes);
|
|
2565
|
+
if (finishReason) result.finishReason = finishReason;
|
|
2566
|
+
const promptMessages = parseIndexedMessages(attributes, PROMPT_MESSAGES);
|
|
2567
|
+
if (promptMessages) {
|
|
2568
|
+
result.input = promptMessages;
|
|
2569
|
+
} else {
|
|
2570
|
+
const entityInput = extractEntityInput(attributes);
|
|
2571
|
+
if (entityInput) {
|
|
2572
|
+
result.input = entityInput;
|
|
2573
|
+
} else {
|
|
2574
|
+
const eventPrompt = extractFromEvents(span.events, "gen_ai.prompt");
|
|
2575
|
+
if (eventPrompt) result.input = [{ role: "user", content: eventPrompt }];
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
const completionMessages = parseIndexedMessages(attributes, COMPLETION_MESSAGES);
|
|
2579
|
+
const toolCalls = extractIndexedToolCalls(attributes, COMPLETION_MESSAGES);
|
|
2580
|
+
if (toolCalls) result.toolCalls = toolCalls;
|
|
2581
|
+
if (completionMessages) {
|
|
2582
|
+
const text = messagesToPlainText(completionMessages);
|
|
2583
|
+
if (text) result.output = text;
|
|
2584
|
+
} else {
|
|
2585
|
+
const entityOutput = extractEntityOutput(attributes);
|
|
2586
|
+
if (entityOutput.output !== void 0) {
|
|
2587
|
+
Object.assign(result, entityOutput);
|
|
2588
|
+
} else {
|
|
2589
|
+
const eventCompletion = extractFromEvents(span.events, "gen_ai.completion");
|
|
2590
|
+
if (eventCompletion) result.output = eventCompletion;
|
|
2591
|
+
}
|
|
2592
|
+
}
|
|
2593
|
+
const entityName = attributes[Attrs3.ENTITY_NAME];
|
|
2594
|
+
if (typeof entityName === "string" && entityName.length > 0) result.traceName = entityName;
|
|
2595
|
+
const metadata = {};
|
|
2596
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
2597
|
+
if (!key.startsWith(Attrs3.ASSOCIATION_PREFIX)) continue;
|
|
2598
|
+
const prop = key.slice(Attrs3.ASSOCIATION_PREFIX.length);
|
|
2599
|
+
if (prop === "session_id") {
|
|
2600
|
+
result.sessionId = String(value);
|
|
2601
|
+
} else if (prop === "user_id") {
|
|
2602
|
+
result.userId = String(value);
|
|
2603
|
+
} else if (prop) {
|
|
2604
|
+
metadata[prop] = typeof value === "string" ? value : JSON.stringify(value);
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
if (Object.keys(metadata).length > 0) result.metadata = metadata;
|
|
2608
|
+
return result;
|
|
2609
|
+
}
|
|
2610
|
+
};
|
|
2611
|
+
|
|
2612
|
+
// src/normalizer/transformers/dispatching/index.ts
|
|
2613
|
+
var OPENINFERENCE_INDEXED = /^llm\.(input_messages|output_messages|token_count)\./;
|
|
2614
|
+
var OPENLLMETRY_INDEXED = /^gen_ai\.(prompt|completion)\.\d+\./;
|
|
2615
|
+
function isOpenInference(attributes) {
|
|
2616
|
+
if (attributes["openinference.span.kind"] !== void 0) return true;
|
|
2617
|
+
if (attributes["llm.model_name"] !== void 0) return true;
|
|
2618
|
+
for (const key of Object.keys(attributes)) {
|
|
2619
|
+
if (OPENINFERENCE_INDEXED.test(key)) return true;
|
|
2620
|
+
}
|
|
2621
|
+
return false;
|
|
2622
|
+
}
|
|
2623
|
+
function isOpenLLMetry(attributes) {
|
|
2624
|
+
for (const key of Object.keys(attributes)) {
|
|
2625
|
+
if (key.startsWith("traceloop.")) return true;
|
|
2626
|
+
if (OPENLLMETRY_INDEXED.test(key)) return true;
|
|
2627
|
+
}
|
|
2628
|
+
return false;
|
|
2629
|
+
}
|
|
2630
|
+
var DispatchingTransformer = class {
|
|
2631
|
+
constructor() {
|
|
2632
|
+
this.openInference = new OpenInferenceTransformer();
|
|
2633
|
+
this.openLLMetry = new OpenLLMetryTransformer();
|
|
2634
|
+
this.otelGenAi = new OtelGenAiTransformer();
|
|
2635
|
+
}
|
|
2636
|
+
/** Choose the extractor for a span from its attribute signature. OpenInference
|
|
2637
|
+
* is checked before OpenLLMetry because its markers (`llm.*`,
|
|
2638
|
+
* `openinference.span.kind`) are more specific; the bare OTel GenAI
|
|
2639
|
+
* transformer is the catch-all. */
|
|
2640
|
+
select(attributes) {
|
|
2641
|
+
if (isOpenInference(attributes)) return this.openInference;
|
|
2642
|
+
if (isOpenLLMetry(attributes)) return this.openLLMetry;
|
|
2643
|
+
return this.otelGenAi;
|
|
2644
|
+
}
|
|
2645
|
+
classify(span, attributes) {
|
|
2646
|
+
return this.select(attributes).classify(span, attributes);
|
|
2647
|
+
}
|
|
2648
|
+
transform(span, attributes) {
|
|
2649
|
+
return this.select(attributes).transform(span, attributes);
|
|
2650
|
+
}
|
|
2651
|
+
};
|
|
2652
|
+
|
|
2139
2653
|
// src/normalizer/converters/otlp-converter.ts
|
|
2140
2654
|
function convertOtlpValue(value) {
|
|
2141
2655
|
var _a;
|
|
@@ -2325,7 +2839,7 @@ registry.register("ai", new AiSdkTransformer());
|
|
|
2325
2839
|
registry.register("default-tracer", new MastraTransformer());
|
|
2326
2840
|
registry.register("agentmark", new AgentMarkTransformer());
|
|
2327
2841
|
registry.register("pydantic-ai", new OtelGenAiTransformer());
|
|
2328
|
-
registry.setDefault(new
|
|
2842
|
+
registry.setDefault(new DispatchingTransformer());
|
|
2329
2843
|
var OTLP_STATUS_CODE_MAP = {
|
|
2330
2844
|
"0": "0",
|
|
2331
2845
|
STATUS_CODE_UNSET: "0",
|
|
@@ -2448,27 +2962,34 @@ export {
|
|
|
2448
2962
|
AgentMarkTransformer,
|
|
2449
2963
|
AiSdkTransformer,
|
|
2450
2964
|
AgentMarkTransformer as ClaudeAgentTransformer,
|
|
2965
|
+
DispatchingTransformer,
|
|
2451
2966
|
MastraTransformer,
|
|
2967
|
+
OpenInferenceTransformer,
|
|
2968
|
+
OpenLLMetryTransformer,
|
|
2452
2969
|
OtelGenAiTransformer,
|
|
2453
2970
|
SEMANTIC_KINDS,
|
|
2454
2971
|
SpanType,
|
|
2455
2972
|
TransformerRegistry,
|
|
2456
2973
|
TypeClassifier,
|
|
2974
|
+
collectIndices,
|
|
2457
2975
|
convertOtlpAttributes,
|
|
2458
2976
|
createSignature,
|
|
2459
2977
|
deriveTraceIO,
|
|
2460
2978
|
detectVersion,
|
|
2461
2979
|
extractCustomMetadata,
|
|
2980
|
+
extractIndexedToolCalls,
|
|
2462
2981
|
extractReasoningFromProviderMetadata,
|
|
2463
2982
|
extractResourceScopeSpan,
|
|
2464
2983
|
fetchPromptsFrontmatter,
|
|
2465
2984
|
findPromptFiles,
|
|
2466
2985
|
generateTypeDefinitions,
|
|
2467
2986
|
generateUnique8CharString,
|
|
2987
|
+
messagesToPlainText,
|
|
2468
2988
|
normalizeOtlpSpans,
|
|
2469
2989
|
normalizeOtlpStatusCode,
|
|
2470
2990
|
normalizeSpan,
|
|
2471
2991
|
parseAgentMarkAttributes,
|
|
2992
|
+
parseIndexedMessages,
|
|
2472
2993
|
parseMetadata,
|
|
2473
2994
|
parseTokens,
|
|
2474
2995
|
registry,
|