@workglow/ai 0.0.52

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/LICENSE +201 -0
  2. package/README.md +531 -0
  3. package/dist/browser.js +1371 -0
  4. package/dist/browser.js.map +30 -0
  5. package/dist/bun.js +1398 -0
  6. package/dist/bun.js.map +32 -0
  7. package/dist/common.d.ts +15 -0
  8. package/dist/common.d.ts.map +1 -0
  9. package/dist/job/AiJob.d.ts +28 -0
  10. package/dist/job/AiJob.d.ts.map +1 -0
  11. package/dist/model/Model.d.ts +26 -0
  12. package/dist/model/Model.d.ts.map +1 -0
  13. package/dist/model/ModelRegistry.d.ts +9 -0
  14. package/dist/model/ModelRegistry.d.ts.map +1 -0
  15. package/dist/model/ModelRepository.d.ts +146 -0
  16. package/dist/model/ModelRepository.d.ts.map +1 -0
  17. package/dist/model/storage/InMemoryModelRepository.d.ts +18 -0
  18. package/dist/model/storage/InMemoryModelRepository.d.ts.map +1 -0
  19. package/dist/model/storage/IndexedDbModelRepository.d.ts +18 -0
  20. package/dist/model/storage/IndexedDbModelRepository.d.ts.map +1 -0
  21. package/dist/model/storage/PostgresModelRepository.d.ts +19 -0
  22. package/dist/model/storage/PostgresModelRepository.d.ts.map +1 -0
  23. package/dist/model/storage/SqliteModelRepository.d.ts +18 -0
  24. package/dist/model/storage/SqliteModelRepository.d.ts.map +1 -0
  25. package/dist/node.js +1397 -0
  26. package/dist/node.js.map +32 -0
  27. package/dist/provider/AiProviderRegistry.d.ts +35 -0
  28. package/dist/provider/AiProviderRegistry.d.ts.map +1 -0
  29. package/dist/source/Document.d.ts +56 -0
  30. package/dist/source/Document.d.ts.map +1 -0
  31. package/dist/source/DocumentConverter.d.ts +15 -0
  32. package/dist/source/DocumentConverter.d.ts.map +1 -0
  33. package/dist/source/DocumentConverterMarkdown.d.ts +13 -0
  34. package/dist/source/DocumentConverterMarkdown.d.ts.map +1 -0
  35. package/dist/source/DocumentConverterText.d.ts +13 -0
  36. package/dist/source/DocumentConverterText.d.ts.map +1 -0
  37. package/dist/source/MasterDocument.d.ts +27 -0
  38. package/dist/source/MasterDocument.d.ts.map +1 -0
  39. package/dist/source/index.d.ts +10 -0
  40. package/dist/source/index.d.ts.map +1 -0
  41. package/dist/task/DocumentSplitterTask.d.ts +58 -0
  42. package/dist/task/DocumentSplitterTask.d.ts.map +1 -0
  43. package/dist/task/DownloadModelTask.d.ts +118 -0
  44. package/dist/task/DownloadModelTask.d.ts.map +1 -0
  45. package/dist/task/TextEmbeddingTask.d.ts +268 -0
  46. package/dist/task/TextEmbeddingTask.d.ts.map +1 -0
  47. package/dist/task/TextGenerationTask.d.ts +140 -0
  48. package/dist/task/TextGenerationTask.d.ts.map +1 -0
  49. package/dist/task/TextQuestionAnswerTask.d.ts +124 -0
  50. package/dist/task/TextQuestionAnswerTask.d.ts.map +1 -0
  51. package/dist/task/TextRewriterTask.d.ts +113 -0
  52. package/dist/task/TextRewriterTask.d.ts.map +1 -0
  53. package/dist/task/TextSummaryTask.d.ts +95 -0
  54. package/dist/task/TextSummaryTask.d.ts.map +1 -0
  55. package/dist/task/TextTranslationTask.d.ts +158 -0
  56. package/dist/task/TextTranslationTask.d.ts.map +1 -0
  57. package/dist/task/VectorSimilarityTask.d.ts +334 -0
  58. package/dist/task/VectorSimilarityTask.d.ts.map +1 -0
  59. package/dist/task/base/AiTask.d.ts +47 -0
  60. package/dist/task/base/AiTask.d.ts.map +1 -0
  61. package/dist/task/base/AiTaskSchemas.d.ts +156 -0
  62. package/dist/task/base/AiTaskSchemas.d.ts.map +1 -0
  63. package/dist/task/index.d.ts +17 -0
  64. package/dist/task/index.d.ts.map +1 -0
  65. package/dist/types.d.ts +11 -0
  66. package/dist/types.d.ts.map +1 -0
  67. package/package.json +67 -0
