Skip to content

Commit 364140a

Browse files
reniowoodcopybara-github
authored andcommitted
ADK changes
PiperOrigin-RevId: 858348685
1 parent 27d8baa commit 364140a

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

src/app/core/services/trace.service.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ describe('TraceService', () => {
122122
const indices = await firstValueFrom(service.hoveredMessageIndices$);
123123
expect(indices).toEqual([1]);
124124
});
125+
126+
it(
127+
'should safely handle messages with missing event data', async () => {
128+
const messages = [
129+
{role: 'bot', eventId: 'e1'},
130+
{role: 'bot', eventId: 'e2'}, // e2 is missing in eventData
131+
];
132+
const eventData = new Map<string, any>([
133+
['e1', {invocationId: 'inv1'}],
134+
]);
135+
const span: Partial<Span> = {
136+
attributes: {'gcp.vertex.agent.event_id': 'e1'},
137+
};
138+
service.setMessages(messages);
139+
service.setEventData(eventData);
140+
service.setHoveredMessages(span as Span, 'inv1');
141+
const indices = await firstValueFrom(service.hoveredMessageIndices$);
142+
expect(indices).toEqual([0]);
143+
});
125144
});
126145

127146
describe('resetTraceService', () => {

src/app/core/services/trace.service.ts

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,73 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
import {Injectable} from '@angular/core';
18-
import {BehaviorSubject, Subject} from 'rxjs';
19+
import {BehaviorSubject} from 'rxjs';
20+
1921
import {Span} from '../models/Trace';
22+
2023
import {TraceService as TraceServiceInterface} from './interfaces/trace';
2124

2225
@Injectable({providedIn: 'root'})
2326
export class TraceService implements TraceServiceInterface {
24-
private selectedTraceRowSource = new BehaviorSubject<Span | undefined>(undefined);
25-
selectedTraceRow$ = this.selectedTraceRowSource.asObservable();
27+
private selectedTraceRowSource =
28+
new BehaviorSubject<Span|undefined>(undefined);
29+
selectedTraceRow$ = this.selectedTraceRowSource.asObservable();
2630

27-
private eventDataSource = new BehaviorSubject<Map<string, any> | undefined>(undefined);
28-
eventData$ = this.eventDataSource.asObservable();
31+
private eventDataSource =
32+
new BehaviorSubject<Map<string, any>|undefined>(undefined);
33+
eventData$ = this.eventDataSource.asObservable();
2934

30-
private hoveredMessageIndicesSource = new BehaviorSubject<number[]>([]);
31-
hoveredMessageIndices$ = this.hoveredMessageIndicesSource.asObservable();
35+
private hoveredMessageIndicesSource = new BehaviorSubject<number[]>([]);
36+
hoveredMessageIndices$ = this.hoveredMessageIndicesSource.asObservable();
3237

33-
private messagesSource = new BehaviorSubject<any[]>([]);
34-
messages$ = this.messagesSource.asObservable();
38+
private messagesSource = new BehaviorSubject<any[]>([]);
39+
messages$ = this.messagesSource.asObservable();
3540

36-
selectedRow(span: Span | undefined) {
37-
this.selectedTraceRowSource.next(span);
38-
}
41+
selectedRow(span: Span|undefined) {
42+
this.selectedTraceRowSource.next(span);
43+
}
3944

40-
setEventData(data: Map<string, any> | undefined) {
41-
this.eventDataSource.next(data);
42-
}
45+
setEventData(data: Map<string, any>|undefined) {
46+
this.eventDataSource.next(data);
47+
}
4348

44-
setMessages(messages: any[]) {
45-
this.messagesSource.next(messages)
46-
}
49+
setMessages(messages: any[]) {
50+
this.messagesSource.next(messages);
51+
}
4752

48-
setHoveredMessages(span: Span | undefined, invocationId: string) {
49-
if (!span) {
50-
this.hoveredMessageIndicesSource.next([]);
51-
return;
52-
}
53+
setHoveredMessages(span: Span|undefined, invocationId: string) {
54+
if (!span) {
55+
this.hoveredMessageIndicesSource.next([]);
56+
return;
57+
}
5358

54-
const attributes = span.attributes
55-
const hasEvent: boolean = attributes && attributes['gcp.vertex.agent.event_id']
56-
let index = 0;
57-
const messageIndices = [];
58-
for (const msg of this.messagesSource.value) {
59-
if (msg.role == 'user') {
60-
index++
61-
continue
62-
}
59+
const attributes = span.attributes;
60+
const hasEvent: boolean =
61+
attributes && attributes['gcp.vertex.agent.event_id'];
62+
const messageIndices = [];
63+
for (const [index, msg] of this.messagesSource.value.entries()) {
64+
if (msg.role === 'user') {
65+
continue;
66+
}
6367

64-
if (this.eventDataSource.value?.get(msg.eventId).invocationId != invocationId) {
65-
index++
66-
continue
67-
}
68+
if (this.eventDataSource.value?.get(msg.eventId)?.invocationId !==
69+
invocationId) {
70+
continue;
71+
}
6872

69-
if (!hasEvent) {
70-
messageIndices.push(index)
71-
index++
72-
continue
73-
} else {
74-
if (attributes['gcp.vertex.agent.event_id'] == msg.eventId) {
75-
messageIndices.push(index)
76-
index++
77-
continue
78-
} else {
79-
index++
80-
continue
81-
}
82-
}
83-
}
84-
this.hoveredMessageIndicesSource.next(messageIndices);
73+
if (!hasEvent ||
74+
attributes['gcp.vertex.agent.event_id'] === msg.eventId) {
75+
messageIndices.push(index);
76+
}
8577
}
78+
this.hoveredMessageIndicesSource.next(messageIndices);
79+
}
8680

87-
resetTraceService() {
88-
this.eventDataSource.next(undefined);
89-
this.messagesSource.next([]);
90-
this.hoveredMessageIndicesSource.next([])
91-
}
81+
resetTraceService() {
82+
this.eventDataSource.next(undefined);
83+
this.messagesSource.next([]);
84+
this.hoveredMessageIndicesSource.next([]);
85+
}
9286
}

0 commit comments

Comments
 (0)