@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.
Files changed (67) hide show
  1. package/.rush/temp/cad001c0dd0af20627d8849ed0e5fa4904160f67.tar.log +30 -0
  2. package/.rush/temp/chunked-rush-logs/ts-extras-transformers.build.chunks.jsonl +9 -0
  3. package/.rush/temp/chunked-rush-logs/ts-extras-transformers.test.chunks.jsonl +28 -0
  4. package/.rush/temp/operation/build/all.log +9 -0
  5. package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
  6. package/.rush/temp/operation/build/state.json +3 -0
  7. package/.rush/temp/operation/test/all.log +28 -0
  8. package/.rush/temp/operation/test/log-chunks.jsonl +28 -0
  9. package/.rush/temp/operation/test/state.json +3 -0
  10. package/.rush/temp/shrinkwrap-deps.json +742 -0
  11. package/CHANGELOG.json +4 -0
  12. package/README.md +121 -0
  13. package/config/api-extractor.json +38 -0
  14. package/config/jest.config.json +13 -0
  15. package/config/rig.json +6 -0
  16. package/dist/index.js +137 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/test/unit/transformers.test.js +197 -0
  19. package/dist/test/unit/transformers.test.js.map +1 -0
  20. package/dist/ts-extras-transformers.d.ts +137 -0
  21. package/dist/tsdoc-metadata.json +11 -0
  22. package/eslint.config.js +15 -0
  23. package/etc/ts-extras-transformers.api.md +43 -0
  24. package/lib/index.d.ts +112 -0
  25. package/lib/index.d.ts.map +1 -0
  26. package/lib/index.js +143 -0
  27. package/lib/index.js.map +1 -0
  28. package/lib/test/unit/transformers.test.d.ts +2 -0
  29. package/lib/test/unit/transformers.test.d.ts.map +1 -0
  30. package/lib/test/unit/transformers.test.js +232 -0
  31. package/lib/test/unit/transformers.test.js.map +1 -0
  32. package/package.json +80 -0
  33. package/rush-logs/ts-extras-transformers.build.cache.log +3 -0
  34. package/rush-logs/ts-extras-transformers.build.log +9 -0
  35. package/rush-logs/ts-extras-transformers.test.cache.log +1 -0
  36. package/rush-logs/ts-extras-transformers.test.log +28 -0
  37. package/src/index.ts +179 -0
  38. package/src/test/unit/transformers.test.ts +267 -0
  39. package/temp/build/lint/_eslint-5eVG3S6w.json +14 -0
  40. package/temp/build/typescript/ts_8nwakTlr.json +1 -0
  41. package/temp/coverage/base.css +224 -0
  42. package/temp/coverage/block-navigation.js +87 -0
  43. package/temp/coverage/favicon.png +0 -0
  44. package/temp/coverage/index.html +116 -0
  45. package/temp/coverage/index.ts.html +622 -0
  46. package/temp/coverage/lcov-report/base.css +224 -0
  47. package/temp/coverage/lcov-report/block-navigation.js +87 -0
  48. package/temp/coverage/lcov-report/favicon.png +0 -0
  49. package/temp/coverage/lcov-report/index.html +116 -0
  50. package/temp/coverage/lcov-report/index.ts.html +622 -0
  51. package/temp/coverage/lcov-report/prettify.css +1 -0
  52. package/temp/coverage/lcov-report/prettify.js +2 -0
  53. package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  54. package/temp/coverage/lcov-report/sorter.js +210 -0
  55. package/temp/coverage/lcov.info +209 -0
  56. package/temp/coverage/prettify.css +1 -0
  57. package/temp/coverage/prettify.js +2 -0
  58. package/temp/coverage/sort-arrow-sprite.png +0 -0
  59. package/temp/coverage/sorter.js +210 -0
  60. package/temp/test/jest/haste-map-502913d9c10083bec2207119c45d7122-cd1aa4a0322f87b67f395127aa5e94ac-091f69b131db2c080ae4b15b7b2d4a16 +0 -0
  61. package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/8d/package_8dec8c7ec8d6c787063a4a014e769a65 +53 -0
  62. package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/d5/package_d56b0a8a49dbfccad646fc2e21950522 +203 -0
  63. package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/fc/versions_fc561f31b0dfb504443c5b8531747bfb +31 -0
  64. package/temp/test/jest/perf-cache-502913d9c10083bec2207119c45d7122-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
  65. package/temp/ts-extras-transformers.api.json +675 -0
  66. package/temp/ts-extras-transformers.api.md +43 -0
  67. package/tsconfig.json +8 -0
