gadk-engine 0.5.17

The gadk runtime: LLM agent and flow, workflow agents and graphs, and the runner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), 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.

//! Live mode of the runner: [`Runner::run_live`], [`RunnerLiveSession`] and [`ClosedLiveSession`].

use std::any::Any;
use std::sync::Arc;

use async_trait::async_trait;
use futures::StreamExt;
use gadk_agent::fiber::FiberStream;
use gadk_agent::{
    AgentError, EventStream, InvocationContext, LiveRequest, LiveRunConfig, LiveSession, RunConfig,
    StreamingMode,
};
use gadk_model::LlmResponse;
use gadk_session::{Event, Session};
use tokio_util::sync::CancellationToken;

use super::compaction::InvocationGuard;
use super::{
    ERR_USER_FUNCTION_CALL, RunOptions, Runner, RunnerInner, from_plugin, has_function_call,
    stamp_timestamp, wrap,
};

impl Runner {
    /// Starts a live (bidirectional streaming) run of the agent that should handle the session's
    /// next turn. Returns the session to send input on and the event stream. Dropping the stream at
    /// a yield runs the after-run callbacks first, on the still-live invocation context, and then
    /// cancels the invocation; dropped inside an await, it cancels at once. The live session task
    /// is detached rather than aborted, so a tool call already running finishes on its own (the
    /// after-run callbacks do not wait for it).
    ///
    /// Must be called inside a Tokio runtime: the live flow spawns tasks. `opts` is accepted for
    /// symmetry with [`Runner::run`] and not applied.
    ///
    /// The returned stream runs on the consumer's fiber ([`FiberStream::consumer`]); this
    /// function's own work (session lookup, before-run callbacks, starting the live flow) runs in
    /// the caller's future.
    pub async fn run_live(
        &self,
        user_id: &str,
        session_id: &str,
        cfg: LiveRunConfig,
        opts: RunOptions,
    ) -> Result<(Arc<dyn LiveSession>, EventStream), AgentError> {
        // Live runs read none of the run options.
        let _ = opts;
        let this = self.inner.clone();
        let stored = this.get_or_create_session(user_id, session_id).await?;
        let agent_to_run = this.find_agent_to_run(&*stored, None)?;
        let Some(live) = agent_to_run.as_live() else {
            return Err(AgentError::other(format!(
                "agent {} does not support Live Run",
                agent_to_run.name()
            )));
        };

        // Live is always bidirectional streaming.
        let run_cfg = RunConfig {
            streaming_mode: StreamingMode::Bidi,
            live: Some(cfg),
            ..Default::default()
        };
        let token = CancellationToken::new();
        // Live runs attach no compaction runtime.
        let ictx = this.new_invocation_context(
            &stored,
            agent_to_run.clone(),
            None,
            &run_cfg,
            &token,
            false,
            // `run_live` takes no caller context, so there is no plugin-inheritance mark.
            this.with_plugin_manager(None),
            // No invocation ID or branch.
            String::new(),
            String::new(),
        );

        // The before-run reply goes through on_event (and is stamped and persisted) as on
        // Runner::run, and the run through the after-run callbacks.
        if let Some(event) = this.run_before_run_callback(&ictx).await? {
            let stream = FiberStream::consumer(early_exit_stream(
                this.clone(),
                ictx.clone(),
                stored.clone(),
                event,
                token,
            ))
            .boxed();
            return Ok((Arc::new(ClosedLiveSession), stream));
        }

        let (agent_sess, inner) = live.run_live(ictx.clone()).await?;
        let stream = FiberStream::consumer(wrap_live_stream(
            this.clone(),
            ictx.clone(),
            stored.clone(),
            inner,
            token,
        ))
        .boxed();
        let session: Arc<dyn LiveSession> = Arc::new(RunnerLiveSession {
            sess: agent_sess,
            runner: this,
            ctx: ictx,
            stored,
        });
        Ok((session, stream))
    }
}

