@cloudgrid-io/mcp 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/package.json +1 -1
  2. package/src/corpus/capability-map.md +44 -15
  3. package/src/corpus/install.md +27 -0
  4. package/src/corpus/templates/semantic-search/AGENTS.md +216 -0
  5. package/src/corpus/templates/semantic-search/README.md +75 -0
  6. package/src/corpus/templates/semantic-search/cloudgrid.yaml +219 -0
  7. package/src/corpus/templates/semantic-search/index.md +108 -0
  8. package/src/corpus/templates/semantic-search/services/backend/requirements.txt +7 -0
  9. package/src/corpus/templates/semantic-search/services/backend/src/app/__init__.py +0 -0
  10. package/src/corpus/templates/semantic-search/services/backend/src/app/answer.py +90 -0
  11. package/src/corpus/templates/semantic-search/services/backend/src/app/auth.py +65 -0
  12. package/src/corpus/templates/semantic-search/services/backend/src/app/config.py +110 -0
  13. package/src/corpus/templates/semantic-search/services/backend/src/app/db.py +89 -0
  14. package/src/corpus/templates/semantic-search/services/backend/src/app/embeddings.py +44 -0
  15. package/src/corpus/templates/semantic-search/services/backend/src/app/indexing.py +223 -0
  16. package/src/corpus/templates/semantic-search/services/backend/src/app/main.py +259 -0
  17. package/src/corpus/templates/semantic-search/services/backend/src/app/search.py +222 -0
  18. package/src/corpus/templates/semantic-search/services/backend/src/app/source.py +259 -0
  19. package/src/corpus/templates/semantic-search/services/backend/src/main.py +12 -0
  20. package/src/corpus/templates/semantic-search/services/refresh/README.md +42 -0
  21. package/src/corpus/templates/semantic-search/services/refresh/requirements.txt +9 -0
  22. package/src/corpus/templates/semantic-search/services/refresh/src/app/__init__.py +0 -0
  23. package/src/corpus/templates/semantic-search/services/refresh/src/app/config.py +110 -0
  24. package/src/corpus/templates/semantic-search/services/refresh/src/app/db.py +89 -0
  25. package/src/corpus/templates/semantic-search/services/refresh/src/app/embeddings.py +44 -0
  26. package/src/corpus/templates/semantic-search/services/refresh/src/app/indexing.py +223 -0
  27. package/src/corpus/templates/semantic-search/services/refresh/src/app/source.py +259 -0
  28. package/src/corpus/templates/semantic-search/services/refresh/src/main.py +45 -0
  29. package/src/corpus/templates/semantic-search/services/web/index.html +12 -0
  30. package/src/corpus/templates/semantic-search/services/web/package-lock.json +1680 -0
  31. package/src/corpus/templates/semantic-search/services/web/package.json +19 -0
  32. package/src/corpus/templates/semantic-search/services/web/src/App.jsx +55 -0
  33. package/src/corpus/templates/semantic-search/services/web/src/api.js +125 -0
  34. package/src/corpus/templates/semantic-search/services/web/src/components/Dashboard.jsx +61 -0
  35. package/src/corpus/templates/semantic-search/services/web/src/components/ManagerLogin.jsx +39 -0
  36. package/src/corpus/templates/semantic-search/services/web/src/components/SearchView.jsx +166 -0
  37. package/src/corpus/templates/semantic-search/services/web/src/components/Settings.jsx +148 -0
  38. package/src/corpus/templates/semantic-search/services/web/src/main.jsx +10 -0
  39. package/src/corpus/templates/semantic-search/services/web/src/styles.css +78 -0
  40. package/src/corpus/templates/semantic-search/services/web/vite.config.js +10 -0
  41. package/src/corpus/workflows/semantic-search.md +142 -0
  42. package/src/tools.js +76 -14
