File size: 7,879 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**

 * Static Potion embedding (D7) — potion-base-8M via lookup + WordPiece minimal.

 *

 * Downloads model files once to <DATA_DIR>/embeddings/potion-base-8M/.

 * No WASM, no @huggingface/tokenizers dependency.

 * Singleton: matrix + vocab cached in module memory after first load.

 */

import fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error.ts";
import type { EmbeddingResult, EmbeddingError } from "./types";

const MODEL_ID = "minishlab/potion-base-8M";
const MODEL_NAME = "potion-base-8M";
const HF_BASE =
  process.env.HF_HUB_ENDPOINT || "https://huggingface.co";

function getModelDir(): string {
  const staticCacheDir = process.env.MEMORY_STATIC_CACHE_DIR;
  if (staticCacheDir) return path.join(staticCacheDir, MODEL_NAME);
  const dataDir = process.env.DATA_DIR ?? path.join(os.homedir(), ".omniroute");
  return path.join(dataDir, "embeddings", MODEL_NAME);
}

export interface PotionModel {
  vocab: Record<string, number>;       // token → index
  matrix: Float32Array;                // flat row-major [vocab_size × dim]
  dim: number;
  vocabSize: number;
  unkIdx: number;
}

// Singleton state
let _model: PotionModel | null = null;
let _loading: Promise<PotionModel> | null = null;

/** For testing: inject a mock model, bypassing download. */
export function _injectModel(model: PotionModel | null): void {
  _model = model;
  _loading = null;
}

async function downloadFile(url: string, dest: string): Promise<void> {
  const resp = await fetch(url);
  if (!resp.ok) {
    throw new Error(`Failed to download ${url}: HTTP ${resp.status}`);
  }
  const buf = await resp.arrayBuffer();
  await fs.writeFile(dest, Buffer.from(buf));
}

async function ensureFile(filePath: string, url: string): Promise<void> {
  try {
    await fs.access(filePath);
  } catch {
    await downloadFile(url, filePath);
  }
}

/**

 * Parse safetensors format to extract the first float32 tensor.

 * Header format: 8-byte little-endian uint64 = header_len, then JSON header,

 * then raw tensor bytes.

 */
function parseSafetensors(buf: Buffer): { matrix: Float32Array; shape: number[] } {
  // Read 8-byte header size (little-endian)
  const headerLen = Number(buf.readBigUInt64LE(0));
  const headerJson = buf.slice(8, 8 + headerLen).toString("utf8");
  const header = JSON.parse(headerJson) as Record<
    string,
    { dtype?: string; shape?: number[]; data_offsets?: [number, number] }
  >;

  // Find the first float32 tensor (ignore __metadata__)
  for (const [key, meta] of Object.entries(header)) {
    if (key === "__metadata__") continue;
    if (!meta.dtype || !meta.shape || !meta.data_offsets) continue;
    const dtype = meta.dtype.toLowerCase();
    if (dtype !== "f32" && dtype !== "float32") continue;

    const [startOffset, endOffset] = meta.data_offsets;
    const dataStart = 8 + headerLen + startOffset;
    const dataEnd = 8 + headerLen + endOffset;
    const dataSlice = buf.slice(dataStart, dataEnd);

    const floatCount = (dataEnd - dataStart) / 4;
    const arr = new Float32Array(floatCount);
    for (let i = 0; i < floatCount; i++) {
      arr[i] = dataSlice.readFloatLE(i * 4);
    }
    return { matrix: arr, shape: meta.shape };
  }
  throw new Error("No float32 tensor found in safetensors file");
}

