embeddings
What it does
Lifecycle management for public.embeddings — per-document vector rows (UNIQUE(model_id, document_id, chunk_index), pgvector embedding vector(1536)) used by knowledge retrieval. Beyond basic CRUD it can generate an item's vector set from its type schema, search by vector similarity or full-text, and gate visibility with approved_at/revoked_at.
Route and methods
/.netlify/functions/embeddings, reachable as /api/embeddings (/api/* → /.netlify/functions/:splat, .framework/public/_redirects). Dispatch is by ?action= + method:
| Call | Handler |
|---|---|
GET /api/embeddings | list |
GET /api/embeddings?id=<uuid> | get |
GET /api/embeddings?action=stats | getStats |
POST /api/embeddings | create |
PATCH /api/embeddings (id in body or query) | update |
POST /api/embeddings?action=generate | generate |
POST /api/embeddings?action=batch-create | batchCreate |
POST /api/embeddings?action=delete-document | deleteByDocument |
POST /api/embeddings?action=cleanup | cleanup |
POST /api/embeddings?action=search-similar | searchSimilar |
POST /api/embeddings?action=search-semantic | searchSemantic |
POST /api/embeddings?action=revoke | revoke |
POST /api/embeddings?action=approve | approve |
There is no DELETE route — delete-document is a POST action.
Authentication
Standard createHandler auth: x-api-key → machine principal, x-internal-secret → internal principal, Bearer <jwt> → human; anonymous → 401.
list/create/batchCreate/getStats/searchSemanticrequirectx.accountId.- No role check anywhere.
generate,revoke, andapproveread and write throughadminDb(service role) for any authenticated caller — including cross-account, sincegenerateaccepts anaccount_id_override. Treat these actions as trusted-surface operations.
Request contract
list (query): model_id, document_id, limit (default 100), offset (default 0). Ordered created_at desc.
get (query): id. getStats: none → { total_embeddings }.
create (body): model_id, document_id, content required; chunk_index (default 0), metadata {}. account_id stamped from context. (Does not compute a vector — it stores a row.)
update (id + body): content, metadata only.
generate (body): item_id required; force_regenerate (deletes and rebuilds all vectors for the document), account_id_override (defaults to the item's account, then the spine-system platform account).
batchCreate (body): embeddings_data array — rows inserted verbatim with account_id stamped.
deleteByDocument (body): document_id — hard-deletes all its rows.
cleanup (body): days_to_keep (default 365) — hard-deletes rows older than the cutoff.
searchSimilar (body): query_embedding required; limit (default 10), threshold (default 0.7), filter_account_ids, filter_vector_type, filter_item_types, filter_document_ids — calls the match_embeddings RPC (.framework/migrations/044_match_embeddings_filtered_knn.sql).
searchSemantic (body): query required; model_id, document_ids, limit (default 10) — Postgres textSearch (websearch, english) over content, scoped to ctx.accountId.
revoke / approve (body): document_id.
Response contract
Standard { data, error, meta } envelope.
generate→{ item_id, embeddings_created, embeddings_skipped, errors }(unchanged content is skipped by hash, not re-embedded).searchSimilar→{ results, count, model_id, threshold };searchSemantic→{ results, count, method: 'text_search', query }.revoke→{ document_id, revoked_count };approve→{ document_id, approved_count };deleteByDocument/cleanup→{ deleted_count };batchCreate→ inserted rows.list/getsanitize viasanitizeRecordData— full row for system admins,{ id }for non-admins (rows carry nodesign_schema).
Side effects
generatereads the item + its type'sdesign_schema(vector_types— a flat array or a per-kb_typemap defaulting to['semantic','structure']— andembedding_fields.structure), builds one text per vector type viaCONTENT_BUILDERS, calls the OpenAI embeddings API (text-embedding-3-small), and upsertspublic.embeddingsrows keyed by fixedchunk_indexslots (VECTOR_TYPE_SLOTS: semantic 0, structure 1, context 2, …) so the(model_id, document_id, chunk_index)unique constraint never collides across vector types.generatedeletes stale rows left over from a previously larger vector-type set, and skips re-embedding rows whosecontent_hashmatches (16-char SHA-256 prefix).generatesetsapproved_atonly when the item'sstatusis'published';metadatacarriesvector_type,item_type,kb_type,security_level,source_item_id.- A
'summary'vector type is generated viacallOpenAIChat(LLM_DEFAULT_MODELorgpt-4o-mini) with a sync title+description fallback. revokesetsrevoked_aton all non-revoked rows for the document;approvesetsapproved_atand clearsrevoked_at— both viaadminDbregardless of caller role.emitLogwritesembedding.created,embedding.updated,embeddings.generated,embeddings.batch_created,embeddings.document_deleted,embeddings.cleaned,embeddings.revoked,embeddings.approvedtopublic.logs.
Errors
| Condition | HTTP | Message |
|---|---|---|
| Missing fields | 500 | model_id, document_id, and content are required / item_id is required / document_id is required / embeddings_data array is required |
| Item missing (generate) | 500 | Item <id> not found |
| Embedding API failure | 500 | Embedding API call failed: <message> |
| Missing search inputs | 500 | query_embedding is required for similarity search / query is required for semantic search |
| Bad action/method | 500 | Invalid action or method |
Agent notes
- Vectors are stored as the pgvector string literal
[v1,v2,…]produced bycallEmbeddingApi— theembeddingcolumn isvector(1536)(.framework/migrations/001_core_schema.sql). metadata.vector_typeis the discriminator retrieval filters use;chunk_indexis a slot convention, not a sequence of text chunks.generate'saccount_id_overrideplus unconditionaladminDbmeans any authenticated caller can regenerate embeddings into another account — andrevoke/approve/cleanup/deleteByDocumentare equally ungated. Do not expose this endpoint to untrusted principals.- Retrieval-time search is
match_embeddings(vector) ortextSearch(keyword) — semantic ranking lives in the RPC, not here.