@langchain/langgraph-sdk 0.1.10 → 1.0.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 (234) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  3. package/dist/auth/error.cjs +76 -86
  4. package/dist/auth/error.cjs.map +1 -0
  5. package/dist/auth/error.d.cts +13 -0
  6. package/dist/auth/error.d.cts.map +1 -0
  7. package/dist/auth/error.d.ts +12 -8
  8. package/dist/auth/error.d.ts.map +1 -0
  9. package/dist/auth/error.js +76 -83
  10. package/dist/auth/error.js.map +1 -0
  11. package/dist/auth/index.cjs +36 -44
  12. package/dist/auth/index.cjs.map +1 -0
  13. package/dist/auth/index.d.cts +31 -0
  14. package/dist/auth/index.d.cts.map +1 -0
  15. package/dist/auth/index.d.ts +20 -15
  16. package/dist/auth/index.d.ts.map +1 -0
  17. package/dist/auth/index.js +35 -39
  18. package/dist/auth/index.js.map +1 -0
  19. package/dist/auth/types.d.cts +294 -0
  20. package/dist/auth/types.d.cts.map +1 -0
  21. package/dist/auth/types.d.ts +162 -165
  22. package/dist/auth/types.d.ts.map +1 -0
  23. package/dist/client.cjs +1108 -1294
  24. package/dist/client.cjs.map +1 -0
  25. package/dist/client.d.cts +688 -0
  26. package/dist/client.d.cts.map +1 -0
  27. package/dist/client.d.ts +647 -639
  28. package/dist/client.d.ts.map +1 -0
  29. package/dist/client.js +1099 -1281
  30. package/dist/client.js.map +1 -0
  31. package/dist/index.cjs +6 -8
  32. package/dist/index.d.cts +7 -0
  33. package/dist/index.d.ts +7 -7
  34. package/dist/index.js +4 -2
  35. package/dist/logging/index.cjs +30 -31
  36. package/dist/logging/index.cjs.map +1 -0
  37. package/dist/logging/index.d.cts +47 -0
  38. package/dist/logging/index.d.cts.map +1 -0
  39. package/dist/logging/index.d.ts +20 -16
  40. package/dist/logging/index.d.ts.map +1 -0
  41. package/dist/logging/index.js +31 -29
  42. package/dist/logging/index.js.map +1 -0
  43. package/dist/react/index.cjs +5 -7
  44. package/dist/react/index.d.cts +4 -0
  45. package/dist/react/index.d.ts +4 -3
  46. package/dist/react/index.js +4 -2
  47. package/dist/react/stream.cjs +14 -15
  48. package/dist/react/stream.cjs.map +1 -0
  49. package/dist/react/stream.custom.cjs +126 -145
  50. package/dist/react/stream.custom.cjs.map +1 -0
  51. package/dist/react/stream.custom.d.cts +39 -0
  52. package/dist/react/stream.custom.d.cts.map +1 -0
  53. package/dist/react/stream.custom.d.ts +35 -37
  54. package/dist/react/stream.custom.d.ts.map +1 -0
  55. package/dist/react/stream.custom.js +122 -137
  56. package/dist/react/stream.custom.js.map +1 -0
  57. package/dist/react/stream.d.cts +73 -0
  58. package/dist/react/stream.d.cts.map +1 -0
  59. package/dist/react/stream.d.ts +21 -15
  60. package/dist/react/stream.d.ts.map +1 -0
  61. package/dist/react/stream.js +12 -11
  62. package/dist/react/stream.js.map +1 -0
  63. package/dist/react/stream.lgp.cjs +438 -498
  64. package/dist/react/stream.lgp.cjs.map +1 -0
  65. package/dist/react/stream.lgp.js +433 -490
  66. package/dist/react/stream.lgp.js.map +1 -0
  67. package/dist/react/thread.cjs +18 -15
  68. package/dist/react/thread.cjs.map +1 -0
  69. package/dist/react/thread.js +19 -13
  70. package/dist/react/thread.js.map +1 -0
  71. package/dist/react/types.d.cts +355 -0
  72. package/dist/react/types.d.cts.map +1 -0
  73. package/dist/react/types.d.ts +333 -329
  74. package/dist/react/types.d.ts.map +1 -0
  75. package/dist/react-ui/client.cjs +123 -184
  76. package/dist/react-ui/client.cjs.map +1 -0
  77. package/dist/react-ui/client.d.cts +90 -0
  78. package/dist/react-ui/client.d.cts.map +1 -0
  79. package/dist/react-ui/client.d.ts +67 -51
  80. package/dist/react-ui/client.d.ts.map +1 -0
  81. package/dist/react-ui/client.js +118 -143
  82. package/dist/react-ui/client.js.map +1 -0
  83. package/dist/react-ui/index.cjs +14 -13
  84. package/dist/react-ui/index.cjs.map +1 -0
  85. package/dist/react-ui/index.d.cts +3 -0
  86. package/dist/react-ui/index.d.ts +3 -2
  87. package/dist/react-ui/index.js +8 -3
  88. package/dist/react-ui/index.js.map +1 -0
  89. package/dist/react-ui/server/index.cjs +5 -7
  90. package/dist/react-ui/server/index.d.cts +3 -0
  91. package/dist/react-ui/server/index.d.ts +3 -2
  92. package/dist/react-ui/server/index.js +4 -2
  93. package/dist/react-ui/server/server.cjs +53 -44
  94. package/dist/react-ui/server/server.cjs.map +1 -0
  95. package/dist/react-ui/server/server.d.cts +55 -0
  96. package/dist/react-ui/server/server.d.cts.map +1 -0
  97. package/dist/react-ui/server/server.d.ts +43 -39
  98. package/dist/react-ui/server/server.d.ts.map +1 -0
  99. package/dist/react-ui/server/server.js +54 -42
  100. package/dist/react-ui/server/server.js.map +1 -0
  101. package/dist/react-ui/types.cjs +32 -36
  102. package/dist/react-ui/types.cjs.map +1 -0
  103. package/dist/react-ui/types.d.cts +25 -0
  104. package/dist/react-ui/types.d.cts.map +1 -0
  105. package/dist/react-ui/types.d.ts +23 -19
  106. package/dist/react-ui/types.d.ts.map +1 -0
  107. package/dist/react-ui/types.js +32 -34
  108. package/dist/react-ui/types.js.map +1 -0
  109. package/dist/schema.d.cts +277 -0
  110. package/dist/schema.d.cts.map +1 -0
  111. package/dist/schema.d.ts +249 -242
  112. package/dist/schema.d.ts.map +1 -0
  113. package/dist/singletons/fetch.cjs +16 -20
  114. package/dist/singletons/fetch.cjs.map +1 -0
  115. package/dist/singletons/fetch.d.cts +14 -0
  116. package/dist/singletons/fetch.d.cts.map +1 -0
  117. package/dist/singletons/fetch.d.ts +5 -2
  118. package/dist/singletons/fetch.d.ts.map +1 -0
  119. package/dist/singletons/fetch.js +16 -17
  120. package/dist/singletons/fetch.js.map +1 -0
  121. package/dist/types.d.cts +187 -0
  122. package/dist/types.d.cts.map +1 -0
  123. package/dist/types.d.ts +178 -173
  124. package/dist/types.d.ts.map +1 -0
  125. package/dist/types.messages.d.cts +91 -0
  126. package/dist/types.messages.d.cts.map +1 -0
  127. package/dist/types.messages.d.ts +71 -66
  128. package/dist/types.messages.d.ts.map +1 -0
  129. package/dist/types.stream.d.cts +235 -0
  130. package/dist/types.stream.d.cts.map +1 -0
  131. package/dist/types.stream.d.ts +149 -142
  132. package/dist/types.stream.d.ts.map +1 -0
  133. package/dist/ui/branching.cjs +145 -141
  134. package/dist/ui/branching.cjs.map +1 -0
  135. package/dist/ui/branching.d.cts +22 -0
  136. package/dist/ui/branching.d.cts.map +1 -0
  137. package/dist/ui/branching.d.ts +17 -30
  138. package/dist/ui/branching.d.ts.map +1 -0
  139. package/dist/ui/branching.js +147 -139
  140. package/dist/ui/branching.js.map +1 -0
  141. package/dist/ui/errors.cjs +14 -12
  142. package/dist/ui/errors.cjs.map +1 -0
  143. package/dist/ui/errors.js +14 -9
  144. package/dist/ui/errors.js.map +1 -0
  145. package/dist/ui/manager.cjs +156 -232
  146. package/dist/ui/manager.cjs.map +1 -0
  147. package/dist/ui/manager.js +156 -228
  148. package/dist/ui/manager.js.map +1 -0
  149. package/dist/ui/messages.cjs +55 -69
  150. package/dist/ui/messages.cjs.map +1 -0
  151. package/dist/ui/messages.js +55 -66
  152. package/dist/ui/messages.js.map +1 -0
  153. package/dist/ui/utils.cjs +10 -10
  154. package/dist/ui/utils.cjs.map +1 -0
  155. package/dist/ui/utils.js +10 -8
  156. package/dist/ui/utils.js.map +1 -0
  157. package/dist/utils/async_caller.cjs +103 -185
  158. package/dist/utils/async_caller.cjs.map +1 -0
  159. package/dist/utils/async_caller.d.cts +53 -0
  160. package/dist/utils/async_caller.d.cts.map +1 -0
  161. package/dist/utils/async_caller.d.ts +36 -31
  162. package/dist/utils/async_caller.d.ts.map +1 -0
  163. package/dist/utils/async_caller.js +101 -177
  164. package/dist/utils/async_caller.js.map +1 -0
  165. package/dist/utils/env.cjs +11 -13
  166. package/dist/utils/env.cjs.map +1 -0
  167. package/dist/utils/env.js +11 -11
  168. package/dist/utils/env.js.map +1 -0
  169. package/dist/utils/signals.cjs +18 -19
  170. package/dist/utils/signals.cjs.map +1 -0
  171. package/dist/utils/signals.js +18 -17
  172. package/dist/utils/signals.js.map +1 -0
  173. package/dist/utils/sse.cjs +110 -140
  174. package/dist/utils/sse.cjs.map +1 -0
  175. package/dist/utils/sse.js +110 -138
  176. package/dist/utils/sse.js.map +1 -0
  177. package/dist/utils/stream.cjs +90 -114
  178. package/dist/utils/stream.cjs.map +1 -0
  179. package/dist/utils/stream.js +90 -111
  180. package/dist/utils/stream.js.map +1 -0
  181. package/package.json +72 -80
  182. package/auth.cjs +0 -1
  183. package/auth.d.cts +0 -1
  184. package/auth.d.ts +0 -1
  185. package/auth.js +0 -1
  186. package/client.cjs +0 -1
  187. package/client.d.cts +0 -1
  188. package/client.d.ts +0 -1
  189. package/client.js +0 -1
  190. package/dist/auth/types.cjs +0 -2
  191. package/dist/auth/types.js +0 -1
  192. package/dist/react/stream.lgp.d.ts +0 -7
  193. package/dist/react/thread.d.ts +0 -4
  194. package/dist/react/types.cjs +0 -2
  195. package/dist/react/types.js +0 -1
  196. package/dist/schema.cjs +0 -2
  197. package/dist/schema.js +0 -1
  198. package/dist/types.cjs +0 -2
  199. package/dist/types.js +0 -1
  200. package/dist/types.messages.cjs +0 -2
  201. package/dist/types.messages.js +0 -1
  202. package/dist/types.stream.cjs +0 -2
  203. package/dist/types.stream.js +0 -1
  204. package/dist/ui/branching.test.cjs +0 -370
  205. package/dist/ui/branching.test.d.ts +0 -1
  206. package/dist/ui/branching.test.js +0 -368
  207. package/dist/ui/errors.d.ts +0 -12
  208. package/dist/ui/manager.d.ts +0 -87
  209. package/dist/ui/messages.d.ts +0 -18
  210. package/dist/ui/utils.d.ts +0 -2
  211. package/dist/utils/env.d.ts +0 -1
  212. package/dist/utils/signals.d.ts +0 -1
  213. package/dist/utils/sse.d.ts +0 -8
  214. package/dist/utils/stream.d.ts +0 -13
  215. package/index.cjs +0 -1
  216. package/index.d.cts +0 -1
  217. package/index.d.ts +0 -1
  218. package/index.js +0 -1
  219. package/logging.cjs +0 -1
  220. package/logging.d.cts +0 -1
  221. package/logging.d.ts +0 -1
  222. package/logging.js +0 -1
  223. package/react-ui/server.cjs +0 -1
  224. package/react-ui/server.d.cts +0 -1
  225. package/react-ui/server.d.ts +0 -1
  226. package/react-ui/server.js +0 -1
  227. package/react-ui.cjs +0 -1
  228. package/react-ui.d.cts +0 -1
  229. package/react-ui.d.ts +0 -1
  230. package/react-ui.js +0 -1
  231. package/react.cjs +0 -1
  232. package/react.d.cts +0 -1
  233. package/react.d.ts +0 -1
  234. package/react.js +0 -1
