feat(embedding): explicit OpenAI-compatible semantic channel

- add a transport client for one current backend protocol with loopback http,
  TLS-only remote endpoints, explicit remote consent, disabled redirects,
  bounded timeout/batch/payload/response/dimension and no credential in URLs
- validate responses strictly (count, index order, dimension, finite values,
  non-zero norm, mixed-model rejection) and abort publication when any vector
  is missing so a mixed index is never stored
- embed search units in the same transaction that writes the projection, with
  content-digest/model-digest guarded reuse for incremental compiles; the
  build metadata records endpoint/model/revision/dimension/distance/chunking
- add the semantic query channel behind explicit --semantic using an exact
  cosine scan with a bounded vector budget; default queries stay completely
  offline even when vectors exist
- add a local mock-server integration test proving protocol handling,
  cross-vocabulary recall, offline default behaviour, remote consent rules and
  all-or-nothing failure; real-model quality remains NOT RUN and is not claimed
This commit is contained in:
marqdo-knowledge committed 2026-11-26 02:26:37 +08:00
1 parent 514d9fbd4c
commit 87b9c132aa
20 files changed
+2882 -106

No files matched your search

+13 -3
View File
@@ -77,6 +77,7 @@ pub struct PublishStats {
pub edges: u64,
pub search_units: u64,
pub grams: u64,
pub embeddings: u64,
pub rebuild_generation: u64,
}
@@ -87,6 +88,7 @@ pub struct PublishRequest<'a> {
pub graph: &'a Graph,
pub snapshot: &'a SourceSnapshot,
pub contexts: &'a [ProjectContext],
pub embeddings: Option<&'a crate::embedding::EmbeddingVectors>,
}
/// Open a connection with the mandatory safety pragmas.
@@ -461,7 +463,7 @@ fn write_rows(
(path, format, source_digest, source, title, front_matter_json,
document_role, okf_json, analysis_context_id, module_id,
facts_json, facts_digest, facts_cache_version, diagnostic_status)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'native', NULL, ?7, ?8, ?9, ?10, ?11, ?12)",
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)",
rusqlite::params![
facts.rel_path,
facts.format.as_str(),
@@ -469,6 +471,8 @@ fn write_rows(
file.text.as_bytes(),
facts.document_title,
front_matter,
facts.okf.role.as_str(),
serde_json::to_string(&facts.okf).unwrap_or_else(|_| "null".into()),
facts
.analysis
.as_ref()
@@ -668,10 +672,16 @@ fn write_rows(
insert_diagnostic(transaction, diagnostic)?;
}
let index_stats = index::build_index(transaction, request.graph, &doc_ids, &node_ids)
.map_err(|error| KnowledgeError::environment(code::DATABASE_CORRUPT, error.to_string()))?;
let index_stats = index::build_index(
transaction,
request.graph,
&doc_ids,
&node_ids,
request.embeddings,
)?;
stats.search_units = index_stats.search_units;
stats.grams = index_stats.grams;
stats.embeddings = index_stats.embeddings;
Ok(stats)
}