multi-llm 1.0.0

Unified multi-provider LLM client with support for OpenAI, Anthropic, Ollama, and LMStudio
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
//! Message conversion between unified format and Anthropic API format

use super::types::{
    AnthropicContent, AnthropicContentBlock, AnthropicMessage, CacheControl, SystemMessage,
};
use crate::config::AnthropicConfig;
use crate::logging::log_debug;
use crate::messages::{CacheType, MessageContent, MessageRole, UnifiedMessage};

/// Get TTL string for a cache type (Anthropic uses "ephemeral" type with different TTLs)
fn get_cache_ttl(cache_type: CacheType) -> &'static str {
    match cache_type {
        CacheType::Ephemeral => "5m",
        CacheType::Extended => "1h",
    }
}

/// Convert unified messages to Anthropic format, applying caching properly per Anthropic hierarchy
pub(super) fn transform_unified_messages(
    messages: &[&UnifiedMessage],
    config: &AnthropicConfig,
    enable_caching: bool,
) -> (Vec<SystemMessage>, Vec<AnthropicMessage>) {
    let (mut system_messages, conversation_msg_sources) =
        separate_system_and_conversation_messages(messages);
    let conversation_messages = convert_and_combine_conversation_messages(
        &conversation_msg_sources,
        config,
        enable_caching,
    );
    apply_system_cache_control(&mut system_messages, messages, config, enable_caching);

    (system_messages, conversation_messages)
}

fn separate_system_and_conversation_messages<'a>(
    messages: &'a [&'a UnifiedMessage],
) -> (Vec<SystemMessage>, Vec<&'a UnifiedMessage>) {
    let mut system_messages = Vec::new();
    let mut conversation_msg_sources = Vec::new();

    for msg in messages {
        match msg.role {
            MessageRole::System => {
                system_messages.push(SystemMessage {
                    message_type: "text".to_string(),
                    text: extract_text_content(&msg.content),
                    cache_control: None,
                });
            }
            MessageRole::User | MessageRole::Assistant | MessageRole::Tool => {
                conversation_msg_sources.push(*msg);
            }
        }
    }

    (system_messages, conversation_msg_sources)
}

fn convert_and_combine_conversation_messages(
    conversation_msg_sources: &[&UnifiedMessage],
    config: &AnthropicConfig,
    enable_caching: bool,
) -> Vec<AnthropicMessage> {
    let mut combined_messages: Vec<AnthropicMessage> = Vec::new();

    for (index, msg) in conversation_msg_sources.iter().enumerate() {
        let should_cache =
            determine_cache_decision(msg, index, conversation_msg_sources.len(), enable_caching);
        let anthropic_msg =
            unified_message_to_anthropic_with_cache_control(msg, config, should_cache);

        combine_or_add_message(&mut combined_messages, anthropic_msg);
    }

    combined_messages
}

fn determine_cache_decision(
    msg: &UnifiedMessage,
    index: usize,
    total_messages: usize,
    enable_caching: bool,
) -> bool {
    if msg.attributes.cacheable && enable_caching {
        let should_cache =
            should_place_cache_breakpoint_at_conversation_index(index, total_messages);
        log_debug!(
            provider = "anthropic",
            msg_index = index,
            total_messages = total_messages,
            should_cache = should_cache,
            role = ?msg.role,
            "Cache breakpoint decision for conversation message"
        );
        should_cache
    } else {
        log_debug!(
            provider = "anthropic",
            msg_index = index,
            should_cache = false,
            role = ?msg.role,
            "Message not cacheable or caching disabled"
        );
        false
    }
}

fn combine_or_add_message(
    combined_messages: &mut Vec<AnthropicMessage>,
    anthropic_msg: AnthropicMessage,
) {
    if let Some(last_msg) = combined_messages.last_mut() {
        if last_msg.role == anthropic_msg.role {
            log_debug!(
                provider = "anthropic",
                role = %anthropic_msg.role,
                "Combining consecutive messages with same role"
            );
            merge_content_blocks(&mut last_msg.content, anthropic_msg.content);
        } else {
            combined_messages.push(anthropic_msg);
        }
    } else {
        combined_messages.push(anthropic_msg);
    }
}

