Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
refactor(markdown-editor): extract MarkdownEditorField, own lazy() + raw/rich switch
Sergey Kozyrenkoe9551f767ff5
diff --git a/frontend/src/components/shared/markdown-editor/index.ts b/frontend/src/components/shared/markdown-editor/index.tsindex d8b2cab5..99a50f00 100644--- a/frontend/src/components/shared/markdown-editor/index.ts+++ b/frontend/src/components/shared/markdown-editor/index.ts@@ -1,7 +1,8 @@-// Deliberately does NOT re-export the heavy MarkdownEditor value — that would statically pull the tiptap chunk-// into any route importing a light util from here, defeating the lazy(MarkdownEditor) in settings-prompt /-// template. Import the component directly from './markdown-editor'. Only its type (erased) is safe to re-export.export type { MarkdownEditorHandle } from './markdown-editor';+// Deliberately does NOT re-export the heavy MarkdownEditor value — that would statically pull the tiptap chunk+// into any route importing a light util from here. Consume the mode-switching MarkdownEditorField instead (it+// owns the lazy() boundary, so importing it is chunk-free until rich mode renders).+export { MarkdownEditorField } from './markdown-editor-field';export { findVariableUseRanges, VARIABLE_RE, variableProbe } from './markdown-editor-variable-syntax';export { EditorViewModeToggle } from './markdown-editor-view-mode';export type { EditorViewMode } from './markdown-editor-view-mode';diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsxnew file mode 100644index 00000000..ae97327a--- /dev/null+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-field.tsx@@ -0,0 +1,91 @@+import type { AriaAttributes, ReactNode, Ref } from 'react';++import { Loader2 } from 'lucide-react';+import { lazy, Suspense } from 'react';++import { Textarea } from '@/components/ui/textarea';+import { cn } from '@/lib/utils';++import type { MarkdownEditorHandle } from './markdown-editor';+import type { EditorViewMode } from './markdown-editor-view-mode';++// Static-importing MarkdownEditor would merge the tiptap chunk into every route that pulls a util from the+// barrel; the lazy() boundary is what lets the barrel re-export this component eagerly.+const MarkdownEditor = lazy(() => import('./markdown-editor').then((module) => ({ default: module.MarkdownEditor })));++interface MarkdownEditorFieldProps extends Pick<AriaAttributes, 'aria-describedby' | 'aria-invalid'> {+ // `className` styles the rich wrapper, `rawClassName` the raw <textarea>. The byte-exact font-mono /+ // no-resize config is baked in — raw mode is a source editor, so callers must not override it.+ className?: string;+ contentClassName?: string;+ disabled?: boolean;+ fallback?: ReactNode;+ id?: string;+ mode: EditorViewMode;+ onBlur?: () => void;+ onChange: (value: string) => void;+ placeholder?: string;+ rawClassName?: string;+ ref?: Ref<MarkdownEditorHandle>;+ value: string;+}++export function MarkdownEditorField({+ 'aria-describedby': ariaDescribedby,+ 'aria-invalid': ariaInvalid,+ className,+ contentClassName,+ disabled,+ fallback,+ id,+ mode,+ onBlur,+ onChange,+ placeholder,+ rawClassName,+ ref,+ value,+}: MarkdownEditorFieldProps) {+ if (mode === 'raw') {+ return (+ <Textarea+ aria-describedby={ariaDescribedby}+ aria-invalid={ariaInvalid}+ autoSize={false}+ className={cn('resize-none font-mono text-sm', rawClassName)}+ disabled={disabled}+ id={id}+ onBlur={onBlur}+ onChange={(event) => onChange(event.target.value)}+ placeholder={placeholder}+ value={value}+ />+ );+ }++ return (+ <Suspense+ fallback={+ fallback ?? (+ <div className={cn('flex items-center justify-center rounded-md border', className)}>+ <Loader2 className="text-muted-foreground size-5 animate-spin" />+ </div>+ )+ }+ >+ <MarkdownEditor+ aria-describedby={ariaDescribedby}+ aria-invalid={ariaInvalid}+ className={className}+ contentClassName={contentClassName}+ disabled={disabled}+ id={id}+ onBlur={onBlur}+ onChange={onChange}+ placeholder={placeholder}+ ref={ref}+ value={value}+ />+ </Suspense>+ );+}diff --git a/frontend/src/features/knowledges/knowledge-form-controls.tsx b/frontend/src/features/knowledges/knowledge-form-controls.tsxindex b0c1f9eb..03ade62e 100644--- a/frontend/src/features/knowledges/knowledge-form-controls.tsx+++ b/frontend/src/features/knowledges/knowledge-form-controls.tsx@@ -5,8 +5,7 @@ import type {KnowledgeGuideType as KnowledgeGuideTypeT,} from '@/graphql/types';-import { type EditorViewMode } from '@/components/shared/markdown-editor';-import { MarkdownEditor } from '@/components/shared/markdown-editor/markdown-editor';+import { type EditorViewMode, MarkdownEditorField } from '@/components/shared/markdown-editor';import {Autocomplete,AutocompleteContent,@@ -18,9 +17,7 @@ import {import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';import { InputGroup, InputGroupTextareaAutosize } from '@/components/ui/input-group';import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';-import { Textarea } from '@/components/ui/textarea';import { KnowledgeAnswerType, KnowledgeDocType, KnowledgeGuideType } from '@/graphql/types';-import { cn } from '@/lib/utils';import type { FormValues } from './knowledge-form';@@ -101,30 +98,17 @@ export function KnowledgeContentField({<FormItem className={fillParent ? 'flex min-h-0 flex-1 flex-col' : undefined}>{hasLabel ? <FormLabel>Content</FormLabel> : null}<FormControl>- {viewMode === 'raw' ? (- <Textarea- autoSize={false}- className={cn(- 'resize-none font-mono text-sm',- fillParent ? 'min-h-0 flex-1' : 'min-h-[280px]',- )}- disabled={isSaving}- onBlur={field.onBlur}- onChange={field.onChange}- placeholder="Knowledge content (will be embedded into the vector store)"- value={field.value}- />- ) : (- <MarkdownEditor- className={fillParent ? 'min-h-0 flex-1' : 'min-h-[280px]'}- contentClassName={fillParent ? undefined : 'min-h-[240px]'}- disabled={isSaving}- onBlur={field.onBlur}- onChange={field.onChange}- placeholder="Knowledge content (will be embedded into the vector store)"- value={field.value}- />- )}+ <MarkdownEditorField+ className={fillParent ? 'min-h-0 flex-1' : 'min-h-[280px]'}+ contentClassName={fillParent ? undefined : 'min-h-[240px]'}+ disabled={isSaving}+ mode={viewMode}+ onBlur={field.onBlur}+ onChange={field.onChange}+ placeholder="Knowledge content (will be embedded into the vector store)"+ rawClassName={fillParent ? 'min-h-0 flex-1' : 'min-h-[280px]'}+ value={field.value}+ /></FormControl><FormMessage /></FormItem>diff --git a/frontend/src/pages/settings/settings-prompt.tsx b/frontend/src/pages/settings/settings-prompt.tsxindex 3a6bd048..f0b14870 100644--- a/frontend/src/pages/settings/settings-prompt.tsx+++ b/frontend/src/pages/settings/settings-prompt.tsx@@ -16,17 +16,7 @@ import {Wrench,XCircle,} from 'lucide-react';-import {- type ComponentProps,- lazy,- type Ref,- Suspense,- useCallback,- useEffect,- useMemo,- useRef,- useState,-} from 'react';+import { type ComponentProps, type Ref, useCallback, useEffect, useMemo, useRef, useState } from 'react';import ReactDiffViewer from 'react-diff-viewer-continued';import {type Control,@@ -51,6 +41,7 @@ import {type EditorViewMode,EditorViewModeToggle,findVariableUseRanges,+ MarkdownEditorField,type MarkdownEditorHandle,VARIABLE_RE,variableProbe,@@ -80,11 +71,10 @@ import {DropdownMenuSeparator,DropdownMenuTrigger,} from '@/components/ui/dropdown-menu';-import { Form, FormControl, FormItem, FormLabel, FormMessage } from '@/components/ui/form';+import { Form, FormControl, FormItem, FormMessage } from '@/components/ui/form';import { FormSubmitButton } from '@/components/ui/form-submit-button';import { StatusCard } from '@/components/ui/status-card';import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';-import { Textarea } from '@/components/ui/textarea';import {CreatePromptDocument,DeletePromptDocument,@@ -94,14 +84,6 @@ import {} from '@/graphql/types';import { useBreakpoint } from '@/hooks/use-breakpoint';import { formatPromptId } from '@/lib/route-titles/format-prompt-id';-import { cn } from '@/lib/utils';--// Dynamic-only import: a static import would merge the tiptap editor chunk into this route bundle.-const MarkdownEditor = lazy(() =>- import('@/components/shared/markdown-editor/markdown-editor').then((module) => ({- default: module.MarkdownEditor,- })),-);const systemFormSchema = z.object({template: z.string().min(1, 'System template is required'),@@ -111,29 +93,15 @@ const humanFormSchema = z.object({template: z.string().min(1, 'Human template is required'),});-interface BaseFieldProps<T extends FieldValues> extends ControllerProps<T> {- label?: string;-}-interface BaseTextareaProps {- className?: string;- placeholder?: string;-}--interface ControllerProps<T extends FieldValues> {+interface FormMarkdownItemProps<T extends FieldValues> {control: Control<T>;disabled?: boolean;- name: FieldPathByValue<T, string>;-}--interface FormCodeItemProps<T extends FieldValues> extends ControllerProps<T> {editorRef?: Ref<MarkdownEditorHandle>;+ mode: EditorViewMode;+ name: FieldPathByValue<T, string>;placeholder?: string;}-interface FormTextareaItemProps<T extends FieldValues> extends BaseFieldProps<T>, BaseTextareaProps {- description?: string;-}-type HumanFormData = z.infer<typeof humanFormSchema>;type SystemFormData = z.infer<typeof systemFormSchema>;@@ -234,13 +202,14 @@ const diffStyles = {},} satisfies ComponentProps<typeof ReactDiffViewer>['styles'];-function FormCodeItem<T extends FieldValues>({+function FormMarkdownItem<T extends FieldValues>({control,disabled,editorRef,+ mode,name,placeholder,-}: FormCodeItemProps<T>) {+}: FormMarkdownItemProps<T>) {const { field, fieldState } = useController({control,disabled,@@ -250,53 +219,21 @@ function FormCodeItem<T extends FieldValues>({return (<FormItem className="flex min-h-0 flex-1 flex-col"><FormControl>- <Suspense+ <MarkdownEditorField+ className="min-h-0 flex-1"+ disabled={disabled}fallback={<div className="flex min-h-0 flex-1 items-center justify-center rounded-md border"><Loader2 className="text-muted-foreground size-5 animate-spin" /></div>}- >- <MarkdownEditor- className="min-h-0 flex-1"- disabled={disabled}- onBlur={field.onBlur}- onChange={field.onChange}- placeholder={placeholder}- ref={editorRef}- value={field.value}- />- </Suspense>- </FormControl>- {fieldState.error && <FormMessage>{fieldState.error.message}</FormMessage>}- </FormItem>- );-}--function FormTextareaItem<T extends FieldValues>({- className,- control,- disabled,- label,- name,- placeholder,-}: FormTextareaItemProps<T>) {- const { field, fieldState } = useController({- control,- disabled,- name,- });-- return (- <FormItem className="flex min-h-0 flex-1 flex-col">- {label && <FormLabel>{label}</FormLabel>}- <FormControl>- <Textarea- {...field}- autoSize={false}- className={cn('min-h-[640px] flex-1 resize-none font-mono text-sm', className)}- disabled={disabled}+ mode={mode}+ onBlur={field.onBlur}+ onChange={field.onChange}placeholder={placeholder}+ rawClassName="min-h-[640px] flex-1"+ ref={editorRef}+ value={field.value}/></FormControl>{fieldState.error && <FormMessage>{fieldState.error.message}</FormMessage>}@@ -1025,22 +962,14 @@ function SettingsPrompt() {id="system-prompt-form"onSubmit={systemForm.handleSubmit(handleSystemSubmit)}>- {viewMode === 'rich' ? (- <FormCodeItem- control={systemForm.control}- disabled={isLoading}- editorRef={editorRef}- name="template"- placeholder={systemPlaceholder}- />- ) : (- <FormTextareaItem- control={systemForm.control}- disabled={isLoading}- name="template"- placeholder={systemPlaceholder}- />- )}+ <FormMarkdownItem+ control={systemForm.control}+ disabled={isLoading}+ editorRef={editorRef}+ mode={viewMode}+ name="template"+ placeholder={systemPlaceholder}+ /></form></Form></TabsContent>@@ -1056,22 +985,14 @@ function SettingsPrompt() {id="human-prompt-form"onSubmit={humanForm.handleSubmit(handleHumanSubmit)}>- {viewMode === 'rich' ? (- <FormCodeItem- control={humanForm.control}- disabled={isLoading}- editorRef={editorRef}- name="template"- placeholder="Enter the human prompt template..."- />- ) : (- <FormTextareaItem- control={humanForm.control}- disabled={isLoading}- name="template"- placeholder="Enter the human prompt template..."- />- )}+ <FormMarkdownItem+ control={humanForm.control}+ disabled={isLoading}+ editorRef={editorRef}+ mode={viewMode}+ name="template"+ placeholder="Enter the human prompt template..."+ /></form></Form></TabsContent>diff --git a/frontend/src/pages/templates/template.tsx b/frontend/src/pages/templates/template.tsxindex cd25d5e8..4d4c0d0b 100644--- a/frontend/src/pages/templates/template.tsx+++ b/frontend/src/pages/templates/template.tsx@@ -3,7 +3,7 @@ import type { ReactNode } from 'react';import { skipToken, useQuery } from '@apollo/client/react';import { zodResolver } from '@hookform/resolvers/zod';import { ChevronDown, Ellipsis, FileSymlink, FileText, Loader2, Pencil, Save, Trash } from 'lucide-react';-import { lazy, Suspense, useCallback, useMemo, useState } from 'react';+import { useCallback, useMemo, useState } from 'react';import { useForm } from 'react-hook-form';import { useNavigate, useParams } from 'react-router-dom';import { toast } from 'sonner';@@ -18,7 +18,7 @@ import {} from '@/components/shared/detail-navigation';import { DetailTwoPanelLayout } from '@/components/shared/detail-two-panel-layout';import { InlineEditInput, useInlineEdit } from '@/components/shared/inline-edit';-import { type EditorViewMode, EditorViewModeToggle } from '@/components/shared/markdown-editor';+import { type EditorViewMode, EditorViewModeToggle, MarkdownEditorField } from '@/components/shared/markdown-editor';import { UnsavedChangesDialog, useUnsavedChangesGuard } from '@/components/shared/unsaved-changes';import { Badge } from '@/components/ui/badge';import { Breadcrumb, BreadcrumbItem, BreadcrumbList, BreadcrumbPage } from '@/components/ui/breadcrumb';@@ -36,7 +36,6 @@ import { Form, FormControl, FormField, FormItem } from '@/components/ui/form';import { FormSubmitButton } from '@/components/ui/form-submit-button';import { Input } from '@/components/ui/input';import { Spinner } from '@/components/ui/spinner';-import { Textarea } from '@/components/ui/textarea';import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';import { useTemplateDetailNavigation } from '@/features/templates/use-template-detail-navigation';import { FlowTemplateDocument } from '@/graphql/types';@@ -45,12 +44,6 @@ import { routes } from '@/lib/routes';import { cn } from '@/lib/utils';import { type Template, useTemplates } from '@/providers/templates-provider';-const MarkdownEditor = lazy(() =>- import('@/components/shared/markdown-editor/markdown-editor').then((module) => ({- default: module.MarkdownEditor,- })),-);-const formSchema = z.object({text: z.string().trim().min(1, { message: 'Text is required' }),title: z.string().trim().min(1, { message: 'Title is required' }),@@ -680,32 +673,21 @@ function Template() {render={({ field }) => (<FormItem className="flex min-h-0 flex-1 flex-col"><FormControl>- {viewMode === 'rich' ? (- <Suspense- fallback={- <div className="flex min-h-0 flex-1 items-center justify-center rounded-md border">- <Spinner variant="circle" />- </div>- }- >- <MarkdownEditor- className="min-h-0 flex-1"- disabled={isSaving}- onBlur={field.onBlur}- onChange={field.onChange}- placeholder="Content"- value={field.value}- />- </Suspense>- ) : (- <Textarea- {...field}- autoSize={false}- className="min-h-[640px] flex-1 resize-none font-mono text-sm"- disabled={isSaving}- placeholder="Content"- />- )}+ <MarkdownEditorField+ className="min-h-0 flex-1"+ disabled={isSaving}+ fallback={+ <div className="flex min-h-0 flex-1 items-center justify-center rounded-md border">+ <Spinner variant="circle" />+ </div>+ }+ mode={viewMode}+ onBlur={field.onBlur}+ onChange={field.onChange}+ placeholder="Content"+ rawClassName="min-h-[640px] flex-1"+ value={field.value}+ /></FormControl></FormItem>)}