@fgv/ts-extras-transformers 5.1.0-29
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/.rush/temp/cad001c0dd0af20627d8849ed0e5fa4904160f67.tar.log +30 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.build.chunks.jsonl +9 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.test.chunks.jsonl +28 -0
- package/.rush/temp/operation/build/all.log +9 -0
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/operation/test/all.log +28 -0
- package/.rush/temp/operation/test/log-chunks.jsonl +28 -0
- package/.rush/temp/operation/test/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +742 -0
- package/CHANGELOG.json +4 -0
- package/README.md +121 -0
- package/config/api-extractor.json +38 -0
- package/config/jest.config.json +13 -0
- package/config/rig.json +6 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/test/unit/transformers.test.js +197 -0
- package/dist/test/unit/transformers.test.js.map +1 -0
- package/dist/ts-extras-transformers.d.ts +137 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/eslint.config.js +15 -0
- package/etc/ts-extras-transformers.api.md +43 -0
- package/lib/index.d.ts +112 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +143 -0
- package/lib/index.js.map +1 -0
- package/lib/test/unit/transformers.test.d.ts +2 -0
- package/lib/test/unit/transformers.test.d.ts.map +1 -0
- package/lib/test/unit/transformers.test.js +232 -0
- package/lib/test/unit/transformers.test.js.map +1 -0
- package/package.json +80 -0
- package/rush-logs/ts-extras-transformers.build.cache.log +3 -0
- package/rush-logs/ts-extras-transformers.build.log +9 -0
- package/rush-logs/ts-extras-transformers.test.cache.log +1 -0
- package/rush-logs/ts-extras-transformers.test.log +28 -0
- package/src/index.ts +179 -0
- package/src/test/unit/transformers.test.ts +267 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +14 -0
- package/temp/build/typescript/ts_8nwakTlr.json +1 -0
- package/temp/coverage/base.css +224 -0
- package/temp/coverage/block-navigation.js +87 -0
- package/temp/coverage/favicon.png +0 -0
- package/temp/coverage/index.html +116 -0
- package/temp/coverage/index.ts.html +622 -0
- package/temp/coverage/lcov-report/base.css +224 -0
- package/temp/coverage/lcov-report/block-navigation.js +87 -0
- package/temp/coverage/lcov-report/favicon.png +0 -0
- package/temp/coverage/lcov-report/index.html +116 -0
- package/temp/coverage/lcov-report/index.ts.html +622 -0
- package/temp/coverage/lcov-report/prettify.css +1 -0
- package/temp/coverage/lcov-report/prettify.js +2 -0
- package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/temp/coverage/lcov-report/sorter.js +210 -0
- package/temp/coverage/lcov.info +209 -0
- package/temp/coverage/prettify.css +1 -0
- package/temp/coverage/prettify.js +2 -0
- package/temp/coverage/sort-arrow-sprite.png +0 -0
- package/temp/coverage/sorter.js +210 -0
- package/temp/test/jest/haste-map-502913d9c10083bec2207119c45d7122-cd1aa4a0322f87b67f395127aa5e94ac-091f69b131db2c080ae4b15b7b2d4a16 +0 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/8d/package_8dec8c7ec8d6c787063a4a014e769a65 +53 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/d5/package_d56b0a8a49dbfccad646fc2e21950522 +203 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/fc/versions_fc561f31b0dfb504443c5b8531747bfb +31 -0
- package/temp/test/jest/perf-cache-502913d9c10083bec2207119c45d7122-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
- package/temp/ts-extras-transformers.api.json +675 -0
- package/temp/ts-extras-transformers.api.md +43 -0
- package/tsconfig.json +8 -0
package/CHANGELOG.json
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @fgv/ts-extras-transformers
|
|
2
|
+
|
|
3
|
+
Result-integration boundary over [`@huggingface/transformers`](https://huggingface.co/docs/transformers.js/en/index)
|
|
4
|
+
for Node-side consumers. The companion package
|
|
5
|
+
[`@fgv/ts-web-extras-transformers`](../ts-web-extras-transformers) provides the same surface for
|
|
6
|
+
browser consumers.
|
|
7
|
+
|
|
8
|
+
**Status:** shipped — the `local-ai-exploration` done-or-discard gate decided to ship this facade.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## What this is
|
|
13
|
+
|
|
14
|
+
A thin facade that wraps `@huggingface/transformers` calls in `Result<T>` from `@fgv/ts-utils`,
|
|
15
|
+
mirroring the discipline established by
|
|
16
|
+
[`@fgv/ts-extras-webauthn`](../ts-extras-webauthn): one-line `captureAsyncResult` wrappers around
|
|
17
|
+
upstream primitives with **no opinionated orchestration** above the boundary. Consumers compose
|
|
18
|
+
the primitives into their own pipelines.
|
|
19
|
+
|
|
20
|
+
## Explicitly NOT in scope
|
|
21
|
+
|
|
22
|
+
These items were considered and explicitly deferred — use `@huggingface/transformers` directly
|
|
23
|
+
(with `captureAsyncResult` for your own Result wrapping) for any of them:
|
|
24
|
+
|
|
25
|
+
- Pipeline cache / lifecycle management
|
|
26
|
+
- Model registry or download management
|
|
27
|
+
- GPU/CPU device selection policy
|
|
28
|
+
- Quantization selection
|
|
29
|
+
- Embedding-store integration
|
|
30
|
+
- Classifier label allowlists
|
|
31
|
+
- Request batching
|
|
32
|
+
- Pipeline dispose semantics
|
|
33
|
+
|
|
34
|
+
## Four primitives. Nothing else.
|
|
35
|
+
|
|
36
|
+
| Function | Return | Wraps |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| `loadPipeline(task, model?, options?)` | `Promise<Result<AllTasks[T]>>` | `pipeline()` |
|
|
39
|
+
| `classify(pipeline, text, options?)` | `Promise<Result<TextClassificationOutput>>` | `TextClassificationPipeline` call |
|
|
40
|
+
| `classifyAll(pipeline, text, options?)` | `Promise<Result<TextClassificationOutput>>` | `classify` with `top_k: null` forced |
|
|
41
|
+
| `embed(extractor, text, options?)` | `Promise<Result<Tensor>>` | `FeatureExtractionPipeline` call |
|
|
42
|
+
|
|
43
|
+
**Deferred (not in scope):**
|
|
44
|
+
|
|
45
|
+
| Function | Rationale |
|
|
46
|
+
|---|---|
|
|
47
|
+
| `generate(pipeline, text, options?)` | No concrete consumer use case; text-generation output shape is more complex (single/batch/chat variants). Defer until a scenario exercises it. |
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { loadPipeline, classify } from '@fgv/ts-extras-transformers';
|
|
55
|
+
import { fail, succeed } from '@fgv/ts-utils';
|
|
56
|
+
|
|
57
|
+
// Load a sentiment-analysis model (wraps @huggingface/transformers pipeline()).
|
|
58
|
+
// The pipeline object is the upstream AllTasks['text-classification'] instance.
|
|
59
|
+
// Lifecycle management (caching, disposal, device selection) is your responsibility.
|
|
60
|
+
const classifierResult = await loadPipeline(
|
|
61
|
+
'text-classification',
|
|
62
|
+
'Xenova/distilbert-base-uncased-finetuned-sst-2-english'
|
|
63
|
+
);
|
|
64
|
+
if (classifierResult.isFailure()) {
|
|
65
|
+
// Model load failed (network error, ONNX init failure, model-not-found, etc.)
|
|
66
|
+
console.error(classifierResult.message);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const classifier = classifierResult.value;
|
|
71
|
+
|
|
72
|
+
// Classify a single text input.
|
|
73
|
+
const result = await classify(classifier, 'I love transformers!');
|
|
74
|
+
if (result.isSuccess()) {
|
|
75
|
+
// [{ label: 'POSITIVE', score: 0.9998 }]
|
|
76
|
+
console.log(result.value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Pass upstream options through verbatim:
|
|
80
|
+
const allLabels = await classify(classifier, 'I love transformers!', { top_k: null });
|
|
81
|
+
|
|
82
|
+
// Compose naturally in a Result chain:
|
|
83
|
+
const screenedResult = await loadPipeline('text-classification', modelId)
|
|
84
|
+
.then(r => r.onSuccess(async (pipe) => classify(pipe, userInput)))
|
|
85
|
+
.then(r => r.onSuccess(labels => {
|
|
86
|
+
const topLabel = labels[0];
|
|
87
|
+
if (topLabel.label === 'UNSAFE' && topLabel.score > 0.9) {
|
|
88
|
+
return fail('Content safety screen rejected the input');
|
|
89
|
+
}
|
|
90
|
+
return succeed(userInput);
|
|
91
|
+
}));
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Runtime requirements
|
|
97
|
+
|
|
98
|
+
- **Node.js:** v20 LTS or later (ONNX runtime native binding requirement)
|
|
99
|
+
- **`@huggingface/transformers`:** ^4.2.0 (peer dependency; bring your own)
|
|
100
|
+
- **`@fgv/ts-utils`:** workspace:* (peer dependency)
|
|
101
|
+
|
|
102
|
+
Model weights are downloaded from the HuggingFace Hub on first use and cached to disk
|
|
103
|
+
(`~/.cache/huggingface/` by default). Pass `cache_dir` in `options` to change the cache
|
|
104
|
+
location. The upstream library manages the download lifecycle; this facade does not.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Notes on `skipLibCheck`
|
|
109
|
+
|
|
110
|
+
This package sets `skipLibCheck: true` in its `tsconfig.json`. The `@huggingface/transformers`
|
|
111
|
+
v4.x type definitions contain internal type errors in transitive dependencies
|
|
112
|
+
(`@huggingface/tokenizers` path aliases, `Float16Array` missing from ES2018 lib,
|
|
113
|
+
`MgpstrProcessor` method signature mismatch). These are upstream issues — not errors in this
|
|
114
|
+
package's own code. `skipLibCheck` silences them without affecting type safety at this package's
|
|
115
|
+
own boundary.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
MIT — same as the parent fgv monorepo. `@huggingface/transformers` is Apache-2.0.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
|
|
3
|
+
"projectFolder": "..",
|
|
4
|
+
"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts",
|
|
5
|
+
"bundledPackages": [],
|
|
6
|
+
"compiler": {},
|
|
7
|
+
"apiReport": {
|
|
8
|
+
"enabled": true
|
|
9
|
+
},
|
|
10
|
+
"docModel": {
|
|
11
|
+
"enabled": true
|
|
12
|
+
},
|
|
13
|
+
"dtsRollup": {
|
|
14
|
+
"enabled": true
|
|
15
|
+
},
|
|
16
|
+
"tsdocMetadata": {},
|
|
17
|
+
"messages": {
|
|
18
|
+
"compilerMessageReporting": {
|
|
19
|
+
"default": {
|
|
20
|
+
"logLevel": "warning"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"extractorMessageReporting": {
|
|
24
|
+
"default": {
|
|
25
|
+
"logLevel": "warning"
|
|
26
|
+
},
|
|
27
|
+
"ae-unresolved-link": {
|
|
28
|
+
"logLevel": "none",
|
|
29
|
+
"addToApiReportFile": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"tsdocMessageReporting": {
|
|
33
|
+
"default": {
|
|
34
|
+
"logLevel": "warning"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@rushstack/heft-node-rig/profiles/default/config/jest.config.json",
|
|
3
|
+
"coverageThreshold": {
|
|
4
|
+
"global": {
|
|
5
|
+
"branches": 100,
|
|
6
|
+
"functions": 100,
|
|
7
|
+
"lines": 100,
|
|
8
|
+
"statements": 100
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"collectCoverage": true,
|
|
12
|
+
"coverageReporters": ["text", "lcov", "html"]
|
|
13
|
+
}
|
package/config/rig.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// The "rig.json" file directs tools to look for their config files in an external package.
|
|
2
|
+
// Documentation for this system: https://www.npmjs.com/package/@rushstack/rig-package
|
|
3
|
+
{
|
|
4
|
+
"$schema": "https://developer.microsoft.com/json-schemas/rig-package/rig.schema.json",
|
|
5
|
+
"rigPackageName": "@fgv/heft-dual-rig"
|
|
6
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@fgv/ts-extras-transformers` — Result-integration boundary over `@huggingface/transformers`
|
|
3
|
+
* (Node-side).
|
|
4
|
+
*
|
|
5
|
+
* A thin facade that wraps `@huggingface/transformers` calls in `Result<T>` from `@fgv/ts-utils`,
|
|
6
|
+
* mirroring the discipline established by `@fgv/ts-extras-webauthn`: one-line `captureAsyncResult`
|
|
7
|
+
* wrappers around upstream primitives with **no opinionated orchestration** above the boundary.
|
|
8
|
+
*
|
|
9
|
+
* **In scope:** `loadPipeline`, `classify`, `classifyAll`, `embed` — the surface needed by the
|
|
10
|
+
* B-3/B-4a local-classifier and embedding scenarios. `generate` is explicitly deferred until a
|
|
11
|
+
* concrete consumer use case surfaces (see `phase-b2-result.md`).
|
|
12
|
+
*
|
|
13
|
+
* **Explicitly NOT in scope:**
|
|
14
|
+
* - Pipeline cache / lifecycle management
|
|
15
|
+
* - Model registry or download management
|
|
16
|
+
* - GPU/CPU device selection policy
|
|
17
|
+
* - Quantization selection
|
|
18
|
+
* - Embedding-store integration
|
|
19
|
+
* - Classifier label allowlists
|
|
20
|
+
* - Request batching
|
|
21
|
+
* - Pipeline dispose semantics
|
|
22
|
+
*
|
|
23
|
+
* For any of the above, use `@huggingface/transformers` directly (with `captureAsyncResult` for
|
|
24
|
+
* your own Result wrapping).
|
|
25
|
+
*
|
|
26
|
+
* @packageDocumentation
|
|
27
|
+
*/
|
|
28
|
+
import { pipeline as _pipeline } from '@huggingface/transformers';
|
|
29
|
+
import { captureAsyncResult } from '@fgv/ts-utils';
|
|
30
|
+
/**
|
|
31
|
+
* Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
|
|
32
|
+
* Loads a model and returns a ready-to-use pipeline object.
|
|
33
|
+
*
|
|
34
|
+
* The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
|
|
35
|
+
* to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
|
|
36
|
+
* consumer's responsibility.
|
|
37
|
+
*
|
|
38
|
+
* Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
|
|
39
|
+
* initialization failures) are captured as `Failure` with the original message.
|
|
40
|
+
*
|
|
41
|
+
* @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
|
|
42
|
+
* @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
|
|
43
|
+
* upstream default for the task is used.
|
|
44
|
+
* @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
|
|
45
|
+
* through verbatim to the upstream `pipeline()` call.
|
|
46
|
+
*
|
|
47
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
export async function loadPipeline(task, model, options) {
|
|
51
|
+
return captureAsyncResult(() => _pipeline(task, model, options));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Normalises the upstream pipeline's output to a flat `TextClassificationOutput`.
|
|
55
|
+
*
|
|
56
|
+
* The upstream `TextClassificationPipeline` has an overloaded call signature: a single-string
|
|
57
|
+
* input returns `TextClassificationOutput` (flat array), while a string-array input returns
|
|
58
|
+
* `TextClassificationOutput[]` (array-of-arrays). Since `classify` always passes a single
|
|
59
|
+
* string, the flat-array path is the live path. The nested-array branch is defensive and
|
|
60
|
+
* ensures consumers always receive a flat array even if the upstream type union leaks through.
|
|
61
|
+
*/
|
|
62
|
+
function flattenIfNeeded(result) {
|
|
63
|
+
if (Array.isArray(result) && result.length > 0 && !Array.isArray(result[0])) {
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
// Defensive: if somehow an array-of-arrays came back, flatten one level.
|
|
67
|
+
return result.flat();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
|
|
71
|
+
* Returns the classification results as a flat `TextClassificationOutput` (array of
|
|
72
|
+
* `{ label: string; score: number }` entries).
|
|
73
|
+
*
|
|
74
|
+
* Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
|
|
75
|
+
* `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
|
|
76
|
+
* pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
|
|
77
|
+
* handle the `string | string[]` overload union.
|
|
78
|
+
*
|
|
79
|
+
* Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
|
|
80
|
+
* tokenisation errors) are captured as `Failure` with the original message.
|
|
81
|
+
*
|
|
82
|
+
* @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
|
|
83
|
+
* @param text - The text to classify.
|
|
84
|
+
* @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
|
|
85
|
+
* all labels). Passed through verbatim to the pipeline call.
|
|
86
|
+
*
|
|
87
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
export async function classify(classifier, text, options) {
|
|
91
|
+
return captureAsyncResult(async () => {
|
|
92
|
+
const result = await classifier(text, options);
|
|
93
|
+
return flattenIfNeeded(result);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
|
|
98
|
+
* is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
|
|
99
|
+
*
|
|
100
|
+
* Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
|
|
101
|
+
*
|
|
102
|
+
* Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
|
|
103
|
+
* with the original message.
|
|
104
|
+
*
|
|
105
|
+
* @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
|
|
106
|
+
* @param text - The text to classify.
|
|
107
|
+
* @param options - Optional upstream classification options. `top_k` is always set to `null`
|
|
108
|
+
* regardless of any value supplied here.
|
|
109
|
+
*
|
|
110
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
export async function classifyAll(classifier, text, options) {
|
|
114
|
+
return classify(classifier, text, Object.assign(Object.assign({}, options), { top_k: null }));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
|
|
118
|
+
* Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
|
|
119
|
+
* applied. Callers receive the raw output and are responsible for any downstream processing.
|
|
120
|
+
*
|
|
121
|
+
* Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
|
|
122
|
+
*
|
|
123
|
+
* Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
|
|
124
|
+
* are captured as `Failure` with the original message.
|
|
125
|
+
*
|
|
126
|
+
* @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
|
|
127
|
+
* @param text - The text (or texts) to embed.
|
|
128
|
+
* @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
|
|
129
|
+
* Passed through verbatim to the pipeline call.
|
|
130
|
+
*
|
|
131
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
export async function embed(extractor, text, options) {
|
|
135
|
+
return captureAsyncResult(() => extractor(text, options));
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;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BG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CACzB,SAAoC,EACpC,IAAuB,EACvB,OAA2D;IAE3D,OAAO,kBAAkB,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["/**\n * `@fgv/ts-extras-transformers` — Result-integration boundary over `@huggingface/transformers`\n * (Node-side).\n *\n * A thin facade that wraps `@huggingface/transformers` calls in `Result<T>` from `@fgv/ts-utils`,\n * mirroring the discipline established by `@fgv/ts-extras-webauthn`: one-line `captureAsyncResult`\n * wrappers around upstream primitives with **no opinionated orchestration** above the boundary.\n *\n * **In scope:** `loadPipeline`, `classify`, `classifyAll`, `embed` — the surface needed by the\n * B-3/B-4a local-classifier and embedding scenarios. `generate` is explicitly deferred until a\n * concrete consumer use case surfaces (see `phase-b2-result.md`).\n *\n * **Explicitly NOT in scope:**\n * - Pipeline cache / lifecycle management\n * - Model registry or download management\n * - GPU/CPU device selection policy\n * - Quantization selection\n * - Embedding-store integration\n * - Classifier label allowlists\n * - Request batching\n * - Pipeline dispose semantics\n *\n * For any of the above, use `@huggingface/transformers` directly (with `captureAsyncResult` for\n * your own Result wrapping).\n *\n * @packageDocumentation\n */\n\nimport {\n pipeline as _pipeline,\n type TextClassificationPipeline,\n type TextClassificationOutput,\n type FeatureExtractionPipeline,\n type Tensor,\n type AllTasks,\n type PipelineType\n} from '@huggingface/transformers';\nimport { captureAsyncResult, type Result } from '@fgv/ts-utils';\n\nexport type {\n TextClassificationPipeline,\n TextClassificationOutput,\n FeatureExtractionPipeline,\n Tensor,\n AllTasks,\n PipelineType\n};\n\nexport type { PretrainedModelOptions } from '@huggingface/transformers';\n\n/**\n * Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.\n * Loads a model and returns a ready-to-use pipeline object.\n *\n * The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access\n * to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the\n * consumer's responsibility.\n *\n * Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX\n * initialization failures) are captured as `Failure` with the original message.\n *\n * @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).\n * @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the\n * upstream default for the task is used.\n * @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed\n * through verbatim to the upstream `pipeline()` call.\n *\n * @see https://huggingface.co/docs/transformers.js\n * @public\n */\nexport async function loadPipeline<T extends PipelineType>(\n task: T,\n model?: string,\n options?: Parameters<typeof _pipeline>[2]\n): Promise<Result<AllTasks[T]>> {\n return captureAsyncResult(() => _pipeline(task, model, options));\n}\n\n/**\n * Normalises the upstream pipeline's output to a flat `TextClassificationOutput`.\n *\n * The upstream `TextClassificationPipeline` has an overloaded call signature: a single-string\n * input returns `TextClassificationOutput` (flat array), while a string-array input returns\n * `TextClassificationOutput[]` (array-of-arrays). Since `classify` always passes a single\n * string, the flat-array path is the live path. The nested-array branch is defensive and\n * ensures consumers always receive a flat array even if the upstream type union leaks through.\n */\nfunction flattenIfNeeded(\n result: TextClassificationOutput | TextClassificationOutput[]\n): TextClassificationOutput {\n if (Array.isArray(result) && result.length > 0 && !Array.isArray(result[0])) {\n return result as TextClassificationOutput;\n }\n // Defensive: if somehow an array-of-arrays came back, flatten one level.\n return (result as unknown as TextClassificationOutput[]).flat();\n}\n\n/**\n * Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.\n * Returns the classification results as a flat `TextClassificationOutput` (array of\n * `{ label: string; score: number }` entries).\n *\n * Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the\n * `'sentiment-analysis'` alias). This helper always passes a single string to the upstream\n * pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to\n * handle the `string | string[]` overload union.\n *\n * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,\n * tokenisation errors) are captured as `Failure` with the original message.\n *\n * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.\n * @param text - The text to classify.\n * @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return\n * all labels). Passed through verbatim to the pipeline call.\n *\n * @see https://huggingface.co/docs/transformers.js\n * @public\n */\nexport async function classify(\n classifier: TextClassificationPipeline,\n text: string,\n options?: Parameters<TextClassificationPipeline>[1]\n): Promise<Result<TextClassificationOutput>> {\n return captureAsyncResult(async () => {\n const result = await classifier(text, options);\n return flattenIfNeeded(result);\n });\n}\n\n/**\n * Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector\n * is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.\n *\n * Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.\n *\n * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`\n * with the original message.\n *\n * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.\n * @param text - The text to classify.\n * @param options - Optional upstream classification options. `top_k` is always set to `null`\n * regardless of any value supplied here.\n *\n * @see https://huggingface.co/docs/transformers.js\n * @public\n */\nexport async function classifyAll(\n classifier: TextClassificationPipeline,\n text: string,\n options?: Parameters<TextClassificationPipeline>[1]\n): Promise<Result<TextClassificationOutput>> {\n return classify(classifier, text, { ...options, top_k: null });\n}\n\n/**\n * Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.\n * Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is\n * applied. Callers receive the raw output and are responsible for any downstream processing.\n *\n * Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.\n *\n * Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)\n * are captured as `Failure` with the original message.\n *\n * @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.\n * @param text - The text (or texts) to embed.\n * @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).\n * Passed through verbatim to the pipeline call.\n *\n * @see https://huggingface.co/docs/transformers.js\n * @public\n */\nexport async function embed(\n extractor: FeatureExtractionPipeline,\n text: string | string[],\n options?: Parameters<FeatureExtractionPipeline['_call']>[1]\n): Promise<Result<Tensor>> {\n return captureAsyncResult(() => extractor(text, options));\n}\n"]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
jest.mock('@huggingface/transformers');
|
|
2
|
+
import '@fgv/ts-utils-jest';
|
|
3
|
+
import * as upstream from '@huggingface/transformers';
|
|
4
|
+
import { loadPipeline, classify, classifyAll, embed } from '../../index';
|
|
5
|
+
// ─── loadPipeline ──────────────────────────────────────────────────────────────
|
|
6
|
+
describe('loadPipeline', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
jest.resetAllMocks();
|
|
9
|
+
});
|
|
10
|
+
test('returns Success wrapping the upstream pipeline on success', async () => {
|
|
11
|
+
const mockPipeline = jest.fn();
|
|
12
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
13
|
+
const result = await loadPipeline('text-classification', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');
|
|
14
|
+
expect(result).toSucceedWith(mockPipeline);
|
|
15
|
+
});
|
|
16
|
+
test('passes task and model to the upstream pipeline factory', async () => {
|
|
17
|
+
const mockPipeline = jest.fn();
|
|
18
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
19
|
+
await loadPipeline('text-classification', 'some-model-id');
|
|
20
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);
|
|
21
|
+
});
|
|
22
|
+
test('passes options through to the upstream pipeline factory', async () => {
|
|
23
|
+
const mockPipeline = jest.fn();
|
|
24
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
25
|
+
const opts = { device: 'cpu' };
|
|
26
|
+
await loadPipeline('text-classification', 'some-model-id', opts);
|
|
27
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);
|
|
28
|
+
});
|
|
29
|
+
test('returns Success when model is omitted', async () => {
|
|
30
|
+
const mockPipeline = jest.fn();
|
|
31
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
32
|
+
const result = await loadPipeline('feature-extraction');
|
|
33
|
+
expect(result).toSucceed();
|
|
34
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);
|
|
35
|
+
});
|
|
36
|
+
test('returns Failure capturing upstream error message on network failure', async () => {
|
|
37
|
+
jest
|
|
38
|
+
.mocked(upstream.pipeline)
|
|
39
|
+
.mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));
|
|
40
|
+
const result = await loadPipeline('text-classification', 'model-not-found');
|
|
41
|
+
expect(result).toFailWith(/could not locate the model/i);
|
|
42
|
+
});
|
|
43
|
+
test('returns Failure capturing upstream error on ONNX init failure', async () => {
|
|
44
|
+
jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));
|
|
45
|
+
const result = await loadPipeline('text-classification', 'some-model');
|
|
46
|
+
expect(result).toFailWith(/failed to initialize onnx runtime/i);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
// ─── classify ─────────────────────────────────────────────────────────────────
|
|
50
|
+
describe('classify', () => {
|
|
51
|
+
let mockClassifier;
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
jest.resetAllMocks();
|
|
54
|
+
mockClassifier = jest.fn();
|
|
55
|
+
});
|
|
56
|
+
test('returns Success wrapping TextClassificationOutput on success', async () => {
|
|
57
|
+
const mockOutput = [{ label: 'POSITIVE', score: 0.9998 }];
|
|
58
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
59
|
+
const result = await classify(mockClassifier, 'I love transformers!');
|
|
60
|
+
expect(result).toSucceedWith(mockOutput);
|
|
61
|
+
});
|
|
62
|
+
test('passes text to the upstream classifier', async () => {
|
|
63
|
+
const mockOutput = [{ label: 'NEGATIVE', score: 0.9997 }];
|
|
64
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
65
|
+
await classify(mockClassifier, 'I hate bugs');
|
|
66
|
+
expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);
|
|
67
|
+
});
|
|
68
|
+
test('passes options through to the upstream classifier', async () => {
|
|
69
|
+
const mockOutput = [
|
|
70
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
71
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
72
|
+
];
|
|
73
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
74
|
+
const opts = { top_k: null };
|
|
75
|
+
await classify(mockClassifier, 'Hello world', opts);
|
|
76
|
+
expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);
|
|
77
|
+
});
|
|
78
|
+
test('normalises flat array result correctly', async () => {
|
|
79
|
+
const mockOutput = [
|
|
80
|
+
{ label: 'SAFE', score: 0.95 },
|
|
81
|
+
{ label: 'UNSAFE', score: 0.05 }
|
|
82
|
+
];
|
|
83
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
84
|
+
expect(await classify(mockClassifier, 'hello')).toSucceedWith(mockOutput);
|
|
85
|
+
});
|
|
86
|
+
test('normalises nested array result by flattening one level', async () => {
|
|
87
|
+
// When upstream returns array-of-arrays (e.g. batch input path leaked through),
|
|
88
|
+
// we flatten one level so consumers always receive a flat TextClassificationOutput.
|
|
89
|
+
const inner = [{ label: 'POSITIVE', score: 0.9 }];
|
|
90
|
+
mockClassifier.mockResolvedValueOnce([inner]);
|
|
91
|
+
expect(await classify(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {
|
|
92
|
+
expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
test('returns Failure capturing upstream error on inference failure', async () => {
|
|
96
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
97
|
+
const result = await classify(mockClassifier, 'some text');
|
|
98
|
+
expect(result).toFailWith(/inference session error/i);
|
|
99
|
+
});
|
|
100
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
101
|
+
mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));
|
|
102
|
+
const result = await classify(mockClassifier, 'bad input');
|
|
103
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
// ─── classifyAll ──────────────────────────────────────────────────────────────
|
|
107
|
+
describe('classifyAll', () => {
|
|
108
|
+
let mockClassifier;
|
|
109
|
+
beforeEach(() => {
|
|
110
|
+
jest.resetAllMocks();
|
|
111
|
+
mockClassifier = jest.fn();
|
|
112
|
+
});
|
|
113
|
+
test('forces top_k: null on the underlying classify call', async () => {
|
|
114
|
+
const mockOutput = [
|
|
115
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
116
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
117
|
+
];
|
|
118
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
119
|
+
await classifyAll(mockClassifier, 'hello');
|
|
120
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
121
|
+
});
|
|
122
|
+
test('overrides caller-supplied top_k with null', async () => {
|
|
123
|
+
const mockOutput = [
|
|
124
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
125
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
126
|
+
];
|
|
127
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
128
|
+
await classifyAll(mockClassifier, 'hello', { top_k: 1 });
|
|
129
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
130
|
+
});
|
|
131
|
+
test('returns all labels on success', async () => {
|
|
132
|
+
const mockOutput = [
|
|
133
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
134
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
135
|
+
];
|
|
136
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
137
|
+
const result = await classifyAll(mockClassifier, 'I love transformers!');
|
|
138
|
+
expect(result).toSucceedWith(mockOutput);
|
|
139
|
+
});
|
|
140
|
+
test('propagates upstream inference failure as Failure', async () => {
|
|
141
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
142
|
+
const result = await classifyAll(mockClassifier, 'some text');
|
|
143
|
+
expect(result).toFailWith(/inference session error/i);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
// ─── embed ────────────────────────────────────────────────────────────────────
|
|
147
|
+
describe('embed', () => {
|
|
148
|
+
let mockExtractor;
|
|
149
|
+
beforeEach(() => {
|
|
150
|
+
jest.resetAllMocks();
|
|
151
|
+
mockExtractor = jest.fn();
|
|
152
|
+
});
|
|
153
|
+
test('returns Success wrapping the upstream Tensor on success', async () => {
|
|
154
|
+
const mockTensor = {
|
|
155
|
+
type: 'float32',
|
|
156
|
+
data: new Float32Array([0.1, 0.2, 0.3]),
|
|
157
|
+
dims: [1, 3]
|
|
158
|
+
};
|
|
159
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
160
|
+
const result = await embed(mockExtractor, 'This is a test.');
|
|
161
|
+
expect(result).toSucceedWith(mockTensor);
|
|
162
|
+
});
|
|
163
|
+
test('passes text to the upstream extractor', async () => {
|
|
164
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] };
|
|
165
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
166
|
+
await embed(mockExtractor, 'hello world');
|
|
167
|
+
expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);
|
|
168
|
+
});
|
|
169
|
+
test('passes string array to the upstream extractor', async () => {
|
|
170
|
+
const mockTensor = {
|
|
171
|
+
type: 'float32',
|
|
172
|
+
data: new Float32Array([0.1, 0.2]),
|
|
173
|
+
dims: [2, 1]
|
|
174
|
+
};
|
|
175
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
176
|
+
await embed(mockExtractor, ['text one', 'text two']);
|
|
177
|
+
expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);
|
|
178
|
+
});
|
|
179
|
+
test('passes options through to the upstream extractor', async () => {
|
|
180
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] };
|
|
181
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
182
|
+
const opts = { pooling: 'mean', normalize: true };
|
|
183
|
+
await embed(mockExtractor, 'some text', opts);
|
|
184
|
+
expect(mockExtractor).toHaveBeenCalledWith('some text', opts);
|
|
185
|
+
});
|
|
186
|
+
test('returns Failure capturing upstream inference error', async () => {
|
|
187
|
+
mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
188
|
+
const result = await embed(mockExtractor, 'some text');
|
|
189
|
+
expect(result).toFailWith(/inference session error/i);
|
|
190
|
+
});
|
|
191
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
192
|
+
mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));
|
|
193
|
+
const result = await embed(mockExtractor, 'bad input');
|
|
194
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
//# sourceMappingURL=transformers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformers.test.js","sourceRoot":"","sources":["../../../src/test/unit/transformers.test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;AAEvC,OAAO,oBAAoB,CAAC;AAC5B,OAAO,KAAK,QAAQ,MAAM,2BAA2B,CAAC;AACtD,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,KAAK,EAMN,MAAM,aAAa,CAAC;AAErB,kFAAkF;AAElF,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,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AM,GAAG,MAAM,KAAK,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAC;IACpD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["jest.mock('@huggingface/transformers');\n\nimport '@fgv/ts-utils-jest';\nimport * as upstream from '@huggingface/transformers';\nimport {\n loadPipeline,\n classify,\n classifyAll,\n embed,\n type TextClassificationPipeline,\n type TextClassificationOutput,\n type FeatureExtractionPipeline,\n type Tensor,\n type AllTasks\n} from '../../index';\n\n// ─── loadPipeline ──────────────────────────────────────────────────────────────\n\ndescribe('loadPipeline', () => {\n beforeEach(() => {\n jest.resetAllMocks();\n });\n\n test('returns Success wrapping the upstream pipeline on success', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const result = await loadPipeline(\n 'text-classification',\n 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'\n );\n expect(result).toSucceedWith(mockPipeline);\n });\n\n test('passes task and model to the upstream pipeline factory', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n await loadPipeline('text-classification', 'some-model-id');\n expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);\n });\n\n test('passes options through to the upstream pipeline factory', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const opts = { device: 'cpu' } as Parameters<typeof upstream.pipeline>[2];\n await loadPipeline('text-classification', 'some-model-id', opts);\n expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);\n });\n\n test('returns Success when model is omitted', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['feature-extraction'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const result = await loadPipeline('feature-extraction');\n expect(result).toSucceed();\n expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);\n });\n\n test('returns Failure capturing upstream error message on network failure', async () => {\n jest\n .mocked(upstream.pipeline)\n .mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));\n\n const result = await loadPipeline('text-classification', 'model-not-found');\n expect(result).toFailWith(/could not locate the model/i);\n });\n\n test('returns Failure capturing upstream error on ONNX init failure', async () => {\n jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));\n\n const result = await loadPipeline('text-classification', 'some-model');\n expect(result).toFailWith(/failed to initialize onnx runtime/i);\n });\n});\n\n// ─── classify ─────────────────────────────────────────────────────────────────\n\ndescribe('classify', () => {\n let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;\n });\n\n test('returns Success wrapping TextClassificationOutput on success', async () => {\n const mockOutput: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9998 }];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const result = await classify(mockClassifier, 'I love transformers!');\n expect(result).toSucceedWith(mockOutput);\n });\n\n test('passes text to the upstream classifier', async () => {\n const mockOutput: TextClassificationOutput = [{ label: 'NEGATIVE', score: 0.9997 }];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classify(mockClassifier, 'I hate bugs');\n expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);\n });\n\n test('passes options through to the upstream classifier', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const opts = { top_k: null };\n await classify(mockClassifier, 'Hello world', opts);\n expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);\n });\n\n test('normalises flat array result correctly', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'SAFE', score: 0.95 },\n { label: 'UNSAFE', score: 0.05 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n expect(await classify(mockClassifier, 'hello')).toSucceedWith(mockOutput);\n });\n\n test('normalises nested array result by flattening one level', async () => {\n // When upstream returns array-of-arrays (e.g. batch input path leaked through),\n // we flatten one level so consumers always receive a flat TextClassificationOutput.\n const inner: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9 }];\n mockClassifier.mockResolvedValueOnce([inner] as unknown as TextClassificationOutput);\n\n expect(await classify(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {\n expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);\n });\n });\n\n test('returns Failure capturing upstream error on inference failure', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await classify(mockClassifier, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n\n test('returns Failure capturing upstream tokenisation error', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));\n\n const result = await classify(mockClassifier, 'bad input');\n expect(result).toFailWith(/tokenisation failed/i);\n });\n});\n\n// ─── classifyAll ──────────────────────────────────────────────────────────────\n\ndescribe('classifyAll', () => {\n let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;\n });\n\n test('forces top_k: null on the underlying classify call', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classifyAll(mockClassifier, 'hello');\n expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });\n });\n\n test('overrides caller-supplied top_k with null', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classifyAll(mockClassifier, 'hello', { top_k: 1 });\n expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });\n });\n\n test('returns all labels on success', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const result = await classifyAll(mockClassifier, 'I love transformers!');\n expect(result).toSucceedWith(mockOutput);\n });\n\n test('propagates upstream inference failure as Failure', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await classifyAll(mockClassifier, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n});\n\n// ─── embed ────────────────────────────────────────────────────────────────────\n\ndescribe('embed', () => {\n let mockExtractor: jest.MockedFunction<FeatureExtractionPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockExtractor = jest.fn() as unknown as jest.MockedFunction<FeatureExtractionPipeline>;\n });\n\n test('returns Success wrapping the upstream Tensor on success', async () => {\n const mockTensor = {\n type: 'float32',\n data: new Float32Array([0.1, 0.2, 0.3]),\n dims: [1, 3]\n } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n const result = await embed(mockExtractor, 'This is a test.');\n expect(result).toSucceedWith(mockTensor);\n });\n\n test('passes text to the upstream extractor', async () => {\n const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n await embed(mockExtractor, 'hello world');\n expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);\n });\n\n test('passes string array to the upstream extractor', async () => {\n const mockTensor = {\n type: 'float32',\n data: new Float32Array([0.1, 0.2]),\n dims: [2, 1]\n } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n await embed(mockExtractor, ['text one', 'text two']);\n expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);\n });\n\n test('passes options through to the upstream extractor', async () => {\n const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n const opts = { pooling: 'mean' as const, normalize: true };\n await embed(mockExtractor, 'some text', opts);\n expect(mockExtractor).toHaveBeenCalledWith('some text', opts);\n });\n\n test('returns Failure capturing upstream inference error', async () => {\n mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await embed(mockExtractor, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n\n test('returns Failure capturing upstream tokenisation error', async () => {\n mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));\n\n const result = await embed(mockExtractor, 'bad input');\n expect(result).toFailWith(/tokenisation failed/i);\n });\n});\n"]}
|