embacle-server 0.21.1

Unified OpenAI-compatible REST API + MCP server for embacle LLM runners
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
// ABOUTME: Live integration tests exercising real Copilot CLI via embacle-server
// ABOUTME: Gated behind EMBACLE_LIVE_TESTS=1 env var; skipped when copilot binary unavailable
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 dravr.ai

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::str_to_string
)]

use std::env;
use std::sync::Arc;

use axum::body::Body;
use axum::http::{Request, StatusCode};
use embacle::config::CliRunnerType;
use embacle_mcp::ServerState;
use http_body_util::BodyExt;
use tower::ServiceExt;

use embacle_server::router;
use embacle_server::state::AppState;

/// Check whether live tests should run.
/// Returns `true` when `EMBACLE_LIVE_TESTS=1` and the copilot binary is on PATH.
fn skip_unless_live() -> bool {
    let env_set = env::var("EMBACLE_LIVE_TESTS").is_ok_and(|v| v == "1");
    let binary_available = which::which("copilot").is_ok();
    !(env_set && binary_available)
}

/// Build a test app wired to the Copilot provider
fn live_app() -> axum::Router {
    let state = Arc::new(ServerState::new(CliRunnerType::Copilot));
    router::build(AppState::new(state))
}

/// Build a POST /v1/chat/completions request from a JSON body
fn post_completions(body: &serde_json::Value) -> Request<Body> {
    Request::builder()
        .method("POST")
        .uri("/v1/chat/completions")
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_vec(body).expect("serialize")))
        .expect("build request")
}

/// Shared tool definition for `get_weather`
fn tool_definitions() -> serde_json::Value {
    serde_json::json!([
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the current weather for a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "City name, e.g. 'San Francisco'"
                        }
                    },
                    "required": ["location"]
                }
            }
        }
    ])
}

/// Parse SSE-formatted bytes into a list of JSON values.
/// Filters out `[DONE]` sentinel and empty lines.
fn parse_sse_events(bytes: &[u8]) -> Vec<serde_json::Value> {
    let text = String::from_utf8_lossy(bytes);
    text.split("\n\n")
        .filter_map(|block| {
            let block = block.trim();
            if block.is_empty() {
                return None;
            }
            // Each SSE block may have multiple lines; find the data: line
            for line in block.lines() {
                if let Some(data) = line.strip_prefix("data: ") {
                    let data = data.trim();
                    if data == "[DONE]" {
                        return None;
                    }
                    return serde_json::from_str(data).ok();
                }
            }
            None
        })
        .collect()
}

// ============================================================================
// Live Copilot Tests
// ============================================================================

#[tokio::test]
async fn live_copilot_non_streaming_completion() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot",
        "messages": [{"role": "user", "content": "Reply with exactly: hello world"}]
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    assert_eq!(response.status(), StatusCode::OK);

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    // Verify standard OpenAI response structure
    let content = json["choices"][0]["message"]["content"]
        .as_str()
        .expect("choices[0].message.content should be a string");
    assert!(!content.is_empty(), "content should not be empty");

    let model = json["model"]
        .as_str()
        .expect("model field should be a string");
    assert!(
        model.contains("copilot"),
        "model should contain 'copilot', got: {model}"
    );
}

#[tokio::test]
async fn live_copilot_streaming_completion() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot",
        "messages": [{"role": "user", "content": "Reply with exactly: hello"}],
        "stream": true
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    assert_eq!(response.status(), StatusCode::OK);

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();

    // Verify SSE format: raw bytes should contain "data: " lines and end with [DONE]
    let raw = String::from_utf8_lossy(&bytes);
    assert!(
        raw.contains("data: [DONE]"),
        "SSE stream should end with [DONE]"
    );

    let chunks = parse_sse_events(&bytes);
    assert!(!chunks.is_empty(), "should have at least one SSE chunk");

    // First chunk should set role
    let first_delta = &chunks[0]["choices"][0]["delta"];
    assert_eq!(
        first_delta["role"].as_str(),
        Some("assistant"),
        "first chunk delta should have role=assistant"
    );

    // Check if any chunk has finish_reason. The Copilot stream may end
    // without signaling is_final, so the SSE layer may omit finish_reason.
    // This is a known gap tracked separately; warn rather than fail.
    let has_finish_reason = chunks
        .iter()
        .any(|c| c["choices"][0]["finish_reason"].is_string());
    if !has_finish_reason {
        eprintln!(
            "WARNING: no SSE chunk had finish_reason (known gap: Copilot stream may not signal is_final)"
        );
    }
}

