yaml-flow 2.3.0 → 2.5.0
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/README.md +368 -0
- package/dist/{constants-DMbnp--H.d.ts → constants-BEbO2_OK.d.ts} +2 -187
- package/dist/{constants-BftTHiuV.d.cts → constants-BNjeIlZ8.d.cts} +2 -187
- package/dist/continuous-event-graph/index.cjs +939 -0
- package/dist/continuous-event-graph/index.cjs.map +1 -0
- package/dist/continuous-event-graph/index.d.cts +179 -0
- package/dist/continuous-event-graph/index.d.ts +179 -0
- package/dist/continuous-event-graph/index.js +916 -0
- package/dist/continuous-event-graph/index.js.map +1 -0
- package/dist/event-graph/index.d.cts +3 -2
- package/dist/event-graph/index.d.ts +3 -2
- package/dist/index.cjs +1008 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +981 -1
- package/dist/index.js.map +1 -1
- package/dist/inference/index.cjs +450 -0
- package/dist/inference/index.cjs.map +1 -0
- package/dist/inference/index.d.cts +229 -0
- package/dist/inference/index.d.ts +229 -0
- package/dist/inference/index.js +443 -0
- package/dist/inference/index.js.map +1 -0
- package/dist/types-C2lOwquM.d.cts +135 -0
- package/dist/types-DAI_a2as.d.cts +198 -0
- package/dist/types-DAI_a2as.d.ts +198 -0
- package/dist/types-mS_pPftm.d.ts +135 -0
- package/package.json +11 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { execFile } from 'child_process';
|
|
2
|
+
|
|
1
3
|
// src/step-machine/reducer.ts
|
|
2
4
|
function applyStepResult(flow, state, stepName, stepResult) {
|
|
3
5
|
const stepConfig = flow.steps[stepName];
|
|
@@ -2300,6 +2302,984 @@ function resolveConfigTemplates(config) {
|
|
|
2300
2302
|
return result;
|
|
2301
2303
|
}
|
|
2302
2304
|
|
|
2303
|
-
|
|
2305
|
+
// src/continuous-event-graph/core.ts
|
|
2306
|
+
function createLiveGraph(config, executionId) {
|
|
2307
|
+
const id = executionId ?? `live-${Date.now()}`;
|
|
2308
|
+
const tasks = {};
|
|
2309
|
+
for (const taskName of Object.keys(config.tasks)) {
|
|
2310
|
+
tasks[taskName] = createDefaultTaskState3();
|
|
2311
|
+
}
|
|
2312
|
+
const state = {
|
|
2313
|
+
status: "running",
|
|
2314
|
+
tasks,
|
|
2315
|
+
availableOutputs: [],
|
|
2316
|
+
stuckDetection: { is_stuck: false, stuck_description: null, outputs_unresolvable: [], tasks_blocked: [] },
|
|
2317
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2318
|
+
executionId: id,
|
|
2319
|
+
executionConfig: {
|
|
2320
|
+
executionMode: config.settings.execution_mode ?? "eligibility-mode",
|
|
2321
|
+
conflictStrategy: config.settings.conflict_strategy ?? "alphabetical",
|
|
2322
|
+
completionStrategy: config.settings.completion
|
|
2323
|
+
}
|
|
2324
|
+
};
|
|
2325
|
+
return { config, state };
|
|
2326
|
+
}
|
|
2327
|
+
function applyEvent(live, event) {
|
|
2328
|
+
const { config, state } = live;
|
|
2329
|
+
if ("executionId" in event && event.executionId && event.executionId !== state.executionId) {
|
|
2330
|
+
return live;
|
|
2331
|
+
}
|
|
2332
|
+
let newState;
|
|
2333
|
+
switch (event.type) {
|
|
2334
|
+
case "task-started":
|
|
2335
|
+
newState = applyTaskStart(state, event.taskName);
|
|
2336
|
+
break;
|
|
2337
|
+
case "task-completed":
|
|
2338
|
+
newState = applyTaskCompletion(state, config, event.taskName, event.result);
|
|
2339
|
+
break;
|
|
2340
|
+
case "task-failed":
|
|
2341
|
+
newState = applyTaskFailure(state, config, event.taskName, event.error);
|
|
2342
|
+
break;
|
|
2343
|
+
case "task-progress":
|
|
2344
|
+
newState = applyTaskProgress(state, event.taskName, event.message, event.progress);
|
|
2345
|
+
break;
|
|
2346
|
+
case "inject-tokens":
|
|
2347
|
+
newState = {
|
|
2348
|
+
...state,
|
|
2349
|
+
availableOutputs: [.../* @__PURE__ */ new Set([...state.availableOutputs, ...event.tokens])],
|
|
2350
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2351
|
+
};
|
|
2352
|
+
break;
|
|
2353
|
+
case "agent-action":
|
|
2354
|
+
newState = applyAgentAction2(state, event.action);
|
|
2355
|
+
break;
|
|
2356
|
+
default:
|
|
2357
|
+
return live;
|
|
2358
|
+
}
|
|
2359
|
+
return { config, state: newState };
|
|
2360
|
+
}
|
|
2361
|
+
function addNode(live, name, taskConfig) {
|
|
2362
|
+
if (live.config.tasks[name]) return live;
|
|
2363
|
+
return {
|
|
2364
|
+
config: {
|
|
2365
|
+
...live.config,
|
|
2366
|
+
tasks: { ...live.config.tasks, [name]: taskConfig }
|
|
2367
|
+
},
|
|
2368
|
+
state: {
|
|
2369
|
+
...live.state,
|
|
2370
|
+
tasks: { ...live.state.tasks, [name]: createDefaultTaskState3() },
|
|
2371
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2372
|
+
}
|
|
2373
|
+
};
|
|
2374
|
+
}
|
|
2375
|
+
function removeNode(live, name) {
|
|
2376
|
+
if (!live.config.tasks[name]) return live;
|
|
2377
|
+
const { [name]: _removedConfig, ...remainingTasks } = live.config.tasks;
|
|
2378
|
+
const { [name]: _removedState, ...remainingStates } = live.state.tasks;
|
|
2379
|
+
return {
|
|
2380
|
+
config: {
|
|
2381
|
+
...live.config,
|
|
2382
|
+
tasks: remainingTasks
|
|
2383
|
+
},
|
|
2384
|
+
state: {
|
|
2385
|
+
...live.state,
|
|
2386
|
+
tasks: remainingStates,
|
|
2387
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2388
|
+
}
|
|
2389
|
+
};
|
|
2390
|
+
}
|
|
2391
|
+
function addRequires(live, nodeName, tokens) {
|
|
2392
|
+
const task = live.config.tasks[nodeName];
|
|
2393
|
+
if (!task) return live;
|
|
2394
|
+
const current = getRequires(task);
|
|
2395
|
+
const toAdd = tokens.filter((t) => !current.includes(t));
|
|
2396
|
+
if (toAdd.length === 0) return live;
|
|
2397
|
+
return {
|
|
2398
|
+
config: {
|
|
2399
|
+
...live.config,
|
|
2400
|
+
tasks: {
|
|
2401
|
+
...live.config.tasks,
|
|
2402
|
+
[nodeName]: { ...task, requires: [...current, ...toAdd] }
|
|
2403
|
+
}
|
|
2404
|
+
},
|
|
2405
|
+
state: live.state
|
|
2406
|
+
};
|
|
2407
|
+
}
|
|
2408
|
+
function removeRequires(live, nodeName, tokens) {
|
|
2409
|
+
const task = live.config.tasks[nodeName];
|
|
2410
|
+
if (!task) return live;
|
|
2411
|
+
const current = getRequires(task);
|
|
2412
|
+
const remaining = current.filter((t) => !tokens.includes(t));
|
|
2413
|
+
if (remaining.length === current.length) return live;
|
|
2414
|
+
return {
|
|
2415
|
+
config: {
|
|
2416
|
+
...live.config,
|
|
2417
|
+
tasks: {
|
|
2418
|
+
...live.config.tasks,
|
|
2419
|
+
[nodeName]: { ...task, requires: remaining }
|
|
2420
|
+
}
|
|
2421
|
+
},
|
|
2422
|
+
state: live.state
|
|
2423
|
+
};
|
|
2424
|
+
}
|
|
2425
|
+
function addProvides(live, nodeName, tokens) {
|
|
2426
|
+
const task = live.config.tasks[nodeName];
|
|
2427
|
+
if (!task) return live;
|
|
2428
|
+
const current = getProvides(task);
|
|
2429
|
+
const toAdd = tokens.filter((t) => !current.includes(t));
|
|
2430
|
+
if (toAdd.length === 0) return live;
|
|
2431
|
+
return {
|
|
2432
|
+
config: {
|
|
2433
|
+
...live.config,
|
|
2434
|
+
tasks: {
|
|
2435
|
+
...live.config.tasks,
|
|
2436
|
+
[nodeName]: { ...task, provides: [...current, ...toAdd] }
|
|
2437
|
+
}
|
|
2438
|
+
},
|
|
2439
|
+
state: live.state
|
|
2440
|
+
};
|
|
2441
|
+
}
|
|
2442
|
+
function removeProvides(live, nodeName, tokens) {
|
|
2443
|
+
const task = live.config.tasks[nodeName];
|
|
2444
|
+
if (!task) return live;
|
|
2445
|
+
const current = getProvides(task);
|
|
2446
|
+
const remaining = current.filter((t) => !tokens.includes(t));
|
|
2447
|
+
if (remaining.length === current.length) return live;
|
|
2448
|
+
return {
|
|
2449
|
+
config: {
|
|
2450
|
+
...live.config,
|
|
2451
|
+
tasks: {
|
|
2452
|
+
...live.config.tasks,
|
|
2453
|
+
[nodeName]: { ...task, provides: remaining }
|
|
2454
|
+
}
|
|
2455
|
+
},
|
|
2456
|
+
state: live.state
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
function injectTokens(live, tokens) {
|
|
2460
|
+
return applyEvent(live, {
|
|
2461
|
+
type: "inject-tokens",
|
|
2462
|
+
tokens,
|
|
2463
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2464
|
+
});
|
|
2465
|
+
}
|
|
2466
|
+
function drainTokens(live, tokens) {
|
|
2467
|
+
const toRemove = new Set(tokens);
|
|
2468
|
+
const remaining = live.state.availableOutputs.filter((t) => !toRemove.has(t));
|
|
2469
|
+
if (remaining.length === live.state.availableOutputs.length) return live;
|
|
2470
|
+
return {
|
|
2471
|
+
config: live.config,
|
|
2472
|
+
state: {
|
|
2473
|
+
...live.state,
|
|
2474
|
+
availableOutputs: remaining,
|
|
2475
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2476
|
+
}
|
|
2477
|
+
};
|
|
2478
|
+
}
|
|
2479
|
+
function resetNode(live, name) {
|
|
2480
|
+
if (!live.config.tasks[name] || !live.state.tasks[name]) return live;
|
|
2481
|
+
return {
|
|
2482
|
+
config: live.config,
|
|
2483
|
+
state: {
|
|
2484
|
+
...live.state,
|
|
2485
|
+
tasks: {
|
|
2486
|
+
...live.state.tasks,
|
|
2487
|
+
[name]: createDefaultTaskState3()
|
|
2488
|
+
},
|
|
2489
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2490
|
+
}
|
|
2491
|
+
};
|
|
2492
|
+
}
|
|
2493
|
+
function disableNode(live, name) {
|
|
2494
|
+
const taskState = live.state.tasks[name];
|
|
2495
|
+
if (!taskState || taskState.status === "inactivated") return live;
|
|
2496
|
+
return {
|
|
2497
|
+
config: live.config,
|
|
2498
|
+
state: {
|
|
2499
|
+
...live.state,
|
|
2500
|
+
tasks: {
|
|
2501
|
+
...live.state.tasks,
|
|
2502
|
+
[name]: { ...taskState, status: "inactivated", lastUpdated: (/* @__PURE__ */ new Date()).toISOString() }
|
|
2503
|
+
},
|
|
2504
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2505
|
+
}
|
|
2506
|
+
};
|
|
2507
|
+
}
|
|
2508
|
+
function enableNode(live, name) {
|
|
2509
|
+
const taskState = live.state.tasks[name];
|
|
2510
|
+
if (!taskState || taskState.status !== "inactivated") return live;
|
|
2511
|
+
return {
|
|
2512
|
+
config: live.config,
|
|
2513
|
+
state: {
|
|
2514
|
+
...live.state,
|
|
2515
|
+
tasks: {
|
|
2516
|
+
...live.state.tasks,
|
|
2517
|
+
[name]: { ...taskState, status: "not-started", lastUpdated: (/* @__PURE__ */ new Date()).toISOString() }
|
|
2518
|
+
},
|
|
2519
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
|
|
2520
|
+
}
|
|
2521
|
+
};
|
|
2522
|
+
}
|
|
2523
|
+
function getNode(live, name) {
|
|
2524
|
+
const config = live.config.tasks[name];
|
|
2525
|
+
if (!config) return void 0;
|
|
2526
|
+
const state = live.state.tasks[name] ?? createDefaultTaskState3();
|
|
2527
|
+
return { name, config, state };
|
|
2528
|
+
}
|
|
2529
|
+
function snapshot(live) {
|
|
2530
|
+
return {
|
|
2531
|
+
version: 1,
|
|
2532
|
+
config: live.config,
|
|
2533
|
+
state: live.state,
|
|
2534
|
+
snapshotAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2535
|
+
};
|
|
2536
|
+
}
|
|
2537
|
+
function restore(data) {
|
|
2538
|
+
if (!data || typeof data !== "object") {
|
|
2539
|
+
throw new Error("Invalid snapshot: expected an object");
|
|
2540
|
+
}
|
|
2541
|
+
const snap = data;
|
|
2542
|
+
if (!snap.config || typeof snap.config !== "object") {
|
|
2543
|
+
throw new Error('Invalid snapshot: missing or invalid "config"');
|
|
2544
|
+
}
|
|
2545
|
+
if (!snap.state || typeof snap.state !== "object") {
|
|
2546
|
+
throw new Error('Invalid snapshot: missing or invalid "state"');
|
|
2547
|
+
}
|
|
2548
|
+
const config = snap.config;
|
|
2549
|
+
const state = snap.state;
|
|
2550
|
+
if (!config.settings || typeof config.settings !== "object") {
|
|
2551
|
+
throw new Error("Invalid snapshot: config.settings missing");
|
|
2552
|
+
}
|
|
2553
|
+
if (!config.tasks || typeof config.tasks !== "object") {
|
|
2554
|
+
throw new Error("Invalid snapshot: config.tasks missing");
|
|
2555
|
+
}
|
|
2556
|
+
if (!state.tasks || typeof state.tasks !== "object") {
|
|
2557
|
+
throw new Error("Invalid snapshot: state.tasks missing");
|
|
2558
|
+
}
|
|
2559
|
+
if (!Array.isArray(state.availableOutputs)) {
|
|
2560
|
+
throw new Error("Invalid snapshot: state.availableOutputs must be an array");
|
|
2561
|
+
}
|
|
2562
|
+
return { config, state };
|
|
2563
|
+
}
|
|
2564
|
+
function createDefaultTaskState3() {
|
|
2565
|
+
return {
|
|
2566
|
+
status: "not-started",
|
|
2567
|
+
executionCount: 0,
|
|
2568
|
+
retryCount: 0,
|
|
2569
|
+
lastEpoch: 0,
|
|
2570
|
+
messages: [],
|
|
2571
|
+
progress: null
|
|
2572
|
+
};
|
|
2573
|
+
}
|
|
2574
|
+
function applyAgentAction2(state, action) {
|
|
2575
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2576
|
+
switch (action) {
|
|
2577
|
+
case "stop":
|
|
2578
|
+
return { ...state, status: "stopped", lastUpdated: now };
|
|
2579
|
+
case "pause":
|
|
2580
|
+
return { ...state, status: "paused", lastUpdated: now };
|
|
2581
|
+
case "resume":
|
|
2582
|
+
return { ...state, status: "running", lastUpdated: now };
|
|
2583
|
+
default:
|
|
2584
|
+
return state;
|
|
2585
|
+
}
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
// src/continuous-event-graph/schedule.ts
|
|
2589
|
+
function schedule(live) {
|
|
2590
|
+
const { config, state } = live;
|
|
2591
|
+
const graphTasks = getAllTasks(config);
|
|
2592
|
+
const taskNames = Object.keys(graphTasks);
|
|
2593
|
+
if (taskNames.length === 0) {
|
|
2594
|
+
return { eligible: [], pending: [], unresolved: [], blocked: [], conflicts: {} };
|
|
2595
|
+
}
|
|
2596
|
+
const producerMap = buildProducerMap3(graphTasks);
|
|
2597
|
+
const computedOutputs = computeAvailableOutputs(config, state.tasks);
|
|
2598
|
+
const availableOutputs = /* @__PURE__ */ new Set([...computedOutputs, ...state.availableOutputs]);
|
|
2599
|
+
const eligible = [];
|
|
2600
|
+
const pending = [];
|
|
2601
|
+
const unresolved = [];
|
|
2602
|
+
const blocked = [];
|
|
2603
|
+
for (const [taskName, taskConfig] of Object.entries(graphTasks)) {
|
|
2604
|
+
const taskState = state.tasks[taskName];
|
|
2605
|
+
if (!isRepeatableTask(taskConfig)) {
|
|
2606
|
+
if (taskState?.status === TASK_STATUS.COMPLETED || taskState?.status === TASK_STATUS.RUNNING || isNonActiveTask(taskState)) {
|
|
2607
|
+
continue;
|
|
2608
|
+
}
|
|
2609
|
+
} else {
|
|
2610
|
+
if (taskState?.status === TASK_STATUS.RUNNING || isNonActiveTask(taskState)) {
|
|
2611
|
+
continue;
|
|
2612
|
+
}
|
|
2613
|
+
const maxExec = getRepeatableMax(taskConfig);
|
|
2614
|
+
if (maxExec !== void 0 && taskState && taskState.executionCount >= maxExec) {
|
|
2615
|
+
continue;
|
|
2616
|
+
}
|
|
2617
|
+
if (taskConfig.circuit_breaker && taskState && taskState.executionCount >= taskConfig.circuit_breaker.max_executions) {
|
|
2618
|
+
continue;
|
|
2619
|
+
}
|
|
2620
|
+
if (taskState?.status === TASK_STATUS.COMPLETED) {
|
|
2621
|
+
const requires2 = getRequires(taskConfig);
|
|
2622
|
+
if (requires2.length > 0) {
|
|
2623
|
+
const hasRefreshed = requires2.some((req) => {
|
|
2624
|
+
for (const [otherName, otherConfig] of Object.entries(graphTasks)) {
|
|
2625
|
+
if (getProvides(otherConfig).includes(req)) {
|
|
2626
|
+
const otherState = state.tasks[otherName];
|
|
2627
|
+
if (otherState && otherState.executionCount > taskState.lastEpoch) return true;
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
return false;
|
|
2631
|
+
});
|
|
2632
|
+
if (!hasRefreshed) continue;
|
|
2633
|
+
} else {
|
|
2634
|
+
continue;
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
const requires = getRequires(taskConfig);
|
|
2639
|
+
if (requires.length === 0) {
|
|
2640
|
+
eligible.push(taskName);
|
|
2641
|
+
continue;
|
|
2642
|
+
}
|
|
2643
|
+
const missingTokens = [];
|
|
2644
|
+
const pendingTokens = [];
|
|
2645
|
+
const failedTokenInfo = [];
|
|
2646
|
+
for (const token of requires) {
|
|
2647
|
+
if (availableOutputs.has(token)) continue;
|
|
2648
|
+
const producers = producerMap[token] || [];
|
|
2649
|
+
if (producers.length === 0) {
|
|
2650
|
+
missingTokens.push(token);
|
|
2651
|
+
} else {
|
|
2652
|
+
const allFailed = producers.every((p) => isNonActiveTask(state.tasks[p]));
|
|
2653
|
+
if (allFailed) {
|
|
2654
|
+
failedTokenInfo.push({ token, failedProducer: producers[0] });
|
|
2655
|
+
} else {
|
|
2656
|
+
pendingTokens.push(token);
|
|
2657
|
+
}
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2660
|
+
if (missingTokens.length > 0) {
|
|
2661
|
+
unresolved.push({ taskName, missingTokens });
|
|
2662
|
+
} else if (failedTokenInfo.length > 0) {
|
|
2663
|
+
blocked.push({
|
|
2664
|
+
taskName,
|
|
2665
|
+
failedTokens: failedTokenInfo.map((f) => f.token),
|
|
2666
|
+
failedProducers: [...new Set(failedTokenInfo.map((f) => f.failedProducer))]
|
|
2667
|
+
});
|
|
2668
|
+
} else if (pendingTokens.length > 0) {
|
|
2669
|
+
pending.push({ taskName, waitingOn: pendingTokens });
|
|
2670
|
+
} else {
|
|
2671
|
+
eligible.push(taskName);
|
|
2672
|
+
}
|
|
2673
|
+
}
|
|
2674
|
+
const conflicts = {};
|
|
2675
|
+
if (eligible.length > 1) {
|
|
2676
|
+
const outputGroups = groupTasksByProvides(eligible, graphTasks);
|
|
2677
|
+
for (const [outputKey, groupTasks] of Object.entries(outputGroups)) {
|
|
2678
|
+
if (groupTasks.length > 1) {
|
|
2679
|
+
conflicts[outputKey] = groupTasks;
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
}
|
|
2683
|
+
return { eligible, pending, unresolved, blocked, conflicts };
|
|
2684
|
+
}
|
|
2685
|
+
function buildProducerMap3(tasks) {
|
|
2686
|
+
const map = {};
|
|
2687
|
+
for (const [name, config] of Object.entries(tasks)) {
|
|
2688
|
+
for (const token of getProvides(config)) {
|
|
2689
|
+
if (!map[token]) map[token] = [];
|
|
2690
|
+
map[token].push(name);
|
|
2691
|
+
}
|
|
2692
|
+
if (config.on) {
|
|
2693
|
+
for (const tokens of Object.values(config.on)) {
|
|
2694
|
+
for (const token of tokens) {
|
|
2695
|
+
if (!map[token]) map[token] = [];
|
|
2696
|
+
if (!map[token].includes(name)) map[token].push(name);
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
if (config.on_failure) {
|
|
2701
|
+
for (const token of config.on_failure) {
|
|
2702
|
+
if (!map[token]) map[token] = [];
|
|
2703
|
+
if (!map[token].includes(name)) map[token].push(name);
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
return map;
|
|
2708
|
+
}
|
|
2709
|
+
|
|
2710
|
+
// src/continuous-event-graph/inspect.ts
|
|
2711
|
+
function inspect(live) {
|
|
2712
|
+
const { config, state } = live;
|
|
2713
|
+
const graphTasks = getAllTasks(config);
|
|
2714
|
+
const taskNames = Object.keys(graphTasks);
|
|
2715
|
+
let running = 0, completed = 0, failed = 0, waiting = 0, notStarted = 0, disabled = 0;
|
|
2716
|
+
for (const taskName of taskNames) {
|
|
2717
|
+
const ts = state.tasks[taskName];
|
|
2718
|
+
if (!ts || ts.status === TASK_STATUS.NOT_STARTED) {
|
|
2719
|
+
notStarted++;
|
|
2720
|
+
} else {
|
|
2721
|
+
switch (ts.status) {
|
|
2722
|
+
case TASK_STATUS.RUNNING:
|
|
2723
|
+
running++;
|
|
2724
|
+
break;
|
|
2725
|
+
case TASK_STATUS.COMPLETED:
|
|
2726
|
+
completed++;
|
|
2727
|
+
break;
|
|
2728
|
+
case TASK_STATUS.FAILED:
|
|
2729
|
+
failed++;
|
|
2730
|
+
break;
|
|
2731
|
+
case "inactivated":
|
|
2732
|
+
disabled++;
|
|
2733
|
+
break;
|
|
2734
|
+
default:
|
|
2735
|
+
waiting++;
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
const producerMap = {};
|
|
2740
|
+
for (const [name, taskConfig] of Object.entries(graphTasks)) {
|
|
2741
|
+
for (const token of getProvides(taskConfig)) {
|
|
2742
|
+
if (!producerMap[token]) producerMap[token] = [];
|
|
2743
|
+
producerMap[token].push(name);
|
|
2744
|
+
}
|
|
2745
|
+
if (taskConfig.on) {
|
|
2746
|
+
for (const tokens of Object.values(taskConfig.on)) {
|
|
2747
|
+
for (const token of tokens) {
|
|
2748
|
+
if (!producerMap[token]) producerMap[token] = [];
|
|
2749
|
+
if (!producerMap[token].includes(name)) producerMap[token].push(name);
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
if (taskConfig.on_failure) {
|
|
2754
|
+
for (const token of taskConfig.on_failure) {
|
|
2755
|
+
if (!producerMap[token]) producerMap[token] = [];
|
|
2756
|
+
if (!producerMap[token].includes(name)) producerMap[token].push(name);
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
const openDeps = /* @__PURE__ */ new Set();
|
|
2761
|
+
let unresolvedCount = 0;
|
|
2762
|
+
let blockedCount = 0;
|
|
2763
|
+
for (const [taskName, taskConfig] of Object.entries(graphTasks)) {
|
|
2764
|
+
const ts = state.tasks[taskName];
|
|
2765
|
+
if (ts?.status === TASK_STATUS.COMPLETED || ts?.status === TASK_STATUS.RUNNING) continue;
|
|
2766
|
+
let hasOpen = false;
|
|
2767
|
+
let hasBlocked = false;
|
|
2768
|
+
for (const token of getRequires(taskConfig)) {
|
|
2769
|
+
const producers = producerMap[token] || [];
|
|
2770
|
+
if (producers.length === 0) {
|
|
2771
|
+
openDeps.add(token);
|
|
2772
|
+
hasOpen = true;
|
|
2773
|
+
} else {
|
|
2774
|
+
const allFailed = producers.every((p) => {
|
|
2775
|
+
const ps = state.tasks[p];
|
|
2776
|
+
return ps?.status === TASK_STATUS.FAILED || ps?.status === "inactivated";
|
|
2777
|
+
});
|
|
2778
|
+
if (allFailed) hasBlocked = true;
|
|
2779
|
+
}
|
|
2780
|
+
}
|
|
2781
|
+
if (hasOpen) unresolvedCount++;
|
|
2782
|
+
if (hasBlocked && !hasOpen) blockedCount++;
|
|
2783
|
+
}
|
|
2784
|
+
const conflictTokens = [];
|
|
2785
|
+
for (const [token, producers] of Object.entries(producerMap)) {
|
|
2786
|
+
if (producers.length > 1) conflictTokens.push(token);
|
|
2787
|
+
}
|
|
2788
|
+
const deps = buildTaskDeps2(graphTasks, producerMap);
|
|
2789
|
+
const cycles = detectCycles2(taskNames, deps);
|
|
2790
|
+
return {
|
|
2791
|
+
totalNodes: taskNames.length,
|
|
2792
|
+
running,
|
|
2793
|
+
completed,
|
|
2794
|
+
failed,
|
|
2795
|
+
waiting,
|
|
2796
|
+
notStarted,
|
|
2797
|
+
disabled,
|
|
2798
|
+
unresolvedCount,
|
|
2799
|
+
blockedCount,
|
|
2800
|
+
openDependencies: [...openDeps],
|
|
2801
|
+
cycles,
|
|
2802
|
+
conflictTokens
|
|
2803
|
+
};
|
|
2804
|
+
}
|
|
2805
|
+
function buildTaskDeps2(tasks, producerMap) {
|
|
2806
|
+
const deps = {};
|
|
2807
|
+
for (const [name, config] of Object.entries(tasks)) {
|
|
2808
|
+
deps[name] = /* @__PURE__ */ new Set();
|
|
2809
|
+
for (const token of getRequires(config)) {
|
|
2810
|
+
for (const producer of producerMap[token] || []) {
|
|
2811
|
+
if (producer !== name) deps[name].add(producer);
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
return deps;
|
|
2816
|
+
}
|
|
2817
|
+
function detectCycles2(taskNames, deps) {
|
|
2818
|
+
const WHITE = 0, GRAY = 1, BLACK = 2;
|
|
2819
|
+
const color = {};
|
|
2820
|
+
const parent = {};
|
|
2821
|
+
const cycles = [];
|
|
2822
|
+
for (const name of taskNames) {
|
|
2823
|
+
color[name] = WHITE;
|
|
2824
|
+
parent[name] = null;
|
|
2825
|
+
}
|
|
2826
|
+
function dfs(node) {
|
|
2827
|
+
color[node] = GRAY;
|
|
2828
|
+
for (const dep of deps[node] || []) {
|
|
2829
|
+
if (color[dep] === GRAY) {
|
|
2830
|
+
const cycle = [dep];
|
|
2831
|
+
let cur = node;
|
|
2832
|
+
while (cur !== dep) {
|
|
2833
|
+
cycle.push(cur);
|
|
2834
|
+
cur = parent[cur];
|
|
2835
|
+
}
|
|
2836
|
+
cycle.push(dep);
|
|
2837
|
+
cycle.reverse();
|
|
2838
|
+
cycles.push(cycle);
|
|
2839
|
+
} else if (color[dep] === WHITE) {
|
|
2840
|
+
parent[dep] = node;
|
|
2841
|
+
dfs(dep);
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2844
|
+
color[node] = BLACK;
|
|
2845
|
+
}
|
|
2846
|
+
for (const name of taskNames) {
|
|
2847
|
+
if (color[name] === WHITE) dfs(name);
|
|
2848
|
+
}
|
|
2849
|
+
return cycles;
|
|
2850
|
+
}
|
|
2851
|
+
function buildProducerMap4(tasks) {
|
|
2852
|
+
const map = {};
|
|
2853
|
+
for (const [name, config] of Object.entries(tasks)) {
|
|
2854
|
+
for (const token of getProvides(config)) {
|
|
2855
|
+
if (!map[token]) map[token] = [];
|
|
2856
|
+
map[token].push(name);
|
|
2857
|
+
}
|
|
2858
|
+
if (config.on) {
|
|
2859
|
+
for (const tokens of Object.values(config.on)) {
|
|
2860
|
+
for (const token of tokens) {
|
|
2861
|
+
if (!map[token]) map[token] = [];
|
|
2862
|
+
if (!map[token].includes(name)) map[token].push(name);
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
}
|
|
2866
|
+
if (config.on_failure) {
|
|
2867
|
+
for (const token of config.on_failure) {
|
|
2868
|
+
if (!map[token]) map[token] = [];
|
|
2869
|
+
if (!map[token].includes(name)) map[token].push(name);
|
|
2870
|
+
}
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
return map;
|
|
2874
|
+
}
|
|
2875
|
+
function getUnreachableTokens(live) {
|
|
2876
|
+
const { config, state } = live;
|
|
2877
|
+
const graphTasks = getAllTasks(config);
|
|
2878
|
+
const producerMap = buildProducerMap4(graphTasks);
|
|
2879
|
+
const available = /* @__PURE__ */ new Set([...state.availableOutputs]);
|
|
2880
|
+
for (const [taskName, taskState] of Object.entries(state.tasks)) {
|
|
2881
|
+
if (taskState.status === "completed") {
|
|
2882
|
+
const tc = graphTasks[taskName];
|
|
2883
|
+
if (tc) getProvides(tc).forEach((t) => available.add(t));
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
const allRequired = /* @__PURE__ */ new Set();
|
|
2887
|
+
for (const taskConfig of Object.values(graphTasks)) {
|
|
2888
|
+
for (const token of getRequires(taskConfig)) {
|
|
2889
|
+
allRequired.add(token);
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
const unreachable = /* @__PURE__ */ new Set();
|
|
2893
|
+
const unreachableNodes = /* @__PURE__ */ new Set();
|
|
2894
|
+
for (const token of allRequired) {
|
|
2895
|
+
if (available.has(token)) continue;
|
|
2896
|
+
const producers = producerMap[token] || [];
|
|
2897
|
+
if (producers.length === 0) {
|
|
2898
|
+
unreachable.add(token);
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
let changed = true;
|
|
2902
|
+
while (changed) {
|
|
2903
|
+
changed = false;
|
|
2904
|
+
for (const [name, taskConfig] of Object.entries(graphTasks)) {
|
|
2905
|
+
if (unreachableNodes.has(name)) continue;
|
|
2906
|
+
const ts = state.tasks[name];
|
|
2907
|
+
if (ts?.status === "completed") continue;
|
|
2908
|
+
const isNonActive = isNonActiveTask(ts);
|
|
2909
|
+
const requires = getRequires(taskConfig);
|
|
2910
|
+
const hasUnreachableDep = requires.some((t) => unreachable.has(t));
|
|
2911
|
+
if (isNonActive || hasUnreachableDep) {
|
|
2912
|
+
if (!unreachableNodes.has(name)) {
|
|
2913
|
+
unreachableNodes.add(name);
|
|
2914
|
+
changed = true;
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
for (const token of allRequired) {
|
|
2919
|
+
if (unreachable.has(token) || available.has(token)) continue;
|
|
2920
|
+
const producers = producerMap[token] || [];
|
|
2921
|
+
const allProducersUnreachable = producers.length > 0 && producers.every((p) => unreachableNodes.has(p) || isNonActiveTask(state.tasks[p]));
|
|
2922
|
+
if (producers.length === 0 || allProducersUnreachable) {
|
|
2923
|
+
if (!unreachable.has(token)) {
|
|
2924
|
+
unreachable.add(token);
|
|
2925
|
+
changed = true;
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
}
|
|
2930
|
+
const tokens = [];
|
|
2931
|
+
for (const token of unreachable) {
|
|
2932
|
+
const producers = producerMap[token] || [];
|
|
2933
|
+
let reason;
|
|
2934
|
+
if (producers.length === 0) {
|
|
2935
|
+
reason = "no-producer";
|
|
2936
|
+
} else {
|
|
2937
|
+
const allFailed = producers.every((p) => isNonActiveTask(state.tasks[p]));
|
|
2938
|
+
reason = allFailed ? "all-producers-failed" : "transitive";
|
|
2939
|
+
}
|
|
2940
|
+
tokens.push({ token, reason, producers });
|
|
2941
|
+
}
|
|
2942
|
+
return { tokens };
|
|
2943
|
+
}
|
|
2944
|
+
function getUnreachableNodes(live) {
|
|
2945
|
+
const { config, state } = live;
|
|
2946
|
+
const graphTasks = getAllTasks(config);
|
|
2947
|
+
const { tokens: unreachableTokens } = getUnreachableTokens(live);
|
|
2948
|
+
const unreachableTokenSet = new Set(unreachableTokens.map((t) => t.token));
|
|
2949
|
+
const nodes = [];
|
|
2950
|
+
for (const [name, taskConfig] of Object.entries(graphTasks)) {
|
|
2951
|
+
const ts = state.tasks[name];
|
|
2952
|
+
if (ts?.status === "completed") continue;
|
|
2953
|
+
const requires = getRequires(taskConfig);
|
|
2954
|
+
const missingTokens = requires.filter((t) => unreachableTokenSet.has(t));
|
|
2955
|
+
if (missingTokens.length > 0) {
|
|
2956
|
+
nodes.push({ nodeName: name, missingTokens });
|
|
2957
|
+
} else if (isNonActiveTask(ts)) {
|
|
2958
|
+
nodes.push({ nodeName: name, missingTokens: [] });
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2961
|
+
return { nodes };
|
|
2962
|
+
}
|
|
2963
|
+
function getUpstream(live, nodeName) {
|
|
2964
|
+
const graphTasks = getAllTasks(live.config);
|
|
2965
|
+
if (!graphTasks[nodeName]) return { nodeName, nodes: [], tokens: [] };
|
|
2966
|
+
const producerMap = buildProducerMap4(graphTasks);
|
|
2967
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2968
|
+
const tokenSet = /* @__PURE__ */ new Set();
|
|
2969
|
+
const nodeEntries = /* @__PURE__ */ new Map();
|
|
2970
|
+
function walk(current) {
|
|
2971
|
+
const taskConfig = graphTasks[current];
|
|
2972
|
+
if (!taskConfig) return;
|
|
2973
|
+
for (const token of getRequires(taskConfig)) {
|
|
2974
|
+
const producers = producerMap[token] || [];
|
|
2975
|
+
for (const producer of producers) {
|
|
2976
|
+
if (producer === nodeName) continue;
|
|
2977
|
+
tokenSet.add(token);
|
|
2978
|
+
if (!nodeEntries.has(producer)) nodeEntries.set(producer, /* @__PURE__ */ new Set());
|
|
2979
|
+
nodeEntries.get(producer).add(token);
|
|
2980
|
+
if (!visited.has(producer)) {
|
|
2981
|
+
visited.add(producer);
|
|
2982
|
+
walk(producer);
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
}
|
|
2987
|
+
walk(nodeName);
|
|
2988
|
+
const nodes = [...nodeEntries.entries()].map(([name, tokens]) => ({
|
|
2989
|
+
nodeName: name,
|
|
2990
|
+
providesTokens: [...tokens]
|
|
2991
|
+
}));
|
|
2992
|
+
return { nodeName, nodes, tokens: [...tokenSet] };
|
|
2993
|
+
}
|
|
2994
|
+
function getDownstream(live, nodeName) {
|
|
2995
|
+
const graphTasks = getAllTasks(live.config);
|
|
2996
|
+
if (!graphTasks[nodeName]) return { nodeName, nodes: [], tokens: [] };
|
|
2997
|
+
const consumerMap = {};
|
|
2998
|
+
for (const [name, config] of Object.entries(graphTasks)) {
|
|
2999
|
+
for (const token of getRequires(config)) {
|
|
3000
|
+
if (!consumerMap[token]) consumerMap[token] = [];
|
|
3001
|
+
consumerMap[token].push(name);
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3005
|
+
const tokenSet = /* @__PURE__ */ new Set();
|
|
3006
|
+
const nodeEntries = /* @__PURE__ */ new Map();
|
|
3007
|
+
function walk(current) {
|
|
3008
|
+
const taskConfig = graphTasks[current];
|
|
3009
|
+
if (!taskConfig) return;
|
|
3010
|
+
for (const token of getProvides(taskConfig)) {
|
|
3011
|
+
const consumers = consumerMap[token] || [];
|
|
3012
|
+
for (const consumer of consumers) {
|
|
3013
|
+
if (consumer === nodeName) continue;
|
|
3014
|
+
tokenSet.add(token);
|
|
3015
|
+
if (!nodeEntries.has(consumer)) nodeEntries.set(consumer, /* @__PURE__ */ new Set());
|
|
3016
|
+
nodeEntries.get(consumer).add(token);
|
|
3017
|
+
if (!visited.has(consumer)) {
|
|
3018
|
+
visited.add(consumer);
|
|
3019
|
+
walk(consumer);
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
}
|
|
3024
|
+
walk(nodeName);
|
|
3025
|
+
const nodes = [...nodeEntries.entries()].map(([name, tokens]) => ({
|
|
3026
|
+
nodeName: name,
|
|
3027
|
+
requiresTokens: [...tokens]
|
|
3028
|
+
}));
|
|
3029
|
+
return { nodeName, nodes, tokens: [...tokenSet] };
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
// src/inference/core.ts
|
|
3033
|
+
var DEFAULT_THRESHOLD = 0.5;
|
|
3034
|
+
var DEFAULT_SYSTEM_PROMPT = `You are a workflow completion analyzer. Given a graph of tasks with their current states, evidence, and inference hints, determine which tasks appear to be completed based on the available evidence.
|
|
3035
|
+
|
|
3036
|
+
For each task you analyze, provide a JSON response. Be conservative \u2014 only mark tasks as completed when the evidence strongly supports it.`;
|
|
3037
|
+
function buildInferencePrompt(live, options = {}) {
|
|
3038
|
+
const { scope, context, systemPrompt } = options;
|
|
3039
|
+
const graphTasks = getAllTasks(live.config);
|
|
3040
|
+
const { state } = live;
|
|
3041
|
+
const candidates = getAnalyzableCandidates(live, scope);
|
|
3042
|
+
if (candidates.length === 0) {
|
|
3043
|
+
return "";
|
|
3044
|
+
}
|
|
3045
|
+
const lines = [];
|
|
3046
|
+
lines.push(systemPrompt || DEFAULT_SYSTEM_PROMPT);
|
|
3047
|
+
lines.push("");
|
|
3048
|
+
lines.push("## Graph State");
|
|
3049
|
+
lines.push("");
|
|
3050
|
+
lines.push(`Available tokens: ${state.availableOutputs.length > 0 ? state.availableOutputs.join(", ") : "(none)"}`);
|
|
3051
|
+
lines.push("");
|
|
3052
|
+
const completedTasks = Object.entries(state.tasks).filter(([_, ts]) => ts.status === "completed").map(([name]) => name);
|
|
3053
|
+
if (completedTasks.length > 0) {
|
|
3054
|
+
lines.push(`Completed tasks: ${completedTasks.join(", ")}`);
|
|
3055
|
+
lines.push("");
|
|
3056
|
+
}
|
|
3057
|
+
lines.push("## Tasks to Analyze");
|
|
3058
|
+
lines.push("");
|
|
3059
|
+
for (const taskName of candidates) {
|
|
3060
|
+
const taskConfig = graphTasks[taskName];
|
|
3061
|
+
const taskState = state.tasks[taskName];
|
|
3062
|
+
lines.push(`### ${taskName}`);
|
|
3063
|
+
if (taskConfig.description) {
|
|
3064
|
+
lines.push(`Description: ${taskConfig.description}`);
|
|
3065
|
+
}
|
|
3066
|
+
const requires = getRequires(taskConfig);
|
|
3067
|
+
const provides = getProvides(taskConfig);
|
|
3068
|
+
if (requires.length > 0) lines.push(`Requires: ${requires.join(", ")}`);
|
|
3069
|
+
if (provides.length > 0) lines.push(`Provides: ${provides.join(", ")}`);
|
|
3070
|
+
lines.push(`Current status: ${taskState?.status || "not-started"}`);
|
|
3071
|
+
const hints = taskConfig.inference;
|
|
3072
|
+
if (hints) {
|
|
3073
|
+
if (hints.criteria) lines.push(`Completion criteria: ${hints.criteria}`);
|
|
3074
|
+
if (hints.keywords?.length) lines.push(`Keywords: ${hints.keywords.join(", ")}`);
|
|
3075
|
+
if (hints.suggestedChecks?.length) lines.push(`Suggested checks: ${hints.suggestedChecks.join("; ")}`);
|
|
3076
|
+
}
|
|
3077
|
+
lines.push("");
|
|
3078
|
+
}
|
|
3079
|
+
if (context) {
|
|
3080
|
+
lines.push("## Additional Context / Evidence");
|
|
3081
|
+
lines.push("");
|
|
3082
|
+
lines.push(context);
|
|
3083
|
+
lines.push("");
|
|
3084
|
+
}
|
|
3085
|
+
lines.push("## Response Format");
|
|
3086
|
+
lines.push("");
|
|
3087
|
+
lines.push("Respond with a JSON array of objects, one per task you have evidence for:");
|
|
3088
|
+
lines.push("```json");
|
|
3089
|
+
lines.push("[");
|
|
3090
|
+
lines.push(" {");
|
|
3091
|
+
lines.push(' "taskName": "task-name",');
|
|
3092
|
+
lines.push(' "confidence": 0.0 to 1.0,');
|
|
3093
|
+
lines.push(' "reasoning": "explanation of why you believe this task is complete or not"');
|
|
3094
|
+
lines.push(" }");
|
|
3095
|
+
lines.push("]");
|
|
3096
|
+
lines.push("```");
|
|
3097
|
+
lines.push("");
|
|
3098
|
+
lines.push("Rules:");
|
|
3099
|
+
lines.push('- Only include tasks from the "Tasks to Analyze" section');
|
|
3100
|
+
lines.push("- confidence 0.0 = no evidence of completion, 1.0 = certain it is complete");
|
|
3101
|
+
lines.push("- If you have no evidence for a task, omit it from the array");
|
|
3102
|
+
lines.push("- Be conservative \u2014 require clear evidence before high confidence");
|
|
3103
|
+
lines.push("- Respond ONLY with the JSON array, no additional text");
|
|
3104
|
+
return lines.join("\n");
|
|
3105
|
+
}
|
|
3106
|
+
async function inferCompletions(live, adapter, options = {}) {
|
|
3107
|
+
options.threshold ?? DEFAULT_THRESHOLD;
|
|
3108
|
+
const analyzedNodes = getAnalyzableCandidates(live, options.scope);
|
|
3109
|
+
if (analyzedNodes.length === 0) {
|
|
3110
|
+
return { suggestions: [], promptUsed: "", rawResponse: "", analyzedNodes: [] };
|
|
3111
|
+
}
|
|
3112
|
+
const prompt = buildInferencePrompt(live, options);
|
|
3113
|
+
const rawResponse = await adapter.analyze(prompt);
|
|
3114
|
+
const suggestions = parseInferenceResponse(rawResponse, analyzedNodes);
|
|
3115
|
+
return {
|
|
3116
|
+
suggestions,
|
|
3117
|
+
promptUsed: prompt,
|
|
3118
|
+
rawResponse,
|
|
3119
|
+
analyzedNodes
|
|
3120
|
+
};
|
|
3121
|
+
}
|
|
3122
|
+
function applyInferences(live, result, threshold = DEFAULT_THRESHOLD) {
|
|
3123
|
+
let current = live;
|
|
3124
|
+
for (const suggestion of result.suggestions) {
|
|
3125
|
+
if (suggestion.confidence < threshold) continue;
|
|
3126
|
+
const taskState = current.state.tasks[suggestion.taskName];
|
|
3127
|
+
if (!taskState) continue;
|
|
3128
|
+
if (taskState.status === "completed" || taskState.status === "running") continue;
|
|
3129
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3130
|
+
current = applyEvent(current, {
|
|
3131
|
+
type: "task-started",
|
|
3132
|
+
taskName: suggestion.taskName,
|
|
3133
|
+
timestamp: now
|
|
3134
|
+
});
|
|
3135
|
+
current = applyEvent(current, {
|
|
3136
|
+
type: "task-completed",
|
|
3137
|
+
taskName: suggestion.taskName,
|
|
3138
|
+
timestamp: now,
|
|
3139
|
+
result: "llm-inferred"
|
|
3140
|
+
});
|
|
3141
|
+
}
|
|
3142
|
+
return current;
|
|
3143
|
+
}
|
|
3144
|
+
async function inferAndApply(live, adapter, options = {}) {
|
|
3145
|
+
const threshold = options.threshold ?? DEFAULT_THRESHOLD;
|
|
3146
|
+
const inference = await inferCompletions(live, adapter, options);
|
|
3147
|
+
const updated = applyInferences(live, inference, threshold);
|
|
3148
|
+
const applied = inference.suggestions.filter((s) => s.confidence >= threshold);
|
|
3149
|
+
const skipped = inference.suggestions.filter((s) => s.confidence < threshold);
|
|
3150
|
+
return {
|
|
3151
|
+
live: updated,
|
|
3152
|
+
inference,
|
|
3153
|
+
applied,
|
|
3154
|
+
skipped
|
|
3155
|
+
};
|
|
3156
|
+
}
|
|
3157
|
+
function getAnalyzableCandidates(live, scope) {
|
|
3158
|
+
const graphTasks = getAllTasks(live.config);
|
|
3159
|
+
const { state } = live;
|
|
3160
|
+
const candidates = [];
|
|
3161
|
+
for (const [name, config] of Object.entries(graphTasks)) {
|
|
3162
|
+
const taskState = state.tasks[name];
|
|
3163
|
+
if (taskState?.status === "completed" || taskState?.status === "running") continue;
|
|
3164
|
+
if (scope) {
|
|
3165
|
+
if (scope.includes(name)) candidates.push(name);
|
|
3166
|
+
} else {
|
|
3167
|
+
if (config.inference?.autoDetectable) candidates.push(name);
|
|
3168
|
+
}
|
|
3169
|
+
}
|
|
3170
|
+
return candidates;
|
|
3171
|
+
}
|
|
3172
|
+
function parseInferenceResponse(rawResponse, validNodes, _threshold) {
|
|
3173
|
+
const validSet = new Set(validNodes);
|
|
3174
|
+
try {
|
|
3175
|
+
const jsonStr = extractJson(rawResponse);
|
|
3176
|
+
if (!jsonStr) return [];
|
|
3177
|
+
const parsed = JSON.parse(jsonStr);
|
|
3178
|
+
if (!Array.isArray(parsed)) return [];
|
|
3179
|
+
const suggestions = [];
|
|
3180
|
+
for (const item of parsed) {
|
|
3181
|
+
if (!item || typeof item !== "object") continue;
|
|
3182
|
+
if (typeof item.taskName !== "string") continue;
|
|
3183
|
+
if (typeof item.confidence !== "number") continue;
|
|
3184
|
+
if (!validSet.has(item.taskName)) continue;
|
|
3185
|
+
const confidence = Math.max(0, Math.min(1, item.confidence));
|
|
3186
|
+
suggestions.push({
|
|
3187
|
+
taskName: item.taskName,
|
|
3188
|
+
confidence,
|
|
3189
|
+
reasoning: typeof item.reasoning === "string" ? item.reasoning : "",
|
|
3190
|
+
detectionMethod: "llm-inferred"
|
|
3191
|
+
});
|
|
3192
|
+
}
|
|
3193
|
+
return suggestions;
|
|
3194
|
+
} catch {
|
|
3195
|
+
return [];
|
|
3196
|
+
}
|
|
3197
|
+
}
|
|
3198
|
+
function extractJson(text) {
|
|
3199
|
+
if (!text || typeof text !== "string") return null;
|
|
3200
|
+
const trimmed = text.trim();
|
|
3201
|
+
const fenceMatch = trimmed.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/);
|
|
3202
|
+
if (fenceMatch) return fenceMatch[1].trim();
|
|
3203
|
+
const firstBracket = trimmed.indexOf("[");
|
|
3204
|
+
const lastBracket = trimmed.lastIndexOf("]");
|
|
3205
|
+
if (firstBracket !== -1 && lastBracket > firstBracket) {
|
|
3206
|
+
return trimmed.slice(firstBracket, lastBracket + 1);
|
|
3207
|
+
}
|
|
3208
|
+
if (trimmed.startsWith("[")) return trimmed;
|
|
3209
|
+
return null;
|
|
3210
|
+
}
|
|
3211
|
+
function createCliAdapter(opts) {
|
|
3212
|
+
const timeout = opts.timeout ?? 6e4;
|
|
3213
|
+
return {
|
|
3214
|
+
analyze: (prompt) => {
|
|
3215
|
+
return new Promise((resolve, reject) => {
|
|
3216
|
+
const args = opts.args(prompt);
|
|
3217
|
+
const child = execFile(
|
|
3218
|
+
opts.command,
|
|
3219
|
+
opts.stdin ? opts.args("") : args,
|
|
3220
|
+
{
|
|
3221
|
+
timeout,
|
|
3222
|
+
cwd: opts.cwd,
|
|
3223
|
+
env: opts.env ? { ...process.env, ...opts.env } : void 0,
|
|
3224
|
+
maxBuffer: 10 * 1024 * 1024
|
|
3225
|
+
// 10MB
|
|
3226
|
+
},
|
|
3227
|
+
(error, stdout, stderr) => {
|
|
3228
|
+
if (error) {
|
|
3229
|
+
reject(new Error(
|
|
3230
|
+
`CLI adapter failed: ${opts.command} exited with ${error.code ?? "error"}` + (stderr ? `
|
|
3231
|
+
stderr: ${stderr.slice(0, 500)}` : "") + `
|
|
3232
|
+
${error.message}`
|
|
3233
|
+
));
|
|
3234
|
+
} else {
|
|
3235
|
+
resolve(stdout);
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3238
|
+
);
|
|
3239
|
+
if (opts.stdin && child.stdin) {
|
|
3240
|
+
child.stdin.write(prompt);
|
|
3241
|
+
child.stdin.end();
|
|
3242
|
+
}
|
|
3243
|
+
});
|
|
3244
|
+
}
|
|
3245
|
+
};
|
|
3246
|
+
}
|
|
3247
|
+
function createHttpAdapter(opts) {
|
|
3248
|
+
const timeout = opts.timeout ?? 6e4;
|
|
3249
|
+
return {
|
|
3250
|
+
analyze: async (prompt) => {
|
|
3251
|
+
const body = opts.buildBody ? opts.buildBody(prompt) : { prompt };
|
|
3252
|
+
const controller = new AbortController();
|
|
3253
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
3254
|
+
try {
|
|
3255
|
+
const response = await fetch(opts.url, {
|
|
3256
|
+
method: "POST",
|
|
3257
|
+
headers: {
|
|
3258
|
+
"Content-Type": "application/json",
|
|
3259
|
+
...opts.headers ?? {}
|
|
3260
|
+
},
|
|
3261
|
+
body: JSON.stringify(body),
|
|
3262
|
+
signal: controller.signal
|
|
3263
|
+
});
|
|
3264
|
+
if (!response.ok) {
|
|
3265
|
+
const text = await response.text().catch(() => "");
|
|
3266
|
+
throw new Error(`HTTP ${response.status}: ${text.slice(0, 500)}`);
|
|
3267
|
+
}
|
|
3268
|
+
const json = await response.json();
|
|
3269
|
+
if (opts.extractResponse) {
|
|
3270
|
+
return opts.extractResponse(json);
|
|
3271
|
+
}
|
|
3272
|
+
if (typeof json.response === "string") return json.response;
|
|
3273
|
+
if (typeof json.text === "string") return json.text;
|
|
3274
|
+
if (typeof json.content === "string") return json.content;
|
|
3275
|
+
return JSON.stringify(json);
|
|
3276
|
+
} finally {
|
|
3277
|
+
clearTimeout(timer);
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
};
|
|
3281
|
+
}
|
|
3282
|
+
|
|
3283
|
+
export { COMPLETION_STRATEGIES, CONFLICT_STRATEGIES, DEFAULTS, EXECUTION_MODES, EXECUTION_STATUS, FileStore, StepMachine as FlowEngine, LocalStorageStore, MemoryStore, StepMachine, TASK_STATUS, addDynamicTask, addNode, addProvides, addRequires, apply, applyAll, applyEvent, applyInferences, applyStepResult, batch, buildInferencePrompt, checkCircuitBreaker, computeAvailableOutputs, computeStepInput, createCliAdapter, createDefaultTaskState, createStepMachine as createEngine, createHttpAdapter, createInitialExecutionState, createInitialState, createLiveGraph, createStepMachine, detectStuckState, disableNode, drainTokens, enableNode, exportGraphConfig, exportGraphConfigToFile, extractReturnData, flowToMermaid, getAllTasks, getCandidateTasks, getDownstream, getNode, getProvides, getRequires, getTask, getUnreachableNodes, getUnreachableTokens, getUpstream, graphToMermaid, hasTask, inferAndApply, inferCompletions, injectTokens, inspect, isExecutionComplete, isNonActiveTask, isRepeatableTask, isTaskCompleted, isTaskRunning, loadGraphConfig, loadStepFlow, next, planExecution, removeNode, removeProvides, removeRequires, resetNode, resolveConfigTemplates, resolveVariables, restore, schedule, snapshot, validateGraph, validateGraphConfig, validateStepFlowConfig };
|
|
2304
3284
|
//# sourceMappingURL=index.js.map
|
|
2305
3285
|
//# sourceMappingURL=index.js.map
|