gadk-engine 0.4.0

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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// 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.

//! The opt-in message content of the GenAI semantic conventions (`gen_ai.input.messages`,
//! `gen_ai.output.messages`, `gen_ai.system_instructions`), recorded as a JSON string in the legacy
//! schema and as a structured value otherwise, and the schema's finish reasons.

use std::borrow::Cow;

use gadk_genai::{Blob, Content, FinishReason, Part};
use gadk_model::{LlmRequest, LlmResponse};
use serde::Serialize;
use serde_json::{Map, Value};

use super::attrs::GenerateContentResult;
use super::converters::{AttrValue, to_log_value};
use super::env::use_legacy_schema;

/// Attribute carrying the input messages.
pub(crate) const GEN_AI_INPUT_MESSAGES: &str = "gen_ai.input.messages";
/// Attribute carrying the output messages.
pub(crate) const GEN_AI_OUTPUT_MESSAGES: &str = "gen_ai.output.messages";
/// Attribute carrying the system instructions.
pub(crate) const GEN_AI_SYSTEM_INSTRUCTIONS: &str = "gen_ai.system_instructions";

const PART_TYPE_TEXT: &str = "text";
const PART_TYPE_REASONING: &str = "reasoning";
const PART_TYPE_TOOL_CALL: &str = "tool_call";
const PART_TYPE_TOOL_CALL_RESPONSE: &str = "tool_call_response";
const PART_TYPE_BLOB: &str = "blob";
const PART_TYPE_URI: &str = "uri";

const ROLE_USER: &str = "user";
const ROLE_ASSISTANT: &str = "assistant";
const ROLE_TOOL: &str = "tool";

/// Finish reason: the model stopped normally.
pub(crate) const FINISH_STOP: &str = "stop";
/// Finish reason: the output hit the token limit.
pub(crate) const FINISH_LENGTH: &str = "length";
/// Finish reason: the output was filtered.
pub(crate) const FINISH_CONTENT_FILTER: &str = "content_filter";
/// Finish reason: the model stopped to call a tool.
pub(crate) const FINISH_TOOL_CALL: &str = "tool_call";
/// Finish reason: the call failed.
pub(crate) const FINISH_ERROR: &str = "error";

/// The most bytes of JSON one content attribute may hold: stays under Cloud Trace's 64 KiB
/// attribute limit.
pub(crate) const MAX_CONTENT_ATTRIBUTE_BYTES: usize = 60 << 10;

/// The most bytes of one inline payload recorded (before base64).
pub(crate) const MAX_INLINE_DATA_BYTES: usize = 16 << 10;

/// A content attribute's value: a message list or a part list.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub(crate) enum ContentValue {
    /// A list of chat messages.
    Messages(Vec<ChatMessage>),
    /// A list of parts.
    Parts(Vec<PartValue>),
    /// An encoded tool payload ([`tool_payload`]).
    Raw(Value),
}

impl ContentValue {
    /// The value as an [`AttrValue`].
    pub(crate) fn value(&self) -> AttrValue {
        match self {
            Self::Messages(ms) => AttrValue::Slice(ms.iter().map(ChatMessage::value).collect()),
            Self::Parts(ps) => parts_value(ps),
            Self::Raw(v) => to_log_value(v),
        }
    }
}

/// A content attribute: its key and value.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ContentAttr {
    /// The attribute key.
    pub(crate) key: &'static str,
    /// The content.
    pub(crate) value: ContentValue,
}

/// One chat message (a turn); `finish_reason` only on an output message.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct ChatMessage {
    /// The message role.
    pub(crate) role: String,
    /// The message parts.
    pub(crate) parts: Vec<PartValue>,
    /// The finish reason; omitted when absent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) finish_reason: Option<String>,
}

impl ChatMessage {
    fn value(&self) -> AttrValue {
        let mut kvs = vec![
            str_kv("role", &self.role),
            ("parts".to_owned(), parts_value(&self.parts)),
        ];
        if let Some(fr) = &self.finish_reason {
            kvs.push(str_kv("finish_reason", fr));
        }
        AttrValue::Map(kvs)
    }
}

fn parts_value(ps: &[PartValue]) -> AttrValue {
    AttrValue::Slice(ps.iter().map(PartValue::value).collect())
}

