Skip to content

Commit

Permalink
feat: adding stories for tool confirmation & better highlighting of r…
Browse files Browse the repository at this point in the history
…ules and links
  • Loading branch information
alashchev17 committed Jan 13, 2025
1 parent 5c1676a commit a5e81c3
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
52 changes: 52 additions & 0 deletions src/__fixtures__/confirmation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ToolConfirmationPauseReason } from "../services/refact";

export const CONFIRMATIONAL_PAUSE_REASONS_WITH_PATH: ToolConfirmationPauseReason[] =
[
{
command: "SELECT *",
rule: "*",
type: "confirmation",
tool_call_id: "1",
integr_config_path:
"\\\\?\\d:\\work\\refact.ai\\refact-lsp\\.refact\\integrations\\postgres.yaml",
},
];
export const CONFIRMATIONAL_PAUSE_REASONS: ToolConfirmationPauseReason[] = [
{
command: "patch",
rule: "default",
type: "confirmation",
tool_call_id: "1",
integr_config_path: null,
},
];

export const DENIAL_PAUSE_REASONS_WITH_PATH: ToolConfirmationPauseReason[] = [
{
command: "SELECT *",
rule: "*",
type: "denial",
tool_call_id: "1",
integr_config_path:
"\\\\?\\d:\\work\\refact.ai\\refact-lsp\\.refact\\integrations\\postgres.yaml",
},
];

export const MIXED_PAUSE_REASONS: ToolConfirmationPauseReason[] = [
{
command: "SELECT *",
rule: "*",
type: "denial",
tool_call_id: "1",
integr_config_path:
"\\\\?\\d:\\work\\refact.ai\\refact-lsp\\.refact\\integrations\\postgres.yaml",
},
{
command: "DROP *",
rule: "*",
type: "confirmation",
tool_call_id: "1",
integr_config_path:
"\\\\?\\d:\\work\\refact.ai\\refact-lsp\\.refact\\integrations\\postgres.yaml",
},
];
75 changes: 75 additions & 0 deletions src/components/ChatForm/ToolConfirmation.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from "react";
import type { Meta, StoryObj } from "@storybook/react";
import { ToolConfirmation } from "./ToolConfirmation";
import { Provider } from "react-redux";
import { setUpStore } from "../../app/store";
import { Theme } from "../Theme";
import { ToolConfirmationPauseReason } from "../../services/refact";
import { AbortControllerProvider } from "../../contexts/AbortControllers";
import {
CONFIRMATIONAL_PAUSE_REASONS,
CONFIRMATIONAL_PAUSE_REASONS_WITH_PATH,
DENIAL_PAUSE_REASONS_WITH_PATH,
MIXED_PAUSE_REASONS,
} from "../../__fixtures__/confirmation";

const MockedStore: React.FC<{
pauseReasons: ToolConfirmationPauseReason[];
}> = ({ pauseReasons }) => {
const store = setUpStore({
confirmation: {
pauseReasons,
pause: true,
status: {
wasInteracted: false,
confirmationStatus: false,
},
},
});

return (
<Provider store={store}>
<AbortControllerProvider>
<Theme accentColor="gray">
<ToolConfirmation pauseReasons={pauseReasons} />
</Theme>
</AbortControllerProvider>
</Provider>
);
};

const meta: Meta<typeof MockedStore> = {
title: "ToolConfirmation",
component: MockedStore,
args: {
pauseReasons: [],
},
};

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
pauseReasons: CONFIRMATIONAL_PAUSE_REASONS_WITH_PATH,
},
};

export const WithDenial: Story = {
args: {
pauseReasons: DENIAL_PAUSE_REASONS_WITH_PATH,
},
};

export const Patch: Story = {
args: {
pauseReasons: CONFIRMATIONAL_PAUSE_REASONS,
},
};

