@vegintech/langchain-react-agent 0.0.32 → 0.0.33
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 +43 -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
|
* 职责:
|
|
@@ -573,11 +613,7 @@ function useToolExecution({ tools, toolCalls, isLoading = false, onExecutionChan
|
|
|
573
613
|
status: "success",
|
|
574
614
|
result
|
|
575
615
|
});
|
|
576
|
-
batchResultsRef.current.set(callId,
|
|
577
|
-
callId,
|
|
578
|
-
name: call.name,
|
|
579
|
-
result
|
|
580
|
-
});
|
|
616
|
+
batchResultsRef.current.set(callId, createToolMessage(callId, call.name, result, "success"));
|
|
581
617
|
} catch (error) {
|
|
582
618
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
583
619
|
onExecutionChange?.({
|
|
@@ -587,11 +623,7 @@ function useToolExecution({ tools, toolCalls, isLoading = false, onExecutionChan
|
|
|
587
623
|
status: "error",
|
|
588
624
|
error: errorMessage
|
|
589
625
|
});
|
|
590
|
-
batchResultsRef.current.set(callId,
|
|
591
|
-
callId,
|
|
592
|
-
name: call.name,
|
|
593
|
-
result: { error: errorMessage }
|
|
594
|
-
});
|
|
626
|
+
batchResultsRef.current.set(callId, createToolMessage(callId, call.name, null, "error", errorMessage));
|
|
595
627
|
} finally {
|
|
596
628
|
executingCallsRef.current.delete(callId);
|
|
597
629
|
executedCallsRef.current.add(callId);
|
|
@@ -947,15 +979,7 @@ const AgentChat = forwardRef(({ stream, className = "", tools, contexts, message
|
|
|
947
979
|
contexts,
|
|
948
980
|
agentState
|
|
949
981
|
]);
|
|
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
|
-
});
|
|
982
|
+
const handleToolResultsBatch = useCallback((toolMessages) => {
|
|
959
983
|
setTimeout(() => {
|
|
960
984
|
submitToStream(toolMessages);
|
|
961
985
|
}, 100);
|