bamboo-server 2026.7.25

HTTP server and API layer for the Bamboo agent framework
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
use actix_web::http::StatusCode;
use actix_web::{web, App};

use bamboo_llm::api::models::{Content, ContentPart, FunctionCall, Role, ToolCall};

use super::helpers::{build_completion_response, responses_input_to_chat_messages};

#[test]
fn responses_input_string_becomes_single_user_message() {
    let msgs = responses_input_to_chat_messages(serde_json::json!("hi")).unwrap();
    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::User);
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "hi"),
        _ => panic!("expected text content"),
    }
}

#[test]
fn responses_input_array_parses_role_and_content_string() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        { "role": "system", "content": "s" },
        { "role": "user", "content": "u" },
        { "role": "assistant", "content": "a" }
    ]))
    .unwrap();
    assert_eq!(msgs.len(), 3);
    assert_eq!(msgs[0].role, Role::System);
    assert_eq!(msgs[1].role, Role::User);
    assert_eq!(msgs[2].role, Role::Assistant);
}

#[test]
fn responses_input_parts_support_input_text() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
          "role": "user",
          "content": [{ "type": "input_text", "text": "hello" }]
        }
    ]))
    .unwrap();
    assert_eq!(msgs.len(), 1);
    match &msgs[0].content {
        Content::Parts(parts) => {
            assert_eq!(parts.len(), 1);
            match &parts[0] {
                ContentPart::Text { text } => assert_eq!(text, "hello"),
                _ => panic!("expected text part"),
            }
        }
        _ => panic!("expected parts content"),
    }
}

#[test]
fn responses_input_parts_support_output_text() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
          "role": "assistant",
          "content": [{ "type": "output_text", "text": "hello from assistant" }]
        }
    ]))
    .unwrap();
    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Assistant);
    match &msgs[0].content {
        Content::Parts(parts) => {
            assert_eq!(parts.len(), 1);
            match &parts[0] {
                ContentPart::Text { text } => assert_eq!(text, "hello from assistant"),
                _ => panic!("expected text part"),
            }
        }
        _ => panic!("expected parts content"),
    }
}

#[test]
fn responses_input_parts_support_refusal() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
          "role": "assistant",
          "content": [{ "type": "refusal", "refusal": "I can't help with that." }]
        }
    ]))
    .unwrap();
    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Assistant);
    match &msgs[0].content {
        Content::Parts(parts) => {
            assert_eq!(parts.len(), 1);
            match &parts[0] {
                ContentPart::Text { text } => assert_eq!(text, "I can't help with that."),
                _ => panic!("expected text part"),
            }
        }
        _ => panic!("expected parts content"),
    }
}

#[test]
fn build_completion_response_populates_core_openai_fields() {
    let tool_call = ToolCall {
        id: "call_1".to_string(),
        tool_type: "function".to_string(),
        function: FunctionCall {
            name: "read_file".to_string(),
            arguments: r#"{"path":"README.md"}"#.to_string(),
        },
    };

    let response = build_completion_response(
        "hello from assistant".to_string(),
        Some(vec![tool_call.clone()]),
        "gpt-test",
    );

    assert!(response.id.starts_with("chatcmpl-"));
    assert_eq!(response.object.as_deref(), Some("chat.completion"));
    assert_eq!(response.model.as_deref(), Some("gpt-test"));
    assert_eq!(response.choices.len(), 1);
    assert_eq!(response.choices[0].message.role, Role::Assistant);
    assert_eq!(
        response.choices[0].message.tool_calls,
        Some(vec![tool_call])
    );
    assert_eq!(response.choices[0].finish_reason.as_deref(), Some("stop"));

    match &response.choices[0].message.content {
        Content::Text(text) => assert_eq!(text, "hello from assistant"),
        _ => panic!("expected text content"),
    }

    let usage = response.usage.expect("usage should always be present");
    assert_eq!(usage.prompt_tokens, 0);
    assert_eq!(usage.completion_tokens, 0);
    assert_eq!(usage.total_tokens, 0);
}

#[test]
fn responses_input_function_call_item_becomes_assistant_with_tool_calls() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
            "type": "function_call",
            "call_id": "call_abc",
            "name": "search",
            "arguments": "{\"q\":\"test\"}"
        }
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Assistant);

    // content should be empty text
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, ""),
        _ => panic!("expected empty text content"),
    }

    // tool_calls should have exactly one entry
    let tool_calls = msgs[0].tool_calls.as_ref().expect("expected tool_calls");
    assert_eq!(tool_calls.len(), 1);
    assert_eq!(tool_calls[0].id, "call_abc");
    assert_eq!(tool_calls[0].tool_type, "function");
    assert_eq!(tool_calls[0].function.name, "search");
    assert_eq!(tool_calls[0].function.arguments, "{\"q\":\"test\"}");

    assert!(msgs[0].tool_call_id.is_none());
}

