@mastra/observability 1.0.0-beta.2 → 1.0.0-beta.3
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/CHANGELOG.md +31 -0
- package/dist/index.cjs +116 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +116 -1
- package/dist/index.js.map +1 -1
- package/dist/model-tracing.d.ts +0 -6
- package/dist/model-tracing.d.ts.map +1 -1
- package/dist/tracing-options.d.ts +27 -0
- package/dist/tracing-options.d.ts.map +1 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAG1C,cAAc,UAAU,CAAC;AAGzB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAG1C,cAAc,UAAU,CAAC;AAGzB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAGhC,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -953,9 +953,30 @@ var ModelSpanTracker = class {
|
|
|
953
953
|
#accumulator = {};
|
|
954
954
|
#stepIndex = 0;
|
|
955
955
|
#chunkSequence = 0;
|
|
956
|
+
/** Tracks whether completionStartTime has been captured for this generation */
|
|
957
|
+
#completionStartTimeCaptured = false;
|
|
958
|
+
/** Tracks tool output accumulators by toolCallId for consolidating sub-agent streams */
|
|
959
|
+
#toolOutputAccumulators = /* @__PURE__ */ new Map();
|
|
960
|
+
/** Tracks toolCallIds that had streaming output (to skip redundant tool-result spans) */
|
|
961
|
+
#streamedToolCallIds = /* @__PURE__ */ new Set();
|
|
956
962
|
constructor(modelSpan) {
|
|
957
963
|
this.#modelSpan = modelSpan;
|
|
958
964
|
}
|
|
965
|
+
/**
|
|
966
|
+
* Capture the completion start time (time to first token) when the first content chunk arrives.
|
|
967
|
+
* This is used by observability providers like Langfuse to calculate TTFT metrics.
|
|
968
|
+
*/
|
|
969
|
+
#captureCompletionStartTime() {
|
|
970
|
+
if (this.#completionStartTimeCaptured || !this.#modelSpan) {
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
this.#completionStartTimeCaptured = true;
|
|
974
|
+
this.#modelSpan.update({
|
|
975
|
+
attributes: {
|
|
976
|
+
completionStartTime: /* @__PURE__ */ new Date()
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
}
|
|
959
980
|
/**
|
|
960
981
|
* Get the tracing context for creating child spans.
|
|
961
982
|
* Returns the current step span if active, otherwise the model span.
|
|
@@ -1180,6 +1201,77 @@ var ModelSpanTracker = class {
|
|
|
1180
1201
|
break;
|
|
1181
1202
|
}
|
|
1182
1203
|
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Handle tool-output chunks from sub-agents.
|
|
1206
|
+
* Consolidates streaming text/reasoning deltas into a single span per tool call.
|
|
1207
|
+
*/
|
|
1208
|
+
#handleToolOutputChunk(chunk) {
|
|
1209
|
+
if (chunk.type !== "tool-output") return;
|
|
1210
|
+
const payload = chunk.payload;
|
|
1211
|
+
const { output, toolCallId, toolName } = payload;
|
|
1212
|
+
let acc = this.#toolOutputAccumulators.get(toolCallId);
|
|
1213
|
+
if (!acc) {
|
|
1214
|
+
if (!this.#currentStepSpan) {
|
|
1215
|
+
this.#startStepSpan();
|
|
1216
|
+
}
|
|
1217
|
+
acc = {
|
|
1218
|
+
toolName: toolName || "unknown",
|
|
1219
|
+
toolCallId,
|
|
1220
|
+
text: "",
|
|
1221
|
+
reasoning: "",
|
|
1222
|
+
sequenceNumber: this.#chunkSequence++,
|
|
1223
|
+
// Name the span 'tool-result' for consistency (tool-call → tool-result)
|
|
1224
|
+
span: this.#currentStepSpan?.createChildSpan({
|
|
1225
|
+
name: `chunk: 'tool-result'`,
|
|
1226
|
+
type: SpanType.MODEL_CHUNK,
|
|
1227
|
+
attributes: {
|
|
1228
|
+
chunkType: "tool-result",
|
|
1229
|
+
sequenceNumber: this.#chunkSequence - 1
|
|
1230
|
+
}
|
|
1231
|
+
})
|
|
1232
|
+
};
|
|
1233
|
+
this.#toolOutputAccumulators.set(toolCallId, acc);
|
|
1234
|
+
}
|
|
1235
|
+
if (output && typeof output === "object" && "type" in output) {
|
|
1236
|
+
const innerType = output.type;
|
|
1237
|
+
switch (innerType) {
|
|
1238
|
+
case "text-delta":
|
|
1239
|
+
if (output.payload?.text) {
|
|
1240
|
+
acc.text += output.payload.text;
|
|
1241
|
+
}
|
|
1242
|
+
break;
|
|
1243
|
+
case "reasoning-delta":
|
|
1244
|
+
if (output.payload?.text) {
|
|
1245
|
+
acc.reasoning += output.payload.text;
|
|
1246
|
+
}
|
|
1247
|
+
break;
|
|
1248
|
+
case "finish":
|
|
1249
|
+
case "workflow-finish":
|
|
1250
|
+
this.#endToolOutputSpan(toolCallId);
|
|
1251
|
+
break;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* End a tool output span and clean up the accumulator
|
|
1257
|
+
*/
|
|
1258
|
+
#endToolOutputSpan(toolCallId) {
|
|
1259
|
+
const acc = this.#toolOutputAccumulators.get(toolCallId);
|
|
1260
|
+
if (!acc) return;
|
|
1261
|
+
const output = {
|
|
1262
|
+
toolCallId: acc.toolCallId,
|
|
1263
|
+
toolName: acc.toolName
|
|
1264
|
+
};
|
|
1265
|
+
if (acc.text) {
|
|
1266
|
+
output.text = acc.text;
|
|
1267
|
+
}
|
|
1268
|
+
if (acc.reasoning) {
|
|
1269
|
+
output.reasoning = acc.reasoning;
|
|
1270
|
+
}
|
|
1271
|
+
acc.span?.end({ output });
|
|
1272
|
+
this.#toolOutputAccumulators.delete(toolCallId);
|
|
1273
|
+
this.#streamedToolCallIds.add(toolCallId);
|
|
1274
|
+
}
|
|
1183
1275
|
/**
|
|
1184
1276
|
* Wraps a stream with model tracing transform to track MODEL_STEP and MODEL_CHUNK spans.
|
|
1185
1277
|
*
|
|
@@ -1187,9 +1279,14 @@ var ModelSpanTracker = class {
|
|
|
1187
1279
|
* create MODEL_STEP and MODEL_CHUNK spans for each semantic unit in the stream.
|
|
1188
1280
|
*/
|
|
1189
1281
|
wrapStream(stream) {
|
|
1282
|
+
let captureCompletionStartTime = false;
|
|
1190
1283
|
return stream.pipeThrough(
|
|
1191
1284
|
new TransformStream({
|
|
1192
1285
|
transform: (chunk, controller) => {
|
|
1286
|
+
if (!captureCompletionStartTime) {
|
|
1287
|
+
captureCompletionStartTime = true;
|
|
1288
|
+
this.#captureCompletionStartTime();
|
|
1289
|
+
}
|
|
1193
1290
|
controller.enqueue(chunk);
|
|
1194
1291
|
switch (chunk.type) {
|
|
1195
1292
|
case "text-start":
|
|
@@ -1223,6 +1320,19 @@ var ModelSpanTracker = class {
|
|
|
1223
1320
|
case "start":
|
|
1224
1321
|
case "finish":
|
|
1225
1322
|
break;
|
|
1323
|
+
case "tool-output":
|
|
1324
|
+
this.#handleToolOutputChunk(chunk);
|
|
1325
|
+
break;
|
|
1326
|
+
case "tool-result": {
|
|
1327
|
+
const toolCallId = chunk.payload?.toolCallId;
|
|
1328
|
+
if (toolCallId && this.#streamedToolCallIds.has(toolCallId)) {
|
|
1329
|
+
this.#streamedToolCallIds.delete(toolCallId);
|
|
1330
|
+
break;
|
|
1331
|
+
}
|
|
1332
|
+
const { args, ...cleanPayload } = chunk.payload || {};
|
|
1333
|
+
this.#createEventSpan(chunk.type, cleanPayload);
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1226
1336
|
// Default: auto-create event span for all other chunk types
|
|
1227
1337
|
default: {
|
|
1228
1338
|
let outputPayload = chunk.payload;
|
|
@@ -2322,6 +2432,11 @@ var Observability = class extends MastraBase {
|
|
|
2322
2432
|
}
|
|
2323
2433
|
};
|
|
2324
2434
|
|
|
2325
|
-
|
|
2435
|
+
// src/tracing-options.ts
|
|
2436
|
+
function buildTracingOptions(...updaters) {
|
|
2437
|
+
return updaters.reduce((opts, updater) => updater(opts), {});
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
export { BaseExporter, BaseObservabilityInstance, BaseSpan, CloudExporter, ConsoleExporter, DefaultExporter, DefaultObservabilityInstance, DefaultSpan, ModelSpanTracker, NoOpSpan, Observability, SamplingStrategyType, SensitiveDataFilter, TestExporter, buildTracingOptions, deepClean, getExternalParentId, observabilityConfigValueSchema, observabilityInstanceConfigSchema, observabilityRegistryConfigSchema, samplingStrategySchema };
|
|
2326
2441
|
//# sourceMappingURL=index.js.map
|
|
2327
2442
|
//# sourceMappingURL=index.js.map
|