agentyk 0.1.1

Compose agents from values and run them: turn loop, event log, capabilities, MCP, multi-provider LLM drivers.
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! OpenAI Chat Completions driver. Also serves any OpenAI-compatible
//! endpoint (OpenRouter, local runtimes, proxies) via `ModelSpec::base_url`.
//!
//! Wire mapping only — sending, status/transport classification, and SSE
//! framing live in the crate-internal `drivers::http` layer.

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};

use agentyk_core::driver::{
    ChatDriver, ChatRequest, ChatResponse, DeltaSink, DriverId, ModelSpec, Usage,
};
use agentyk_core::error::{Error, LlmErrorKind, Result};
use agentyk_core::message::{ContentPart, Message, Role, ToolCall};

use super::http::{self, HttpProvider, StreamAccumulator, decode};

const DEFAULT_BASE_URL: &str = "https://api.openai.com/v1";

/// Speaks the OpenAI Chat Completions protocol.
///
/// Also serves any compatible endpoint — OpenRouter, local runtimes, proxies
/// — via [`ModelSpec::base_url`]. An api key is optional, since local
/// runtimes routinely need none. Streams real incremental deltas.
pub struct OpenAiDriver {
    id: DriverId,
    client: reqwest::Client,
}

impl OpenAiDriver {
    /// A driver registered as `"openai"`, on a default HTTP client.
    pub fn new() -> Self {
        Self::with_id(DriverId::openai())
    }

    /// Register the same protocol under a different driver id (e.g.
    /// `"openrouter"`) so multiple OpenAI-compatible providers can coexist.
    pub fn with_id(id: DriverId) -> Self {
        Self {
            id,
            client: super::http::client(),
        }
    }

    /// Supply your own client — timeouts, proxies, and connection pooling are
    /// its business, not the driver's.
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.client = client;
        self
    }
}

impl Default for OpenAiDriver {
    fn default() -> Self {
        Self::new()
    }
}

/// OpenAI accepts `content` as either a plain string or an array of typed
/// parts. Send a plain string for the common text-only case (matches what
/// most OpenAI-compatible servers expect and keeps the wire payload small);
/// switch to the array form only when an image is present.
fn wire_content(content: &[ContentPart]) -> Value {
    if content
        .iter()
        .all(|part| matches!(part, ContentPart::Text(_)))
    {
        return Value::String(
            content
                .iter()
                .filter_map(ContentPart::as_text)
                .collect::<Vec<_>>()
                .join(""),
        );
    }
    Value::Array(
        content
            .iter()
            .map(|part| match part {
                ContentPart::Text(text) => json!({"type": "text", "text": text.text}),
                ContentPart::Image(image) => {
                    let url = image.url.clone().unwrap_or_else(|| {
                        format!(
                            "data:{};base64,{}",
                            image.media_type.as_deref().unwrap_or("image/png"),
                            image.base64.as_deref().unwrap_or_default()
                        )
                    });
                    json!({"type": "image_url", "image_url": {"url": url}})
                }
            })
            .collect(),
    )
}

/// Expand one message into the wire messages it needs.
///
/// Almost always exactly one. The exception is a tool result carrying images:
/// the Chat Completions protocol accepts only text in a `tool` message, so the
/// images follow as a user message rather than being dropped. That is a
/// deliberate, visible workaround — the model still sees them, and the
/// transcript says where they came from. Anthropic needs no such trick (see
/// its driver), which is exactly why `ToolOutput::parts` is documented as
/// best-effort per protocol.
fn to_wire_messages(message: &Message) -> Vec<Value> {
    let mut wire = vec![to_wire_message(message)];
    if message.role == Role::Tool {
        let images: Vec<ContentPart> = message
            .content
            .iter()
            .filter(|part| matches!(part, ContentPart::Image(_)))
            .cloned()
            .collect();
        if !images.is_empty() {
            let mut content = vec![ContentPart::text(format!(
                "Images returned by tool call {}:",
                message.tool_call_id.as_deref().unwrap_or("(unknown)")
            ))];
            content.extend(images);
            wire.push(json!({"role": "user", "content": wire_content(&content)}));
        }
    }
    wire
}

