@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,149 +1,153 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBranchSequence = getBranchSequence;
4
- exports.getBranchView = getBranchView;
5
- exports.getBranchContext = getBranchContext;
1
+
2
+ //#region src/ui/branching.ts
6
3
  function getBranchSequence(history) {
7
- const nodeIds = new Set();
8
- const childrenMap = {};
9
- // Short circuit if there's only a singular one state
10
- if (history.length <= 1) {
11
- return {
12
- rootSequence: {
13
- type: "sequence",
14
- items: history.map((value) => ({ type: "node", value, path: [] })),
15
- },
16
- paths: [],
17
- };
18
- }
19
- // First pass - collect nodes for each checkpoint
20
- history.forEach((state) => {
21
- const checkpointId = state.parent_checkpoint?.checkpoint_id ?? "$";
22
- childrenMap[checkpointId] ??= [];
23
- childrenMap[checkpointId].push(state);
24
- if (state.checkpoint?.checkpoint_id != null) {
25
- nodeIds.add(state.checkpoint.checkpoint_id);
26
- }
27
- });
28
- // If dealing with partial history, take the branch
29
- // with the latest checkpoint and mark it as the root.
30
- const maxId = (...ids) => ids
31
- .filter((i) => i != null)
32
- .sort((a, b) => a.localeCompare(b))
33
- .at(-1);
34
- const lastOrphanedNode = childrenMap.$ == null
35
- ? Object.keys(childrenMap)
36
- .filter((parentId) => !nodeIds.has(parentId))
37
- .map((parentId) => {
38
- const queue = [parentId];
39
- const seen = new Set();
40
- let lastId = parentId;
41
- while (queue.length > 0) {
42
- const current = queue.shift();
43
- if (seen.has(current))
44
- continue;
45
- seen.add(current);
46
- const children = (childrenMap[current] ?? []).flatMap((i) => i.checkpoint?.checkpoint_id ?? []);
47
- lastId = maxId(lastId, ...children);
48
- queue.push(...children);
49
- }
50
- return { parentId, lastId };
51
- })
52
- .sort((a, b) => a.lastId.localeCompare(b.lastId))
53
- .at(-1)?.parentId
54
- : undefined;
55
- if (lastOrphanedNode != null)
56
- childrenMap.$ = childrenMap[lastOrphanedNode];
57
- const rootSequence = { type: "sequence", items: [] };
58
- const queue = [{ id: "$", sequence: rootSequence, path: [] }];
59
- const paths = [];
60
- const visited = new Set();
61
- while (queue.length > 0) {
62
- const task = queue.shift();
63
- if (visited.has(task.id))
64
- continue;
65
- visited.add(task.id);
66
- const children = childrenMap[task.id];
67
- if (children == null || children.length === 0)
68
- continue;
69
- // If we've encountered a fork (2+ children), push the fork
70
- // to the sequence and add a new sequence for each child
71
- let fork;
72
- if (children.length > 1) {
73
- fork = { type: "fork", items: [] };
74
- task.sequence.items.push(fork);
75
- }
76
- for (const value of children) {
77
- const id = value.checkpoint?.checkpoint_id;
78
- if (id == null)
79
- continue;
80
- let { sequence } = task;
81
- let { path } = task;
82
- if (fork != null) {
83
- sequence = { type: "sequence", items: [] };
84
- fork.items.unshift(sequence);
85
- path = path.slice();
86
- path.push(id);
87
- paths.push(path);
88
- }
89
- sequence.items.push({ type: "node", value, path });
90
- queue.push({ id, sequence, path });
91
- }
92
- }
93
- return { rootSequence, paths };
4
+ const nodeIds = /* @__PURE__ */ new Set();
5
+ const childrenMap = {};
6
+ if (history.length <= 1) return {
7
+ rootSequence: {
8
+ type: "sequence",
9
+ items: history.map((value) => ({
10
+ type: "node",
11
+ value,
12
+ path: []
13
+ }))
14
+ },
15
+ paths: []
16
+ };
17
+ history.forEach((state) => {
18
+ const checkpointId = state.parent_checkpoint?.checkpoint_id ?? "$";
19
+ childrenMap[checkpointId] ??= [];
20
+ childrenMap[checkpointId].push(state);
21
+ if (state.checkpoint?.checkpoint_id != null) nodeIds.add(state.checkpoint.checkpoint_id);
22
+ });
23
+ const maxId = (...ids) => ids.filter((i) => i != null).sort((a, b) => a.localeCompare(b)).at(-1);
24
+ const lastOrphanedNode = childrenMap.$ == null ? Object.keys(childrenMap).filter((parentId) => !nodeIds.has(parentId)).map((parentId) => {
25
+ const queue$1 = [parentId];
26
+ const seen = /* @__PURE__ */ new Set();
27
+ let lastId = parentId;
28
+ while (queue$1.length > 0) {
29
+ const current = queue$1.shift();
30
+ if (seen.has(current)) continue;
31
+ seen.add(current);
32
+ const children = (childrenMap[current] ?? []).flatMap((i) => i.checkpoint?.checkpoint_id ?? []);
33
+ lastId = maxId(lastId, ...children);
34
+ queue$1.push(...children);
35
+ }
36
+ return {
37
+ parentId,
38
+ lastId
39
+ };
40
+ }).sort((a, b) => a.lastId.localeCompare(b.lastId)).at(-1)?.parentId : void 0;
41
+ if (lastOrphanedNode != null) childrenMap.$ = childrenMap[lastOrphanedNode];
42
+ const rootSequence = {
43
+ type: "sequence",
44
+ items: []
45
+ };
46
+ const queue = [{
47
+ id: "$",
48
+ sequence: rootSequence,
49
+ path: []
50
+ }];
51
+ const paths = [];
52
+ const visited = /* @__PURE__ */ new Set();
53
+ while (queue.length > 0) {
54
+ const task = queue.shift();
55
+ if (visited.has(task.id)) continue;
56
+ visited.add(task.id);
57
+ const children = childrenMap[task.id];
58
+ if (children == null || children.length === 0) continue;
59
+ let fork;
60
+ if (children.length > 1) {
61
+ fork = {
62
+ type: "fork",
63
+ items: []
64
+ };
65
+ task.sequence.items.push(fork);
66
+ }
67
+ for (const value of children) {
68
+ const id = value.checkpoint?.checkpoint_id;
69
+ if (id == null) continue;
70
+ let { sequence } = task;
71
+ let { path } = task;
72
+ if (fork != null) {
73
+ sequence = {
74
+ type: "sequence",
75
+ items: []
76
+ };
77
+ fork.items.unshift(sequence);
78
+ path = path.slice();
79
+ path.push(id);
80
+ paths.push(path);
81
+ }
82
+ sequence.items.push({
83
+ type: "node",
84
+ value,
85
+ path
86
+ });
87
+ queue.push({
88
+ id,
89
+ sequence,
90
+ path
91
+ });
92
+ }
93
+ }
94
+ return {
95
+ rootSequence,
96
+ paths
97
+ };
94
98
  }
