@ai-setting/roy-agent-core 1.5.81 → 1.5.84
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/env/agent/index.js +1 -1
- package/dist/env/event-source/index.js +2 -2
- package/dist/env/index.js +11 -10
- package/dist/env/prompt/index.js +1 -1
- package/dist/env/task/index.js +3 -3
- package/dist/env/task/plugins/index.js +1 -1
- package/dist/env/task/storage/index.js +1 -1
- package/dist/env/task/tools/index.js +1 -1
- package/dist/env/workflow/index.js +4 -3
- package/dist/env/workflow/service/index.js +8 -2
- package/dist/env/workflow/storage/index.js +11 -6
- package/dist/env/workflow/tools/index.js +15 -1
- package/dist/index.js +14 -12
- package/dist/shared/@ai-setting/{roy-agent-core-qbgd7bp6.js → roy-agent-core-015vw11k.js} +27 -1
- package/dist/shared/@ai-setting/{roy-agent-core-t2q0rwt7.js → roy-agent-core-29fh9mxg.js} +4 -7
- package/dist/shared/@ai-setting/roy-agent-core-3sv590cv.js +132 -0
- package/dist/shared/@ai-setting/{roy-agent-core-w83v54mj.js → roy-agent-core-7fdzfsm6.js} +42 -135
- package/dist/shared/@ai-setting/{roy-agent-core-df3ng0pz.js → roy-agent-core-7nh5h9fn.js} +31 -2
- package/dist/shared/@ai-setting/{roy-agent-core-am646wfz.js → roy-agent-core-86d4exyf.js} +5 -2
- package/dist/shared/@ai-setting/{roy-agent-core-vgyj2rq9.js → roy-agent-core-akepggsr.js} +150 -6
- package/dist/shared/@ai-setting/{roy-agent-core-v3t28k5n.js → roy-agent-core-bt7wezvg.js} +19 -10
- package/dist/shared/@ai-setting/roy-agent-core-c67wr8sp.js +11 -0
- package/dist/shared/@ai-setting/{roy-agent-core-496zzm5a.js → roy-agent-core-h4h55x4h.js} +183 -3
- package/dist/shared/@ai-setting/roy-agent-core-hhrg314p.js +34 -0
- package/dist/shared/@ai-setting/{roy-agent-core-r9y8ct0w.js → roy-agent-core-pcdzfwdv.js} +4 -0
- package/dist/shared/@ai-setting/{roy-agent-core-p8jv13bm.js → roy-agent-core-qjv8537d.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-r5axf0ad.js +751 -0
- package/dist/shared/@ai-setting/{roy-agent-core-74cp3zp1.js → roy-agent-core-taxvytzz.js} +4 -1
- package/dist/shared/@ai-setting/roy-agent-core-tq9528d3.js +336 -0
- package/dist/shared/@ai-setting/{roy-agent-core-j8q8119y.js → roy-agent-core-ya1ayt1k.js} +1 -1
- package/package.json +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +0 -206
- package/dist/shared/@ai-setting/roy-agent-core-6b57g1zk.js +0 -103
- package/dist/shared/@ai-setting/roy-agent-core-a6j7g1qe.js +0 -428
|
@@ -5,7 +5,10 @@ import {
|
|
|
5
5
|
initDatabase,
|
|
6
6
|
initializeTables,
|
|
7
7
|
resetDatabase
|
|
8
|
-
} from "./roy-agent-core-
|
|
8
|
+
} from "./roy-agent-core-h4h55x4h.js";
|
|
9
|
+
import"./roy-agent-core-3sv590cv.js";
|
|
10
|
+
import"./roy-agent-core-rvxg1wps.js";
|
|
11
|
+
import"./roy-agent-core-k05v31rc.js";
|
|
9
12
|
import"./roy-agent-core-shme7set.js";
|
|
10
13
|
import"./roy-agent-core-7z9b1fm8.js";
|
|
11
14
|
import"./roy-agent-core-c6592r3c.js";
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TracedAs,
|
|
3
|
+
init_decorator
|
|
4
|
+
} from "./roy-agent-core-k05v31rc.js";
|
|
5
|
+
import {
|
|
6
|
+
__legacyDecorateClassTS
|
|
7
|
+
} from "./roy-agent-core-fs0mn2jk.js";
|
|
8
|
+
|
|
9
|
+
// src/env/workflow/service/workflow-service.ts
|
|
10
|
+
init_decorator();
|
|
11
|
+
var TAG_MIN_COUNT = 1;
|
|
12
|
+
var TAG_MAX_COUNT = 3;
|
|
13
|
+
class TagValidationError extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "TagValidationError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class WorkflowService {
|
|
21
|
+
workflowRepository;
|
|
22
|
+
engineFactory;
|
|
23
|
+
sessionComponent;
|
|
24
|
+
tagRepository;
|
|
25
|
+
constructor(workflowRepository, engineFactory, sessionComponent, tagRepository) {
|
|
26
|
+
this.workflowRepository = workflowRepository;
|
|
27
|
+
this.engineFactory = engineFactory;
|
|
28
|
+
this.sessionComponent = sessionComponent;
|
|
29
|
+
this.tagRepository = tagRepository;
|
|
30
|
+
}
|
|
31
|
+
validateTags(tags) {
|
|
32
|
+
const effective = tags ?? [];
|
|
33
|
+
if (effective.length < TAG_MIN_COUNT) {
|
|
34
|
+
throw new TagValidationError(`Workflow requires at least ${TAG_MIN_COUNT} tag (use --tags to provide 1-3 tags)`);
|
|
35
|
+
}
|
|
36
|
+
if (effective.length > TAG_MAX_COUNT) {
|
|
37
|
+
throw new TagValidationError(`Workflow supports at most ${TAG_MAX_COUNT} tags (received ${effective.length}). Please reduce to 1-${TAG_MAX_COUNT} tags.`);
|
|
38
|
+
}
|
|
39
|
+
return effective;
|
|
40
|
+
}
|
|
41
|
+
async createWorkflow(definition, options) {
|
|
42
|
+
const effectiveTags = this.validateTags(options?.tags || definition.metadata?.tags);
|
|
43
|
+
const existing = this.workflowRepository.getByName(definition.name);
|
|
44
|
+
if (existing) {
|
|
45
|
+
if (options?.force !== true) {
|
|
46
|
+
throw new Error(`Workflow already exists: ${definition.name}`);
|
|
47
|
+
}
|
|
48
|
+
const oldTags = existing.tags;
|
|
49
|
+
const updated = this.workflowRepository.update(existing.id, {
|
|
50
|
+
name: definition.name,
|
|
51
|
+
version: definition.version,
|
|
52
|
+
description: definition.description || existing.description,
|
|
53
|
+
definition,
|
|
54
|
+
config: definition.config || {},
|
|
55
|
+
metadata: {
|
|
56
|
+
...options.metadata || definition.metadata || existing.metadata || {},
|
|
57
|
+
taskId: options.taskId || definition.metadata?.taskId || existing.metadata?.taskId
|
|
58
|
+
},
|
|
59
|
+
tags: effectiveTags
|
|
60
|
+
});
|
|
61
|
+
this.syncTagsOnUpdate(oldTags, effectiveTags);
|
|
62
|
+
return updated;
|
|
63
|
+
}
|
|
64
|
+
const workflow = {
|
|
65
|
+
name: definition.name,
|
|
66
|
+
version: definition.version,
|
|
67
|
+
description: definition.description,
|
|
68
|
+
definition,
|
|
69
|
+
config: definition.config || {},
|
|
70
|
+
metadata: {
|
|
71
|
+
...options?.metadata || definition.metadata || {},
|
|
72
|
+
taskId: options?.taskId || definition.metadata?.taskId
|
|
73
|
+
},
|
|
74
|
+
tags: effectiveTags
|
|
75
|
+
};
|
|
76
|
+
return this.workflowRepository.transaction(() => {
|
|
77
|
+
const wf = this.workflowRepository.create(workflow);
|
|
78
|
+
if (this.tagRepository) {
|
|
79
|
+
this.tagRepository.upsertMany(effectiveTags);
|
|
80
|
+
}
|
|
81
|
+
return wf;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
syncTagsOnUpdate(oldTags, newTags) {
|
|
85
|
+
if (!this.tagRepository)
|
|
86
|
+
return;
|
|
87
|
+
const oldSet = new Set(oldTags);
|
|
88
|
+
const newSet = new Set(newTags);
|
|
89
|
+
const removed = oldTags.filter((t) => !newSet.has(t));
|
|
90
|
+
const added = newTags.filter((t) => !oldSet.has(t));
|
|
91
|
+
if (removed.length > 0)
|
|
92
|
+
this.tagRepository.decrementMany(removed);
|
|
93
|
+
if (added.length > 0)
|
|
94
|
+
this.tagRepository.upsertMany(added);
|
|
95
|
+
}
|
|
96
|
+
getWorkflow(idOrName) {
|
|
97
|
+
let workflow = this.workflowRepository.getById(idOrName);
|
|
98
|
+
if (workflow)
|
|
99
|
+
return workflow;
|
|
100
|
+
return this.workflowRepository.getByName(idOrName);
|
|
101
|
+
}
|
|
102
|
+
getWorkflowById(id) {
|
|
103
|
+
return this.workflowRepository.getById(id);
|
|
104
|
+
}
|
|
105
|
+
getWorkflowByName(name) {
|
|
106
|
+
return this.workflowRepository.getByName(name);
|
|
107
|
+
}
|
|
108
|
+
listWorkflows(options) {
|
|
109
|
+
return this.workflowRepository.list(options);
|
|
110
|
+
}
|
|
111
|
+
searchWorkflows(options = {}) {
|
|
112
|
+
const all = this.workflowRepository.list();
|
|
113
|
+
const keyword = options.keyword?.trim().toLowerCase();
|
|
114
|
+
const tags = options.tags ?? [];
|
|
115
|
+
let filtered = all;
|
|
116
|
+
if (tags.length > 0) {
|
|
117
|
+
filtered = filtered.filter((w) => tags.every((t) => w.tags.includes(t)));
|
|
118
|
+
}
|
|
119
|
+
if (keyword && keyword.length > 0) {
|
|
120
|
+
filtered = filtered.filter((w) => {
|
|
121
|
+
const nameMatch = w.name.toLowerCase().includes(keyword);
|
|
122
|
+
const descMatch = w.description?.toLowerCase().includes(keyword) ?? false;
|
|
123
|
+
return nameMatch || descMatch;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (options.offset !== undefined && options.offset > 0) {
|
|
127
|
+
filtered = filtered.slice(options.offset);
|
|
128
|
+
}
|
|
129
|
+
if (options.limit !== undefined && options.limit > 0) {
|
|
130
|
+
filtered = filtered.slice(0, options.limit);
|
|
131
|
+
}
|
|
132
|
+
return filtered;
|
|
133
|
+
}
|
|
134
|
+
async updateWorkflow(idOrName, updates, options) {
|
|
135
|
+
let workflow = this.workflowRepository.getByName(idOrName);
|
|
136
|
+
if (!workflow) {
|
|
137
|
+
workflow = this.workflowRepository.getById(idOrName);
|
|
138
|
+
}
|
|
139
|
+
if (!workflow) {
|
|
140
|
+
throw new Error(`Workflow not found: ${idOrName}`);
|
|
141
|
+
}
|
|
142
|
+
const updateData = {
|
|
143
|
+
...updates
|
|
144
|
+
};
|
|
145
|
+
if (options?.tags !== undefined) {
|
|
146
|
+
updateData.tags = options.tags;
|
|
147
|
+
}
|
|
148
|
+
const updated = this.workflowRepository.update(workflow.id, updateData);
|
|
149
|
+
if (!updated) {
|
|
150
|
+
throw new Error(`Failed to update workflow: ${idOrName}`);
|
|
151
|
+
}
|
|
152
|
+
return updated;
|
|
153
|
+
}
|
|
154
|
+
async deleteWorkflow(idOrName) {
|
|
155
|
+
let workflow = this.workflowRepository.getByName(idOrName);
|
|
156
|
+
if (!workflow) {
|
|
157
|
+
workflow = this.workflowRepository.getById(idOrName);
|
|
158
|
+
}
|
|
159
|
+
if (!workflow) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
const tagsToDecrement = [...workflow.tags];
|
|
163
|
+
return this.workflowRepository.transaction(() => {
|
|
164
|
+
const deleted = this.workflowRepository.delete(workflow.id);
|
|
165
|
+
if (deleted && this.tagRepository) {
|
|
166
|
+
this.tagRepository.decrementMany(tagsToDecrement);
|
|
167
|
+
}
|
|
168
|
+
return deleted;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
async listWorkflowsWithTags(options) {
|
|
172
|
+
const workflows = this.workflowRepository.list(options);
|
|
173
|
+
const availableTags = this.tagRepository ? this.tagRepository.list().map((t) => ({ name: t.name, count: t.usage_count })) : [];
|
|
174
|
+
const total = this.workflowRepository.countAll(options);
|
|
175
|
+
return {
|
|
176
|
+
workflows,
|
|
177
|
+
available_tags: availableTags,
|
|
178
|
+
total
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
async searchWorkflowsWithTags(options = {}) {
|
|
182
|
+
const workflows = this.searchWorkflows(options);
|
|
183
|
+
const availableTags = this.tagRepository ? this.tagRepository.list().map((t) => ({ name: t.name, count: t.usage_count })) : [];
|
|
184
|
+
const total = this.workflowRepository.countAll({
|
|
185
|
+
tag: options.tags?.length === 1 ? options.tags[0] : undefined,
|
|
186
|
+
tags: options.tags && options.tags.length > 1 ? options.tags : undefined,
|
|
187
|
+
search: options.keyword
|
|
188
|
+
});
|
|
189
|
+
return {
|
|
190
|
+
workflows,
|
|
191
|
+
available_tags: availableTags,
|
|
192
|
+
total,
|
|
193
|
+
keyword: options.keyword,
|
|
194
|
+
tags: options.tags
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async listTags(options) {
|
|
198
|
+
const sortBy = options?.sortBy ?? "usage_count";
|
|
199
|
+
const order = options?.order ?? "desc";
|
|
200
|
+
const limit = options?.limit ?? 100;
|
|
201
|
+
const list = this.tagRepository ? this.tagRepository.list({ sortBy, order, limit }) : [];
|
|
202
|
+
return { tags: list, total: list.length };
|
|
203
|
+
}
|
|
204
|
+
getTag(name) {
|
|
205
|
+
if (!this.tagRepository)
|
|
206
|
+
return;
|
|
207
|
+
return this.tagRepository.get(name);
|
|
208
|
+
}
|
|
209
|
+
async runWorkflow(idOrNameOrDefinition, input, options) {
|
|
210
|
+
let workflow = null;
|
|
211
|
+
let definition = null;
|
|
212
|
+
if (typeof idOrNameOrDefinition === "string") {
|
|
213
|
+
workflow = this.getWorkflow(idOrNameOrDefinition);
|
|
214
|
+
if (!workflow) {
|
|
215
|
+
throw new Error(`Workflow not found: ${idOrNameOrDefinition}`);
|
|
216
|
+
}
|
|
217
|
+
definition = workflow.definition;
|
|
218
|
+
} else {
|
|
219
|
+
definition = idOrNameOrDefinition;
|
|
220
|
+
workflow = this.getWorkflowByName(definition.name);
|
|
221
|
+
if (!workflow) {
|
|
222
|
+
workflow = {
|
|
223
|
+
id: `wf_inline_${definition.name}`,
|
|
224
|
+
name: definition.name,
|
|
225
|
+
version: definition.version,
|
|
226
|
+
description: definition.description,
|
|
227
|
+
definition,
|
|
228
|
+
config: definition.config || {},
|
|
229
|
+
metadata: definition.metadata || {},
|
|
230
|
+
tags: definition.metadata?.tags || [],
|
|
231
|
+
createdAt: new Date,
|
|
232
|
+
updatedAt: new Date
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const engine = this.engineFactory(workflow, {
|
|
237
|
+
...options,
|
|
238
|
+
input
|
|
239
|
+
});
|
|
240
|
+
const result = await engine.runWorkflow(workflow, {
|
|
241
|
+
...options,
|
|
242
|
+
input
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
...result,
|
|
246
|
+
runId: result.runId || result.sessionId || "",
|
|
247
|
+
sessionId: result.sessionId || result.runId || ""
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async runWorkflowByName(name, input, options) {
|
|
251
|
+
const workflow = this.getWorkflowByName(name);
|
|
252
|
+
if (!workflow) {
|
|
253
|
+
throw new Error(`Workflow not found: ${name}`);
|
|
254
|
+
}
|
|
255
|
+
return this.runWorkflow(name, input, options);
|
|
256
|
+
}
|
|
257
|
+
async stopRun(sessionId, reason) {
|
|
258
|
+
const session = await this.sessionComponent?.get(sessionId);
|
|
259
|
+
if (!session) {
|
|
260
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
261
|
+
}
|
|
262
|
+
const metadata = session.metadata;
|
|
263
|
+
if (metadata.type !== "workflow") {
|
|
264
|
+
throw new Error(`Session is not a workflow session: ${sessionId}`);
|
|
265
|
+
}
|
|
266
|
+
const workflow = this.getWorkflowByName(metadata.workflowName);
|
|
267
|
+
if (!workflow) {
|
|
268
|
+
throw new Error(`Workflow not found: ${metadata.workflowName}`);
|
|
269
|
+
}
|
|
270
|
+
const engine = this.engineFactory(workflow);
|
|
271
|
+
await engine.stop(sessionId, reason);
|
|
272
|
+
}
|
|
273
|
+
async getSession(sessionId) {
|
|
274
|
+
if (!this.sessionComponent) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const session = await this.sessionComponent.get(sessionId);
|
|
278
|
+
return session ?? null;
|
|
279
|
+
}
|
|
280
|
+
async listSessions(options) {
|
|
281
|
+
if (!this.sessionComponent?.list) {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
return this.sessionComponent.list(options);
|
|
285
|
+
}
|
|
286
|
+
async getSessionMessages(sessionId) {
|
|
287
|
+
if (!this.sessionComponent) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
return this.sessionComponent.getMessages(sessionId);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
__legacyDecorateClassTS([
|
|
294
|
+
TracedAs("workflow.service.createWorkflow", {
|
|
295
|
+
recordParams: true,
|
|
296
|
+
recordResult: false,
|
|
297
|
+
log: false
|
|
298
|
+
})
|
|
299
|
+
], WorkflowService.prototype, "createWorkflow", null);
|
|
300
|
+
__legacyDecorateClassTS([
|
|
301
|
+
TracedAs("workflow.service.deleteWorkflow", {
|
|
302
|
+
recordParams: true,
|
|
303
|
+
recordResult: false,
|
|
304
|
+
log: false
|
|
305
|
+
})
|
|
306
|
+
], WorkflowService.prototype, "deleteWorkflow", null);
|
|
307
|
+
__legacyDecorateClassTS([
|
|
308
|
+
TracedAs("workflow.service.listWorkflowsWithTags", {
|
|
309
|
+
recordParams: true,
|
|
310
|
+
recordResult: false,
|
|
311
|
+
log: false
|
|
312
|
+
})
|
|
313
|
+
], WorkflowService.prototype, "listWorkflowsWithTags", null);
|
|
314
|
+
__legacyDecorateClassTS([
|
|
315
|
+
TracedAs("workflow.service.searchWorkflowsWithTags", {
|
|
316
|
+
recordParams: true,
|
|
317
|
+
recordResult: false,
|
|
318
|
+
log: false
|
|
319
|
+
})
|
|
320
|
+
], WorkflowService.prototype, "searchWorkflowsWithTags", null);
|
|
321
|
+
__legacyDecorateClassTS([
|
|
322
|
+
TracedAs("workflow.service.listTags", {
|
|
323
|
+
recordParams: true,
|
|
324
|
+
recordResult: false,
|
|
325
|
+
log: false
|
|
326
|
+
})
|
|
327
|
+
], WorkflowService.prototype, "listTags", null);
|
|
328
|
+
__legacyDecorateClassTS([
|
|
329
|
+
TracedAs("workflow.service.getTag", {
|
|
330
|
+
recordParams: true,
|
|
331
|
+
recordResult: false,
|
|
332
|
+
log: false
|
|
333
|
+
})
|
|
334
|
+
], WorkflowService.prototype, "getTag", null);
|
|
335
|
+
|
|
336
|
+
export { TAG_MIN_COUNT, TAG_MAX_COUNT, WorkflowService };
|
package/package.json
CHANGED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// src/env/workflow/service/workflow-service.ts
|
|
2
|
-
class WorkflowService {
|
|
3
|
-
workflowRepository;
|
|
4
|
-
engineFactory;
|
|
5
|
-
sessionComponent;
|
|
6
|
-
constructor(workflowRepository, engineFactory, sessionComponent) {
|
|
7
|
-
this.workflowRepository = workflowRepository;
|
|
8
|
-
this.engineFactory = engineFactory;
|
|
9
|
-
this.sessionComponent = sessionComponent;
|
|
10
|
-
}
|
|
11
|
-
async createWorkflow(definition, options) {
|
|
12
|
-
const existing = this.workflowRepository.getByName(definition.name);
|
|
13
|
-
if (existing) {
|
|
14
|
-
if (options?.force !== true) {
|
|
15
|
-
throw new Error(`Workflow already exists: ${definition.name}`);
|
|
16
|
-
}
|
|
17
|
-
return this.workflowRepository.update(existing.id, {
|
|
18
|
-
name: definition.name,
|
|
19
|
-
version: definition.version,
|
|
20
|
-
description: definition.description || existing.description,
|
|
21
|
-
definition,
|
|
22
|
-
config: definition.config || {},
|
|
23
|
-
metadata: {
|
|
24
|
-
...options.metadata || definition.metadata || existing.metadata || {},
|
|
25
|
-
taskId: options.taskId || definition.metadata?.taskId || existing.metadata?.taskId
|
|
26
|
-
},
|
|
27
|
-
tags: options.tags || definition.metadata?.tags || []
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
const workflow = {
|
|
31
|
-
name: definition.name,
|
|
32
|
-
version: definition.version,
|
|
33
|
-
description: definition.description,
|
|
34
|
-
definition,
|
|
35
|
-
config: definition.config || {},
|
|
36
|
-
metadata: {
|
|
37
|
-
...options?.metadata || definition.metadata || {},
|
|
38
|
-
taskId: options?.taskId || definition.metadata?.taskId
|
|
39
|
-
},
|
|
40
|
-
tags: options?.tags || definition.metadata?.tags || []
|
|
41
|
-
};
|
|
42
|
-
return this.workflowRepository.create(workflow);
|
|
43
|
-
}
|
|
44
|
-
getWorkflow(idOrName) {
|
|
45
|
-
let workflow = this.workflowRepository.getById(idOrName);
|
|
46
|
-
if (workflow)
|
|
47
|
-
return workflow;
|
|
48
|
-
return this.workflowRepository.getByName(idOrName);
|
|
49
|
-
}
|
|
50
|
-
getWorkflowById(id) {
|
|
51
|
-
return this.workflowRepository.getById(id);
|
|
52
|
-
}
|
|
53
|
-
getWorkflowByName(name) {
|
|
54
|
-
return this.workflowRepository.getByName(name);
|
|
55
|
-
}
|
|
56
|
-
listWorkflows(options) {
|
|
57
|
-
return this.workflowRepository.list(options);
|
|
58
|
-
}
|
|
59
|
-
async updateWorkflow(idOrName, updates, options) {
|
|
60
|
-
let workflow = this.workflowRepository.getByName(idOrName);
|
|
61
|
-
if (!workflow) {
|
|
62
|
-
workflow = this.workflowRepository.getById(idOrName);
|
|
63
|
-
}
|
|
64
|
-
if (!workflow) {
|
|
65
|
-
throw new Error(`Workflow not found: ${idOrName}`);
|
|
66
|
-
}
|
|
67
|
-
const updateData = {
|
|
68
|
-
...updates
|
|
69
|
-
};
|
|
70
|
-
if (options?.tags !== undefined) {
|
|
71
|
-
updateData.tags = options.tags;
|
|
72
|
-
}
|
|
73
|
-
const updated = this.workflowRepository.update(workflow.id, updateData);
|
|
74
|
-
if (!updated) {
|
|
75
|
-
throw new Error(`Failed to update workflow: ${idOrName}`);
|
|
76
|
-
}
|
|
77
|
-
return updated;
|
|
78
|
-
}
|
|
79
|
-
async deleteWorkflow(idOrName) {
|
|
80
|
-
let workflow = this.workflowRepository.getByName(idOrName);
|
|
81
|
-
if (!workflow) {
|
|
82
|
-
workflow = this.workflowRepository.getById(idOrName);
|
|
83
|
-
}
|
|
84
|
-
if (!workflow) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
return this.workflowRepository.delete(workflow.id);
|
|
88
|
-
}
|
|
89
|
-
async runWorkflow(idOrNameOrDefinition, input, options) {
|
|
90
|
-
let workflow = null;
|
|
91
|
-
let definition = null;
|
|
92
|
-
if (typeof idOrNameOrDefinition === "string") {
|
|
93
|
-
workflow = this.getWorkflow(idOrNameOrDefinition);
|
|
94
|
-
if (!workflow) {
|
|
95
|
-
throw new Error(`Workflow not found: ${idOrNameOrDefinition}`);
|
|
96
|
-
}
|
|
97
|
-
definition = workflow.definition;
|
|
98
|
-
} else {
|
|
99
|
-
definition = idOrNameOrDefinition;
|
|
100
|
-
workflow = this.getWorkflowByName(definition.name);
|
|
101
|
-
if (!workflow) {
|
|
102
|
-
workflow = {
|
|
103
|
-
id: `wf_inline_${definition.name}`,
|
|
104
|
-
name: definition.name,
|
|
105
|
-
version: definition.version,
|
|
106
|
-
description: definition.description,
|
|
107
|
-
definition,
|
|
108
|
-
config: definition.config || {},
|
|
109
|
-
metadata: definition.metadata || {},
|
|
110
|
-
tags: definition.metadata?.tags || [],
|
|
111
|
-
createdAt: new Date,
|
|
112
|
-
updatedAt: new Date
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const engine = this.engineFactory(workflow, {
|
|
117
|
-
...options,
|
|
118
|
-
input
|
|
119
|
-
});
|
|
120
|
-
const result = await engine.runWorkflow(workflow, {
|
|
121
|
-
...options,
|
|
122
|
-
input
|
|
123
|
-
});
|
|
124
|
-
return {
|
|
125
|
-
...result,
|
|
126
|
-
runId: result.runId || result.sessionId || "",
|
|
127
|
-
sessionId: result.sessionId || result.runId || ""
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
async runWorkflowByName(name, input, options) {
|
|
131
|
-
const workflow = this.getWorkflowByName(name);
|
|
132
|
-
if (!workflow) {
|
|
133
|
-
throw new Error(`Workflow not found: ${name}`);
|
|
134
|
-
}
|
|
135
|
-
return this.runWorkflow(name, input, options);
|
|
136
|
-
}
|
|
137
|
-
async stopRun(sessionId, reason) {
|
|
138
|
-
const session = await this.sessionComponent?.get(sessionId);
|
|
139
|
-
if (!session) {
|
|
140
|
-
throw new Error(`Session not found: ${sessionId}`);
|
|
141
|
-
}
|
|
142
|
-
const metadata = session.metadata;
|
|
143
|
-
if (metadata.type !== "workflow") {
|
|
144
|
-
throw new Error(`Session is not a workflow session: ${sessionId}`);
|
|
145
|
-
}
|
|
146
|
-
const workflow = this.getWorkflowByName(metadata.workflowName);
|
|
147
|
-
if (!workflow) {
|
|
148
|
-
throw new Error(`Workflow not found: ${metadata.workflowName}`);
|
|
149
|
-
}
|
|
150
|
-
const engine = this.engineFactory(workflow);
|
|
151
|
-
await engine.stop(sessionId, reason);
|
|
152
|
-
}
|
|
153
|
-
async getSession(sessionId) {
|
|
154
|
-
if (!this.sessionComponent) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
const session = await this.sessionComponent.get(sessionId);
|
|
158
|
-
return session ?? null;
|
|
159
|
-
}
|
|
160
|
-
async listSessions(options) {
|
|
161
|
-
if (!this.sessionComponent?.list) {
|
|
162
|
-
return [];
|
|
163
|
-
}
|
|
164
|
-
return this.sessionComponent.list(options);
|
|
165
|
-
}
|
|
166
|
-
async getSessionMessages(sessionId) {
|
|
167
|
-
if (!this.sessionComponent) {
|
|
168
|
-
return [];
|
|
169
|
-
}
|
|
170
|
-
return this.sessionComponent.getMessages(sessionId);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
// src/env/workflow/service/registry.ts
|
|
174
|
-
class WorkflowServiceRegistry {
|
|
175
|
-
static instance;
|
|
176
|
-
service = null;
|
|
177
|
-
constructor() {}
|
|
178
|
-
static getInstance() {
|
|
179
|
-
if (!WorkflowServiceRegistry.instance) {
|
|
180
|
-
WorkflowServiceRegistry.instance = new WorkflowServiceRegistry;
|
|
181
|
-
}
|
|
182
|
-
return WorkflowServiceRegistry.instance;
|
|
183
|
-
}
|
|
184
|
-
register(service) {
|
|
185
|
-
this.service = service;
|
|
186
|
-
}
|
|
187
|
-
get() {
|
|
188
|
-
if (!this.service) {
|
|
189
|
-
throw new Error("WorkflowService is not registered. Call register() first.");
|
|
190
|
-
}
|
|
191
|
-
return this.service;
|
|
192
|
-
}
|
|
193
|
-
isRegistered() {
|
|
194
|
-
return this.service !== null;
|
|
195
|
-
}
|
|
196
|
-
reset() {
|
|
197
|
-
this.service = null;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
function getWorkflowService() {
|
|
201
|
-
return WorkflowServiceRegistry.getInstance().get();
|
|
202
|
-
}
|
|
203
|
-
function registerWorkflowService(service) {
|
|
204
|
-
WorkflowServiceRegistry.getInstance().register(service);
|
|
205
|
-
}
|
|
206
|
-
export { WorkflowService, WorkflowServiceRegistry, getWorkflowService, registerWorkflowService };
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createLogger,
|
|
3
|
-
init_logger
|
|
4
|
-
} from "./roy-agent-core-shme7set.js";
|
|
5
|
-
import {
|
|
6
|
-
__require
|
|
7
|
-
} from "./roy-agent-core-fs0mn2jk.js";
|
|
8
|
-
|
|
9
|
-
// src/env/workflow/storage/sqlite.ts
|
|
10
|
-
init_logger();
|
|
11
|
-
import { join } from "path";
|
|
12
|
-
var logger = createLogger("workflow:sqlite");
|
|
13
|
-
function getDatabaseClass() {
|
|
14
|
-
if (typeof Bun !== "undefined") {
|
|
15
|
-
const { Database } = __require("bun:sqlite");
|
|
16
|
-
return Database;
|
|
17
|
-
} else {
|
|
18
|
-
return __require("better-sqlite3");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function createCompatibleDatabase(path) {
|
|
22
|
-
const Database = getDatabaseClass();
|
|
23
|
-
const db = new Database(path);
|
|
24
|
-
if (typeof db.run !== "function") {
|
|
25
|
-
db.run = function(sql, ...params) {
|
|
26
|
-
if (params.length > 0) {
|
|
27
|
-
return this.prepare(sql).run(...params);
|
|
28
|
-
} else {
|
|
29
|
-
return this.exec(sql);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
return db;
|
|
34
|
-
}
|
|
35
|
-
function getDefaultDataDir() {
|
|
36
|
-
const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
|
|
37
|
-
return join(home, ".local", "share", "roy-agent");
|
|
38
|
-
}
|
|
39
|
-
function getDatabasePath() {
|
|
40
|
-
return join(getDefaultDataDir(), "workflows.db");
|
|
41
|
-
}
|
|
42
|
-
var dbInstance = null;
|
|
43
|
-
function getDatabase(path) {
|
|
44
|
-
if (!dbInstance) {
|
|
45
|
-
const dbPath = path ?? getDatabasePath();
|
|
46
|
-
const dbDir = dbPath.substring(0, dbPath.lastIndexOf("/"));
|
|
47
|
-
if (dbDir && dbDir !== "/") {
|
|
48
|
-
try {
|
|
49
|
-
const { mkdirSync } = __require("fs");
|
|
50
|
-
mkdirSync(dbDir, { recursive: true });
|
|
51
|
-
} catch {}
|
|
52
|
-
}
|
|
53
|
-
dbInstance = createCompatibleDatabase(dbPath);
|
|
54
|
-
try {
|
|
55
|
-
if (typeof dbInstance.pragma === "function") {
|
|
56
|
-
dbInstance.pragma("journal_mode = WAL");
|
|
57
|
-
}
|
|
58
|
-
} catch {
|
|
59
|
-
logger?.debug("WAL mode not available, using default journal mode");
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return dbInstance;
|
|
63
|
-
}
|
|
64
|
-
function closeDatabase() {
|
|
65
|
-
if (dbInstance) {
|
|
66
|
-
dbInstance.close();
|
|
67
|
-
dbInstance = null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
function initializeTables() {
|
|
71
|
-
const db = getDatabase();
|
|
72
|
-
db.exec(`
|
|
73
|
-
CREATE TABLE IF NOT EXISTS workflows (
|
|
74
|
-
id TEXT PRIMARY KEY,
|
|
75
|
-
name TEXT NOT NULL UNIQUE,
|
|
76
|
-
version TEXT NOT NULL DEFAULT '1.0',
|
|
77
|
-
description TEXT,
|
|
78
|
-
definition TEXT NOT NULL,
|
|
79
|
-
config TEXT NOT NULL DEFAULT '{}',
|
|
80
|
-
metadata TEXT NOT NULL DEFAULT '{}',
|
|
81
|
-
tags TEXT NOT NULL DEFAULT '[]',
|
|
82
|
-
task_id INTEGER,
|
|
83
|
-
created_at TEXT NOT NULL,
|
|
84
|
-
updated_at TEXT NOT NULL
|
|
85
|
-
)
|
|
86
|
-
`);
|
|
87
|
-
db.exec(`
|
|
88
|
-
CREATE INDEX IF NOT EXISTS idx_workflows_name ON workflows(name);
|
|
89
|
-
CREATE INDEX IF NOT EXISTS idx_workflows_tags ON workflows(tags);
|
|
90
|
-
CREATE INDEX IF NOT EXISTS idx_workflows_task_id ON workflows(task_id);
|
|
91
|
-
`);
|
|
92
|
-
}
|
|
93
|
-
function resetDatabase() {
|
|
94
|
-
closeDatabase();
|
|
95
|
-
dbInstance = null;
|
|
96
|
-
}
|
|
97
|
-
function initDatabase(path) {
|
|
98
|
-
const db = getDatabase(path);
|
|
99
|
-
initializeTables();
|
|
100
|
-
return db;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export { getDatabasePath, getDatabase, closeDatabase, initializeTables, resetDatabase, initDatabase };
|