fn merge_content_blocks(existing: &mut AnthropicContent, new: AnthropicContent) {
    match (&mut *existing, new) {
        (AnthropicContent::Blocks(existing_blocks), AnthropicContent::Blocks(new)) => {
            existing_blocks.extend(new);
        }
        (AnthropicContent::Blocks(existing_blocks), AnthropicContent::Text(text)) => {
            existing_blocks.push(AnthropicContentBlock::Text {
                text,
                cache_control: None,
            });
        }
        (AnthropicContent::Text(existing_text), AnthropicContent::Text(new_text)) => {
            existing_text.push('\n');
            existing_text.push_str(&new_text);
        }
        (AnthropicContent::Text(existing_text), AnthropicContent::Blocks(blocks)) => {
            let mut all_blocks = vec![AnthropicContentBlock::Text {
                text: existing_text.clone(),
                cache_control: None,
            }];
            all_blocks.extend(blocks);
            *existing = AnthropicContent::Blocks(all_blocks);
        }
    }
}

fn apply_system_cache_control(
    system_messages: &mut [SystemMessage],
    messages: &[&UnifiedMessage],
    config: &AnthropicConfig,
    enable_caching: bool,
) {
    if !enable_caching || system_messages.is_empty() {
        return;
    }

    let system_msgs: Vec<_> = messages
        .iter()
        .filter(|m| m.role == MessageRole::System)
        .collect();

    if let Some(last_cacheable_index) = system_msgs.iter().rposition(|msg| msg.attributes.cacheable)
    {
        if let Some(system_msg) = system_messages.get_mut(last_cacheable_index) {
            // Use message's cache_type if specified, otherwise fall back to config
            let ttl = system_msgs[last_cacheable_index]
                .attributes
                .cache_type
                .map(get_cache_ttl)
                .map(|s| s.to_string())
                .unwrap_or_else(|| config.cache_ttl.clone());

            system_msg.cache_control = Some(CacheControl {
                cache_type: "ephemeral".to_string(),
                ttl: Some(ttl),
            });
        }
    }
}

/// Convert a UnifiedMessage to AnthropicMessage with proper cache control
fn unified_message_to_anthropic_with_cache_control(
    msg: &UnifiedMessage,
    config: &AnthropicConfig,
    should_cache: bool,
) -> AnthropicMessage {
    let role = convert_message_role(&msg.role);
    let content = convert_message_content(&msg.content, &role, msg, config, should_cache);
    AnthropicMessage { role, content }
}

/// Convert UnifiedMessage role to Anthropic role
fn convert_message_role(role: &MessageRole) -> String {
    match role {
        MessageRole::User => "user".to_string(),
        MessageRole::Assistant => "assistant".to_string(),
        MessageRole::Tool => "user".to_string(), // Tool results become user messages in Anthropic
        MessageRole::System => "user".to_string(), // Should not happen here
    }
}

/// Convert MessageContent to AnthropicContent with cache control
fn convert_message_content(
    content: &MessageContent,
    role: &str,
    msg: &UnifiedMessage,
    config: &AnthropicConfig,
    should_cache: bool,
) -> AnthropicContent {
    match content {
        MessageContent::Text(text) => convert_text_content(text, role, msg, config, should_cache),
        MessageContent::Json(value) => convert_json_content(value, msg, config, should_cache),
        MessageContent::ToolCall {
            id,
            name,
            arguments,
        } => convert_tool_call(id, name, arguments),
        MessageContent::ToolResult {
            tool_call_id,
            content,
            is_error,
        } => convert_tool_result(tool_call_id, content, *is_error),
    }
}

/// Convert text content with optional caching
fn convert_text_content(
    text: &str,
    role: &str,
    msg: &UnifiedMessage,
    config: &AnthropicConfig,
    should_cache: bool,
) -> AnthropicContent {
    // NEVER apply cache_control to empty text blocks (Anthropic API rejects this)
    if should_cache && !text.is_empty() {
        // Use message's cache_type if specified, otherwise fall back to config
        let ttl = msg
            .attributes
            .cache_type
            .map(get_cache_ttl)
            .map(|s| s.to_string())
            .unwrap_or_else(|| config.cache_ttl.clone());

        log_debug!(
            provider = "anthropic",
            role = %role,
            text_preview = %text.chars().take(100).collect::<String>(),
            "Applying cache_control to conversation message"
        );
        AnthropicContent::Blocks(vec![AnthropicContentBlock::Text {
            text: text.to_string(),
            cache_control: Some(CacheControl {
                cache_type: "ephemeral".to_string(),
                ttl: Some(ttl),
            }),
        }])
    } else {
        log_debug!(
            provider = "anthropic",
            role = %role,
            text_preview = %text.chars().take(100).collect::<String>(),
            "NOT applying cache_control to conversation message"
        );
        AnthropicContent::Text(text.to_string())
    }
}

