@arizeai/openinference-instrumentation-beeai 1.1.1 → 1.2.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.
Files changed (35) hide show
  1. package/dist/esm/helpers/getSerializedObjectSafe.d.ts.map +1 -1
  2. package/dist/esm/helpers/getSerializedObjectSafe.js +171 -164
  3. package/dist/esm/helpers/getSerializedObjectSafe.js.map +1 -1
  4. package/dist/esm/middleware.d.ts.map +1 -1
  5. package/dist/esm/middleware.js +113 -81
  6. package/dist/esm/middleware.js.map +1 -1
  7. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  8. package/dist/esnext/helpers/getSerializedObjectSafe.d.ts.map +1 -1
  9. package/dist/esnext/helpers/getSerializedObjectSafe.js +128 -121
  10. package/dist/esnext/helpers/getSerializedObjectSafe.js.map +1 -1
  11. package/dist/esnext/middleware.d.ts.map +1 -1
  12. package/dist/esnext/middleware.js +103 -71
  13. package/dist/esnext/middleware.js.map +1 -1
  14. package/dist/esnext/tsconfig.esnext.tsbuildinfo +1 -1
  15. package/dist/examples/run-react-agent.d.ts +2 -0
  16. package/dist/examples/run-react-agent.d.ts.map +1 -0
  17. package/dist/examples/{run-agent.js → run-react-agent.js} +1 -1
  18. package/dist/examples/run-react-agent.js.map +1 -0
  19. package/dist/examples/run-toolcalling-agent.d.ts +2 -0
  20. package/dist/examples/run-toolcalling-agent.d.ts.map +1 -0
  21. package/dist/examples/run-toolcalling-agent.js +36 -0
  22. package/dist/examples/run-toolcalling-agent.js.map +1 -0
  23. package/dist/src/helpers/getSerializedObjectSafe.d.ts.map +1 -1
  24. package/dist/src/helpers/getSerializedObjectSafe.js +128 -121
  25. package/dist/src/helpers/getSerializedObjectSafe.js.map +1 -1
  26. package/dist/src/middleware.d.ts.map +1 -1
  27. package/dist/src/middleware.js +103 -71
  28. package/dist/src/middleware.js.map +1 -1
  29. package/dist/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +3 -3
  31. package/src/helpers/getSerializedObjectSafe.ts +222 -210
  32. package/src/middleware.ts +140 -98
  33. package/dist/examples/run-agent.d.ts +0 -2
  34. package/dist/examples/run-agent.d.ts.map +0 -1
  35. package/dist/examples/run-agent.js.map +0 -1
package/src/middleware.ts CHANGED
@@ -36,7 +36,7 @@ import {
36
36
  successLLMEventName,
37
37
  } from "./config";
38
38
  import { createFullPath } from "beeai-framework/emitter/utils";
39
- import type { ReActAgent } from "beeai-framework/agents/react/agent";
39
+ import { ReActAgent } from "beeai-framework/agents/react/agent";
40
40
  import { BaseAgent } from "beeai-framework/agents/base";
41
41
  import { diag } from "@opentelemetry/api";
42
42
  import { FrameworkSpan, GeneratedResponse } from "./types";
@@ -45,6 +45,8 @@ import {
45
45
  OpenInferenceSpanKind,
46
46
  SemanticConventions,
47
47
  } from "@arizeai/openinference-semantic-conventions";
48
+ import { ToolCallingAgent } from "beeai-framework/agents/toolCalling/agent";
49
+ import type { ToolCallingAgentCallbacks } from "beeai-framework/agents/toolCalling/types";
48
50
 
49
51
  export const activeTracesMap = new Map<string, string>();
50
52
 
@@ -207,118 +209,158 @@ export function createTelemetryMiddleware(
207
209
  );
208
210
  }
209
211
 
210
- /**
211
- * create groupId span level (id does not exist)
212
- * I use only the top-level groups like iterations other nested groups like tokens would introduce unuseful complexity
213
- */
214
- if (
215
- meta.groupId &&
216
- !meta.trace.parentRunId &&
217
- !groupIterations.includes(meta.groupId)
218
- ) {
219
- spansMap.set(
220
- meta.groupId,
221
- createSpan({
222
- id: meta.groupId,
223
- name: meta.groupId,
224
- target: "groupId",
225
- data: {
226
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: "Chain",
227
- },
228
- startedAt: convertDateToPerformance(meta.createdAt),
229
- }),
230
- );
231
- groupIterations.push(meta.groupId);
232
- }
233
-
234
- const { spanId, parentSpanId } = idNameManager.getIds({
235
- path: meta.path,
236
- id: meta.id,
237
- runId: meta.trace.runId,
238
- parentRunId: meta.trace.parentRunId,
239
- groupId: meta.groupId,
240
- });
212
+ try {
213
+ /**
214
+ * create groupId span level (id does not exist)
215
+ * I use only the top-level groups like iterations other nested groups like tokens would introduce unuseful complexity
216
+ */
217
+ if (
218
+ meta.groupId &&
219
+ !meta.trace.parentRunId &&
220
+ !groupIterations.includes(meta.groupId)
221
+ ) {
222
+ spansMap.set(
223
+ meta.groupId,
224
+ createSpan({
225
+ id: meta.groupId,
226
+ name: meta.groupId,
227
+ target: "groupId",
228
+ data: {
229
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]: "Chain",
230
+ },
231
+ startedAt: convertDateToPerformance(meta.createdAt),
232
+ }),
233
+ );
234
+ groupIterations.push(meta.groupId);
235
+ }
241
236
 
