调用(即程序中执行某段代码的行为)上下文(Context)

data class InvocationContext(val session: Session, val runConfig: RunConfig? = null, val agent: BaseAgent, val branch: String? = null, val invocationId: String = "e-" + Uuid.random(), val artifactService: ArtifactService? = null, val memoryService: MemoryService? = null, val sessionService: SessionService? = null, val resumabilityConfig: ResumabilityConfig? = null, val eventsCompactionConfig: EventsCompactionConfig? = null, val contextCacheConfig: ContextCacheConfig? = null, val userContent: Content? = null, val agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), val endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), val extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), val frameworkData: ContextFrameworkData = ContextFrameworkData(), var isEndOfInvocation: Boolean = false, val pluginManager: PluginManager = PluginManager(), invocationCostManager: InvocationCostManager = InvocationCostManager(), val node: Node? = null)

An invocation context represents the data of a single invocation of an agent.

An invocation:

  1. Starts with a user message and ends with a final response.

  2. Can contain one or multiple agent calls.

  3. Is handled by runner.run_async().

An invocation runs an agent until it does not request to transfer to another agent.

An agent call:

  1. Is handled by agent.run().

  2. Ends when agent.run() ends.

An LLM agent call is an agent with a BaseLLMFlow. An LLM agent call can contain one or multiple steps.

An LLM agent runs steps in a loop until:

  1. A final response is generated.

  2. The agent transfers to another agent.

  3. The end_invocation is set to true by any callbacks or tools.

A step:

  1. Calls the LLM only once and yields its response.

  2. Calls the tools and yields their responses if requested.

The summarization of the function response is considered another step, since it is another llm call. A step ends when it's done calling llm and tools, or if the end_invocation is set to true at any time.

    ┌─────────────────────── invocation ──────────────────────────┐
┌──────────── llm_agent_call_1 ────────────┐ ┌─ agent_call_2 ─┐
┌──── step_1 ────────┐ ┌───── step_2 ──────┐
[call_llm] [call_tool] [call_llm] [transfer]

构造函数(Constructors)

Link copied to clipboard
constructor(session: Session, runConfig: RunConfig? = null, agent: BaseAgent, branch: String? = null, invocationId: String = "e-" + Uuid.random(), artifactService: ArtifactService? = null, memoryService: MemoryService? = null, sessionService: SessionService? = null, resumabilityConfig: ResumabilityConfig? = null, eventsCompactionConfig: EventsCompactionConfig? = null, contextCacheConfig: ContextCacheConfig? = null, userContent: Content? = null, agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), frameworkData: ContextFrameworkData = ContextFrameworkData(), isEndOfInvocation: Boolean = false, pluginManager: PluginManager = PluginManager(), invocationCostManager: InvocationCostManager = InvocationCostManager(), node: Node? = null)
constructor(session: Session, runConfig: RunConfig? = null, agent: BaseAgent, branch: String? = null, invocationId: String = "e-" + Uuid.random(), artifactService: ArtifactService? = null, memoryService: MemoryService? = null, sessionService: SessionService? = null, resumabilityConfig: ResumabilityConfig? = null, eventsCompactionConfig: EventsCompactionConfig? = null, contextCacheConfig: ContextCacheConfig? = null, userContent: Content? = null, agentStates: MutableMap<String, TypedData> = concurrentMutableMapOf(), endOfAgents: MutableMap<String, Boolean> = concurrentMutableMapOf(), extraTools: MutableMap<String, BaseTool> = concurrentMutableMapOf(), frameworkData: ContextFrameworkData = ContextFrameworkData(), isEndOfInvocation: Boolean = false, pluginManager: PluginManager = PluginManager(), invocationCostManager: InvocationCostManager = InvocationCostManager())

1.1.0 版本的构造函数(constructor)没有任何特殊功能(即没有实现任何特定的逻辑或方法)。 node该构造函数仍然可见,因为 `javac` 会跳过那些被标记为“隐藏”的构造函数;因此,如果使用 1.1.0 版本的 Java 编写代码,编译过程将会失败。

属性(Properties)

Link copied to clipboard

本次调用所使用的代理。对于以节点为根的调用来说,这个代理是一个针对正在运行的节点的 `NodeViewAgent`。 agent.name 还有另一个…… BaseAgent 成员的信息保持不变(即成员的名称和身份仍然正确)。

