greentic-aw-runtime 1.2.1

Enterprise Agentic Worker runtime — Plan-Act-Observe loop, Redis state, tool dispatch via greentic-ext-runtime
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
//! `GreenticLlmBackend` — adapts a [`greentic_llm`] provider to the AW
//! [`LlmBackend`](crate::llm::LlmBackend) trait, giving `dw.agent` nodes the
//! full multi-provider matrix (DeepSeek, Anthropic, Gemini, …) instead of the
//! hardwired in-process OpenAI client.
//!
//! Mirrors the designer's `aw_backend_adapter`, but maps the AW request/response
//! types straight to `greentic_llm` types (no wire-`Value` round-trip) and reuses
//! the `_FN_`/`_DOT_` tool-name codec from [`crate::llm_openai`] so dotted
//! extension ids survive providers that reject dots in function names.

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use greentic_llm::{
    ChatImage as GChatImage, ChatMessage as GChatMessage, ChatRequest as GChatRequest,
    ChatResponse as GChatResponse, Credential, LlmProvider, MessageRole, ProviderKind, RigBackend,
    ToolCall as GToolCall, ToolDef as GToolDef,
};

use crate::artifact_reader::ArtifactReader;
use crate::attachment_guard::AttachmentTextGuard;
use crate::attachments_materialize::{TurnAttachments, TurnKey, materialize};
use crate::error::LlmError;
use crate::llm::{LlmBackend, LlmRequest, LlmResponse};
use crate::state::{ChatMessage, ToolCallRecord};
use crate::tool_wire_name::{ToolNameCodec, wire_tool_name};

/// AW LLM backend backed by greentic-llm's multi-provider `RigBackend`.
///
/// One backend serves every agent in the process; a `RigBackend` is built
/// lazily per `(provider, model)` pair — taken from each agent's
/// `AgentConfig.llm` and carried on the request — and cached. All providers
/// share the single env-resolved API key; per-provider keys are a follow-up.
pub struct GreenticLlmBackend {
    api_key: String,
    base_url: Option<String>,
    cache: Mutex<HashMap<(String, String), Arc<dyn LlmProvider>>>,
    /// Resolves attachment bytes for the current turn. `None`: attachments
    /// are not materialised and the agent is told so (the turn still runs).
    artifacts: Option<Arc<dyn ArtifactReader>>,
    /// Makes the per-turn nonce of document-block markers. Injected only in
    /// tests, to make the markers deterministic.
    nonce_source: fn() -> String,
}

/// 122 random bits (a v4 UUID), as 32 lowercase hex characters.
fn random_nonce() -> String {
    uuid::Uuid::new_v4().simple().to_string()
}

