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
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,35 @@ class NewFileSystemSourceTest {
assertThat(result.getOrThrow().name).isEqualTo("skill1")
}

@Test
fun loadFrontmatter_adkAdditionalToolsMetadata_returnsFrontmatterWithMetadata() = runTest {
writeRawSkillMd(
dirName = "test-skill",
content =
"""
---
name: test-skill
description: Description
metadata:
adk_additional_tools:
- extra_tool_a
- extra_tool_b
owner: adk
---
Instructions.
"""
.trimIndent(),
)

val result = source.loadFrontmatter("test-skill")

assertThat(result.isSuccess).isTrue()
val frontmatter = result.getOrThrow()
assertThat(frontmatter.metadata["adk_additional_tools"])
.isEqualTo(listOf("extra_tool_a", "extra_tool_b"))
assertThat(frontmatter.metadata["owner"]).isEqualTo("adk")
}

@Test
fun loadFrontmatter_nonexistentSkill_returnsFailure() = runTest {
val result = source.loadFrontmatter("nonexistent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.google.adk.kt.callbacks.runOnModelErrorCallbacksPipeline
import com.google.adk.kt.events.Event
import com.google.adk.kt.events.getLongRunningFunctionIds
import com.google.adk.kt.ids.Uuid
import com.google.adk.kt.logging.LoggerFactory
import com.google.adk.kt.models.LlmRequest
import com.google.adk.kt.models.LlmResponse
import com.google.adk.kt.models.toTracePayload
Expand Down Expand Up @@ -53,6 +54,12 @@ import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

private fun Iterable<BaseTool>.associateByNameFirstWins(): Map<String, BaseTool> = buildMap {
for (tool in this@associateByNameFirstWins) {
if (tool.name !in this) put(tool.name, tool)
}
}

/**
* Encapsulates the logic for a single turn of an [LlmAgent].
*
Expand All @@ -70,6 +77,7 @@ internal class LlmAgentTurn(
private val requestProcessors: List<LlmRequestProcessor>,
private val responseProcessors: List<LlmResponseProcessor>,
) {
private val logger = LoggerFactory.getLogger(LlmAgentTurn::class)

/**
* Executes the turn logic and returns a flow of events.
Expand Down Expand Up @@ -151,25 +159,51 @@ internal class LlmAgentTurn(
}

val toolContext = ToolContext(invocationContext = context)
val registeredToolsByName =
processedRequest.toolsDict.associateByNameFirstWins().toMutableMap()

suspend fun registerTool(req: LlmRequest, tool: BaseTool): LlmRequest {
val existing = registeredToolsByName[tool.name]
if (existing != null) {
if (existing !== tool) {
logger.warn {
"Duplicate tool name '${tool.name}'; keeping the first registered tool and skipping " +
"the later one."
}
}
return req
}
registeredToolsByName[tool.name] = tool
return tool.processLlmRequest(toolContext, req)
}

val reqAfterCodeExecutor =
canonicalDirectTools.fold(processedRequest) { req, tool ->
tool.processLlmRequest(toolContext, req)
registerTool(req, tool)
}

val finalRequest =
agent.toolsets.fold(reqAfterCodeExecutor) { req, toolset ->
val setReq = toolset.processLlmRequest(toolContext, req)
for (tool in setReq.toolsDict) {
if (tool.name !in registeredToolsByName) {
registeredToolsByName[tool.name] = tool
}
}
toolset.getTools(context.toReadonlyContext()).fold(setReq) { r, tool ->
tool.processLlmRequest(toolContext, r)
registerTool(r, tool)
}
}
return RequestOrResponse.Request(finalRequest)
}

private suspend fun getToolMap(request: LlmRequest?): Map<String, BaseTool> {
val readonlyCtx = context.toReadonlyContext()
val allTools = canonicalDirectTools + agent.toolsets.flatMap { it.getTools(readonlyCtx) }
return (allTools + request?.toolsDict.orEmpty()).associateBy { it.name }
val toolsInRegistrationOrder =
request?.toolsDict.orEmpty() +
canonicalDirectTools +
agent.toolsets.flatMap { it.getTools(readonlyCtx) }
return toolsInRegistrationOrder.associateByNameFirstWins()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.adk.kt.annotations

/**
* Marks APIs for exposing skill-provided additional tools as experimental.
*
* These APIs may change or be removed as skill behavior is aligned across ADK implementations.
*/
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@RequiresOptIn(
level = RequiresOptIn.Level.ERROR,
message = "Skill-provided additional tools are experimental and may change or be removed.",
)
annotation class ExperimentalSkillTools
Loading