@lzn1998/dsh-langgraph 0.0.0-stage → 0.13.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.
@@ -0,0 +1,117 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="1240" height="1010" viewBox="0 0 1240 1010" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" role="img" aria-label="dsh-langgraph v0.4 architecture">
2
+ <defs>
3
+ <marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="#94a3b8"/></marker>
4
+ <marker id="dot" viewBox="0 0 6 6" refX="1" refY="3" markerWidth="5" markerHeight="5"><circle cx="3" cy="3" r="2.5" fill="#94a3b8"/></marker>
5
+ </defs>
6
+ <rect width="1240" height="1010" fill="#ffffff"/>
7
+ <text x="60" y="46" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="21" font-weight="700" fill="#0f172a" text-anchor="start">dsh-langgraph v0.6 — 运行时架构</text>
8
+ <text x="60" y="70" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12.5" font-weight="400" fill="#64748b" text-anchor="start">LangGraph 负责流程与状态契约,DSH 负责执行、鉴权、存储与审计</text>
9
+ <rect x="790" y="52" width="12" height="12" rx="3" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
10
+ <text x="808" y="63" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12" font-weight="400" fill="#64748b" text-anchor="start">DSH</text>
11
+ <rect x="855" y="52" width="12" height="12" rx="3" fill="#ecfdf5" stroke="#059669" stroke-width="1.5"/>
12
+ <text x="873" y="63" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12" font-weight="400" fill="#64748b" text-anchor="start">本插件</text>
13
+ <rect x="920" y="52" width="12" height="12" rx="3" fill="#f5f3ff" stroke="#7c3aed" stroke-width="1.5"/>
14
+ <text x="938" y="63" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12" font-weight="400" fill="#64748b" text-anchor="start">LangGraph</text>
15
+ <rect x="60" y="96" width="420" height="76" rx="10" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
16
+ <text x="78" y="126" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="15" font-weight="600" fill="#0f172a" text-anchor="start">Agent(模型)</text>
17
+ <text x="78" y="148" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">读工具 schema → 选图 → 带 thread / resume 调用</text>
18
+ <rect x="780" y="96" width="400" height="76" rx="10" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
19
+ <text x="798" y="126" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="15" font-weight="600" fill="#0f172a" text-anchor="start">Session log(唯一真相)</text>
20
+ <text x="798" y="148" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">外层 langgraph_run + 子调用(Trajectory 可见,log-only)</text>
21
+ <path d="M300,172 L300,196" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
22
+ <rect x="280.6" y="167.0" width="38.8" height="16" rx="4" fill="#ffffff" opacity="0.92"/>
23
+ <text x="300.0" y="180.0" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="middle">工具调用</text>
24
+ <path d="M1000,172 L1000,196" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
25
+ <rect x="60" y="200" width="1120" height="104" rx="10" fill="#ffffff" stroke="#2563eb" stroke-width="1.4" stroke-dasharray="5 4"/>
26
+ <text x="78" y="224" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="14" font-weight="600" fill="#0f172a" text-anchor="start">DSH tools registry</text>
27
+ <text x="210" y="224" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">作用域 = 本 session 的 agent</text>
28
+ <rect x="84" y="240" width="330" height="46" rx="8" fill="#eff6ff" stroke="#2563eb" stroke-width="1.2"/>
29
+ <text x="98" y="261" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13" font-weight="600" fill="#0f172a" text-anchor="start">langgraph_run</text>
30
+ <text x="98" y="278" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">graph / input / thread / resume / maxSteps</text>
31
+ <rect x="444" y="240" width="330" height="46" rx="8" fill="#eff6ff" stroke="#2563eb" stroke-width="1.2"/>
32
+ <text x="458" y="261" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13" font-weight="600" fill="#0f172a" text-anchor="start">langgraph_threads</text>
33
+ <text x="458" y="278" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">action: list / state / delete</text>
34
+ <rect x="804" y="240" width="330" height="46" rx="8" fill="#f8fafc" stroke="#64748b" stroke-width="1.2"/>
35
+ <text x="818" y="261" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13" font-weight="600" fill="#0f172a" text-anchor="start">…… 会话里的其它工具</text>
36
+ <text x="818" y="278" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">glob / read / bash / write …</text>
37
+ <path d="M600,304 L600,332" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
38
+ <rect x="587.8" y="301.0" width="24.4" height="16" rx="4" fill="#ffffff" opacity="0.92"/>
39
+ <text x="600.0" y="314.0" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="middle">调用</text>
40
+ <rect x="60" y="336" width="1120" height="314" rx="10" fill="#ecfdf5" stroke="#059669" stroke-width="1.8"/>
41
+ <text x="78" y="362" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="14.5" font-weight="600" fill="#065f46" text-anchor="start">dsh-langgraph 插件(Host 进程内)</text>
42
+ <rect x="84" y="378" width="330" height="252" rx="10" fill="#ffffff" stroke="#059669" stroke-width="1.6"/>
43
+ <rect x="84" y="378" width="330" height="34" rx="10" fill="#059669"/>
44
+ <rect x="84" y="398" width="330" height="14" fill="#059669"/>
45
+ <text x="98" y="401" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#ffffff" text-anchor="start">图注册表 + helpers</text>
46
+ <text x="98" y="440" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">graphs/*.js → 图模块</text>
47
+ <text x="98" y="465" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start"> · id / description</text>
48
+ <text x="98" y="490" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start"> · input(JSON Schema)</text>
49
+ <text x="98" y="515" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start"> · build(api) → StateGraph</text>
50
+ <text x="98" y="565" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">helpers.callTool(name, args)</text>
51
+ <text x="98" y="590" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">helpers.callModel / chatModel()</text>
52
+ <text x="98" y="615" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">helpers.tools([...]) / createReactAgent()</text>
53
+ <rect x="444" y="378" width="356" height="252" rx="10" fill="#f5f3ff" stroke="#7c3aed" stroke-width="1.6"/>
54
+ <rect x="444" y="378" width="356" height="34" rx="10" fill="#7c3aed"/>
55
+ <rect x="444" y="398" width="356" height="14" fill="#7c3aed"/>
56
+ <text x="458" y="401" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#ffffff" text-anchor="start">LangGraph 运行时</text>
57
+ <text x="458" y="440" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">StateGraph → compile({ checkpointer })</text>
58
+ <text x="458" y="465" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">superstep 循环(recursionLimit)</text>
59
+ <text x="458" y="490" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">interrupt() → 暂停并返回 payload</text>
60
+ <text x="458" y="515" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">Command({ resume }) → 从原节点继续</text>
61
+ <text x="458" y="540" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">serde(JsonPlusSerializer)</text>
62
+ <text x="458" y="590" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#4c1d95" text-anchor="start" opacity="0.9">→ 差异化:可恢复 · 可中断 · 有结构</text>
63
+ <rect x="830" y="378" width="326" height="252" rx="10" fill="#ecfdf5" stroke="#059669" stroke-width="1.6"/>
64
+ <rect x="830" y="378" width="326" height="34" rx="10" fill="#059669"/>
65
+ <rect x="830" y="398" width="326" height="14" fill="#059669"/>
66
+ <text x="844" y="401" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#ffffff" text-anchor="start">DshCheckpointSaver</text>
67
+ <text x="1142" y="400" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="10.5" font-weight="500" fill="#ffffff" text-anchor="end" opacity="0.85">实现 LangGraph 契约</text>
68
+ <text x="844" y="440" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">extends BaseCheckpointSaver</text>
69
+ <text x="844" y="465" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">put / putWrites 写入</text>
70
+ <text x="844" y="490" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">getTuple / list 读取</text>
71
+ <text x="844" y="515" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">deleteThread 回收</text>
72
+ <text x="844" y="540" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">线程簿记(graph、session)</text>
73
+ <text x="844" y="590" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="400" fill="#0f172a" text-anchor="start">key = base64url(复合键)</text>
74
+ <path d="M414,504 L444,504" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
75
+ <path d="M800,490 L830,490" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)" marker-start="url(#dot)"/>
76
+ <rect x="84" y="690" width="330" height="86" rx="10" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
77
+ <text x="102" y="718" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#0f172a" text-anchor="start">ctx.tools.execute</text>
78
+ <text x="102" y="740" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">在调用方 agent 作用域执行真实工具</text>
79
+ <text x="102" y="758" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">同沙箱、同审批、同会话</text>
80
+ <rect x="444" y="690" width="356" height="86" rx="10" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
81
+ <text x="462" y="718" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#0f172a" text-anchor="start">ctx.llm.stream</text>
82
+ <text x="462" y="740" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">部署自己的模型路由</text>
83
+ <text x="462" y="758" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">provider / model / reasoningEffort</text>
84
+ <rect x="830" y="690" width="326" height="86" rx="10" fill="#eff6ff" stroke="#2563eb" stroke-width="1.5"/>
85
+ <text x="848" y="718" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="13.5" font-weight="600" fill="#0f172a" text-anchor="start">storageDomain</text>
86
+ <text x="848" y="740" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">→ storage-json(per-record)</text>
87
+ <text x="848" y="758" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#64748b" text-anchor="start">zod 校验 · 同步读 · 写即持久</text>
88
+ <path d="M230,630 L230,690" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
89
+ <rect x="196.2" y="648" width="67.6" height="16" rx="4" fill="#ffffff" opacity="0.92"/>
90
+ <text x="230" y="661" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="middle">callTool</text>
91
+ <path d="M360,630 L360,660 L600,660 L600,690" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
92
+ <rect x="432.6" y="648" width="74.8" height="16" rx="4" fill="#ffffff" opacity="0.92"/>
93
+ <text x="470" y="661" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="middle">callModel</text>
94
+ <path d="M990,630 L990,690" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
95
+ <rect x="952.6" y="648" width="74.8" height="16" rx="4" fill="#ffffff" opacity="0.92"/>
96
+ <text x="990" y="661" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="middle">putRecord</text>
97
+ <rect x="84" y="806" width="330" height="68" rx="8" fill="#f8fafc" stroke="#94a3b8" stroke-width="1.1" stroke-dasharray="4 3"/>
98
+ <text x="100" y="832" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="600" fill="#0f172a" text-anchor="start">沙箱 · 审批策略</text>
99
+ <text x="100" y="852" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">拦截仍生效;执行记入 Trajectory 子调用</text>
100
+ <rect x="444" y="806" width="356" height="68" rx="8" fill="#f8fafc" stroke="#94a3b8" stroke-width="1.1" stroke-dasharray="4 3"/>
101
+ <text x="460" y="832" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="600" fill="#0f172a" text-anchor="start">provider · model</text>
102
+ <text x="460" y="852" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">可在 config.model 覆盖</text>
103
+ <rect x="830" y="806" width="326" height="68" rx="8" fill="#f8fafc" stroke="#94a3b8" stroke-width="1.1" stroke-dasharray="4 3"/>
104
+ <text x="846" y="832" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="12" font-weight="600" fill="#0f172a" text-anchor="start">~/.dsh/storages/langgraph_checkpoints/</text>
105
+ <text x="846" y="852" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11" font-weight="400" fill="#64748b" text-anchor="start">checkpoints/ writes/ threads/</text>
106
+ <path d="M249,776 L249,806" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
107
+ <path d="M622,776 L622,806" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
108
+ <path d="M993,776 L993,806" fill="none" stroke="#94a3b8" stroke-width="1.6" stroke-linecap="round" marker-end="url(#arrow)"/>
109
+ <rect x="60" y="906" width="550" height="68" rx="10" fill="#fffbeb" stroke="#d97706" stroke-width="1.3" stroke-dasharray="5 4"/>
110
+ <text x="78" y="930" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12.5" font-weight="600" fill="#92400e" text-anchor="start">没有 storageDomain 时</text>
111
+ <text x="78" y="950" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#78350f" text-anchor="start">退回 LangGraph 自带 MemorySaver(进程内),重启即失</text>
112
+ <text x="78" y="966" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11.5" font-weight="400" fill="#78350f" text-anchor="start">工具列表会明说 Threads are not durable</text>
113
+ <rect x="630" y="906" width="550" height="68" rx="10" fill="#fffbeb" stroke="#d97706" stroke-width="1.5" stroke-dasharray="5 4"/>
114
+ <text x="648" y="930" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="12.5" font-weight="600" fill="#92400e" text-anchor="start">节点内部动作不进会话日志</text>
115
+ <text x="648" y="950" font-family="-apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Arial, sans-serif" font-size="11.5" font-weight="400" fill="#78350f" text-anchor="start">记录 tool/call 需要 turn/step,只有 agent loop 会传</text>
116
+ <text x="648" y="966" font-family="'SF Mono', SFMono-Regular, Menlo, Consolas, monospace" font-size="11.5" font-weight="400" fill="#78350f" text-anchor="start">→ 所以可恢复状态另存;Chat 里每次运行只有一行</text>
117
+ </svg>
@@ -0,0 +1,157 @@
1
+ /**
2
+ * A LangChain chat model backed by the DSH `llm` service.
3
+ *
4
+ * It lets a LangGraph node use the deployment's own model route, and it supports the
5
+ * tool-calling protocol LangGraph's prebuilt agents expect: `bindTools` records the
6
+ * LangChain tools, `_generate` passes their JSON schemas to DSH, and tool calls come
7
+ * back as an `AIMessage` with `tool_calls`.
8
+ * @module dsh-langgraph/chat-model
9
+ */
10
+
11
+ import { BaseChatModel } from '@langchain/core/language_models/chat_models'
12
+ import { AIMessage, HumanMessage, SystemMessage, ToolMessage } from '@langchain/core/messages'
13
+
14
+ /** Text of one LangChain message's content blocks. */
15
+ function textOf(message) {
16
+ const content = message?.content
17
+ if (typeof content === 'string') return content
18
+ if (!Array.isArray(content)) return ''
19
+ return content
20
+ .map(part => (typeof part === 'string' ? part : part?.type === 'text' ? part.text : ''))
21
+ .filter(Boolean)
22
+ .join('\n')
23
+ }
24
+
25
+ /**
26
+ * Convert one LangChain message into a DSH request message.
27
+ * @param {object} message - LangChain BaseMessage.
28
+ * @param {{ provider: string, model: string }} route - route used for assistant provenance.
29
+ * @returns {{ role: string, content: object[], source?: object, toolCallId?: string } | undefined}
30
+ */
31
+ function toRequestMessage(message, route) {
32
+ const text = textOf(message)
33
+ const type = message?.getType?.() ?? message?._getType?.()
34
+ if (type === 'human') return { role: 'user', content: [{ type: 'text', text }] }
35
+ if (type === 'ai') {
36
+ const content = [{ type: 'text', text }]
37
+ for (const call of message.tool_calls ?? []) {
38
+ content.push({ type: 'tool-call', id: call.id ?? `call_${content.length}`, name: call.name, arguments: JSON.stringify(call.args ?? {}) })
39
+ }
40
+ return { role: 'assistant', content, source: { kind: 'model', provider: route.provider, model: route.model } }
41
+ }
42
+ if (type === 'tool') {
43
+ const callId = message.tool_call_id ?? message.additional_kwargs?.tool_call_id
44
+ return { role: 'tool', content: [{ type: 'text', text }], source: { kind: 'tool', callId }, toolCallId: callId }
45
+ }
46
+ return undefined
47
+ }
48
+
49
+ /** The LangChain tool schema a DSH tool exposes to the model. */
50
+ function toolSchema(tool) {
51
+ return {
52
+ name: tool.name,
53
+ description: tool.description ?? '',
54
+ parameters: tool.dshParameters ?? { type: 'object', properties: {}, additionalProperties: false },
55
+ }
56
+ }
57
+
58
+ /** A chat model over one DSH route, bound to one run's cancellation and session. */
59
+ export class DshChatModel extends BaseChatModel {
60
+ /**
61
+ * @param {{ stream: Function, route: { provider: string, model: string, reasoningEffort?: string }, sessionId?: string, signal?: AbortSignal, tools?: object[] }} fields
62
+ */
63
+ constructor(fields) {
64
+ super({})
65
+ this.streamFn = fields.stream
66
+ this.route = fields.route
67
+ this.sessionId = fields.sessionId
68
+ this.signal = fields.signal
69
+ this.tools = fields.tools ?? []
70
+ }
71
+
72
+ /** @returns {string} provider family name shown by LangChain. */
73
+ _llmType() {
74
+ return 'dsh'
75
+ }
76
+
77
+ /**
78
+ * Record tools for the next call; LangGraph's prebuilt agents call this.
79
+ * @param {object[]} tools - LangChain tools.
80
+ * @returns {DshChatModel} a model bound to these tools.
81
+ */
82
+ bindTools(tools) {
83
+ return new DshChatModel({
84
+ stream: this.streamFn,
85
+ route: this.route,
86
+ sessionId: this.sessionId,
87
+ signal: this.signal,
88
+ tools: [...this.tools, ...tools],
89
+ })
90
+ }
91
+
92
+ /**
93
+ * One generation against the DSH model route.
94
+ * @param {object[]} messages - LangChain messages.
95
+ * @returns {Promise<{ generations: object[] }>} LangChain chat result.
96
+ */
97
+ async _generate(messages) {
98
+ const system = messages
99
+ .filter(message => (message?.getType?.() ?? message?._getType?.()) === 'system')
100
+ .map(textOf)
101
+ .filter(Boolean)
102
+ .join('\n\n')
103
+ const request = messages
104
+ .filter(message => (message?.getType?.() ?? message?._getType?.()) !== 'system')
105
+ .map(message => toRequestMessage(message, this.route))
106
+ .filter(Boolean)
107
+ let text = ''
108
+ let usage
109
+ const calls = new Map()
110
+ let finish
111
+ for await (const chunk of this.streamFn({
112
+ provider: this.route.provider,
113
+ model: this.route.model,
114
+ ...(this.route.reasoningEffort === undefined ? {} : { reasoningEffort: this.route.reasoningEffort }),
115
+ messages: request,
116
+ ...(system === '' ? {} : { system }),
117
+ ...(this.tools.length === 0 ? {} : { tools: this.tools.map(toolSchema) }),
118
+ ...(this.sessionId === undefined ? {} : { sessionId: this.sessionId }),
119
+ signal: this.signal,
120
+ })) {
121
+ if (chunk.type === 'text-delta') text += chunk.text
122
+ else if (chunk.type === 'usage') usage = chunk.usage
123
+ else if (chunk.type === 'tool-call-delta') {
124
+ const entry = calls.get(chunk.id) ?? { id: chunk.id, name: '', arguments: '' }
125
+ if (chunk.name !== undefined) entry.name = chunk.name
126
+ entry.arguments += chunk.argumentsDelta
127
+ calls.set(chunk.id, entry)
128
+ } else if (chunk.type === 'block-end' && chunk.block?.type === 'tool-call') {
129
+ const block = chunk.block
130
+ const previous = calls.get(block.id)
131
+ calls.set(block.id, {
132
+ id: block.id,
133
+ name: block.name ?? previous?.name ?? '',
134
+ arguments: block.arguments ?? previous?.arguments ?? '',
135
+ })
136
+ } else if (chunk.type === 'finish') finish = chunk.reason
137
+ }
138
+ if (finish?.kind === 'error') throw new Error(`model call failed: ${finish.failure.message}`)
139
+ const toolCalls = [...calls.values()].filter(entry => typeof entry.name === 'string' && entry.name !== '').map(entry => {
140
+ let args = {}
141
+ try {
142
+ args = entry.arguments === '' ? {} : JSON.parse(entry.arguments)
143
+ } catch {
144
+ args = {}
145
+ }
146
+ return { id: entry.id, name: entry.name, args }
147
+ })
148
+ const message = new AIMessage({ content: text, tool_calls: toolCalls })
149
+ return {
150
+ generations: [{ text, message }],
151
+ ...(usage === undefined ? {} : { llmOutput: { tokenUsage: usage } }),
152
+ }
153
+ }
154
+ }
155
+
156
+ /** Re-exported so graph modules can build message arrays without their own dependency. */
157
+ export { AIMessage, HumanMessage, SystemMessage, ToolMessage }