@agentmark-ai/shared-utils 0.5.0 → 0.6.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 +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +219 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +217 -65
- package/dist/index.mjs.map +1 -1
- package/dist/trace-io.d.mts +51 -0
- package/dist/trace-io.d.ts +51 -0
- package/dist/trace-io.js +52 -0
- package/dist/trace-io.js.map +1 -0
- package/dist/trace-io.mjs +27 -0
- package/dist/trace-io.mjs.map +1 -0
- package/package.json +24 -2
package/dist/index.mjs
CHANGED
|
@@ -71,53 +71,24 @@ function hexToBytes(hex) {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
// src/serialize.ts
|
|
74
|
+
import yaml from "js-yaml";
|
|
74
75
|
function toFrontMatter(content) {
|
|
75
|
-
|
|
76
|
-
let frontMatter = "";
|
|
77
|
-
const indentation = " ".repeat(indent);
|
|
78
|
-
for (const key in json) {
|
|
79
|
-
if (Object.prototype.hasOwnProperty.call(json, key)) {
|
|
80
|
-
const value = json[key];
|
|
81
|
-
if (typeof value === "object" && !Array.isArray(value)) {
|
|
82
|
-
frontMatter += `${indentation}${key}:
|
|
83
|
-
`;
|
|
84
|
-
frontMatter += jsonToFrontMatter(value, indent + 1);
|
|
85
|
-
} else if (Array.isArray(value)) {
|
|
86
|
-
frontMatter += `${indentation}${key}:
|
|
87
|
-
`;
|
|
88
|
-
value.forEach((item) => {
|
|
89
|
-
if (typeof item === "object") {
|
|
90
|
-
frontMatter += `${indentation}-
|
|
91
|
-
`;
|
|
92
|
-
frontMatter += jsonToFrontMatter(item, indent + 2);
|
|
93
|
-
} else {
|
|
94
|
-
frontMatter += `${indentation}- ${item}
|
|
95
|
-
`;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
frontMatter += `${indentation}${key}: ${value}
|
|
100
|
-
`;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return frontMatter;
|
|
105
|
-
}
|
|
76
|
+
const body = Object.keys(content).length === 0 ? "" : yaml.dump(content, { lineWidth: -1, noRefs: true, skipInvalid: true });
|
|
106
77
|
return `---
|
|
107
|
-
${
|
|
78
|
+
${body}---
|
|
108
79
|
`;
|
|
109
80
|
}
|
|
110
81
|
|
|
111
82
|
// src/generate-types.ts
|
|
112
83
|
import * as fs from "fs-extra";
|
|
113
84
|
import path from "path";
|
|
114
|
-
import
|
|
85
|
+
import yaml2 from "js-yaml";
|
|
115
86
|
function extractFrontmatter(content) {
|
|
116
87
|
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
|
|
117
88
|
if (!match) {
|
|
118
89
|
return { attributes: {} };
|
|
119
90
|
}
|
|
120
|
-
const attributes =
|
|
91
|
+
const attributes = yaml2.load(match[1]);
|
|
121
92
|
return { attributes: attributes || {} };
|
|
122
93
|
}
|
|
123
94
|
var _compile = null;
|
|
@@ -1638,8 +1609,77 @@ var GenAIAttributes = {
|
|
|
1638
1609
|
TOOL_NAME: "gen_ai.tool.name",
|
|
1639
1610
|
TOOL_CALL_ID: "gen_ai.tool.call.id",
|
|
1640
1611
|
TOOL_INPUT: "gen_ai.tool.input",
|
|
1641
|
-
TOOL_OUTPUT: "gen_ai.tool.output"
|
|
1612
|
+
TOOL_OUTPUT: "gen_ai.tool.output",
|
|
1613
|
+
// Spec replacement for the deprecated gen_ai.system attribute.
|
|
1614
|
+
PROVIDER_NAME: "gen_ai.provider.name",
|
|
1615
|
+
// Vendor-namespaced IO keys (dual-emitted by observe()/setInput()/
|
|
1616
|
+
// setOutput() alongside the deprecated gen_ai.request.input /
|
|
1617
|
+
// gen_ai.response.output during the OTel GenAI spec migration).
|
|
1618
|
+
AM_REQUEST_INPUT: "agentmark.request.input",
|
|
1619
|
+
AM_RESPONSE_OUTPUT: "agentmark.response.output",
|
|
1620
|
+
// Standard OTel GenAI semconv content keys (spec status: Development).
|
|
1621
|
+
// Accepted as fallbacks so spec-conformant instrumentation routed to
|
|
1622
|
+
// this transformer doesn't silently lose IO data on ingest.
|
|
1623
|
+
// https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/
|
|
1624
|
+
INPUT_MESSAGES: "gen_ai.input.messages",
|
|
1625
|
+
OUTPUT_MESSAGES: "gen_ai.output.messages",
|
|
1626
|
+
SYSTEM_INSTRUCTIONS: "gen_ai.system_instructions",
|
|
1627
|
+
// Legacy (pre-1.27) OTel GenAI content keys.
|
|
1628
|
+
LEGACY_PROMPT: "gen_ai.prompt",
|
|
1629
|
+
LEGACY_COMPLETION: "gen_ai.completion",
|
|
1630
|
+
// Legacy OTel GenAI usage keys (pre input_tokens/output_tokens rename).
|
|
1631
|
+
USAGE_PROMPT_TOKENS: "gen_ai.usage.prompt_tokens",
|
|
1632
|
+
USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens"
|
|
1642
1633
|
};
|
|
1634
|
+
function parseLooseInput(raw) {
|
|
1635
|
+
try {
|
|
1636
|
+
const parsed = JSON.parse(raw);
|
|
1637
|
+
if (Array.isArray(parsed) && parsed.length > 0) {
|
|
1638
|
+
const isMessagesArray = parsed.every(
|
|
1639
|
+
(item) => item && typeof item === "object" && "role" in item && "content" in item
|
|
1640
|
+
);
|
|
1641
|
+
if (isMessagesArray) {
|
|
1642
|
+
return parsed;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
} catch {
|
|
1646
|
+
}
|
|
1647
|
+
return [{ role: "user", content: raw }];
|
|
1648
|
+
}
|
|
1649
|
+
function specPartsToText(parts) {
|
|
1650
|
+
return parts.filter((p) => p && p.type === "text" && typeof p.content === "string").map((p) => p.content).join("\n");
|
|
1651
|
+
}
|
|
1652
|
+
function parseSpecMessages(raw) {
|
|
1653
|
+
try {
|
|
1654
|
+
const parsed = JSON.parse(raw);
|
|
1655
|
+
if (!Array.isArray(parsed) || parsed.length === 0) return null;
|
|
1656
|
+
const messages = [];
|
|
1657
|
+
for (const msg of parsed) {
|
|
1658
|
+
if (!msg || !msg.role) continue;
|
|
1659
|
+
if (typeof msg.content === "string") {
|
|
1660
|
+
messages.push({ role: msg.role, content: msg.content });
|
|
1661
|
+
} else if (Array.isArray(msg.parts)) {
|
|
1662
|
+
const text = specPartsToText(msg.parts);
|
|
1663
|
+
if (text) messages.push({ role: msg.role, content: text });
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
return messages.length > 0 ? messages : null;
|
|
1667
|
+
} catch {
|
|
1668
|
+
return null;
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
function parseSystemInstructions(raw) {
|
|
1672
|
+
try {
|
|
1673
|
+
const parsed = JSON.parse(raw);
|
|
1674
|
+
if (Array.isArray(parsed)) {
|
|
1675
|
+
const text = specPartsToText(parsed);
|
|
1676
|
+
return text || null;
|
|
1677
|
+
}
|
|
1678
|
+
if (typeof parsed === "string") return parsed || null;
|
|
1679
|
+
} catch {
|
|
1680
|
+
}
|
|
1681
|
+
return raw || null;
|
|
1682
|
+
}
|
|
1643
1683
|
var SpanNames = {
|
|
1644
1684
|
// OTEL GenAI standard operation names (these can be followed by model/tool/agent name)
|
|
1645
1685
|
CHAT: "chat",
|
|
@@ -1702,7 +1742,7 @@ var AgentMarkTransformer = class {
|
|
|
1702
1742
|
if (span.name === SpanNames.CONVERSATION || span.name === SpanNames.SUBAGENT) {
|
|
1703
1743
|
return "SPAN" /* SPAN */;
|
|
1704
1744
|
}
|
|
1705
|
-
if (attributes[GenAIAttributes.SYSTEM] === "anthropic") {
|
|
1745
|
+
if (attributes[GenAIAttributes.SYSTEM] === "anthropic" || attributes[GenAIAttributes.PROVIDER_NAME] === "anthropic") {
|
|
1706
1746
|
if (attributes[GenAIAttributes.USAGE_INPUT_TOKENS] !== void 0 && attributes[GenAIAttributes.RESPONSE_OUTPUT] !== void 0) {
|
|
1707
1747
|
return "GENERATION" /* GENERATION */;
|
|
1708
1748
|
}
|
|
@@ -1713,7 +1753,7 @@ var AgentMarkTransformer = class {
|
|
|
1713
1753
|
* Transform the span and extract normalized fields from GenAI attributes.
|
|
1714
1754
|
*/
|
|
1715
1755
|
transform(_span, attributes) {
|
|
1716
|
-
var _a;
|
|
1756
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1717
1757
|
const result = {};
|
|
1718
1758
|
const responseModel = attributes[GenAIAttributes.RESPONSE_MODEL];
|
|
1719
1759
|
const requestModel = attributes[GenAIAttributes.REQUEST_MODEL];
|
|
@@ -1722,8 +1762,8 @@ var AgentMarkTransformer = class {
|
|
|
1722
1762
|
} else if (requestModel) {
|
|
1723
1763
|
result.model = String(requestModel);
|
|
1724
1764
|
}
|
|
1725
|
-
const inputTokens = attributes[GenAIAttributes.USAGE_INPUT_TOKENS];
|
|
1726
|
-
const outputTokens = attributes[GenAIAttributes.USAGE_OUTPUT_TOKENS];
|
|
1765
|
+
const inputTokens = (_a = attributes[GenAIAttributes.USAGE_INPUT_TOKENS]) != null ? _a : attributes[GenAIAttributes.USAGE_PROMPT_TOKENS];
|
|
1766
|
+
const outputTokens = (_b = attributes[GenAIAttributes.USAGE_OUTPUT_TOKENS]) != null ? _b : attributes[GenAIAttributes.USAGE_COMPLETION_TOKENS];
|
|
1727
1767
|
if (typeof inputTokens === "number") {
|
|
1728
1768
|
result.inputTokens = inputTokens;
|
|
1729
1769
|
}
|
|
@@ -1759,37 +1799,72 @@ var AgentMarkTransformer = class {
|
|
|
1759
1799
|
result.settings.temperature = temperature;
|
|
1760
1800
|
}
|
|
1761
1801
|
}
|
|
1762
|
-
const requestInput = attributes[GenAIAttributes.REQUEST_INPUT];
|
|
1802
|
+
const requestInput = (_c = attributes[GenAIAttributes.REQUEST_INPUT]) != null ? _c : attributes[GenAIAttributes.AM_REQUEST_INPUT];
|
|
1803
|
+
const hasAgentmarkInput = Boolean(
|
|
1804
|
+
(_d = attributes["agentmark.input"]) != null ? _d : attributes["agentmark.props"]
|
|
1805
|
+
);
|
|
1763
1806
|
if (requestInput && typeof requestInput === "string") {
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
} else {
|
|
1776
|
-
result.input = [{ role: "user", content: requestInput }];
|
|
1807
|
+
result.input = parseLooseInput(requestInput);
|
|
1808
|
+
} else if (!hasAgentmarkInput) {
|
|
1809
|
+
const inputMessages = attributes[GenAIAttributes.INPUT_MESSAGES];
|
|
1810
|
+
if (inputMessages && typeof inputMessages === "string") {
|
|
1811
|
+
const messages = parseSpecMessages(inputMessages);
|
|
1812
|
+
if (messages) result.input = messages;
|
|
1813
|
+
}
|
|
1814
|
+
if (!result.input) {
|
|
1815
|
+
const legacyPrompt = attributes[GenAIAttributes.LEGACY_PROMPT];
|
|
1816
|
+
if (legacyPrompt && typeof legacyPrompt === "string") {
|
|
1817
|
+
result.input = parseLooseInput(legacyPrompt);
|
|
1777
1818
|
}
|
|
1778
|
-
} catch {
|
|
1779
|
-
result.input = [{ role: "user", content: requestInput }];
|
|
1780
1819
|
}
|
|
1781
1820
|
}
|
|
1782
|
-
const
|
|
1821
|
+
const systemInstructions = attributes[GenAIAttributes.SYSTEM_INSTRUCTIONS];
|
|
1822
|
+
if (systemInstructions && typeof systemInstructions === "string" && !hasAgentmarkInput) {
|
|
1823
|
+
const text = parseSystemInstructions(systemInstructions);
|
|
1824
|
+
if (text && (!result.input || ((_e = result.input[0]) == null ? void 0 : _e.role) !== "system")) {
|
|
1825
|
+
result.input = [{ role: "system", content: text }, ...(_f = result.input) != null ? _f : []];
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
const responseOutput = (_g = attributes[GenAIAttributes.RESPONSE_OUTPUT]) != null ? _g : attributes[GenAIAttributes.AM_RESPONSE_OUTPUT];
|
|
1829
|
+
const hasAgentmarkOutput = Boolean(attributes["agentmark.output"]);
|
|
1783
1830
|
if (responseOutput && typeof responseOutput === "string") {
|
|
1784
1831
|
result.output = responseOutput;
|
|
1785
1832
|
try {
|
|
1786
1833
|
result.outputObject = JSON.parse(responseOutput);
|
|
1787
1834
|
} catch {
|
|
1788
1835
|
}
|
|
1836
|
+
} else if (!hasAgentmarkOutput) {
|
|
1837
|
+
const outputMessages = attributes[GenAIAttributes.OUTPUT_MESSAGES];
|
|
1838
|
+
if (outputMessages && typeof outputMessages === "string") {
|
|
1839
|
+
const messages = parseSpecMessages(outputMessages);
|
|
1840
|
+
if (messages) {
|
|
1841
|
+
result.output = messages.map((m) => m.content).join("\n");
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
if (result.output === void 0) {
|
|
1845
|
+
const legacyCompletion = attributes[GenAIAttributes.LEGACY_COMPLETION];
|
|
1846
|
+
if (legacyCompletion && typeof legacyCompletion === "string") {
|
|
1847
|
+
result.output = legacyCompletion;
|
|
1848
|
+
try {
|
|
1849
|
+
result.outputObject = JSON.parse(legacyCompletion);
|
|
1850
|
+
} catch {
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1789
1854
|
}
|
|
1790
|
-
const amInput = (
|
|
1855
|
+
const amInput = (_h = attributes["agentmark.input"]) != null ? _h : attributes["agentmark.props"];
|
|
1791
1856
|
if (amInput && typeof amInput === "string" && !result.input) {
|
|
1792
|
-
|
|
1857
|
+
let parsedMessages = null;
|
|
1858
|
+
try {
|
|
1859
|
+
const parsed = JSON.parse(amInput);
|
|
1860
|
+
if (Array.isArray(parsed) && parsed.length > 0 && parsed.every(
|
|
1861
|
+
(item) => item && typeof item === "object" && "role" in item && "content" in item
|
|
1862
|
+
)) {
|
|
1863
|
+
parsedMessages = parsed;
|
|
1864
|
+
}
|
|
1865
|
+
} catch {
|
|
1866
|
+
}
|
|
1867
|
+
result.input = parsedMessages != null ? parsedMessages : [{ role: "user", content: amInput }];
|
|
1793
1868
|
}
|
|
1794
1869
|
const amOutput = attributes["agentmark.output"];
|
|
1795
1870
|
if (amOutput && typeof amOutput === "string" && !result.output) {
|
|
@@ -1865,6 +1940,13 @@ var Attrs = {
|
|
|
1865
1940
|
RESPONSE_OUTPUT_FALLBACK: "gen_ai.response.output",
|
|
1866
1941
|
SYSTEM_INSTRUCTIONS: "gen_ai.system_instructions",
|
|
1867
1942
|
TOOL_DEFINITIONS: "gen_ai.tool.definitions",
|
|
1943
|
+
// Legacy (pre-1.27) OTel GenAI content keys, accepted as last-resort
|
|
1944
|
+
// fallbacks below the canonical and AgentMark-scoped keys.
|
|
1945
|
+
LEGACY_PROMPT: "gen_ai.prompt",
|
|
1946
|
+
LEGACY_COMPLETION: "gen_ai.completion",
|
|
1947
|
+
// Legacy OTel GenAI usage keys (pre input_tokens/output_tokens rename).
|
|
1948
|
+
USAGE_PROMPT_TOKENS: "gen_ai.usage.prompt_tokens",
|
|
1949
|
+
USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens",
|
|
1868
1950
|
// Tool call attributes (v1.37.0+)
|
|
1869
1951
|
TOOL_NAME: "gen_ai.tool.name",
|
|
1870
1952
|
TOOL_CALL_ID: "gen_ai.tool.call.id",
|
|
@@ -1927,14 +2009,14 @@ var OtelGenAiTransformer = class {
|
|
|
1927
2009
|
return "SPAN" /* SPAN */;
|
|
1928
2010
|
}
|
|
1929
2011
|
transform(span, attributes) {
|
|
1930
|
-
var _a, _b;
|
|
2012
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1931
2013
|
const result = {};
|
|
1932
2014
|
const model = attributes[Attrs.RESPONSE_MODEL] || attributes[Attrs.REQUEST_MODEL];
|
|
1933
2015
|
if (model && typeof model === "string") {
|
|
1934
2016
|
result.model = model;
|
|
1935
2017
|
}
|
|
1936
|
-
const inputTokens = attributes[Attrs.USAGE_INPUT_TOKENS];
|
|
1937
|
-
const outputTokens = attributes[Attrs.USAGE_OUTPUT_TOKENS];
|
|
2018
|
+
const inputTokens = (_a = attributes[Attrs.USAGE_INPUT_TOKENS]) != null ? _a : attributes[Attrs.USAGE_PROMPT_TOKENS];
|
|
2019
|
+
const outputTokens = (_b = attributes[Attrs.USAGE_OUTPUT_TOKENS]) != null ? _b : attributes[Attrs.USAGE_COMPLETION_TOKENS];
|
|
1938
2020
|
if (typeof inputTokens === "number") result.inputTokens = inputTokens;
|
|
1939
2021
|
if (typeof outputTokens === "number") result.outputTokens = outputTokens;
|
|
1940
2022
|
if (result.inputTokens !== void 0 && result.outputTokens !== void 0) {
|
|
@@ -1948,12 +2030,29 @@ var OtelGenAiTransformer = class {
|
|
|
1948
2030
|
if (typeof temperature === "number") {
|
|
1949
2031
|
result.settings = { ...result.settings, temperature };
|
|
1950
2032
|
}
|
|
1951
|
-
const inputMessages = (
|
|
2033
|
+
const inputMessages = (_d = (_c = attributes[Attrs.INPUT_MESSAGES]) != null ? _c : attributes[Attrs.REQUEST_INPUT_FALLBACK]) != null ? _d : attributes[Attrs.LEGACY_PROMPT];
|
|
1952
2034
|
if (inputMessages && typeof inputMessages === "string") {
|
|
1953
2035
|
const messages = normalizeMessages(inputMessages);
|
|
1954
2036
|
if (messages) result.input = messages;
|
|
1955
2037
|
}
|
|
1956
|
-
const
|
|
2038
|
+
const systemInstructions = attributes[Attrs.SYSTEM_INSTRUCTIONS];
|
|
2039
|
+
if (systemInstructions && typeof systemInstructions === "string") {
|
|
2040
|
+
let text = null;
|
|
2041
|
+
try {
|
|
2042
|
+
const parsed = JSON.parse(systemInstructions);
|
|
2043
|
+
if (Array.isArray(parsed)) {
|
|
2044
|
+
text = partsToText(parsed) || null;
|
|
2045
|
+
} else if (typeof parsed === "string") {
|
|
2046
|
+
text = parsed || null;
|
|
2047
|
+
}
|
|
2048
|
+
} catch {
|
|
2049
|
+
text = systemInstructions;
|
|
2050
|
+
}
|
|
2051
|
+
if (text && (!result.input || ((_e = result.input[0]) == null ? void 0 : _e.role) !== "system")) {
|
|
2052
|
+
result.input = [{ role: "system", content: text }, ...(_f = result.input) != null ? _f : []];
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
const outputMessages = (_h = (_g = attributes[Attrs.OUTPUT_MESSAGES]) != null ? _g : attributes[Attrs.RESPONSE_OUTPUT_FALLBACK]) != null ? _h : attributes[Attrs.LEGACY_COMPLETION];
|
|
1957
2056
|
if (outputMessages && typeof outputMessages === "string") {
|
|
1958
2057
|
const extracted = extractStructuredOutput(outputMessages);
|
|
1959
2058
|
if (extracted) {
|
|
@@ -1961,6 +2060,8 @@ var OtelGenAiTransformer = class {
|
|
|
1961
2060
|
if (extracted.outputObject) {
|
|
1962
2061
|
result.outputObject = extracted.outputObject;
|
|
1963
2062
|
}
|
|
2063
|
+
} else {
|
|
2064
|
+
result.output = outputMessages;
|
|
1964
2065
|
}
|
|
1965
2066
|
}
|
|
1966
2067
|
const allMessages = attributes["pydantic_ai.all_messages"];
|
|
@@ -2205,7 +2306,7 @@ function resolveSemanticKind(normalized, allAttributes) {
|
|
|
2205
2306
|
// src/normalizer/type-classifier.ts
|
|
2206
2307
|
var TypeClassifier = class {
|
|
2207
2308
|
classify(span, attributes) {
|
|
2208
|
-
if (attributes["gen_ai.system"] || attributes["gen_ai.request.model"] || attributes["gen_ai.operation.name"]) {
|
|
2309
|
+
if (attributes["gen_ai.system"] || attributes["gen_ai.provider.name"] || attributes["gen_ai.request.model"] || attributes["gen_ai.operation.name"]) {
|
|
2209
2310
|
return "GENERATION" /* GENERATION */;
|
|
2210
2311
|
}
|
|
2211
2312
|
if (attributes["ai.response.text"] || attributes["ai.result.text"] || attributes["ai.response.toolCalls"] || attributes["ai.result.toolCalls"]) {
|
|
@@ -2225,6 +2326,28 @@ registry.register("default-tracer", new MastraTransformer());
|
|
|
2225
2326
|
registry.register("agentmark", new AgentMarkTransformer());
|
|
2226
2327
|
registry.register("pydantic-ai", new OtelGenAiTransformer());
|
|
2227
2328
|
registry.setDefault(new OtelGenAiTransformer());
|
|
2329
|
+
var OTLP_STATUS_CODE_MAP = {
|
|
2330
|
+
"0": "0",
|
|
2331
|
+
STATUS_CODE_UNSET: "0",
|
|
2332
|
+
UNSET: "0",
|
|
2333
|
+
Unset: "0",
|
|
2334
|
+
"1": "1",
|
|
2335
|
+
STATUS_CODE_OK: "1",
|
|
2336
|
+
OK: "1",
|
|
2337
|
+
Ok: "1",
|
|
2338
|
+
"2": "2",
|
|
2339
|
+
STATUS_CODE_ERROR: "2",
|
|
2340
|
+
ERROR: "2",
|
|
2341
|
+
Error: "2"
|
|
2342
|
+
};
|
|
2343
|
+
function normalizeOtlpStatusCode(raw) {
|
|
2344
|
+
var _a;
|
|
2345
|
+
if (raw === void 0 || raw === null || raw === "") {
|
|
2346
|
+
return "0";
|
|
2347
|
+
}
|
|
2348
|
+
const key = String(raw);
|
|
2349
|
+
return (_a = OTLP_STATUS_CODE_MAP[key]) != null ? _a : key;
|
|
2350
|
+
}
|
|
2228
2351
|
function normalizeSpan(resource, scope, span) {
|
|
2229
2352
|
var _a, _b, _c;
|
|
2230
2353
|
const allAttributes = {
|
|
@@ -2257,7 +2380,7 @@ function normalizeSpan(resource, scope, span) {
|
|
|
2257
2380
|
name: span.name,
|
|
2258
2381
|
kind: span.kind.toString(),
|
|
2259
2382
|
serviceName: (_a = resource.attributes) == null ? void 0 : _a["service.name"],
|
|
2260
|
-
statusCode: ((_b = span.status) == null ? void 0 : _b.code
|
|
2383
|
+
statusCode: normalizeOtlpStatusCode((_b = span.status) == null ? void 0 : _b.code),
|
|
2261
2384
|
statusMessage: (_c = span.status) == null ? void 0 : _c.message,
|
|
2262
2385
|
// Raw Data
|
|
2263
2386
|
resourceAttributes: resource.attributes || {},
|
|
@@ -2280,6 +2403,9 @@ function normalizeSpan(resource, scope, span) {
|
|
|
2280
2403
|
}
|
|
2281
2404
|
const agentMarkAttributes = parseAgentMarkAttributes(allAttributes);
|
|
2282
2405
|
Object.assign(normalized, agentMarkAttributes);
|
|
2406
|
+
if (!normalized.sessionId && allAttributes["gen_ai.conversation.id"]) {
|
|
2407
|
+
normalized.sessionId = String(allAttributes["gen_ai.conversation.id"]);
|
|
2408
|
+
}
|
|
2283
2409
|
normalized.semanticKind = resolveSemanticKind(normalized, allAttributes);
|
|
2284
2410
|
return normalized;
|
|
2285
2411
|
}
|
|
@@ -2293,6 +2419,30 @@ function normalizeOtlpSpans(resourceSpans) {
|
|
|
2293
2419
|
}
|
|
2294
2420
|
return normalizedSpans;
|
|
2295
2421
|
}
|
|
2422
|
+
|
|
2423
|
+
// src/trace-io.ts
|
|
2424
|
+
function present(v) {
|
|
2425
|
+
if (v === null || v === void 0) return false;
|
|
2426
|
+
if (typeof v === "string") return v.length > 0;
|
|
2427
|
+
return true;
|
|
2428
|
+
}
|
|
2429
|
+
function deriveTraceIO(spans) {
|
|
2430
|
+
var _a, _b;
|
|
2431
|
+
const rootSpan = (_a = spans.find((s) => !s.parentId)) != null ? _a : spans[0];
|
|
2432
|
+
const generationSpans = spans.filter((s) => s.type === "GENERATION").sort((a, b) => {
|
|
2433
|
+
var _a2, _b2;
|
|
2434
|
+
const ta = (_a2 = a.timestamp) != null ? _a2 : 0;
|
|
2435
|
+
const tb = (_b2 = b.timestamp) != null ? _b2 : 0;
|
|
2436
|
+
return ta < tb ? -1 : ta > tb ? 1 : 0;
|
|
2437
|
+
});
|
|
2438
|
+
const input = present(rootSpan == null ? void 0 : rootSpan.input) ? rootSpan == null ? void 0 : rootSpan.input : (_b = generationSpans.find((s) => present(s.input))) == null ? void 0 : _b.input;
|
|
2439
|
+
const lastGenWithOutput = [...generationSpans].reverse().find((s) => present(s.output));
|
|
2440
|
+
const output = present(rootSpan == null ? void 0 : rootSpan.output) ? rootSpan == null ? void 0 : rootSpan.output : lastGenWithOutput == null ? void 0 : lastGenWithOutput.output;
|
|
2441
|
+
return {
|
|
2442
|
+
...present(input) ? { input } : {},
|
|
2443
|
+
...present(output) ? { output } : {}
|
|
2444
|
+
};
|
|
2445
|
+
}
|
|
2296
2446
|
export {
|
|
2297
2447
|
AGENTMARK_SCOPE_NAME,
|
|
2298
2448
|
AgentMarkTransformer,
|
|
@@ -2306,6 +2456,7 @@ export {
|
|
|
2306
2456
|
TypeClassifier,
|
|
2307
2457
|
convertOtlpAttributes,
|
|
2308
2458
|
createSignature,
|
|
2459
|
+
deriveTraceIO,
|
|
2309
2460
|
detectVersion,
|
|
2310
2461
|
extractCustomMetadata,
|
|
2311
2462
|
extractReasoningFromProviderMetadata,
|
|
@@ -2315,6 +2466,7 @@ export {
|
|
|
2315
2466
|
generateTypeDefinitions,
|
|
2316
2467
|
generateUnique8CharString,
|
|
2317
2468
|
normalizeOtlpSpans,
|
|
2469
|
+
normalizeOtlpStatusCode,
|
|
2318
2470
|
normalizeSpan,
|
|
2319
2471
|
parseAgentMarkAttributes,
|
|
2320
2472
|
parseMetadata,
|