fn to_wire_message(message: &Message) -> Value {
    match message.role {
        Role::System => json!({"role": "system", "content": wire_content(&message.content)}),
        Role::User => json!({"role": "user", "content": wire_content(&message.content)}),
        Role::Assistant => {
            let mut wire = json!({"role": "assistant", "content": wire_content(&message.content)});
            if !message.tool_calls.is_empty() {
                wire["tool_calls"] = Value::Array(
                    message
                        .tool_calls
                        .iter()
                        .map(|c| {
                            json!({
                                "id": c.id,
                                "type": "function",
                                "function": {
                                    "name": c.name,
                                    "arguments": c.arguments.to_string(),
                                },
                            })
                        })
                        .collect(),
                );
            }
            wire
        }
        // Text only: images in a tool result travel as a following user
        // message — see `to_wire_messages`.
        Role::Tool => json!({
            "role": "tool",
            "tool_call_id": message.tool_call_id,
            "content": message.text(),
        }),
    }
}

/// Arguments arrive as a JSON *string* on the non-streaming path and as
/// fragments of one on the streaming path; both land here.
pub(crate) fn parse_tool_call_arguments(raw: &Value) -> Value {
    match raw {
        Value::String(s) => serde_json::from_str(s).unwrap_or_else(|_| json!({})),
        other => other.clone(),
    }
}

#[derive(Debug, Default, Deserialize)]
struct WireUsage {
    #[serde(default)]
    prompt_tokens: u64,
    #[serde(default)]
    completion_tokens: u64,
    /// Breakdown of `prompt_tokens`; absent on OpenAI-compatible servers that
    /// don't report caching.
    #[serde(default)]
    prompt_tokens_details: WirePromptDetails,
    /// Breakdown of `completion_tokens`; absent on non-reasoning models.
    #[serde(default)]
    completion_tokens_details: WireCompletionDetails,
}

/// The parts of `prompt_tokens` OpenAI itemizes. Cached tokens are already
/// inside `prompt_tokens` — this is a split, not an addend.
#[derive(Debug, Default, Deserialize)]
struct WirePromptDetails {
    #[serde(default)]
    cached_tokens: u64,
}

/// The parts of `completion_tokens` OpenAI itemizes.
#[derive(Debug, Default, Deserialize)]
struct WireCompletionDetails {
    #[serde(default)]
    reasoning_tokens: u64,
}

impl From<WireUsage> for Usage {
    fn from(usage: WireUsage) -> Self {
        Usage::new(usage.prompt_tokens, usage.completion_tokens)
            // OpenAI caches automatically, so a host that never asked for
            // caching still needs these to price a request correctly.
            .with_cache(usage.prompt_tokens_details.cached_tokens, 0)
            .with_reasoning(usage.completion_tokens_details.reasoning_tokens)
    }
}

#[derive(Debug, Default, Deserialize)]
struct WireFunction {
    #[serde(default)]
    name: String,
    /// A JSON *string*, not an object — whole on the non-streaming path,
    /// fragmented on the streaming one.
    #[serde(default)]
    arguments: String,
}

#[derive(Debug, Deserialize)]
struct WireToolCall {
    #[serde(default)]
    id: String,
    #[serde(default)]
    function: WireFunction,
}

impl From<WireToolCall> for ToolCall {
    fn from(call: WireToolCall) -> Self {
        ToolCall {
            id: call.id,
            name: call.function.name,
            arguments: parse_tool_call_arguments(&Value::String(call.function.arguments)),
        }
    }
}

#[derive(Debug, Deserialize)]
struct ChoiceMessage {
    /// `null` when the model only asked for tools.
    #[serde(default)]
    content: Option<String>,
    #[serde(default)]
    tool_calls: Vec<WireToolCall>,
}

#[derive(Debug, Deserialize)]
struct Choice {
    message: ChoiceMessage,
}

/// A non-streaming Chat Completions response.
///
/// `choices` is required, and an empty one is an error rather than an empty
/// answer: a completion with nowhere to read the reply from is a failure the
/// caller should see, not a blank turn.
#[derive(Debug, Deserialize)]
struct ChatCompletion {
    choices: Vec<Choice>,
    #[serde(default)]
    usage: WireUsage,
}