impl GreenticLlmBackend {
    /// `api_key` is the env-resolved key used for whatever provider an agent
    /// declares; `base_url` overrides the provider endpoint (self-hosted
    /// gateways / Ollama) and is omitted when blank.
    pub fn new(api_key: impl Into<String>, base_url: Option<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: base_url.filter(|s| !s.trim().is_empty()),
            cache: Mutex::new(HashMap::new()),
            artifacts: None,
            nonce_source: random_nonce,
        }
    }

    /// Inject the reader used to resolve attachment bytes for the current turn.
    pub fn with_artifact_reader(mut self, reader: Arc<dyn ArtifactReader>) -> Self {
        self.artifacts = Some(reader);
        self
    }

    /// Test seam: make document-block nonces deterministic.
    #[cfg(any(test, feature = "test-mock"))]
    pub fn with_nonce_source(mut self, source: fn() -> String) -> Self {
        self.nonce_source = source;
        self
    }

    /// Test seam: serve `(provider, model)` from `p` instead of building a
    /// `RigBackend`.
    #[cfg(any(test, feature = "test-mock"))]
    pub fn with_cached_provider(
        self,
        provider: &str,
        model: &str,
        p: Arc<dyn LlmProvider>,
    ) -> Self {
        self.cache
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert((provider.to_string(), model.to_string()), p);
        self
    }

    /// Resolve (or build + cache) the greentic-llm provider for an agent's
    /// declared provider + model.
    fn provider_for(&self, provider: &str, model: &str) -> Result<Arc<dyn LlmProvider>, LlmError> {
        let key = (provider.to_string(), model.to_string());
        if let Some(found) = self
            .cache
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&key)
        {
            return Ok(found.clone());
        }
        let kind: ProviderKind = provider
            .parse()
            .map_err(|_| LlmError::BadRequest(format!("unknown LLM provider '{provider}'")))?;
        // `Credential` is `ZeroizeOnDrop` (implements Drop), so struct-update
        // syntax (`..Default::default()`) cannot move out of it — build a
        // default and set the fields we have.
        #[allow(clippy::field_reassign_with_default)]
        let credential = {
            let mut credential = Credential::default();
            credential.api_key = self.api_key.clone();
            credential.base_url = self.base_url.clone();
            credential
        };
        let backend = RigBackend::new(kind, model, &credential)
            .map_err(|e| LlmError::BadRequest(format!("build {provider} backend: {e}")))?;
        let arc: Arc<dyn LlmProvider> = Arc::new(backend);
        self.cache
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(key, arc.clone());
        Ok(arc)
    }
}

impl LlmBackend for GreenticLlmBackend {
    fn complete<'a>(
        &'a self,
        request: LlmRequest,
    ) -> Pin<Box<dyn Future<Output = Result<LlmResponse, LlmError>> + Send + 'a>> {
        Box::pin(async move {
            let provider =
                self.provider_for(&request.provider.provider, &request.provider.model)?;
            // The vision gate runs BEFORE chat(): a provider without vision
            // refuses any image with a hard error, so an image it cannot take
            // is never sent and the agent is told instead.
            let vision = provider.capabilities().vision;
            let mut request = request;
            let memo = request.turn_attachments.clone();
            let guard = request.attachment_text_guard.clone();
            let images = self
                .apply_attachments(&mut request.history, vision, &memo, guard.as_deref())
                .await;
            let image_count = images.len();
            let chat_request = build_chat_request(&request, images);
            // Built from the list the model is shown: a sanitised wire name
            // cannot be split back apart by string surgery.
            let codec = ToolNameCodec::for_tools(&request.tools);
            // greentic-llm errors are auth/config/transport-class; surface as
            // BadRequest so `RetryingLlmBackend` does not loop on a
            // deterministic failure.
            let response = match provider.chat(chat_request).await {
                Ok(response) => response,
                Err(first) if image_count > 0 && refuses_images(&first) => {
                    // Vision is advertised per PROVIDER, not per model: a model
                    // that takes no images fails the call. Retry ONCE without
                    // them, telling the agent; if that fails too, the original
                    // error is what the caller sees. Only an error that reads
                    // as the request being refused is retried: a rate limit,
                    // an auth failure, a timeout or a server error would only
                    // repeat (and a rate limit would be spent twice).
                    tracing::warn!(
                        code = "images_refused_retry",
                        images = image_count,
                        "provider refused a request carrying images; retrying without them"
                    );
                    append_to_last_user(
                        &mut request.history,
                        &crate::attachments_materialize::images_unseen_note(image_count),
                    );
                    provider
                        .chat(build_chat_request(&request, Vec::new()))
                        .await
                        .map_err(|_| LlmError::BadRequest(first.to_string()))?
                }
                Err(e) => return Err(LlmError::BadRequest(e.to_string())),
            };
            Ok(map_response(response, &codec))
        })
    }
}

