@lzn1998/dsh-langgraph 0.0.0-stage → 0.5.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.
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/cordis.patch.yml +11 -0
- package/docs/DETAIL.md +239 -0
- package/docs/architecture.svg +117 -0
- package/dsh-chat-model.js +157 -0
- package/dsh-checkpointer.js +520 -0
- package/dsh-tools.js +56 -0
- package/dsh-trace.js +313 -0
- package/graphs/approval-gate.js +52 -0
- package/graphs/ask-subagent.js +48 -0
- package/graphs/ask-tools.js +57 -0
- package/graphs/ask-workspace.js +48 -0
- package/graphs/scan-report.js +51 -0
- package/graphs/typed-report.ts +58 -0
- package/icon.svg +11 -0
- package/index.d.ts +119 -0
- package/index.js +889 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +56 -4
package/dsh-trace.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangGraph activity as DSH log-only trace events.
|
|
3
|
+
*
|
|
4
|
+
* DSH's Trajectory view renders a tool call and its nested sub-calls from
|
|
5
|
+
* `tool/ptc-dispatch-start` and `tool/ptc-dispatch`. Those two events are
|
|
6
|
+
* log-only: `deriveMessages()` ignores them, so a trace never enters the model
|
|
7
|
+
* context. They are also the only nested-call vocabulary the stock Trajectory
|
|
8
|
+
* view understands, which makes them the non-invasive bridge for a third-party
|
|
9
|
+
* plugin: neither DSH nor LangGraph is modified.
|
|
10
|
+
*
|
|
11
|
+
* Every event below has the shape the PTC executor writes. The outer tool call
|
|
12
|
+
* is the `langgraph_run` row; node boundaries, LangChain tool calls, and model
|
|
13
|
+
* calls are its nested sub-calls, so the whole graph is one expandable run.
|
|
14
|
+
* @module dsh-langgraph/trace
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const MAX_TEXT = 4000
|
|
18
|
+
const MAX_MESSAGE_TEXT = 1500
|
|
19
|
+
|
|
20
|
+
/** JSON-safe value, or the fallback when the value cannot round-trip. */
|
|
21
|
+
function jsonValue(value, fallback = {}) {
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(JSON.stringify(value, (_key, item) => (typeof item === 'bigint' ? String(item) : item)))
|
|
24
|
+
} catch {
|
|
25
|
+
return fallback
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Concatenated text of a content value. */
|
|
30
|
+
function contentText(value) {
|
|
31
|
+
if (typeof value === 'string') return value
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
return value
|
|
34
|
+
.map(part => (typeof part === 'string' ? part : part?.type === 'text' ? part.text : ''))
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
.join('\n')
|
|
37
|
+
}
|
|
38
|
+
if (value === null || value === undefined) return ''
|
|
39
|
+
return JSON.stringify(jsonValue(value))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Bound one trace field so a large state or result cannot flood the log. */
|
|
43
|
+
function clip(text, limit = MAX_TEXT) {
|
|
44
|
+
const value = String(text ?? '')
|
|
45
|
+
return value.length <= limit ? value : `${value.slice(0, limit)}\n… [truncated ${value.length - limit} chars]`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Summarize the LangChain message batches of one model call. */
|
|
49
|
+
function summarizeMessages(messages) {
|
|
50
|
+
const flat = Array.isArray(messages) ? messages.flat(1) : []
|
|
51
|
+
return flat.map(message => ({
|
|
52
|
+
role: String(message?.getType?.() ?? message?._getType?.() ?? message?.role ?? 'message'),
|
|
53
|
+
text: clip(contentText(message?.content), MAX_MESSAGE_TEXT),
|
|
54
|
+
}))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Text of one `LLMResult`, falling back to the tool calls a model-only reply requested. */
|
|
58
|
+
function resultText(output) {
|
|
59
|
+
const generation = output?.generations?.[0]?.[0]
|
|
60
|
+
if (generation === undefined) return ''
|
|
61
|
+
if (typeof generation.text === 'string' && generation.text !== '') return generation.text
|
|
62
|
+
const content = contentText(generation.message?.content)
|
|
63
|
+
if (content !== '') return content
|
|
64
|
+
const toolCalls = generation.message?.tool_calls
|
|
65
|
+
return Array.isArray(toolCalls) && toolCalls.length > 0 ? JSON.stringify(jsonValue(toolCalls, [])) : ''
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a tracer bound to one `langgraph_run` execution.
|
|
70
|
+
* @param {{ agent?: object, callId?: string, rootCallId?: string }} exec - the tool execution.
|
|
71
|
+
* @param {string} graphId - the graph being run.
|
|
72
|
+
* @param {boolean} [configured=true] - row config; false disables tracing.
|
|
73
|
+
* @param {(event: { phase: 'start' | 'end', name: string, isError?: boolean }) => void} [onEvent]
|
|
74
|
+
* optional progress sink for a background job's output ring.
|
|
75
|
+
* @returns {object} tracer with `callbacks`, `begin`, `finish`, and `currentNode`.
|
|
76
|
+
*/
|
|
77
|
+
export function createTracer(exec, graphId, configured = true, onEvent) {
|
|
78
|
+
const session = exec?.agent?.session
|
|
79
|
+
const outerCallId = exec?.callId
|
|
80
|
+
const rootCallId = exec?.rootCallId ?? outerCallId
|
|
81
|
+
const enabled = configured && session !== undefined && typeof session.append === 'function'
|
|
82
|
+
&& typeof outerCallId === 'string' && typeof rootCallId === 'string'
|
|
83
|
+
let live = enabled
|
|
84
|
+
let sequence = 0
|
|
85
|
+
/** runId -> trace handle for a node, inner chain, tool, or model call. */
|
|
86
|
+
const runs = new Map()
|
|
87
|
+
/** Active node traces in start order; the last one is the best-effort current node. */
|
|
88
|
+
const nodes = []
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Append one trace event. A rejected append disables tracing for this run
|
|
92
|
+
* rather than risking the graph, which is the product work.
|
|
93
|
+
*/
|
|
94
|
+
function append(type, data) {
|
|
95
|
+
if (!live) return false
|
|
96
|
+
try {
|
|
97
|
+
session.append(type, data)
|
|
98
|
+
return true
|
|
99
|
+
} catch {
|
|
100
|
+
live = false
|
|
101
|
+
return false
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Report one trace transition to the optional background-job sink. */
|
|
106
|
+
function notify(event) {
|
|
107
|
+
if (onEvent === undefined) return
|
|
108
|
+
try {
|
|
109
|
+
onEvent(event)
|
|
110
|
+
} catch {
|
|
111
|
+
// A progress sink must never break the graph.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Start one nested trace.
|
|
117
|
+
* @param {string} name - display name in Trajectory.
|
|
118
|
+
* @param {unknown} args - JSON-safe arguments shown by the inspector.
|
|
119
|
+
* @param {object} [parent] - explicit parent trace; defaults to the current node.
|
|
120
|
+
* @returns {object | undefined} a handle for `finish`, or undefined when tracing is off.
|
|
121
|
+
*/
|
|
122
|
+
function begin(name, args, parent) {
|
|
123
|
+
if (!live) return undefined
|
|
124
|
+
// An omitted parent means "the current node"; null means "explicitly outer"
|
|
125
|
+
// (siblings under the root graph must not nest under the last active node).
|
|
126
|
+
const resolved = parent === undefined ? nodes.at(-1) : parent
|
|
127
|
+
const parentCallId = resolved === null ? outerCallId : resolved?.subCallId ?? outerCallId
|
|
128
|
+
const subCallId = `${outerCallId}:lg:${++sequence}`
|
|
129
|
+
const argumentsValue = jsonValue(args ?? {})
|
|
130
|
+
if (!append('tool/ptc-dispatch-start', {
|
|
131
|
+
rootCallId,
|
|
132
|
+
parentCallId,
|
|
133
|
+
subCallId,
|
|
134
|
+
name,
|
|
135
|
+
arguments: argumentsValue,
|
|
136
|
+
})) return undefined
|
|
137
|
+
notify({ phase: 'start', name })
|
|
138
|
+
return { rootCallId, parentCallId, subCallId, name, arguments: argumentsValue }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Settle one nested trace.
|
|
143
|
+
* @param {object | undefined} handle - the handle from `begin`.
|
|
144
|
+
* @param {{ content?: unknown[], isError?: boolean, error?: object, meta?: unknown }} [outcome] - trace outcome.
|
|
145
|
+
*/
|
|
146
|
+
function finish(handle, outcome = {}) {
|
|
147
|
+
if (handle === undefined || !live) return
|
|
148
|
+
const { content = [], isError = false, error, meta } = outcome
|
|
149
|
+
if (!append('tool/ptc-dispatch', {
|
|
150
|
+
rootCallId: handle.rootCallId,
|
|
151
|
+
parentCallId: handle.parentCallId,
|
|
152
|
+
subCallId: handle.subCallId,
|
|
153
|
+
name: handle.name,
|
|
154
|
+
arguments: handle.arguments,
|
|
155
|
+
isError,
|
|
156
|
+
content: jsonValue(content, []),
|
|
157
|
+
...(error === undefined ? {} : { error: jsonValue(error, {}) }),
|
|
158
|
+
...(meta === undefined ? {} : { meta: jsonValue(meta, {}) }),
|
|
159
|
+
})) return
|
|
160
|
+
notify({ phase: 'end', name: handle.name, isError })
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The trace a nested callback belongs to. An undefined parent run means the
|
|
165
|
+
* caller has no enclosing run and the current node is the right fallback; a
|
|
166
|
+
* defined but unmapped run (the graph root) must stay at the outer level so
|
|
167
|
+
* fan-out siblings do not nest under whichever node started last.
|
|
168
|
+
*/
|
|
169
|
+
function parentOf(runId) {
|
|
170
|
+
if (runId === undefined) return undefined
|
|
171
|
+
return runs.get(runId) ?? null
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Close a trace as a failure, or as a pause when LangGraph interrupted it. */
|
|
175
|
+
function closeError(handle, error) {
|
|
176
|
+
if (error?.name === 'GraphInterrupt' || error?.name === 'NodeInterrupt') {
|
|
177
|
+
finish(handle, {
|
|
178
|
+
isError: false,
|
|
179
|
+
content: [{ type: 'text', text: `interrupted: ${clip(error?.message ?? '')}` }],
|
|
180
|
+
})
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
finish(handle, {
|
|
184
|
+
isError: true,
|
|
185
|
+
content: [{ type: 'text', text: clip(error?.message ?? String(error ?? 'failed')) }],
|
|
186
|
+
error: {
|
|
187
|
+
name: error?.name ?? 'Error',
|
|
188
|
+
code: typeof error?.code === 'string' ? error.code : 'LANGGRAPH_ERROR',
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Whether one chain run is an actual graph node rather than an inner runnable. */
|
|
194
|
+
function isNodeRun(node, tags) {
|
|
195
|
+
return typeof node === 'string' && node !== '' && node !== '__start__' && node !== '__end__'
|
|
196
|
+
&& Array.isArray(tags) && tags.some(tag => typeof tag === 'string' && tag.startsWith('graph:step:'))
|
|
197
|
+
&& !tags.includes('langsmith:hidden')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const handler = {
|
|
201
|
+
name: 'dsh-langgraph-trace',
|
|
202
|
+
|
|
203
|
+
handleChainStart(serialized, inputs, runId, parentRunId, tags, metadata) {
|
|
204
|
+
const node = metadata?.langgraph_node
|
|
205
|
+
if (isNodeRun(node, tags)) {
|
|
206
|
+
const handle = begin(`langgraph:node ${node}`, {
|
|
207
|
+
graph: graphId,
|
|
208
|
+
node,
|
|
209
|
+
step: metadata?.langgraph_step,
|
|
210
|
+
}, parentRunId === undefined ? null : parentOf(parentRunId))
|
|
211
|
+
if (handle !== undefined) {
|
|
212
|
+
const trace = { ...handle, kind: 'node', runId, node }
|
|
213
|
+
runs.set(runId, trace)
|
|
214
|
+
nodes.push(trace)
|
|
215
|
+
}
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
// Inner chains inherit their node's trace, so a tool/model event nested
|
|
219
|
+
// under a RunnableLambda still lands inside the node.
|
|
220
|
+
const inherited = runs.get(parentRunId)
|
|
221
|
+
if (inherited !== undefined) runs.set(runId, inherited)
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
handleChainEnd(outputs, runId) {
|
|
225
|
+
const trace = runs.get(runId)
|
|
226
|
+
if (trace === undefined) return
|
|
227
|
+
if (trace.kind === 'node' && trace.runId === runId) {
|
|
228
|
+
finish(trace, {
|
|
229
|
+
content: [{ type: 'text', text: clip(JSON.stringify(jsonValue(outputs))) }],
|
|
230
|
+
})
|
|
231
|
+
const index = nodes.lastIndexOf(trace)
|
|
232
|
+
if (index >= 0) nodes.splice(index, 1)
|
|
233
|
+
}
|
|
234
|
+
runs.delete(runId)
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
handleChainError(error, runId) {
|
|
238
|
+
const trace = runs.get(runId)
|
|
239
|
+
if (trace === undefined) return
|
|
240
|
+
if (trace.kind === 'node' && trace.runId === runId) {
|
|
241
|
+
closeError(trace, error)
|
|
242
|
+
const index = nodes.lastIndexOf(trace)
|
|
243
|
+
if (index >= 0) nodes.splice(index, 1)
|
|
244
|
+
}
|
|
245
|
+
runs.delete(runId)
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
handleToolStart(tool, input, runId, parentRunId) {
|
|
249
|
+
// Prebuilt ToolNode serializes a DynamicStructuredTool wrapper without a
|
|
250
|
+
// name; the DSH bridge traces the real name from the tool definition.
|
|
251
|
+
const name = typeof tool?.name === 'string' && tool.name !== '' ? tool.name : undefined
|
|
252
|
+
if (name === undefined) return
|
|
253
|
+
const handle = begin(`langgraph:tool ${name}`, input, parentOf(parentRunId))
|
|
254
|
+
if (handle !== undefined) runs.set(runId, { ...handle, kind: 'tool' })
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
handleToolEnd(output, runId) {
|
|
258
|
+
const trace = runs.get(runId)
|
|
259
|
+
if (trace === undefined) return
|
|
260
|
+
finish(trace, { content: [{ type: 'text', text: clip(contentText(output)) }] })
|
|
261
|
+
runs.delete(runId)
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
handleToolError(error, runId) {
|
|
265
|
+
const trace = runs.get(runId)
|
|
266
|
+
if (trace === undefined) return
|
|
267
|
+
closeError(trace, error)
|
|
268
|
+
runs.delete(runId)
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
handleChatModelStart(llm, messages, runId, parentRunId) {
|
|
272
|
+
const handle = begin('langgraph:model', {
|
|
273
|
+
model: llm?.model ?? llm?.name ?? llm?._llmType?.(),
|
|
274
|
+
messages: summarizeMessages(messages),
|
|
275
|
+
}, parentOf(parentRunId))
|
|
276
|
+
if (handle !== undefined) runs.set(runId, { ...handle, kind: 'model' })
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
handleLLMStart(llm, prompts, runId, parentRunId) {
|
|
280
|
+
const handle = begin('langgraph:model', {
|
|
281
|
+
model: llm?.model ?? llm?.name ?? llm?._llmType?.(),
|
|
282
|
+
prompts: (Array.isArray(prompts) ? prompts : []).map(prompt => clip(prompt, MAX_MESSAGE_TEXT)),
|
|
283
|
+
}, parentOf(parentRunId))
|
|
284
|
+
if (handle !== undefined) runs.set(runId, { ...handle, kind: 'model' })
|
|
285
|
+
},
|
|
286
|
+
|
|
287
|
+
handleLLMEnd(output, runId) {
|
|
288
|
+
const trace = runs.get(runId)
|
|
289
|
+
if (trace === undefined) return
|
|
290
|
+
const usage = output?.llmOutput?.tokenUsage
|
|
291
|
+
finish(trace, {
|
|
292
|
+
content: [{ type: 'text', text: clip(resultText(output)) }],
|
|
293
|
+
...(usage === undefined ? {} : { meta: { usage } }),
|
|
294
|
+
})
|
|
295
|
+
runs.delete(runId)
|
|
296
|
+
},
|
|
297
|
+
|
|
298
|
+
handleLLMError(error, runId) {
|
|
299
|
+
const trace = runs.get(runId)
|
|
300
|
+
if (trace === undefined) return
|
|
301
|
+
closeError(trace, error)
|
|
302
|
+
runs.delete(runId)
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
enabled,
|
|
308
|
+
callbacks: enabled ? [handler] : [],
|
|
309
|
+
begin,
|
|
310
|
+
finish,
|
|
311
|
+
currentNode: () => nodes.at(-1),
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example workflow that pauses for a decision before doing any work.
|
|
3
|
+
*
|
|
4
|
+
* The gate node calls LangGraph's `interrupt(...)`: the run stops, the agent sees
|
|
5
|
+
* `status: "interrupted"` with the question, and `langgraph_run({ graph, thread,
|
|
6
|
+
* resume })` continues exactly at the same node with the answer.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
10
|
+
export const id = 'approval-gate'
|
|
11
|
+
|
|
12
|
+
/** One-line description shown when the agent lists graphs. */
|
|
13
|
+
export const description = 'Ask before scanning: pause on a decision, then glob the approved pattern.'
|
|
14
|
+
|
|
15
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
16
|
+
export const input = {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
pattern: { type: 'string', description: 'Glob pattern to scan once approved; defaults to **/*.js.' },
|
|
20
|
+
},
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Interpret the resume value as an approve/reject decision. */
|
|
25
|
+
function approvedBy(answer) {
|
|
26
|
+
if (typeof answer === 'boolean') return answer
|
|
27
|
+
return /^(y|yes|ok|approve|approved|true)$/i.test(String(answer ?? '').trim())
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build the graph.
|
|
32
|
+
* @param {object} api - LangGraph API plus `helpers` bound to one run.
|
|
33
|
+
* @returns {object} an uncompiled StateGraph; the plugin compiles it with the checkpointer.
|
|
34
|
+
*/
|
|
35
|
+
export function build({ StateGraph, Annotation, START, END, interrupt, helpers }) {
|
|
36
|
+
const State = Annotation.Root({
|
|
37
|
+
pattern: Annotation({ reducer: (_, next) => next, default: () => '**/*.js' }),
|
|
38
|
+
decision: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
39
|
+
approved: Annotation({ reducer: (_, next) => next, default: () => false }),
|
|
40
|
+
listing: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
41
|
+
})
|
|
42
|
+
return new StateGraph(State)
|
|
43
|
+
.addNode('confirm', async state => {
|
|
44
|
+
// Pauses the run; the value returned here is the `resume` value.
|
|
45
|
+
const answer = interrupt({ question: `Scan ${state.pattern} in this workspace?` })
|
|
46
|
+
return { decision: String(answer), approved: approvedBy(answer) }
|
|
47
|
+
})
|
|
48
|
+
.addNode('scan', async state => ({ listing: await helpers.callTool('glob', { pattern: state.pattern }) }))
|
|
49
|
+
.addEdge(START, 'confirm')
|
|
50
|
+
.addConditionalEdges('confirm', state => (state.approved ? 'scan' : END))
|
|
51
|
+
.addEdge('scan', END)
|
|
52
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example graph that delegates one task to a DSH subagent.
|
|
3
|
+
*
|
|
4
|
+
* `helpers.subagent(prompt, options)` starts a caller-delivered child through
|
|
5
|
+
* `ctx.subagents`, awaits its final output, and disposes the exact activation.
|
|
6
|
+
* The child has its own Session and tool scope; the parent graph only sees the
|
|
7
|
+
* returned text/structured result. In Trajectory the delegation appears as a
|
|
8
|
+
* `langgraph:subagent` sub-call under the enclosing node.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
12
|
+
export const id = 'ask-subagent'
|
|
13
|
+
|
|
14
|
+
/** One-line description shown when the agent lists graphs. */
|
|
15
|
+
export const description = 'Delegate a question to a DSH subagent from inside a graph node.'
|
|
16
|
+
|
|
17
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
18
|
+
export const input = {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
question: { type: 'string', description: 'Task sent to the child agent.' },
|
|
22
|
+
},
|
|
23
|
+
additionalProperties: false,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Build the graph.
|
|
28
|
+
* @param {object} api - LangGraph API plus `helpers` bound to one run.
|
|
29
|
+
* @returns {object} an uncompiled StateGraph.
|
|
30
|
+
*/
|
|
31
|
+
export function build({ StateGraph, Annotation, START, END, helpers }) {
|
|
32
|
+
const State = Annotation.Root({
|
|
33
|
+
question: Annotation({ reducer: (_, next) => next, default: () => 'What is this workspace?' }),
|
|
34
|
+
answer: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
35
|
+
stopReason: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
36
|
+
child: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
37
|
+
})
|
|
38
|
+
return new StateGraph(State)
|
|
39
|
+
.addNode('delegate', async state => {
|
|
40
|
+
const result = await helpers.subagent(
|
|
41
|
+
`${state.question}\n\nUse this workspace's tools when you need evidence, and answer in at most three sentences.`,
|
|
42
|
+
{ label: 'langgraph delegation' },
|
|
43
|
+
)
|
|
44
|
+
return { answer: result.text, stopReason: result.stopReason, child: result.name }
|
|
45
|
+
})
|
|
46
|
+
.addEdge(START, 'delegate')
|
|
47
|
+
.addEdge('delegate', END)
|
|
48
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example ReAct workflow: LangGraph's prebuilt agent driving DSH tools with this
|
|
3
|
+
* deployment's model.
|
|
4
|
+
*
|
|
5
|
+
* This is the "LangGraph in DSH" case end to end: the graph is LangGraph's own
|
|
6
|
+
* prebuilt agent, its chat model is the DSH model route (`helpers.chatModel()`), and
|
|
7
|
+
* every tool it calls is a DSH tool ran in this session's scope (`helpers.tools`).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
11
|
+
export const id = 'ask-tools'
|
|
12
|
+
|
|
13
|
+
/** One-line description shown when the agent lists graphs. */
|
|
14
|
+
export const description = 'Answer a question about this workspace with a ReAct agent over the glob/read tools.'
|
|
15
|
+
|
|
16
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
17
|
+
export const input = {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
question: { type: 'string', description: 'Question about this workspace.' },
|
|
21
|
+
},
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build the graph.
|
|
27
|
+
* @param {object} api - LangGraph API plus `helpers` bound to one run.
|
|
28
|
+
* @returns {Promise<object>} a compiled LangGraph.
|
|
29
|
+
*/
|
|
30
|
+
export async function build({ StateGraph, Annotation, START, END, helpers }) {
|
|
31
|
+
const State = Annotation.Root({
|
|
32
|
+
question: Annotation({ reducer: (_, next) => next, default: () => 'What kind of project is this workspace?' }),
|
|
33
|
+
answer: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
34
|
+
toolCalls: Annotation({ reducer: (_, next) => next, default: () => 0 }),
|
|
35
|
+
})
|
|
36
|
+
const agent = await helpers.createReactAgent({
|
|
37
|
+
llm: helpers.chatModel({ temperature: 0 }),
|
|
38
|
+
tools: helpers.tools(['glob', 'read']),
|
|
39
|
+
prompt: 'You inspect a workspace with two tools and answer in at most three sentences.',
|
|
40
|
+
})
|
|
41
|
+
return new StateGraph(State)
|
|
42
|
+
.addNode('react', async state => {
|
|
43
|
+
const result = await agent.invoke({ messages: [['human', state.question]] })
|
|
44
|
+
const messages = result.messages ?? []
|
|
45
|
+
const last = messages[messages.length - 1]
|
|
46
|
+
const text = typeof last?.content === 'string'
|
|
47
|
+
? last.content
|
|
48
|
+
: Array.isArray(last?.content)
|
|
49
|
+
? last.content.map(part => (part?.type === 'text' ? part.text : '')).join('')
|
|
50
|
+
: String(last?.content ?? '')
|
|
51
|
+
const toolCalls = messages.filter(message => (message.getType?.() ?? '') === 'ai')
|
|
52
|
+
.reduce((total, message) => total + (message.tool_calls?.length ?? 0), 0)
|
|
53
|
+
return { answer: text.trim(), toolCalls }
|
|
54
|
+
})
|
|
55
|
+
.addEdge(START, 'react')
|
|
56
|
+
.addEdge('react', END)
|
|
57
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example workflow that mixes DSH tool calls with a model call inside one graph.
|
|
3
|
+
*
|
|
4
|
+
* `scan` reads the workspace through the DSH `glob` tool; `answer` asks the
|
|
5
|
+
* deployment's model to explain the result. The graph owns the control flow, DSH
|
|
6
|
+
* owns the tools and the model route.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
10
|
+
export const id = 'ask-workspace'
|
|
11
|
+
|
|
12
|
+
/** One-line description shown when the agent lists graphs. */
|
|
13
|
+
export const description = 'List files matching a glob, then have the model explain the result in one sentence.'
|
|
14
|
+
|
|
15
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
16
|
+
export const input = {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
pattern: { type: 'string', description: 'Glob pattern to list; defaults to **/*.md.' },
|
|
20
|
+
},
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Build the graph.
|
|
26
|
+
* @param {object} api - LangGraph API plus `helpers` bound to one run.
|
|
27
|
+
* @returns {object} a compiled LangGraph.
|
|
28
|
+
*/
|
|
29
|
+
export function build({ StateGraph, Annotation, START, END, helpers }) {
|
|
30
|
+
const State = Annotation.Root({
|
|
31
|
+
pattern: Annotation({ reducer: (_, next) => next, default: () => '**/*.md' }),
|
|
32
|
+
files: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
33
|
+
explanation: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
34
|
+
})
|
|
35
|
+
return new StateGraph(State)
|
|
36
|
+
.addNode('scan', async state => ({ files: await helpers.callTool('glob', { pattern: state.pattern }) }))
|
|
37
|
+
.addNode('answer', async state => {
|
|
38
|
+
const { text } = await helpers.callModel(
|
|
39
|
+
`Files matching ${state.pattern}:\n${state.files || '(none)'}\n\n`
|
|
40
|
+
+ 'In one sentence, say what this says about the project layout.',
|
|
41
|
+
{ system: 'You are explaining a repository layout to its owner. Be concrete and brief.' },
|
|
42
|
+
)
|
|
43
|
+
return { explanation: text }
|
|
44
|
+
})
|
|
45
|
+
.addEdge(START, 'scan')
|
|
46
|
+
.addEdge('scan', 'answer')
|
|
47
|
+
.addEdge('answer', END)
|
|
48
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example LangGraph workflow shipped with the plugin.
|
|
3
|
+
*
|
|
4
|
+
* It demonstrates the two things this plugin adds to LangGraph inside DSH:
|
|
5
|
+
* - a graph is registered by id and run from the agent's `langgraph_run` tool;
|
|
6
|
+
* - nodes call real DSH tools through `helpers.callTool`, in the calling agent's
|
|
7
|
+
* scope (same sandbox, same approvals, same session), so the graph is a
|
|
8
|
+
* coordinator of DSH work rather than a separate runtime.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
12
|
+
export const id = 'scan-report'
|
|
13
|
+
|
|
14
|
+
/** One-line description shown when the agent lists graphs. */
|
|
15
|
+
export const description = 'Glob a pattern in the workspace, then report whether the match set is large.'
|
|
16
|
+
|
|
17
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
18
|
+
export const input = {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
pattern: { type: 'string', description: 'Glob pattern to scan; defaults to **/*.js.' },
|
|
22
|
+
},
|
|
23
|
+
additionalProperties: false,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Build the graph.
|
|
28
|
+
* @param {object} api - LangGraph API plus `helpers` bound to one run.
|
|
29
|
+
* @returns {object} a compiled LangGraph.
|
|
30
|
+
*/
|
|
31
|
+
export function build({ StateGraph, Annotation, START, END, helpers }) {
|
|
32
|
+
const State = Annotation.Root({
|
|
33
|
+
pattern: Annotation({ reducer: (_, next) => next, default: () => '**/*.js' }),
|
|
34
|
+
matches: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
35
|
+
count: Annotation({ reducer: (_, next) => next, default: () => 0 }),
|
|
36
|
+
verdict: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
37
|
+
report: Annotation({ reducer: (_, next) => next, default: () => '' }),
|
|
38
|
+
})
|
|
39
|
+
return new StateGraph(State)
|
|
40
|
+
.addNode('scan', async state => {
|
|
41
|
+
const listing = await helpers.callTool('glob', { pattern: state.pattern })
|
|
42
|
+
const count = listing.split('\n').map(line => line.trim()).filter(Boolean).length
|
|
43
|
+
return { matches: listing, count }
|
|
44
|
+
})
|
|
45
|
+
.addNode('large', async state => ({ verdict: 'large', report: `${state.count} files match ${state.pattern}` }))
|
|
46
|
+
.addNode('small', async state => ({ verdict: 'small', report: `${state.count} files match ${state.pattern}` }))
|
|
47
|
+
.addEdge(START, 'scan')
|
|
48
|
+
.addConditionalEdges('scan', state => (state.count > 5 ? 'large' : 'small'))
|
|
49
|
+
.addEdge('large', END)
|
|
50
|
+
.addEdge('small', END)
|
|
51
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A TypeScript example graph.
|
|
3
|
+
*
|
|
4
|
+
* The plugin loads `.ts` graph modules directly on Node 24.22+ (type stripping),
|
|
5
|
+
* so this file needs no build step. Keep the syntax erasable: no enums,
|
|
6
|
+
* namespaces, parameter properties, or decorators.
|
|
7
|
+
*
|
|
8
|
+
* It is also the smallest end-to-end proof that a typed graph module can use
|
|
9
|
+
* `GraphApi` without importing the plugin at runtime: `import type` is erased.
|
|
10
|
+
*/
|
|
11
|
+
import type { GraphApi, GraphModule } from '../index.js'
|
|
12
|
+
|
|
13
|
+
/** Graph id used by the `langgraph_run` tool. */
|
|
14
|
+
export const id: string = 'typed-report'
|
|
15
|
+
|
|
16
|
+
/** One-line description shown when the agent lists graphs. */
|
|
17
|
+
export const description: string = 'TypeScript example: glob a pattern, then classify the match set.'
|
|
18
|
+
|
|
19
|
+
/** JSON Schema of the initial state, shown to the agent when it lists graphs. */
|
|
20
|
+
export const input = {
|
|
21
|
+
type: 'object',
|
|
22
|
+
properties: {
|
|
23
|
+
pattern: { type: 'string', description: 'Glob pattern to scan; defaults to **/*.js.' },
|
|
24
|
+
},
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Build a small typed graph over `helpers.callTool`. */
|
|
29
|
+
export function build({ StateGraph, Annotation, START, END, helpers }: GraphApi): unknown {
|
|
30
|
+
const State = Annotation.Root({
|
|
31
|
+
pattern: Annotation({ reducer: (_previous: string, next: string) => next, default: () => '**/*.js' }),
|
|
32
|
+
files: Annotation({ reducer: (_previous: string, next: string) => next, default: () => '' }),
|
|
33
|
+
count: Annotation({ reducer: (_previous: number, next: number) => next, default: () => 0 }),
|
|
34
|
+
verdict: Annotation({ reducer: (_previous: string, next: string) => next, default: () => '' }),
|
|
35
|
+
})
|
|
36
|
+
return new StateGraph(State)
|
|
37
|
+
.addNode('scan', async (state: { pattern: string }) => {
|
|
38
|
+
const listing = await helpers.callTool('glob', { pattern: state.pattern })
|
|
39
|
+
const count = listing.split('\n').map(line => line.trim()).filter(Boolean).length
|
|
40
|
+
return { files: listing, count }
|
|
41
|
+
})
|
|
42
|
+
.addNode('large', async (state: { count: number; pattern: string }) => ({
|
|
43
|
+
verdict: 'large',
|
|
44
|
+
files: `${state.count} files match ${state.pattern}`,
|
|
45
|
+
}))
|
|
46
|
+
.addNode('small', async (state: { count: number; pattern: string }) => ({
|
|
47
|
+
verdict: 'small',
|
|
48
|
+
files: `${state.count} files match ${state.pattern}`,
|
|
49
|
+
}))
|
|
50
|
+
.addEdge(START, 'scan')
|
|
51
|
+
.addConditionalEdges('scan', (state: { count: number }) => (state.count > 5 ? 'large' : 'small'))
|
|
52
|
+
.addEdge('large', END)
|
|
53
|
+
.addEdge('small', END)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The module shape the plugin checks at load time. */
|
|
57
|
+
const module: GraphModule = { id, description, input, build }
|
|
58
|
+
export default module
|
package/icon.svg
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128" role="img" aria-label="LangGraph workflow">
|
|
2
|
+
<rect width="128" height="128" rx="28" fill="#0f172a"/>
|
|
3
|
+
<g stroke="#38bdf8" stroke-width="4" stroke-linecap="round" fill="none">
|
|
4
|
+
<path d="M34 40 L64 64 L34 88"/>
|
|
5
|
+
<path d="M64 64 L96 64"/>
|
|
6
|
+
</g>
|
|
7
|
+
<circle cx="34" cy="40" r="9" fill="#38bdf8"/>
|
|
8
|
+
<circle cx="34" cy="88" r="9" fill="#38bdf8"/>
|
|
9
|
+
<circle cx="64" cy="64" r="10" fill="#e2e8f0"/>
|
|
10
|
+
<circle cx="98" cy="64" r="9" fill="#38bdf8"/>
|
|
11
|
+
</svg>
|