95
99
  const PATH_SEP = ">";
96
100
  const ROOT_ID = "$";
97
- // Get flat view
98
101
  function getBranchView(sequence, paths, branch) {
99
- const path = branch.split(PATH_SEP);
100
- const pathMap = {};
101
- for (const path of paths) {
102
- const parent = path.at(-2) ?? ROOT_ID;
103
- pathMap[parent] ??= [];
104
- pathMap[parent].unshift(path);
105
- }
106
- const history = [];
107
- const branchByCheckpoint = {};
108
- const forkStack = path.slice();
109
- const queue = [...sequence.items];
110
- while (queue.length > 0) {
111
- const item = queue.shift();
112
- if (item.type === "node") {
113
- history.push(item.value);
114
- const checkpointId = item.value.checkpoint?.checkpoint_id;
115
- if (checkpointId == null)
116
- continue;
117
- branchByCheckpoint[checkpointId] = {
118
- branch: item.path.join(PATH_SEP),
119
- branchOptions: (item.path.length > 0
120
- ? pathMap[item.path.at(-2) ?? ROOT_ID] ?? []
121
- : []).map((p) => p.join(PATH_SEP)),
122
- };
123
- }
124
- if (item.type === "fork") {
125
- const forkId = forkStack.shift();
126
- const index = forkId != null
127
- ? item.items.findIndex((value) => {
128
- const firstItem = value.items.at(0);
129
- if (!firstItem || firstItem.type !== "node")
130
- return false;
131
- return firstItem.value.checkpoint?.checkpoint_id === forkId;
132
- })
133
- : -1;
134
- const nextItems = item.items.at(index)?.items ?? [];
135
- queue.push(...nextItems);
136
- }
137
- }
138
- return { history, branchByCheckpoint };
102
+ const path = branch.split(PATH_SEP);
103
+ const pathMap = {};
104
+ for (const path$1 of paths) {
105
+ const parent = path$1.at(-2) ?? ROOT_ID;
106
+ pathMap[parent] ??= [];
107
+ pathMap[parent].unshift(path$1);
108
+ }
109
+ const history = [];
110
+ const branchByCheckpoint = {};
111
+ const forkStack = path.slice();
112
+ const queue = [...sequence.items];
113
+ while (queue.length > 0) {
114
+ const item = queue.shift();
115
+ if (item.type === "node") {
116
+ history.push(item.value);
117
+ const checkpointId = item.value.checkpoint?.checkpoint_id;
118
+ if (checkpointId == null) continue;
119
+ branchByCheckpoint[checkpointId] = {
120
+ branch: item.path.join(PATH_SEP),
121
+ branchOptions: (item.path.length > 0 ? pathMap[item.path.at(-2) ?? ROOT_ID] ?? [] : []).map((p) => p.join(PATH_SEP))
122
+ };
123
+ }
124
+ if (item.type === "fork") {
125
+ const forkId = forkStack.shift();
126
+ const index = forkId != null ? item.items.findIndex((value) => {
127
+ const firstItem = value.items.at(0);
128
+ if (!firstItem || firstItem.type !== "node") return false;
129
+ return firstItem.value.checkpoint?.checkpoint_id === forkId;
130
+ }) : -1;
131
+ const nextItems = item.items.at(index)?.items ?? [];
132
+ queue.push(...nextItems);
133
+ }
134
+ }
135
+ return {
136
+ history,
137
+ branchByCheckpoint
138
+ };
139
139
  }