@@ -1,144 +1,152 @@
1
- export function getBranchSequence(history) {
2
- const nodeIds = new Set();
3
- const childrenMap = {};
4
- // Short circuit if there's only a singular one state
5
- if (history.length <= 1) {
6
- return {
7
- rootSequence: {
8
- type: "sequence",
9
- items: history.map((value) => ({ type: "node", value, path: [] })),
10
- },
11
- paths: [],
12
- };
13
- }
14
- // First pass - collect nodes for each checkpoint
15
- history.forEach((state) => {
16
- const checkpointId = state.parent_checkpoint?.checkpoint_id ?? "$";
17
- childrenMap[checkpointId] ??= [];
18
- childrenMap[checkpointId].push(state);
19
- if (state.checkpoint?.checkpoint_id != null) {
20
- nodeIds.add(state.checkpoint.checkpoint_id);
21
- }
22
- });
23
- // If dealing with partial history, take the branch
24
- // with the latest checkpoint and mark it as the root.
25
- const maxId = (...ids) => ids
26
- .filter((i) => i != null)
27
- .sort((a, b) => a.localeCompare(b))
28
- .at(-1);
29
- const lastOrphanedNode = childrenMap.$ == null
30
- ? Object.keys(childrenMap)
31
- .filter((parentId) => !nodeIds.has(parentId))
32
- .map((parentId) => {
33
- const queue = [parentId];
34
- const seen = new Set();
35
- let lastId = parentId;
36
- while (queue.length > 0) {
37
- const current = queue.shift();
38
- if (seen.has(current))
39
- continue;
40
- seen.add(current);
41
- const children = (childrenMap[current] ?? []).flatMap((i) => i.checkpoint?.checkpoint_id ?? []);
42
- lastId = maxId(lastId, ...children);
43
- queue.push(...children);
44
- }
45
- return { parentId, lastId };
46
- })
47
- .sort((a, b) => a.lastId.localeCompare(b.lastId))
48
- .at(-1)?.parentId
49
- : undefined;
50
- if (lastOrphanedNode != null)
51
- childrenMap.$ = childrenMap[lastOrphanedNode];
52
- const rootSequence = { type: "sequence", items: [] };
53
- const queue = [{ id: "$", sequence: rootSequence, path: [] }];
54
- const paths = [];
55
- const visited = new Set();
56
- while (queue.length > 0) {
57
- const task = queue.shift();
58
- if (visited.has(task.id))
59
- continue;
60
- visited.add(task.id);
61
- const children = childrenMap[task.id];
62
- if (children == null || children.length === 0)
63
- continue;
64
- // If we've encountered a fork (2+ children), push the fork
65
- // to the sequence and add a new sequence for each child
66
- let fork;
67
- if (children.length > 1) {
68
- fork = { type: "fork", items: [] };
69
- task.sequence.items.push(fork);
70
- }
71
- for (const value of children) {
72
- const id = value.checkpoint?.checkpoint_id;
73
- if (id == null)
74
- continue;
75
- let { sequence } = task;
76
- let { path } = task;
77
- if (fork != null) {
78
- sequence = { type: "sequence", items: [] };
79
- fork.items.unshift(sequence);
80
- path = path.slice();
81
- path.push(id);
82
- paths.push(path);
83
- }
84
- sequence.items.push({ type: "node", value, path });
85
- queue.push({ id, sequence, path });
86
- }
87
- }
88
- return { rootSequence, paths };
1
+ //#region src/ui/branching.ts
2
+ function getBranchSequence(history) {
3
+ const nodeIds = /* @__PURE__ */ new Set();
4
+ const childrenMap = {};
5
+ if (history.length <= 1) return {
6
+ rootSequence: {
7
+ type: "sequence",
8
+ items: history.map((value) => ({
9
+ type: "node",
10
+ value,
11
+ path: []
12
+ }))
13
+ },
14
+ paths: []
15
+ };
16
+ history.forEach((state) => {
17
+ const checkpointId = state.parent_checkpoint?.checkpoint_id ?? "$";
18
+ childrenMap[checkpointId] ??= [];
19
+ childrenMap[checkpointId].push(state);
20
+ if (state.checkpoint?.checkpoint_id != null) nodeIds.add(state.checkpoint.checkpoint_id);
21
+ });
22
+ const maxId = (...ids) => ids.filter((i) => i != null).sort((a, b) => a.localeCompare(b)).at(-1);
23
+ const lastOrphanedNode = childrenMap.$ == null ? Object.keys(childrenMap).filter((parentId) => !nodeIds.has(parentId)).map((parentId) => {
24
+ const queue$1 = [parentId];
25
+ const seen = /* @__PURE__ */ new Set();
26
+ let lastId = parentId;
27
+ while (queue$1.length > 0) {
28
+ const current = queue$1.shift();
29
+ if (seen.has(current)) continue;
30
+ seen.add(current);
31
+ const children = (childrenMap[current] ?? []).flatMap((i) => i.checkpoint?.checkpoint_id ?? []);
32
+ lastId = maxId(lastId, ...children);
33
+ queue$1.push(...children);
34
+ }
35
+ return {
36
+ parentId,
37
+ lastId
38
+ };
39
+ }).sort((a, b) => a.lastId.localeCompare(b.lastId)).at(-1)?.parentId : void 0;
40
+ if (lastOrphanedNode != null) childrenMap.$ = childrenMap[lastOrphanedNode];
41
+ const rootSequence = {
42
+ type: "sequence",
43
+ items: []
44
+ };
45
+ const queue = [{
46
+ id: "$",
47
+ sequence: rootSequence,
48
+ path: []
49
+ }];
50
+ const paths = [];
51
+ const visited = /* @__PURE__ */ new Set();
52
+ while (queue.length > 0) {
53
+ const task = queue.shift();
54
+ if (visited.has(task.id)) continue;
55
+ visited.add(task.id);
56
+ const children = childrenMap[task.id];
57
+ if (children == null || children.length === 0) continue;
58
+ let fork;
59
+ if (children.length > 1) {
60
+ fork = {
61
+ type: "fork",
62
+ items: []
63
+ };
64
+ task.sequence.items.push(fork);
65
+ }
66
+ for (const value of children) {
67
+ const id = value.checkpoint?.checkpoint_id;
68
+ if (id == null) continue;
69
+ let { sequence } = task;
70
+ let { path } = task;
71
+ if (fork != null) {
72
+ sequence = {
73
+ type: "sequence",
74
+ items: []
75
+ };
76
+ fork.items.unshift(sequence);
77
+ path = path.slice();
78
+ path.push(id);
79
+ paths.push(path);
80
+ }
81
+ sequence.items.push({
82
+ type: "node",
83
+ value,
84
+ path
85
+ });
86
+ queue.push({
87
+ id,
88
+ sequence,
89
+ path
90
+ });
91
+ }
92
+ }
93
+ return {
94
+ rootSequence,
95
+ paths
96
+ };
89
97
  }
90
98
  const PATH_SEP = ">";
91
99
  const ROOT_ID = "$";
92
- // Get flat view
93
- export function getBranchView(sequence, paths, branch) {
94
- const path = branch.split(PATH_SEP);
95
- const pathMap = {};
96
- for (const path of paths) {
97
- const parent = path.at(-2) ?? ROOT_ID;
98
- pathMap[parent] ??= [];
99
- pathMap[parent].unshift(path);
100
- }
101
- const history = [];
102
- const branchByCheckpoint = {};
103
- const forkStack = path.slice();
104
- const queue = [...sequence.items];
105
- while (queue.length > 0) {
106
- const item = queue.shift();
107
- if (item.type === "node") {
108
- history.push(item.value);
109
- const checkpointId = item.value.checkpoint?.checkpoint_id;
110
- if (checkpointId == null)
111
- continue;
112
- branchByCheckpoint[checkpointId] = {
113
- branch: item.path.join(PATH_SEP),
114
- branchOptions: (item.path.length > 0
115
- ? pathMap[item.path.at(-2) ?? ROOT_ID] ?? []
116
- : []).map((p) => p.join(PATH_SEP)),
117
- };
118
- }
119
- if (item.type === "fork") {
120
- const forkId = forkStack.shift();
121
- const index = forkId != null
122
- ? item.items.findIndex((value) => {
123
- const firstItem = value.items.at(0);
124
- if (!firstItem || firstItem.type !== "node")
125
- return false;
126
- return firstItem.value.checkpoint?.checkpoint_id === forkId;
127
- })
128
- : -1;
129
- const nextItems = item.items.at(index)?.items ?? [];
130
- queue.push(...nextItems);
131
- }
132
- }
133
- return { history, branchByCheckpoint };
100
+ function getBranchView(sequence, paths, branch) {
101
+ const path = branch.split(PATH_SEP);
102
+ const pathMap = {};
103
+ for (const path$1 of paths) {
104
+ const parent = path$1.at(-2) ?? ROOT_ID;
105
+ pathMap[parent] ??= [];
106
+ pathMap[parent].unshift(path$1);
107
+ }
108
+ const history = [];
109
+ const branchByCheckpoint = {};
110
+ const forkStack = path.slice();
111
+ const queue = [...sequence.items];
112
+ while (queue.length > 0) {
113
+ const item = queue.shift();
114
+ if (item.type === "node") {
115
+ history.push(item.value);
116
+ const checkpointId = item.value.checkpoint?.checkpoint_id;
117
+ if (checkpointId == null) continue;
118
+ branchByCheckpoint[checkpointId] = {
119
+ branch: item.path.join(PATH_SEP),
120
+ branchOptions: (item.path.length > 0 ? pathMap[item.path.at(-2) ?? ROOT_ID] ?? [] : []).map((p) => p.join(PATH_SEP))
121
+ };
122
+ }
123
+ if (item.type === "fork") {
124
+ const forkId = forkStack.shift();
125
+ const index = forkId != null ? item.items.findIndex((value) => {
126
+ const firstItem = value.items.at(0);
127
+ if (!firstItem || firstItem.type !== "node") return false;
128
+ return firstItem.value.checkpoint?.checkpoint_id === forkId;
129
+ }) : -1;
130
+ const nextItems = item.items.at(index)?.items ?? [];
131
+ queue.push(...nextItems);
132
+ }
133
+ }
134
+ return {
135
+ history,
136
+ branchByCheckpoint
137
+ };
134
138
  }
135
- export function getBranchContext(branch, history) {
136
- const { rootSequence: branchTree, paths } = getBranchSequence(history ?? []);
137
- const { history: flatHistory, branchByCheckpoint } = getBranchView(branchTree, paths, branch);
138
- return {
139
- branchTree,
140
- flatHistory,
141
- branchByCheckpoint,
142
- threadHead: flatHistory.at(-1),
143
- };
139
+ function getBranchContext(branch, history) {
140
+ const { rootSequence: branchTree, paths } = getBranchSequence(history ?? []);
141
+ const { history: flatHistory, branchByCheckpoint } = getBranchView(branchTree, paths, branch);
142
+ return {
143
+ branchTree,
144
+ flatHistory,
145
+ branchByCheckpoint,
146
+ threadHead: flatHistory.at(-1)
147
+ };
144
148
  }
149
+
150
+ //#endregion
151
+ export { getBranchContext };
152
+ //# sourceMappingURL=branching.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.js","names":["childrenMap: Record<string, ThreadState<StateType>[]>","queue: string[]","queue","rootSequence: Sequence","queue: Task[]","paths: string[][]","fork: Fork | undefined","pathMap: Record<string, string[][]>","path","history: ThreadState<StateType>[]","branchByCheckpoint: Record<\n string,\n { branch: string | undefined; branchOptions: string[] | undefined }\n >","queue: (Node<StateType> | Fork<StateType>)[]"],"sources":["../../src/ui/branching.ts"],"sourcesContent":["import type { ThreadState } from \"../schema.js\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface Node<StateType = any> {\n type: \"node\";\n value: ThreadState<StateType>;\n path: string[];\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface Fork<StateType = any> {\n type: \"fork\";\n items: Array<Sequence<StateType>>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface Sequence<StateType = any> {\n type: \"sequence\";\n items: Array<Node<StateType> | Fork<StateType>>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface ValidFork<StateType = any> {\n type: \"fork\";\n items: Array<ValidSequence<StateType>>;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface ValidSequence<StateType = any> {\n type: \"sequence\";\n items: [Node<StateType>, ...(Node<StateType> | ValidFork<StateType>)[]];\n}\n\nexport function getBranchSequence<StateType extends Record<string, unknown>>(\n history: ThreadState<StateType>[]\n) {\n const nodeIds = new Set<string>();\n const childrenMap: Record<string, ThreadState<StateType>[]> = {};\n\n // Short circuit if there's only a singular one state\n if (history.length <= 1) {\n return {\n rootSequence: {\n type: \"sequence\",\n items: history.map((value) => ({ type: \"node\", value, path: [] })),\n } satisfies Sequence<StateType>,\n paths: [],\n };\n }\n\n // First pass - collect nodes for each checkpoint\n history.forEach((state) => {\n const checkpointId = state.parent_checkpoint?.checkpoint_id ?? \"$\";\n childrenMap[checkpointId] ??= [];\n childrenMap[checkpointId].push(state);\n\n if (state.checkpoint?.checkpoint_id != null) {\n nodeIds.add(state.checkpoint.checkpoint_id);\n }\n });\n\n // If dealing with partial history, take the branch\n // with the latest checkpoint and mark it as the root.\n const maxId = (...ids: (string | null)[]) =>\n ids\n .filter((i) => i != null)\n .sort((a, b) => a.localeCompare(b))\n .at(-1)!;\n\n const lastOrphanedNode =\n childrenMap.$ == null\n ? Object.keys(childrenMap)\n .filter((parentId) => !nodeIds.has(parentId))\n .map((parentId) => {\n const queue: string[] = [parentId];\n const seen = new Set<string>();\n\n let lastId = parentId;\n\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n if (seen.has(current)) continue;\n seen.add(current);\n\n const children = (childrenMap[current] ?? []).flatMap(\n (i) => i.checkpoint?.checkpoint_id ?? []\n );\n\n lastId = maxId(lastId, ...children);\n queue.push(...children);\n }\n\n return { parentId, lastId };\n })\n .sort((a, b) => a.lastId.localeCompare(b.lastId))\n .at(-1)?.parentId\n : undefined;\n\n if (lastOrphanedNode != null) childrenMap.$ = childrenMap[lastOrphanedNode];\n\n // Second pass - create a tree of sequences\n type Task = { id: string; sequence: Sequence; path: string[] };\n const rootSequence: Sequence = { type: \"sequence\", items: [] };\n const queue: Task[] = [{ id: \"$\", sequence: rootSequence, path: [] }];\n\n const paths: string[][] = [];\n\n const visited = new Set<string>();\n while (queue.length > 0) {\n const task = queue.shift()!;\n if (visited.has(task.id)) continue;\n visited.add(task.id);\n\n const children = childrenMap[task.id];\n if (children == null || children.length === 0) continue;\n\n // If we've encountered a fork (2+ children), push the fork\n // to the sequence and add a new sequence for each child\n let fork: Fork | undefined;\n if (children.length > 1) {\n fork = { type: \"fork\", items: [] };\n task.sequence.items.push(fork);\n }\n\n for (const value of children) {\n const id = value.checkpoint?.checkpoint_id;\n if (id == null) continue;\n\n let { sequence } = task;\n let { path } = task;\n if (fork != null) {\n sequence = { type: \"sequence\", items: [] };\n fork.items.unshift(sequence);\n\n path = path.slice();\n path.push(id);\n paths.push(path);\n }\n\n sequence.items.push({ type: \"node\", value, path });\n queue.push({ id, sequence, path });\n }\n }\n\n return { rootSequence, paths };\n}\n\nconst PATH_SEP = \">\";\nconst ROOT_ID = \"$\";\n\n// Get flat view\nexport function getBranchView<StateType extends Record<string, unknown>>(\n sequence: Sequence<StateType>,\n paths: string[][],\n branch: string\n) {\n const path = branch.split(PATH_SEP);\n const pathMap: Record<string, string[][]> = {};\n\n for (const path of paths) {\n const parent = path.at(-2) ?? ROOT_ID;\n pathMap[parent] ??= [];\n pathMap[parent].unshift(path);\n }\n\n const history: ThreadState<StateType>[] = [];\n const branchByCheckpoint: Record<\n string,\n { branch: string | undefined; branchOptions: string[] | undefined }\n > = {};\n\n const forkStack = path.slice();\n const queue: (Node<StateType> | Fork<StateType>)[] = [...sequence.items];\n\n while (queue.length > 0) {\n const item = queue.shift()!;\n\n if (item.type === \"node\") {\n history.push(item.value);\n const checkpointId = item.value.checkpoint?.checkpoint_id;\n if (checkpointId == null) continue;\n\n branchByCheckpoint[checkpointId] = {\n branch: item.path.join(PATH_SEP),\n branchOptions: (item.path.length > 0\n ? pathMap[item.path.at(-2) ?? ROOT_ID] ?? []\n : []\n ).map((p) => p.join(PATH_SEP)),\n };\n }\n if (item.type === \"fork\") {\n const forkId = forkStack.shift();\n const index =\n forkId != null\n ? item.items.findIndex((value) => {\n const firstItem = value.items.at(0);\n if (!firstItem || firstItem.type !== \"node\") return false;\n return firstItem.value.checkpoint?.checkpoint_id === forkId;\n })\n : -1;\n\n const nextItems = item.items.at(index)?.items ?? [];\n queue.push(...nextItems);\n }\n }\n\n return { history, branchByCheckpoint };\n}\n\nexport function getBranchContext<StateType extends Record<string, unknown>>(\n branch: string,\n history: ThreadState<StateType>[] | undefined\n) {\n const { rootSequence: branchTree, paths } = getBranchSequence(history ?? []);\n const { history: flatHistory, branchByCheckpoint } = getBranchView(\n branchTree,\n paths,\n branch\n );\n\n return {\n branchTree,\n flatHistory,\n branchByCheckpoint,\n threadHead: flatHistory.at(-1),\n };\n}\n"],"mappings":";AAiCA,SAAgB,kBACd,SACA;CACA,MAAM,0BAAU,IAAI;CACpB,MAAMA,cAAwD;AAG9D,KAAI,QAAQ,UAAU,EACpB,QAAO;EACL,cAAc;GACZ,MAAM;GACN,OAAO,QAAQ,KAAK,WAAW;IAAE,MAAM;IAAQ;IAAO,MAAM;;;EAE9D,OAAO;;AAKX,SAAQ,SAAS,UAAU;EACzB,MAAM,eAAe,MAAM,mBAAmB,iBAAiB;AAC/D,cAAY,kBAAkB;AAC9B,cAAY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UAAU,SAAS,OAAQ,QAAO;AACpD,WAAO,UAAU,MAAM,YAAY,kBAAkB;QAEvD;GAEN,MAAM,YAAY,KAAK,MAAM,GAAG,QAAQ,SAAS;AACjD,SAAM,KAAK,GAAG;;;AAIlB,QAAO;EAAE;EAAS;;;AAGpB,SAAgB,iBACd,QACA,SACA;CACA,MAAM,EAAE,cAAc,YAAY,UAAU,kBAAkB,WAAW;CACzE,MAAM,EAAE,SAAS,aAAa,uBAAuB,cACnD,YACA,OACA;AAGF,QAAO;EACL;EACA;EACA;EACA,YAAY,YAAY,GAAG"}
@@ -1,13 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StreamError = void 0;
4
- class StreamError extends Error {
5
- constructor(data) {
6
- super(data.message);
7
- this.name = data.name ?? data.error ?? "StreamError";
8
- }
9
- static isStructuredError(error) {
10
- return typeof error === "object" && error != null && "message" in error;
11
- }
12
- }
1
+
2
+ //#region src/ui/errors.ts
3
+ var StreamError = class extends Error {
4
+ constructor(data) {
5
+ super(data.message);
6
+ this.name = data.name ?? data.error ?? "StreamError";
7
+ }
8
+ static isStructuredError(error) {
9
+ return typeof error === "object" && error != null && "message" in error;
10
+ }
11
+ };
12
+
13
+ //#endregion
13
14
  exports.StreamError = StreamError;
15
+ //# sourceMappingURL=errors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.cjs","names":[],"sources":["../../src/ui/errors.ts"],"sourcesContent":["export class StreamError extends Error {\n constructor(data: { error?: string; name?: string; message: string }) {\n super(data.message);\n this.name = data.name ?? data.error ?? \"StreamError\";\n }\n\n static isStructuredError(error: unknown): error is {\n error?: string;\n name?: string;\n message: string;\n } {\n return typeof error === \"object\" && error != null && \"message\" in error;\n }\n}\n"],"mappings":";;AAAA,IAAa,cAAb,cAAiC,MAAM;CACrC,YAAY,MAA0D;AACpE,QAAM,KAAK;AACX,OAAK,OAAO,KAAK,QAAQ,KAAK,SAAS;;CAGzC,OAAO,kBAAkB,OAIvB;AACA,SAAO,OAAO,UAAU,YAAY,SAAS,QAAQ,aAAa"}
package/dist/ui/errors.js CHANGED
@@ -1,9 +1,14 @@
1
- export class StreamError extends Error {
2
- constructor(data) {
3
- super(data.message);
4
- this.name = data.name ?? data.error ?? "StreamError";
5
- }
6
- static isStructuredError(error) {
7
- return typeof error === "object" && error != null && "message" in error;
8
- }
9
- }
1
+ //#region src/ui/errors.ts
2
+ var StreamError = class extends Error {
3
+ constructor(data) {
4
+ super(data.message);
5
+ this.name = data.name ?? data.error ?? "StreamError";
6
+ }
7
+ static isStructuredError(error) {
8
+ return typeof error === "object" && error != null && "message" in error;
9
+ }
10
+ };
11
+
12
+ //#endregion
13
+ export { StreamError };
14
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","names":[],"sources":["../../src/ui/errors.ts"],"sourcesContent":["export class StreamError extends Error {\n constructor(data: { error?: string; name?: string; message: string }) {\n super(data.message);\n this.name = data.name ?? data.error ?? \"StreamError\";\n }\n\n static isStructuredError(error: unknown): error is {\n error?: string;\n name?: string;\n message: string;\n } {\n return typeof error === \"object\" && error != null && \"message\" in error;\n }\n}\n"],"mappings":";AAAA,IAAa,cAAb,cAAiC,MAAM;CACrC,YAAY,MAA0D;AACpE,QAAM,KAAK;AACX,OAAK,OAAO,KAAK,QAAQ,KAAK,SAAS;;CAGzC,OAAO,kBAAkB,OAIvB;AACA,SAAO,OAAO,UAAU,YAAY,SAAS,QAAQ,aAAa"}