Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions core/src/main/java/com/google/adk/agents/CallbackContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class CallbackContext extends ReadonlyContext {

protected EventActions eventActions;
private final State state;
private final String eventId;

/**
* Initializes callback context.
Expand All @@ -39,9 +40,22 @@ public class CallbackContext extends ReadonlyContext {
* @param eventActions Callback event actions.
*/
public CallbackContext(InvocationContext invocationContext, EventActions eventActions) {
this(invocationContext, eventActions, null);
}

/**
* Initializes callback context.
*
* @param invocationContext Current invocation context.
* @param eventActions Callback event actions.
* @param eventId The ID of the event associated with this context.
*/
public CallbackContext(
InvocationContext invocationContext, EventActions eventActions, String eventId) {
super(invocationContext);
this.eventActions = eventActions != null ? eventActions : EventActions.builder().build();
this.state = new State(invocationContext.session().state(), this.eventActions.stateDelta());
this.eventId = eventId;
}

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

/** Returns the ID of the event associated with this context. */
public String eventId() {
return eventId;
}

/**
* Lists the filenames of the artifacts attached to the current session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ private Flowable<LlmResponse> callLlm(
private Single<Optional<LlmResponse>> handleBeforeModelCallback(
InvocationContext context, LlmRequest.Builder llmRequestBuilder, Event modelResponseEvent) {
Event callbackEvent = modelResponseEvent.toBuilder().build();
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
CallbackContext callbackContext =
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());

Maybe<LlmResponse> pluginResult =
context.pluginManager().beforeModelCallback(callbackContext, llmRequestBuilder);
Expand Down Expand Up @@ -274,7 +275,8 @@ private Maybe<LlmResponse> handleOnModelErrorCallback(
Event modelResponseEvent,
Throwable throwable) {
Event callbackEvent = modelResponseEvent.toBuilder().build();
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
CallbackContext callbackContext =
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());
Exception ex = throwable instanceof Exception e ? e : new Exception(throwable);

Maybe<LlmResponse> pluginResult =
Expand Down Expand Up @@ -308,7 +310,8 @@ private Maybe<LlmResponse> handleOnModelErrorCallback(
private Single<LlmResponse> handleAfterModelCallback(
InvocationContext context, LlmResponse llmResponse, Event modelResponseEvent) {
Event callbackEvent = modelResponseEvent.toBuilder().build();
CallbackContext callbackContext = new CallbackContext(context, callbackEvent.actions());
CallbackContext callbackContext =
new CallbackContext(context, callbackEvent.actions(), callbackEvent.id());

Maybe<LlmResponse> pluginResult =
context.pluginManager().afterModelCallback(callbackContext, llmResponse);
Expand Down
18 changes: 14 additions & 4 deletions core/src/main/java/com/google/adk/tools/ToolContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ private ToolContext(
InvocationContext invocationContext,
EventActions eventActions,
Optional<String> functionCallId,
Optional<ToolConfirmation> toolConfirmation) {
super(invocationContext, eventActions);
Optional<ToolConfirmation> toolConfirmation,
@Nullable String eventId) {
super(invocationContext, eventActions, eventId);
this.functionCallId = functionCallId;
this.toolConfirmation = toolConfirmation;
}
Expand Down Expand Up @@ -125,7 +126,8 @@ public Builder toBuilder() {
return new Builder(invocationContext)
.actions(eventActions)
.functionCallId(functionCallId.orElse(null))
.toolConfirmation(toolConfirmation.orElse(null));
.toolConfirmation(toolConfirmation.orElse(null))
.eventId(eventId());
}

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

private Builder(InvocationContext invocationContext) {
this.invocationContext = invocationContext;
Expand All @@ -171,8 +174,15 @@ public Builder toolConfirmation(ToolConfirmation toolConfirmation) {
return this;
}

@CanIgnoreReturnValue
public Builder eventId(String eventId) {
this.eventId = eventId;
return this;
}

public ToolContext build() {
return new ToolContext(invocationContext, eventActions, functionCallId, toolConfirmation);
return new ToolContext(
invocationContext, eventActions, functionCallId, toolConfirmation, eventId);
}
}
}