@try-works/tool-mode-runtime-litert 0.0.0-stage → 0.1.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.
Files changed (50) hide show
  1. package/lib/ear-frontend.d.ts +113 -0
  2. package/lib/ear-frontend.d.ts.map +1 -0
  3. package/lib/ear-frontend.js +258 -0
  4. package/lib/ear.d.ts +51 -0
  5. package/lib/ear.d.ts.map +1 -0
  6. package/lib/ear.js +296 -0
  7. package/lib/emo-tokenizer.d.ts +87 -0
  8. package/lib/emo-tokenizer.d.ts.map +1 -0
  9. package/lib/emo-tokenizer.js +365 -0
  10. package/lib/emo.d.ts +53 -0
  11. package/lib/emo.d.ts.map +1 -0
  12. package/lib/emo.js +218 -0
  13. package/lib/errors.d.ts +14 -0
  14. package/lib/errors.d.ts.map +1 -0
  15. package/lib/errors.js +29 -0
  16. package/lib/index.d.ts +28 -0
  17. package/lib/index.d.ts.map +1 -0
  18. package/lib/index.js +34 -0
  19. package/lib/model.d.ts +103 -0
  20. package/lib/model.d.ts.map +1 -0
  21. package/lib/model.js +112 -0
  22. package/lib/moderator.d.ts +59 -0
  23. package/lib/moderator.d.ts.map +1 -0
  24. package/lib/moderator.js +189 -0
  25. package/lib/pillow.d.ts +75 -0
  26. package/lib/pillow.d.ts.map +1 -0
  27. package/lib/pillow.js +187 -0
  28. package/lib/png.d.ts +27 -0
  29. package/lib/png.d.ts.map +1 -0
  30. package/lib/png.js +176 -0
  31. package/lib/processor.d.ts +43 -0
  32. package/lib/processor.d.ts.map +1 -0
  33. package/lib/processor.js +48 -0
  34. package/lib/provider-factory.d.ts +44 -0
  35. package/lib/provider-factory.d.ts.map +1 -0
  36. package/lib/provider-factory.js +219 -0
  37. package/lib/runtime.d.ts +41 -0
  38. package/lib/runtime.d.ts.map +1 -0
  39. package/lib/runtime.js +66 -0
  40. package/lib/shapes.d.ts +28 -0
  41. package/lib/shapes.d.ts.map +1 -0
  42. package/lib/shapes.js +187 -0
  43. package/lib/shim.d.ts +45 -0
  44. package/lib/shim.d.ts.map +1 -0
  45. package/lib/shim.js +215 -0
  46. package/lib/wav.d.ts +39 -0
  47. package/lib/wav.d.ts.map +1 -0
  48. package/lib/wav.js +134 -0
  49. package/package.json +47 -4
  50. package/README.md +0 -3