impl GreenticLlmBackend {
    /// Materialise the attachments of the LAST user message only; an earlier
    /// user message keeps a fixed marker so a long conversation never fetches
    /// old files again. Mutates the REQUEST's copy of the history, never the
    /// conversation state (the loop hands over a clone), so state keeps
    /// references only. Returns the images for the last user message.
    async fn apply_attachments(
        &self,
        history: &mut [ChatMessage],
        vision: bool,
        memo: &TurnAttachments,
        guard: Option<&dyn AttachmentTextGuard>,
    ) -> Vec<GChatImage> {
        let last_user = history
            .iter()
            .rposition(|m| matches!(m, ChatMessage::User { .. }));
        let mut images = Vec::new();
        for (idx, msg) in history.iter_mut().enumerate() {
            let ChatMessage::User {
                content,
                attachments,
            } = msg
            else {
                continue;
            };
            if attachments.is_empty() {
                continue;
            }
            if Some(idx) == last_user {
                let key = TurnKey {
                    last_user_index: idx,
                    vision,
                };
                let refs: &[_] = attachments;
                let m = memo
                    .get_or_materialize(key, || {
                        let nonce = (self.nonce_source)();
                        async move {
                            materialize(self.artifacts.as_deref(), refs, vision, &nonce, guard)
                                .await
                        }
                    })
                    .await;
                content.push_str(&m.text);
                images = m
                    .images
                    .into_iter()
                    .map(|i| GChatImage {
                        data_base64: i.data_base64,
                        media_type: i.media_type,
                    })
                    .collect();
            } else {
                content.push_str(&format!(
                    "\n[This message had {} attachment(s), handled in an earlier turn; they \
                     are not repeated here.]",
                    attachments.len()
                ));
            }
            attachments.clear();
        }
        images
    }
}

/// Whether `e` can be the model refusing the images it was sent: a provider
/// status of 400, 415 or 422 (rig maps an HTTP status onto
/// [`greentic_llm::LlmError::Status`]), or the provider refusing vision. Never
/// a transport failure or timeout, 401/403/408/429, or a 5xx.
fn refuses_images(e: &greentic_llm::LlmError) -> bool {
    matches!(
        e,
        greentic_llm::LlmError::Status {
            status: 400 | 415 | 422,
            ..
        } | greentic_llm::LlmError::UnsupportedCapability("vision")
    )
}

/// Append `text` to the last user message of `history` (the current turn's).
fn append_to_last_user(history: &mut [ChatMessage], text: &str) {
    if let Some(ChatMessage::User { content, .. }) = history
        .iter_mut()
        .rev()
        .find(|m| matches!(m, ChatMessage::User { .. }))
    {
        content.push_str(text);
    }
}

/// AW [`LlmRequest`] → greentic-llm [`ChatRequest`](greentic_llm::ChatRequest).
/// `images` ride on the last user message (the current turn's).
fn build_chat_request(req: &LlmRequest, images: Vec<GChatImage>) -> GChatRequest {
    let mut messages: Vec<GChatMessage> = Vec::with_capacity(req.history.len() + 1);
    messages.push(text_message(MessageRole::System, req.system_prompt.clone()));
    for msg in &req.history {
        messages.push(map_message(msg));
    }
    if !images.is_empty()
        && let Some(m) = messages
            .iter_mut()
            .rev()
            .find(|m| m.role == MessageRole::User)
    {
        m.images = images;
    }
    let tools: Vec<GToolDef> = req
        .tools
        .iter()
        .map(|t| GToolDef {
            name: wire_tool_name(&t.extension_id, &t.tool_name),
            description: t.description.clone(),
            schema: t.parameters.clone(),
        })
        .collect();
    let tool_choice = (!tools.is_empty()).then(|| "auto".to_string());
    GChatRequest {
        messages,
        tools,
        tool_choice,
        max_tokens: None,
        temperature: None,
    }
}

fn text_message(role: MessageRole, content: String) -> GChatMessage {
    GChatMessage {
        role,
        content,
        images: Vec::new(),
        tool_calls: Vec::new(),
        tool_call_id: None,
    }
}

