feat(ai): AI 生成强化——流式进度、可取消、超时、增量修改、自动纠错与结果清洗

- 流式接收(SSE):实时显示已用时 / 思考字符数 / 输出字符数,可随时取消
- 空闲 180 秒无数据自动超时并给出可读提示(此前可能无限等待)
- 新增「修改当前工程」模式:在现有工程上增删改,不再只能整图替换
- 解析/校验失败时自动把错误反馈给模型修复(最多 2 次重试)
- 结果清洗:修正非法时长与越界角度、剔除无效依赖、层号归一化,并提示修正项
- 提示词优化:layer 按并行资源分配(避免层数爆炸)、命名与 id 引用约束
- 版本号 1.1.0+11
This commit is contained in:
2026-10-08 15:44:37 +08:00
parent 0a0ea3c0ec
commit 91c6132ef9
6 changed files with 428 additions and 60 deletions
+184 -29
View File
@@ -1,17 +1,21 @@
/// AI 生成时序图对话框。
///
/// 交互流程:填写 DeepSeek API Key → 选择模型(Flash / Pro)与推理强度
/// (高 / 最高)→ 在较大的自然语言输入框中描述工艺流程 → 点击「生成」,
/// 由 [AiClient] 调用大模型并解析为 [Project],成功后载入工作台。
/// 交互流程:填写 DeepSeek API Key → 选择模型(Flash / Pro)、推理强度
/// (高 / 最高)与生成方式(新建工程 / 在当前工程上修改)→ 在较大的自然语言
/// 输入框中描述工艺流程或修改要求 → 点击「生成」。
///
/// 生成过程为**流式**:实时显示已用时、思考与输出字符数,可随时取消;
/// 输出无法解析时自动让模型修复重试(最多两次)。
///
/// 安全:API Key 仅保存在内存(静态字段,便于本次会话内复用),
/// 不写入工程文件,也不会被日志输出。
library;
import 'dart:async';
import 'package:flutter/material.dart';
import '../ai/ai_client.dart';
import '../model/models.dart';
import '../state/app_controller.dart';
import '../theme/tokens.dart';
import 'dialogs.dart';
@@ -45,6 +49,22 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
bool _busy = false;
String? _error;
/// 生成方式:`new` = 新建工程(整图生成);`modify` = 在当前工程上修改。
String _mode = 'new';
/// 当前生成用的取消令牌(每次生成重建)。
AiCancelToken? _token;
/// 生成计时器(每秒刷新已用时)。
Timer? _ticker;
/// 已用时(秒)。
int _secs = 0;
/// 已接收的思考字符数(推理模型)。
int _thinkChars = 0;
/// 已接收的 JSON 输出字符数。
int _outChars = 0;
/// 当前第几次尝试(>0 表示正在自动修复上一次的输出)。
int _attempt = 0;
@override
void initState() {
super.initState();
@@ -54,32 +74,61 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
@override
void dispose() {
_ticker?.cancel();
_key.dispose();
_prompt.dispose();
super.dispose();
}
Future<void> _generate() async {
final AiCancelToken token = AiCancelToken();
setState(() {
_busy = true;
_error = null;
_secs = 0;
_thinkChars = 0;
_outChars = 0;
_attempt = 0;
_token = token;
});
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted) setState(() => _secs++);
});
try {
_sessionKey = _key.text.trim();
final Project p = await AiClient.generate(
final AiResult r = await AiClient.generate(
prompt: _prompt.text,
settings: AiSettings(apiKey: _sessionKey, model: _model, effort: _effort),
base: _mode == 'modify' ? widget.controller.project : null,
cancelToken: token,
onProgress: (AiProgress p) {
if (!mounted) return;
setState(() {
_thinkChars = p.reasoningChars;
_outChars = p.contentChars;
_attempt = p.attempt;
});
},
);
widget.controller.loadProject(p, dirty: true);
widget.controller.setStatus('AI 已生成时序图(${p.actions.length} 个动作)');
widget.controller.loadProject(r.project, dirty: true);
final String extra = r.warnings.isEmpty ? '' : '(${r.warnings.join('、')})';
widget.controller
.setStatus('AI 已生成时序图(${r.project.actions.length} 个动作)$extra');
if (mounted) Navigator.pop(context);
} catch (e) {
if (mounted) setState(() => _error = '$e');
} finally {
_ticker?.cancel();
_ticker = null;
if (mounted) setState(() => _busy = false);
}
}
/// 取消正在进行的生成(中断 HTTP 请求)。
void _cancel() {
_token?.cancel();
}
@override
Widget build(BuildContext context) {
final Tokens t = context.tok;
@@ -189,6 +238,27 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
),
const SizedBox(height: 14),
// 生成方式:新建整图 / 在当前工程上修改(增量)。
Row(
children: <Widget>[
Text('生成方式', style: TextStyle(color: t.sub, fontSize: 12.5)),
const SizedBox(width: 12),
SizedBox(width: 106, child: _modeChip(t, '新建工程', 'new')),
const SizedBox(width: 8),
SizedBox(width: 128, child: _modeChip(t, '修改当前工程', 'modify')),
const SizedBox(width: 12),
Expanded(
child: Text(
_mode == 'modify'
? '在现有工程上增删改,未涉及部分保持不变'
: '重新生成整张时序图(替换当前工程)',
style: TextStyle(color: t.dim, fontSize: 11),
),
),
],
),
const SizedBox(height: 14),
// 自然语言输入(大输入框)
Row(
children: <Widget>[
@@ -237,16 +307,70 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: t.danger.withValues(alpha: 0.12),
color: _error == '已取消' ? t.panel2 : t.danger.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(8),
border: Border.all(color: t.danger.withValues(alpha: 0.5)),
border: Border.all(
color: _error == '已取消' ? t.line2 : t.danger.withValues(alpha: 0.5)),
),
child: Text(_error!,
style: TextStyle(
color: _error == '已取消' ? t.sub : t.danger,
fontSize: 12,
height: 1.5)),
),
],
if (_busy) ...<Widget>[
const SizedBox(height: 10),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: t.line2),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
SizedBox(
width: 13,
height: 13,
child: CircularProgressIndicator(strokeWidth: 2, color: t.accent),
),
const SizedBox(width: 8),
Text(
_attempt > 0
? '格式有误,正在自动修复(第 ${_attempt + 1} 次尝试)· 已用时 $_secs 秒'
: '生成中… 已用时 $_secs 秒',
style: TextStyle(color: t.sub, fontSize: 12),
),
const Spacer(),
Text(
_thinkChars > 0 || _outChars > 0
? '思考 $_thinkChars 字 · 输出 $_outChars 字'
: '等待模型响应…',
style: TextStyle(color: t.dim, fontSize: 11),
),
],
),
const SizedBox(height: 8),
ClipRRect(
borderRadius: BorderRadius.circular(2),
child: LinearProgressIndicator(
minHeight: 4,
color: t.accent2,
backgroundColor: t.line2,
),
),
],
),
child: Text(_error!, style: TextStyle(color: t.danger, fontSize: 12, height: 1.5)),
),
],
const SizedBox(height: 14),
// 操作按钮
// 操作按钮:生成 ↔ 取消生成(流式过程中可随时中断)。
Row(
children: <Widget>[
Expanded(
@@ -255,25 +379,37 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
style: TextStyle(color: t.dim, fontSize: 11),
),
),
TextButton(onPressed: _busy ? null : () => Navigator.pop(context), child: const Text('取消')),
const SizedBox(width: 8),
FilledButton.icon(
onPressed: _busy ? null : _generate,
icon: _busy
? const SizedBox(
width: 15,
height: 15,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.auto_awesome, size: 16),
label: Text(_busy ? '生成中…' : '生成'),
style: FilledButton.styleFrom(
backgroundColor: t.accent2,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(9)),
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
),
TextButton(
onPressed: () {
if (_busy) _cancel();
Navigator.pop(context);
},
child: const Text('取消'),
),
const SizedBox(width: 8),
_busy
? FilledButton.icon(
onPressed: _cancel,
icon: const Icon(Icons.stop_circle_outlined, size: 16),
label: const Text('取消生成'),
style: FilledButton.styleFrom(
backgroundColor: t.danger,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(9)),
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
),
)
: FilledButton.icon(
onPressed: _generate,
icon: const Icon(Icons.auto_awesome, size: 16),
label: const Text('生成'),
style: FilledButton.styleFrom(
backgroundColor: t.accent2,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(9)),
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
),
),
],
),
],
@@ -301,4 +437,23 @@ class _AiGenerateDialogState extends State<AiGenerateDialog> {
),
);
}
/// 生成方式分段按钮(新建工程 / 修改当前工程);生成中不可切换。
Widget _modeChip(Tokens t, String label, String value) {
final bool active = _mode == value;
return InkWell(
onTap: _busy ? null : () => setState(() => _mode = value),
borderRadius: BorderRadius.circular(8),
child: Container(
height: 34,
alignment: Alignment.center,
decoration: BoxDecoration(
color: active ? t.accent.withValues(alpha: 0.18) : t.panel2,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: active ? t.accent : t.line2),
),
child: Text(label, style: TextStyle(color: active ? t.accent : t.sub, fontSize: 12)),
),
);
}
}
+1 -1
View File
@@ -2124,7 +2124,7 @@ class HelpDialog extends StatelessWidget {
Text('时序图设计 · Sequence Diagram Studio',
style: TextStyle(color: t.text, fontSize: 15, fontWeight: FontWeight.w700)),
const SizedBox(height: 4),
Text('V1.0.9(${android ? '安卓版' : 'Windows 版'})',
Text('V1.1.0(${android ? '安卓版' : 'Windows 版'})',
style: TextStyle(color: t.dim, fontSize: 12)),
],
),