#[tokio::test]
async fn live_copilot_streaming_with_tools_returns_sse() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot",
        "messages": [{"role": "user", "content": "What is the weather in San Francisco?"}],
        "tools": tool_definitions(),
        "stream": true
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    // The key assertion: streaming + tools should NOT return 400
    // (downgrade path converts to non-streaming internally)
    assert_eq!(
        response.status(),
        StatusCode::OK,
        "streaming + tools should succeed via downgrade path"
    );

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();

    let raw = String::from_utf8_lossy(&bytes);
    assert!(
        raw.contains("data: "),
        "response should be SSE format with data: lines"
    );
    assert!(
        raw.contains("data: [DONE]"),
        "SSE stream should end with [DONE]"
    );

    let chunks = parse_sse_events(&bytes);
    assert!(!chunks.is_empty(), "should have at least one SSE chunk");

    // First chunk should have role=assistant
    let first_delta = &chunks[0]["choices"][0]["delta"];
    assert_eq!(
        first_delta["role"].as_str(),
        Some("assistant"),
        "first chunk delta should have role=assistant"
    );

    // Note: we intentionally do NOT assert tool_calls presence here.
    // Copilot currently ignores the tool catalog and returns plain text.
    // Once tool forwarding is fixed, add assertions for finish_reason=="tool_calls".
}

#[tokio::test]
async fn live_copilot_non_streaming_with_tools() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot",
        "messages": [{"role": "user", "content": "What is the weather in Paris?"}],
        "tools": tool_definitions()
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    assert_eq!(response.status(), StatusCode::OK);

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    // Verify response has valid structure
    assert!(
        json["choices"][0]["message"].is_object(),
        "response should have choices[0].message"
    );

    let finish_reason = json["choices"][0]["finish_reason"]
        .as_str()
        .expect("should have finish_reason");
    assert!(
        !finish_reason.is_empty(),
        "finish_reason should not be empty"
    );
}

#[tokio::test]
async fn live_copilot_model_override() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot:claude-opus-4.6",
        "messages": [{"role": "user", "content": "Reply with exactly: ok"}]
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    assert_eq!(response.status(), StatusCode::OK);

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();
    let json: serde_json::Value = serde_json::from_slice(&bytes).expect("parse json");

    let model = json["model"]
        .as_str()
        .expect("model field should be a string");
    assert!(
        model.contains("copilot"),
        "model should contain 'copilot', got: {model}"
    );

    let content = json["choices"][0]["message"]["content"]
        .as_str()
        .expect("should have content");
    assert!(!content.is_empty(), "content should not be empty");
}

#[tokio::test]
async fn live_copilot_streaming_chunk_structure() {
    if skip_unless_live() {
        eprintln!("SKIP: EMBACLE_LIVE_TESTS not set or copilot not on PATH");
        return;
    }
    env::remove_var("EMBACLE_API_KEY");

    let body = serde_json::json!({
        "model": "copilot",
        "messages": [{"role": "user", "content": "Reply with exactly: test"}],
        "stream": true
    });

    let response = live_app()
        .oneshot(post_completions(&body))
        .await
        .expect("send request");

    assert_eq!(response.status(), StatusCode::OK);

    let bytes = response
        .into_body()
        .collect()
        .await
        .expect("collect")
        .to_bytes();
    let chunks = parse_sse_events(&bytes);
    assert!(!chunks.is_empty(), "should have at least one SSE chunk");

    // Validate every chunk has the required OpenAI SSE fields
    for (i, chunk) in chunks.iter().enumerate() {
        assert!(
            chunk["id"].is_string(),
            "chunk {i} missing 'id' field: {chunk}"
        );
        assert_eq!(
            chunk["object"].as_str(),
            Some("chat.completion.chunk"),
            "chunk {i} should have object=chat.completion.chunk"
        );
        assert!(
            chunk["created"].is_number(),
            "chunk {i} missing 'created' field: {chunk}"
        );
        assert!(
            chunk["model"].is_string(),
            "chunk {i} missing 'model' field: {chunk}"
        );
        assert!(
            chunk["choices"].is_array(),
            "chunk {i} missing 'choices' array: {chunk}"
        );
    }
}