@gmickel/gno 2.14.0 → 2.16.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 +1 -1
- package/assets/skill/cli-reference.md +4 -2
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/{gno-browser-clipper-v2.14.0.zip → gno-browser-clipper-v2.16.0.zip} +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.16.0.zip.sha256 +1 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/package.json +1 -1
- package/spec/cli.md +88 -45
- package/spec/db/schema.sql +45 -0
- package/spec/output-schemas/embed-receipt.schema.json +89 -0
- package/src/cli/commands/agents/commands.ts +1 -1
- package/src/cli/commands/ask.ts +1 -1
- package/src/cli/commands/context-build.ts +1 -1
- package/src/cli/commands/embed.ts +341 -20
- package/src/cli/commands/links.ts +1 -1
- package/src/cli/commands/mcp/install.ts +2 -2
- package/src/cli/commands/mcp/status.ts +1 -1
- package/src/cli/commands/mcp/uninstall.ts +1 -1
- package/src/cli/commands/query.ts +1 -1
- package/src/cli/commands/shared.ts +4 -4
- package/src/cli/commands/skill/install.ts +1 -1
- package/src/cli/commands/skill/paths-cmd.ts +1 -1
- package/src/cli/commands/skill/uninstall.ts +1 -1
- package/src/cli/commands/status.ts +75 -58
- package/src/cli/config-cache.ts +86 -0
- package/src/cli/globals.ts +64 -0
- package/src/cli/options.ts +23 -0
- package/src/cli/program.ts +44 -37
- package/src/cli/run.ts +121 -67
- package/src/cli/runtime-health.ts +132 -122
- package/src/cli/session-binding.ts +11 -2
- package/src/config/saver.ts +8 -0
- package/src/converters/adapters/browser-export/formats.ts +25 -15
- package/src/converters/adapters/email/adapter.ts +37 -53
- package/src/converters/adapters/email/html.ts +37 -20
- package/src/converters/adapters/email/mime.ts +71 -39
- package/src/converters/adapters/ical/adapter.ts +20 -12
- package/src/converters/adapters/jsonl/adapter.ts +8 -6
- package/src/converters/adapters/shared/html-text.ts +55 -37
- package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
- package/src/converters/adapters/shared/utf8-lines.ts +69 -31
- package/src/converters/adapters/transcript/adapter.ts +12 -7
- package/src/converters/adapters/xlsx/adapter.ts +10 -7
- package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
- package/src/converters/canonicalize.ts +17 -3
- package/src/converters/native/markdown.ts +24 -17
- package/src/converters/native/plaintext.ts +5 -6
- package/src/converters/registry.ts +130 -8
- package/src/core/activation-status.ts +4 -8
- package/src/core/change-diff.ts +38 -11
- package/src/core/context-compiler.ts +17 -18
- package/src/core/context-evidence.ts +116 -25
- package/src/core/context-verifier.ts +70 -31
- package/src/core/file-lock.ts +10 -2
- package/src/core/graph-analysis.ts +3 -3
- package/src/core/link-inventory-markdown.ts +2 -2
- package/src/core/links.ts +20 -87
- package/src/core/memory-fence.ts +16 -9
- package/src/core/memory-recall.ts +168 -34
- package/src/core/path-rules.ts +143 -0
- package/src/core/section-parse.ts +16 -6
- package/src/core/typed-metadata.ts +68 -0
- package/src/core/wiki-link-syntax.ts +88 -0
- package/src/embed/backlog.ts +173 -60
- package/src/embed/batch.ts +83 -12
- package/src/embed/resume-cursor.ts +103 -0
- package/src/embed/retry.ts +41 -32
- package/src/embed/variant-backlog.ts +697 -63
- package/src/embed/variant-plan.ts +41 -45
- package/src/embed/variant-retry.ts +157 -44
- package/src/index.ts +3 -3
- package/src/ingestion/budget-error-codes.ts +13 -0
- package/src/ingestion/chunker.ts +60 -49
- package/src/ingestion/compiled-context.ts +4 -5
- package/src/ingestion/convert-file.ts +1 -1
- package/src/ingestion/document-metadata.ts +16 -9
- package/src/ingestion/excluded-ranges.ts +97 -0
- package/src/ingestion/file-processor.ts +122 -37
- package/src/ingestion/frontmatter.ts +35 -7
- package/src/ingestion/graph-reconciliation.ts +187 -51
- package/src/ingestion/language.ts +66 -61
- package/src/ingestion/position.ts +5 -5
- package/src/ingestion/prepare-file.ts +50 -29
- package/src/ingestion/record-adapter-canonical.ts +37 -26
- package/src/ingestion/record-adapter.ts +46 -26
- package/src/ingestion/record-container.ts +121 -53
- package/src/ingestion/record-path.ts +5 -2
- package/src/ingestion/source-availability/darwin-io.ts +10 -7
- package/src/ingestion/source-availability/darwin-path.ts +21 -3
- package/src/ingestion/source-availability/readers.ts +51 -0
- package/src/ingestion/strip.ts +39 -105
- package/src/ingestion/sync.ts +692 -386
- package/src/ingestion/typed-metadata.ts +10 -4
- package/src/ingestion/walker.ts +485 -158
- package/src/llm/cache.ts +128 -84
- package/src/llm/http-inference.ts +112 -35
- package/src/llm/http-policy.ts +101 -0
- package/src/llm/httpEmbedding.ts +8 -1
- package/src/llm/httpGeneration.ts +5 -1
- package/src/llm/httpRerank.ts +5 -1
- package/src/llm/inference-scope.ts +15 -2
- package/src/llm/lazy-ports.ts +33 -3
- package/src/llm/lockfile.ts +34 -7
- package/src/llm/model-cleanup.ts +24 -0
- package/src/llm/native-worker/client.ts +23 -12
- package/src/llm/native-worker/dispatcher.ts +19 -9
- package/src/llm/native-worker/embedding-identity.ts +42 -16
- package/src/llm/native-worker/entry.ts +2 -1
- package/src/llm/native-worker/owner.ts +1 -0
- package/src/llm/native-worker/ports.ts +86 -23
- package/src/llm/native-worker/protocol.ts +386 -55
- package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
- package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
- package/src/llm/nodeLlamaCpp/generation.ts +194 -30
- package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
- package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
- package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
- package/src/llm/types.ts +14 -0
- package/src/mcp/context.ts +342 -23
- package/src/mcp/http-transport.ts +21 -3
- package/src/mcp/tool-profile-types.ts +62 -0
- package/src/mcp/tool-profile.ts +32 -56
- package/src/mcp/tools/context.ts +5 -1
- package/src/mcp/tools/index.ts +269 -191
- package/src/mcp/tools/links.ts +7 -5
- package/src/mcp/tools/query.ts +60 -37
- package/src/mcp/tools/vsearch.ts +25 -14
- package/src/pipeline/answer.ts +133 -40
- package/src/pipeline/claim-verification.ts +293 -189
- package/src/pipeline/claim-verifier.ts +6 -7
- package/src/pipeline/egress-lineage.ts +37 -26
- package/src/pipeline/exclude.ts +34 -11
- package/src/pipeline/expansion.ts +233 -19
- package/src/pipeline/filters.ts +169 -33
- package/src/pipeline/graph-retrieval.ts +117 -61
- package/src/pipeline/hybrid.ts +598 -329
- package/src/pipeline/hydration.ts +86 -24
- package/src/pipeline/intent.ts +15 -7
- package/src/pipeline/owner-fusion.ts +61 -19
- package/src/pipeline/query-language.ts +15 -2
- package/src/pipeline/rerank.ts +15 -22
- package/src/pipeline/search.ts +193 -72
- package/src/pipeline/snippet.ts +15 -2
- package/src/pipeline/temporal.ts +51 -10
- package/src/pipeline/vsearch.ts +250 -152
- package/src/publish/attachment-raster.ts +2 -16
- package/src/publish/encrypted-export.ts +15 -5
- package/src/serve/browse-tree.ts +50 -42
- package/src/serve/connectors.ts +2 -1
- package/src/serve/context.ts +13 -4
- package/src/serve/doc-events.ts +3 -0
- package/src/serve/public/app.tsx +49 -21
- package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
- package/src/serve/public/components/CaptureModal.tsx +14 -10
- package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
- package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/globals.css +19 -0
- package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
- package/src/serve/public/lib/metadata-filter.ts +234 -9
- package/src/serve/public/pages/Browse.tsx +61 -27
- package/src/serve/public/pages/DocView.tsx +44 -25
- package/src/serve/public/pages/GraphView.tsx +8 -3
- package/src/serve/resident-runtime.ts +199 -48
- package/src/serve/routes/api.ts +98 -67
- package/src/serve/routes/links.ts +18 -4
- package/src/serve/server.ts +1370 -1180
- package/src/serve/spa-bundle-source.ts +52 -27
- package/src/serve/status.ts +101 -52
- package/src/serve/watch-reconciliation-fallback.ts +11 -7
- package/src/serve/watch-reconciliation-shared.ts +42 -4
- package/src/serve/watch-reconciliation.ts +21 -8
- package/src/serve/watch-service-events.ts +7 -1
- package/src/serve/watch-service-hosts.ts +3 -1
- package/src/serve/watch-service.ts +46 -21
- package/src/serve/watch-snapshot-scan.ts +6 -15
- package/src/sessions/archive.ts +20 -8
- package/src/sessions/sanitize.ts +38 -18
- package/src/sessions/service.ts +180 -102
- package/src/store/migrations/036-vector-mirror-state.ts +83 -0
- package/src/store/migrations/037-performance-indexes.ts +56 -0
- package/src/store/migrations/index.ts +4 -0
- package/src/store/sqlite/adapter.ts +1217 -651
- package/src/store/sqlite/chunking-policy.ts +15 -1
- package/src/store/sqlite/eligibility.ts +110 -70
- package/src/store/sqlite/graph-neighbors.ts +71 -46
- package/src/store/sqlite/graph-reference-state.ts +27 -3
- package/src/store/sqlite/graph-similarity.ts +59 -12
- package/src/store/sqlite/metadata-predicate.ts +123 -28
- package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
- package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
- package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
- package/src/store/types.ts +32 -0
- package/src/store/vector/eligibility.ts +39 -0
- package/src/store/vector/lazy.ts +17 -0
- package/src/store/vector/partition-index.ts +49 -47
- package/src/store/vector/proof-cache.ts +103 -0
- package/src/store/vector/sqlite-vec.ts +230 -139
- package/src/store/vector/stats.ts +29 -73
- package/src/store/vector/status.ts +151 -52
- package/src/store/vector/stored-vectors.ts +131 -45
- package/src/store/vector/types.ts +10 -0
- package/src/store/vector/variant-search.ts +771 -109
- package/src/store/vector/variants.ts +785 -144
- package/browser-extension/artifacts/gno-browser-clipper-v2.14.0.zip.sha256 +0 -1
|
@@ -155,12 +155,111 @@ export const NativeResponseSchema = z.strictObject({
|
|
|
155
155
|
});
|
|
156
156
|
export type NativeResponse = z.infer<typeof NativeResponseSchema>;
|
|
157
157
|
|
|
158
|
+
const UTF8_ENCODER = new TextEncoder();
|
|
159
|
+
const UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
|
|
160
|
+
/**
|
|
161
|
+
* Binary frame payload tag for `embed` / `embedBatch` `ok: true` responses.
|
|
162
|
+
* JSON payloads always begin with `0x7b` ('{'), so `0x01` is unambiguous.
|
|
163
|
+
*/
|
|
164
|
+
export const NATIVE_BINARY_EMBED_MAGIC = 0x01;
|
|
165
|
+
const PREVALIDATED_EMBED_VALUE = Symbol("gno.nativeBinaryEmbedValidated");
|
|
166
|
+
|
|
167
|
+
/** Mark an embed/embedBatch vector payload already validated by binary decode. */
|
|
168
|
+
function markPrevalidatedEmbedValue<T extends object>(value: T): T {
|
|
169
|
+
Object.defineProperty(value, PREVALIDATED_EMBED_VALUE, {
|
|
170
|
+
value: true,
|
|
171
|
+
enumerable: false,
|
|
172
|
+
configurable: false,
|
|
173
|
+
writable: false,
|
|
174
|
+
});
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** True when `value` was decoded from a binary Float64Array frame and already verified finite/uniform. */
|
|
179
|
+
export function isPrevalidatedEmbedValue(value: unknown): boolean {
|
|
180
|
+
return (
|
|
181
|
+
typeof value === "object" &&
|
|
182
|
+
value !== null &&
|
|
183
|
+
(value as Record<symbol, unknown>)[PREVALIDATED_EMBED_VALUE] === true
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const NativeResponseEnvelopeSchema = z.strictObject({
|
|
188
|
+
...envelope,
|
|
189
|
+
op: z.enum(["init", "embed", "embedBatch", "rerank", "generate", "dispose"]),
|
|
190
|
+
lifecycle: NativeLifecycleStatsSchema.optional(),
|
|
191
|
+
result: z.discriminatedUnion("ok", [
|
|
192
|
+
z.strictObject({ ok: z.literal(false), error }),
|
|
193
|
+
z.strictObject({ ok: z.literal(true), value: z.unknown() }),
|
|
194
|
+
]),
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const BinaryEmbedHeaderSchema = z.strictObject({
|
|
198
|
+
...envelope,
|
|
199
|
+
op: z.enum(["embed", "embedBatch"]),
|
|
200
|
+
lifecycle: NativeLifecycleStatsSchema.optional(),
|
|
201
|
+
vectorCount: id,
|
|
202
|
+
dims: id,
|
|
203
|
+
});
|
|
204
|
+
|
|
158
205
|
function parse<T>(schema: z.ZodType<T>, input: unknown): T {
|
|
159
206
|
const parsed = schema.safeParse(input);
|
|
160
207
|
if (!parsed.success) throw new NativeWorkerError("protocol");
|
|
161
208
|
return parsed.data;
|
|
162
209
|
}
|
|
163
210
|
|
|
211
|
+
/** Tight non-Zod validation for a non-empty finite float vector. */
|
|
212
|
+
export function validateFiniteVector(
|
|
213
|
+
value: unknown,
|
|
214
|
+
expectedDims?: number
|
|
215
|
+
): number[] {
|
|
216
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
217
|
+
throw new NativeWorkerError("protocol");
|
|
218
|
+
}
|
|
219
|
+
if (expectedDims !== undefined && value.length !== expectedDims) {
|
|
220
|
+
throw new NativeWorkerError("protocol");
|
|
221
|
+
}
|
|
222
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
223
|
+
const v = value[i];
|
|
224
|
+
if (typeof v !== "number" || !Number.isFinite(v)) {
|
|
225
|
+
throw new NativeWorkerError("protocol");
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return value as number[];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Tight non-Zod validation for a uniform batch of non-empty finite float vectors. */
|
|
232
|
+
export function validateFiniteVectorBatch(
|
|
233
|
+
value: unknown,
|
|
234
|
+
expectedCount: number,
|
|
235
|
+
expectedDims?: number
|
|
236
|
+
): number[][] {
|
|
237
|
+
if (!Array.isArray(value) || value.length !== expectedCount) {
|
|
238
|
+
throw new NativeWorkerError("protocol");
|
|
239
|
+
}
|
|
240
|
+
if (expectedCount === 0) {
|
|
241
|
+
return [];
|
|
242
|
+
}
|
|
243
|
+
let dims = expectedDims;
|
|
244
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
245
|
+
const row = validateFiniteVector(value[i], dims);
|
|
246
|
+
if (dims === undefined) {
|
|
247
|
+
dims = row.length;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return value as number[][];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function assertLogicalByteLength(byteLength: number): void {
|
|
254
|
+
if (
|
|
255
|
+
!Number.isSafeInteger(byteLength) ||
|
|
256
|
+
byteLength <= 0 ||
|
|
257
|
+
byteLength > NATIVE_LOGICAL_BYTES
|
|
258
|
+
) {
|
|
259
|
+
throw new NativeWorkerError("oversized");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
164
263
|
/** JSON size is measured in UTF-8 bytes, including escaping and envelope. */
|
|
165
264
|
export function encodeNativeMessage(input: unknown): Uint8Array {
|
|
166
265
|
let json: string | undefined;
|
|
@@ -170,18 +269,195 @@ export function encodeNativeMessage(input: unknown): Uint8Array {
|
|
|
170
269
|
throw new NativeWorkerError("protocol");
|
|
171
270
|
}
|
|
172
271
|
if (json === undefined) throw new NativeWorkerError("protocol");
|
|
173
|
-
const bytes =
|
|
272
|
+
const bytes = UTF8_ENCODER.encode(json);
|
|
174
273
|
if (bytes.length > NATIVE_LOGICAL_BYTES)
|
|
175
274
|
throw new NativeWorkerError("oversized");
|
|
176
275
|
return bytes;
|
|
177
276
|
}
|
|
178
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Encode `embed` / `embedBatch` `ok: true` responses into a compact, bit-exact
|
|
280
|
+
* IEEE-754 Float64Array binary payload (2.5x smaller than JSON, zero float-to-
|
|
281
|
+
* ASCII formatting/parsing). Returns null for non-embed/error/malformed payloads
|
|
282
|
+
* so callers fall back to standard JSON encoding.
|
|
283
|
+
*/
|
|
284
|
+
function tryEncodeBinaryEmbedPayload(
|
|
285
|
+
message: NativeRequest | NativeResponse
|
|
286
|
+
): Uint8Array | null {
|
|
287
|
+
if (!("result" in message) || !message.result.ok) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
if (message.op !== "embed" && message.op !== "embedBatch") {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
const value = message.result.value;
|
|
294
|
+
let vectors: readonly (readonly number[])[];
|
|
295
|
+
if (message.op === "embed") {
|
|
296
|
+
if (!Array.isArray(value) || value.length === 0) return null;
|
|
297
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
298
|
+
const v = value[i];
|
|
299
|
+
if (typeof v !== "number" || !Number.isFinite(v)) return null;
|
|
300
|
+
}
|
|
301
|
+
vectors = [value as number[]];
|
|
302
|
+
} else {
|
|
303
|
+
if (!Array.isArray(value) || value.length === 0) return null;
|
|
304
|
+
const first = value[0];
|
|
305
|
+
if (!Array.isArray(first) || first.length === 0) return null;
|
|
306
|
+
const dims = first.length;
|
|
307
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
308
|
+
const row = value[i];
|
|
309
|
+
if (!Array.isArray(row) || row.length !== dims) return null;
|
|
310
|
+
for (let j = 0; j < dims; j += 1) {
|
|
311
|
+
const v = row[j];
|
|
312
|
+
if (typeof v !== "number" || !Number.isFinite(v)) return null;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
vectors = value as number[][];
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const vectorCount = vectors.length;
|
|
319
|
+
const dims = vectors[0]!.length;
|
|
320
|
+
const headerObj: Record<string, unknown> = {
|
|
321
|
+
version: message.version,
|
|
322
|
+
generation: message.generation,
|
|
323
|
+
requestId: message.requestId,
|
|
324
|
+
op: message.op,
|
|
325
|
+
vectorCount,
|
|
326
|
+
dims,
|
|
327
|
+
};
|
|
328
|
+
if (message.lifecycle !== undefined) {
|
|
329
|
+
headerObj.lifecycle = message.lifecycle;
|
|
330
|
+
}
|
|
331
|
+
const headerBytes = UTF8_ENCODER.encode(JSON.stringify(headerObj));
|
|
332
|
+
const unalignedPrefix = 1 + 4 + headerBytes.length;
|
|
333
|
+
const alignedOffset = (unalignedPrefix + 7) & ~7;
|
|
334
|
+
const floatBytes = vectorCount * dims * 8;
|
|
335
|
+
const totalBytes = alignedOffset + floatBytes;
|
|
336
|
+
if (totalBytes > NATIVE_LOGICAL_BYTES) {
|
|
337
|
+
throw new NativeWorkerError("oversized");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const payload = new Uint8Array(totalBytes);
|
|
341
|
+
payload[0] = NATIVE_BINARY_EMBED_MAGIC;
|
|
342
|
+
const view = new DataView(
|
|
343
|
+
payload.buffer,
|
|
344
|
+
payload.byteOffset,
|
|
345
|
+
payload.byteLength
|
|
346
|
+
);
|
|
347
|
+
view.setUint32(1, headerBytes.length, true);
|
|
348
|
+
payload.set(headerBytes, 5);
|
|
349
|
+
let byteOffset = alignedOffset;
|
|
350
|
+
for (let i = 0; i < vectorCount; i += 1) {
|
|
351
|
+
const row = vectors[i]!;
|
|
352
|
+
for (let j = 0; j < dims; j += 1) {
|
|
353
|
+
view.setFloat64(byteOffset, row[j]!, true);
|
|
354
|
+
byteOffset += 8;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return payload;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function decodeBinaryEmbedPayload(
|
|
361
|
+
payload: Uint8Array,
|
|
362
|
+
expectedGeneration: number,
|
|
363
|
+
expectedRequestId: number
|
|
364
|
+
): NativeResponse {
|
|
365
|
+
if (payload.length < 5) {
|
|
366
|
+
throw new NativeWorkerError("protocol");
|
|
367
|
+
}
|
|
368
|
+
const view = new DataView(
|
|
369
|
+
payload.buffer,
|
|
370
|
+
payload.byteOffset,
|
|
371
|
+
payload.byteLength
|
|
372
|
+
);
|
|
373
|
+
const headerLen = view.getUint32(1, true);
|
|
374
|
+
const unalignedPrefix = 5 + headerLen;
|
|
375
|
+
const alignedOffset = (unalignedPrefix + 7) & ~7;
|
|
376
|
+
if (headerLen === 0 || alignedOffset > payload.length) {
|
|
377
|
+
throw new NativeWorkerError("protocol");
|
|
378
|
+
}
|
|
379
|
+
// Ensure padding bytes are zeroed so malformed frames fail closed.
|
|
380
|
+
for (let i = unalignedPrefix; i < alignedOffset; i += 1) {
|
|
381
|
+
if (payload[i] !== 0) {
|
|
382
|
+
throw new NativeWorkerError("protocol");
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
let rawHeader: unknown;
|
|
386
|
+
try {
|
|
387
|
+
rawHeader = JSON.parse(
|
|
388
|
+
UTF8_DECODER.decode(payload.subarray(5, unalignedPrefix))
|
|
389
|
+
);
|
|
390
|
+
} catch {
|
|
391
|
+
throw new NativeWorkerError("protocol");
|
|
392
|
+
}
|
|
393
|
+
const header = parse(BinaryEmbedHeaderSchema, rawHeader);
|
|
394
|
+
if (
|
|
395
|
+
header.generation !== expectedGeneration ||
|
|
396
|
+
header.requestId !== expectedRequestId
|
|
397
|
+
) {
|
|
398
|
+
throw new NativeWorkerError("protocol");
|
|
399
|
+
}
|
|
400
|
+
if (header.op === "embed" && header.vectorCount !== 1) {
|
|
401
|
+
throw new NativeWorkerError("protocol");
|
|
402
|
+
}
|
|
403
|
+
const expectedFloatBytes = header.vectorCount * header.dims * 8;
|
|
404
|
+
if (payload.length - alignedOffset !== expectedFloatBytes) {
|
|
405
|
+
throw new NativeWorkerError("protocol");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
let byteOffset = alignedOffset;
|
|
409
|
+
if (header.op === "embed") {
|
|
410
|
+
const vec = Array.from<number>({ length: header.dims });
|
|
411
|
+
for (let j = 0; j < header.dims; j += 1) {
|
|
412
|
+
const val = view.getFloat64(byteOffset, true);
|
|
413
|
+
if (!Number.isFinite(val)) throw new NativeWorkerError("protocol");
|
|
414
|
+
vec[j] = val;
|
|
415
|
+
byteOffset += 8;
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
version: 1,
|
|
419
|
+
generation: header.generation,
|
|
420
|
+
requestId: header.requestId,
|
|
421
|
+
op: "embed",
|
|
422
|
+
...(header.lifecycle !== undefined
|
|
423
|
+
? { lifecycle: header.lifecycle }
|
|
424
|
+
: {}),
|
|
425
|
+
result: { ok: true, value: markPrevalidatedEmbedValue(vec) },
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const vectors = Array.from<number[]>({ length: header.vectorCount });
|
|
430
|
+
for (let i = 0; i < header.vectorCount; i += 1) {
|
|
431
|
+
const vec = Array.from<number>({ length: header.dims });
|
|
432
|
+
for (let j = 0; j < header.dims; j += 1) {
|
|
433
|
+
const val = view.getFloat64(byteOffset, true);
|
|
434
|
+
if (!Number.isFinite(val)) throw new NativeWorkerError("protocol");
|
|
435
|
+
vec[j] = val;
|
|
436
|
+
byteOffset += 8;
|
|
437
|
+
}
|
|
438
|
+
vectors[i] = vec;
|
|
439
|
+
}
|
|
440
|
+
return {
|
|
441
|
+
version: 1,
|
|
442
|
+
generation: header.generation,
|
|
443
|
+
requestId: header.requestId,
|
|
444
|
+
op: "embedBatch",
|
|
445
|
+
...(header.lifecycle !== undefined ? { lifecycle: header.lifecycle } : {}),
|
|
446
|
+
result: { ok: true, value: markPrevalidatedEmbedValue(vectors) },
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
179
450
|
export function parseNativeRequest(
|
|
180
451
|
input: unknown,
|
|
181
452
|
generation: number,
|
|
182
|
-
approved: readonly ApprovedModel[]
|
|
453
|
+
approved: readonly ApprovedModel[],
|
|
454
|
+
knownByteLength?: number
|
|
183
455
|
): NativeRequest {
|
|
184
|
-
|
|
456
|
+
if (knownByteLength !== undefined) {
|
|
457
|
+
assertLogicalByteLength(knownByteLength);
|
|
458
|
+
} else {
|
|
459
|
+
encodeNativeMessage(input);
|
|
460
|
+
}
|
|
185
461
|
const request = parse(NativeRequestSchema, input);
|
|
186
462
|
if (request.generation !== generation)
|
|
187
463
|
throw new NativeWorkerError("stale_generation");
|
|
@@ -202,15 +478,20 @@ export function parseNativeRequest(
|
|
|
202
478
|
|
|
203
479
|
export function parseNativeResponse(
|
|
204
480
|
input: unknown,
|
|
205
|
-
request: NativeRequest
|
|
481
|
+
request: NativeRequest,
|
|
482
|
+
knownByteLength?: number
|
|
206
483
|
): NativeResponse {
|
|
207
|
-
|
|
208
|
-
|
|
484
|
+
if (knownByteLength !== undefined) {
|
|
485
|
+
assertLogicalByteLength(knownByteLength);
|
|
486
|
+
} else {
|
|
487
|
+
encodeNativeMessage(input);
|
|
488
|
+
}
|
|
489
|
+
const response = parse(NativeResponseEnvelopeSchema, input);
|
|
209
490
|
if (response.generation !== request.generation)
|
|
210
491
|
throw new NativeWorkerError("stale_generation");
|
|
211
492
|
if (response.requestId !== request.requestId || response.op !== request.op)
|
|
212
493
|
throw new NativeWorkerError("protocol");
|
|
213
|
-
if (!response.result.ok) return response;
|
|
494
|
+
if (!response.result.ok) return response as NativeResponse;
|
|
214
495
|
const value = response.result.value;
|
|
215
496
|
switch (request.op) {
|
|
216
497
|
case "init":
|
|
@@ -223,15 +504,16 @@ export function parseNativeResponse(
|
|
|
223
504
|
parse(z.string(), value);
|
|
224
505
|
break;
|
|
225
506
|
case "embed":
|
|
226
|
-
|
|
507
|
+
if (!isPrevalidatedEmbedValue(value)) {
|
|
508
|
+
validateFiniteVector(value);
|
|
509
|
+
}
|
|
227
510
|
break;
|
|
228
511
|
case "embedBatch": {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
);
|
|
233
|
-
if (vectors.some((vector) => vector.length !== vectors[0]?.length))
|
|
512
|
+
if (!isPrevalidatedEmbedValue(value)) {
|
|
513
|
+
validateFiniteVectorBatch(value, request.texts.length);
|
|
514
|
+
} else if ((value as number[][]).length !== request.texts.length) {
|
|
234
515
|
throw new NativeWorkerError("protocol");
|
|
516
|
+
}
|
|
235
517
|
break;
|
|
236
518
|
}
|
|
237
519
|
case "rerank": {
|
|
@@ -252,7 +534,7 @@ export function parseNativeResponse(
|
|
|
252
534
|
default:
|
|
253
535
|
throw new NativeWorkerError("protocol");
|
|
254
536
|
}
|
|
255
|
-
return response;
|
|
537
|
+
return response as NativeResponse;
|
|
256
538
|
}
|
|
257
539
|
|
|
258
540
|
/** Binary frame header: version, generation, requestId (float64 safe integers),
|
|
@@ -260,25 +542,23 @@ export function parseNativeResponse(
|
|
|
260
542
|
* One ordered logical message at a time per direction; diagnostics are never frames.
|
|
261
543
|
*/
|
|
262
544
|
const HEADER_BYTES = 32;
|
|
263
|
-
|
|
264
|
-
|
|
545
|
+
|
|
546
|
+
export function frameNativeBytes(
|
|
547
|
+
identity: { generation: number; requestId: number },
|
|
548
|
+
bytes: Uint8Array
|
|
265
549
|
): Uint8Array[] {
|
|
266
|
-
|
|
550
|
+
if (bytes.length === 0 || bytes.length > NATIVE_LOGICAL_BYTES) {
|
|
551
|
+
throw new NativeWorkerError("oversized");
|
|
552
|
+
}
|
|
267
553
|
const frames: Uint8Array[] = [];
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
offset += NATIVE_FRAME_BYTES - HEADER_BYTES
|
|
272
|
-
) {
|
|
273
|
-
const part = bytes.subarray(
|
|
274
|
-
offset,
|
|
275
|
-
offset + NATIVE_FRAME_BYTES - HEADER_BYTES
|
|
276
|
-
);
|
|
554
|
+
const maxPart = NATIVE_FRAME_BYTES - HEADER_BYTES;
|
|
555
|
+
for (let offset = 0; offset < bytes.length; offset += maxPart) {
|
|
556
|
+
const part = bytes.subarray(offset, offset + maxPart);
|
|
277
557
|
const frame = new Uint8Array(HEADER_BYTES + part.length);
|
|
278
558
|
const view = new DataView(frame.buffer);
|
|
279
559
|
view.setFloat64(0, NATIVE_PROTOCOL_VERSION);
|
|
280
|
-
view.setFloat64(8,
|
|
281
|
-
view.setFloat64(16,
|
|
560
|
+
view.setFloat64(8, identity.generation);
|
|
561
|
+
view.setFloat64(16, identity.requestId);
|
|
282
562
|
view.setUint32(24, bytes.length);
|
|
283
563
|
view.setUint32(28, offset);
|
|
284
564
|
frame.set(part, HEADER_BYTES);
|
|
@@ -287,15 +567,29 @@ export function frameNativeMessage(
|
|
|
287
567
|
return frames;
|
|
288
568
|
}
|
|
289
569
|
|
|
570
|
+
export function frameNativeMessage(
|
|
571
|
+
message: NativeRequest | NativeResponse
|
|
572
|
+
): Uint8Array[] {
|
|
573
|
+
const bytes =
|
|
574
|
+
tryEncodeBinaryEmbedPayload(message) ?? encodeNativeMessage(message);
|
|
575
|
+
return frameNativeBytes(message, bytes);
|
|
576
|
+
}
|
|
577
|
+
|
|
290
578
|
export class NativeFrameDecoder {
|
|
291
579
|
private bytes?: Uint8Array;
|
|
292
580
|
private received = 0;
|
|
293
581
|
private requestId = 0;
|
|
582
|
+
private lastDecodedBytes = 0;
|
|
294
583
|
|
|
295
584
|
constructor(private readonly generation: number) {
|
|
296
585
|
parse(id, generation);
|
|
297
586
|
}
|
|
298
587
|
|
|
588
|
+
/** Logical payload byte length of the most recently decoded message. */
|
|
589
|
+
get lastDecodedByteLength(): number {
|
|
590
|
+
return this.lastDecodedBytes;
|
|
591
|
+
}
|
|
592
|
+
|
|
299
593
|
reset(): void {
|
|
300
594
|
this.bytes = undefined;
|
|
301
595
|
this.received = 0;
|
|
@@ -327,29 +621,41 @@ export class NativeFrameDecoder {
|
|
|
327
621
|
throw new NativeWorkerError("oversized");
|
|
328
622
|
if (offset !== this.received || offset + part.length > total)
|
|
329
623
|
throw new NativeWorkerError("protocol");
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
624
|
+
|
|
625
|
+
// Zero-copy fast path for single-frame messages (the common case).
|
|
626
|
+
let payload: Uint8Array;
|
|
627
|
+
if (!this.bytes && offset === 0 && part.length === total) {
|
|
628
|
+
payload = part;
|
|
629
|
+
} else {
|
|
630
|
+
if (!this.bytes) {
|
|
631
|
+
this.bytes = new Uint8Array(total);
|
|
632
|
+
this.requestId = requestId;
|
|
633
|
+
}
|
|
634
|
+
if (this.bytes.length !== total || this.requestId !== requestId)
|
|
635
|
+
throw new NativeWorkerError("protocol");
|
|
636
|
+
this.bytes.set(part, offset);
|
|
637
|
+
this.received += part.length;
|
|
638
|
+
if (this.received !== total) return;
|
|
639
|
+
payload = this.bytes;
|
|
333
640
|
}
|
|
334
|
-
|
|
335
|
-
throw new NativeWorkerError("protocol");
|
|
336
|
-
this.bytes.set(part, offset);
|
|
337
|
-
this.received += part.length;
|
|
338
|
-
if (this.received !== total) return;
|
|
641
|
+
|
|
339
642
|
let decoded: unknown;
|
|
340
|
-
|
|
341
|
-
decoded =
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
643
|
+
if (payload[0] === NATIVE_BINARY_EMBED_MAGIC) {
|
|
644
|
+
decoded = decodeBinaryEmbedPayload(payload, this.generation, requestId);
|
|
645
|
+
} else {
|
|
646
|
+
try {
|
|
647
|
+
decoded = JSON.parse(UTF8_DECODER.decode(payload));
|
|
648
|
+
} catch {
|
|
649
|
+
throw new NativeWorkerError("protocol");
|
|
650
|
+
}
|
|
651
|
+
const identity = parse(z.object(envelope), decoded);
|
|
652
|
+
if (
|
|
653
|
+
identity.generation !== this.generation ||
|
|
654
|
+
identity.requestId !== requestId
|
|
655
|
+
)
|
|
656
|
+
throw new NativeWorkerError("protocol");
|
|
346
657
|
}
|
|
347
|
-
|
|
348
|
-
if (
|
|
349
|
-
identity.generation !== this.generation ||
|
|
350
|
-
identity.requestId !== requestId
|
|
351
|
-
)
|
|
352
|
-
throw new NativeWorkerError("protocol");
|
|
658
|
+
this.lastDecodedBytes = total;
|
|
353
659
|
this.reset();
|
|
354
660
|
return decoded;
|
|
355
661
|
}
|
|
@@ -362,14 +668,35 @@ export class NativeFrameDecoder {
|
|
|
362
668
|
export function splitEmbeddingRequest(
|
|
363
669
|
request: Extract<NativeRequest, { op: "embedBatch" }>
|
|
364
670
|
): string[][] {
|
|
365
|
-
|
|
671
|
+
const maxFramePayload = NATIVE_FRAME_BYTES - HEADER_BYTES;
|
|
672
|
+
// O(N) char-length fast path: worst-case JSON UTF-8 expansion is 6 bytes per
|
|
673
|
+
// UTF-16 code unit (`\uXXXX`) plus 2 quote bytes + 1 comma per item + ~256 B
|
|
674
|
+
// envelope overhead. When even the worst case fits in one frame, return
|
|
675
|
+
// immediately without allocating JSON or UTF-8 buffers.
|
|
676
|
+
let upperBound = 512 + request.modelId.length * 6;
|
|
677
|
+
for (let i = 0; i < request.texts.length; i += 1) {
|
|
678
|
+
upperBound += request.texts[i]!.length * 6 + 3;
|
|
679
|
+
if (upperBound > maxFramePayload) {
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (upperBound <= maxFramePayload) {
|
|
684
|
+
return [request.texts];
|
|
685
|
+
}
|
|
686
|
+
|
|
366
687
|
const overhead = encodeNativeMessage({ ...request, texts: [] }).length;
|
|
367
688
|
const batches: string[][] = [];
|
|
368
689
|
let batch: string[] = [];
|
|
369
690
|
let size = overhead;
|
|
370
|
-
|
|
691
|
+
let totalSize = overhead;
|
|
692
|
+
for (let i = 0; i < request.texts.length; i += 1) {
|
|
693
|
+
const text = request.texts[i]!;
|
|
371
694
|
const length = encodeNativeMessage(text).length;
|
|
372
|
-
|
|
695
|
+
totalSize += length + (i > 0 ? 1 : 0);
|
|
696
|
+
if (totalSize > NATIVE_LOGICAL_BYTES) {
|
|
697
|
+
throw new NativeWorkerError("oversized");
|
|
698
|
+
}
|
|
699
|
+
if (batch.length && size + length + 1 > maxFramePayload) {
|
|
373
700
|
batches.push(batch);
|
|
374
701
|
batch = [];
|
|
375
702
|
size = overhead;
|
|
@@ -396,7 +723,7 @@ export class NativeRequestLedger {
|
|
|
396
723
|
return this.pending.size;
|
|
397
724
|
}
|
|
398
725
|
|
|
399
|
-
admit(request: NativeRequest): void {
|
|
726
|
+
admit(request: NativeRequest, knownByteLength?: number): void {
|
|
400
727
|
const snapshot = parse(NativeRequestSchema, request);
|
|
401
728
|
if (request.generation !== this.generation)
|
|
402
729
|
throw new NativeWorkerError("stale_generation");
|
|
@@ -404,7 +731,11 @@ export class NativeRequestLedger {
|
|
|
404
731
|
throw new NativeWorkerError("duplicate_completion");
|
|
405
732
|
if (this.pending.size >= NATIVE_QUEUE_LIMIT + 1)
|
|
406
733
|
throw new NativeWorkerError("overloaded");
|
|
407
|
-
|
|
734
|
+
if (knownByteLength !== undefined) {
|
|
735
|
+
assertLogicalByteLength(knownByteLength);
|
|
736
|
+
} else {
|
|
737
|
+
encodeNativeMessage(request);
|
|
738
|
+
}
|
|
408
739
|
this.highestId = request.requestId;
|
|
409
740
|
this.pending.set(request.requestId, snapshot);
|
|
410
741
|
}
|
|
@@ -415,13 +746,13 @@ export class NativeRequestLedger {
|
|
|
415
746
|
throw new NativeWorkerError("protocol");
|
|
416
747
|
}
|
|
417
748
|
|
|
418
|
-
settle(input: unknown): NativeResponse {
|
|
749
|
+
settle(input: unknown, knownByteLength?: number): NativeResponse {
|
|
419
750
|
const identity = parse(z.object(envelope), input);
|
|
420
751
|
if (identity.generation !== this.generation)
|
|
421
752
|
throw new NativeWorkerError("stale_generation");
|
|
422
753
|
const request = this.pending.get(identity.requestId);
|
|
423
754
|
if (!request) throw new NativeWorkerError("duplicate_completion");
|
|
424
|
-
const response = parseNativeResponse(input, request);
|
|
755
|
+
const response = parseNativeResponse(input, request, knownByteLength);
|
|
425
756
|
this.pending.delete(request.requestId);
|
|
426
757
|
return response;
|
|
427
758
|
}
|