pi-harness-runtime 1.1.410 → 1.1.412
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/harness/notification-settings.ts +130 -0
- package/harness/skill-remote-source.ts +157 -0
- package/harness/skill-sync.ts +74 -22
- package/harness/skills-startup.ts +202 -0
- package/harness/startup-report.ts +98 -56
- package/harness-startup-status.ts +31 -2
- package/index.ts +125 -31
- package/package.json +1 -1
- package/packages/a2a-adapter/package.json +1 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/architecture-generator/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-refactor/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/autonomous-runtime/test/types.test.ts +59 -32
- package/packages/cache-strategy/package.json +1 -1
- package/packages/capability-registry/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/cli-plugin-sdk/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/clipboard-plugin/package.json +1 -1
- package/packages/code-generation/package.json +1 -1
- package/packages/code-review/package.json +1 -1
- package/packages/codex-adapter/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/connection-runtime/package.json +1 -1
- package/packages/context-compiler/package.json +1 -1
- package/packages/context-discovery/package.json +1 -1
- package/packages/context-manager/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/cookie-sanitizer/test/sync.test.ts +7 -1
- package/packages/cost-optimizer/package.json +1 -1
- package/packages/dependency-analyzer/dist/src/imports.js +3 -3
- package/packages/dependency-analyzer/dist/src/imports.js.map +1 -1
- package/packages/dependency-analyzer/package.json +1 -1
- package/packages/dependency-analyzer/src/imports.ts +3 -3
- package/packages/dependency-analyzer/test/analyzer.test.ts +1 -0
- package/packages/django-plugin/package.json +1 -1
- package/packages/doc-generator/package.json +1 -1
- package/packages/durable-review/package.json +1 -1
- package/packages/durable-review/test/acceptance.test.ts +24 -2
- package/packages/durable-review/test/smoke.test.ts +85 -47
- package/packages/e2e-tester/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/evaluation-runner/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-bus/test/bus.test.ts +16 -4
- package/packages/event-sink/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/feedback-collector/package.json +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/framework-detector/package.json +1 -1
- package/packages/framework-plugin-sdk/package.json +1 -1
- package/packages/frappe-plugin/package.json +1 -1
- package/packages/generic-web-plugin/package.json +1 -1
- package/packages/health-monitor/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/knowledge-graph/package.json +1 -1
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/laravel-plugin/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/mcp-adapter/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/milestone-manager/package.json +1 -1
- package/packages/model-registry/package.json +1 -1
- package/packages/nextjs-plugin/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/observability/package.json +1 -1
- package/packages/okf-indexer/package.json +1 -1
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/permission-mode/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/project-analyzer/package.json +1 -1
- package/packages/project-bootstrap/package.json +1 -1
- package/packages/projection-engine/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/prompt-versioning/package.json +1 -1
- package/packages/provider-adapter-sdk/package.json +1 -1
- package/packages/provider-router/package.json +1 -1
- package/packages/provider-selector/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/dist/client.d.ts.map +1 -1
- package/packages/qdrant-skills/dist/client.js +21 -8
- package/packages/qdrant-skills/dist/client.js.map +1 -1
- package/packages/qdrant-skills/dist/embedder.d.ts +37 -0
- package/packages/qdrant-skills/dist/embedder.d.ts.map +1 -1
- package/packages/qdrant-skills/dist/embedder.js +65 -1
- package/packages/qdrant-skills/dist/embedder.js.map +1 -1
- package/packages/qdrant-skills/dist/index.d.ts +1 -1
- package/packages/qdrant-skills/dist/index.d.ts.map +1 -1
- package/packages/qdrant-skills/dist/index.js +1 -1
- package/packages/qdrant-skills/dist/index.js.map +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/qdrant-skills/src/client.ts +19 -8
- package/packages/qdrant-skills/src/embedder.ts +103 -1
- package/packages/qdrant-skills/src/index.ts +5 -0
- package/packages/qdrant-skills/test/collection-info.test.ts +129 -0
- package/packages/qdrant-skills/test/embedder-batch.test.ts +184 -0
- package/packages/quota-manager/package.json +1 -1
- package/packages/rate-limiter/package.json +1 -1
- package/packages/react-vite-plugin/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/release-manager/package.json +1 -1
- package/packages/requirement-compiler/package.json +1 -1
- package/packages/runtime/package.json +1 -1
- package/packages/scheduler/package.json +1 -1
- package/packages/scheduler-adapter/package.json +1 -1
- package/packages/session/package.json +1 -1
- package/packages/session-api/package.json +1 -1
- package/packages/session-export/package.json +1 -1
- package/packages/shared-context/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skill-registry/package.json +1 -1
- package/packages/skills/dist/index.d.ts +19 -1
- package/packages/skills/dist/index.d.ts.map +1 -1
- package/packages/skills/dist/index.js +7 -2
- package/packages/skills/dist/index.js.map +1 -1
- package/packages/skills/dist/loader.d.ts +273 -3
- package/packages/skills/dist/loader.d.ts.map +1 -1
- package/packages/skills/dist/loader.js +525 -36
- package/packages/skills/dist/loader.js.map +1 -1
- package/packages/skills/dist/remote-source.d.ts +107 -0
- package/packages/skills/dist/remote-source.d.ts.map +1 -0
- package/packages/skills/dist/remote-source.js +79 -0
- package/packages/skills/dist/remote-source.js.map +1 -0
- package/packages/skills/dist/vector-schema.d.ts +58 -0
- package/packages/skills/dist/vector-schema.d.ts.map +1 -0
- package/packages/skills/dist/vector-schema.js +55 -0
- package/packages/skills/dist/vector-schema.js.map +1 -0
- package/packages/skills/package.json +1 -1
- package/packages/skills/src/index.ts +28 -5
- package/packages/skills/src/loader.ts +688 -53
- package/packages/skills/src/remote-source.ts +148 -0
- package/packages/skills/src/vector-schema.ts +72 -0
- package/packages/skills/test/init-skills.test.ts +107 -111
- package/packages/skills/test/remote-skills.test.ts +242 -0
- package/packages/skills/test/remote-source.test.ts +248 -0
- package/packages/skills/test/vector-indexing.test.ts +205 -0
- package/packages/sprint-planner/package.json +1 -1
- package/packages/subscription-engine/package.json +1 -1
- package/packages/task-compiler/package.json +1 -1
- package/packages/telegram/package.json +1 -1
- package/packages/tencentdb-memory/package.json +1 -1
- package/packages/tencentdb-sync/package.json +1 -1
- package/packages/test-data-generator/package.json +1 -1
- package/packages/test-generator/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/token-optimizer/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/voice-api/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/workflow-events/package.json +1 -1
- package/packages/workspace-scanner/package.json +1 -1
- package/packages/workspace-scanner/test/scanner.test.ts +4 -3
- package/packages/worktree/package.json +1 -1
- package/packages/write-review/package.json +1 -1
- package/scripts/sync-to-qdrant.ts +79 -14
- package/packages/autonomous-runtime/test/autonomous-runtime.test.ts +0 -194
- package/packages/autonomous-runtime/test/inbox.test.ts +0 -80
|
@@ -86,19 +86,30 @@ export async function getCollectionInfo(
|
|
|
86
86
|
} | null> {
|
|
87
87
|
try {
|
|
88
88
|
const info = await client.getCollection(name) as Record<string, unknown>;
|
|
89
|
-
//
|
|
90
|
-
|
|
89
|
+
// Qdrant reports the vector configuration under
|
|
90
|
+
// `config.params.vectors` - NOT at the top level. Reading `info.vectors`
|
|
91
|
+
// always yields undefined, which turns every schema check into a
|
|
92
|
+
// "dimension mismatch: got undefined" that names no real cause.
|
|
93
|
+
const config = info.config as Record<string, unknown> | undefined;
|
|
94
|
+
const params = config?.params as Record<string, unknown> | undefined;
|
|
95
|
+
const vectors = (params?.vectors ?? info.vectors) as Record<string, unknown> | undefined;
|
|
91
96
|
let vectorsSize: number | undefined;
|
|
92
97
|
let vectorsDistance: string | undefined;
|
|
93
98
|
|
|
94
|
-
if (
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
if (vectors) {
|
|
100
|
+
// A named vector configuration keeps the size in every named vector.
|
|
101
|
+
const named = vectors.named as Record<string, Record<string, unknown>> | undefined;
|
|
102
|
+
const firstNamed = named ? Object.values(named)[0] : undefined;
|
|
103
|
+
const nested = vectors.config as Record<string, unknown> | undefined;
|
|
104
|
+
const size = vectors.size ?? firstNamed?.size ?? nested?.size;
|
|
105
|
+
const distance = vectors.distance ?? firstNamed?.distance ?? nested?.distance;
|
|
106
|
+
if (typeof size === "number") vectorsSize = size;
|
|
107
|
+
if (typeof distance === "string") vectorsDistance = distance;
|
|
98
108
|
}
|
|
99
109
|
|
|
100
|
-
//
|
|
101
|
-
|
|
110
|
+
// The point count lives in `points_count`; the other names are the ones
|
|
111
|
+
// other Qdrant versions and proxies use.
|
|
112
|
+
const pointsCount = (info.points_count ?? info.num_points ?? info.points) as number | undefined;
|
|
102
113
|
|
|
103
114
|
return {
|
|
104
115
|
exists: true,
|
|
@@ -18,6 +18,15 @@ export interface EmbeddingResult {
|
|
|
18
18
|
tokens: number;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The result of a batch embedding: one vector per input text, in the order
|
|
23
|
+
* of the input.
|
|
24
|
+
*/
|
|
25
|
+
export interface BatchEmbeddingResult {
|
|
26
|
+
embeddings: number[][];
|
|
27
|
+
tokens: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
21
30
|
/** The embeddings endpoint of OpenAI. */
|
|
22
31
|
const OPENAI_EMBEDDINGS_URL = "https://api.openai.com/v1/embeddings";
|
|
23
32
|
|
|
@@ -82,6 +91,18 @@ export function getOpenAIApiKey(): string | null {
|
|
|
82
91
|
return readPiAuthToken();
|
|
83
92
|
}
|
|
84
93
|
|
|
94
|
+
/**
|
|
95
|
+
* The embedding model of the skill collection.
|
|
96
|
+
*
|
|
97
|
+
* The model decides the vector size, so it is part of the schema of the
|
|
98
|
+
* collection: a collection built with a different model holds vectors of a
|
|
99
|
+
* different size and cannot be searched with these vectors.
|
|
100
|
+
*/
|
|
101
|
+
export const EMBEDDING_MODEL = "text-embedding-3-small";
|
|
102
|
+
|
|
103
|
+
/** The vector size of {@link EMBEDDING_MODEL}. */
|
|
104
|
+
export const EMBEDDING_DIMENSIONS = 1536;
|
|
105
|
+
|
|
85
106
|
/**
|
|
86
107
|
* Create embedding for text using OpenAI
|
|
87
108
|
*/
|
|
@@ -101,7 +122,7 @@ export async function createEmbedding(
|
|
|
101
122
|
"Authorization": `Bearer ${key}`,
|
|
102
123
|
},
|
|
103
124
|
body: JSON.stringify({
|
|
104
|
-
model:
|
|
125
|
+
model: EMBEDDING_MODEL,
|
|
105
126
|
input: text.slice(0, 8000), // Limit to avoid token limits
|
|
106
127
|
}),
|
|
107
128
|
});
|
|
@@ -122,6 +143,87 @@ export async function createEmbedding(
|
|
|
122
143
|
};
|
|
123
144
|
}
|
|
124
145
|
|
|
146
|
+
/**
|
|
147
|
+
* The number of texts one embeddings request may carry. The endpoint of
|
|
148
|
+
* OpenAI accepts an array of inputs, so a batch costs one request instead of
|
|
149
|
+
* one request per text.
|
|
150
|
+
*/
|
|
151
|
+
const MAX_BATCH_INPUTS = 64;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The options of an embeddings request.
|
|
155
|
+
*/
|
|
156
|
+
export interface EmbeddingRequestOptions {
|
|
157
|
+
/**
|
|
158
|
+
* The HTTP client of the request. The default is the global `fetch`; a
|
|
159
|
+
* caller (a test) can pass its own, so no global has to be replaced.
|
|
160
|
+
*/
|
|
161
|
+
fetchImpl?: typeof fetch;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Create embeddings for many texts with as few requests as possible.
|
|
166
|
+
*
|
|
167
|
+
* One request per text exhausts the sockets of the process when a registry
|
|
168
|
+
* holds hundreds of skills: every request opens its own connection and the
|
|
169
|
+
* process then cannot talk to anything else. The endpoint of OpenAI accepts
|
|
170
|
+
* an array of inputs, so a batch stays one request per chunk.
|
|
171
|
+
*/
|
|
172
|
+
export async function createEmbeddingsBatch(
|
|
173
|
+
texts: string[],
|
|
174
|
+
apiKey?: string,
|
|
175
|
+
options: EmbeddingRequestOptions = {}
|
|
176
|
+
): Promise<BatchEmbeddingResult> {
|
|
177
|
+
const key = apiKey || getOpenAIApiKey();
|
|
178
|
+
if (!key) {
|
|
179
|
+
throw new Error("No OpenAI API key available for embeddings");
|
|
180
|
+
}
|
|
181
|
+
if (texts.length === 0) {
|
|
182
|
+
return { embeddings: [], tokens: 0 };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const send = options.fetchImpl ?? fetch;
|
|
186
|
+
const embeddings: number[][] = [];
|
|
187
|
+
let tokens = 0;
|
|
188
|
+
|
|
189
|
+
for (let offset = 0; offset < texts.length; offset += MAX_BATCH_INPUTS) {
|
|
190
|
+
const chunk = texts
|
|
191
|
+
.slice(offset, offset + MAX_BATCH_INPUTS)
|
|
192
|
+
.map((text) => text.slice(0, 8000));
|
|
193
|
+
|
|
194
|
+
const response = await send(OPENAI_EMBEDDINGS_URL, {
|
|
195
|
+
method: "POST",
|
|
196
|
+
headers: {
|
|
197
|
+
"Content-Type": "application/json",
|
|
198
|
+
"Authorization": `Bearer ${key}`,
|
|
199
|
+
},
|
|
200
|
+
body: JSON.stringify({
|
|
201
|
+
model: EMBEDDING_MODEL,
|
|
202
|
+
input: chunk,
|
|
203
|
+
}),
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
const error = await response.text();
|
|
208
|
+
throw new Error(`OpenAI embedding failed: ${error}`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const data = (await response.json()) as {
|
|
212
|
+
data: Array<{ embedding: number[]; index: number }>;
|
|
213
|
+
usage?: { total_tokens?: number };
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// The endpoint may answer out of order, so the index decides the place.
|
|
217
|
+
const ordered = [...data.data].sort((a, b) => a.index - b.index);
|
|
218
|
+
for (const entry of ordered) {
|
|
219
|
+
embeddings.push(entry.embedding || []);
|
|
220
|
+
}
|
|
221
|
+
tokens += data.usage?.total_tokens || 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return { embeddings, tokens };
|
|
225
|
+
}
|
|
226
|
+
|
|
125
227
|
/**
|
|
126
228
|
* Create embedding for a skill document
|
|
127
229
|
*/
|
|
@@ -37,8 +37,13 @@ export {
|
|
|
37
37
|
|
|
38
38
|
export {
|
|
39
39
|
createEmbedding,
|
|
40
|
+
createEmbeddingsBatch,
|
|
40
41
|
embedSkillDocument,
|
|
41
42
|
embedSkillsBatch,
|
|
42
43
|
getOpenAIApiKey,
|
|
44
|
+
EMBEDDING_MODEL,
|
|
45
|
+
EMBEDDING_DIMENSIONS,
|
|
46
|
+
type BatchEmbeddingResult,
|
|
47
|
+
type EmbeddingRequestOptions,
|
|
43
48
|
type EmbeddingResult,
|
|
44
49
|
} from './embedder.js';
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The collection info of Qdrant.
|
|
3
|
+
*
|
|
4
|
+
* Qdrant reports the vector configuration under `config.params.vectors` -
|
|
5
|
+
* not at the top level. Reading `info.vectors` yields undefined for every
|
|
6
|
+
* collection, which turned every schema check into "dimension mismatch:
|
|
7
|
+
* got undefined" and hid the real cause of an incompatible collection.
|
|
8
|
+
*
|
|
9
|
+
* The client is a small double, so the test never reaches the network.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, expect, it } from "bun:test";
|
|
13
|
+
|
|
14
|
+
import { getCollectionInfo, validateCollectionSchema } from "../src/client.js";
|
|
15
|
+
|
|
16
|
+
/** A client that answers with the given collection description. */
|
|
17
|
+
function fakeClient(description: unknown): {
|
|
18
|
+
getCollection: (name: string) => Promise<unknown>;
|
|
19
|
+
} {
|
|
20
|
+
return {
|
|
21
|
+
getCollection: async () => description,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The description Qdrant answers with for a 384-dimensional collection. */
|
|
26
|
+
const MINILM_DESCRIPTION = {
|
|
27
|
+
status: "green",
|
|
28
|
+
points_count: 79,
|
|
29
|
+
config: {
|
|
30
|
+
params: {
|
|
31
|
+
vectors: { size: 384, distance: "Cosine" },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("the collection info", () => {
|
|
37
|
+
it("reads the vector size and the distance", async () => {
|
|
38
|
+
const info = await getCollectionInfo(
|
|
39
|
+
fakeClient(MINILM_DESCRIPTION) as never,
|
|
40
|
+
"pi-harness-skills",
|
|
41
|
+
);
|
|
42
|
+
expect(info).not.toBeNull();
|
|
43
|
+
expect(info?.exists).toBe(true);
|
|
44
|
+
expect(info?.vectorsSize).toBe(384);
|
|
45
|
+
expect(info?.vectorsDistance).toBe("Cosine");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("reads the number of points", async () => {
|
|
49
|
+
const info = await getCollectionInfo(
|
|
50
|
+
fakeClient(MINILM_DESCRIPTION) as never,
|
|
51
|
+
"pi-harness-skills",
|
|
52
|
+
);
|
|
53
|
+
expect(info?.pointsCount).toBe(79);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("reads a named vector configuration", async () => {
|
|
57
|
+
const info = await getCollectionInfo(
|
|
58
|
+
fakeClient({
|
|
59
|
+
points_count: 3,
|
|
60
|
+
config: {
|
|
61
|
+
params: {
|
|
62
|
+
vectors: { named: { text: { size: 1536, distance: "Dot" } } },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
}) as never,
|
|
66
|
+
"pi-harness-skills",
|
|
67
|
+
);
|
|
68
|
+
expect(info?.vectorsSize).toBe(1536);
|
|
69
|
+
expect(info?.vectorsDistance).toBe("Dot");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("still reads a flat description", async () => {
|
|
73
|
+
// An older proxy answers without the config wrapper.
|
|
74
|
+
const info = await getCollectionInfo(
|
|
75
|
+
fakeClient({ points_count: 1, vectors: { size: 1536, distance: "Cosine" } }) as never,
|
|
76
|
+
"pi-harness-skills",
|
|
77
|
+
);
|
|
78
|
+
expect(info?.vectorsSize).toBe(1536);
|
|
79
|
+
expect(info?.vectorsDistance).toBe("Cosine");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("reports a missing collection", async () => {
|
|
83
|
+
const client = {
|
|
84
|
+
getCollection: async () => {
|
|
85
|
+
throw new Error("Not found: Collection not found");
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
const info = await getCollectionInfo(client as never, "pi-harness-skills");
|
|
89
|
+
expect(info).toEqual({ exists: false });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("reports an unreadable collection as unknown", async () => {
|
|
93
|
+
const client = {
|
|
94
|
+
getCollection: async () => {
|
|
95
|
+
throw new Error("connection refused");
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
expect(await getCollectionInfo(client as never, "pi-harness-skills")).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("the schema check", () => {
|
|
103
|
+
it("names the real dimensions of an incompatible collection", async () => {
|
|
104
|
+
const result = await validateCollectionSchema(
|
|
105
|
+
fakeClient(MINILM_DESCRIPTION) as never,
|
|
106
|
+
"pi-harness-skills",
|
|
107
|
+
1536,
|
|
108
|
+
);
|
|
109
|
+
expect(result.valid).toBe(false);
|
|
110
|
+
// The message names the size the collection really has - not
|
|
111
|
+
// "undefined".
|
|
112
|
+
expect(result.error).toContain("384");
|
|
113
|
+
expect(result.error).not.toContain("undefined");
|
|
114
|
+
expect(result.actualDimensions).toBe(384);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("accepts a collection with the expected dimensions", async () => {
|
|
118
|
+
const result = await validateCollectionSchema(
|
|
119
|
+
fakeClient({
|
|
120
|
+
points_count: 79,
|
|
121
|
+
config: { params: { vectors: { size: 1536, distance: "Cosine" } } },
|
|
122
|
+
}) as never,
|
|
123
|
+
"pi-harness-skills",
|
|
124
|
+
1536,
|
|
125
|
+
);
|
|
126
|
+
expect(result.valid).toBe(true);
|
|
127
|
+
expect(result.actualDimensions).toBe(1536);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The batch embeddings of the embedder.
|
|
3
|
+
*
|
|
4
|
+
* One request per text exhausts the sockets of the process when a registry
|
|
5
|
+
* holds hundreds of skills. The endpoint of OpenAI accepts an array of
|
|
6
|
+
* inputs, so a batch stays one request per chunk.
|
|
7
|
+
*
|
|
8
|
+
* The HTTP client is injected, so the test never replaces a global and never
|
|
9
|
+
* reaches the network.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
|
13
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { createEmbeddingsBatch } from "../src/embedder.js";
|
|
17
|
+
|
|
18
|
+
/** One recorded request. */
|
|
19
|
+
interface RecordedRequest {
|
|
20
|
+
url: string;
|
|
21
|
+
body: { model: string; input: string[] };
|
|
22
|
+
authorization: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A client that answers with the given vectors. */
|
|
26
|
+
function fakeFetch(
|
|
27
|
+
vectors: number[][],
|
|
28
|
+
recorded: RecordedRequest[],
|
|
29
|
+
): typeof fetch {
|
|
30
|
+
return (async (input: string | URL | Request, init?: RequestInit) => {
|
|
31
|
+
const body = JSON.parse(String(init?.body ?? "{}")) as {
|
|
32
|
+
model: string;
|
|
33
|
+
input: string[];
|
|
34
|
+
};
|
|
35
|
+
recorded.push({
|
|
36
|
+
url: String(input),
|
|
37
|
+
body,
|
|
38
|
+
authorization: String(
|
|
39
|
+
(init?.headers as Record<string, string>)?.Authorization ?? "",
|
|
40
|
+
),
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
ok: true,
|
|
44
|
+
json: async () => ({
|
|
45
|
+
// The endpoint may answer out of order.
|
|
46
|
+
data: body.input.map((_, index) => {
|
|
47
|
+
const position = body.input.length - 1 - index;
|
|
48
|
+
return { index: position, embedding: vectors[position] };
|
|
49
|
+
}),
|
|
50
|
+
usage: { total_tokens: body.input.length * 3 },
|
|
51
|
+
}),
|
|
52
|
+
text: async () => "",
|
|
53
|
+
} as Response;
|
|
54
|
+
}) as unknown as typeof fetch;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The environment variables the key lookup looks at. */
|
|
58
|
+
const ENV_KEYS = ["OPENAI_API_KEY", "HOME", "USERPROFILE"] as const;
|
|
59
|
+
|
|
60
|
+
let saved: Record<string, string | undefined> = {};
|
|
61
|
+
let tempHome: string;
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
saved = {};
|
|
65
|
+
for (const key of ENV_KEYS) {
|
|
66
|
+
saved[key] = process.env[key];
|
|
67
|
+
delete process.env[key];
|
|
68
|
+
}
|
|
69
|
+
// A fresh home directory: the test never reads the key of the machine
|
|
70
|
+
// it runs on.
|
|
71
|
+
tempHome = mkdtempSync(join(homedir(), ".test-qdrant-batch-"));
|
|
72
|
+
process.env.HOME = tempHome;
|
|
73
|
+
process.env.USERPROFILE = tempHome;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
afterEach(() => {
|
|
77
|
+
for (const key of ENV_KEYS) {
|
|
78
|
+
if (saved[key] === undefined) delete process.env[key];
|
|
79
|
+
else process.env[key] = saved[key];
|
|
80
|
+
}
|
|
81
|
+
rmSync(tempHome, { recursive: true, force: true });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("the batch embeddings of the embedder", () => {
|
|
85
|
+
it("sends one request for many texts", async () => {
|
|
86
|
+
const recorded: RecordedRequest[] = [];
|
|
87
|
+
const texts = ["first", "second", "third"];
|
|
88
|
+
|
|
89
|
+
const result = await createEmbeddingsBatch(
|
|
90
|
+
texts,
|
|
91
|
+
"sk-test-key",
|
|
92
|
+
{ fetchImpl: fakeFetch([[1], [2], [3]], recorded) },
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(recorded.length).toBe(1);
|
|
96
|
+
expect(recorded[0].body.input).toEqual(texts);
|
|
97
|
+
expect(result.embeddings).toEqual([[1], [2], [3]]);
|
|
98
|
+
expect(result.tokens).toBe(9);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("keeps the order of the texts even when the answer is out of order", async () => {
|
|
102
|
+
const recorded: RecordedRequest[] = [];
|
|
103
|
+
|
|
104
|
+
const result = await createEmbeddingsBatch(
|
|
105
|
+
["a", "b", "c"],
|
|
106
|
+
"sk-test-key",
|
|
107
|
+
{ fetchImpl: fakeFetch([[10], [20], [30]], recorded) },
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(result.embeddings).toEqual([[10], [20], [30]]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("splits a large input into chunks", async () => {
|
|
114
|
+
const recorded: RecordedRequest[] = [];
|
|
115
|
+
const texts = Array.from({ length: 150 }, (_, index) => `text-${index}`);
|
|
116
|
+
|
|
117
|
+
const result = await createEmbeddingsBatch(
|
|
118
|
+
texts,
|
|
119
|
+
"sk-test-key",
|
|
120
|
+
{ fetchImpl: fakeFetch(texts.map((_, index) => [index]), recorded) },
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// The chunk size is 64, so 150 texts need three requests.
|
|
124
|
+
expect(recorded.length).toBe(3);
|
|
125
|
+
expect(recorded[0].body.input.length).toBe(64);
|
|
126
|
+
expect(recorded[1].body.input.length).toBe(64);
|
|
127
|
+
expect(recorded[2].body.input.length).toBe(22);
|
|
128
|
+
expect(result.embeddings.length).toBe(150);
|
|
129
|
+
expect(result.tokens).toBe(150 * 3);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("sends no request for an empty input", async () => {
|
|
133
|
+
const recorded: RecordedRequest[] = [];
|
|
134
|
+
|
|
135
|
+
const result = await createEmbeddingsBatch([], "sk-test-key", {
|
|
136
|
+
fetchImpl: fakeFetch([], recorded),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(recorded.length).toBe(0);
|
|
140
|
+
expect(result.embeddings).toEqual([]);
|
|
141
|
+
expect(result.tokens).toBe(0);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("sends the OpenAI key of the caller", async () => {
|
|
145
|
+
const recorded: RecordedRequest[] = [];
|
|
146
|
+
|
|
147
|
+
await createEmbeddingsBatch(["text"], "sk-test-key", {
|
|
148
|
+
fetchImpl: fakeFetch([[1]], recorded),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(recorded[0].authorization).toBe("Bearer sk-test-key");
|
|
152
|
+
expect(recorded[0].url).toBe("https://api.openai.com/v1/embeddings");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("trims a very long text", async () => {
|
|
156
|
+
const recorded: RecordedRequest[] = [];
|
|
157
|
+
|
|
158
|
+
await createEmbeddingsBatch(["x".repeat(9000)], "sk-test-key", {
|
|
159
|
+
fetchImpl: fakeFetch([[1]], recorded),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(recorded[0].body.input[0].length).toBe(8000);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("reports a failed request", async () => {
|
|
166
|
+
const failing = (async () =>
|
|
167
|
+
({
|
|
168
|
+
ok: false,
|
|
169
|
+
text: async () => "the endpoint refused the request",
|
|
170
|
+
}) as Response) as unknown as typeof fetch;
|
|
171
|
+
|
|
172
|
+
await expect(
|
|
173
|
+
createEmbeddingsBatch(["text"], "sk-test-key", { fetchImpl: failing }),
|
|
174
|
+
).rejects.toThrow(/OpenAI embedding failed/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("reports a missing key", async () => {
|
|
178
|
+
// The home directory points at a fresh directory and the environment
|
|
179
|
+
// carries no key, so the machine of the test cannot provide one.
|
|
180
|
+
await expect(
|
|
181
|
+
createEmbeddingsBatch(["text"], "", { fetchImpl: fakeFetch([[1]], []) }),
|
|
182
|
+
).rejects.toThrow(/No OpenAI API key/);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
@@ -11,18 +11,36 @@ export * from "./types.js";
|
|
|
11
11
|
export * from "./parser.js";
|
|
12
12
|
export * from "./scanner.js";
|
|
13
13
|
export * from "./loader.js";
|
|
14
|
+
export * from "./vector-schema.js";
|
|
15
|
+
export * from "./remote-source.js";
|
|
14
16
|
export * from "./skill-commands.js";
|
|
15
17
|
export * from "./skill-tool.js";
|
|
16
18
|
export * from "./skill-versioning.js";
|
|
17
19
|
export * from "./linter.js";
|
|
18
20
|
import type { SkillsConfig, ScanResult } from "./types.js";
|
|
21
|
+
import { type SkillRegistry } from "./loader.js";
|
|
22
|
+
/**
|
|
23
|
+
* The result of the skill initialization.
|
|
24
|
+
*
|
|
25
|
+
* The registry is part of the result so a caller can ask the vector store
|
|
26
|
+
* about its state (how many skills it holds) without reaching for the
|
|
27
|
+
* global registry.
|
|
28
|
+
*/
|
|
29
|
+
export interface SkillsInitResult extends ScanResult {
|
|
30
|
+
/** The registry the skills were registered in. */
|
|
31
|
+
registry: SkillRegistry;
|
|
32
|
+
}
|
|
19
33
|
/**
|
|
20
34
|
* Initialize skills system
|
|
35
|
+
*
|
|
36
|
+
* A caller (a test) can pass its own registry, so the initialization does
|
|
37
|
+
* not have to touch the global one.
|
|
21
38
|
*/
|
|
22
39
|
export declare function initSkills(config?: Partial<SkillsConfig & {
|
|
23
40
|
maxDepth?: number;
|
|
24
41
|
includeHidden?: boolean;
|
|
25
|
-
|
|
42
|
+
registry?: SkillRegistry;
|
|
43
|
+
}>): SkillsInitResult;
|
|
26
44
|
/**
|
|
27
45
|
* Get skills summary
|
|
28
46
|
*/
|