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 { useMemo, useRef, useState, useCallback, useEffect } from 'react';
import copy from 'copy-to-clipboard';
import { Button } from '@librechat/client';
import { PANE_COPY_REVEAL, TOOL_ROW_CLASSES, BareStatus } from '@librechat/chat/components';
import {
toolPanelSpacingClassName,
useToolContentPending,
useFollowScroll,
useToolCallIntent,
} from '@librechat/chat/react';
import {
parseBackgroundHandle,
splitBackgroundAttachments,
parseJsonField,
areToolCallArgsComplete,
parseCommandOutput,
} from '@librechat/chat';
import type { TAttachment, PartMetadata } from 'librechat-data-provider';
import ProgressText from '~/components/Chat/Messages/Content/ProgressText';
import { useMessagePartsHost } from '~/Providers/MessagePartsHostContext';
import CopyButton from '~/components/Messages/Content/CopyButton';
import LangIcon from '~/components/Messages/Content/LangIcon';
import useToolCallState from './useToolCallState';
import useLazyHighlight from './useLazyHighlight';
import { OutputRenderer } from '../ToolOutput';
import { ERROR_PATTERNS } from './ExecuteCode';
import { AttachmentGroup } from './Attachment';
import PtcToolTrace from './PtcToolTrace';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
/** The SDK's sandbox executors emit this line in place of empty stdout. */
const SANDBOX_EMPTY_OUTPUT = "stdout: Empty. Ensure you're writing output explicitly.";
export default function BashCall({
isSubmitting,
runStepStatus,
runStepDurationMs,
backgrounded,
backgroundCancelled = false,
executor,
initialProgress = 0.1,
args,
output = '',
attachments,
commandField = 'command',
hideAttachments = false,
onExpand,
toolCallId,
stepId,
}: {
initialProgress: number;
isSubmitting: boolean;
runStepStatus?: PartMetadata['runStepStatus'];
runStepDurationMs?: PartMetadata['runStepDurationMs'];
backgrounded?: PartMetadata['backgrounded'];
backgroundCancelled?: boolean;
executor?: PartMetadata['executor'];
args?: string | Record<string, unknown>;
output?: string;
attachments?: TAttachment[];
commandField?: string;
hideAttachments?: boolean;
onExpand?: () => void;
toolCallId?: string;
stepId?: string;
}) {
const localize = useLocalize();
const [showTimeoutDetails, setShowTimeoutDetails] = useState(false);
const command = useMemo(() => parseJsonField(args, commandField), [args, commandField]);
const isWritingCommand = !command || !areToolCallArgsComplete(args);
const { useSandboxStarting } = useMessagePartsHost();
const sandboxStarting = useSandboxStarting(toolCallId ?? '');
/** Only a call the server stamped as attached-workspace carries an exit
* status trailer; sandbox output keeps the text heuristic even when it
* prints something that looks like one. */
const result = useMemo(
() => (executor === 'attached_workspace' ? parseCommandOutput(output) : null),
[executor, output],
);
const outputHasError = useMemo(() => ERROR_PATTERNS.test(output), [output]);
const outputIsEmpty = output.trim() === SANDBOX_EMPTY_OUTPUT;
const verdict = (() => {
if (result?.timedOut === true) {
return localize('com_ui_command_timed_out');
}
if (result?.signal != null) {
return localize('com_ui_command_terminated', { 0: result.signal });
}
if (result?.failed === true && result.exitCode != null) {
return localize('com_ui_command_exit_code', { 0: String(result.exitCode) });
}
return undefined;
})();
const outputSegments = useMemo(
() =>
result == null
? undefined
: [
{ text: result.head },
{
text: result.stderr,
className: result.failed ? 'text-status-error' : 'text-text-secondary',
},
...(!result.timedOut || showTimeoutDetails
? [{ text: result.trailer, className: 'text-text-tertiary' }]
: []),
],
[result, showTimeoutDetails],
);
/** A backgrounded call's persisted output stays the dispatch handle until
* the detached run settles and patches it; render a background state
* instead of the handle JSON. Completion arrives live as the status marker
* attachment (also covers stdout-only runs) or as harvested files.
*
* Resolved before the phase, which folds `backgroundFailed` in: the
* detached task's outcome is this card's outcome, and the dispatch step's
* own output cannot express it. */
const backgroundHandle = useMemo(() => parseBackgroundHandle(output), [output]);
const { fileAttachments, backgroundStatus } = useMemo(
() => splitBackgroundAttachments(attachments, toolCallId),
[attachments, toolCallId],
);
const backgroundFailed = backgroundHandle != null && backgroundStatus === 'error';
const cancelledInBackground =
backgroundCancelled || (backgroundHandle != null && backgroundStatus === 'cancelled');
const backgroundFinishedText = backgroundHandle
? localize(
backgroundStatus != null || (fileAttachments?.length ?? 0) > 0
? 'com_ui_background_finished'
: 'com_ui_background_running',
)
: null;
/** The model-authored `intent` is the settled label too, and only the row
* renders it, so a call that carries one keeps its row. */
const intent = useToolCallIntent(args);
const { showCode, toggleCode, expandStyle, expandRef, phase, hasOutput, bare, rowRef } =
useToolCallState({
initialProgress,
isSubmitting,
output,
hasInput: !!command,
onExpand,
runStepStatus,
extraError: backgroundFailed || result?.failed === true,
extraCancelled: cancelledInBackground,
keepRow: backgroundHandle != null || intent != null,
});
const highlighted = useLazyHighlight(showCode ? command || undefined : undefined, 'bash');
const { ref: commandPaneRef, onScroll: onCommandPaneScroll } = useFollowScroll<HTMLDivElement>(
highlighted ?? command,
phase === 'running',
showCode,
);
const [isCopied, setIsCopied] = useState(false);
const timerRef = useRef<ReturnType<typeof setTimeout>>();
useEffect(() => () => clearTimeout(timerRef.current), []);
const contentPending = useToolContentPending();
const handleCopy = useCallback(() => {
if (contentPending) {
return;
}
setIsCopied(true);
copy(command, { format: 'text/plain' });
clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => setIsCopied(false), 3000);
}, [command, contentPending]);
/** The model-authored `intent` streams as the FIRST args key, so it is the
* live label from the earliest delta — before the command exists and while
* it runs. It persists as the settled label too (completion is a UI state,
* not a tense change); the generic texts are the no-intent fallback. */
const inProgressText = (() => {
if (intent != null) {
return intent;
}
if (isWritingCommand) {
return localize('com_ui_writing_command');
}
if (sandboxStarting) {
return localize('com_ui_sandbox_starting');
}
return localize('com_ui_running_command');
})();
const finishedText =
phase === 'cancelled'
? localize('com_ui_cancelled')
: (backgroundFinishedText ?? intent ?? localize('com_ui_command_finished'));
return (
<>
<BareStatus active={bare} text={finishedText} />
{!bare && (
<div
className={TOOL_ROW_CLASSES}
ref={rowRef}
data-task-tool-call={toolCallId}
data-task-step={stepId}
>
<ProgressText
phase={phase}
onClick={toggleCode}
inProgressText={inProgressText}
finishedText={finishedText}
/** A backgrounded call's run step closes when dispatch returns the
* handle, so its duration is the dispatch time, and showing it would
* misstate a detached task's runtime as seconds. The handle check
* covers the live card; the persisted `backgrounded` marker covers
* the card after harvest replaces the handle with real stdout
* (and after any reload), when no transient signal survives. */
durationMs={
backgroundHandle == null && backgrounded !== true ? runStepDurationMs : undefined
}
icon={
<LangIcon
lang="bash"
className={cn(
'text-text-secondary size-4 shrink-0',
phase === 'running' && 'animate-pulse',
)}
/>
}
hasInput={!!command || hasOutput}
isExpanded={showCode}
verdict={verdict}
/>
</div>
)}
<div style={expandStyle}>
<div className="overflow-hidden" ref={expandRef}>
<div
className={cn(
toolPanelSpacingClassName,
'border-border-light overflow-hidden rounded-lg border',
)}
>
{command && (
// The command is a code surface, so it takes the role every other one takes
// (`DiffView`, the user-turn code bars) instead of a palette shade a theme
// cannot reach: the previous dark-only gray-950 fill was Tailwind's blue-black,
// outside this palette entirely.
<div className="bg-surface-code group/copy relative">
<CopyButton
iconOnly
isCopied={isCopied}
onClick={handleCopy}
disabled={contentPending}
className={cn(
'bg-surface-code absolute inset-e-1.5 top-1 z-[1]',
PANE_COPY_REVEAL,
)}
label={localize('com_ui_copy_code')}
/>
<div
ref={commandPaneRef}
onScroll={onCommandPaneScroll}
className="max-h-[18.75rem] overflow-auto"
>
<pre
dir="ltr"
className="px-3 py-2.5 font-mono text-xs break-words whitespace-pre-wrap"
>
<span className="text-text-tertiary select-none" aria-hidden="true">
{'$ '}
</span>
{/* `code.hljs` in style.css sets `white-space: pre`, `word-wrap: normal`
and 0.85rem, which would stop long commands wrapping and size the
command larger than the `$` prompt. */}
<code className="hljs language-bash !text-xs !break-words !whitespace-pre-wrap">
{highlighted ?? command}
</code>
</pre>
</div>
</div>
)}
<PtcToolTrace
toolCallId={toolCallId}
expanded={showCode}
className={cn(command && 'border-border-inset border-t')}
/>
{hasOutput && backgroundHandle == null && (
<div className={cn('px-3 py-2.5', command && 'border-border-inset border-t')}>
{outputIsEmpty ? (
<p className="text-text-secondary text-xs italic">
{localize('com_ui_no_output')}
</p>
) : (
<OutputRenderer
text={output}
copyText={output}
error={result == null && outputHasError}
segments={outputSegments}
variant="terminal"
/>
)}
{result?.timedOut === true && (
<div className="mt-2 flex items-center justify-between gap-2 text-xs">
<span className="text-text-secondary">
{localize('com_ui_command_timeout_note')}
</span>
<Button
type="button"
variant="inline-link"
size="bare"
aria-expanded={showTimeoutDetails}
onClick={() => setShowTimeoutDetails((previous) => !previous)}
>
{localize(showTimeoutDetails ? 'com_ui_hide' : 'com_ui_details')}
</Button>
</div>
)}
</div>
)}
</div>
</div>
</div>
{!hideAttachments && fileAttachments && fileAttachments.length > 0 && (
<AttachmentGroup attachments={fileAttachments} />
)}
</>
);
}