PromptForm.tsx
8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { useEffect, useRef, useState, useCallback } from 'react';
import { Modal, Form, Input, Select, Drawer, Space, Tag, Button, Typography } from 'antd';
import { CloseOutlined, InfoCircleOutlined } from '@ant-design/icons';
import { usePromptStore } from '../../stores/promptStore';
import type { Prompt } from '../../types';
import { TEMPLATE_TYPE_OPTIONS } from '../../types';
const { TextArea } = Input;
interface Props {
open: boolean;
prompt: Prompt | null;
onClose: () => void;
}
export default function PromptForm({ open, prompt, onClose }: Props) {
const [form] = Form.useForm();
const { createPrompt, updatePrompt } = usePromptStore();
const isEdit = !!prompt;
const [submitting, setSubmitting] = useState(false);
const [dirty, setDirty] = useState(false);
const firstFieldRef = useRef<HTMLInputElement>(null);
const isInitialMount = useRef(true);
// Watch system_prompt/user_prompt for variable extraction
const [sysPrompt, setSysPrompt] = useState('');
const [usrPrompt, setUsrPrompt] = useState('');
useEffect(() => {
if (open) {
setDirty(false);
isInitialMount.current = true;
if (prompt) {
form.setFieldsValue(prompt);
setSysPrompt(prompt.system_prompt || '');
setUsrPrompt(prompt.user_prompt || '');
} else {
form.resetFields();
form.setFieldValue('template_type', 'general');
form.setFieldValue('status', 'published');
setSysPrompt('');
setUsrPrompt('');
}
setTimeout(() => firstFieldRef.current?.focus(), 300);
}
}, [open, prompt, form]);
const onFieldsChange = useCallback(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
return;
}
if (!dirty && form.isFieldsTouched()) {
setDirty(true);
}
}, [dirty, form]);
const handleClose = useCallback(() => {
if (dirty) {
Modal.confirm({
title: '未保存的更改',
content: '当前表单有未保存的修改,确定要关闭吗?',
okText: '关闭',
cancelText: '继续编辑',
onOk: onClose,
});
} else {
onClose();
}
}, [dirty, onClose]);
const handleOk = async () => {
try {
setSubmitting(true);
const values = await form.validateFields();
if (values.user_prompt === '') values.user_prompt = undefined;
// 从 system_prompt 和 user_prompt 中提取 {{变量}} 并保存到 variables 字段
const allVars = [
...extractVars(values.system_prompt || ''),
...extractVars(values.user_prompt || ''),
];
const uniqueVars = [...new Set(allVars)];
values.variables = uniqueVars.map((name) => ({ name, type: 'string' }));
if (isEdit) {
await updatePrompt(prompt!.id, values);
} else {
await createPrompt(values);
}
onClose();
} catch {
// validation errors shown inline
} finally {
setSubmitting(false);
}
};
// Extract {{variables}} from text
const extractVars = (text: string): string[] => {
const matches = text.match(/\{\{(\w+)\}\}/g);
if (!matches) return [];
return [...new Set(matches.map((m) => m.replace(/\{|\}/g, '')))];
};
const sysVars = extractVars(sysPrompt);
return (
<Drawer
title={
<span style={{ fontSize: 16, fontWeight: 600 }}>
{isEdit ? '编辑提示词模板' : '新增提示词模板'}
</span>
}
open={open}
onClose={handleClose}
width={960}
extra={
<Space>
<Button onClick={handleClose}>取消</Button>
<Button type="primary" onClick={handleOk} loading={submitting}>
保存
</Button>
</Space>
}
destroyOnClose
className="prompt-drawer"
>
<Form
form={form}
layout="vertical"
onFieldsChange={onFieldsChange}
style={{ height: '100%' }}
>
<div className="prompt-editor-layout">
{/* Left: Editor area */}
<div className="prompt-editor-main">
<div className="prompt-editor-section prompt-editor-section-small">
<div className="prompt-editor-label">
<span>System Prompt</span>
<Tag style={{ fontSize: 10, lineHeight: '16px', padding: '0 6px' }}>必填</Tag>
</div>
<Form.Item
name="system_prompt"
rules={[{ required: true, message: '请输入 System Prompt' }]}
className="prompt-textarea-item"
>
<TextArea
rows={5}
className="prompt-textarea"
placeholder="你是一个专业的质检审核助手。请根据标准对以下内容进行审核。"
onChange={(e) => setSysPrompt(e.target.value)}
/>
</Form.Item>
<div className="prompt-editor-meta">
<span className="prompt-char-count">{sysPrompt.length} 字符</span>
<span className="prompt-line-count">{sysPrompt.split('\n').length} 行</span>
{sysVars.length > 0 && (
<span className="prompt-vars-display">
变量: {sysVars.map((v) => <Tag key={v} className="prompt-var-tag">{`{{${v}}}`}</Tag>)}
</span>
)}
</div>
</div>
<div className="prompt-editor-section prompt-editor-section-grow">
<div className="prompt-editor-label">
<span>User Prompt</span>
<Tag style={{ fontSize: 10, lineHeight: '16px', padding: '0 6px', border: '1px dashed #d4d4d8' }}>可选</Tag>
</div>
<Form.Item
name="user_prompt"
className="prompt-textarea-item prompt-textarea-item-grow"
>
<TextArea
className="prompt-textarea prompt-textarea-grow"
placeholder="请审核以下内容:{{content}}"
onChange={(e) => setUsrPrompt(e.target.value)}
/>
</Form.Item>
<div className="prompt-editor-meta">
<span className="prompt-char-count">{usrPrompt.length} 字符</span>
<span className="prompt-line-count">{usrPrompt.split('\n').length} 行</span>
</div>
</div>
</div>
{/* Right: Properties panel */}
<div className="prompt-editor-side">
<div className="prompt-editor-side-sticky">
<div className="form-side-section">
<h6 className="form-side-section-title">基本信息</h6>
<Form.Item
name="template_type"
label="模板类型"
rules={[{ required: true, message: '请选择模板类型' }]}
>
<Select
placeholder="选择模板类型"
options={TEMPLATE_TYPE_OPTIONS}
/>
</Form.Item>
<Form.Item
name="title"
label="模板标题"
rules={[{ required: true, message: '请输入模板标题' }]}
>
<Input placeholder="例如:通用质检提示词" ref={firstFieldRef} />
</Form.Item>
<Form.Item
name="code"
label="业务编码"
rules={[
{ required: !isEdit, message: '请输入业务编码' },
{ pattern: /^[a-zA-Z0-9_-]+$/, message: '仅支持字母、数字、下划线和连字符' },
]}
extra="创建后不可修改"
>
<Input placeholder="TEMPLATE-GENERAL-001" disabled={isEdit} />
</Form.Item>
<Form.Item
name="status"
label="状态"
rules={[{ required: true, message: '请选择状态' }]}
>
<Select
options={[
{ label: '已发布', value: 'published' },
{ label: '草稿', value: 'draft' },
{ label: '已归档', value: 'archived' },
]}
/>
</Form.Item>
</div>
<div className="form-side-section">
<h6 className="form-side-section-title">
<InfoCircleOutlined style={{ marginRight: 4 }} />
使用提示
</h6>
<ul className="prompt-tips">
<li>使用 <code className="inline-code">{'{{变量名}}'}</code> 插入动态变量</li>
<li>System Prompt 定义角色和行为准则</li>
<li>User Prompt 为可选的用户输入模板</li>
<li>按 <kbd>Ctrl</kbd> + <kbd>Enter</kbd> 快速保存</li>
</ul>
</div>
</div>
</div>
</div>
</Form>
</Drawer>
);
}