@microsoft/opentelemetry 0.1.0-alpha.6 → 1.0.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/README.md +90 -3
- package/dist/commonjs/a365/configuration/A365Configuration.d.ts +32 -0
- package/dist/commonjs/a365/configuration/A365Configuration.d.ts.map +1 -1
- package/dist/commonjs/a365/configuration/A365Configuration.js +74 -5
- package/dist/commonjs/a365/configuration/A365Configuration.js.map +1 -1
- package/dist/commonjs/a365/configuration/A365ConfigurationOptions.d.ts +49 -0
- package/dist/commonjs/a365/configuration/A365ConfigurationOptions.d.ts.map +1 -1
- package/dist/commonjs/a365/constants.d.ts +1 -1
- package/dist/commonjs/a365/constants.d.ts.map +1 -1
- package/dist/commonjs/a365/exporter/Agent365Exporter.d.ts +4 -2
- package/dist/commonjs/a365/exporter/Agent365Exporter.d.ts.map +1 -1
- package/dist/commonjs/a365/exporter/Agent365Exporter.js +55 -30
- package/dist/commonjs/a365/exporter/Agent365Exporter.js.map +1 -1
- package/dist/commonjs/a365/exporter/Agent365ExporterOptions.d.ts +3 -0
- package/dist/commonjs/a365/exporter/Agent365ExporterOptions.d.ts.map +1 -1
- package/dist/commonjs/a365/exporter/Agent365ExporterOptions.js +5 -1
- package/dist/commonjs/a365/exporter/Agent365ExporterOptions.js.map +1 -1
- package/dist/commonjs/a365/exporter/utils.d.ts +29 -1
- package/dist/commonjs/a365/exporter/utils.d.ts.map +1 -1
- package/dist/commonjs/a365/exporter/utils.js +121 -5
- package/dist/commonjs/a365/exporter/utils.js.map +1 -1
- package/dist/commonjs/a365/hosting/agenticTokenCache.d.ts +58 -0
- package/dist/commonjs/a365/hosting/agenticTokenCache.d.ts.map +1 -0
- package/dist/commonjs/a365/hosting/agenticTokenCache.js +229 -0
- package/dist/commonjs/a365/hosting/agenticTokenCache.js.map +1 -0
- package/dist/commonjs/a365/hosting/index.d.ts +2 -0
- package/dist/commonjs/a365/hosting/index.d.ts.map +1 -1
- package/dist/commonjs/a365/hosting/index.js +4 -1
- package/dist/commonjs/a365/hosting/index.js.map +1 -1
- package/dist/commonjs/a365/index.d.ts +2 -2
- package/dist/commonjs/a365/index.d.ts.map +1 -1
- package/dist/commonjs/a365/index.js +3 -1
- package/dist/commonjs/a365/index.js.map +1 -1
- package/dist/commonjs/azureMonitor/index.d.ts +9 -1
- package/dist/commonjs/azureMonitor/index.d.ts.map +1 -1
- package/dist/commonjs/azureMonitor/index.js +13 -18
- package/dist/commonjs/azureMonitor/index.js.map +1 -1
- package/dist/commonjs/azureMonitor/metrics/quickpulse/liveMetrics.js +1 -1
- package/dist/commonjs/azureMonitor/metrics/quickpulse/liveMetrics.js.map +1 -1
- package/dist/commonjs/azureMonitor/metrics/quickpulse/utils.d.ts.map +1 -1
- package/dist/commonjs/azureMonitor/metrics/quickpulse/utils.js +4 -6
- package/dist/commonjs/azureMonitor/metrics/quickpulse/utils.js.map +1 -1
- package/dist/commonjs/distro/distro.d.ts +11 -2
- package/dist/commonjs/distro/distro.d.ts.map +1 -1
- package/dist/commonjs/distro/distro.js +183 -36
- package/dist/commonjs/distro/distro.js.map +1 -1
- package/dist/commonjs/distro/loader.d.ts +10 -0
- package/dist/commonjs/distro/loader.d.ts.map +1 -0
- package/dist/commonjs/distro/loader.js +16 -0
- package/dist/commonjs/distro/loader.js.map +1 -0
- package/dist/commonjs/genai/instrumentations/langchain/langchainTraceInstrumentor.d.ts +2 -6
- package/dist/commonjs/genai/instrumentations/langchain/langchainTraceInstrumentor.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/langchain/langchainTraceInstrumentor.js +10 -14
- package/dist/commonjs/genai/instrumentations/langchain/langchainTraceInstrumentor.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/langchain/tracer.d.ts +9 -8
- package/dist/commonjs/genai/instrumentations/langchain/tracer.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/langchain/tracer.js +40 -15
- package/dist/commonjs/genai/instrumentations/langchain/tracer.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/langchain/utils.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/langchain/utils.js +259 -75
- package/dist/commonjs/genai/instrumentations/langchain/utils.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.js +0 -1
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceProcessor.d.ts +4 -5
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceProcessor.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceProcessor.js +72 -37
- package/dist/commonjs/genai/instrumentations/openai/openAIAgentsTraceProcessor.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/semconv.d.ts +0 -2
- package/dist/commonjs/genai/instrumentations/openai/semconv.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/semconv.js +1 -3
- package/dist/commonjs/genai/instrumentations/openai/semconv.js.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/utils.d.ts +37 -16
- package/dist/commonjs/genai/instrumentations/openai/utils.d.ts.map +1 -1
- package/dist/commonjs/genai/instrumentations/openai/utils.js +239 -60
- package/dist/commonjs/genai/instrumentations/openai/utils.js.map +1 -1
- package/dist/commonjs/genai/semconv.d.ts +2 -0
- package/dist/commonjs/genai/semconv.d.ts.map +1 -1
- package/dist/commonjs/genai/semconv.js +5 -1
- package/dist/commonjs/genai/semconv.js.map +1 -1
- package/dist/commonjs/index.d.ts +2 -2
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +3 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/sdkstats/index.d.ts +18 -0
- package/dist/commonjs/sdkstats/index.d.ts.map +1 -0
- package/dist/commonjs/sdkstats/index.js +37 -0
- package/dist/commonjs/sdkstats/index.js.map +1 -0
- package/dist/commonjs/sdkstats/manager.d.ts +31 -0
- package/dist/commonjs/sdkstats/manager.d.ts.map +1 -0
- package/dist/commonjs/sdkstats/manager.js +190 -0
- package/dist/commonjs/sdkstats/manager.js.map +1 -0
- package/dist/commonjs/sdkstats/metrics.d.ts +20 -0
- package/dist/commonjs/sdkstats/metrics.d.ts.map +1 -0
- package/dist/commonjs/sdkstats/metrics.js +84 -0
- package/dist/commonjs/sdkstats/metrics.js.map +1 -0
- package/dist/commonjs/sdkstats/state.d.ts +58 -0
- package/dist/commonjs/sdkstats/state.d.ts.map +1 -0
- package/dist/commonjs/sdkstats/state.js +94 -0
- package/dist/commonjs/sdkstats/state.js.map +1 -0
- package/dist/commonjs/types.d.ts +6 -9
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +5 -1
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/utils/common.js +2 -2
- package/dist/commonjs/utils/common.js.map +1 -1
- package/dist/commonjs/utils/index.d.ts +2 -0
- package/dist/commonjs/utils/index.d.ts.map +1 -1
- package/dist/commonjs/utils/index.js +5 -1
- package/dist/commonjs/utils/index.js.map +1 -1
- package/dist/commonjs/utils/opentelemetryInstrumentationPatcher.d.ts.map +1 -0
- package/dist/commonjs/{azureMonitor/utils → utils}/opentelemetryInstrumentationPatcher.js +13 -11
- package/dist/commonjs/utils/opentelemetryInstrumentationPatcher.js.map +1 -0
- package/dist/commonjs/{azureMonitor/utils → utils}/statsbeat.d.ts +1 -1
- package/dist/commonjs/utils/statsbeat.d.ts.map +1 -0
- package/dist/commonjs/{azureMonitor/utils → utils}/statsbeat.js +8 -8
- package/dist/commonjs/utils/statsbeat.js.map +1 -0
- package/dist/esm/a365/configuration/A365Configuration.d.ts +32 -0
- package/dist/esm/a365/configuration/A365Configuration.d.ts.map +1 -1
- package/dist/esm/a365/configuration/A365Configuration.js +74 -5
- package/dist/esm/a365/configuration/A365Configuration.js.map +1 -1
- package/dist/esm/a365/configuration/A365ConfigurationOptions.d.ts +49 -0
- package/dist/esm/a365/configuration/A365ConfigurationOptions.d.ts.map +1 -1
- package/dist/esm/a365/constants.d.ts +1 -1
- package/dist/esm/a365/constants.d.ts.map +1 -1
- package/dist/esm/a365/exporter/Agent365Exporter.d.ts +4 -2
- package/dist/esm/a365/exporter/Agent365Exporter.d.ts.map +1 -1
- package/dist/esm/a365/exporter/Agent365Exporter.js +56 -31
- package/dist/esm/a365/exporter/Agent365Exporter.js.map +1 -1
- package/dist/esm/a365/exporter/Agent365ExporterOptions.d.ts +3 -0
- package/dist/esm/a365/exporter/Agent365ExporterOptions.d.ts.map +1 -1
- package/dist/esm/a365/exporter/Agent365ExporterOptions.js +5 -1
- package/dist/esm/a365/exporter/Agent365ExporterOptions.js.map +1 -1
- package/dist/esm/a365/exporter/utils.d.ts +29 -1
- package/dist/esm/a365/exporter/utils.d.ts.map +1 -1
- package/dist/esm/a365/exporter/utils.js +119 -6
- package/dist/esm/a365/exporter/utils.js.map +1 -1
- package/dist/esm/a365/hosting/agenticTokenCache.d.ts +58 -0
- package/dist/esm/a365/hosting/agenticTokenCache.d.ts.map +1 -0
- package/dist/esm/a365/hosting/agenticTokenCache.js +225 -0
- package/dist/esm/a365/hosting/agenticTokenCache.js.map +1 -0
- package/dist/esm/a365/hosting/index.d.ts +2 -0
- package/dist/esm/a365/hosting/index.d.ts.map +1 -1
- package/dist/esm/a365/hosting/index.js +1 -0
- package/dist/esm/a365/hosting/index.js.map +1 -1
- package/dist/esm/a365/index.d.ts +2 -2
- package/dist/esm/a365/index.d.ts.map +1 -1
- package/dist/esm/a365/index.js +1 -1
- package/dist/esm/a365/index.js.map +1 -1
- package/dist/esm/azureMonitor/index.d.ts +9 -1
- package/dist/esm/azureMonitor/index.d.ts.map +1 -1
- package/dist/esm/azureMonitor/index.js +12 -18
- package/dist/esm/azureMonitor/index.js.map +1 -1
- package/dist/esm/azureMonitor/metrics/quickpulse/liveMetrics.js +1 -1
- package/dist/esm/azureMonitor/metrics/quickpulse/liveMetrics.js.map +1 -1
- package/dist/esm/azureMonitor/metrics/quickpulse/utils.d.ts.map +1 -1
- package/dist/esm/azureMonitor/metrics/quickpulse/utils.js +5 -7
- package/dist/esm/azureMonitor/metrics/quickpulse/utils.js.map +1 -1
- package/dist/esm/distro/distro.d.ts +11 -2
- package/dist/esm/distro/distro.d.ts.map +1 -1
- package/dist/esm/distro/distro.js +181 -35
- package/dist/esm/distro/distro.js.map +1 -1
- package/dist/esm/distro/loader.d.ts +10 -0
- package/dist/esm/distro/loader.d.ts.map +1 -0
- package/dist/esm/distro/loader.js +14 -0
- package/dist/esm/distro/loader.js.map +1 -0
- package/dist/esm/genai/instrumentations/langchain/langchainTraceInstrumentor.d.ts +2 -6
- package/dist/esm/genai/instrumentations/langchain/langchainTraceInstrumentor.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/langchain/langchainTraceInstrumentor.js +10 -14
- package/dist/esm/genai/instrumentations/langchain/langchainTraceInstrumentor.js.map +1 -1
- package/dist/esm/genai/instrumentations/langchain/tracer.d.ts +9 -8
- package/dist/esm/genai/instrumentations/langchain/tracer.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/langchain/tracer.js +41 -16
- package/dist/esm/genai/instrumentations/langchain/tracer.js.map +1 -1
- package/dist/esm/genai/instrumentations/langchain/utils.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/langchain/utils.js +260 -76
- package/dist/esm/genai/instrumentations/langchain/utils.js.map +1 -1
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.js +0 -1
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceInstrumentor.js.map +1 -1
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceProcessor.d.ts +4 -5
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceProcessor.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceProcessor.js +75 -40
- package/dist/esm/genai/instrumentations/openai/openAIAgentsTraceProcessor.js.map +1 -1
- package/dist/esm/genai/instrumentations/openai/semconv.d.ts +0 -2
- package/dist/esm/genai/instrumentations/openai/semconv.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/openai/semconv.js +0 -2
- package/dist/esm/genai/instrumentations/openai/semconv.js.map +1 -1
- package/dist/esm/genai/instrumentations/openai/utils.d.ts +37 -16
- package/dist/esm/genai/instrumentations/openai/utils.d.ts.map +1 -1
- package/dist/esm/genai/instrumentations/openai/utils.js +233 -57
- package/dist/esm/genai/instrumentations/openai/utils.js.map +1 -1
- package/dist/esm/genai/semconv.d.ts +2 -0
- package/dist/esm/genai/semconv.d.ts.map +1 -1
- package/dist/esm/genai/semconv.js +4 -0
- package/dist/esm/genai/semconv.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/sdkstats/index.d.ts +18 -0
- package/dist/esm/sdkstats/index.d.ts.map +1 -0
- package/dist/esm/sdkstats/index.js +20 -0
- package/dist/esm/sdkstats/index.js.map +1 -0
- package/dist/esm/sdkstats/manager.d.ts +31 -0
- package/dist/esm/sdkstats/manager.d.ts.map +1 -0
- package/dist/esm/sdkstats/manager.js +186 -0
- package/dist/esm/sdkstats/manager.js.map +1 -0
- package/dist/esm/sdkstats/metrics.d.ts +20 -0
- package/dist/esm/sdkstats/metrics.d.ts.map +1 -0
- package/dist/esm/sdkstats/metrics.js +77 -0
- package/dist/esm/sdkstats/metrics.js.map +1 -0
- package/dist/esm/sdkstats/state.d.ts +58 -0
- package/dist/esm/sdkstats/state.d.ts.map +1 -0
- package/dist/esm/sdkstats/state.js +83 -0
- package/dist/esm/sdkstats/state.js.map +1 -0
- package/dist/esm/types.d.ts +6 -9
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +5 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/common.js +2 -2
- package/dist/esm/utils/common.js.map +1 -1
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/opentelemetryInstrumentationPatcher.d.ts.map +1 -0
- package/dist/esm/{azureMonitor/utils → utils}/opentelemetryInstrumentationPatcher.js +13 -11
- package/dist/esm/utils/opentelemetryInstrumentationPatcher.js.map +1 -0
- package/dist/esm/{azureMonitor/utils → utils}/statsbeat.d.ts +1 -1
- package/dist/esm/utils/statsbeat.d.ts.map +1 -0
- package/dist/esm/{azureMonitor/utils → utils}/statsbeat.js +8 -8
- package/dist/esm/utils/statsbeat.js.map +1 -0
- package/package.json +6 -6
- package/dist/commonjs/azureMonitor/utils/opentelemetryInstrumentationPatcher.d.ts.map +0 -1
- package/dist/commonjs/azureMonitor/utils/opentelemetryInstrumentationPatcher.js.map +0 -1
- package/dist/commonjs/azureMonitor/utils/statsbeat.d.ts.map +0 -1
- package/dist/commonjs/azureMonitor/utils/statsbeat.js.map +0 -1
- package/dist/esm/azureMonitor/utils/opentelemetryInstrumentationPatcher.d.ts.map +0 -1
- package/dist/esm/azureMonitor/utils/opentelemetryInstrumentationPatcher.js.map +0 -1
- package/dist/esm/azureMonitor/utils/statsbeat.d.ts.map +0 -1
- package/dist/esm/azureMonitor/utils/statsbeat.js.map +0 -1
- /package/dist/commonjs/{azureMonitor/utils → utils}/opentelemetryInstrumentationPatcher.d.ts +0 -0
- /package/dist/esm/{azureMonitor/utils → utils}/opentelemetryInstrumentationPatcher.d.ts +0 -0
|
@@ -58,22 +58,20 @@ const Utils = __importStar(require("./utils.js"));
|
|
|
58
58
|
* - Skips LangChain-internal runs (tagged `langsmith:hidden`, `Branch*`, or
|
|
59
59
|
* unmapped run types) to avoid noisy traces.
|
|
60
60
|
* - Guards against unbounded memory with a hard cap of {@link MAX_RUNS}.
|
|
61
|
-
* - Content
|
|
62
|
-
*
|
|
61
|
+
* - Content attributes (messages, tool args) are always recorded
|
|
62
|
+
* (aligned with Python/.NET SDKs).
|
|
63
63
|
*/
|
|
64
64
|
class LangChainTracer extends base_1.BaseTracer {
|
|
65
65
|
/** Hard cap on concurrent tracked runs to prevent memory leaks. */
|
|
66
66
|
static MAX_RUNS = 10_000;
|
|
67
67
|
tracer;
|
|
68
|
-
isContentRecordingEnabled;
|
|
69
68
|
/** Active runs keyed by LangChain run ID. */
|
|
70
69
|
runs = new Map();
|
|
71
70
|
/** Maps each run ID → its parent run ID for parent-span-context lookup. */
|
|
72
71
|
parentByRunId = new Map();
|
|
73
|
-
constructor(tracer
|
|
72
|
+
constructor(tracer) {
|
|
74
73
|
super();
|
|
75
74
|
this.tracer = tracer;
|
|
76
|
-
this.isContentRecordingEnabled = options?.isContentRecordingEnabled ?? false;
|
|
77
75
|
}
|
|
78
76
|
name = "OpenTelemetryLangChainTracer";
|
|
79
77
|
persistRun(_run) {
|
|
@@ -113,14 +111,18 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
113
111
|
: api_1.context.active();
|
|
114
112
|
// Build span name: "<operation> <name|model>"
|
|
115
113
|
let spanName = run.name;
|
|
114
|
+
let kind = api_1.SpanKind.INTERNAL;
|
|
116
115
|
if (operation === "invoke_agent") {
|
|
117
116
|
spanName = `${operation} ${run.name}`;
|
|
117
|
+
kind = api_1.SpanKind.SERVER;
|
|
118
118
|
}
|
|
119
119
|
else if (operation === "execute_tool") {
|
|
120
120
|
spanName = `${operation} ${run.name}`;
|
|
121
|
+
kind = api_1.SpanKind.CLIENT;
|
|
121
122
|
}
|
|
122
123
|
else if (operation === "chat") {
|
|
123
124
|
spanName = `${operation} ${Utils.getModel(run) || run.name}`.trim();
|
|
125
|
+
kind = api_1.SpanKind.CLIENT;
|
|
124
126
|
}
|
|
125
127
|
if (this.runs.size >= LangChainTracer.MAX_RUNS) {
|
|
126
128
|
api_2.diag.warn(`[LangChainTracer] Max runs (${LangChainTracer.MAX_RUNS}) reached, skipping span`);
|
|
@@ -129,7 +131,7 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
129
131
|
}
|
|
130
132
|
const startTime = run.start_time ?? Date.now();
|
|
131
133
|
const span = this.tracer.startSpan(spanName, {
|
|
132
|
-
kind
|
|
134
|
+
kind,
|
|
133
135
|
startTime,
|
|
134
136
|
attributes: { [index_js_1.ATTR_GEN_AI_PROVIDER_NAME]: "langchain" },
|
|
135
137
|
}, activeContext);
|
|
@@ -137,8 +139,7 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
137
139
|
}
|
|
138
140
|
/**
|
|
139
141
|
* Called by LangChain when a run finishes. Sets status, enriches the span
|
|
140
|
-
* with GenAI attributes, and ends it.
|
|
141
|
-
* message bodies, tool arguments, and system instructions are also attached.
|
|
142
|
+
* with GenAI attributes, and ends it.
|
|
142
143
|
*/
|
|
143
144
|
async _endTrace(run) {
|
|
144
145
|
if ((0, core_1.isTracingSuppressed)(api_1.context.active())) {
|
|
@@ -168,6 +169,11 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
168
169
|
if (run.error) {
|
|
169
170
|
span.setStatus({ code: api_1.SpanStatusCode.ERROR });
|
|
170
171
|
span.setAttribute(index_js_1.ATTR_ERROR_MESSAGE, String(run.error));
|
|
172
|
+
const errorType = run.error?.name ??
|
|
173
|
+
run.error?.constructor?.name;
|
|
174
|
+
if (typeof errorType === "string" && errorType.length > 0) {
|
|
175
|
+
span.setAttribute(index_js_1.ATTR_ERROR_TYPE, errorType);
|
|
176
|
+
}
|
|
171
177
|
}
|
|
172
178
|
else {
|
|
173
179
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
@@ -175,17 +181,21 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
175
181
|
// Always-on attributes: operation type, agent info, model, provider, session, tokens
|
|
176
182
|
Utils.setOperationTypeAttribute(operation, span);
|
|
177
183
|
Utils.setAgentAttributes(run, span);
|
|
184
|
+
if (operation === "invoke_agent") {
|
|
185
|
+
const callerName = this.findCallerAgentName(run);
|
|
186
|
+
if (callerName) {
|
|
187
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_CALLER_AGENT_NAME, callerName);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
178
190
|
Utils.setModelAttribute(run, span);
|
|
179
191
|
Utils.setProviderNameAttribute(run, span);
|
|
180
192
|
Utils.setSessionIdAttribute(run, span);
|
|
181
193
|
Utils.setTokenAttributes(run, span);
|
|
182
|
-
//
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
Utils.setSystemInstructionsAttribute(run, span);
|
|
188
|
-
}
|
|
194
|
+
// Content attributes — always recorded (aligned with Python/.NET SDKs)
|
|
195
|
+
Utils.setToolAttributes(run, span);
|
|
196
|
+
Utils.setInputMessagesAttribute(run, span);
|
|
197
|
+
Utils.setOutputMessagesAttribute(run, span);
|
|
198
|
+
Utils.setSystemInstructionsAttribute(run, span);
|
|
189
199
|
}
|
|
190
200
|
catch (error) {
|
|
191
201
|
api_2.diag.error(`[LangChainTracer] Error setting span attributes for run ${run.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -213,6 +223,21 @@ class LangChainTracer extends base_1.BaseTracer {
|
|
|
213
223
|
}
|
|
214
224
|
return undefined;
|
|
215
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Walk up the parent run chain to find the nearest ancestor that is an
|
|
228
|
+
* invoke_agent run, returning its name as the caller agent name.
|
|
229
|
+
*/
|
|
230
|
+
findCallerAgentName(run) {
|
|
231
|
+
let pid = run.parent_run_id;
|
|
232
|
+
while (pid) {
|
|
233
|
+
const entry = this.runs.get(pid);
|
|
234
|
+
if (entry && Utils.getOperationType(entry.run) === "invoke_agent") {
|
|
235
|
+
return entry.run.name;
|
|
236
|
+
}
|
|
237
|
+
pid = this.parentByRunId.get(pid);
|
|
238
|
+
}
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
216
241
|
}
|
|
217
242
|
exports.LangChainTracer = LangChainTracer;
|
|
218
243
|
//# sourceMappingURL=tracer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracer.js","sourceRoot":"","sources":["../../../../../src/genai/instrumentations/langchain/tracer.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;AAClC,kGAAkG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAElG,4CAA4F;AAC5F,uDAA+D;AAC/D,8CAA0D;AAC1D,4CAA0C;AAC1C,
|
|
1
|
+
{"version":3,"file":"tracer.js","sourceRoot":"","sources":["../../../../../src/genai/instrumentations/langchain/tracer.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;AAClC,kGAAkG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAElG,4CAA4F;AAC5F,uDAA+D;AAC/D,8CAA0D;AAC1D,4CAA0C;AAC1C,6CAKwB;AACxB,kDAAoC;AAIpC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,eAAgB,SAAQ,iBAAU;IAC7C,mEAAmE;IAC3D,MAAM,CAAU,QAAQ,GAAG,MAAM,CAAC;IAClC,MAAM,CAAS;IACvB,6CAA6C;IACrC,IAAI,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC9C,2EAA2E;IACnE,aAAa,GAAG,IAAI,GAAG,EAA8B,CAAC;IAE9D,YAAY,MAAc;QACxB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,GAAG,8BAA8B,CAAC;IAE5B,UAAU,CAAC,IAAS;QAC5B,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,WAAW,CAAC,GAAQ;QACxB,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EA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AAC,gBAAgB,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,cAAc,EAAE,CAAC;gBAClE,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;YACxB,CAAC;YACD,GAAG,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;;AA9MH,0CA+MC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../../src/genai/instrumentations/langchain/utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,8BAA8B,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../../src/genai/instrumentations/langchain/utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,GAAG,EAAE,MAAM,8BAA8B,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAiC1C,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAGD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAWjD;AAGD,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,QAEtE;AAGD,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QAOtD;AAGD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QA4CrD;AAED,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QAgC7D;AAmOD,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QA6D9D;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS,CAarD;AAGD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QAGrD;AAGD,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QAG5D;AAED,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAahE;AAGD,wBAAgB,8BAA8B,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,oBA+BlE;AAGD,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,QAoCtD"}
|
|
@@ -17,6 +17,8 @@ exports.setSessionIdAttribute = setSessionIdAttribute;
|
|
|
17
17
|
exports.setSystemInstructionsAttribute = setSystemInstructionsAttribute;
|
|
18
18
|
exports.setTokenAttributes = setTokenAttributes;
|
|
19
19
|
const index_js_1 = require("../../index.js");
|
|
20
|
+
const message_utils_js_1 = require("../../../a365/message-utils.js");
|
|
21
|
+
const contracts_js_1 = require("../../../a365/contracts.js");
|
|
20
22
|
// Type guards
|
|
21
23
|
function isString(value) {
|
|
22
24
|
return typeof value === "string";
|
|
@@ -59,49 +61,78 @@ function setToolAttributes(run, span) {
|
|
|
59
61
|
if (isString(run.name)) {
|
|
60
62
|
span.setAttribute(index_js_1.ATTR_GEN_AI_TOOL_NAME, run.name);
|
|
61
63
|
}
|
|
62
|
-
if (run.inputs)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
if (run.inputs) {
|
|
65
|
+
const argsValue = run.inputs?.input ?? run.inputs;
|
|
66
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_TOOL_CALL_ARGUMENTS, (0, message_utils_js_1.safeSerializeToJson)(typeof argsValue === "object" ? argsValue : String(argsValue), "arguments"));
|
|
67
|
+
}
|
|
68
|
+
// Tool result: v0 uses output.kwargs.content, v1 returns output as a plain string or has content directly
|
|
69
|
+
const toolResult = run.outputs?.output?.kwargs?.content ??
|
|
70
|
+
(isString(run.outputs?.output) ? run.outputs.output : null) ??
|
|
71
|
+
run.outputs?.output?.content;
|
|
72
|
+
if (toolResult != null) {
|
|
73
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_TOOL_CALL_RESULT, (0, message_utils_js_1.safeSerializeToJson)(typeof toolResult === "object"
|
|
74
|
+
? toolResult
|
|
75
|
+
: String(toolResult), "result"));
|
|
76
|
+
}
|
|
66
77
|
span.setAttribute(index_js_1.ATTR_GEN_AI_TOOL_TYPE, "extension");
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
// Tool call ID: v0 uses output.tool_call_id, v1 may have it on inputs
|
|
79
|
+
const toolCallId = run.outputs?.output?.tool_call_id ?? run.inputs?.tool_call_id;
|
|
80
|
+
if (toolCallId)
|
|
81
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_TOOL_CALL_ID, toolCallId);
|
|
69
82
|
}
|
|
70
83
|
function setInputMessagesAttribute(run, span) {
|
|
71
84
|
const messages = run.inputs?.messages;
|
|
72
85
|
if (!Array.isArray(messages)) {
|
|
73
86
|
return;
|
|
74
87
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
// LangChain may provide messages as a direct array or as a single nested array.
|
|
89
|
+
// Normalize both shapes so agent/inference inputs are consistently processed.
|
|
90
|
+
const preprocess = getScopeType(run) !== "unknown" && messages.length > 0 && Array.isArray(messages[0])
|
|
91
|
+
? messages[0]
|
|
92
|
+
: messages;
|
|
93
|
+
const chatMessages = [];
|
|
94
|
+
for (const msg of preprocess) {
|
|
95
|
+
if (!msg || typeof msg !== "object")
|
|
96
|
+
continue;
|
|
97
|
+
const msgObj = msg;
|
|
98
|
+
const parts = buildPartsFromMessage(msgObj);
|
|
99
|
+
if (parts.length === 0)
|
|
100
|
+
continue;
|
|
101
|
+
const msgType = getMessageType(msgObj);
|
|
102
|
+
const role = mapLangChainRole(msgType);
|
|
103
|
+
chatMessages.push({ role, parts });
|
|
104
|
+
}
|
|
105
|
+
if (chatMessages.length > 0) {
|
|
106
|
+
const wrapper = {
|
|
107
|
+
version: contracts_js_1.A365_MESSAGE_SCHEMA_VERSION,
|
|
108
|
+
messages: chatMessages,
|
|
109
|
+
};
|
|
110
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_INPUT_MESSAGES, (0, message_utils_js_1.serializeMessages)(wrapper));
|
|
90
111
|
}
|
|
91
112
|
}
|
|
92
|
-
// Helper: Extract
|
|
93
|
-
function
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
113
|
+
// Helper: Extract string content from a message (used for fallback text extraction and system instructions)
|
|
114
|
+
function extractStringContent(msg) {
|
|
115
|
+
const raw = extractRawContent(msg);
|
|
116
|
+
return isString(raw) ? raw : null;
|
|
117
|
+
}
|
|
118
|
+
// Helper: Extract raw content (string or content block array) from various message formats
|
|
119
|
+
function extractRawContent(msg) {
|
|
120
|
+
// Simple format: {role: "user", content: string | array}
|
|
121
|
+
if (msg.content !== undefined && msg.content !== null) {
|
|
122
|
+
if (isString(msg.content))
|
|
123
|
+
return msg.content;
|
|
124
|
+
if (Array.isArray(msg.content))
|
|
125
|
+
return msg.content;
|
|
97
126
|
}
|
|
98
127
|
// LangChain format: {lc_type: "human", lc_kwargs: {content}}
|
|
99
128
|
if (msg.lc_kwargs && typeof msg.lc_kwargs === "object" && !Array.isArray(msg.lc_kwargs)) {
|
|
100
129
|
const kwargs = msg.lc_kwargs;
|
|
101
130
|
if (isString(kwargs.content))
|
|
102
131
|
return kwargs.content;
|
|
132
|
+
if (Array.isArray(kwargs.content))
|
|
133
|
+
return kwargs.content;
|
|
103
134
|
}
|
|
104
|
-
//
|
|
135
|
+
// LangChain v1 serialized class instance format: { lc: 1, type: "constructor", kwargs: {...} }
|
|
105
136
|
if (msg.lc === 1 &&
|
|
106
137
|
msg.type === "constructor" &&
|
|
107
138
|
msg.kwargs &&
|
|
@@ -110,9 +141,140 @@ function extractMessageContent(msg) {
|
|
|
110
141
|
const kwargs = msg.kwargs;
|
|
111
142
|
if (isString(kwargs.content))
|
|
112
143
|
return kwargs.content;
|
|
144
|
+
if (Array.isArray(kwargs.content))
|
|
145
|
+
return kwargs.content;
|
|
113
146
|
}
|
|
114
147
|
return null;
|
|
115
148
|
}
|
|
149
|
+
// Helper: Map LangChain message type to MessageRole
|
|
150
|
+
function mapLangChainRole(msgType) {
|
|
151
|
+
switch (msgType) {
|
|
152
|
+
case "user":
|
|
153
|
+
case "human":
|
|
154
|
+
return contracts_js_1.MessageRole.USER;
|
|
155
|
+
case "assistant":
|
|
156
|
+
case "ai":
|
|
157
|
+
return contracts_js_1.MessageRole.ASSISTANT;
|
|
158
|
+
case "system":
|
|
159
|
+
return contracts_js_1.MessageRole.SYSTEM;
|
|
160
|
+
case "tool":
|
|
161
|
+
return contracts_js_1.MessageRole.TOOL;
|
|
162
|
+
default:
|
|
163
|
+
return msgType;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Helper: Build MessagePart[] from a LangChain message
|
|
167
|
+
function buildPartsFromMessage(msg) {
|
|
168
|
+
const parts = [];
|
|
169
|
+
const rawContent = extractRawContent(msg);
|
|
170
|
+
const addUnknownBlockPart = (blockType, block) => {
|
|
171
|
+
try {
|
|
172
|
+
parts.push({ type: blockType, content: JSON.stringify(block) });
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
parts.push({ type: blockType, content: "[unserializable]" });
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const addPartFromContentBlock = (block) => {
|
|
179
|
+
if (!block || typeof block !== "object")
|
|
180
|
+
return;
|
|
181
|
+
const contentBlock = block;
|
|
182
|
+
const blockType = contentBlock.type;
|
|
183
|
+
if (!blockType)
|
|
184
|
+
return;
|
|
185
|
+
if (blockType === "text" && isString(contentBlock.text)) {
|
|
186
|
+
parts.push({ type: "text", content: contentBlock.text });
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (blockType === "reasoning" && isString(contentBlock.reasoning)) {
|
|
190
|
+
parts.push({ type: "reasoning", content: contentBlock.reasoning });
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (blockType === "tool_call") {
|
|
194
|
+
parts.push({
|
|
195
|
+
type: "tool_call",
|
|
196
|
+
name: String(contentBlock.name ?? ""),
|
|
197
|
+
id: contentBlock.id != null ? String(contentBlock.id) : undefined,
|
|
198
|
+
arguments: contentBlock.args && typeof contentBlock.args === "object"
|
|
199
|
+
? contentBlock.args
|
|
200
|
+
: undefined,
|
|
201
|
+
});
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
addUnknownBlockPart(blockType, contentBlock);
|
|
205
|
+
};
|
|
206
|
+
if (isString(rawContent)) {
|
|
207
|
+
parts.push({ type: "text", content: rawContent });
|
|
208
|
+
}
|
|
209
|
+
else if (Array.isArray(rawContent)) {
|
|
210
|
+
for (const block of rawContent) {
|
|
211
|
+
addPartFromContentBlock(block);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Extract tool_calls from the message (AI messages may have a separate tool_calls array)
|
|
215
|
+
// Deduplicate by ID to avoid duplicates when tool_calls appear in both content blocks and tool_calls array
|
|
216
|
+
const seenToolCallIds = new Set();
|
|
217
|
+
for (const part of parts) {
|
|
218
|
+
if (part.type !== "tool_call")
|
|
219
|
+
continue;
|
|
220
|
+
const partId = part.id;
|
|
221
|
+
if (isString(partId)) {
|
|
222
|
+
seenToolCallIds.add(partId);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
for (const toolCall of extractToolCalls(msg)) {
|
|
226
|
+
const toolCallId = toolCall.id;
|
|
227
|
+
if (isString(toolCallId) && seenToolCallIds.has(toolCallId)) {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (isString(toolCallId)) {
|
|
231
|
+
seenToolCallIds.add(toolCallId);
|
|
232
|
+
}
|
|
233
|
+
parts.push(toolCall);
|
|
234
|
+
}
|
|
235
|
+
// Fallback: if no parts were built, use text extraction
|
|
236
|
+
if (parts.length === 0) {
|
|
237
|
+
const textContent = extractStringContent(msg);
|
|
238
|
+
if (textContent) {
|
|
239
|
+
parts.push({ type: "text", content: textContent });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return parts;
|
|
243
|
+
}
|
|
244
|
+
// Helper: Extract tool_calls from a LangChain message
|
|
245
|
+
function extractToolCalls(msg) {
|
|
246
|
+
const parts = [];
|
|
247
|
+
// Standard format: message.tool_calls[] — check direct, lc_kwargs, and kwargs paths
|
|
248
|
+
const directToolCalls = getNestedValue(msg, "tool_calls") ??
|
|
249
|
+
getNestedValue(msg, "lc_kwargs", "tool_calls") ??
|
|
250
|
+
getNestedValue(msg, "kwargs", "tool_calls");
|
|
251
|
+
if (Array.isArray(directToolCalls)) {
|
|
252
|
+
for (const tc of directToolCalls) {
|
|
253
|
+
if (!tc || typeof tc !== "object")
|
|
254
|
+
continue;
|
|
255
|
+
const call = tc;
|
|
256
|
+
parts.push({
|
|
257
|
+
type: "tool_call",
|
|
258
|
+
name: String(call.name ?? ""),
|
|
259
|
+
id: call.id != null ? String(call.id) : undefined,
|
|
260
|
+
arguments: call.args && typeof call.args === "object"
|
|
261
|
+
? call.args
|
|
262
|
+
: undefined,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return parts;
|
|
267
|
+
}
|
|
268
|
+
// Helper: Safely get a nested value from a message object
|
|
269
|
+
function getNestedValue(obj, ...keys) {
|
|
270
|
+
let current = obj;
|
|
271
|
+
for (const key of keys) {
|
|
272
|
+
if (!current || typeof current !== "object" || Array.isArray(current))
|
|
273
|
+
return undefined;
|
|
274
|
+
current = current[key];
|
|
275
|
+
}
|
|
276
|
+
return current;
|
|
277
|
+
}
|
|
116
278
|
// Helper: Determine message type
|
|
117
279
|
function getMessageType(msg) {
|
|
118
280
|
// Simple format
|
|
@@ -121,9 +283,10 @@ function getMessageType(msg) {
|
|
|
121
283
|
// LangChain old format
|
|
122
284
|
if (isString(msg.lc_type))
|
|
123
285
|
return msg.lc_type;
|
|
124
|
-
|
|
286
|
+
// Skip v1 constructor type marker — fall through to id array check
|
|
287
|
+
if (isString(msg.type) && msg.type !== "constructor")
|
|
125
288
|
return msg.type;
|
|
126
|
-
// LangChain
|
|
289
|
+
// LangChain v1 format - check id array for message type (e.g., ["langchain_core", "messages", "HumanMessage"])
|
|
127
290
|
if (Array.isArray(msg.id)) {
|
|
128
291
|
const lastId = msg.id[msg.id.length - 1];
|
|
129
292
|
if (isString(lastId)) {
|
|
@@ -133,6 +296,8 @@ function getMessageType(msg) {
|
|
|
133
296
|
return "ai";
|
|
134
297
|
if (lastId.includes("System"))
|
|
135
298
|
return "system";
|
|
299
|
+
if (lastId.includes("Tool"))
|
|
300
|
+
return "tool";
|
|
136
301
|
}
|
|
137
302
|
}
|
|
138
303
|
return "unknown";
|
|
@@ -150,11 +315,6 @@ function getScopeType(run) {
|
|
|
150
315
|
}
|
|
151
316
|
return "unknown";
|
|
152
317
|
}
|
|
153
|
-
// Helper: Check if input message should be included based on scope and message type
|
|
154
|
-
function shouldIncludeInputMessage(msgType) {
|
|
155
|
-
// For input messages: all scopes want user/human messages only
|
|
156
|
-
return msgType === "user" || msgType === "human";
|
|
157
|
-
}
|
|
158
318
|
// Helper: Check if output message should be included based on scope and message type
|
|
159
319
|
function shouldIncludeOutputMessage(scopeType, msgType) {
|
|
160
320
|
if (scopeType === "agent" || scopeType === "inference") {
|
|
@@ -174,18 +334,23 @@ function setOutputMessagesAttribute(run, span) {
|
|
|
174
334
|
return;
|
|
175
335
|
}
|
|
176
336
|
const scopeType = getScopeType(run);
|
|
177
|
-
const
|
|
337
|
+
const outputMessages = [];
|
|
338
|
+
// Helper: process a single message object into an OutputMessage
|
|
339
|
+
const processMessage = (msg) => {
|
|
340
|
+
const msgType = getMessageType(msg);
|
|
341
|
+
if (!shouldIncludeOutputMessage(scopeType, msgType))
|
|
342
|
+
return;
|
|
343
|
+
const parts = buildPartsFromMessage(msg);
|
|
344
|
+
if (parts.length === 0)
|
|
345
|
+
return;
|
|
346
|
+
const role = mapLangChainRole(msgType);
|
|
347
|
+
outputMessages.push({ role, parts });
|
|
348
|
+
};
|
|
178
349
|
// Direct messages array (used in agent/chain outputs)
|
|
179
350
|
if (Array.isArray(outputs.messages)) {
|
|
180
|
-
outputs.messages
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return;
|
|
184
|
-
const msgType = getMessageType(msg);
|
|
185
|
-
if (shouldIncludeOutputMessage(scopeType, msgType)) {
|
|
186
|
-
messages.push(content);
|
|
187
|
-
}
|
|
188
|
-
});
|
|
351
|
+
for (const msg of outputs.messages) {
|
|
352
|
+
processMessage(msg);
|
|
353
|
+
}
|
|
189
354
|
}
|
|
190
355
|
// LangChain generations format (used in LLM/inference outputs)
|
|
191
356
|
if (Array.isArray(outputs.generations)) {
|
|
@@ -194,19 +359,14 @@ function setOutputMessagesAttribute(run, span) {
|
|
|
194
359
|
gen.forEach((item) => {
|
|
195
360
|
// Try message property
|
|
196
361
|
if (item.message && typeof item.message === "object" && !Array.isArray(item.message)) {
|
|
197
|
-
|
|
198
|
-
const content = extractMessageContent(msg);
|
|
199
|
-
if (!content) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const msgType = getMessageType(msg);
|
|
203
|
-
if (shouldIncludeOutputMessage(scopeType, msgType)) {
|
|
204
|
-
messages.push(content);
|
|
205
|
-
}
|
|
362
|
+
processMessage(item.message);
|
|
206
363
|
}
|
|
207
364
|
// Try direct text property (for generation items)
|
|
208
365
|
else if (isString(item.text) && scopeType === "inference") {
|
|
209
|
-
|
|
366
|
+
outputMessages.push({
|
|
367
|
+
role: contracts_js_1.MessageRole.ASSISTANT,
|
|
368
|
+
parts: [{ type: "text", content: item.text }],
|
|
369
|
+
});
|
|
210
370
|
}
|
|
211
371
|
});
|
|
212
372
|
}
|
|
@@ -214,23 +374,24 @@ function setOutputMessagesAttribute(run, span) {
|
|
|
214
374
|
}
|
|
215
375
|
// Check for direct message object (some models return this)
|
|
216
376
|
if (outputs.message && typeof outputs.message === "object" && !Array.isArray(outputs.message)) {
|
|
217
|
-
|
|
218
|
-
const content = extractMessageContent(msg);
|
|
219
|
-
if (content) {
|
|
220
|
-
const msgType = getMessageType(msg);
|
|
221
|
-
if (shouldIncludeOutputMessage(scopeType, msgType)) {
|
|
222
|
-
messages.push(content);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
377
|
+
processMessage(outputs.message);
|
|
225
378
|
}
|
|
226
|
-
if (
|
|
227
|
-
|
|
379
|
+
if (outputMessages.length > 0) {
|
|
380
|
+
const wrapper = {
|
|
381
|
+
version: contracts_js_1.A365_MESSAGE_SCHEMA_VERSION,
|
|
382
|
+
messages: outputMessages,
|
|
383
|
+
};
|
|
384
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_OUTPUT_MESSAGES, (0, message_utils_js_1.serializeMessages)(wrapper));
|
|
228
385
|
}
|
|
229
386
|
}
|
|
230
387
|
// Model - Helper to extract model name from run
|
|
231
388
|
function getModel(run) {
|
|
232
389
|
return [
|
|
390
|
+
// v1: response_metadata directly on message
|
|
391
|
+
run.outputs?.generations?.[0]?.[0]?.message?.response_metadata?.model_name,
|
|
392
|
+
// v0: response_metadata nested under kwargs
|
|
233
393
|
run.outputs?.generations?.[0]?.[0]?.message?.kwargs?.response_metadata?.model_name,
|
|
394
|
+
// Metadata paths (both v0 and v1)
|
|
234
395
|
run.extra?.metadata?.ls_model_name,
|
|
235
396
|
run.extra?.invocation_params?.model,
|
|
236
397
|
run.extra?.invocation_params?.model_name,
|
|
@@ -254,10 +415,14 @@ function setSessionIdAttribute(run, span) {
|
|
|
254
415
|
const metadata = run.extra?.metadata;
|
|
255
416
|
if (!metadata)
|
|
256
417
|
return;
|
|
257
|
-
const sessionId = metadata.session_id ?? metadata.
|
|
258
|
-
if (
|
|
418
|
+
const sessionId = metadata.session_id ?? metadata.thread_id;
|
|
419
|
+
if (isString(sessionId) && sessionId.length > 0) {
|
|
259
420
|
span.setAttribute(index_js_1.ATTR_MICROSOFT_SESSION_ID, sessionId);
|
|
260
421
|
}
|
|
422
|
+
const conversationId = metadata.conversation_id;
|
|
423
|
+
if (isString(conversationId) && conversationId.length > 0) {
|
|
424
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_CONVERSATION_ID, conversationId);
|
|
425
|
+
}
|
|
261
426
|
}
|
|
262
427
|
// System instructions
|
|
263
428
|
function setSystemInstructionsAttribute(run, span) {
|
|
@@ -273,10 +438,20 @@ function setSystemInstructionsAttribute(run, span) {
|
|
|
273
438
|
: "";
|
|
274
439
|
if (prompts)
|
|
275
440
|
return span.setAttribute(index_js_1.ATTR_GEN_AI_SYSTEM_INSTRUCTIONS, prompts);
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
441
|
+
// Check both flat and nested message arrays
|
|
442
|
+
const rawMessages = Array.isArray(inputs.messages) ? inputs.messages : [];
|
|
443
|
+
const flatMessages = rawMessages.length > 0 && Array.isArray(rawMessages[0])
|
|
444
|
+
? rawMessages[0]
|
|
445
|
+
: rawMessages;
|
|
446
|
+
const systemText = flatMessages
|
|
447
|
+
.filter((m) => {
|
|
448
|
+
if (!m || typeof m !== "object")
|
|
449
|
+
return false;
|
|
450
|
+
const msgType = getMessageType(m);
|
|
451
|
+
return msgType === "system";
|
|
452
|
+
})
|
|
453
|
+
.map((m) => extractStringContent(m) ?? "")
|
|
454
|
+
.map((s) => s.trim())
|
|
280
455
|
.filter(Boolean)
|
|
281
456
|
.join("\n");
|
|
282
457
|
if (systemText)
|
|
@@ -285,10 +460,15 @@ function setSystemInstructionsAttribute(run, span) {
|
|
|
285
460
|
// Tokens (input and output)
|
|
286
461
|
function setTokenAttributes(run, span) {
|
|
287
462
|
// Try multiple paths to find usage metadata (LLM direct/kwargs/response_metadata, agent calls, and chain/model_request outputs)
|
|
463
|
+
// v1: usage_metadata is often on the last AI message in outputs.messages
|
|
464
|
+
const lastMsg = Array.isArray(run.outputs?.messages)
|
|
465
|
+
? run.outputs.messages[run.outputs.messages.length - 1]
|
|
466
|
+
: undefined;
|
|
288
467
|
const usage = run.outputs?.generations?.[0]?.[0]?.message?.usage_metadata ||
|
|
289
468
|
run.outputs?.generations?.[0]?.[0]?.message?.kwargs?.usage_metadata ||
|
|
469
|
+
run.outputs?.generations?.[0]?.[0]?.message?.response_metadata?.tokenUsage ||
|
|
290
470
|
run.outputs?.generations?.[0]?.[0]?.message?.kwargs?.response_metadata?.tokenUsage ||
|
|
291
|
-
|
|
471
|
+
lastMsg?.usage_metadata ||
|
|
292
472
|
run.outputs?.message?.response_metadata?.usage ||
|
|
293
473
|
run.outputs?.message?.response_metadata?.tokenUsage ||
|
|
294
474
|
run.outputs?.messages
|
|
@@ -298,11 +478,15 @@ function setTokenAttributes(run, span) {
|
|
|
298
478
|
return;
|
|
299
479
|
}
|
|
300
480
|
const usageObj = usage;
|
|
301
|
-
|
|
302
|
-
|
|
481
|
+
// Support both usage_metadata shape (input_tokens/output_tokens) and
|
|
482
|
+
// tokenUsage shape (promptTokens/completionTokens) from LangChain OpenAI provider
|
|
483
|
+
const inputTokens = usageObj.input_tokens ?? usageObj.promptTokens;
|
|
484
|
+
const outputTokens = usageObj.output_tokens ?? usageObj.completionTokens;
|
|
485
|
+
if (typeof inputTokens === "number") {
|
|
486
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_USAGE_INPUT_TOKENS, inputTokens);
|
|
303
487
|
}
|
|
304
|
-
if (typeof
|
|
305
|
-
span.setAttribute(index_js_1.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
|
|
488
|
+
if (typeof outputTokens === "number") {
|
|
489
|
+
span.setAttribute(index_js_1.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, outputTokens);
|
|
306
490
|
}
|
|
307
491
|
}
|
|
308
492
|
// LangGraph agent check
|