Class Agent<S>
- Type Parameters:
S- the type of custom session state
- All Implemented Interfaces:
AgentApi<S>,Action<AgentInput,,AgentOutput<S>, AgentStreamChunk> BidiAction<AgentInput,,AgentOutput<S>, AgentStreamChunk, AgentInit<S>> Registerable
Instances are created via AgentActions.defineCustomAgent. Do not instantiate directly.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.google.genkit.core.BidiAction
BidiAction.BidiHandler<I,O, S, Init> -
Constructor Summary
ConstructorsConstructorDescriptionAgent(BidiActionImpl<AgentInput, AgentOutput<S>, AgentStreamChunk, AgentInit<S>> impl, SessionStore<S> store, boolean serverManaged, Action<?, ?, ?> snapshotAction, Action<?, ?, ?> abortAction, ClientTransform<S> clientTransform, SessionStoreOptions opts, String name, String description, Registry registry) Constructs an Agent. -
Method Summary
Modifier and TypeMethodDescriptionAttempts to abort a snapshot that is currently inSnapshotStatus.PENDINGstate.Action<?, ?, ?> Returns the companion abort action, ornullif not applicable.chat()Creates a freshAgentChatfor a new conversation, using a default action context built from the registry the agent was defined in.Creates anAgentChatseeded frominit(snapshotId / sessionId / inline state), using a default action context built from the registry the agent was defined in.chat(ActionContext ctx) Creates a freshAgentChatfor a new conversation.chat(ActionContext ctx, AgentInit<S> init) Creates anAgentChatseeded frominit(snapshotId / sessionId / inline state).getDesc()Returns the descriptor of the action containing metadata, schemas, etc.Returns the JSON schema for the action's input type.Returns additional metadata for the action.getName()Returns the name of the action.Returns the JSON schema for the action's output type.Retrieves a session snapshot by snapshot ID or session ID.Action<?, ?, ?> Returns the companion snapshot action, ornullif not applicable.getType()Returns the type of the action.loadChat(GetSnapshotRequest lookup) Loads a snapshot and hydrates anAgentChatwhose next send resumes it, using a default action context built from the registry the agent was defined in.loadChat(ActionContext ctx, GetSnapshotRequest lookup) Loads a snapshot and hydrates anAgentChatwhose next send resumes it.ref()Returns a lightweight reference to this agent.voidRegisters this primitive with the given registry.run(ActionContext ctx, AgentInput input) Runs the action with the given input.run(ActionContext ctx, AgentInput input, Consumer<AgentStreamChunk> cb) Runs the action with the given input and streaming callback.runBidi(ActionContext ctx, AgentInit<S> init, InputSource<AgentInput> inputs, Consumer<AgentStreamChunk> streamCallback) Runs the action in bidirectional-streaming mode.com.fasterxml.jackson.databind.JsonNoderunBidiJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action in bidirectional-streaming mode with JSON-typed arguments.ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runBidiJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action in bidirectional-streaming mode with JSON-typed arguments and telemetry.com.fasterxml.jackson.databind.JsonNoderunJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action with JSON input and returns JSON output.ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action with JSON input and returns the result with telemetry information.booleanReturns whether this agent uses server-managed session state.store()Returns the session store, ornullfor client-managed agents.
-
Constructor Details
-
Agent
public Agent(BidiActionImpl<AgentInput, AgentOutput<S>, AgentStreamChunk, AgentInit<S>> impl, SessionStore<S> store, boolean serverManaged, Action<?, ?, ?> snapshotAction, Action<?, ?, ?> abortAction, ClientTransform<S> clientTransform, SessionStoreOptions opts, String name, String description, Registry registry) Constructs an Agent. Called exclusively byAgentActions.- Parameters:
impl- the underlying BidiActionImplstore- the session store, ornullfor client-managed modeserverManaged- whether the agent is server-managedsnapshotAction- companion snapshot action; nullableabortAction- companion abort action; nullableclientTransform- state transform for client-managed mode; nullableopts- store options; nullablename- the agent namedescription- the agent description; nullableregistry- the registry the agent is defined in; used to build a defaultActionContextfor the no-argchat()convenience overloads
-
-
Method Details
-
getName
Description copied from interface:ActionReturns the name of the action.- Specified by:
getNamein interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the action name
-
getType
Description copied from interface:ActionReturns the type of the action.- Specified by:
getTypein interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the action type
-
getDesc
Description copied from interface:ActionReturns the descriptor of the action containing metadata, schemas, etc.- Specified by:
getDescin interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the action descriptor
-
getInputSchema
Description copied from interface:ActionReturns the JSON schema for the action's input type.- Specified by:
getInputSchemain interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the input schema as a map, or null if not defined
-
getOutputSchema
Description copied from interface:ActionReturns the JSON schema for the action's output type.- Specified by:
getOutputSchemain interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the output schema as a map, or null if not defined
-
getMetadata
Description copied from interface:ActionReturns additional metadata for the action.- Specified by:
getMetadatain interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Returns:
- the metadata map
-
run
Description copied from interface:ActionRuns the action with the given input.- Specified by:
runin interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Parameters:
ctx- the action contextinput- the input to the action- Returns:
- the output of the action
- Throws:
GenkitException- if the action fails
-
run
public AgentOutput<S> run(ActionContext ctx, AgentInput input, Consumer<AgentStreamChunk> cb) throws GenkitException Description copied from interface:ActionRuns the action with the given input and streaming callback.- Specified by:
runin interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Parameters:
ctx- the action contextinput- the input to the actioncb- callback for receiving streaming chunks, may be null- Returns:
- the output of the action
- Throws:
GenkitException- if the action fails
-
runJson
public com.fasterxml.jackson.databind.JsonNode runJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:ActionRuns the action with JSON input and returns JSON output.- Specified by:
runJsonin interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Parameters:
ctx- the action contextinput- the JSON inputstreamCallback- callback for receiving streaming JSON chunks, may be null- Returns:
- the JSON output
- Throws:
GenkitException- if the action fails
-
runJsonWithTelemetry
public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:ActionRuns the action with JSON input and returns the result with telemetry information.- Specified by:
runJsonWithTelemetryin interfaceAction<AgentInput,AgentOutput<S>, AgentStreamChunk> - Parameters:
ctx- the action contextinput- the JSON inputstreamCallback- callback for receiving streaming JSON chunks, may be null- Returns:
- the action result including telemetry data
- Throws:
GenkitException- if the action fails
-
runBidi
public AgentOutput<S> runBidi(ActionContext ctx, AgentInit<S> init, InputSource<AgentInput> inputs, Consumer<AgentStreamChunk> streamCallback) throws GenkitException Description copied from interface:BidiActionRuns the action in bidirectional-streaming mode.- Specified by:
runBidiin interfaceBidiAction<AgentInput,AgentOutput<S>, AgentStreamChunk, AgentInit<S>> - Parameters:
ctx- the action contextinit- the one-time session-init value; may benullinputs- the stream of per-turn inputsstreamCallback- callback invoked for each emitted chunk; may benull- Returns:
- the final output
- Throws:
GenkitException- if execution fails
-
runBidiJson
public com.fasterxml.jackson.databind.JsonNode runBidiJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:BidiActionRuns the action in bidirectional-streaming mode with JSON-typed arguments.Deserializes
initand each element frominputsbefore handing them to the typed handler, and serializes all chunks and the final result back toJsonNode.- Specified by:
runBidiJsonin interfaceBidiAction<AgentInput,AgentOutput<S>, AgentStreamChunk, AgentInit<S>> - Parameters:
ctx- the action contextinit- the one-time session-init as aJsonNode; may benullinputs- the stream of per-turn inputs asJsonNodevaluesstreamCallback- callback invoked for each emitted chunk serialized toJsonNode; may benull- Returns:
- the final output serialized to
JsonNode - Throws:
GenkitException- if execution fails
-
runBidiJsonWithTelemetry
public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runBidiJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode init, InputSource<com.fasterxml.jackson.databind.JsonNode> inputs, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:BidiActionRuns the action in bidirectional-streaming mode with JSON-typed arguments and telemetry.Behaves like
BidiAction.runBidiJson(com.google.genkit.core.ActionContext, com.fasterxml.jackson.databind.JsonNode, com.google.genkit.core.InputSource<com.fasterxml.jackson.databind.JsonNode>, java.util.function.Consumer<com.fasterxml.jackson.databind.JsonNode>)(deserializinginitand each element frominputs, serializing all chunks and the final result back toJsonNode) but additionally creates a new tracing span and captures the resulting trace/span IDs, returning them in anActionRunResult. Unlike the unaryAction.runJsonWithTelemetry(com.google.genkit.core.ActionContext, com.fasterxml.jackson.databind.JsonNode, java.util.function.Consumer<com.fasterxml.jackson.databind.JsonNode>), this threads the realinitand the full stream ofinputsthrough to the handler.- Specified by:
runBidiJsonWithTelemetryin interfaceBidiAction<AgentInput,AgentOutput<S>, AgentStreamChunk, AgentInit<S>> - Parameters:
ctx- the action contextinit- the one-time session-init as aJsonNode; may benullinputs- the stream of per-turn inputs asJsonNodevaluesstreamCallback- callback invoked for each emitted chunk serialized toJsonNode; may benull- Returns:
- the final output serialized to
JsonNodetogether with the captured trace/span IDs - Throws:
GenkitException- if execution fails
-
register
Description copied from interface:RegisterableRegisters this primitive with the given registry.- Specified by:
registerin interfaceRegisterable- Parameters:
registry- the registry to register with
-
getSnapshotData
Description copied from interface:AgentApiRetrieves a session snapshot by snapshot ID or session ID.- Specified by:
getSnapshotDatain interfaceAgentApi<S>- Parameters:
req- the request specifying which snapshot to retrieve- Returns:
- the matching session snapshot, or
nullif not found
-
abort
Description copied from interface:AgentApiAttempts to abort a snapshot that is currently inSnapshotStatus.PENDINGstate.If the snapshot is already in a terminal state, returns that state unchanged. If the snapshot does not exist, returns
null.- Specified by:
abortin interfaceAgentApi<S>- Parameters:
snapshotId- the ID of the snapshot to abort- Returns:
- the resulting
SnapshotStatus, ornullif the snapshot was not found
-
ref
Description copied from interface:AgentApiReturns a lightweight reference to this agent. -
chat
Description copied from interface:AgentApiCreates a freshAgentChatfor a new conversation, using a default action context built from the registry the agent was defined in.This is the convenience overload for the common case. Use
AgentApi.chat(ActionContext)when you need to supply request-scoped context (e.g. auth / user info that tools should observe). -
chat
Description copied from interface:AgentApiCreates anAgentChatseeded frominit(snapshotId / sessionId / inline state), using a default action context built from the registry the agent was defined in. -
loadChat
Description copied from interface:AgentApiLoads a snapshot and hydrates anAgentChatwhose next send resumes it, using a default action context built from the registry the agent was defined in. -
chat
Description copied from interface:AgentApiCreates a freshAgentChatfor a new conversation. -
chat
Description copied from interface:AgentApiCreates anAgentChatseeded frominit(snapshotId / sessionId / inline state). -
loadChat
Description copied from interface:AgentApiLoads a snapshot and hydrates anAgentChatwhose next send resumes it. -
serverManaged
public boolean serverManaged()Returns whether this agent uses server-managed session state.- Returns:
- true if server-managed, false if client-managed
-
store
Returns the session store, ornullfor client-managed agents.- Returns:
- the session store, or null
-
getSnapshotDataAction
Returns the companion snapshot action, ornullif not applicable.- Returns:
- the snapshot action, or null
-
abortAgentAction
Returns the companion abort action, ornullif not applicable.- Returns:
- the abort action, or null
-