//! Responses over real gRPC: streaming, usage, length, tool calls and stop markers. use crate::harness::{Runtime, Stack, assert_one_terminal, body, sse_events, text_of, usage}; use leleka_backend_api::FinishReason; use leleka_backend_api::scripted::Script; use serde_json::{Value, json}; use strata_control::worker::WorkerState; #[tokio::test(flavor = "multi_thread")] async fn responses_over_real_grpc_stream_and_complete_with_the_engines_usage() { let runtime = Runtime::start(None).await; runtime.operator_loads_the_model().await; let stack = Stack::start("chain-basic", runtime.address).await; stack.wait_state(WorkerState::Ready).await; runtime .engine .push(Script::stop_with(&["hel", "lo ", "жук"], usage(11, 3))); let (status, text) = stack .post("/v1/responses", &body(json!({"stream": true}))) .await; assert_eq!(status, 200, "{text}"); let events = sse_events(&text); assert_one_terminal(&events); assert_eq!(events.last().unwrap()["type"], "response.completed"); let deltas: String = events .iter() .filter(|e| e["type"] == "response.output_text.delta") .map(|e| e["delta"].as_str().unwrap()) .collect(); assert_eq!(deltas, "hello жук"); runtime .engine .push(Script::stop_with(&["done"], usage(11, 1))); let (status, text) = stack.post("/v1/responses", &body(json!({}))).await; assert_eq!(status, 200, "{text}"); let response: Value = serde_json::from_str(&text).unwrap(); assert_eq!(text_of(&response), "done"); assert_eq!(response["usage"]["input_tokens"], 11); // The prompt the engine got through gRPC is the rendered template, and the sampling defaults // stayed unset. let seen = runtime.engine.requests(); assert_eq!( seen[0].prompt, "<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\n\n\n\n\n" ); assert_eq!(seen[0].max_output_tokens, 4096); stack.finish().await; runtime.stop().await; } #[tokio::test(flavor = "multi_thread")] async fn length_tool_call_and_stop_marker_over_real_grpc() { let runtime = Runtime::start(None).await; runtime.operator_loads_the_model().await; let stack = Stack::start("chain-finish", runtime.address).await; stack.wait_state(WorkerState::Ready).await; runtime.engine.push(Script::finishing( &["cut"], FinishReason::Length, usage(5, 1), )); let (_, text) = stack .post( "/v1/responses", &body(json!({"stream": true, "max_output_tokens": 1})), ) .await; let events = sse_events(&text); assert_one_terminal(&events); assert_eq!(events.last().unwrap()["type"], "response.incomplete"); let markup = "\n\n\nvalue\n\n\n"; let pieces: Vec = markup .chars() .collect::>() .chunks(9) .map(|c| c.iter().collect()) .collect(); let refs: Vec<&str> = pieces.iter().map(String::as_str).collect(); runtime.engine.push(Script::stop_with(&refs, usage(30, 20))); let (status, text) = stack .post( "/v1/responses", &body(json!({"tools": [{"type": "function", "name": "lookup", "parameters": {"type": "object", "properties": {"key": {"type": "string"}}, "required": ["key"], "additionalProperties": false}}]})), ) .await; assert_eq!(status, 200, "{text}"); let response: Value = serde_json::from_str(&text).unwrap(); let call = response["output"] .as_array() .unwrap() .iter() .find(|item| item["type"] == "function_call") .unwrap(); assert_eq!(call["name"], "lookup"); assert_eq!( serde_json::from_str::(call["arguments"].as_str().unwrap()).unwrap(), json!({"key": "value"}) ); // The model ends its turn with a marker; the engine would keep going, the marker stops it. runtime .engine .push(Script::hanging_after(&["ok", "<|im_end|>", "junk"])); let (status, text) = stack.post("/v1/responses", &body(json!({}))).await; assert_eq!(status, 200, "{text}"); assert_eq!( text_of(&serde_json::from_str(&text).unwrap()), "ok", "{text}" ); let ids = runtime.engine.cancels(); assert_eq!(ids.len(), 1, "the marker cancelled the runtime generation"); stack.finish().await; runtime.stop().await; }