Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
import { useCallback, useContext } from 'react';
import {
SoleToolContext,
LoneGroupContext,
useToolExpansion,
useToolContentRequest,
useRowHandoff,
} from '@librechat/chat/react';
import type { PartMetadata } from 'librechat-data-provider';
import type { ToolCallPhase } from '~/utils/toolCallPhase';
import { isError } from '~/components/Chat/Messages/Content/ToolOutput';
import { resolveToolCallPhase } from '~/utils/toolCallPhase';
import { useProgress, useExpandCollapse } from '~/hooks';
interface ToolCallState {
showCode: boolean;
toggleCode: () => void;
expandStyle: React.CSSProperties;
expandRef: React.RefObject<HTMLDivElement>;
/**
* The card's settled state, resolved once. Label, announcement, icon and
* animation all read this — deriving any of them separately is what let
* them disagree.
*/
phase: ToolCallPhase;
hasOutput: boolean;
hasContent: boolean;
/** Goes on the card's row so focus can follow it out. */
rowRef: (node: HTMLElement | null) => void;
/** The only call of its group, with a panel to show: the group header is
* already the row, so the card renders its panel alone, held open. */
bare: boolean;
}
export interface UseToolCallStateInput {
initialProgress: number;
isSubmitting: boolean;
output: string;
hasInput: boolean;
onExpand?: () => void;
runStepStatus?: PartMetadata['runStepStatus'];
/**
* A failure this call's own output cannot express — currently a
* backgrounded task that settled as `error`, where the dispatch step's
* output is the handle rather than the task's result. Folded into the
* phase so those cards resolve their state through the same path as
* every other card instead of patching the result afterwards.
*/
extraError?: boolean;
/** A terminal verdict carried outside the dispatch run step, such as a
* cancelled ordinary background tool. Cancellation outranks error-shaped
* output so the card never relabels an intentional stop as failure. */
extraCancelled?: boolean;
/** Keep the card's own row even as the only call, for a state its phase does
* not carry (a detached task still running after its dispatch step closed). */
keepRow?: boolean;
/** Whether the panel has a body to show. Defaults to `hasContent`; a card
* whose panel needs more than its input (a read file's output) passes its
* own, so a sole call never loses its row to an empty panel. */
panelReady?: boolean;
}
export default function useToolCallState({
initialProgress,
isSubmitting,
output,
hasInput,
onExpand,
runStepStatus,
extraError = false,
extraCancelled = false,
keepRow = false,
panelReady,
}: UseToolCallStateInput): ToolCallState {
const hasOutput = output.length > 0;
const hasContent = hasInput || hasOutput;
const isClosed = runStepStatus != null;
/**
* Passing 1 in for a closed step stops `useProgress` scheduling its 200ms
* interval, which it keeps alive for any input below 1. The result no
* longer needs masking: the phase treats an explicit close as terminal
* outright, so a step that closes while the hook is still settling through
* 0.99 can no longer read as in-flight.
*/
const rawProgress = useProgress(isClosed ? 1 : initialProgress);
/**
* One resolution; everything the card shows is a read of this value. The
* two progress inputs are deliberately distinct — see `resolveToolCallPhase`
* for why the cancellation inference must not read the animated one.
*/
const phase = resolveToolCallPhase({
runStepStatus: extraCancelled ? 'cancelled' : runStepStatus,
displayProgress: rawProgress,
reportedProgress: initialProgress,
isSubmitting,
hasError: (hasOutput && isError(output)) || extraError,
});
/** The only call of its group, settled successfully: the group header is
* already the row, so the card renders its panel alone, held open. While it
* runs, or once it failed or was stopped, the row is the only place that
* says so, and `keepRow` covers a state the phase cannot express. */
const lone = useContext(LoneGroupContext);
const solo = useContext(SoleToolContext) === true || lone;
const bare = solo && (panelReady ?? hasContent) && phase === 'completed' && !keepRow;
const [expanded, setExpansionOverride] = useToolExpansion(hasContent);
const showCode = bare || expanded;
/** A bare panel is open without the reader expanding it, so it asks for the stored part too. */
useToolContentRequest(bare);
const { style: expandStyle, ref: expandRef } = useExpandCollapse(showCode);
const rowRef = useRowHandoff(bare);
const toggleCode = useCallback(() => {
const next = !showCode;
setExpansionOverride(next);
if (next) {
onExpand?.();
}
}, [onExpand, setExpansionOverride, showCode]);
return {
showCode,
toggleCode,
expandStyle,
expandRef,
phase,
hasOutput,
hasContent,
rowRef,
bare,
};
}