|
| 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.kt.examples.runtime |
| 18 | + |
| 19 | +import com.google.adk.kt.agents.LlmAgent |
| 20 | +import com.google.adk.kt.agents.ResumabilityConfig |
| 21 | +import com.google.adk.kt.agents.RunConfig |
| 22 | +import com.google.adk.kt.agents.StreamingMode |
| 23 | +import com.google.adk.kt.annotations.ExperimentalResumabilityFeature |
| 24 | +import com.google.adk.kt.models.Gemini |
| 25 | +import com.google.adk.kt.runners.InMemoryRunner |
| 26 | +import com.google.adk.kt.sessions.InMemorySessionService |
| 27 | +import com.google.adk.kt.types.Content |
| 28 | +import com.google.adk.kt.types.Role |
| 29 | +import kotlinx.coroutines.flow.collect |
| 30 | +import kotlinx.coroutines.runBlocking |
| 31 | + |
| 32 | +// --8<-- [start:basic_usage] |
| 33 | +val config = |
| 34 | + RunConfig( |
| 35 | + streamingMode = StreamingMode.SSE, |
| 36 | + ) |
| 37 | + |
| 38 | +// Pass it to runner.runAsync |
| 39 | +// runner.runAsync(..., runConfig = config) |
| 40 | +// --8<-- [end:basic_usage] |
| 41 | + |
| 42 | +// --8<-- [start:full_example] |
| 43 | +fun runWithConfig(runner: InMemoryRunner) = |
| 44 | + runBlocking { |
| 45 | + val config = |
| 46 | + RunConfig( |
| 47 | + streamingMode = StreamingMode.SSE, |
| 48 | + customMetadata = mapOf("priority" to "high"), |
| 49 | + ) |
| 50 | + |
| 51 | + runner |
| 52 | + .runAsync( |
| 53 | + userId = "user123", |
| 54 | + sessionId = "session456", |
| 55 | + newMessage = Content.fromText(Role.USER, "Hello!"), |
| 56 | + runConfig = config, |
| 57 | + ).collect { event -> |
| 58 | + // handle events |
| 59 | + } |
| 60 | + } |
| 61 | +// --8<-- [end:full_example] |
| 62 | + |
| 63 | +// --8<-- [start:custom_metadata] |
| 64 | +val metadataConfig = |
| 65 | + RunConfig( |
| 66 | + customMetadata = mapOf("user_tier" to "premium"), |
| 67 | + ) |
| 68 | +// --8<-- [end:custom_metadata] |
| 69 | + |
| 70 | +// --8<-- [start:streaming_config] |
| 71 | +val streamingConfig = |
| 72 | + RunConfig( |
| 73 | + streamingMode = StreamingMode.SSE, |
| 74 | + ) |
| 75 | +// --8<-- [end:streaming_config] |
| 76 | + |
| 77 | +private val rootAgent = |
| 78 | + LlmAgent(name = "my_agent", model = Gemini(name = "gemini-flash-latest")) |
| 79 | + |
| 80 | +// --8<-- [start:resumability_config] |
| 81 | +@OptIn(ExperimentalResumabilityFeature::class) |
| 82 | +val runner = |
| 83 | + InMemoryRunner( |
| 84 | + agent = rootAgent, |
| 85 | + appName = "my_resumable_agent", |
| 86 | + sessionService = InMemorySessionService(), |
| 87 | + resumabilityConfig = ResumabilityConfig(isResumable = true), |
| 88 | + ) |
| 89 | +// --8<-- [end:resumability_config] |
| 90 | + |
| 91 | +// --8<-- [start:resume_usage] |
| 92 | +fun resumeAgent(runner: InMemoryRunner) = |
| 93 | + runBlocking { |
| 94 | + runner |
| 95 | + .runAsync( |
| 96 | + userId = "user123", |
| 97 | + sessionId = "session456", |
| 98 | + invocationId = "previous-invocation-id", |
| 99 | + ).collect { event -> |
| 100 | + // resume execution from previous state |
| 101 | + } |
| 102 | + } |
| 103 | +// --8<-- [end:resume_usage] |
0 commit comments