#[test]
fn responses_input_function_call_output_item_becomes_tool_result() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
            "type": "function_call_output",
            "call_id": "call_abc",
            "output": "search results here"
        }
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Tool);

    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "search results here"),
        _ => panic!("expected text content with output"),
    }

    assert_eq!(msgs[0].tool_call_id.as_deref(), Some("call_abc"));
    assert!(msgs[0].tool_calls.is_none());
}

#[test]
fn responses_input_mixed_messages_and_function_calls() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        { "role": "system", "content": "You are a helpful assistant." },
        { "role": "user", "content": "Find info about Rust." },
        {
            "type": "function_call",
            "call_id": "call_1",
            "name": "web_search",
            "arguments": "{\"query\":\"Rust programming\"}"
        },
        {
            "type": "function_call_output",
            "call_id": "call_1",
            "output": "Rust is a systems programming language."
        },
        { "role": "assistant", "content": "Here is what I found about Rust." }
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 5);

    // 0: system message
    assert_eq!(msgs[0].role, Role::System);
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "You are a helpful assistant."),
        _ => panic!("expected text"),
    }

    // 1: user message
    assert_eq!(msgs[1].role, Role::User);

    // 2: function_call -> assistant with tool_calls
    assert_eq!(msgs[2].role, Role::Assistant);
    let tc = msgs[2].tool_calls.as_ref().expect("expected tool_calls");
    assert_eq!(tc.len(), 1);
    assert_eq!(tc[0].id, "call_1");
    assert_eq!(tc[0].function.name, "web_search");
    assert_eq!(tc[0].function.arguments, "{\"query\":\"Rust programming\"}");

    // 3: function_call_output -> tool message
    assert_eq!(msgs[3].role, Role::Tool);
    assert_eq!(msgs[3].tool_call_id.as_deref(), Some("call_1"));
    match &msgs[3].content {
        Content::Text(t) => assert_eq!(t, "Rust is a systems programming language."),
        _ => panic!("expected text"),
    }

    // 4: assistant message
    assert_eq!(msgs[4].role, Role::Assistant);
    match &msgs[4].content {
        Content::Text(t) => assert_eq!(t, "Here is what I found about Rust."),
        _ => panic!("expected text"),
    }
}

#[actix_web::test]
async fn openai_config_registers_models_and_completion_routes() {
    let app = actix_web::test::init_service(
        App::new().service(web::scope("/openai/v1").configure(super::config)),
    )
    .await;

    for (method, uri) in [
        ("GET", "/openai/v1/models"),
        ("POST", "/openai/v1/chat/completions"),
        ("POST", "/openai/v1/responses"),
    ] {
        let req = match method {
            "GET" => actix_web::test::TestRequest::get().uri(uri).to_request(),
            "POST" => actix_web::test::TestRequest::post().uri(uri).to_request(),
            _ => unreachable!("unexpected HTTP method"),
        };
        let resp = actix_web::test::call_service(&app, req).await;
        assert_ne!(
            resp.status(),
            StatusCode::NOT_FOUND,
            "expected route to be registered: {uri}"
        );
    }
}

// ── #525: Responses-API tool declarations + input hardening ────────────

#[test]
fn convert_responses_tools_maps_flat_function_tool() {
    let tools: Vec<super::types::ResponsesToolParam> = serde_json::from_value(serde_json::json!([
        {
            "type": "function",
            "name": "get_weather",
            "description": "Get weather",
            "strict": false,
            "parameters": {"type": "object", "properties": {"location": {"type": "string"}}}
        }
    ]))
    .unwrap();

    let schemas = super::helpers::convert_responses_tools(Some(tools)).unwrap();
    assert_eq!(schemas.len(), 1);
    assert_eq!(schemas[0].schema_type, "function");
    assert_eq!(schemas[0].function.name, "get_weather");
    assert_eq!(schemas[0].function.description, "Get weather");
    assert!(schemas[0].function.parameters.get("properties").is_some());
}

#[test]
fn convert_responses_tools_accepts_legacy_nested_shape() {
    let tools: Vec<super::types::ResponsesToolParam> = serde_json::from_value(serde_json::json!([
        {
            "type": "function",
            "function": {"name": "search", "parameters": {"type": "object"}}
        }
    ]))
    .unwrap();

    let schemas = super::helpers::convert_responses_tools(Some(tools)).unwrap();
    assert_eq!(schemas.len(), 1);
    assert_eq!(schemas[0].function.name, "search");
}

#[test]
fn convert_responses_tools_skips_non_function_tool_types() {
    let tools: Vec<super::types::ResponsesToolParam> = serde_json::from_value(serde_json::json!([
        {"type": "web_search"},
        {"type": "function", "name": "search", "parameters": {"type": "object"}}
    ]))
    .unwrap();

    let schemas = super::helpers::convert_responses_tools(Some(tools)).unwrap();
    assert_eq!(schemas.len(), 1, "web_search skipped, function kept");
    assert_eq!(schemas[0].function.name, "search");
}

