Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,26 @@ export class OpenAIAgentsTraceProcessor implements TracingProcessor {
// Store the complete _input structure as JSON
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
otelSpan.setAttribute(
OpenTelemetryConstants.GEN_AI_INPUT_MESSAGES_KEY,
JSON.stringify(inputObj)
this.buildInputMessages(inputObj)
);
}
}
}

Copy link

Copilot AI Jan 13, 2026

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.

Suggested change
/**
* 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 uses AI. Check for mistakes.
// Get attributes but filter out unwanted ones
const attrs = Utils.getAttributesFromInput(inputObj);
Object.entries(attrs).forEach(([key, value]) => {
if (value !== null && value !== undefined &&
key !== Constants.GEN_AI_REQUEST_CONTENT_KEY) {
otelSpan.setAttribute(key, value as string | number | boolean);
}
});
private buildInputMessages(arr: Array<{role?: string; parts?: Array<{type: string; content: unknown}>}>): string {
const userTexts = [];
for (const message of arr) {
if (message && message.role === "user" && Array.isArray(message.parts)) {
for (const p of message.parts) {
if (p && p.type === "text" && typeof p.content === "string") {
userTexts.push(p.content);
}
}
}
}
return userTexts.length ? JSON.stringify(userTexts) : JSON.stringify(arr);
}
Comment thread
fpfp100 marked this conversation as resolved.
Outdated


Comment thread
fpfp100 marked this conversation as resolved.
/**
* Process generation span data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('OpenAIAgentsTraceProcessor', () => {

processor.onSpanStart(agentSpan);

const otelSpans = (processor as any).otelSpans;
const otelSpans = (processor as any).otelSpans;
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
expect(otelSpans.has('agent-1')).toBe(true);

processor.onSpanEnd(agentSpan);
Expand Down Expand Up @@ -550,5 +550,81 @@ 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',
parts: [
{ type: 'text', 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);
Comment thread
threddy marked this conversation as resolved.
});
Comment thread
fpfp100 marked this conversation as resolved.
Outdated
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',
parts: [
{ type: 'text', content: 'Hello user 1' },
{ type: 'text', 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']);
});
});
});