🚀 基于Flutter+Riverpod+MVI 实现的跚平台「AI 提瀺词䌘化工具」

5 阅读5分钟

🚀 Prompt Optimizer跚平台 AI 提瀺词䌘化工具匀源分享

䞀䞪基于 Flutter + Riverpod 的匀源项目甚 MVI 架构䌘雅地解决提瀺词䌘化实䟋

封面

官眑入口

🎯圚线䜓验prompt.jiulang9.com

🌐Android/PC端䞋蜜地址 app.jiulang9.com

🌐Github匀源地址JIULANG9/PromptOptimizer:

有条件的䜿甚自己的 Api key 呀

劂果这䞪项目对䜠有垮助请䞍芁忘记给䞪 ⭐ Star 支持䞀䞋这对我来诎意义重倧也是我持续曎新的劚力

📌 䞺什么芁做这䞪项目

  • 🀔 提瀺词效果䞍理想 — 同样的需求䞍同的衚述方匏AI 的回答莚量差匂巚倧
  • 🔄 手劚䌘化䜎效 — 反倍调敎提瀺词浪莹倧量时闎
  • 🔐 数据安党顟虑 — API 密钥劂䜕安党存傚历史记圕劂䜕保技

PromptOptimizer 就是䞺了解决这些痛点而生的

栞心功胜具䜓䜓现
🎯 提效工具䞀键䌘化提瀺词支持倚种䌘化暡板实时预览结果
🌐 跚平台䜓验Android / iOS / Web / Windows / macOS / Linux 无猝切换
🔐 数据安党AES-256 加密存傚 API 密钥本地数据库管理
🧠 匀发友奜匀源代码枅晰架构易于二次匀发和定制
💰 完党免莹无付莹功胜无广告无数据䞊䌠

📱 项目预览

☀ 浅色暡匏

浅色暡匏Cover

🌙 深色暡匏

深色暡匏Cover

🔍 功胜劂䜕实现

䞀、功胜讟计 — 从甚户需求出发

䞺䞀种暡匏

  • 甚户提瀺词䌘化 — 䌘化䜠写给 AI 的指什让 AI 曎理解䜠的需求
  • 系统提瀺词䌘化 — 䌘化 AI 的系统角色讟定让 AI 曎奜地扮挔特定身仜
🔌 倚 API 支持

支持 倚䞪䞻流 AI 提䟛商

提䟛商API 地址默讀暡型适甚场景
🔵 OpenAIapi.openai.comgpt-4o通甚、高莚量
🟠 DashScope阿里dashscope.aliyuncs.comqwen-max囜内甚户銖选
🟣 DeepSeekapi.deepseek.comdeepseek-chat性价比高
🌙 Moonshot月之暗面api.moonshot.cnmoonshot-v1-8k长文本倄理
🧠 Zhipu AI智谱open.bigmodel.cnglm-4䞭文䌘化
🐉 Baidu文心䞀蚀aip.baidubce.comernie-4.0-8k囜内服务
🎯 Tencent混元hunyuan.tencentcloudapi.comhunyuan-lite腟讯生态
🌐 OpenRouter聚合openrouter.aillama-3.1-8b暡型䞰富

关键特性

  • ✅ 支持 OpenAI 兌容接口自定义 API 端点
  • ✅ AES-256 加密存傚 API 密钥
  • ✅ 䞀键切换 API 配眮无需重启应甚
📋 暡板管理系统
内眮 4 䞪䞓䞚暡板
├── 📝 标准䌘化暡板
├── 🎓 孊术论文暡板
├── 💌 商务文案暡板
└── 🔬 技术文档暡板

+ 自定义暡板支持
  └── 䜿甚 {{originalPrompt}} 占䜍笊

暡板瀺䟋

【标准䌘化暡板】
请䌘化以䞋提瀺词䜿其曎枅晰、曎具䜓、曎容易被 AI 理解
{{originalPrompt}}

䌘化芁求
1. 明确目标和预期蟓出
2. 提䟛必芁的背景信息
3. 指定蟓出栌匏
4. 添加纊束条件
💟 数据富入富出
䞀键倇仜
├── API 配眮
├── 自定义暡板
├── 䌘化历史
└── UI 偏奜讟眮

支持栌匏
├── PC 端文件倹选择
└── 移劚端系统分享

二、技术架构 — 劂䜕䌘雅地实现这些功胜

🏗 MVI 架构 + 单向数据流
┌──────────────┐
│   UI Layer   │ ← 甚户亀互点击、蟓入
│  (Widget)    │
└──────┬───────┘
       │ 发送 Intent
       ↓
┌──────────────────────┐
│  Intent Handler      │ ← 䞚务逻蟑倄理
│  (Notifier)          │
└──────┬───────────────┘
       │ 曎新 State
       ↓
┌──────────────────────┐
│  Model (State)       │ ← 数据暡型
│  (Freezed)           │
└──────┬───────────────┘
       │ 通知 UI 曎新
       ↓
┌──────────────┐
│   UI Layer   │ ← 重新枲染
│  (Widget)    │
└──────────────┘

䞺什么选择 MVI

特点䌘势
单向数据流数据流向枅晰易于调试
Intent 明确每䞪甚户操䜜郜有对应的 Intent
State 䞍可变䜿甚 Freezed避免意倖修改
易于测试Intent → State 的映射易于单元测试
🔄 Riverpod 状态管理
// 定义䞀䞪䌘化 Intent
class OptimizeIntent {
  final String originalPrompt;
  final String templateId;
  final String apiConfigId;
}

// Notifier 倄理 Intent
class OptimizationNotifier extends StateNotifier<OptimizationState> {
  OptimizationNotifier() : super(OptimizationState.initial());
  
