velaclaw 0.3.0

Protocol-driven autonomous AI agent runtime with intelligent model selection and multi-model negotiation.
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
//! Protocol-backed provider adapter — **trait bridge only** (VL-EVO-004).
//!
//! Maps VelaClaw's [`Provider`] trait to an existing `AiClient` built by
//! [`ExecutionHandle`](crate::execution::ExecutionHandle). Execution semantics
//! (init, transport retry, telemetry) live in `crate::execution::byok`.
//! 协议适配器仅负责 trait 桥接;执行逻辑归执行层。

use crate::execution::execute_chat_with_retry;
use crate::providers::traits::{
    ChatMessage, ChatRequest, ChatResponse, Provider, ProviderCapabilities, StreamChunk,
    StreamOptions, StreamResult, ToolCall, ToolsPayload,
};
use crate::telemetry::ByokTelemetryReporter;
use crate::tools::ToolSpec;
use async_trait::async_trait;
use futures_util::{stream, StreamExt};
use std::sync::Arc;

pub struct ProtocolBackedProvider {
    client: Arc<ai_lib_rust::AiClient>,
    provider_id: String,
    model_id: String,
    telemetry: Option<Arc<ByokTelemetryReporter>>,
}

impl ProtocolBackedProvider {
    /// Wrap an existing `AiClient` (model bound at construction; no per-request override).
    pub fn from_client(
        client: Arc<ai_lib_rust::AiClient>,
        logical_model_id: &str,
        telemetry: Option<Arc<ByokTelemetryReporter>>,
    ) -> anyhow::Result<Self> {
        let (provider_id, model_id) = crate::execution::split_logical_model_id(logical_model_id)?;
        Ok(Self {
            client,
            provider_id,
            model_id,
            telemetry,
        })
    }

    /// Factory helper for `create_provider` — builds client via execution layer.
    pub fn from_logical_model(provider_id: &str, model_id: &str) -> anyhow::Result<Self> {
        let logical = format!("{provider_id}/{model_id}");
        let client = crate::execution::init_ai_client_sync(&logical)?;
        Self::from_client(client, &logical, None)
    }

    pub fn provider_id(&self) -> &str {
        &self.provider_id
    }

    fn telemetry_ref(&self) -> Option<&ByokTelemetryReporter> {
        self.telemetry.as_deref()
    }

    async fn run_chat(
        &self,
        messages: Vec<ai_lib_rust::Message>,
        temperature: f64,
        tools: Option<Vec<serde_json::Value>>,
    ) -> anyhow::Result<ai_lib_rust::client::UnifiedResponse> {
        execute_chat_with_retry(
            &self.client,
            &self.provider_id,
            &self.model_id,
            messages,
            temperature,
            tools,
            self.telemetry_ref(),
        )
        .await
        .map_err(|e| anyhow::anyhow!("Protocol provider error: {e}"))
    }

    fn convert_messages(messages: &[ChatMessage]) -> Vec<ai_lib_rust::Message> {
        messages
            .iter()
            .map(|m| match m.role.as_str() {
                "system" => ai_lib_rust::Message::system(&m.content),
                "assistant" => ai_lib_rust::Message::assistant(&m.content),
                "tool" => {
                    if let Some(ref id) = m.tool_call_id {
                        ai_lib_rust::Message::tool(id.as_str(), &m.content)
                    } else {
                        ai_lib_rust::Message::user(format!(
                            "[tool role without tool_call_id] {}",
                            m.content
                        ))
                    }
                }
                _ => ai_lib_rust::Message::user(&m.content),
            })
            .collect()
    }

    fn stream_event_to_chunk(event: ai_lib_rust::StreamingEvent) -> Option<StreamChunk> {
        match event {
            ai_lib_rust::StreamingEvent::PartialContentDelta { content, .. } => {
                (!content.is_empty()).then(|| StreamChunk::delta(content).with_token_estimate())
            }
            ai_lib_rust::StreamingEvent::ThinkingDelta { thinking, .. } => (!thinking.is_empty())
                .then(|| {
                    StreamChunk::delta(format!("[thinking] {thinking}")).with_token_estimate()
                }),
            ai_lib_rust::StreamingEvent::ToolCallStarted {
                tool_call_id,
                tool_name,
                index,
            } => Some(StreamChunk::tool_call_started(
                tool_call_id,
                tool_name,
                index,
            )),
            ai_lib_rust::StreamingEvent::PartialToolCall {
                tool_call_id,
                arguments,
                index,
                is_complete,
            } => Some(StreamChunk::tool_call_arguments(
                tool_call_id,
                arguments,
                index,
                is_complete,
            )),
            ai_lib_rust::StreamingEvent::ToolCallEnded {
                tool_call_id,
                index,
            } => Some(StreamChunk::tool_call_ended(tool_call_id, index)),
            ai_lib_rust::StreamingEvent::StreamEnd { .. } => Some(StreamChunk::final_chunk()),
            ai_lib_rust::StreamingEvent::StreamError { error, .. } => Some(StreamChunk::error(
                format!("Protocol stream error: {error}"),
            )),
            ai_lib_rust::StreamingEvent::Metadata { .. }
            | ai_lib_rust::StreamingEvent::FinalCandidate { .. } => None,
        }
    }
}

