`Class Context`

Direct Known Subclasses:
CallbackContext

public class Context extends ReadonlyContext
在调用过程中,相关上下文信息会被传递给回调函数、工具以及工作流节点。

代理(Agent)和模型(Model)的回调函数会接收到某些数据或信息。 CallbackContext以及工具和工具回调函数(a) ToolContext;这两个类都继承自同一个基类,因此可以创建一个能够同时为这两个类提供帮助的辅助工具(helper),或者是一个能够为这两个类提供功能支持的函数(function tool)。 toolContext 该参数可以接受一个值(即参数可以有一个输入)。 Context. requestConfirmation() 需要一个函数调用 ID(Function Call ID),而这个 ID 只存在于工具调用(Tool Call)中。 requireNodeState() 其他工作流节点的方法需要工作流节点的上下文信息(即该节点所处的工作流环境及其相关数据)。

  • 字段摘要

    Fields
    Modifier and Type
    Field
    Description
    protected EventActions
    用于记录该上下文变化的事件操作(event actions)。

    Fields inherited from class ReadonlyContext

    invocationContext
    Modifier and Type
    Field
    Description
    protected final InvocationContext
     
  • 构造函数概述

    Constructors
    Constructor
    Description
    Context(InvocationContext invocationContext, NodeExecutionState nodeState)
    创建工作流节点运行的上下文环境;该节点的状态为(此处应填写具体的状态信息)。 nodeState.
  • 方法概述

    Modifier and Type
    Method
    Description
    返回相同的结果。 EventActions as eventActions().
    final int
    返回该工作流节点当前所处的尝试次数(从 1 开始计数);每次重试都会使尝试次数增加 1。
    final void
    emit(Event event)
    发送 event 从这个工作流节点的…… FunctionNode 在之前的调用之后、以及输出结果之前,该内容会被显示在 `body` 部分中。
    final void
    emit(com.google.genai.types.Content message)
    发送 message 作为一个没有输出结果的事件…… emit(Event) does.
    返回 EventActions 与这个上下文相关联。
    final String
    返回此工作流节点所触发事件的作者信息;如果未找到相关作者信息,则返回空字符串(此时可以使用该节点的名称作为默认显示内容)。
    final void
    eventAuthor(String eventAuthor)
    用于设置此工作流节点所生成的事件的作者信息。
    返回与此上下文相关联的事件的 ID;如果不存在此类事件,则返回 null。
    如果存在的话,该函数会返回调用当前工具的函数调用的 ID。
    void
    functionCallId(@Nullable String functionCallId)
    设置调用当前工具的函数调用的 ID;如果该 ID 为 `null`,则将其清除。
    io.reactivex.rxjava3.core.Single<List<String>>
    列出当前会话中附加的所有工件的文件名。
    io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part>
    loadArtifact(String filename)
    从服务中加载该“工件”(artifact)的最新版本。
    io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part>
    loadArtifact(String filename, int version)
    从服务中加载某个特定版本的工件(artifact)。
    返回此工作流节点的输出结果;该输出结果实际上是由该节点内部执行的操作所产生的最终结果。 useAsOutput 如果该子对象包含某个值,则返回该值;如果该子对象不包含任何值,则返回空字符串(即 “”)。
    void
    请求确认当前的函数调用是否成功(即是否已经执行完毕)。
    void
    requestConfirmation(@Nullable String hint)
    请求确认当前的函数调用是否成功(即是否已经执行完毕)。
    void
    requestConfirmation(@Nullable String hint, @Nullable Object payload)
    请求确认当前的函数调用是否成功(即是否已经执行完毕)。
    返回该工作流节点所处的状态;该节点属于当前上下文(context)所管理的流程。
    final Map<String, @Nullable Object>
    返回用户对该工作流节点或其子节点在之前阶段发出的输入请求以及长时间运行的调用的响应结果;这些响应结果是根据中断 ID 进行分类的。
    final Optional<com.google.common.collect.ImmutableList<Route>>
    返回此工作流节点所选择的路由;如果没有做出任何路由决策,则返回空值。
    final void
    routes(List<? extends Route> routes)
    选择该工作流节点的路由(即决定数据应通过哪些路径进行传输),从而取代之前的所有路由设置。
    final io.reactivex.rxjava3.core.Maybe<Object>
    runNode(Node node)
    运行 node 作为这个工作流节点的“子节点”(即没有输入参数的节点),其行为如下: runNode(Node, Object, RunNodeConfig) 这与默认设置有关。 RunNodeConfig.
    final io.reactivex.rxjava3.core.Maybe<Object>
    runNode(Node node, @Nullable Object nodeInput)
    运行 node 作为该工作流节点的“子节点”(即该节点所依赖的另一个节点), runNode(Node, Object, RunNodeConfig) 这与默认设置有关。 RunNodeConfig.
    final io.reactivex.rxjava3.core.Maybe<Object>
    runNode(Node node, @Nullable Object nodeInput, RunNodeConfig config)
    返回一个 Maybe 也就是说,一旦用户订阅了该服务,相关程序就会自动运行。 node 作为该工作流程的一部分(即作为该流程中的一个节点),该节点会输出该节点自身的处理结果;如果该节点没有任何需要处理的数据或任务,那么它将输出空内容(即没有任何输出)。
    io.reactivex.rxjava3.core.Completable
    saveArtifact(String filename, com.google.genai.types.Part artifact)
    保存一个“工件”(artifact),并将其记录为当前会话的变更数据(即“差异信息”或“delta”)。
    io.reactivex.rxjava3.core.Single<SearchMemoryResponse>
    搜索当前用户的内存数据。
    返回当前上下文的“delta-aware”(即能够感知变化)状态。
    如果当前工具调用成功(即产生了预期的结果),则返回相应的确认信息。
    void
    toolConfirmation(@Nullable ToolConfirmation toolConfirmation)
    用于设置当前工具调用的确认状态;如果该值为 `null`,则会清除确认状态。

    Methods inherited from class ReadonlyContext

    agentName, branch, events, invocationContext, invocationId, sessionId, userContent, userId
    Modifier and Type
    Method
    Description
    返回当前正在运行的代理的名称。
    如果存在的话,返回当前调用对应的代码分支(即分支名称)。
    返回一个不可修改的视图,用于显示会话中的所有事件。
    返回调用上下文( invocation context)。
    返回当前调用的 ID。
    返回会话 ID。
    Optional<com.google.genai.types.Content>
    返回触发此次调用的用户内容。
    返回用户 ID。

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详情

    • eventActions

      protected EventActions eventActions
      用于记录该上下文变化的事件操作(event actions)。
  • 构造函数详细信息

    • Context

      @Experimental public Context(InvocationContext invocationContext, NodeExecutionState nodeState)
      创建工作流节点运行的上下文环境;该节点的状态为(此处应填写具体的状态信息)。 nodeState只有工作流运行时(workflow runtime)才会创建节点状态(node state)。
  • 方法详细信息(Method Details)

    • state

      public State state()
      返回当前上下文的“delta-aware”(即能够感知变化)状态。
      Overrides:
      state in class ReadonlyContext
    • eventActions

      public EventActions eventActions()
      返回 EventActions 与这个上下文相关联。
    • actions

      public EventActions actions()
      返回相同的结果。 EventActions as eventActions().
    • eventId

      public String eventId()
      返回与此上下文相关联的事件的 ID;如果不存在此类事件,则返回 null。
    • functionCallId

      public Optional<String> functionCallId()
      如果存在的话,该函数会返回调用当前工具的函数调用的 ID。
    • functionCallId

      public void functionCallId(@Nullable String functionCallId)
      设置调用当前工具的函数调用的 ID;如果该 ID 为 `null`,则将其清除。
    • toolConfirmation

      public Optional<ToolConfirmation> toolConfirmation()
      如果当前工具调用成功(即产生了预期的结果),则返回相应的确认信息。
    • toolConfirmation

      public void toolConfirmation(@Nullable ToolConfirmation toolConfirmation)
      用于设置当前工具调用的确认状态;如果该值为 `null`,则会清除确认状态。
    • listArtifacts

      public io.reactivex.rxjava3.core.Single<List<String>> listArtifacts()
      列出当前会话中附加的所有工件的文件名。
      Returns:
      the list of artifact filenames
    • loadArtifact

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part> loadArtifact(String filename)
      从服务中加载该“工件”(artifact)的最新版本。
    • loadArtifact

      public io.reactivex.rxjava3.core.Maybe<com.google.genai.types.Part> loadArtifact(String filename, int version)
      从服务中加载某个特定版本的工件(artifact)。
    • saveArtifact

      public io.reactivex.rxjava3.core.Completable saveArtifact(String filename, com.google.genai.types.Part artifact)
      保存一个“工件”(artifact),并将其记录为当前会话的变更数据(即“差异信息”或“delta”)。
      Parameters:
      filename - Artifact file name.
      artifact - Artifact content to save.
      Returns:
      a Completable that completes when the artifact is saved.
      Throws:
      IllegalStateException - if the artifact service is not initialized.
    • requestConfirmation

      public void requestConfirmation(@Nullable String hint, @Nullable Object payload)
      请求确认当前的函数调用是否成功(即是否已经执行完毕)。
      Parameters:
      hint - A hint to the user on how to confirm the tool call.
      payload - The payload used to confirm the tool call.
      Throws:
      IllegalStateException - if this context has no function call ID.
    • requestConfirmation

      public void requestConfirmation(@Nullable String hint)
      请求确认当前的函数调用是否成功(即是否已经执行完毕)。
      Parameters:
      hint - A hint to the user on how to confirm the tool call.
      Throws:
      IllegalStateException - if this context has no function call ID.
    • requestConfirmation

      public void requestConfirmation()
      请求确认当前的函数调用是否成功(即是否已经执行完毕)。
      Throws:
      IllegalStateException - if this context has no function call ID.
    • searchMemory

      public io.reactivex.rxjava3.core.Single<SearchMemoryResponse> searchMemory(String query)
      搜索当前用户的内存数据。
    • requireNodeState

      @Experimental public final NodeExecutionState requireNodeState()
      返回该工作流节点所处的状态;该节点属于当前上下文(context)所管理的流程。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • eventAuthor

      @Experimental public final String eventAuthor()
      返回此工作流节点所触发事件的作者信息;如果未找到相关作者信息,则返回空字符串(此时可以使用该节点的名称作为默认显示内容)。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • eventAuthor

      @Experimental public final void eventAuthor(String eventAuthor)
      用于设置此工作流节点所生成的事件的作者信息。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • output

      @Experimental public final Optional<Object> output()
      返回此工作流节点的输出结果;该输出结果实际上是由该节点内部执行的操作所产生的最终结果。 useAsOutput 如果该子对象包含某个值,则返回该值;如果该子对象不包含任何值,则返回空字符串(即 “”)。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • routes

      @Experimental public final Optional<com.google.common.collect.ImmutableList<Route>> routes()
      返回此工作流节点所选择的路由;如果没有做出任何路由选择,则返回空列表。空列表表示“未选择任何路由”的状态。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • routes

      @Experimental public final void routes(List<? extends Route> routes)
      选择该工作流节点的路由(即决定数据应通过哪些路径进行传输),从而取代之前的所有路由设置。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • resumeInputs

      @Experimental public final Map<String, @Nullable Object> resumeInputs()
      该函数返回用户对该工作流节点或其子节点在之前阶段发出的输入请求以及长时间运行的调用的响应结果,这些响应结果是根据中断 ID 进行分类的。当工作流节点重新启动(即进入 “resume” 状态)时,会读取这些响应数据。如果某个请求没有收到任何响应,那么在映射(map)中对应的值将为 `null`。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • attemptCount

      @Experimental public final int attemptCount()
      返回该工作流节点当前所处的尝试次数(从 1 开始计数);每次重试都会使尝试次数增加 1。
      Throws:
      IllegalStateException - if this is not the context of a workflow node.
    • emit

      @Experimental public final void emit(Event event)
      发送 event 从这个工作流节点的…… FunctionNode `body`:在之前的调用事件之后、输出结果之前执行。当程序运行完成并已将相关事件数据存储起来并传递给调用者时,`body` 会被执行一次;`body` 中的任何线程都可以调用该函数。 Runner 那个负责执行任务的节点需要被设置为 `inline` 类型。 FunctionNode.Builder#scheduler 那些不符合要求的选项(例如……)。 Schedulers.io(). The FunctionNode 文档中规定了状态(state)、线程(threads)、事件(events)、取消操作(cancellation)以及恢复操作(resumes)的相关规则。
      Throws:
      IllegalArgumentException - if event has an output, or has the ID of an event another emit call still waits on
      IllegalStateException - if no FunctionNode body of this node is running
      CancellationException - if the node is canceled, or the calling thread is interrupted while this call waits; the thread stays interrupted
    • emit

      @Experimental public final void emit(com.google.genai.types.Content message)
      发送 message 作为一个没有输出结果的事件…… emit(Event) does.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node)
      运行 node 作为这个工作流节点的“子节点”(即没有输入参数的节点),其行为如下: runNode(Node, Object, RunNodeConfig) 这与默认设置有关。 RunNodeConfig.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput)
      运行 node 作为该工作流节点的“子节点”(即该节点所依赖的另一个节点), runNode(Node, Object, RunNodeConfig) 这与默认设置有关。 RunNodeConfig.
      Throws:
      IllegalArgumentException - if nodeInput is a RunNodeConfig, which belongs in the three-argument overload.
    • runNode

      @Experimental public final io.reactivex.rxjava3.core.Maybe<Object> runNode(Node node, @Nullable Object nodeInput, RunNodeConfig config)
      返回一个 Maybe 也就是说,一旦用户订阅了该服务,相关程序就会自动运行。 node 作为该工作流程中的一个子节点,该节点会输出子节点的处理结果;如果子节点没有输出结果,则会返回空值。请将该节点包含在主节点的 `run` 配置中。当子节点正在等待处理结果时,主节点也会继续执行其自身的任务。 Maybe 由于内部错误而失败。 Error 这样一来,这个节点也会等待(即它的执行会被延迟)。 catch (Exception e) 虽然可以“通过”(即允许某些东西通过),但实际上…… catch (Throwable t) 以及 Rx 错误处理程序(如……) onErrorResumeNext stop.

      这个操作会失败(即无法完成预期的任务)。 DynamicNodeFailedException 如果孩子失败了…… IllegalStateException 如果这不是工作流节点的上下文(即如果当前上下文不属于该工作流节点),那么…… Node.rerunOnResume() 如果该值为 `false`,或者…… config 该节点会将其已经拥有的输出结果,或者已经委托给其他节点的输出结果,再次委托出去;同时…… IllegalArgumentException 如果孩子的名字无法构成一个节点路径(即无法被用于表示文件或目录的路径),则……