@langchain/langgraph-sdk 0.1.10 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +11 -0
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth/error.cjs +76 -86
  5. package/dist/auth/error.cjs.map +1 -0
  6. package/dist/auth/error.d.cts +13 -0
  7. package/dist/auth/error.d.cts.map +1 -0
  8. package/dist/auth/error.d.ts +12 -8
  9. package/dist/auth/error.d.ts.map +1 -0
  10. package/dist/auth/error.js +76 -83
  11. package/dist/auth/error.js.map +1 -0
  12. package/dist/auth/index.cjs +36 -44
  13. package/dist/auth/index.cjs.map +1 -0
  14. package/dist/auth/index.d.cts +31 -0
  15. package/dist/auth/index.d.cts.map +1 -0
  16. package/dist/auth/index.d.ts +20 -15
  17. package/dist/auth/index.d.ts.map +1 -0
  18. package/dist/auth/index.js +35 -39
  19. package/dist/auth/index.js.map +1 -0
  20. package/dist/auth/types.d.cts +294 -0
  21. package/dist/auth/types.d.cts.map +1 -0
  22. package/dist/auth/types.d.ts +162 -165
  23. package/dist/auth/types.d.ts.map +1 -0
  24. package/dist/client.cjs +1110 -1294
  25. package/dist/client.cjs.map +1 -0
  26. package/dist/client.d.cts +697 -0
  27. package/dist/client.d.cts.map +1 -0
  28. package/dist/client.d.ts +656 -639
  29. package/dist/client.d.ts.map +1 -0
  30. package/dist/client.js +1101 -1281
  31. package/dist/client.js.map +1 -0
  32. package/dist/index.cjs +6 -8
  33. package/dist/index.d.cts +7 -0
  34. package/dist/index.d.ts +7 -7
  35. package/dist/index.js +4 -2
  36. package/dist/logging/index.cjs +30 -31
  37. package/dist/logging/index.cjs.map +1 -0
  38. package/dist/logging/index.d.cts +47 -0
  39. package/dist/logging/index.d.cts.map +1 -0
  40. package/dist/logging/index.d.ts +20 -16
  41. package/dist/logging/index.d.ts.map +1 -0
  42. package/dist/logging/index.js +31 -29
  43. package/dist/logging/index.js.map +1 -0
  44. package/dist/react/index.cjs +5 -7
  45. package/dist/react/index.d.cts +4 -0
  46. package/dist/react/index.d.ts +4 -3
  47. package/dist/react/index.js +4 -2
  48. package/dist/react/stream.cjs +14 -15
  49. package/dist/react/stream.cjs.map +1 -0
  50. package/dist/react/stream.custom.cjs +126 -145
  51. package/dist/react/stream.custom.cjs.map +1 -0
  52. package/dist/react/stream.custom.d.cts +39 -0
  53. package/dist/react/stream.custom.d.cts.map +1 -0
  54. package/dist/react/stream.custom.d.ts +35 -37
  55. package/dist/react/stream.custom.d.ts.map +1 -0
  56. package/dist/react/stream.custom.js +122 -137
  57. package/dist/react/stream.custom.js.map +1 -0
  58. package/dist/react/stream.d.cts +73 -0
  59. package/dist/react/stream.d.cts.map +1 -0
  60. package/dist/react/stream.d.ts +21 -15
  61. package/dist/react/stream.d.ts.map +1 -0
  62. package/dist/react/stream.js +12 -11
  63. package/dist/react/stream.js.map +1 -0
  64. package/dist/react/stream.lgp.cjs +438 -498
  65. package/dist/react/stream.lgp.cjs.map +1 -0
  66. package/dist/react/stream.lgp.js +433 -490
  67. package/dist/react/stream.lgp.js.map +1 -0
  68. package/dist/react/thread.cjs +18 -15
  69. package/dist/react/thread.cjs.map +1 -0
  70. package/dist/react/thread.js +19 -13
  71. package/dist/react/thread.js.map +1 -0
  72. package/dist/react/types.d.cts +356 -0
  73. package/dist/react/types.d.cts.map +1 -0
  74. package/dist/react/types.d.ts +334 -329
  75. package/dist/react/types.d.ts.map +1 -0
  76. package/dist/react-ui/client.cjs +123 -184
  77. package/dist/react-ui/client.cjs.map +1 -0
  78. package/dist/react-ui/client.d.cts +90 -0
  79. package/dist/react-ui/client.d.cts.map +1 -0
  80. package/dist/react-ui/client.d.ts +67 -51
  81. package/dist/react-ui/client.d.ts.map +1 -0
  82. package/dist/react-ui/client.js +118 -143
  83. package/dist/react-ui/client.js.map +1 -0
  84. package/dist/react-ui/index.cjs +14 -13
  85. package/dist/react-ui/index.cjs.map +1 -0
  86. package/dist/react-ui/index.d.cts +3 -0
  87. package/dist/react-ui/index.d.ts +3 -2
  88. package/dist/react-ui/index.js +8 -3
  89. package/dist/react-ui/index.js.map +1 -0
  90. package/dist/react-ui/server/index.cjs +5 -7
  91. package/dist/react-ui/server/index.d.cts +3 -0
  92. package/dist/react-ui/server/index.d.ts +3 -2
  93. package/dist/react-ui/server/index.js +4 -2
  94. package/dist/react-ui/server/server.cjs +53 -44
  95. package/dist/react-ui/server/server.cjs.map +1 -0
  96. package/dist/react-ui/server/server.d.cts +55 -0
  97. package/dist/react-ui/server/server.d.cts.map +1 -0
  98. package/dist/react-ui/server/server.d.ts +43 -39
  99. package/dist/react-ui/server/server.d.ts.map +1 -0
  100. package/dist/react-ui/server/server.js +54 -42
  101. package/dist/react-ui/server/server.js.map +1 -0
  102. package/dist/react-ui/types.cjs +32 -36
  103. package/dist/react-ui/types.cjs.map +1 -0
  104. package/dist/react-ui/types.d.cts +25 -0
  105. package/dist/react-ui/types.d.cts.map +1 -0
  106. package/dist/react-ui/types.d.ts +23 -19
  107. package/dist/react-ui/types.d.ts.map +1 -0
  108. package/dist/react-ui/types.js +32 -34
  109. package/dist/react-ui/types.js.map +1 -0
  110. package/dist/schema.d.cts +278 -0
  111. package/dist/schema.d.cts.map +1 -0
  112. package/dist/schema.d.ts +250 -242
  113. package/dist/schema.d.ts.map +1 -0
  114. package/dist/singletons/fetch.cjs +16 -20
  115. package/dist/singletons/fetch.cjs.map +1 -0
  116. package/dist/singletons/fetch.d.cts +14 -0
  117. package/dist/singletons/fetch.d.cts.map +1 -0
  118. package/dist/singletons/fetch.d.ts +5 -2
  119. package/dist/singletons/fetch.d.ts.map +1 -0
  120. package/dist/singletons/fetch.js +16 -17
  121. package/dist/singletons/fetch.js.map +1 -0
  122. package/dist/types.d.cts +187 -0
  123. package/dist/types.d.cts.map +1 -0
  124. package/dist/types.d.ts +178 -173
  125. package/dist/types.d.ts.map +1 -0
  126. package/dist/types.messages.d.cts +91 -0
  127. package/dist/types.messages.d.cts.map +1 -0
  128. package/dist/types.messages.d.ts +71 -66
  129. package/dist/types.messages.d.ts.map +1 -0
  130. package/dist/types.stream.d.cts +235 -0
  131. package/dist/types.stream.d.cts.map +1 -0
  132. package/dist/types.stream.d.ts +149 -142
  133. package/dist/types.stream.d.ts.map +1 -0
  134. package/dist/ui/branching.cjs +145 -141
  135. package/dist/ui/branching.cjs.map +1 -0
  136. package/dist/ui/branching.d.cts +22 -0
  137. package/dist/ui/branching.d.cts.map +1 -0
  138. package/dist/ui/branching.d.ts +17 -30
  139. package/dist/ui/branching.d.ts.map +1 -0
  140. package/dist/ui/branching.js +147 -139
  141. package/dist/ui/branching.js.map +1 -0
  142. package/dist/ui/errors.cjs +14 -12
  143. package/dist/ui/errors.cjs.map +1 -0
  144. package/dist/ui/errors.js +14 -9
  145. package/dist/ui/errors.js.map +1 -0
  146. package/dist/ui/manager.cjs +156 -232
  147. package/dist/ui/manager.cjs.map +1 -0
  148. package/dist/ui/manager.js +156 -228
  149. package/dist/ui/manager.js.map +1 -0
  150. package/dist/ui/messages.cjs +55 -69
  151. package/dist/ui/messages.cjs.map +1 -0
  152. package/dist/ui/messages.js +55 -66
  153. package/dist/ui/messages.js.map +1 -0
  154. package/dist/ui/utils.cjs +10 -10
  155. package/dist/ui/utils.cjs.map +1 -0
  156. package/dist/ui/utils.js +10 -8
  157. package/dist/ui/utils.js.map +1 -0
  158. package/dist/utils/async_caller.cjs +103 -185
  159. package/dist/utils/async_caller.cjs.map +1 -0
  160. package/dist/utils/async_caller.d.cts +53 -0
  161. package/dist/utils/async_caller.d.cts.map +1 -0
  162. package/dist/utils/async_caller.d.ts +36 -31
  163. package/dist/utils/async_caller.d.ts.map +1 -0
  164. package/dist/utils/async_caller.js +101 -177
  165. package/dist/utils/async_caller.js.map +1 -0
  166. package/dist/utils/env.cjs +11 -13
  167. package/dist/utils/env.cjs.map +1 -0
  168. package/dist/utils/env.js +11 -11
  169. package/dist/utils/env.js.map +1 -0
  170. package/dist/utils/signals.cjs +18 -19
  171. package/dist/utils/signals.cjs.map +1 -0
  172. package/dist/utils/signals.js +18 -17
  173. package/dist/utils/signals.js.map +1 -0
  174. package/dist/utils/sse.cjs +110 -140
  175. package/dist/utils/sse.cjs.map +1 -0
  176. package/dist/utils/sse.js +110 -138
  177. package/dist/utils/sse.js.map +1 -0
  178. package/dist/utils/stream.cjs +90 -114
  179. package/dist/utils/stream.cjs.map +1 -0
  180. package/dist/utils/stream.js +90 -111
  181. package/dist/utils/stream.js.map +1 -0
  182. package/package.json +73 -81
  183. package/auth.cjs +0 -1
  184. package/auth.d.cts +0 -1
  185. package/auth.d.ts +0 -1
  186. package/auth.js +0 -1
  187. package/client.cjs +0 -1
  188. package/client.d.cts +0 -1
  189. package/client.d.ts +0 -1
  190. package/client.js +0 -1
  191. package/dist/auth/types.cjs +0 -2
  192. package/dist/auth/types.js +0 -1
  193. package/dist/react/stream.lgp.d.ts +0 -7
  194. package/dist/react/thread.d.ts +0 -4
  195. package/dist/react/types.cjs +0 -2
  196. package/dist/react/types.js +0 -1
  197. package/dist/schema.cjs +0 -2
  198. package/dist/schema.js +0 -1
  199. package/dist/types.cjs +0 -2
  200. package/dist/types.js +0 -1
  201. package/dist/types.messages.cjs +0 -2
  202. package/dist/types.messages.js +0 -1
  203. package/dist/types.stream.cjs +0 -2
  204. package/dist/types.stream.js +0 -1
  205. package/dist/ui/branching.test.cjs +0 -370
  206. package/dist/ui/branching.test.d.ts +0 -1
  207. package/dist/ui/branching.test.js +0 -368
  208. package/dist/ui/errors.d.ts +0 -12
  209. package/dist/ui/manager.d.ts +0 -87
  210. package/dist/ui/messages.d.ts +0 -18
  211. package/dist/ui/utils.d.ts +0 -2
  212. package/dist/utils/env.d.ts +0 -1
  213. package/dist/utils/signals.d.ts +0 -1
  214. package/dist/utils/sse.d.ts +0 -8
  215. package/dist/utils/stream.d.ts +0 -13
  216. package/index.cjs +0 -1
  217. package/index.d.cts +0 -1
  218. package/index.d.ts +0 -1
  219. package/index.js +0 -1
  220. package/logging.cjs +0 -1
  221. package/logging.d.cts +0 -1
  222. package/logging.d.ts +0 -1
  223. package/logging.js +0 -1
  224. package/react-ui/server.cjs +0 -1
  225. package/react-ui/server.d.cts +0 -1
  226. package/react-ui/server.d.ts +0 -1
  227. package/react-ui/server.js +0 -1
  228. package/react-ui.cjs +0 -1
  229. package/react-ui.d.cts +0 -1
  230. package/react-ui.d.ts +0 -1
  231. package/react-ui.js +0 -1
  232. package/react.cjs +0 -1
  233. package/react.d.cts +0 -1
  234. package/react.d.ts +0 -1
  235. 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,kBA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AAI,CAAC,aAAa,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"}