@hasna/microservices 0.0.5 → 0.0.7
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/bin/index.js +9 -1
- package/bin/mcp.js +9 -1
- package/dist/index.js +9 -1
- package/microservices/microservice-company/package.json +27 -0
- package/microservices/microservice-company/src/cli/index.ts +1126 -0
- package/microservices/microservice-company/src/db/company.ts +854 -0
- package/microservices/microservice-company/src/db/database.ts +93 -0
- package/microservices/microservice-company/src/db/migrations.ts +214 -0
- package/microservices/microservice-company/src/db/workflow-migrations.ts +44 -0
- package/microservices/microservice-company/src/index.ts +60 -0
- package/microservices/microservice-company/src/lib/audit.ts +168 -0
- package/microservices/microservice-company/src/lib/finance.ts +299 -0
- package/microservices/microservice-company/src/lib/settings.ts +85 -0
- package/microservices/microservice-company/src/lib/workflows.ts +698 -0
- package/microservices/microservice-company/src/mcp/index.ts +991 -0
- package/microservices/microservice-domains/src/cli/index.ts +420 -0
- package/microservices/microservice-domains/src/lib/brandsight.ts +350 -0
- package/microservices/microservice-domains/src/lib/godaddy.ts +338 -0
- package/microservices/microservice-domains/src/lib/namecheap.ts +262 -0
- package/microservices/microservice-domains/src/lib/registrar.ts +355 -0
- package/microservices/microservice-domains/src/mcp/index.ts +245 -0
- package/package.json +1 -1
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workflow engine — orchestrates multi-service automation for microservice-company
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { join, dirname, resolve } from "node:path";
|
|
7
|
+
import { execFile } from "node:child_process";
|
|
8
|
+
|
|
9
|
+
// ── Types ──────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
export interface WorkflowStep {
|
|
12
|
+
service: string;
|
|
13
|
+
action: string;
|
|
14
|
+
args?: Record<string, string>;
|
|
15
|
+
on_failure?: "stop" | "continue" | `retry:${number}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Workflow {
|
|
19
|
+
id: string;
|
|
20
|
+
org_id: string | null;
|
|
21
|
+
name: string;
|
|
22
|
+
trigger_event: string;
|
|
23
|
+
steps: WorkflowStep[];
|
|
24
|
+
enabled: boolean;
|
|
25
|
+
last_run_at: string | null;
|
|
26
|
+
run_count: number;
|
|
27
|
+
metadata: Record<string, unknown>;
|
|
28
|
+
created_at: string;
|
|
29
|
+
updated_at: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface WorkflowRow {
|
|
33
|
+
id: string;
|
|
34
|
+
org_id: string | null;
|
|
35
|
+
name: string;
|
|
36
|
+
trigger_event: string;
|
|
37
|
+
steps: string;
|
|
38
|
+
enabled: number;
|
|
39
|
+
last_run_at: string | null;
|
|
40
|
+
run_count: number;
|
|
41
|
+
metadata: string;
|
|
42
|
+
created_at: string;
|
|
43
|
+
updated_at: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface StepResult {
|
|
47
|
+
step_index: number;
|
|
48
|
+
service: string;
|
|
49
|
+
action: string;
|
|
50
|
+
status: "success" | "failed" | "skipped";
|
|
51
|
+
output: string;
|
|
52
|
+
duration_ms: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface WorkflowRun {
|
|
56
|
+
id: string;
|
|
57
|
+
workflow_id: string;
|
|
58
|
+
trigger_data: Record<string, unknown> | null;
|
|
59
|
+
status: "running" | "completed" | "failed" | "partial";
|
|
60
|
+
steps_completed: number;
|
|
61
|
+
steps_total: number;
|
|
62
|
+
results: StepResult[];
|
|
63
|
+
started_at: string;
|
|
64
|
+
completed_at: string | null;
|
|
65
|
+
error: string | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface WorkflowRunRow {
|
|
69
|
+
id: string;
|
|
70
|
+
workflow_id: string;
|
|
71
|
+
trigger_data: string | null;
|
|
72
|
+
status: string;
|
|
73
|
+
steps_completed: number;
|
|
74
|
+
steps_total: number;
|
|
75
|
+
results: string;
|
|
76
|
+
started_at: string;
|
|
77
|
+
completed_at: string | null;
|
|
78
|
+
error: string | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface CreateWorkflowInput {
|
|
82
|
+
name: string;
|
|
83
|
+
trigger_event: string;
|
|
84
|
+
steps: WorkflowStep[];
|
|
85
|
+
org_id?: string;
|
|
86
|
+
metadata?: Record<string, unknown>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface UpdateWorkflowInput {
|
|
90
|
+
name?: string;
|
|
91
|
+
trigger_event?: string;
|
|
92
|
+
steps?: WorkflowStep[];
|
|
93
|
+
org_id?: string;
|
|
94
|
+
metadata?: Record<string, unknown>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ListWorkflowsOptions {
|
|
98
|
+
org_id?: string;
|
|
99
|
+
trigger_event?: string;
|
|
100
|
+
enabled?: boolean;
|
|
101
|
+
limit?: number;
|
|
102
|
+
offset?: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── Database access ────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
// The database getter is injected to avoid circular dependency with the
|
|
108
|
+
// other agent's database.ts. Tests provide their own DB via setDatabase().
|
|
109
|
+
type DatabaseGetter = () => import("bun:sqlite").Database;
|
|
110
|
+
|
|
111
|
+
let _getDb: DatabaseGetter | null = null;
|
|
112
|
+
|
|
113
|
+
export function setDatabase(getter: DatabaseGetter): void {
|
|
114
|
+
_getDb = getter;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function db() {
|
|
118
|
+
if (!_getDb) {
|
|
119
|
+
// Lazy import — the other agent's database.ts will exist at runtime
|
|
120
|
+
try {
|
|
121
|
+
const mod = require("../db/database.js");
|
|
122
|
+
_getDb = mod.getDatabase;
|
|
123
|
+
} catch {
|
|
124
|
+
throw new Error(
|
|
125
|
+
"No database configured. Call setDatabase() or ensure ../db/database.ts exists."
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return _getDb();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── Row converters ─────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
function rowToWorkflow(row: WorkflowRow): Workflow {
|
|
135
|
+
return {
|
|
136
|
+
...row,
|
|
137
|
+
steps: JSON.parse(row.steps || "[]"),
|
|
138
|
+
enabled: row.enabled === 1,
|
|
139
|
+
metadata: JSON.parse(row.metadata || "{}"),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function rowToWorkflowRun(row: WorkflowRunRow): WorkflowRun {
|
|
144
|
+
return {
|
|
145
|
+
...row,
|
|
146
|
+
trigger_data: row.trigger_data ? JSON.parse(row.trigger_data) : null,
|
|
147
|
+
status: row.status as WorkflowRun["status"],
|
|
148
|
+
results: JSON.parse(row.results || "[]"),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ── CRUD ───────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
export function createWorkflow(input: CreateWorkflowInput): Workflow {
|
|
155
|
+
const id = crypto.randomUUID();
|
|
156
|
+
const steps = JSON.stringify(input.steps);
|
|
157
|
+
const metadata = JSON.stringify(input.metadata || {});
|
|
158
|
+
|
|
159
|
+
db()
|
|
160
|
+
.prepare(
|
|
161
|
+
`INSERT INTO workflows (id, org_id, name, trigger_event, steps, metadata)
|
|
162
|
+
VALUES (?, ?, ?, ?, ?, ?)`
|
|
163
|
+
)
|
|
164
|
+
.run(id, input.org_id || null, input.name, input.trigger_event, steps, metadata);
|
|
165
|
+
|
|
166
|
+
return getWorkflow(id)!;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function getWorkflow(id: string): Workflow | null {
|
|
170
|
+
const row = db()
|
|
171
|
+
.prepare("SELECT * FROM workflows WHERE id = ?")
|
|
172
|
+
.get(id) as WorkflowRow | null;
|
|
173
|
+
return row ? rowToWorkflow(row) : null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function listWorkflows(options: ListWorkflowsOptions = {}): Workflow[] {
|
|
177
|
+
const conditions: string[] = [];
|
|
178
|
+
const params: unknown[] = [];
|
|
179
|
+
|
|
180
|
+
if (options.org_id !== undefined) {
|
|
181
|
+
conditions.push("org_id = ?");
|
|
182
|
+
params.push(options.org_id);
|
|
183
|
+
}
|
|
184
|
+
if (options.trigger_event !== undefined) {
|
|
185
|
+
conditions.push("trigger_event = ?");
|
|
186
|
+
params.push(options.trigger_event);
|
|
187
|
+
}
|
|
188
|
+
if (options.enabled !== undefined) {
|
|
189
|
+
conditions.push("enabled = ?");
|
|
190
|
+
params.push(options.enabled ? 1 : 0);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let sql = "SELECT * FROM workflows";
|
|
194
|
+
if (conditions.length > 0) {
|
|
195
|
+
sql += " WHERE " + conditions.join(" AND ");
|
|
196
|
+
}
|
|
197
|
+
sql += " ORDER BY created_at DESC";
|
|
198
|
+
|
|
199
|
+
if (options.limit) {
|
|
200
|
+
sql += " LIMIT ?";
|
|
201
|
+
params.push(options.limit);
|
|
202
|
+
}
|
|
203
|
+
if (options.offset) {
|
|
204
|
+
sql += " OFFSET ?";
|
|
205
|
+
params.push(options.offset);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const rows = db().prepare(sql).all(...params) as WorkflowRow[];
|
|
209
|
+
return rows.map(rowToWorkflow);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function updateWorkflow(id: string, input: UpdateWorkflowInput): Workflow | null {
|
|
213
|
+
const existing = getWorkflow(id);
|
|
214
|
+
if (!existing) return null;
|
|
215
|
+
|
|
216
|
+
const sets: string[] = [];
|
|
217
|
+
const params: unknown[] = [];
|
|
218
|
+
|
|
219
|
+
if (input.name !== undefined) {
|
|
220
|
+
sets.push("name = ?");
|
|
221
|
+
params.push(input.name);
|
|
222
|
+
}
|
|
223
|
+
if (input.trigger_event !== undefined) {
|
|
224
|
+
sets.push("trigger_event = ?");
|
|
225
|
+
params.push(input.trigger_event);
|
|
226
|
+
}
|
|
227
|
+
if (input.steps !== undefined) {
|
|
228
|
+
sets.push("steps = ?");
|
|
229
|
+
params.push(JSON.stringify(input.steps));
|
|
230
|
+
}
|
|
231
|
+
if (input.org_id !== undefined) {
|
|
232
|
+
sets.push("org_id = ?");
|
|
233
|
+
params.push(input.org_id);
|
|
234
|
+
}
|
|
235
|
+
if (input.metadata !== undefined) {
|
|
236
|
+
sets.push("metadata = ?");
|
|
237
|
+
params.push(JSON.stringify(input.metadata));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (sets.length === 0) return existing;
|
|
241
|
+
|
|
242
|
+
sets.push("updated_at = datetime('now')");
|
|
243
|
+
params.push(id);
|
|
244
|
+
|
|
245
|
+
db()
|
|
246
|
+
.prepare(`UPDATE workflows SET ${sets.join(", ")} WHERE id = ?`)
|
|
247
|
+
.run(...params);
|
|
248
|
+
|
|
249
|
+
return getWorkflow(id);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function deleteWorkflow(id: string): boolean {
|
|
253
|
+
const result = db().prepare("DELETE FROM workflows WHERE id = ?").run(id);
|
|
254
|
+
return result.changes > 0;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function enableWorkflow(id: string): Workflow | null {
|
|
258
|
+
const existing = getWorkflow(id);
|
|
259
|
+
if (!existing) return null;
|
|
260
|
+
db()
|
|
261
|
+
.prepare("UPDATE workflows SET enabled = 1, updated_at = datetime('now') WHERE id = ?")
|
|
262
|
+
.run(id);
|
|
263
|
+
return getWorkflow(id);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function disableWorkflow(id: string): Workflow | null {
|
|
267
|
+
const existing = getWorkflow(id);
|
|
268
|
+
if (!existing) return null;
|
|
269
|
+
db()
|
|
270
|
+
.prepare("UPDATE workflows SET enabled = 0, updated_at = datetime('now') WHERE id = ?")
|
|
271
|
+
.run(id);
|
|
272
|
+
return getWorkflow(id);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ── Template resolution ────────────────────────────────────────────────
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Resolve template variables in a string.
|
|
279
|
+
* Supports:
|
|
280
|
+
* {{data.field}} — from trigger data
|
|
281
|
+
* {{data.nested.field}} — dot-path into trigger data
|
|
282
|
+
* {{steps[0].output}} — output from a previous step
|
|
283
|
+
*/
|
|
284
|
+
export function resolveTemplate(
|
|
285
|
+
template: string,
|
|
286
|
+
context: { data?: Record<string, unknown>; steps?: StepResult[] }
|
|
287
|
+
): string {
|
|
288
|
+
return template.replace(/\{\{([^}]+)\}\}/g, (_match, expr: string) => {
|
|
289
|
+
const trimmed = expr.trim();
|
|
290
|
+
|
|
291
|
+
// {{steps[N].output}}
|
|
292
|
+
const stepMatch = trimmed.match(/^steps\[(\d+)\]\.output$/);
|
|
293
|
+
if (stepMatch) {
|
|
294
|
+
const idx = parseInt(stepMatch[1], 10);
|
|
295
|
+
if (context.steps && context.steps[idx]) {
|
|
296
|
+
return context.steps[idx].output;
|
|
297
|
+
}
|
|
298
|
+
return "";
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// {{data.X}} or {{data.X.Y}}
|
|
302
|
+
if (trimmed.startsWith("data.")) {
|
|
303
|
+
const path = trimmed.slice(5).split(".");
|
|
304
|
+
let current: unknown = context.data || {};
|
|
305
|
+
for (const key of path) {
|
|
306
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
307
|
+
return "";
|
|
308
|
+
}
|
|
309
|
+
current = (current as Record<string, unknown>)[key];
|
|
310
|
+
}
|
|
311
|
+
return current !== null && current !== undefined ? String(current) : "";
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return "";
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ── Microservice CLI path resolution ───────────────────────────────────
|
|
319
|
+
|
|
320
|
+
function getMicroservicesDir(): string {
|
|
321
|
+
if (process.env["MICROSERVICES_DIR"]) {
|
|
322
|
+
return process.env["MICROSERVICES_DIR"];
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
let dir = resolve(process.cwd());
|
|
326
|
+
while (true) {
|
|
327
|
+
const candidate = join(dir, ".microservices");
|
|
328
|
+
if (existsSync(candidate)) return candidate;
|
|
329
|
+
const parent = dirname(dir);
|
|
330
|
+
if (parent === dir) break;
|
|
331
|
+
dir = parent;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
|
|
335
|
+
return join(home, ".microservices");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function getMicroserviceCliPath(name: string): string | null {
|
|
339
|
+
const dir = getMicroservicesDir();
|
|
340
|
+
const msName = name.startsWith("microservice-") ? name : `microservice-${name}`;
|
|
341
|
+
|
|
342
|
+
const candidates = [
|
|
343
|
+
join(dir, msName, "src", "cli", "index.ts"),
|
|
344
|
+
join(dir, msName, "cli.ts"),
|
|
345
|
+
join(dir, msName, "src", "index.ts"),
|
|
346
|
+
];
|
|
347
|
+
|
|
348
|
+
for (const candidate of candidates) {
|
|
349
|
+
if (existsSync(candidate)) return candidate;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// ── Step execution ─────────────────────────────────────────────────────
|
|
356
|
+
|
|
357
|
+
async function executeStep(
|
|
358
|
+
step: WorkflowStep,
|
|
359
|
+
context: { data: Record<string, unknown>; steps: StepResult[] }
|
|
360
|
+
): Promise<StepResult> {
|
|
361
|
+
const start = Date.now();
|
|
362
|
+
|
|
363
|
+
// Build CLI args from the step definition
|
|
364
|
+
const cliArgs: string[] = [step.action];
|
|
365
|
+
if (step.args) {
|
|
366
|
+
for (const [key, rawValue] of Object.entries(step.args)) {
|
|
367
|
+
const value = resolveTemplate(rawValue, context);
|
|
368
|
+
cliArgs.push(`--${key}`, value);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const cliPath = getMicroserviceCliPath(step.service);
|
|
373
|
+
if (!cliPath) {
|
|
374
|
+
return {
|
|
375
|
+
step_index: context.steps.length,
|
|
376
|
+
service: step.service,
|
|
377
|
+
action: step.action,
|
|
378
|
+
status: "failed",
|
|
379
|
+
output: `CLI not found for service '${step.service}'`,
|
|
380
|
+
duration_ms: Date.now() - start,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
try {
|
|
385
|
+
const output = await new Promise<string>((resolve, reject) => {
|
|
386
|
+
execFile(
|
|
387
|
+
"bun",
|
|
388
|
+
["run", cliPath, ...cliArgs],
|
|
389
|
+
{
|
|
390
|
+
timeout: 30000,
|
|
391
|
+
env: { ...process.env, MICROSERVICES_DIR: getMicroservicesDir() },
|
|
392
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
393
|
+
},
|
|
394
|
+
(error, stdout, stderr) => {
|
|
395
|
+
if (error) {
|
|
396
|
+
reject(new Error(stderr || stdout || error.message));
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
resolve((stdout || "").trim());
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
return {
|
|
405
|
+
step_index: context.steps.length,
|
|
406
|
+
service: step.service,
|
|
407
|
+
action: step.action,
|
|
408
|
+
status: "success",
|
|
409
|
+
output,
|
|
410
|
+
duration_ms: Date.now() - start,
|
|
411
|
+
};
|
|
412
|
+
} catch (err) {
|
|
413
|
+
return {
|
|
414
|
+
step_index: context.steps.length,
|
|
415
|
+
service: step.service,
|
|
416
|
+
action: step.action,
|
|
417
|
+
status: "failed",
|
|
418
|
+
output: err instanceof Error ? err.message : String(err),
|
|
419
|
+
duration_ms: Date.now() - start,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ── Workflow runner ────────────────────────────────────────────────────
|
|
425
|
+
|
|
426
|
+
export async function runWorkflow(
|
|
427
|
+
id: string,
|
|
428
|
+
triggerData: Record<string, unknown> = {}
|
|
429
|
+
): Promise<WorkflowRun> {
|
|
430
|
+
const workflow = getWorkflow(id);
|
|
431
|
+
if (!workflow) throw new Error(`Workflow '${id}' not found`);
|
|
432
|
+
if (!workflow.enabled) throw new Error(`Workflow '${workflow.name}' is disabled`);
|
|
433
|
+
|
|
434
|
+
// Create workflow_run record
|
|
435
|
+
const runId = crypto.randomUUID();
|
|
436
|
+
db()
|
|
437
|
+
.prepare(
|
|
438
|
+
`INSERT INTO workflow_runs (id, workflow_id, trigger_data, status, steps_total)
|
|
439
|
+
VALUES (?, ?, ?, 'running', ?)`
|
|
440
|
+
)
|
|
441
|
+
.run(runId, id, JSON.stringify(triggerData), workflow.steps.length);
|
|
442
|
+
|
|
443
|
+
const context: { data: Record<string, unknown>; steps: StepResult[] } = {
|
|
444
|
+
data: triggerData,
|
|
445
|
+
steps: [],
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
let finalStatus: WorkflowRun["status"] = "completed";
|
|
449
|
+
let errorMsg: string | null = null;
|
|
450
|
+
|
|
451
|
+
for (let i = 0; i < workflow.steps.length; i++) {
|
|
452
|
+
const step = workflow.steps[i];
|
|
453
|
+
const onFailure = step.on_failure || "stop";
|
|
454
|
+
|
|
455
|
+
let result: StepResult | null = null;
|
|
456
|
+
|
|
457
|
+
if (onFailure.startsWith("retry:")) {
|
|
458
|
+
const maxRetries = parseInt(onFailure.split(":")[1], 10) || 1;
|
|
459
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
460
|
+
result = await executeStep(step, context);
|
|
461
|
+
if (result.status === "success") break;
|
|
462
|
+
}
|
|
463
|
+
} else {
|
|
464
|
+
result = await executeStep(step, context);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
context.steps.push(result!);
|
|
468
|
+
|
|
469
|
+
// Update progress in DB
|
|
470
|
+
db()
|
|
471
|
+
.prepare(
|
|
472
|
+
`UPDATE workflow_runs SET steps_completed = ?, results = ? WHERE id = ?`
|
|
473
|
+
)
|
|
474
|
+
.run(i + 1, JSON.stringify(context.steps), runId);
|
|
475
|
+
|
|
476
|
+
if (result!.status === "failed") {
|
|
477
|
+
if (onFailure === "stop") {
|
|
478
|
+
finalStatus = "failed";
|
|
479
|
+
errorMsg = `Step ${i} (${step.service}.${step.action}) failed: ${result!.output}`;
|
|
480
|
+
// Mark remaining steps as skipped
|
|
481
|
+
for (let j = i + 1; j < workflow.steps.length; j++) {
|
|
482
|
+
context.steps.push({
|
|
483
|
+
step_index: j,
|
|
484
|
+
service: workflow.steps[j].service,
|
|
485
|
+
action: workflow.steps[j].action,
|
|
486
|
+
status: "skipped",
|
|
487
|
+
output: "",
|
|
488
|
+
duration_ms: 0,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
break;
|
|
492
|
+
} else if (onFailure === "continue") {
|
|
493
|
+
finalStatus = "partial";
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Finalize the run
|
|
499
|
+
db()
|
|
500
|
+
.prepare(
|
|
501
|
+
`UPDATE workflow_runs
|
|
502
|
+
SET status = ?, results = ?, completed_at = datetime('now'), error = ?
|
|
503
|
+
WHERE id = ?`
|
|
504
|
+
)
|
|
505
|
+
.run(finalStatus, JSON.stringify(context.steps), errorMsg, runId);
|
|
506
|
+
|
|
507
|
+
// Update workflow stats
|
|
508
|
+
db()
|
|
509
|
+
.prepare(
|
|
510
|
+
`UPDATE workflows
|
|
511
|
+
SET last_run_at = datetime('now'), run_count = run_count + 1, updated_at = datetime('now')
|
|
512
|
+
WHERE id = ?`
|
|
513
|
+
)
|
|
514
|
+
.run(id);
|
|
515
|
+
|
|
516
|
+
return getWorkflowRun(runId)!;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// ── Run queries ────────────────────────────────────────────────────────
|
|
520
|
+
|
|
521
|
+
export function getWorkflowRun(runId: string): WorkflowRun | null {
|
|
522
|
+
const row = db()
|
|
523
|
+
.prepare("SELECT * FROM workflow_runs WHERE id = ?")
|
|
524
|
+
.get(runId) as WorkflowRunRow | null;
|
|
525
|
+
return row ? rowToWorkflowRun(row) : null;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
export function getWorkflowRuns(workflowId: string, limit: number = 20): WorkflowRun[] {
|
|
529
|
+
const rows = db()
|
|
530
|
+
.prepare(
|
|
531
|
+
"SELECT * FROM workflow_runs WHERE workflow_id = ? ORDER BY started_at DESC LIMIT ?"
|
|
532
|
+
)
|
|
533
|
+
.all(workflowId, limit) as WorkflowRunRow[];
|
|
534
|
+
return rows.map(rowToWorkflowRun);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// ── Preset workflows ───────────────────────────────────────────────────
|
|
538
|
+
|
|
539
|
+
export interface PresetWorkflow {
|
|
540
|
+
name: string;
|
|
541
|
+
trigger_event: string;
|
|
542
|
+
steps: WorkflowStep[];
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export function getPresetWorkflows(): PresetWorkflow[] {
|
|
546
|
+
return [
|
|
547
|
+
{
|
|
548
|
+
name: "Deal Won",
|
|
549
|
+
trigger_event: "deal.won",
|
|
550
|
+
steps: [
|
|
551
|
+
{
|
|
552
|
+
service: "invoices",
|
|
553
|
+
action: "create",
|
|
554
|
+
args: {
|
|
555
|
+
client: "{{data.customer_name}}",
|
|
556
|
+
amount: "{{data.deal_amount}}",
|
|
557
|
+
description: "Invoice for deal {{data.deal_name}}",
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
service: "contracts",
|
|
562
|
+
action: "create",
|
|
563
|
+
args: {
|
|
564
|
+
title: "Contract for {{data.customer_name}}",
|
|
565
|
+
type: "service-agreement",
|
|
566
|
+
value: "{{data.deal_amount}}",
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
service: "notes",
|
|
571
|
+
action: "create",
|
|
572
|
+
args: {
|
|
573
|
+
title: "Deal won: {{data.deal_name}}",
|
|
574
|
+
content: "Deal closed with {{data.customer_name}} for {{data.deal_amount}}",
|
|
575
|
+
},
|
|
576
|
+
},
|
|
577
|
+
],
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
name: "Invoice Overdue",
|
|
581
|
+
trigger_event: "invoice.overdue",
|
|
582
|
+
steps: [
|
|
583
|
+
{
|
|
584
|
+
service: "notes",
|
|
585
|
+
action: "create",
|
|
586
|
+
args: {
|
|
587
|
+
title: "Overdue reminder: {{data.invoice_id}}",
|
|
588
|
+
content: "Invoice {{data.invoice_id}} for {{data.customer_name}} is overdue by {{data.days_overdue}} days",
|
|
589
|
+
},
|
|
590
|
+
on_failure: "continue",
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
service: "calendar",
|
|
594
|
+
action: "create",
|
|
595
|
+
args: {
|
|
596
|
+
title: "Follow up on overdue invoice {{data.invoice_id}}",
|
|
597
|
+
description: "Contact {{data.customer_name}} about overdue payment",
|
|
598
|
+
},
|
|
599
|
+
on_failure: "continue",
|
|
600
|
+
},
|
|
601
|
+
{
|
|
602
|
+
service: "notes",
|
|
603
|
+
action: "create",
|
|
604
|
+
args: {
|
|
605
|
+
title: "Audit: overdue invoice {{data.invoice_id}}",
|
|
606
|
+
content: "Logged overdue event for {{data.customer_name}}, amount: {{data.amount}}",
|
|
607
|
+
},
|
|
608
|
+
},
|
|
609
|
+
],
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
name: "Payroll Close",
|
|
613
|
+
trigger_event: "payroll.close",
|
|
614
|
+
steps: [
|
|
615
|
+
{
|
|
616
|
+
service: "payroll",
|
|
617
|
+
action: "process",
|
|
618
|
+
args: {
|
|
619
|
+
period: "{{data.period}}",
|
|
620
|
+
org_id: "{{data.org_id}}",
|
|
621
|
+
},
|
|
622
|
+
},
|
|
623
|
+
{
|
|
624
|
+
service: "bookkeeping",
|
|
625
|
+
action: "create",
|
|
626
|
+
args: {
|
|
627
|
+
type: "expense",
|
|
628
|
+
category: "payroll",
|
|
629
|
+
amount: "{{data.total_amount}}",
|
|
630
|
+
description: "Payroll for period {{data.period}}",
|
|
631
|
+
},
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
service: "notes",
|
|
635
|
+
action: "create",
|
|
636
|
+
args: {
|
|
637
|
+
title: "Payroll report: {{data.period}}",
|
|
638
|
+
content: "Processed payroll for {{data.employee_count}} employees, total: {{data.total_amount}}",
|
|
639
|
+
},
|
|
640
|
+
},
|
|
641
|
+
],
|
|
642
|
+
},
|
|
643
|
+
{
|
|
644
|
+
name: "New Hire",
|
|
645
|
+
trigger_event: "new.hire",
|
|
646
|
+
steps: [
|
|
647
|
+
{
|
|
648
|
+
service: "contracts",
|
|
649
|
+
action: "create",
|
|
650
|
+
args: {
|
|
651
|
+
title: "Employment contract: {{data.employee_name}}",
|
|
652
|
+
type: "employment",
|
|
653
|
+
value: "{{data.salary}}",
|
|
654
|
+
},
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
service: "payroll",
|
|
658
|
+
action: "add-employee",
|
|
659
|
+
args: {
|
|
660
|
+
name: "{{data.employee_name}}",
|
|
661
|
+
salary: "{{data.salary}}",
|
|
662
|
+
start_date: "{{data.start_date}}",
|
|
663
|
+
},
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
service: "calendar",
|
|
667
|
+
action: "create",
|
|
668
|
+
args: {
|
|
669
|
+
title: "Onboarding: {{data.employee_name}}",
|
|
670
|
+
description: "Complete onboarding tasks for {{data.employee_name}} starting {{data.start_date}}",
|
|
671
|
+
},
|
|
672
|
+
},
|
|
673
|
+
],
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
name: "Contract Expiring",
|
|
677
|
+
trigger_event: "contract.expiring",
|
|
678
|
+
steps: [
|
|
679
|
+
{
|
|
680
|
+
service: "notes",
|
|
681
|
+
action: "create",
|
|
682
|
+
args: {
|
|
683
|
+
title: "Contract expiring: {{data.contract_title}}",
|
|
684
|
+
content: "Contract {{data.contract_id}} with {{data.party_name}} expires on {{data.expiry_date}}",
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
service: "calendar",
|
|
689
|
+
action: "create",
|
|
690
|
+
args: {
|
|
691
|
+
title: "Renew contract: {{data.contract_title}}",
|
|
692
|
+
description: "Review and renew contract with {{data.party_name}} before {{data.expiry_date}}",
|
|
693
|
+
},
|
|
694
|
+
},
|
|
695
|
+
],
|
|
696
|
+
},
|
|
697
|
+
];
|
|
698
|
+
}
|