@arizeai/openinference-instrumentation-beeai 1.1.0 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arizeai/openinference-instrumentation-beeai",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "license": "Apache-2.0",
5
5
  "author": "gallas.milan@gmail.com",
6
6
  "description": "OpenInference Instrumentation for BeeAI framework",
@@ -36,12 +36,12 @@
36
36
  "@arizeai/openinference-semantic-conventions": "1.0.0"
37
37
  },
38
38
  "peerDependencies": {
39
- "beeai-framework": "^0.1.8"
39
+ "beeai-framework": "^0.1.9"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/jest": "^29.5.12",
43
43
  "@types/node": "^20.14.11",
44
- "beeai-framework": "^0.1.8",
44
+ "beeai-framework": "^0.1.9",
45
45
  "import-in-the-middle": "^1.13.0",
46
46
  "@opentelemetry/exporter-trace-otlp-proto": "^0.50.0",
47
47
  "@opentelemetry/resources": "^1.30.1",
@@ -15,7 +15,6 @@
15
15
  */
16
16
 
17
17
  import { Serializable } from "beeai-framework/internals/serializable";
18
- import { instrumentationLogger } from "beeai-framework/instrumentation/logger";
19
18
  import { getProp } from "beeai-framework/internals/helpers/object";
20
19
  import { EventMeta, InferCallbackValue } from "beeai-framework/emitter/types";
21
20
  import type { ReActAgentCallbacks } from "beeai-framework/agents/react/types";
@@ -52,6 +51,7 @@ import {
52
51
  SemanticConventions,
53
52
  } from "@arizeai/openinference-semantic-conventions";
54
53
  import { Tool, ToolEvents } from "beeai-framework/tools/base";
54
+ import { diag } from "@opentelemetry/api";
55
55
 
56
56
  function parserLLMInputMessages(
57
57
  messages: readonly Message<MessageContentPart, string>[],
@@ -93,233 +93,242 @@ export function getSerializedObjectSafe(
93
93
  dataObject: unknown,
94
94
  meta: EventMeta<unknown>,
95
95
  ) {
96
- // agent events
97
- if (
98
- [startEventName, successEventName, errorEventName, retryEventName].includes(
99
- meta.name as keyof ReActAgentCallbacks,
100
- ) &&
101
- meta.creator instanceof BaseAgent
102
- ) {
103
- const { meta, tools, memory, error, data } =
104
- dataObject as InferCallbackValue<
105
- | ReActAgentCallbacks["start"]
106
- | ReActAgentCallbacks["error"]
107
- | ReActAgentCallbacks["success"]
108
- | ReActAgentCallbacks["retry"]
109
- >;
110
- return {
111
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
112
- OpenInferenceSpanKind.AGENT,
113
- iteration: meta.iteration,
114
- ...(tools?.length > 0 && {
115
- [SemanticConventions.LLM_TOOLS]: tools.map((tool) => ({
116
- [SemanticConventions.TOOL_NAME]: tool.name,
117
- [SemanticConventions.TOOL_DESCRIPTION]: tool.description,
118
- "tool.options": tool.options,
119
- })),
120
- }),
121
- ...(memory.messages.length > 0 && {
122
- [SemanticConventions.INPUT_MIME_TYPE]: MimeType.JSON,
123
- [SemanticConventions.INPUT_VALUE]: JSON.stringify(memory.messages),
124
- }),
125
- ...(error && {
126
- "exception.message": error.message,
127
- "exception.stacktrace": error.stack,
128
- "exception.type": error.name,
129
- }),
130
- ...(data && {
131
- [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
132
- [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(data),
133
- }),
134
- };
135
- }
96
+ try {
97
+ // agent events
98
+ if (
99
+ [
100
+ startEventName,
101
+ successEventName,
102
+ errorEventName,
103
+ retryEventName,
104
+ ].includes(meta.name as keyof ReActAgentCallbacks) &&
105
+ meta.creator instanceof BaseAgent
106
+ ) {
107
+ const { meta, tools, memory, error, data } =
108
+ dataObject as InferCallbackValue<
109
+ | ReActAgentCallbacks["start"]
110
+ | ReActAgentCallbacks["error"]
111
+ | ReActAgentCallbacks["success"]
112
+ | ReActAgentCallbacks["retry"]
113
+ >;
114
+ return {
115
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
116
+ OpenInferenceSpanKind.AGENT,
117
+ iteration: meta.iteration,
118
+ ...(tools?.length > 0 && {
119
+ [SemanticConventions.LLM_TOOLS]: tools.map((tool) => ({
120
+ [SemanticConventions.TOOL_NAME]: tool.name,
121
+ [SemanticConventions.TOOL_DESCRIPTION]: tool.description,
122
+ "tool.options": tool.options,
123
+ })),
124
+ }),
125
+ ...(memory?.messages.length > 0 && {
126
+ [SemanticConventions.INPUT_MIME_TYPE]: MimeType.JSON,
127
+ [SemanticConventions.INPUT_VALUE]: JSON.stringify(memory.messages),
128
+ }),
129
+ ...(error && {
130
+ "exception.message": error.message,
131
+ "exception.stacktrace": error.stack,
132
+ "exception.type": error.name,
133
+ }),
134
+ ...(data && {
135
+ [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
136
+ [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(data),
137
+ }),
138
+ };
139
+ }
136
140
 
137
- // update events
138
- if (
139
- [updateEventName, partialUpdateEventName].includes(
140
- meta.name as keyof ReActAgentCallbacks,
141
- )
142
- ) {
143
- const { data } = dataObject as InferCallbackValue<
144
- ReActAgentCallbacks["partialUpdate"] | ReActAgentCallbacks["update"]
145
- >;
141
+ // update events
142
+ if (
143
+ [updateEventName, partialUpdateEventName].includes(
144
+ meta.name as keyof ReActAgentCallbacks,
145
+ )
146
+ ) {
147
+ const { data } = dataObject as InferCallbackValue<
148
+ ReActAgentCallbacks["partialUpdate"] | ReActAgentCallbacks["update"]
149
+ >;
146
150
 
147
- const output = data.final_answer || data.tool_output;
148
- return {
149
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
150
- OpenInferenceSpanKind.AGENT,
151
- thought: data.thought,
152
- ...(data.tool_name && {
153
- [SemanticConventions.TOOL_NAME]: data.tool_name,
154
- }),
155
- ...(data.tool_input && {
156
- [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(data.tool_input),
157
- }),
158
- ...(output && {
159
- [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
160
- [SemanticConventions.OUTPUT_VALUE]: output,
161
- }),
162
- };
163
- }
151
+ const output = data?.final_answer || data?.tool_output;
152
+ return {
153
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
154
+ OpenInferenceSpanKind.AGENT,
155
+ thought: data.thought,
156
+ ...(data?.tool_name && {
157
+ [SemanticConventions.TOOL_NAME]: data.tool_name,
158
+ }),
159
+ ...(data?.tool_input && {
160
+ [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(
161
+ data.tool_input,
162
+ ),
163
+ }),
164
+ ...(output && {
165
+ [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
166
+ [SemanticConventions.OUTPUT_VALUE]: output,
167
+ }),
168
+ };
169
+ }
164
170
 
165
- // tool events (from agent)
166
- if (
167
- [toolErrorEventName, toolStartEventName, toolSuccessEventName].includes(
168
- meta.name as keyof ReActAgentCallbacks,
169
- )
170
- ) {
171
- const { data } = dataObject as InferCallbackValue<
172
- | ReActAgentCallbacks["toolError"]
173
- | ReActAgentCallbacks["toolStart"]
174
- | ReActAgentCallbacks["toolSuccess"]
175
- >;
176
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
177
- const output: any = data.result && data.result.createSnapshot();
171
+ // tool events (from agent)
172
+ if (
173
+ [toolErrorEventName, toolStartEventName, toolSuccessEventName].includes(
174
+ meta.name as keyof ReActAgentCallbacks,
175
+ )
176
+ ) {
177
+ const { data } = dataObject as InferCallbackValue<
178
+ | ReActAgentCallbacks["toolError"]
179
+ | ReActAgentCallbacks["toolStart"]
180
+ | ReActAgentCallbacks["toolSuccess"]
181
+ >;
182
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
183
+ const output: any = data?.result && data.result.createSnapshot();
178
184
 
179
- return {
180
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.TOOL,
181
- thought: data.iteration.thought,
182
- ...(data.input
183
- ? { [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(data.input) }
184
- : {}),
185
- ...(data.tool.description && {
186
- [SemanticConventions.TOOL_DESCRIPTION]: data.tool.description,
187
- }),
188
- ...(data.tool.name && {
189
- [SemanticConventions.TOOL_NAME]: data.tool.name,
190
- }),
191
- ...(data.error && {
192
- "exception.message": data.error.message,
193
- "exception.stacktrace": data.error.stack,
194
- "exception.type": data.error.name,
195
- }),
196
- ...(output && {
197
- [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(output),
198
- }),
199
- };
200
- }
201
- // tool events native
202
- if (
203
- [
204
- startToolEventName,
205
- successToolEventName,
206
- finishToolEventName,
207
- errorToolEventName,
208
- retryToolEventName,
209
- ].includes(meta.name as keyof ToolEvents) &&
210
- meta.creator instanceof Tool
211
- ) {
212
- if (!dataObject) {
213
185
  return {
214
186
  [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
215
187
  OpenInferenceSpanKind.TOOL,
188
+ thought: data.iteration.thought,
189
+ ...(data?.input
190
+ ? {
191
+ [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(data.input),
192
+ }
193
+ : {}),
194
+ ...(data?.tool.description && {
195
+ [SemanticConventions.TOOL_DESCRIPTION]: data.tool.description,
196
+ }),
197
+ ...(data?.tool.name && {
198
+ [SemanticConventions.TOOL_NAME]: data.tool.name,
199
+ }),
200
+ ...(data?.error && {
201
+ "exception.message": data.error.message,
202
+ "exception.stacktrace": data.error.stack,
203
+ "exception.type": data.error.name,
204
+ }),
205
+ ...(output && {
206
+ [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(output),
207
+ }),
216
208
  };
217
209
  }
218
- const { input, output, error } = dataObject as InferCallbackValue<
219
- | ToolEvents["start"]
220
- | ToolEvents["success"]
221
- | ToolEvents["retry"]
222
- | ToolEvents["error"]
223
- >;
210
+ // tool events native
211
+ if (
212
+ [
213
+ startToolEventName,
214
+ successToolEventName,
215
+ finishToolEventName,
216
+ errorToolEventName,
217
+ retryToolEventName,
218
+ ].includes(meta.name as keyof ToolEvents) &&
219
+ meta.creator instanceof Tool
220
+ ) {
221
+ if (!dataObject) {
222
+ return {
223
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
224
+ OpenInferenceSpanKind.TOOL,
225
+ };
226
+ }
227
+ const { input, output, error } = dataObject as InferCallbackValue<
228
+ | ToolEvents["start"]
229
+ | ToolEvents["success"]
230
+ | ToolEvents["retry"]
231
+ | ToolEvents["error"]
232
+ >;
224
233
 
225
- return {
226
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.TOOL,
227
- ...(input
228
- ? { [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(input) }
229
- : {}),
230
- ...(output && {
231
- [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(output),
232
- }),
233
- ...(error && {
234
- "exception.message": error.message,
235
- "exception.stacktrace": error.stack,
236
- "exception.type": error.name,
237
- }),
238
- };
239
- }
234
+ return {
235
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
236
+ OpenInferenceSpanKind.TOOL,
237
+ ...(input
238
+ ? { [SemanticConventions.TOOL_PARAMETERS]: JSON.stringify(input) }
239
+ : {}),
240
+ ...(output && {
241
+ [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(output),
242
+ }),
243
+ ...(error && {
244
+ "exception.message": error.message,
245
+ "exception.stacktrace": error.stack,
246
+ "exception.type": error.name,
247
+ }),
248
+ };
249
+ }
240
250
 
241
- // llm events
242
- if (
243
- [
244
- successLLMEventName,
245
- startLLMEventName,
246
- errorLLMEventName,
247
- newTokenLLMEventName,
248
- ].includes(meta.name as keyof ChatModelEvents) &&
249
- meta.creator instanceof ChatModel
250
- ) {
251
- const { value, input, error } = dataObject as InferCallbackValue<
252
- | ChatModelEvents["success"]
253
- | ChatModelEvents["start"]
254
- | ChatModelEvents["error"]
255
- | ChatModelEvents["newToken"]
256
- >;
251
+ // llm events
252
+ if (
253
+ [
254
+ successLLMEventName,
255
+ startLLMEventName,
256
+ errorLLMEventName,
257
+ newTokenLLMEventName,
258
+ ].includes(meta.name as keyof ChatModelEvents) &&
259
+ meta.creator instanceof ChatModel
260
+ ) {
261
+ const { value, input, error } = dataObject as InferCallbackValue<
262
+ | ChatModelEvents["success"]
263
+ | ChatModelEvents["start"]
264
+ | ChatModelEvents["error"]
265
+ | ChatModelEvents["newToken"]
266
+ >;
257
267
 
258
- const creator = meta.creator.createSnapshot();
259
- return {
260
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.LLM,
261
- ...(value?.usage?.completionTokens && {
262
- [SemanticConventions.LLM_TOKEN_COUNT_COMPLETION]:
263
- value?.usage?.completionTokens,
264
- }),
265
- ...(value?.usage?.promptTokens && {
266
- [SemanticConventions.LLM_TOKEN_COUNT_PROMPT]:
267
- value?.usage?.promptTokens,
268
- }),
269
- ...(value?.usage?.totalTokens && {
270
- [SemanticConventions.LLM_TOKEN_COUNT_TOTAL]: value?.usage?.totalTokens,
271
- }),
272
- ...(input?.messages.length > 0 && {
273
- [SemanticConventions.INPUT_MIME_TYPE]: MimeType.JSON,
274
- [SemanticConventions.INPUT_VALUE]: JSON.stringify(input.messages),
275
- ...parserLLMInputMessages(input.messages),
276
- }),
277
- ...(value?.messages.length > 0 && {
278
- [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
279
- [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(value.messages),
280
- ...parseLLMOutputMessages(value.messages),
281
- }),
282
- ...(error && {
283
- "exception.message": error.message,
284
- "exception.stacktrace": error.stack,
285
- "exception.type": error.name,
286
- }),
287
- ...("providerId" in creator && {
288
- [SemanticConventions.LLM_PROVIDER]: creator.providerId,
289
- [`${SemanticAttributePrefixes.metadata}.${LLMAttributePostfixes.provider}`]:
290
- creator.providerId,
291
- }),
292
- ...("modelId" in creator && {
293
- [SemanticConventions.LLM_MODEL_NAME]: creator.modelId,
294
- [`${SemanticAttributePrefixes.metadata}.${LLMAttributePostfixes.model_name}`]:
295
- creator.modelId,
296
- }),
297
- ...(creator.parameters && {
298
- [SemanticConventions.LLM_INVOCATION_PARAMETERS]: JSON.stringify(
299
- creator.parameters,
300
- ),
301
- }),
302
- };
303
- }
304
- if (meta.name === finishLLMEventName && meta.creator instanceof ChatModel) {
305
- return {
306
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]: OpenInferenceSpanKind.LLM,
307
- };
308
- }
268
+ const creator = meta.creator.createSnapshot();
269
+ return {
270
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
271
+ OpenInferenceSpanKind.LLM,
272
+ ...(value?.usage?.completionTokens && {
273
+ [SemanticConventions.LLM_TOKEN_COUNT_COMPLETION]:
274
+ value?.usage?.completionTokens,
275
+ }),
276
+ ...(value?.usage?.promptTokens && {
277
+ [SemanticConventions.LLM_TOKEN_COUNT_PROMPT]:
278
+ value?.usage?.promptTokens,
279
+ }),
280
+ ...(value?.usage?.totalTokens && {
281
+ [SemanticConventions.LLM_TOKEN_COUNT_TOTAL]:
282
+ value?.usage?.totalTokens,
283
+ }),
284
+ ...(input?.messages.length > 0 && {
285
+ [SemanticConventions.INPUT_MIME_TYPE]: MimeType.JSON,
286
+ [SemanticConventions.INPUT_VALUE]: JSON.stringify(input.messages),
287
+ ...parserLLMInputMessages(input.messages),
288
+ }),
289
+ ...(value?.messages.length > 0 && {
290
+ [SemanticConventions.OUTPUT_MIME_TYPE]: MimeType.JSON,
291
+ [SemanticConventions.OUTPUT_VALUE]: JSON.stringify(value.messages),
292
+ ...parseLLMOutputMessages(value.messages),
293
+ }),
294
+ ...(error && {
295
+ "exception.message": error.message,
296
+ "exception.stacktrace": error.stack,
297
+ "exception.type": error.name,
298
+ }),
299
+ ...("providerId" in creator && {
300
+ [SemanticConventions.LLM_PROVIDER]: creator.providerId,
301
+ [`${SemanticAttributePrefixes.metadata}.${LLMAttributePostfixes.provider}`]:
302
+ creator.providerId,
303
+ }),
304
+ ...("modelId" in creator && {
305
+ [SemanticConventions.LLM_MODEL_NAME]: creator.modelId,
306
+ [`${SemanticAttributePrefixes.metadata}.${LLMAttributePostfixes.model_name}`]:
307
+ creator.modelId,
308
+ }),
309
+ ...(creator?.parameters && {
310
+ [SemanticConventions.LLM_INVOCATION_PARAMETERS]: JSON.stringify(
311
+ creator.parameters,
312
+ ),
313
+ }),
314
+ };
315
+ }
316
+ if (meta.name === finishLLMEventName && meta.creator instanceof ChatModel) {
317
+ return {
318
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
319
+ OpenInferenceSpanKind.LLM,
320
+ };
321
+ }
309
322
 
310
- // other events
311
- const data = getProp(dataObject, ["data"], dataObject);
312
- if (data instanceof Serializable) {
313
- try {
323
+ // other events
324
+ const data = getProp(dataObject, ["data"], dataObject);
325
+ if (data instanceof Serializable) {
314
326
  return { data: JSON.stringify(data.createSnapshot()), test: "hallo" };
315
- } catch (e) {
316
- instrumentationLogger.warn(
317
- e,
318
- "Invalid createSnapshot method in the Serializable class",
319
- );
320
- return null;
321
327
  }
322
- }
323
328
 
324
- return { data: JSON.stringify(data) };
329
+ return { data: JSON.stringify(data) };
330
+ } catch (e) {
331
+ diag.warn("Failed to parse event data", e);
332
+ return null;
333
+ }
325
334
  }