async function loadModel(): Promise<PotionModel> {
  const modelDir = getModelDir();
  await fs.mkdir(modelDir, { recursive: true });

  const hfBase = `${HF_BASE}/${MODEL_ID}/resolve/main`;

  const vocabPath = path.join(modelDir, "vocab.json");
  const modelPath = path.join(modelDir, "model.safetensors");
  const tokenizerPath = path.join(modelDir, "tokenizer.json");

  await Promise.all([
    ensureFile(vocabPath, `${hfBase}/vocab.json`),
    ensureFile(modelPath, `${hfBase}/model.safetensors`),
    ensureFile(tokenizerPath, `${hfBase}/tokenizer.json`),
  ]);

  // Load vocab
  const vocabRaw = await fs.readFile(vocabPath, "utf8");
  const vocab = JSON.parse(vocabRaw) as Record<string, number>;

  // Load matrix from safetensors
  const modelBuf = await fs.readFile(modelPath);
  const { matrix, shape } = parseSafetensors(modelBuf);

  if (shape.length < 2) {
    throw new Error(`Unexpected safetensors shape: ${JSON.stringify(shape)}`);
  }
  const vocabSize = shape[0];
  const dim = shape[1];

  const unkIdx = vocab["[UNK]"] ?? 0;

  return { vocab, matrix, dim, vocabSize, unkIdx };
}

export function getOrLoadModel(): Promise<PotionModel> {
  if (_model) return Promise.resolve(_model);
  if (_loading) return _loading;
  _loading = loadModel().then((m) => {
    _model = m;
    _loading = null;
    return m;
  });
  return _loading;
}

/**

 * Minimal WordPiece tokenizer.

 * 1. Split text by whitespace.

 * 2. For each word, try full match in vocab.

 * 3. If not found, greedily split into ##sub-tokens.

 * 4. Any unresolved piece becomes [UNK].

 */
export function tokenizeWordPiece(text: string, vocab: Record<string, number>): number[] {
  const words = text.trim().toLowerCase().split(/\s+/);
  const tokenIds: number[] = [];
  const unkId = vocab["[UNK]"] ?? 0;

  for (const word of words) {
    if (!word) continue;
    if (vocab[word] !== undefined) {
      tokenIds.push(vocab[word]);
      continue;
    }

    // WordPiece greedy sub-tokenization
    const subTokens: number[] = [];
    let remaining = word;
    let failed = false;

    while (remaining.length > 0) {
      let found = false;
      for (let end = remaining.length; end > 0; end--) {
        const candidate = subTokens.length === 0 ? remaining.slice(0, end) : `##${remaining.slice(0, end)}`;
        if (vocab[candidate] !== undefined) {
          subTokens.push(vocab[candidate]);
          remaining = remaining.slice(end);
          found = true;
          break;
        }
      }
      if (!found) {
        failed = true;
        break;
      }
    }

    if (failed || subTokens.length === 0) {
      tokenIds.push(unkId);
    } else {
      for (const id of subTokens) tokenIds.push(id);
    }
  }

  return tokenIds;
}

/**

 * Mean pooling over token vectors.

 */
export function meanPool(tokenIds: number[], matrix: Float32Array, dim: number, vocabSize: number, unkIdx: number): Float32Array {
  const result = new Float32Array(dim);
  let validCount = 0;

  for (const id of tokenIds) {
    const safeId = id >= 0 && id < vocabSize ? id : unkIdx;
    const offset = safeId * dim;
    for (let d = 0; d < dim; d++) {
      result[d] += matrix[offset + d];
    }
    validCount++;
  }

  if (validCount > 0) {
    for (let d = 0; d < dim; d++) {
      result[d] /= validCount;
    }
  }

  return result;
}

export async function embedStatic(text: string): Promise<EmbeddingResult | EmbeddingError> {
  const t0 = Date.now();
  let model: PotionModel;
  try {
    model = await getOrLoadModel();
  } catch (err: unknown) {
    return {
      source: "static",
      model: MODEL_NAME,
      reason: "model_load_failed",
      message: sanitizeErrorMessage(err instanceof Error ? err.message : String(err)),
    };
  }

  try {
    const tokenIds = tokenizeWordPiece(text, model.vocab);
    const vector = meanPool(tokenIds, model.matrix, model.dim, model.vocabSize, model.unkIdx);
    return {
      vector,
      source: "static",
      model: MODEL_NAME,
      dimensions: model.dim,
      latencyMs: Date.now() - t0,
      cached: false,
    };
  } catch (err: unknown) {
    return {
      source: "static",
      model: MODEL_NAME,
      reason: "request_failed",
      message: sanitizeErrorMessage(err instanceof Error ? err.message : String(err)),
    };
  }
}