@assistant-ui/react-langgraph 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +40 -8
- package/dist/index.d.ts +40 -8
- package/dist/index.js +579 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +589 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// ../../node_modules/.pnpm/secure-json-parse@3.0.2/node_modules/secure-json-parse/index.js
|
|
28
|
+
var require_secure_json_parse = __commonJS({
|
|
29
|
+
"../../node_modules/.pnpm/secure-json-parse@3.0.2/node_modules/secure-json-parse/index.js"(exports, module) {
|
|
30
|
+
"use strict";
|
|
31
|
+
var hasBuffer = typeof Buffer !== "undefined";
|
|
32
|
+
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
33
|
+
var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
34
|
+
function _parse(text, reviver, options) {
|
|
35
|
+
if (options == null) {
|
|
36
|
+
if (reviver !== null && typeof reviver === "object") {
|
|
37
|
+
options = reviver;
|
|
38
|
+
reviver = void 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (hasBuffer && Buffer.isBuffer(text)) {
|
|
42
|
+
text = text.toString();
|
|
43
|
+
}
|
|
44
|
+
if (text && text.charCodeAt(0) === 65279) {
|
|
45
|
+
text = text.slice(1);
|
|
46
|
+
}
|
|
47
|
+
const obj = JSON.parse(text, reviver);
|
|
48
|
+
if (obj === null || typeof obj !== "object") {
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
const protoAction = options && options.protoAction || "error";
|
|
52
|
+
const constructorAction = options && options.constructorAction || "error";
|
|
53
|
+
if (protoAction === "ignore" && constructorAction === "ignore") {
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
if (protoAction !== "ignore" && constructorAction !== "ignore") {
|
|
57
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
} else if (protoAction !== "ignore" && constructorAction === "ignore") {
|
|
61
|
+
if (suspectProtoRx.test(text) === false) {
|
|
62
|
+
return obj;
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
if (suspectConstructorRx.test(text) === false) {
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
|
|
70
|
+
}
|
|
71
|
+
function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
|
|
72
|
+
let next = [obj];
|
|
73
|
+
while (next.length) {
|
|
74
|
+
const nodes = next;
|
|
75
|
+
next = [];
|
|
76
|
+
for (const node of nodes) {
|
|
77
|
+
if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
78
|
+
if (safe === true) {
|
|
79
|
+
return null;
|
|
80
|
+
} else if (protoAction === "error") {
|
|
81
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
82
|
+
}
|
|
83
|
+
delete node.__proto__;
|
|
84
|
+
}
|
|
85
|
+
if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
86
|
+
if (safe === true) {
|
|
87
|
+
return null;
|
|
88
|
+
} else if (constructorAction === "error") {
|
|
89
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
90
|
+
}
|
|
91
|
+
delete node.constructor;
|
|
92
|
+
}
|
|
93
|
+
for (const key in node) {
|
|
94
|
+
const value = node[key];
|
|
95
|
+
if (value && typeof value === "object") {
|
|
96
|
+
next.push(value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return obj;
|
|
102
|
+
}
|
|
103
|
+
function parse(text, reviver, options) {
|
|
104
|
+
const { stackTraceLimit } = Error;
|
|
105
|
+
Error.stackTraceLimit = 0;
|
|
106
|
+
try {
|
|
107
|
+
return _parse(text, reviver, options);
|
|
108
|
+
} finally {
|
|
109
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function safeParse(text, reviver) {
|
|
113
|
+
const { stackTraceLimit } = Error;
|
|
114
|
+
Error.stackTraceLimit = 0;
|
|
115
|
+
try {
|
|
116
|
+
return _parse(text, reviver, { safe: true });
|
|
117
|
+
} catch (_e) {
|
|
118
|
+
return null;
|
|
119
|
+
} finally {
|
|
120
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
module.exports = parse;
|
|
124
|
+
module.exports.default = parse;
|
|
125
|
+
module.exports.parse = parse;
|
|
126
|
+
module.exports.safeParse = safeParse;
|
|
127
|
+
module.exports.scan = filter;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
1
131
|
// src/useLangGraphRuntime.ts
|
|
2
132
|
import { useEffect, useRef as useRef2, useState as useState2 } from "react";
|
|
3
133
|
import {
|
|
@@ -7,7 +137,7 @@ import {
|
|
|
7
137
|
useThreadListItemRuntime
|
|
8
138
|
} from "@assistant-ui/react";
|
|
9
139
|
|
|
10
|
-
// src/
|
|
140
|
+
// src/convertLangChainMessages.ts
|
|
11
141
|
var contentToParts = (content) => {
|
|
12
142
|
if (typeof content === "string")
|
|
13
143
|
return [{ type: "text", text: content }];
|
|
@@ -33,7 +163,7 @@ var contentToParts = (content) => {
|
|
|
33
163
|
}
|
|
34
164
|
}).filter((a) => a !== null);
|
|
35
165
|
};
|
|
36
|
-
var
|
|
166
|
+
var convertLangChainMessages = (message) => {
|
|
37
167
|
switch (message.type) {
|
|
38
168
|
case "system":
|
|
39
169
|
return {
|
|
@@ -76,25 +206,59 @@ var convertLangchainMessages = (message) => {
|
|
|
76
206
|
|
|
77
207
|
// src/useLangGraphMessages.ts
|
|
78
208
|
import { useState, useCallback, useRef } from "react";
|
|
209
|
+
import { v4 as uuidv42 } from "uuid";
|
|
210
|
+
|
|
211
|
+
// src/LangGraphMessageAccumulator.ts
|
|
79
212
|
import { v4 as uuidv4 } from "uuid";
|
|
213
|
+
var LangGraphMessageAccumulator = class {
|
|
214
|
+
messagesMap = /* @__PURE__ */ new Map();
|
|
215
|
+
appendMessage;
|
|
216
|
+
constructor({
|
|
217
|
+
initialMessages = [],
|
|
218
|
+
appendMessage = (_, curr) => curr
|
|
219
|
+
} = {}) {
|
|
220
|
+
this.appendMessage = appendMessage;
|
|
221
|
+
this.addMessages(initialMessages);
|
|
222
|
+
}
|
|
223
|
+
ensureMessageId(message) {
|
|
224
|
+
return message.id ? message : { ...message, id: uuidv4() };
|
|
225
|
+
}
|
|
226
|
+
addMessages(newMessages) {
|
|
227
|
+
if (newMessages.length === 0) return this.getMessages();
|
|
228
|
+
for (const message of newMessages.map(this.ensureMessageId)) {
|
|
229
|
+
const previous = message.id ? this.messagesMap.get(message.id) : void 0;
|
|
230
|
+
this.messagesMap.set(
|
|
231
|
+
message.id ?? uuidv4(),
|
|
232
|
+
this.appendMessage(previous, message)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return this.getMessages();
|
|
236
|
+
}
|
|
237
|
+
getMessages() {
|
|
238
|
+
return [...this.messagesMap.values()];
|
|
239
|
+
}
|
|
240
|
+
clear() {
|
|
241
|
+
this.messagesMap.clear();
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// src/useLangGraphMessages.ts
|
|
246
|
+
var DEFAULT_APPEND_MESSAGE = (_, curr) => curr;
|
|
80
247
|
var useLangGraphMessages = ({
|
|
81
|
-
stream
|
|
248
|
+
stream,
|
|
249
|
+
appendMessage = DEFAULT_APPEND_MESSAGE
|
|
82
250
|
}) => {
|
|
83
251
|
const [interrupt, setInterrupt] = useState();
|
|
84
252
|
const [messages, setMessages] = useState([]);
|
|
85
253
|
const abortControllerRef = useRef(null);
|
|
86
254
|
const sendMessage = useCallback(
|
|
87
255
|
async (newMessages, config) => {
|
|
88
|
-
newMessages = newMessages.map((m) => m.id ? m : { ...m, id:
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
setMessages([...messagesMap.values()]);
|
|
96
|
-
};
|
|
97
|
-
addMessages([...messages, ...newMessages]);
|
|
256
|
+
newMessages = newMessages.map((m) => m.id ? m : { ...m, id: uuidv42() });
|
|
257
|
+
const accumulator = new LangGraphMessageAccumulator({
|
|
258
|
+
initialMessages: messages,
|
|
259
|
+
appendMessage
|
|
260
|
+
});
|
|
261
|
+
setMessages(accumulator.addMessages(newMessages));
|
|
98
262
|
const abortController = new AbortController();
|
|
99
263
|
abortControllerRef.current = abortController;
|
|
100
264
|
const response = await stream(newMessages, {
|
|
@@ -103,24 +267,417 @@ var useLangGraphMessages = ({
|
|
|
103
267
|
});
|
|
104
268
|
for await (const chunk of response) {
|
|
105
269
|
if (chunk.event === "messages/partial" || chunk.event === "messages/complete") {
|
|
106
|
-
addMessages(chunk.data);
|
|
270
|
+
setMessages(accumulator.addMessages(chunk.data));
|
|
107
271
|
} else if (chunk.event === "updates") {
|
|
108
272
|
setInterrupt(chunk.data.__interrupt__?.[0]);
|
|
109
273
|
}
|
|
110
274
|
}
|
|
111
275
|
},
|
|
112
|
-
[messages, stream]
|
|
276
|
+
[messages, stream, appendMessage]
|
|
113
277
|
);
|
|
114
278
|
const cancel = useCallback(() => {
|
|
115
279
|
if (abortControllerRef.current) {
|
|
116
280
|
abortControllerRef.current.abort();
|
|
117
281
|
}
|
|
118
282
|
}, [abortControllerRef]);
|
|
119
|
-
return {
|
|
283
|
+
return {
|
|
284
|
+
interrupt,
|
|
285
|
+
messages,
|
|
286
|
+
sendMessage,
|
|
287
|
+
cancel,
|
|
288
|
+
setInterrupt,
|
|
289
|
+
setMessages
|
|
290
|
+
};
|
|
120
291
|
};
|
|
121
292
|
|
|
122
293
|
// src/useLangGraphRuntime.ts
|
|
123
294
|
import { SimpleImageAttachmentAdapter } from "@assistant-ui/react";
|
|
295
|
+
|
|
296
|
+
// ../react/src/utils/json/parse-partial-json.ts
|
|
297
|
+
var import_secure_json_parse = __toESM(require_secure_json_parse());
|
|
298
|
+
|
|
299
|
+
// ../react/src/utils/json/fix-json.ts
|
|
300
|
+
function fixJson(input) {
|
|
301
|
+
const stack = ["ROOT"];
|
|
302
|
+
let lastValidIndex = -1;
|
|
303
|
+
let literalStart = null;
|
|
304
|
+
function processValueStart(char, i, swapState) {
|
|
305
|
+
{
|
|
306
|
+
switch (char) {
|
|
307
|
+
case '"': {
|
|
308
|
+
lastValidIndex = i;
|
|
309
|
+
stack.pop();
|
|
310
|
+
stack.push(swapState);
|
|
311
|
+
stack.push("INSIDE_STRING");
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
case "f":
|
|
315
|
+
case "t":
|
|
316
|
+
case "n": {
|
|
317
|
+
lastValidIndex = i;
|
|
318
|
+
literalStart = i;
|
|
319
|
+
stack.pop();
|
|
320
|
+
stack.push(swapState);
|
|
321
|
+
stack.push("INSIDE_LITERAL");
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
case "-": {
|
|
325
|
+
stack.pop();
|
|
326
|
+
stack.push(swapState);
|
|
327
|
+
stack.push("INSIDE_NUMBER");
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case "0":
|
|
331
|
+
case "1":
|
|
332
|
+
case "2":
|
|
333
|
+
case "3":
|
|
334
|
+
case "4":
|
|
335
|
+
case "5":
|
|
336
|
+
case "6":
|
|
337
|
+
case "7":
|
|
338
|
+
case "8":
|
|
339
|
+
case "9": {
|
|
340
|
+
lastValidIndex = i;
|
|
341
|
+
stack.pop();
|
|
342
|
+
stack.push(swapState);
|
|
343
|
+
stack.push("INSIDE_NUMBER");
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
case "{": {
|
|
347
|
+
lastValidIndex = i;
|
|
348
|
+
stack.pop();
|
|
349
|
+
stack.push(swapState);
|
|
350
|
+
stack.push("INSIDE_OBJECT_START");
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
case "[": {
|
|
354
|
+
lastValidIndex = i;
|
|
355
|
+
stack.pop();
|
|
356
|
+
stack.push(swapState);
|
|
357
|
+
stack.push("INSIDE_ARRAY_START");
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function processAfterObjectValue(char, i) {
|
|
364
|
+
switch (char) {
|
|
365
|
+
case ",": {
|
|
366
|
+
stack.pop();
|
|
367
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
case "}": {
|
|
371
|
+
lastValidIndex = i;
|
|
372
|
+
stack.pop();
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
function processAfterArrayValue(char, i) {
|
|
378
|
+
switch (char) {
|
|
379
|
+
case ",": {
|
|
380
|
+
stack.pop();
|
|
381
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
case "]": {
|
|
385
|
+
lastValidIndex = i;
|
|
386
|
+
stack.pop();
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
for (let i = 0; i < input.length; i++) {
|
|
392
|
+
const char = input[i];
|
|
393
|
+
const currentState = stack[stack.length - 1];
|
|
394
|
+
switch (currentState) {
|
|
395
|
+
case "ROOT":
|
|
396
|
+
processValueStart(char, i, "FINISH");
|
|
397
|
+
break;
|
|
398
|
+
case "INSIDE_OBJECT_START": {
|
|
399
|
+
switch (char) {
|
|
400
|
+
case '"': {
|
|
401
|
+
stack.pop();
|
|
402
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
case "}": {
|
|
406
|
+
lastValidIndex = i;
|
|
407
|
+
stack.pop();
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
414
|
+
switch (char) {
|
|
415
|
+
case '"': {
|
|
416
|
+
stack.pop();
|
|
417
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "INSIDE_OBJECT_KEY": {
|
|
424
|
+
switch (char) {
|
|
425
|
+
case '"': {
|
|
426
|
+
stack.pop();
|
|
427
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
434
|
+
switch (char) {
|
|
435
|
+
case ":": {
|
|
436
|
+
stack.pop();
|
|
437
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
444
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
448
|
+
processAfterObjectValue(char, i);
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case "INSIDE_STRING": {
|
|
452
|
+
switch (char) {
|
|
453
|
+
case '"': {
|
|
454
|
+
stack.pop();
|
|
455
|
+
lastValidIndex = i;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "\\": {
|
|
459
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
default: {
|
|
463
|
+
lastValidIndex = i;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case "INSIDE_ARRAY_START": {
|
|
469
|
+
switch (char) {
|
|
470
|
+
case "]": {
|
|
471
|
+
lastValidIndex = i;
|
|
472
|
+
stack.pop();
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
default: {
|
|
476
|
+
lastValidIndex = i;
|
|
477
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
484
|
+
switch (char) {
|
|
485
|
+
case ",": {
|
|
486
|
+
stack.pop();
|
|
487
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
case "]": {
|
|
491
|
+
lastValidIndex = i;
|
|
492
|
+
stack.pop();
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
default: {
|
|
496
|
+
lastValidIndex = i;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
503
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
case "INSIDE_STRING_ESCAPE": {
|
|
507
|
+
stack.pop();
|
|
508
|
+
lastValidIndex = i;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
case "INSIDE_NUMBER": {
|
|
512
|
+
switch (char) {
|
|
513
|
+
case "0":
|
|
514
|
+
case "1":
|
|
515
|
+
case "2":
|
|
516
|
+
case "3":
|
|
517
|
+
case "4":
|
|
518
|
+
case "5":
|
|
519
|
+
case "6":
|
|
520
|
+
case "7":
|
|
521
|
+
case "8":
|
|
522
|
+
case "9": {
|
|
523
|
+
lastValidIndex = i;
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
case "e":
|
|
527
|
+
case "E":
|
|
528
|
+
case "-":
|
|
529
|
+
case ".": {
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
case ",": {
|
|
533
|
+
stack.pop();
|
|
534
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
535
|
+
processAfterArrayValue(char, i);
|
|
536
|
+
}
|
|
537
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
538
|
+
processAfterObjectValue(char, i);
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
case "}": {
|
|
543
|
+
stack.pop();
|
|
544
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
545
|
+
processAfterObjectValue(char, i);
|
|
546
|
+
}
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
case "]": {
|
|
550
|
+
stack.pop();
|
|
551
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
552
|
+
processAfterArrayValue(char, i);
|
|
553
|
+
}
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
default: {
|
|
557
|
+
stack.pop();
|
|
558
|
+
break;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case "INSIDE_LITERAL": {
|
|
564
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
|
565
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
566
|
+
stack.pop();
|
|
567
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
568
|
+
processAfterObjectValue(char, i);
|
|
569
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
570
|
+
processAfterArrayValue(char, i);
|
|
571
|
+
}
|
|
572
|
+
} else {
|
|
573
|
+
lastValidIndex = i;
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
let result = input.slice(0, lastValidIndex + 1);
|
|
580
|
+
let partialCount = 0;
|
|
581
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
582
|
+
const state = stack[i];
|
|
583
|
+
switch (state) {
|
|
584
|
+
case "INSIDE_STRING": {
|
|
585
|
+
result += '"';
|
|
586
|
+
partialCount++;
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
case "INSIDE_OBJECT_KEY":
|
|
590
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
|
591
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
592
|
+
case "INSIDE_OBJECT_START":
|
|
593
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
594
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
595
|
+
result += "}";
|
|
596
|
+
partialCount++;
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
case "INSIDE_ARRAY_START":
|
|
600
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
601
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
602
|
+
result += "]";
|
|
603
|
+
partialCount++;
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
case "INSIDE_LITERAL": {
|
|
607
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
|
608
|
+
if ("true".startsWith(partialLiteral)) {
|
|
609
|
+
result += "true".slice(partialLiteral.length);
|
|
610
|
+
} else if ("false".startsWith(partialLiteral)) {
|
|
611
|
+
result += "false".slice(partialLiteral.length);
|
|
612
|
+
} else if ("null".startsWith(partialLiteral)) {
|
|
613
|
+
result += "null".slice(partialLiteral.length);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return [result, partialCount];
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// ../react/src/utils/json/parse-partial-json.ts
|
|
622
|
+
var PARTIAL_JSON_COUNT_SYMBOL = Symbol("partial-json-count");
|
|
623
|
+
var parsePartialJson = (json) => {
|
|
624
|
+
try {
|
|
625
|
+
return import_secure_json_parse.default.parse(json);
|
|
626
|
+
} catch {
|
|
627
|
+
try {
|
|
628
|
+
const [fixedJson, partialCount] = fixJson(json);
|
|
629
|
+
const res = import_secure_json_parse.default.parse(fixedJson);
|
|
630
|
+
res[PARTIAL_JSON_COUNT_SYMBOL] = partialCount;
|
|
631
|
+
return res;
|
|
632
|
+
} catch {
|
|
633
|
+
return void 0;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
var COMPLETE_STATUS = Object.freeze({ type: "complete" });
|
|
638
|
+
|
|
639
|
+
// src/appendLangChainChunk.ts
|
|
640
|
+
var appendLangChainChunk = (prev, curr) => {
|
|
641
|
+
if (curr.type !== "AIMessageChunk") {
|
|
642
|
+
return curr;
|
|
643
|
+
}
|
|
644
|
+
if (!prev || prev.type !== "ai") {
|
|
645
|
+
return {
|
|
646
|
+
...curr,
|
|
647
|
+
type: curr.type.replace("MessageChunk", "").toLowerCase()
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
const newContent = typeof prev.content === "string" ? [{ type: "text", text: prev.content }] : [...prev.content];
|
|
651
|
+
for (const chunk of curr.content) {
|
|
652
|
+
if (chunk.type === "text") {
|
|
653
|
+
const existing = newContent[chunk.index] ?? { type: "text", text: "" };
|
|
654
|
+
if (existing.type !== "text") throw new Error("");
|
|
655
|
+
newContent[chunk.index] = {
|
|
656
|
+
...existing,
|
|
657
|
+
...chunk,
|
|
658
|
+
text: existing.text + chunk.text
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
const newToolCalls = [...prev.tool_calls ?? []];
|
|
663
|
+
for (const chunk of curr.tool_call_chunks) {
|
|
664
|
+
const existing = newToolCalls[chunk.index - 1] ?? { argsText: "" };
|
|
665
|
+
const newArgsText = existing.argsText + chunk.args;
|
|
666
|
+
newToolCalls[chunk.index - 1] = {
|
|
667
|
+
...chunk,
|
|
668
|
+
...existing,
|
|
669
|
+
argsText: newArgsText,
|
|
670
|
+
args: parsePartialJson(newArgsText)
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
return {
|
|
674
|
+
...prev,
|
|
675
|
+
content: newContent,
|
|
676
|
+
tool_calls: newToolCalls
|
|
677
|
+
};
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
// src/useLangGraphRuntime.ts
|
|
124
681
|
var getPendingToolCalls = (messages) => {
|
|
125
682
|
const pendingToolCalls = /* @__PURE__ */ new Map();
|
|
126
683
|
for (const message of messages) {
|
|
@@ -191,7 +748,15 @@ var useLangGraphRuntime = ({
|
|
|
191
748
|
onSwitchToNewThread,
|
|
192
749
|
onSwitchToThread
|
|
193
750
|
}) => {
|
|
194
|
-
const {
|
|
751
|
+
const {
|
|
752
|
+
interrupt,
|
|
753
|
+
setInterrupt,
|
|
754
|
+
messages,
|
|
755
|
+
sendMessage,
|
|
756
|
+
cancel,
|
|
757
|
+
setMessages
|
|
758
|
+
} = useLangGraphMessages({
|
|
759
|
+
appendMessage: appendLangChainChunk,
|
|
195
760
|
stream
|
|
196
761
|
});
|
|
197
762
|
const [isRunning, setIsRunning] = useState2(false);
|
|
@@ -206,7 +771,7 @@ var useLangGraphRuntime = ({
|
|
|
206
771
|
}
|
|
207
772
|
};
|
|
208
773
|
const threadMessages = useExternalMessageConverter({
|
|
209
|
-
callback:
|
|
774
|
+
callback: convertLangChainMessages,
|
|
210
775
|
messages,
|
|
211
776
|
isRunning
|
|
212
777
|
});
|
|
@@ -217,8 +782,9 @@ var useLangGraphRuntime = ({
|
|
|
217
782
|
if (unstable_allowImageAttachments)
|
|
218
783
|
attachments = new SimpleImageAttachmentAdapter();
|
|
219
784
|
const switchToThread = !onSwitchToThread ? void 0 : async (externalId) => {
|
|
220
|
-
const { messages: messages2 } = await onSwitchToThread(externalId);
|
|
785
|
+
const { messages: messages2, interrupts } = await onSwitchToThread(externalId);
|
|
221
786
|
setMessages(messages2);
|
|
787
|
+
setInterrupt(interrupts?.[0]);
|
|
222
788
|
};
|
|
223
789
|
const threadList = {
|
|
224
790
|
threadId,
|
|
@@ -296,7 +862,10 @@ var useLangGraphRuntime = ({
|
|
|
296
862
|
});
|
|
297
863
|
};
|
|
298
864
|
export {
|
|
299
|
-
|
|
865
|
+
LangGraphMessageAccumulator,
|
|
866
|
+
appendLangChainChunk,
|
|
867
|
+
convertLangChainMessages,
|
|
868
|
+
convertLangChainMessages as convertLangchainMessages,
|
|
300
869
|
useLangGraphInterruptState,
|
|
301
870
|
useLangGraphMessages,
|
|
302
871
|
useLangGraphRuntime,
|