1.优化AI请求替换OpenAI SDK调用,使用httpx和自定义头请求,避免触发部分公益站的cloudflare
2.修复deepseek模型调用问题,舍弃思考过程AI响应内容,只获取结果内容 3.新增会话过期机制,更新后添加到.env中 4.支持用户在生成章节内容时设置字数
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { List, Button, Modal, Form, Input, Select, message, Empty, Space, Badge, Tag, Card, Tooltip } from 'antd';
|
||||
import { List, Button, Modal, Form, Input, Select, message, Empty, Space, Badge, Tag, Card, Tooltip, InputNumber } from 'antd';
|
||||
import { EditOutlined, FileTextOutlined, ThunderboltOutlined, LockOutlined, DownloadOutlined, SettingOutlined } from '@ant-design/icons';
|
||||
import { useStore } from '../store';
|
||||
import { useChapterSync } from '../store/hooks';
|
||||
@@ -22,6 +22,7 @@ export default function Chapters() {
|
||||
const contentTextAreaRef = useRef<any>(null);
|
||||
const [writingStyles, setWritingStyles] = useState<WritingStyle[]>([]);
|
||||
const [selectedStyleId, setSelectedStyleId] = useState<number | undefined>();
|
||||
const [targetWordCount, setTargetWordCount] = useState<number>(3000);
|
||||
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
@@ -167,7 +168,7 @@ export default function Chapters() {
|
||||
textArea.scrollTop = textArea.scrollHeight;
|
||||
}
|
||||
}
|
||||
}, selectedStyleId);
|
||||
}, selectedStyleId, targetWordCount);
|
||||
|
||||
message.success('AI创作成功');
|
||||
} catch (error) {
|
||||
@@ -201,6 +202,7 @@ export default function Chapters() {
|
||||
{selectedStyle && (
|
||||
<li><strong>写作风格:{selectedStyle.name}</strong></li>
|
||||
)}
|
||||
<li><strong>目标字数:{targetWordCount}字</strong></li>
|
||||
</ul>
|
||||
|
||||
{previousChapters.length > 0 && (
|
||||
@@ -519,7 +521,7 @@ export default function Chapters() {
|
||||
} : undefined}
|
||||
styles={{
|
||||
body: {
|
||||
maxHeight: isMobile ? 'calc(100vh - 150px)' : 'calc(85vh - 110px)',
|
||||
maxHeight: isMobile ? 'calc(100vh - 150px)' : 'calc(100vh - 110px)',
|
||||
overflowY: 'auto',
|
||||
padding: isMobile ? '16px 12px' : '8px'
|
||||
}
|
||||
@@ -592,6 +594,27 @@ export default function Chapters() {
|
||||
)}
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="目标字数"
|
||||
tooltip="AI生成章节时的目标字数,实际生成字数可能略有偏差"
|
||||
>
|
||||
<InputNumber
|
||||
min={500}
|
||||
max={10000}
|
||||
step={100}
|
||||
value={targetWordCount}
|
||||
onChange={(value) => setTargetWordCount(value || 3000)}
|
||||
size="large"
|
||||
disabled={isGenerating}
|
||||
style={{ width: '100%' }}
|
||||
formatter={(value) => `${value} 字`}
|
||||
parser={(value) => value?.replace(' 字', '') as any}
|
||||
/>
|
||||
<div style={{ color: '#666', fontSize: 12, marginTop: 4 }}>
|
||||
建议范围:500-10000字,默认3000字
|
||||
</div>
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="章节内容" name="content">
|
||||
<TextArea
|
||||
ref={contentTextAreaRef}
|
||||
|
||||
@@ -25,6 +25,7 @@ export default function ProjectWizardNew() {
|
||||
const [form] = Form.useForm();
|
||||
const [characterForm] = Form.useForm();
|
||||
const [worldForm] = Form.useForm();
|
||||
const [generateForm] = Form.useForm();
|
||||
const [current, setCurrent] = useState(0);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [isResumingWizard, setIsResumingWizard] = useState(false);
|
||||
@@ -814,11 +815,85 @@ export default function ProjectWizardNew() {
|
||||
return (
|
||||
<Card>
|
||||
<div style={{ marginBottom: 16 }}>
|
||||
<Title level={isMobile ? 5 : 4} style={{ margin: 0, fontSize: isMobile ? 16 : undefined }}>
|
||||
角色与组织列表 (当前: {safeCharacters.length}个,目标: {requiredCharacterCount}个)
|
||||
</Title>
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: isMobile ? 'flex-start' : 'center',
|
||||
marginBottom: 12,
|
||||
flexDirection: isMobile ? 'column' : 'row',
|
||||
gap: isMobile ? 12 : 0
|
||||
}}>
|
||||
<Title level={isMobile ? 5 : 4} style={{ margin: 0, fontSize: isMobile ? 16 : undefined }}>
|
||||
角色与组织列表 (当前: {safeCharacters.length}个,目标: {requiredCharacterCount}个)
|
||||
</Title>
|
||||
<Button
|
||||
type="dashed"
|
||||
icon={<TeamOutlined />}
|
||||
onClick={() => {
|
||||
Modal.confirm({
|
||||
title: 'AI生成角色',
|
||||
width: 600,
|
||||
centered: true,
|
||||
content: (
|
||||
<Form form={generateForm} layout="vertical" style={{ marginTop: 16 }}>
|
||||
<Form.Item
|
||||
label="角色名称"
|
||||
name="name"
|
||||
>
|
||||
<Input placeholder="如:张三、李四(可选,AI会自动生成)" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
label="角色定位"
|
||||
name="role_type"
|
||||
rules={[{ required: true, message: '请选择角色定位' }]}
|
||||
>
|
||||
<Select placeholder="选择角色定位">
|
||||
<Select.Option value="protagonist">主角</Select.Option>
|
||||
<Select.Option value="supporting">配角</Select.Option>
|
||||
<Select.Option value="antagonist">反派</Select.Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item label="背景设定" name="background">
|
||||
<TextArea rows={3} placeholder="简要描述角色背景和故事环境..." />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
),
|
||||
okText: '生成',
|
||||
cancelText: '取消',
|
||||
onOk: async () => {
|
||||
try {
|
||||
const values = await generateForm.validateFields();
|
||||
setLoading(true);
|
||||
|
||||
// 调用单个角色生成API
|
||||
const newCharacter = await characterApi.generateCharacter({
|
||||
project_id: projectId,
|
||||
name: values.name,
|
||||
role_type: values.role_type,
|
||||
background: values.background,
|
||||
});
|
||||
|
||||
// 添加到列表
|
||||
setCharacters([...safeCharacters, newCharacter]);
|
||||
message.success('AI生成角色成功');
|
||||
generateForm.resetFields();
|
||||
} catch (error) {
|
||||
const apiError = error as ApiError;
|
||||
message.error('AI生成失败:' + (apiError.response?.data?.detail || apiError.message || '未知错误'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}}
|
||||
disabled={loading}
|
||||
size={isMobile ? 'middle' : 'middle'}
|
||||
>
|
||||
AI生成角色
|
||||
</Button>
|
||||
</div>
|
||||
<Paragraph type="secondary" style={{ margin: '8px 0 0 0', fontSize: isMobile ? 12 : 14 }}>
|
||||
所有角色均由AI生成,您可以点击卡片上的编辑按钮进行调整
|
||||
所有角色均由AI生成,您可以点击卡片上的编辑按钮进行调整,或使用"AI生成角色"按钮继续生成
|
||||
</Paragraph>
|
||||
</div>
|
||||
|
||||
|
||||
+203
-25
@@ -1,7 +1,7 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Card, Form, Input, Button, Select, Slider, InputNumber, message, Space, Typography, Spin, Modal, Tooltip, Alert, Grid } from 'antd';
|
||||
import { SettingOutlined, SaveOutlined, DeleteOutlined, ReloadOutlined, ArrowLeftOutlined, InfoCircleOutlined} from '@ant-design/icons';
|
||||
import { SettingOutlined, SaveOutlined, DeleteOutlined, ReloadOutlined, ArrowLeftOutlined, InfoCircleOutlined, CheckCircleOutlined, CloseCircleOutlined, ThunderboltOutlined } from '@ant-design/icons';
|
||||
import { settingsApi } from '../services/api';
|
||||
import type { SettingsUpdate } from '../types';
|
||||
|
||||
@@ -21,6 +21,17 @@ export default function SettingsPage() {
|
||||
const [modelOptions, setModelOptions] = useState<Array<{ value: string; label: string; description: string }>>([]);
|
||||
const [fetchingModels, setFetchingModels] = useState(false);
|
||||
const [modelsFetched, setModelsFetched] = useState(false);
|
||||
const [testingApi, setTestingApi] = useState(false);
|
||||
const [testResult, setTestResult] = useState<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
response_time_ms?: number;
|
||||
response_preview?: string;
|
||||
error?: string;
|
||||
error_type?: string;
|
||||
suggestions?: string[];
|
||||
} | null>(null);
|
||||
const [showTestResult, setShowTestResult] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
loadSettings();
|
||||
@@ -178,6 +189,52 @@ export default function SettingsPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleTestConnection = async () => {
|
||||
const apiKey = form.getFieldValue('api_key');
|
||||
const apiBaseUrl = form.getFieldValue('api_base_url');
|
||||
const provider = form.getFieldValue('api_provider');
|
||||
const modelName = form.getFieldValue('model_name');
|
||||
|
||||
if (!apiKey || !apiBaseUrl || !provider || !modelName) {
|
||||
message.warning('请先填写完整的配置信息');
|
||||
return;
|
||||
}
|
||||
|
||||
setTestingApi(true);
|
||||
setTestResult(null);
|
||||
|
||||
try {
|
||||
const result = await settingsApi.testApiConnection({
|
||||
api_key: apiKey,
|
||||
api_base_url: apiBaseUrl,
|
||||
provider: provider,
|
||||
model_name: modelName
|
||||
});
|
||||
|
||||
setTestResult(result);
|
||||
setShowTestResult(true);
|
||||
|
||||
if (result.success) {
|
||||
message.success(`测试成功!响应时间: ${result.response_time_ms}ms`);
|
||||
} else {
|
||||
message.error('API 测试失败,请查看详细信息');
|
||||
}
|
||||
} catch (error: any) {
|
||||
const errorMsg = error?.response?.data?.detail || '测试请求失败';
|
||||
message.error(errorMsg);
|
||||
setTestResult({
|
||||
success: false,
|
||||
message: '测试请求失败',
|
||||
error: errorMsg,
|
||||
error_type: 'RequestError',
|
||||
suggestions: ['请检查网络连接', '请确认后端服务是否正常运行']
|
||||
});
|
||||
setShowTestResult(true);
|
||||
} finally {
|
||||
setTestingApi(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
minHeight: '100vh',
|
||||
@@ -491,6 +548,94 @@ export default function SettingsPage() {
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
{/* 测试结果展示 */}
|
||||
{showTestResult && testResult && (
|
||||
<Alert
|
||||
message={
|
||||
<Space>
|
||||
{testResult.success ? (
|
||||
<CheckCircleOutlined style={{ color: '#52c41a', fontSize: isMobile ? '16px' : '18px' }} />
|
||||
) : (
|
||||
<CloseCircleOutlined style={{ color: '#ff4d4f', fontSize: isMobile ? '16px' : '18px' }} />
|
||||
)}
|
||||
<span style={{ fontSize: isMobile ? '14px' : '16px', fontWeight: 500 }}>
|
||||
{testResult.message}
|
||||
</span>
|
||||
</Space>
|
||||
}
|
||||
description={
|
||||
<div style={{ marginTop: 8 }}>
|
||||
{testResult.success ? (
|
||||
<Space direction="vertical" size="small" style={{ width: '100%' }}>
|
||||
{testResult.response_time_ms && (
|
||||
<div style={{ fontSize: isMobile ? '12px' : '14px' }}>
|
||||
⚡ 响应时间: <strong>{testResult.response_time_ms} ms</strong>
|
||||
</div>
|
||||
)}
|
||||
{testResult.response_preview && (
|
||||
<div style={{
|
||||
fontSize: isMobile ? '12px' : '13px',
|
||||
padding: '8px 12px',
|
||||
background: '#f6ffed',
|
||||
borderRadius: '4px',
|
||||
border: '1px solid #b7eb8f',
|
||||
marginTop: '8px'
|
||||
}}>
|
||||
<div style={{ marginBottom: '4px', fontWeight: 500 }}>AI 响应预览:</div>
|
||||
<div style={{ color: '#595959' }}>{testResult.response_preview}</div>
|
||||
</div>
|
||||
)}
|
||||
<div style={{ color: '#52c41a', fontSize: isMobile ? '12px' : '13px', marginTop: '4px' }}>
|
||||
✓ API 配置正确,可以正常使用
|
||||
</div>
|
||||
</Space>
|
||||
) : (
|
||||
<Space direction="vertical" size="small" style={{ width: '100%' }}>
|
||||
{testResult.error && (
|
||||
<div style={{
|
||||
fontSize: isMobile ? '12px' : '13px',
|
||||
padding: '8px 12px',
|
||||
background: '#fff2e8',
|
||||
borderRadius: '4px',
|
||||
border: '1px solid #ffbb96',
|
||||
color: '#d4380d'
|
||||
}}>
|
||||
<strong>错误信息:</strong> {testResult.error}
|
||||
</div>
|
||||
)}
|
||||
{testResult.error_type && (
|
||||
<div style={{ fontSize: isMobile ? '11px' : '12px', color: '#8c8c8c' }}>
|
||||
错误类型: {testResult.error_type}
|
||||
</div>
|
||||
)}
|
||||
{testResult.suggestions && testResult.suggestions.length > 0 && (
|
||||
<div style={{ marginTop: '8px' }}>
|
||||
<div style={{ fontSize: isMobile ? '12px' : '13px', fontWeight: 500, marginBottom: '4px' }}>
|
||||
💡 解决建议:
|
||||
</div>
|
||||
<ul style={{
|
||||
margin: 0,
|
||||
paddingLeft: isMobile ? '16px' : '20px',
|
||||
fontSize: isMobile ? '12px' : '13px',
|
||||
color: '#595959'
|
||||
}}>
|
||||
{testResult.suggestions.map((suggestion, index) => (
|
||||
<li key={index} style={{ marginBottom: '4px' }}>{suggestion}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</Space>
|
||||
)}
|
||||
</div>
|
||||
}
|
||||
type={testResult.success ? 'success' : 'error'}
|
||||
closable
|
||||
onClose={() => setShowTestResult(false)}
|
||||
style={{ marginBottom: isMobile ? 16 : 24 }}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<Form.Item style={{ marginBottom: 0, marginTop: isMobile ? 24 : 32 }}>
|
||||
{isMobile ? (
|
||||
@@ -535,9 +680,58 @@ export default function SettingsPage() {
|
||||
</Space>
|
||||
</Space>
|
||||
) : (
|
||||
// 桌面端:原有的水平布局
|
||||
<Space size="middle" style={{ width: '100%', justifyContent: 'space-between' }}>
|
||||
<Space>
|
||||
// 桌面端:删除在左边,测试、重置和保存在右边
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
gap: '16px',
|
||||
flexWrap: 'wrap'
|
||||
}}>
|
||||
{/* 左侧:删除按钮 */}
|
||||
{hasSettings ? (
|
||||
<Button
|
||||
danger
|
||||
size="large"
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={handleDelete}
|
||||
loading={loading}
|
||||
style={{
|
||||
minWidth: '100px'
|
||||
}}
|
||||
>
|
||||
删除配置
|
||||
</Button>
|
||||
) : (
|
||||
<div /> // 占位符,保持右侧按钮位置
|
||||
)}
|
||||
|
||||
{/* 右侧:测试、重置和保存按钮组 */}
|
||||
<Space size="middle">
|
||||
<Button
|
||||
size="large"
|
||||
icon={<ThunderboltOutlined />}
|
||||
onClick={handleTestConnection}
|
||||
loading={testingApi}
|
||||
style={{
|
||||
borderColor: '#52c41a',
|
||||
color: '#52c41a',
|
||||
fontWeight: 500,
|
||||
minWidth: '100px'
|
||||
}}
|
||||
>
|
||||
{testingApi ? '测试中...' : '测试'}
|
||||
</Button>
|
||||
<Button
|
||||
size="large"
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={handleReset}
|
||||
style={{
|
||||
minWidth: '100px'
|
||||
}}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
size="large"
|
||||
@@ -546,31 +740,15 @@ export default function SettingsPage() {
|
||||
loading={loading}
|
||||
style={{
|
||||
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
|
||||
border: 'none'
|
||||
border: 'none',
|
||||
minWidth: '120px',
|
||||
fontWeight: 500
|
||||
}}
|
||||
>
|
||||
保存设置
|
||||
</Button>
|
||||
<Button
|
||||
size="large"
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={handleReset}
|
||||
>
|
||||
重置
|
||||
保存
|
||||
</Button>
|
||||
</Space>
|
||||
{hasSettings && (
|
||||
<Button
|
||||
danger
|
||||
size="large"
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={handleDelete}
|
||||
loading={loading}
|
||||
>
|
||||
删除设置
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
</div>
|
||||
)}
|
||||
</Form.Item>
|
||||
</Form>
|
||||
|
||||
@@ -328,9 +328,9 @@ export default function WritingStyles() {
|
||||
createForm.resetFields();
|
||||
}}
|
||||
footer={null}
|
||||
centered={!isMobile}
|
||||
width={isMobile ? '100%' : 600}
|
||||
style={isMobile ? { top: 0, paddingBottom: 0, maxWidth: '100vw' } : undefined}
|
||||
centered
|
||||
width={isMobile ? 'calc(100vw - 32px)' : 600}
|
||||
style={isMobile ? { maxWidth: 'calc(100vw - 32px)', margin: '0 16px' } : undefined}
|
||||
>
|
||||
<Form
|
||||
form={createForm}
|
||||
@@ -387,9 +387,9 @@ export default function WritingStyles() {
|
||||
setEditingStyle(null);
|
||||
}}
|
||||
footer={null}
|
||||
centered={!isMobile}
|
||||
width={isMobile ? '100%' : 600}
|
||||
style={isMobile ? { top: 0, paddingBottom: 0, maxWidth: '100vw' } : undefined}
|
||||
centered
|
||||
width={isMobile ? 'calc(100vw - 32px)' : 600}
|
||||
style={isMobile ? { maxWidth: 'calc(100vw - 32px)', margin: '0 16px' } : undefined}
|
||||
>
|
||||
<Form form={editForm} layout="vertical" onFinish={handleUpdate} style={{ marginTop: 16 }}>
|
||||
<Form.Item
|
||||
|
||||
Reference in New Issue
Block a user