140
140
  function getBranchContext(branch, history) {
141
- const { rootSequence: branchTree, paths } = getBranchSequence(history ?? []);
142
- const { history: flatHistory, branchByCheckpoint } = getBranchView(branchTree, paths, branch);
143
- return {
144
- branchTree,
145
- flatHistory,
146
- branchByCheckpoint,
147
- threadHead: flatHistory.at(-1),
148
- };
141
+ const { rootSequence: branchTree, paths } = getBranchSequence(history ?? []);
142
+ const { history: flatHistory, branchByCheckpoint } = getBranchView(branchTree, paths, branch);
143
+ return {
144
+ branchTree,
145
+ flatHistory,
146
+ branchByCheckpoint,
147
+ threadHead: flatHistory.at(-1)
148
+ };
149
149
  }
150
+
151
+ //#endregion
152
+ exports.getBranchContext = getBranchContext;
153
+ //# sourceMappingURL=branching.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.cjs","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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ACN,KAAK,MAAM,WAAW,UAAU;IAC9B,MAAM,YAAY,MAAM,MAAM,GAAG;AACjC,QAAI,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"}
@@ -0,0 +1,22 @@
1
+ import { ThreadState } from "../schema.cjs";
2
+
3
+ //#region src/ui/branching.d.ts
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ interface Node<StateType = any> {
6
+ type: "node";
7
+ value: ThreadState<StateType>;
8
+ path: string[];
9
+ }
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ interface Fork<StateType = any> {
12
+ type: "fork";
13
+ items: Array<Sequence<StateType>>;
14
+ }
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ interface Sequence<StateType = any> {
17
+ type: "sequence";
18
+ items: Array<Node<StateType> | Fork<StateType>>;
19
+ }
20
+ //#endregion
21
+ export { Sequence };
22
+ //# sourceMappingURL=branching.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.d.cts","names":["ThreadState","Node","StateType","Fork","Sequence","Array","getBranchSequence","Record","getBranchView","getBranchContext"],"sources":["../../src/ui/branching.d.ts"],"sourcesContent":["import type { ThreadState } from \"../schema.js\";\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// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface Fork<StateType = any> {\n type: \"fork\";\n items: Array<Sequence<StateType>>;\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}\nexport declare function getBranchSequence<StateType extends Record<string, unknown>>(history: ThreadState<StateType>[]): {\n rootSequence: Sequence<any>;\n paths: string[][];\n};\n// Get flat view\nexport declare function getBranchView<StateType extends Record<string, unknown>>(sequence: Sequence<StateType>, paths: string[][], branch: string): {\n history: ThreadState<StateType>[];\n branchByCheckpoint: Record<string, {\n branch: string | undefined;\n branchOptions: string[] | undefined;\n }>;\n};\nexport declare function getBranchContext<StateType extends Record<string, unknown>>(branch: string, history: ThreadState<StateType>[] | undefined): {\n branchTree: Sequence<any>;\n flatHistory: ThreadState<any>[];\n branchByCheckpoint: Record<string, {\n branch: string | undefined;\n branchOptions: string[] | undefined;\n }>;\n threadHead: ThreadState<any> | undefined;\n};\nexport {};\n"],"mappings":";;;;UAEUC;EAAAA,IAAAA,EAAAA,MAAI;EAAA,KAAA,EAEHD,WAFG,CAESE,SAFT,CAAA;MAESA,EAAAA,MAAAA,EAAAA;;;AAAD,UAIZC,IAAI,CAAA,YAAA,GAAA,CAAA,CAAA;EAAA,IAAA,EAAA,MAAA;OAEYD,EAAfG,KAAeH,CAATE,QAASF,CAAAA,SAAAA,CAAAA,CAAAA;;;UAGTE;EAAAA,IAAAA,EAAAA,UAAQ;EAAA,KAAA,EAEdC,KAFc,CAERJ,IAFQ,CAEHC,SAFG,CAAA,GAEUC,IAFV,CAEeD,SAFf,CAAA,CAAA"}
@@ -1,35 +1,22 @@
1
- import type { ThreadState } from "../schema.js";
1
+ import { ThreadState } from "../schema.js";
2
+
3
+ //#region src/ui/branching.d.ts
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
5
  interface Node<StateType = any> {
3
- type: "node";
4
- value: ThreadState<StateType>;
5
- path: string[];
6
+ type: "node";
7
+ value: ThreadState<StateType>;
8
+ path: string[];
6
9
  }
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
11
  interface Fork<StateType = any> {
8
- type: "fork";
9
- items: Array<Sequence<StateType>>;
12
+ type: "fork";
13
+ items: Array<Sequence<StateType>>;
10
14
  }