/// One converted part: text, tool call, blob, URI or tool response. Fields serialize in declaration
/// order, and empty optional fields are omitted.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub(crate) enum PartValue {
    /// A text part (`text` or `reasoning`).
    Text {
        /// The part type.
        #[serde(rename = "type")]
        kind: &'static str,
        /// The text.
        content: String,
    },
    /// A tool call.
    ToolCall {
        /// The part type.
        #[serde(rename = "type")]
        kind: &'static str,
        /// The call ID.
        #[serde(skip_serializing_if = "String::is_empty")]
        id: String,
        /// The tool name.
        name: String,
        /// The call arguments ([`tool_payload`]).
        arguments: Value,
    },
    /// An inline blob.
    Blob {
        /// The part type.
        #[serde(rename = "type")]
        kind: &'static str,
        /// The MIME type.
        #[serde(skip_serializing_if = "String::is_empty")]
        mime_type: String,
        /// The modality (`image`, `video`, `audio` or `document`).
        modality: &'static str,
        /// The payload, base64-encoded.
        content: String,
        /// Whether the payload was cut to the inline-data limit.
        #[serde(skip_serializing_if = "std::ops::Not::not")]
        truncated: bool,
    },
    /// A file referenced by URI.
    Uri {
        /// The part type.
        #[serde(rename = "type")]
        kind: &'static str,
        /// The MIME type.
        #[serde(skip_serializing_if = "String::is_empty")]
        mime_type: String,
        /// The modality (`image`, `video`, `audio` or `document`).
        modality: &'static str,
        /// The URI.
        uri: String,
    },
    /// A tool response.
    ToolResponse {
        /// The part type.
        #[serde(rename = "type")]
        kind: &'static str,
        /// The call ID.
        #[serde(skip_serializing_if = "String::is_empty")]
        id: String,
        /// The tool name.
        #[serde(skip_serializing_if = "String::is_empty")]
        name: String,
        /// The tool response ([`tool_payload`]).
        response: Value,
    },
}

impl PartValue {
    /// The part as a map [`AttrValue`], with the same fields and omissions as its serialization.
    fn value(&self) -> AttrValue {
        let mut kvs = Vec::new();
        match self {
            Self::Text { kind, content } => {
                kvs.push(str_kv("type", kind));
                kvs.push(str_kv("content", content));
            }
            Self::ToolCall {
                kind,
                id,
                name,
                arguments,
            } => {
                kvs.push(str_kv("type", kind));
                if !id.is_empty() {
                    kvs.push(str_kv("id", id));
                }
                kvs.push(str_kv("name", name));
                kvs.push(("arguments".to_owned(), to_log_value(arguments)));
            }
            Self::Blob {
                kind,
                mime_type,
                modality,
                content,
                truncated,
            } => {
                kvs.push(str_kv("type", kind));
                if !mime_type.is_empty() {
                    kvs.push(str_kv("mime_type", mime_type));
                }
                kvs.push(str_kv("modality", modality));
                kvs.push(str_kv("content", content));
                if *truncated {
                    kvs.push(("truncated".to_owned(), AttrValue::Bool(true)));
                }
            }
            Self::Uri {
                kind,
                mime_type,
                modality,
                uri,
            } => {
                kvs.push(str_kv("type", kind));
                if !mime_type.is_empty() {
                    kvs.push(str_kv("mime_type", mime_type));
                }
                kvs.push(str_kv("modality", modality));
                kvs.push(str_kv("uri", uri));
            }
            Self::ToolResponse {
                kind,
                id,
                name,
                response,
            } => {
                kvs.push(str_kv("type", kind));
                if !id.is_empty() {
                    kvs.push(str_kv("id", id));
                }
                if !name.is_empty() {
                    kvs.push(str_kv("name", name));
                }
                kvs.push(("response".to_owned(), to_log_value(response)));
            }
        }
        AttrValue::Map(kvs)
    }
}

/// A string key-value pair. A Rust string is always valid UTF-8, so nothing needs replacing.
fn str_kv(k: &str, v: &str) -> (String, AttrValue) {
    (k.to_owned(), AttrValue::string(v))
}

/// Request content: the system instruction and the conversation, in the order sent. User content,
/// so callers gate it on content capture.
pub(crate) fn request_content(req: &LlmRequest) -> Vec<ContentAttr> {
    let mut content = Vec::new();
    if let Some(si) = req
        .config
        .as_ref()
        .and_then(|c| c.system_instruction.as_ref())
    {
        let parts = semconv_parts(&si.parts);
        if !parts.is_empty() {
            content.push(ContentAttr {
                key: GEN_AI_SYSTEM_INSTRUCTIONS,
                value: ContentValue::Parts(parts),
            });
        }
    }
    if !req.contents.is_empty() {
        let msgs = req
            .contents
            .iter()
            .map(|c| ChatMessage {
                role: schema_role(Some(c)).to_owned(),
                parts: semconv_parts(&c.parts),
                finish_reason: None,
            })
            .collect();
        content.push(ContentAttr {
            key: GEN_AI_INPUT_MESSAGES,
            value: ContentValue::Messages(msgs),
        });
    }
    content
}

