Skip to content

Commit cdaac7b

Browse files
committed
fix(web): run only complete chat shell fences
1 parent 183e6fa commit cdaac7b

3 files changed

Lines changed: 52 additions & 17 deletions

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,25 @@ describe("ChatMarkdown streaming", () => {
210210
});
211211
expect(onRunShellCommand).toHaveBeenCalledExactlyOnceWith("echo hello");
212212

213+
for (const text of [
214+
"~~~bash\necho tilde\n~~~",
215+
"> ```bash\n> echo quote\n> ```",
216+
"````bash\necho four\n````",
217+
]) {
218+
await act(async () => {
219+
mounted.update(message(text));
220+
});
221+
expect(codeButton(mounted, "Run in terminal")).toBeDefined();
222+
}
223+
213224
for (const text of [
214225
"```bash\necho one\necho two\n```",
215226
"```typescript\necho hello\n```",
216227
"```bash\n\n```",
228+
"```bash\necho incomplete",
229+
"~~~bash\necho incomplete",
230+
"````bash\necho incomplete\n```",
231+
'<pre><code class="language-bash">echo html</code></pre>',
217232
]) {
218233
await act(async () => {
219234
mounted.update(message(text));

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ function extractPreCodeMeta(node: unknown): string | undefined {
585585
return typeof meta === "string" && meta.trim().length > 0 ? meta.trim() : undefined;
586586
}
587587

588+
function isClosedCodeFence(node: ReactMarkdownExtraProps["node"], text: string): boolean {
589+
const start = node?.position?.start.offset;
590+
const end = node?.position?.end.offset;
591+
if (start === undefined || end === undefined) return false;
592+
const source = text.slice(start, end);
593+
const opening = /^(?:`{3,}|~{3,})/.exec(source)?.[0];
594+
const closing = /(?:^|\n)(?:[ \t]*>[ \t]*)*[ \t]*(`{3,}|~{3,})[ \t\r]*$/.exec(source)?.[1];
595+
return (
596+
opening !== undefined &&
597+
closing !== undefined &&
598+
opening[0] === closing[0] &&
599+
closing.length >= opening.length
600+
);
601+
}
602+
588603
type MarkdownAstNode = {
589604
type?: string;
590605
meta?: unknown;
@@ -3273,7 +3288,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
32733288
return <MarkdownDetails open={detailsOpen}>{children}</MarkdownDetails>;
32743289
},
32753290
pre: function MarkdownPre({ node, children, ...props }) {
3276-
const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand } = use(
3291+
const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand, text } = use(
32773292
ChatMarkdownRendererContext,
32783293
);
32793294
const codeBlock = extractCodeBlock(children);
@@ -3289,7 +3304,11 @@ const CHAT_MARKDOWN_COMPONENTS = {
32893304
language={language}
32903305
fenceTitle={fenceTitle}
32913306
theme={resolvedTheme}
3292-
onRunShellCommand={onRunShellCommand}
3307+
onRunShellCommand={
3308+
onRunShellCommand && !isStreaming && isClosedCodeFence(node, text)
3309+
? onRunShellCommand
3310+
: undefined
3311+
}
32933312
isStreaming={isStreaming}
32943313
>
32953314
<RenderErrorBoundary

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,21 +4309,22 @@ export default function ChatView(props: ChatViewProps) {
43094309
],
43104310
);
43114311

4312-
const runShellCommand = useCallback(
4313-
(command: string) => {
4314-
void runProjectScript(
4315-
{
4316-
id: "chat-code-block",
4317-
name: "Chat code block",
4318-
command,
4319-
icon: "play",
4320-
runOnWorktreeCreate: false,
4321-
},
4322-
{ rememberAsLastInvoked: false },
4323-
);
4324-
},
4325-
[runProjectScript],
4326-
);
4312+
const runProjectScriptRef = useRef(runProjectScript);
4313+
useLayoutEffect(() => {
4314+
runProjectScriptRef.current = runProjectScript;
4315+
}, [runProjectScript]);
4316+
const runShellCommand = useCallback((command: string) => {
4317+
void runProjectScriptRef.current(
4318+
{
4319+
id: "chat-code-block",
4320+
name: "Chat code block",
4321+
command,
4322+
icon: "play",
4323+
runOnWorktreeCreate: false,
4324+
},
4325+
{ rememberAsLastInvoked: false },
4326+
);
4327+
}, []);
43274328

43284329
const supportsProjectSettingsOverrides =
43294330
environmentById.get(environmentId)?.serverConfig?.environment.capabilities

0 commit comments

Comments
 (0)