localm-web 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +88 -0
- package/README.md +15 -3
- package/dist/assets/{inference.worker-CwvQtobb.js → inference.worker-DZbXKJZY.js} +49 -5
- package/dist/assets/inference.worker-DZbXKJZY.js.map +1 -0
- package/dist/index.d.ts +362 -5
- package/dist/index.js +381 -7
- package/dist/index.js.map +1 -1
- package/package.json +8 -1
- package/dist/assets/inference.worker-CwvQtobb.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,30 @@
|
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* JSON Schema helpers for structured output.
|
|
11
|
+
*
|
|
12
|
+
* The SDK delegates the actual constrained decoding to the underlying
|
|
13
|
+
* runtime (xgrammar inside WebLLM today, ORT-Web equivalent later). These
|
|
14
|
+
* helpers normalize user input — turning a JS object schema into the
|
|
15
|
+
* JSON-string shape that WebLLM's `response_format.schema` expects — and
|
|
16
|
+
* parse the runtime's textual output back into typed JSON.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Minimal structural sanity check for a JSON Schema.
|
|
20
|
+
*
|
|
21
|
+
* Does not validate the schema against the JSON Schema meta-schema. The goal
|
|
22
|
+
* is to fail fast on obvious mistakes (passing a string, an array, `null`)
|
|
23
|
+
* before handing the value off to the runtime, where errors surface much
|
|
24
|
+
* later and with much worse messages.
|
|
25
|
+
*
|
|
26
|
+
* @param schema - Candidate JSON Schema object.
|
|
27
|
+
* @throws StructuredOutputError when `schema` is not a plain object or has
|
|
28
|
+
* no recognizable schema shape (`type`, `$ref`, `oneOf`, `anyOf`, `allOf`,
|
|
29
|
+
* `enum`).
|
|
30
|
+
*/
|
|
31
|
+
export declare function assertJsonSchema(schema: unknown): asserts schema is object;
|
|
32
|
+
|
|
9
33
|
/** Thrown when no usable backend is available on the current platform. */
|
|
10
34
|
export declare class BackendNotAvailableError extends LocalmWebError {
|
|
11
35
|
}
|
|
@@ -118,6 +142,18 @@ export declare class ChatReply {
|
|
|
118
142
|
tokensGenerated: number,
|
|
119
143
|
/** Why the generation loop stopped. */
|
|
120
144
|
finishReason: FinishReason);
|
|
145
|
+
/**
|
|
146
|
+
* Parse {@link ChatReply.text} as JSON.
|
|
147
|
+
*
|
|
148
|
+
* Intended for replies generated with `json: true` or `jsonSchema`.
|
|
149
|
+
* The result is cast to `T` without runtime validation; pair with Zod /
|
|
150
|
+
* Ajv on the call site if you need to verify the schema.
|
|
151
|
+
*
|
|
152
|
+
* @typeParam T - Expected parsed shape.
|
|
153
|
+
* @returns The parsed JSON value.
|
|
154
|
+
* @throws StructuredOutputError if the text is not valid JSON.
|
|
155
|
+
*/
|
|
156
|
+
json<T = unknown>(): T;
|
|
121
157
|
}
|
|
122
158
|
|
|
123
159
|
/**
|
|
@@ -208,6 +244,17 @@ export declare class CompletionResult {
|
|
|
208
244
|
tokensGenerated: number,
|
|
209
245
|
/** Why the generation loop stopped. */
|
|
210
246
|
finishReason: FinishReason);
|
|
247
|
+
/**
|
|
248
|
+
* Parse {@link CompletionResult.text} as JSON.
|
|
249
|
+
*
|
|
250
|
+
* Intended for completions generated with `json: true` or `jsonSchema`.
|
|
251
|
+
* The result is cast to `T` without runtime validation.
|
|
252
|
+
*
|
|
253
|
+
* @typeParam T - Expected parsed shape.
|
|
254
|
+
* @returns The parsed JSON value.
|
|
255
|
+
* @throws StructuredOutputError if the text is not valid JSON.
|
|
256
|
+
*/
|
|
257
|
+
json<T = unknown>(): T;
|
|
211
258
|
}
|
|
212
259
|
|
|
213
260
|
/**
|
|
@@ -224,6 +271,116 @@ export declare class CompletionResult {
|
|
|
224
271
|
*/
|
|
225
272
|
export declare function createInferenceWorker(): WorkerLike;
|
|
226
273
|
|
|
274
|
+
/**
|
|
275
|
+
* Curated registry of supported embedding models for v0.3.
|
|
276
|
+
*
|
|
277
|
+
* Each entry maps a friendly id to the underlying transformers.js model id.
|
|
278
|
+
*/
|
|
279
|
+
export declare const EMBEDDING_PRESETS: Readonly<Record<string, EmbeddingPreset>>;
|
|
280
|
+
|
|
281
|
+
/** Curated metadata for a supported embedding model. */
|
|
282
|
+
export declare interface EmbeddingPreset {
|
|
283
|
+
/** Friendly identifier (e.g. `"bge-small-en-v1.5"`). */
|
|
284
|
+
id: string;
|
|
285
|
+
/** Family name (e.g. `"BGE"`). */
|
|
286
|
+
family: string;
|
|
287
|
+
/** Embedding dimension. */
|
|
288
|
+
dimension: number;
|
|
289
|
+
/** Maximum input length in tokens. */
|
|
290
|
+
maxTokens: number;
|
|
291
|
+
/** Identifier passed to `@huggingface/transformers`. */
|
|
292
|
+
transformersId: string;
|
|
293
|
+
/** Approximate quantization scheme (e.g. `"fp32"`, `"int8"`). */
|
|
294
|
+
quantization: string;
|
|
295
|
+
/** Short human description. */
|
|
296
|
+
description: string;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Sentence embedding task backed by `@huggingface/transformers`.
|
|
301
|
+
*
|
|
302
|
+
* Use {@link Embeddings.create} to construct an instance — the constructor is
|
|
303
|
+
* private. The default backend lazy-loads the transformers.js runtime; tests
|
|
304
|
+
* inject a {@link EmbedPipeline} mock instead.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* const emb = await Embeddings.create("bge-small-en-v1.5");
|
|
309
|
+
* const vectors = await emb.embed(["hello world", "another sentence"]);
|
|
310
|
+
* console.log(vectors[0].length); // 384
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
export declare class Embeddings {
|
|
314
|
+
private readonly pipeline;
|
|
315
|
+
/** Resolved metadata for the loaded model. */
|
|
316
|
+
readonly preset: EmbeddingPreset;
|
|
317
|
+
private constructor();
|
|
318
|
+
/**
|
|
319
|
+
* Create and load an `Embeddings` task for the given model.
|
|
320
|
+
*
|
|
321
|
+
* @param modelId - Friendly id from the embedding registry.
|
|
322
|
+
* @param options - Optional creation options.
|
|
323
|
+
* @throws UnknownModelError if `modelId` is not in the registry.
|
|
324
|
+
* @throws ModelLoadError if the underlying pipeline fails to load.
|
|
325
|
+
*/
|
|
326
|
+
static create(modelId: string, options?: EmbeddingsCreateOptions): Promise<Embeddings>;
|
|
327
|
+
/**
|
|
328
|
+
* Encode an array of strings into dense vectors.
|
|
329
|
+
*
|
|
330
|
+
* Returns one vector per input, in the same order. Empty input array
|
|
331
|
+
* returns an empty array (no error).
|
|
332
|
+
*
|
|
333
|
+
* @param texts - Input strings.
|
|
334
|
+
* @param options - Pooling + normalization. Defaults: `pooling: "mean"`, `normalize: true`.
|
|
335
|
+
*/
|
|
336
|
+
embed(texts: string[], options?: EmbedOptions): Promise<number[][]>;
|
|
337
|
+
/**
|
|
338
|
+
* Convenience: encode a single string and return its vector.
|
|
339
|
+
*
|
|
340
|
+
* @param text - Input string.
|
|
341
|
+
* @param options - Forwarded to {@link Embeddings.embed}.
|
|
342
|
+
*/
|
|
343
|
+
embedSingle(text: string, options?: EmbedOptions): Promise<number[]>;
|
|
344
|
+
/** Embedding dimension exposed by the loaded model. */
|
|
345
|
+
get dimension(): number;
|
|
346
|
+
/** Release pipeline resources. Safe to call multiple times. */
|
|
347
|
+
unload(): Promise<void>;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** Options accepted by {@link Embeddings.create}. */
|
|
351
|
+
export declare interface EmbeddingsCreateOptions {
|
|
352
|
+
/** Optional callback for model load progress updates. */
|
|
353
|
+
onProgress?: ProgressCallback;
|
|
354
|
+
/** Override the embedding pipeline. Intended for testing. */
|
|
355
|
+
pipeline?: EmbedPipeline;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Options accepted by {@link Embeddings.embed}. */
|
|
359
|
+
export declare interface EmbedOptions {
|
|
360
|
+
/** L2-normalize each vector. Recommended for cosine similarity downstream. Default `true`. */
|
|
361
|
+
normalize?: boolean;
|
|
362
|
+
/** Pooling strategy. BGE-style models use `"cls"`. Most sentence-transformers use `"mean"`. Default `"mean"`. */
|
|
363
|
+
pooling?: "mean" | "cls";
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Minimal pipeline contract that {@link Embeddings} depends on.
|
|
368
|
+
*
|
|
369
|
+
* The default implementation wraps `@huggingface/transformers`. Tests inject
|
|
370
|
+
* a fake satisfying the same shape — they never load the real runtime.
|
|
371
|
+
*/
|
|
372
|
+
export declare interface EmbedPipeline {
|
|
373
|
+
/**
|
|
374
|
+
* Run the encoder on a batch of inputs and return raw vectors.
|
|
375
|
+
*
|
|
376
|
+
* @param texts - Input strings.
|
|
377
|
+
* @param options - Pooling + normalization passed to the underlying pipeline.
|
|
378
|
+
*/
|
|
379
|
+
embed(texts: string[], options: Required<EmbedOptions>): Promise<number[][]>;
|
|
380
|
+
/** Release pipeline resources. */
|
|
381
|
+
unload?(): Promise<void>;
|
|
382
|
+
}
|
|
383
|
+
|
|
227
384
|
/**
|
|
228
385
|
* Runtime-agnostic inference contract.
|
|
229
386
|
*
|
|
@@ -313,15 +470,36 @@ export declare interface GenerationOptions {
|
|
|
313
470
|
/** Cancellation signal. When triggered, the engine stops generation. */
|
|
314
471
|
signal?: AbortSignal;
|
|
315
472
|
/**
|
|
316
|
-
*
|
|
317
|
-
*
|
|
473
|
+
* Force the engine to emit a string parseable as JSON.
|
|
474
|
+
*
|
|
475
|
+
* When `true` (and `jsonSchema` is not also set), the engine maps to
|
|
476
|
+
* WebLLM's `response_format: { type: "json_object" }` — the model is free
|
|
477
|
+
* to choose any JSON shape, but the output is guaranteed to parse.
|
|
478
|
+
*
|
|
479
|
+
* Ignored when {@link GenerationOptions.jsonSchema} is set.
|
|
480
|
+
*/
|
|
481
|
+
json?: boolean;
|
|
482
|
+
/**
|
|
483
|
+
* JSON Schema for structured output. When set, the engine constrains
|
|
484
|
+
* decoding (xgrammar inside WebLLM) so the output parses as JSON matching
|
|
485
|
+
* the schema. Takes priority over {@link GenerationOptions.json}.
|
|
486
|
+
*
|
|
487
|
+
* The schema is passed verbatim to the runtime — the SDK does not validate
|
|
488
|
+
* the parsed value against it. Use Ajv/Zod on the consumer side if you
|
|
489
|
+
* need runtime validation in addition to constrained decoding.
|
|
318
490
|
*/
|
|
319
491
|
jsonSchema?: object;
|
|
320
492
|
}
|
|
321
493
|
|
|
494
|
+
/** Return the list of supported embedding model ids. */
|
|
495
|
+
export declare function listSupportedEmbeddingModels(): string[];
|
|
496
|
+
|
|
322
497
|
/** Return the list of supported friendly model ids. */
|
|
323
498
|
export declare function listSupportedModels(): string[];
|
|
324
499
|
|
|
500
|
+
/** Return the list of supported reranker model ids. */
|
|
501
|
+
export declare function listSupportedRerankerModels(): string[];
|
|
502
|
+
|
|
325
503
|
/**
|
|
326
504
|
* Base class shared by all language-model tasks (`Chat` for v0.1; `Completion`,
|
|
327
505
|
* `Embeddings` and `Reranker` planned for later versions).
|
|
@@ -370,9 +548,10 @@ export declare interface LMTaskCreateOptions {
|
|
|
370
548
|
engine?: Engine;
|
|
371
549
|
/**
|
|
372
550
|
* Run inference inside a Web Worker, isolating the UI thread from
|
|
373
|
-
* tokenization and generation.
|
|
374
|
-
*
|
|
375
|
-
*
|
|
551
|
+
* tokenization and generation. **Default `true` from v0.3** — the
|
|
552
|
+
* `WorkerEngine` is the recommended path. Pass `false` to keep
|
|
553
|
+
* inference on the main thread (useful for environments without
|
|
554
|
+
* `Worker` support or when debugging the runtime directly).
|
|
376
555
|
*
|
|
377
556
|
* Ignored when {@link engine} is provided.
|
|
378
557
|
*/
|
|
@@ -560,6 +739,17 @@ export declare interface ModelPreset {
|
|
|
560
739
|
description: string;
|
|
561
740
|
}
|
|
562
741
|
|
|
742
|
+
/**
|
|
743
|
+
* Parse the textual output of a structured-decoding generation as JSON.
|
|
744
|
+
*
|
|
745
|
+
* @typeParam T - The expected parsed shape. The function does not validate
|
|
746
|
+
* the parsed value against `T`; that is the caller's responsibility.
|
|
747
|
+
* @param text - Raw text returned by the engine.
|
|
748
|
+
* @returns The parsed JSON value cast to `T`.
|
|
749
|
+
* @throws StructuredOutputError when the text is not valid JSON.
|
|
750
|
+
*/
|
|
751
|
+
export declare function parseStructuredOutput<T = unknown>(text: string): T;
|
|
752
|
+
|
|
563
753
|
/** Callback signature for model load progress. */
|
|
564
754
|
export declare type ProgressCallback = (progress: ModelLoadProgress) => void;
|
|
565
755
|
|
|
@@ -567,12 +757,147 @@ export declare type ProgressCallback = (progress: ModelLoadProgress) => void;
|
|
|
567
757
|
export declare class QuotaExceededError extends LocalmWebError {
|
|
568
758
|
}
|
|
569
759
|
|
|
760
|
+
/** A document paired with its score, for {@link Reranker.rank}. */
|
|
761
|
+
export declare interface RankedDocument {
|
|
762
|
+
/** The document text. */
|
|
763
|
+
text: string;
|
|
764
|
+
/** Score from the cross-encoder. */
|
|
765
|
+
score: number;
|
|
766
|
+
/** Original index of the document in the input array. */
|
|
767
|
+
index: number;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Cross-encoder reranking task backed by `@huggingface/transformers`.
|
|
772
|
+
*
|
|
773
|
+
* Use {@link Reranker.create} to construct an instance — the constructor is
|
|
774
|
+
* private. Useful as a second-stage step in a retrieve-then-rerank pipeline:
|
|
775
|
+
* pull top-K candidates with a fast embedding similarity, then rerank with
|
|
776
|
+
* a cross-encoder for higher precision.
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* ```ts
|
|
780
|
+
* const rerank = await Reranker.create("bge-reranker-base");
|
|
781
|
+
* const scores = await rerank.score("what is webgpu?", [
|
|
782
|
+
* "WebGPU is a modern graphics API",
|
|
783
|
+
* "Bananas grow on trees",
|
|
784
|
+
* ]);
|
|
785
|
+
* // scores[0] >> scores[1]
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* @example Ranked output sorted by score
|
|
789
|
+
* ```ts
|
|
790
|
+
* const ranked = await rerank.rank("what is webgpu?", docs);
|
|
791
|
+
* for (const r of ranked) console.log(r.score, r.text);
|
|
792
|
+
* ```
|
|
793
|
+
*/
|
|
794
|
+
export declare class Reranker {
|
|
795
|
+
private readonly pipeline;
|
|
796
|
+
/** Resolved metadata for the loaded model. */
|
|
797
|
+
readonly preset: RerankerPreset;
|
|
798
|
+
private constructor();
|
|
799
|
+
/**
|
|
800
|
+
* Create and load a `Reranker` task for the given model.
|
|
801
|
+
*
|
|
802
|
+
* @param modelId - Friendly id from the reranker registry.
|
|
803
|
+
* @param options - Optional creation options.
|
|
804
|
+
* @throws UnknownModelError if `modelId` is not in the registry.
|
|
805
|
+
* @throws ModelLoadError if the underlying pipeline fails to load.
|
|
806
|
+
*/
|
|
807
|
+
static create(modelId: string, options?: RerankerCreateOptions): Promise<Reranker>;
|
|
808
|
+
/**
|
|
809
|
+
* Score each document against the query. Returns one score per doc, in
|
|
810
|
+
* the same order. Empty `docs` returns `[]` (no error).
|
|
811
|
+
*
|
|
812
|
+
* @param query - Query string.
|
|
813
|
+
* @param docs - Documents to score.
|
|
814
|
+
* @param options - `sigmoid: true` maps logits into `[0, 1]`.
|
|
815
|
+
*/
|
|
816
|
+
score(query: string, docs: string[], options?: RerankOptions): Promise<number[]>;
|
|
817
|
+
/**
|
|
818
|
+
* Score and sort documents by score in descending order. Returns a list of
|
|
819
|
+
* {@link RankedDocument}s carrying the original index.
|
|
820
|
+
*
|
|
821
|
+
* @param query - Query string.
|
|
822
|
+
* @param docs - Documents to rank.
|
|
823
|
+
* @param options - Forwarded to {@link Reranker.score}.
|
|
824
|
+
*/
|
|
825
|
+
rank(query: string, docs: string[], options?: RerankOptions): Promise<RankedDocument[]>;
|
|
826
|
+
/** Release pipeline resources. Safe to call multiple times. */
|
|
827
|
+
unload(): Promise<void>;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Curated registry of supported reranker models for v0.3.
|
|
832
|
+
*/
|
|
833
|
+
export declare const RERANKER_PRESETS: Readonly<Record<string, RerankerPreset>>;
|
|
834
|
+
|
|
835
|
+
/** Options accepted by {@link Reranker.create}. */
|
|
836
|
+
export declare interface RerankerCreateOptions {
|
|
837
|
+
/** Optional callback for model load progress updates. */
|
|
838
|
+
onProgress?: ProgressCallback;
|
|
839
|
+
/** Override the rerank pipeline. Intended for testing. */
|
|
840
|
+
pipeline?: RerankPipeline;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/** Curated metadata for a supported reranker (cross-encoder) model. */
|
|
844
|
+
export declare interface RerankerPreset {
|
|
845
|
+
/** Friendly identifier (e.g. `"bge-reranker-base"`). */
|
|
846
|
+
id: string;
|
|
847
|
+
/** Family name (e.g. `"BGE Reranker"`). */
|
|
848
|
+
family: string;
|
|
849
|
+
/** Maximum input length in tokens (combined query + document). */
|
|
850
|
+
maxTokens: number;
|
|
851
|
+
/** Identifier passed to `@huggingface/transformers`. */
|
|
852
|
+
transformersId: string;
|
|
853
|
+
/** Approximate quantization (e.g. `"fp32"`). */
|
|
854
|
+
quantization: string;
|
|
855
|
+
/** Short human description. */
|
|
856
|
+
description: string;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
/** Options accepted by {@link Reranker.score}. */
|
|
860
|
+
export declare interface RerankOptions {
|
|
861
|
+
/**
|
|
862
|
+
* Apply sigmoid to logits to map scores into `[0, 1]`. Recommended when the
|
|
863
|
+
* downstream code uses scores as probabilities. Default `false` (raw logits).
|
|
864
|
+
*/
|
|
865
|
+
sigmoid?: boolean;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/**
|
|
869
|
+
* Minimal pipeline contract that {@link Reranker} depends on.
|
|
870
|
+
*
|
|
871
|
+
* The default implementation wraps `@huggingface/transformers`. Tests inject
|
|
872
|
+
* a fake satisfying the same shape — they never load the real runtime.
|
|
873
|
+
*/
|
|
874
|
+
export declare interface RerankPipeline {
|
|
875
|
+
/**
|
|
876
|
+
* Score `(query, doc)` pairs. One score per doc, in the same order.
|
|
877
|
+
*
|
|
878
|
+
* @param query - Single query string.
|
|
879
|
+
* @param docs - Documents to score against the query.
|
|
880
|
+
*/
|
|
881
|
+
score(query: string, docs: string[]): Promise<number[]>;
|
|
882
|
+
/** Release pipeline resources. */
|
|
883
|
+
unload?(): Promise<void>;
|
|
884
|
+
}
|
|
885
|
+
|
|
570
886
|
/** Internal payload returned by {@link LMTask.createEngine}. */
|
|
571
887
|
declare interface ResolvedEngine {
|
|
572
888
|
engine: Engine;
|
|
573
889
|
preset: ModelPreset;
|
|
574
890
|
}
|
|
575
891
|
|
|
892
|
+
/**
|
|
893
|
+
* Resolve a friendly embedding model id to its full preset metadata.
|
|
894
|
+
*
|
|
895
|
+
* @param modelId - Friendly id (e.g. `"bge-small-en-v1.5"`).
|
|
896
|
+
* @returns The matching preset.
|
|
897
|
+
* @throws UnknownModelError if no preset matches.
|
|
898
|
+
*/
|
|
899
|
+
export declare function resolveEmbeddingPreset(modelId: string): EmbeddingPreset;
|
|
900
|
+
|
|
576
901
|
/**
|
|
577
902
|
* Resolve a friendly model id to its full preset metadata.
|
|
578
903
|
*
|
|
@@ -582,6 +907,14 @@ declare interface ResolvedEngine {
|
|
|
582
907
|
*/
|
|
583
908
|
export declare function resolveModelPreset(modelId: string): ModelPreset;
|
|
584
909
|
|
|
910
|
+
/**
|
|
911
|
+
* Resolve a friendly reranker model id to its full preset metadata.
|
|
912
|
+
*
|
|
913
|
+
* @param modelId - Friendly id (e.g. `"bge-reranker-base"`).
|
|
914
|
+
* @throws UnknownModelError if no preset matches.
|
|
915
|
+
*/
|
|
916
|
+
export declare function resolveRerankerPreset(modelId: string): RerankerPreset;
|
|
917
|
+
|
|
585
918
|
/**
|
|
586
919
|
* Public type primitives for localm-web.
|
|
587
920
|
*/
|
|
@@ -596,6 +929,30 @@ export declare type Role = "system" | "user" | "assistant" | "tool";
|
|
|
596
929
|
*/
|
|
597
930
|
declare type SerializableGenerationOptions = Omit<GenerationOptions, "signal">;
|
|
598
931
|
|
|
932
|
+
/**
|
|
933
|
+
* Serialize a JSON Schema object for the WebLLM `response_format.schema`
|
|
934
|
+
* field.
|
|
935
|
+
*
|
|
936
|
+
* WebLLM expects the schema as a JSON-encoded string (xgrammar parses it
|
|
937
|
+
* server-side). Validates the shape via {@link assertJsonSchema} first.
|
|
938
|
+
*
|
|
939
|
+
* @param schema - JSON Schema object.
|
|
940
|
+
* @returns The schema serialized as a JSON string.
|
|
941
|
+
* @throws StructuredOutputError when `schema` is not a recognizable JSON
|
|
942
|
+
* Schema shape.
|
|
943
|
+
*/
|
|
944
|
+
export declare function serializeJsonSchema(schema: unknown): string;
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Thrown when structured output (JSON mode or JSON Schema constrained
|
|
948
|
+
* decoding) fails to parse as valid JSON.
|
|
949
|
+
*
|
|
950
|
+
* Wraps the underlying `SyntaxError` from `JSON.parse` so consumers can
|
|
951
|
+
* distinguish SDK-issued failures from unrelated runtime exceptions.
|
|
952
|
+
*/
|
|
953
|
+
export declare class StructuredOutputError extends LocalmWebError {
|
|
954
|
+
}
|
|
955
|
+
|
|
599
956
|
/**
|
|
600
957
|
* Wrap an async iterable so that each `TokenChunk` is also passed to a
|
|
601
958
|
* caller-supplied side-effect callback before being yielded downstream.
|