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
pub use OpenAiChatAdapter;
/// Shared OpenAI wire encode/decode helpers (chat + responses).
pub
use Debug;
use ;
use Value;
use crateProviderError;
use crate;
/// Authentication method for a provider.
///
/// Represents the various ways providers authenticate API requests.
/// The [`ProtocolAdapter::build_auth_headers`] method converts this
/// into the appropriate HTTP headers.
/// A protocol adapter translates between the unified request/response types
/// and a specific provider's API protocol.
///
/// This trait is object-safe so it can be used as `&dyn ProtocolAdapter`
/// or `Box<dyn ProtocolAdapter>`. That means:
/// - No async methods
/// - No generic type parameters
/// - No `impl Trait` return types
///
/// Each variant represents a different API protocol:
/// - OpenAI Chat Completions (`/v1/chat/completions`)
/// - OpenAI Responses (`/v1/responses`)
/// - Anthropic Messages (`/v1/messages`)
/// - Ollama (`/api/chat`)
///
/// # Examples
///
/// ```rust
/// use xz_provider::protocol::{ProtocolAdapter, AuthMethod};
/// use xz_provider::ProviderError;
///
/// # #[derive(Debug)]
/// # struct DummyAdapter;
/// # impl ProtocolAdapter for DummyAdapter {
/// # fn endpoint_path(&self) -> &str { "/v1/test" }
/// # fn build_request_body(&self, _: &xz_provider::CompletionRequest, _: bool) -> Result<serde_json::Value, ProviderError> {
/// # Ok(serde_json::json!({}))
/// # }
/// # fn build_auth_headers(&self, _: &AuthMethod) -> Vec<(String, String)> { vec![] }
/// # fn parse_response(&self, _: &serde_json::Value) -> Result<xz_provider::CompletionResponse, ProviderError> {
/// # Err(ProviderError::Format("not implemented".to_owned()))
/// # }
/// # fn parse_sse_event(&self, _: &str) -> Result<Option<xz_provider::StreamEvent>, ProviderError> {
/// # Ok(None)
/// # }
/// # fn protocol_name(&self) -> &str { "test" }
/// # }
/// let adapter: &dyn ProtocolAdapter = &DummyAdapter;
/// assert_eq!(adapter.protocol_name(), "test");
/// ```