@vegintech/langchain-react-agent 0.0.32 → 0.0.34
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.mjs +47 -19
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -506,6 +506,46 @@ function useInterrupt({ interrupt, config, onSubmit }) {
|
|
|
506
506
|
//#endregion
|
|
507
507
|
//#region src/hooks/useToolExecution.ts
|
|
508
508
|
/**
|
|
509
|
+
* 根据工具执行结果构建 ToolMessage
|
|
510
|
+
*
|
|
511
|
+
* 支持两种模式:
|
|
512
|
+
* 1. 工具返回 { content, artifact } 结构,自动拆分为 ToolMessage 字段
|
|
513
|
+
* 2. 工具直接返回 ToolMessage(对象且 type === "tool"),透传使用
|
|
514
|
+
*/
|
|
515
|
+
function createToolMessage(callId, name, result, status, errorMessage) {
|
|
516
|
+
if (status === "error") return {
|
|
517
|
+
type: "tool",
|
|
518
|
+
content: `Error: ${errorMessage}`,
|
|
519
|
+
tool_call_id: callId,
|
|
520
|
+
name,
|
|
521
|
+
status
|
|
522
|
+
};
|
|
523
|
+
if (result !== null && typeof result === "object" && "type" in result && result.type === "tool") {
|
|
524
|
+
const toolMsg = result;
|
|
525
|
+
return {
|
|
526
|
+
...toolMsg,
|
|
527
|
+
tool_call_id: toolMsg.tool_call_id || callId,
|
|
528
|
+
name: toolMsg.name || name
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
let content;
|
|
532
|
+
let artifact;
|
|
533
|
+
if (result !== null && typeof result === "object" && "content" in result && "artifact" in result) {
|
|
534
|
+
const r = result;
|
|
535
|
+
content = typeof r.content === "string" ? r.content : JSON.stringify(r.content);
|
|
536
|
+
artifact = r.artifact;
|
|
537
|
+
} else content = typeof result === "string" ? result : JSON.stringify(result);
|
|
538
|
+
const message = {
|
|
539
|
+
type: "tool",
|
|
540
|
+
content,
|
|
541
|
+
tool_call_id: callId,
|
|
542
|
+
name,
|
|
543
|
+
status
|
|
544
|
+
};
|
|
545
|
+
if (artifact !== void 0) message.artifact = artifact;
|
|
546
|
+
return message;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
509
549
|
* useToolExecution - 管理前端工具执行的 Hook
|
|
510
550
|
*
|
|
511
551
|
* 职责:
|
|
@@ -527,6 +567,10 @@ function useToolExecution({ tools, toolCalls, isLoading = false, onExecutionChan
|
|
|
527
567
|
if (!completedToolResults || completedToolResults.size === 0) return;
|
|
528
568
|
completedToolResults.forEach((result, callId) => {
|
|
529
569
|
if (notifiedCompletedRef.current.has(callId)) return;
|
|
570
|
+
if (executedCallsRef.current.has(callId)) {
|
|
571
|
+
notifiedCompletedRef.current.add(callId);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
530
574
|
notifiedCompletedRef.current.add(callId);
|
|
531
575
|
executedCallsRef.current.add(callId);
|
|
532
576
|
const call = toolCalls.find((c) => c.id === callId);
|
|
@@ -573,11 +617,7 @@ function useToolExecution({ tools, toolCalls, isLoading = false, onExecutionChan
|
|
|
573
617
|
status: "success",
|
|
574
618
|
result
|
|
575
619
|
});
|
|
576
|
-
batchResultsRef.current.set(callId,
|
|
577
|
-
callId,
|
|
578
|
-
name: call.name,
|
|
579
|
-
result
|
|
580
|
-
});
|
|
620
|
+
batchResultsRef.current.set(callId, createToolMessage(callId, call.name, result, "success"));
|
|
581
621
|
} catch (error) {
|
|
582
622
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
583
623
|
onExecutionChange?.({
|
|
@@ -587,11 +627,7 @@ function useToolExecution({ tools, toolCalls, isLoading = false, onExecutionChan
|
|
|
587
627
|
status: "error",
|
|
588
628
|
error: errorMessage
|
|
589
629
|
});
|
|
590
|
-
batchResultsRef.current.set(callId,
|
|
591
|
-
callId,
|
|
592
|
-
name: call.name,
|
|
593
|
-
result: { error: errorMessage }
|
|
594
|
-
});
|
|
630
|
+
batchResultsRef.current.set(callId, createToolMessage(callId, call.name, null, "error", errorMessage));
|
|
595
631
|
} finally {
|
|
596
632
|
executingCallsRef.current.delete(callId);
|
|
597
633
|
executedCallsRef.current.add(callId);
|
|
@@ -947,15 +983,7 @@ const AgentChat = forwardRef(({ stream, className = "", tools, contexts, message
|
|
|
947
983
|
contexts,
|
|
948
984
|
agentState
|
|
949
985
|
]);
|
|
950
|
-
const handleToolResultsBatch = useCallback((
|
|
951
|
-
const toolMessages = results.map(({ callId, name, result }) => {
|
|
952
|
-
return {
|
|
953
|
-
type: "tool",
|
|
954
|
-
content: typeof result === "string" ? result : JSON.stringify(result),
|
|
955
|
-
tool_call_id: callId,
|
|
956
|
-
name
|
|
957
|
-
};
|
|
958
|
-
});
|
|
986
|
+
const handleToolResultsBatch = useCallback((toolMessages) => {
|
|
959
987
|
setTimeout(() => {
|
|
960
988
|
submitToStream(toolMessages);
|
|
961
989
|
}, 100);
|