darwin-langgraph 0.1.0-alpha.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/CHANGELOG.md +119 -0
- package/LICENSE +21 -0
- package/README.md +276 -0
- package/dist/create-darwin-node.d.ts +151 -0
- package/dist/create-darwin-node.d.ts.map +1 -0
- package/dist/create-darwin-node.js +121 -0
- package/dist/create-darwin-node.js.map +1 -0
- package/dist/darwin-annotation.d.ts +80 -0
- package/dist/darwin-annotation.d.ts.map +1 -0
- package/dist/darwin-annotation.js +92 -0
- package/dist/darwin-annotation.js.map +1 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +23 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +20 -0
- package/dist/types.js.map +1 -0
- package/dist/with-darwin-evolution.d.ts +92 -0
- package/dist/with-darwin-evolution.d.ts.map +1 -0
- package/dist/with-darwin-evolution.js +256 -0
- package/dist/with-darwin-evolution.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 1 — `createDarwinNode(agent, opts?)`.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a Darwin `AgentDefinition` as a LangGraph node action. The
|
|
5
|
+
* returned function:
|
|
6
|
+
* 1. Reads the task from `state[opts.taskKey ?? "task"]`.
|
|
7
|
+
* 2. Invokes `runAgent(agent, task, { memory })` from `darwin-agents`.
|
|
8
|
+
* 3. Writes the agent output to `state[opts.outputKey ?? "output"]`.
|
|
9
|
+
* 4. Optionally writes the captured `ExecutionTrace` to
|
|
10
|
+
* `state[opts.trajectoryKey ?? "darwinTrajectory"]` so downstream
|
|
11
|
+
* nodes (or the evolution hook) can read it.
|
|
12
|
+
*
|
|
13
|
+
* Design notes (S1185):
|
|
14
|
+
* - **Non-generic return type.** The adapter returns `(state, config?) =>
|
|
15
|
+
* Promise<Record<string, unknown>>`. LangGraph accepts any function
|
|
16
|
+
* with the shape `(state, config?) => Partial<State>` as a NodeAction;
|
|
17
|
+
* leaving the return type generic-free keeps the public API trivial
|
|
18
|
+
* to consume from JS and TS alike. Strong typing is recovered via
|
|
19
|
+
* `darwinAnnotation()` on the StateGraph side.
|
|
20
|
+
* - **`runAgent` errors are wrapped** in `DarwinNodeError` so consumers
|
|
21
|
+
* can `instanceof`-check adapter failures vs upstream failures.
|
|
22
|
+
* - **`opts.onResult` is fire-and-forget swallowed.** A user callback
|
|
23
|
+
* must NEVER break the graph; we await it, but suppress any throw
|
|
24
|
+
* and surface it via `console.warn` once per Node. Mirrors the
|
|
25
|
+
* `darwinPostRun` no-throw guarantee from `agents/lib/darwin-hook.ts`.
|
|
26
|
+
* - **No `ANTHROPIC_API_KEY` manipulation.** `runAgent` makes its own
|
|
27
|
+
* subprocess/provider decisions; consumers running on Claude Max
|
|
28
|
+
* subscriptions must `delete process.env.ANTHROPIC_API_KEY` in
|
|
29
|
+
* their own bootstrap (documented in README).
|
|
30
|
+
*/
|
|
31
|
+
import { isGraphInterrupt } from "@langchain/langgraph";
|
|
32
|
+
import { runAgent } from "darwin-agents";
|
|
33
|
+
import { DarwinNodeError } from "./errors.js";
|
|
34
|
+
/**
|
|
35
|
+
* Build a LangGraph node from a Darwin agent.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { StateGraph } from "@langchain/langgraph";
|
|
40
|
+
* import { createDarwinNode, darwinAnnotation } from "darwin-langgraph";
|
|
41
|
+
* import { defineAgent } from "darwin-agents";
|
|
42
|
+
*
|
|
43
|
+
* const researcher = defineAgent({
|
|
44
|
+
* name: "researcher",
|
|
45
|
+
* role: "Topic Researcher",
|
|
46
|
+
* description: "Researches a topic and returns 5 bullet points.",
|
|
47
|
+
* systemPrompt: "Return exactly 5 bullets.",
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* const graph = new StateGraph(darwinAnnotation())
|
|
51
|
+
* .addNode("research", createDarwinNode(researcher))
|
|
52
|
+
* .addEdge("__start__", "research")
|
|
53
|
+
* .compile();
|
|
54
|
+
*
|
|
55
|
+
* const result = await graph.invoke({ task: "What is GEPA?" });
|
|
56
|
+
* console.log(result.output);
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function createDarwinNode(agent, opts = {}) {
|
|
60
|
+
if (!agent || typeof agent !== "object" || typeof agent.name !== "string") {
|
|
61
|
+
throw new DarwinNodeError("createDarwinNode: agent must be an AgentDefinition with a `name` field.", typeof agent === "object" && agent !== null && "name" in agent
|
|
62
|
+
? String(agent.name ?? "<unknown>")
|
|
63
|
+
: "<unknown>");
|
|
64
|
+
}
|
|
65
|
+
const taskKey = opts.taskKey ?? "task";
|
|
66
|
+
const outputKey = opts.outputKey ?? "output";
|
|
67
|
+
const trajectoryKey = opts.trajectoryKey ?? "darwinTrajectory";
|
|
68
|
+
const captureTrace = opts.captureTrace !== false;
|
|
69
|
+
const agentName = agent.name;
|
|
70
|
+
const onResult = opts.onResult;
|
|
71
|
+
const runOptions = opts.runOptions ?? {};
|
|
72
|
+
// Track whether we already warned about a swallowed callback so the
|
|
73
|
+
// log stays at most one line per node, not one per invocation.
|
|
74
|
+
let onResultWarned = false;
|
|
75
|
+
return async function darwinNode(state) {
|
|
76
|
+
if (state === null || typeof state !== "object") {
|
|
77
|
+
throw new DarwinNodeError(`createDarwinNode(${agentName}): state must be an object, got ${typeof state}.`, agentName);
|
|
78
|
+
}
|
|
79
|
+
const rawTask = state[taskKey];
|
|
80
|
+
if (typeof rawTask !== "string" || rawTask.length === 0) {
|
|
81
|
+
throw new DarwinNodeError(`createDarwinNode(${agentName}): state["${taskKey}"] must be a non-empty string, ` +
|
|
82
|
+
`got ${rawTask === undefined ? "undefined" : typeof rawTask}.`, agentName);
|
|
83
|
+
}
|
|
84
|
+
let result;
|
|
85
|
+
try {
|
|
86
|
+
result = await runAgent(agent, rawTask, runOptions);
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
// R1 Research Finding 5: LangGraph `interrupt()` surfaces as a
|
|
90
|
+
// `GraphInterrupt` thrown from inside the node call stack. The
|
|
91
|
+
// adapter MUST re-throw it untouched so LangGraph's HITL
|
|
92
|
+
// persistence + resume protocol works. Only non-interrupt errors
|
|
93
|
+
// get wrapped in `DarwinNodeError`.
|
|
94
|
+
if (isGraphInterrupt(err)) {
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
throw new DarwinNodeError(`createDarwinNode(${agentName}): runAgent failed: ${err?.message ?? String(err)}`, agentName, { cause: err });
|
|
98
|
+
}
|
|
99
|
+
if (onResult) {
|
|
100
|
+
try {
|
|
101
|
+
await onResult(result);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
if (!onResultWarned) {
|
|
105
|
+
onResultWarned = true;
|
|
106
|
+
console.warn(`[darwin-langgraph] onResult callback for "${agentName}" threw — swallowed. ` +
|
|
107
|
+
`Subsequent throws will be silent. Original error: ` +
|
|
108
|
+
`${err?.message ?? String(err)}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const update = {
|
|
113
|
+
[outputKey]: result.output,
|
|
114
|
+
};
|
|
115
|
+
if (captureTrace && result.experiment?.trajectory) {
|
|
116
|
+
update[trajectoryKey] = result.experiment.trajectory;
|
|
117
|
+
}
|
|
118
|
+
return update;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=create-darwin-node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-darwin-node.js","sourceRoot":"","sources":["../src/create-darwin-node.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 2 — `darwinAnnotation(extra?)`.
|
|
3
|
+
*
|
|
4
|
+
* Returns a LangGraph `Annotation.Root({ ... })` pre-populated with three
|
|
5
|
+
* Darwin-aware channels:
|
|
6
|
+
* - `task` (string, last-write-wins): the task fed to the Darwin agent.
|
|
7
|
+
* - `output` (string, last-write-wins): the agent's final text output.
|
|
8
|
+
* - `darwinTrajectory` (ExecutionTrace | undefined, last-write-wins):
|
|
9
|
+
* the captured execution trace, available downstream nodes or to
|
|
10
|
+
* the post-run evolution hook.
|
|
11
|
+
*
|
|
12
|
+
* Composable: pass any extra channels via the `extra` argument and they
|
|
13
|
+
* merge into the same `Annotation.Root` spread.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { darwinAnnotation } from "darwin-langgraph";
|
|
18
|
+
* import { Annotation, StateGraph } from "@langchain/langgraph";
|
|
19
|
+
*
|
|
20
|
+
* const State = darwinAnnotation({
|
|
21
|
+
* reviewNotes: Annotation<string[]>({
|
|
22
|
+
* reducer: (a, b) => a.concat(b),
|
|
23
|
+
* default: () => [],
|
|
24
|
+
* }),
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const graph = new StateGraph(State)
|
|
28
|
+
* .addNode("research", createDarwinNode(researcher))
|
|
29
|
+
* .addNode("review", createDarwinNode(reviewer))
|
|
30
|
+
* .compile();
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Design note: the channels use default last-write-wins reducers. If you
|
|
34
|
+
* want trajectory-accumulation (e.g. one entry per node), define your own
|
|
35
|
+
* channel via `extra` (e.g. `trajectories: Annotation<ExecutionTrace[]>(...)`)
|
|
36
|
+
* and skip `captureTrace` on every `createDarwinNode` call except the one
|
|
37
|
+
* you want to overwrite the singleton.
|
|
38
|
+
*/
|
|
39
|
+
import type { ExecutionTrace } from "darwin-agents";
|
|
40
|
+
/**
|
|
41
|
+
* Pure last-write-wins reducer for trajectory. Exported for testability
|
|
42
|
+
* and so consumers can wrap it (e.g. cap at last-N).
|
|
43
|
+
*/
|
|
44
|
+
export declare function lastWriteWinsTrajectoryReducer(_prev: ExecutionTrace | undefined, next: ExecutionTrace | undefined): ExecutionTrace | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Spec object used inside `Annotation.Root({ ... })`. Exported so power
|
|
47
|
+
* users can spread it manually into their own `Annotation.Root` call when
|
|
48
|
+
* the generic helper doesn't fit their setup (e.g. when they need a
|
|
49
|
+
* differently named `task` or `output` channel and don't want the
|
|
50
|
+
* defaults).
|
|
51
|
+
*
|
|
52
|
+
* Returned as a fresh object on every call so consumers can mutate it
|
|
53
|
+
* without affecting other graphs in the same process — the underlying
|
|
54
|
+
* `Annotation<T>` references are reused, which is what LangGraph expects.
|
|
55
|
+
*
|
|
56
|
+
* Return type is intentionally inferred. The internal `LastValue<T>` vs
|
|
57
|
+
* `BaseChannel<...>` mismatch between `Annotation<T>()` (zero-arg, plain
|
|
58
|
+
* last-write) and `Annotation<T>({reducer,default})` (full BaseChannel)
|
|
59
|
+
* is opaque to `Annotation.Root` (it accepts the union via `SDZod`).
|
|
60
|
+
* Hand-typing the return triggers TS2741 — inference works correctly.
|
|
61
|
+
*/
|
|
62
|
+
export declare function getDarwinChannelSpec(): {
|
|
63
|
+
task: import("@langchain/langgraph").LastValue<string>;
|
|
64
|
+
output: import("@langchain/langgraph").LastValue<string>;
|
|
65
|
+
darwinTrajectory: import("@langchain/langgraph").BaseChannel<ExecutionTrace | undefined, ExecutionTrace | import("@langchain/langgraph").OverwriteValue<ExecutionTrace | undefined> | undefined, unknown>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Build an `Annotation.Root` schema with Darwin's three default channels
|
|
69
|
+
* plus any extra channels supplied by the caller.
|
|
70
|
+
*
|
|
71
|
+
* The return type is intentionally inferred from
|
|
72
|
+
* `Annotation.Root({...})` so `typeof darwinAnnotation().State` works
|
|
73
|
+
* exactly like a hand-rolled `Annotation.Root`.
|
|
74
|
+
*/
|
|
75
|
+
export declare function darwinAnnotation<Extra extends Record<string, any> = {}>(extra?: Extra): import("@langchain/langgraph").AnnotationRoot<{
|
|
76
|
+
task: import("@langchain/langgraph").LastValue<string>;
|
|
77
|
+
output: import("@langchain/langgraph").LastValue<string>;
|
|
78
|
+
darwinTrajectory: import("@langchain/langgraph").BaseChannel<ExecutionTrace | undefined, ExecutionTrace | import("@langchain/langgraph").OverwriteValue<ExecutionTrace | undefined> | undefined, unknown>;
|
|
79
|
+
} & Extra>;
|
|
80
|
+
//# sourceMappingURL=darwin-annotation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"darwin-annotation.d.ts","sourceRoot":"","sources":["../src/darwin-annotation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD;;;GAGG;AACH,wBAAgB,8BAA8B,CAC5C,KAAK,EAAE,cAAc,GAAG,SAAS,EACjC,IAAI,EAAE,cAAc,GAAG,SAAS,GAC/B,cAAc,GAAG,SAAS,CAE5B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,oBAAoB;;;;EASnC;AAED;;;;;;;GAOG;AAKH,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,EACrE,KAAK,CAAC,EAAE,KAAK;;;;WAMd"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 2 — `darwinAnnotation(extra?)`.
|
|
3
|
+
*
|
|
4
|
+
* Returns a LangGraph `Annotation.Root({ ... })` pre-populated with three
|
|
5
|
+
* Darwin-aware channels:
|
|
6
|
+
* - `task` (string, last-write-wins): the task fed to the Darwin agent.
|
|
7
|
+
* - `output` (string, last-write-wins): the agent's final text output.
|
|
8
|
+
* - `darwinTrajectory` (ExecutionTrace | undefined, last-write-wins):
|
|
9
|
+
* the captured execution trace, available downstream nodes or to
|
|
10
|
+
* the post-run evolution hook.
|
|
11
|
+
*
|
|
12
|
+
* Composable: pass any extra channels via the `extra` argument and they
|
|
13
|
+
* merge into the same `Annotation.Root` spread.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { darwinAnnotation } from "darwin-langgraph";
|
|
18
|
+
* import { Annotation, StateGraph } from "@langchain/langgraph";
|
|
19
|
+
*
|
|
20
|
+
* const State = darwinAnnotation({
|
|
21
|
+
* reviewNotes: Annotation<string[]>({
|
|
22
|
+
* reducer: (a, b) => a.concat(b),
|
|
23
|
+
* default: () => [],
|
|
24
|
+
* }),
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* const graph = new StateGraph(State)
|
|
28
|
+
* .addNode("research", createDarwinNode(researcher))
|
|
29
|
+
* .addNode("review", createDarwinNode(reviewer))
|
|
30
|
+
* .compile();
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Design note: the channels use default last-write-wins reducers. If you
|
|
34
|
+
* want trajectory-accumulation (e.g. one entry per node), define your own
|
|
35
|
+
* channel via `extra` (e.g. `trajectories: Annotation<ExecutionTrace[]>(...)`)
|
|
36
|
+
* and skip `captureTrace` on every `createDarwinNode` call except the one
|
|
37
|
+
* you want to overwrite the singleton.
|
|
38
|
+
*/
|
|
39
|
+
import { Annotation } from "@langchain/langgraph";
|
|
40
|
+
/**
|
|
41
|
+
* Pure last-write-wins reducer for trajectory. Exported for testability
|
|
42
|
+
* and so consumers can wrap it (e.g. cap at last-N).
|
|
43
|
+
*/
|
|
44
|
+
export function lastWriteWinsTrajectoryReducer(_prev, next) {
|
|
45
|
+
return next;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Spec object used inside `Annotation.Root({ ... })`. Exported so power
|
|
49
|
+
* users can spread it manually into their own `Annotation.Root` call when
|
|
50
|
+
* the generic helper doesn't fit their setup (e.g. when they need a
|
|
51
|
+
* differently named `task` or `output` channel and don't want the
|
|
52
|
+
* defaults).
|
|
53
|
+
*
|
|
54
|
+
* Returned as a fresh object on every call so consumers can mutate it
|
|
55
|
+
* without affecting other graphs in the same process — the underlying
|
|
56
|
+
* `Annotation<T>` references are reused, which is what LangGraph expects.
|
|
57
|
+
*
|
|
58
|
+
* Return type is intentionally inferred. The internal `LastValue<T>` vs
|
|
59
|
+
* `BaseChannel<...>` mismatch between `Annotation<T>()` (zero-arg, plain
|
|
60
|
+
* last-write) and `Annotation<T>({reducer,default})` (full BaseChannel)
|
|
61
|
+
* is opaque to `Annotation.Root` (it accepts the union via `SDZod`).
|
|
62
|
+
* Hand-typing the return triggers TS2741 — inference works correctly.
|
|
63
|
+
*/
|
|
64
|
+
export function getDarwinChannelSpec() {
|
|
65
|
+
return {
|
|
66
|
+
task: Annotation(),
|
|
67
|
+
output: Annotation(),
|
|
68
|
+
darwinTrajectory: Annotation({
|
|
69
|
+
reducer: lastWriteWinsTrajectoryReducer,
|
|
70
|
+
default: () => undefined,
|
|
71
|
+
}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Build an `Annotation.Root` schema with Darwin's three default channels
|
|
76
|
+
* plus any extra channels supplied by the caller.
|
|
77
|
+
*
|
|
78
|
+
* The return type is intentionally inferred from
|
|
79
|
+
* `Annotation.Root({...})` so `typeof darwinAnnotation().State` works
|
|
80
|
+
* exactly like a hand-rolled `Annotation.Root`.
|
|
81
|
+
*/
|
|
82
|
+
// Wide return type because `Annotation.Root` returns a complex inferred
|
|
83
|
+
// generic that depends on the precise channel spec. Consumers extract
|
|
84
|
+
// `typeof X.State` so the concrete shape is recovered at the use site.
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
+
export function darwinAnnotation(extra) {
|
|
87
|
+
return Annotation.Root({
|
|
88
|
+
...getDarwinChannelSpec(),
|
|
89
|
+
...(extra ?? {}),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=darwin-annotation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"darwin-annotation.js","sourceRoot":"","sources":["../src/darwin-annotation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD;;;GAGG;AACH,MAAM,UAAU,8BAA8B,CAC5C,KAAiC,EACjC,IAAgC;IAEhC,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,IAAI,EAAE,UAAU,EAAU;QAC1B,MAAM,EAAE,UAAU,EAAU;QAC5B,gBAAgB,EAAE,UAAU,CAA6B;YACvD,OAAO,EAAE,8BAA8B;YACvC,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS;SACzB,CAAC;KACH,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,wEAAwE;AACxE,sEAAsE;AACtE,uEAAuE;AACvE,8DAA8D;AAC9D,MAAM,UAAU,gBAAgB,CAC9B,KAAa;IAEb,OAAO,UAAU,CAAC,IAAI,CAAC;QACrB,GAAG,oBAAoB,EAAE;QACzB,GAAG,CAAC,KAAK,IAAK,EAAY,CAAC;KAC5B,CAAC,CAAC;AACL,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter-specific error classes. Both subclass `Error` so consumers can
|
|
3
|
+
* use `err instanceof DarwinNodeError` to distinguish adapter failures
|
|
4
|
+
* from upstream `darwin-agents` or `@langchain/langgraph` errors.
|
|
5
|
+
*
|
|
6
|
+
* Pattern mirrors `mcp-armor` and `darwin-agents@0.4.9` McpBridge*Error
|
|
7
|
+
* (see `nex_decide 34a506ba` for the upstream pattern).
|
|
8
|
+
*/
|
|
9
|
+
export declare class DarwinNodeError extends Error {
|
|
10
|
+
readonly agentName: string;
|
|
11
|
+
readonly name = "DarwinNodeError";
|
|
12
|
+
constructor(message: string, agentName: string, options?: {
|
|
13
|
+
cause?: unknown;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export declare class DarwinEvolutionHookError extends Error {
|
|
17
|
+
readonly name = "DarwinEvolutionHookError";
|
|
18
|
+
constructor(message: string, options?: {
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,qBAAa,eAAgB,SAAQ,KAAK;aAKtB,SAAS,EAAE,MAAM;IAJnC,SAAyB,IAAI,qBAAqB;gBAGhD,OAAO,EAAE,MAAM,EACC,SAAS,EAAE,MAAM,EACjC,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAIhC;AAED,qBAAa,wBAAyB,SAAQ,KAAK;IACjD,SAAyB,IAAI,8BAA8B;gBAE/C,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAG3D"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter-specific error classes. Both subclass `Error` so consumers can
|
|
3
|
+
* use `err instanceof DarwinNodeError` to distinguish adapter failures
|
|
4
|
+
* from upstream `darwin-agents` or `@langchain/langgraph` errors.
|
|
5
|
+
*
|
|
6
|
+
* Pattern mirrors `mcp-armor` and `darwin-agents@0.4.9` McpBridge*Error
|
|
7
|
+
* (see `nex_decide 34a506ba` for the upstream pattern).
|
|
8
|
+
*/
|
|
9
|
+
export class DarwinNodeError extends Error {
|
|
10
|
+
agentName;
|
|
11
|
+
name = "DarwinNodeError";
|
|
12
|
+
constructor(message, agentName, options) {
|
|
13
|
+
super(message, options);
|
|
14
|
+
this.agentName = agentName;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class DarwinEvolutionHookError extends Error {
|
|
18
|
+
name = "DarwinEvolutionHookError";
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAKtB;IAJO,IAAI,GAAG,iBAAiB,CAAC;IAElD,YACE,OAAe,EACC,SAAiB,EACjC,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAuB,CAAC,CAAC;QAHxB,cAAS,GAAT,SAAS,CAAQ;IAInC,CAAC;CACF;AAED,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACxB,IAAI,GAAG,0BAA0B,CAAC;IAE3D,YAAY,OAAe,EAAE,OAA6B;QACxD,KAAK,CAAC,OAAO,EAAE,OAAuB,CAAC,CAAC;IAC1C,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* darwin-langgraph — LangGraph.js adapter for darwin-agents.
|
|
3
|
+
*
|
|
4
|
+
* Public entry. Three surfaces + types + errors.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { StateGraph } from "@langchain/langgraph";
|
|
9
|
+
* import { defineAgent } from "darwin-agents";
|
|
10
|
+
* import {
|
|
11
|
+
* createDarwinNode,
|
|
12
|
+
* darwinAnnotation,
|
|
13
|
+
* withDarwinEvolution,
|
|
14
|
+
* } from "darwin-langgraph";
|
|
15
|
+
*
|
|
16
|
+
* const researcher = defineAgent({
|
|
17
|
+
* name: "researcher",
|
|
18
|
+
* role: "Topic Researcher",
|
|
19
|
+
* description: "Five bullets on a topic.",
|
|
20
|
+
* systemPrompt: "Return exactly 5 bullet points.",
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const graph = withDarwinEvolution(
|
|
24
|
+
* new StateGraph(darwinAnnotation())
|
|
25
|
+
* .addNode("research", createDarwinNode(researcher))
|
|
26
|
+
* .addEdge("__start__", "research")
|
|
27
|
+
* .compile(),
|
|
28
|
+
* {
|
|
29
|
+
* nodeMap: { research: "researcher" },
|
|
30
|
+
* onTrajectory: (event) => {
|
|
31
|
+
* console.log(`${event.nodeName} (${event.agentName}) trajectory:`, event.trajectory);
|
|
32
|
+
* },
|
|
33
|
+
* },
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* const result = await graph.invoke({ task: "What is GEPA?" });
|
|
37
|
+
* console.log(result.output);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export { createDarwinNode, type CreateDarwinNodeOptions, type DarwinNodeFn, } from "./create-darwin-node.js";
|
|
41
|
+
export { darwinAnnotation, getDarwinChannelSpec, lastWriteWinsTrajectoryReducer, } from "./darwin-annotation.js";
|
|
42
|
+
export { withDarwinEvolution, type DarwinEvolutionOptions, type DarwinNodeMapEntry, type DarwinTrajectoryEvent, } from "./with-darwin-evolution.js";
|
|
43
|
+
export { DarwinNodeError, DarwinEvolutionHookError } from "./errors.js";
|
|
44
|
+
export type { AgentDefinition, DarwinExperiment, ExecutionTrace, MemoryProvider, RunResult, TraceToolCall, TraceTokenUsage, TraceTurnError, } from "./types.js";
|
|
45
|
+
/** Adapter version — sync with `package.json` on every release. */
|
|
46
|
+
export declare const VERSION = "0.1.0-alpha.1";
|
|
47
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,EACL,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,YAAY,GAClB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,8BAA8B,GAC/B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,mBAAmB,EACnB,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,GAC3B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAExE,YAAY,EACV,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,cAAc,EACd,SAAS,EACT,aAAa,EACb,eAAe,EACf,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,mEAAmE;AACnE,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* darwin-langgraph — LangGraph.js adapter for darwin-agents.
|
|
3
|
+
*
|
|
4
|
+
* Public entry. Three surfaces + types + errors.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { StateGraph } from "@langchain/langgraph";
|
|
9
|
+
* import { defineAgent } from "darwin-agents";
|
|
10
|
+
* import {
|
|
11
|
+
* createDarwinNode,
|
|
12
|
+
* darwinAnnotation,
|
|
13
|
+
* withDarwinEvolution,
|
|
14
|
+
* } from "darwin-langgraph";
|
|
15
|
+
*
|
|
16
|
+
* const researcher = defineAgent({
|
|
17
|
+
* name: "researcher",
|
|
18
|
+
* role: "Topic Researcher",
|
|
19
|
+
* description: "Five bullets on a topic.",
|
|
20
|
+
* systemPrompt: "Return exactly 5 bullet points.",
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const graph = withDarwinEvolution(
|
|
24
|
+
* new StateGraph(darwinAnnotation())
|
|
25
|
+
* .addNode("research", createDarwinNode(researcher))
|
|
26
|
+
* .addEdge("__start__", "research")
|
|
27
|
+
* .compile(),
|
|
28
|
+
* {
|
|
29
|
+
* nodeMap: { research: "researcher" },
|
|
30
|
+
* onTrajectory: (event) => {
|
|
31
|
+
* console.log(`${event.nodeName} (${event.agentName}) trajectory:`, event.trajectory);
|
|
32
|
+
* },
|
|
33
|
+
* },
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* const result = await graph.invoke({ task: "What is GEPA?" });
|
|
37
|
+
* console.log(result.output);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export { createDarwinNode, } from "./create-darwin-node.js";
|
|
41
|
+
export { darwinAnnotation, getDarwinChannelSpec, lastWriteWinsTrajectoryReducer, } from "./darwin-annotation.js";
|
|
42
|
+
export { withDarwinEvolution, } from "./with-darwin-evolution.js";
|
|
43
|
+
export { DarwinNodeError, DarwinEvolutionHookError } from "./errors.js";
|
|
44
|
+
/** Adapter version — sync with `package.json` on every release. */
|
|
45
|
+
export const VERSION = "0.1.0-alpha.1";
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,EACL,gBAAgB,GAGjB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,8BAA8B,GAC/B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,mBAAmB,GAIpB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAaxE,mEAAmE;AACnE,MAAM,CAAC,MAAM,OAAO,GAAG,eAAe,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-exports the darwin-agents types that consumers of darwin-langgraph
|
|
3
|
+
* need most often, so a typical project can import everything from one
|
|
4
|
+
* place:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import {
|
|
8
|
+
* createDarwinNode,
|
|
9
|
+
* darwinAnnotation,
|
|
10
|
+
* withDarwinEvolution,
|
|
11
|
+
* type AgentDefinition,
|
|
12
|
+
* type ExecutionTrace,
|
|
13
|
+
* } from "darwin-langgraph";
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Re-export only — no runtime code. Keeps the bundle tiny and avoids
|
|
17
|
+
* accidental dual-import of darwin-agents.
|
|
18
|
+
*/
|
|
19
|
+
export type { AgentDefinition, DarwinExperiment, ExecutionTrace, MemoryProvider, RunResult, TraceToolCall, TraceTokenUsage, TraceTurnError, } from "darwin-agents";
|
|
20
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,YAAY,EACV,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,cAAc,EACd,SAAS,EACT,aAAa,EACb,eAAe,EACf,cAAc,GACf,MAAM,eAAe,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-exports the darwin-agents types that consumers of darwin-langgraph
|
|
3
|
+
* need most often, so a typical project can import everything from one
|
|
4
|
+
* place:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import {
|
|
8
|
+
* createDarwinNode,
|
|
9
|
+
* darwinAnnotation,
|
|
10
|
+
* withDarwinEvolution,
|
|
11
|
+
* type AgentDefinition,
|
|
12
|
+
* type ExecutionTrace,
|
|
13
|
+
* } from "darwin-langgraph";
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* Re-export only — no runtime code. Keeps the bundle tiny and avoids
|
|
17
|
+
* accidental dual-import of darwin-agents.
|
|
18
|
+
*/
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 3 — `withDarwinEvolution(graph, opts)`.
|
|
3
|
+
*
|
|
4
|
+
* Wraps a compiled `StateGraph` with a fire-and-forget post-run hook
|
|
5
|
+
* that mirrors Darwin's closed-loop evolution pattern (see
|
|
6
|
+
* `agents/lib/darwin-hook.ts` for the in-tree Nex implementation):
|
|
7
|
+
*
|
|
8
|
+
* 1. After `graph.invoke(...)` resolves, we extract trajectories from
|
|
9
|
+
* the final state and dispatch them to `opts.onTrajectory` per
|
|
10
|
+
* mapped node.
|
|
11
|
+
* 2. The same applies to `graph.stream(...)` — we wrap the async
|
|
12
|
+
* iterator and replay the final-state chunk to the hook once the
|
|
13
|
+
* consumer finishes iterating.
|
|
14
|
+
*
|
|
15
|
+
* Design notes (S1185):
|
|
16
|
+
* - **Never throws inside the hook.** Caller logic that fails inside
|
|
17
|
+
* `onTrajectory` is logged once per process and otherwise swallowed.
|
|
18
|
+
* The graph result the consumer sees is identical with or without
|
|
19
|
+
* the wrap.
|
|
20
|
+
* - **No mutation of `graph` if it doesn't expose `invoke`/`stream`
|
|
21
|
+
* as own properties.** We bind via `Object.defineProperty` and fall
|
|
22
|
+
* back to a no-op wrap if the graph is frozen. This means future
|
|
23
|
+
* LangGraph internals that move `invoke` to a hidden prototype slot
|
|
24
|
+
* won't crash — at worst, the hook silently never fires (the test
|
|
25
|
+
* suite catches that case).
|
|
26
|
+
* - **`nodeMap` shape:** either `string` (treated as
|
|
27
|
+
* `{ agentName: string, trajectoryKey: defaultKey }`) or an object
|
|
28
|
+
* with explicit `trajectoryKey`. Supports both ergonomic one-liner
|
|
29
|
+
* and per-node trajectory routing in the same graph.
|
|
30
|
+
*/
|
|
31
|
+
import type { ExecutionTrace } from "darwin-agents";
|
|
32
|
+
/** Map entry — either a bare agent name or an explicit `{agent, key}` tuple. */
|
|
33
|
+
export type DarwinNodeMapEntry = string | {
|
|
34
|
+
agentName: string;
|
|
35
|
+
trajectoryKey: string;
|
|
36
|
+
};
|
|
37
|
+
/** Argument passed to {@link DarwinEvolutionOptions.onTrajectory}. */
|
|
38
|
+
export interface DarwinTrajectoryEvent {
|
|
39
|
+
/** Name of the StateGraph node that produced the trajectory. */
|
|
40
|
+
nodeName: string;
|
|
41
|
+
/** Darwin agent name (from the `nodeMap` mapping). */
|
|
42
|
+
agentName: string;
|
|
43
|
+
/** The captured execution trace. */
|
|
44
|
+
trajectory: ExecutionTrace;
|
|
45
|
+
/**
|
|
46
|
+
* Snapshot of the final graph state. The TOP-LEVEL object is frozen
|
|
47
|
+
* (shallow), but NESTED objects (incl. the trajectory itself) are NOT
|
|
48
|
+
* deep-frozen for performance — treat them as logically immutable by
|
|
49
|
+
* convention. Mutating nested values leaks back into the live graph
|
|
50
|
+
* state and corrupts subsequent runs (R1 Critic Finding 4, S1185).
|
|
51
|
+
*/
|
|
52
|
+
finalState: Readonly<Record<string, unknown>>;
|
|
53
|
+
}
|
|
54
|
+
/** Options for {@link withDarwinEvolution}. */
|
|
55
|
+
export interface DarwinEvolutionOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Maps StateGraph node names to either a bare Darwin agent name
|
|
58
|
+
* (uses `darwinTrajectory` as the state key) or an object specifying
|
|
59
|
+
* a custom `trajectoryKey` for that node.
|
|
60
|
+
*
|
|
61
|
+
* Example:
|
|
62
|
+
* ```ts
|
|
63
|
+
* nodeMap: {
|
|
64
|
+
* research: "researcher",
|
|
65
|
+
* review: { agentName: "critic", trajectoryKey: "criticTrajectory" },
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
nodeMap: Record<string, DarwinNodeMapEntry>;
|
|
70
|
+
/**
|
|
71
|
+
* Called once per mapped node whose trajectory was found in the final
|
|
72
|
+
* state. Errors are swallowed — never breaks the graph result.
|
|
73
|
+
*/
|
|
74
|
+
onTrajectory?: (event: DarwinTrajectoryEvent) => void | Promise<void>;
|
|
75
|
+
/** Default trajectory key when `nodeMap` entry is a bare string. Default: `"darwinTrajectory"`. */
|
|
76
|
+
defaultTrajectoryKey?: string;
|
|
77
|
+
}
|
|
78
|
+
/** Minimal interface the adapter needs — covers `CompiledStateGraph`. */
|
|
79
|
+
interface InvokableGraph {
|
|
80
|
+
invoke(input: unknown, config?: unknown): Promise<unknown>;
|
|
81
|
+
stream(input: unknown, config?: unknown): Promise<AsyncIterable<unknown>>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Wraps a compiled LangGraph so the supplied `onTrajectory` callback
|
|
85
|
+
* fires for each node listed in `nodeMap` after every `invoke` / `stream`
|
|
86
|
+
* run. Returns the same graph instance for chaining.
|
|
87
|
+
*
|
|
88
|
+
* @throws {DarwinEvolutionHookError} when `nodeMap` is empty or missing.
|
|
89
|
+
*/
|
|
90
|
+
export declare function withDarwinEvolution<G extends InvokableGraph>(graph: G, opts: DarwinEvolutionOptions): G;
|
|
91
|
+
export {};
|
|
92
|
+
//# sourceMappingURL=with-darwin-evolution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-darwin-evolution.d.ts","sourceRoot":"","sources":["../src/with-darwin-evolution.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAIpD,gFAAgF;AAChF,MAAM,MAAM,kBAAkB,GAC1B,MAAM,GACN;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjD,sEAAsE;AACtE,MAAM,WAAW,qBAAqB;IACpC,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB,sDAAsD;IACtD,SAAS,EAAE,MAAM,CAAC;IAClB,oCAAoC;IACpC,UAAU,EAAE,cAAc,CAAC;IAC3B;;;;;;OAMG;IACH,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;CAC/C;AAED,+CAA+C;AAC/C,MAAM,WAAW,sBAAsB;IACrC;;;;;;;;;;;;OAYG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;IAC5C;;;OAGG;IACH,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACtE,mGAAmG;IACnG,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,yEAAyE;AACzE,UAAU,cAAc;IACtB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;CAC3E;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,cAAc,EAC1D,KAAK,EAAE,CAAC,EACR,IAAI,EAAE,sBAAsB,GAC3B,CAAC,CA+OH"}
|