/// The stream for a before-run early exit: yields the before-run reply, then runs the after-run
/// callbacks (deferred: also when the consumer drops the stream at the yield).
fn early_exit_stream(
    this: Arc<RunnerInner>,
    ctx: InvocationContext,
    stored: Arc<dyn Session>,
    event: Event,
    token: CancellationToken,
) -> EventStream {
    let cancel_on_drop = token.drop_guard();
    Box::pin(async_stream::stream! {
        let mut guard = InvocationGuard::new(this, stored, ctx);
        guard.cancel_on_drop(cancel_on_drop);
        guard.no_compaction();
        guard.defer_after_run();
        guard.at_yield(true);
        yield Ok(event);
        guard.at_yield(false);
        guard.finish().await;
    })
}

/// Wraps the live flow's stream: plugin `on_event`, chronological buffering of tool calls and
/// responses while a transcription is in progress, persistence of non-partial events without inline
/// data, and the EOF flush of buffered events.
///
/// An [`InvocationGuard`] without compaction (live runs do not compact) owns the deferred after-run
/// callbacks and the invocation token's cancel-on-drop: a consumer that drops the stream at a yield
/// gets the after-run callbacks on its fiber before the token is cancelled; a drop inside an await
/// cancels at once.
fn wrap_live_stream(
    this: Arc<RunnerInner>,
    ctx: InvocationContext,
    stored: Arc<dyn Session>,
    mut inner: EventStream,
    token: CancellationToken,
) -> EventStream {
    // Dropping the stream cancels the invocation, which stops the live flow. The drop guard is
    // made here, not on first poll, so a stream dropped before it is ever polled cancels too.
    let cancel_on_drop = token.drop_guard();
    Box::pin(async_stream::stream! {
        // Made at the first poll, inside the consumer's fiber, which its `Drop` re-enters.
        let mut guard = InvocationGuard::new(this.clone(), stored.clone(), ctx.clone());
        guard.cancel_on_drop(cancel_on_drop);
        guard.no_compaction();
        // The after-run callbacks run once when the stream ends, or (spawned) when the consumer
        // drops it early.
        guard.defer_after_run();
        let mut buffered: Vec<Event> = Vec::new();
        let mut is_transcribing = false;

        while let Some(item) = inner.next().await {
            let mut event = match item {
                Ok(ev) => ev,
                Err(e) => {
                    guard.at_yield(true);
                    yield Err(e);
                    guard.at_yield(false);
                    continue;
                }
            };
            stamp_timestamp(&ctx, &mut event);

            if !event.response.partial
                && event.node_info.as_ref().is_some_and(|n| n.message_as_output)
                && event.response.content.is_some()
            {
                event.output = None;
            }

            let record = event.actions.compaction.clone();
            match this.plugin_manager.run_on_event(ctx.clone(), event.clone()).await {
                Err(e) => {
                    guard.at_yield(true);
                    yield Err(e);
                    guard.at_yield(false);
                    continue;
                }
                Ok(modified) => event = from_plugin(event, modified, record),
            }
            // A plugin may have handed back an event without a timestamp.
            stamp_timestamp(&ctx, &mut event);

            let has_transcription = event.response.input_transcription.is_some()
                || event.response.output_transcription.is_some();
            if event.response.partial && has_transcription {
                is_transcribing = true;
            }

            // Tool calls and responses that arrive mid-transcription wait for it, so the
            // session records them after the transcription they interrupted.
            if is_transcribing && is_tool_call_or_response(&event) {
                buffered.push(event);
                continue;
            }

            if !event.response.partial && has_transcription {
                is_transcribing = false;
                if let Err(e) = this.session_service.append_event(&*stored, &mut event).await {
                    guard.at_yield(true);
                    yield Err(wrap("failed to add event to session", e));
                    guard.at_yield(false);
                    continue;
                }
                guard.at_yield(true);
                yield Ok(event);
                guard.at_yield(false);
                for mut buffered_event in std::mem::take(&mut buffered) {
                    if let Err(e) = this
                        .session_service
                        .append_event(&*stored, &mut buffered_event)
                        .await
                    {
                        guard.at_yield(true);
                        yield Err(wrap("failed to add event to session", e));
                        guard.at_yield(false);
                        continue;
                    }
                    guard.at_yield(true);
                    yield Ok(buffered_event);
                    guard.at_yield(false);
                }
                continue;
            }

            if !event.response.partial
                && !has_inline_data(&event)
                && let Err(e) = this.session_service.append_event(&*stored, &mut event).await
            {
                guard.at_yield(true);
                yield Err(wrap("failed to add event to session", e));
                guard.at_yield(false);
                continue;
            }
            guard.at_yield(true);
            yield Ok(event);
            guard.at_yield(false);
        }

        // EOF: flush what is still buffered. An append takes no context, so a cancelled invocation
        // cannot cut the flush short. Reaching this point means the consumer is still reading: a
        // dropped stream never resumes.
        for mut buffered_event in std::mem::take(&mut buffered) {
            if let Err(e) = this
                .session_service
                .append_event(&*stored, &mut buffered_event)
                .await
            {
                guard.at_yield(true);
                yield Err(wrap("failed to add event to session", e));
                guard.at_yield(false);
                continue;
            }
            guard.at_yield(true);
            yield Ok(buffered_event);
            guard.at_yield(false);
        }

        guard.finish().await;
    })
}