#[derive(Default)]
struct PartialToolCall {
    id: String,
    name: String,
    arguments: String,
}

/// One streaming chunk. Every field is optional because a chunk carries
/// either deltas or the trailing usage report, never necessarily both.
#[derive(Debug, Deserialize)]
struct StreamChunk {
    #[serde(default)]
    choices: Vec<StreamChoice>,
    #[serde(default)]
    usage: Option<WireUsage>,
}

#[derive(Debug, Deserialize)]
struct StreamChoice {
    #[serde(default)]
    delta: StreamDelta,
}

#[derive(Debug, Default, Deserialize)]
struct StreamDelta {
    #[serde(default)]
    content: Option<String>,
    #[serde(default)]
    tool_calls: Vec<StreamToolCall>,
}

#[derive(Debug, Deserialize)]
struct StreamToolCall {
    /// Which call in the batch this fragment belongs to.
    #[serde(default)]
    index: usize,
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    function: Option<WireFunction>,
}

/// Accumulates a Chat Completions streaming response: `delta.content`
/// fragments concatenate directly; `delta.tool_calls[].function.arguments`
/// fragments are JSON-fragment text keyed by `index` and only parsed once
/// complete, at [`StreamAccumulator::finish`].
#[derive(Default)]
pub(crate) struct OpenAiStream {
    content: String,
    tool_calls: Vec<PartialToolCall>,
    usage: Usage,
}

impl StreamAccumulator for OpenAiStream {
    fn apply(&mut self, data: &str) -> Result<Option<String>> {
        let chunk: StreamChunk = decode("openai", data)?;

        // Usage rides the same stream as the deltas (with
        // `stream_options.include_usage`), typically on a final chunk whose
        // `choices` array is empty.
        if let Some(usage) = chunk.usage {
            self.usage = usage.into();
        }

        let Some(choice) = chunk.choices.into_iter().next() else {
            return Ok(None);
        };

        let mut text_delta = None;
        if let Some(content) = choice.delta.content
            && !content.is_empty()
        {
            self.content.push_str(&content);
            text_delta = Some(content);
        }
        for call in choice.delta.tool_calls {
            while self.tool_calls.len() <= call.index {
                self.tool_calls.push(PartialToolCall::default());
            }
            let entry = &mut self.tool_calls[call.index];
            if let Some(id) = call.id {
                entry.id.push_str(&id);
            }
            if let Some(function) = call.function {
                entry.name.push_str(&function.name);
                entry.arguments.push_str(&function.arguments);
            }
        }
        Ok(text_delta)
    }

    /// `[DONE]` closes an OpenAI stream and is not JSON.
    fn is_terminator(data: &str) -> bool {
        data == "[DONE]"
    }

    fn text(&self) -> &str {
        &self.content
    }

    fn finish(self) -> ChatResponse {
        let tool_calls: Vec<ToolCall> = self
            .tool_calls
            .into_iter()
            .filter(|c| !c.name.is_empty())
            .map(|c| ToolCall {
                id: c.id,
                name: c.name,
                arguments: parse_tool_call_arguments(&Value::String(c.arguments)),
            })
            .collect();
        let message = if tool_calls.is_empty() {
            Message::assistant(self.content)
        } else {
            Message::assistant_with_calls(self.content, tool_calls)
        };
        ChatResponse::new(message, self.usage)
    }
}

impl HttpProvider for OpenAiDriver {
    type Accumulator = OpenAiStream;

    fn label(&self) -> &str {
        "openai"
    }

    fn default_base_url(&self) -> &str {
        DEFAULT_BASE_URL
    }

    fn endpoint(&self) -> &str {
        "/chat/completions"
    }

    /// A key is optional: local runtimes and proxies routinely need none.
    fn authorize(
        &self,
        builder: reqwest::RequestBuilder,
        model: &ModelSpec,
    ) -> Result<reqwest::RequestBuilder> {
        Ok(match &model.api_key {
            Some(key) => builder.bearer_auth(key),
            None => builder,
        })
    }