export const Mixed: Story = {
args: {
pauseReasons: MIXED_PAUSE_REASONS,
},
};
3 changes: 2 additions & 1 deletion src/components/ChatForm/ToolConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const ToolConfirmation: React.FC<ToolConfirmationProps> = ({
>{`${"```bash\n"}${command}${"\n```"}`}</Markdown>
))}
<Text className={styles.ToolConfirmationText}>
<Markdown>{message.concat("\n\n")}</Markdown>
<Markdown color="indigo">{message.concat("\n\n")}</Markdown>
{maybeIntegrationPath && (
<Text className={styles.ToolConfirmationText} mt="3">
You can modify the ruleset on{" "}
Expand All @@ -114,6 +114,7 @@ export const ToolConfirmation: React.FC<ToolConfirmationProps> = ({
}),
);
}}
color="indigo"
>
Configuration Page
</Link>
Expand Down
9 changes: 7 additions & 2 deletions src/components/Markdown/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { CSSProperties } from "react";
import SyntaxHighlighter, {
type SyntaxHighlighterProps,
} from "react-syntax-highlighter";
import { Code, Text } from "@radix-ui/themes";
import { Code, CodeProps, Text } from "@radix-ui/themes";
import classNames from "classnames";
import { PreTag, PreTagProps } from "./Pre";
// import "./highlightjs.css";
Expand All @@ -19,6 +19,7 @@ export type MarkdownCodeBlockProps = React.JSX.IntrinsicElements["code"] & {
node?: Element | undefined;
style?: Record<string, CSSProperties> | SyntaxHighlighterProps["style"];
wrap?: boolean;
color?: CodeProps["color"];
} & Pick<
SyntaxHighlighterProps,
"showLineNumbers" | "startingLineNumber" | "useInlineStyles"
Expand All @@ -31,6 +32,7 @@ const _MarkdownCodeBlock: React.FC<MarkdownCodeBlockProps> = ({
style = hljsStyle,
onCopyClick,
wrap = false,
color = undefined,
}) => {
const codeRef = React.useRef<HTMLElement | null>(null);
const match = /language-(\w+)/.exec(className ?? "");
Expand Down Expand Up @@ -76,7 +78,10 @@ const _MarkdownCodeBlock: React.FC<MarkdownCodeBlockProps> = ({
}

return (
<Code className={classNames(styles.code, styles.code_inline, className)}>
<Code
className={classNames(styles.code, styles.code_inline, className)}
color={color}
>
{children}
</Code>
);
Expand Down
13 changes: 9 additions & 4 deletions src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export type MarkdownProps = Pick<
> &
Pick<
MarkdownCodeBlockProps,
"startingLineNumber" | "showLineNumbers" | "useInlineStyles" | "style"
| "startingLineNumber"
| "showLineNumbers"
| "useInlineStyles"
| "style"
| "color"
> & {
canHaveInteractiveElements?: boolean;
wrap?: boolean;
Expand Down Expand Up @@ -237,6 +241,7 @@ const _Markdown: React.FC<MarkdownProps> = ({
allowedElements,
unwrapDisallowed,
canHaveInteractiveElements,
color,
...rest
}) => {
const components: Partial<Components> = useMemo(() => {
Expand All @@ -251,8 +256,8 @@ const _Markdown: React.FC<MarkdownProps> = ({
<ul {...props} className={classNames(styles.list, props.className)} />
);
},
code({ style: _style, ...props }) {
return <MarkdownCodeBlock {...props} {...rest} />;
code({ style: _style, color: _color, ...props }) {
return <MarkdownCodeBlock color={color} {...props} {...rest} />;
},
p({ color: _color, ref: _ref, node: _node, ...props }) {
if (canHaveInteractiveElements) {
Expand Down Expand Up @@ -329,7 +334,7 @@ const _Markdown: React.FC<MarkdownProps> = ({
return <Table.Cell {...props} />;
},
};
}, [rest, canHaveInteractiveElements]);
}, [rest, canHaveInteractiveElements, color]);
return (
<ReactMarkdown
className={styles.markdown}
Expand Down

0 comments on commit a5e81c3

Please sign in to comment.