/// Whether the event's content has a function call or a function response part.
fn is_tool_call_or_response(event: &Event) -> bool {
    event.response.content.as_ref().is_some_and(|c| {
        c.parts
            .iter()
            .any(|p| p.function_call.is_some() || p.function_response.is_some())
    })
}

/// Whether any part of the event's content carries inline data.
fn has_inline_data(event: &Event) -> bool {
    event
        .response
        .content
        .as_ref()
        .is_some_and(|c| c.parts.iter().any(|p| p.inline_data.is_some()))
}

/// Forwards to the agent's live session and persists the user's non-function-response content.
pub(crate) struct RunnerLiveSession {
    sess: Arc<dyn LiveSession>,
    runner: Arc<RunnerInner>,
    ctx: InvocationContext,
    stored: Arc<dyn Session>,
}

#[async_trait]
impl LiveSession for RunnerLiveSession {
    /// Forwards the request unchanged; a request carrying a function call is refused.
    async fn send(&self, req: LiveRequest) -> Result<(), AgentError> {
        // Refused before anything is forwarded or stored.
        if has_function_call(req.content.as_ref()) {
            return Err(AgentError::other(ERR_USER_FUNCTION_CALL));
        }
        let content = req.content.clone();
        self.sess.send(req).await?;

        if let Some(content) = content
            && !content.parts.is_empty()
            && !content.parts.iter().any(|p| p.function_response.is_some())
        {
            let mut event = Event::new(self.ctx.platform(), self.ctx.invocation_id());
            event.author = "user".to_string();
            event.response = LlmResponse {
                content: Some(content),
                ..Default::default()
            };
            self.runner
                .session_service
                .append_event(&*self.stored, &mut event)
                .await
                .map_err(|e| wrap("failed to add user event to session", e))?;
        }
        Ok(())
    }

