Skip to content
Open
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
18 changes: 18 additions & 0 deletions core/src/main/java/com/google/adk/agents/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ public Optional<List<? extends AfterAgentCallback>> afterAgentCallback() {
return afterAgentCallback;
}

/**
* The resolved beforeAgentCallback field as a list.
*
* <p>This method is only for use by Agent Development Kit.
*/
public List<? extends BeforeAgentCallback> canonicalBeforeAgentCallbacks() {
return beforeAgentCallback.orElse(ImmutableList.of());
}

/**
* The resolved afterAgentCallback field as a list.
*
* <p>This method is only for use by Agent Development Kit.
*/
public List<? extends AfterAgentCallback> canonicalAfterAgentCallbacks() {
return afterAgentCallback.orElse(ImmutableList.of());
}

/**
* Creates a shallow copy of the parent context with the agent properly being set to this
* instance.
Expand Down
14 changes: 6 additions & 8 deletions core/src/main/java/com/google/adk/agents/CallbackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ public final class CallbackUtil {
builder.add(beforeAgentCallbackInstance);
} else if (callback instanceof BeforeAgentCallbackSync beforeAgentCallbackSyncInstance) {
builder.add(
(BeforeAgentCallback)
(callbackContext) ->
Maybe.fromOptional(beforeAgentCallbackSyncInstance.call(callbackContext)));
(callbackContext) ->
Maybe.fromOptional(beforeAgentCallbackSyncInstance.call(callbackContext)));
} else {
logger.warn(
"Invalid beforeAgentCallback callback type: %s. Ignoring this callback.",
"Invalid beforeAgentCallback callback type: {}. Ignoring this callback.",
callback.getClass().getName());
}
}
Expand Down Expand Up @@ -87,12 +86,11 @@ public final class CallbackUtil {
builder.add(afterAgentCallbackInstance);
} else if (callback instanceof AfterAgentCallbackSync afterAgentCallbackSyncInstance) {
builder.add(
(AfterAgentCallback)
(callbackContext) ->
Maybe.fromOptional(afterAgentCallbackSyncInstance.call(callbackContext)));
(callbackContext) ->
Maybe.fromOptional(afterAgentCallbackSyncInstance.call(callbackContext)));
} else {
logger.warn(
"Invalid afterAgentCallback callback type: %s. Ignoring this callback.",
"Invalid afterAgentCallback callback type: {}. Ignoring this callback.",
callback.getClass().getName());
}
}
Expand Down
68 changes: 68 additions & 0 deletions core/src/main/java/com/google/adk/agents/Callbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,36 @@ public interface AfterModelCallbackSync extends AfterModelCallbackBase {
Optional<LlmResponse> call(CallbackContext callbackContext, LlmResponse llmResponse);
}

interface OnModelErrorCallbackBase {}

/** Async callback interface for handling errors that occur during an LLM model call. */
@FunctionalInterface
public interface OnModelErrorCallback extends OnModelErrorCallbackBase {
/**
* Async callback when model call fails.
*
* @param callbackContext Callback context.
* @param llmRequest LLM request.
* @param error The exception that occurred.
* @return response override, or empty to continue with error.
*/
Maybe<LlmResponse> call(
CallbackContext callbackContext, LlmRequest llmRequest, Exception error);
}

/**
* Helper interface to allow for sync onModelErrorCallback. The function is wrapped into an async
* one before being processed further.
*/
@FunctionalInterface
public interface OnModelErrorCallbackSync extends OnModelErrorCallbackBase {
Optional<LlmResponse> call(
CallbackContext callbackContext, LlmRequest llmRequest, Exception error);
}

interface BeforeAgentCallbackBase {}

/** Async callback interface for actions to be performed before an agent starts running. */
@FunctionalInterface
public interface BeforeAgentCallback extends BeforeAgentCallbackBase {
/**
Expand All @@ -99,6 +127,7 @@ public interface BeforeAgentCallbackSync extends BeforeAgentCallbackBase {

interface AfterAgentCallbackBase {}

/** Async callback interface for actions to be performed after an agent has finished running. */
@FunctionalInterface
public interface AfterAgentCallback extends AfterAgentCallbackBase {
/**
Expand All @@ -121,6 +150,7 @@ public interface AfterAgentCallbackSync extends AfterAgentCallbackBase {

interface BeforeToolCallbackBase {}

/** Async callback interface for actions to be performed before a tool is invoked. */
@FunctionalInterface
public interface BeforeToolCallback extends BeforeToolCallbackBase {
/**
Expand Down Expand Up @@ -154,6 +184,7 @@ Optional<Map<String, Object>> call(

interface AfterToolCallbackBase {}

/** Async callback interface for actions to be performed after a tool has been invoked. */
@FunctionalInterface
public interface AfterToolCallback extends AfterToolCallbackBase {
/**
Expand Down Expand Up @@ -188,5 +219,42 @@ Optional<Map<String, Object>> call(
Object response);
}

interface OnToolErrorCallbackBase {}

/** Async callback interface for handling errors that occur during a tool invocation. */
@FunctionalInterface
public interface OnToolErrorCallback extends OnToolErrorCallbackBase {
/**
* Async callback when tool call fails.
*
* @param invocationContext Invocation context.
* @param baseTool Tool instance.
* @param input Tool input arguments.
* @param toolContext Tool context.
* @param error The exception that occurred.
* @return override result, or empty to continue with error.
*/
Maybe<Map<String, Object>> call(
InvocationContext invocationContext,
BaseTool baseTool,
Map<String, Object> input,
ToolContext toolContext,
Exception error);
}

/**
* Helper interface to allow for sync onToolErrorCallback. The function is wrapped into an async
* one before being processed further.
*/
@FunctionalInterface
public interface OnToolErrorCallbackSync extends OnToolErrorCallbackBase {
Optional<Map<String, Object>> call(
InvocationContext invocationContext,
BaseTool baseTool,
Map<String, Object> input,
ToolContext toolContext,
Exception error);
}

private Callbacks() {}
}
Loading