MCPcopy Create free account
hub / github.com/GCWing/BitFun / useToolExecution

Function useToolExecution

src/web-ui/src/hooks/useToolExecution.ts:22–101  ·  view source on GitHub ↗
(
  options: UseToolExecutionOptions = {}
)

Source from the content-addressed store, hash-verified

20}
21
22export const useToolExecution = (
23 options: UseToolExecutionOptions = {}
24): UseToolExecutionReturn => {
25 const {
26 autoConnect = true,
27 eventTypes = ['all'],
28 maxMessages = 50
29 } = options;
30
31 const [toolMessages, setToolMessages] = useState<ToolDisplayMessage[]>([]);
32 const [activeExecutions, setActiveExecutions] = useState<ToolExecutionInfo[]>([]);
33
34 const handleToolEvent = useCallback((message: ToolDisplayMessage) => {
35 if (!message.id || message.id === 'tool_exec_undefined' || message.id === 'tool_result_undefined') {
36 log.warn('Ignoring invalid tool message', { messageId: message.id });
37 return;
38 }
39
40 setToolMessages(prev => {
41 const exists = prev.some(msg => msg.id === message.id);
42 if (exists) {
43 return prev;
44 }
45
46 const newMessages = [...prev, message];
47 if (newMessages.length > maxMessages) {
48 return newMessages.slice(-maxMessages);
49 }
50
51 return newMessages;
52 });
53
54 if (message.type === 'tool_use' || message.toolExecution) {
55 const service = ToolExecutionService.getInstance();
56 setActiveExecutions(service.getActiveExecutions());
57 }
58 }, [maxMessages]);
59
60 useEffect(() => {
61 if (!autoConnect) return;
62
63 const service = ToolExecutionService.getInstance();
64 const cleanupFunctions: (() => void)[] = [];
65
66 eventTypes.forEach(eventType => {
67 const cleanup = service.onToolEvent(eventType, handleToolEvent);
68 cleanupFunctions.push(cleanup);
69 });
70
71 setActiveExecutions(service.getActiveExecutions());
72
73 return () => {
74 cleanupFunctions.forEach(cleanup => cleanup());
75 };
76 // eslint-disable-next-line react-hooks/exhaustive-deps
77 }, [autoConnect]);
78
79 const clearToolMessages = useCallback(() => {

Callers

nothing calls this directly

Calls 7

warnMethod · 0.80
sliceMethod · 0.80
getActiveExecutionsMethod · 0.80
cleanupFunction · 0.50
getInstanceMethod · 0.45
onToolEventMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected