Files
zeekayandHanzo Dev 635fbb7c0c Hanzo Cloud — OSS core (Apache-2.0)
The single Go binary that runs the compute/backend control plane: KMS secrets,
object storage, DNS/provisioning, PaaS, code + exec, zero-trust sharing, audit,
gateway edge, feature flags, security, and the plugin runtime — every subsystem
mounted from one composition root.

Open-core by construction, not by deletion. The proprietary planes — identity,
commerce, AI inference, observability, the message bus, billing — are injected
through the cloud/types extension interfaces, so this binary links only public
infrastructure libraries and a missing plane mounts fail-closed and says so
rather than pretending to work.

Published from a fresh root rather than a filtered history: the extraction it
came from began life without git, and a squashed root is the only way to be sure
no earlier blob travels with it.

Runs on a laptop with no Kubernetes, no cluster and no external services —
`CLOUD_DEV_UNENCRYPTED=1 cloud -data-dir /tmp/cloud` boots ~32 subsystems on
HTTP and ZAP. Encryption at rest stays fatal-by-default on a capable build; the
dev flag relaxes only the no-key case, loudly.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-04 22:20:29 -07:00

81 lines
2.6 KiB
Go

package code
import (
"context"
"os"
"github.com/hanzoai/cloud"
)
// Embedder turns text into vectors for the semantic tier. It is an interface so
// the subsystem wires the real AI client while tests inject a deterministic
// offline embedder — the semantic tier is fully testable without a live model.
type Embedder interface {
// Embed returns one vector per input, aligned by index, metered against the
// (org, project) billing scope. A disabled embedder returns (nil, nil) so
// indexing proceeds lexically + symbolically.
Embed(ctx context.Context, org, billingOrg, project string, texts []string) ([][]float32, error)
Enabled() bool
}
// aiEmbedder routes embeddings through the shared cloud.AIClient (deps.AI) — the
// SAME org/project-aligned, metered, observed path chat runs through. There is
// deliberately NO embed-specific key or endpoint: one AI identity, one way. The
// gateway owns model routing; model is CLOUD_EMBED_MODEL (default bge-m3, the
// 1024-dim BAAI embedder the gateway serves).
type aiEmbedder struct {
ai cloud.AIClient
model string
}
func newEmbedder(ai cloud.AIClient, model string) *aiEmbedder {
if model == "" {
// The gateway serves the SKU (zen-embedding), not the raw upstream name:
// a request for "bge-m3" 400s ("model not available"), which silently left
// the semantic tier empty (vectors:0). zen-embedding routes to bge-m3.
model = getenv("CLOUD_EMBED_MODEL", "zen-embedding")
}
return &aiEmbedder{ai: ai, model: model}
}
// Enabled mirrors the synthesizer (a non-nil AI client). The disabled stub errors
// at call time, so indexing degrades to lexical + symbolic rather than
// fabricating vectors.
func (e *aiEmbedder) Enabled() bool { return e.ai != nil }
// embedBatch bounds one request so a large index call fans out over several
// round-trips rather than one oversized body.
const embedBatch = 64
func (e *aiEmbedder) Embed(ctx context.Context, org, billingOrg, project string, texts []string) ([][]float32, error) {
if e.ai == nil || len(texts) == 0 {
return nil, nil
}
out := make([][]float32, 0, len(texts))
for start := 0; start < len(texts); start += embedBatch {
end := start + embedBatch
if end > len(texts) {
end = len(texts)
}
vecs, err := e.ai.Embed(ctx, &cloud.EmbedRequest{
Model: e.model,
Inputs: texts[start:end],
Org: org,
BillingOrg: billingOrg,
Project: project,
})
if err != nil {
return nil, err
}
out = append(out, vecs...)
}
return out, nil
}
func getenv(key, dflt string) string {
if v := os.Getenv(key); v != "" {
return v
}
return dflt
}