/// Convert JSON content with optional caching
fn convert_json_content(
    value: &serde_json::Value,
    msg: &UnifiedMessage,
    config: &AnthropicConfig,
    should_cache: bool,
) -> AnthropicContent {
    let json_text = serde_json::to_string_pretty(value).unwrap_or_default();
    // NEVER apply cache_control to empty text blocks (Anthropic API rejects this)
    if should_cache && !json_text.is_empty() {
        // Use message's cache_type if specified, otherwise fall back to config
        let ttl = msg
            .attributes
            .cache_type
            .map(get_cache_ttl)
            .map(|s| s.to_string())
            .unwrap_or_else(|| config.cache_ttl.clone());

        AnthropicContent::Blocks(vec![AnthropicContentBlock::Text {
            text: json_text,
            cache_control: Some(CacheControl {
                cache_type: "ephemeral".to_string(),
                ttl: Some(ttl),
            }),
        }])
    } else {
        AnthropicContent::Text(json_text)
    }
}

/// Convert tool call to Anthropic format
fn convert_tool_call(id: &str, name: &str, arguments: &serde_json::Value) -> AnthropicContent {
    log_debug!(
        provider = "anthropic",
        tool_id = %id,
        tool_name = %name,
        "Converting ToolCall to Anthropic format for tool result pairing"
    );

    AnthropicContent::Blocks(vec![AnthropicContentBlock::ToolUse {
        id: id.to_string(),
        name: name.to_string(),
        input: arguments.clone(),
    }])
}

/// Convert tool result to Anthropic format
fn convert_tool_result(tool_call_id: &str, content: &str, is_error: bool) -> AnthropicContent {
    log_debug!(
        provider = "anthropic",
        tool_call_id = %tool_call_id,
        is_error = is_error,
        content_len = content.len(),
        content_preview = %if content.len() > 200 {
            format!("{}...", &content[..200])
        } else {
            content.to_string()
        },
        "DEBUG: Converting ToolResult to Anthropic format"
    );

    AnthropicContent::Blocks(vec![AnthropicContentBlock::ToolResult {
        tool_use_id: tool_call_id.to_string(),
        content: if is_error {
            format!("Error: {}", content)
        } else {
            content.to_string()
        },
    }])
}

/// Extract text content from any MessageContent type
fn extract_text_content(content: &MessageContent) -> String {
    match content {
        MessageContent::Text(text) => text.clone(),
        MessageContent::Json(value) => serde_json::to_string_pretty(value).unwrap_or_default(),
        MessageContent::ToolCall {
            name, arguments, ..
        } => {
            format!(
                "Tool call: {} with args: {}",
                name,
                serde_json::to_string(arguments).unwrap_or_default()
            )
        }
        MessageContent::ToolResult {
            content, is_error, ..
        } => {
            if *is_error {
                format!("Error: {}", content)
            } else {
                content.clone()
            }
        }
    }
}

/// Implements 2-breakpoint progressive sliding window strategy for optimal cache utilization
/// Based on testing: achieves significant cache improvement while respecting Anthropic's limits
/// Uses Anthropic's 4 cache block limit: 1 for tools + 1 for system + 2 for conversation sliding window
fn should_place_cache_breakpoint_at_conversation_index(
    index: usize,
    total_messages: usize,
) -> bool {
    // Strategy: Use 2 cache breakpoints that progressively move forward through conversation
    // This ensures cache grows by including more recent conversation history

    match total_messages {
        // For short conversations, cache strategically to build up cache
        1..=2 => {
            // Cache first message to start building cache
            index == 0
        }
        3..=5 => {
            // Cache messages 0, 2 to build cache with good spacing
            index == 0 || index == 2
        }
        _ => {
            // Simple incremental caching strategy: always cache the last 2 messages
            // This mimics the working test pattern where cache grows incrementally
            // Last message becomes "previous last" next time, ensuring cache growth

            if total_messages >= 2 {
                // Cache last 2 messages
                index == total_messages - 1 || index == total_messages - 2
            } else {
                // For single message, cache it
                index == total_messages - 1
            }
        }
    }
}