Link copied to clipboard

此次调用中代理的状态。

Link copied to clipboard
Link copied to clipboard

调用上下文( invocation context)的一个分支。

Link copied to clipboard

此调用的可选上下文缓存配置,该配置是从……传播而来的。 应用程序(App). 当……的时候 null此次调用中,上下文缓存(context caching)功能已被禁用。

Link copied to clipboard

本次调用中,每个代理的状态信息均已获取完毕。

Link copied to clipboard

此调用可配置事件压缩功能(可选)。

Link copied to clipboard

每个工具都具备以下功能: LlmAgent “在此上下文中运行”(run under this context)除了具有其自身的功能外,还提供了其他额外的优势或特性。 LlmAgent.tools为下属代理人复制的上下文环境会共享这张“地图”(即数据结构或信息模型),因此他们的工具也能访问这些数据。

Link copied to clipboard

每次调用时框架内部使用的数据(详见……) ContextFrameworkData该对象被作为一个专用的容器(holder)来使用,因此那些需要用户主动选择(opt-in)才能使用的成员(members)无法通过这个公共构造函数(public constructor)来创建对象。根据合同规定,这个对象本身并不属于公共 API(public API);不过,对这个对象的引用本身并不需要用户的主动选择(opt-in),但该对象的各个字段(fields)则需要用户的主动选择才能被使用。

Link copied to clipboard

此调用上下文的标识符。该属性为只读(read-only)。

Link copied to clipboard
Link copied to clipboard

返回当前调用是否可以恢复(即是否可以在中断后继续执行)。

Link copied to clipboard
Link copied to clipboard
val node: Node?

此次调用所执行的节点是在节点图中(而非代理树中)被确定的。`go-forward` 字段:一旦被设置后…… 代理;工作人员 如果该属性被删除,那么每次调用时都会使用默认的运行单元(即默认的实现)。该属性为只读(read-only)类型。

Link copied to clipboard

负责在此次调用中跟踪插件状态的管理员。

Link copied to clipboard

此调用支持可选的可恢复性(resumability)配置。

Link copied to clipboard

本次调用中为在线客服人员(live agents)配置的相关设置。

Link copied to clipboard

当前调用上下文的会话信息。该信息为只读(不可修改)。

Link copied to clipboard
Link copied to clipboard

触发此次调用的用户内容。该内容为只读(不可修改)。

函数

Link copied to clipboard

为子代理创建一个新的 `InvocationContext` 对象,该对象继承自当前上下文(`this context`)。同时,会将给定代理的名称添加到分支路径中。

Link copied to clipboard
suspend fun findMatchingFunctionCall(functionResponseEvent: Event): Event?

在当前的函数调用过程中,查找与该函数响应 ID 匹配的函数调用事件。

Link copied to clipboard
suspend fun getEvents(currentInvocation: Boolean = false, currentBranch: Boolean = false): List<Event>

从内存中返回当前会话的所有事件。 Session.events,其中 SessionService.appendEvent 这些数据会保持同步;程序会直接从内存中读取所需信息,而无需在每一步都重新从会话服务中获取数据。这一机制适用于 Python、Java 和 Go 语言编写的应用程序(ADK)。

Link copied to clipboard
suspend fun handleFunctionCalls(functionCalls: List<FunctionCall>, tools: Map<String, BaseTool>, filters: Set<String> = emptySet(), toolConfirmations: Map<String, ToolConfirmation>? = null): Event?

该系统能够高效且安全地执行一系列函数调用(即处理这些函数调用)。

Link copied to clipboard

统计这次对大型语言模型(LLM)的调用次数,并执行相应的规则/操作。 RunConfig.maxLlmCalls.

Link copied to clipboard

如果当前的调用可以恢复(即可以继续执行),则该函数会更新代理的状态信息。

Link copied to clipboard
fun resetSubAgentStates(agentName: String)

递归地重置指定代理的所有子代理的状态。

Link copied to clipboard
fun setAgentState(agentName: String, agentState: TypedData? = null, endOfAgent: Boolean = false)

显式设置代理的状态;不会自动进行初始化。

Link copied to clipboard

返回一个布尔值,表示是否应在该事件发生后立即暂停函数的执行。