@independo/inderun-web 0.4.0 → 0.5.0-rc.2
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/dist/core/artifacts.d.ts +110 -0
- package/dist/core/artifacts.d.ts.map +1 -0
- package/dist/core/artifacts.js +190 -0
- package/dist/core/engine.d.ts +28 -1
- package/dist/core/engine.d.ts.map +1 -1
- package/dist/core/engine.js +232 -28
- package/dist/core/generated/inderun-api.d.ts +12 -4
- package/dist/core/generated/inderun-api.d.ts.map +1 -1
- package/dist/core/host.d.ts +1 -1
- package/dist/core/host.d.ts.map +1 -1
- package/dist/core/provenance.d.ts +5 -2
- package/dist/core/provenance.d.ts.map +1 -1
- package/dist/core/provenance.js +6 -2
- package/dist/core/provider.d.ts +50 -130
- package/dist/core/provider.d.ts.map +1 -1
- package/dist/core/route-planner.d.ts +5 -5
- package/dist/core/route-planner.d.ts.map +1 -1
- package/dist/core/route-planner.js +15 -29
- package/dist/core/router.d.ts +16 -0
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/router.js +14 -5
- package/dist/core/task-profiles.d.ts +54 -0
- package/dist/core/task-profiles.d.ts.map +1 -0
- package/dist/core/task-profiles.js +92 -0
- package/dist/facade/text.d.ts.map +1 -1
- package/dist/facade/text.js +5 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/providers/onnx/runtime.d.ts +2 -2
- package/dist/providers/onnx/runtime.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/core/artifacts.ts +280 -0
- package/src/core/engine.ts +281 -27
- package/src/core/generated/inderun-api.ts +12 -4
- package/src/core/host.ts +1 -0
- package/src/core/provenance.ts +7 -2
- package/src/core/provider.ts +64 -135
- package/src/core/route-planner.ts +20 -36
- package/src/core/router.ts +30 -7
- package/src/core/task-profiles.ts +145 -0
- package/src/facade/text.ts +5 -3
- package/src/index.ts +6 -1
- package/src/providers/onnx/runtime.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@independo/inderun-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0-rc.2",
|
|
4
4
|
"description": "On-device AI with automatic cloud fallback for web apps. Runs ONNX Runtime or the Chrome Prompt API in the browser and falls back to an OpenAI-compatible endpoint, with streaming and cancellation behind one API.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -72,8 +72,8 @@
|
|
|
72
72
|
"README.md"
|
|
73
73
|
],
|
|
74
74
|
"dependencies": {
|
|
75
|
-
"@independo/inderun-contracts": "^0.
|
|
76
|
-
"@independo/inderun-route-core-wasm": "^0.
|
|
75
|
+
"@independo/inderun-contracts": "^0.5.0-rc.2",
|
|
76
|
+
"@independo/inderun-route-core-wasm": "^0.5.0-rc.2"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
79
|
"@huggingface/transformers": "^4.0.0"
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import type { ArtifactReadRequest, MediaArtifact } from "@independo/inderun-contracts";
|
|
2
|
+
import { createInternal, createUnavailable } from "./errors.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Size limits of an engine's artifact store (ADR 017). Every limit is in bytes.
|
|
6
|
+
*/
|
|
7
|
+
export interface ArtifactLimits {
|
|
8
|
+
/** Largest single artifact a provider may write. Default 16 MiB. */
|
|
9
|
+
maxArtifactBytes?: number;
|
|
10
|
+
/** Most bytes the store holds across all undisposed artifacts. Default 64 MiB. */
|
|
11
|
+
maxStoreBytes?: number;
|
|
12
|
+
/** Most bytes one `readArtifact` call returns. Default 1 MiB. */
|
|
13
|
+
maxReadBytes?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_ARTIFACT_LIMITS: Required<ArtifactLimits> = {
|
|
17
|
+
maxArtifactBytes: 16 * 1024 * 1024,
|
|
18
|
+
maxStoreBytes: 64 * 1024 * 1024,
|
|
19
|
+
maxReadBytes: 1024 * 1024
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One range of an artifact's bytes, as `IndeRun.readArtifact` returns it.
|
|
24
|
+
*/
|
|
25
|
+
export interface ArtifactChunk {
|
|
26
|
+
/** The artifact the bytes belong to. */
|
|
27
|
+
artifactId: string;
|
|
28
|
+
/** Byte offset of `bytes` within the artifact. */
|
|
29
|
+
offset: number;
|
|
30
|
+
/** The bytes of this range; empty when `offset` is the artifact's end. */
|
|
31
|
+
bytes: Uint8Array;
|
|
32
|
+
/** Size of the whole artifact in bytes. */
|
|
33
|
+
totalBytes: number;
|
|
34
|
+
/** Whether this range reaches the end of the artifact. */
|
|
35
|
+
done: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What a provider declares when it starts writing an artifact. */
|
|
39
|
+
export interface ArtifactSpec {
|
|
40
|
+
/** IANA media type of the bytes, e.g. `audio/wav`; parameters ride on it. */
|
|
41
|
+
mediaType: string;
|
|
42
|
+
/** Duration of time-based media in milliseconds, where the provider knows it. */
|
|
43
|
+
durationMs?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Writes one artifact into the engine's store. `finish()` hands it over: it commits the run
|
|
48
|
+
* to the provider and returns the reference the provider puts in its output. A provider that
|
|
49
|
+
* fails or stops before `finish()` calls `abort()`; the engine also discards any writer an
|
|
50
|
+
* attempt left open.
|
|
51
|
+
*/
|
|
52
|
+
export interface ArtifactWriter {
|
|
53
|
+
/**
|
|
54
|
+
* Appends bytes. Throws `Unavailable` (`details.code: "artifact_too_large"`) once the
|
|
55
|
+
* artifact would exceed the engine's per-artifact limit; the attempt has not committed, so
|
|
56
|
+
* the run may fall back.
|
|
57
|
+
*/
|
|
58
|
+
write(bytes: Uint8Array): void;
|
|
59
|
+
/**
|
|
60
|
+
* Hands the finished artifact to the engine and commits the run. Throws `Unavailable`
|
|
61
|
+
* (`artifact_store_full`) when the store has no room, and `Unavailable`
|
|
62
|
+
* (`run_not_active`) when the run was cancelled or has ended -- in both cases before
|
|
63
|
+
* committing.
|
|
64
|
+
*/
|
|
65
|
+
finish(): MediaArtifact;
|
|
66
|
+
/** Discards everything written. Idempotent. */
|
|
67
|
+
abort(): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Where a provider creates artifacts: `RunContext.artifacts`. */
|
|
71
|
+
export interface ArtifactSink {
|
|
72
|
+
create(spec: ArtifactSpec): ArtifactWriter;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface StoredArtifact {
|
|
76
|
+
reference: MediaArtifact;
|
|
77
|
+
bytes: Uint8Array;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The engine-owned, in-memory artifact store (ADR 017): one per `IndeRun` instance. It holds
|
|
82
|
+
* every finished artifact until the application disposes it; there are no temporary files,
|
|
83
|
+
* so nothing outlives the process. Reads are ranged and bounded by `maxReadBytes`.
|
|
84
|
+
*/
|
|
85
|
+
export class ArtifactStore {
|
|
86
|
+
private readonly limits: Required<ArtifactLimits>;
|
|
87
|
+
private readonly artifacts = new Map<string, StoredArtifact>();
|
|
88
|
+
private storedBytes = 0;
|
|
89
|
+
|
|
90
|
+
constructor(limits: ArtifactLimits = {}) {
|
|
91
|
+
this.limits = { ...DEFAULT_ARTIFACT_LIMITS, ...limits };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Opens the artifacts of one attempt. `commit` is the attempt's commit hook; a writer's
|
|
96
|
+
* `finish()` calls it, and refuses the handoff when it returns `false`.
|
|
97
|
+
*/
|
|
98
|
+
attempt(commit: () => boolean): AttemptArtifacts {
|
|
99
|
+
return new AttemptArtifacts(this, commit);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Reads one range. See `IndeRunApi.readArtifact` for the contract. */
|
|
103
|
+
read(request: ArtifactReadRequest): ArtifactChunk {
|
|
104
|
+
const stored = this.artifacts.get(request.artifactId);
|
|
105
|
+
if (stored === undefined) {
|
|
106
|
+
throw createUnavailable(`Artifact '${request.artifactId}' is not held by this engine.`, {
|
|
107
|
+
retryable: false,
|
|
108
|
+
details: { code: "artifact_not_found" }
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const totalBytes = stored.bytes.byteLength;
|
|
112
|
+
const offset = request.offset ?? 0;
|
|
113
|
+
if (offset > totalBytes) {
|
|
114
|
+
const validationIssues = [
|
|
115
|
+
{
|
|
116
|
+
path: "/offset",
|
|
117
|
+
message: `offset ${offset} is beyond the artifact's ${totalBytes} bytes`
|
|
118
|
+
}
|
|
119
|
+
];
|
|
120
|
+
throw createInternal(
|
|
121
|
+
`Validation failed for ArtifactReadRequest: /offset - ${validationIssues[0]!.message}`,
|
|
122
|
+
{ details: { validationIssues } }
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const length = Math.min(request.maxBytes ?? this.limits.maxReadBytes, this.limits.maxReadBytes);
|
|
126
|
+
const end = Math.min(offset + length, totalBytes);
|
|
127
|
+
return {
|
|
128
|
+
artifactId: request.artifactId,
|
|
129
|
+
offset,
|
|
130
|
+
// A copy: the caller may keep or mutate it without touching the stored bytes.
|
|
131
|
+
bytes: stored.bytes.slice(offset, end),
|
|
132
|
+
totalBytes,
|
|
133
|
+
done: end === totalBytes
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Releases an artifact's bytes. Idempotent; unknown ids are ignored. */
|
|
138
|
+
dispose(artifactId: string): void {
|
|
139
|
+
const stored = this.artifacts.get(artifactId);
|
|
140
|
+
if (stored === undefined) return;
|
|
141
|
+
this.artifacts.delete(artifactId);
|
|
142
|
+
this.storedBytes -= stored.bytes.byteLength;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The stored reference for `artifactId`, or `undefined` once it was disposed. */
|
|
146
|
+
reference(artifactId: string): MediaArtifact | undefined {
|
|
147
|
+
return this.artifacts.get(artifactId)?.reference;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** @internal Registers a finished artifact. */
|
|
151
|
+
store(spec: ArtifactSpec, bytes: Uint8Array): MediaArtifact {
|
|
152
|
+
if (this.storedBytes + bytes.byteLength > this.limits.maxStoreBytes) {
|
|
153
|
+
throw createUnavailable(
|
|
154
|
+
"The engine's artifact store is full; dispose artifacts to free it.",
|
|
155
|
+
{
|
|
156
|
+
details: { code: "artifact_store_full" }
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const reference: MediaArtifact = {
|
|
161
|
+
id: `art_${randomId()}`,
|
|
162
|
+
mediaType: spec.mediaType,
|
|
163
|
+
owner: "engine",
|
|
164
|
+
lifetime: "until_disposed",
|
|
165
|
+
byteLength: bytes.byteLength,
|
|
166
|
+
...(spec.durationMs !== undefined ? { durationMs: spec.durationMs } : {})
|
|
167
|
+
};
|
|
168
|
+
this.artifacts.set(reference.id, { reference, bytes });
|
|
169
|
+
this.storedBytes += bytes.byteLength;
|
|
170
|
+
return reference;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** @internal The whole artifact, for writing it to an application destination. */
|
|
174
|
+
bytes(artifactId: string): Uint8Array | undefined {
|
|
175
|
+
return this.artifacts.get(artifactId)?.bytes;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** @internal */
|
|
179
|
+
get maxArtifactBytes(): number {
|
|
180
|
+
return this.limits.maxArtifactBytes;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The artifacts one attempt created. The engine keeps the one the attempt returned and
|
|
186
|
+
* releases the rest: everything from an attempt that failed, was cancelled or lost to
|
|
187
|
+
* fallback, and anything a winning attempt finished but did not return. A returned artifact
|
|
188
|
+
* is never released behind the application's back.
|
|
189
|
+
*/
|
|
190
|
+
export class AttemptArtifacts implements ArtifactSink {
|
|
191
|
+
private readonly finished = new Set<string>();
|
|
192
|
+
private readonly open = new Set<ArtifactWriter>();
|
|
193
|
+
private released = false;
|
|
194
|
+
|
|
195
|
+
constructor(
|
|
196
|
+
private readonly store: ArtifactStore,
|
|
197
|
+
private readonly commit: () => boolean
|
|
198
|
+
) {}
|
|
199
|
+
|
|
200
|
+
create(spec: ArtifactSpec): ArtifactWriter {
|
|
201
|
+
const chunks: Uint8Array[] = [];
|
|
202
|
+
let size = 0;
|
|
203
|
+
let closed = false;
|
|
204
|
+
const writer: ArtifactWriter = {
|
|
205
|
+
write: (bytes) => {
|
|
206
|
+
if (closed) throw createInternal("The artifact writer is already closed.");
|
|
207
|
+
if (size + bytes.byteLength > this.store.maxArtifactBytes) {
|
|
208
|
+
writer.abort();
|
|
209
|
+
throw createUnavailable(
|
|
210
|
+
`The artifact exceeds the engine's ${this.store.maxArtifactBytes}-byte limit.`,
|
|
211
|
+
{ details: { code: "artifact_too_large" } }
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
chunks.push(bytes.slice());
|
|
215
|
+
size += bytes.byteLength;
|
|
216
|
+
},
|
|
217
|
+
finish: () => {
|
|
218
|
+
if (closed) throw createInternal("The artifact writer is already closed.");
|
|
219
|
+
const bytes = concat(chunks, size);
|
|
220
|
+
writer.abort();
|
|
221
|
+
// A provider that keeps working after its attempt ended (a late callback, say) must
|
|
222
|
+
// not leave an artifact behind that nobody owns.
|
|
223
|
+
if (this.released) {
|
|
224
|
+
throw createUnavailable("The attempt has ended; the artifact was discarded.", {
|
|
225
|
+
details: { code: "run_not_active" }
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
// Stored before the commit so a full store fails the attempt pre-commit; dropped
|
|
229
|
+
// again when the run refuses the commit.
|
|
230
|
+
const reference = this.store.store(spec, bytes);
|
|
231
|
+
if (!this.commit()) {
|
|
232
|
+
this.store.dispose(reference.id);
|
|
233
|
+
throw createUnavailable("The run is no longer active; the artifact was discarded.", {
|
|
234
|
+
details: { code: "run_not_active" }
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
this.finished.add(reference.id);
|
|
238
|
+
return reference;
|
|
239
|
+
},
|
|
240
|
+
abort: () => {
|
|
241
|
+
closed = true;
|
|
242
|
+
chunks.length = 0;
|
|
243
|
+
this.open.delete(writer);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
this.open.add(writer);
|
|
247
|
+
return writer;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Whether `artifactId` was finished by this attempt and is still held. */
|
|
251
|
+
owns(artifactId: string): boolean {
|
|
252
|
+
return this.finished.has(artifactId) && this.store.reference(artifactId) !== undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Releases every artifact of this attempt except `keep`, and closes open writers. */
|
|
256
|
+
release(keep?: string): void {
|
|
257
|
+
this.released = true;
|
|
258
|
+
for (const writer of [...this.open]) writer.abort();
|
|
259
|
+
for (const id of this.finished) {
|
|
260
|
+
if (id !== keep) this.store.dispose(id);
|
|
261
|
+
}
|
|
262
|
+
this.finished.clear();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function concat(chunks: Uint8Array[], size: number): Uint8Array {
|
|
267
|
+
const bytes = new Uint8Array(size);
|
|
268
|
+
let offset = 0;
|
|
269
|
+
for (const chunk of chunks) {
|
|
270
|
+
bytes.set(chunk, offset);
|
|
271
|
+
offset += chunk.byteLength;
|
|
272
|
+
}
|
|
273
|
+
return bytes;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function randomId(): string {
|
|
277
|
+
const random = new Uint8Array(12);
|
|
278
|
+
globalThis.crypto.getRandomValues(random);
|
|
279
|
+
return Array.from(random, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
280
|
+
}
|