Generate a canned HTTP response appropriate for the route's protocol. The response is protocol-aware: for `openai_chat_completions` it returns a valid `OpenAI` chat completion JSON, for `anthropic_messages` a valid Anthropic response, etc. The route's `model` field is echoed in the response.
(route: &ResolvedRoute, source_protocol: &str)
| 18 | /// a valid `OpenAI` chat completion JSON, for `anthropic_messages` a valid |
| 19 | /// Anthropic response, etc. The route's `model` field is echoed in the response. |
| 20 | pub fn mock_response(route: &ResolvedRoute, source_protocol: &str) -> ProxyResponse { |
| 21 | tracing::warn!( |
| 22 | endpoint = %route.endpoint, |
| 23 | "Serving mock response — mock:// routes should only be used in development/testing" |
| 24 | ); |
| 25 | |
| 26 | let protocol = if route.protocols.iter().any(|p| p == source_protocol) { |
| 27 | source_protocol |
| 28 | } else { |
| 29 | route.protocols.first().map_or("", String::as_str) |
| 30 | }; |
| 31 | |
| 32 | let body = match protocol { |
| 33 | "openai_chat_completions" => openai_chat_completion_body(&route.model), |
| 34 | "openai_completions" => openai_completion_body(&route.model), |
| 35 | "openai_embeddings" => openai_embeddings_body(&route.model), |
| 36 | "anthropic_messages" => anthropic_messages_body(&route.model), |
| 37 | _ => generic_body(&route.model), |
| 38 | }; |
| 39 | |
| 40 | let body_bytes = bytes::Bytes::from(body); |
| 41 | ProxyResponse { |
| 42 | status: 200, |
| 43 | headers: vec![ |
| 44 | ("content-type".to_string(), "application/json".to_string()), |
| 45 | ("x-openshell-mock".to_string(), "true".to_string()), |
| 46 | ], |
| 47 | body: body_bytes, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn openai_chat_completion_body(model: &str) -> Vec<u8> { |
| 52 | serde_json::to_vec(&serde_json::json!({ |