Skip to main content

elph_ai/api/
github_copilot_headers.rs

1use std::collections::HashMap;
2
3use crate::types::{ContentBlock, Message, UserContent};
4
5pub fn infer_copilot_initiator(messages: &[Message]) -> &'static str {
6    match messages.last() {
7        Some(Message::User { .. }) | None => "user",
8        _ => "agent",
9    }
10}
11
12pub fn has_copilot_vision_input(messages: &[Message]) -> bool {
13    messages.iter().any(|msg| match msg {
14        Message::User { content, .. } => match content {
15            UserContent::Blocks(blocks) => blocks.iter().any(|b| matches!(b, ContentBlock::Image { .. })),
16            UserContent::Text(_) => false,
17        },
18        Message::ToolResult { content, .. } => content.iter().any(|b| matches!(b, ContentBlock::Image { .. })),
19        _ => false,
20    })
21}
22
23pub fn build_copilot_dynamic_headers(messages: &[Message], has_images: bool) -> HashMap<String, String> {
24    let mut headers = HashMap::new();
25    headers.insert("X-Initiator".to_string(), infer_copilot_initiator(messages).to_string());
26    headers.insert("Openai-Intent".to_string(), "conversation-edits".to_string());
27    if has_images {
28        headers.insert("Copilot-Vision-Request".to_string(), "true".to_string());
29    }
30    headers
31}