#[async_trait]
impl Provider for ProtocolBackedProvider {
    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities {
            native_tool_calling: true,
            vision: true,
        }
    }

    async fn chat_with_system(
        &self,
        system_prompt: Option<&str>,
        message: &str,
        _model: &str,
        temperature: f64,
    ) -> anyhow::Result<String> {
        let mut messages = Vec::new();
        if let Some(sys) = system_prompt {
            messages.push(ai_lib_rust::Message::system(sys));
        }
        messages.push(ai_lib_rust::Message::user(message));

        let response = self.run_chat(messages, temperature, None).await?;

        Ok(response.content)
    }

    async fn chat_with_history(
        &self,
        messages: &[ChatMessage],
        _model: &str,
        temperature: f64,
    ) -> anyhow::Result<String> {
        let converted = Self::convert_messages(messages);

        let response = self.run_chat(converted, temperature, None).await?;

        Ok(response.content)
    }

    async fn chat(
        &self,
        request: ChatRequest<'_>,
        _model: &str,
        temperature: f64,
    ) -> anyhow::Result<ChatResponse> {
        let converted = Self::convert_messages(request.messages);

        let tools = request.tools.map(|tools| {
            tools
                .iter()
                .map(|t| {
                    serde_json::json!({
                        "type": "function",
                        "function": {
                            "name": t.name,
                            "description": t.description,
                            "parameters": t.parameters
                        }
                    })
                })
                .collect::<Vec<_>>()
        });

        let response = self.run_chat(converted, temperature, tools).await?;

        Ok(ChatResponse {
            text: Some(response.content),
            tool_calls: response
                .tool_calls
                .into_iter()
                .map(|tc| ToolCall {
                    id: tc.id,
                    name: tc.name,
                    arguments: tc.arguments.to_string(),
                })
                .collect(),
        })
    }

    async fn chat_with_tools(
        &self,
        messages: &[ChatMessage],
        tools: &[serde_json::Value],
        _model: &str,
        temperature: f64,
    ) -> anyhow::Result<ChatResponse> {
        let converted = Self::convert_messages(messages);

        let tools_opt = if tools.is_empty() {
            None
        } else {
            Some(tools.to_vec())
        };

        let response = self.run_chat(converted, temperature, tools_opt).await?;

        Ok(ChatResponse {
            text: Some(response.content),
            tool_calls: response
                .tool_calls
                .into_iter()
                .map(|tc| ToolCall {
                    id: tc.id,
                    name: tc.name,
                    arguments: tc.arguments.to_string(),
                })
                .collect(),
        })
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn stream_chat_with_system(
        &self,
        system_prompt: Option<&str>,
        message: &str,
        _model: &str,
        temperature: f64,
        _options: StreamOptions,
    ) -> stream::BoxStream<'static, StreamResult<StreamChunk>> {
        let mut messages = Vec::new();
        if let Some(sys) = system_prompt {
            messages.push(ai_lib_rust::Message::system(sys));
        }
        messages.push(ai_lib_rust::Message::user(message));

        let client = Arc::clone(&self.client);

        async_stream::try_stream! {
            let mut stream = client.chat()
                .messages(messages)
                .temperature(temperature)
                .stream()
                .execute_stream()
                .await
                .map_err(|e| crate::providers::traits::StreamError::Provider(e.to_string()))?;

            while let Some(event) = stream.next().await {
                match event {
                    Ok(event) => {
                        if let Some(chunk) = Self::stream_event_to_chunk(event) {
                            let done = chunk.is_final;
                            yield chunk;
                            if done {
                                break;
                            }
                        }
                    }
                    Err(e) => {
                        yield StreamChunk::error(e.to_string());
                        break;
                    }
                }
            }
        }
        .boxed()
    }

    fn convert_tools(&self, tools: &[ToolSpec]) -> ToolsPayload {
        let tools_json: Vec<serde_json::Value> = tools
            .iter()
            .map(|t| {
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": t.name,
                        "description": t.description,
                        "parameters": t.parameters
                    }
                })
            })
            .collect();

        ToolsPayload::OpenAI { tools: tools_json }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::traits::StreamToolCallDelta;

    #[test]
    fn test_convert_messages() {
        let messages = vec![
            ChatMessage::system("You are helpful."),
            ChatMessage::user("Hello"),
        ];
        let converted = ProtocolBackedProvider::convert_messages(&messages);
        assert_eq!(converted.len(), 2);
    }

    #[test]
    fn test_convert_messages_tool_role_with_call_id() {
        let messages = vec![ChatMessage::tool_with_call_id("call_1", "result json")];
        let converted = ProtocolBackedProvider::convert_messages(&messages);
        assert_eq!(converted.len(), 1);
        assert_eq!(converted[0].tool_call_id.as_deref(), Some("call_1"));
    }

    #[test]
    fn convert_messages_preserves_multi_turn_tool_conversation() {
        let messages = vec![
            ChatMessage::user("Find the current status"),
            ChatMessage::assistant("I will call a tool."),
            ChatMessage::tool_with_call_id("call_1", r#"{"status":"ok"}"#),
            ChatMessage::assistant("The status is ok."),
        ];
        let converted = ProtocolBackedProvider::convert_messages(&messages);

        assert_eq!(converted.len(), 4);
        assert_eq!(converted[2].tool_call_id.as_deref(), Some("call_1"));

        let serialized = serde_json::to_value(&converted).expect("serialize messages");
        assert_eq!(serialized[0]["role"], "user");
        assert_eq!(serialized[1]["role"], "assistant");
        assert_eq!(serialized[2]["role"], "tool");
        assert_eq!(serialized[3]["role"], "assistant");
    }

    #[test]
    fn stream_event_to_chunk_maps_content_and_thinking() {
        let content = ProtocolBackedProvider::stream_event_to_chunk(
            ai_lib_rust::StreamingEvent::PartialContentDelta {
                content: "hello".to_string(),
                sequence_id: Some(1),
            },
        )
        .expect("content chunk");
        assert_eq!(content.delta, "hello");
        assert!(content.tool_call_delta.is_none());
        assert!(!content.is_final);
        assert!(content.token_count > 0);

        let thinking = ProtocolBackedProvider::stream_event_to_chunk(
            ai_lib_rust::StreamingEvent::ThinkingDelta {
                thinking: "checking".to_string(),
                tool_consideration: None,
            },
        )
        .expect("thinking chunk");
        assert_eq!(thinking.delta, "[thinking] checking");
        assert!(thinking.tool_call_delta.is_none());
    }

    #[test]
    fn stream_event_to_chunk_emits_tool_call_deltas() {
        let started = ProtocolBackedProvider::stream_event_to_chunk(
            ai_lib_rust::StreamingEvent::ToolCallStarted {
                tool_call_id: "call_1".to_string(),
                tool_name: "lookup".to_string(),
                index: Some(0),
            },
        )
        .expect("tool start chunk");
        assert_eq!(
            started.tool_call_delta,
            Some(StreamToolCallDelta::Started {
                id: "call_1".to_string(),
                name: "lookup".to_string(),
                index: Some(0),
            })
        );

        let partial = ProtocolBackedProvider::stream_event_to_chunk(
            ai_lib_rust::StreamingEvent::PartialToolCall {
                tool_call_id: "call_1".to_string(),
                arguments: r#"{"query":"velaclaw"}"#.to_string(),
                index: Some(0),
                is_complete: Some(false),
            },
        )
        .expect("tool arguments chunk");
        assert_eq!(
            partial.tool_call_delta,
            Some(StreamToolCallDelta::Arguments {
                id: "call_1".to_string(),
                arguments: r#"{"query":"velaclaw"}"#.to_string(),
                index: Some(0),
                is_complete: Some(false),
            })
        );

        let ended = ProtocolBackedProvider::stream_event_to_chunk(
            ai_lib_rust::StreamingEvent::ToolCallEnded {
                tool_call_id: "call_1".to_string(),
                index: Some(0),
            },
        )
        .expect("tool end chunk");
        assert_eq!(
            ended.tool_call_delta,
            Some(StreamToolCallDelta::Ended {
                id: "call_1".to_string(),
                index: Some(0),
            })
        );
    }

    #[test]
    fn stream_event_to_chunk_handles_end_and_ignores_metadata() {
        let final_chunk =
            ProtocolBackedProvider::stream_event_to_chunk(ai_lib_rust::StreamingEvent::StreamEnd {
                finish_reason: Some("stop".to_string()),
            })
            .expect("final chunk");
        assert!(final_chunk.is_final);

        let metadata =
            ProtocolBackedProvider::stream_event_to_chunk(ai_lib_rust::StreamingEvent::Metadata {
                usage: Some(serde_json::json!({"input_tokens": 1})),
                finish_reason: None,
                stop_reason: None,
            });
        assert!(metadata.is_none());
    }
}