package/README.md ADDED
@@ -0,0 +1,531 @@
1
+ # @workglow/ai
2
+
3
+ Core AI abstractions and functionality for Workglow AI task pipelines.
4
+
5
+ ## Overview
6
+
7
+ The `@workglow/ai` package provides the core AI abstractions, job definitions, and task implementations that form the foundation of Workglow's AI task execution system. It defines the interfaces and base classes that AI providers implement, along with a comprehensive set of AI tasks ready for use.
8
+
9
+ ## Features
10
+
11
+ - **Built-in AI Tasks**: Pre-implemented tasks for common AI operations
12
+ - **Provider Interface**: Standard interface for AI service providers
13
+ - **Model Management**: Complete system for managing AI models and their associations with tasks, and can persist with multiple storage backends
14
+ - **Multi-Platform Support**: Works in browser, Node.js, and Bun environments
15
+ - **Type Safety**: Full TypeScript support with comprehensive type definitions
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ bun add @workglow/ai
21
+ ```
22
+
23
+ ## Quick Start
24
+
25
+ Here's a complete example of setting up and using the AI package with the Hugging Face Transformers ONNX provider from `@workglow/ai-provider`:
26
+
27
+ ```typescript
28
+ import {
29
+ TextGenerationTask,
30
+ TextEmbeddingTask,
31
+ getGlobalModelRepository,
32
+ setGlobalModelRepository,
33
+ InMemoryModelRepository,
34
+ AiJob,
35
+ } from "@workglow/ai";
36
+ import { Workflow, getTaskQueueRegistry } from "@workglow/task-graph";
37
+ import { JobQueue } from "@workglow/job-queue";
38
+ import { HF_TRANSFORMERS_ONNX, register_HFT_InlineJobFns } from "@workglow/ai-provider";
39
+
40
+ // 1. Set up a model repository
41
+ const modelRepo = new InMemoryModelRepository();
42
+ setGlobalModelRepository(modelRepo);
43
+
44
+ // 2. Add a local ONNX model (Hugging Face Transformers)
45
+ await modelRepo.addModel({
46
+ name: "onnx:Xenova/LaMini-Flan-T5-783M:q8",
47
+ url: "Xenova/LaMini-Flan-T5-783M",
48
+ provider: HF_TRANSFORMERS_ONNX,
49
+ availableOnBrowser: true,
50
+ availableOnServer: true,
51
+ contextWindow: 4096,
52
+ pipeline: "text2text-generation",
53
+ });
54
+
55
+ // 3. Connect models to tasks
56
+ await modelRepo.connectTaskToModel("TextGenerationTask", "onnx:Xenova/LaMini-Flan-T5-783M:q8");
57
+
58
+ // 4. Register provider functions (inline, same thread)
59
+ await register_HFT_InlineJobFns();
60
+
61
+ // 5. Set up job queue for the provider
62
+ getTaskQueueRegistry().registerQueue(new JobQueue(HF_TRANSFORMERS_ONNX, AiJob));
63
+
64
+ // 6. Create and run a workflow
65
+ const workflow = new Workflow();
66
+
67
+ const result = await workflow
68
+ .TextGenerationTask({
69
+ model: "onnx:Xenova/LaMini-Flan-T5-783M:q8",
70
+ prompt: "Write a short story about a robot learning to paint.",
71
+ maxTokens: 200,
72
+ temperature: 0.8,
73
+ })
74
+ .run();
75
+
76
+ console.log(result.text);
77
+ ```
78
+
79
+ ## Available AI Tasks
80
+
81
+ ### Text Processing Tasks
82
+
83
+ #### TextGenerationTask
84
+
85
+ Generates text based on prompts using language models.
86
+
87
+ ```typescript
88
+ import { TextGenerationTask } from "@workglow/ai";
89
+
90
+ const task = new TextGenerationTask({
91
+ model: "onnx:Xenova/gpt2:q8",
92
+ prompt: "Explain quantum computing in simple terms",
93
+ });
94
+
95
+ const result = await task.run();
96
+ // Output: { text: "Quantum computing is..." }
97
+ ```
98
+
99
+ #### TextEmbeddingTask
100
+
101
+ Generates vector embeddings for text using embedding models.
102
+
103
+ ```typescript
104
+ import { TextEmbeddingTask } from "@workglow/ai";
105
+
106
+ const task = new TextEmbeddingTask({
107
+ model: "onnx:Xenova/LaMini-Flan-T5-783M:q8",
108
+ text: "This is a sample text for embedding",
109
+ });
110
+
111
+ const result = await task.run();
112
+ // Output: { vector: [0.1, -0.2, 0.3, ...] }
113
+ ```
114
+
115
+ #### TextTranslationTask
116
+
117
+ Translates text between different languages.
118
+
119
+ ```typescript
120
+ import { TextTranslationTask } from "@workglow/ai";
121
+
122
+ const task = new TextTranslationTask({
123
+ model: "translation-model",
124
+ text: "Hello, how are you?",
125
+ sourceLanguage: "en",
126
+ targetLanguage: "es",
127
+ });
128
+
129
+ const result = await task.run();
130
+ // Output: { translatedText: "Hola, ¿cómo estás?" }
131
+ ```
132
+
133
+ #### TextSummaryTask
134
+
135
+ Generates summaries of longer text content.
136
+
137
+ ```typescript
138
+ import { TextSummaryTask } from "@workglow/ai";
139
+
140
+ const task = new TextSummaryTask({
141
+ model: "onnx:Falconsai/text_summarization:fp32",
142
+ text: "Long article content here...",
143
+ maxLength: 100,
144
+ });
145
+
146
+ const result = await task.run();
147
+ // Output: { summary: "Brief summary of the article..." }
148
+ ```
149
+
150
+ #### TextRewriterTask
151
+
152
+ Rewrites text in different styles or tones.
153
+
154
+ ```typescript
155
+ import { TextRewriterTask } from "@workglow/ai";
156
+
157
+ const task = new TextRewriterTask({
158
+ model: "onnx:Xenova/LaMini-Flan-T5-783M:q8",
159
+ text: "This is a formal business proposal.",
160
+ style: "casual",
161
+ });
162
+
163
+ const result = await task.run();
164
+ // Output: { rewrittenText: "This is a casual business idea..." }
165
+ ```
166
+
167
+ #### TextQuestionAnswerTask
168
+
169
+ Answers questions based on provided context.
170
+
171
+ ```typescript
172
+ import { TextQuestionAnswerTask } from "@workglow/ai";
173
+
174
+ const task = new TextQuestionAnswerTask({
175
+ model: "onnx:Xenova/distilbert-base-uncased-distilled-squad:q8",
176
+ context: "The capital of France is Paris. It has a population of about 2.1 million.",
177
+ question: "What is the population of Paris?",
178
+ });
179
+
180
+ const result = await task.run();
181
+ // Output: { answer: "About 2.1 million" }
182
+ ```
183
+
184
+ ### Analysis Tasks
185
+
186
+ #### VectorSimilarityTask
187
+
188
+ Computes similarity between texts or embeddings.
189
+
190
+ ```typescript
191
+ import { VectorSimilarityTask } from "@workglow/ai";
192
+
193
+ const task = new VectorSimilarityTask({
194
+ model: "onnx:Supabase/gte-small:q8",
195
+ text1: "I love programming",
196
+ text2: "Coding is my passion",
197
+ });
198
+
199
+ const result = await task.run();
200
+ // Output: { similarity: 0.85 }
201
+ ```
202
+
203
+ ### Document Processing Tasks
204
+
205
+ #### DocumentSplitterTask
206
+
207
+ Splits documents into smaller chunks for processing.
208
+
209
+ ```typescript
210
+ import { DocumentSplitterTask } from "@workglow/ai";
211
+
212
+ const task = new DocumentSplitterTask({
213
+ document: "Very long document content...",
214
+ chunkSize: 1000,
215
+ chunkOverlap: 200,
216
+ });
217
+
218
+ const result = await task.run();
219
+ // Output: { chunks: ["chunk1...", "chunk2...", "chunk3..."] }
220
+ ```
221
+
222
+ ### Model Management Tasks
223
+
224
+ #### DownloadModelTask
225
+
226
+ Downloads and prepares AI models for use.
227
+
228
+ ```typescript
229
+ import { DownloadModelTask } from "@workglow/ai";
230
+
231
+ import { HF_TRANSFORMERS_ONNX } from "@workglow/ai-provider";
232
+
233
+ const task = new DownloadModelTask({
234
+ modelName: "onnx:Xenova/LaMini-Flan-T5-783M:q8",
235
+ modelUrl: "Xenova/LaMini-Flan-T5-783M",
236
+ provider: HF_TRANSFORMERS_ONNX,
237
+ });
238
+
239
+ const result = await task.run();
240
+ // Output: { status: "downloaded", path: "/models/..." }
241
+ ```
242
+
243
+ ## Model Management
244
+
245
+ ### Setting Up Models
246
+
247
+ Models are managed through the `ModelRepository` system. You can use different storage backends:
248
+
249
+ #### In-Memory Repository (Development)
250
+
251
+ ```typescript
252
+ import { InMemoryModelRepository, setGlobalModelRepository } from "@workglow/ai";
253
+
254
+ const modelRepo = new InMemoryModelRepository();
255
+ setGlobalModelRepository(modelRepo);
256
+ ```
257
+
258
+ #### IndexedDB Repository (Browser)
259
+
260
+ ```typescript
261
+ import { IndexedDbModelRepository, setGlobalModelRepository } from "@workglow/ai";
262
+
263
+ const modelRepo = new IndexedDbModelRepository("models", "task2models");
264
+ setGlobalModelRepository(modelRepo);
265
+ ```
266
+
267
+ #### SQLite Repository (Server)
268
+
269
+ ```typescript
270
+ import { SqliteModelRepository, setGlobalModelRepository } from "@workglow/ai";
271
+
272
+ const modelRepo = new SqliteModelRepository("./models.db");
273
+ setGlobalModelRepository(modelRepo);
274
+ ```
275
+
276
+ #### PostgreSQL Repository (Production)
277
+
278
+ ```typescript
279
+ import { PostgresModelRepository, setGlobalModelRepository } from "@workglow/ai";
280
+ import { Pool } from "pg";
281
+
282
+ const pool = new Pool({
283
+ user: "username",
284
+ host: "localhost",
285
+ database: "mydb",
286
+ password: "password",
287
+ port: 5432,
288
+ });
289
+
290
+ const modelRepo = new PostgresModelRepository(pool);
291
+ setGlobalModelRepository(modelRepo);
292
+ ```
293
+
294
+ ### Adding Models
295
+
296
+ ```typescript
297
+ import { getGlobalModelRepository } from "@workglow/ai";
298
+ import { HF_TRANSFORMERS_ONNX } from "@workglow/ai-provider";
299
+
300
+ const modelRepo = getGlobalModelRepository();
301
+
302
+ // Add an ONNX model from Hugging Face
303
+ await modelRepo.addModel({
304
+ name: "onnx:Xenova/gpt2:q8",
305
+ url: "Xenova/gpt2",
306
+ provider: HF_TRANSFORMERS_ONNX,
307
+ availableOnBrowser: true,
308
+ availableOnServer: true,
309
+ contextWindow: 8192,
310
+ });
311
+
312
+ // Connect model to specific tasks
313
+ await modelRepo.connectTaskToModel("TextGenerationTask", "onnx:Xenova/gpt2:q8");
314
+
315
+ // Find models for a specific task
316
+ const textGenModels = await modelRepo.findModelsByTask("TextGenerationTask");
317
+ ```
318
+
319
+ ## Provider Setup
320
+
321
+ AI providers handle the actual execution of AI tasks. You need to register provider functions for each model provider and task type combination.
322
+
323
+ ### Basic Provider Registration
324
+
325
+ ```typescript
326
+ import { register_HFT_InlineJobFns } from "@workglow/ai-provider";
327
+
328
+ // Registers run functions for all supported AI tasks on the current thread
329
+ await register_HFT_InlineJobFns();
330
+ ```
331
+
332
+ ### Worker-Based Provider Registration
333
+
334
+ For compute-intensive tasks that should run in workers:
335
+
336
+ ```typescript
337
+ // See `@workglow/ai-provider` exports for worker/client registration helpers
338
+ // - register_HFT_WorkerJobFns (in worker)
339
+ // - register_HFT_ClientJobFns (in main thread)
340
+ ```
341
+
342
+ ### Job Queue Setup
343
+
344
+ Each provider needs a job queue for task execution:
345
+
346
+ ```typescript
347
+ import { getTaskQueueRegistry } from "@workglow/task-graph";
348
+ import { JobQueue } from "@workglow/job-queue";
349
+ import { AiJob } from "@workglow/ai";
350
+ import { HF_TRANSFORMERS_ONNX } from "@workglow/ai-provider";
351
+
352
+ getTaskQueueRegistry().registerQueue(new JobQueue(HF_TRANSFORMERS_ONNX, AiJob));
353
+ ```
354
+
355
+ ## Workflow Integration
356
+
357
+ AI tasks integrate seamlessly with Workglow workflows:
358
+
359
+ ```typescript
360
+ import { Workflow } from "@workglow/task-graph";
361
+ import { TextGenerationTask, TextEmbeddingTask, VectorSimilarityTask } from "@workglow/ai";
362
+
363
+ const workflow = new Workflow();
364
+
365
+ // Chain AI tasks together
366
+ const result = await workflow
367
+ .TextGenerationTask({
368
+ model: "onnx:Xenova/gpt2:q8",
369
+ prompt: "Write about artificial intelligence",
370
+ })
371
+ .TextEmbeddingTask({
372
+ model: "onnx:Supabase/gte-small:q8",
373
+ text: workflow.previous().text, // Use previous task output
374
+ })
375
+ .VectorSimilarityTask({
376
+ model: "onnx:Supabase/gte-small:q8",
377
+ text1: "artificial intelligence",
378
+ embedding2: workflow.previous().vector, // Use embedding from previous task
379
+ })
380
+ .run();
381
+
382
+ console.log("Final similarity score:", result.similarity);
383
+ ```
384
+
385
+ ## Document Processing
386
+
387
+ The package includes document processing capabilities:
388
+
389
+ ```typescript
390
+ import { Document, DocumentConverterMarkdown } from "@workglow/ai";
391
+
392
+ // Create a document
393
+ const doc = new Document("# My Document\n\nThis is content...", { title: "Sample Doc" });
394
+
395
+ // Convert markdown to structured format
396
+ const converter = new DocumentConverterMarkdown();
397
+ const processedDoc = await converter.convert(doc);
398
+
399
+ // Use with document splitter
400
+ const splitter = new DocumentSplitterTask({
401
+ document: processedDoc.content,
402
+ chunkSize: 500,
403
+ chunkOverlap: 50,
404
+ });
405
+
406
+ const chunks = await splitter.run();
407
+ ```
408
+
409
+ ## Error Handling
410
+
411
+ AI tasks include comprehensive error handling:
412
+
413
+ ```typescript
414
+ import { TaskConfigurationError } from "@workglow/task-graph";
415
+
416
+ try {
417
+ const task = new TextGenerationTask({
418
+ model: "nonexistent-model",
419
+ prompt: "Test prompt",
420
+ });
421
+
422
+ const result = await task.run();
423
+ } catch (error) {
424
+ if (error instanceof TaskConfigurationError) {
425
+ console.error("Configuration error:", error.message);
426
+ // Handle missing model, invalid parameters, etc.
427
+ } else {
428
+ console.error("Runtime error:", error.message);
429
+ // Handle API failures, network issues, etc.
430
+ }
431
+ }
432
+ ```
433
+
434
+ ## Advanced Configuration
435
+
436
+ ### Custom Model Validation
437
+
438
+ Tasks automatically validate that specified models exist and are compatible:
439
+
440
+ ```typescript
441
+ // Models are validated before task execution
442
+ const task = new TextGenerationTask({
443
+ model: "onnx:Xenova/gpt2:q8", // Must exist in ModelRepository and be connected to TextGenerationTask
444
+ prompt: "Generate text",
445
+ });
446
+
447
+ // Validation happens during task.run()
448
+ ```
449
+
450
+ ### Progress Tracking
451
+
452
+ Monitor AI task progress:
453
+
454
+ ```typescript
455
+ const task = new TextGenerationTask({
456
+ model: "onnx:Xenova/gpt2:q8",
457
+ prompt: "Long generation task...",
458
+ });
459
+
460
+ task.on("progress", (progress, message, details) => {
461
+ console.log(`Progress: ${progress}% - ${message}`);
462
+ });
463
+
464
+ const result = await task.run();
465
+ ```
466
+
467
+ ### Task Cancellation
468
+
469
+ All AI tasks support cancellation via AbortSignal:
470
+
471
+ ```typescript
472
+ const controller = new AbortController();
473
+
474
+ const task = new TextGenerationTask({
475
+ model: "onnx:Xenova/gpt2:q8",
476
+ prompt: "Generate text...",
477
+ });
478
+
479
+ // Cancel after 10 seconds
480
+ setTimeout(() => controller.abort(), 10000);
481
+
482
+ try {
483
+ const result = await task.run({ signal: controller.signal });
484
+ } catch (error) {
485
+ if (error.name === "AbortError") {
486
+ console.log("Task was cancelled");
487
+ }
488
+ }
489
+ ```
490
+
491
+ ## Environment-Specific Usage
492
+
493
+ ### Browser Usage
494
+
495
+ ```typescript
496
+ import { IndexedDbModelRepository } from "@workglow/ai";
497
+
498
+ // Use IndexedDB for persistent storage in browser
499
+ const modelRepo = new IndexedDbModelRepository();
500
+ ```
501
+
502
+ ### Node.js Usage
503
+
504
+ ```typescript
505
+ import { SqliteModelRepository } from "@workglow/ai";
506
+
507
+ // Use SQLite for server-side storage
508
+ const modelRepo = new SqliteModelRepository("./models.db");
509
+ ```
510
+
511
+ ### Bun Usage
512
+
513
+ ```typescript
514
+ import { InMemoryModelRepository } from "@workglow/ai";
515
+
516
+ // Direct imports work with Bun via conditional exports
517
+ const modelRepo = new InMemoryModelRepository();
518
+ ```
519
+
520
+ ## Dependencies
521
+
522
+ This package depends on:
523
+
524
+ - `@workglow/job-queue` - Job queue system for task execution
525
+ - `@workglow/storage` - Storage abstractions for model and data persistence
526
+ - `@workglow/task-graph` - Task graph system for workflow management
527
+ - `@workglow/util` - Utility functions and shared components, including JSON Schema types and utilities
528
+
529
+ ## License
530
+
531
+ Apache 2.0 - See [LICENSE](./LICENSE) for details.