Skip to content

Commit d55babe

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Refine user message extraction from invocation groups.
The findUserMsgFromInvocGroup function now requires both 'gcp.vertex.agent.invocation_id' and 'gcp.vertex.agent.llm_request' attributes to be present on a span to extract the user message. PiperOrigin-RevId: 841953660
1 parent dac059f commit d55babe

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

src/app/components/trace-tab/trace-tab.component.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,76 @@ describe('TraceTabComponent', () => {
146146
// mocking TraceTreeComponent
147147
});
148148
});
149+
150+
describe('findUserMsgFromInvocGroup', () => {
151+
it('should find user message from span with both invocation_id and llm_request',
152+
() => {
153+
// First span has only invocation_id, second span has both
154+
const group: Span[] = [
155+
{
156+
name: 'invocation',
157+
start_time: 1733084700000000000,
158+
end_time: 1733084760000000000,
159+
span_id: 'span-1',
160+
trace_id: 'trace-1',
161+
attributes: {
162+
'gcp.vertex.agent.invocation_id': 'invoc-1',
163+
},
164+
},
165+
{
166+
name: 'call_llm',
167+
start_time: 1733084710000000000,
168+
end_time: 1733084750000000000,
169+
span_id: 'span-2',
170+
parent_span_id: 'span-1',
171+
trace_id: 'trace-1',
172+
attributes: {
173+
'gcp.vertex.agent.invocation_id': 'invoc-1',
174+
'gcp.vertex.agent.llm_request':
175+
'{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}',
176+
},
177+
},
178+
];
179+
180+
const result = component.findUserMsgFromInvocGroup(group);
181+
expect(result).toBe('hi');
182+
});
183+
184+
it('should return fallback when no span has llm_request', () => {
185+
const group: Span[] = [
186+
{
187+
name: 'invocation',
188+
start_time: 1733084700000000000,
189+
end_time: 1733084760000000000,
190+
span_id: 'span-1',
191+
trace_id: 'trace-1',
192+
attributes: {
193+
'gcp.vertex.agent.invocation_id': 'invoc-1',
194+
},
195+
},
196+
];
197+
198+
const result = component.findUserMsgFromInvocGroup(group);
199+
expect(result).toBe('[no invocation id found]');
200+
});
201+
202+
it('should return error message on invalid JSON', () => {
203+
const group: Span[] = [
204+
{
205+
name: 'call_llm',
206+
start_time: 1733084700000000000,
207+
end_time: 1733084760000000000,
208+
span_id: 'span-1',
209+
trace_id: 'trace-1',
210+
attributes: {
211+
'gcp.vertex.agent.invocation_id': 'invoc-1',
212+
'gcp.vertex.agent.llm_request': 'invalid json{',
213+
},
214+
},
215+
];
216+
217+
const result = component.findUserMsgFromInvocGroup(group);
218+
expect(result).toBe('[error parsing request]');
219+
});
220+
});
149221
});

src/app/components/trace-tab/trace-tab.component.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,28 @@ export class TraceTabComponent implements OnInit, OnChanges {
7070
}
7171

7272
findUserMsgFromInvocGroup(group: any[]) {
73+
// Find a span that has both invocation_id and llm_request
74+
// The invocation_id is present on multiple spans, but llm_request
75+
// is only on the call_llm span
7376
const eventItem = group?.find(
7477
item => item.attributes !== undefined &&
75-
'gcp.vertex.agent.invocation_id' in item.attributes)
78+
'gcp.vertex.agent.invocation_id' in item.attributes &&
79+
'gcp.vertex.agent.llm_request' in item.attributes)
7680

7781
if (!eventItem) {
7882
return '[no invocation id found]';
7983
}
8084

81-
const requestJson =
82-
JSON.parse(eventItem.attributes['gcp.vertex.agent.llm_request']) as LlmRequest
83-
const userContent =
84-
requestJson.contents.filter((c: any) => c.role == 'user').at(-1)
85-
return userContent?.parts[0]?.text ?? '[attachment]';
85+
try {
86+
const requestJson =
87+
JSON.parse(eventItem.attributes['gcp.vertex.agent.llm_request']) as
88+
LlmRequest
89+
const userContent =
90+
requestJson.contents.filter((c: any) => c.role == 'user').at(-1)
91+
return userContent?.parts[0]?.text ?? '[attachment]';
92+
} catch {
93+
return '[error parsing request]';
94+
}
8695
}
8796

8897
findInvocIdFromTraceId(traceId: string) {

0 commit comments

Comments
 (0)