@ginkgolab/harness-core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/graph.js CHANGED
@@ -1,301 +1,11 @@
1
- // src/graph/checkpointer.ts
2
- var InMemoryCheckpointer = class {
3
- checkpoints = /* @__PURE__ */ new Map();
4
- async save(checkpoint) {
5
- this.checkpoints.set(checkpoint.id, {
6
- ...checkpoint,
7
- state: JSON.parse(JSON.stringify(checkpoint.state))
8
- });
9
- }
10
- async load(checkpointId) {
11
- const cp = this.checkpoints.get(checkpointId);
12
- return cp ? JSON.parse(JSON.stringify(cp)) : void 0;
13
- }
14
- async loadLatest(sessionId) {
15
- const list = await this.list(sessionId);
16
- return list[list.length - 1];
17
- }
18
- async list(sessionId) {
19
- const matched = [];
20
- for (const cp of this.checkpoints.values()) {
21
- if (cp.sessionId === sessionId) {
22
- matched.push(JSON.parse(JSON.stringify(cp)));
23
- }
24
- }
25
- return matched.sort((a, b) => a.createdAt - b.createdAt);
26
- }
27
- };
28
-
29
- // src/graph/stateGraph.ts
30
- import { randomUUID } from "crypto";
31
- var START = "__start__";
32
- var END = "__end__";
33
- var StateGraph = class {
34
- nodes = /* @__PURE__ */ new Map();
35
- edges = /* @__PURE__ */ new Map();
36
- conditionalEdges = /* @__PURE__ */ new Map();
37
- breakpoints = /* @__PURE__ */ new Map();
38
- /**
39
- * 注册图节点
40
- */
41
- addNode(name, action) {
42
- if (name === START || name === END) {
43
- throw new Error(`Reserved node name cannot be used: ${name}`);
44
- }
45
- this.nodes.set(name, action);
46
- return this;
47
- }
48
- /**
49
- * 注册普通静态边
50
- */
51
- addEdge(from, to) {
52
- if (from !== START && !this.nodes.has(from)) {
53
- throw new Error(`Source node does not exist: ${from}`);
54
- }
55
- if (to !== END && !this.nodes.has(to)) {
56
- throw new Error(`Target node does not exist: ${to}`);
57
- }
58
- this.edges.set(from, to);
59
- return this;
60
- }
61
- /**
62
- * 注册条件路由边
63
- */
64
- addConditionalEdge(from, router) {
65
- if (from !== START && !this.nodes.has(from)) {
66
- throw new Error(`Source node does not exist: ${from}`);
67
- }
68
- this.conditionalEdges.set(from, router);
69
- return this;
70
- }
71
- /**
72
- * 在特定节点设置人机交互断点 (Human-in-the-Loop)
73
- */
74
- addBreakpoint(node, options) {
75
- if (!this.nodes.has(node)) {
76
- throw new Error(`Cannot set breakpoint on nonexistent node: ${node}`);
77
- }
78
- if (typeof options === "string") {
79
- this.breakpoints.set(node, {
80
- when: "before",
81
- defaultReason: options
82
- });
83
- } else {
84
- this.breakpoints.set(node, {
85
- when: options?.when ?? "before",
86
- defaultReason: options?.reason ?? "Breakpoint encountered",
87
- condition: options?.condition
88
- });
89
- }
90
- return this;
91
- }
92
- /**
93
- * 编译为可执行的状态图实例
94
- */
95
- compile(options) {
96
- return new CompiledStateGraph(
97
- new Map(this.nodes),
98
- new Map(this.edges),
99
- new Map(this.conditionalEdges),
100
- new Map(this.breakpoints),
101
- options?.checkpointer ?? new InMemoryCheckpointer()
102
- );
103
- }
104
- };
105
- var CompiledStateGraph = class {
106
- constructor(nodes, edges, conditionalEdges, breakpoints, checkpointer) {
107
- this.nodes = nodes;
108
- this.edges = edges;
109
- this.conditionalEdges = conditionalEdges;
110
- this.breakpoints = breakpoints;
111
- this.checkpointer = checkpointer;
112
- }
113
- nodes;
114
- edges;
115
- conditionalEdges;
116
- breakpoints;
117
- checkpointer;
118
- /**
119
- * 从头开始调用执行图
120
- */
121
- async invoke(initialState, options) {
122
- const runId = options?.runId ?? `run-${randomUUID()}`;
123
- const sessionId = options?.sessionId ?? `sess-${randomUUID()}`;
124
- const startNode = this.edges.get(START);
125
- if (!startNode) {
126
- throw new Error("No START edge configured in graph. Call addEdge(START, 'firstNode') first.");
127
- }
128
- return this.runFrom(startNode, initialState, runId, sessionId, false, options);
129
- }
130
- /**
131
- * 从指定检查点唤醒并恢复执行 (Human-in-the-Loop Resume)
132
- */
133
- async resume(checkpointId, statePatch, options) {
134
- const checkpoint = await this.checkpointer.load(checkpointId);
135
- if (!checkpoint) {
136
- throw new Error(`Checkpoint not found: ${checkpointId}`);
137
- }
138
- const mergedState = {
139
- ...checkpoint.state,
140
- ...statePatch ?? {}
141
- };
142
- const bp = this.breakpoints.get(checkpoint.node);
143
- const resumeMode = bp?.when === "after" ? "skipToNext" : "resumeAction";
144
- if (resumeMode === "skipToNext") {
145
- const nextNode = await this.getNextNode(checkpoint.node, mergedState);
146
- if (!nextNode || nextNode === END) {
147
- return {
148
- state: mergedState,
149
- status: "completed",
150
- checkpointId
151
- };
152
- }
153
- return this.runFrom(
154
- nextNode,
155
- mergedState,
156
- options?.runId ?? checkpoint.runId,
157
- options?.sessionId ?? checkpoint.sessionId,
158
- false,
159
- options
160
- );
161
- }
162
- return this.runFrom(
163
- checkpoint.node,
164
- mergedState,
165
- options?.runId ?? checkpoint.runId,
166
- options?.sessionId ?? checkpoint.sessionId,
167
- true,
168
- // skipBeforeBreakpoint
169
- options
170
- );
171
- }
172
- /**
173
- * 执行循环
174
- */
175
- async runFrom(currentNode, initialState, runId, sessionId, skipBeforeBreakpoint, options) {
176
- let state = { ...initialState };
177
- let nodeName = currentNode;
178
- let steps = 0;
179
- const maxSteps = options?.maxSteps ?? 50;
180
- let bypassNextBeforeBp = skipBeforeBreakpoint;
181
- while (nodeName && nodeName !== END) {
182
- if (options?.signal?.aborted) {
183
- return {
184
- state,
185
- status: "aborted"
186
- };
187
- }
188
- if (++steps > maxSteps) {
189
- throw new Error(
190
- `Graph execution exceeded maximum step limit of ${maxSteps}. Check for cyclic dependencies.`
191
- );
192
- }
193
- const bp = this.breakpoints.get(nodeName);
194
- if (bp && bp.when === "before" && !bypassNextBeforeBp) {
195
- const hit = this.checkBreakpoint(bp, state);
196
- if (hit.interrupt) {
197
- const cp = {
198
- id: `cp-${randomUUID()}`,
199
- runId,
200
- sessionId,
201
- node: nodeName,
202
- state,
203
- createdAt: Date.now(),
204
- interrupt: {
205
- reason: hit.reason,
206
- payload: hit.payload
207
- }
208
- };
209
- await this.checkpointer.save(cp);
210
- return {
211
- state,
212
- status: "suspended",
213
- checkpointId: cp.id,
214
- interrupt: {
215
- node: nodeName,
216
- reason: hit.reason,
217
- payload: hit.payload
218
- }
219
- };
220
- }
221
- }
222
- bypassNextBeforeBp = false;
223
- const action = this.nodes.get(nodeName);
224
- if (!action) {
225
- throw new Error(`Node action not found for: ${nodeName}`);
226
- }
227
- const patch = await action(state, options?.signal);
228
- state = { ...state, ...patch ?? {} };
229
- if (bp && bp.when === "after") {
230
- const hit = this.checkBreakpoint(bp, state);
231
- if (hit.interrupt) {
232
- const cp = {
233
- id: `cp-${randomUUID()}`,
234
- runId,
235
- sessionId,
236
- node: nodeName,
237
- state,
238
- createdAt: Date.now(),
239
- interrupt: {
240
- reason: hit.reason,
241
- payload: hit.payload
242
- }
243
- };
244
- await this.checkpointer.save(cp);
245
- return {
246
- state,
247
- status: "suspended",
248
- checkpointId: cp.id,
249
- interrupt: {
250
- node: nodeName,
251
- reason: hit.reason,
252
- payload: hit.payload
253
- }
254
- };
255
- }
256
- }
257
- nodeName = await this.getNextNode(nodeName, state);
258
- }
259
- const finalCp = {
260
- id: `cp-${randomUUID()}`,
261
- runId,
262
- sessionId,
263
- node: END,
264
- state,
265
- createdAt: Date.now()
266
- };
267
- await this.checkpointer.save(finalCp);
268
- return {
269
- state,
270
- status: "completed",
271
- checkpointId: finalCp.id
272
- };
273
- }
274
- async getNextNode(currentNode, state) {
275
- if (this.conditionalEdges.has(currentNode)) {
276
- const router = this.conditionalEdges.get(currentNode);
277
- return router(state);
278
- }
279
- return this.edges.get(currentNode);
280
- }
281
- checkBreakpoint(bp, state) {
282
- if (!bp.condition) {
283
- return { interrupt: true, reason: bp.defaultReason ?? "Breakpoint hit" };
284
- }
285
- const res = bp.condition(state);
286
- if (typeof res === "boolean") {
287
- return {
288
- interrupt: res,
289
- reason: bp.defaultReason ?? "Breakpoint condition satisfied"
290
- };
291
- }
292
- return {
293
- interrupt: res.interrupt,
294
- reason: res.reason ?? bp.defaultReason ?? "Breakpoint condition satisfied",
295
- payload: res.payload
296
- };
297
- }
298
- };
1
+ import {
2
+ CompiledStateGraph,
3
+ END,
4
+ InMemoryCheckpointer,
5
+ START,
6
+ StateGraph
7
+ } from "./chunk-VIREJDUO.js";
8
+ import "./chunk-6OYMWYZS.js";
299
9
  export {
300
10
  CompiledStateGraph,
301
11
  END,