@@ -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
+
29
+ import { AllTasks } from '@huggingface/transformers';
30
+ import { FeatureExtractionPipeline } from '@huggingface/transformers';
31
+ import { pipeline } from '@huggingface/transformers';
32
+ import { PipelineType } from '@huggingface/transformers';
33
+ import { PretrainedModelOptions } from '@huggingface/transformers';
34
+ import { Result } from '@fgv/ts-utils';
35
+ import { Tensor } from '@huggingface/transformers';
36
+ import { TextClassificationOutput } from '@huggingface/transformers';
37
+ import { TextClassificationPipeline } from '@huggingface/transformers';
38
+
39
+ export { AllTasks }
40
+
41
+ /**
42
+ * Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
43
+ * Returns the classification results as a flat `TextClassificationOutput` (array of
44
+ * `{ label: string; score: number }` entries).
45
+ *
46
+ * Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
47
+ * `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
48
+ * pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
49
+ * handle the `string | string[]` overload union.
50
+ *
51
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
52
+ * tokenisation errors) are captured as `Failure` with the original message.
53
+ *
54
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
55
+ * @param text - The text to classify.
56
+ * @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
57
+ * all labels). Passed through verbatim to the pipeline call.
58
+ *
59
+ * @see https://huggingface.co/docs/transformers.js
60
+ * @public
61
+ */
62
+ export declare function classify(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
63
+
64
+ /**
65
+ * Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
66
+ * is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
67
+ *
68
+ * Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
69
+ *
70
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
71
+ * with the original message.
72
+ *
73
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
74
+ * @param text - The text to classify.
75
+ * @param options - Optional upstream classification options. `top_k` is always set to `null`
76
+ * regardless of any value supplied here.
77
+ *
78
+ * @see https://huggingface.co/docs/transformers.js
79
+ * @public
80
+ */
81
+ export declare function classifyAll(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
82
+
83
+ /**
84
+ * Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
85
+ * Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
86
+ * applied. Callers receive the raw output and are responsible for any downstream processing.
87
+ *
88
+ * Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
89
+ *
90
+ * Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
91
+ * are captured as `Failure` with the original message.
92
+ *
93
+ * @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
94
+ * @param text - The text (or texts) to embed.
95
+ * @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
96
+ * Passed through verbatim to the pipeline call.
97
+ *
98
+ * @see https://huggingface.co/docs/transformers.js
99
+ * @public
100
+ */
101
+ export declare function embed(extractor: FeatureExtractionPipeline, text: string | string[], options?: Parameters<FeatureExtractionPipeline['_call']>[1]): Promise<Result<Tensor>>;
102
+
103
+ export { FeatureExtractionPipeline }
104
+
105
+ /**
106
+ * Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
107
+ * Loads a model and returns a ready-to-use pipeline object.
108
+ *
109
+ * The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
110
+ * to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
111
+ * consumer's responsibility.
112
+ *
113
+ * Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
114
+ * initialization failures) are captured as `Failure` with the original message.
115
+ *
116
+ * @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
117
+ * @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
118
+ * upstream default for the task is used.
119
+ * @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
120
+ * through verbatim to the upstream `pipeline()` call.
121
+ *
122
+ * @see https://huggingface.co/docs/transformers.js
123
+ * @public
124
+ */
125
+ export declare function loadPipeline<T extends PipelineType>(task: T, model?: string, options?: Parameters<typeof pipeline>[2]): Promise<Result<AllTasks[T]>>;
126
+
127
+ export { PipelineType }
128
+
129
+ export { PretrainedModelOptions }
130
+
131
+ export { Tensor }
132
+
133
+ export { TextClassificationOutput }
134
+
135
+ export { TextClassificationPipeline }
136
+
137
+ export { }
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.58.7"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,15 @@
1
+ // ESLint 9 flat config
2
+ const nodeProfile = require('@rushstack/eslint-config/flat/profile/node');
3
+ const packletsPlugin = require('@rushstack/eslint-config/flat/mixins/packlets');
4
+ const tsdocPlugin = require('@rushstack/eslint-config/flat/mixins/tsdoc');
5
+
6
+ module.exports = [
7
+ ...nodeProfile,
8
+ packletsPlugin,
9
+ ...tsdocPlugin,
10
+ {
11
+ rules: {
12
+ '@rushstack/packlets/mechanics': 'warn'
13
+ }
14
+ }
15
+ ];
@@ -0,0 +1,43 @@
1
+ ## API Report File for "@fgv/ts-extras-transformers"
2
+
3
+ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4
+
5
+ ```ts
6
+
7
+ import { AllTasks } from '@huggingface/transformers';
8
+ import { FeatureExtractionPipeline } from '@huggingface/transformers';
9
+ import { pipeline } from '@huggingface/transformers';
10
+ import { PipelineType } from '@huggingface/transformers';
11
+ import { PretrainedModelOptions } from '@huggingface/transformers';
12
+ import { Result } from '@fgv/ts-utils';
13
+ import { Tensor } from '@huggingface/transformers';
14
+ import { TextClassificationOutput } from '@huggingface/transformers';
15
+ import { TextClassificationPipeline } from '@huggingface/transformers';
16
+
17
+ export { AllTasks }
18
+
19
+ // @public
20
+ export function classify(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
21
+
22
+ // @public
23
+ export function classifyAll(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
24
+
25
+ // @public
26
+ export function embed(extractor: FeatureExtractionPipeline, text: string | string[], options?: Parameters<FeatureExtractionPipeline['_call']>[1]): Promise<Result<Tensor>>;
27
+
28
+ export { FeatureExtractionPipeline }
29
+
30
+ // @public
31
+ export function loadPipeline<T extends PipelineType>(task: T, model?: string, options?: Parameters<typeof pipeline>[2]): Promise<Result<AllTasks[T]>>;
32
+
33
+ export { PipelineType }
34
+
35
+ export { PretrainedModelOptions }
36
+
37
+ export { Tensor }
38
+
39
+ export { TextClassificationOutput }
40
+
41
+ export { TextClassificationPipeline }
42
+
43
+ ```
package/lib/index.d.ts ADDED
@@ -0,0 +1,112 @@
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, type TextClassificationPipeline, type TextClassificationOutput, type FeatureExtractionPipeline, type Tensor, type AllTasks, type PipelineType } from '@huggingface/transformers';
29
+ import { type Result } from '@fgv/ts-utils';
30
+ export type { TextClassificationPipeline, TextClassificationOutput, FeatureExtractionPipeline, Tensor, AllTasks, PipelineType };
31
+ export type { PretrainedModelOptions } from '@huggingface/transformers';
32
+ /**
33
+ * Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
34
+ * Loads a model and returns a ready-to-use pipeline object.
35
+ *
36
+ * The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
37
+ * to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
38
+ * consumer's responsibility.
39
+ *
40
+ * Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
41
+ * initialization failures) are captured as `Failure` with the original message.
42
+ *
43
+ * @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
44
+ * @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
45
+ * upstream default for the task is used.
46
+ * @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
47
+ * through verbatim to the upstream `pipeline()` call.
48
+ *
49
+ * @see https://huggingface.co/docs/transformers.js
50
+ * @public
51
+ */
52
+ export declare function loadPipeline<T extends PipelineType>(task: T, model?: string, options?: Parameters<typeof _pipeline>[2]): Promise<Result<AllTasks[T]>>;
53
+ /**
54
+ * Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
55
+ * Returns the classification results as a flat `TextClassificationOutput` (array of
56
+ * `{ label: string; score: number }` entries).
57
+ *
58
+ * Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
59
+ * `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
60
+ * pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
61
+ * handle the `string | string[]` overload union.
62
+ *
63
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
64
+ * tokenisation errors) are captured as `Failure` with the original message.
65
+ *
66
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
67
+ * @param text - The text to classify.
68
+ * @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
69
+ * all labels). Passed through verbatim to the pipeline call.
70
+ *
71
+ * @see https://huggingface.co/docs/transformers.js
72
+ * @public
73
+ */
74
+ export declare function classify(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
75
+ /**
76
+ * Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
77
+ * is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
78
+ *
79
+ * Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
80
+ *
81
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
82
+ * with the original message.
83
+ *
84
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
85
+ * @param text - The text to classify.
86
+ * @param options - Optional upstream classification options. `top_k` is always set to `null`
87
+ * regardless of any value supplied here.
88
+ *
89
+ * @see https://huggingface.co/docs/transformers.js
90
+ * @public
91
+ */
92
+ export declare function classifyAll(classifier: TextClassificationPipeline, text: string, options?: Parameters<TextClassificationPipeline>[1]): Promise<Result<TextClassificationOutput>>;
93
+ /**
94
+ * Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
95
+ * Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
96
+ * applied. Callers receive the raw output and are responsible for any downstream processing.
97
+ *
98
+ * Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
99
+ *
100
+ * Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
101
+ * are captured as `Failure` with the original message.
102
+ *
103
+ * @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
104
+ * @param text - The text (or texts) to embed.
105
+ * @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
106
+ * Passed through verbatim to the pipeline call.
107
+ *
108
+ * @see https://huggingface.co/docs/transformers.js
109
+ * @public
110
+ */
111
+ export declare function embed(extractor: FeatureExtractionPipeline, text: string | string[], options?: Parameters<FeatureExtractionPipeline['_call']>[1]): Promise<Result<Tensor>>;
112
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EACL,QAAQ,IAAI,SAAS,EACrB,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC9B,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,YAAY,EAClB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAsB,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAEhE,YAAY,EACV,0BAA0B,EAC1B,wBAAwB,EACxB,yBAAyB,EACzB,MAAM,EACN,QAAQ,EACR,YAAY,EACb,CAAC;AAEF,YAAY,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,YAAY,CAAC,CAAC,SAAS,YAAY,EACvD,IAAI,EAAE,CAAC,EACP,KAAK,CAAC,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,GACxC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAE9B;AAqBD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,QAAQ,CAC5B,UAAU,EAAE,0BAA0B,EACtC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,GAClD,OAAO,CAAC,MAAM,CAAC,wBAAwB,CAAC,CAAC,CAK3C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,WAAW,CAC/B,UAAU,EAAE,0BAA0B,EACtC,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,GAClD,OAAO,CAAC,MAAM,CAAC,wBAAwB,CAAC,CAAC,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,KAAK,CACzB,SAAS,EAAE,yBAAyB,EACpC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,EACvB,OAAO,CAAC,EAAE,UAAU,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,GAC1D,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAEzB"}
package/lib/index.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ /**
3
+ * `@fgv/ts-extras-transformers` — Result-integration boundary over `@huggingface/transformers`
4
+ * (Node-side).
5
+ *
6
+ * A thin facade that wraps `@huggingface/transformers` calls in `Result<T>` from `@fgv/ts-utils`,
7
+ * mirroring the discipline established by `@fgv/ts-extras-webauthn`: one-line `captureAsyncResult`
8
+ * wrappers around upstream primitives with **no opinionated orchestration** above the boundary.
9
+ *
10
+ * **In scope:** `loadPipeline`, `classify`, `classifyAll`, `embed` — the surface needed by the
11
+ * B-3/B-4a local-classifier and embedding scenarios. `generate` is explicitly deferred until a
12
+ * concrete consumer use case surfaces (see `phase-b2-result.md`).
13
+ *
14
+ * **Explicitly NOT in scope:**
15
+ * - Pipeline cache / lifecycle management
16
+ * - Model registry or download management
17
+ * - GPU/CPU device selection policy
18
+ * - Quantization selection
19
+ * - Embedding-store integration
20
+ * - Classifier label allowlists
21
+ * - Request batching
22
+ * - Pipeline dispose semantics
23
+ *
24
+ * For any of the above, use `@huggingface/transformers` directly (with `captureAsyncResult` for
25
+ * your own Result wrapping).
26
+ *
27
+ * @packageDocumentation
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.loadPipeline = loadPipeline;
31
+ exports.classify = classify;
32
+ exports.classifyAll = classifyAll;
33
+ exports.embed = embed;
34
+ const transformers_1 = require("@huggingface/transformers");
35
+ const ts_utils_1 = require("@fgv/ts-utils");
36
+ /**
37
+ * Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
38
+ * Loads a model and returns a ready-to-use pipeline object.
39
+ *
40
+ * The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
41
+ * to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
42
+ * consumer's responsibility.
43
+ *
44
+ * Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
45
+ * initialization failures) are captured as `Failure` with the original message.
46
+ *
47
+ * @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
48
+ * @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
49
+ * upstream default for the task is used.
50
+ * @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
51
+ * through verbatim to the upstream `pipeline()` call.
52
+ *
53
+ * @see https://huggingface.co/docs/transformers.js
54
+ * @public
55
+ */
56
+ async function loadPipeline(task, model, options) {
57
+ return (0, ts_utils_1.captureAsyncResult)(() => (0, transformers_1.pipeline)(task, model, options));
58
+ }
59
+ /**
60
+ * Normalises the upstream pipeline's output to a flat `TextClassificationOutput`.
61
+ *
62
+ * The upstream `TextClassificationPipeline` has an overloaded call signature: a single-string
63
+ * input returns `TextClassificationOutput` (flat array), while a string-array input returns
64
+ * `TextClassificationOutput[]` (array-of-arrays). Since `classify` always passes a single
65
+ * string, the flat-array path is the live path. The nested-array branch is defensive and
66
+ * ensures consumers always receive a flat array even if the upstream type union leaks through.
67
+ */
68
+ function flattenIfNeeded(result) {
69
+ if (Array.isArray(result) && result.length > 0 && !Array.isArray(result[0])) {
70
+ return result;
71
+ }
72
+ // Defensive: if somehow an array-of-arrays came back, flatten one level.
73
+ return result.flat();
74
+ }
75
+ /**
76
+ * Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
77
+ * Returns the classification results as a flat `TextClassificationOutput` (array of
78
+ * `{ label: string; score: number }` entries).
79
+ *
80
+ * Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
81
+ * `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
82
+ * pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
83
+ * handle the `string | string[]` overload union.
84
+ *
85
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
86
+ * tokenisation errors) are captured as `Failure` with the original message.
87
+ *
88
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
89
+ * @param text - The text to classify.
90
+ * @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
91
+ * all labels). Passed through verbatim to the pipeline call.
92
+ *
93
+ * @see https://huggingface.co/docs/transformers.js
94
+ * @public
95
+ */
96
+ async function classify(classifier, text, options) {
97
+ return (0, ts_utils_1.captureAsyncResult)(async () => {
98
+ const result = await classifier(text, options);
99
+ return flattenIfNeeded(result);
100
+ });
101
+ }
102
+ /**
103
+ * Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
104
+ * is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
105
+ *
106
+ * Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
107
+ *
108
+ * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
109
+ * with the original message.
110
+ *
111
+ * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
112
+ * @param text - The text to classify.
113
+ * @param options - Optional upstream classification options. `top_k` is always set to `null`
114
+ * regardless of any value supplied here.
115
+ *
116
+ * @see https://huggingface.co/docs/transformers.js
117
+ * @public
118
+ */
119
+ async function classifyAll(classifier, text, options) {
120
+ return classify(classifier, text, Object.assign(Object.assign({}, options), { top_k: null }));
121
+ }
122
+ /**
123
+ * Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
124
+ * Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
125
+ * applied. Callers receive the raw output and are responsible for any downstream processing.
126
+ *
127
+ * Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
128
+ *
129
+ * Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
130
+ * are captured as `Failure` with the original message.
131
+ *
132
+ * @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
133
+ * @param text - The text (or texts) to embed.
134
+ * @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
135
+ * Passed through verbatim to the pipeline call.
136
+ *
137
+ * @see https://huggingface.co/docs/transformers.js
138
+ * @public
139
+ */
140
+ async function embed(extractor, text, options) {
141
+ return (0, ts_utils_1.captureAsyncResult)(() => extractor(text, options));
142
+ }
143
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;;AA4CH,oCAMC;AA0CD,4BASC;AAmBD,kCAMC;AAoBD,sBAMC;AAtJD,4DAQmC;AACnC,4CAAgE;AAahE;;;;;;;;;;;;;;;;;;;GAmBG;AACI,KAAK,UAAU,YAAY,CAChC,IAAO,EACP,KAAc,EACd,OAAyC;IAEzC,OAAO,IAAA,6BAAkB,EAAC,GAAG,EAAE,CAAC,IAAA,uBAAS,EAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;AACnE,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,eAAe,CACtB,MAA6D;IAE7D,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5E,OAAO,MAAkC,CAAC;IAC5C,CAAC;IACD,yEAAyE;IACzE,OAAQ,MAAgD,CAAC,IAAI,EAAE,CAAC;AAClE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACI,KAAK,UAAU,QAAQ,CAC5B,UAAsC,EACtC,IAAY,EACZ,OAAmD;IAEnD,OAAO,IAAA,6BAAkB,EAAC,KAAK,IAAI,EAAE;QACnC,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/C,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACI,KAAK,UAAU,WAAW,CAC/B,UAAsC,EACtC,IAAY,EACZ,OAAmD;IAEnD,OAAO,QAAQ,CAAC,UAAU,EAAE,IAAI,kCAAO,OAAO,KAAE,KAAK,EAAE,IAAI,IAAG,CAAC;AACjE,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,KAAK,CACzB,SAAoC,EACpC,IAAuB,EACvB,OAA2D;IAE3D,OAAO,IAAA,6BAAkB,EAAC,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,2 @@
1
+ import '@fgv/ts-utils-jest';
2
+ //# sourceMappingURL=transformers.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformers.test.d.ts","sourceRoot":"","sources":["../../../src/test/unit/transformers.test.ts"],"names":[],"mappings":"AAEA,OAAO,oBAAoB,CAAC"}