|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.tokt |
| 18 | + |
| 19 | +import com.google.adk.artifacts.BaseArtifactService as JavaArtifactService |
| 20 | +import com.google.adk.kt.artifacts.ArtifactService as KtArtifactService |
| 21 | +import com.google.adk.kt.memory.MemoryService as KtMemoryService |
| 22 | +import com.google.adk.kt.models.Model as KtModel |
| 23 | +import com.google.adk.kt.plugins.Plugin as KtPlugin |
| 24 | +import com.google.adk.kt.sessions.SessionService as KtSessionService |
| 25 | +import com.google.adk.kt.tools.BaseTool as KtBaseTool |
| 26 | +import com.google.adk.kt.tools.Toolset as KtToolset |
| 27 | +import com.google.adk.memory.BaseMemoryService as JavaMemoryService |
| 28 | +import com.google.adk.models.BaseLlm as JavaBaseLlm |
| 29 | +import com.google.adk.plugins.Plugin as JavaPlugin |
| 30 | +import com.google.adk.sessions.BaseSessionService as JavaSessionService |
| 31 | +import com.google.adk.tokt.adapters.JavaModelToKt |
| 32 | +import com.google.adk.tokt.adapters.JavaPluginToKt |
| 33 | +import com.google.adk.tokt.adapters.JavaToolToKt |
| 34 | +import com.google.adk.tokt.adapters.JavaToolsetToKt |
| 35 | +import com.google.adk.tokt.services.javaArtifactServiceAsKt |
| 36 | +import com.google.adk.tokt.services.javaMemoryServiceAsKt |
| 37 | +import com.google.adk.tokt.services.javaSessionServiceAsKt |
| 38 | +import com.google.adk.tools.BaseTool as JavaBaseTool |
| 39 | +import com.google.adk.tools.BaseToolset as JavaBaseToolset |
| 40 | + |
| 41 | +/** |
| 42 | + * Forward interop entry point: adapts ADK Java tools, toolsets, plugins, services, and models so |
| 43 | + * they can run on the ADK Kotlin engine. Wrap the adapted pieces in a Kotlin `LlmAgent`; this does |
| 44 | + * not convert a whole Java agent. |
| 45 | + * |
| 46 | + * An adapted component behaves as it does on ADK Java. It sees the session as it currently stands, |
| 47 | + * including events and state written earlier in the same turn, and its state, artifact and |
| 48 | + * control-flow writes reach the engine. Blocking work is fine: calls are dispatched off the thread |
| 49 | + * driving the agent. |
| 50 | + * |
| 51 | + * Two things to know before relying on them: |
| 52 | + * - Setting `branch` on a bridged context throws. The branch is the engine's to set. |
| 53 | + * - Behind an adapted Java session service ([asKtSessionService]), a resumable workflow restarts |
| 54 | + * instead of resuming, because ADK Java has nowhere to store the engine's resumption state. Keep |
| 55 | + * the Kotlin session service if you rely on resumability. |
| 56 | + */ |
| 57 | +object JavaAdkToKt { |
| 58 | + |
| 59 | + /** Adapts an ADK Java tool. */ |
| 60 | + @JvmStatic fun asKtTool(javaTool: JavaBaseTool): KtBaseTool = JavaToolToKt(javaTool) |
| 61 | + |
| 62 | + /** |
| 63 | + * Adapts a whole collection of ADK Java tools (e.g. an `LlmAgent`'s `tools`). Kept alongside |
| 64 | + * [asKtTool] for Java callers, who would otherwise write `stream().map(...).toList()`. |
| 65 | + */ |
| 66 | + @JvmStatic |
| 67 | + fun asKtTools(javaTools: List<JavaBaseTool>): List<KtBaseTool> = javaTools.map { asKtTool(it) } |
| 68 | + |
| 69 | + /** Adapts an ADK Java toolset. */ |
| 70 | + @JvmStatic fun asKtToolset(javaToolset: JavaBaseToolset): KtToolset = JavaToolsetToKt(javaToolset) |
| 71 | + |
| 72 | + /** Adapts a whole collection of ADK Java toolsets. */ |
| 73 | + @JvmStatic |
| 74 | + fun asKtToolsets(javaToolsets: List<JavaBaseToolset>): List<KtToolset> = javaToolsets.map { |
| 75 | + asKtToolset(it) |
| 76 | + } |
| 77 | + |
| 78 | + /** Adapts an ADK Java plugin. */ |
| 79 | + @JvmStatic fun asKtPlugin(javaPlugin: JavaPlugin): KtPlugin = JavaPluginToKt(javaPlugin) |
| 80 | + |
| 81 | + /** Adapts a whole collection of ADK Java plugins (e.g. a `Runner`'s `plugins`). */ |
| 82 | + @JvmStatic |
| 83 | + fun asKtPlugins(javaPlugins: List<JavaPlugin>): List<KtPlugin> = javaPlugins.map { |
| 84 | + asKtPlugin(it) |
| 85 | + } |
| 86 | + |
| 87 | + /** Adapts an ADK Java model so the Kotlin engine can call it. */ |
| 88 | + @JvmStatic fun asKtModel(javaLlm: JavaBaseLlm): KtModel = JavaModelToKt(javaLlm) |
| 89 | + |
| 90 | + /** |
| 91 | + * Adapts an ADK Java session service for the Kotlin engine. Passing a service that is itself an |
| 92 | + * adapted Kotlin one returns the original rather than stacking a second adapter. Note the |
| 93 | + * `agentState` / `rewindBeforeInvocationId` loss described in [JavaAdkToKt]. |
| 94 | + */ |
| 95 | + @JvmStatic |
| 96 | + fun asKtSessionService(service: JavaSessionService): KtSessionService = |
| 97 | + javaSessionServiceAsKt(service) |
| 98 | + |
| 99 | + /** |
| 100 | + * Adapts an ADK Java artifact service for the Kotlin engine, unwrapping a round-tripped Kotlin |
| 101 | + * one rather than stacking adapters. An empty or unmapped artifact part is rejected outright. |
| 102 | + */ |
| 103 | + @JvmStatic |
| 104 | + fun asKtArtifactService(service: JavaArtifactService): KtArtifactService = |
| 105 | + javaArtifactServiceAsKt(service) |
| 106 | + |
| 107 | + /** |
| 108 | + * Adapts an ADK Java memory service for the Kotlin engine, unwrapping a round-tripped Kotlin one |
| 109 | + * rather than stacking adapters. |
| 110 | + */ |
| 111 | + @JvmStatic |
| 112 | + fun asKtMemoryService(service: JavaMemoryService): KtMemoryService = |
| 113 | + javaMemoryServiceAsKt(service) |
| 114 | +} |
0 commit comments