Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(embeddings): add GitHub Copilot embedding provider (#739)

AkitaOnRails1c18d3e6d52e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7a2ad2d3..d23b4eb5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
per-client default. `vertex` is accepted as an alias. `uninstall` now matches
every marker on every client, so an entry pinned this way is still removed
(#735).
+- `AI_MEMORY_EMBEDDING_PROVIDER=copilot` adds GitHub Copilot as an embedding
+ provider, reusing the `copilot` LLM provider's OAuth login and GitHub-token
+ exchange (no separate API key). Defaults to `text-embedding-3-small`,
+ 1536-dim; calls Copilot's `/embeddings` endpoint following the
+ OpenAI-compatible contract Copilot documents for chat (#739).
### Security
- Updated `rustls` 0.23.40 → 0.23.45 for [RUSTSEC-2026-0285](https://rustsec.org/advisories/RUSTSEC-2026-0285),
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index f7f52297..48cbd269 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -1471,10 +1471,11 @@ impl Config {
"google" | "gemini" => EmbedderChoice::Google,
"openai-compat" | "openai_compat" => EmbedderChoice::OpenAiCompat,
"local" => EmbedderChoice::Local,
+ "copilot" => EmbedderChoice::Copilot,
other => {
return Err(LlmError::NotConfigured(format!(
"AI_MEMORY_EMBEDDING_PROVIDER={other} not one of \
- openai|voyage|google|gemini|openai-compat|local|none"
+ openai|voyage|google|gemini|openai-compat|local|copilot|none"
)));
}
};
@@ -1492,6 +1493,7 @@ impl Config {
));
}
EmbedderChoice::Local => ai_memory_llm::LOCAL_MODEL.to_string(),
+ EmbedderChoice::Copilot => ai_memory_llm::COPILOT_DEFAULT_EMBED_MODEL.to_string(),
},
};
let dim = match self.embedding_dim {
@@ -1532,6 +1534,8 @@ impl Config {
.unwrap_or_else(|| SecretString::from(String::new())),
// In-process: no key, ever.
EmbedderChoice::Local => SecretString::from(String::new()),
+ // OAuth-backed: no API key, ever; see `copilot_auth` below.
+ EmbedderChoice::Copilot => SecretString::from(String::new()),
};
let base_url = self.embedding_base_url.clone();
if provider == EmbedderChoice::OpenAiCompat && non_empty(base_url.as_deref()).is_none() {
@@ -1539,6 +1543,17 @@ impl Config {
"AI_MEMORY_EMBEDDING_BASE_URL required for openai-compat embeddings".into(),
));
}
+ // Resolve Copilot auth only when the embedding provider is actually
+ // copilot (invariant 14: auth resolves before construction). Mirrors
+ // exactly how the chat-provider path builds `ProviderAuth::copilot`.
+ let copilot_auth = if provider == EmbedderChoice::Copilot {
+ Some(
+ self.provider_auth(ProviderChoice::Copilot, None)
+ .require_copilot_auth()?,
+ )
+ } else {
+ None
+ };
Ok(Some(EmbedderConfig {
provider,
model,
@@ -1546,6 +1561,7 @@ impl Config {
api_key,
base_url,
models_dir: Some(self.data_dir.join("models")),
+ copilot_auth,
defaulted,
}))
}
@@ -2718,6 +2734,68 @@ mod tests {
));
}
+ #[test]
+ fn copilot_embedding_defaults_model_dim_and_reuses_copilot_auth() {
+ let tmp = TempDir::new().unwrap();
+ let cfg = Config {
+ data_dir: tmp.path().to_path_buf(),
+ embedding_provider: Some("copilot".into()),
+ runtime_env: RuntimeEnv {
+ copilot_github_token: Some(SecretString::from("ghu-test")),
+ ..RuntimeEnv::default()
+ },
+ ..Config::default()
+ };
+
+ let embedder = cfg.embedder_config().unwrap().unwrap();
+ assert_eq!(embedder.provider, EmbedderChoice::Copilot);
+ assert_eq!(embedder.model, "text-embedding-3-small");
+ assert_eq!(embedder.dim, 1536);
+ assert!(embedder.api_key.expose_secret().is_empty());
+ let auth = embedder
+ .copilot_auth
+ .expect("copilot embedder config carries resolved Copilot auth");
+ assert_eq!(auth.token_file, tmp.path().join("auth.json"));
+ assert_eq!(auth.github_token.unwrap().expose_secret(), "ghu-test");
+ }
+
+ #[test]
+ fn copilot_embedding_without_credentials_still_resolves_auth_material() {
+ // `embedder_config` only resolves auth *inputs*, mirroring the chat
+ // provider path (`copilot_provider_uses_data_dir_token_file_and_env_token`);
+ // whether a usable credential exists is checked at construction time
+ // by `CopilotEmbedder::new` (`ai-memory-llm`), not here.
+ let tmp = TempDir::new().unwrap();
+ let cfg = Config {
+ data_dir: tmp.path().to_path_buf(),
+ embedding_provider: Some("copilot".into()),
+ ..Config::default()
+ };
+
+ let embedder = cfg.embedder_config().unwrap().unwrap();
+ let auth = embedder.copilot_auth.expect("copilot auth is resolved");
+ assert!(auth.github_token.is_none());
+ assert!(auth.direct_api_token.is_none());
+ }
+
+ #[test]
+ fn non_copilot_embedding_leaves_copilot_auth_unresolved() {
+ // Auth resolution must be gated on the embedding provider actually
+ // being copilot — resolving it unconditionally would fail closed for
+ // every operator who has not logged into Copilot at all.
+ let cfg = Config {
+ embedding_provider: Some("openai".into()),
+ runtime_env: RuntimeEnv {
+ openai_api_key: Some(SecretString::from("sk-embed-key")),
+ ..RuntimeEnv::default()
+ },
+ ..Config::default()
+ };
+
+ let embedder = cfg.embedder_config().unwrap().unwrap();
+ assert!(embedder.copilot_auth.is_none());
+ }
+
#[test]
fn openai_embedding_does_not_use_llm_api_key_without_custom_base_url() {
let cfg = Config {
diff --git a/crates/ai-memory-llm/src/copilot.rs b/crates/ai-memory-llm/src/copilot.rs
index c091caf9..3d9a4d58 100644
--- a/crates/ai-memory-llm/src/copilot.rs
+++ b/crates/ai-memory-llm/src/copilot.rs
@@ -16,10 +16,14 @@ use tracing::{debug, info};
use crate::auth::CopilotAuth;
use crate::auth_file::{load_entry, now_ms, save_entry};
+use crate::embedding::{
+ Embedder, OPENAI_EMBED_MAX_TOKENS, normalise, parse_openai_embedding_values,
+};
use crate::error::{LlmError, LlmResult};
use crate::openai::{STRUCTURED_OUTPUT_SCHEMA_NAME, enforce_strict_object_schemas};
use crate::provider::LlmProvider;
-use crate::response::{provider_error_body, response_json_limited};
+use crate::response::{provider_error_body, response_json_limited, response_text_limited};
+use crate::text::truncate_for_embedding;
use crate::types::{ChatRequest, ChatResponse, ExtraHeaders, Usage};
/// GitHub Copilot's public OAuth client id used by Copilot clients.
@@ -179,23 +183,20 @@ impl CopilotToken {
}
}
-/// Copilot provider backed by Copilot chat completions.
-pub struct CopilotProvider {
+/// Shared Copilot token-resolution state: the GitHub->Copilot token exchange,
+/// cache, and persistence used by every Copilot-backed client (chat
+/// completions, embeddings). Factored out so both clients call the exact
+/// same `exchange_copilot_token` / `resolve_copilot_api_base_url` path
+/// instead of duplicating the auth chain.
+struct CopilotAuthState {
client: reqwest::Client,
- model: String,
auth: CopilotAuth,
stored: Mutex<CopilotToken>,
timeout: Duration,
- extra_headers: ExtraHeaders,
}
-impl CopilotProvider {
- /// Build a provider from resolved Copilot auth inputs.
- ///
- /// # Errors
- /// Returns [`LlmError::NotConfigured`] when no GitHub/direct token is
- /// available.
- pub fn new(auth: CopilotAuth, model: impl Into<String>) -> LlmResult<Self> {
+impl CopilotAuthState {
+ fn new(auth: CopilotAuth) -> LlmResult<Self> {
let stored = CopilotToken::load(&auth.token_file)?.unwrap_or_default();
if auth.direct_api_token.is_none()
&& auth.github_token.is_none()
@@ -213,32 +214,12 @@ impl CopilotProvider {
.map_err(LlmError::from)?;
Ok(Self {
client,
- model: model.into(),
auth,
stored: Mutex::new(stored),
timeout: Duration::from_secs(crate::DEFAULT_REQUEST_TIMEOUT_SECS),
- extra_headers: ExtraHeaders::default(),
})
}
- /// Override the per-request timeout (default
- /// [`crate::DEFAULT_REQUEST_TIMEOUT_SECS`]). Also bounds the
- /// GitHub token-exchange request.
- #[must_use]
- pub fn with_timeout_secs(mut self, secs: u64) -> Self {
- self.timeout = Duration::from_secs(secs);
- self
- }
-
- /// Attach operator-configured headers to every chat request. The factory
- /// calls this with `ProviderConfig::extra_headers`. Replaces the client's
- /// default `user-agent` when the operator configures one.
- #[must_use]
- pub fn with_extra_headers(mut self, headers: ExtraHeaders) -> Self {
- self.extra_headers = headers;
- self
- }
-
async fn current_token(&self) -> LlmResult<CopilotApiToken> {
if let Some(token) = self.auth.direct_api_token.as_ref() {
return Ok(CopilotApiToken {
@@ -290,15 +271,56 @@ impl CopilotProvider {
.unwrap_or_else(|| DEFAULT_COPILOT_API_BASE_URL.into()),
})
}
+}
+
+/// Copilot provider backed by Copilot chat completions.
+pub struct CopilotProvider {
+ model: String,
+ state: CopilotAuthState,
+ extra_headers: ExtraHeaders,
+}
+
+impl CopilotProvider {
+ /// Build a provider from resolved Copilot auth inputs.
+ ///
+ /// # Errors
+ /// Returns [`LlmError::NotConfigured`] when no GitHub/direct token is
+ /// available.
+ pub fn new(auth: CopilotAuth, model: impl Into<String>) -> LlmResult<Self> {
+ Ok(Self {
+ model: model.into(),
+ state: CopilotAuthState::new(auth)?,
+ extra_headers: ExtraHeaders::default(),
+ })
+ }
+
+ /// Override the per-request timeout (default
+ /// [`crate::DEFAULT_REQUEST_TIMEOUT_SECS`]). Also bounds the
+ /// GitHub token-exchange request.
+ #[must_use]
+ pub fn with_timeout_secs(mut self, secs: u64) -> Self {
+ self.state.timeout = Duration::from_secs(secs);
+ self
+ }
+
+ /// Attach operator-configured headers to every chat request. The factory
+ /// calls this with `ProviderConfig::extra_headers`. Replaces the client's
+ /// default `user-agent` when the operator configures one.
+ #[must_use]
+ pub fn with_extra_headers(mut self, headers: ExtraHeaders) -> Self {
+ self.extra_headers = headers;
+ self
+ }
async fn post(&self, body: &CopilotChatRequest<'_>) -> LlmResult<CopilotChatResponse> {
- let token = self.current_token().await?;
+ let token = self.state.current_token().await?;
let url = format!("{}/chat/completions", token.base_url.trim_end_matches('/'));
debug!(url = %url, "POST copilot chat completions");
let request = self.extra_headers.apply(
- self.client
+ self.state
+ .client
.post(&url)
- .timeout(self.timeout)
+ .timeout(self.state.timeout)
.bearer_auth(token.access.expose_secret())
.headers(copilot_runtime_headers()),
);
@@ -368,6 +390,131 @@ struct CopilotApiToken {
base_url: String,
}
+/// Default embedding model requested from Copilot's `/embeddings` endpoint.
+pub const COPILOT_DEFAULT_EMBED_MODEL: &str = "text-embedding-3-small";
+
+/// Default vector dimensionality for [`COPILOT_DEFAULT_EMBED_MODEL`].
+pub const COPILOT_DEFAULT_EMBED_DIM: u32 = 1536;
+
+#[derive(Debug, Serialize)]
+struct CopilotEmbeddingRequest<'a> {
+ input: &'a str,
+ model: &'a str,
+}
+
+/// GitHub Copilot embedder.
+///
+/// **Caveat:** the Copilot `/embeddings` endpoint path and wire shape are
+/// taken from the OpenAI-compatible contract Copilot documents for chat and
+/// from prior art in other Copilot clients (openclaw#61717), not from a live
+/// test against Copilot here — GitHub does not publish a dedicated
+/// embeddings spec. Default model [`COPILOT_DEFAULT_EMBED_MODEL`]
+/// (`text-embedding-3-small`) and dim [`COPILOT_DEFAULT_EMBED_DIM`] (1536)
+/// follow the same prior art. Reuses the identical GitHub-token ->
+/// short-lived Copilot API token exchange as [`CopilotProvider`] via
+/// [`CopilotAuthState`] — no separate exchange path.
+pub struct CopilotEmbedder {
+ state: CopilotAuthState,
+ model: String,
+ dim: u32,
+}
+
+impl CopilotEmbedder {
+ /// Build an embedder from resolved Copilot auth inputs.
+ ///
+ /// # Errors
+ /// Returns [`LlmError::NotConfigured`] when no GitHub/direct token is
+ /// available.
+ pub fn new(auth: CopilotAuth, model: impl Into<String>, dim: u32) -> LlmResult<Self> {
+ Ok(Self {
+ state: CopilotAuthState::new(auth)?,
+ model: model.into(),
+ dim,
+ })
+ }
+
+ /// Override the per-request timeout (default
+ /// [`crate::DEFAULT_REQUEST_TIMEOUT_SECS`]).
+ #[must_use]
+ pub fn with_timeout_secs(mut self, secs: u64) -> Self {
+ self.state.timeout = Duration::from_secs(secs);
+ self
+ }
+}
+
+#[async_trait]
+impl Embedder for CopilotEmbedder {
+ fn provider(&self) -> &'static str {
+ "copilot"
+ }
+
+ fn model(&self) -> &str {
+ &self.model
+ }
+
+ fn dim(&self) -> u32 {
+ self.dim
+ }
+
+ async fn embed(&self, text: &str) -> LlmResult<Vec<f32>> {
+ let token = self.state.current_token().await?;
+ let input = truncate_for_embedding(text, OPENAI_EMBED_MAX_TOKENS);
+ let url = format!("{}/embeddings", token.base_url.trim_end_matches('/'));
+ debug!(url = %url, model = %self.model, "POST copilot embeddings");
+ let req = CopilotEmbeddingRequest {
+ input: &input,
+ model: &self.model,
+ };
+ let mut attempt = 0u32;
+ loop {
+ let resp = self
+ .state
+ .client
+ .post(&url)
+ .timeout(self.state.timeout)
+ .bearer_auth(token.access.expose_secret())
+ .headers(copilot_runtime_headers())
+ .json(&req)
+ .send()
+ .await
+ .map_err(LlmError::from)?;
+ let status = resp.status();
+ if status.as_u16() == 429 && attempt < 5 {
+ attempt += 1;
+ let delay = Duration::from_secs(2u64.saturating_pow(attempt));
+ debug!(attempt, ?delay, "copilot embeddings rate-limited; retrying");
+ tokio::time::sleep(delay).await;
+ continue;
+ }
+ // Client errors (e.g. input too long) are not retried.
+ if status.as_u16() == 400 {
+ let body = provider_error_body(resp).await;
+ return Err(LlmError::Provider {
+ status: status.as_u16(),
+ body,
+ });
+ }
+ if !status.is_success() {
+ let body = provider_error_body(resp).await;
+ return Err(LlmError::Provider {
+ status: status.as_u16(),
+ body,
+ });
+ }
+ let body = response_text_limited(resp).await?;
+ let values = parse_openai_embedding_values(&body, status.as_u16())?;
+ if values.len() as u32 != self.dim {
+ return Err(LlmError::UnexpectedShape(format!(
+ "expected dim {}, got {}",
+ self.dim,
+ values.len()
+ )));
+ }
+ return Ok(normalise(values));
+ }
+ }
+}
+
#[derive(Debug, Deserialize)]
struct CopilotExchangeResponse {
token: String,
diff --git a/crates/ai-memory-llm/src/embedding.rs b/crates/ai-memory-llm/src/embedding.rs
index e2a8f4f5..f2abc1ba 100644
--- a/crates/ai-memory-llm/src/embedding.rs
+++ b/crates/ai-memory-llm/src/embedding.rs
@@ -27,7 +27,7 @@ use crate::text::{truncate_for_embedding, truncate_with_ellipsis};
/// Conservative per-request input cap for OpenAI-compatible embedding APIs
/// (8192 token server limit; we stay well below with head truncation).
-const OPENAI_EMBED_MAX_TOKENS: usize = 5000;
+pub(crate) const OPENAI_EMBED_MAX_TOKENS: usize = 5000;
/// Provider-agnostic embedding API.
///
@@ -118,7 +118,7 @@ struct OpenAiEmbeddingDatum {
}
/// Parse OpenAI-compatible embedding responses, including OpenRouter error bodies.
-fn parse_openai_embedding_values(body: &str, status: u16) -> LlmResult<Vec<f32>> {
+pub(crate) fn parse_openai_embedding_values(body: &str, status: u16) -> LlmResult<Vec<f32>> {
let v: serde_json::Value = serde_json::from_str(body).map_err(|e| LlmError::Provider {
status,
body: truncate_with_ellipsis(&format!("openai embeddings json: {e}; body={body}"), 1024),
diff --git a/crates/ai-memory-llm/src/factory.rs b/crates/ai-memory-llm/src/factory.rs
index 9b2babab..7ad56ddc 100644
--- a/crates/ai-memory-llm/src/factory.rs
+++ b/crates/ai-memory-llm/src/factory.rs
@@ -9,13 +9,14 @@ use secrecy::{ExposeSecret, SecretString};
use crate::AnthropicProvider;
use crate::CodexProvider;
+use crate::CopilotEmbedder;
use crate::CopilotProvider;
use crate::GeminiProvider;
use crate::OpenAiCompatProvider;
use crate::OpenAiOAuthProvider;
use crate::OpenAiProvider;
use crate::OpenCodeProvider;
-use crate::auth::{AuthRequirement, ProviderAuth};
+use crate::auth::{AuthRequirement, CopilotAuth, ProviderAuth};
use crate::embedding::{Embedder, OpenAiCompatEmbedder, OpenAiEmbedder, VoyageEmbedder};
use crate::error::{LlmError, LlmResult};
use crate::google::GoogleEmbedder;
@@ -157,6 +158,9 @@ pub enum EmbedderChoice {
/// manually; docs/local-embeddings.md).
#[cfg(feature = "local-embeddings")]
Local,
+ /// GitHub Copilot's OpenAI-compatible `/embeddings` endpoint. Reuses the
+ /// Copilot OAuth login (no separate API key).
+ Copilot,
}
impl EmbedderChoice {
@@ -171,6 +175,7 @@ impl EmbedderChoice {
Self::OpenAiCompat => "openai-compat",
#[cfg(feature = "local-embeddings")]
Self::Local => "local",
+ Self::Copilot => "copilot",
}
}
}
@@ -192,6 +197,9 @@ pub struct EmbedderConfig {
pub base_url: Option<String>,
/// `<data_dir>/models/` root, required by the `local` provider.
pub models_dir: Option<std::path::PathBuf>,
+ /// Resolved Copilot auth, required by the `copilot` provider. `None`
+ /// for every other provider.
+ pub copilot_auth: Option<CopilotAuth>,
/// True when no provider was configured and `local` was chosen as
/// the 2.0 default. Best-effort semantics: a defaulted embedder
/// that cannot fetch or load its model degrades to no-embedder with
@@ -252,6 +260,12 @@ pub fn build_embedder(config: EmbedderConfig) -> LlmResult<Arc<dyn Embedder>> {
})?;
Arc::new(crate::local::LocalEmbedder::load(&models_dir)?)
}
+ EmbedderChoice::Copilot => {
+ let auth = config.copilot_auth.ok_or_else(|| {
+ LlmError::NotConfigured("copilot embedding provider requires Copilot auth".into())
+ })?;
+ Arc::new(CopilotEmbedder::new(auth, config.model, config.dim)?)
+ }
};
Ok(arc)
}
@@ -281,6 +295,7 @@ pub fn try_default_embedding_dim(provider: EmbedderChoice, model: &str) -> Optio
(EmbedderChoice::Google, "gemini-embedding-001") => Some(768),
(EmbedderChoice::Google, _) => Some(768),
(EmbedderChoice::OpenAiCompat, _) => None,
+ (EmbedderChoice::Copilot, _) => Some(crate::copilot::COPILOT_DEFAULT_EMBED_DIM),
}
}
diff --git a/crates/ai-memory-llm/src/lib.rs b/crates/ai-memory-llm/src/lib.rs
index 7b9f356c..b9d5b2b2 100644
--- a/crates/ai-memory-llm/src/lib.rs
+++ b/crates/ai-memory-llm/src/lib.rs
@@ -101,7 +101,8 @@ pub use auth::{
};
pub use codex::CodexProvider;
pub use copilot::{
- COPILOT_INTEGRATION_ID, CopilotProvider, CopilotToken, DEFAULT_COPILOT_API_BASE_URL,
+ COPILOT_DEFAULT_EMBED_DIM, COPILOT_DEFAULT_EMBED_MODEL, COPILOT_INTEGRATION_ID,
+ CopilotEmbedder, CopilotProvider, CopilotToken, DEFAULT_COPILOT_API_BASE_URL,
GITHUB_ACCESS_TOKEN_URL, GITHUB_COPILOT_CLIENT_ID, GITHUB_COPILOT_TOKEN_URL,
GITHUB_DEVICE_CODE_URL,
};
diff --git a/crates/ai-memory-llm/tests/suite/copilot_embedder.rs b/crates/ai-memory-llm/tests/suite/copilot_embedder.rs
new file mode 100644
index 00000000..223ed205
--- /dev/null
+++ b/crates/ai-memory-llm/tests/suite/copilot_embedder.rs
@@ -0,0 +1,91 @@
+//! Integration tests for `CopilotEmbedder` against an in-process HTTP mock
+//! (wiremock).
+//!
+//! The load-bearing behaviours: (1) requests hit `POST {base}/embeddings`
+//! carrying the exchanged Copilot API token as a bearer and the Copilot
+//! runtime headers GitHub's API requires (`copilot-integration-id`,
+//! `editor-version`); (2) the request body carries `input` and `model`;
+//! (3) a mocked response is parsed and unit-normalised; (4) the embedder
+//! identifies as provider `copilot`, matching `EmbedderChoice::Copilot`;
+//! (5) a dim mismatch is a hard error.
+//!
+//! A direct Copilot API token (rather than a GitHub token) is configured so
+//! `current_token()` short-circuits before the GitHub token-exchange call —
+//! no live GitHub auth is needed to exercise the embeddings wire contract.
+
+use ai_memory_llm::{
+ CopilotAuth, CopilotEmbedder, Embedder, EmbedderChoice, LlmError, ProviderAuth,
+};
+use secrecy::SecretString;
+use serde_json::json;
+use wiremock::matchers::{header, method, path};
+use wiremock::{Mock, MockServer, Request, ResponseTemplate};
+
+fn direct_auth(base_url: &str) -> CopilotAuth {
+ ProviderAuth::copilot(
+ "/nonexistent/auth.json",
+ None,
+ Some(SecretString::from("copilot-api-token")),
+ Some(base_url.to_string()),
+ )
+ .require_copilot_auth()
+ .expect("direct-token copilot auth resolves")
+}
+
+fn embedding_body(values: Vec<f32>) -> serde_json::Value {
+ json!({
+ "object": "list",
+ "data": [{ "object": "embedding", "index": 0, "embedding": values }],
+ "model": "text-embedding-3-small",
+ "usage": { "prompt_tokens": 1, "total_tokens": 1 },
+ })
+}
+
+#[tokio::test]
+async fn embed_sends_bearer_and_copilot_runtime_headers() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/embeddings"))
+ .and(header("authorization", "Bearer copilot-api-token"))
+ .and(header("copilot-integration-id", "vscode-chat"))
+ .and(header("editor-version", "vscode/1.107.0"))
+ .respond_with(move |req: &Request| {
+ let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
+ assert_eq!(body["input"], "hello world");
+ assert_eq!(body["model"], "text-embedding-3-small");
+ ResponseTemplate::new(200).set_body_json(embedding_body(vec![0.3, 0.4]))
+ })
+ .expect(1)
+ .mount(&server)
+ .await;
+
+ let e = CopilotEmbedder::new(direct_auth(&server.uri()), "text-embedding-3-small", 2)
+ .expect("embedder builds");
+ assert_eq!(e.provider(), "copilot");
+ assert_eq!(e.provider(), EmbedderChoice::Copilot.name());
+ assert_eq!(e.model(), "text-embedding-3-small");
+ assert_eq!(e.dim(), 2);
+
+ let v = e.embed("hello world").await.expect("embed succeeds");
+ assert_eq!(v.len(), 2);
+ let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
+ assert!((norm - 1.0).abs() < 1e-5, "expected unit norm, got {norm}");
+}
+
+#[tokio::test]
+async fn dim_mismatch_is_a_hard_error() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/embeddings"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(embedding_body(vec![0.1, 0.2])))
+ .mount(&server)
+ .await;
+
+ let e = CopilotEmbedder::new(direct_auth(&server.uri()), "text-embedding-3-small", 3)
+ .expect("embedder builds");
+ let err = e.embed("hello").await.expect_err("dim mismatch must error");
+ assert!(
+ matches!(err, LlmError::UnexpectedShape(ref msg) if msg.contains("expected dim 3")),
+ "expected UnexpectedShape, got {err:?}"
+ );
+}
diff --git a/crates/ai-memory-llm/tests/suite/mod.rs b/crates/ai-memory-llm/tests/suite/mod.rs
index 5a50f954..259acac9 100644
--- a/crates/ai-memory-llm/tests/suite/mod.rs
+++ b/crates/ai-memory-llm/tests/suite/mod.rs
@@ -2,6 +2,7 @@
//! test harness (see the `integration` module in `src/lib.rs`), so they cost
//! no extra binary; a new file must be declared below.
+mod copilot_embedder;
mod extra_headers_on_the_wire;
mod fallback_provider;
mod openai_compat_embedder;
diff --git a/crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs b/crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs
index e0d8c3ad..2cd5021b 100644
--- a/crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs
+++ b/crates/ai-memory-llm/tests/suite/openai_compat_embedder.rs
@@ -85,6 +85,7 @@ async fn factory_builds_compat_embedder_and_requires_base_url() {
api_key: SecretString::from(String::new()),
base_url: Some("http://localhost:11434/v1".into()),
models_dir: None,
+ copilot_auth: None,
defaulted: false,
})
.expect("factory builds compat embedder");
@@ -98,6 +99,7 @@ async fn factory_builds_compat_embedder_and_requires_base_url() {
api_key: SecretString::from(String::new()),
base_url: None,
models_dir: None,
+ copilot_auth: None,
defaulted: false,
}) {
Ok(_) => panic!("compat embedder must not build without a base URL"),
@@ -115,6 +117,7 @@ async fn factory_builds_compat_embedder_and_requires_base_url() {
api_key: SecretString::from(String::new()),
base_url: Some("http://localhost:11434/v1".into()),
models_dir: None,
+ copilot_auth: None,
defaulted: false,
});
assert!(
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 4a3f3e3c..b49f29da 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -727,10 +727,10 @@ Copilot chat endpoint.
**Embedder env** (opt-in):
```
-AI_MEMORY_EMBEDDING_PROVIDER openai | voyage | google | gemini | openai-compat
+AI_MEMORY_EMBEDDING_PROVIDER openai | voyage | google | gemini | openai-compat | copilot
AI_MEMORY_EMBEDDING_MODEL e.g. text-embedding-3-small, gemini-embedding-001
AI_MEMORY_EMBEDDING_BASE_URL optional override; required for openai-compat
-AI_MEMORY_EMBEDDING_DIM 1536 (OpenAI), 1024 (Voyage), 768 (Google);
+AI_MEMORY_EMBEDDING_DIM 1536 (OpenAI, Copilot), 1024 (Voyage), 768 (Google);
required explicitly for openai-compat
OPENAI_API_KEY / VOYAGE_API_KEY / GEMINI_API_KEY / GOOGLE_API_KEY
LLM_API_KEY accepted for openai with a custom base URL and as
@@ -753,6 +753,16 @@ no safe shared model or dimensionality default. It sends no authorization header
when both `EMBEDDING_API_KEY` and `LLM_API_KEY` are absent and stores vectors
under the distinct `provider="openai-compat"` identity.
+`copilot` takes no API key at all: it resolves the same `CopilotAuth` as the
+`copilot` LLM provider (`auth login copilot`, `COPILOT_GITHUB_TOKEN`, or
+`GITHUB_COPILOT_API_TOKEN`) and shares its GitHub-token -> short-lived
+Copilot-API-token exchange (`CopilotAuthState` in `ai-memory-llm::copilot`) —
+no separate exchange path. It defaults to `text-embedding-3-small` / dim 1536
+and calls Copilot's `/embeddings` endpoint with the same `vscode-chat`
+integration headers as chat. That endpoint's wire shape follows the
+OpenAI-compatible contract Copilot documents for chat, not a published
+embeddings spec, and is not covered by a live test against Copilot here.
+
## Future work
* **M9.5 - local embeddings via `ort`.** Bundle `bge-small-en-v1.5`
diff --git a/docs/install.md b/docs/install.md
index 29803c51..b6d4fe75 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -1599,6 +1599,7 @@ If you set only the provider, ai-memory picks a sensible default:
| `AI_MEMORY_EMBEDDING_PROVIDER=voyage` | `voyage-3` (1024-dim) | Voyage's current general-purpose recommendation. |
| `AI_MEMORY_EMBEDDING_PROVIDER=google` / `gemini` | `gemini-embedding-001` (768-dim) | Google-hosted embeddings via `embedContent`. Set `GEMINI_API_KEY` (or `GOOGLE_API_KEY`). |
| `AI_MEMORY_EMBEDDING_PROVIDER=openai-compat` | no default — set model, dim, and base URL explicitly | Self-hosted engines (Ollama, LM Studio, vLLM). Keyless by default; `EMBEDDING_API_KEY`, else `LLM_API_KEY`, is sent as a bearer token when present (gateways). Example: `AI_MEMORY_EMBEDDING_BASE_URL=http://localhost:11434/v1`, `AI_MEMORY_EMBEDDING_MODEL=nomic-embed-text`, `AI_MEMORY_EMBEDDING_DIM=768`. Switching an existing `openai`+base-URL setup to `openai-compat` changes the stored `{provider, model, dim}` triple — run `ai-memory embed --force` to re-embed. |
+| `AI_MEMORY_EMBEDDING_PROVIDER=copilot` | `text-embedding-3-small` (1536-dim) | Reuses the `copilot` LLM provider's OAuth login (`ai-memory auth login copilot`, `COPILOT_GITHUB_TOKEN`, or `GITHUB_COPILOT_API_TOKEN`) — no separate API key. Calls Copilot's `/embeddings` endpoint following the OpenAI-compatible contract Copilot documents for chat; that endpoint's exact shape is not covered by a live test against Copilot here, so treat it as needing a real-Copilot smoke test. |
> **What we don't recommend:** reasoning-mode models (Claude with extended
> thinking, GPT-o3, Gemini "thinking" variants) — they burn token budget on
diff --git a/docs/llm-providers.md b/docs/llm-providers.md
index 800115e7..424b68ad 100644
--- a/docs/llm-providers.md
+++ b/docs/llm-providers.md
@@ -160,10 +160,10 @@ hits keep their existing non-RRF ranking. Concurrent provider calls are capped
at four; saturated queries keep their local ranking without waiting.
Embeddings are optional and separate from the LLM provider. Set
-`AI_MEMORY_EMBEDDING_PROVIDER=openai`, `voyage`, `google`/`gemini`, or
-`openai-compat` when you want vector retrieval in addition to FTS5 + entity +
-graph-neighbor retrieval. `openai-compat` targets self-hosted engines
-(Ollama, LM Studio, vLLM): it needs no API key and requires explicit
+`AI_MEMORY_EMBEDDING_PROVIDER=openai`, `voyage`, `google`/`gemini`,
+`openai-compat`, or `copilot` when you want vector retrieval in addition to
+FTS5 + entity + graph-neighbor retrieval. `openai-compat` targets self-hosted
+engines (Ollama, LM Studio, vLLM): it needs no API key and requires explicit
`AI_MEMORY_EMBEDDING_BASE_URL`, `AI_MEMORY_EMBEDDING_MODEL`, and
`AI_MEMORY_EMBEDDING_DIM`. The optional `EMBEDDING_API_KEY` credentials the
embedder alone and is checked before `OPENAI_API_KEY` and `LLM_API_KEY`, so
@@ -172,6 +172,15 @@ hybrid paths apply the same bounded page-authority adjustment after candidate
generation; embeddings improve relevance recall but do not decide which source
is canonical.
+`AI_MEMORY_EMBEDDING_PROVIDER=copilot` reuses the same Copilot OAuth login as
+the `copilot` LLM provider (`ai-memory auth login copilot` or
+`COPILOT_GITHUB_TOKEN`/`GITHUB_COPILOT_API_TOKEN`) — no separate API key.
+It defaults to model `text-embedding-3-small`, dim 1536, and calls Copilot's
+`/embeddings` endpoint following the same OpenAI-compatible contract Copilot
+documents for chat. That endpoint's exact shape is not covered by a live
+test against Copilot here; treat it as needing a real-Copilot smoke test
+before relying on it in production.
+
`AI_MEMORY_EMBEDDING_PROVIDER=local` needs no key and no server at all:
sentence embeddings run in-process (pure-Rust `all-MiniLM-L6-v2`,
384-dim), with the model fetched once into `<data_dir>/models/` under