internal/infra/pg/entityembedding.go
internal/infra/pg · 734 lines · 29 declarations · source
This file carries the package documentation, rendered on the package page.
Declarations
const EntityEmbeddingInputVersion
const EntityEmbeddingInputVersion = "entity-evidence/v1"
const MaxEntityEmbeddingSources
const MaxEntityEmbeddingSources = 4096
const MaxEntityEmbeddingPageSources
const MaxEntityEmbeddingPageSources = 4096
const MaxEntityEmbeddingTargets
const MaxEntityEmbeddingTargets = 1_000_000
type EntityEmbeddingGeneration
type EntityEmbeddingGeneration struct {
ID string `json:"id"`
Project string `json:"project"`
Key string `json:"operation_key"`
Model EmbeddingModel `json:"model"`
TargetCount int64 `json:"target_count"`
After string `json:"after_entity_id,omitempty"`
State string `json:"state"`
Active bool `json:"active"`
Examined int64 `json:"examined"`
Stored int64 `json:"stored"`
CreatedAt time.Time `json:"created_at"`
}
type EntityEmbeddingBuildPage
type EntityEmbeddingBuildPage struct {
Examined int `json:"examined"`
Stored int `json:"stored"`
Complete bool `json:"complete"`
After string `json:"after_entity_id,omitempty"`
}
type EntityCandidateOptions
type EntityCandidateOptions struct {
Limit int
Candidates int
applicationRead bool
}
type EntityCandidate
type EntityCandidate struct {
EntityID string `json:"entity_id"`
IdentityKind string `json:"identity_kind"`
NamePreview string `json:"name_preview"`
NameBytes int `json:"name_bytes"`
NameTruncated bool `json:"name_truncated"`
Similarity float64 `json:"similarity"`
}
type EntityCandidateResult
type EntityCandidateResult struct {
Generation EntityEmbeddingGeneration `json:"generation"`
Approximate bool `json:"approximate"`
Candidates []EntityCandidate `json:"candidates"`
}
type EntityEmbeddingStore
type EntityEmbeddingStore struct {
pool *pgxpool.Pool
schema Schema
}
func NewEntityEmbeddingStore
func NewEntityEmbeddingStore(pool *pgxpool.Pool, schema Schema) *EntityEmbeddingStore
func entityEmbeddingModelIdentity
func entityEmbeddingModelIdentity(model EmbeddingModel) string
const entityEmbeddingGenerationSQL
const entityEmbeddingGenerationSQL = `SELECT g.generation_id::text,g.scope,g.operation_key::text,
g.model_name,g.model_revision,g.endpoint_hash,g.dimensions,g.target_count,coalesce(b.after_entity_id::text,''),
b.state,coalesce(a.generation_id=g.generation_id,false),b.examined,b.stored,g.created_at
FROM {schema}.entity_embedding_generation g JOIN {schema}.entity_embedding_build b USING(scope,generation_id)
LEFT JOIN {schema}.entity_embedding_active a USING(scope)`
func scanEntityEmbeddingGeneration
func scanEntityEmbeddingGeneration(row pgx.Row) (EntityEmbeddingGeneration, error)
func entityEmbeddingPartitionName
func entityEmbeddingPartitionName(id string) string
method EntityEmbeddingStore.Generation
func (s *EntityEmbeddingStore) Generation(ctx context.Context, scope, id string) (EntityEmbeddingGeneration, error)
method EntityEmbeddingStore.Active
func (s *EntityEmbeddingStore) Active(ctx context.Context, scope string) (EntityEmbeddingGeneration, error)
method EntityEmbeddingStore.Start
func (s *EntityEmbeddingStore) Start(ctx context.Context, scope, key, actor string, model EmbeddingModel) (EntityEmbeddingGeneration, error)
Start materializes the current named-entity IDs behind the project row lock. Named-entity inserts take that lock in their migration trigger, so an insert is either included or makes this finite snapshot stale after Start commits.
type entityEmbeddingInput
type entityEmbeddingInput struct {
entity string
text string
digest []byte
sources []string
sourceDigest []byte
}
func digestEntitySources
func digestEntitySources(sources []string) []byte
method EntityEmbeddingStore.loadEntityEmbeddingInput
func (s *EntityEmbeddingStore) loadEntityEmbeddingInput(ctx context.Context, q interface {
QueryRow(context.Context, string, ...any) pgx.Row
}, scope, generation, entity string) (entityEmbeddingInput, bool, error)
loadEntityEmbeddingInput bounds transferred aliases, quotes and source IDs before the caller allocates them. Quotes beyond the validation budget cannot affect the 2KB surface.
method EntityEmbeddingStore.BuildPage
func (s *EntityEmbeddingStore) BuildPage(ctx context.Context, scope, id, actor string, model EmbeddingModel, limit int, embed EmbeddingBatch) (EntityEmbeddingBuildPage, error)
method EntityEmbeddingStore.commitEntityEmbeddingInputs
func (s *EntityEmbeddingStore) commitEntityEmbeddingInputs(ctx context.Context, tx pgx.Tx,
generation EntityEmbeddingGeneration, entries []entityEmbeddingInput, vectors [][]float32) (int, error)
method EntityEmbeddingStore.Activate
func (s *EntityEmbeddingStore) Activate(ctx context.Context, scope, id, actor string) error
method EntityEmbeddingStore.Repair
func (s *EntityEmbeddingStore) Repair(ctx context.Context, scope, id, actor string) error
method EntityEmbeddingStore.Cancel
func (s *EntityEmbeddingStore) Cancel(ctx context.Context, scope, id, actor string) error
method EntityEmbeddingStore.Prune
func (s *EntityEmbeddingStore) Prune(ctx context.Context, scope, id, actor string, limit int) (int, error)
method EntityEmbeddingStore.Search
func (s *EntityEmbeddingStore) Search(ctx context.Context, scope, actor string, model EmbeddingModel,
vector []float32, options EntityCandidateOptions) (EntityCandidateResult, error)
method EntityEmbeddingStore.FindCandidates
func (s *EntityEmbeddingStore) FindCandidates(ctx context.Context, scope string, model EmbeddingModel,
vector []float32, options EntityCandidateOptions) (EntityCandidateResult, error)
FindCandidates is for a caller that has already enforced project authorization and credential audit. It returns candidate identities only and cannot merge or mutate them.