/// Response content: the legacy schema records the final response (none for a partial one);
/// otherwise every response of the call is one output message.
pub(crate) fn response_content(r: &GenerateContentResult) -> Vec<ContentAttr> {
    if use_legacy_schema() {
        let Some(resp) = r.response.as_deref().filter(|resp| !resp.partial) else {
            return Vec::new();
        };
        let fr = schema_finish_reason(Some(resp), r.error.is_some());
        return vec![ContentAttr {
            key: GEN_AI_OUTPUT_MESSAGES,
            value: ContentValue::Messages(vec![output_message(resp, &fr)]),
        }];
    }
    let msgs: Vec<ChatMessage> = r
        .responses()
        .iter()
        .map(|resp| output_message(resp, &reported_finish_reason(Some(resp), false)))
        .collect();
    if msgs.is_empty() {
        return Vec::new();
    }
    vec![ContentAttr {
        key: GEN_AI_OUTPUT_MESSAGES,
        value: ContentValue::Messages(msgs),
    }]
}

/// One output message for `resp`: a suppressed candidate keeps an empty parts list.
fn output_message(resp: &LlmResponse, finish_reason: &str) -> ChatMessage {
    ChatMessage {
        role: ROLE_ASSISTANT.to_owned(),
        parts: resp
            .content
            .as_ref()
            .map(|c| semconv_parts(&c.parts))
            .unwrap_or_default(),
        finish_reason: Some(finish_reason.to_owned()),
    }
}

/// Span content attributes: the legacy schema records each value's JSON text, the default one the
/// structured value; a value whose JSON is over [`MAX_CONTENT_ATTRIBUTE_BYTES`] is left off and
/// reported.
pub(crate) fn span_content_attributes(content: &[ContentAttr]) -> Vec<(&'static str, AttrValue)> {
    let legacy = use_legacy_schema();
    let mut attrs = Vec::new();
    for c in content {
        let Ok(encoded) = gadk_model::to_go_json(&c.value) else {
            // Unreachable.
            continue;
        };
        if encoded.len() > MAX_CONTENT_ATTRIBUTE_BYTES {
            tracing::warn!(
                target: "gadk_engine::telemetry",
                "adk: telemetry: leaving {} off the span: its JSON encoding is {} bytes, over the {}-byte limit",
                c.key,
                encoded.len(),
                MAX_CONTENT_ATTRIBUTE_BYTES
            );
            continue;
        }
        if legacy {
            attrs.push((
                c.key,
                AttrValue::String(String::from_utf8_lossy(&encoded).into_owned()),
            ));
        } else {
            attrs.push((c.key, c.value.value()));
        }
    }
    attrs
}

/// Log-record content attributes: always the structured value.
pub(crate) fn event_content_attributes(content: &[ContentAttr]) -> Vec<(&'static str, AttrValue)> {
    content.iter().map(|c| (c.key, c.value.value())).collect()
}

/// Converts parts; parts with no mapping are skipped.
pub(crate) fn semconv_parts(ps: &[Part]) -> Vec<PartValue> {
    ps.iter().filter_map(semconv_part).collect()
}

/// Converts one part: structured variants are tried before text; code execution and provider-side
/// tool parts are dropped.
pub(crate) fn semconv_part(p: &Part) -> Option<PartValue> {
    if let Some(fc) = &p.function_call {
        return Some(PartValue::ToolCall {
            kind: PART_TYPE_TOOL_CALL,
            id: fc.id.clone(),
            name: fc.name.clone(),
            arguments: tool_payload(&fc.args),
        });
    }
    if let Some(fr) = &p.function_response {
        return Some(PartValue::ToolResponse {
            kind: PART_TYPE_TOOL_CALL_RESPONSE,
            id: fr.id.clone(),
            name: fr.name.clone(),
            response: tool_payload(&fr.response),
        });
    }
    if let Some(blob) = &p.inline_data {
        let (content, truncated) = encode_inline_data(blob);
        return Some(PartValue::Blob {
            kind: PART_TYPE_BLOB,
            mime_type: blob.mime_type.clone(),
            modality: modality_of(&blob.mime_type),
            content,
            truncated,
        });
    }
    if let Some(fd) = &p.file_data {
        return Some(PartValue::Uri {
            kind: PART_TYPE_URI,
            mime_type: fd.mime_type.clone(),
            modality: modality_of(&fd.mime_type),
            uri: fd.file_uri.clone(),
        });
    }
    if !p.text.is_empty() {
        let kind = if p.thought {
            PART_TYPE_REASONING
        } else {
            PART_TYPE_TEXT
        };
        return Some(PartValue::Text {
            kind,
            content: p.text.clone(),
        });
    }
    None
}

