@fieldwangai/agentflow 0.1.99 → 0.1.101

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.
@@ -0,0 +1,125 @@
1
+ function childHasExited(child) {
2
+ return !child || child.exitCode != null || child.signalCode != null;
3
+ }
4
+
5
+ function waitForPromise(promise, timeoutMs) {
6
+ const ms = Math.max(0, Number(timeoutMs) || 0);
7
+ if (ms === 0) return Promise.resolve({ timedOut: true });
8
+ return new Promise((resolve) => {
9
+ let settled = false;
10
+ const timer = setTimeout(() => {
11
+ if (settled) return;
12
+ settled = true;
13
+ resolve({ timedOut: true });
14
+ }, ms);
15
+ Promise.resolve(promise).then((value) => {
16
+ if (settled) return;
17
+ settled = true;
18
+ clearTimeout(timer);
19
+ resolve({ timedOut: false, value });
20
+ });
21
+ });
22
+ }
23
+
24
+ export function terminateWorkspaceChild(child, options = {}) {
25
+ if (childHasExited(child)) return false;
26
+ const signal = String(options.signal || "SIGTERM");
27
+ const processGroup = options.processGroup === true && process.platform !== "win32";
28
+ const pid = Number(child?.pid || 0);
29
+ if (processGroup && pid > 0) {
30
+ try {
31
+ process.kill(-pid, signal);
32
+ return true;
33
+ } catch {
34
+ // The group may already be gone. Fall back to the direct child.
35
+ }
36
+ }
37
+ try {
38
+ return child.kill(signal) !== false;
39
+ } catch {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ export function createWorkspaceRunController(options = {}) {
45
+ const abortController = options.abortController || new AbortController();
46
+ const gracefulTimeoutMs = Math.max(0, Number(options.gracefulTimeoutMs ?? 3_000));
47
+ const forceTimeoutMs = Math.max(0, Number(options.forceTimeoutMs ?? 1_000));
48
+ let activeChild = null;
49
+ let activeChildProcessGroup = false;
50
+ let state = "running";
51
+ let finished = false;
52
+ let finishResult = null;
53
+ let resolveDone;
54
+ let stopPromise = null;
55
+ const done = new Promise((resolve) => {
56
+ resolveDone = resolve;
57
+ });
58
+
59
+ const setChild = (child, childOptions = {}) => {
60
+ activeChild = child || null;
61
+ activeChildProcessGroup = Boolean(child && childOptions.processGroup === true);
62
+ if (state === "stopping" && activeChild) {
63
+ terminateWorkspaceChild(activeChild, {
64
+ signal: "SIGTERM",
65
+ processGroup: activeChildProcessGroup,
66
+ });
67
+ }
68
+ };
69
+
70
+ const finish = (status = "finished") => {
71
+ if (finished) return finishResult;
72
+ finished = true;
73
+ state = state === "stopping" ? "stopped" : String(status || "finished");
74
+ finishResult = { status: state };
75
+ activeChild = null;
76
+ activeChildProcessGroup = false;
77
+ resolveDone(finishResult);
78
+ return finishResult;
79
+ };
80
+
81
+ const stop = () => {
82
+ if (stopPromise) return stopPromise;
83
+ stopPromise = (async () => {
84
+ if (finished) return { stopped: true, alreadyFinished: true, status: state };
85
+ state = "stopping";
86
+ try {
87
+ abortController.abort();
88
+ } catch {
89
+ // Best effort; process termination below is authoritative.
90
+ }
91
+ terminateWorkspaceChild(activeChild, {
92
+ signal: "SIGTERM",
93
+ processGroup: activeChildProcessGroup,
94
+ });
95
+ let waited = await waitForPromise(done, gracefulTimeoutMs);
96
+ if (!waited.timedOut) return { stopped: true, forced: false, status: waited.value?.status || state };
97
+
98
+ terminateWorkspaceChild(activeChild, {
99
+ signal: "SIGKILL",
100
+ processGroup: activeChildProcessGroup,
101
+ });
102
+ waited = await waitForPromise(done, forceTimeoutMs);
103
+ if (!waited.timedOut) return { stopped: true, forced: true, status: waited.value?.status || state };
104
+ return { stopped: false, forced: true, timedOut: true, status: state };
105
+ })();
106
+ return stopPromise;
107
+ };
108
+
109
+ return {
110
+ abortController,
111
+ done,
112
+ finish,
113
+ setChild,
114
+ stop,
115
+ get child() {
116
+ return activeChild;
117
+ },
118
+ get state() {
119
+ return state;
120
+ },
121
+ get finished() {
122
+ return finished;
123
+ },
124
+ };
125
+ }