    fn build_body(&self, request: &ChatRequest) -> Value {
        let mut messages = Vec::new();
        if let Some(system) = &request.system_prompt {
            messages.push(json!({"role": "system", "content": system}));
        }
        messages.extend(request.messages.iter().flat_map(to_wire_messages));

        let mut body = json!({
            "model": request.model.model,
            "messages": messages,
        });
        if let Some(effort) = request
            .model
            .reasoning
            .as_ref()
            .and_then(|r| r.effort.as_deref())
        {
            body["reasoning_effort"] = json!(effort);
        }
        if !request.tools.is_empty() {
            body["tools"] = Value::Array(
                request
                    .tools
                    .iter()
                    .map(|t| {
                        json!({
                            "type": "function",
                            "function": {
                                "name": t.name,
                                "description": t.description,
                                "parameters": t.parameters,
                            },
                        })
                    })
                    .collect(),
            );
        }
        body
    }

    fn enable_streaming(&self, body: &mut Value) {
        body["stream"] = json!(true);
        body["stream_options"] = json!({"include_usage": true});
    }

    fn parse_response(&self, body: &str) -> Result<ChatResponse> {
        let response: ChatCompletion = decode(self.label(), body)?;
        let usage = Usage::from(response.usage);
        let choice = response.choices.into_iter().next().ok_or_else(|| {
            Error::driver(
                LlmErrorKind::Unknown,
                "openai returned a completion with no choices",
            )
        })?;
        let message = Message::assistant_with_calls(
            choice.message.content.unwrap_or_default(),
            choice
                .message
                .tool_calls
                .into_iter()
                .map(Into::into)
                .collect(),
        );
        Ok(ChatResponse::new(message, usage))
    }
}

#[async_trait]
impl ChatDriver for OpenAiDriver {
    fn id(&self) -> DriverId {
        self.id.clone()
    }

    async fn complete(&self, request: ChatRequest) -> Result<ChatResponse> {
        http::complete(self, &self.client, request).await
    }

