@abhishek944/pi-embeddings 0.1.0 → 0.4.0
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.
- package/README.md +59 -9
- package/dist/execute.js +56 -3
- package/dist/extension.js +8 -39
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/mcp-server.js +1 -14
- package/dist/providers.d.ts +13 -2
- package/dist/providers.js +263 -3
- package/dist/response.d.ts +21 -0
- package/dist/response.js +115 -0
- package/dist/runtime.d.ts +22 -2
- package/dist/runtime.js +12 -3
- package/dist/schemas.d.ts +703 -81
- package/dist/schemas.js +137 -16
- package/dist/settings-write.js +2 -2
- package/dist/settings.js +8 -1
- package/dist/tool-contracts.d.ts +459 -184
- package/dist/tool-contracts.js +2 -11
- package/dist/vertex-provider.d.ts +18 -0
- package/dist/vertex-provider.js +139 -0
- package/package.json +9 -2
- package/src/execute.ts +59 -3
- package/src/extension.ts +12 -47
- package/src/index.ts +11 -2
- package/src/mcp-server.ts +0 -19
- package/src/providers.ts +322 -3
- package/src/response.ts +135 -0
- package/src/runtime.ts +44 -6
- package/src/schemas.ts +180 -26
- package/src/settings-write.ts +2 -2
- package/src/settings.ts +10 -0
- package/src/tool-contracts.ts +1 -13
- package/src/vertex-provider.ts +152 -0
package/dist/schemas.js
CHANGED
|
@@ -7,20 +7,80 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Type } from 'typebox';
|
|
9
9
|
export const EMBEDDINGS_SETTINGS_KEY = 'pi-embeddings';
|
|
10
|
-
export const EMBEDDINGS_PROTOCOL_VERSION =
|
|
10
|
+
export const EMBEDDINGS_PROTOCOL_VERSION = 2;
|
|
11
11
|
export const EMBEDDING_FORMAT_VERSION = 'host-display-attributes-v1';
|
|
12
12
|
export const EMBEDDING_DOCUMENT_PREFIX = 'title: none | text: ';
|
|
13
13
|
export const EMBEDDING_NORMALIZATION_VERSION = 'finite-nonzero-l2-v1';
|
|
14
14
|
export const EMBEDDING_CONTEXT_META_KEY = 'pi-embeddings/context';
|
|
15
|
-
export const EMBEDDING_ROUTE_IDS = [
|
|
15
|
+
export const EMBEDDING_ROUTE_IDS = [
|
|
16
|
+
'local',
|
|
17
|
+
'openai',
|
|
18
|
+
'sarvam',
|
|
19
|
+
'gateway',
|
|
20
|
+
'openrouter',
|
|
21
|
+
'cloudflare',
|
|
22
|
+
'gemini',
|
|
23
|
+
'vertex',
|
|
24
|
+
];
|
|
16
25
|
export const GATEWAY_EMBEDDING_MODELS = [
|
|
17
26
|
'text-embedding-3-small',
|
|
18
27
|
'openai/text-embedding-3-small',
|
|
19
28
|
];
|
|
29
|
+
export const OPENROUTER_EMBEDDING_MODELS = ['openai/text-embedding-3-small'];
|
|
30
|
+
export const CLOUDFLARE_EMBEDDING_MODELS = ['@cf/baai/bge-base-en-v1.5'];
|
|
31
|
+
export const GEMINI_EMBEDDING_MODELS = ['gemini-embedding-2'];
|
|
32
|
+
export const GEMINI_EMBEDDING_MIN_DIMENSIONS = 128;
|
|
33
|
+
export const GEMINI_EMBEDDING_MAX_DIMENSIONS = 3072;
|
|
34
|
+
export const DEFAULT_GEMINI_DIMENSIONS = 3072;
|
|
35
|
+
export const GEMINI_EMBEDDING_TASK_FORMATS = [
|
|
36
|
+
'retrieval-document',
|
|
37
|
+
'search-query',
|
|
38
|
+
'question-answering',
|
|
39
|
+
'fact-checking',
|
|
40
|
+
'code-retrieval',
|
|
41
|
+
'classification',
|
|
42
|
+
'clustering',
|
|
43
|
+
'sentence-similarity',
|
|
44
|
+
];
|
|
45
|
+
export const DEFAULT_GEMINI_TASK_FORMAT = 'retrieval-document';
|
|
46
|
+
/** Official Gemini Embedding 2 text task prefixes. `task_type` is not used for this model. */
|
|
47
|
+
export const GEMINI_TASK_PREFIXES = {
|
|
48
|
+
'retrieval-document': 'title: none | text: ',
|
|
49
|
+
'search-query': 'task: search result | query: ',
|
|
50
|
+
'question-answering': 'task: question answering | query: ',
|
|
51
|
+
'fact-checking': 'task: fact checking | query: ',
|
|
52
|
+
'code-retrieval': 'task: code retrieval | query: ',
|
|
53
|
+
classification: 'task: classification | query: ',
|
|
54
|
+
clustering: 'task: clustering | query: ',
|
|
55
|
+
'sentence-similarity': 'task: sentence similarity | query: ',
|
|
56
|
+
};
|
|
20
57
|
const gatewayModelSchema = Type.Union([
|
|
21
58
|
Type.Literal('text-embedding-3-small'),
|
|
22
59
|
Type.Literal('openai/text-embedding-3-small'),
|
|
23
60
|
]);
|
|
61
|
+
const openRouterModelSchema = Type.Literal('openai/text-embedding-3-small');
|
|
62
|
+
const cloudflareModelSchema = Type.Literal('@cf/baai/bge-base-en-v1.5');
|
|
63
|
+
const geminiModelSchema = Type.Literal(GEMINI_EMBEDDING_MODELS[0]);
|
|
64
|
+
export const VERTEX_EMBEDDING_LOCATIONS = ['us', 'eu', 'global'];
|
|
65
|
+
const vertexLocationSchema = Type.Union([
|
|
66
|
+
Type.Literal('us'),
|
|
67
|
+
Type.Literal('eu'),
|
|
68
|
+
Type.Literal('global'),
|
|
69
|
+
]);
|
|
70
|
+
const geminiDimensionsSchema = Type.Integer({
|
|
71
|
+
minimum: GEMINI_EMBEDDING_MIN_DIMENSIONS,
|
|
72
|
+
maximum: GEMINI_EMBEDDING_MAX_DIMENSIONS,
|
|
73
|
+
});
|
|
74
|
+
const geminiTaskFormatSchema = Type.Union([
|
|
75
|
+
Type.Literal('retrieval-document'),
|
|
76
|
+
Type.Literal('search-query'),
|
|
77
|
+
Type.Literal('question-answering'),
|
|
78
|
+
Type.Literal('fact-checking'),
|
|
79
|
+
Type.Literal('code-retrieval'),
|
|
80
|
+
Type.Literal('classification'),
|
|
81
|
+
Type.Literal('clustering'),
|
|
82
|
+
Type.Literal('sentence-similarity'),
|
|
83
|
+
]);
|
|
24
84
|
const embeddingErrorCodeSchema = Type.Union([
|
|
25
85
|
Type.Literal('configuration-invalid'),
|
|
26
86
|
Type.Literal('route-unconfigured'),
|
|
@@ -80,6 +140,10 @@ const embeddingProviderSchema = Type.Union([
|
|
|
80
140
|
Type.Literal('openai'),
|
|
81
141
|
Type.Literal('sarvam'),
|
|
82
142
|
Type.Literal('gateway'),
|
|
143
|
+
Type.Literal('openrouter'),
|
|
144
|
+
Type.Literal('cloudflare'),
|
|
145
|
+
Type.Literal('gemini'),
|
|
146
|
+
Type.Literal('vertex'),
|
|
83
147
|
]);
|
|
84
148
|
export const embeddingRouteSchema = Type.Object({
|
|
85
149
|
provider: embeddingProviderSchema,
|
|
@@ -90,6 +154,25 @@ const spaceCommon = {
|
|
|
90
154
|
prefix: Type.Literal(EMBEDDING_DOCUMENT_PREFIX),
|
|
91
155
|
normalization: Type.Literal(EMBEDDING_NORMALIZATION_VERSION),
|
|
92
156
|
};
|
|
157
|
+
const geminiSpaceCommon = {
|
|
158
|
+
format: Type.Literal(EMBEDDING_FORMAT_VERSION),
|
|
159
|
+
prefix: Type.Union([
|
|
160
|
+
Type.Literal('title: none | text: '),
|
|
161
|
+
Type.Literal('task: search result | query: '),
|
|
162
|
+
Type.Literal('task: question answering | query: '),
|
|
163
|
+
Type.Literal('task: fact checking | query: '),
|
|
164
|
+
Type.Literal('task: code retrieval | query: '),
|
|
165
|
+
Type.Literal('task: classification | query: '),
|
|
166
|
+
Type.Literal('task: clustering | query: '),
|
|
167
|
+
Type.Literal('task: sentence similarity | query: '),
|
|
168
|
+
]),
|
|
169
|
+
normalization: Type.Literal(EMBEDDING_NORMALIZATION_VERSION),
|
|
170
|
+
model: geminiModelSchema,
|
|
171
|
+
revision: Type.Null(),
|
|
172
|
+
quantization: Type.Null(),
|
|
173
|
+
dimensions: geminiDimensionsSchema,
|
|
174
|
+
taskFormat: geminiTaskFormatSchema,
|
|
175
|
+
};
|
|
93
176
|
/**
|
|
94
177
|
* Complete generating-space identity. `provider` is canonical: the gateway route deliberately
|
|
95
178
|
* reports the OpenAI recipe it declares, not its transport alias.
|
|
@@ -122,6 +205,26 @@ export const embeddingSpaceSchema = Type.Union([
|
|
|
122
205
|
runtime: Type.Literal('sarvam-v2'),
|
|
123
206
|
dimensions: Type.Literal(768),
|
|
124
207
|
}, { additionalProperties: false }),
|
|
208
|
+
Type.Object({
|
|
209
|
+
...geminiSpaceCommon,
|
|
210
|
+
provider: Type.Literal('gemini'),
|
|
211
|
+
runtime: Type.Literal('gemini-api-v1beta'),
|
|
212
|
+
}, { additionalProperties: false }),
|
|
213
|
+
Type.Object({
|
|
214
|
+
...geminiSpaceCommon,
|
|
215
|
+
provider: Type.Literal('vertex'),
|
|
216
|
+
runtime: Type.Literal('vertex-ai-v1'),
|
|
217
|
+
}, { additionalProperties: false }),
|
|
218
|
+
Type.Object({
|
|
219
|
+
...spaceCommon,
|
|
220
|
+
provider: Type.Literal('cloudflare'),
|
|
221
|
+
model: cloudflareModelSchema,
|
|
222
|
+
revision: Type.Null(),
|
|
223
|
+
quantization: Type.Null(),
|
|
224
|
+
runtime: Type.Literal('cloudflare-workers-ai'),
|
|
225
|
+
dimensions: Type.Literal(768),
|
|
226
|
+
pooling: Type.Union([Type.Literal('mean'), Type.Literal('cls')]),
|
|
227
|
+
}, { additionalProperties: false }),
|
|
125
228
|
]);
|
|
126
229
|
export const embeddingSettingsSchema = Type.Object({
|
|
127
230
|
version: Type.Optional(Type.Literal(1)),
|
|
@@ -142,6 +245,25 @@ export const embeddingSettingsSchema = Type.Object({
|
|
|
142
245
|
model: gatewayModelSchema,
|
|
143
246
|
stage: Type.Union([Type.Literal('dev'), Type.Literal('prod')]),
|
|
144
247
|
}, { additionalProperties: false })),
|
|
248
|
+
openrouter: Type.Optional(Type.Object({ apiKey: Type.Optional(Type.String({ minLength: 1 })) }, { additionalProperties: false })),
|
|
249
|
+
cloudflare: Type.Optional(Type.Object({
|
|
250
|
+
accountId: Type.Optional(Type.String({ minLength: 1 })),
|
|
251
|
+
apiKey: Type.Optional(Type.String({ minLength: 1 })),
|
|
252
|
+
gatewayId: Type.Optional(Type.String({ minLength: 1 })),
|
|
253
|
+
model: Type.Optional(cloudflareModelSchema),
|
|
254
|
+
pooling: Type.Optional(Type.Union([Type.Literal('mean'), Type.Literal('cls')])),
|
|
255
|
+
}, { additionalProperties: false })),
|
|
256
|
+
vertex: Type.Optional(Type.Object({
|
|
257
|
+
project: Type.String({ minLength: 1, pattern: '^[a-zA-Z0-9_-]+$' }),
|
|
258
|
+
location: vertexLocationSchema,
|
|
259
|
+
dimensions: geminiDimensionsSchema,
|
|
260
|
+
taskFormat: geminiTaskFormatSchema,
|
|
261
|
+
}, { additionalProperties: false })),
|
|
262
|
+
gemini: Type.Optional(Type.Object({
|
|
263
|
+
apiKey: Type.Optional(Type.String({ minLength: 1 })),
|
|
264
|
+
dimensions: Type.Optional(geminiDimensionsSchema),
|
|
265
|
+
taskFormat: Type.Optional(geminiTaskFormatSchema),
|
|
266
|
+
}, { additionalProperties: false })),
|
|
145
267
|
}, { additionalProperties: false })),
|
|
146
268
|
}, { additionalProperties: false });
|
|
147
269
|
export const embeddingRequestSchema = Type.Object({
|
|
@@ -183,8 +305,21 @@ const errorOutcomeSchema = Type.Object({
|
|
|
183
305
|
attemptId: Type.Union([Type.String({ minLength: 1 }), Type.Null()]),
|
|
184
306
|
}, { additionalProperties: false });
|
|
185
307
|
export const embeddingOutcomeSchema = Type.Union([successOutcomeSchema, errorOutcomeSchema]);
|
|
308
|
+
/**
|
|
309
|
+
* Actual generating identity observed on successful outcomes. `generations` is the complete set;
|
|
310
|
+
* the singular fields are populated only when every successful vector came from one generation.
|
|
311
|
+
*/
|
|
312
|
+
export const embeddingGenerationSchema = Type.Object({
|
|
313
|
+
provider: embeddingProviderSchema,
|
|
314
|
+
model: Type.String({ minLength: 1 }),
|
|
315
|
+
space: embeddingSpaceSchema,
|
|
316
|
+
}, { additionalProperties: false });
|
|
186
317
|
export const embeddingResultSchema = Type.Object({
|
|
187
318
|
schemaVersion: Type.Literal(EMBEDDINGS_PROTOCOL_VERSION),
|
|
319
|
+
provider: Type.Union([embeddingProviderSchema, Type.Null()]),
|
|
320
|
+
model: Type.Union([Type.String({ minLength: 1 }), Type.Null()]),
|
|
321
|
+
space: Type.Union([embeddingSpaceSchema, Type.Null()]),
|
|
322
|
+
generations: Type.Array(embeddingGenerationSchema),
|
|
188
323
|
outcomes: Type.Array(embeddingOutcomeSchema),
|
|
189
324
|
attempts: Type.Array(embeddingAttemptSchema),
|
|
190
325
|
}, { additionalProperties: false });
|
|
@@ -198,17 +333,3 @@ export const embeddingCompareResultSchema = Type.Object({
|
|
|
198
333
|
schemaVersion: Type.Literal(EMBEDDINGS_PROTOCOL_VERSION),
|
|
199
334
|
entries: Type.Array(embeddingCompareEntrySchema),
|
|
200
335
|
}, { additionalProperties: false });
|
|
201
|
-
export const embeddingDiagnosticSchema = Type.Object({
|
|
202
|
-
code: Type.String({ minLength: 1 }),
|
|
203
|
-
severity: Type.Union([Type.Literal('error'), Type.Literal('warning'), Type.Literal('info')]),
|
|
204
|
-
}, { additionalProperties: false });
|
|
205
|
-
export const embeddingDescribeSchema = Type.Object({
|
|
206
|
-
schemaVersion: Type.Literal(EMBEDDINGS_PROTOCOL_VERSION),
|
|
207
|
-
ready: Type.Boolean(),
|
|
208
|
-
selected: embeddingRouteSchema,
|
|
209
|
-
space: Type.Union([embeddingSpaceSchema, Type.Null()]),
|
|
210
|
-
default: embeddingRouteSchema,
|
|
211
|
-
fallback: Type.Array(embeddingRouteSchema),
|
|
212
|
-
compare: Type.Array(embeddingRouteSchema),
|
|
213
|
-
diagnostics: Type.Array(embeddingDiagnosticSchema),
|
|
214
|
-
}, { additionalProperties: false });
|
package/dist/settings-write.js
CHANGED
|
@@ -2,13 +2,13 @@ import { randomUUID } from 'node:crypto';
|
|
|
2
2
|
import { lstat, mkdir, open, readFile, rename, unlink } from 'node:fs/promises';
|
|
3
3
|
import { withFileMutationQueue } from '@earendil-works/pi-coding-agent';
|
|
4
4
|
import { join, resolve } from 'node:path';
|
|
5
|
-
import {
|
|
5
|
+
import { configuredDefault } from './settings.js';
|
|
6
6
|
import { EMBEDDINGS_SETTINGS_KEY } from './schemas.js';
|
|
7
7
|
export class EmbeddingsSettingsWriteError extends Error {
|
|
8
8
|
}
|
|
9
9
|
/** Update only the trusted project's `pi-embeddings.default`, preserving every unrelated key. */
|
|
10
10
|
export function updateProjectEmbeddingSettings({ cwd, projectTrusted, route, }) {
|
|
11
|
-
|
|
11
|
+
configuredDefault({ default: route });
|
|
12
12
|
const settingsPath = resolve(cwd, '.pi', 'settings.json');
|
|
13
13
|
return withFileMutationQueue(settingsPath, () => updateProjectEmbeddingSettingsNow({ cwd, projectTrusted, route }));
|
|
14
14
|
}
|
package/dist/settings.js
CHANGED
|
@@ -4,12 +4,16 @@ import { join, resolve } from 'node:path';
|
|
|
4
4
|
import { loadPiSettings, resolveConfigDir } from '@amaster.ai/pi-shared/settings';
|
|
5
5
|
import { Value } from 'typebox/value';
|
|
6
6
|
import { EmbeddingsError } from './errors.js';
|
|
7
|
-
import { EMBEDDINGS_SETTINGS_KEY, GATEWAY_EMBEDDING_MODELS, embeddingSettingsSchema, } from './schemas.js';
|
|
7
|
+
import { CLOUDFLARE_EMBEDDING_MODELS, EMBEDDINGS_SETTINGS_KEY, GATEWAY_EMBEDDING_MODELS, GEMINI_EMBEDDING_MODELS, OPENROUTER_EMBEDDING_MODELS, embeddingSettingsSchema, } from './schemas.js';
|
|
8
8
|
const MODELS_BY_PROVIDER = {
|
|
9
9
|
local: ['embeddinggemma-2'],
|
|
10
10
|
openai: ['text-embedding-3-small'],
|
|
11
11
|
sarvam: ['embeddinggemma-2'],
|
|
12
12
|
gateway: GATEWAY_EMBEDDING_MODELS,
|
|
13
|
+
openrouter: OPENROUTER_EMBEDDING_MODELS,
|
|
14
|
+
cloudflare: CLOUDFLARE_EMBEDDING_MODELS,
|
|
15
|
+
gemini: GEMINI_EMBEDDING_MODELS,
|
|
16
|
+
vertex: GEMINI_EMBEDDING_MODELS,
|
|
13
17
|
};
|
|
14
18
|
export const isEmbeddingSettings = (value) => Value.Check(embeddingSettingsSchema, value);
|
|
15
19
|
function configuration(message) {
|
|
@@ -39,6 +43,9 @@ export function validateEmbeddingSettings(value) {
|
|
|
39
43
|
assertRoute(route, `fallback[${index}]`);
|
|
40
44
|
for (const [index, route] of (value.compare ?? []).entries())
|
|
41
45
|
assertRoute(route, `compare[${index}]`);
|
|
46
|
+
const routes = [value.default, ...(value.fallback ?? []), ...(value.compare ?? [])];
|
|
47
|
+
if (routes.some((route) => route.provider === 'vertex') && value.providers?.vertex === undefined)
|
|
48
|
+
return configuration('configuration-invalid');
|
|
42
49
|
if (value.version !== undefined && value.version !== 1)
|
|
43
50
|
return configuration('configuration-invalid');
|
|
44
51
|
return value;
|