    async fn close(&self) -> Result<(), AgentError> {
        self.sess.close().await
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// The session returned by a before-run early exit.
pub(crate) struct ClosedLiveSession;

#[async_trait]
impl LiveSession for ClosedLiveSession {
    async fn send(&self, _req: LiveRequest) -> Result<(), AgentError> {
        Err(AgentError::other("session is closed"))
    }

    async fn close(&self) -> Result<(), AgentError> {
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use std::sync::atomic::{AtomicBool, Ordering};

    use gadk_agent::{AgentConfig, Plugin};
    use gadk_genai::Content;
    use gadk_platform::Platform;
    use gadk_session::InMemorySessionService;

    use super::*;
    use crate::runner::{PluginConfig, RunnerConfig};

    /// R12: an agent without live support fails before any plugin runs.
    #[tokio::test]
    async fn run_live_rejects_agent_without_live_support_before_plugins() {
        let before_run = Arc::new(AtomicBool::new(false));
        let flag = before_run.clone();
        let plugin = Plugin::new(gadk_agent::PluginConfig {
            name: "p".into(),
            before_run_callback: Some(Arc::new(move |_ctx| {
                flag.store(true, Ordering::SeqCst);
                Box::pin(async { Ok::<Option<Content>, AgentError>(None) })
            })),
            ..Default::default()
        });
        let agent = gadk_agent::agent::new(AgentConfig {
            name: "plain".into(),
            ..Default::default()
        })
        .unwrap();
        let runner = Runner::new(RunnerConfig {
            app_name: "app".into(),
            agent,
            session_service: Arc::new(InMemorySessionService::new()),
            artifact_service: None,
            memory_service: None,
            plugin_config: PluginConfig {
                plugins: vec![plugin],
                ..Default::default()
            },
            auto_create_session: true,
            platform: Platform::default(),
            compaction: None,
        })
        .unwrap();
        let err = runner
            .run_live("u", "s", LiveRunConfig::default(), RunOptions::default())
            .await
            .err()
            .expect("run_live must fail");
        assert_eq!(err.to_string(), "agent plain does not support Live Run");
        assert!(
            !before_run.load(Ordering::SeqCst),
            "no plugin may run first"
        );
    }

    #[tokio::test]
    async fn closed_live_session_rejects_send() {
        let err = ClosedLiveSession
            .send(LiveRequest::default())
            .await
            .expect_err("send must fail");
        assert_eq!(err.to_string(), "session is closed");
        ClosedLiveSession.close().await.unwrap();
    }

    /// Records the invocation's cancellation token and yields nothing.
    struct TokenCapture(Arc<parking_lot::Mutex<Option<CancellationToken>>>);

    #[async_trait]
    impl gadk_agent::LiveAgent for TokenCapture {
        async fn run_live(
            &self,
            ctx: InvocationContext,
        ) -> Result<(Arc<dyn LiveSession>, EventStream), AgentError> {
            *self.0.lock() = Some(ctx.cancellation_token().clone());
            Ok((
                Arc::new(ClosedLiveSession),
                futures::stream::pending().boxed(),
            ))
        }
    }

    /// Dropping the returned stream cancels the invocation, even if it was never polled.
    #[tokio::test]
    async fn dropping_unpolled_stream_cancels_invocation() {
        let captured = Arc::new(parking_lot::Mutex::new(None));
        let agent = gadk_agent::new_with_state(
            AgentConfig {
                name: "live".into(),
                ..Default::default()
            },
            gadk_agent::AgentState {
                agent_type: gadk_agent::AgentType::CustomAgent,
                config: None,
                on_before_agent_callback_event: None,
                live: Some(Arc::new(TokenCapture(captured.clone()))),
            },
        )
        .unwrap();
        let runner = Runner::new(RunnerConfig {
            app_name: "app".into(),
            agent,
            session_service: Arc::new(InMemorySessionService::new()),
            artifact_service: None,
            memory_service: None,
            plugin_config: PluginConfig::default(),
            auto_create_session: true,
            platform: Platform::default(),
            compaction: None,
        })
        .unwrap();
        let (_sess, stream) = runner
            .run_live("u", "s", LiveRunConfig::default(), RunOptions::default())
            .await
            .unwrap();
        let token = captured.lock().clone().expect("run_live was called");
        assert!(!token.is_cancelled());
        drop(stream);
        assert!(token.is_cancelled(), "dropping the stream must cancel");
    }
}