-
Notifications
You must be signed in to change notification settings - Fork 5
[nodejs] Clean up inputs/outputs for InvokeAgent spans based on new OTel format #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b4a7717
aa10faf
aa0f134
9ecfd83
a20c19a
7e9dc62
e99a70d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -231,7 +231,10 @@ export class OpenAIAgentsTraceProcessor implements TracingProcessor { | |||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof resp.output === 'string') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY, resp.output); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY, JSON.stringify(resp.output)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| otelSpan.setAttribute( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| OpenTelemetryConstants.GEN_AI_OUTPUT_MESSAGES_KEY, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.buildOutputMessages(resp.output as Array<{ role: string; content: Array<{ type: string; text: string }> }>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -250,24 +253,53 @@ export class OpenAIAgentsTraceProcessor implements TracingProcessor { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inputObj && !this.suppressInvokeAgentInput) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof inputObj === 'string') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const parsed = JSON.parse(inputObj as string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (Array.isArray(parsed)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| otelSpan.setAttribute( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.buildInputMessages(parsed) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // If parsing fails, fall back to raw string behavior | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| otelSpan.setAttribute(OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY, inputObj); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (Array.isArray(inputObj)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Store the complete _input structure as JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // build the input messages from array | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // build the input messages from array | |
| // Build the input messages from array |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new private method 'buildInputMessages' is missing JSDoc documentation. Other private methods in this file have JSDoc comments explaining their purpose. Add a JSDoc comment describing what this method does, its parameters, and return value.
| /** | |
| * Builds a JSON-encoded representation of input messages for tracing. | |
| * Extracts the text content of user-role messages when available, otherwise | |
| * falls back to serializing the full input array. | |
| * | |
| * @param arr - Array of message objects containing a role and string content. | |
| * @returns A JSON string of user message contents, or the original array if no valid user content is found. | |
| */ |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new private method 'buildOutputMessages' is missing JSDoc documentation. Other private methods in this file have JSDoc comments explaining their purpose. Add a JSDoc comment describing what this method does, its parameters, and return value.
| /** | |
| * Builds a serialized representation of output messages from the agent response. | |
| * | |
| * Extracts text from message content parts where the part type is `output_text` | |
| * and returns a JSON string of those texts. If no such texts are found, the | |
| * original message array is serialized instead. | |
| * | |
| * @param arr - Array of response messages, each containing a role and a content | |
| * array of typed text parts. | |
| * @returns JSON string containing the extracted output texts or the original | |
| * message array as a fallback. | |
| */ |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name 'userTexts' is misleading in the context of 'buildOutputMessages'. This variable collects output texts (type 'output_text'), not user texts. Consider renaming to 'outputTexts' for clarity.
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage for the edge case where elements in the output array have non-array content properties. The code handles this case with a check on line 291-292, but there's no test verifying this behavior works correctly.
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructuring pattern 'for (const { content } of arr)' on line 290 will throw a runtime error if any element in the array is null, undefined, or doesn't have a 'content' property. Add a null check before destructuring to prevent potential runtime errors.
| for (const { content } of arr) { | |
| if (!Array.isArray(content)) { | |
| continue; | |
| } | |
| for (const { type, text } of content) { | |
| for (const message of arr) { | |
| if (!message || !Array.isArray(message.content)) { | |
| continue; | |
| } | |
| for (const part of message.content) { | |
| if (!part) { | |
| continue; | |
| } | |
| const { type, text } = part; |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructuring pattern 'for (const { type, text } of content)' on line 295 will throw a runtime error if any element in the content array is null, undefined, or doesn't have 'type' or 'text' properties. Add a null check before destructuring to prevent potential runtime errors.
| for (const { type, text } of content) { | |
| for (const item of content) { | |
| if (!item) { | |
| continue; | |
| } | |
| const { type, text } = item; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -550,5 +550,210 @@ describe('OpenAIAgentsTraceProcessor', () => { | |||||
| const keys = (respMock._attrs as Array<[string, unknown]>).map(([k]) => k); | ||||||
| expect(keys).not.toContain(OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY); | ||||||
| }); | ||||||
|
|
||||||
| it('records full array JSON when only assistant messages are present', async () => { | ||||||
| const processor = new OpenAIAgentsTraceProcessor(tracer); | ||||||
| const traceData = { traceId: 'trace-assistant-only', name: 'Agent' } as any; | ||||||
| await processor.onTraceStart(traceData); | ||||||
|
|
||||||
| const inputArray = [ | ||||||
| { | ||||||
| role: 'assistant', | ||||||
| content: 'Assistant reply', | ||||||
| }, | ||||||
| ]; | ||||||
|
|
||||||
| const respSpan = { | ||||||
| spanId: 'resp-assistant-span', | ||||||
| traceId: 'trace-assistant-only', | ||||||
| startedAt: new Date().toISOString(), | ||||||
| spanData: { | ||||||
| type: 'response' as const, | ||||||
| name: 'ResponseAssistantOnly', | ||||||
| _input: inputArray, | ||||||
| _response: { model: 'gpt-4', output: 'ok' }, | ||||||
| }, | ||||||
| } as any; | ||||||
|
|
||||||
| await processor.onSpanStart(respSpan); | ||||||
| await processor.onSpanEnd(respSpan); | ||||||
|
|
||||||
| const respMock = spansByName['ResponseAssistantOnly']; | ||||||
| const attrs = respMock._attrs as Array<[string, unknown]>; | ||||||
| const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY); | ||||||
| expect(entry).toBeDefined(); | ||||||
|
|
||||||
| const value = entry![1] as string; | ||||||
| const parsed = JSON.parse(value); | ||||||
| expect(parsed).toEqual(inputArray); | ||||||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| }); | ||||||
| it('records user text content for array _input on response spans', async () => { | ||||||
| const processor = new OpenAIAgentsTraceProcessor(tracer); | ||||||
| const traceData = { traceId: 'trace-array-input', name: 'Agent' } as any; | ||||||
| await processor.onTraceStart(traceData); | ||||||
|
|
||||||
| const respSpan = { | ||||||
| spanId: 'resp-array-span', | ||||||
| traceId: 'trace-array-input', | ||||||
| startedAt: new Date().toISOString(), | ||||||
| spanData: { | ||||||
| type: 'response' as const, | ||||||
| name: 'ResponseArray', | ||||||
| _input: [ | ||||||
| { role: 'user', content: 'Hello user 1' }, | ||||||
| { role: 'user', content: 'Hello user 2' }, | ||||||
| ], | ||||||
| _response: { model: 'gpt-4', output: 'ok' }, | ||||||
| }, | ||||||
| } as any; | ||||||
|
|
||||||
| await processor.onSpanStart(respSpan); | ||||||
| await processor.onSpanEnd(respSpan); | ||||||
|
|
||||||
| const respMock = spansByName['ResponseArray']; | ||||||
| const attrs = respMock._attrs as Array<[string, unknown]>; | ||||||
| const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY); | ||||||
| expect(entry).toBeDefined(); | ||||||
|
|
||||||
| const value = entry![1] as string; | ||||||
| const parsed = JSON.parse(value); | ||||||
| expect(parsed).toEqual(['Hello user 1', 'Hello user 2']); | ||||||
| }); | ||||||
|
|
||||||
| it('parses stringified array _input and records only user text content', async () => { | ||||||
| const processor = new OpenAIAgentsTraceProcessor(tracer); | ||||||
| const traceData = { traceId: 'trace-array-input-string', name: 'Agent' } as any; | ||||||
| await processor.onTraceStart(traceData); | ||||||
|
|
||||||
| const inputArray = [ | ||||||
| { role: 'user', content: 'Hello user 1' }, | ||||||
| { role: 'user', content: 'Hello user 2' }, | ||||||
| { role: 'assistant', content: 'Assistant reply' }, | ||||||
| ]; | ||||||
|
|
||||||
| const respSpan = { | ||||||
| spanId: 'resp-array-span-string', | ||||||
| traceId: 'trace-array-input-string', | ||||||
| startedAt: new Date().toISOString(), | ||||||
| spanData: { | ||||||
| type: 'response' as const, | ||||||
| name: 'ResponseArrayString', | ||||||
| _input: JSON.stringify(inputArray), | ||||||
| _response: { model: 'gpt-4', output: 'ok' }, | ||||||
| }, | ||||||
| } as any; | ||||||
|
|
||||||
| await processor.onSpanStart(respSpan); | ||||||
| await processor.onSpanEnd(respSpan); | ||||||
|
|
||||||
| const respMock = spansByName['ResponseArrayString']; | ||||||
| const attrs = respMock._attrs as Array<[string, unknown]>; | ||||||
| const entry = attrs.find(([k]) => k === OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY); | ||||||
| expect(entry).toBeDefined(); | ||||||
|
|
||||||
| const value = entry![1] as string; | ||||||
| const parsed = JSON.parse(value); | ||||||
| expect(parsed).toEqual(['Hello user 1', 'Hello user 2']); | ||||||
| }); | ||||||
|
|
||||||
| it('records [gen_ai.input.messages] attribute for array input with non standard schema on response spans', async () => { | ||||||
| const processor = new OpenAIAgentsTraceProcessor(tracer); | ||||||
| const traceData = { traceId: 'trace-array-input', name: 'Agent' } as any; | ||||||
| await processor.onTraceStart(traceData); | ||||||
| const inputArray = [ | ||||||
| { type: 'text', content: 'message 1' }, | ||||||
| { type: 'text', content: 'message 2' }, | ||||||
| ]; | ||||||
| const respSpan = { | ||||||
| spanId: 'resp-array-span', | ||||||
| traceId: 'trace-array-input', | ||||||
| startedAt: new Date().toISOString(), | ||||||
| spanData: { | ||||||
| type: 'response' as const, | ||||||
| name: 'ResponseArray', | ||||||
fpfp100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| _input: inputArray, | ||||||
fpfp100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| _input: inputArray, | |
| _input: inputArray, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra whitespace before 'as' keyword. There should be only one space between 'output' and 'as'.