添加工具调用解析和聊天工具操作处理
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import {
|
||||
useChatToolStore,
|
||||
type ChatToolAction,
|
||||
} from "@/store/chatToolStore";
|
||||
|
||||
/**
|
||||
* Subscribe to chat tool actions and invoke `handler` for each new action.
|
||||
*
|
||||
* Usage (inside a component with map access):
|
||||
* ```ts
|
||||
* useChatToolActionHandler((action) => {
|
||||
* switch (action.type) {
|
||||
* case "locate_nodes": handleLocateNodes(action.ids); break;
|
||||
* case "locate_pipes": handleLocatePipes(action.ids); break;
|
||||
* case "view_history": openHistoryPanel(action.featureInfos, action.dataType); break;
|
||||
* case "view_scada": openScadaPanel(action.featureInfos); break;
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function useChatToolActionHandler(
|
||||
handler: (action: ChatToolAction) => void,
|
||||
) {
|
||||
const handlerRef = useRef(handler);
|
||||
handlerRef.current = handler;
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = useChatToolStore.subscribe(
|
||||
(state, prevState) => {
|
||||
if (
|
||||
state.actionSeq !== prevState.actionSeq &&
|
||||
state.lastAction
|
||||
) {
|
||||
handlerRef.current(state.lastAction);
|
||||
}
|
||||
},
|
||||
);
|
||||
return unsubscribe;
|
||||
}, []);
|
||||
}
|
||||
Reference in New Issue
Block a user