@abhishek944/pi-embeddings 0.0.0-stage → 0.2.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/LICENSE +201 -0
- package/README.md +97 -2
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +41 -0
- package/dist/execute.d.ts +25 -0
- package/dist/execute.js +264 -0
- package/dist/extension.d.ts +2 -0
- package/dist/extension.js +164 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/local-runtime.d.ts +14 -0
- package/dist/local-runtime.js +246 -0
- package/dist/mcp-cli.d.ts +2 -0
- package/dist/mcp-cli.js +46 -0
- package/dist/mcp-server.d.ts +23 -0
- package/dist/mcp-server.js +141 -0
- package/dist/provider-http.d.ts +11 -0
- package/dist/provider-http.js +60 -0
- package/dist/provider-types.d.ts +30 -0
- package/dist/provider-types.js +1 -0
- package/dist/providers.d.ts +25 -0
- package/dist/providers.js +415 -0
- package/dist/response.d.ts +17 -0
- package/dist/response.js +105 -0
- package/dist/runtime.d.ts +39 -0
- package/dist/runtime.js +130 -0
- package/dist/schemas.d.ts +758 -0
- package/dist/schemas.js +244 -0
- package/dist/settings-write.d.ts +9 -0
- package/dist/settings-write.js +77 -0
- package/dist/settings.d.ts +8 -0
- package/dist/settings.js +99 -0
- package/dist/tool-contracts.d.ts +343 -0
- package/dist/tool-contracts.js +10 -0
- package/package.json +83 -4
- package/src/errors.ts +52 -0
- package/src/execute.ts +300 -0
- package/src/extension.ts +210 -0
- package/src/index.ts +36 -0
- package/src/local-runtime.ts +273 -0
- package/src/mcp-cli.ts +52 -0
- package/src/mcp-server.ts +187 -0
- package/src/provider-http.ts +67 -0
- package/src/provider-types.ts +33 -0
- package/src/providers.ts +517 -0
- package/src/response.ts +125 -0
- package/src/runtime.ts +184 -0
- package/src/schemas.ts +368 -0
- package/src/settings-write.ts +96 -0
- package/src/settings.ts +119 -0
- package/src/tool-contracts.ts +22 -0
package/src/settings.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { loadPiSettings, resolveConfigDir } from '@amaster.ai/pi-shared/settings';
|
|
5
|
+
import { Value } from 'typebox/value';
|
|
6
|
+
import { EmbeddingsError } from './errors.js';
|
|
7
|
+
import {
|
|
8
|
+
CLOUDFLARE_EMBEDDING_MODELS,
|
|
9
|
+
EMBEDDINGS_SETTINGS_KEY,
|
|
10
|
+
GATEWAY_EMBEDDING_MODELS,
|
|
11
|
+
OPENROUTER_EMBEDDING_MODELS,
|
|
12
|
+
embeddingSettingsSchema,
|
|
13
|
+
type EmbeddingRoute,
|
|
14
|
+
type EmbeddingSettings,
|
|
15
|
+
} from './schemas.js';
|
|
16
|
+
|
|
17
|
+
const MODELS_BY_PROVIDER: Readonly<Record<EmbeddingRoute['provider'], readonly string[]>> = {
|
|
18
|
+
local: ['embeddinggemma-2'],
|
|
19
|
+
openai: ['text-embedding-3-small'],
|
|
20
|
+
sarvam: ['embeddinggemma-2'],
|
|
21
|
+
gateway: GATEWAY_EMBEDDING_MODELS,
|
|
22
|
+
openrouter: OPENROUTER_EMBEDDING_MODELS,
|
|
23
|
+
cloudflare: CLOUDFLARE_EMBEDDING_MODELS,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const isEmbeddingSettings = (value: unknown): value is EmbeddingSettings =>
|
|
27
|
+
Value.Check(embeddingSettingsSchema, value);
|
|
28
|
+
|
|
29
|
+
function configuration(message: EmbeddingErrorCodeMessage): never {
|
|
30
|
+
throw new EmbeddingsError(message);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type EmbeddingErrorCodeMessage = ConstructorParameters<typeof EmbeddingsError>[0];
|
|
34
|
+
|
|
35
|
+
function assertRoute(value: unknown, location: string): EmbeddingRoute {
|
|
36
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
37
|
+
return configuration('configuration-invalid');
|
|
38
|
+
const record = value as Record<string, unknown>;
|
|
39
|
+
const provider = record.provider;
|
|
40
|
+
const model = record.model;
|
|
41
|
+
if (typeof provider !== 'string' || typeof model !== 'string' || model.length === 0)
|
|
42
|
+
return configuration('configuration-invalid');
|
|
43
|
+
if (!Object.hasOwn(MODELS_BY_PROVIDER, provider))
|
|
44
|
+
return configuration('configuration-invalid');
|
|
45
|
+
const allowed = MODELS_BY_PROVIDER[provider as EmbeddingRoute['provider']];
|
|
46
|
+
if (allowed === undefined || !allowed.includes(model))
|
|
47
|
+
return configuration('configuration-invalid');
|
|
48
|
+
return { provider: provider as EmbeddingRoute['provider'], model };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Validate the whole settings object before any route is resolved or inference is attempted. */
|
|
52
|
+
export function validateEmbeddingSettings(value: unknown): EmbeddingSettings {
|
|
53
|
+
if (!isEmbeddingSettings(value)) return configuration('configuration-invalid');
|
|
54
|
+
assertRoute(value.default, 'default');
|
|
55
|
+
for (const [index, route] of (value.fallback ?? []).entries())
|
|
56
|
+
assertRoute(route, `fallback[${index}]`);
|
|
57
|
+
for (const [index, route] of (value.compare ?? []).entries())
|
|
58
|
+
assertRoute(route, `compare[${index}]`);
|
|
59
|
+
if (value.version !== undefined && value.version !== 1)
|
|
60
|
+
return configuration('configuration-invalid');
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function validateSettingsLayer(path: string): void {
|
|
65
|
+
let text: string;
|
|
66
|
+
try {
|
|
67
|
+
text = readFileSync(path, 'utf8');
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
if (text.charCodeAt(0) === 0xfeff) return configuration('configuration-invalid');
|
|
73
|
+
const root: unknown = JSON.parse(text);
|
|
74
|
+
if (
|
|
75
|
+
typeof root !== 'object' ||
|
|
76
|
+
root === null ||
|
|
77
|
+
Array.isArray(root) ||
|
|
78
|
+
(Object.hasOwn(root, EMBEDDINGS_SETTINGS_KEY) &&
|
|
79
|
+
(typeof (root as Record<string, unknown>)[EMBEDDINGS_SETTINGS_KEY] !== 'object' ||
|
|
80
|
+
(root as Record<string, unknown>)[EMBEDDINGS_SETTINGS_KEY] === null ||
|
|
81
|
+
Array.isArray((root as Record<string, unknown>)[EMBEDDINGS_SETTINGS_KEY])))
|
|
82
|
+
)
|
|
83
|
+
return configuration('configuration-invalid');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Shared precedence: global < agent config < explicitly trusted project. */
|
|
87
|
+
export function loadEmbeddingSettings(cwd: string, projectTrusted = false): EmbeddingSettings {
|
|
88
|
+
try {
|
|
89
|
+
const paths = new Set([
|
|
90
|
+
join(resolveConfigDir(join(homedir(), '.pi', 'agent')), 'settings.json'),
|
|
91
|
+
join(resolveConfigDir(), 'settings.json'),
|
|
92
|
+
...(projectTrusted ? [join(resolve(cwd), '.pi', 'settings.json')] : []),
|
|
93
|
+
]);
|
|
94
|
+
for (const path of paths) {
|
|
95
|
+
statSync(path, { throwIfNoEntry: false });
|
|
96
|
+
validateSettingsLayer(path);
|
|
97
|
+
}
|
|
98
|
+
const raw = loadPiSettings<unknown>(EMBEDDINGS_SETTINGS_KEY, {
|
|
99
|
+
cwd,
|
|
100
|
+
projectTrusted,
|
|
101
|
+
strictProjectSettings: true,
|
|
102
|
+
});
|
|
103
|
+
return validateEmbeddingSettings(raw);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error instanceof EmbeddingsError) throw error;
|
|
106
|
+
return configuration('configuration-invalid');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const configuredDefault = (settings: EmbeddingSettings): EmbeddingRoute =>
|
|
111
|
+
assertRoute(settings.default, 'default');
|
|
112
|
+
|
|
113
|
+
export const configuredRoutes = (
|
|
114
|
+
settings: EmbeddingSettings,
|
|
115
|
+
kind: 'fallback' | 'compare'
|
|
116
|
+
): readonly EmbeddingRoute[] => {
|
|
117
|
+
const raw = kind === 'fallback' ? settings.fallback : settings.compare;
|
|
118
|
+
return (raw ?? []).map((route, index) => assertRoute(route, `${kind}[${index}]`));
|
|
119
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
embeddingCompareResultSchema,
|
|
3
|
+
embeddingRequestSchema,
|
|
4
|
+
embeddingResultSchema,
|
|
5
|
+
} from './schemas.js';
|
|
6
|
+
|
|
7
|
+
export const EMBED_TOOL = 'embed';
|
|
8
|
+
export const EMBED_COMPARE_TOOL = 'embed_compare';
|
|
9
|
+
|
|
10
|
+
export const EMBED_TOOL_DESCRIPTION =
|
|
11
|
+
'Embed one or more text inputs with the server-configured embedding route. Provider, model, fallback routes and credentials stay in trusted settings; this tool cannot override them. Returns vectors and their actual generating space as structured data. Full text is preserved; the package owns prefixing and provider batching. Use embed_compare only for an explicit comparison.';
|
|
12
|
+
|
|
13
|
+
export const EMBED_COMPARE_TOOL_DESCRIPTION =
|
|
14
|
+
'Explicitly compare the same text inputs using exactly the configured compare routes. Every entry is a separate request and may incur charges. No implicit default, fallback, ranking or settings mutation.';
|
|
15
|
+
|
|
16
|
+
export const embedParameters = embeddingRequestSchema;
|
|
17
|
+
export const embedCompareParameters = embeddingRequestSchema;
|
|
18
|
+
|
|
19
|
+
export const embedOutput = embeddingResultSchema;
|
|
20
|
+
export const embedCompareOutput = embeddingCompareResultSchema;
|
|
21
|
+
|
|
22
|
+
export const EMBEDDINGS_TOOL_NAMES = [EMBED_TOOL, EMBED_COMPARE_TOOL] as const;
|