@@ -0,0 +1,219 @@
1
+ /**
2
+ * The declarative provider factory (Phase 2, docs/runtime/PHASE2.md).
3
+ *
4
+ * A code-free package names this engine in its recipe, and the host -- which declares no
5
+ * engine dependency (AT-25) -- calls the factory with the parsed recipe and the host-verified
6
+ * asset map. The factory owns the engine and the PROCESSORS; the recipe supplies the artifact
7
+ * location, the limits, the tool schemas and the processor id.
8
+ *
9
+ * PROCESSORS IMPLEMENTED HERE (the models live in their own modules):
10
+ * - shapes/points-v1 -> src/shapes.ts (stroke -> 128 points -> 13 classes)
11
+ * - moderator/crops-v1 -> src/moderator.ts (PNG -> Pillow crops -> 5 NSFW heads)
12
+ *
13
+ * THE SDK BOUNDARY IS PROMISE-BASED, THE ENGINE IS EFFECT-BASED. This module keeps that
14
+ * mismatch in one place: a ManagedRuntime wraps the LiteRT session, load() forces the lazy
15
+ * runtime into existence so a broken deployment is reported at load time, and invoke() runs
16
+ * the selected processor, which maps the recipe's JSON contract onto serialized inference.
17
+ *
18
+ * TYPED FAILURES: internally every failure is InvalidInput, RuntimeUnavailable or
19
+ * InferenceFailed. The SDK declares Promise methods, and a Schema.TaggedError carries an EMPTY
20
+ * Error.message, so the boundary throws a readable Error whose cause is the typed value. The
21
+ * typed error is therefore still inspectable (error.cause) while a host log is not blank.
22
+ */
23
+ /*
24
+ * This file imports the shapes processor whose domain names contain "shape" (the 2D geometric
25
+ * shapes the model recognizes and the tool schema's "shape" field). The vendored anti-slop
26
+ * no-shape rule targets structural placeholders, so it is disabled for this file only.
27
+ */
28
+ /* oxlint-disable anti-slop/no-shape-in-symbol-names */
29
+ import { readFile } from 'node:fs/promises';
30
+ import path from 'node:path';
31
+ import { Effect, ManagedRuntime } from 'effect';
32
+ import { InferenceFailed, RuntimeUnavailable, } from '@try-works/tool-mode-runtime-core';
33
+ import { EAR_PROCESSOR, earMelProcessor } from "./ear.js";
34
+ import { EMO_PROCESSOR, emoTextProcessor } from "./emo.js";
35
+ import { describeCause } from "./errors.js";
36
+ import { MODERATOR_PROCESSOR, moderatorCropsProcessor } from "./moderator.js";
37
+ import { describeFailure } from "./processor.js";
38
+ import { litertLayer } from "./runtime.js";
39
+ import { SHAPES_POINTS_PROCESSOR, shapesPointsProcessor } from "./shapes.js";
40
+ import { resolveLitertWasmDir } from "./shim.js";
41
+ /** The processors this engine package implements, keyed by the recipe's processor id. */
42
+ const PROCESSORS = new Map([
43
+ [shapesPointsProcessor.id, shapesPointsProcessor],
44
+ [moderatorCropsProcessor.id, moderatorCropsProcessor],
45
+ [earMelProcessor.id, earMelProcessor],
46
+ [emoTextProcessor.id, emoTextProcessor],
47
+ ]);
48
+ /** The ids a recipe may name, in a stable order for refusal messages. */
49
+ const PROCESSOR_IDS = [SHAPES_POINTS_PROCESSOR, MODERATOR_PROCESSOR, EAR_PROCESSOR, EMO_PROCESSOR];
50
+ const PROVIDER = 'litert';
51
+ /** Wrap a typed failure for the Promise-based SDK boundary. */
52
+ function boundaryError(message, cause) {
53
+ return new Error(message, { cause });
54
+ }
55
+ /** The first asset map that resolves the recipe's asset set wins. */
56
+ function resolveAssetSet(primary, fallback, assetSet) {
57
+ return primary.get(assetSet) ?? fallback.get(assetSet);
58
+ }
59
+ async function readModelArtifact(recipe, primary, fallback) {
60
+ const assetSet = recipe.model.assetSet;
61
+ const entry = resolveAssetSet(primary, fallback, assetSet);
62
+ if (entry === undefined) {
63
+ throw new RuntimeUnavailable({
64
+ provider: PROVIDER,
65
+ reason: 'the "' + assetSet + '" asset set is not prepared; load() needs the host-verified asset map',
66
+ });
67
+ }
68
+ const directory = path.resolve(entry.path);
69
+ const file = path.resolve(directory, recipe.model.fileName);
70
+ if (file !== directory && !file.startsWith(directory + path.sep)) {
71
+ throw new RuntimeUnavailable({
72
+ provider: PROVIDER,
73
+ reason: 'the recipe file name "' + recipe.model.fileName + '" escapes the "' + assetSet + '" asset directory',
74
+ });
75
+ }
76
+ try {
77
+ return { bytes: new Uint8Array(await readFile(file)), directory };
78
+ }
79
+ catch (cause) {
80
+ throw new RuntimeUnavailable({
81
+ provider: PROVIDER,
82
+ reason: 'the "' + assetSet + '" model artifact "' + recipe.model.fileName + '" cannot be read: ' + describeCause(cause),
83
+ });
84
+ }
85
+ }
86
+ /**
87
+ * Build the SDK provider for one validated, code-free model-runtime recipe.
88
+ *
89
+ * Creation is lightweight (EXT-19): it validates what can be validated statically and reads
90
+ * nothing. Assets are resolved at load() time, preferring the map the SDK hands to load()
91
+ * over the map captured at creation, because the host may build the factory before a
92
+ * preparation completes.
93
+ *
94
+ * @param recipe - the parsed, frozen model-runtime recipe.
95
+ * @param assets - the host-verified asset map captured at creation.
96
+ * @param options - test seams (a fake engine boot); production callers omit it.
97
+ * @returns the SDK's Promise-based provider.
98
+ */
99
+ export function createProviderFromRecipe(recipe, assets, options = {}) {
100
+ const label = recipe.tool.name;
101
+ if (recipe.model.format !== 'tflite') {
102
+ throw new Error('the litert provider factory executes only tflite artifacts; the recipe "' + label + '" declares "' + recipe.model.format + '"');
103
+ }
104
+ const processor = PROCESSORS.get(recipe.processor.id);
105
+ if (processor === undefined) {
106
+ throw new Error('the litert provider factory does not implement processor "' + recipe.processor.id + '"; this build implements ' + PROCESSOR_IDS.join(', '));
107
+ }
108
+ let state = { _tag: 'cold' };
109
+ const inspect = async (signal) => {
110
+ if (signal.aborted)
111
+ throw new Error('the "' + label + '" inspection was aborted before it started');
112
+ switch (state._tag) {
113
+ case 'disposed':
114
+ return { readiness: 'unavailable', features: [], missing: [], reason: 'the provider has been disposed' };
115
+ case 'failed':
116
+ return { readiness: 'failed', features: [], missing: [], reason: state.reason };
117
+ case 'cold':
118
+ return {
119
+ readiness: 'prepared-cold',
120
+ features: processor.features,
121
+ missing: [],
122
+ reason: label + ' has not been loaded yet; load() must run before the first call',
123
+ };
124
+ case 'ready':
125
+ return { readiness: 'ready', features: processor.features, missing: [] };
126
+ }
127
+ };
128
+ const load = async (request) => {
129
+ if (request.signal.aborted)
130
+ throw new Error('the "' + label + '" load was aborted before it started');
131
+ if (state._tag === 'disposed')
132
+ throw new Error('the provider has been disposed');
133
+ if (state._tag === 'ready')
134
+ return;
135
+ try {
136
+ const artifact = await readModelArtifact(recipe, request.assets, assets);
137
+ const wasmDir = resolveLitertWasmDir();
138
+ const layer = options.boot === undefined
139
+ ? litertLayer({ wasmDir, modelBytes: artifact.bytes })
140
+ : litertLayer({ wasmDir, modelBytes: artifact.bytes, boot: options.boot });
141
+ const candidate = ManagedRuntime.make(layer);
142
+ try {
143
+ // The runtime is lazy; force the layer to build so a broken deployment fails HERE.
144
+ await candidate.runPromise(Effect.void, { signal: request.signal });
145
+ }
146
+ catch (cause) {
147
+ await candidate.dispose().catch(() => undefined);
148
+ throw cause;
149
+ }
150
+ state = { _tag: 'ready', runtime: candidate, assetDirectory: artifact.directory };
151
+ }
152
+ catch (cause) {
153
+ const reason = describeFailure(cause);
154
+ state = { _tag: 'failed', reason };
155
+ throw boundaryError('the "' + label + '" model failed to load: ' + reason, cause);
156
+ }
157
+ };
158
+ const invoke = async (request) => {
159
+ if (request.context.signal.aborted)
160
+ throw new Error('the "' + label + '" invocation was aborted before it started');
161
+ switch (state._tag) {
162
+ case 'disposed':
163
+ throw new Error('the provider has been disposed');
164
+ case 'failed':
165
+ throw new Error('the "' + label + '" runtime is not loaded: ' + state.reason);
166
+ case 'cold':
167
+ throw new Error('the "' + label + '" runtime is not loaded: load() must be called before invoke()');
168
+ case 'ready': {
169
+ const runtime = state.runtime;
170
+ const controller = new AbortController();
171
+ const relayAbort = () => controller.abort(request.context.signal.reason);
172
+ request.context.signal.addEventListener('abort', relayAbort, { once: true });
173
+ const timer = setTimeout(() => {
174
+ controller.abort(new Error('the "' + label + '" invocation timed out after ' + String(recipe.limits.timeoutMs) + 'ms'));
175
+ }, recipe.limits.timeoutMs);
176
+ try {
177
+ const program = processor.invoke({
178
+ input: request.input,
179
+ context: request.context,
180
+ limits: recipe.limits,
181
+ assetDirectory: state.assetDirectory,
182
+ });
183
+ return await runtime.runPromise(program, { signal: controller.signal });
184
+ }
185
+ catch (cause) {
186
+ if (request.context.signal.aborted) {
187
+ throw boundaryError('the "' + label + '" invocation was aborted', cause);
188
+ }
189
+ if (controller.signal.aborted) {
190
+ const message = 'the "' + label + '" inference timed out after ' + String(recipe.limits.timeoutMs) + 'ms';
191
+ throw boundaryError(message, new InferenceFailed({ provider: PROVIDER, phase: 'run', message }));
192
+ }
193
+ throw boundaryError('the "' + label + '" invocation failed: ' + describeFailure(cause), cause);
194
+ }
195
+ finally {
196
+ clearTimeout(timer);
197
+ request.context.signal.removeEventListener('abort', relayAbort);
198
+ }
199
+ }
200
+ }
201
+ };
202
+ const unload = async (_signal) => {
203
+ if (state._tag !== 'ready')
204
+ return;
205
+ const runtime = state.runtime;
206
+ state = { _tag: 'cold' };
207
+ await runtime.dispose();
208
+ };
209
+ const dispose = async () => {
210
+ if (state._tag === 'disposed')
211
+ return;
212
+ const runtime = state._tag === 'ready' ? state.runtime : undefined;
213
+ state = { _tag: 'disposed' };
214
+ if (runtime !== undefined)
215
+ await runtime.dispose();
216
+ };
217
+ return { inspect, load, invoke, unload, dispose };
218
+ }
219
+ //# sourceMappingURL=provider-factory.js.map
@@ -0,0 +1,41 @@
1
+ /**
2
+ * `LitertSession`: the Effect service a model provider depends on.
3
+ *
4
+ * Contract (docs/runtime/PLAN.md): the layer builds one compiled model and acquires it in
5
+ * the layer scope; `run` is serialized with `Semaphore.make(1)` because the LiteRT C ABI
6
+ * keeps module-global state; a failed load fails the layer with `RuntimeUnavailable`,
7
+ * while a failed run or output conversion fails with `InferenceFailed` (`run`/`post`).
8
+ *
9
+ * TESTS: the unit suite injects a fake `LitertBoot`, so it proves the lifecycle, the feed
10
+ * mapping, the serialization and the error mapping — not real inference.
11
+ */
12
+ import { Context, Effect, Layer } from 'effect';
13
+ import { InferenceFailed, RuntimeUnavailable } from './errors.ts';
14
+ import { type LitertBoot, type LitertFeed } from './model.ts';
15
+ /** The outputs of one inference, keyed by the model's output name. */
16
+ export interface LitertOutputs {
17
+ readonly [name: string]: Float32Array;
18
+ }
19
+ /** The session members the provider layer uses. */
20
+ export interface LitertSessionApi {
21
+ readonly inputNames: readonly string[];
22
+ readonly run: (feeds: Readonly<Record<string, LitertFeed>>) => Effect.Effect<LitertOutputs, InferenceFailed>;
23
+ }
24
+ declare const LitertSession_base: Context.ServiceClass<LitertSession, "tool-mode-runtime/LitertSession", LitertSessionApi>;
25
+ /** One compiled LiteRT model, safe to share across concurrent callers. */
26
+ export declare class LitertSession extends LitertSession_base {
27
+ }
28
+ /** Options for `litertLayer`; `boot` is the unit-test seam. */
29
+ export interface LitertLayerOptions {
30
+ readonly wasmDir: string;
31
+ readonly modelBytes: Uint8Array;
32
+ readonly threads?: number;
33
+ readonly boot?: LitertBoot;
34
+ }
35
+ /**
36
+ * Builds the session for one compiled model. The model is acquired when the layer builds,
37
+ * released when the layer's scope closes, and every `run` holds the single permit.
38
+ */
39
+ export declare const litertLayer: (options: LitertLayerOptions) => Layer.Layer<LitertSession, RuntimeUnavailable>;
40
+ export {};
41
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAa,MAAM,QAAQ,CAAA;AAE1D,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAiB,MAAM,aAAa,CAAA;AAChF,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,UAAU,EAEhB,MAAM,YAAY,CAAA;AAInB,sEAAsE;AACtE,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAAA;CACtC;AAED,mDAAmD;AACnD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAA;IACtC,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,eAAe,CAAC,CAAA;CAC7G;;AAED,0EAA0E;AAC1E,qBAAa,aAAc,SAAQ,kBAElC;CAAG;AAEJ,+DAA+D;AAC/D,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAA;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,CAAA;CAC3B;AAYD;;;GAGG;AACH,eAAO,MAAM,WAAW,GAAI,SAAS,kBAAkB,KAAG,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE,kBAAkB,CAsDpG,CAAA"}
package/lib/runtime.js ADDED
@@ -0,0 +1,66 @@
1
+ /**
2
+ * `LitertSession`: the Effect service a model provider depends on.
3
+ *
4
+ * Contract (docs/runtime/PLAN.md): the layer builds one compiled model and acquires it in
5
+ * the layer scope; `run` is serialized with `Semaphore.make(1)` because the LiteRT C ABI
6
+ * keeps module-global state; a failed load fails the layer with `RuntimeUnavailable`,
7
+ * while a failed run or output conversion fails with `InferenceFailed` (`run`/`post`).
8
+ *
9
+ * TESTS: the unit suite injects a fake `LitertBoot`, so it proves the lifecycle, the feed
10
+ * mapping, the serialization and the error mapping — not real inference.
11
+ */
12
+ import { Context, Effect, Layer, Semaphore } from 'effect';
13
+ import { InferenceFailed, RuntimeUnavailable, describeCause } from "./errors.js";
14
+ import { createNodeLitertBoot, } from "./model.js";
15
+ const PROVIDER = 'litert';
16
+ /** One compiled LiteRT model, safe to share across concurrent callers. */
17
+ export class LitertSession extends Context.Service()('tool-mode-runtime/LitertSession') {
18
+ }
19
+ const toFloat32 = (name, value) => {
20
+ if (value instanceof Float32Array)
21
+ return value;
22
+ if (value instanceof Int32Array)
23
+ return Float32Array.from(value);
24
+ // A fake or a future engine can return something else; that is a post-processing failure.
25
+ throw new Error(`the compiled model returned output ${name} as an unsupported value`);
26
+ };
27
+ const toOutputs = (raw) => Object.fromEntries(Object.entries(raw).map(([name, value]) => [name, toFloat32(name, value)]));
28
+ /**
29
+ * Builds the session for one compiled model. The model is acquired when the layer builds,
30
+ * released when the layer's scope closes, and every `run` holds the single permit.
31
+ */
32
+ export const litertLayer = (options) => Layer.effect(LitertSession, Effect.gen(function* () {
33
+ const boot = options.boot ??
34
+ createNodeLitertBoot(options.threads === undefined
35
+ ? { wasmDir: options.wasmDir }
36
+ : { wasmDir: options.wasmDir, threads: options.threads });
37
+ const loadOptions = options.threads === undefined
38
+ ? { modelBytes: options.modelBytes }
39
+ : { modelBytes: options.modelBytes, threads: options.threads };
40
+ const model = yield* Effect.acquireRelease(Effect.tryPromise({
41
+ try: () => boot.load(loadOptions),
42
+ catch: (cause) => cause instanceof RuntimeUnavailable
43
+ ? cause
44
+ : new RuntimeUnavailable({ provider: PROVIDER, reason: describeCause(cause) }),
45
+ }), (loaded) => Effect.sync(() => {
46
+ try {
47
+ loaded.close();
48
+ }
49
+ catch {
50
+ // Teardown must not fail; the Wasm module may already be unusable.
51
+ }
52
+ }));
53
+ const permits = yield* Semaphore.make(1);
54
+ const run = (feeds) => permits.withPermit(Effect.gen(function* () {
55
+ const raw = yield* Effect.tryPromise({
56
+ try: () => model.run(feeds),
57
+ catch: (cause) => new InferenceFailed({ provider: PROVIDER, phase: 'run', message: describeCause(cause) }),
58
+ });
59
+ return yield* Effect.try({
60
+ try: () => toOutputs(raw),
61
+ catch: (cause) => new InferenceFailed({ provider: PROVIDER, phase: 'post', message: describeCause(cause) }),
62
+ });
63
+ }));
64
+ return { inputNames: model.inputNames, run };
65
+ }));
66
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Processor shapes/points-v1 (Phase 2, extension-shapes-recipe).
3
+ *
4
+ * Input: { points: [[x, y], ...] (at least two finite pairs), closed?: boolean }.
5
+ * closed === true appends the first point; the stroke is resampled to 128 points by arc
6
+ * length, the bounding box is centred on the origin and the longer side is scaled to 1.
7
+ * The model input is points [1,128,2] float32; outputs are probs [13] + head [128].
8
+ * Output: { shape, confidence, candidates, rejected } in the MEASURED class order.
9
+ */
10
+ import { type RecipeProcessor } from './processor.ts';
11
+ /** The processor id this module owns. */
12
+ export declare const SHAPES_POINTS_PROCESSOR = "shapes/points-v1";
13
+ /** The fixed stroke length the model consumes. */
14
+ export declare const SHAPES_POINT_COUNT = 128;
15
+ /** The number of classes the classifier head emits. */
16
+ export declare const SHAPES_CLASS_COUNT = 13;
17
+ /**
18
+ * The model's class-index order, measured on the pinned shapes.tflite artifact (2026-11-14):
19
+ * a horizontal line puts 0.9505 on index 1, an axis-aligned rectangle 0.9534 on index 3, a
20
+ * triangle 0.9528 on index 5 and a circle 0.9514 on index 4. That matches the pinned
21
+ * shapes_meta.json order, NOT the order a JSON-Schema enum happens to list.
22
+ */
23
+ export declare const SHAPES_CLASSES: readonly ["none", "line", "arrow", "rectangle", "ellipse", "triangle", "diamond", "pentagon", "hexagon", "star", "heart", "cloud", "scribble"];
24
+ /** One class name from the measured model order. */
25
+ export type ShapesClass = (typeof SHAPES_CLASSES)[number];
26
+ /** The shapes processor. */
27
+ export declare const shapesPointsProcessor: RecipeProcessor;
28
+ //# sourceMappingURL=shapes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shapes.d.ts","sourceRoot":"","sources":["../src/shapes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAgBH,OAAO,EAAyC,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAG5F,yCAAyC;AACzC,eAAO,MAAM,uBAAuB,qBAAqB,CAAA;AAEzD,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC,uDAAuD;AACvD,eAAO,MAAM,kBAAkB,KAAK,CAAA;AAEpC;;;;;GAKG;AACH,eAAO,MAAM,cAAc,gJAcjB,CAAA;AAEV,oDAAoD;AACpD,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAA;AAoIzD,4BAA4B;AAC5B,eAAO,MAAM,qBAAqB,EAAE,eAanC,CAAA"}
package/lib/shapes.js ADDED
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Processor shapes/points-v1 (Phase 2, extension-shapes-recipe).
3
+ *
4
+ * Input: { points: [[x, y], ...] (at least two finite pairs), closed?: boolean }.
5
+ * closed === true appends the first point; the stroke is resampled to 128 points by arc
6
+ * length, the bounding box is centred on the origin and the longer side is scaled to 1.
7
+ * The model input is points [1,128,2] float32; outputs are probs [13] + head [128].
8
+ * Output: { shape, confidence, candidates, rejected } in the MEASURED class order.
9
+ */
10
+ /*
11
+ * The anti-slop no-shape rule targets structural "shape" placeholders in type names. Here
12
+ * "shape" is the DOMAIN: the shapes/points-v1 processor recognizes 2D geometric shapes, and
13
+ * the recipe's output schema field is literally "shape". Renaming the domain noun to satisfy
14
+ * the rule would make the contract less honest, so the rule is disabled for this file only.
15
+ */
16
+ /* oxlint-disable anti-slop/no-shape-in-symbol-names */
17
+ import { Effect, Schema } from 'effect';
18
+ import { InferenceFailed, InvalidInput } from '@try-works/tool-mode-runtime-core';
19
+ import { describeCause } from "./errors.js";
20
+ import { closedKeyFailure, inputCeilingFailure } from "./processor.js";
21
+ import { LitertSession } from "./runtime.js";
22
+ /** The processor id this module owns. */
23
+ export const SHAPES_POINTS_PROCESSOR = 'shapes/points-v1';
24
+ /** The fixed stroke length the model consumes. */
25
+ export const SHAPES_POINT_COUNT = 128;
26
+ /** The number of classes the classifier head emits. */
27
+ export const SHAPES_CLASS_COUNT = 13;
28
+ /**
29
+ * The model's class-index order, measured on the pinned shapes.tflite artifact (2026-11-14):
30
+ * a horizontal line puts 0.9505 on index 1, an axis-aligned rectangle 0.9534 on index 3, a
31
+ * triangle 0.9528 on index 5 and a circle 0.9514 on index 4. That matches the pinned
32
+ * shapes_meta.json order, NOT the order a JSON-Schema enum happens to list.
33
+ */
34
+ export const SHAPES_CLASSES = [
35
+ 'none',
36
+ 'line',
37
+ 'arrow',
38
+ 'rectangle',
39
+ 'ellipse',
40
+ 'triangle',
41
+ 'diamond',
42
+ 'pentagon',
43
+ 'hexagon',
44
+ 'star',
45
+ 'heart',
46
+ 'cloud',
47
+ 'scribble',
48
+ ];
49
+ const INPUT_KEYS = new Set(['points', 'closed']);
50
+ const ShapesInputSchema = Schema.Struct({
51
+ points: Schema.Array(Schema.Tuple([Schema.Finite, Schema.Finite])),
52
+ closed: Schema.optional(Schema.Boolean),
53
+ });
54
+ /** Refuse one input before any inference: size ceiling, closed keys and the stroke shape. */
55
+ function parseInput(input, maxInputBytes) {
56
+ const ceiling = inputCeilingFailure(input, { maxInputBytes, timeoutMs: 0 });
57
+ if (ceiling !== undefined)
58
+ return Effect.fail(ceiling);
59
+ const keys = closedKeyFailure(input, INPUT_KEYS, 'points and closed');
60
+ if (keys !== undefined)
61
+ return Effect.fail(keys);
62
+ const decoded = Schema.decodeUnknownOption(ShapesInputSchema)(input);
63
+ if (decoded._tag === 'None') {
64
+ return Effect.fail(new InvalidInput({
65
+ field: 'points',
66
+ message: 'points must be an array of [x, y] finite number pairs and closed must be a boolean when present',
67
+ }));
68
+ }
69
+ if (decoded.value.points.length < 2) {
70
+ return Effect.fail(new InvalidInput({ field: 'points', message: 'a stroke needs at least two points' }));
71
+ }
72
+ return Effect.succeed(decoded.value);
73
+ }
74
+ /**
75
+ * The processor's preprocessing: close when asked, resample by arc length to 128 points,
76
+ * centre the bounding box on the origin and scale the longer side to 1.
77
+ *
78
+ * A stroke with no length or a zero-extent bounding box is refused rather than fed as 128
79
+ * copies of one point: that is not a drawing the classifier can judge.
80
+ */
81
+ function resampleStroke(input) {
82
+ const source = input.points;
83
+ const first = source[0];
84
+ if (source.length < 2 || first === undefined)
85
+ throw new Error('a stroke needs at least two points');
86
+ const stroke = input.closed === true ? [...source, first] : [...source];
87
+ const cumulative = [0];
88
+ let total = 0;
89
+ for (let index = 1; index < stroke.length; index += 1) {
90
+ const previous = stroke[index - 1];
91
+ const current = stroke[index];
92
+ if (previous === undefined || current === undefined)
93
+ throw new Error('the stroke lost a point while resampling');
94
+ total += Math.hypot(current[0] - previous[0], current[1] - previous[1]);
95
+ cumulative.push(total);
96
+ }
97
+ if (total <= 0)
98
+ throw new Error('the stroke is degenerate: its total length is zero');
99
+ const xs = [];
100
+ const ys = [];
101
+ let segment = 0;
102
+ for (let sample = 0; sample < SHAPES_POINT_COUNT; sample += 1) {
103
+ const target = (sample * total) / (SHAPES_POINT_COUNT - 1);
104
+ while (segment < stroke.length - 2 && (cumulative[segment + 1] ?? 0) < target)
105
+ segment += 1;
106
+ const start = stroke[segment];
107
+ const end = stroke[segment + 1];
108
+ if (start === undefined || end === undefined)
109
+ throw new Error('the stroke lost a segment while resampling');
110
+ const startLength = cumulative[segment] ?? 0;
111
+ const endLength = cumulative[segment + 1] ?? startLength;
112
+ const span = endLength - startLength;
113
+ const portion = span > 0 ? (target - startLength) / span : 0;
114
+ xs.push(start[0] + (end[0] - start[0]) * portion);
115
+ ys.push(start[1] + (end[1] - start[1]) * portion);
116
+ }
117
+ const minX = Math.min(...xs);
118
+ const maxX = Math.max(...xs);
119
+ const minY = Math.min(...ys);
120
+ const maxY = Math.max(...ys);
121
+ const centerX = (minX + maxX) / 2;
122
+ const centerY = (minY + maxY) / 2;
123
+ const extent = Math.max(maxX - minX, maxY - minY);
124
+ if (extent <= 0)
125
+ throw new Error('the stroke is degenerate: its bounding box has zero extent');
126
+ const data = new Float32Array(SHAPES_POINT_COUNT * 2);
127
+ for (let index = 0; index < SHAPES_POINT_COUNT; index += 1) {
128
+ const x = xs[index];
129
+ const y = ys[index];
130
+ if (x === undefined || y === undefined)
131
+ throw new Error('the stroke produced too few samples');
132
+ data[index * 2] = (x - centerX) / extent;
133
+ data[index * 2 + 1] = (y - centerY) / extent;
134
+ }
135
+ return data;
136
+ }
137
+ /** The preprocessing as a typed effect: a degenerate stroke is an invalid input. */
138
+ function feedFor(input) {
139
+ return Effect.try({
140
+ try: () => resampleStroke(input),
141
+ catch: (cause) => new InvalidInput({ field: 'points', message: describeCause(cause) }),
142
+ });
143
+ }
144
+ /** Map the engine's 13-class probabilities onto the shapes recipe's output schema. */
145
+ function toolOutput(raw) {
146
+ return Effect.try({
147
+ try: () => {
148
+ const probs = raw['probs'];
149
+ const fitted = raw['head'];
150
+ if (!(probs instanceof Float32Array) || probs.length !== SHAPES_CLASS_COUNT) {
151
+ throw new Error('the model returned probs with ' + String(probs?.length ?? 'no') + ' values; expected ' + String(SHAPES_CLASS_COUNT));
152
+ }
153
+ if (!(fitted instanceof Float32Array) || fitted.length !== SHAPES_POINT_COUNT) {
154
+ throw new Error('the model returned head with ' + String(fitted?.length ?? 'no') + ' values; expected ' + String(SHAPES_POINT_COUNT));
155
+ }
156
+ const ranked = SHAPES_CLASSES.map((shape, index) => ({ shape, index, confidence: probs[index] ?? 0 })).sort((a, b) => b.confidence - a.confidence || a.index - b.index);
157
+ const top = ranked[0];
158
+ if (top === undefined)
159
+ throw new Error('the model returned no class probabilities');
160
+ const candidates = ranked
161
+ .slice(1)
162
+ .map((entry) => ({ shape: entry.shape, confidence: entry.confidence }));
163
+ return {
164
+ shape: top.shape,
165
+ confidence: top.confidence,
166
+ candidates,
167
+ rejected: top.shape === 'none' || top.shape === 'scribble',
168
+ };
169
+ },
170
+ catch: (cause) => new InferenceFailed({ provider: 'litert', phase: 'post', message: describeCause(cause) }),
171
+ });
172
+ }
173
+ /** The shapes processor. */
174
+ export const shapesPointsProcessor = {
175
+ id: SHAPES_POINTS_PROCESSOR,
176
+ features: [SHAPES_POINTS_PROCESSOR],
177
+ invoke: ({ input, limits }) => Effect.gen(function* () {
178
+ const parsed = yield* parseInput(input, limits.maxInputBytes);
179
+ const data = yield* feedFor(parsed);
180
+ const session = yield* LitertSession;
181
+ const raw = yield* session.run({
182
+ points: { data, dims: [1, SHAPES_POINT_COUNT, 2] },
183
+ });
184
+ return yield* toolOutput(raw);
185
+ }),
186
+ };
187
+ //# sourceMappingURL=shapes.js.map
package/lib/shim.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The socket-free LiteRT.js boot shim.
3
+ *
4
+ * WHY THIS EXISTS. `@litertjs/core` loads its Emscripten build by injecting a <script> into a
5
+ * browser document and fetching the Wasm binary from a URL. In node it finds neither, so its
6
+ * loader fails unless a document, a `__dirname`, and a URL scheme that resolves to a local file
7
+ * are supplied. The proven spike
8
+ * (D:/DEV/dsh-model-as-tool/spike/runtime-probe/litert/litert-file-shim.mjs, 2026-11-14)
9
+ * showed that installing those globals plus a `fetch` interceptor for `litert://wasm/` is
10
+ * sufficient — no HTTP server and no copied Wasm.
11
+ *
12
+ * WHAT IT DOES: installs `self/document/module/exports/require/__dirname/__filename` and a
13
+ * `fetch` interceptor that maps `litert://wasm/<file>` to `path.join(wasmDir, <file>)`
14
+ * (application/wasm for .wasm, text/javascript for .js). It never opens a socket and never
15
+ * copies the Wasm files.
16
+ *
17
+ * WHAT IT DOES NOT DO: it does not load or compile a model (see model.ts), and it does not
18
+ * restore itself automatically — keep the handle and call `dispose()` only when the process is
19
+ * done with LiteRT, because the Wasm module is process-global.
20
+ */
21
+ /** The only URL scheme this shim intercepts. */
22
+ export declare const LITERT_WASM_PREFIX = "litert://wasm/";
23
+ /** A handle for the installed shim; `dispose` restores the previous globals and fetch. */
24
+ export interface LitertShim {
25
+ readonly wasmDir: string;
26
+ readonly dispose: () => void;
27
+ }
28
+ /**
29
+ * Installs the globals and fetch interceptor LiteRT.js needs in node.
30
+ *
31
+ * Idempotent for one wasm directory; refuses a second directory in the same process with
32
+ * `RuntimeUnavailable` because the Wasm module is process-global. Call `dispose()` to restore
33
+ * the globals and fetch.
34
+ */
35
+ export declare const installLitertFetchShim: (options: {
36
+ wasmDir: string;
37
+ }) => LitertShim;
38
+ /**
39
+ * The directory of Emscripten files shipped inside the installed `@litertjs/core`.
40
+ *
41
+ * Resolution is relative to this module, so it works from the built `lib/` output in any host
42
+ * that installed the dependency (the package has no `exports` map restricting deep paths).
43
+ */
44
+ export declare const resolveLitertWasmDir: () => string;
45
+ //# sourceMappingURL=shim.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shim.d.ts","sourceRoot":"","sources":["../src/shim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAWH,gDAAgD;AAChD,eAAO,MAAM,kBAAkB,mBAAmB,CAAA;AAElD,0FAA0F;AAC1F,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,IAAI,CAAA;CAC7B;AAmKD;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,GAAI,SAAS;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAG,UA0CrE,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,QAAO,MAIvC,CAAA"}