工具

annotation class Tool(val name: String = "", val description: String = "", val requireConfirmation: Boolean = false, val isLongRunning: Boolean = false, val behavior: Behavior = Behavior.UNSPECIFIED)

Annotates a function to be exposed as an executable tool by the Google ADK Kotlin. KSP will generate a corresponding com.google.adk.kt.tools.FunctionTool wrapper for annotated functions.

The generated wrapper converts the function's return value into a JSON-native Map: data classes, enums, lists, maps, and nested structures are translated at compile time. A function may therefore return a data class directly, unlike a hand-written com.google.adk.kt.tools.BaseTool, whose result must already be JSON-native. It may also return a kotlinx.coroutines.flow.Flow or a Reactive Streams Publisher; a Flow or Publisher of Event or Any passes its values through unconverted. A connected live run streams each value to the model, except an Event, which goes to the caller; anywhere else, the call fails (see com.google.adk.kt.tools.BaseTool.run), so return a value instead.

A streaming tool may take one experimental Publisher<LiveRequest> for the run's input. The input is set only while the returned stream runs, so subscribe to it from inside that stream; outside it, the input is empty. A Publisher return or input needs kotlinx-coroutines-reactive on the classpath.

Note: this annotation shares its simple name with com.google.adk.kt.types.Tool, the GenAI tool definition data class. Files that need to reference both should use an import alias.

Retained at runtime so the JVM-only ReflectiveTools can read it via reflection to build tools from Java methods, which the KSP path cannot process without the Kotlin compiler.

属性(Properties)

Link copied to clipboard

在运行过程中,如何向模型声明所使用的工具: Behavior.NON_BLOCKING 让模型在通话未完成期间继续维持对话的进行。 Behavior.BLOCKING 该功能会使得程序等待响应结果。只有在进行实时运行(live run)时才会使用这个机制。任何返回 `Flow` 类型数据的函数都被声明为“非阻塞”(non-blocking)的;而那些不返回 `Flow` 类型数据的函数,则会在实时运行开始之前继续执行完毕(即会完成自身的所有操作),之后才会读取模型的下一条消息。

Link copied to clipboard

这是一个可选的、明确的描述性文本。如果未提供该描述性文本,系统会使用 KDoc 中提供的摘要信息。

Link copied to clipboard
val isLongRunning: Boolean = false

如果该值为 `true`,则表示该工具当前处于“待处理”(Pending)状态,稍后会继续执行其任务。

Link copied to clipboard

该工具的可选显式名称;如果未提供该名称,则会使用函数的名称。

Link copied to clipboard

如果该选项设置为“true”,则工具的执行需要用户的确认。