pi-mega-compact 0.13.3 → 0.13.4

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.
@@ -158,10 +158,19 @@ export function embeddingConfigFromEnv() {
158
158
  dim: Number.isFinite(dim) ? dim : undefined,
159
159
  };
160
160
  }
161
- /** Extract a single embedding vector from a tolerant OpenAI-style response. */
161
+ /** True if the URL is an Ollama /api/embeddings endpoint. */
162
+ function isOllamaEndpoint(url) {
163
+ return url.includes("/api/embeddings");
164
+ }
165
+ /** Extract a single embedding vector from a tolerant response.
166
+ * Handles Ollama ({embedding:[...]}), OpenAI ({data:[{embedding:[...]}]}),
167
+ * and simple ({data:[...]} / {embeddings:[...]}) shapes. */
162
168
  function parseEmbedding(body) {
163
169
  if (body && typeof body === "object") {
164
170
  const b = body;
171
+ // Ollama: { embedding: [0.1, ...] }
172
+ if (Array.isArray(b.embedding))
173
+ return b.embedding;
165
174
  if (Array.isArray(b.data) && b.data[0] && typeof b.data[0] === "object") {
166
175
  const first = b.data[0];
167
176
  if (Array.isArray(first.embedding))
@@ -210,7 +219,10 @@ export class HttpEmbedder {
210
219
  return new URL("/api/chat", this.url).href;
211
220
  }
212
221
  embed(text) {
213
- const body = JSON.stringify({ input: [text] });
222
+ const ollama = isOllamaEndpoint(this.url);
223
+ const body = ollama
224
+ ? JSON.stringify({ model: process.env.MEGACOMPACT_OLLAMA_MODEL || "nomic-embed-text", prompt: text })
225
+ : JSON.stringify({ input: [text] });
214
226
  const headers = {
215
227
  "content-type": "application/json",
216
228
  ...this.headers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.13.3",
3
+ "version": "0.13.4",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-3-Clause",
@@ -173,10 +173,19 @@ export function embeddingConfigFromEnv(): HttpEmbedderOptions | null {
173
173
  };
174
174
  }
175
175
 
176
- /** Extract a single embedding vector from a tolerant OpenAI-style response. */
176
+ /** True if the URL is an Ollama /api/embeddings endpoint. */
177
+ function isOllamaEndpoint(url: string): boolean {
178
+ return url.includes("/api/embeddings");
179
+ }
180
+
181
+ /** Extract a single embedding vector from a tolerant response.
182
+ * Handles Ollama ({embedding:[...]}), OpenAI ({data:[{embedding:[...]}]}),
183
+ * and simple ({data:[...]} / {embeddings:[...]}) shapes. */
177
184
  function parseEmbedding(body: unknown): number[] {
178
185
  if (body && typeof body === "object") {
179
186
  const b = body as Record<string, unknown>;
187
+ // Ollama: { embedding: [0.1, ...] }
188
+ if (Array.isArray(b.embedding)) return b.embedding as number[];
180
189
  if (Array.isArray(b.data) && b.data[0] && typeof b.data[0] === "object") {
181
190
  const first = b.data[0] as Record<string, unknown>;
182
191
  if (Array.isArray(first.embedding)) return first.embedding as number[];
@@ -227,7 +236,10 @@ export class HttpEmbedder implements Embedder {
227
236
  }
228
237
 
229
238
  embed(text: string): Vector {
230
- const body = JSON.stringify({ input: [text] });
239
+ const ollama = isOllamaEndpoint(this.url);
240
+ const body = ollama
241
+ ? JSON.stringify({ model: process.env.MEGACOMPACT_OLLAMA_MODEL || "nomic-embed-text", prompt: text })
242
+ : JSON.stringify({ input: [text] });
231
243
  const headers: Record<string, string> = {
232
244
  "content-type": "application/json",
233
245
  ...this.headers,