Skip to content

Commit ac05fde

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add eventId in CallbackContext and ToolContext
This allows callbacks and tools to know which event triggered them. This is useful in telemetry and tracing. PiperOrigin-RevId: 866050613
1 parent 12defee commit ac05fde

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

core/src/main/java/com/google/adk/agents/CallbackContext.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class CallbackContext extends ReadonlyContext {
3131

3232
protected EventActions eventActions;
3333
private final State state;
34+
private final String eventId;
3435

3536
/**
3637
* Initializes callback context.
@@ -39,9 +40,22 @@ public class CallbackContext extends ReadonlyContext {
3940
* @param eventActions Callback event actions.
4041
*/
4142
public CallbackContext(InvocationContext invocationContext, EventActions eventActions) {
43+
this(invocationContext, eventActions, null);
44+
}
45+
46+
/**
47+
* Initializes callback context.
48+
*
49+
* @param invocationContext Current invocation context.
50+
* @param eventActions Callback event actions.
51+
* @param eventId The ID of the event associated with this context.
52+
*/
53+
public CallbackContext(
54+
InvocationContext invocationContext, EventActions eventActions, String eventId) {
4255
super(invocationContext);
4356
this.eventActions = eventActions != null ? eventActions : EventActions.builder().build();
4457
this.state = new State(invocationContext.session().state(), this.eventActions.stateDelta());
58+
this.eventId = eventId;
4559
}
4660

4761
/** Returns the delta-aware state of the current callback. */
@@ -55,6 +69,11 @@ public EventActions eventActions() {
5569
return eventActions;
5670
}
5771

72+
/** Returns the ID of the event associated with this context. */
73+
public String eventId() {
74+
return eventId;
75+
}
76+
5877
/**
5978
* Lists the filenames of the artifacts attached to the current session.
6079
*

core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ private Flowable<LlmResponse> callLlm(
237237
private Single<Optional<LlmResponse>> handleBeforeModelCallback(
238238
InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent) {
239239
Event callbackEvent = modelResponseEvent.toBuilder().build();
240-
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
240+
CallbackContext callbackContext =
241+
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());
241242

242243
Maybe<LlmResponse> pluginResult =
243244
context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder);
@@ -274,7 +275,8 @@ private Maybe<LlmResponse> handleOnModelErrorCallback(
274275
Event modelResponseEvent,
275276
Throwable throwable) {
276277
Event callbackEvent = modelResponseEvent.toBuilder().build();
277-
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
278+
CallbackContext callbackContext =
279+
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());
278280
Exception ex = throwable instanceof Exception e ? e : new Exception(throwable);
279281

280282
Maybe<LlmResponse> pluginResult =
@@ -308,7 +310,8 @@ private Maybe<LlmResponse> handleOnModelErrorCallback(
308310
private Single<LlmResponse> handleAfterModelCallback(
309311
InvocationContext context, LlmResponse llmResponse, Event modelResponseEvent) {
310312
Event callbackEvent = modelResponseEvent.toBuilder().build();
311-
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
313+
CallbackContext callbackContext =
314+
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());
312315

313316
Maybe<LlmResponse> pluginResult =
314317
context.pluginManager().afterModelCallback(callbackContext, llmResponse);

core/src/main/java/com/google/adk/tools/ToolContext.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ private ToolContext(
3535
InvocationContext invocationContext,
3636
EventActions eventActions,
3737
Optional<String> functionCallId,
38-
Optional<ToolConfirmation> toolConfirmation) {
39-
super(invocationContext, eventActions);
38+
Optional<ToolConfirmation> toolConfirmation,
39+
@Nullable String eventId) {
40+
super(invocationContext, eventActions, eventId);
4041
this.functionCallId = functionCallId;
4142
this.toolConfirmation = toolConfirmation;
4243
}
@@ -125,7 +126,8 @@ public Builder toBuilder() {
125126
return new Builder(invocationContext)
126127
.actions(eventActions)
127128
.functionCallId(functionCallId.orElse(null))
128-
.toolConfirmation(toolConfirmation.orElse(null));
129+
.toolConfirmation(toolConfirmation.orElse(null))
130+
.eventId(eventId());
129131
}
130132

131133
@Override
@@ -148,6 +150,7 @@ public static final class Builder {
148150
private EventActions eventActions = EventActions.builder().build(); // Default empty actions
149151
private Optional<String> functionCallId = Optional.empty();
150152
private Optional<ToolConfirmation> toolConfirmation = Optional.empty();
153+
private String eventId;
151154

152155
private Builder(InvocationContext invocationContext) {
153156
this.invocationContext = invocationContext;
@@ -171,8 +174,15 @@ public Builder toolConfirmation(ToolConfirmation toolConfirmation) {
171174
return this;
172175
}
173176

177+
@CanIgnoreReturnValue
178+
public Builder eventId(String eventId) {
179+
this.eventId = eventId;
180+
return this;
181+
}
182+
174183
public ToolContext build() {
175-
return new ToolContext(invocationContext, eventActions, functionCallId, toolConfirmation);
184+
return new ToolContext(
185+
invocationContext, eventActions, functionCallId, toolConfirmation, eventId);
176186
}
177187
}
178188
}

0 commit comments

Comments
 (0)