    async fn complete_streaming(
        &self,
        request: ChatRequest,
        sink: &mut dyn DeltaSink,
    ) -> Result<ChatResponse> {
        http::complete_streaming(self, &self.client, request, sink).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::drivers::http::{RecordingSink, drive_stream};

    fn request(model: ModelSpec) -> ChatRequest {
        ChatRequest::new(model, vec![Message::user("hi")])
    }

    #[test]
    fn arguments_parse_from_string_or_object() {
        assert_eq!(
            parse_tool_call_arguments(&json!("{\"a\":1}")),
            json!({"a": 1})
        );
        assert_eq!(parse_tool_call_arguments(&json!({"a": 1})), json!({"a": 1}));
        assert_eq!(parse_tool_call_arguments(&json!("not json")), json!({}));
    }

    #[test]
    fn text_only_content_serializes_as_plain_string() {
        let message = Message::user("hello");
        let wire = to_wire_message(&message);
        assert_eq!(wire["content"], "hello");
    }

    #[test]
    fn reasoning_effort_is_forwarded_when_set() {
        let body = OpenAiDriver::new().build_body(&request(
            ModelSpec::openai("gpt-5.5").reasoning_effort("high"),
        ));
        assert_eq!(body["reasoning_effort"], "high");
    }

    #[test]
    fn reasoning_effort_omitted_when_unset() {
        let body = OpenAiDriver::new().build_body(&request(ModelSpec::openai("gpt-5.5")));
        assert!(body.get("reasoning_effort").is_none());
    }

    #[test]
    fn image_content_serializes_as_content_part_array() {
        let message = Message::user_multimodal(vec![
            ContentPart::text("what is this?"),
            ContentPart::image_url("https://example.com/cat.png"),
        ]);
        let wire = to_wire_message(&message);
        assert_eq!(wire["content"][0]["type"], "text");
        assert_eq!(wire["content"][1]["type"], "image_url");
        assert_eq!(
            wire["content"][1]["image_url"]["url"],
            "https://example.com/cat.png"
        );
    }

    #[test]
    fn assistant_tool_calls_serialize_arguments_as_string() {
        let message = Message::assistant_with_calls(
            "",
            vec![ToolCall {
                id: "call_0".into(),
                name: "add".into(),
                arguments: json!({"a": 1}),
            }],
        );
        let wire = to_wire_message(&message);
        assert_eq!(wire["tool_calls"][0]["function"]["arguments"], "{\"a\":1}");
    }

    #[test]
    fn parse_response_reads_content_calls_and_usage() {
        let payload = json!({
            "choices": [{"message": {
                "content": "done",
                "tool_calls": [{"id": "c1", "function": {"name": "add", "arguments": "{\"a\":1}"}}],
            }}],
            "usage": {"prompt_tokens": 5, "completion_tokens": 9},
        });
        let response = OpenAiDriver::new()
            .parse_response(&payload.to_string())
            .unwrap();
        assert_eq!(response.message.text(), "done");
        assert_eq!(response.message.tool_calls[0].arguments, json!({"a": 1}));
        assert_eq!(response.usage, Usage::new(5, 9));
    }

    /// OpenAI itemizes cached prompt tokens and reasoning tokens inside the
    /// totals; both are kept as breakdowns so a host can price the request and
    /// see whether the automatic prompt cache hit.
    #[test]
    fn parse_response_keeps_the_cache_and_reasoning_breakdown() {
        let payload = json!({
            "choices": [{"message": {"content": "done"}}],
            "usage": {
                "prompt_tokens": 1200,
                "completion_tokens": 40,
                "prompt_tokens_details": {"cached_tokens": 1024},
                "completion_tokens_details": {"reasoning_tokens": 32},
            },
        });
        let response = OpenAiDriver::new()
            .parse_response(&payload.to_string())
            .unwrap();
        assert_eq!(response.usage.input_tokens, 1200, "totals stay totals");
        assert_eq!(response.usage.cache_read_input_tokens, 1024);
        assert_eq!(response.usage.reasoning_tokens, 32);
    }

    /// A completion with nowhere to read the reply from is a failure the
    /// caller should see, not a blank turn.
    #[test]
    fn parse_response_reports_an_empty_choices_array() {
        let payload = json!({"choices": []}).to_string();
        let error = OpenAiDriver::new().parse_response(&payload).unwrap_err();
        assert!(error.to_string().contains("no choices"), "{error}");
    }

    #[test]
    fn parse_response_reports_a_changed_shape_instead_of_returning_nothing() {
        let payload = json!({"completions": [{"message": {"content": "hi"}}]}).to_string();
        let error = OpenAiDriver::new().parse_response(&payload).unwrap_err();
        let message = error.to_string();
        assert!(
            message.contains("did not match the expected shape"),
            "{message}"
        );
        assert!(message.contains("choices"), "{message}");
        assert!(!error.is_retryable());
    }

    #[tokio::test]
    async fn streaming_reports_deltas_and_returns_the_final_message() {
        let mut sink = RecordingSink::default();
        let response = drive_stream::<OpenAiStream>(
            &[
                "data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n",
                // Split mid-line, to prove reassembly through the real loop.
                "data: {\"choices\":[{\"delta\":{\"conte",
                "nt\":\"lo\"}}]}\n",
                "data: {\"choices\":[{\"delta\":{}}],\"usage\":{\"prompt_tokens\":3,\"completion_tokens\":2}}\n",
                "data: [DONE]\n",
            ],
            &mut sink,
        )
        .await
        .unwrap();

        assert_eq!(sink.deltas, vec!["Hel", "lo"]);
        assert_eq!(sink.accumulated, vec!["Hel", "Hello"]);
        assert_eq!(response.message.text(), "Hello");
        assert_eq!(response.usage, Usage::new(3, 2));
    }

    #[tokio::test]
    async fn streaming_assembles_tool_call_fragments_by_index() {
        let mut sink = RecordingSink::default();
        let response = drive_stream::<OpenAiStream>(
            &[
                "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_0\",\"function\":{\"name\":\"add\",\"arguments\":\"\"}}]}}]}\n",
                "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"a\\\":\"}}]}}]}\n",
                "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"1}\"}}]}}]}\n",
            ],
            &mut sink,
        )
        .await
        .unwrap();

        assert!(sink.deltas.is_empty(), "tool JSON is not answer text");
        assert_eq!(response.message.tool_calls.len(), 1);
        assert_eq!(response.message.tool_calls[0].id, "call_0");
        assert_eq!(response.message.tool_calls[0].name, "add");
        assert_eq!(response.message.tool_calls[0].arguments, json!({"a": 1}));
    }
}