@abhishek944/pi-embeddings 0.2.0 → 0.5.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 +50 -5
- package/dist/execute.js +22 -3
- package/dist/extension.js +25 -7
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/provider-types.d.ts +4 -0
- package/dist/providers.d.ts +17 -4
- package/dist/providers.js +257 -4
- package/dist/response.d.ts +26 -0
- package/dist/response.js +135 -2
- package/dist/runtime.d.ts +6 -1
- package/dist/runtime.js +8 -3
- package/dist/schemas.d.ts +570 -22
- package/dist/schemas.js +156 -0
- package/dist/settings-write.js +2 -2
- package/dist/settings.js +8 -1
- package/dist/tool-contracts.d.ts +261 -9
- package/dist/vertex-provider.d.ts +18 -0
- package/dist/vertex-provider.js +139 -0
- package/package.json +10 -2
- package/src/execute.ts +21 -3
- package/src/extension.ts +45 -7
- package/src/index.ts +16 -0
- package/src/provider-types.ts +7 -0
- package/src/providers.ts +338 -3
- package/src/response.ts +156 -2
- package/src/runtime.ts +25 -6
- package/src/schemas.ts +199 -0
- package/src/settings-write.ts +2 -2
- package/src/settings.ts +10 -0
- package/src/vertex-provider.ts +152 -0
package/src/response.ts
CHANGED
|
@@ -17,12 +17,48 @@ const responseSchema = Type.Object({
|
|
|
17
17
|
}),
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
+
const openCodeResponseSchema = Type.Object({
|
|
21
|
+
model: Type.Optional(Type.String({ minLength: 1 })),
|
|
22
|
+
data: Type.Array(
|
|
23
|
+
Type.Object({
|
|
24
|
+
index: Type.Integer({ minimum: 0 }),
|
|
25
|
+
embedding: Type.Array(Type.Number()),
|
|
26
|
+
})
|
|
27
|
+
),
|
|
28
|
+
usage: Type.Optional(
|
|
29
|
+
Type.Object({
|
|
30
|
+
prompt_tokens: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
31
|
+
})
|
|
32
|
+
),
|
|
33
|
+
});
|
|
34
|
+
|
|
20
35
|
const cloudflareResponseSchema = Type.Object({
|
|
21
36
|
shape: Type.Optional(Type.Array(Type.Integer({ minimum: 0 }))),
|
|
22
37
|
data: Type.Array(Type.Array(Type.Number())),
|
|
23
38
|
pooling: Type.Optional(Type.Union([Type.Literal('mean'), Type.Literal('cls')])),
|
|
24
39
|
});
|
|
25
40
|
|
|
41
|
+
const geminiEmbeddingResponseSchema = Type.Object({
|
|
42
|
+
embedding: Type.Object({ values: Type.Array(Type.Number()) }),
|
|
43
|
+
usageMetadata: Type.Object({ promptTokenCount: Type.Integer({ minimum: 0 }) }),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const vertexEmbeddingResponseSchema = Type.Object({
|
|
47
|
+
...geminiEmbeddingResponseSchema.properties,
|
|
48
|
+
truncated: Type.Optional(Type.Boolean()),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const geminiCountTokensResponseSchema = Type.Object({
|
|
52
|
+
totalTokens: Type.Integer({ minimum: 0 }),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/** Exact prepared-prompt count from the model's own tokenizer, used before inference. */
|
|
56
|
+
export function parseGeminiCountTokensResponse(response: unknown): number {
|
|
57
|
+
if (!Value.Check(geminiCountTokensResponseSchema, response))
|
|
58
|
+
throw new EmbeddingsError('provider-response-invalid');
|
|
59
|
+
return response.totalTokens;
|
|
60
|
+
}
|
|
61
|
+
|
|
26
62
|
/** Cloudflare REST wraps model output in a standard API envelope; bindings return the raw model output. */
|
|
27
63
|
function unwrapCloudflareModelOutput(response: unknown): unknown {
|
|
28
64
|
if (typeof response !== 'object' || response === null || Array.isArray(response))
|
|
@@ -46,6 +82,11 @@ export function normalizeVector(vector: readonly number[], dimensions: number):
|
|
|
46
82
|
return vector.map((value) => value / scale / norm);
|
|
47
83
|
}
|
|
48
84
|
|
|
85
|
+
function dimensionsOf(space: EmbeddingSpace): number {
|
|
86
|
+
if (typeof space.dimensions !== 'number') throw new EmbeddingsError('vector-invalid');
|
|
87
|
+
return space.dimensions;
|
|
88
|
+
}
|
|
89
|
+
|
|
49
90
|
export function parseBatchResponse(args: {
|
|
50
91
|
response: unknown;
|
|
51
92
|
space: EmbeddingSpace;
|
|
@@ -76,7 +117,7 @@ export function parseBatchResponse(args: {
|
|
|
76
117
|
throw known('provider-response-mismatch');
|
|
77
118
|
let vectors: number[][];
|
|
78
119
|
try {
|
|
79
|
-
vectors = ordered.map((item) => normalizeVector(item.embedding, args.space
|
|
120
|
+
vectors = ordered.map((item) => normalizeVector(item.embedding, dimensionsOf(args.space)));
|
|
80
121
|
} catch (error) {
|
|
81
122
|
if (error instanceof EmbeddingsError)
|
|
82
123
|
throw known(error.code === 'vector-zero' ? 'vector-zero' : 'vector-invalid');
|
|
@@ -92,6 +133,119 @@ export function parseBatchResponse(args: {
|
|
|
92
133
|
};
|
|
93
134
|
}
|
|
94
135
|
|
|
136
|
+
/**
|
|
137
|
+
* OpenCode's OpenAI-compatible embeddings response does not publish per-model dimensions, so the
|
|
138
|
+
* dimension is observed from the returned vectors and included in the actual generating space.
|
|
139
|
+
*/
|
|
140
|
+
export function parseOpenCodeBatchResponse(args: {
|
|
141
|
+
response: unknown;
|
|
142
|
+
expectedModels: readonly string[];
|
|
143
|
+
expectedCount: number;
|
|
144
|
+
}): ProviderBatchResult & { readonly dimensions: number } {
|
|
145
|
+
if (!Value.Check(openCodeResponseSchema, args.response))
|
|
146
|
+
throw new EmbeddingsError('provider-response-invalid', { knownUsage: {} });
|
|
147
|
+
const parsed = args.response;
|
|
148
|
+
const providerReportedInputTokens = parsed.usage?.prompt_tokens ?? null;
|
|
149
|
+
const knownUsage = {
|
|
150
|
+
providerReportedInputTokens,
|
|
151
|
+
preparedInputTokens: null,
|
|
152
|
+
};
|
|
153
|
+
const fail = (
|
|
154
|
+
code: 'provider-response-mismatch' | 'vector-invalid' | 'vector-zero'
|
|
155
|
+
): never => {
|
|
156
|
+
throw new EmbeddingsError(code, { knownUsage });
|
|
157
|
+
};
|
|
158
|
+
if (parsed.model !== undefined && !args.expectedModels.includes(parsed.model))
|
|
159
|
+
fail('provider-response-mismatch');
|
|
160
|
+
const ordered = [...parsed.data].sort((left, right) => left.index - right.index);
|
|
161
|
+
if (ordered.length !== args.expectedCount || ordered.some((item, index) => item.index !== index))
|
|
162
|
+
fail('provider-response-mismatch');
|
|
163
|
+
const first = ordered[0];
|
|
164
|
+
if (first === undefined) throw new EmbeddingsError('provider-response-mismatch', { knownUsage });
|
|
165
|
+
const dimensions = first.embedding.length;
|
|
166
|
+
if (dimensions === 0) fail('vector-invalid');
|
|
167
|
+
let vectors: number[][];
|
|
168
|
+
try {
|
|
169
|
+
vectors = ordered.map((item) => normalizeVector(item.embedding, dimensions));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error instanceof EmbeddingsError)
|
|
172
|
+
fail(error.code === 'vector-zero' ? 'vector-zero' : 'vector-invalid');
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
vectors,
|
|
177
|
+
usage: {
|
|
178
|
+
providerReportedInputTokens,
|
|
179
|
+
preparedInputTokens: null,
|
|
180
|
+
billedTokens: null,
|
|
181
|
+
},
|
|
182
|
+
dimensions,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Gemini `embedContent` returns one embedding and provider-counted prompt tokens. A vector is accepted
|
|
188
|
+
* only when that response count matches the exact prepared prompt counted before inference; a mismatch
|
|
189
|
+
* or over-limit count fails closed instead of relying on undocumented truncation semantics.
|
|
190
|
+
*/
|
|
191
|
+
export function parseGeminiEmbeddingResponse(args: {
|
|
192
|
+
response: unknown;
|
|
193
|
+
space: EmbeddingSpace;
|
|
194
|
+
maxInputTokens: number;
|
|
195
|
+
preparedInputTokens: number;
|
|
196
|
+
}): ProviderBatchResult {
|
|
197
|
+
if (!Value.Check(geminiEmbeddingResponseSchema, args.response))
|
|
198
|
+
throw new EmbeddingsError('provider-response-invalid', {
|
|
199
|
+
knownUsage: { preparedInputTokens: args.preparedInputTokens },
|
|
200
|
+
});
|
|
201
|
+
const parsed = args.response;
|
|
202
|
+
const promptTokens = parsed.usageMetadata.promptTokenCount;
|
|
203
|
+
const knownUsage = {
|
|
204
|
+
providerReportedInputTokens: promptTokens,
|
|
205
|
+
preparedInputTokens: args.preparedInputTokens,
|
|
206
|
+
};
|
|
207
|
+
if (promptTokens !== args.preparedInputTokens)
|
|
208
|
+
throw new EmbeddingsError('provider-response-mismatch', { knownUsage });
|
|
209
|
+
if (promptTokens > args.maxInputTokens)
|
|
210
|
+
throw new EmbeddingsError('input-too-long', { knownUsage });
|
|
211
|
+
let vector: number[];
|
|
212
|
+
try {
|
|
213
|
+
vector = normalizeVector(parsed.embedding.values, dimensionsOf(args.space));
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error instanceof EmbeddingsError)
|
|
216
|
+
throw new EmbeddingsError(error.code === 'vector-zero' ? 'vector-zero' : 'vector-invalid', {
|
|
217
|
+
knownUsage,
|
|
218
|
+
});
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
vectors: [vector],
|
|
223
|
+
usage: {
|
|
224
|
+
providerReportedInputTokens: promptTokens,
|
|
225
|
+
preparedInputTokens: args.preparedInputTokens,
|
|
226
|
+
billedTokens: null,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Vertex's explicit truncation flag is checked in addition to the exact preflight token count. */
|
|
232
|
+
export function parseVertexEmbeddingResponse(
|
|
233
|
+
args: Parameters<typeof parseGeminiEmbeddingResponse>[0]
|
|
234
|
+
): ProviderBatchResult {
|
|
235
|
+
if (!Value.Check(vertexEmbeddingResponseSchema, args.response))
|
|
236
|
+
throw new EmbeddingsError('provider-response-invalid', {
|
|
237
|
+
knownUsage: { preparedInputTokens: args.preparedInputTokens },
|
|
238
|
+
});
|
|
239
|
+
if (args.response.truncated === true)
|
|
240
|
+
throw new EmbeddingsError('input-too-long', {
|
|
241
|
+
knownUsage: {
|
|
242
|
+
providerReportedInputTokens: args.response.usageMetadata.promptTokenCount,
|
|
243
|
+
preparedInputTokens: args.preparedInputTokens,
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
return parseGeminiEmbeddingResponse(args);
|
|
247
|
+
}
|
|
248
|
+
|
|
95
249
|
/** Cloudflare Workers AI `/ai/run` wraps ordered vectors without usage in the REST API envelope. */
|
|
96
250
|
export function parseCloudflareBatchResponse(args: {
|
|
97
251
|
response: unknown;
|
|
@@ -106,7 +260,7 @@ export function parseCloudflareBatchResponse(args: {
|
|
|
106
260
|
throw new EmbeddingsError('provider-response-mismatch', { knownUsage: {} });
|
|
107
261
|
let vectors: number[][];
|
|
108
262
|
try {
|
|
109
|
-
vectors = parsed.data.map((vector) => normalizeVector(vector, args.space
|
|
263
|
+
vectors = parsed.data.map((vector) => normalizeVector(vector, dimensionsOf(args.space)));
|
|
110
264
|
} catch (error) {
|
|
111
265
|
if (error instanceof EmbeddingsError)
|
|
112
266
|
throw new EmbeddingsError(error.code === 'vector-zero' ? 'vector-zero' : 'vector-invalid', {
|
package/src/runtime.ts
CHANGED
|
@@ -5,7 +5,9 @@ import {
|
|
|
5
5
|
spaceForRoute,
|
|
6
6
|
type AcquiredProvider,
|
|
7
7
|
type EmbeddingDiagnostic,
|
|
8
|
+
type OpenCodeCredentialAvailability,
|
|
8
9
|
} from './providers.js';
|
|
10
|
+
import type { EmbeddingCredentialResolver } from './provider-types.js';
|
|
9
11
|
import { configuredDefault, configuredRoutes, validateEmbeddingSettings } from './settings.js';
|
|
10
12
|
import { executeCompare, executeEmbeddings } from './execute.js';
|
|
11
13
|
import { EmbeddingsError } from './errors.js';
|
|
@@ -54,6 +56,8 @@ const noSignal = (signal: AbortSignal | undefined): AbortSignal => signal ?? new
|
|
|
54
56
|
function createRuntime(args: {
|
|
55
57
|
settings: EmbeddingSettings;
|
|
56
58
|
cwd?: string | undefined;
|
|
59
|
+
credentialResolver?: EmbeddingCredentialResolver | undefined;
|
|
60
|
+
piCredentialAvailable?: OpenCodeCredentialAvailability | undefined;
|
|
57
61
|
}): EmbeddingsRuntimeWithDiagnostics {
|
|
58
62
|
const settings = validateEmbeddingSettings(args.settings);
|
|
59
63
|
const defaultRoute = configuredDefault(settings);
|
|
@@ -76,7 +80,12 @@ function createRuntime(args: {
|
|
|
76
80
|
if (cached !== undefined) return Promise.resolve(cached);
|
|
77
81
|
const inFlight = pending.get(key);
|
|
78
82
|
if (inFlight !== undefined) return inFlight;
|
|
79
|
-
const promise = acquireProvider({
|
|
83
|
+
const promise = acquireProvider({
|
|
84
|
+
settings,
|
|
85
|
+
route,
|
|
86
|
+
signal: lifetime.signal,
|
|
87
|
+
credentialResolver: args.credentialResolver,
|
|
88
|
+
})
|
|
80
89
|
.then((entry) => {
|
|
81
90
|
pending.delete(key);
|
|
82
91
|
if (closed || closing !== undefined) {
|
|
@@ -131,13 +140,19 @@ function createRuntime(args: {
|
|
|
131
140
|
});
|
|
132
141
|
},
|
|
133
142
|
describe() {
|
|
134
|
-
const defaultDiagnostics = routeDiagnostics(
|
|
143
|
+
const defaultDiagnostics = routeDiagnostics(
|
|
144
|
+
settings,
|
|
145
|
+
defaultRoute,
|
|
146
|
+
args.piCredentialAvailable ?? {}
|
|
147
|
+
);
|
|
135
148
|
const diagnostics = [...defaultDiagnostics];
|
|
136
149
|
for (const route of fallbackRoutes)
|
|
137
|
-
diagnostics.push(
|
|
138
|
-
...entry
|
|
139
|
-
|
|
140
|
-
|
|
150
|
+
diagnostics.push(
|
|
151
|
+
...routeDiagnostics(settings, route, args.piCredentialAvailable ?? {}).map((entry) => ({
|
|
152
|
+
...entry,
|
|
153
|
+
severity: 'warning' as const,
|
|
154
|
+
}))
|
|
155
|
+
);
|
|
141
156
|
return {
|
|
142
157
|
schemaVersion: EMBEDDINGS_PROTOCOL_VERSION,
|
|
143
158
|
ready: defaultDiagnostics.length === 0,
|
|
@@ -170,6 +185,8 @@ function createRuntime(args: {
|
|
|
170
185
|
export function createEmbeddingsRuntime(args: {
|
|
171
186
|
settings: EmbeddingSettings;
|
|
172
187
|
cwd?: string | undefined;
|
|
188
|
+
credentialResolver?: EmbeddingCredentialResolver | undefined;
|
|
189
|
+
piCredentialAvailable?: OpenCodeCredentialAvailability | undefined;
|
|
173
190
|
}): EmbeddingsRuntime {
|
|
174
191
|
const { start, embed, compare, close } = createRuntime(args);
|
|
175
192
|
return { start, embed, compare, close };
|
|
@@ -179,6 +196,8 @@ export function createEmbeddingsRuntime(args: {
|
|
|
179
196
|
export function createEmbeddingsRuntimeWithDiagnostics(args: {
|
|
180
197
|
settings: EmbeddingSettings;
|
|
181
198
|
cwd?: string | undefined;
|
|
199
|
+
credentialResolver?: EmbeddingCredentialResolver | undefined;
|
|
200
|
+
piCredentialAvailable?: OpenCodeCredentialAvailability | undefined;
|
|
182
201
|
}): EmbeddingsRuntimeWithDiagnostics {
|
|
183
202
|
return createRuntime(args);
|
|
184
203
|
}
|
package/src/schemas.ts
CHANGED
|
@@ -21,16 +21,76 @@ export const EMBEDDING_ROUTE_IDS = [
|
|
|
21
21
|
'gateway',
|
|
22
22
|
'openrouter',
|
|
23
23
|
'cloudflare',
|
|
24
|
+
'gemini',
|
|
25
|
+
'vertex',
|
|
26
|
+
'opencode',
|
|
27
|
+
'opencode-go',
|
|
24
28
|
] as const;
|
|
25
29
|
export type EmbeddingRouteId = (typeof EMBEDDING_ROUTE_IDS)[number];
|
|
30
|
+
export type OpenCodeRouteId = Extract<EmbeddingRouteId, 'opencode' | 'opencode-go'>;
|
|
26
31
|
|
|
27
32
|
export const GATEWAY_EMBEDDING_MODELS = [
|
|
28
33
|
'text-embedding-3-small',
|
|
29
34
|
'openai/text-embedding-3-small',
|
|
30
35
|
] as const;
|
|
31
36
|
|
|
37
|
+
/** Console (pay-as-you-go) embedding model IDs documented by OpenCode. */
|
|
38
|
+
export const OPENCODE_EMBEDDING_MODELS = [
|
|
39
|
+
'text-embedding-3-small',
|
|
40
|
+
'text-embedding-3-large',
|
|
41
|
+
'gemini-embedding-2',
|
|
42
|
+
'gemini-embedding-001',
|
|
43
|
+
'codestral-embed',
|
|
44
|
+
'mistral-embed',
|
|
45
|
+
'cohere-embed-v5-pro',
|
|
46
|
+
'cohere-embed-v5-fast',
|
|
47
|
+
'voyage-4',
|
|
48
|
+
'voyage-code-4',
|
|
49
|
+
'qwen3-embedding-8b',
|
|
50
|
+
'bge-m3',
|
|
51
|
+
] as const;
|
|
52
|
+
export type OpenCodeEmbeddingModel = (typeof OPENCODE_EMBEDDING_MODELS)[number];
|
|
53
|
+
|
|
54
|
+
/** Go subscription embedding model IDs documented by OpenCode. */
|
|
55
|
+
export const OPENCODE_GO_EMBEDDING_MODELS = [
|
|
56
|
+
'text-embedding-3-small',
|
|
57
|
+
'qwen3-embedding-8b',
|
|
58
|
+
'gemini-embedding-2',
|
|
59
|
+
] as const;
|
|
60
|
+
export type OpenCodeGoEmbeddingModel = (typeof OPENCODE_GO_EMBEDDING_MODELS)[number];
|
|
61
|
+
|
|
32
62
|
export const OPENROUTER_EMBEDDING_MODELS = ['openai/text-embedding-3-small'] as const;
|
|
33
63
|
export const CLOUDFLARE_EMBEDDING_MODELS = ['@cf/baai/bge-base-en-v1.5'] as const;
|
|
64
|
+
export const GEMINI_EMBEDDING_MODELS = ['gemini-embedding-2'] as const;
|
|
65
|
+
export const GEMINI_EMBEDDING_MIN_DIMENSIONS = 128;
|
|
66
|
+
export const GEMINI_EMBEDDING_MAX_DIMENSIONS = 3072;
|
|
67
|
+
export const DEFAULT_GEMINI_DIMENSIONS = 3072;
|
|
68
|
+
export const GEMINI_EMBEDDING_TASK_FORMATS = [
|
|
69
|
+
'retrieval-document',
|
|
70
|
+
'search-query',
|
|
71
|
+
'question-answering',
|
|
72
|
+
'fact-checking',
|
|
73
|
+
'code-retrieval',
|
|
74
|
+
'classification',
|
|
75
|
+
'clustering',
|
|
76
|
+
'sentence-similarity',
|
|
77
|
+
] as const;
|
|
78
|
+
export const DEFAULT_GEMINI_TASK_FORMAT: GeminiEmbeddingTaskFormat = 'retrieval-document';
|
|
79
|
+
|
|
80
|
+
export type GeminiEmbeddingDimension = number;
|
|
81
|
+
export type GeminiEmbeddingTaskFormat = (typeof GEMINI_EMBEDDING_TASK_FORMATS)[number];
|
|
82
|
+
|
|
83
|
+
/** Official Gemini Embedding 2 text task prefixes. `task_type` is not used for this model. */
|
|
84
|
+
export const GEMINI_TASK_PREFIXES = {
|
|
85
|
+
'retrieval-document': 'title: none | text: ',
|
|
86
|
+
'search-query': 'task: search result | query: ',
|
|
87
|
+
'question-answering': 'task: question answering | query: ',
|
|
88
|
+
'fact-checking': 'task: fact checking | query: ',
|
|
89
|
+
'code-retrieval': 'task: code retrieval | query: ',
|
|
90
|
+
classification: 'task: classification | query: ',
|
|
91
|
+
clustering: 'task: clustering | query: ',
|
|
92
|
+
'sentence-similarity': 'task: sentence similarity | query: ',
|
|
93
|
+
} as const satisfies Record<GeminiEmbeddingTaskFormat, string>;
|
|
34
94
|
|
|
35
95
|
const gatewayModelSchema = Type.Union([
|
|
36
96
|
Type.Literal('text-embedding-3-small'),
|
|
@@ -39,6 +99,46 @@ const gatewayModelSchema = Type.Union([
|
|
|
39
99
|
|
|
40
100
|
const openRouterModelSchema = Type.Literal('openai/text-embedding-3-small');
|
|
41
101
|
const cloudflareModelSchema = Type.Literal('@cf/baai/bge-base-en-v1.5');
|
|
102
|
+
const geminiModelSchema = Type.Literal(GEMINI_EMBEDDING_MODELS[0]);
|
|
103
|
+
const openCodeModelSchema = Type.Union([
|
|
104
|
+
Type.Literal('text-embedding-3-small'),
|
|
105
|
+
Type.Literal('text-embedding-3-large'),
|
|
106
|
+
Type.Literal('gemini-embedding-2'),
|
|
107
|
+
Type.Literal('gemini-embedding-001'),
|
|
108
|
+
Type.Literal('codestral-embed'),
|
|
109
|
+
Type.Literal('mistral-embed'),
|
|
110
|
+
Type.Literal('cohere-embed-v5-pro'),
|
|
111
|
+
Type.Literal('cohere-embed-v5-fast'),
|
|
112
|
+
Type.Literal('voyage-4'),
|
|
113
|
+
Type.Literal('voyage-code-4'),
|
|
114
|
+
Type.Literal('qwen3-embedding-8b'),
|
|
115
|
+
Type.Literal('bge-m3'),
|
|
116
|
+
]);
|
|
117
|
+
const openCodeGoModelSchema = Type.Union([
|
|
118
|
+
Type.Literal('text-embedding-3-small'),
|
|
119
|
+
Type.Literal('qwen3-embedding-8b'),
|
|
120
|
+
Type.Literal('gemini-embedding-2'),
|
|
121
|
+
]);
|
|
122
|
+
export const VERTEX_EMBEDDING_LOCATIONS = ['us', 'eu', 'global'] as const;
|
|
123
|
+
const vertexLocationSchema = Type.Union([
|
|
124
|
+
Type.Literal('us'),
|
|
125
|
+
Type.Literal('eu'),
|
|
126
|
+
Type.Literal('global'),
|
|
127
|
+
]);
|
|
128
|
+
const geminiDimensionsSchema = Type.Integer({
|
|
129
|
+
minimum: GEMINI_EMBEDDING_MIN_DIMENSIONS,
|
|
130
|
+
maximum: GEMINI_EMBEDDING_MAX_DIMENSIONS,
|
|
131
|
+
});
|
|
132
|
+
const geminiTaskFormatSchema = Type.Union([
|
|
133
|
+
Type.Literal('retrieval-document'),
|
|
134
|
+
Type.Literal('search-query'),
|
|
135
|
+
Type.Literal('question-answering'),
|
|
136
|
+
Type.Literal('fact-checking'),
|
|
137
|
+
Type.Literal('code-retrieval'),
|
|
138
|
+
Type.Literal('classification'),
|
|
139
|
+
Type.Literal('clustering'),
|
|
140
|
+
Type.Literal('sentence-similarity'),
|
|
141
|
+
]);
|
|
42
142
|
|
|
43
143
|
const embeddingErrorCodeSchema = Type.Union([
|
|
44
144
|
Type.Literal('configuration-invalid'),
|
|
@@ -104,6 +204,10 @@ const embeddingProviderSchema = Type.Union([
|
|
|
104
204
|
Type.Literal('gateway'),
|
|
105
205
|
Type.Literal('openrouter'),
|
|
106
206
|
Type.Literal('cloudflare'),
|
|
207
|
+
Type.Literal('gemini'),
|
|
208
|
+
Type.Literal('vertex'),
|
|
209
|
+
Type.Literal('opencode'),
|
|
210
|
+
Type.Literal('opencode-go'),
|
|
107
211
|
]);
|
|
108
212
|
|
|
109
213
|
export const embeddingRouteSchema = Type.Object(
|
|
@@ -120,6 +224,34 @@ const spaceCommon = {
|
|
|
120
224
|
normalization: Type.Literal(EMBEDDING_NORMALIZATION_VERSION),
|
|
121
225
|
};
|
|
122
226
|
|
|
227
|
+
const openCodeSpaceCommon = {
|
|
228
|
+
...spaceCommon,
|
|
229
|
+
revision: Type.Null(),
|
|
230
|
+
quantization: Type.Null(),
|
|
231
|
+
// Observed only after a successful response; preflight descriptors never invent a dimension.
|
|
232
|
+
dimensions: Type.Union([Type.Integer({ minimum: 1 }), Type.Null()]),
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const geminiSpaceCommon = {
|
|
236
|
+
format: Type.Literal(EMBEDDING_FORMAT_VERSION),
|
|
237
|
+
prefix: Type.Union([
|
|
238
|
+
Type.Literal('title: none | text: '),
|
|
239
|
+
Type.Literal('task: search result | query: '),
|
|
240
|
+
Type.Literal('task: question answering | query: '),
|
|
241
|
+
Type.Literal('task: fact checking | query: '),
|
|
242
|
+
Type.Literal('task: code retrieval | query: '),
|
|
243
|
+
Type.Literal('task: classification | query: '),
|
|
244
|
+
Type.Literal('task: clustering | query: '),
|
|
245
|
+
Type.Literal('task: sentence similarity | query: '),
|
|
246
|
+
]),
|
|
247
|
+
normalization: Type.Literal(EMBEDDING_NORMALIZATION_VERSION),
|
|
248
|
+
model: geminiModelSchema,
|
|
249
|
+
revision: Type.Null(),
|
|
250
|
+
quantization: Type.Null(),
|
|
251
|
+
dimensions: geminiDimensionsSchema,
|
|
252
|
+
taskFormat: geminiTaskFormatSchema,
|
|
253
|
+
};
|
|
254
|
+
|
|
123
255
|
/**
|
|
124
256
|
* Complete generating-space identity. `provider` is canonical: the gateway route deliberately
|
|
125
257
|
* reports the OpenAI recipe it declares, not its transport alias.
|
|
@@ -161,6 +293,22 @@ export const embeddingSpaceSchema = Type.Union([
|
|
|
161
293
|
},
|
|
162
294
|
{ additionalProperties: false }
|
|
163
295
|
),
|
|
296
|
+
Type.Object(
|
|
297
|
+
{
|
|
298
|
+
...geminiSpaceCommon,
|
|
299
|
+
provider: Type.Literal('gemini'),
|
|
300
|
+
runtime: Type.Literal('gemini-api-v1beta'),
|
|
301
|
+
},
|
|
302
|
+
{ additionalProperties: false }
|
|
303
|
+
),
|
|
304
|
+
Type.Object(
|
|
305
|
+
{
|
|
306
|
+
...geminiSpaceCommon,
|
|
307
|
+
provider: Type.Literal('vertex'),
|
|
308
|
+
runtime: Type.Literal('vertex-ai-v1'),
|
|
309
|
+
},
|
|
310
|
+
{ additionalProperties: false }
|
|
311
|
+
),
|
|
164
312
|
Type.Object(
|
|
165
313
|
{
|
|
166
314
|
...spaceCommon,
|
|
@@ -174,6 +322,24 @@ export const embeddingSpaceSchema = Type.Union([
|
|
|
174
322
|
},
|
|
175
323
|
{ additionalProperties: false }
|
|
176
324
|
),
|
|
325
|
+
Type.Object(
|
|
326
|
+
{
|
|
327
|
+
...openCodeSpaceCommon,
|
|
328
|
+
provider: Type.Literal('opencode'),
|
|
329
|
+
model: openCodeModelSchema,
|
|
330
|
+
runtime: Type.Literal('opencode-console-embeddings-v1'),
|
|
331
|
+
},
|
|
332
|
+
{ additionalProperties: false }
|
|
333
|
+
),
|
|
334
|
+
Type.Object(
|
|
335
|
+
{
|
|
336
|
+
...openCodeSpaceCommon,
|
|
337
|
+
provider: Type.Literal('opencode-go'),
|
|
338
|
+
model: openCodeGoModelSchema,
|
|
339
|
+
runtime: Type.Literal('opencode-go-embeddings-v1'),
|
|
340
|
+
},
|
|
341
|
+
{ additionalProperties: false }
|
|
342
|
+
),
|
|
177
343
|
]);
|
|
178
344
|
|
|
179
345
|
export const embeddingSettingsSchema = Type.Object(
|
|
@@ -238,6 +404,39 @@ export const embeddingSettingsSchema = Type.Object(
|
|
|
238
404
|
{ additionalProperties: false }
|
|
239
405
|
)
|
|
240
406
|
),
|
|
407
|
+
vertex: Type.Optional(
|
|
408
|
+
Type.Object(
|
|
409
|
+
{
|
|
410
|
+
project: Type.String({ minLength: 1, pattern: '^[a-zA-Z0-9_-]+$' }),
|
|
411
|
+
location: vertexLocationSchema,
|
|
412
|
+
dimensions: geminiDimensionsSchema,
|
|
413
|
+
taskFormat: geminiTaskFormatSchema,
|
|
414
|
+
},
|
|
415
|
+
{ additionalProperties: false }
|
|
416
|
+
)
|
|
417
|
+
),
|
|
418
|
+
gemini: Type.Optional(
|
|
419
|
+
Type.Object(
|
|
420
|
+
{
|
|
421
|
+
apiKey: Type.Optional(Type.String({ minLength: 1 })),
|
|
422
|
+
dimensions: Type.Optional(geminiDimensionsSchema),
|
|
423
|
+
taskFormat: Type.Optional(geminiTaskFormatSchema),
|
|
424
|
+
},
|
|
425
|
+
{ additionalProperties: false }
|
|
426
|
+
)
|
|
427
|
+
),
|
|
428
|
+
opencode: Type.Optional(
|
|
429
|
+
Type.Object(
|
|
430
|
+
{ apiKey: Type.Optional(Type.String({ minLength: 1 })) },
|
|
431
|
+
{ additionalProperties: false }
|
|
432
|
+
)
|
|
433
|
+
),
|
|
434
|
+
'opencode-go': Type.Optional(
|
|
435
|
+
Type.Object(
|
|
436
|
+
{ apiKey: Type.Optional(Type.String({ minLength: 1 })) },
|
|
437
|
+
{ additionalProperties: false }
|
|
438
|
+
)
|
|
439
|
+
),
|
|
241
440
|
},
|
|
242
441
|
{ additionalProperties: false }
|
|
243
442
|
)
|
package/src/settings-write.ts
CHANGED
|
@@ -2,7 +2,7 @@ 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, type EmbeddingRoute } from './schemas.js';
|
|
7
7
|
|
|
8
8
|
export class EmbeddingsSettingsWriteError extends Error {}
|
|
@@ -17,7 +17,7 @@ export function updateProjectEmbeddingSettings({
|
|
|
17
17
|
projectTrusted: boolean;
|
|
18
18
|
route: EmbeddingRoute;
|
|
19
19
|
}): Promise<void> {
|
|
20
|
-
|
|
20
|
+
configuredDefault({ default: route });
|
|
21
21
|
const settingsPath = resolve(cwd, '.pi', 'settings.json');
|
|
22
22
|
return withFileMutationQueue(settingsPath, () =>
|
|
23
23
|
updateProjectEmbeddingSettingsNow({ cwd, projectTrusted, route })
|
package/src/settings.ts
CHANGED
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
CLOUDFLARE_EMBEDDING_MODELS,
|
|
9
9
|
EMBEDDINGS_SETTINGS_KEY,
|
|
10
10
|
GATEWAY_EMBEDDING_MODELS,
|
|
11
|
+
GEMINI_EMBEDDING_MODELS,
|
|
12
|
+
OPENCODE_EMBEDDING_MODELS,
|
|
13
|
+
OPENCODE_GO_EMBEDDING_MODELS,
|
|
11
14
|
OPENROUTER_EMBEDDING_MODELS,
|
|
12
15
|
embeddingSettingsSchema,
|
|
13
16
|
type EmbeddingRoute,
|
|
@@ -21,6 +24,10 @@ const MODELS_BY_PROVIDER: Readonly<Record<EmbeddingRoute['provider'], readonly s
|
|
|
21
24
|
gateway: GATEWAY_EMBEDDING_MODELS,
|
|
22
25
|
openrouter: OPENROUTER_EMBEDDING_MODELS,
|
|
23
26
|
cloudflare: CLOUDFLARE_EMBEDDING_MODELS,
|
|
27
|
+
gemini: GEMINI_EMBEDDING_MODELS,
|
|
28
|
+
vertex: GEMINI_EMBEDDING_MODELS,
|
|
29
|
+
opencode: OPENCODE_EMBEDDING_MODELS,
|
|
30
|
+
'opencode-go': OPENCODE_GO_EMBEDDING_MODELS,
|
|
24
31
|
};
|
|
25
32
|
|
|
26
33
|
export const isEmbeddingSettings = (value: unknown): value is EmbeddingSettings =>
|
|
@@ -56,6 +63,9 @@ export function validateEmbeddingSettings(value: unknown): EmbeddingSettings {
|
|
|
56
63
|
assertRoute(route, `fallback[${index}]`);
|
|
57
64
|
for (const [index, route] of (value.compare ?? []).entries())
|
|
58
65
|
assertRoute(route, `compare[${index}]`);
|
|
66
|
+
const routes = [value.default, ...(value.fallback ?? []), ...(value.compare ?? [])];
|
|
67
|
+
if (routes.some((route) => route.provider === 'vertex') && value.providers?.vertex === undefined)
|
|
68
|
+
return configuration('configuration-invalid');
|
|
59
69
|
if (value.version !== undefined && value.version !== 1)
|
|
60
70
|
return configuration('configuration-invalid');
|
|
61
71
|
return value;
|