@workglow/ai 0.0.58 → 0.0.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.js +67 -92
- package/dist/browser.js.map +12 -12
- package/dist/bun.js +67 -118
- package/dist/bun.js.map +12 -14
- package/dist/common.d.ts +2 -1
- package/dist/common.d.ts.map +1 -1
- package/dist/model/{storage/InMemoryModelRepository.d.ts → InMemoryModelRepository.d.ts} +2 -2
- package/dist/model/InMemoryModelRepository.d.ts.map +1 -0
- package/dist/model/ModelRegistry.d.ts +12 -0
- package/dist/model/ModelRegistry.d.ts.map +1 -1
- package/dist/model/ModelRepository.d.ts +59 -60
- package/dist/model/ModelRepository.d.ts.map +1 -1
- package/dist/model/ModelSchema.d.ts +42 -0
- package/dist/model/ModelSchema.d.ts.map +1 -0
- package/dist/node.js +67 -118
- package/dist/node.js.map +12 -14
- package/dist/provider/AiProviderRegistry.d.ts +15 -3
- package/dist/provider/AiProviderRegistry.d.ts.map +1 -1
- package/dist/task/DownloadModelTask.d.ts +179 -19
- package/dist/task/DownloadModelTask.d.ts.map +1 -1
- package/dist/task/TextEmbeddingTask.d.ts +77 -9
- package/dist/task/TextEmbeddingTask.d.ts.map +1 -1
- package/dist/task/TextGenerationTask.d.ts +77 -9
- package/dist/task/TextGenerationTask.d.ts.map +1 -1
- package/dist/task/TextQuestionAnswerTask.d.ts +77 -9
- package/dist/task/TextQuestionAnswerTask.d.ts.map +1 -1
- package/dist/task/TextRewriterTask.d.ts +77 -9
- package/dist/task/TextRewriterTask.d.ts.map +1 -1
- package/dist/task/TextSummaryTask.d.ts +77 -9
- package/dist/task/TextSummaryTask.d.ts.map +1 -1
- package/dist/task/TextTranslationTask.d.ts +77 -9
- package/dist/task/TextTranslationTask.d.ts.map +1 -1
- package/dist/task/base/AiTask.d.ts +3 -3
- package/dist/task/base/AiTask.d.ts.map +1 -1
- package/dist/task/base/AiTaskSchemas.d.ts +43 -2
- package/dist/task/base/AiTaskSchemas.d.ts.map +1 -1
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -12
- package/dist/model/Model.d.ts +0 -26
- package/dist/model/Model.d.ts.map +0 -1
- package/dist/model/storage/InMemoryModelRepository.d.ts.map +0 -1
- package/dist/model/storage/IndexedDbModelRepository.d.ts +0 -18
- package/dist/model/storage/IndexedDbModelRepository.d.ts.map +0 -1
- package/dist/model/storage/PostgresModelRepository.d.ts +0 -19
- package/dist/model/storage/PostgresModelRepository.d.ts.map +0 -1
- package/dist/model/storage/SqliteModelRepository.d.ts +0 -18
- package/dist/model/storage/SqliteModelRepository.d.ts.map +0 -1
package/dist/browser.js
CHANGED
|
@@ -6,31 +6,14 @@ import {
|
|
|
6
6
|
PermanentJobError
|
|
7
7
|
} from "@workglow/job-queue";
|
|
8
8
|
|
|
9
|
-
// src/model/
|
|
9
|
+
// src/model/ModelRegistry.ts
|
|
10
|
+
import { createServiceToken, globalServiceRegistry } from "@workglow/util";
|
|
11
|
+
|
|
12
|
+
// src/model/InMemoryModelRepository.ts
|
|
10
13
|
import { InMemoryTabularRepository } from "@workglow/storage";
|
|
11
14
|
|
|
12
15
|
// src/model/ModelRepository.ts
|
|
13
16
|
import { EventEmitter } from "@workglow/util";
|
|
14
|
-
var ModelSchema = {
|
|
15
|
-
type: "object",
|
|
16
|
-
properties: {
|
|
17
|
-
name: { type: "string" },
|
|
18
|
-
details: { type: "string" }
|
|
19
|
-
},
|
|
20
|
-
required: ["name", "details"],
|
|
21
|
-
additionalProperties: false
|
|
22
|
-
};
|
|
23
|
-
var ModelPrimaryKeyNames = ["name"];
|
|
24
|
-
var Task2ModelSchema = {
|
|
25
|
-
type: "object",
|
|
26
|
-
properties: {
|
|
27
|
-
task: { type: "string" },
|
|
28
|
-
model: { type: "string" }
|
|
29
|
-
},
|
|
30
|
-
required: ["task", "model"],
|
|
31
|
-
additionalProperties: false
|
|
32
|
-
};
|
|
33
|
-
var Task2ModelPrimaryKeyNames = ["task", "model"];
|
|
34
17
|
|
|
35
18
|
class ModelRepository {
|
|
36
19
|
type = "ModelRepository";
|
|
@@ -48,101 +31,101 @@ class ModelRepository {
|
|
|
48
31
|
return this.events.waitOn(name);
|
|
49
32
|
}
|
|
50
33
|
async addModel(model) {
|
|
51
|
-
await this.modelTabularRepository.put(
|
|
52
|
-
this.models.set(model.name, model);
|
|
34
|
+
await this.modelTabularRepository.put(model);
|
|
53
35
|
this.events.emit("model_added", model);
|
|
54
36
|
return model;
|
|
55
37
|
}
|
|
56
38
|
async findModelsByTask(task) {
|
|
57
39
|
if (typeof task != "string")
|
|
58
40
|
return;
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
41
|
+
const allModels = await this.modelTabularRepository.getAll();
|
|
42
|
+
if (!allModels || allModels.length === 0)
|
|
43
|
+
return;
|
|
44
|
+
const models = allModels.filter((model) => model.tasks?.includes(task));
|
|
45
|
+
if (models.length === 0)
|
|
61
46
|
return;
|
|
62
|
-
const models = [];
|
|
63
|
-
for (const junction of junctions) {
|
|
64
|
-
const model = await this.modelTabularRepository.get({ name: junction.model });
|
|
65
|
-
if (model)
|
|
66
|
-
models.push(JSON.parse(model.details));
|
|
67
|
-
}
|
|
68
|
-
models.forEach((m) => this.models.set(m.name, m));
|
|
69
|
-
this.taskModels.set(task, models);
|
|
70
47
|
return models;
|
|
71
48
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
async findTasksByModel(model) {
|
|
75
|
-
if (typeof model != "string")
|
|
49
|
+
async findTasksByModel(model_id) {
|
|
50
|
+
if (typeof model_id != "string")
|
|
76
51
|
return;
|
|
77
|
-
const
|
|
78
|
-
if (!
|
|
52
|
+
const modelRecord = await this.modelTabularRepository.get({ model_id });
|
|
53
|
+
if (!modelRecord)
|
|
79
54
|
return;
|
|
80
|
-
return
|
|
55
|
+
return modelRecord.tasks && modelRecord.tasks.length > 0 ? modelRecord.tasks : undefined;
|
|
81
56
|
}
|
|
82
57
|
async enumerateAllTasks() {
|
|
83
|
-
const
|
|
84
|
-
if (!
|
|
58
|
+
const allModels = await this.modelTabularRepository.getAll();
|
|
59
|
+
if (!allModels || allModels.length === 0)
|
|
85
60
|
return;
|
|
86
|
-
const uniqueTasks =
|
|
87
|
-
|
|
61
|
+
const uniqueTasks = new Set;
|
|
62
|
+
for (const model of allModels) {
|
|
63
|
+
if (model.tasks) {
|
|
64
|
+
for (const task of model.tasks) {
|
|
65
|
+
uniqueTasks.add(task);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return uniqueTasks.size > 0 ? Array.from(uniqueTasks) : undefined;
|
|
88
70
|
}
|
|
89
71
|
async enumerateAllModels() {
|
|
90
72
|
const models = await this.modelTabularRepository.getAll();
|
|
91
73
|
if (!models || models.length === 0)
|
|
92
74
|
return;
|
|
93
|
-
|
|
94
|
-
parsedModels.forEach((m) => this.models.set(m.name, m));
|
|
95
|
-
return parsedModels;
|
|
96
|
-
}
|
|
97
|
-
async connectTaskToModel(task, model) {
|
|
98
|
-
await this.task2ModelTabularRepository.put({ task, model });
|
|
99
|
-
this.events.emit("task_model_connected", task, model);
|
|
75
|
+
return models;
|
|
100
76
|
}
|
|
101
|
-
async findByName(
|
|
102
|
-
if (typeof
|
|
77
|
+
async findByName(model_id) {
|
|
78
|
+
if (typeof model_id != "string")
|
|
103
79
|
return;
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
return;
|
|
107
|
-
const model = JSON.parse(modelstr.details);
|
|
108
|
-
this.models.set(model.name, model);
|
|
109
|
-
return model;
|
|
80
|
+
const model = await this.modelTabularRepository.get({ model_id });
|
|
81
|
+
return model ?? undefined;
|
|
110
82
|
}
|
|
111
83
|
async size() {
|
|
112
84
|
return await this.modelTabularRepository.size();
|
|
113
85
|
}
|
|
114
|
-
async clear() {
|
|
115
|
-
await this.modelTabularRepository.deleteAll();
|
|
116
|
-
}
|
|
117
86
|
}
|
|
118
87
|
|
|
119
|
-
// src/model/
|
|
88
|
+
// src/model/ModelSchema.ts
|
|
89
|
+
var ModelSchema = {
|
|
90
|
+
type: "object",
|
|
91
|
+
properties: {
|
|
92
|
+
model_id: { type: "string" },
|
|
93
|
+
tasks: { type: "array", items: { type: "string" } },
|
|
94
|
+
title: { type: "string" },
|
|
95
|
+
description: { type: "string" },
|
|
96
|
+
provider: { type: "string" },
|
|
97
|
+
providerConfig: { type: "object", default: {} },
|
|
98
|
+
metadata: { type: "object", default: {} }
|
|
99
|
+
},
|
|
100
|
+
required: ["model_id", "tasks", "provider", "title", "description", "providerConfig", "metadata"],
|
|
101
|
+
additionalProperties: false
|
|
102
|
+
};
|
|
103
|
+
var ModelPrimaryKeyNames = ["model_id"];
|
|
104
|
+
|
|
105
|
+
// src/model/InMemoryModelRepository.ts
|
|
120
106
|
class InMemoryModelRepository extends ModelRepository {
|
|
121
107
|
modelTabularRepository;
|
|
122
|
-
task2ModelTabularRepository;
|
|
123
108
|
type = "InMemoryModelRepository";
|
|
124
109
|
constructor() {
|
|
125
110
|
super();
|
|
126
111
|
this.modelTabularRepository = new InMemoryTabularRepository(ModelSchema, ModelPrimaryKeyNames);
|
|
127
|
-
this.task2ModelTabularRepository = new InMemoryTabularRepository(Task2ModelSchema, Task2ModelPrimaryKeyNames, ["model"]);
|
|
128
112
|
}
|
|
129
113
|
}
|
|
130
114
|
|
|
131
115
|
// src/model/ModelRegistry.ts
|
|
132
|
-
|
|
116
|
+
var MODEL_REPOSITORY = createServiceToken("model.repository");
|
|
117
|
+
if (!globalServiceRegistry.has(MODEL_REPOSITORY)) {
|
|
118
|
+
globalServiceRegistry.register(MODEL_REPOSITORY, () => new InMemoryModelRepository, true);
|
|
133
119
|
}
|
|
134
|
-
var modelRegistry;
|
|
135
120
|
function getGlobalModelRepository() {
|
|
136
|
-
|
|
137
|
-
modelRegistry = new FallbackModelRegistry;
|
|
138
|
-
return modelRegistry;
|
|
121
|
+
return globalServiceRegistry.get(MODEL_REPOSITORY);
|
|
139
122
|
}
|
|
140
123
|
function setGlobalModelRepository(pr) {
|
|
141
|
-
|
|
124
|
+
globalServiceRegistry.registerInstance(MODEL_REPOSITORY, pr);
|
|
142
125
|
}
|
|
143
126
|
|
|
144
127
|
// src/provider/AiProviderRegistry.ts
|
|
145
|
-
import { globalServiceRegistry, WORKER_MANAGER } from "@workglow/util";
|
|
128
|
+
import { globalServiceRegistry as globalServiceRegistry2, WORKER_MANAGER } from "@workglow/util";
|
|
146
129
|
|
|
147
130
|
class AiProviderRegistry {
|
|
148
131
|
runFnRegistry = new Map;
|
|
@@ -154,7 +137,7 @@ class AiProviderRegistry {
|
|
|
154
137
|
}
|
|
155
138
|
registerAsWorkerRunFn(modelProvider, taskType) {
|
|
156
139
|
const workerFn = async (input, model, update_progress, signal) => {
|
|
157
|
-
const workerManager =
|
|
140
|
+
const workerManager = globalServiceRegistry2.get(WORKER_MANAGER);
|
|
158
141
|
const result = await workerManager.callWorkerFunction(modelProvider, taskType, [input, model], {
|
|
159
142
|
signal,
|
|
160
143
|
onProgress: update_progress
|
|
@@ -536,7 +519,7 @@ class AiTask extends JobQueueTask {
|
|
|
536
519
|
for (const [key, propSchema] of modelTaskProperties) {
|
|
537
520
|
let requestedModels = Array.isArray(input[key]) ? input[key] : [input[key]];
|
|
538
521
|
for (const model of requestedModels) {
|
|
539
|
-
const foundModel = taskModels?.find((m) => m.
|
|
522
|
+
const foundModel = taskModels?.find((m) => m.model_id === model);
|
|
540
523
|
if (!foundModel) {
|
|
541
524
|
throw new TaskConfigurationError(`AiTask: Missing model for '${key}' named '${model}' for task '${this.type}'`);
|
|
542
525
|
}
|
|
@@ -570,7 +553,7 @@ class AiTask extends JobQueueTask {
|
|
|
570
553
|
const taskModels = await getGlobalModelRepository().findModelsByTask(this.type);
|
|
571
554
|
for (const [key, propSchema] of modelTaskProperties) {
|
|
572
555
|
let requestedModels = Array.isArray(input[key]) ? input[key] : [input[key]];
|
|
573
|
-
let usingModels = requestedModels.filter((model) => taskModels?.find((m) => m.
|
|
556
|
+
let usingModels = requestedModels.filter((model) => taskModels?.find((m) => m.model_id === model));
|
|
574
557
|
usingModels = usingModels.length > 1 ? usingModels : usingModels[0];
|
|
575
558
|
input[key] = usingModels;
|
|
576
559
|
}
|
|
@@ -667,7 +650,7 @@ var TypeLanguage = (annotations = {}) => ({
|
|
|
667
650
|
minLength: 2,
|
|
668
651
|
...annotations
|
|
669
652
|
});
|
|
670
|
-
function
|
|
653
|
+
function TypeModelAsString(semantic = "model", options = {}) {
|
|
671
654
|
if (semantic !== "model" && !semantic.startsWith("model:")) {
|
|
672
655
|
throw new Error("Invalid semantic value");
|
|
673
656
|
}
|
|
@@ -680,6 +663,11 @@ function TypeModel(semantic = "model", options = {}) {
|
|
|
680
663
|
type: "string"
|
|
681
664
|
};
|
|
682
665
|
}
|
|
666
|
+
function TypeModel(semantic = "model", options = {}) {
|
|
667
|
+
return {
|
|
668
|
+
oneOf: [TypeModelAsString(semantic, options), ModelSchema]
|
|
669
|
+
};
|
|
670
|
+
}
|
|
683
671
|
var TypeReplicateArray = (type, annotations = {}) => ({
|
|
684
672
|
oneOf: [type, { type: "array", items: type }],
|
|
685
673
|
title: type.title,
|
|
@@ -1303,18 +1291,6 @@ function normalize(vector) {
|
|
|
1303
1291
|
}
|
|
1304
1292
|
return new Float32Array(normalized);
|
|
1305
1293
|
}
|
|
1306
|
-
// src/model/storage/IndexedDbModelRepository.ts
|
|
1307
|
-
import { IndexedDbTabularRepository } from "@workglow/storage";
|
|
1308
|
-
class IndexedDbModelRepository extends ModelRepository {
|
|
1309
|
-
modelTabularRepository;
|
|
1310
|
-
task2ModelTabularRepository;
|
|
1311
|
-
type = "IndexedDbModelRepository";
|
|
1312
|
-
constructor(tableModels = "models", tableTask2Models = "task2models") {
|
|
1313
|
-
super();
|
|
1314
|
-
this.modelTabularRepository = new IndexedDbTabularRepository(tableModels, ModelSchema, ModelPrimaryKeyNames);
|
|
1315
|
-
this.task2ModelTabularRepository = new IndexedDbTabularRepository(tableTask2Models, Task2ModelSchema, Task2ModelPrimaryKeyNames, ["model"]);
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
1294
|
export {
|
|
1319
1295
|
setGlobalModelRepository,
|
|
1320
1296
|
setAiProviderRegistry,
|
|
@@ -1326,6 +1302,7 @@ export {
|
|
|
1326
1302
|
VectorSimilarityTask,
|
|
1327
1303
|
TypedArraySchema,
|
|
1328
1304
|
TypeReplicateArray,
|
|
1305
|
+
TypeModelAsString,
|
|
1329
1306
|
TypeModel,
|
|
1330
1307
|
TypeLanguage,
|
|
1331
1308
|
TextTranslationTask,
|
|
@@ -1353,15 +1330,13 @@ export {
|
|
|
1353
1330
|
TextEmbeddingOutputSchema,
|
|
1354
1331
|
TextEmbeddingInputSchema,
|
|
1355
1332
|
TextEmbedding,
|
|
1356
|
-
Task2ModelSchema,
|
|
1357
|
-
Task2ModelPrimaryKeyNames,
|
|
1358
1333
|
TableFragment,
|
|
1359
1334
|
SimilarityFn,
|
|
1360
1335
|
Similarity,
|
|
1361
1336
|
ModelSchema,
|
|
1362
1337
|
ModelRepository,
|
|
1363
1338
|
ModelPrimaryKeyNames,
|
|
1364
|
-
|
|
1339
|
+
MODEL_REPOSITORY,
|
|
1365
1340
|
InMemoryModelRepository,
|
|
1366
1341
|
ImageFragment,
|
|
1367
1342
|
DownloadModelTask,
|
|
@@ -1378,4 +1353,4 @@ export {
|
|
|
1378
1353
|
AiJob
|
|
1379
1354
|
};
|
|
1380
1355
|
|
|
1381
|
-
//# debugId=
|
|
1356
|
+
//# debugId=55D3E7D54413645E64756E2164756E21
|