@lzn1998/dsh-langgraph 1.0.1 → 1.6.0

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.
Files changed (44) hide show
  1. package/README.md +172 -51
  2. package/bench/README.md +54 -0
  3. package/bench/demo-video.mjs +208 -0
  4. package/bench/results-harness.json +125 -0
  5. package/bench/run.mjs +139 -0
  6. package/compose/analysis.js +359 -0
  7. package/compose/build.js +274 -0
  8. package/compose/constants.js +83 -0
  9. package/compose/guard.js +72 -0
  10. package/compose/nodes.js +214 -0
  11. package/compose/resolve.js +97 -0
  12. package/compose/schema.js +975 -0
  13. package/compose/specs.js +218 -0
  14. package/compose/template.js +260 -0
  15. package/compose/tool.js +713 -0
  16. package/docs/BENCHMARK.md +116 -0
  17. package/docs/COMPOSE.md +376 -0
  18. package/docs/CONTEXT.md +34 -18
  19. package/docs/DETAIL.md +48 -1
  20. package/docs/ECOSYSTEM.md +30 -5
  21. package/gallery/README.md +69 -0
  22. package/gallery/assets/gallery.png +0 -0
  23. package/gallery/assets/session.png +0 -0
  24. package/gallery/index.html +516 -0
  25. package/gallery/specs/approval-plan.lg.json +51 -0
  26. package/gallery/specs/data-extract.lg.json +75 -0
  27. package/gallery/specs/incident-triage.lg.json +64 -0
  28. package/gallery/specs/pr-review.lg.json +49 -0
  29. package/gallery/specs/repo-scan.lg.json +54 -0
  30. package/gallery/specs/research-brief.lg.json +94 -0
  31. package/index.d.ts +136 -0
  32. package/index.js +423 -46
  33. package/media/dsh-langgraph-demo.webm +0 -0
  34. package/media/raw/page@0d0adc4d6eb7b946d722093988db7e67.webm +0 -0
  35. package/media/raw/page@0fceaacbda6d8f2da8485736c565ed87.webm +0 -0
  36. package/media/raw/page@49c6bb9135c0c35ed8cf9054a8e05711.webm +0 -0
  37. package/media/raw/page@86f5e37e1c1218697f8724a6bafda8d2.webm +0 -0
  38. package/media/raw/page@9b5b70b442c0ace7d4b135fc80edba73.webm +0 -0
  39. package/media/raw/page@beb9164240f13ad017c3fa7e8f20a772.webm +0 -0
  40. package/media/raw/page@db4ee1834adcc9e8d5b4708eabe4eb5c.webm +0 -0
  41. package/media/raw/page@e9c63a0029d3181e41506fc4bf8da05a.webm +0 -0
  42. package/media/raw/page@eaf729b06925fe86e9e9e4bf48e2cdcc.webm +0 -0
  43. package/media/raw/page@f0d6fd1a2534e69bc2f228b30ae19a9c.webm +0 -0
  44. package/package.json +7 -3
