darwin-langgraph 0.2.0-alpha.1 → 0.4.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 +348 -0
- package/README.md +108 -3
- package/dist/create-darwin-node.d.ts +55 -0
- package/dist/create-darwin-node.d.ts.map +1 -1
- package/dist/create-darwin-node.js +56 -1
- package/dist/create-darwin-node.js.map +1 -1
- package/dist/darwin-accumulating-annotation.d.ts +81 -0
- package/dist/darwin-accumulating-annotation.d.ts.map +1 -0
- package/dist/darwin-accumulating-annotation.js +144 -0
- package/dist/darwin-accumulating-annotation.js.map +1 -0
- package/dist/darwin-callback-handler.d.ts +161 -2
- package/dist/darwin-callback-handler.d.ts.map +1 -1
- package/dist/darwin-callback-handler.js +417 -25
- package/dist/darwin-callback-handler.js.map +1 -1
- package/dist/errors.d.ts +16 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +19 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/dist/to-otel-attributes.d.ts +49 -0
- package/dist/to-otel-attributes.d.ts.map +1 -1
- package/dist/to-otel-attributes.js +38 -2
- package/dist/to-otel-attributes.js.map +1 -1
- package/dist/to-w3c-trace-context.d.ts +81 -0
- package/dist/to-w3c-trace-context.d.ts.map +1 -0
- package/dist/to-w3c-trace-context.js +134 -0
- package/dist/to-w3c-trace-context.js.map +1 -0
- package/dist/token-budget.d.ts +141 -0
- package/dist/token-budget.d.ts.map +1 -0
- package/dist/token-budget.js +225 -0
- package/dist/token-budget.js.map +1 -0
- package/dist/with-darwin-evolution.d.ts +40 -0
- package/dist/with-darwin-evolution.d.ts.map +1 -1
- package/dist/with-darwin-evolution.js +55 -1
- package/dist/with-darwin-evolution.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 9 (V0.4) — `darwinAccumulatingAnnotation(extra?)`.
|
|
3
|
+
*
|
|
4
|
+
* Variant of {@link darwinAnnotation} that REPLACES the singleton
|
|
5
|
+
* `darwinTrajectory: ExecutionTrace | undefined` channel with an
|
|
6
|
+
* accumulating `darwinTrajectories: ExecutionTrace[]` channel — every
|
|
7
|
+
* node write appends, the channel never overwrites.
|
|
8
|
+
*
|
|
9
|
+
* Use this when you orchestrate multiple Darwin nodes in a single graph
|
|
10
|
+
* (fan-out, sequential pipeline, supervisor pattern) and want ALL
|
|
11
|
+
* trajectories preserved in graph state for end-of-run analysis without
|
|
12
|
+
* having to declare a separate `*Trace` channel per node manually.
|
|
13
|
+
*
|
|
14
|
+
* The matching `trajectoryKey` you pass to each `createDarwinNode(...)`
|
|
15
|
+
* call is `"darwinTrajectories"`. The reducer accepts EITHER a single
|
|
16
|
+
* `ExecutionTrace` (matching the bare {@link createDarwinNode} contract)
|
|
17
|
+
* OR an `ExecutionTrace[]` (lets advanced users batch-append from a
|
|
18
|
+
* worker). Both paths flatten into the same accumulator array.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import {
|
|
23
|
+
* createDarwinNode,
|
|
24
|
+
* darwinAccumulatingAnnotation,
|
|
25
|
+
* } from "darwin-langgraph";
|
|
26
|
+
* import { StateGraph } from "@langchain/langgraph";
|
|
27
|
+
*
|
|
28
|
+
* const State = darwinAccumulatingAnnotation();
|
|
29
|
+
*
|
|
30
|
+
* const graph = new StateGraph(State)
|
|
31
|
+
* .addNode("research", createDarwinNode(researcher, {
|
|
32
|
+
* trajectoryKey: "darwinTrajectories",
|
|
33
|
+
* }))
|
|
34
|
+
* .addNode("critique", createDarwinNode(critic, {
|
|
35
|
+
* trajectoryKey: "darwinTrajectories",
|
|
36
|
+
* taskKey: "output",
|
|
37
|
+
* }))
|
|
38
|
+
* .addEdge("__start__", "research")
|
|
39
|
+
* .addEdge("research", "critique")
|
|
40
|
+
* .compile();
|
|
41
|
+
*
|
|
42
|
+
* const result = await graph.invoke({ task: "What is GEPA?" });
|
|
43
|
+
* console.log(`captured ${result.darwinTrajectories.length} trajectories`);
|
|
44
|
+
* // → captured 2 trajectories
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* NEW V0.4 (S1235).
|
|
48
|
+
*/
|
|
49
|
+
import type { ExecutionTrace } from "darwin-agents";
|
|
50
|
+
/**
|
|
51
|
+
* Tolerant accumulator reducer — accepts either a single
|
|
52
|
+
* `ExecutionTrace` (the shape `createDarwinNode` actually emits) or an
|
|
53
|
+
* `ExecutionTrace[]` (lets advanced users batch-append from a worker
|
|
54
|
+
* graph). Both paths flatten into the previous array.
|
|
55
|
+
*
|
|
56
|
+
* Pure, exported for testability and so advanced users can hand-roll
|
|
57
|
+
* their own `Annotation.Root` with the same semantics.
|
|
58
|
+
*/
|
|
59
|
+
export declare function darwinTrajectoryAccumulatorReducer(prev: ExecutionTrace[] | undefined, next: ExecutionTrace | ExecutionTrace[] | undefined): ExecutionTrace[];
|
|
60
|
+
/**
|
|
61
|
+
* Channel spec for the accumulating trajectory variant — re-exported for
|
|
62
|
+
* power-users who want to spread it into their own `Annotation.Root`
|
|
63
|
+
* call manually. Mirrors the {@link getDarwinChannelSpec} pattern.
|
|
64
|
+
*
|
|
65
|
+
* NEW V0.4 (S1235).
|
|
66
|
+
*/
|
|
67
|
+
export declare function getDarwinAccumulatingChannelSpec(): {
|
|
68
|
+
task: import("@langchain/langgraph").LastValue<string>;
|
|
69
|
+
output: import("@langchain/langgraph").LastValue<string>;
|
|
70
|
+
darwinTrajectories: import("@langchain/langgraph").BaseChannel<ExecutionTrace[], ExecutionTrace[] | import("@langchain/langgraph").OverwriteValue<ExecutionTrace[]>, unknown>;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Build an `Annotation.Root` with `task` + `output` + accumulating
|
|
74
|
+
* `darwinTrajectories: ExecutionTrace[]` plus any extra channels.
|
|
75
|
+
*/
|
|
76
|
+
export declare function darwinAccumulatingAnnotation<Extra extends Record<string, any> = {}>(extra?: Extra): import("@langchain/langgraph").AnnotationRoot<{
|
|
77
|
+
task: import("@langchain/langgraph").LastValue<string>;
|
|
78
|
+
output: import("@langchain/langgraph").LastValue<string>;
|
|
79
|
+
darwinTrajectories: import("@langchain/langgraph").BaseChannel<ExecutionTrace[], ExecutionTrace[] | import("@langchain/langgraph").OverwriteValue<ExecutionTrace[]>, unknown>;
|
|
80
|
+
} & Extra>;
|
|
81
|
+
//# sourceMappingURL=darwin-accumulating-annotation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"darwin-accumulating-annotation.d.ts","sourceRoot":"","sources":["../src/darwin-accumulating-annotation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAIpD;;;;;;;;GAQG;AACH,wBAAgB,kCAAkC,CAChD,IAAI,EAAE,cAAc,EAAE,GAAG,SAAS,EAClC,IAAI,EAAE,cAAc,GAAG,cAAc,EAAE,GAAG,SAAS,GAClD,cAAc,EAAE,CAoBlB;AAeD;;;;;;GAMG;AACH,wBAAgB,gCAAgC;;;;EAe/C;AAED;;;GAGG;AAEH,wBAAgB,4BAA4B,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,EACjF,KAAK,CAAC,EAAE,KAAK;;;;WAsBd"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface 9 (V0.4) — `darwinAccumulatingAnnotation(extra?)`.
|
|
3
|
+
*
|
|
4
|
+
* Variant of {@link darwinAnnotation} that REPLACES the singleton
|
|
5
|
+
* `darwinTrajectory: ExecutionTrace | undefined` channel with an
|
|
6
|
+
* accumulating `darwinTrajectories: ExecutionTrace[]` channel — every
|
|
7
|
+
* node write appends, the channel never overwrites.
|
|
8
|
+
*
|
|
9
|
+
* Use this when you orchestrate multiple Darwin nodes in a single graph
|
|
10
|
+
* (fan-out, sequential pipeline, supervisor pattern) and want ALL
|
|
11
|
+
* trajectories preserved in graph state for end-of-run analysis without
|
|
12
|
+
* having to declare a separate `*Trace` channel per node manually.
|
|
13
|
+
*
|
|
14
|
+
* The matching `trajectoryKey` you pass to each `createDarwinNode(...)`
|
|
15
|
+
* call is `"darwinTrajectories"`. The reducer accepts EITHER a single
|
|
16
|
+
* `ExecutionTrace` (matching the bare {@link createDarwinNode} contract)
|
|
17
|
+
* OR an `ExecutionTrace[]` (lets advanced users batch-append from a
|
|
18
|
+
* worker). Both paths flatten into the same accumulator array.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import {
|
|
23
|
+
* createDarwinNode,
|
|
24
|
+
* darwinAccumulatingAnnotation,
|
|
25
|
+
* } from "darwin-langgraph";
|
|
26
|
+
* import { StateGraph } from "@langchain/langgraph";
|
|
27
|
+
*
|
|
28
|
+
* const State = darwinAccumulatingAnnotation();
|
|
29
|
+
*
|
|
30
|
+
* const graph = new StateGraph(State)
|
|
31
|
+
* .addNode("research", createDarwinNode(researcher, {
|
|
32
|
+
* trajectoryKey: "darwinTrajectories",
|
|
33
|
+
* }))
|
|
34
|
+
* .addNode("critique", createDarwinNode(critic, {
|
|
35
|
+
* trajectoryKey: "darwinTrajectories",
|
|
36
|
+
* taskKey: "output",
|
|
37
|
+
* }))
|
|
38
|
+
* .addEdge("__start__", "research")
|
|
39
|
+
* .addEdge("research", "critique")
|
|
40
|
+
* .compile();
|
|
41
|
+
*
|
|
42
|
+
* const result = await graph.invoke({ task: "What is GEPA?" });
|
|
43
|
+
* console.log(`captured ${result.darwinTrajectories.length} trajectories`);
|
|
44
|
+
* // → captured 2 trajectories
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* NEW V0.4 (S1235).
|
|
48
|
+
*/
|
|
49
|
+
import { Annotation } from "@langchain/langgraph";
|
|
50
|
+
import { getDarwinChannelSpec } from "./darwin-annotation.js";
|
|
51
|
+
/**
|
|
52
|
+
* Tolerant accumulator reducer — accepts either a single
|
|
53
|
+
* `ExecutionTrace` (the shape `createDarwinNode` actually emits) or an
|
|
54
|
+
* `ExecutionTrace[]` (lets advanced users batch-append from a worker
|
|
55
|
+
* graph). Both paths flatten into the previous array.
|
|
56
|
+
*
|
|
57
|
+
* Pure, exported for testability and so advanced users can hand-roll
|
|
58
|
+
* their own `Annotation.Root` with the same semantics.
|
|
59
|
+
*/
|
|
60
|
+
export function darwinTrajectoryAccumulatorReducer(prev, next) {
|
|
61
|
+
const base = prev ?? [];
|
|
62
|
+
if (next === undefined || next === null)
|
|
63
|
+
return base;
|
|
64
|
+
// R1 P0-2 fix (S1235): forward-compat — match `isExecutionTrace` and
|
|
65
|
+
// accept `version >= 1` rather than strict `=== 1`. The mismatch let
|
|
66
|
+
// v=2 trajectories through the DarwinCallbackHandler.onTrajectory
|
|
67
|
+
// hook but silently dropped them on the accumulator side.
|
|
68
|
+
const isTrace = (t) => {
|
|
69
|
+
if (!t || typeof t !== "object")
|
|
70
|
+
return false;
|
|
71
|
+
const v = t.version;
|
|
72
|
+
return typeof v === "number" && Number.isFinite(v) && v >= 1;
|
|
73
|
+
};
|
|
74
|
+
if (Array.isArray(next)) {
|
|
75
|
+
const valid = next.filter(isTrace);
|
|
76
|
+
if (valid.length === 0)
|
|
77
|
+
return base;
|
|
78
|
+
return [...base, ...valid];
|
|
79
|
+
}
|
|
80
|
+
if (!isTrace(next))
|
|
81
|
+
return base;
|
|
82
|
+
return [...base, next];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Reserved channel keys the accumulating annotation owns. Used by
|
|
86
|
+
* {@link darwinAccumulatingAnnotation} to fail loud when an `extra`
|
|
87
|
+
* spread would silently overwrite a Darwin-managed channel.
|
|
88
|
+
*
|
|
89
|
+
* NEW V0.4 (R1 P2-2 fix, S1235).
|
|
90
|
+
*/
|
|
91
|
+
const DARWIN_ACCUMULATING_RESERVED_KEYS = new Set([
|
|
92
|
+
"task",
|
|
93
|
+
"output",
|
|
94
|
+
"darwinTrajectories",
|
|
95
|
+
]);
|
|
96
|
+
/**
|
|
97
|
+
* Channel spec for the accumulating trajectory variant — re-exported for
|
|
98
|
+
* power-users who want to spread it into their own `Annotation.Root`
|
|
99
|
+
* call manually. Mirrors the {@link getDarwinChannelSpec} pattern.
|
|
100
|
+
*
|
|
101
|
+
* NEW V0.4 (S1235).
|
|
102
|
+
*/
|
|
103
|
+
export function getDarwinAccumulatingChannelSpec() {
|
|
104
|
+
// Take task + output from the base spec but DROP the singleton
|
|
105
|
+
// darwinTrajectory channel — callers using this annotation want the
|
|
106
|
+
// accumulator under the plural key.
|
|
107
|
+
const base = getDarwinChannelSpec();
|
|
108
|
+
// Surgical: pluck task + output, drop darwinTrajectory.
|
|
109
|
+
const { task, output } = base;
|
|
110
|
+
return {
|
|
111
|
+
task,
|
|
112
|
+
output,
|
|
113
|
+
darwinTrajectories: Annotation({
|
|
114
|
+
reducer: darwinTrajectoryAccumulatorReducer,
|
|
115
|
+
default: () => [],
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Build an `Annotation.Root` with `task` + `output` + accumulating
|
|
121
|
+
* `darwinTrajectories: ExecutionTrace[]` plus any extra channels.
|
|
122
|
+
*/
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
export function darwinAccumulatingAnnotation(extra) {
|
|
125
|
+
// R1 P2-2 fix (S1235): fail loud if `extra` would silently overwrite
|
|
126
|
+
// a Darwin-managed channel. Before this guard a caller passing
|
|
127
|
+
// `extra = { darwinTrajectories: customAnnotation }` silently replaced
|
|
128
|
+
// the accumulator and broke the contract advertised by the function
|
|
129
|
+
// name without a single warning.
|
|
130
|
+
if (extra) {
|
|
131
|
+
for (const key of Object.keys(extra)) {
|
|
132
|
+
if (DARWIN_ACCUMULATING_RESERVED_KEYS.has(key)) {
|
|
133
|
+
throw new Error(`darwinAccumulatingAnnotation: "extra" cannot redefine the ` +
|
|
134
|
+
`Darwin-managed channel "${key}". Reserved keys: ` +
|
|
135
|
+
`${[...DARWIN_ACCUMULATING_RESERVED_KEYS].join(", ")}.`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return Annotation.Root({
|
|
140
|
+
...getDarwinAccumulatingChannelSpec(),
|
|
141
|
+
...(extra ?? {}),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=darwin-accumulating-annotation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"darwin-accumulating-annotation.js","sourceRoot":"","sources":["../src/darwin-accumulating-annotation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,UAAU,kCAAkC,CAChD,IAAkC,EAClC,IAAmD;IAEnD,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;IACxB,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IACrD,qEAAqE;IACrE,qEAAqE;IACrE,kEAAkE;IAClE,0DAA0D;IAC1D,MAAM,OAAO,GAAG,CAAC,CAAU,EAAuB,EAAE;QAClD,IAAI,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;QAC9C,MAAM,CAAC,GAAI,CAA2B,CAAC,OAAO,CAAC;QAC/C,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QA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iC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC/C,MAAM,IAAI,KAAK,CACb,4DAA4D;oBAC1D,2BAA2B,GAAG,oBAAoB;oBAClD,GAAG,CAAC,GAAG,iCAAiC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAC1D,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,UAAU,CAAC,IAAI,CAAC;QACrB,GAAG,gCAAgC,EAAE;QACrC,GAAG,CAAC,KAAK,IAAK,EAAY,CAAC;KAC5B,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -52,6 +52,102 @@
|
|
|
52
52
|
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
53
53
|
import type { ChainValues } from "@langchain/core/utils/types";
|
|
54
54
|
import type { DarwinEvolutionOptions } from "./with-darwin-evolution.js";
|
|
55
|
+
/**
|
|
56
|
+
* Highest `ExecutionTrace.version` this handler natively understands.
|
|
57
|
+
* The forward-compat guard in {@link isExecutionTrace} warns once for
|
|
58
|
+
* higher versions but still emits them (structural shape covers the
|
|
59
|
+
* required fields) so a Darwin upgrade ahead of the adapter doesn't
|
|
60
|
+
* silently drop trajectories.
|
|
61
|
+
*
|
|
62
|
+
* NEW V0.4 (S1235).
|
|
63
|
+
*/
|
|
64
|
+
export declare const MAX_KNOWN_TRACE_VERSION = 1;
|
|
65
|
+
/**
|
|
66
|
+
* Surface 8 (V0.4) — `DarwinToolEvent`.
|
|
67
|
+
*
|
|
68
|
+
* Emitted by {@link DarwinCallbackHandler.onToolEvent} whenever LangGraph
|
|
69
|
+
* surfaces a tool-chain start, end, or error inside a tracked node-chain.
|
|
70
|
+
* Lets consumers correlate per-tool spans with the parent agent trajectory
|
|
71
|
+
* BEFORE the chain completes — useful for live OTEL span emission,
|
|
72
|
+
* progress UIs, and abort-on-tool-failure logic.
|
|
73
|
+
*
|
|
74
|
+
* Phase semantics:
|
|
75
|
+
* - `"start"` — populated `input`, no `output`/`error`. Fired from
|
|
76
|
+
* `handleToolStart`.
|
|
77
|
+
* - `"end"` — populated `output`, no `error`. Fired from `handleToolEnd`.
|
|
78
|
+
* - `"error"` — populated `error`, no `output`. Fired from `handleToolError`.
|
|
79
|
+
*
|
|
80
|
+
* NEW V0.4 (S1235).
|
|
81
|
+
*/
|
|
82
|
+
export interface DarwinToolEvent {
|
|
83
|
+
/** Node name the tool ran under (resolved from `runIdToName` via the
|
|
84
|
+
* tool-chain's parentRunId — the chain runId from `handleChainStart`). */
|
|
85
|
+
nodeName: string;
|
|
86
|
+
/** Darwin agent name (from `nodeMap`). */
|
|
87
|
+
agentName: string;
|
|
88
|
+
/** Tool name LangChain passed in `handleToolStart` (often the function name). */
|
|
89
|
+
toolName: string;
|
|
90
|
+
/** Phase of the tool-chain event. */
|
|
91
|
+
phase: "start" | "end" | "error";
|
|
92
|
+
/** LangChain runId of this tool-chain (NOT the parent chain). */
|
|
93
|
+
runId: string;
|
|
94
|
+
/** LangChain parentRunId — chain runId of the node the tool ran under. */
|
|
95
|
+
parentRunId: string;
|
|
96
|
+
/** Raw input arg passed to `handleToolStart`. Only present on phase=start. */
|
|
97
|
+
input?: string;
|
|
98
|
+
/** Raw output from `handleToolEnd`. Only present on phase=end. */
|
|
99
|
+
output?: unknown;
|
|
100
|
+
/** Error from `handleToolError`. Only present on phase=error. */
|
|
101
|
+
error?: Error;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* V0.3 — extra options on top of `DarwinEvolutionOptions`. Pass to
|
|
105
|
+
* `new DarwinCallbackHandler({ ...opts, maxInFlightRuns })`.
|
|
106
|
+
*
|
|
107
|
+
* NEW V0.3 (S1187), extended V0.4 (S1235).
|
|
108
|
+
*/
|
|
109
|
+
export interface DarwinCallbackHandlerOptions extends DarwinEvolutionOptions {
|
|
110
|
+
/**
|
|
111
|
+
* Maximum number of in-flight `runId → nodeName` mappings the handler
|
|
112
|
+
* holds at once. If `handleChainEnd` / `handleChainError` never fires
|
|
113
|
+
* (LangGraph internal bug, OS-level kill of the worker mid-invoke,
|
|
114
|
+
* etc.) the map would otherwise grow without bound and leak memory.
|
|
115
|
+
*
|
|
116
|
+
* When the limit is exceeded, the OLDEST entry is evicted and a
|
|
117
|
+
* one-shot warning is logged. Default: 1024 (enough for typical
|
|
118
|
+
* fan-out patterns with safety margin, small enough to surface real
|
|
119
|
+
* leaks within minutes of an incident).
|
|
120
|
+
*
|
|
121
|
+
* Set to `Infinity` to opt out — discouraged in production.
|
|
122
|
+
*/
|
|
123
|
+
maxInFlightRuns?: number;
|
|
124
|
+
/**
|
|
125
|
+
* V0.4 — soft cap on the serialised trajectory size before it is
|
|
126
|
+
* surfaced through `onTrajectory`. When a trajectory's `JSON.stringify`
|
|
127
|
+
* length exceeds `maxTrajectoryBytes`, the handler emits a one-shot
|
|
128
|
+
* warning and replaces the `trajectory` field on the event with a
|
|
129
|
+
* structurally-compatible BUT minimal stub (same `version`, empty
|
|
130
|
+
* `toolCalls` and `errors`, original counts and capturedAt preserved).
|
|
131
|
+
* Downstream consumers can detect the swap by inspecting
|
|
132
|
+
* `event.trajectoryTruncated === true`.
|
|
133
|
+
*
|
|
134
|
+
* Default: `262_144` (256 KiB). Set to `Infinity` to opt out.
|
|
135
|
+
*
|
|
136
|
+
* NEW V0.4 (S1235).
|
|
137
|
+
*/
|
|
138
|
+
maxTrajectoryBytes?: number;
|
|
139
|
+
/**
|
|
140
|
+
* V0.4 — Fired for every tool-chain start / end / error observed inside
|
|
141
|
+
* a tracked node-chain. Use this to emit per-tool OTEL spans, drive a
|
|
142
|
+
* progress UI, or abort downstream work on a tool failure.
|
|
143
|
+
*
|
|
144
|
+
* Errors thrown inside `onToolEvent` are swallowed with a single warn
|
|
145
|
+
* per handler instance — never breaks the graph.
|
|
146
|
+
*
|
|
147
|
+
* NEW V0.4 (S1235).
|
|
148
|
+
*/
|
|
149
|
+
onToolEvent?: (event: DarwinToolEvent) => void | Promise<void>;
|
|
150
|
+
}
|
|
55
151
|
/**
|
|
56
152
|
* LangChain `BaseCallbackHandler` that listens for LangGraph node-chain
|
|
57
153
|
* events and dispatches Darwin trajectory hooks. Pass it via the
|
|
@@ -63,9 +159,49 @@ export declare class DarwinCallbackHandler extends BaseCallbackHandler {
|
|
|
63
159
|
readonly awaitHandlers = false;
|
|
64
160
|
private readonly resolved;
|
|
65
161
|
private readonly onTrajectory;
|
|
162
|
+
private readonly onToolEvent;
|
|
163
|
+
/**
|
|
164
|
+
* runId → in-flight run state. Map preserves insertion order so the
|
|
165
|
+
* oldest entry is always at `.keys().next().value` for LRU eviction
|
|
166
|
+
* when the cap is exceeded (V0.3 hung-invoke guard).
|
|
167
|
+
*/
|
|
66
168
|
private readonly runIdToName;
|
|
169
|
+
/**
|
|
170
|
+
* V0.4 (R1 P1-1 fix, S1235) — `toolRunId → toolName` cache populated
|
|
171
|
+
* from `handleToolStart` and consumed by `handleToolEnd` /
|
|
172
|
+
* `handleToolError`. LangChain v0.3 `handleToolEnd` does NOT reliably
|
|
173
|
+
* carry the tool name in its signature (the slot was repurposed
|
|
174
|
+
* across releases); reading `output.name` only works for some shapes
|
|
175
|
+
* and yields "<anonymous-tool>" for the common ToolNode case.
|
|
176
|
+
*/
|
|
177
|
+
private readonly toolRunIdToName;
|
|
178
|
+
/**
|
|
179
|
+
* V0.4 (R1 P1-1 fix, S1235) — pending callbacks keyed by parent chain
|
|
180
|
+
* runId so we can also restore the chain's tool name at end-time even
|
|
181
|
+
* after the `toolRunIdToName` entry is cleaned up.
|
|
182
|
+
*/
|
|
183
|
+
private readonly maxInFlightRuns;
|
|
184
|
+
/** V0.4 — byte cap before trajectory replacement. */
|
|
185
|
+
private readonly maxTrajectoryBytes;
|
|
186
|
+
/**
|
|
187
|
+
* V0.4 (R1 P1-1 fix) — also cap the tool-name cache. Default matches
|
|
188
|
+
* `maxInFlightRuns` since tool runs are children of chain runs.
|
|
189
|
+
*/
|
|
190
|
+
private readonly maxInFlightToolRuns;
|
|
191
|
+
/**
|
|
192
|
+
* Pending trajectory dispatches keyed by chain runId. When a chain
|
|
193
|
+
* ends with tools still in flight we hold the dispatch closure here
|
|
194
|
+
* and fire it from the LAST `handleToolEnd` / `handleToolError`.
|
|
195
|
+
*
|
|
196
|
+
* V0.4 (R1 P1-4 fix, S1235).
|
|
197
|
+
*/
|
|
198
|
+
private readonly pendingTrajectoryDispatch;
|
|
67
199
|
private warned;
|
|
68
|
-
|
|
200
|
+
private evictionWarned;
|
|
201
|
+
private toolWarned;
|
|
202
|
+
/** V0.4 — once-per-handler warn for trajectory size cap. */
|
|
203
|
+
private trajectoryTruncatedWarned;
|
|
204
|
+
constructor(opts: DarwinCallbackHandlerOptions);
|
|
69
205
|
/**
|
|
70
206
|
* Capture the run-id → node-name mapping.
|
|
71
207
|
*
|
|
@@ -76,7 +212,7 @@ export declare class DarwinCallbackHandler extends BaseCallbackHandler {
|
|
|
76
212
|
* `BaseCallbackHandler` d.ts is undefined for LangGraph chains at
|
|
77
213
|
* runtime — we keep it as a fallback for non-LangGraph chains.
|
|
78
214
|
*/
|
|
79
|
-
handleChainStart(_chain: unknown, _inputs: ChainValues, runId: string, _runType?: string, _tags?: string[], metadata?: Record<string, unknown>, runName?: string,
|
|
215
|
+
handleChainStart(_chain: unknown, _inputs: ChainValues, runId: string, _runType?: string, _tags?: string[], metadata?: Record<string, unknown>, runName?: string, parentRunId?: string, _extra?: Record<string, unknown>): void;
|
|
80
216
|
/**
|
|
81
217
|
* When a node-chain finishes, look up its name, locate the matching
|
|
82
218
|
* trajectory in `outputs`, and dispatch onTrajectory.
|
|
@@ -94,7 +230,30 @@ export declare class DarwinCallbackHandler extends BaseCallbackHandler {
|
|
|
94
230
|
* the hook on error — the trajectory is by definition incomplete.
|
|
95
231
|
*/
|
|
96
232
|
handleChainError(_err: Error, runId: string, _parentRunId?: string): void;
|
|
233
|
+
/**
|
|
234
|
+
* V0.4 (S1235) — tool-chain start event. LangGraph fires `handleToolStart`
|
|
235
|
+
* for every tool-invocation a node performs (whether via `ToolNode`,
|
|
236
|
+
* `createReactAgent`, or a custom tool-call dispatcher). We use the
|
|
237
|
+
* `parentRunId` slot to associate the tool with the tracking node and
|
|
238
|
+
* emit a `DarwinToolEvent` so consumers can build per-tool OTEL spans
|
|
239
|
+
* or progress UIs.
|
|
240
|
+
*
|
|
241
|
+
* If `parentRunId` is missing OR the parent is not a tracked node, the
|
|
242
|
+
* tool event is silently dropped — the handler never surfaces unmapped
|
|
243
|
+
* activity (consistent with the chain-side `resolved.has(nodeName)` gate).
|
|
244
|
+
*/
|
|
245
|
+
handleToolStart(_tool: unknown, input: string, runId: string, parentRunId?: string, _tags?: string[], _metadata?: Record<string, unknown>, _runType?: string, name?: string): void;
|
|
246
|
+
/**
|
|
247
|
+
* V0.4 (S1235) — tool-chain end event.
|
|
248
|
+
*/
|
|
249
|
+
handleToolEnd(output: unknown, runId: string, parentRunId?: string): void;
|
|
250
|
+
/**
|
|
251
|
+
* V0.4 (S1235) — tool-chain error event.
|
|
252
|
+
*/
|
|
253
|
+
handleToolError(err: Error, runId: string, parentRunId?: string): void;
|
|
254
|
+
private dispatchToolEvent;
|
|
97
255
|
private swallow;
|
|
256
|
+
private swallowTool;
|
|
98
257
|
/**
|
|
99
258
|
* Helper for tests + debug introspection — returns how many in-flight
|
|
100
259
|
* chain runs we are currently tracking. Should be 0 between
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"darwin-callback-handler.d.ts","sourceRoot":"","sources":["../src/darwin-callback-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAI/D,OAAO,KAAK,EACV,sBAAsB,EAGvB,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"darwin-callback-handler.d.ts","sourceRoot":"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