Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

feat(prompts): add byte-faithful code editor view for prompt templates

Sergey Kozyrenko65a037b91af2
diff --git a/frontend/package.json b/frontend/package.json
index a7c6ed0a..6a79c0a5 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -18,6 +18,7 @@
},
"dependencies": {
"@apollo/client": "^4.2.3",
+ "@codemirror/lang-markdown": "^6.5.0",
"@graphql-typed-document-node/core": "^3.2.0",
"@hookform/resolvers": "^5.4.0",
"@radix-ui/react-accordion": "^1.2.14",
@@ -48,6 +49,7 @@
"@tiptap/pm": "^3.27.0",
"@tiptap/react": "^3.27.0",
"@tiptap/starter-kit": "^3.27.0",
+ "@uiw/react-codemirror": "^4.25.10",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-search": "^0.16.0",
"@xterm/addon-unicode11": "^0.9.0",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 9e0a6588..40c3f057 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -11,6 +11,9 @@ importers:
'@apollo/client':
specifier: ^4.2.3
+ '@codemirror/lang-markdown':
+ specifier: ^6.5.0
+ version: 6.5.0
'@graphql-typed-document-node/core':
specifier: ^3.2.0
version: 3.2.0([email protected])
@@ -101,6 +104,9 @@ importers:
'@tiptap/starter-kit':
specifier: ^3.27.0
version: 3.27.0
+ '@uiw/react-codemirror':
+ specifier: ^4.25.10
'@xterm/addon-fit':
specifier: ^0.11.0
version: 0.11.0
@@ -471,6 +477,42 @@ packages:
resolution: {integrity: sha512-ctxtJ/eA+t+6q2++vj5j7FYX3nRu311q1wfYH3xjlLOsczhlhxAg2FWNUXhpGvAw3BWo1xBcvOV6/YLc2r5FJw==}
hasBin: true
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-tlosUqb+3BbxCxZdu4tKeRghPFC+QM7q4X5YhKV2eCmPG+1r2F3f4AaSz5sCrFqUtX4Jh20VFTKecl16MgiV9g==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-Ryk9y9T0FFVF0cUGhAknveAyUOl/A1qReTFi+qPKtOh2Z9F4AUBz3XOrYD4ZEgZirdugVzHvd/2/Wcwy5OliTg==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-kr5fwBGiGtmz6l0LSJIbno9QrifNMUusivHbnA1H6Dmqy4HZFte3UAICix1VuKo0lMPKQr2rqB+0BkKi/S3Ejg==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-9NsXp7Nwp891pQchI7gPdTwBuSuT3K65NGTHWHNJ55HjYcHLllr0rbIZNdOzas9ztc1EUVBlHou85FFZS4BNnw==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-zD4e5mS+50htS7F+TYjBPsiIFGanfVqg4HyUz6WNFikgOPf2BgKlx+TQedI1w6n/IqRBVBbBWmGFdLB/7uxO4A==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-0K40bZ35jpHya6FriukbgaleaqzBLZfOh7HuzqbMxBXkbYMJDxfF39c23xOgxFezR+3G+tR2/Mup+Xk865OMvw==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-1q4PaT+o6PbgpkJt4Q8Fv5XJxTy4FUZ4MWETtyiDw3J0Pyr9E2vqcKL+k9wcvjNTIsauxvE7OfmWj3FRPHQ76A==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-28/+iWLYxKxsvGYhSYL7zaCZqLz5+FFFDq9tVsvGv9kv8RY4fFAchJ5WX9M3YrrRlTIsECjsXPqeNgnSmNP2dg==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-uMe5UO6PamJtSHrXhhHOzSX3ReWtiJrva6GnPMwSOrZtiExb5X5eExhr2OUZQVvdxPsKpY3Ro2mFbQadpPWmHA==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-Zbl9NyscLMZkfXPQnNAIIAFftidrA1UbcJEIMp24C0Bukc2I5T8wJS0wsXYsnDOqCFJUeJ1BITGNs5CqPDSmSg==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-NzBdIvEJmx6fjeremiGp3t/okrLPYT0d9orIc7AFun8oZcRk58aejkqhv6spnz4MLAevrKNPMQYXEWMg4s+sKA==}
+
+ '@codemirror/[email protected]':
+ resolution: {integrity: sha512-MwEwCAr/o0agJefhC2+reBv5kfOQpMcDRUNQrRYZgWlhH8IwQcerMZrpqWyUFSyO0ebgN2cnh/w87F7G4BGSng==}
+
'@csstools/[email protected]':
resolution: {integrity: sha512-LMGQLS9EuADloEFkcTBR3BwV/CGHV7zyDxVRtVDTwdI2Ca4it0CCVTT9wCkxSgokjE5Ho41hEPgb8OEUwoXr6Q==}
engines: {node: '>=20.19.0'}
@@ -1206,6 +1248,30 @@ packages:
'@jridgewell/[email protected]':
resolution: {integrity: sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==}
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-sxQE460fPZyU3sdc8lafxiPwJHBzZRy/udNFynGQky1SePYBdhkBl1kOagA9uT3pxR8K09bOrmTUqA9wb/PjSQ==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-RzBo8r+/6QJeow7aPHIpGVIH59xTcJXp399820gZoMo9noQDRVpJLheIBUicYwKcsbOYoBRoLZlf2720dG/4Tg==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-qXdH7UqTvGfdVBINrgKhDsVTJTxactNNxLk7+UMwZhU13lMHaOBlJe9Vqp907ya56Y3+ed2tlqzys7jDkTmW0g==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-oI7n6NJml729m7pjm9lvLvmXbdoMoi2f+1pwSDJkl9d68zGr7a9Btz8NdHTGQZtW2DA25ybeuv/SyDb9D5tseg==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-vvYx3MhWqeZtGPwDStM2dwgljd5smolYD2lR2UyFcHfxbBQebqx8yjmFmxtJ/E6nN6u1D9srOiVWm3Rb4tmcUA==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-rnCpTIBafOx4mRp43xOxDJbFipJm/c0cia/V5TiGlhmMa+wsSdoGmUN3w5Bqrks/09Q/D4tNAmWaT8p6NRi77A==}
+
+ '@lezer/[email protected]':
+ resolution: {integrity: sha512-N0SxazMj4k65DBfaf1azqtMZd6u7MqluP84/NZnB/io8Td9aleFmAhz9hcbvSfsxT5tdYlJ5qgv5aMJGY4zEtA==}
+
+ '@marijn/[email protected]':
+ resolution: {integrity: sha512-l0h88YhZFyKdXIFNfSWpyjStDjGHwZ/U7iobcK1cQQD8sejsONdQtTVU+1wVN1PBw40PiiHB1vA5S7VTfQiP9g==}
+
'@napi-rs/[email protected]':
resolution: {integrity: sha512-AWPoBRJ9tsnVhor4sjO7rkni+7p+2IAEFj6cx06UgP10jkQHqay/36uRV/bFkgrh18D9vb4cr8Q0Pthskgzy+Q==}
peerDependencies:
@@ -2488,6 +2554,28 @@ packages:
resolution: {integrity: sha512-6fJ9MHWtK14C1DSkiMlHUSOmrVebL7150xZJBlJiL62jjhIA4JmOq6flwBgDxIdBKKdoiZRel+dfPD5MLfny3w==}
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
+ resolution: {integrity: sha512-P3vytLlpE62KYSWrMUnwDCv2lvaQDuDZzyj03mHntuHo5bSl34fRZpjTY3kQTPGuXHxkGSYpoPFFj+hMTqaaMQ==}
+ peerDependencies:
+ '@codemirror/autocomplete': '>=6.0.0'
+ '@codemirror/commands': '>=6.0.0'
+ '@codemirror/language': '>=6.0.0'
+ '@codemirror/lint': '>=6.0.0'
+ '@codemirror/search': '>=6.0.0'
+ '@codemirror/state': '>=6.0.0'
+ '@codemirror/view': '>=6.0.0'
+
+ resolution: {integrity: sha512-DzgSMwM5qzB7v1FIb4gEeriYt67iiay756/HIOM9mAbeOVK0MO7rqefHf0O5c0269pJKMW7AH9FjclExD23V9w==}
+ peerDependencies:
+ '@babel/runtime': '>=7.11.0'
+ '@codemirror/state': '>=6.0.0'
+ '@codemirror/theme-one-dark': '>=6.0.0'
+ '@codemirror/view': '>=6.0.0'
+ codemirror: '>=6.0.0'
+ react: '>=17.0.0'
+ react-dom: '>=17.0.0'
+
resolution: {integrity: sha512-mUFwbeTqrVgDQxFveS+df2yfap6iuP20NAKAsBt5jDEoOTDew+zwLAOilHCeQJOVSvmgCX4ogqIrA0mnyr08yQ==}
@@ -2867,6 +2955,9 @@ packages:
react: ^18 || ^19 || ^19.0.0-rc
react-dom: ^18 || ^19 || ^19.0.0-rc
+ resolution: {integrity: sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw==}
+
resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==}
engines: {node: '>=7.0.0'}
@@ -2945,6 +3036,9 @@ packages:
typescript:
optional: true
+ resolution: {integrity: sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==}
+
resolution: {integrity: sha512-Pcw1JTvZLSJH83iiGWt6fRcT+BjZlCDRVwYLbUcHzv/CRpB7r0MlSrGbIyQvVSNyGnbt7G4AXuyCiDR3POvZ1A==}
engines: {node: '>=16.0.0'}
@@ -5103,6 +5197,9 @@ packages:
resolution: {integrity: sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ==}
+
resolution: {integrity: sha512-RjQetxJrrUJLQPHbLku6U/ocGtzyjbJMP9lCNK7Ag0CNh690nSH8woqWH9u16nMjYBAok+i7JO1NP2pOy8IsPQ==}
@@ -5797,6 +5894,99 @@ snapshots:
dependencies:
css-tree: 3.2.1
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@lezer/common': 1.5.2
+ '@lezer/css': 1.3.3
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/lang-css': 6.3.1
+ '@codemirror/lang-javascript': 6.2.5
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+ '@lezer/css': 1.3.3
+ '@lezer/html': 1.3.13
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/language': 6.12.4
+ '@codemirror/lint': 6.9.7
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+ '@lezer/javascript': 1.5.4
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/lang-html': 6.4.11
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+ '@lezer/markdown': 1.6.4
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/common': 1.5.2
+ '@lezer/highlight': 1.2.3
+ '@lezer/lr': 1.4.10
+ style-mod: 4.1.3
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ crelt: 1.0.6
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ crelt: 1.0.6
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@marijn/find-cluster-break': 1.0.2
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/language': 6.12.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+ '@lezer/highlight': 1.2.3
+
+ '@codemirror/[email protected]':
+ dependencies:
+ '@codemirror/state': 6.7.0
+ crelt: 1.0.6
+ style-mod: 4.1.3
+ w3c-keyname: 2.2.8
+
'@csstools/[email protected]': {}
'@csstools/[email protected](@csstools/[email protected](@csstools/[email protected]))(@csstools/[email protected])':
@@ -6632,6 +6822,41 @@ snapshots:
'@jridgewell/resolve-uri': 3.1.2
'@jridgewell/sourcemap-codec': 1.5.5
+ '@lezer/[email protected]': {}
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+ '@lezer/highlight': 1.2.3
+ '@lezer/lr': 1.4.10
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+ '@lezer/highlight': 1.2.3
+ '@lezer/lr': 1.4.10
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+ '@lezer/highlight': 1.2.3
+ '@lezer/lr': 1.4.10
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+
+ '@lezer/[email protected]':
+ dependencies:
+ '@lezer/common': 1.5.2
+ '@lezer/highlight': 1.2.3
+
+ '@marijn/[email protected]': {}
+
dependencies:
'@emnapi/core': 1.10.0
@@ -7934,6 +8159,33 @@ snapshots:
'@typescript-eslint/types': 8.61.1
eslint-visitor-keys: 5.0.1
+ '@uiw/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/commands': 6.10.4
+ '@codemirror/language': 6.12.4
+ '@codemirror/lint': 6.9.7
+ '@codemirror/search': 6.7.1
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+
+ dependencies:
+ '@babel/runtime': 7.29.7
+ '@codemirror/commands': 6.10.4
+ '@codemirror/state': 6.7.0
+ '@codemirror/theme-one-dark': 6.1.3
+ '@codemirror/view': 6.43.3
+ '@uiw/codemirror-extensions-basic-setup': 4.25.10(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])
+ codemirror: 6.0.2
+ react: 19.2.7
+ react-dom: 19.2.7([email protected])
+ transitivePeerDependencies:
+ - '@codemirror/autocomplete'
+ - '@codemirror/language'
+ - '@codemirror/lint'
+ - '@codemirror/search'
+
'@ungap/[email protected]': {}
@@ -8352,6 +8604,16 @@ snapshots:
- '@types/react'
- '@types/react-dom'
+ dependencies:
+ '@codemirror/autocomplete': 6.20.3
+ '@codemirror/commands': 6.10.4
+ '@codemirror/language': 6.12.4
+ '@codemirror/lint': 6.9.7
+ '@codemirror/search': 6.7.1
+ '@codemirror/state': 6.7.0
+ '@codemirror/view': 6.43.3
+
dependencies:
color-name: 1.1.4
@@ -8416,6 +8678,8 @@ snapshots:
optionalDependencies:
typescript: 6.0.3
+
dependencies:
tslib: 2.8.1
@@ -10972,6 +11236,8 @@ snapshots:
+
dependencies:
style-to-object: 1.0.14
diff --git a/frontend/src/components/shared/code-editor.tsx b/frontend/src/components/shared/code-editor.tsx
new file mode 100644
index 00000000..92e26567
--- /dev/null
+++ b/frontend/src/components/shared/code-editor.tsx
@@ -0,0 +1,49 @@
+import { markdown } from '@codemirror/lang-markdown';
+import CodeMirror, { EditorView, type ReactCodeMirrorRef } from '@uiw/react-codemirror';
+import { type Ref, useMemo } from 'react';
+
+import { useTheme } from '@/hooks/use-theme';
+import { cn } from '@/lib/utils';
+
+export type { ReactCodeMirrorRef };
+
+export interface CodeEditorProps {
+ className?: string;
+ disabled?: boolean;
+ onBlur?: () => void;
+ onChange: (value: string) => void;
+ placeholder?: string;
+ ref?: Ref<ReactCodeMirrorRef>;
+ value: string;
+}
+
+// CodeMirror edits the raw document string verbatim — no parse/serialize round-trip —
+// so it is byte-faithful for content (Go templates) a markdown editor would normalize.
+const extensions = [markdown(), EditorView.lineWrapping];
+
+export function CodeEditor({ className, disabled, onBlur, onChange, placeholder, ref, value }: CodeEditorProps) {
+ const { theme } = useTheme();
+ const isDark = useMemo(
+ () => theme === 'dark' || (theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches),
+ [theme],
+ );
+
+ return (
+ <CodeMirror
+ className={cn(
+ 'border-input dark:bg-input/30 focus-within:ring-ring h-full overflow-hidden rounded-md border text-sm shadow-2xs focus-within:ring-1',
+ disabled && 'pointer-events-none opacity-60',
+ className,
+ )}
+ editable={!disabled}
+ extensions={extensions}
+ height="100%"
+ onBlur={onBlur}
+ onChange={onChange}
+ placeholder={placeholder}
+ ref={ref}
+ theme={isDark ? 'dark' : 'light'}
+ value={value}
+ />
+ );
+}
diff --git a/frontend/src/pages/settings/settings-prompt.tsx b/frontend/src/pages/settings/settings-prompt.tsx
index dd37a545..628d641d 100644
--- a/frontend/src/pages/settings/settings-prompt.tsx
+++ b/frontend/src/pages/settings/settings-prompt.tsx
@@ -13,11 +13,12 @@ import {
Loader2,
RotateCcw,
Save,
+ Type,
User,
Wrench,
XCircle,
} from 'lucide-react';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import { lazy, type Ref, Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import ReactDiffViewer from 'react-diff-viewer-continued';
import {
type Control,
@@ -40,6 +41,8 @@ import type {
type AgentPrompt = AgentPrompts;
type AgentPrompts = { human?: DefaultPrompt; system: DefaultPrompt };
+import type { ReactCodeMirrorRef } from '@/components/shared/code-editor';
+
import {
AppHeader,
AppHeaderAction,
@@ -58,6 +61,7 @@ import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
+ DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Form, FormControl, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
@@ -77,6 +81,11 @@ 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 CodeEditor import would merge its ~600KB CodeMirror chunk into this route bundle.
+const CodeEditor = lazy(() =>
+ import('@/components/shared/code-editor').then((module) => ({ default: module.CodeEditor })),
+);
+
const systemFormSchema = z.object({
template: z.string().min(1, 'System template is required'),
});
@@ -99,6 +108,11 @@ interface ControllerProps<T extends FieldValues> {
name: FieldPathByValue<T, string>;
}
+interface FormCodeItemProps<T extends FieldValues> extends ControllerProps<T> {
+ editorRef?: Ref<ReactCodeMirrorRef>;
+ placeholder?: string;
+}
+
interface FormTextareaItemProps<T extends FieldValues> extends BaseFieldProps<T>, BaseTextareaProps {
description?: string;
}
@@ -107,6 +121,45 @@ type HumanFormData = z.infer<typeof humanFormSchema>;
type SystemFormData = z.infer<typeof systemFormSchema>;
+function FormCodeItem<T extends FieldValues>({
+ control,
+ disabled,
+ editorRef,
+ name,
+ placeholder,
+}: FormCodeItemProps<T>) {
+ const { field, fieldState } = useController({
+ control,
+ disabled,
+ name,
+ });
+
+ return (
+ <FormItem className="flex min-h-0 flex-1 flex-col">
+ <FormControl>
+ <Suspense
+ 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>
+ }
+ >
+ <CodeEditor
+ 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,
@@ -183,6 +236,7 @@ interface VariablesProps {
currentTemplate: string;
onVariableClick: (variable: string) => void;
variables: string[];
+ viewMode: 'code' | 'plain';
}
function SettingsPrompt() {
@@ -201,50 +255,73 @@ function SettingsPrompt() {
const [validationResult, setValidationResult] = useState<null | ValidatePromptMutation['validatePrompt']>(null);
const [validationDialogOpen, setValidationDialogOpen] = useState(false);
const [isDiffDialogOpen, setIsDiffDialogOpen] = useState(false);
+ const [viewMode, setViewMode] = useState<'code' | 'plain'>('plain');
+ const editorRef = useRef<ReactCodeMirrorRef>(null);
const isLoading = isCreateLoading || isUpdateLoading || isDeleteLoading || isValidateLoading;
- const handleVariableClick = (
- variable: string,
- field: { onChange: (value: string) => void; value: string },
- formId: string,
- ) => {
- const textarea = document.querySelector(`#${formId} textarea`) as HTMLTextAreaElement;
-
- if (textarea) {
- const currentValue = field.value || '';
- const variablePattern = `{{.${variable}}}`;
- const matches = [...currentValue.matchAll(new RegExp(variableActionRegex(variable).source, 'g'))];
-
- if (matches.length > 0) {
- // Cycle through occurrences: advance from the one the caret is already on
- // (wrapping past the last), else jump to the first occurrence at/after the caret.
- const { selectionEnd, selectionStart } = textarea;
- const currentIdx = matches.findIndex(
- (match) => match.index === selectionStart && match.index + match[0].length === selectionEnd,
- );
- const target =
- currentIdx >= 0
- ? matches[(currentIdx + 1) % matches.length]
- : (matches.find((match) => match.index >= selectionStart) ?? matches[0]);
- const matchStart = target.index ?? 0;
- textarea.focus();
- textarea.setSelectionRange(matchStart, matchStart + target[0].length);
- textarea.scrollTop = Math.max(0, caretOffsetTop(textarea, matchStart) - textarea.clientHeight / 2);
- } else {
- const start = textarea.selectionStart;
- const end = textarea.selectionEnd;
- const newValue =
- currentValue.slice(0, Math.max(0, start)) + variablePattern + currentValue.slice(Math.max(0, end));
- field.onChange(newValue);
-
- setTimeout(() => {
- textarea.focus({ preventScroll: true });
- textarea.setSelectionRange(start + variablePattern.length, start + variablePattern.length);
- }, 0);
+ const handleVariableClick = useCallback(
+ (variable: string, field: { onChange: (value: string) => void; value: string }, formId: string) => {
+ if (viewMode === 'code') {
+ const view = editorRef.current?.view;
+
+ if (view) {
+ const insert = `{{.${variable}}}`;
+ const pos = view.state.selection.main.head;
+ view.dispatch({
+ changes: { from: pos, insert },
+ scrollIntoView: true,
+ selection: { anchor: pos + insert.length },
+ });
+ view.focus();
+ }
+
+ return;
}
- }
- };
+
+ const textarea = document.querySelector(`#${formId} textarea`) as HTMLTextAreaElement;
+
+ if (textarea) {
+ const currentValue = field.value || '';
+ const variablePattern = `{{.${variable}}}`;
+ const matches = [...currentValue.matchAll(new RegExp(variableActionRegex(variable).source, 'g'))];
+
+ if (matches.length > 0) {
+ // Cycle through occurrences: advance from the one the caret is already on
+ // (wrapping past the last), else jump to the first occurrence at/after the caret.
+ const { selectionEnd, selectionStart } = textarea;
+ const currentIdx = matches.findIndex(
+ (match) => match.index === selectionStart && match.index + match[0].length === selectionEnd,
+ );
+ const target =
+ currentIdx >= 0
+ ? matches[(currentIdx + 1) % matches.length]
+ : (matches.find((match) => match.index >= selectionStart) ?? matches[0]);
+
+ if (target) {
+ const matchStart = target.index;
+ textarea.focus();
+ textarea.setSelectionRange(matchStart, matchStart + target[0].length);
+ textarea.scrollTop = Math.max(0, caretOffsetTop(textarea, matchStart) - textarea.clientHeight / 2);
+ }
+ } else {
+ const start = textarea.selectionStart;
+ const end = textarea.selectionEnd;
+ const newValue =
+ currentValue.slice(0, Math.max(0, start)) +
+ variablePattern +
+ currentValue.slice(Math.max(0, end));
+ field.onChange(newValue);
+
+ setTimeout(() => {
+ textarea.focus({ preventScroll: true });
+ textarea.setSelectionRange(start + variablePattern.length, start + variablePattern.length);
+ }, 0);
+ }
+ }
+ },
+ [viewMode],
+ );
const handleReset = () => {
setResetDialogOpen(true);
@@ -445,7 +522,7 @@ function SettingsPrompt() {
};
handleVariableClick(variable, field, variablesData.formId);
},
- [activeTab, systemTemplate, humanTemplate, variablesData, systemForm, humanForm],
+ [activeTab, systemTemplate, humanTemplate, variablesData, systemForm, humanForm, handleVariableClick],
);
useEffect(() => {
@@ -643,40 +720,49 @@ function SettingsPrompt() {
>
{isNew ? 'Create' : 'Save'}
</FormSubmitButton>
- {hasOverride && (
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button
- aria-label="Prompt actions"
- className="size-8 p-0"
- type="button"
- variant="ghost"
- >
- <Ellipsis />
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent
- align="end"
- className="min-w-24"
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ aria-label="Prompt actions"
+ className="size-8 p-0"
+ type="button"
+ variant="ghost"
>
- <DropdownMenuItem onClick={() => setIsDiffDialogOpen(true)}>
- <FileDiff className="size-4" />
- Diff
- </DropdownMenuItem>
- <DropdownMenuItem
- disabled={isLoading}
- onClick={handleReset}
- >
- {isDeleteLoading ? (
- <Loader2 className="size-4 animate-spin" />
- ) : (
- <RotateCcw className="size-4" />
- )}
- {isDeleteLoading ? 'Resetting...' : 'Reset'}
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- )}
+ <Ellipsis />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent
+ align="end"
+ className="min-w-24"
+ >
+ <DropdownMenuItem
+ onClick={() => setViewMode((mode) => (mode === 'code' ? 'plain' : 'code'))}
+ >
+ {viewMode === 'code' ? <Type className="size-4" /> : <Code className="size-4" />}
+ {viewMode === 'code' ? 'Plain text' : 'Code editor'}
+ </DropdownMenuItem>
+ {hasOverride && (
+ <>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem onClick={() => setIsDiffDialogOpen(true)}>
+ <FileDiff className="size-4" />
+ Diff
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ disabled={isLoading}
+ onClick={handleReset}
+ >
+ {isDeleteLoading ? (
+ <Loader2 className="size-4 animate-spin" />
+ ) : (
+ <RotateCcw className="size-4" />
+ )}
+ {isDeleteLoading ? 'Resetting...' : 'Reset'}
+ </DropdownMenuItem>
+ </>
+ )}
+ </DropdownMenuContent>
+ </DropdownMenu>
</AppHeaderActions>
)}
</AppHeader>
@@ -863,9 +949,13 @@ function SettingsPrompt() {
currentTemplate={variablesData.currentTemplate}
onVariableClick={handleVariableClickCallback}
variables={variablesData.variables}
+ viewMode={viewMode}
/>
) : null;
+ const systemPlaceholder =
+ promptInfo.type === 'tool' ? 'Enter the tool template...' : 'Enter the system prompt template...';
+
const promptEditor = (
<>
<TabsContent
@@ -878,16 +968,22 @@ function SettingsPrompt() {
id="system-prompt-form"
onSubmit={systemForm.handleSubmit(handleSystemSubmit)}
>
- <FormTextareaItem
- control={systemForm.control}
- disabled={isLoading}
- name="template"
- placeholder={
- promptInfo.type === 'tool'
- ? 'Enter the tool template...'
- : 'Enter the system prompt template...'
- }
- />
+ {viewMode === 'code' ? (
+ <FormCodeItem
+ control={systemForm.control}
+ disabled={isLoading}
+ editorRef={editorRef}
+ name="template"
+ placeholder={systemPlaceholder}
+ />
+ ) : (
+ <FormTextareaItem
+ control={systemForm.control}
+ disabled={isLoading}
+ name="template"
+ placeholder={systemPlaceholder}
+ />
+ )}
</form>
</Form>
</TabsContent>
@@ -903,12 +999,22 @@ function SettingsPrompt() {
id="human-prompt-form"
onSubmit={humanForm.handleSubmit(handleHumanSubmit)}
>
- <FormTextareaItem
- control={humanForm.control}
- disabled={isLoading}
- name="template"
- placeholder="Enter the human prompt template..."
- />
+ {viewMode === 'code' ? (
+ <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..."
+ />
+ )}
</form>
</Form>
</TabsContent>
@@ -1066,7 +1172,7 @@ function SettingsPrompt() {
);
}
-function Variables({ currentTemplate, onVariableClick, variables }: VariablesProps) {
+function Variables({ currentTemplate, onVariableClick, variables, viewMode }: VariablesProps) {
if (variables.length === 0) {
return null;
}
@@ -1075,16 +1181,23 @@ function Variables({ currentTemplate, onVariableClick, variables }: VariablesPro
<div className="bg-card overflow-hidden rounded-lg border">
<div className="border-b px-4 py-3">
<h4 className="text-sm font-medium">Available variables</h4>
- <p className="text-muted-foreground mt-1 text-xs">Click a variable to insert it into the template.</p>
+ <p className="text-muted-foreground mt-1 text-xs">
+ {viewMode === 'code'
+ ? 'Click a variable to insert it into the editor.'
+ : 'Click to insert at the cursor, or cycle through existing uses.'}
+ </p>
</div>
<div className="bg-background flex flex-wrap gap-1.5 px-4 py-3">
{variables.map((variable) => {
const count = (currentTemplate.match(new RegExp(variableActionRegex(variable).source, 'g')) ?? [])
.length;
const isUsed = count > 0;
- const action = isUsed
- ? `Go to next {{.${variable}}} in the template${count > 1 ? ` (${count} uses)` : ''}`
- : `Insert {{.${variable}}} at the cursor`;
+ const action =
+ viewMode === 'code'
+ ? `Insert {{.${variable}}} into the editor`
+ : isUsed
+ ? `Go to next {{.${variable}}} in the template${count > 1 ? ` (${count} uses)` : ''}`
+ : `Insert {{.${variable}}} at the cursor`;
return (
<Badge