@jshookmcp/jshook 0.2.8 → 0.2.9
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 +36 -5
- package/README.zh.md +36 -5
- package/dist/{AntiCheatDetector-S8VRj-dD.mjs → AntiCheatDetector-BNk-EoBt.mjs} +3 -3
- package/dist/{CodeInjector-4Z3ngPoX.mjs → CodeInjector-Cq8q01kp.mjs} +5 -5
- package/dist/ConsoleMonitor-CPVQW1Y-.mjs +2201 -0
- package/dist/{DarwinAPI-B8hg_yhz.mjs → DarwinAPI-BNPxu0RH.mjs} +1 -1
- package/dist/DetailedDataManager-BQQcxh64.mjs +217 -0
- package/dist/EventBus-DgPmwpeu.mjs +141 -0
- package/dist/EvidenceGraphBridge-SFesNera.mjs +153 -0
- package/dist/{ExtensionManager-D5-bO9D8.mjs → ExtensionManager-CWYgw0YW.mjs} +13 -6
- package/dist/{FingerprintManager-BVxFJL2-.mjs → FingerprintManager-gzWtkKuf.mjs} +1 -1
- package/dist/{HardwareBreakpoint-DK1yjWkV.mjs → HardwareBreakpoint-B9gZCdFP.mjs} +3 -3
- package/dist/{HeapAnalyzer-CEbo10xU.mjs → HeapAnalyzer-BLDH0dCv.mjs} +4 -4
- package/dist/HookGeneratorBuilders.core.generators.storage-CtcdK78Q.mjs +639 -0
- package/dist/InstrumentationSession-CvPC7Jwy.mjs +244 -0
- package/dist/{MemoryController-DdtnBdD4.mjs → MemoryController-CbVdCIJF.mjs} +3 -3
- package/dist/{MemoryScanSession-RMixN3bX.mjs → MemoryScanSession-BsDZbLYm.mjs} +81 -78
- package/dist/{MemoryScanner-QjK4ld0B.mjs → MemoryScanner-Bcpml6II.mjs} +44 -18
- package/dist/{NativeMemoryManager.impl-CB6gJ0NM.mjs → NativeMemoryManager.impl-dZtA1ZGn.mjs} +14 -53
- package/dist/{NativeMemoryManager.utils-BML4q1ry.mjs → NativeMemoryManager.utils-B-FjA2mJ.mjs} +1 -1
- package/dist/{PEAnalyzer-CK0xe0Fs.mjs → PEAnalyzer-D1lzJ_VG.mjs} +2 -2
- package/dist/PageController-Bqm2kZ_X.mjs +417 -0
- package/dist/{PointerChainEngine-Cd73qu5b.mjs → PointerChainEngine-BOhyVsjx.mjs} +4 -4
- package/dist/PrerequisiteError-Dl33Svkz.mjs +20 -0
- package/dist/ResponseBuilder-D3iFYx2N.mjs +143 -0
- package/dist/ReverseEvidenceGraph-Dlsk94LC.mjs +269 -0
- package/dist/ScriptManager-aHHq0X7U.mjs +3000 -0
- package/dist/{Speedhack-CeF0XmEz.mjs → Speedhack-CqdIFlQl.mjs} +2 -2
- package/dist/{StructureAnalyzer-D4GkMduU.mjs → StructureAnalyzer-DhFaPvRO.mjs} +3 -3
- package/dist/ToolCatalog-C0JGZoOm.mjs +582 -0
- package/dist/ToolError-jh9whhMd.mjs +15 -0
- package/dist/ToolProbe-oC7aPrkv.mjs +45 -0
- package/dist/ToolRegistry-BjaF4oNz.mjs +131 -0
- package/dist/ToolRouter.policy-BWV67ZK-.mjs +304 -0
- package/dist/TraceRecorder-DgxyVbdQ.mjs +519 -0
- package/dist/{Win32API-Bc0QnQsN.mjs → Win32API-CePkipZY.mjs} +1 -1
- package/dist/{Win32Debug-DUHt9XUn.mjs → Win32Debug-BvKs-gxc.mjs} +2 -2
- package/dist/WorkflowEngine-CuvkZtWu.mjs +598 -0
- package/dist/analysis-CL9uACt9.mjs +463 -0
- package/dist/antidebug-CqDTB_uk.mjs +1081 -0
- package/dist/artifactRetention-CFEprwPw.mjs +591 -0
- package/dist/artifacts-Bk2-_uPq.mjs +59 -0
- package/dist/betterSqlite3-0pqusHHH.mjs +74 -0
- package/dist/binary-instrument-CXfpx6fT.mjs +979 -0
- package/dist/bind-helpers-xFfRF-qm.mjs +22 -0
- package/dist/boringssl-inspector-BH2D3VKc.mjs +180 -0
- package/dist/browser-BpOr5PEx.mjs +4082 -0
- package/dist/concurrency-Bt0yv1kJ.mjs +41 -0
- package/dist/{constants-CCvsN80K.mjs → constants-B0OANIBL.mjs} +88 -46
- package/dist/coordination-qUbyF8KU.mjs +259 -0
- package/dist/debugger-gnKxRSN0.mjs +1271 -0
- package/dist/definitions-6M-eejaT.mjs +53 -0
- package/dist/definitions-B18eyf0B.mjs +18 -0
- package/dist/definitions-B3QdlrHv.mjs +34 -0
- package/dist/definitions-B4rAvHNZ.mjs +63 -0
- package/dist/definitions-BB_4jnmy.mjs +37 -0
- package/dist/definitions-BMfYXoNC.mjs +43 -0
- package/dist/definitions-Beid2EB3.mjs +27 -0
- package/dist/definitions-C1UvM5Iy.mjs +126 -0
- package/dist/definitions-CXEI7QC72.mjs +216 -0
- package/dist/definitions-C_4r7Fo-2.mjs +14 -0
- package/dist/definitions-CkFDALoa.mjs +26 -0
- package/dist/definitions-Cke7zEb8.mjs +94 -0
- package/dist/definitions-ClJLzsJQ.mjs +25 -0
- package/dist/definitions-Cq-zroAU.mjs +28 -0
- package/dist/definitions-Cy3Sl6gV.mjs +34 -0
- package/dist/definitions-D3VsGcvz.mjs +47 -0
- package/dist/definitions-DVGfrn7y.mjs +96 -0
- package/dist/definitions-LKpC3-nL.mjs +9 -0
- package/dist/definitions-bAhHQJq9.mjs +359 -0
- package/dist/encoding-Bvz5jLRv.mjs +1065 -0
- package/dist/evidence-graph-bridge-C_fv9PuC.mjs +135 -0
- package/dist/{factory-CibqTNC8.mjs → factory-DxlGh9Xf.mjs} +37 -52
- package/dist/graphql-DYWzJ29s.mjs +1026 -0
- package/dist/handlers-9sAbfIg-.mjs +2552 -0
- package/dist/handlers-Bl8zkwz1.mjs +2716 -0
- package/dist/handlers-C67ktuRN.mjs +710 -0
- package/dist/handlers-C87g8oCe.mjs +276 -0
- package/dist/handlers-CTsDAO6p.mjs +681 -0
- package/dist/handlers-Cgyg6c0U.mjs +645 -0
- package/dist/handlers-D6j6yka7.mjs +2124 -0
- package/dist/handlers-DdFzXLvF.mjs +446 -0
- package/dist/handlers-DeLOCd5m.mjs +799 -0
- package/dist/handlers-DlCJN4Td.mjs +757 -0
- package/dist/handlers-DxGIq15_2.mjs +917 -0
- package/dist/handlers-U6L4xhuF.mjs +585 -0
- package/dist/handlers-tB9Mp9ZK.mjs +84 -0
- package/dist/handlers-tiy7EIBp.mjs +572 -0
- package/dist/handlers.impl-DS0d9fUw.mjs +761 -0
- package/dist/hooks-CzCWByww.mjs +898 -0
- package/dist/index.mjs +377 -155
- package/dist/{logger-BmWzC2lM.mjs → logger-Dh_xb7_2.mjs} +14 -6
- package/dist/maintenance-P7ePRXQC.mjs +830 -0
- package/dist/manifest-2ToTpjv8.mjs +106 -0
- package/dist/manifest-3g71z6Bg.mjs +79 -0
- package/dist/manifest-82baTv4U.mjs +45 -0
- package/dist/manifest-B3QVVeBS.mjs +82 -0
- package/dist/manifest-BB2J8IMJ.mjs +149 -0
- package/dist/manifest-BKbgbSiY.mjs +60 -0
- package/dist/manifest-Bcf-TJzH.mjs +848 -0
- package/dist/manifest-BmtZzQiQ2.mjs +45 -0
- package/dist/manifest-Bnd7kqEY.mjs +55 -0
- package/dist/manifest-BqQX6OQC2.mjs +65 -0
- package/dist/manifest-BqrQ4Tpj.mjs +81 -0
- package/dist/manifest-Br4RPFt5.mjs +370 -0
- package/dist/manifest-C5qDjysN.mjs +107 -0
- package/dist/manifest-C9RT5nk32.mjs +34 -0
- package/dist/manifest-CAhOuvSl.mjs +204 -0
- package/dist/manifest-CBYWCUBJ.mjs +51 -0
- package/dist/manifest-CFADCRa1.mjs +37 -0
- package/dist/manifest-CQVhavRF.mjs +114 -0
- package/dist/manifest-CT7zZBV1.mjs +48 -0
- package/dist/manifest-CV12bcrF.mjs +121 -0
- package/dist/manifest-CXsRWjjI.mjs +224 -0
- package/dist/manifest-CZLUCfG02.mjs +95 -0
- package/dist/manifest-D6phHKFd.mjs +131 -0
- package/dist/manifest-DCyjf4n2.mjs +294 -0
- package/dist/manifest-DHsnKgP6.mjs +60 -0
- package/dist/manifest-Df_dliIe.mjs +55 -0
- package/dist/manifest-Dh8WBmEW.mjs +129 -0
- package/dist/manifest-DhKRAT8_.mjs +92 -0
- package/dist/manifest-DlpTj4ic2.mjs +193 -0
- package/dist/manifest-DrbmZcFl2.mjs +253 -0
- package/dist/manifest-DuwHjUa5.mjs +70 -0
- package/dist/manifest-DzwvxPJX.mjs +38 -0
- package/dist/manifest-NXctwWQq.mjs +68 -0
- package/dist/manifest-Sc_0JQ13.mjs +418 -0
- package/dist/manifest-gZ4s_UtG.mjs +96 -0
- package/dist/manifest-qSleDqdO.mjs +1023 -0
- package/dist/modules-C184v-S9.mjs +11365 -0
- package/dist/mojo-ipc-B_H61Afw.mjs +525 -0
- package/dist/network-671Cw6hV.mjs +3346 -0
- package/dist/{artifacts-BbdOMET5.mjs → outputPaths-B1uGmrWZ.mjs} +219 -212
- package/dist/parse-args-BlRjqlkL.mjs +39 -0
- package/dist/platform-WmNn8Sxb.mjs +2070 -0
- package/dist/process-QcbIy5Zq.mjs +1401 -0
- package/dist/proxy-DqNs0bAd.mjs +170 -0
- package/dist/registry-D-6e18lB.mjs +34 -0
- package/dist/response-BQVP-xUn.mjs +28 -0
- package/dist/server/plugin-api.mjs +2 -2
- package/dist/shared-state-board-DV-dpHFJ.mjs +586 -0
- package/dist/sourcemap-Dq8ez8vS.mjs +650 -0
- package/dist/ssrf-policy-ZaUfvhq7.mjs +166 -0
- package/dist/streaming-BUQ0VJsg.mjs +725 -0
- package/dist/tool-builder-DCbIC5Eo.mjs +186 -0
- package/dist/transform-CiYJfNX0.mjs +1007 -0
- package/dist/types-Bx92KJfT.mjs +4 -0
- package/dist/wasm-DQTnHDs4.mjs +531 -0
- package/dist/workflow-f3xJOcjx.mjs +725 -0
- package/package.json +16 -16
- package/dist/ExtensionManager-CPTJhHFg.mjs +0 -2
- package/dist/ToolCatalog-Bq4V2sbJ.mjs +0 -67201
- package/dist/{CacheAdapters-CzFNpD9a.mjs → CacheAdapters-CDe5WPSV.mjs} +0 -0
- package/dist/{StealthVerifier-BzBCFiwx.mjs → StealthVerifier-Bo4T3bz8.mjs} +0 -0
- package/dist/{VersionDetector-CNXcvD46.mjs → VersionDetector-CwVLVdDM.mjs} +0 -0
- package/dist/{formatAddress-ChCSIRWT.mjs → formatAddress-DVkj9kpI.mjs} +0 -0
- package/dist/{types-BBjOqye-.mjs → types-CPhOReNX.mjs} +1 -1
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-CjcI7cDX.mjs";
|
|
2
|
+
import { t as logger } from "./logger-Dh_xb7_2.mjs";
|
|
3
|
+
import { a as getEffectivePrerequisites, c as getRoutingState } from "./ToolRouter.policy-BWV67ZK-.mjs";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
//#region src/server/workflows/WorkflowRunStore.ts
|
|
6
|
+
var WorkflowRunStore = class {
|
|
7
|
+
runs = /* @__PURE__ */ new Map();
|
|
8
|
+
lastSuccess = /* @__PURE__ */ new Map();
|
|
9
|
+
recordSuccess(result) {
|
|
10
|
+
const entry = {
|
|
11
|
+
workflowId: result.workflowId,
|
|
12
|
+
runId: result.runId,
|
|
13
|
+
startedAt: result.startedAt,
|
|
14
|
+
finishedAt: result.finishedAt,
|
|
15
|
+
durationMs: result.durationMs,
|
|
16
|
+
status: "success",
|
|
17
|
+
stepResultKeys: Object.keys(result.stepResults)
|
|
18
|
+
};
|
|
19
|
+
this.runs.set(result.runId, entry);
|
|
20
|
+
this.lastSuccess.set(result.workflowId, result);
|
|
21
|
+
logger.debug(`workflow run recorded: ${result.runId} (${result.workflowId})`);
|
|
22
|
+
}
|
|
23
|
+
recordError(workflowId, runId, startedAt, error) {
|
|
24
|
+
const entry = {
|
|
25
|
+
workflowId,
|
|
26
|
+
runId,
|
|
27
|
+
startedAt,
|
|
28
|
+
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
29
|
+
durationMs: Date.now() - new Date(startedAt).getTime(),
|
|
30
|
+
status: "error",
|
|
31
|
+
stepResultKeys: []
|
|
32
|
+
};
|
|
33
|
+
this.runs.set(runId, entry);
|
|
34
|
+
logger.debug(`workflow run error: ${runId} (${workflowId}): ${error}`);
|
|
35
|
+
}
|
|
36
|
+
getRun(runId) {
|
|
37
|
+
return this.runs.get(runId);
|
|
38
|
+
}
|
|
39
|
+
getLastSuccess(workflowId) {
|
|
40
|
+
return this.lastSuccess.get(workflowId);
|
|
41
|
+
}
|
|
42
|
+
listRuns(workflowId) {
|
|
43
|
+
const entries = [...this.runs.values()];
|
|
44
|
+
if (workflowId) return entries.filter((e) => e.workflowId === workflowId);
|
|
45
|
+
return entries;
|
|
46
|
+
}
|
|
47
|
+
clear() {
|
|
48
|
+
this.runs.clear();
|
|
49
|
+
this.lastSuccess.clear();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/server/workflows/WorkflowEngine.ts
|
|
54
|
+
var WorkflowEngine_exports = /* @__PURE__ */ __exportAll({ executeExtensionWorkflow: () => executeExtensionWorkflow });
|
|
55
|
+
/**
|
|
56
|
+
* WorkflowDataBus — cross-node data bus for dynamic parameter passing.
|
|
57
|
+
*
|
|
58
|
+
* Supports expression templates like "${get-requests.scriptId}" to reference
|
|
59
|
+
* outputs from previous steps.
|
|
60
|
+
*/
|
|
61
|
+
var WorkflowDataBus = class {
|
|
62
|
+
store = /* @__PURE__ */ new Map();
|
|
63
|
+
set(key, value) {
|
|
64
|
+
this.store.set(key, value);
|
|
65
|
+
}
|
|
66
|
+
get(key) {
|
|
67
|
+
return this.store.get(key);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get a value at a specific path within a stored object.
|
|
71
|
+
* @param key - The key in the store
|
|
72
|
+
* @param path - Dot-separated path (e.g., "content.0.text")
|
|
73
|
+
*/
|
|
74
|
+
getValueAtPath(key, path) {
|
|
75
|
+
const value = this.store.get(key);
|
|
76
|
+
if (!value || typeof value !== "object") return value;
|
|
77
|
+
const obj = parseToolPayload(value) || value;
|
|
78
|
+
return path.split(".").reduce((current, segment) => {
|
|
79
|
+
if (current && typeof current === "object") {
|
|
80
|
+
const arrayMatch = segment.match(/^(\d+)$/);
|
|
81
|
+
if (arrayMatch && Array.isArray(current)) return current[Number(arrayMatch[1])];
|
|
82
|
+
return current[segment];
|
|
83
|
+
}
|
|
84
|
+
}, obj);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolve expression templates like "${stepId.fieldPath}".
|
|
88
|
+
* If the value is not an expression, returns it as-is.
|
|
89
|
+
*/
|
|
90
|
+
resolve(template) {
|
|
91
|
+
const match = template.match(/^\$\{(.+)\}$/);
|
|
92
|
+
if (!match || !match[1]) return template;
|
|
93
|
+
const ref = match[1];
|
|
94
|
+
const dotIndex = ref.indexOf(".");
|
|
95
|
+
if (dotIndex === -1) return this.store.get(ref);
|
|
96
|
+
const stepId = ref.slice(0, dotIndex);
|
|
97
|
+
const fieldPath = ref.slice(dotIndex + 1);
|
|
98
|
+
return this.getValueAtPath(stepId, fieldPath);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var PreflightError = class extends Error {
|
|
102
|
+
constructor(warnings) {
|
|
103
|
+
super(`Workflow preflight failed with ${warnings.length} unsatisfied prerequisite(s)`);
|
|
104
|
+
this.warnings = warnings;
|
|
105
|
+
this.name = "PreflightError";
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const globalRunStore = new WorkflowRunStore();
|
|
109
|
+
function extractConfigValue(config, path, fallback) {
|
|
110
|
+
const segments = path.split(".").filter(Boolean);
|
|
111
|
+
let current = config;
|
|
112
|
+
for (const segment of segments) {
|
|
113
|
+
if (!current || typeof current !== "object") return fallback;
|
|
114
|
+
current = current[segment];
|
|
115
|
+
}
|
|
116
|
+
return current ?? fallback;
|
|
117
|
+
}
|
|
118
|
+
function sleep(ms) {
|
|
119
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
120
|
+
}
|
|
121
|
+
async function withTimeout(promise, timeoutMs, label) {
|
|
122
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) return promise;
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
const timeoutId = setTimeout(() => reject(/* @__PURE__ */ new Error(`${label} timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
125
|
+
promise.then((value) => {
|
|
126
|
+
clearTimeout(timeoutId);
|
|
127
|
+
resolve(value);
|
|
128
|
+
}, (error) => {
|
|
129
|
+
clearTimeout(timeoutId);
|
|
130
|
+
reject(error);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
function parseToolPayload(response) {
|
|
135
|
+
if (!response || typeof response !== "object") return void 0;
|
|
136
|
+
const text = response.content?.find((item) => item.type === "text")?.text;
|
|
137
|
+
if (typeof text !== "string") return void 0;
|
|
138
|
+
try {
|
|
139
|
+
const parsed = JSON.parse(text);
|
|
140
|
+
return parsed && typeof parsed === "object" ? parsed : void 0;
|
|
141
|
+
} catch {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function responseIndicatesFailure(response) {
|
|
146
|
+
if (!response || typeof response !== "object") return void 0;
|
|
147
|
+
if (response.isError) return "Tool returned MCP error response";
|
|
148
|
+
const payload = parseToolPayload(response);
|
|
149
|
+
if (payload?.success === false) return typeof payload.error === "string" ? payload.error : "Tool reported success=false";
|
|
150
|
+
}
|
|
151
|
+
function collectSuccessStats(value) {
|
|
152
|
+
if (Array.isArray(value)) return value.reduce((acc, item) => {
|
|
153
|
+
const next = collectSuccessStats(item);
|
|
154
|
+
acc.success += next.success;
|
|
155
|
+
acc.failure += next.failure;
|
|
156
|
+
return acc;
|
|
157
|
+
}, {
|
|
158
|
+
success: 0,
|
|
159
|
+
failure: 0
|
|
160
|
+
});
|
|
161
|
+
if (!value || typeof value !== "object") return {
|
|
162
|
+
success: 0,
|
|
163
|
+
failure: 0
|
|
164
|
+
};
|
|
165
|
+
const obj = value;
|
|
166
|
+
if (Array.isArray(obj["__order"])) return obj["__order"].reduce((acc, key) => {
|
|
167
|
+
const next = collectSuccessStats(obj[key]);
|
|
168
|
+
acc.success += next.success;
|
|
169
|
+
acc.failure += next.failure;
|
|
170
|
+
return acc;
|
|
171
|
+
}, {
|
|
172
|
+
success: 0,
|
|
173
|
+
failure: 0
|
|
174
|
+
});
|
|
175
|
+
const payload = parseToolPayload(value);
|
|
176
|
+
if (payload?.success === true) return {
|
|
177
|
+
success: 1,
|
|
178
|
+
failure: 0
|
|
179
|
+
};
|
|
180
|
+
/* istanbul ignore next */
|
|
181
|
+
if (payload?.success === false) return {
|
|
182
|
+
success: 0,
|
|
183
|
+
failure: 1
|
|
184
|
+
};
|
|
185
|
+
if ("error" in value) return {
|
|
186
|
+
success: 0,
|
|
187
|
+
failure: 1
|
|
188
|
+
};
|
|
189
|
+
return {
|
|
190
|
+
success: 0,
|
|
191
|
+
failure: 0
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function resolveInputFrom(mapping, dataBus) {
|
|
195
|
+
const resolved = {};
|
|
196
|
+
for (const [targetKey, sourceRef] of Object.entries(mapping)) {
|
|
197
|
+
const template = sourceRef.startsWith("${") ? sourceRef : `\${${sourceRef}}`;
|
|
198
|
+
resolved[targetKey] = dataBus.resolve(template);
|
|
199
|
+
}
|
|
200
|
+
return resolved;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Recursively resolve expression templates in input values.
|
|
204
|
+
* Handles nested objects and arrays.
|
|
205
|
+
*/
|
|
206
|
+
function resolveInputValues(input, dataBus) {
|
|
207
|
+
if (!input) return {};
|
|
208
|
+
const resolved = {};
|
|
209
|
+
for (const [key, value] of Object.entries(input)) resolved[key] = resolveValue(value, dataBus);
|
|
210
|
+
return resolved;
|
|
211
|
+
}
|
|
212
|
+
function resolveValue(value, dataBus) {
|
|
213
|
+
if (typeof value === "string") return dataBus.resolve(value);
|
|
214
|
+
if (Array.isArray(value)) return value.map((item) => resolveValue(item, dataBus));
|
|
215
|
+
if (value && typeof value === "object") {
|
|
216
|
+
const resolved = {};
|
|
217
|
+
for (const [k, v] of Object.entries(value)) resolved[k] = resolveValue(v, dataBus);
|
|
218
|
+
return resolved;
|
|
219
|
+
}
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
async function runToolNode(ctx, node, overrides, executionContext) {
|
|
223
|
+
const fromResolved = node.inputFrom ? resolveInputFrom(node.inputFrom, executionContext.dataBus) : {};
|
|
224
|
+
const mergedInput = {
|
|
225
|
+
...node.input ? resolveInputValues(node.input, executionContext.dataBus) : {},
|
|
226
|
+
...fromResolved,
|
|
227
|
+
...overrides?.[node.id]
|
|
228
|
+
};
|
|
229
|
+
const runAttempt = async () => {
|
|
230
|
+
const response = await withTimeout(ctx.executeToolWithTracking(node.toolName, mergedInput), node.timeoutMs ?? 0, `Workflow tool node "${node.id}"`);
|
|
231
|
+
const failure = responseIndicatesFailure(response);
|
|
232
|
+
if (failure) throw new Error(failure);
|
|
233
|
+
executionContext.dataBus.set(node.id, response);
|
|
234
|
+
return response;
|
|
235
|
+
};
|
|
236
|
+
const retry = node.retry;
|
|
237
|
+
if (!retry) return runAttempt();
|
|
238
|
+
let attempt = 0;
|
|
239
|
+
let backoffMs = retry.backoffMs;
|
|
240
|
+
while (attempt < retry.maxAttempts) try {
|
|
241
|
+
return await runAttempt();
|
|
242
|
+
} catch (error) {
|
|
243
|
+
attempt += 1;
|
|
244
|
+
if (attempt >= retry.maxAttempts) throw error;
|
|
245
|
+
await sleep(backoffMs);
|
|
246
|
+
backoffMs = Math.max(0, Math.floor(backoffMs * (retry.multiplier ?? 1)));
|
|
247
|
+
}
|
|
248
|
+
throw new Error(`Workflow tool node "${node.id}" exhausted retries`);
|
|
249
|
+
}
|
|
250
|
+
async function runParallelNode(ctx, node, executionContext, options) {
|
|
251
|
+
const concurrency = Math.max(1, node.maxConcurrency ?? 4);
|
|
252
|
+
const keyedResults = { __order: [] };
|
|
253
|
+
let nextIndex = 0;
|
|
254
|
+
let stopped = false;
|
|
255
|
+
const worker = async () => {
|
|
256
|
+
while (true) {
|
|
257
|
+
if (stopped) return;
|
|
258
|
+
const currentIndex = nextIndex;
|
|
259
|
+
nextIndex += 1;
|
|
260
|
+
if (currentIndex >= node.steps.length) return;
|
|
261
|
+
const step = node.steps[currentIndex];
|
|
262
|
+
/* istanbul ignore next */
|
|
263
|
+
if (!step) return;
|
|
264
|
+
try {
|
|
265
|
+
const result = await executeNode(ctx, step, executionContext, options);
|
|
266
|
+
keyedResults[step.id] = result;
|
|
267
|
+
keyedResults.__order.push(step.id);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
if (node.failFast) {
|
|
270
|
+
stopped = true;
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
keyedResults[step.id] = {
|
|
274
|
+
success: false,
|
|
275
|
+
error: error instanceof Error ? error.message : String(error)
|
|
276
|
+
};
|
|
277
|
+
keyedResults.__order.push(step.id);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, node.steps.length) }, () => worker()));
|
|
282
|
+
return keyedResults;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get a variable value from workflow context by key path.
|
|
286
|
+
* Supports dot notation for nested access within step results.
|
|
287
|
+
*/
|
|
288
|
+
function getWorkflowVariable(stepResults, keyPath) {
|
|
289
|
+
if (stepResults.has(keyPath)) return stepResults.get(keyPath);
|
|
290
|
+
const segments = keyPath.split(".");
|
|
291
|
+
const stepId = segments[0];
|
|
292
|
+
const fieldSegments = segments.slice(1);
|
|
293
|
+
if (!stepId || !stepResults.has(stepId)) return;
|
|
294
|
+
let current = stepResults.get(stepId);
|
|
295
|
+
if (current && typeof current === "object") {
|
|
296
|
+
const payload = parseToolPayload(current);
|
|
297
|
+
if (payload) current = payload;
|
|
298
|
+
}
|
|
299
|
+
for (const segment of fieldSegments) if (current && typeof current === "object") {
|
|
300
|
+
const arrayMatch = segment.match(/^(\d+)$/);
|
|
301
|
+
if (arrayMatch && Array.isArray(current)) {
|
|
302
|
+
current = current[Number(arrayMatch[1])];
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
current = current[segment];
|
|
306
|
+
} else return;
|
|
307
|
+
return current;
|
|
308
|
+
}
|
|
309
|
+
async function evaluatePredicate(node, ctx) {
|
|
310
|
+
if (node.predicateFn) return await node.predicateFn(ctx);
|
|
311
|
+
if (node.predicateId === "always_true") return true;
|
|
312
|
+
if (node.predicateId === "always_false") return false;
|
|
313
|
+
if (node.predicateId === "any_step_failed") return [...ctx.stepResults.values()].some((value) => collectSuccessStats(value).failure > 0);
|
|
314
|
+
const successRateMatch = node.predicateId.match(/success_rate_gte_(\d+)/i);
|
|
315
|
+
if (successRateMatch) {
|
|
316
|
+
const threshold = Number(successRateMatch[1]);
|
|
317
|
+
const aggregate = [...ctx.stepResults.values()].reduce((acc, value) => {
|
|
318
|
+
const next = collectSuccessStats(value);
|
|
319
|
+
acc.success += next.success;
|
|
320
|
+
acc.failure += next.failure;
|
|
321
|
+
return acc;
|
|
322
|
+
}, {
|
|
323
|
+
success: 0,
|
|
324
|
+
failure: 0
|
|
325
|
+
});
|
|
326
|
+
const total = aggregate.success + aggregate.failure;
|
|
327
|
+
if (total === 0) return false;
|
|
328
|
+
return aggregate.success / total >= threshold / 100;
|
|
329
|
+
}
|
|
330
|
+
const equalsMatch = node.predicateId.match(/^variable_equals_(.+?)_(.+)$/);
|
|
331
|
+
if (equalsMatch && equalsMatch[1] && equalsMatch[2]) {
|
|
332
|
+
const keyPath = equalsMatch[1];
|
|
333
|
+
const expectedValue = equalsMatch[2];
|
|
334
|
+
return deepEquals(getWorkflowVariable(ctx.stepResults, keyPath), expectedValue);
|
|
335
|
+
}
|
|
336
|
+
const containsMatch = node.predicateId.match(/^variable_contains_(.+?)_(.+)$/);
|
|
337
|
+
if (containsMatch && containsMatch[1] && containsMatch[2]) {
|
|
338
|
+
const keyPath = containsMatch[1];
|
|
339
|
+
const substring = containsMatch[2];
|
|
340
|
+
const value = getWorkflowVariable(ctx.stepResults, keyPath);
|
|
341
|
+
if (typeof value !== "string" && !Array.isArray(value)) return false;
|
|
342
|
+
return String(value).includes(substring);
|
|
343
|
+
}
|
|
344
|
+
const matchesMatch = node.predicateId.match(/^variable_matches_(.+?)_(.+)$/);
|
|
345
|
+
if (matchesMatch && matchesMatch[1] && matchesMatch[2]) {
|
|
346
|
+
const keyPath = matchesMatch[1];
|
|
347
|
+
const pattern = matchesMatch[2];
|
|
348
|
+
const value = getWorkflowVariable(ctx.stepResults, keyPath);
|
|
349
|
+
if (typeof value !== "string") return false;
|
|
350
|
+
try {
|
|
351
|
+
return new RegExp(pattern).test(value);
|
|
352
|
+
} catch {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
throw new Error(`Unknown workflow predicateId "${node.predicateId}"`);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Deep equality check for two values.
|
|
360
|
+
*/
|
|
361
|
+
function deepEquals(a, b) {
|
|
362
|
+
if (a === b) return true;
|
|
363
|
+
if (typeof a !== typeof b) return false;
|
|
364
|
+
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
365
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
366
|
+
if (Array.isArray(a)) {
|
|
367
|
+
const arrA = a;
|
|
368
|
+
const arrB = b;
|
|
369
|
+
return arrA.length === arrB.length && arrA.every((v, i) => deepEquals(v, arrB[i]));
|
|
370
|
+
}
|
|
371
|
+
const keysA = Object.keys(a);
|
|
372
|
+
const keysB = Object.keys(b);
|
|
373
|
+
if (keysA.length !== keysB.length) return false;
|
|
374
|
+
return keysA.every((key) => deepEquals(a[key], b[key]));
|
|
375
|
+
}
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
async function executeNode(ctx, node, executionContext, options) {
|
|
379
|
+
executionContext.emitSpan("workflow.node.start", {
|
|
380
|
+
nodeId: node.id,
|
|
381
|
+
kind: node.kind
|
|
382
|
+
});
|
|
383
|
+
let result;
|
|
384
|
+
switch (node.kind) {
|
|
385
|
+
case "tool":
|
|
386
|
+
result = await runToolNode(ctx, node, options.nodeInputOverrides, executionContext);
|
|
387
|
+
break;
|
|
388
|
+
case "sequence": {
|
|
389
|
+
const sequenceNode = node;
|
|
390
|
+
const items = [];
|
|
391
|
+
for (const step of sequenceNode.steps) items.push(await executeNode(ctx, step, executionContext, options));
|
|
392
|
+
result = items;
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "parallel":
|
|
396
|
+
result = await runParallelNode(ctx, node, executionContext, options);
|
|
397
|
+
break;
|
|
398
|
+
case "branch": {
|
|
399
|
+
const branchNode = node;
|
|
400
|
+
const selected = await evaluatePredicate(branchNode, executionContext) ? branchNode.whenTrue : branchNode.whenFalse;
|
|
401
|
+
if (selected) result = await executeNode(ctx, selected, executionContext, options);
|
|
402
|
+
else result = void 0;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
case "fallback": {
|
|
406
|
+
const fallbackNode = node;
|
|
407
|
+
try {
|
|
408
|
+
result = await executeNode(ctx, fallbackNode.primary, executionContext, options);
|
|
409
|
+
} catch (error) {
|
|
410
|
+
executionContext.emitSpan("workflow.node.fallback", {
|
|
411
|
+
nodeId: fallbackNode.id,
|
|
412
|
+
primaryNodeId: fallbackNode.primary.id,
|
|
413
|
+
fallbackNodeId: fallbackNode.fallback.id,
|
|
414
|
+
error: error instanceof Error ? error.message : String(error)
|
|
415
|
+
});
|
|
416
|
+
result = await executeNode(ctx, fallbackNode.fallback, executionContext, options);
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
default: throw new Error(`Unsupported workflow node kind: ${node.kind}`);
|
|
421
|
+
}
|
|
422
|
+
executionContext.stepResults.set(node.id, result);
|
|
423
|
+
executionContext.emitSpan("workflow.node.finish", {
|
|
424
|
+
nodeId: node.id,
|
|
425
|
+
kind: node.kind
|
|
426
|
+
});
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
/** Recursively collect all ToolNode instances from a workflow graph. */
|
|
430
|
+
function collectToolNodes(node) {
|
|
431
|
+
switch (node.kind) {
|
|
432
|
+
case "tool": return [node];
|
|
433
|
+
case "sequence":
|
|
434
|
+
case "parallel": return node.steps.flatMap((step) => collectToolNodes(step));
|
|
435
|
+
case "branch": return [...collectToolNodes(node.whenTrue), ...node.whenFalse ? collectToolNodes(node.whenFalse) : []];
|
|
436
|
+
case "fallback": {
|
|
437
|
+
const fallbackNode = node;
|
|
438
|
+
return [...collectToolNodes(fallbackNode.primary), ...collectToolNodes(fallbackNode.fallback)];
|
|
439
|
+
}
|
|
440
|
+
default: return [];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Collect unsatisfied prerequisites for all tool nodes in the workflow graph.
|
|
445
|
+
* Returns an array of warnings (not errors — preflight is warn-only mode).
|
|
446
|
+
*/
|
|
447
|
+
function getEvidenceState(ctx) {
|
|
448
|
+
try {
|
|
449
|
+
const evidenceGraph = ctx.getDomainInstance("evidenceGraph");
|
|
450
|
+
return evidenceGraph ? {
|
|
451
|
+
hasGraph: true,
|
|
452
|
+
nodeCount: evidenceGraph.nodeCount,
|
|
453
|
+
edgeCount: evidenceGraph.edgeCount
|
|
454
|
+
} : {
|
|
455
|
+
hasGraph: false,
|
|
456
|
+
nodeCount: 0,
|
|
457
|
+
edgeCount: 0
|
|
458
|
+
};
|
|
459
|
+
} catch {
|
|
460
|
+
return {
|
|
461
|
+
hasGraph: false,
|
|
462
|
+
nodeCount: 0,
|
|
463
|
+
edgeCount: 0
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
function collectUnsatisfiedPrerequisites(graph, routingState) {
|
|
468
|
+
const prerequisites = getEffectivePrerequisites();
|
|
469
|
+
const warnings = [];
|
|
470
|
+
for (const toolNode of collectToolNodes(graph)) {
|
|
471
|
+
const toolPrerequisites = prerequisites[toolNode.toolName] ?? [];
|
|
472
|
+
for (const prerequisite of toolPrerequisites) {
|
|
473
|
+
if (prerequisite.check(routingState)) continue;
|
|
474
|
+
warnings.push({
|
|
475
|
+
nodeId: toolNode.id,
|
|
476
|
+
toolName: toolNode.toolName,
|
|
477
|
+
condition: prerequisite.condition,
|
|
478
|
+
fix: prerequisite.fix
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return warnings;
|
|
483
|
+
}
|
|
484
|
+
async function executeExtensionWorkflow(ctx, workflow, options = {}) {
|
|
485
|
+
const runId = randomUUID();
|
|
486
|
+
const profile = options.profile ?? String(ctx.baseTier ?? "workflow");
|
|
487
|
+
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
488
|
+
const startedAtMs = Date.now();
|
|
489
|
+
const metrics = [];
|
|
490
|
+
const spans = [];
|
|
491
|
+
const stepResults = /* @__PURE__ */ new Map();
|
|
492
|
+
const dataBus = new WorkflowDataBus();
|
|
493
|
+
const mergedConfig = options.config ? {
|
|
494
|
+
...ctx.config,
|
|
495
|
+
...options.config
|
|
496
|
+
} : ctx.config;
|
|
497
|
+
const executionContext = {
|
|
498
|
+
workflowRunId: runId,
|
|
499
|
+
profile,
|
|
500
|
+
stepResults,
|
|
501
|
+
dataBus,
|
|
502
|
+
invokeTool(toolName, args) {
|
|
503
|
+
return ctx.executeToolWithTracking(toolName, args);
|
|
504
|
+
},
|
|
505
|
+
emitSpan(name, attrs) {
|
|
506
|
+
spans.push({
|
|
507
|
+
name,
|
|
508
|
+
attrs,
|
|
509
|
+
at: (/* @__PURE__ */ new Date()).toISOString()
|
|
510
|
+
});
|
|
511
|
+
},
|
|
512
|
+
emitMetric(name, value, type, attrs) {
|
|
513
|
+
metrics.push({
|
|
514
|
+
name,
|
|
515
|
+
value,
|
|
516
|
+
type,
|
|
517
|
+
attrs,
|
|
518
|
+
at: (/* @__PURE__ */ new Date()).toISOString()
|
|
519
|
+
});
|
|
520
|
+
},
|
|
521
|
+
getConfig(path, fallback) {
|
|
522
|
+
return extractConfigValue(mergedConfig, path, fallback);
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
try {
|
|
526
|
+
await workflow.onStart?.(executionContext);
|
|
527
|
+
const graph = workflow.build(executionContext);
|
|
528
|
+
const preflightMode = options.preflightMode ?? "warn";
|
|
529
|
+
let preflightWarnings = [];
|
|
530
|
+
if (preflightMode === "skip") executionContext.emitSpan("workflow.preflight", {
|
|
531
|
+
mode: preflightMode,
|
|
532
|
+
skipped: true,
|
|
533
|
+
evidenceState: getEvidenceState(ctx),
|
|
534
|
+
warningCount: 0
|
|
535
|
+
});
|
|
536
|
+
else try {
|
|
537
|
+
const routingState = await getRoutingState(ctx);
|
|
538
|
+
const evidenceState = getEvidenceState(ctx);
|
|
539
|
+
preflightWarnings = collectUnsatisfiedPrerequisites(graph, routingState);
|
|
540
|
+
executionContext.emitSpan("workflow.preflight", {
|
|
541
|
+
mode: preflightMode,
|
|
542
|
+
routingState,
|
|
543
|
+
evidenceState,
|
|
544
|
+
warningCount: preflightWarnings.length,
|
|
545
|
+
warnings: preflightWarnings
|
|
546
|
+
});
|
|
547
|
+
if (preflightMode === "strict" && preflightWarnings.length > 0) throw new PreflightError(preflightWarnings);
|
|
548
|
+
} catch (error) {
|
|
549
|
+
if (error instanceof PreflightError) throw error;
|
|
550
|
+
executionContext.emitSpan("workflow.preflight", {
|
|
551
|
+
mode: preflightMode,
|
|
552
|
+
warningCount: 0,
|
|
553
|
+
skipped: true,
|
|
554
|
+
error: error instanceof Error ? error.message : String(error),
|
|
555
|
+
evidenceState: getEvidenceState(ctx)
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
const result = await withTimeout(executeNode(ctx, graph, executionContext, options), options.timeoutMs ?? workflow.timeoutMs ?? 0, `Workflow "${workflow.id}"`);
|
|
559
|
+
await workflow.onFinish?.(executionContext, result);
|
|
560
|
+
try {
|
|
561
|
+
const evidenceGraph = typeof ctx.getDomainInstance === "function" ? ctx.getDomainInstance("evidenceGraph") : void 0;
|
|
562
|
+
if (evidenceGraph && evidenceGraph.nodeCount > 0) {
|
|
563
|
+
stepResults.set("__evidenceSnapshot", evidenceGraph.exportJson());
|
|
564
|
+
executionContext.emitSpan("workflow.evidence.auto-export", {
|
|
565
|
+
nodeCount: evidenceGraph.nodeCount,
|
|
566
|
+
edgeCount: evidenceGraph.edgeCount
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
} catch (exportError) {
|
|
570
|
+
executionContext.emitSpan("workflow.evidence.auto-export", {
|
|
571
|
+
skipped: true,
|
|
572
|
+
error: exportError instanceof Error ? exportError.message : String(exportError)
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
const runResult = {
|
|
576
|
+
workflowId: workflow.id,
|
|
577
|
+
displayName: workflow.displayName,
|
|
578
|
+
runId,
|
|
579
|
+
profile,
|
|
580
|
+
startedAt,
|
|
581
|
+
finishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
582
|
+
durationMs: Date.now() - startedAtMs,
|
|
583
|
+
result,
|
|
584
|
+
stepResults: Object.fromEntries(stepResults.entries()),
|
|
585
|
+
metrics,
|
|
586
|
+
spans
|
|
587
|
+
};
|
|
588
|
+
globalRunStore.recordSuccess(runResult);
|
|
589
|
+
return runResult;
|
|
590
|
+
} catch (error) {
|
|
591
|
+
const workflowError = error instanceof Error ? error : new Error(String(error));
|
|
592
|
+
globalRunStore.recordError(workflow.id, runId, startedAt, workflowError);
|
|
593
|
+
await workflow.onError?.(executionContext, workflowError);
|
|
594
|
+
throw workflowError;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
//#endregion
|
|
598
|
+
export { executeExtensionWorkflow as n, WorkflowEngine_exports as t };
|