Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

1.1 KB
Raw Blame
import { RefObject, useCallback } from 'react';

import { TOOL_CALL_ID_ATTR } from './utils';

import styles from './AIThreadMessage.module.scss';

const HIGHLIGHT_MS = 1500;

interface ToolCallLinks {
	canLinkToToolCall: (toolCallKey: string) => boolean;
	onToolLinkClick: (toolCallKey: string) => void;
}

/** Links tool results to their calls in `rootRef`; enabled only for keys in `toolCallKeys`. */
export function useToolCallLinks(
	rootRef: RefObject<HTMLElement>,
	toolCallKeys: Set<string>,
): ToolCallLinks {
	const canLinkToToolCall = useCallback(
		(toolCallKey: string): boolean => toolCallKeys.has(toolCallKey),
		[toolCallKeys],
	);

	const onToolLinkClick = useCallback(
		(toolCallKey: string): void => {
			const target = rootRef.current?.querySelector<HTMLElement>(
				`[${TOOL_CALL_ID_ATTR}="${CSS.escape(toolCallKey)}"]`,
			);
			if (!target) {
				return;
			}
			target.scrollIntoView({ behavior: 'smooth', block: 'center' });
			target.classList.add(styles.isHighlighted);
			window.setTimeout(
				() => target.classList.remove(styles.isHighlighted),
				HIGHLIGHT_MS,
			);
		},
		[rootRef],
	);

	return { canLinkToToolCall, onToolLinkClick };
}