242
- const serializedData = getSerializedObjectSafe(data, meta);
237
+ const { spanId, parentSpanId } = idNameManager.getIds({
238
+ path: meta.path,
239
+ id: meta.id,
240
+ runId: meta.trace.runId,
241
+ parentRunId: meta.trace.parentRunId,
242
+ groupId: meta.groupId,
243
+ });
243
244
 
244
- // skip partialUpdate events with no data
245
- if (meta.name === partialUpdateEventName && isEmpty(serializedData)) {
246
- return;
247
- }
245
+ const serializedData = getSerializedObjectSafe(data, meta);
248
246
 
249
- const span = createSpan({
250
- id: spanId,
251
- name: meta.name,
252
- target: meta.path,
253
- ...(parentSpanId && { parent: { id: parentSpanId } }),
254
- ctx: meta.context,
255
- data: serializedData,
256
- error: getErrorSafe(data),
257
- startedAt: convertDateToPerformance(meta.createdAt),
258
- });
259
-
260
- const lastIteration = groupIterations[groupIterations.length - 1];
261
-
262
- // delete the `partialUpdate` event if does not have nested spans
263
- const lastIterationEventSpanId = eventsIterationsMap
264
- .get(lastIteration)
265
- ?.get(meta.name);
266
- if (
267
- lastIterationEventSpanId &&
268
- partialUpdateEventName === meta.name &&
269
- spansMap.has(lastIterationEventSpanId)
270
- ) {
271
- const { context } = spansMap.get(lastIterationEventSpanId)!;
272
- if (parentIdsMap.has(context.span_id)) {
273
- spansToDeleteMap.set(lastIterationEventSpanId, undefined);
274
- } else {
275
- // delete span
276
- cleanSpanSources({ spanId: lastIterationEventSpanId });
277
- spansMap.delete(lastIterationEventSpanId);
247
+ // skip partialUpdate events with no data
248
+ if (meta.name === partialUpdateEventName && isEmpty(serializedData)) {
249
+ return;
278
250
  }
279
- }
280
251
 
281
- // create new span
282
- spansMap.set(span.context.span_id, span);
283
- // update number of nested spans for parent_id if exists
284
- if (span.parent_id) {
285
- parentIdsMap.set(
286
- span.parent_id,
287
- (parentIdsMap.get(span.parent_id) || 0) + 1,
288
- );
289
- }
252
+ const span = createSpan({
253
+ id: spanId,
254
+ name: meta.name,
255
+ target: meta.path,
256
+ ...(parentSpanId && { parent: { id: parentSpanId } }),
257
+ ctx: meta.context,
258
+ data: serializedData,
259
+ error: getErrorSafe(data),
260
+ startedAt: convertDateToPerformance(meta.createdAt),
261
+ });
290
262
 
291
- // save the last event for each iteration
292
- if (groupIterations.length > 0) {
293
- if (eventsIterationsMap.has(lastIteration)) {
294
- eventsIterationsMap
295
- .get(lastIteration)!
296
- .set(meta.name, span.context.span_id);
297
- } else {
298
- eventsIterationsMap.set(
299
- lastIteration,
300
- new Map().set(meta.name, span.context.span_id),
263
+ const lastIteration = groupIterations[groupIterations.length - 1];
264
+
265
+ // delete the `partialUpdate` event if does not have nested spans
266
+ const lastIterationEventSpanId = eventsIterationsMap
267
+ .get(lastIteration)
268
+ ?.get(meta.name);
269
+ if (
270
+ lastIterationEventSpanId &&
271
+ partialUpdateEventName === meta.name &&
272
+ spansMap.has(lastIterationEventSpanId)
273
+ ) {
274
+ const { context } = spansMap.get(lastIterationEventSpanId)!;
275
+ if (parentIdsMap.has(context.span_id)) {
276
+ spansToDeleteMap.set(lastIterationEventSpanId, undefined);
277
+ } else {
278
+ // delete span
279
+ cleanSpanSources({ spanId: lastIterationEventSpanId });
280
+ spansMap.delete(lastIterationEventSpanId);
281
+ }
282
+ }
283
+
284
+ // create new span
285
+ spansMap.set(span.context.span_id, span);
286
+ // update number of nested spans for parent_id if exists
287
+ if (span.parent_id) {
288
+ parentIdsMap.set(
289
+ span.parent_id,
290
+ (parentIdsMap.get(span.parent_id) || 0) + 1,
301
291
  );
302
292
  }
293
+
294
+ // save the last event for each iteration
295
+ if (groupIterations.length > 0) {
296
+ if (eventsIterationsMap.has(lastIteration)) {
297
+ eventsIterationsMap
298
+ .get(lastIteration)!
299
+ .set(meta.name, span.context.span_id);
300
+ } else {
301
+ eventsIterationsMap.set(
302
+ lastIteration,
303
+ new Map().set(meta.name, span.context.span_id),
304
+ );
305
+ }
306
+ }
307
+ } catch (e) {
308
+ diag.warn("Instrumentation error", e);
303
309
  }
304
310
  });
305
311
 
306
- // The generated response and message history are collected from the `success` agent's event
312
+ // The generated response and message history are collected from the `success` event generated by ReActAgent
307
313
  emitter.match(
308
314
  (event) =>
309
315
  event.name === successLLMEventName &&
310
- event.creator instanceof BaseAgent,
316
+ event.creator instanceof ReActAgent,
311
317
  (data: InferCallbackValue<ReActAgentCallbacks["success"]>) => {
312
- const { data: dataObject, memory } = data;
313
-
314
- generatedMessage = {
315
- role: dataObject.role,
316
- text: dataObject.text,
317
- };
318
- history = memory.messages.map((msg) => ({
319
- text: msg.text,
320
- role: msg.role,
321
- }));
318
+ try {
319
+ const { data: dataObject, memory } = data;
320
+
321
+ generatedMessage = {
322
+ role: dataObject.role,
323
+ text: dataObject.text,
324
+ };
325
+ history = memory.messages.map((msg) => ({
326
+ text: msg.text,
327
+ role: msg.role,
328
+ }));
329
+ } catch (e) {
330
+ diag.warn(
331
+ "Instrumentation error. Unable to map messages to history for ReActAgent",
332
+ e,
333
+ );
334
+ }
335
+ },
336
+ );
337
+
338
+ // The generated response and message history are collected from the `success` event generated by ToolCallingAgentCallbacks
339
+ emitter.match(
340
+ (event) =>
341
+ event.name === successLLMEventName &&
342
+ event.creator instanceof ToolCallingAgent,
343
+ (data: InferCallbackValue<ToolCallingAgentCallbacks["success"]>) => {
344
+ try {
345
+ const { state } = data;
346
+
347
+ if (state.result) {
348
+ generatedMessage = {
349
+ role: state.result.role,
350
+ text: state.result.text,
351
+ };
352
+ }
353
+
354
+ history = state.memory.messages.map((msg) => ({
355
+ text: msg.text,
356
+ role: msg.role,
357
+ }));
358
+ } catch (e) {
359
+ diag.warn(
360
+ "Instrumentation error. Unable to map messages to history for ToolCallingAgent",
361
+ e,
362
+ );
363
+ }
322
364
  },
323
365
  );
324
366
  };
