@fgv/ts-extras-transformers 5.1.0-47 → 5.1.0-49

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.
@@ -1,269 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- jest.mock('@huggingface/transformers');
37
- require("@fgv/ts-utils-jest");
38
- const upstream = __importStar(require("@huggingface/transformers"));
39
- const index_1 = require("../../index");
40
- // ─── loadPipeline ──────────────────────────────────────────────────────────────
41
- describe('loadPipeline', () => {
42
- beforeEach(() => {
43
- jest.resetAllMocks();
44
- });
45
- test('returns Success wrapping the upstream pipeline on success', async () => {
46
- const mockPipeline = jest.fn();
47
- jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
48
- const result = await (0, index_1.loadPipeline)('text-classification', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');
49
- expect(result).toSucceedWith(mockPipeline);
50
- });
51
- test('passes task and model to the upstream pipeline factory', async () => {
52
- const mockPipeline = jest.fn();
53
- jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
54
- await (0, index_1.loadPipeline)('text-classification', 'some-model-id');
55
- expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);
56
- });
57
- test('passes options through to the upstream pipeline factory', async () => {
58
- const mockPipeline = jest.fn();
59
- jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
60
- const opts = { device: 'cpu' };
61
- await (0, index_1.loadPipeline)('text-classification', 'some-model-id', opts);
62
- expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);
63
- });
64
- test('returns Success when model is omitted', async () => {
65
- const mockPipeline = jest.fn();
66
- jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
67
- const result = await (0, index_1.loadPipeline)('feature-extraction');
68
- expect(result).toSucceed();
69
- expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);
70
- });
71
- test('returns Failure capturing upstream error message on network failure', async () => {
72
- jest
73
- .mocked(upstream.pipeline)
74
- .mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));
75
- const result = await (0, index_1.loadPipeline)('text-classification', 'model-not-found');
76
- expect(result).toFailWith(/could not locate the model/i);
77
- });
78
- test('returns Failure capturing upstream error on ONNX init failure', async () => {
79
- jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));
80
- const result = await (0, index_1.loadPipeline)('text-classification', 'some-model');
81
- expect(result).toFailWith(/failed to initialize onnx runtime/i);
82
- });
83
- });
84
- // ─── classify ─────────────────────────────────────────────────────────────────
85
- describe('classify', () => {
86
- let mockClassifier;
87
- beforeEach(() => {
88
- jest.resetAllMocks();
89
- mockClassifier = jest.fn();
90
- });
91
- test('returns Success wrapping TextClassificationOutput on success', async () => {
92
- const mockOutput = [{ label: 'POSITIVE', score: 0.9998 }];
93
- mockClassifier.mockResolvedValueOnce(mockOutput);
94
- const result = await (0, index_1.classify)(mockClassifier, 'I love transformers!');
95
- expect(result).toSucceedWith(mockOutput);
96
- });
97
- test('passes text to the upstream classifier', async () => {
98
- const mockOutput = [{ label: 'NEGATIVE', score: 0.9997 }];
99
- mockClassifier.mockResolvedValueOnce(mockOutput);
100
- await (0, index_1.classify)(mockClassifier, 'I hate bugs');
101
- expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);
102
- });
103
- test('passes options through to the upstream classifier', async () => {
104
- const mockOutput = [
105
- { label: 'POSITIVE', score: 0.9998 },
106
- { label: 'NEGATIVE', score: 0.0002 }
107
- ];
108
- mockClassifier.mockResolvedValueOnce(mockOutput);
109
- const opts = { top_k: null };
110
- await (0, index_1.classify)(mockClassifier, 'Hello world', opts);
111
- expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);
112
- });
113
- test('normalises flat array result correctly', async () => {
114
- const mockOutput = [
115
- { label: 'SAFE', score: 0.95 },
116
- { label: 'UNSAFE', score: 0.05 }
117
- ];
118
- mockClassifier.mockResolvedValueOnce(mockOutput);
119
- expect(await (0, index_1.classify)(mockClassifier, 'hello')).toSucceedWith(mockOutput);
120
- });
121
- test('normalises nested array result by flattening one level', async () => {
122
- // When upstream returns array-of-arrays (e.g. batch input path leaked through),
123
- // we flatten one level so consumers always receive a flat TextClassificationOutput.
124
- const inner = [{ label: 'POSITIVE', score: 0.9 }];
125
- mockClassifier.mockResolvedValueOnce([inner]);
126
- expect(await (0, index_1.classify)(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {
127
- expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);
128
- });
129
- });
130
- test('returns Failure capturing upstream error on inference failure', async () => {
131
- mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
132
- const result = await (0, index_1.classify)(mockClassifier, 'some text');
133
- expect(result).toFailWith(/inference session error/i);
134
- });
135
- test('returns Failure capturing upstream tokenisation error', async () => {
136
- mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));
137
- const result = await (0, index_1.classify)(mockClassifier, 'bad input');
138
- expect(result).toFailWith(/tokenisation failed/i);
139
- });
140
- });
141
- // ─── classifyAll ──────────────────────────────────────────────────────────────
142
- describe('classifyAll', () => {
143
- let mockClassifier;
144
- beforeEach(() => {
145
- jest.resetAllMocks();
146
- mockClassifier = jest.fn();
147
- });
148
- test('forces top_k: null on the underlying classify call', async () => {
149
- const mockOutput = [
150
- { label: 'POSITIVE', score: 0.9998 },
151
- { label: 'NEGATIVE', score: 0.0002 }
152
- ];
153
- mockClassifier.mockResolvedValueOnce(mockOutput);
154
- await (0, index_1.classifyAll)(mockClassifier, 'hello');
155
- expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
156
- });
157
- test('overrides caller-supplied top_k with null', async () => {
158
- const mockOutput = [
159
- { label: 'POSITIVE', score: 0.9998 },
160
- { label: 'NEGATIVE', score: 0.0002 }
161
- ];
162
- mockClassifier.mockResolvedValueOnce(mockOutput);
163
- await (0, index_1.classifyAll)(mockClassifier, 'hello', { top_k: 1 });
164
- expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
165
- });
166
- test('returns all labels on success', async () => {
167
- const mockOutput = [
168
- { label: 'POSITIVE', score: 0.9998 },
169
- { label: 'NEGATIVE', score: 0.0002 }
170
- ];
171
- mockClassifier.mockResolvedValueOnce(mockOutput);
172
- const result = await (0, index_1.classifyAll)(mockClassifier, 'I love transformers!');
173
- expect(result).toSucceedWith(mockOutput);
174
- });
175
- test('propagates upstream inference failure as Failure', async () => {
176
- mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
177
- const result = await (0, index_1.classifyAll)(mockClassifier, 'some text');
178
- expect(result).toFailWith(/inference session error/i);
179
- });
180
- });
181
- // ─── embed ────────────────────────────────────────────────────────────────────
182
- describe('embed', () => {
183
- let mockExtractor;
184
- beforeEach(() => {
185
- jest.resetAllMocks();
186
- mockExtractor = jest.fn();
187
- });
188
- test('returns Success wrapping the upstream Tensor on success', async () => {
189
- const mockTensor = {
190
- type: 'float32',
191
- data: new Float32Array([0.1, 0.2, 0.3]),
192
- dims: [1, 3]
193
- };
194
- mockExtractor.mockResolvedValueOnce(mockTensor);
195
- const result = await (0, index_1.embed)(mockExtractor, 'This is a test.');
196
- expect(result).toSucceedWith(mockTensor);
197
- });
198
- test('passes text to the upstream extractor', async () => {
199
- const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] };
200
- mockExtractor.mockResolvedValueOnce(mockTensor);
201
- await (0, index_1.embed)(mockExtractor, 'hello world');
202
- expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);
203
- });
204
- test('passes string array to the upstream extractor', async () => {
205
- const mockTensor = {
206
- type: 'float32',
207
- data: new Float32Array([0.1, 0.2]),
208
- dims: [2, 1]
209
- };
210
- mockExtractor.mockResolvedValueOnce(mockTensor);
211
- await (0, index_1.embed)(mockExtractor, ['text one', 'text two']);
212
- expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);
213
- });
214
- test('passes options through to the upstream extractor', async () => {
215
- const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] };
216
- mockExtractor.mockResolvedValueOnce(mockTensor);
217
- const opts = { pooling: 'mean', normalize: true };
218
- await (0, index_1.embed)(mockExtractor, 'some text', opts);
219
- expect(mockExtractor).toHaveBeenCalledWith('some text', opts);
220
- });
221
- test('returns Failure capturing upstream inference error', async () => {
222
- mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
223
- const result = await (0, index_1.embed)(mockExtractor, 'some text');
224
- expect(result).toFailWith(/inference session error/i);
225
- });
226
- test('returns Failure capturing upstream tokenisation error', async () => {
227
- mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));
228
- const result = await (0, index_1.embed)(mockExtractor, 'bad input');
229
- expect(result).toFailWith(/tokenisation failed/i);
230
- });
231
- });
232
- // ─── summarize ──────────────────────────────────────────────────────────────────
233
- describe('summarize', () => {
234
- let mockSummarizer;
235
- beforeEach(() => {
236
- jest.resetAllMocks();
237
- mockSummarizer = jest.fn();
238
- });
239
- test('returns Success wrapping SummarizationOutput on success', async () => {
240
- const mockOutput = [{ summary_text: 'A short summary.' }];
241
- mockSummarizer.mockResolvedValueOnce(mockOutput);
242
- const result = await (0, index_1.summarize)(mockSummarizer, 'A long article that needs summarizing.');
243
- expect(result).toSucceedWith(mockOutput);
244
- });
245
- test('passes text to the upstream summarizer', async () => {
246
- const mockOutput = [{ summary_text: 'Summary.' }];
247
- mockSummarizer.mockResolvedValueOnce(mockOutput);
248
- await (0, index_1.summarize)(mockSummarizer, 'some long document');
249
- expect(mockSummarizer).toHaveBeenCalledWith('some long document', undefined);
250
- });
251
- test('passes options through to the upstream summarizer', async () => {
252
- const mockOutput = [{ summary_text: 'Bounded summary.' }];
253
- mockSummarizer.mockResolvedValueOnce(mockOutput);
254
- const opts = { min_length: 10, max_length: 50 };
255
- await (0, index_1.summarize)(mockSummarizer, 'a document', opts);
256
- expect(mockSummarizer).toHaveBeenCalledWith('a document', opts);
257
- });
258
- test('returns Failure capturing upstream inference error', async () => {
259
- mockSummarizer.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
260
- const result = await (0, index_1.summarize)(mockSummarizer, 'some text');
261
- expect(result).toFailWith(/inference session error/i);
262
- });
263
- test('returns Failure capturing upstream tokenisation error', async () => {
264
- mockSummarizer.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));
265
- const result = await (0, index_1.summarize)(mockSummarizer, 'bad input');
266
- expect(result).toFailWith(/tokenisation failed/i);
267
- });
268
- });
269
- //# sourceMappingURL=transformers.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transformers.test.js","sourceRoot":"","sources":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GAAG,MAAM,IAAA,iBAAS,EAAC,cAAc,EAAE,WAAW,CAAC,CAAC;QAC5D,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 summarize,\n type TextClassificationPipeline,\n type TextClassificationOutput,\n type FeatureExtractionPipeline,\n type SummarizationPipeline,\n type SummarizationOutput,\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\n// ─── summarize ──────────────────────────────────────────────────────────────────\n\ndescribe('summarize', () => {\n let mockSummarizer: jest.MockedFunction<SummarizationPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockSummarizer = jest.fn() as unknown as jest.MockedFunction<SummarizationPipeline>;\n });\n\n test('returns Success wrapping SummarizationOutput on success', async () => {\n const mockOutput: SummarizationOutput = [{ summary_text: 'A short summary.' }];\n mockSummarizer.mockResolvedValueOnce(mockOutput);\n\n const result = await summarize(mockSummarizer, 'A long article that needs summarizing.');\n expect(result).toSucceedWith(mockOutput);\n });\n\n test('passes text to the upstream summarizer', async () => {\n const mockOutput: SummarizationOutput = [{ summary_text: 'Summary.' }];\n mockSummarizer.mockResolvedValueOnce(mockOutput);\n\n await summarize(mockSummarizer, 'some long document');\n expect(mockSummarizer).toHaveBeenCalledWith('some long document', undefined);\n });\n\n test('passes options through to the upstream summarizer', async () => {\n const mockOutput: SummarizationOutput = [{ summary_text: 'Bounded summary.' }];\n mockSummarizer.mockResolvedValueOnce(mockOutput);\n\n const opts = { min_length: 10, max_length: 50 };\n await summarize(mockSummarizer, 'a document', opts);\n expect(mockSummarizer).toHaveBeenCalledWith('a document', opts);\n });\n\n test('returns Failure capturing upstream inference error', async () => {\n mockSummarizer.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await summarize(mockSummarizer, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n\n test('returns Failure capturing upstream tokenisation error', async () => {\n mockSummarizer.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));\n\n const result = await summarize(mockSummarizer, 'bad input');\n expect(result).toFailWith(/tokenisation failed/i);\n });\n});\n"]}
@@ -1,3 +0,0 @@
1
- Caching build output folders: dist, lib, temp, .rush/temp/operation/build
2
- Successfully set cache entry.
3
- Cache key: 07e0671e768f0e37ed8eb3497bda5f1e14278f10
@@ -1,9 +0,0 @@
1
- Invoking: heft build --clean
2
- ---- build started ----
3
- [build:typescript] The TypeScript compiler version 5.9.3 is newer than the latest version that was tested with Heft (5.8); it may not work correctly.
4
- [build:typescript] Using TypeScript version 5.9.3
5
- [build:lint] Using ESLint version 9.39.5
6
- [build:api-extractor] Using API Extractor version 7.58.9
7
- [build:api-extractor] Analysis will use the bundled TypeScript version 5.9.3
8
- ---- build finished (7.772s) ----
9
- -------------------- Finished (7.779s) --------------------
package/src/index.ts DELETED
@@ -1,212 +0,0 @@
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`, `summarize` — the task types
10
- * exercised by real consumers so far. The general `generate` primitive is explicitly deferred
11
- * until a concrete consumer use case surfaces (summarization is its own task-specific primitive).
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 {
30
- pipeline as _pipeline,
31
- type TextClassificationPipeline,
32
- type TextClassificationOutput,
33
- type FeatureExtractionPipeline,
34
- type SummarizationPipeline,
35
- type SummarizationOutput,
36
- type Tensor,
37
- type AllTasks,
38
- type PipelineType
39
- } from '@huggingface/transformers';
40
- import { captureAsyncResult, type Result } from '@fgv/ts-utils';
41
-
42
- export type {
43
- TextClassificationPipeline,
44
- TextClassificationOutput,
45
- FeatureExtractionPipeline,
46
- SummarizationPipeline,
47
- SummarizationOutput,
48
- Tensor,
49
- AllTasks,
50
- PipelineType
51
- };
52
-
53
- export type { PretrainedModelOptions } from '@huggingface/transformers';
54
-
55
- /**
56
- * Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
57
- * Loads a model and returns a ready-to-use pipeline object.
58
- *
59
- * The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
60
- * to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
61
- * consumer's responsibility.
62
- *
63
- * Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
64
- * initialization failures) are captured as `Failure` with the original message.
65
- *
66
- * @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
67
- * @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
68
- * upstream default for the task is used.
69
- * @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
70
- * through verbatim to the upstream `pipeline()` call.
71
- *
72
- * @see https://huggingface.co/docs/transformers.js
73
- * @public
74
- */
75
- export async function loadPipeline<T extends PipelineType>(
76
- task: T,
77
- model?: string,
78
- options?: Parameters<typeof _pipeline>[2]
79
- ): Promise<Result<AllTasks[T]>> {
80
- return captureAsyncResult(() => _pipeline(task, model, options));
81
- }
82
-
83
- /**
84
- * Normalises the upstream pipeline's output to a flat `TextClassificationOutput`.
85
- *
86
- * The upstream `TextClassificationPipeline` has an overloaded call signature: a single-string
87
- * input returns `TextClassificationOutput` (flat array), while a string-array input returns
88
- * `TextClassificationOutput[]` (array-of-arrays). Since `classify` always passes a single
89
- * string, the flat-array path is the live path. The nested-array branch is defensive and
90
- * ensures consumers always receive a flat array even if the upstream type union leaks through.
91
- */
92
- function flattenIfNeeded(
93
- result: TextClassificationOutput | TextClassificationOutput[]
94
- ): TextClassificationOutput {
95
- if (Array.isArray(result) && result.length > 0 && !Array.isArray(result[0])) {
96
- return result as TextClassificationOutput;
97
- }
98
- // Defensive: if somehow an array-of-arrays came back, flatten one level.
99
- return (result as unknown as TextClassificationOutput[]).flat();
100
- }
101
-
102
- /**
103
- * Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
104
- * Returns the classification results as a flat `TextClassificationOutput` (array of
105
- * `{ label: string; score: number }` entries).
106
- *
107
- * Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
108
- * `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
109
- * pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
110
- * handle the `string | string[]` overload union.
111
- *
112
- * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
113
- * tokenisation errors) are captured as `Failure` with the original message.
114
- *
115
- * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
116
- * @param text - The text to classify.
117
- * @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
118
- * all labels). Passed through verbatim to the pipeline call.
119
- *
120
- * @see https://huggingface.co/docs/transformers.js
121
- * @public
122
- */
123
- export async function classify(
124
- classifier: TextClassificationPipeline,
125
- text: string,
126
- options?: Parameters<TextClassificationPipeline>[1]
127
- ): Promise<Result<TextClassificationOutput>> {
128
- return captureAsyncResult(async () => {
129
- const result = await classifier(text, options);
130
- return flattenIfNeeded(result);
131
- });
132
- }
133
-
134
- /**
135
- * Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
136
- * is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
137
- *
138
- * Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
139
- *
140
- * Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
141
- * with the original message.
142
- *
143
- * @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
144
- * @param text - The text to classify.
145
- * @param options - Optional upstream classification options. `top_k` is always set to `null`
146
- * regardless of any value supplied here.
147
- *
148
- * @see https://huggingface.co/docs/transformers.js
149
- * @public
150
- */
151
- export async function classifyAll(
152
- classifier: TextClassificationPipeline,
153
- text: string,
154
- options?: Parameters<TextClassificationPipeline>[1]
155
- ): Promise<Result<TextClassificationOutput>> {
156
- return classify(classifier, text, { ...options, top_k: null });
157
- }
158
-
159
- /**
160
- * Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
161
- * Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
162
- * applied. Callers receive the raw output and are responsible for any downstream processing.
163
- *
164
- * Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
165
- *
166
- * Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
167
- * are captured as `Failure` with the original message.
168
- *
169
- * @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
170
- * @param text - The text (or texts) to embed.
171
- * @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
172
- * Passed through verbatim to the pipeline call.
173
- *
174
- * @see https://huggingface.co/docs/transformers.js
175
- * @public
176
- */
177
- export async function embed(
178
- extractor: FeatureExtractionPipeline,
179
- text: string | string[],
180
- options?: Parameters<FeatureExtractionPipeline['_call']>[1]
181
- ): Promise<Result<Tensor>> {
182
- return captureAsyncResult(() => extractor(text, options));
183
- }
184
-
185
- /**
186
- * Result-integration wrapper that invokes a `SummarizationPipeline` on a single text input.
187
- * Returns the upstream `SummarizationOutput` Result-wrapped (an array of `{ summary_text }`
188
- * entries; one for a single-string input).
189
- *
190
- * Callers should retrieve the summarizer via `loadPipeline('summarization', modelId)` — e.g.
191
- * `Xenova/distilbart-cnn-6-6` for a small, local-friendly model. This facade applies no opinionated
192
- * orchestration (no length policy, no local-vs-cloud routing) — `options` are passed through
193
- * verbatim and the caller owns any escalation decision.
194
- *
195
- * Returns `Promise<Result<SummarizationOutput>>`; upstream errors (inference failures, tokenisation
196
- * errors) are captured as `Failure` with the original message.
197
- *
198
- * @param summarizer - A `SummarizationPipeline` obtained from `loadPipeline`.
199
- * @param text - The text to summarize.
200
- * @param options - Optional upstream summarization options (e.g. `min_length`, `max_length`,
201
- * `max_new_tokens`). Passed through verbatim to the pipeline call.
202
- *
203
- * @see https://huggingface.co/docs/transformers.js
204
- * @public
205
- */
206
- export async function summarize(
207
- summarizer: SummarizationPipeline,
208
- text: string,
209
- options?: Parameters<SummarizationPipeline>[1]
210
- ): Promise<Result<SummarizationOutput>> {
211
- return captureAsyncResult(() => summarizer(text, options));
212
- }