fn map_message(msg: &ChatMessage) -> GChatMessage {
    match msg {
        ChatMessage::System { content } => text_message(MessageRole::System, content.clone()),
        ChatMessage::User { content, .. } => text_message(MessageRole::User, content.clone()),
        ChatMessage::Assistant {
            content,
            tool_calls,
        } => GChatMessage {
            role: MessageRole::Assistant,
            content: content.clone(),
            images: Vec::new(),
            tool_calls: tool_calls
                .iter()
                .map(|tc| GToolCall {
                    id: tc.call_id.clone(),
                    name: wire_tool_name(&tc.extension_id, &tc.tool_name),
                    arguments: tc.args.clone(),
                })
                .collect(),
            tool_call_id: None,
        },
        ChatMessage::Tool { call_id, content } => GChatMessage {
            role: MessageRole::Tool,
            content: content.to_string(),
            images: Vec::new(),
            tool_calls: Vec::new(),
            tool_call_id: Some(call_id.clone()),
        },
    }
}

/// greentic-llm [`ChatResponse`](greentic_llm::ChatResponse) → AW [`LlmResponse`].
fn map_response(resp: GChatResponse, codec: &ToolNameCodec) -> LlmResponse {
    let content = (!resp.content.trim().is_empty()).then_some(resp.content);
    let tool_calls = resp
        .tool_calls
        .into_iter()
        .map(|tc| {
            let (extension_id, tool_name) = codec.decode(&tc.name);
            ToolCallRecord {
                call_id: tc.id,
                extension_id,
                tool_name,
                args: tc.arguments,
            }
        })
        .collect();
    // greentic-llm surfaces token usage on its ChatResponse (since 1.1.0), so
    // carry it through — this is what makes real per-turn token counts show up
    // in a caller's trace (e.g. the designer Run Demo) instead of zeros.
    let (tokens_in, tokens_out) = resp
        .usage
        .map_or((0, 0), |u| (u.input_tokens as u32, u.output_tokens as u32));
    LlmResponse {
        content,
        tool_calls,
        tokens_in,
        tokens_out,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::config::LlmProviderRef;
    use crate::llm::LlmToolSchema;
    use serde_json::json;

    fn req(history: Vec<ChatMessage>, tools: Vec<LlmToolSchema>) -> LlmRequest {
        LlmRequest {
            system_prompt: "be helpful".into(),
            history,
            tools,
            provider: LlmProviderRef {
                provider: "deepseek".into(),
                model: "deepseek-chat".into(),
                credential_ref: None,
            },
            turn_attachments: Default::default(),
            attachment_text_guard: None,
        }
    }

    #[test]
    fn build_chat_request_prepends_system_and_encodes_tools() {
        let request = req(
            vec![ChatMessage::User {
                content: "hi".into(),
                attachments: Vec::new(),
            }],
            vec![LlmToolSchema {
                extension_id: "greentic.tavily".into(),
                tool_name: "tavily_search".into(),
                description: "search".into(),
                parameters: json!({"type": "object"}),
            }],
        );
        let chat = build_chat_request(&request, Vec::new());
        assert_eq!(chat.messages.len(), 2);
        assert!(matches!(chat.messages[0].role, MessageRole::System));
        assert_eq!(chat.messages[0].content, "be helpful");
        assert!(matches!(chat.messages[1].role, MessageRole::User));
        assert_eq!(chat.tools.len(), 1);
        // Dotted extension id is escaped, not passed through raw.
        assert_eq!(chat.tools[0].name, "greentic_DOT_tavily_FN_tavily_search");
        assert_eq!(chat.tool_choice.as_deref(), Some("auto"));
    }

    #[test]
    fn build_chat_request_omits_tool_choice_without_tools() {
        let chat = build_chat_request(&req(vec![], vec![]), Vec::new());
        assert!(chat.tools.is_empty());
        assert!(chat.tool_choice.is_none());
    }

    #[test]
    fn map_message_maps_assistant_tool_calls_and_tool_results() {
        let assistant = map_message(&ChatMessage::Assistant {
            content: "calling".into(),
            tool_calls: vec![ToolCallRecord {
                call_id: "c1".into(),
                extension_id: "greentic.tavily".into(),
                tool_name: "tavily_search".into(),
                args: json!({"q": "rust"}),
            }],
        });
        assert!(matches!(assistant.role, MessageRole::Assistant));
        assert_eq!(assistant.tool_calls.len(), 1);
        assert_eq!(
            assistant.tool_calls[0].name,
            "greentic_DOT_tavily_FN_tavily_search"
        );

        let tool = map_message(&ChatMessage::Tool {
            call_id: "c1".into(),
            content: json!({"answer": "1.89"}),
        });
        assert!(matches!(tool.role, MessageRole::Tool));
        assert_eq!(tool.tool_call_id.as_deref(), Some("c1"));
    }

    #[tokio::test]
    #[ignore = "live: needs GREENTIC_LLM_API_KEY (DeepSeek)"]
    async fn live_deepseek_completes() {
        let key = std::env::var("GREENTIC_LLM_API_KEY").unwrap_or_default();
        if key.is_empty() {
            eprintln!("SKIP: set GREENTIC_LLM_API_KEY");
            return;
        }
        let backend = GreenticLlmBackend::new(key, None);
        // With tool schemas (mirrors the demo agent) — exercises DeepSeek
        // function-calling through greentic-llm with the _FN_/_DOT_ codec.
        let request = req(
            vec![ChatMessage::User {
                content: "What is the latest stable Rust version? Use tavily_search.".into(),
                attachments: Vec::new(),
            }],
            vec![LlmToolSchema {
                extension_id: "greentic.tavily".into(),
                tool_name: "tavily_search".into(),
                description: "Search the web".into(),
                parameters: json!({
                    "type": "object",
                    "properties": { "query": { "type": "string" } },
                    "required": ["query"]
                }),
            }],
        );
        match backend.complete(request).await {
            Ok(resp) => eprintln!(
                "LIVE OK content={:?} tool_calls={} first={:?}",
                resp.content,
                resp.tool_calls.len(),
                resp.tool_calls
                    .first()
                    .map(|t| (&t.extension_id, &t.tool_name)),
            ),
            Err(e) => panic!("greentic-llm deepseek error: {e}"),
        }
    }

    #[test]
    fn map_response_splits_tool_name_and_blanks_empty_content() {
        let resp = map_response(
            GChatResponse {
                content: "  ".into(),
                tool_calls: vec![GToolCall {
                    id: "c1".into(),
                    name: "greentic_DOT_tavily_FN_tavily_search".into(),
                    arguments: json!({"q": "rust"}),
                }],
                finish_reason: greentic_llm::FinishReason::ToolCalls,
                usage: None,
            },
            &ToolNameCodec::default(),
        );
        assert!(resp.content.is_none(), "whitespace-only content → None");
        assert_eq!(resp.tool_calls.len(), 1);
        assert_eq!(resp.tool_calls[0].extension_id, "greentic.tavily");
        assert_eq!(resp.tool_calls[0].tool_name, "tavily_search");
    }

    #[test]
    fn map_response_carries_token_usage() {
        let resp = map_response(
            GChatResponse {
                content: "hi".into(),
                tool_calls: vec![],
                finish_reason: greentic_llm::FinishReason::Stop,
                usage: Some(greentic_llm::Usage {
                    input_tokens: 42,
                    output_tokens: 17,
                    ..Default::default()
                }),
            },
            &ToolNameCodec::default(),
        );
        assert_eq!(resp.tokens_in, 42);
        assert_eq!(resp.tokens_out, 17);
    }
}

#[cfg(test)]
#[path = "llm_greentic_attachment_tests.rs"]
mod attachment_tests;