@@ -1,2 +0,0 @@
1
- import "./instrumentation";
2
- //# sourceMappingURL=run-agent.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-agent.d.ts","sourceRoot":"","sources":["../../examples/run-agent.ts"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-agent.js","sourceRoot":"","sources":["../../examples/run-agent.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,6BAA2B;AAC3B,8DAAgE;AAChE,uEAA+E;AAC/E,oEAAiE;AACjE,oFAAqF;AACrF,uEAAwE;AAExE,MAAM,GAAG,GAAG,IAAI,sBAAe,CAAC,UAAU,CAAC,CAAC,CAAC,+DAA+D;AAC5G,MAAM,KAAK,GAAG,IAAI,kBAAU,CAAC;IAC3B,GAAG,EAAE,8CAA8C;IACnD,MAAM,EAAE,IAAI,yBAAW,EAAE,EAAE,4CAA4C;IACvE,KAAK,EAAE,CAAC,IAAI,uCAAoB,EAAE,EAAE,IAAI,yBAAa,EAAE,CAAC,EAAE,2CAA2C;CACtG,CAAC,CAAC;AAEH,SAAe,IAAI;;QACjB,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC;YAC/B,MAAM,EAAE,0CAA0C;SACnD,CAAC,CAAC;QACH,sCAAsC;QACtC,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACnD,CAAC;CAAA;AAED,sCAAsC;AACtC,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}