/// An application tool value as JSON; an empty map is `null`, as elsewhere in gadk. A `serde_json`
/// value always encodes, so no unserializable placeholder is needed.
pub(crate) fn tool_payload(v: &Map<String, Value>) -> Value {
    if v.is_empty() {
        return Value::Null;
    }
    Value::Object(v.clone())
}

/// Encodes standard base64 of at most [`MAX_INLINE_DATA_BYTES`] bytes of `blob.data`, and whether
/// the payload was cut (gadk-genai's `Blob` serde does the encoding).
pub(crate) fn encode_inline_data(blob: &Blob) -> (String, bool) {
    let truncated = blob.data.len() > MAX_INLINE_DATA_BYTES;
    let data = if truncated {
        blob.data.slice(..MAX_INLINE_DATA_BYTES)
    } else {
        blob.data.clone()
    };
    let probe = Blob {
        data,
        ..Default::default()
    };
    let encoded = serde_json::to_value(&probe)
        .ok()
        .and_then(|v| v.get("data").and_then(Value::as_str).map(str::to_owned))
        .unwrap_or_default();
    (encoded, truncated)
}

/// Maps an IANA MIME type onto the schema's modality enum.
pub(crate) fn modality_of(mime_type: &str) -> &'static str {
    let t = gadk_platform::go_to_lower(mime_type);
    if t.starts_with("image/") {
        "image"
    } else if t.starts_with("video/") {
        "video"
    } else if t.starts_with("audio/") {
        "audio"
    } else {
        "document"
    }
}

/// Whether the content holds a function call.
pub(crate) fn has_function_call(c: Option<&Content>) -> bool {
    c.is_some_and(|c| c.parts.iter().any(|p| p.function_call.is_some()))
}

/// The schema role of a turn: a turn carrying a tool result is `tool`; `user`/`model` map to
/// `user`/`assistant`; any other role passes through.
pub(crate) fn schema_role(c: Option<&Content>) -> &str {
    let Some(c) = c else { return ROLE_USER };
    if c.parts
        .iter()
        .any(|p| p.function_response.is_some() || p.tool_response.is_some())
    {
        return ROLE_TOOL;
    }
    match c.role.as_str() {
        "" | gadk_genai::ROLE_USER => ROLE_USER,
        gadk_genai::ROLE_MODEL => ROLE_ASSISTANT,
        other => other,
    }
}

/// The finish reason to report: [`schema_finish_reason`], or `""` when `resp` reports no reason
/// (`FINISH_REASON_UNSPECIFIED` counts as none). `err` is whether the call failed.
pub(crate) fn reported_finish_reason(resp: Option<&LlmResponse>, err: bool) -> Cow<'static, str> {
    if !err
        && let Some(r) = resp
        && r.error_code.is_empty()
        && !r.interrupted
        && matches!(r.finish_reason, None | Some(FinishReason::Unspecified))
    {
        return Cow::Borrowed("");
    }
    schema_finish_reason(resp, err)
}

/// The schema's finish reason: a failed call, an error code or an interruption is `error`; a stop
/// carrying a tool call is `tool_call`; a value this mapping does not know is lowercased.
pub(crate) fn schema_finish_reason(resp: Option<&LlmResponse>, err: bool) -> Cow<'static, str> {
    let Some(resp) = resp else {
        return Cow::Borrowed(FINISH_ERROR);
    };
    if err || !resp.error_code.is_empty() || resp.interrupted {
        return Cow::Borrowed(FINISH_ERROR);
    }
    use FinishReason as F;
    Cow::Borrowed(match &resp.finish_reason {
        None | Some(F::Unspecified | F::Stop) => {
            if has_function_call(resp.content.as_ref()) {
                FINISH_TOOL_CALL
            } else {
                FINISH_STOP
            }
        }
        Some(F::MaxTokens) => FINISH_LENGTH,
        Some(
            F::Safety
            | F::Recitation
            | F::Language
            | F::Blocklist
            | F::ProhibitedContent
            | F::Spii
            | F::ImageSafety
            | F::ImageProhibitedContent
            | F::ImageRecitation,
        ) => FINISH_CONTENT_FILTER,
        Some(
            F::Other
            | F::MalformedFunctionCall
            | F::UnexpectedToolCall
            | F::TooManyToolCalls
            | F::NoImage
            | F::ImageOther,
        ) => FINISH_ERROR,
        Some(other) => return Cow::Owned(gadk_platform::go_to_lower(other.as_str())),
    })
}