@@ -0,0 +1,108 @@
1
+ # Template: semantic-search (CloudGrid-native document search)
2
+
3
+ A deployable, domain-agnostic **document search** app — index a folder of
4
+ documents (PDF/DOCX/TXT/MD), then search by keyword, by meaning, and by metadata,
5
+ with a grounded answer mode and a manager admin. Two services: a React (Vite)
6
+ static frontend at `/` and a FastAPI backend at `/backend`, backed by
7
+ grid-shared Mongo. This template ships **real, runnable code** — read `AGENTS.md`,
8
+ adapt the naming/metadata to your domain, then deploy.
9
+
10
+ **Hybrid search** = Mongo `$text` (lexical) + in-app NumPy cosine over chunk
11
+ embeddings (semantic) + metadata filters, blended and aggregated to the document
12
+ level with a best-passage highlight.
13
+
14
+ **Key rules (proven by a live build — do not regress these):**
15
+
16
+ 1. **Every service's code lives under `services/<name>/`.** `path:` in
17
+ `cloudgrid.yaml` is the URL mount, NOT the filesystem path. The `web` service
18
+ → `services/web/`; the `backend` service → `services/backend/`.
19
+ 2. **Read the Mongo URL LAZILY** — `os.environ.get("DATABASE_MONGODB_URL") or
20
+ os.environ.get("MONGODB_URL")` inside `get_client()`, never at import time.
21
+ 3. **Health is green with NO secrets set.** No secret is read at import time;
22
+ every secret is gated inside a function. `GET /backend/health` needs only Mongo.
23
+ 4. **`@app.on_event("startup")` calls `db.ensure_indexes()`** so the chunks
24
+ `$text` index exists at boot — `$text` returns `[]` on an empty catalog rather
25
+ than raising `IndexNotFound` (a 500). The `$text` query is also guarded in
26
+ `except OperationFailure` as belt-and-suspenders.
27
+ 5. **No `needs: vector`** — pgvector runtime role-grant is blocked (#1545), so
28
+ embeddings live in the Mongo `chunks` collection (a float array) and are
29
+ cosine-ranked in-app. **The refresh cron IS active** — a Python `type: cron`,
30
+ `run: job` service (`services/refresh/`) reindexes daily at 03:00 UTC, and the
31
+ manager "Refresh now" endpoint reindexes on demand; both call the same
32
+ `indexing.run_sync` (the cron vendors the backend `app/` modules — see AGENTS.md §8).
33
+
34
+ ## File tree
35
+
36
+ ```
37
+ cloudgrid.yaml # name + services(web static+build, backend python, refresh cron) + needs:{database:true}
38
+ services/web/ # React + Vite static frontend (LTR, neutral)
39
+ package.json vite.config.js index.html
40
+ src/main.jsx App.jsx api.js styles.css
41
+ src/components/SearchView.jsx ManagerLogin.jsx Settings.jsx Dashboard.jsx
42
+ services/backend/ # FastAPI backend, mounted at /backend
43
+ requirements.txt
44
+ src/main.py # uvicorn entry (reads PORT; no secret at import)
45
+ src/app/
46
+ main.py # routes + startup ensure_indexes()
47
+ config.py # lazy secret gating (SOURCE_TYPE, EMBEDDINGS_*, ...)
48
+ db.py # lazy Mongo; ensure_indexes(); collections documents+chunks
49
+ search.py # hybrid: $text + cosine + metadata, blended to doc level
50
+ indexing.py # discover -> extract -> chunk -> embed -> store (incremental)
51
+ source.py # pluggable adapter: dropbox | local | url
52
+ embeddings.py # one config point (OpenAI-compatible)
53
+ answer.py # grounded answer + citations (extractive fallback)
54
+ auth.py # manager session auth (MANAGER_PASSWORD_HASH)
55
+ services/refresh/ # type: cron, run: job — scheduled reindex (daily 03:00 UTC)
56
+ requirements.txt README.md
57
+ src/main.py # calls app.indexing.run_sync(triggered_by="cron")
58
+ src/app/ # VENDORED copies: config db source embeddings indexing __init__
59
+ ```
60
+
61
+ ## cloudgrid.yaml (active fields)
62
+
63
+ ```yaml
64
+ # On disk this file is the full-annotated reference (templates/_cloudgrid.yaml.reference)
65
+ # with EVERY field present as a comment; only the fields below are uncommented.
66
+ name: semantic-search
67
+ services:
68
+ web:
69
+ type: static
70
+ path: /
71
+ build:
72
+ command: npm run build
73
+ output: dist
74
+ node_version: "22"
75
+ backend:
76
+ type: python
77
+ path: /backend
78
+ refresh:
79
+ type: cron
80
+ schedule: "0 3 * * *"
81
+ timezone: UTC
82
+ run: job
83
+ needs:
84
+ database: true
85
+ ```
86
+
87
+ > **Capability:** the need is `database: true` — the deployer provisions Mongo and
88
+ > injects `DATABASE_MONGODB_URL` (+ legacy `MONGODB_URL`). NO `needs: vector`
89
+ > (#1545). Three services: `web` (static), `backend` (python), `refresh` (cron).
90
+ > `requires:` is the deprecated v1 alias — don't mix it with `needs:`.
91
+
92
+ ## Configure + deploy
93
+
94
+ 1. `grid init semantic-search` — creates the entity + `.cloudgrid/link.json`.
95
+ 2. `grid secrets set EMBEDDINGS_API_KEY=sk-…` plus the source secrets
96
+ (`SOURCE_TYPE` + Dropbox/local/url config) and `MANAGER_PASSWORD_HASH`
97
+ (sha256 hex of the manager password). See `AGENTS.md`.
98
+ 3. (Optional) `grid dev` to run locally against the injected dev Mongo.
99
+ 4. `grid plug` to deploy — a runtime deploy is **async**: poll status until live,
100
+ then sign in as manager and hit "Refresh now" to index the source.
101
+
102
+ ## Adapt it
103
+
104
+ - Rename the `collection` / `date` metadata to your domain in
105
+ `services/backend/src/app/indexing.py` (`_parse_metadata`).
106
+ - Add a source adapter: subclass `Source` in `source.py`, return it from
107
+ `get_source()`, gate its secrets in `config.py`.
108
+ - Point embeddings at any OpenAI-compatible provider via `EMBEDDINGS_BASE_URL`.
@@ -0,0 +1,7 @@
1
+ fastapi==0.111.0
2
+ uvicorn[standard]==0.30.1
3
+ pymongo==4.8.0
4
+ numpy==1.26.4
5
+ httpx==0.27.0
6
+ pypdf==4.2.0
7
+ python-docx==1.1.2
@@ -0,0 +1,90 @@
1
+ """Grounded answer mode.
2
+
3
+ Composes an answer from the indexed documents ONLY, with citations back to the
4
+ documents used. No outside knowledge. Toggleable from Settings. If the answer LLM
5
+ is not configured, it falls back to an extractive answer (the top passages
6
+ themselves), still grounded and cited — never crashes.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import httpx
11
+
12
+ from . import config, search
13
+
14
+
15
+ def _answer_enabled_from_db() -> bool:
16
+ from . import db
17
+
18
+ doc = db.settings().find_one({"_id": "features"}) or {}
19
+ return bool(doc.get("answer_mode_enabled", True))
20
+
21
+
22
+ def compose_answer(question: str) -> dict:
23
+ """Return a grounded answer + the documents cited. Extractive fallback if no LLM."""
24
+ if not _answer_enabled_from_db():
25
+ return {"answer": None, "citations": [], "note": "Answer mode is disabled."}
26
+
27
+ hits = search.search(question, mode="hybrid", limit=5)
28
+ passages = hits["results"]
29
+ if not passages:
30
+ return {
31
+ "answer": None,
32
+ "citations": [],
33
+ "note": "No indexed material matches this question.",
34
+ }
35
+
36
+ citations = [
37
+ {"document_id": p["document_id"], "title": p["title"], "collection": p["collection"]}
38
+ for p in passages
39
+ ]
40
+
41
+ # Grounded generation only if the LLM key is present; otherwise return an
42
+ # extractive answer built from the retrieved passages.
43
+ key = config.embeddings_api_key()
44
+ if not key:
45
+ extractive = "\n\n".join(
46
+ f"- {p['title']}: {p['best_passage']}" for p in passages[:3]
47
+ )
48
+ return {
49
+ "answer": extractive,
50
+ "citations": citations,
51
+ "mode": "extractive",
52
+ "note": "Grounded generation unavailable (no API key) — showing "
53
+ "the most relevant source passages instead.",
54
+ }
55
+
56
+ context = "\n\n".join(
57
+ f"[{i+1}] {p['title']}:\n{p['best_passage']}" for i, p in enumerate(passages)
58
+ )
59
+ system = (
60
+ "You answer strictly and only from the provided document passages. "
61
+ "Cite sources as [n]. If the passages do not contain the answer, say so. "
62
+ "Never use outside knowledge."
63
+ )
64
+ try:
65
+ resp = httpx.post(
66
+ f"{config.embeddings_base_url()}/chat/completions",
67
+ headers={"Authorization": f"Bearer {key}"},
68
+ json={
69
+ "model": config.answer_model(),
70
+ "messages": [
71
+ {"role": "system", "content": system},
72
+ {"role": "user", "content": f"Question: {question}\n\nSources:\n{context}"},
73
+ ],
74
+ "temperature": 0.2,
75
+ },
76
+ timeout=60.0,
77
+ )
78
+ resp.raise_for_status()
79
+ text = resp.json()["choices"][0]["message"]["content"]
80
+ return {"answer": text, "citations": citations, "mode": "generated", "note": None}
81
+ except Exception as exc: # noqa: BLE001 - degrade to extractive, never crash
82
+ extractive = "\n\n".join(
83
+ f"- {p['title']}: {p['best_passage']}" for p in passages[:3]
84
+ )
85
+ return {
86
+ "answer": extractive,
87
+ "citations": citations,
88
+ "mode": "extractive",
89
+ "note": f"Generation failed ({exc}); showing source passages.",
90
+ }
@@ -0,0 +1,65 @@
1
+ """Server-side manager session auth.
2
+
3
+ Admin functions are enforced on the server, not just hidden in the UI. Sessions
4
+ are opaque tokens stored in Mongo. The password is compared against
5
+ MANAGER_PASSWORD_HASH (sha256 hex). If no hash is configured, admin login is
6
+ refused (never open by default) — but the app still starts and search stays
7
+ public.
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import datetime as dt
12
+ import hashlib
13
+ import secrets
14
+
15
+ from fastapi import Header, HTTPException
16
+
17
+ from . import config, db
18
+
19
+ SESSION_TTL_HOURS = 12
20
+
21
+
22
+ def _hash(password: str) -> str:
23
+ return hashlib.sha256(password.encode("utf-8")).hexdigest()
24
+
25
+
26
+ def login(password: str) -> str:
27
+ configured = config.manager_password_hash()
28
+ if not configured:
29
+ raise HTTPException(
30
+ status_code=503,
31
+ detail="Manager auth is not configured (MANAGER_PASSWORD_HASH unset).",
32
+ )
33
+ if not secrets.compare_digest(_hash(password), configured):
34
+ raise HTTPException(status_code=401, detail="Invalid password.")
35
+ token = secrets.token_urlsafe(32)
36
+ db.get_db().sessions.insert_one({
37
+ "_id": token,
38
+ "created_at": dt.datetime.now(dt.timezone.utc),
39
+ "expires_at": dt.datetime.now(dt.timezone.utc) + dt.timedelta(hours=SESSION_TTL_HOURS),
40
+ })
41
+ return token
42
+
43
+
44
+ def require_manager(authorization: str | None = Header(default=None)) -> str:
45
+ """FastAPI dependency: validate the Bearer session token."""
46
+ if not authorization or not authorization.lower().startswith("bearer "):
47
+ raise HTTPException(status_code=401, detail="Manager session required.")
48
+ token = authorization.split(" ", 1)[1].strip()
49
+ session = db.get_db().sessions.find_one({"_id": token})
50
+ if not session:
51
+ raise HTTPException(status_code=401, detail="Invalid session.")
52
+ if session["expires_at"] < dt.datetime.now(dt.timezone.utc):
53
+ db.get_db().sessions.delete_one({"_id": token})
54
+ raise HTTPException(status_code=401, detail="Session expired.")
55
+ return token
56
+
57
+
58
+ def change_password(current: str, new: str) -> str:
59
+ """Self-contained password change. Returns the NEW hash for the manager to
60
+ store as the MANAGER_PASSWORD_HASH secret (we never persist the plaintext or
61
+ the secret ourselves)."""
62
+ configured = config.manager_password_hash()
63
+ if configured and not secrets.compare_digest(_hash(current), configured):
64
+ raise HTTPException(status_code=401, detail="Current password is incorrect.")
65
+ return _hash(new)
@@ -0,0 +1,110 @@
1
+ """Central configuration and secret gating.
2
+
3
+ CRITICAL CloudGrid rule: NEVER read a secret at module import time. Every value
4
+ here is resolved lazily inside a function, so the process starts and passes
5
+ `GET /backend/health` even when NO secret is set. Search and indexing degrade
6
+ gracefully (empty results + a clear "not indexed / missing secret" note) rather
7
+ than crashing.
8
+
9
+ The Mongo connection is the ONLY hard dependency for health, and it is injected
10
+ automatically by CloudGrid (`needs: database`) as DATABASE_MONGODB_URL.
11
+
12
+ Two pluggable config points:
13
+ - SOURCE_TYPE selects the document-source adapter (dropbox | local | url).
14
+ - EMBEDDINGS_* selects the embeddings provider (OpenAI-compatible by default).
15
+ """
16
+ from __future__ import annotations
17
+
18
+ import os
19
+
20
+
21
+ # ── Mongo (injected by the grid — the only health-critical config) ──────────
22
+
23
+ def mongo_url() -> str | None:
24
+ """Injected by CloudGrid `needs: database`. Never set by hand."""
25
+ return os.environ.get("DATABASE_MONGODB_URL") or os.environ.get("MONGODB_URL")
26
+
27
+
28
+ # ── Embeddings (one config point, gated) ─────────────────────────────────────
29
+
30
+ def embeddings_api_key() -> str | None:
31
+ return os.environ.get("EMBEDDINGS_API_KEY")
32
+
33
+
34
+ def embeddings_model() -> str:
35
+ # Changing the model changes vector dimensions and requires a full reindex.
36
+ return os.environ.get("EMBEDDINGS_MODEL", "text-embedding-3-small")
37
+
38
+
39
+ def embeddings_base_url() -> str:
40
+ # Any OpenAI-compatible endpoint. Default is OpenAI.
41
+ return os.environ.get("EMBEDDINGS_BASE_URL", "https://api.openai.com/v1")
42
+
43
+
44
+ def embeddings_configured() -> bool:
45
+ return bool(embeddings_api_key())
46
+
47
+
48
+ def answer_model() -> str:
49
+ return os.environ.get("ANSWER_MODEL", "gpt-4o-mini")
50
+
51
+
52
+ # ── Source adapter (one config point, gated) ─────────────────────────────────
53
+
54
+ def source_type() -> str:
55
+ """dropbox | local | url. Selects the document-source adapter."""
56
+ return os.environ.get("SOURCE_TYPE", "dropbox")
57
+
58
+
59
+ def dropbox_config() -> dict[str, str | None]:
60
+ return {
61
+ "app_key": os.environ.get("DROPBOX_APP_KEY"),
62
+ "app_secret": os.environ.get("DROPBOX_APP_SECRET"),
63
+ "refresh_token": os.environ.get("DROPBOX_REFRESH_TOKEN"),
64
+ "folder_path": os.environ.get("DROPBOX_FOLDER_PATH", ""),
65
+ }
66
+
67
+
68
+ def local_source_path() -> str:
69
+ """Directory the local adapter reads. Point at a `needs: disk` mount
70
+ (DISK_PATH) or a bundled folder."""
71
+ return os.environ.get("LOCAL_SOURCE_PATH", os.environ.get("DISK_PATH", ""))
72
+
73
+
74
+ def url_source_manifest() -> str | None:
75
+ """A newline- or comma-separated list of document URLs the url adapter
76
+ fetches. Set as a secret or plain env var."""
77
+ return os.environ.get("URL_SOURCE_MANIFEST")
78
+
79
+
80
+ def source_configured() -> bool:
81
+ st = source_type()
82
+ if st == "dropbox":
83
+ c = dropbox_config()
84
+ return bool(c["app_key"] and c["app_secret"] and c["refresh_token"])
85
+ if st == "local":
86
+ return bool(local_source_path())
87
+ if st == "url":
88
+ return bool(url_source_manifest())
89
+ return False
90
+
91
+
92
+ # ── Manager auth (gated) ──────────────────────────────────────────────────────
93
+
94
+ def manager_password_hash() -> str | None:
95
+ """A sha256 hex digest of the manager password. Compared server-side."""
96
+ return os.environ.get("MANAGER_PASSWORD_HASH")
97
+
98
+
99
+ def manager_auth_configured() -> bool:
100
+ return bool(manager_password_hash())
101
+
102
+
103
+ # ── Search blend weights ─────────────────────────────────────────────────────
104
+
105
+ def semantic_weight() -> float:
106
+ return float(os.environ.get("SEMANTIC_WEIGHT", "0.55"))
107
+
108
+
109
+ def keyword_weight() -> float:
110
+ return float(os.environ.get("KEYWORD_WEIGHT", "0.45"))
@@ -0,0 +1,89 @@
1
+ """Lazy MongoDB access.
2
+
3
+ The client and connection string are resolved LAZILY (inside get_client), never
4
+ at module top level — so importing this module never touches the network and
5
+ never requires the env var to exist. CloudGrid injects DATABASE_MONGODB_URL at
6
+ runtime from `needs: database`.
7
+
8
+ Collections:
9
+ documents — one row per source document (title, collection tag, date, ...).
10
+ chunks — one embeddable passage per document (content + embedding array).
11
+ settings — feature flags, synonyms, catalog metadata (last_refresh).
12
+ usage_events, index_reports, sessions — telemetry / admin.
13
+ """
14
+ from __future__ import annotations
15
+
16
+ from typing import Optional
17
+
18
+ from pymongo import ASCENDING, MongoClient, TEXT
19
+ from pymongo.database import Database
20
+
21
+ from . import config
22
+
23
+ _client: Optional[MongoClient] = None
24
+
25
+
26
+ def get_client() -> MongoClient:
27
+ global _client
28
+ if _client is None:
29
+ uri = config.mongo_url()
30
+ if not uri:
31
+ raise RuntimeError(
32
+ "DATABASE_MONGODB_URL is not set. CloudGrid injects it "
33
+ "automatically from `needs: database` — run with `grid dev` "
34
+ "locally or deploy with `grid plug`. Do not set it by hand."
35
+ )
36
+ # Short server-selection timeout so health checks fail fast, not hang.
37
+ _client = MongoClient(uri, serverSelectionTimeoutMS=3000)
38
+ return _client
39
+
40
+
41
+ def get_db() -> Database:
42
+ # Default DB name comes from the connection-string path segment the grid injects.
43
+ return get_client().get_default_database()
44
+
45
+
46
+ def ping() -> bool:
47
+ """True if Mongo is reachable. Used by /backend/health."""
48
+ get_client().admin.command("ping")
49
+ return True
50
+
51
+
52
+ def ensure_indexes() -> None:
53
+ """Create the indexes the search relies on. Safe to call repeatedly.
54
+
55
+ Called at FastAPI startup so the chunks $text index exists before any query
56
+ — without it, `$text` raises IndexNotFound (a 500) on an empty catalog.
57
+ """
58
+ db = get_db()
59
+ # Text index on chunk content for lexical / $text search.
60
+ db.chunks.create_index([("content", TEXT)], name="chunks_content_text")
61
+ db.chunks.create_index([("document_id", ASCENDING)], name="chunks_document_id")
62
+ # Metadata filter indexes on documents.
63
+ db.documents.create_index([("date", ASCENDING)], name="documents_date")
64
+ db.documents.create_index([("collection", ASCENDING)], name="documents_collection")
65
+ db.documents.create_index(
66
+ [("source_filename", ASCENDING)], name="documents_source_filename", unique=True
67
+ )
68
+
69
+
70
+ # ── Collection accessors ─────────────────────────────────────────────────────
71
+
72
+ def documents():
73
+ return get_db().documents
74
+
75
+
76
+ def chunks():
77
+ return get_db().chunks
78
+
79
+
80
+ def settings():
81
+ return get_db().settings
82
+
83
+
84
+ def events():
85
+ return get_db().usage_events
86
+
87
+
88
+ def index_reports():
89
+ return get_db().index_reports
@@ -0,0 +1,44 @@
1
+ """Single config point for embeddings (OpenAI-compatible).
2
+
3
+ Isolated behind one module so the provider can be swapped by env var
4
+ (EMBEDDINGS_BASE_URL / EMBEDDINGS_MODEL / EMBEDDINGS_API_KEY). NEVER reads the
5
+ API key at import time — only inside embed(). If the key is missing, embed()
6
+ raises EmbeddingsUnavailable, which callers translate into a graceful degraded
7
+ state (lexical + metadata search still work).
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import httpx
12
+
13
+ from . import config
14
+
15
+
16
+ class EmbeddingsUnavailable(RuntimeError):
17
+ """Raised when embeddings are requested but not configured/reachable."""
18
+
19
+
20
+ def embed(texts: list[str]) -> list[list[float]]:
21
+ """Return one embedding vector per input text.
22
+
23
+ Raises EmbeddingsUnavailable if EMBEDDINGS_API_KEY is not set.
24
+ """
25
+ if not texts:
26
+ return []
27
+ key = config.embeddings_api_key()
28
+ if not key:
29
+ raise EmbeddingsUnavailable(
30
+ "EMBEDDINGS_API_KEY is not set — semantic embedding is unavailable."
31
+ )
32
+ resp = httpx.post(
33
+ f"{config.embeddings_base_url()}/embeddings",
34
+ headers={"Authorization": f"Bearer {key}"},
35
+ json={"model": config.embeddings_model(), "input": texts},
36
+ timeout=60.0,
37
+ )
38
+ resp.raise_for_status()
39
+ data = resp.json()["data"]
40
+ return [row["embedding"] for row in data]
41
+
42
+
43
+ def embed_one(text: str) -> list[float]:
44
+ return embed([text])[0]