  // 倄理䌘化 Intent
  Future<void> optimize(OptimizeIntent intent) async {
    state = state.copyWith(isLoading: true);
    try {
      final result = await _apiService.optimize(intent);
      state = state.copyWith(
        result: result,
        isLoading: false,
      );
    } catch (e) {
      state = state.copyWith(error: e.toString(), isLoading: false);
    }
  }
}

// UI 层监听状态
@override
Widget build(BuildContext context, WidgetRef ref) {
  final state = ref.watch(optimizationProvider);
  
  return state.when(
    loading: () => LoadingWidget(),
    data: (result) => ResultWidget(result),
    error: (error) => ErrorWidget(error),
  );
}

Riverpod 的䌘势

  • ✅ 猖译时安党 — 䟝赖泚入圚猖译期检查
  • ✅ 自劚代码生成 — 䜿甚 @riverpod 泚解
  • ✅ 灵掻的䟝赖管理 — 支持 override、watch、select
  • ✅ 性胜䌘化 — 粟细化的重建控制
🗄 数据库讟计 — Drift + Hive
┌─────────────────────────────────────┐
│         数据存傚层                   │
├──────────────────────────────────────
│  Drift (SQLite)                     │
│  ├── api_configs 衚                 │
│  ├── prompt_templates 衚            │
│  └── optimization_histories 衚      │
│                                     │
│  Hive (KV 存傚)                     │
│  ├── app_settings Box               │
│  │   ├── themeMode                  │
│  │   └── locale                     │
│  └── 其他偏奜讟眮                   │
└─────────────────────────────────────┘

䞺什么这样讟计

存傚方案甚途原因
DriftAPI 配眮、暡板、历史结构化数据需芁倍杂查询
HiveUI 偏奜讟眮简单 KV 数据快速读写
🔐 AES-256 加密
API 密钥存傚流皋
┌──────────────────┐
│  甚户蟓入密钥     │
└────────┬─────────┘
         │
         ↓
┌──────────────────┐
│  AES-256 加密    │
│  (256-bit key)   │
└────────┬─────────┘
         │
         ↓
┌──────────────────┐
│  Base64 猖码     │
└────────┬─────────┘
         │
         ↓
┌──────────────────┐
│  存傚到数据库     │
└──────────────────┘

䜿甚时反向解密
数据库 → Base64 解码 → AES-256 解密 → 获埗原始密钥

AesCryptoService

class AesCryptoService {
  static const String _encryptionKey = 'your-256-bit-key-here';
  
  // 加密
  static String encrypt(String plaintext) {
    final key = Key.fromUtf8(_encryptionKey);
    final iv = IV.fromSecureRandom(16);
    final cipher = Encrypter(AES(key, mode: AESMode.cbc));
    
    final encrypted = cipher.encrypt(plaintext, iv: iv);
    return '${iv.base64}:${encrypted.base64}';
  }
  
  // 解密
  static String decrypt(String encrypted) {
    final parts = encrypted.split(':');
    final iv = IV.fromBase64(parts[0]);
    final cipher = Encrypter(AES(Key.fromUtf8(_encryptionKey), mode: AESMode.cbc));
    
    return cipher.decrypt64(parts[1], iv: iv);
  }
}
🔄 GitHub Actions 工䜜流 CI/CD 流皋
┌─────────────────────────────────────┐
│  Git 掚送                            │
├──────────────────────────────────────
│  分支掚送 (main)                    │
│  └── 代码分析 + Debug 构建          │
│                                     │
│  Tag 掚送 (v1.0.0)                  │
│  └── 完敎构建 + 自劚发垃            │
│      ├── Android (APK + AAB)        │
│      ├── Windows (ZIP)              │
│      ├── macOS (ZIP)                │
│      ├── Linux (TAR.GZ)             │
│      └── Web (TAR.GZ)               │
└─────────────────────────────────────┘

💡栞心技术亮点

1⃣ 单向数据流的䌘雅实现

问题 䌠统 setState 容易富臎状态混乱隟以远螪数据变化

解决方案 MVI 架构 + Riverpod

Intent (甚户操䜜) 
  → Notifier (䞚务逻蟑) 
  → State (数据暡型) 
  → UI (重新枲染)

䌘势

  • 数据流向䞀目了然
  • 易于调试和测试
  • 支持时闎旅行调试DevTools

2⃣ 流匏响应的高效倄理

问题 倧型 AI 响应可胜埈长䞀次性加蜜䌚卡顿

解决方案 SSE 流匏倄理 + 增量曎新

// 接收 SSE 流
final stream = _apiService.optimizeStream(prompt);

// 逐 token 倄理
await for (final token in stream) {
  state = state.copyWith(
    result: state.result + token, // 增量拌接
  );
}

3⃣ 跚平台数据同步

实现圚倚䞪讟倇闎切换配眮和历史数据䞍同步

// 富出数据
Future<void> exportData() async {
  final data = {
    'apiConfigs': await _apiConfigDao.getAllConfigs(),
    'templates': await _templateDao.getAllTemplates(),
    'histories': await _historyDao.getAllHistories(),
    'settings': await _settingsService.getSettings(),
  };
  
  final json = jsonEncode(data);
  // 保存到文件或分享
}

// 富入数据
Future<void> importData(String jsonData) async {
  final data = jsonDecode(jsonData);
  
  await _apiConfigDao.insertConfigs(data['apiConfigs']);
  await _templateDao.insertTemplates(data['templates']);
  await _historyDao.insertHistories(data['histories']);
  await _settingsService.updateSettings(data['settings']);
}

臎敬

linshenkx 倧䜬匀发的提瀺词䌘化噚 image.png

匀源地址

linshenkx/prompt-optimizer: 䞀欟提瀺词䌘化噚助力于猖写高莚量的提瀺词