11
- export interface Sequence<StateType = any> {
12
- type: "sequence";
13
- items: Array<Node<StateType> | Fork<StateType>>;
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ interface Sequence<StateType = any> {
17
+ type: "sequence";
18
+ items: Array<Node<StateType> | Fork<StateType>>;
14
19
  }
15
- export declare function getBranchSequence<StateType extends Record<string, unknown>>(history: ThreadState<StateType>[]): {
16
- rootSequence: Sequence<any>;
17
- paths: string[][];
18
- };
19
- export declare function getBranchView<StateType extends Record<string, unknown>>(sequence: Sequence<StateType>, paths: string[][], branch: string): {
20
- history: ThreadState<StateType>[];
21
- branchByCheckpoint: Record<string, {
22
- branch: string | undefined;
23
- branchOptions: string[] | undefined;
24
- }>;
25
- };
26
- export declare function getBranchContext<StateType extends Record<string, unknown>>(branch: string, history: ThreadState<StateType>[] | undefined): {
27
- branchTree: Sequence<any>;
28
- flatHistory: ThreadState<any>[];
29
- branchByCheckpoint: Record<string, {
30
- branch: string | undefined;
31
- branchOptions: string[] | undefined;
32
- }>;
33
- threadHead: ThreadState<any> | undefined;
34
- };
35
- export {};
20
+ //#endregion
21
+ export { Sequence };
22
+ //# sourceMappingURL=branching.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.d.ts","names":["ThreadState","Node","StateType","Fork","Sequence","Array","getBranchSequence","Record","getBranchView","getBranchContext"],"sources":["../../src/ui/branching.d.ts"],"sourcesContent":["import type { ThreadState } from \"../schema.js\";\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// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface Fork<StateType = any> {\n type: \"fork\";\n items: Array<Sequence<StateType>>;\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}\nexport declare function getBranchSequence<StateType extends Record<string, unknown>>(history: ThreadState<StateType>[]): {\n rootSequence: Sequence<any>;\n paths: string[][];\n};\n// Get flat view\nexport declare function getBranchView<StateType extends Record<string, unknown>>(sequence: Sequence<StateType>, paths: string[][], branch: string): {\n history: ThreadState<StateType>[];\n branchByCheckpoint: Record<string, {\n branch: string | undefined;\n branchOptions: string[] | undefined;\n }>;\n};\nexport declare function getBranchContext<StateType extends Record<string, unknown>>(branch: string, history: ThreadState<StateType>[] | undefined): {\n branchTree: Sequence<any>;\n flatHistory: ThreadState<any>[];\n branchByCheckpoint: Record<string, {\n branch: string | undefined;\n branchOptions: string[] | undefined;\n }>;\n threadHead: ThreadState<any> | undefined;\n};\nexport {};\n"],"mappings":";;;;UAEUC;EAAAA,IAAAA,EAAAA,MAAI;EAAA,KAAA,EAEHD,WAFG,CAESE,SAFT,CAAA;MAESA,EAAAA,MAAAA,EAAAA;;;AAAD,UAIZC,IAAI,CAAA,YAAA,GAAA,CAAA,CAAA;EAAA,IAAA,EAAA,MAAA;OAEYD,EAAfG,KAAeH,CAATE,QAASF,CAAAA,SAAAA,CAAAA,CAAAA;;;UAGTE;EAAAA,IAAAA,EAAAA,UAAQ;EAAA,KAAA,EAEdC,KAFc,CAERJ,IAFQ,CAEHC,SAFG,CAAA,GAEUC,IAFV,CAEeD,SAFf,CAAA,CAAA"}