@hiveai/embeddings 0.2.8 → 0.2.10

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 (2) hide show
  1. package/README.md +151 -0
  2. package/package.json +3 -3
package/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # @hiveai/embeddings
2
+
3
+ > **Optional add-on for hAIve** — local sentence embeddings and semantic search for your AI memory layer. No data leaves your machine.
4
+
5
+ When installed alongside `@hiveai/cli` and `@hiveai/mcp`, this package enables similarity-based memory retrieval: instead of keyword matching, `get_briefing` and `mem_search` can find memories that are *semantically* related to your task description, even if they share no common words.
6
+
7
+ ---
8
+
9
+ ## Why optional?
10
+
11
+ This package pulls in heavy ML dependencies (`@xenova/transformers`, `onnxruntime-node`, `sharp`) and downloads a ~110MB model on first use. It is **not installed by default** so that the core hAIve experience stays lightweight.
12
+
13
+ Install it explicitly when you want semantic search:
14
+
15
+ ```bash
16
+ npm install -g @hiveai/embeddings
17
+ # or alongside the CLI:
18
+ npm install -g @hiveai/cli @hiveai/embeddings
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Quick start
24
+
25
+ ```bash
26
+ # Build (or refresh) the index. First run downloads the model (~110MB, cached locally).
27
+ haive embeddings index
28
+
29
+ # Check index status
30
+ haive embeddings status
31
+
32
+ # Run a semantic search from the terminal
33
+ haive embeddings query "how do we handle retries on payment failures"
34
+ ```
35
+
36
+ From an MCP client, pass `semantic: true` to `mem_search` or `get_briefing`:
37
+
38
+ ```json
39
+ { "task": "add a mobile payment provider", "semantic": true }
40
+ ```
41
+
42
+ ---
43
+
44
+ ## Commands
45
+
46
+ ### `haive embeddings index`
47
+
48
+ Build or refresh the embeddings index for all memories.
49
+
50
+ ```bash
51
+ haive embeddings index # Index all memories in the current project
52
+ haive embeddings index --dir /path # Specify project root
53
+ haive embeddings index --force # Force full rebuild (ignore content hashes)
54
+ ```
55
+
56
+ The index is stored at `.ai/.cache/embeddings/embeddings-index.json`. Each entry is keyed by content hash, so only changed memories are re-embedded on subsequent runs.
57
+
58
+ ### `haive embeddings status`
59
+
60
+ Show the current state of the embeddings index.
61
+
62
+ ```bash
63
+ haive embeddings status
64
+ # Output:
65
+ # Index: .ai/.cache/embeddings/embeddings-index.json
66
+ # Entries: 24
67
+ # Model: Xenova/bge-small-en-v1.5 (384 dimensions)
68
+ # Last updated: 2025-01-20T14:32:00Z
69
+ ```
70
+
71
+ ### `haive embeddings query`
72
+
73
+ Run a semantic query against the local index.
74
+
75
+ ```bash
76
+ haive embeddings query "payment retry logic"
77
+ haive embeddings query "JWT expiration handling" --limit 5
78
+ haive embeddings query "database migration" --dir /path/to/project
79
+ ```
80
+
81
+ ---
82
+
83
+ ## How it works
84
+
85
+ 1. **Model**: [`Xenova/bge-small-en-v1.5`](https://huggingface.co/BAAI/bge-small-en-v1.5) — a 33M-parameter sentence embedding model, 384 dimensions, optimized for retrieval tasks. Downloaded once and cached in `~/.cache/huggingface/` (or `TRANSFORMERS_CACHE`).
86
+
87
+ 2. **Indexing**: Each memory's body is converted to a 384-dimensional vector and stored alongside its id and content hash.
88
+
89
+ 3. **Search**: At query time, the query text is embedded and cosine similarity is computed against all indexed memories. The top-k results are returned ranked by score.
90
+
91
+ 4. **Integration**: When `@hiveai/embeddings` is installed and the index exists, `get_briefing` and `mem_search` automatically use semantic ranking. If the package is missing or the index is empty, they fall back to literal (keyword) search transparently.
92
+
93
+ ---
94
+
95
+ ## Auto-rebuild on sync
96
+
97
+ Add `--embed` to `haive sync` to automatically rebuild the index after every sync:
98
+
99
+ ```bash
100
+ haive sync --embed
101
+
102
+ # Or in your git hook / CI:
103
+ haive sync --quiet --embed
104
+ ```
105
+
106
+ ---
107
+
108
+ ## Programmatic API
109
+
110
+ ```typescript
111
+ import { rebuildIndex, semanticSearch } from "@hiveai/embeddings";
112
+ import { resolveHaivePaths, findProjectRoot } from "@hiveai/core";
113
+
114
+ const paths = resolveHaivePaths(findProjectRoot());
115
+
116
+ // Rebuild the full index
117
+ const report = await rebuildIndex(paths);
118
+ // report.added, report.updated, report.removed, report.skipped
119
+
120
+ // Search
121
+ const result = await semanticSearch(paths, "payment retry logic", { limit: 5 });
122
+ if (result) {
123
+ for (const hit of result.hits) {
124
+ console.log(hit.id, hit.score); // score: 0.0–1.0
125
+ }
126
+ }
127
+
128
+ // Custom embedder (for testing or alternative models)
129
+ import { Embedder, type EmbedderLike } from "@hiveai/embeddings";
130
+
131
+ const embedder: EmbedderLike = {
132
+ model: "Xenova/bge-small-en-v1.5",
133
+ dimension: 384,
134
+ encode: async (texts) => { /* ... */ return [[0.1, 0.2, ...]]; },
135
+ };
136
+ ```
137
+
138
+ ---
139
+
140
+ ## Privacy
141
+
142
+ - The model runs **entirely locally** via [Transformers.js](https://huggingface.co/docs/transformers.js) + ONNX Runtime.
143
+ - No API keys required.
144
+ - No network calls during search or indexing (only on first model download).
145
+ - Memory content never leaves your machine.
146
+
147
+ ---
148
+
149
+ ## License
150
+
151
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hiveai/embeddings",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "hAIve embeddings — local sentence embeddings via Transformers.js for semantic memory search",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,8 +32,8 @@
32
32
  "LICENSE"
33
33
  ],
34
34
  "dependencies": {
35
- "@xenova/transformers": "^2.17.2",
36
- "@hiveai/core": "0.2.8"
35
+ "@hiveai/core": "^0.2.9",
36
+ "@xenova/transformers": "^2.17.2"
37
37
  },
38
38
  "overrides": {
39
39
  "protobufjs": "^7.5.5"