@@ -0,0 +1,75 @@
1
+ {
2
+ "ir": 1,
3
+ "name": "data-extract",
4
+ "description": "Extract one JSON record per file and write a single JSON array.",
5
+ "state": {
6
+ "pattern": { "type": "string", "default": "data/*.txt" },
7
+ "files": { "type": "string[]", "default": [] },
8
+ "contents": { "type": "string[]", "default": [], "reducer": "append" },
9
+ "records": { "type": "string[]", "default": [], "reducer": "append" },
10
+ "json": { "type": "string", "default": "" }
11
+ },
12
+ "nodes": [
13
+ {
14
+ "id": "list",
15
+ "do": "tool",
16
+ "tool": "glob",
17
+ "args": { "pattern": { "$state": "pattern" } },
18
+ "out": { "files": "lines" }
19
+ },
20
+ {
21
+ "id": "read",
22
+ "do": "map",
23
+ "over": "files",
24
+ "maxConcurrency": 8,
25
+ "body": {
26
+ "do": "tool",
27
+ "tool": "read",
28
+ "args": { "file_path": { "$state": "item" } },
29
+ "out": { "contents": "text" }
30
+ }
31
+ },
32
+ {
33
+ "id": "extract",
34
+ "do": "map",
35
+ "over": "contents",
36
+ "maxConcurrency": 8,
37
+ "body": {
38
+ "do": "model",
39
+ "prompt": ["Return one compact JSON object for this record; no prose.\n", { "$state": "item" }],
40
+ "schema": {
41
+ "type": "object",
42
+ "required": ["id", "value"],
43
+ "properties": {
44
+ "id": { "type": "string" },
45
+ "value": { "type": "number" }
46
+ }
47
+ },
48
+ "out": { "records": "text" }
49
+ }
50
+ },
51
+ {
52
+ "id": "join",
53
+ "do": "model",
54
+ "prompt": ["Merge these JSON objects into one valid JSON array. Return the array only.\n", { "$state": "records" }],
55
+ "out": { "json": "text" }
56
+ },
57
+ {
58
+ "id": "write",
59
+ "do": "tool",
60
+ "tool": "write",
61
+ "args": { "file_path": "records.json", "content": { "$state": "json" } },
62
+ "out": {}
63
+ }
64
+ ],
65
+ "edges": [
66
+ { "from": "START", "to": "list" },
67
+ { "from": "list", "to": "read" },
68
+ { "from": "read", "to": "extract" },
69
+ { "from": "extract", "to": "join" },
70
+ { "from": "join", "to": "write" },
71
+ { "from": "write", "to": "END" }
72
+ ],
73
+ "output": ["json"],
74
+ "maxSteps": 20
75
+ }
@@ -0,0 +1,64 @@
1
+ {
2
+ "ir": 1,
3
+ "name": "incident-triage",
4
+ "description": "Read logs, classify the incident, ask whether to page, and write a triage note.",
5
+ "state": {
6
+ "pattern": { "type": "string", "default": "logs/*.log" },
7
+ "files": { "type": "string[]", "default": [] },
8
+ "logs": { "type": "string[]", "default": [], "reducer": "append" },
9
+ "triage": { "type": "string", "default": "" },
10
+ "decision": { "type": "string", "default": "" }
11
+ },
12
+ "nodes": [
13
+ {
14
+ "id": "list",
15
+ "do": "tool",
16
+ "tool": "glob",
17
+ "args": { "pattern": { "$state": "pattern" } },
18
+ "out": { "files": "lines" }
19
+ },
20
+ {
21
+ "id": "read",
22
+ "do": "map",
23
+ "over": "files",
24
+ "maxConcurrency": 8,
25
+ "body": {
26
+ "do": "tool",
27
+ "tool": "read",
28
+ "args": { "file_path": { "$state": "item" } },
29
+ "out": { "logs": "text" }
30
+ }
31
+ },
32
+ {
33
+ "id": "classify",
34
+ "do": "model",
35
+ "prompt": ["Triage these logs: likely cause, confidence, and the next check.\n", { "$state": "logs" }],
36
+ "out": { "triage": "text" }
37
+ },
38
+ {
39
+ "id": "page",
40
+ "do": "gate",
41
+ "question": "Page the on-call engineer?",
42
+ "options": ["Page", "No"],
43
+ "out": { "decision": "text" }
44
+ },
45
+ {
46
+ "id": "write",
47
+ "do": "tool",
48
+ "tool": "write",
49
+ "args": { "file_path": "triage.md", "content": { "$state": "triage" } },
50
+ "out": {}
51
+ }
52
+ ],
53
+ "edges": [
54
+ { "from": "START", "to": "list" },
55
+ { "from": "list", "to": "read" },
56
+ { "from": "read", "to": "classify" },
57
+ { "from": "classify", "to": "page" },
58
+ { "from": "page", "to": "write", "when": { "op": "eq", "args": [{ "$state": "decision" }, "Page"] } },
59
+ { "from": "page", "to": "END", "when": true },
60
+ { "from": "write", "to": "END" }
61
+ ],
62
+ "output": ["triage", "decision"],
63
+ "maxSteps": 20
64
+ }
@@ -0,0 +1,49 @@
1
+ {
2
+ "ir": 1,
3
+ "name": "pr-review",
4
+ "description": "Read the current diff, draft a review, ask for approval, and write it.",
5
+ "state": {
6
+ "diff": { "type": "string", "default": "" },
7
+ "review": { "type": "string", "default": "" },
8
+ "decision": { "type": "string", "default": "" }
9
+ },
10
+ "nodes": [
11
+ {
12
+ "id": "read_diff",
13
+ "do": "tool",
14
+ "tool": "bash",
15
+ "args": { "command": "git diff --stat && git diff --no-ext-diff" },
16
+ "out": { "diff": "text" }
17
+ },
18
+ {
19
+ "id": "draft_review",
20
+ "do": "model",
21
+ "prompt": ["Review this diff. List only concrete correctness risks; say none if there are none.\n", { "$state": "diff" }],
22
+ "out": { "review": "text" }
23
+ },
24
+ {
25
+ "id": "approve",
26
+ "do": "gate",
27
+ "question": "Post this review?",
28
+ "options": ["Approve", "Revise"],
29
+ "out": { "decision": "text" }
30
+ },
31
+ {
32
+ "id": "write",
33
+ "do": "tool",
34
+ "tool": "write",
35
+ "args": { "file_path": "review.md", "content": { "$state": "review" } },
36
+ "out": {}
37
+ }
38
+ ],
39
+ "edges": [
40
+ { "from": "START", "to": "read_diff" },
41
+ { "from": "read_diff", "to": "draft_review" },
42
+ { "from": "draft_review", "to": "approve" },
43
+ { "from": "approve", "to": "write", "when": { "op": "eq", "args": [{ "$state": "decision" }, "Approve"] } },
44
+ { "from": "approve", "to": "END", "when": true },
45
+ { "from": "write", "to": "END" }
46
+ ],
47
+ "output": ["review", "decision"],
48
+ "maxSteps": 20
49
+ }
@@ -0,0 +1,54 @@
1
+ {
2
+ "ir": 1,
3
+ "name": "repo-scan",
4
+ "description": "Scan a set of source files in parallel and write one review.",
5
+ "state": {
6
+ "pattern": { "type": "string", "default": "src/**/*.js" },
7
+ "files": { "type": "string[]", "default": [] },
8
+ "snippets": { "type": "string[]", "default": [], "reducer": "append" },
9
+ "report": { "type": "string", "default": "" }
10
+ },
11
+ "nodes": [
12
+ {
13
+ "id": "list",
14
+ "do": "tool",
15
+ "tool": "glob",
16
+ "args": { "pattern": { "$state": "pattern" } },
17
+ "out": { "files": "lines" }
18
+ },
19
+ {
20
+ "id": "read",
21
+ "do": "map",
22
+ "over": "files",
23
+ "maxConcurrency": 8,
24
+ "body": {
25
+ "do": "tool",
26
+ "tool": "read",
27
+ "args": { "file_path": { "$state": "item" } },
28
+ "out": { "snippets": "text" }
29
+ }
30
+ },
31
+ {
32
+ "id": "review",
33
+ "do": "model",
34
+ "prompt": ["Review these source files. Give the main risks and the smallest next action:\n", { "$state": "snippets" }],
35
+ "out": { "report": "text" }
36
+ },
37
+ {
38
+ "id": "write",
39
+ "do": "tool",
40
+ "tool": "write",
41
+ "args": { "file_path": "scan-report.md", "content": { "$state": "report" } },
42
+ "out": {}
43
+ }
44
+ ],
45
+ "edges": [
46
+ { "from": "START", "to": "list" },
47
+ { "from": "list", "to": "read" },
48
+ { "from": "read", "to": "review" },
49
+ { "from": "review", "to": "write" },
50
+ { "from": "write", "to": "END" }
51
+ ],
52
+ "output": ["report"],
53
+ "maxSteps": 20
54
+ }
@@ -0,0 +1,94 @@
1
+ {
2
+ "ir": 1,
3
+ "name": "research-brief",
4
+ "description": "Read a folder, extract one row per file, approve an outline, and write a brief.",
5
+ "state": {
6
+ "pattern": { "type": "string", "default": "topics/*.txt" },
7
+ "files": { "type": "string[]", "default": [] },
8
+ "contents": { "type": "string[]", "default": [], "reducer": "append" },
9
+ "extracts": { "type": "json", "default": [], "reducer": "append" },
10
+ "outline": { "type": "string", "default": "" },
11
+ "decision": { "type": "string", "default": "" },
12
+ "brief": { "type": "string", "default": "" }
13
+ },
14
+ "nodes": [
15
+ {
16
+ "id": "list",
17
+ "do": "tool",
18
+ "tool": "glob",
19
+ "args": { "pattern": { "$state": "pattern" } },
20
+ "out": { "files": "lines" }
21
+ },
22
+ {
23
+ "id": "read",
24
+ "do": "map",
25
+ "over": "files",
26
+ "maxConcurrency": 8,
27
+ "body": {
28
+ "do": "tool",
29
+ "tool": "read",
30
+ "args": { "file_path": { "$state": "item" } },
31
+ "out": { "contents": "text" }
32
+ }
33
+ },
34
+ {
35
+ "id": "extract",
36
+ "do": "map",
37
+ "over": "contents",
38
+ "maxConcurrency": 8,
39
+ "body": {
40
+ "do": "model",
41
+ "prompt": ["Extract topic, claim and risk as JSON. Source:\n", { "$state": "item" }],
42
+ "schema": {
43
+ "type": "object",
44
+ "required": ["topic", "claim", "risk"],
45
+ "properties": {
46
+ "topic": { "type": "string" },
47
+ "claim": { "type": "string" },
48
+ "risk": { "type": "string" }
49
+ }
50
+ },
51
+ "out": { "extracts": "json" }
52
+ }
53
+ },
54
+ {
55
+ "id": "draft_outline",
56
+ "do": "model",
57
+ "prompt": ["Draft a one-paragraph summary and an ordered outline from these extracts:\n", { "$state": "extracts" }],
58
+ "out": { "outline": "text" }
59
+ },
60
+ {
61
+ "id": "approve",
62
+ "do": "gate",
63
+ "question": "Approve this outline before I write the brief?",
64
+ "options": ["Approve", "Revise"],
65
+ "out": { "decision": "text" }
66
+ },
67
+ {
68
+ "id": "compose",
69
+ "do": "model",
70
+ "prompt": ["Write the final brief from this approved outline.\nOutline:\n", { "$state": "outline" }],
71
+ "out": { "brief": "text" }
72
+ },
73
+ {
74
+ "id": "write",
75
+ "do": "tool",
76
+ "tool": "write",
77
+ "args": { "file_path": "brief.md", "content": { "$state": "brief" } },
78
+ "out": {}
79
+ }
80
+ ],
81
+ "edges": [
82
+ { "from": "START", "to": "list" },
83
+ { "from": "list", "to": "read" },
84
+ { "from": "read", "to": "extract" },
85
+ { "from": "extract", "to": "draft_outline" },
86
+ { "from": "draft_outline", "to": "approve" },
87
+ { "from": "approve", "to": "compose", "when": { "op": "eq", "args": [{ "$state": "decision" }, "Approve"] } },
88
+ { "from": "approve", "to": "END", "when": true },
89
+ { "from": "compose", "to": "write" },
90
+ { "from": "write", "to": "END" }
91
+ ],
92
+ "output": ["brief", "decision"],
93
+ "maxSteps": 30
94
+ }
package/index.d.ts CHANGED
@@ -153,3 +153,139 @@ export interface GraphModule {
153
153
 
154
154
  /** Identity helper for a typed graph module. */
155
155
  export function defineGraph<T extends GraphModule>(module: T): T
156
+
157
+ // --- composed graphs (`langgraph_compose`) -----------------------------------
158
+
159
+ /** One declared state field. */
160
+ export interface StateField {
161
+ type: 'string' | 'number' | 'boolean' | 'json' | 'string[]' | 'number[]'
162
+ /** Required; must match `type`. */
163
+ default: unknown
164
+ /** Defaults to `replace`; parallel writers need append/add/max/min/merge. */
165
+ reducer?: 'replace' | 'append' | 'add' | 'max' | 'min' | 'merge'
166
+ }
167
+
168
+ /** A state reference; `field` or `field.path` into a json field. */
169
+ export interface StateRef {
170
+ $state: string
171
+ }
172
+
173
+ /** One prompt part: literal text or a state reference. */
174
+ export type PromptPart = string | StateRef
175
+
176
+ /** A node argument or set value: a JSON literal or a state reference. */
177
+ export type SpecValue = unknown | StateRef
178
+
179
+ /** How a node result is written to state. */
180
+ export type Transform = 'text' | 'lines' | 'json' | 'bool' | 'number' | 'first' | 'count'
181
+
182
+ /** A guard operator. */
183
+ export type GuardOp = 'eq' | 'ne' | 'in' | 'gt' | 'gte' | 'lt' | 'lte' | 'exists' | 'and' | 'or' | 'not'
184
+
185
+ /** A guard operand: a JSON literal or a state reference. */
186
+ export type GuardOperand = unknown | StateRef
187
+
188
+ /** A structured edge guard; `and`, `or`, and `not` nest guards. */
189
+ export type Guard = true | false | { op: GuardOp; args: Array<Guard | GuardOperand> }
190
+
191
+ /** One known atomic DSH tool action. */
192
+ export interface ComposeToolNode {
193
+ id: string
194
+ do: 'tool'
195
+ tool: string
196
+ args: Record<string, SpecValue>
197
+ out: Record<string, Transform>
198
+ }
199
+
200
+ /** One model judgment. */
201
+ export interface ComposeModelNode {
202
+ id: string
203
+ do: 'model'
204
+ prompt: PromptPart[]
205
+ system?: string
206
+ schema?: Record<string, unknown>
207
+ out: Record<string, Transform>
208
+ }
209
+
210
+ /** Open work: a prose brief plus tools, run by LangGraph's prebuilt ReAct agent. */
211
+ export interface ComposeAgentNode {
212
+ id: string
213
+ do: 'agent'
214
+ prompt: PromptPart[]
215
+ tools: string[]
216
+ out: Record<string, Transform>
217
+ }
218
+
219
+ /** A human decision through LangGraph `interrupt()`. */
220
+ export interface ComposeGateNode {
221
+ id: string
222
+ do: 'gate'
223
+ question: string
224
+ options?: string[]
225
+ out: Record<string, Transform>
226
+ }
227
+
228
+ /** A DSH child session. */
229
+ export interface ComposeSubagentNode {
230
+ id: string
231
+ do: 'subagent'
232
+ prompt: PromptPart[]
233
+ schema?: Record<string, unknown>
234
+ out: Record<string, Transform>
235
+ }
236
+
237
+ /** A map body; its `id` is optional and ignored. */
238
+ export type ComposeMapBody =
239
+ | (Omit<ComposeToolNode, 'id'> & { id?: string })
240
+ | (Omit<ComposeModelNode, 'id'> & { id?: string })
241
+ | (Omit<ComposeAgentNode, 'id'> & { id?: string })
242
+ | (Omit<ComposeSubagentNode, 'id'> & { id?: string })
243
+
244
+ /** Dynamic fan-out over a string[] or number[] field. */
245
+ export interface ComposeMapNode {
246
+ id: string
247
+ do: 'map'
248
+ over: string
249
+ body: ComposeMapBody
250
+ maxItems?: number
251
+ maxConcurrency?: number
252
+ }
253
+
254
+ /** A literal state update. */
255
+ export interface ComposeSetNode {
256
+ id: string
257
+ do: 'set'
258
+ values: Record<string, SpecValue>
259
+ }
260
+
261
+ /** Any node the compiler accepts. */
262
+ export type ComposeNode =
263
+ | ComposeToolNode
264
+ | ComposeModelNode
265
+ | ComposeAgentNode
266
+ | ComposeGateNode
267
+ | ComposeMapNode
268
+ | ComposeSetNode
269
+ | ComposeSubagentNode
270
+
271
+ /** One edge. `from` is a node id or `START`; `to` is a node id or `END`. */
272
+ export interface ComposeEdge {
273
+ from: string
274
+ to: string
275
+ when?: Guard
276
+ }
277
+
278
+ /** The strict JSON IR accepted by `langgraph_compose`. */
279
+ export interface GraphSpec {
280
+ ir: 1
281
+ name?: string
282
+ description?: string
283
+ state: Record<string, StateField>
284
+ nodes: ComposeNode[]
285
+ edges: ComposeEdge[]
286
+ output?: string[]
287
+ maxSteps?: number
288
+ }
289
+
290
+ /** Actions accepted by `langgraph_compose`. */
291
+ export type ComposeAction = 'describe' | 'validate' | 'compose' | 'list' | 'get' | 'delete' | 'fork'