@moss-dev/moss-web 1.0.1 → 1.3.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.
@@ -0,0 +1,106 @@
1
+ export class ModelArtifactOwnership {
2
+ constructor(retain) {
3
+ this.identities = new Map();
4
+ this.retain = retain;
5
+ }
6
+ has(indexName) {
7
+ return this.identities.has(indexName);
8
+ }
9
+ get(indexName) {
10
+ return this.identities.get(indexName);
11
+ }
12
+ synchronize(indexName, identity) {
13
+ const hadPrevious = this.identities.has(indexName);
14
+ const previous = this.identities.get(indexName);
15
+ this.identities.set(indexName, identity);
16
+ try {
17
+ this.retainCurrentArtifacts();
18
+ }
19
+ catch (error) {
20
+ if (hadPrevious) {
21
+ this.identities.set(indexName, previous ?? null);
22
+ }
23
+ else {
24
+ this.identities.delete(indexName);
25
+ }
26
+ throw error;
27
+ }
28
+ }
29
+ clear(indexName) {
30
+ if (!this.identities.delete(indexName))
31
+ return;
32
+ this.retainCurrentArtifacts();
33
+ }
34
+ clearAll() {
35
+ if (this.identities.size === 0)
36
+ return;
37
+ this.identities.clear();
38
+ this.retainCurrentArtifacts();
39
+ }
40
+ async replaceAfter(indexName, transition, identityFromResult, rollbackSynchronizedTransition) {
41
+ let result;
42
+ let transitioned = false;
43
+ let synchronizationStarted = false;
44
+ try {
45
+ result = await transition();
46
+ transitioned = true;
47
+ const identity = identityFromResult(result);
48
+ synchronizationStarted = true;
49
+ this.synchronize(indexName, identity);
50
+ return result;
51
+ }
52
+ catch (error) {
53
+ if (!transitioned) {
54
+ // The transition itself failed: the index did not advance, but its
55
+ // identity may now be stale, so drop it. A retain failure while
56
+ // clearing must never mask the transition error.
57
+ this.clearWithoutMasking(indexName);
58
+ throw error;
59
+ }
60
+ if (rollbackSynchronizedTransition) {
61
+ // The index transitioned but its identity could not be derived or
62
+ // synchronized. Undo the transition; the index ends unloaded, so its
63
+ // identity entry goes with it.
64
+ try {
65
+ await rollbackSynchronizedTransition(result);
66
+ }
67
+ catch { }
68
+ this.clearWithoutMasking(indexName);
69
+ throw error;
70
+ }
71
+ if (!synchronizationStarted) {
72
+ this.clearWithoutMasking(indexName);
73
+ }
74
+ // No rollback: the index remains loaded. When `synchronize` failed it
75
+ // already restored the previous identity, so leave the map alone.
76
+ throw error;
77
+ }
78
+ }
79
+ async removeAfter(indexName, transition) {
80
+ let result;
81
+ try {
82
+ result = await transition();
83
+ }
84
+ catch (error) {
85
+ // The identity must still be dropped, but a retain failure during the
86
+ // unwind must not replace the transition error.
87
+ this.clearWithoutMasking(indexName);
88
+ throw error;
89
+ }
90
+ this.clear(indexName);
91
+ return result;
92
+ }
93
+ /** Clear an index's identity while an error is already propagating: the
94
+ * map entry is always removed, but a failure releasing runtimes is
95
+ * swallowed so it cannot mask the primary error. */
96
+ clearWithoutMasking(indexName) {
97
+ try {
98
+ this.clear(indexName);
99
+ }
100
+ catch { }
101
+ }
102
+ retainCurrentArtifacts() {
103
+ const artifacts = Array.from(this.identities.values()).filter((identity) => identity !== null);
104
+ this.retain(artifacts);
105
+ }
106
+ }
@@ -0,0 +1,3 @@
1
+ import type { MossModel } from "./types";
2
+ /** Width of the model's last hidden state, as expected by `poolAndNormalize`. */
3
+ export declare function modelHiddenDim(modelId: MossModel): number;
@@ -0,0 +1,8 @@
1
+ const MODEL_CONFIGS = {
2
+ "moss-minilm": { hiddenDim: 384 },
3
+ "moss-mediumlm": { hiddenDim: 512 },
4
+ };
5
+ /** Width of the model's last hidden state, as expected by `poolAndNormalize`. */
6
+ export function modelHiddenDim(modelId) {
7
+ return MODEL_CONFIGS[modelId].hiddenDim;
8
+ }
@@ -0,0 +1,81 @@
1
+ import type { ModelContentKeys, ModelKeyExchange, SealedModel, Tokenizer } from "@moss-dev/moss-wasm";
2
+ import type { ModelArtifactIdentity, ModelDownloadProgress } from "./types.js";
3
+ export declare const DEFAULT_INDEX_BASE_URL = "https://service.usemoss.dev/index";
4
+ export declare const DEFAULT_MODEL_ARTIFACT_ORIGIN = "https://models.moss.link";
5
+ export interface SealedModelRuntime {
6
+ newKeyExchange(): ModelKeyExchange;
7
+ openSealedModel(bytes: Uint8Array, keys: ModelContentKeys, sku: string): SealedModel;
8
+ }
9
+ export interface ModelDeliveryAuth {
10
+ getAuthHeader(): Promise<string>;
11
+ invalidate(): Promise<void> | void;
12
+ }
13
+ export interface ModelDeliveryOptions {
14
+ projectId: string;
15
+ auth: ModelDeliveryAuth;
16
+ runtime: SealedModelRuntime;
17
+ baseUrl?: string;
18
+ artifactOrigin?: string;
19
+ fetch?: typeof fetch;
20
+ onProgress?: (progress: ModelDownloadProgress) => void;
21
+ /** Cache API storage for the verified model file. `null` turns caching off; unset uses `globalThis.caches`. */
22
+ caches?: CacheStorage | null;
23
+ /** How long the grant request, or an artifact response or its next chunk, may take before it is aborted. */
24
+ requestTimeoutMs?: number;
25
+ }
26
+ export interface GrantedObject {
27
+ url: string;
28
+ capability: string;
29
+ expiresAtMs: number;
30
+ }
31
+ export interface ModelDeliveryGrant {
32
+ modelId: string;
33
+ identity: ModelArtifactIdentity;
34
+ release: GrantedObject;
35
+ model: GrantedObject;
36
+ acceptedArtifactVersions: string[];
37
+ json: string;
38
+ }
39
+ export interface SealedModelBundle {
40
+ grant: ModelDeliveryGrant;
41
+ graph: Uint8Array;
42
+ tokenizer: Tokenizer;
43
+ }
44
+ export declare class ModelDeliveryError extends Error {
45
+ readonly status: number | undefined;
46
+ readonly code: string | undefined;
47
+ constructor(message: string, status?: number, code?: string);
48
+ }
49
+ interface ArtifactOrigin {
50
+ protocol: string;
51
+ host: string;
52
+ }
53
+ interface ReleaseFile {
54
+ relativePath: string;
55
+ sha256: string;
56
+ sizeBytes: number;
57
+ }
58
+ export declare function parseArtifactOrigin(value: string): ArtifactOrigin;
59
+ export declare function parseModelDeliveryGrant(json: string, sku: string, artifactOrigin?: string): ModelDeliveryGrant;
60
+ export declare function grantAcceptsStamp(grant: ModelDeliveryGrant, stamped: ModelArtifactIdentity): boolean;
61
+ export declare function requestModelDeliveryGrant(options: ModelDeliveryOptions, sku: string, capabilityQuery: string): Promise<ModelDeliveryGrant>;
62
+ export interface ArtifactTransfer {
63
+ /** The transfer aborts as soon as more bytes than this arrive. */
64
+ maxBytes: number;
65
+ /** Writes the body into one buffer of exactly `maxBytes` bytes. A shorter body is retried. */
66
+ preallocate?: boolean;
67
+ /** Receives the running byte count of the current attempt after each chunk. */
68
+ onBytes?: (loadedBytes: number) => void;
69
+ timeoutMs?: number;
70
+ }
71
+ /**
72
+ * Streams one granted artifact into memory.
73
+ *
74
+ * Each attempt aborts when the response or the next chunk takes longer than
75
+ * the deadline. A stall, a network error, a 408, a 429 or a 5xx is retried once.
76
+ */
77
+ export declare function fetchGrantedBytes(fetchFn: typeof fetch, object: GrantedObject, transfer: ArtifactTransfer): Promise<Uint8Array>;
78
+ export declare function verifyReleaseManifest(releaseBytes: Uint8Array, grant: ModelDeliveryGrant): Promise<ReleaseFile>;
79
+ export declare function verifyModelBytes(bytes: Uint8Array, record: ReleaseFile): Promise<void>;
80
+ export declare function loadSealedModel(options: ModelDeliveryOptions, stamped: ModelArtifactIdentity): Promise<SealedModelBundle>;
81
+ export {};