Skip to content

Commit e346e46

Browse files
committed
feat(openinference-vercel): Instrument multi-part tool calls and tool responses
1 parent 017879d commit e346e46

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

js/packages/openinference-vercel/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
"import": "./dist/esm/utils.js",
2424
"require": "./dist/src/utils.js",
2525
"types": "./dist/src/utils.d.ts"
26+
},
27+
"./types": {
28+
"import": "./dist/esm/types.js",
29+
"require": "./dist/src/types.js",
30+
"types": "./dist/src/types.d.ts"
2631
}
2732
},
2833
"files": [

js/packages/openinference-vercel/src/utils.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,33 @@ const getInputMessageAttributes = (promptMessages?: AttributeValue) => {
242242

243243
return messages.reduce((acc: Attributes, message, index) => {
244244
const MESSAGE_PREFIX = `${SemanticConventions.LLM_INPUT_MESSAGES}.${index}`;
245-
if (isArrayOfObjects(message.content)) {
245+
if (message.role === "tool") {
246+
return {
247+
...acc,
248+
...message,
249+
[`${MESSAGE_PREFIX}.${SemanticConventions.MESSAGE_ROLE}`]: message.role,
250+
[`${MESSAGE_PREFIX}.${SemanticConventions.TOOL_CALL_ID}`]:
251+
typeof message.toolCallId === "string"
252+
? message.toolCallId
253+
: undefined,
254+
[`${MESSAGE_PREFIX}.${SemanticConventions.TOOL_NAME}`]:
255+
typeof message.toolName === "string" ? message.toolName : undefined,
256+
[`${MESSAGE_PREFIX}.${SemanticConventions.MESSAGE_CONTENT}`]:
257+
Array.isArray(message.content)
258+
? typeof message.content[0]?.result === "string"
259+
? message.content[0].result
260+
: message.content[0]?.result
261+
? JSON.stringify(message.content[0].result)
262+
: undefined
263+
: typeof message.content === "string"
264+
? message.content
265+
: undefined,
266+
};
267+
} else if (isArrayOfObjects(message.content)) {
246268
const messageAttributes = message.content.reduce(
247269
(acc: Attributes, content, contentIndex) => {
248270
const CONTENTS_PREFIX = `${MESSAGE_PREFIX}.${SemanticConventions.MESSAGE_CONTENTS}.${contentIndex}`;
271+
const TOOL_CALL_PREFIX = `${MESSAGE_PREFIX}.${SemanticConventions.MESSAGE_TOOL_CALLS}.${contentIndex}`;
249272
return {
250273
...acc,
251274
[`${CONTENTS_PREFIX}.${SemanticConventions.MESSAGE_CONTENT_TYPE}`]:
@@ -254,6 +277,20 @@ const getInputMessageAttributes = (promptMessages?: AttributeValue) => {
254277
typeof content.text === "string" ? content.text : undefined,
255278
[`${CONTENTS_PREFIX}.${SemanticConventions.MESSAGE_CONTENT_IMAGE}`]:
256279
typeof content.image === "string" ? content.image : undefined,
280+
[`${TOOL_CALL_PREFIX}.${SemanticConventions.TOOL_CALL_ID}`]:
281+
typeof content.toolCallId === "string"
282+
? content.toolCallId
283+
: undefined,
284+
[`${TOOL_CALL_PREFIX}.${SemanticConventions.TOOL_CALL_FUNCTION_NAME}`]:
285+
typeof content.toolName === "string"
286+
? content.toolName
287+
: undefined,
288+
[`${TOOL_CALL_PREFIX}.${SemanticConventions.TOOL_CALL_FUNCTION_ARGUMENTS_JSON}`]:
289+
typeof content.args === "string"
290+
? content.args
291+
: typeof content.args === "object"
292+
? JSON.stringify(content.args)
293+
: undefined,
257294
};
258295
},
259296
{},
@@ -368,8 +405,6 @@ const getOpenInferenceAttributes = (attributes: Attributes): Attributes => {
368405
const openInferenceAttributes = {
369406
[SemanticConventions.OPENINFERENCE_SPAN_KIND]: spanKind ?? undefined,
370407
};
371-
console.log("new span attributes");
372-
console.table(attributes);
373408
return AISemanticConventionsList.reduce(
374409
(openInferenceAttributes: Attributes, convention) => {
375410
/**

0 commit comments

Comments
 (0)