#[test]
fn convert_responses_tools_rejects_malformed_function_tool() {
    // A function-typed entry matching neither the flat nor the nested shape
    // (no name, no function object) must fail with a targeted message.
    let tools: Vec<super::types::ResponsesToolParam> =
        serde_json::from_value(serde_json::json!([{"type": "function"}])).unwrap();

    let err = super::helpers::convert_responses_tools(Some(tools))
        .expect_err("malformed function tool must be rejected");
    assert!(matches!(err, crate::error::AppError::BadRequest(_)));
}

#[test]
fn responses_input_function_call_output_accepts_content_part_array() {
    // The spec allows `output` to be an array of content parts, which used to
    // silently degrade to "" (#525).
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {
            "type": "function_call_output",
            "call_id": "call_abc",
            "output": [
                {"type": "output_text", "text": "72"},
                {"type": "output_text", "text": "°F"}
            ]
        }
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Tool);
    assert_eq!(msgs[0].tool_call_id.as_deref(), Some("call_abc"));
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "72°F"),
        _ => panic!("expected text content"),
    }
}

#[test]
fn responses_input_skips_unknown_item_types() {
    // Codex round-trips `reasoning` items in `input`; they must be skipped,
    // not turned into empty user messages that pollute the prompt (#525).
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {"type": "reasoning", "id": "rs_1", "summary": []},
        {"role": "user", "content": "hello"}
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 1, "reasoning item skipped");
    assert_eq!(msgs[0].role, Role::User);
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "hello"),
        _ => panic!("expected text content"),
    }
}

#[test]
fn convert_responses_tools_normalizes_missing_parameters_to_empty_schema() {
    // An omitted `parameters` must not forward `parameters: null` upstream.
    let tools: Vec<super::types::ResponsesToolParam> = serde_json::from_value(serde_json::json!([
        {"type": "function", "name": "ping"}
    ]))
    .unwrap();

    let schemas = super::helpers::convert_responses_tools(Some(tools)).unwrap();
    assert_eq!(schemas.len(), 1);
    assert_eq!(
        schemas[0].function.parameters,
        serde_json::json!({"type": "object", "properties": {}})
    );
}

#[test]
fn convert_responses_tools_rejects_entry_without_type() {
    // A tools[] entry with no `type` at all is garbage, not an unsupported
    // feature — fail fast like the old strict parsing did.
    let tools: Vec<super::types::ResponsesToolParam> =
        serde_json::from_value(serde_json::json!([{"name": "my_tool", "parameters": {}}])).unwrap();

    let err = super::helpers::convert_responses_tools(Some(tools))
        .expect_err("type-less tools entry must be rejected");
    assert!(matches!(err, crate::error::AppError::BadRequest(_)));
}

// #525 review: parallel tool calls round-tripped as consecutive function_call
// items must merge into ONE assistant message — one single-call assistant
// message per item is rejected by Chat-Completions upstreams ("assistant
// message with tool_calls must be followed by tool messages responding to
// each tool_call_id").
#[test]
fn responses_input_merges_parallel_function_calls_into_one_assistant_turn() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {"type": "function_call", "call_id": "c1", "name": "read", "arguments": "{\"p\":\"a\"}"},
        {"type": "function_call", "call_id": "c2", "name": "read", "arguments": "{\"p\":\"b\"}"},
        {"type": "function_call_output", "call_id": "c1", "output": "A"},
        {"type": "function_call_output", "call_id": "c2", "output": "B"}
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 3, "one assistant turn + two tool results");
    assert_eq!(msgs[0].role, Role::Assistant);
    let calls = msgs[0].tool_calls.as_ref().expect("tool_calls");
    assert_eq!(calls.len(), 2);
    assert_eq!(calls[0].id, "c1");
    assert_eq!(calls[1].id, "c2");
    assert_eq!(msgs[1].role, Role::Tool);
    assert_eq!(msgs[1].tool_call_id.as_deref(), Some("c1"));
    assert_eq!(msgs[2].role, Role::Tool);
    assert_eq!(msgs[2].tool_call_id.as_deref(), Some("c2"));
}

// A function_call following the assistant's text message from the same turn
// attaches to that message (Chat-Completions puts tool_calls ON the assistant
// message that carries the turn's content).
#[test]
fn responses_input_attaches_function_call_to_preceding_assistant_message() {
    let msgs = responses_input_to_chat_messages(serde_json::json!([
        {"role": "assistant", "content": "Let me check."},
        {"type": "function_call", "call_id": "c1", "name": "search", "arguments": "{}"}
    ]))
    .unwrap();

    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, Role::Assistant);
    match &msgs[0].content {
        Content::Text(t) => assert_eq!(t, "Let me check."),
        _ => panic!("expected text content"),
    }
    let calls = msgs[0].tool_calls.as_ref().expect("tool_calls attached");
    assert_eq!(calls.len(), 1);
    assert_eq!(calls[0].id, "c1");
}