Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat(web): Is this request related to a challenge you're experiencing? Tell me about your story (#261)
布莱恩·奥夫托·杰森张98d9a7baafda
diff --git a/project/web/AI_FEATURES_IMPLEMENTATION.md b/project/web/AI_FEATURES_IMPLEMENTATION.mdnew file mode 100644index 00000000..507620cc--- /dev/null+++ b/project/web/AI_FEATURES_IMPLEMENTATION.md@@ -0,0 +1,219 @@+# AI功能模块实现文档++## 概述+本次更新将发布弹窗的左侧"写作助手"改造为全新的"AI功能模块",提供了多种AI辅助创作功能,并增加了划词快捷操作。++## 新增功能++### 1. AI助手功能模块+位置:左侧面板(PublishDialogAi组件)++#### 支持的AI功能:+1. **缩写 (Shorten Text)**+ - 将过长的文本缩短,符合平台要求+ - 保持核心信息不变++2. **扩写 (Expand Text)**+ - 扩展文本内容,增加更多细节+ - 支持自定义默认提示词(可折叠编辑)++3. **润色 (Polish Writing)**+ - 优化文本表达,使其更流畅专业+ - 支持自定义默认提示词(可折叠编辑)++4. **翻译 (Translate)**+ - 将文本翻译成目标语言+ - 需要用户输入目标语言(如:英文、日文、法文)++5. **制图 (Generate Image)**+ - 根据文本生成图片创作提示词+ - 界面改为图片生成模式++6. **创作视频 (Generate Video)**+ - 根据文本生成视频创作提示词+ - 界面改为视频生成模式++### 2. 划词快捷操作+位置:中间编辑区域(TextSelectionToolbar组件)++**使用方法:**+- 在文本编辑区域选中任意文本+- 自动弹出快捷操作工具栏+- 点击对应功能按钮,将选中文本发送到左侧AI助手处理++**支持的快捷操作:**+- 缩写、扩写、润色、翻译、制图、创作视频++### 3. AI对话同步功能+**功能说明:**+- AI返回结果后,每条对话下方显示"同步到编辑器"按钮+- 点击后将AI生成的内容同步到中间的文本编辑器+- 支持复制到剪贴板功能++**同步逻辑:**+- 单账号模式:直接更新该账号的描述内容+- 多账号模式(第一步):更新公共参数的描述+- 多账号模式(第二步):更新当前展开账号的描述++## 技术实现++### 新增/修改的文件++#### 1. API接口 (`src/api/ai.ts`)+- 新增 `aiChatStream` 函数+- 支持流式响应+- 自动携带认证token和语言设置+- 接口地址:`https://aitoearn.ai/api/ai/chat`++**请求参数:**+```typescript+{+ messages: Array<{ role: string, content: string }>,+ stream: boolean,+ model: string,+ temperature: number,+ presence_penalty: number,+ frequency_penalty: number,+ top_p: number,+ max_tokens: number+}+```++#### 2. AI助手组件 (`src/components/PublishDialog/compoents/PublishDialogAi.tsx`)+- 重构为全新的AI功能模块+- 使用Tabs切换不同AI功能+- 实现流式聊天响应+- 支持多会话管理(每个功能独立会话)+- 提供同步和复制功能++**主要功能:**+- `processText(text, action)`: 接收划词选中的文本并处理+- 流式响应显示+- 会话历史记录+- 自定义提示词编辑(扩写、润色)++#### 3. 划词工具栏 (`src/components/PublishDialog/compoents/TextSelectionToolbar.tsx`)+- 监听文本选择事件+- 自动定位到选中文本上方+- 提供快捷操作按钮+- 点击外部自动隐藏++**样式文件:** `TextSelectionToolbar.module.scss`+- 优雅的动画效果+- 响应式设计++#### 4. 主弹窗组件 (`src/components/PublishDialog/index.tsx`)+- 集成划词工具栏+- 添加AI助手ref引用+- 实现 `handleTextSelection` 处理划词操作+- 实现 `handleSyncToEditor` 同步内容到编辑器+- 支持左侧AI面板的开关++#### 5. 国际化翻译 (`src/app/i18n/locales/zh-CN/publish.json`)+- 新增 `aiAssistant` 标题+- 新增 `aiFeatures` 相关翻译+ - 各功能名称+ - 提示词模板+ - 交互文案++## 使用流程++### 方式一:直接使用AI功能+1. 点击发布弹窗左上角打开AI助手+2. 选择需要的功能标签(缩写、扩写、润色等)+3. 输入内容并发送+4. 等待AI响应+5. 点击"同步到编辑器"应用结果++### 方式二:划词快捷操作+1. 在中间编辑区域选中需要处理的文本+2. 在弹出的工具栏中点击对应功能+3. 左侧AI助手自动打开并开始处理+4. 查看结果并同步到编辑器++## 布局说明++```+┌─────────────────────────────────────────────────────────┐+│ 发布作品弹窗 │+├─────────────┬─────────────────┬────────────────────────┤+│ │ │ │+│ AI助手 │ 发布编辑区域 │ 预览区域 │+│ (左侧面板) │ (中间面板) │ (右侧面板) │+│ │ │ │+│ - 缩写 │ - 账号选择 │ - 内容预览 │+│ - 扩写 │ - 文本编辑 │ - 格式预览 │+│ - 润色 │ - 媒体上传 │ │+│ - 翻译 │ - 参数设置 │ │+│ - 制图 │ │ │+│ - 视频 │ [划词工具栏] │ │+│ │ │ │+└─────────────┴─────────────────┴────────────────────────┘+```++## 注意事项++1. **API认证**+ - AI聊天接口需要用户登录+ - 自动携带Bearer Token+ - 401错误会自动触发登录流程++2. **流式响应**+ - 使用SSE(Server-Sent Events)流式传输+ - 实时显示AI生成内容+ - 网络中断会自动停止++3. **会话管理**+ - 每个AI功能维护独立会话+ - 切换标签保留历史记录+ - 刷新页面清空所有会话++4. **性能优化**+ - 使用React.memo避免不必要的重渲染+ - 流式响应减少等待时间+ - 防抖处理文本选择事件++## 后续优化建议++1. **会话持久化**+ - 将会话历史保存到localStorage+ - 支持会话历史查看和管理++2. **提示词库**+ - 提供预设提示词模板+ - 支持用户自定义并保存提示词++3. **多轮对话**+ - 支持连续对话+ - 上下文理解优化++4. **图片和视频生成**+ - 完善制图和视频生成功能+ - 集成实际的图片/视频生成API++5. **快捷键支持**+ - 添加键盘快捷键+ - 提升操作效率++## 测试建议++1. 测试各个AI功能的正常响应+2. 测试划词工具栏在不同场景下的表现+3. 测试同步功能在单账号/多账号模式下的正确性+4. 测试网络异常情况的错误处理+5. 测试流式响应的中断和恢复+6. 测试在不同屏幕尺寸下的响应式布局++## 问题反馈++如遇到问题,请检查:+1. 用户是否已登录+2. 网络连接是否正常+3. API接口是否可访问+4. 浏览器控制台是否有错误信息++---++**实现日期:** 2025-11-18+**版本:** 1.0.0+diff --git a/project/web/README.md b/project/web/README.mdindex f9d71382..59e6b554 100644--- a/project/web/README.md+++ b/project/web/README.md@@ -7,3 +7,7 @@ https://aitoearn.ai## Test api foxhttps://app.apifox.com/project/6987843++++diff --git a/project/web/package.json b/project/web/package.jsonindex 698bfb84..2bfe3d9f 100644--- a/project/web/package.json+++ b/project/web/package.json@@ -58,6 +58,7 @@"react-i18next": "15.4.1","react-icons": "^5.5.0","react-infinite-scroll-component": "^6.1.0",+ "react-markdown": "^10.1.0","react-qrcode-logo": "^4.0.0","react-sortablejs": "^6.1.4","react-transition-group": "^4.4.5",diff --git a/project/web/pnpm-lock.yaml b/project/web/pnpm-lock.yamlindex 5b62f3d7..3271f76d 100644--- a/project/web/pnpm-lock.yaml+++ b/project/web/pnpm-lock.yaml@@ -128,6 +128,9 @@ importers:react-infinite-scroll-component:specifier: ^6.1.0version: 6.1.0([email protected])+ react-markdown:+ specifier: ^10.1.0+ version: 10.1.0(@types/[email protected])([email protected])react-qrcode-logo:specifier: ^4.0.0@@ -1719,9 +1722,15 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-FXx2pKgId/WyYo2jXw63kk7/+TY7u7AziEJxJAnSFzHlqTAS3Ync6SvgYAN/k4/PQpnnVuzoMuVnByKK2qp0ag==}+ '@types/[email protected]':+ resolution: {integrity: sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg==}+'@types/[email protected]':resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}+ '@types/[email protected]':+ resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==}+'@types/[email protected]':resolution: {integrity: sha512-PQTyIulDkIDro8P+IHbKCsw7U2xxBYflVzW/FgWdCAePD9xGSidgA76/GeJ6lBKoblyhf9pBY763gbrN+1dI8g==}peerDependencies:@@ -1770,6 +1779,9 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-b79830lW+RZfwaztgs1aVPgbasJ8e7AXtZYHTELNXZPsERt4ymJdjV4OccDbHQAvHrCcFpbF78jkm0R6h/pZVg==}+ '@types/[email protected]':+ resolution: {integrity: sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA==}+'@types/[email protected]':resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==}@@ -1839,6 +1851,9 @@ packages:resolution: {integrity: sha512-/++5CYLQqsO9HFGLI7APrxBJYo+5OCMpViuhV8q5/Qa3o5mMrF//eQHks+PXcsAVaLdn817fMuS7zqoXNNZGaw==}engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}+ '@ungap/[email protected]':+ resolution: {integrity: sha512-WmoN8qaIAo7WTYWbAZuG8PYEhn5fkz7dZrqTBZ7dtt//lL2Gwms1IcnQ5yHqjDfX8Ft5j4YzDM23f87zBfDe9g==}+'@vitest/[email protected]':resolution: {integrity: sha512-7rENIOOk6eqgtf2FsQWilgfQheeuGRiU3lB2H+pmtbcA4jOdtBfQHVqS7f/KCOJ2JKjrIJgow8yUeANi34rj9w==}engines: {node: '>=18'}@@ -2140,6 +2155,9 @@ packages:peerDependencies:'@babel/core': ^7.4.0 || ^8.0.0-0 <8.0.0+ resolution: {integrity: sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw==}+resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}@@ -2254,9 +2272,18 @@ packages:resolution: {integrity: sha512-HRQyTk2/YPEkt9TnUPbOpr64Uw3KOicFWPVBb+xiHvd6eBx/qPr9xqfBFDT8P2vWsvvz4jbEkfDe71W3VyNu2w==}+ resolution: {integrity: sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA==}++ resolution: {integrity: sha512-RpPp0asT/6ufRm//AJVwpViZbGM/MkjQFxJccQRHmISF/22NBtsHqAWmL+/pmkPWoIUJdWyeVleTl1wydHATVQ==}+resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==}+ resolution: {integrity: sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw==}+resolution: {integrity: sha512-9v9kG0LvzrlcungtnJtpGNxY+fzECQKhK4EGJX2vByejiMX84MFNQw4UxPJl3bFbTMw+Dfs37XaIkCwTZfLh4g==}@@ -2312,6 +2339,9 @@ packages:resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}engines: {node: '>= 0.8'}+ resolution: {integrity: sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==}+resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==}@@ -2966,6 +2996,9 @@ packages:resolution: {integrity: sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==}engines: {node: '>=4.0'}+ resolution: {integrity: sha512-hFtqIDZTIUZ9BXLb8y4pYGyk6+wekIivNVTcmvk8NoOh+VeRn5y6cEHzbURrWbfp1fIqdVipilzj+lfaadNZmg==}+resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==}@@ -3245,6 +3278,12 @@ packages:resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==}engines: {node: '>= 0.4'}+ resolution: {integrity: sha512-zl6s8LwNyo1P9uw+XJGvZtdFF1GdAkOg8ujOw+4Pyb76874fLps4ueHXDhXWdk6YHQ6OgUtinliG7RsYvCbbBg==}++ resolution: {integrity: sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw==}+resolution: {integrity: sha512-0wUoCcLp+5Ev5pDW2OriHC2MJCbwLwuRx+gAqMTOkGKJJiBCLjtrvy4PWUGn6MIVefecRpzoOZ/UV6iGdOr+Cw==}@@ -3263,6 +3302,9 @@ packages:resolution: {integrity: sha512-KknJ50kTInJ7qIScF3jeaFRpMpE8/lfiTdzf/twXyPBLAGrLRTmkz3AdTnKeh40X8k9L2fdYwEp/42WGXIRGcg==}+ resolution: {integrity: sha512-ol6UPyBWqsrO6EJySPz2O7ZSr856WDrEzM5zMqp+FJJLGMW35cLYmmZnl0vztAZxRUoNZJFTCohfjuIJ8I4QBQ==}+resolution: {integrity: sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==}@@ -3344,6 +3386,9 @@ packages:resolution: {integrity: sha512-X7rqawQBvfdjS10YU1y1YVreA3SsLrW9dX2CewP2EbBJM4ypVNLDkO5y04gejPwKIY9lR+7r9gn3rFPt/kmWFg==}engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0}+ resolution: {integrity: sha512-Nb2ctOyNR8DqQoR0OwRG95uNWIC0C1lCgf5Naz5H6Ji72KZ8OcFZLz2P5sNgwlyoJ8Yif11oMuYs5pBQa86csA==}+resolution: {integrity: sha512-lhYo5qNTQp3EvSSp3sRvXMbVQTLrvGV6DycRMJ5dm2BLMiJ30wpXKdDdgX+GmJZ5uQMucwRKHamXSst3Sj/Giw==}@@ -3355,6 +3400,12 @@ packages:resolution: {integrity: sha512-58yWmlHpp7VYfcdTwMTvwMmqx/Elfxjd9RXTDyMsbL7lLWmhMylLEqiYVLKuLzOZqVgiWXD9MfR62Vv89VRxkw==}engines: {node: '>=4'}+ resolution: {integrity: sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==}++ resolution: {integrity: sha512-hmbYhX/9MUMF5uh7tOXyK/n0ZvWpad5caBA17GsC6vyuCqaWliRG5K1qS9inmUhEMaOBIW7/whAnSwveW/LtZw==}+resolution: {integrity: sha512-DDfANUiiG2wC1qawP66qlTugJeL5HyzMpfr8lLK+jMQirGzNod0B12cFB/9q838Ru27sBwfw78/rdoU7RERz6A==}engines: {node: '>= 0.4'}@@ -3394,6 +3445,9 @@ packages:resolution: {integrity: sha512-PwwhEakHVKTdRNVOw+/Gyh0+MzlCl4R6qKvkhuvLtPMggI1WAHt9sOwZxQLSGpUaDnrdyDsomoRgNnCfKNSXXg==}engines: {node: '>= 0.4'}+ resolution: {integrity: sha512-AAB9hiomQs5DXWcRB1rqsxGUstbRroFOPPVAomNk/3XHR5JyEZChOyTWe2oayKnsSsr/kcGqF+z6yuH6HHpN0A==}+resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}engines: {node: '>=0.10.0'}@@ -3414,6 +3468,9 @@ packages:resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==}engines: {node: '>=0.10.0'}+ resolution: {integrity: sha512-DgZQp241c8oO6cA1SbTEWiXeoxV42vlcJxgH+B3hi1AiqqKruZR3ZGF8In3fj4+/y/7rHvlOZLZtgJ/4ttYGZg==}+resolution: {integrity: sha512-LkHEOGVZZXxGl8vDs+10k3DvP++SEoYEAJLRk6buTFi6kD7QekThV7xHS0j6gpnUCQ0zpud/gMDGiV4dQneLTg==}peerDependencies:@@ -3436,6 +3493,10 @@ packages:resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==}engines: {node: '>=0.12.0'}+ resolution: {integrity: sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==}+ engines: {node: '>=12'}+resolution: {integrity: sha512-MjYsKHO5O7mCsmRGxWcLWheFqN9DJ/2TmngvjKXihe6efViPqc274+Fx/4fYj/r03+ESvBdTXK0V6tA3rgez1g==}engines: {node: '>= 0.4'}@@ -3725,9 +3786,21 @@ packages:resolution: {integrity: sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==}+ resolution: {integrity: sha512-J6f+9hUp+ldTZqKRSg7Vw5V6MqjATc+3E4gf3CFNcuZNWD8XdyI6zQ8GqH7f8169MM6P7hMBRDVGnn7oHB9kXQ==}++ resolution: {integrity: sha512-lj/z8v0r6ZtsN/cGNNtemmmfoLAFZnjMbNyLzBafjzikOM+glrjNHPlf6lQDOTccj9n5b0PPihEBbhneMyGs1Q==}++ resolution: {integrity: sha512-EcmOpxsZ96CvlP03NghtH1EsLtr0n9Tm4lPUJUBccV9RwUOneqSycg19n5HGzCf+10LozMRSObtVr3ee1WoHtg==}+resolution: {integrity: sha512-TqICwyvJJpBwvGAMZjj4J2n0X8QWp21b9l0o7eXyVJ25YNWYbJDVIyD1bZXE6WtV6RmKJVYmQAKWa0zWOABz2w==}+ resolution: {integrity: sha512-QGYKEuUsYT9ykKBCMOEDLsU5JRObWQusAolFMeko/tYPufNkRffBAQjIE+99jbA87xv6FgmjLtwjh9wBWajwAA==}+resolution: {integrity: sha512-xj68wMTvGXVOKonmog6LwyJKrYXZPvlwabaryTjLh9LuvovB/KAH+kvi8Gjj+7rJjsFi23nkUxRQv1KqSroMqA==}@@ -4056,6 +4129,9 @@ packages:resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==}engines: {node: '>=6'}+ resolution: {integrity: sha512-GG2AQYWoLgL877gQIKeRPGO1xF9+eG1ujIb5soS5gPvLQ1y2o8FL90w2QWNdf9I361Mpp7726c+lj3U0qK1uGw==}+resolution: {integrity: sha512-RmVuCHWsfu0QPNW+mraxh/xjQVw/lhUCUru8Zni3Ctq3AoMhpDTq0OVdKS6iesd6Kqb7viCV3isAL43dciOSog==}engines: {node: '>=14'}@@ -4192,6 +4268,9 @@ packages:resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==}+ resolution: {integrity: sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ==}+resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==}@@ -4514,6 +4593,12 @@ packages:resolution: {integrity: sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==}+ resolution: {integrity: sha512-qKxVopLT/TyA6BX3Ue5NwabOsAzm0Q7kAPwq6L+wWDwisYs7R8vZ0nRXqq6rkueboxpkjvLGU9fWifiX/ZZFxQ==}+ peerDependencies:+ '@types/react': '>=18'+ react: '>=18'+resolution: {integrity: sha512-TcDdsJQe7P0OY7uA7Do4Z0DfIIjjqx81RbBGQY+90T2Ba42pUCx/cSI2UTwPPoH9WwE0StLb8A98mFgKIAI4JQ==}peerDependencies:@@ -4630,6 +4715,12 @@ packages:resolution: {integrity: sha512-NZQZdC5wOE/H3UT28fVGL+ikOZcEzfMGk/c3iN9UGxzWHMa1op7274oyiUVrAG4B2EuFhus8SvkaYnhvW92p9Q==}hasBin: true+ resolution: {integrity: sha512-FCxlKLNGknS5ba/1lmpYijMUzX2esxW5xQqjWxw2eHFfS2MSdaHVINFmhjo+qN1WhZhNimq0dZATN9pH0IDrpA==}++ resolution: {integrity: sha512-Dh7l57ianaEoIpzbp0PC9UKAdCSVklD8E5Rpw7ETfbTl3FqcOOgq5q2LVDhgGCkaBv7p24JXikPdvhhmHvKMsw==}+resolution: {integrity: sha512-TTbAfBBRdWD7aNNOoVOBH4pN/KigV6LyapYNNlAPA8JwbovRti1E88m3sYAwsLi5ryhPKsE9APwnjFTgdUjTpw==}engines: {node: '>=0.10.0'}@@ -4848,6 +4939,9 @@ packages:resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==}engines: {node: '>=0.10.0'}+ resolution: {integrity: sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q==}+resolution: {integrity: sha512-PiU42r+xO4UbUS1buo3LPJkjlO7430Xn5SVAhdpzzsPHsjbYVflnnFdATgabnLude+Cqu25p6N+g2lw/PFsa4w==}@@ -4930,6 +5024,9 @@ packages:resolution: {integrity: sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==}+ resolution: {integrity: sha512-IwfBptatlO+QCJUo19AqvrPNqlVMpW9YEL2LIVY+Rpv2qsjCGxaDLNRgeGsQWJhfItebuJhsGSLjaBbNSQ+ieg==}+resolution: {integrity: sha512-4/Fc8WshjKl/6MctVvdcdH3pQpbSP3J481hMUo9qMSpEK+piDYKYg8oSTGGKFMt2AyeZCQCnGF1uMq3i/xg/wQ==}@@ -4949,6 +5046,12 @@ packages:resolution: {integrity: sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==}engines: {node: '>=8'}+ resolution: {integrity: sha512-RjQetxJrrUJLQPHbLku6U/ocGtzyjbJMP9lCNK7Ag0CNh690nSH8woqWH9u16nMjYBAok+i7JO1NP2pOy8IsPQ==}++ resolution: {integrity: sha512-LIN7rULI0jBscWQYaSswptyderlarFkjQ+t79nzty8tcIAceVomEVlLzH5VP4Cmsv6MtKhs7qaAiwlcp+Mgaxw==}+resolution: {integrity: sha512-pW7uC1l4mBZ8ugbiZrcIsiIvVx1UmTfw7UkC3Um2tmfUq9Bhk8IiyEIPl6F8agHgjzku6j0xQEZbfA5uSgSaCw==}engines: {node: '>= 12.0.0'}@@ -5087,6 +5190,12 @@ packages:resolution: {integrity: sha512-dTpowEjclQ7Kgx5SdBkqRzVhERQXov8/l9Ft9dVM9fmg0W0KQSVaXX9T4i6twCPNtYiZM53lpSSUAwJbFPOHxA==}+ resolution: {integrity: sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg==}++ resolution: {integrity: sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==}+resolution: {integrity: sha512-CUgTZL1irw8u29bzrOD/nH85jqyc74D6SshFgujOIA7osm2Rz7dYH77agkx7H4FBNxDq7Cjf+IjaX/8zwFW+ZQ==}engines: {node: '>=18.12'}@@ -5180,9 +5289,15 @@ packages:resolution: {integrity: sha512-6t3foTQI9qne+OZoVQB/8x8rk2k1eVy1gRXhV3oFQ5T6R1dqQ1xtin3XqSlx3+ATBkliTaR/hHyJBm+LVPNM8w==}engines: {node: '>=4'}+ resolution: {integrity: sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA==}+resolution: {integrity: sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g==}+ resolution: {integrity: sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA==}+resolution: {integrity: sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==}@@ -5229,6 +5344,12 @@ packages:resolution: {integrity: sha512-ZZKSmDAEFOijERBLkmYfJ+vmk3w+7hOLYDNkRCuRuMJGEmqYNCNLyBBFwWKVMhfwaEF3WOd0Zlw86U/WC/+nYw==}engines: {'0': node >=0.6.0}+ resolution: {integrity: sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw==}++ resolution: {integrity: sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q==}+resolution: {integrity: sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==}engines: {node: '>=0.10.0'}@@ -5582,7 +5703,7 @@ snapshots:'@babel/types': 7.28.4'@jridgewell/remapping': 2.3.5convert-source-map: 2.0.0- debug: 4.4.1+ debug: 4.4.3gensync: 1.0.0-beta.2json5: 2.2.3semver: 6.3.1@@ -7238,8 +7359,16 @@ snapshots:'@types/estree': 1.0.8'@types/json-schema': 7.0.15+ '@types/[email protected]':+ dependencies:+ '@types/estree': 1.0.8+'@types/[email protected]': {}+ '@types/[email protected]':+ dependencies:+ '@types/unist': 3.0.3+'@types/[email protected](@types/[email protected])':dependencies:'@types/react': 18.3.23@@ -7282,6 +7411,8 @@ snapshots:'@types/[email protected]': {}+ '@types/[email protected]': {}+'@types/[email protected]': {}'@types/[email protected]': {}@@ -7309,7 +7440,7 @@ snapshots:'@typescript-eslint/types': 8.46.4'@typescript-eslint/typescript-estree': 8.46.4([email protected])'@typescript-eslint/visitor-keys': 8.46.4- debug: 4.4.1+ debug: 4.4.3eslint: 9.39.1typescript: 5.9.3transitivePeerDependencies:@@ -7381,6 +7512,8 @@ snapshots:'@typescript-eslint/types': 8.46.4eslint-visitor-keys: 4.2.1+ '@ungap/[email protected]': {}+dependencies:'@typescript-eslint/scope-manager': 8.46.4@@ -7800,6 +7933,8 @@ snapshots:transitivePeerDependencies:- supports-color+ [email protected]: {}+@@ -7912,8 +8047,14 @@ snapshots:+ [email protected]: {}++ [email protected]: {}++ [email protected]: {}+dependencies:boolbase: 1.0.0@@ -7969,6 +8110,8 @@ snapshots:dependencies:delayed-stream: 1.0.0+ [email protected]: {}+@@ -8717,7 +8860,7 @@ snapshots:dependencies:- debug: 4.4.1+ debug: 4.4.3eslint: 9.39.1eslint-compat-utils: 0.6.5([email protected])lodash: 4.17.21@@ -8769,7 +8912,7 @@ snapshots:dependencies:- debug: 4.4.1+ debug: 4.4.3diff-sequences: 27.5.1escape-string-regexp: 4.0.0eslint: 9.39.1@@ -8877,6 +9020,8 @@ snapshots:+ [email protected]: {}+@@ -8885,8 +9030,7 @@ snapshots:- optional: true+ [email protected]: {}optional: true@@ -9173,6 +9317,30 @@ snapshots:dependencies:function-bind: 1.1.2+ dependencies:+ '@types/estree': 1.0.8+ '@types/hast': 3.0.4+ '@types/unist': 3.0.3+ comma-separated-tokens: 2.0.3+ devlop: 1.1.0+ estree-util-is-identifier-name: 3.0.0+ hast-util-whitespace: 3.0.0+ mdast-util-mdx-expression: 2.0.1+ mdast-util-mdx-jsx: 3.2.0+ mdast-util-mdxjs-esm: 2.0.1+ property-information: 7.1.0+ space-separated-tokens: 2.0.2+ style-to-js: 1.1.21+ unist-util-position: 5.0.0+ vfile-message: 4.0.3+ transitivePeerDependencies:+ - supports-color++ dependencies:+ '@types/hast': 3.0.4+@@ -9194,6 +9362,8 @@ snapshots:dependencies:void-elements: 3.1.0+ [email protected]: {}+dependencies:domelementtype: 2.3.0@@ -9280,6 +9450,8 @@ snapshots:+ [email protected]: {}+dependencies:css-in-js-utils: 3.1.0@@ -9293,6 +9465,13 @@ snapshots:optional: true+ [email protected]: {}++ dependencies:+ is-alphabetical: 2.0.1+ is-decimal: 2.0.1+dependencies:call-bind: 1.0.8@@ -9339,6 +9518,8 @@ snapshots:call-bound: 1.0.4has-tostringtag: 1.0.2+ [email protected]: {}+@@ -9358,6 +9539,8 @@ snapshots:dependencies:is-extglob: 2.1.1+ [email protected]: {}+dependencies:'@typescript-eslint/type-utils': 8.46.4([email protected])([email protected])@@ -9380,6 +9563,8 @@ snapshots:+ [email protected]: {}+dependencies:call-bound: 1.0.4@@ -9777,11 +9962,62 @@ snapshots:transitivePeerDependencies:- supports-color+ dependencies:+ '@types/estree-jsx': 1.0.5+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ devlop: 1.1.0+ mdast-util-from-markdown: 2.0.2+ mdast-util-to-markdown: 2.1.2+ transitivePeerDependencies:+ - supports-color++ dependencies:+ '@types/estree-jsx': 1.0.5+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ '@types/unist': 3.0.3+ ccount: 2.0.1+ devlop: 1.1.0+ mdast-util-from-markdown: 2.0.2+ mdast-util-to-markdown: 2.1.2+ parse-entities: 4.0.2+ stringify-entities: 4.0.4+ unist-util-stringify-position: 4.0.0+ vfile-message: 4.0.3+ transitivePeerDependencies:+ - supports-color++ dependencies:+ '@types/estree-jsx': 1.0.5+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ devlop: 1.1.0+ mdast-util-from-markdown: 2.0.2+ mdast-util-to-markdown: 2.1.2+ transitivePeerDependencies:+ - supports-color+dependencies:'@types/mdast': 4.0.4unist-util-is: 6.0.1+ dependencies:+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ '@ungap/structured-clone': 1.3.0+ devlop: 1.1.0+ micromark-util-sanitize-uri: 2.0.1+ trim-lines: 3.0.1+ unist-util-position: 5.0.0+ unist-util-visit: 5.0.0+ vfile: 6.0.3+dependencies:'@types/mdast': 4.0.4@@ -10248,6 +10484,16 @@ snapshots:dependencies:callsites: 3.1.0+ dependencies:+ '@types/unist': 2.0.11+ character-entities-legacy: 3.0.0+ character-reference-invalid: 2.0.1+ decode-named-character-reference: 1.2.0+ is-alphanumerical: 2.0.1+ is-decimal: 2.0.1+ is-hexadecimal: 2.0.1+@@ -10376,6 +10622,8 @@ snapshots:object-assign: 4.1.1react-is: 16.13.1+ [email protected]: {}+@@ -10779,6 +11027,24 @@ snapshots:+ dependencies:+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ '@types/react': 18.3.23+ devlop: 1.1.0+ hast-util-to-jsx-runtime: 2.3.6+ html-url-attributes: 3.0.1+ mdast-util-to-hast: 13.2.0+ react: 18.3.1+ remark-parse: 11.0.0+ remark-rehype: 11.1.2+ unified: 11.0.5+ unist-util-visit: 5.0.0+ vfile: 6.0.3+ transitivePeerDependencies:+ - supports-color+dependencies:qrcode-generator: 2.0.4@@ -10920,6 +11186,23 @@ snapshots:dependencies:jsesc: 3.1.0+ dependencies:+ '@types/mdast': 4.0.4+ mdast-util-from-markdown: 2.0.2+ micromark-util-types: 2.0.2+ unified: 11.0.5+ transitivePeerDependencies:+ - supports-color++ dependencies:+ '@types/hast': 3.0.4+ '@types/mdast': 4.0.4+ mdast-util-to-hast: 13.2.0+ unified: 11.0.5+ vfile: 6.0.3+dependencies:lodash: 4.17.21@@ -11200,6 +11483,8 @@ snapshots:+ [email protected]: {}+@@ -11324,6 +11609,11 @@ snapshots:safe-buffer: 5.2.1optional: true+ dependencies:+ character-entities-html4: 2.1.0+ character-entities-legacy: 3.0.0+dependencies:iconv-lite: 0.4.24@@ -11340,6 +11630,14 @@ snapshots:+ dependencies:+ style-to-object: 1.0.14++ dependencies:+ inline-style-parser: 0.2.7+dependencies:client-only: 0.0.1@@ -11469,6 +11767,10 @@ snapshots:punycode: 2.3.1optional: true+ [email protected]: {}++ [email protected]: {}+dependencies:typescript: 5.9.3@@ -11576,10 +11878,24 @@ snapshots:+ dependencies:+ '@types/unist': 3.0.3+ bail: 2.0.2+ devlop: 1.1.0+ extend: 3.0.2+ is-plain-obj: 4.1.0+ trough: 2.2.0+ vfile: 6.0.3+dependencies:'@types/unist': 3.0.3+ dependencies:+ '@types/unist': 3.0.3+dependencies:'@types/unist': 3.0.3@@ -11631,11 +11947,21 @@ snapshots:extsprintf: 1.3.0optional: true+ dependencies:+ '@types/unist': 3.0.3+ unist-util-stringify-position: 4.0.0++ dependencies:+ '@types/unist': 3.0.3+ vfile-message: 4.0.3+dependencies:- debug: 4.4.1+ debug: 4.4.3eslint: 9.39.1eslint-scope: 8.3.0eslint-visitor-keys: 4.2.0diff --git a/project/web/src/api/ai.ts b/project/web/src/api/ai.tsindex ae95b392..6e7af0c2 100644--- a/project/web/src/api/ai.ts+++ b/project/web/src/api/ai.ts@@ -1,5 +1,6 @@import { getOssUrl } from '@/utils/oss'import http from '@/utils/request'+import { useUserStore } from '@/store/user'// 获取聊天大模型列表export function getChatModels() {@@ -191,3 +192,43 @@ export function editImage(data: {export function getImageEditTaskStatus(logId: string) {return http.get(`ai/image/task/${logId}`)}++// AI聊天接口 - 支持流式和非流式响应+export async function aiChatStream(data: {+ messages: Array<{ role: string, content: string }>+ stream?: boolean+ model?: string+ temperature?: number+ presence_penalty?: number+ frequency_penalty?: number+ top_p?: number+ max_tokens?: number+}) {+ const token = useUserStore.getState().token+ const lang = useUserStore.getState().lang++ const response = await fetch('https://aitoearn.ai/api/ai/chat', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'Authorization': token ? `Bearer ${token}` : '',+ 'Accept-Language': lang || 'zh-CN',+ },+ body: JSON.stringify({+ stream: false, // 使用非流式响应+ model: 'gpt-5.1-all',+ temperature: 1,+ presence_penalty: 0,+ frequency_penalty: 0,+ top_p: 1,+ max_tokens: 8000, // 增加到8000以支持更长的响应(包括base64图片)+ ...data,+ }),+ })++ if (!response.ok) {+ throw new Error(`HTTP error! status: ${response.status}`)+ }++ return response+}\ No newline at end of filediff --git a/project/web/src/app/i18n/locales/en/publish.json b/project/web/src/app/i18n/locales/en/publish.jsonindex b486dfd5..e32e647c 100644--- a/project/web/src/app/i18n/locales/en/publish.json+++ b/project/web/src/app/i18n/locales/en/publish.json@@ -69,6 +69,72 @@"descriptionPlaceholder": "Start writing"},"writingAssistant": "AI Assistant",+ "aiAssistant": "AI Assistant",+ "aiFeatures": {+ "shorten": "Shorten",+ "expand": "Expand",+ "polish": "Polish",+ "translate": "Translate",+ "generateImage": "Image",+ "generateVideo": "Video",+ "generateHashtags": "Hashtags",+ "syncToEditor": "Sync to Editor",+ "targetLanguage": "Target Language",+ "targetLanguagePlaceholder": "Enter target language, e.g.: English, Japanese, French",+ "inputPrompt": "Enter prompt...",+ "send": "Send",+ "defaultPrompts": {+ "shorten": "Please shorten the following text while keeping core information:",+ "expand": "Please expand the following text with more details and descriptions suitable for publishing. Double the content but don't make it too long, and don't include images in response.",+ "polish": "Please polish the following text to make it more fluent and professional:",+ "translate": "You are a translator. If the content below is in Chinese, translate it to English. If it's in English, translate it to Chinese. Only output the translation result without other explanations.",+ "generateImage": "Please generate an image based on the content provided below. The image must be relevant to the content, small, and generated quickly.",+ "generateVideo": "Generate video creation prompts based on the content I provide below. Ensure the content is suitable for publishing and contains no violations.",+ "generateHashtags": "Generate 3-6 hashtags with # based on the content provided below. If it's Chinese content, reply with Chinese hashtags; if English content, reply with English hashtags. If indistinguishable, prefer English hashtags. Only reply with hashtags, no other text."+ },+ "processing": "Processing...",+ "selectText": "Please select text to process",+ "syncSuccess": "Synced to editor",+ "settings": "Settings",+ "close": "Close",+ "chatModel": "Chat Model (Shorten/Expand/Polish/Translate/Hashtags)",+ "imageModel": "Image Model",+ "videoModel": "Video Model",+ "selectChatModel": "Select chat model",+ "selectImageModel": "Select image model",+ "selectVideoModel": "Select video model",+ "chatModelSaved": "Chat model saved",+ "imageModelSaved": "Image model saved",+ "videoModelSaved": "Video model saved",+ "defaultDuration": "Default duration",+ "defaultResolution": "Default resolution",+ "seconds": "s",+ "emptyChat": "Start chatting or select a feature to quickly process text",+ "copied": "Copied to clipboard",+ "hideRaw": "Hide raw",+ "showRaw": "Show raw",+ "imageMissing": "⚠️ Image data missing",+ "imageLoadFailed": "⚠️ Image load failed",+ "downloadingImages": "Downloading images...",+ "downloadingVideo": "Downloading video...",+ "imagesSynced": "Images synced",+ "videoSynced": "Video synced",+ "hashtagsAppended": "Hashtags appended",+ "selectVideoModelFirst": "Please select video model first",+ "videoModelUnavailable": "Video model {{model}} unavailable, please reselect in settings",+ "generatingVideo": "Generating video...",+ "taskSubmitted": "Task submitted, waiting...",+ "generatingStatus": "Generating...",+ "videoFailed": "Failed",+ "videoTaskSubmitted": "Video task submitted",+ "videoGenerationFailed": "Video generation failed",+ "videoGenerated": "Video generated successfully",+ "loadingVideoModels": "Loading video models...",+ "videoModelLoadTimeout": "Video model load timeout, please retry",+ "videoProcessingFailed": "Video processing failed, please retry",+ "imageProcessingFailed": "Image {{index}} processing failed: {{error}}",+ "videoReady": "✅ Video ready!"+ },"upload": {"finishingUp": "Finishing up...","uploadImageOrVideo": "Upload image or video",@@ -191,7 +257,9 @@"facebookAuthFailed": "Facebook authorization failed","accountListRefreshed": "Account list refreshed","refreshAccountListFailed": "Failed to refresh account list",- "authFailed": "Authorization failed"+ "authFailed": "Authorization failed",+ "parseCachedChatModelsFailed": "Failed to parse cached chat models",+ "getChatModelsFailed": "Failed to get chat models"},"badges": {"offline": "Offline",diff --git a/project/web/src/app/i18n/locales/zh-CN/publish.json b/project/web/src/app/i18n/locales/zh-CN/publish.jsonindex bd7e51fe..3ec5feb7 100644--- a/project/web/src/app/i18n/locales/zh-CN/publish.json+++ b/project/web/src/app/i18n/locales/zh-CN/publish.json@@ -70,6 +70,72 @@"descriptionPlaceholder": "开始写"},"writingAssistant": "写作助手",+ "aiAssistant": "AI助手",+ "aiFeatures": {+ "shorten": "缩写",+ "expand": "扩写",+ "polish": "润色",+ "translate": "翻译",+ "generateImage": "制图",+ "generateVideo": "创作视频",+ "generateHashtags": "生成话题",+ "syncToEditor": "同步到编辑器",+ "targetLanguage": "目标语言",+ "targetLanguagePlaceholder": "请输入目标语言,如:英文、日文、法文",+ "inputPrompt": "请输入提示词...",+ "send": "发送",+ "defaultPrompts": {+ "shorten": "请将以下文本缩短,保持核心信息:",+ "expand": "请将以下文本增加更多细节和描述扩写成适合发布使用的内容,内容翻倍即可不要写的太多,并且回复不要带图片。",+ "polish": "请润色以下文本,使其更加流畅和专业:",+ "translate": "你现在是个翻译。如果下面内容是中文,请翻译成英文。如果下面内容是英文,请翻译成中文。只输出翻译结果,不要有其他解释。",+ "generateImage": "根据我下面提供的内容生成图片,必须生成一张与内容相关的图片。",+ "generateVideo": "根据我下面提供的内容生成视频创作提示词,确保内容适合发布且不含违规内容。",+ "generateHashtags": "请按照下面给的内容生成带#的话题生成3-6个,如果是中文内容回复中文话题如果是英文内容回复英文话题,无法区分的优先英文话题,只要回复话题不要其他任何回复"+ },+ "processing": "处理中...",+ "selectText": "请选择要处理的文本",+ "syncSuccess": "已同步到编辑器",+ "settings": "设置",+ "close": "关闭",+ "chatModel": "聊天模型(缩写/扩写/润色/翻译/话题)",+ "imageModel": "图片生成模型",+ "videoModel": "视频生成模型",+ "selectChatModel": "选择聊天模型",+ "selectImageModel": "选择图片生成模型",+ "selectVideoModel": "选择视频模型",+ "chatModelSaved": "聊天模型设置已保存",+ "imageModelSaved": "图片生成模型设置已保存",+ "videoModelSaved": "视频模型设置已保存",+ "defaultDuration": "默认时长",+ "defaultResolution": "默认分辨率",+ "seconds": "秒",+ "emptyChat": "输入内容开始对话,或选择功能快速处理文本",+ "copied": "已复制到剪贴板",+ "hideRaw": "隐藏原始",+ "showRaw": "查看原始",+ "imageMissing": "⚠️ 图片数据缺失",+ "imageLoadFailed": "⚠️ 图片加载失败",+ "downloadingImages": "正在下载图片...",+ "downloadingVideo": "正在下载视频...",+ "imagesSynced": "图片同步成功",+ "videoSynced": "视频同步成功",+ "hashtagsAppended": "话题追加成功",+ "selectVideoModelFirst": "请先选择视频模型",+ "videoModelUnavailable": "视频模型 {{model}} 不可用,请在设置中重新选择",+ "generatingVideo": "正在生成视频...",+ "taskSubmitted": "任务已提交,等待处理...",+ "generatingStatus": "正在生成视频...",+ "videoFailed": "视频生成失败",+ "videoTaskSubmitted": "视频生成任务已提交",+ "videoGenerationFailed": "视频生成失败,请重试",+ "videoGenerated": "视频生成成功",+ "loadingVideoModels": "正在加载视频模型...",+ "videoModelLoadTimeout": "视频模型加载超时,请稍后重试",+ "videoProcessingFailed": "视频处理失败,请重试",+ "imageProcessingFailed": "图片 {{index}} 处理失败: {{error}}",+ "videoReady": "✅ 视频生成完成!"+ },"upload": {"finishingUp": "正在完成...","uploadImageOrVideo": "上传图片或视频",@@ -192,7 +258,9 @@"facebookAuthFailed": "Facebook授权失败","accountListRefreshed": "账号列表已刷新","refreshAccountListFailed": "刷新账号列表失败",- "authFailed": "授权失败"+ "authFailed": "授权失败",+ "parseCachedChatModelsFailed": "解析缓存的聊天模型失败",+ "getChatModelsFailed": "获取聊天模型失败"},"badges": {"offline": "已离线",diff --git a/project/web/src/components/PublishDialog/compoents/PublishDialogAi.tsx b/project/web/src/components/PublishDialog/compoents/PublishDialogAi.tsxindex 186449c2..23f33e29 100644--- a/project/web/src/components/PublishDialog/compoents/PublishDialogAi.tsx+++ b/project/web/src/components/PublishDialog/compoents/PublishDialogAi.tsx@@ -1,34 +1,1333 @@import type { ForwardedRef } from 'react'-import { CloseCircleFilled } from '@ant-design/icons'-import { forwardRef, memo } from 'react'+import {+ CloseCircleFilled,+ CopyOutlined,+ SendOutlined,+ SyncOutlined,+ CompressOutlined,+ ExpandOutlined,+ EditOutlined,+ TranslationOutlined,+ PictureOutlined,+ VideoCameraOutlined,+ SettingOutlined,+ TagsOutlined,+} from '@ant-design/icons'+import { Button, Collapse, Input, message, Modal, Spin, Tooltip, Select, Progress } from 'antd'+import { forwardRef, memo, useCallback, useImperativeHandle, useRef, useEffect, useState, useMemo } from 'react'+import ReactMarkdown from 'react-markdown'import { useTransClient } from '@/app/i18n/client'-import Chat from '@/components/Chat'+import { aiChatStream, getVideoGenerationModels, generateVideo, getVideoTaskStatus } from '@/api/ai'+import { formatImg, VideoGrabFrame } from '@/components/PublishDialog/PublishDialog.util'+import type { IImgFile, IVideoFile } from '@/components/PublishDialog/publishDialog.type'+import { getOssUrl } from '@/utils/oss'+import { OSS_URL } from '@/constant'import styles from '../publishDialog.module.scss'-export interface IPublishDialogAiRef {}++// Custom urlTransform to allow data: URLs (base64 images)+const urlTransform = (url: string) => {+ // Allow data: protocol (base64 images)+ if (url.startsWith('data:image/')) {+ return url+ }+ // Allow blob: protocol+ if (url.startsWith('blob:')) {+ return url+ }+ // Allow http and https+ if (url.startsWith('http://') || url.startsWith('https://')) {+ return url+ }+ // Return empty string for other protocols (security handling)+ return ''+}++const { Option } = Select++export interface IPublishDialogAiRef {+ // AI text processing+ processText: (text: string, action: AIAction) => void+}export interface IPublishDialogAiProps {onClose: () => void+ // Callback to sync content to editor+ // append: true means append content, false means replace content+ onSyncToEditor?: (content: string, images?: IImgFile[], video?: IVideoFile, append?: boolean) => void+ // Chat model list+ chatModels?: any[]}-// AI写作+export type AIAction = 'shorten' | 'expand' | 'polish' | 'translate' | 'generateImage' | 'generateVideo' | 'generateHashtags'++interface Message {+ role: 'user' | 'assistant'+ content: string+ action?: AIAction+}++ // Message item component - use memo to avoid unnecessary re-renders+ const MessageItem = memo(({+ msg,+ index,+ showRawContent,+ setShowRawContent,+ syncToEditor,+ videoStatus,+ videoProgress,+ messagesLength,+ t,+ markdownComponents+ }: {+ msg: Message+ index: number+ showRawContent: number | null+ setShowRawContent: (index: number | null) => void+ syncToEditor: (content: string, action?: AIAction) => void+ videoStatus: string+ videoProgress: number+ messagesLength: number+ t: any+ markdownComponents: any+ }) => {+ // Process video markers+ const processVideoContent = (content: string) => {+ const videoMatch = content.match(/__VIDEO__(.*?)__VIDEO__/)+ if (videoMatch) {+ const videoUrl = videoMatch[1]+ const fullVideoUrl = getOssUrl(videoUrl)+ const textPart = content.replace(/__VIDEO__.*?__VIDEO__/, '').trim()++ return (+ <>+ {textPart && <div style={{ marginBottom: '12px' }}>{textPart}</div>}+ <video+ src={fullVideoUrl}+ controls+ style={{+ maxWidth: '100%',+ maxHeight: '400px',+ borderRadius: '8px',+ display: 'block'+ }}+ />+ </>+ )+ }+ return null+ }++ return (+ <div+ key={index}+ style={{+ marginBottom: 16,+ display: 'flex',+ flexDirection: 'column',+ alignItems: msg.role === 'user' ? 'flex-end' : 'flex-start',+ }}+ >+ <div+ style={{+ maxWidth: '85%',+ padding: '12px',+ background: msg.role === 'user' ? '#1890ff' : '#f5f5f5',+ color: msg.role === 'user' ? '#fff' : '#000',+ borderRadius: '8px',+ boxShadow: '0 1px 2px rgba(0,0,0,0.1)',+ }}+ className="ai-message-content"+ >+ {msg.content ? (+ msg.role === 'assistant' ? (+ <>+ {msg.content.includes('__VIDEO__') ? (+ processVideoContent(msg.content)+ ) : (+ <ReactMarkdown+ urlTransform={urlTransform}+ components={markdownComponents}+ >+ {msg.content}+ </ReactMarkdown>+ )}+ {/* Show progress bar if it's a video generation message with progress */}+ {msg.action === 'generateVideo' && videoStatus && videoStatus !== 'completed' && index === messagesLength - 1 && (+ <div style={{ marginTop: 8 }}>+ <Progress percent={videoProgress} status={videoStatus === 'failed' ? 'exception' : 'active'} />+ <div style={{ fontSize: '12px', color: '#666', marginTop: 4 }}>+ {videoStatus === 'submitted' && t('aiFeatures.taskSubmitted' as any)}+ {videoStatus === 'processing' && t('aiFeatures.generatingStatus' as any)}+ {videoStatus === 'failed' && t('aiFeatures.videoFailed' as any)}+ </div>+ </div>+ )}+ </>+ ) : (+ <div style={{ whiteSpace: 'pre-wrap' }}>{msg.content}</div>+ )+ ) : (+ <Spin size="small" />+ )}+ </div>+ {msg.role === 'assistant' && msg.content && (+ <div style={{ marginTop: 4, display: 'flex', flexDirection: 'column', gap: 8 }}>+ <div style={{ display: 'flex', gap: 8 }}>+ <Button+ size="small"+ icon={<SyncOutlined />}+ onClick={() => syncToEditor(msg.content, msg.action)}+ >+ {t('aiFeatures.syncToEditor' as any)}+ </Button>+ <Button+ size="small"+ icon={<CopyOutlined />}+ onClick={() => {+ navigator.clipboard.writeText(msg.content)+ message.success(t('aiFeatures.copied' as any))+ }}+ />+ <Button+ size="small"+ onClick={() => setShowRawContent(showRawContent === index ? null : index)}+ >+ {showRawContent === index ? t('aiFeatures.hideRaw' as any) : t('aiFeatures.showRaw' as any)}+ </Button>+ </div>+ {showRawContent === index && (+ <div style={{+ background: '#f0f0f0',+ padding: '8px',+ borderRadius: '4px',+ fontSize: '12px',+ maxHeight: '200px',+ overflowY: 'auto',+ wordBreak: 'break-all',+ }}>+ <pre style={{ margin: 0, whiteSpace: 'pre-wrap' }}>{msg.content}</pre>+ </div>+ )}+ </div>+ )}+ </div>+ )+ }, (prevProps, nextProps) => {+ // Only re-render when these properties change+ return (+ prevProps.msg.content === nextProps.msg.content &&+ prevProps.showRawContent === nextProps.showRawContent &&+ prevProps.videoStatus === nextProps.videoStatus &&+ prevProps.videoProgress === nextProps.videoProgress+ )+ })++ // AI-generated image component+ const AIGeneratedImage = memo(({ src, alt }: { src: string; alt?: string }) => {+ const { t } = useTransClient('publish')+ const [imageLoading, setImageLoading] = useState(true)+ const [imageError, setImageError] = useState(false)++ // Show error if no src+ if (!src) {+ const { t } = useTransClient('publish')+ return (+ <div style={{ padding: '8px', background: '#fee', border: '1px solid #fcc', borderRadius: '4px' }}>+ {t('aiFeatures.imageMissing' as any)}+ </div>+ )+ }++ return (+ <div style={{ margin: '8px 0', position: 'relative' }}>+ {/* Loading placeholder */}+ {imageLoading && !imageError && (+ <div style={{+ width: '100%',+ minWidth: '200px',+ height: '200px',+ background: 'linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%)',+ backgroundSize: '200% 100%',+ animation: 'loading 1.5s ease-in-out infinite',+ borderRadius: '4px',+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'center',+ color: '#999',+ fontSize: '14px',+ }}>+ <Spin />+ </div>+ )}+ <img+ src={src}+ alt={alt || 'AI-generated image'}+ style={{+ maxWidth: '100%',+ height: 'auto',+ borderRadius: '4px',+ display: imageLoading ? 'none' : 'block',+ }}+ onError={(e) => {+ console.error('Image load failed:', e)+ setImageLoading(false)+ setImageError(true)+ }}+ onLoad={() => {+ setImageLoading(false)+ setImageError(false)+ }}+ />+ {/* Error message */}+ {imageError && (+ <div style={{+ padding: '8px',+ background: '#fee',+ border: '1px solid #fcc',+ borderRadius: '4px',+ fontSize: '12px',+ color: '#c00',+ }}>+ {t('aiFeatures.imageLoadFailed' as any)}+ </div>+ )}+ </div>+ )+}, (prevProps, nextProps) => {+ // Only re-render when src or alt changes+ return prevProps.src === nextProps.src && prevProps.alt === nextProps.alt+})++// AI Feature Assistantconst PublishDialogAi = memo(forwardRef((- { onClose }: IPublishDialogAiProps,+ { onClose, onSyncToEditor, chatModels = [] }: IPublishDialogAiProps,ref: ForwardedRef<IPublishDialogAiRef>,) => {const { t } = useTransClient('publish')+ const [activeAction, setActiveAction] = useState<AIAction | null>(null)+ const [messages, setMessages] = useState<Message[]>([])+ const [inputValue, setInputValue] = useState('')+ const [customPrompts, setCustomPrompts] = useState<Record<string, string>>({+ expand: t('aiFeatures.defaultPrompts.expand' as any),+ polish: t('aiFeatures.defaultPrompts.polish' as any),+ translate: t('aiFeatures.defaultPrompts.translate' as any),+ generateImage: t('aiFeatures.defaultPrompts.generateImage' as any),+ generateVideo: t('aiFeatures.defaultPrompts.generateVideo' as any),+ generateHashtags: t('aiFeatures.defaultPrompts.generateHashtags' as any),+ })+ const [isProcessing, setIsProcessing] = useState(false)+ const [settingsVisible, setSettingsVisible] = useState(false)+ const [showRawContent, setShowRawContent] = useState<number | null>(null)+ const chatContainerRef = useRef<HTMLDivElement>(null)+ // Save syncToEditor reference to avoid circular dependencies+ const syncToEditorRef = useRef<((content: string, action?: AIAction) => Promise<void>) | null>(null)++ // Chat model state (for shorten/expand/polish/translate/hashtags)+ const [selectedChatModel, setSelectedChatModel] = useState(() => {+ const savedModel = localStorage.getItem('ai_chat_model')+ return savedModel || 'gpt-5.1-all'+ })++ // Image generation model state+ const [selectedImageModel, setSelectedImageModel] = useState(() => {+ const savedModel = localStorage.getItem('ai_image_model')+ return savedModel || 'gemini-2.5-flash-image'+ })++ // Video generation state+ const [videoModels, setVideoModels] = useState<any[]>([])+ const videoModelsLoadingRef = useRef(true) // Use ref to track loading state+ // Initialize from localStorage or use default 'sora-2'+ const [selectedVideoModel, setSelectedVideoModel] = useState(() => {+ const savedModel = localStorage.getItem('ai_video_model')+ return savedModel || 'sora-2'+ })+ const [videoTaskId, setVideoTaskId] = useState<string | null>(null)+ const [videoStatus, setVideoStatus] = useState<string>('')+ const [videoProgress, setVideoProgress] = useState(0)+ const [videoResult, setVideoResult] = useState<string | null>(null)++ // Auto-scroll to bottom+ useEffect(() => {+ if (chatContainerRef.current) {+ chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight+ }+ }, [messages])++ // Initialize chat and image models+ useEffect(() => {+ if (chatModels.length > 0) {+ // Check if current chat model exists in list+ const chatModelExists = chatModels.find((m: any) => m.name === selectedChatModel)+ if (!chatModelExists) {+ // Try to find default model+ const defaultModel = chatModels.find((m: any) => m.name === 'gpt-5.1-all')+ if (defaultModel) {+ setSelectedChatModel(defaultModel.name)+ localStorage.setItem('ai_chat_model', defaultModel.name)+ } else if (chatModels.length > 0) {+ // Use first model if default not found+ setSelectedChatModel(chatModels[0].name)+ localStorage.setItem('ai_chat_model', chatModels[0].name)+ }+ }++ // Check if current image model exists in list+ const imageModelExists = chatModels.find((m: any) => m.name === selectedImageModel)+ if (!imageModelExists) {+ // Try to find default model+ const defaultModel = chatModels.find((m: any) => m.name === 'gemini-2.5-flash-image')+ if (defaultModel) {+ setSelectedImageModel(defaultModel.name)+ localStorage.setItem('ai_image_model', defaultModel.name)+ } else if (chatModels.length > 0) {+ // Use first model if default not found+ setSelectedImageModel(chatModels[0].name)+ localStorage.setItem('ai_image_model', chatModels[0].name)+ }+ }+ }+ }, [chatModels])++ // Initialize video models+ useEffect(() => {+ const fetchVideoModels = async () => {+ try {+ videoModelsLoadingRef.current = true+ const res: any = await getVideoGenerationModels()+ if (res.data && Array.isArray(res.data)) {+ setVideoModels(res.data)++ // Read saved model from localStorage+ const savedModel = localStorage.getItem('ai_video_model')++ // Check if current model exists in list+ const currentModelExists = res.data.find((m: any) => m.name === selectedVideoModel)++ if (currentModelExists) {+ // Current model is valid, no update needed+ } else if (savedModel && res.data.find((m: any) => m.name === savedModel)) {+ // Use saved model if it exists in list+ setSelectedVideoModel(savedModel)+ } else {+ // Try to find sora-related model+ const soraModel = res.data.find((m: any) =>+ m.name?.toLowerCase().includes('sora') || m.name === 'sora-2'+ )++ if (soraModel) {+ // Prefer sora model+ setSelectedVideoModel(soraModel.name)+ localStorage.setItem('ai_video_model', soraModel.name)+ } else if (res.data.length > 0) {+ // Use first model if no sora found+ setSelectedVideoModel(res.data[0].name)+ localStorage.setItem('ai_video_model', res.data[0].name)+ }+ }+ }+ } catch (error) {+ console.error('Failed to fetch video models:', error)+ } finally {+ videoModelsLoadingRef.current = false+ }+ }++ fetchVideoModels()+ }, []) // Only execute once on component mount++ // Poll video task status+ const pollVideoTaskStatus = useCallback(async (taskId: string) => {+ const checkStatus = async () => {+ try {+ const res: any = await getVideoTaskStatus(taskId)+ if (res.data) {+ const { status, fail_reason, progress, data } : any = res.data;+ // {+ // "task_id": "691d59a0cf8f85110ff2538d",+ // "action": "",+ // "status": "SUCCESS",+ // "fail_reason": "ai/video/sora-2/68abbe6af812ccb3e1a53d68/691d59a0cf8f85110ff2538d.mp4",+ // "submit_time": 1763531161,+ // "start_time": 1763531161,+ // "finish_time": 1763531405,+ // "progress": "100%",+ // "data": {+ // "id": "video_1459b06c-e279-4765-8465-3040eec4e10a",+ // "size": "720x1280",+ // "model": "sora-2",+ // "object": "video",+ // "status": "completed",+ // "seconds": "10",+ // "progress": 100,+ // "video_url": "ai/video/sora-2/68abbe6af812ccb3e1a53d68/691d59a0cf8f85110ff2538d.mp4",+ // "created_at": 1763531162,+ // "completed_at": 1763531339+ // }+ // }+ const up = typeof status === 'string' ? status.toUpperCase() : ''+ const normalized = up === 'SUCCESS' ? 'completed' : up === 'FAILED' ? 'failed' : up === 'PROCESSING' ? 'processing' : up === 'NOT_START' || up === 'NOT_STARTED' || up === 'QUEUED' || up === 'PENDING' ? 'submitted' : (status || '').toString().toLowerCase()+ setVideoStatus(normalized)++ let percent = 0+ if (typeof progress === 'string') {+ const m = progress.match(/(\d+)/)+ percent = m ? Number(m[1]) : 0+ } else if (typeof progress === 'number') {+ percent = progress > -1 ? Math.round(progress) : Math.round(progress * 100)+ }++ if (normalized === 'completed') {+ let videoUrl = res.data?.data?.video_url || data?.video_url+ console.log('videoUrl', videoUrl)+ setVideoResult(videoUrl)+ setVideoProgress(100)++ // Update last message, embed video (using custom marker)+ setMessages(prev => {+ const newMessages = [...prev]+ if (newMessages.length > 0 && newMessages[newMessages.length - 1].role === 'assistant') {+ // Save original video URL to message for syncing+ newMessages[newMessages.length - 1] = {+ ...newMessages[newMessages.length - 1],+ content: `${t('aiFeatures.videoReady' as any)}\n\n__VIDEO__${videoUrl}__VIDEO__`,+ }+ }+ return newMessages+ })++ message.success(t('aiFeatures.videoGenerated' as any))+ setIsProcessing(false)+ return true+ }+ if (normalized === 'failed') {+ setVideoProgress(0)+ message.error(fail_reason || t('aiFeatures.videoFailed' as any))+ setMessages(prev => prev.slice(0, -1))+ setIsProcessing(false)+ return true+ }+ setVideoProgress(percent)+ return false+ }+ return false+ } catch {+ return false+ }+ }++ const poll = async () => {+ const done = await checkStatus()+ if (!done) {+ setTimeout(poll, 5000)+ }+ }+ poll()+ }, [])++ // Handle video generation+ const handleVideoGeneration = useCallback(async (prompt: string) => {+ // Wait for model list to load if still loading+ if (videoModelsLoadingRef.current) {+ message.loading({ content: t('aiFeatures.loadingVideoModels' as any), key: 'loadingModels', duration: 0 })++ // Poll to check if models loaded, max wait 30 seconds+ const maxWaitTime = 30000+ const checkInterval = 200+ let waited = 0++ while (videoModelsLoadingRef.current && waited < maxWaitTime) {+ await new Promise(resolve => setTimeout(resolve, checkInterval))+ waited += checkInterval+ }++ message.destroy('loadingModels')++ if (videoModelsLoadingRef.current) {+ message.error(t('aiFeatures.videoModelLoadTimeout' as any))+ return+ }+ }++ if (!selectedVideoModel) {+ message.error(t('aiFeatures.selectVideoModelFirst' as any))+ return+ }++ const selectedModel = videoModels.find((m: any) => m.name === selectedVideoModel)+ if (!selectedModel) {+ console.error('Video model unavailable:', selectedVideoModel, 'Available models:', videoModels.map(m => m.name))+ message.error(t('aiFeatures.videoModelUnavailable' as any, { model: selectedVideoModel }))+ return+ }++ try {+ setIsProcessing(true)+ setVideoStatus('submitted')+ setVideoProgress(10)++ // Add assistant message placeholder+ const placeholderMsg: Message = { role: 'assistant', content: t('aiFeatures.generatingVideo' as any), action: 'generateVideo' }+ setMessages(prev => [...prev, placeholderMsg])++ // Get first available resolution and duration+ let duration = 5+ let size = '720p'++ if (selectedModel?.channel === 'kling' && selectedModel?.pricing) {+ const durations = [...new Set(selectedModel.pricing.map((p: any) => p.duration))] as number[]+ const modes = [...new Set(selectedModel.pricing.map((p: any) => p.mode))] as string[]+ if (durations.length > 0) duration = durations[0]+ if (modes.length > 0) size = modes[0]+ } else {+ if (selectedModel?.durations?.length > 0) duration = selectedModel.durations[0]+ if (selectedModel?.resolutions?.length > 0) size = selectedModel.resolutions[0]+ }++ const data: any = {+ model: selectedVideoModel,+ prompt,+ duration,+ }++ // Use mode parameter for kling model+ if (selectedModel?.channel === 'kling') {+ data.mode = size+ } else {+ data.size = size+ }++ const res: any = await generateVideo(data)++ if (res?.data?.task_id) {+ setVideoTaskId(res.data.task_id)+ setVideoStatus(res.data.status)+ message.success(t('aiFeatures.videoTaskSubmitted' as any))+ pollVideoTaskStatus(res.data.task_id)+ } else {+ throw new Error(t('aiFeatures.videoGenerationFailed' as any))+ }+ } catch (error: any) {+ console.error('Video Generation Error:', error)+ setMessages(prev => prev.slice(0, -1))+ message.error(error.message || t('aiFeatures.videoGenerationFailed' as any))+ setVideoStatus('')+ setIsProcessing(false)+ }+ }, [selectedVideoModel, videoModels, pollVideoTaskStatus, t])+++ // Handle AI response+ const handleAIResponse = useCallback(async (+ action: AIAction,+ apiMessages: Array<{ role: string, content: string }>,+ ) => {+ try {+ setIsProcessing(true)++ // Add assistant message placeholder+ const placeholderMsg: Message = { role: 'assistant', content: '', action }+ setMessages(prev => [...prev, placeholderMsg])++ // Select model based on action+ const modelToUse = action === 'generateImage' ? selectedImageModel : selectedChatModel++ const response = await aiChatStream({+ messages: apiMessages,+ model: modelToUse+ })++ // Check response status+ if (!response.ok) {+ throw new Error(`HTTP error! status: ${response.status}`)+ }++ // Try to read JSON response+ const result = await response.json()++ if (result.code === 0 && result.data?.content) {+ // Optimize: convert base64 images to blob URLs to avoid storing large data causing input lag+ let processedContent = result.data.content++ // Find all base64 images and convert to blob URLs+ const base64Regex = /!\[([^\]]*)\]\((data:image\/[^;]+;base64,[^)]+)\)/g+ let match+ const replacements: Array<{ original: string; blobUrl: string }> = []++ while ((match = base64Regex.exec(result.data.content)) !== null) {+ const [fullMatch, alt, base64Url] = match+ try {+ // Extract base64 data+ const parts = base64Url.split(',')+ if (parts.length === 2) {+ const mimeType = base64Url.match(/data:(.*?);/)?.[1] || 'image/png'+ const base64Data = parts[1]++ // Convert to blob+ const byteCharacters = atob(base64Data)+ const byteNumbers = new Array(byteCharacters.length)+ for (let i = 0; i < byteCharacters.length; i++) {+ byteNumbers[i] = byteCharacters.charCodeAt(i)+ }+ const byteArray = new Uint8Array(byteNumbers)+ const blob = new Blob([byteArray], { type: mimeType })+ const blobUrl = URL.createObjectURL(blob)++ replacements.push({+ original: fullMatch,+ blobUrl: ``+ })+ }+ } catch (error) {+ console.error('Base64 to blob conversion failed:', error)+ }+ }++ // Execute all replacements+ replacements.forEach(({ original, blobUrl }) => {+ processedContent = processedContent.replace(original, blobUrl)+ })++ // Update last message+ setMessages(prev => {+ const newMessages = [...prev]+ newMessages[newMessages.length - 1] = {+ role: 'assistant',+ content: processedContent,+ action,+ }+ return newMessages+ })++ // Auto-sync to editor if hashtags generation (append mode)+ if (action === 'generateHashtags') {+ // Delay to let user see the result+ setTimeout(() => {+ if (syncToEditorRef.current) {+ syncToEditorRef.current(processedContent, action)+ }+ }, 500)+ }+ } else {+ throw new Error(result.message || 'AI response failed')+ }++ setIsProcessing(false)+ } catch (error: any) {+ console.error('AI Response Error:', error)+ // Remove placeholder message+ setMessages(prev => prev.slice(0, -1))+ message.error(error.message || 'AI processing failed, please retry')+ setIsProcessing(false)+ }+ }, [selectedChatModel, selectedImageModel])++ // Handle action button click+ const handleActionClick = useCallback((action: AIAction) => {+ setActiveAction(action)+ }, [])++ // 发送消息+ const sendMessage = useCallback(async (content?: string, forceAction?: AIAction) => {+ const messageContent = content || inputValue+ if (!messageContent.trim()) {+ message.warning(t('aiFeatures.selectText' as any))+ return+ }++ // Use passed action or current state action+ const currentAction = forceAction || activeAction++ // Add user message+ const userMessage: Message = { role: 'user', content: messageContent, action: currentAction || undefined }+ setMessages(prev => [...prev, userMessage])++ // Call video generation directly if it's video generation+ if (currentAction === 'generateVideo') {+ await handleVideoGeneration(messageContent)+ setInputValue('')+ return+ }++ let systemPrompt = ''++ // Generate prompt based on selected action+ if (currentAction) {+ switch (currentAction) {+ case 'shorten':+ systemPrompt = t('aiFeatures.defaultPrompts.shorten' as any)+ break+ case 'expand':+ systemPrompt = customPrompts.expand || t('aiFeatures.defaultPrompts.expand' as any)+ break+ case 'polish':+ systemPrompt = customPrompts.polish || t('aiFeatures.defaultPrompts.polish' as any)+ break+ case 'translate':+ systemPrompt = customPrompts.translate || t('aiFeatures.defaultPrompts.translate' as any)+ break+ case 'generateImage':+ systemPrompt = customPrompts.generateImage || t('aiFeatures.defaultPrompts.generateImage' as any)+ break+ case 'generateHashtags':+ systemPrompt = customPrompts.generateHashtags || t('aiFeatures.defaultPrompts.generateHashtags' as any)+ break+ }+ }++ // Prepare API messages+ const apiMessages: Array<{ role: string, content: string }> = []++++ // Only add system prompt if exists+ if (systemPrompt) {+ apiMessages.push({ role: 'system', content: systemPrompt })+ }++ apiMessages.push({ role: 'user', content: messageContent })++ // Call AI interface+ await handleAIResponse(currentAction || 'polish', apiMessages)++ // Clear input+ setInputValue('')+ }, [activeAction, inputValue, customPrompts, handleAIResponse, handleVideoGeneration, t])++ // Download image from URL and convert to IImgFile object+ const downloadImageAsImgFile = async (url: string, index: number): Promise<IImgFile | null> => {+ try {+ let blob: Blob++ // If it's a blob URL+ if (url.startsWith('blob:')) {+ const response = await fetch(url)+ if (!response.ok) {+ throw new Error(`Cannot read blob: ${response.status}`)+ }+ blob = await response.blob()+ }+ // If it's base64 image, convert directly+ else if (url.startsWith('data:image/')) {+ if (!url.includes(',')) {+ throw new Error('Invalid base64 image format')+ }++ const parts = url.split(',')+ if (parts.length !== 2) {+ throw new Error('Base64 data format error')+ }++ const base64Data = parts[1]+ const mimeType = url.match(/data:(.*?);/)?.[1] || 'image/png'++ // Check if base64 data is valid+ if (!base64Data || base64Data.length < 100) {+ throw new Error('Base64 data too short or empty')+ }++ const byteCharacters = atob(base64Data)+ const byteNumbers = new Array(byteCharacters.length)+ for (let i = 0; i < byteCharacters.length; i++) {+ byteNumbers[i] = byteCharacters.charCodeAt(i)+ }+ const byteArray = new Uint8Array(byteNumbers)+ blob = new Blob([byteArray], { type: mimeType })+ }+ // Regular URL, need to download+ else {+ const response = await fetch(url)+ if (!response.ok) {+ throw new Error(`HTTP ${response.status}`)+ }+ blob = await response.blob()+ }++ const filename = `ai-generated-image-${index + 1}.png`++ // Use formatImg function to properly process image+ const imgFile = await formatImg({+ blob,+ path: filename,+ })++ return imgFile+ } catch (error) {+ console.error('Image processing failed:', error)+ message.error(t('aiFeatures.imageProcessingFailed' as any, { index: index + 1, error: error instanceof Error ? error.message : 'Unknown' }))+ return null+ }+ }++ // Create IVideoFile object from URL (no download, use generated URL directly)+ const downloadVideoAsVideoFile = async (url: string): Promise<IVideoFile | null> => {+ try {+ // Convert relative path to full S3 URL for publishing+ // API returns: ai/video/sora-2/...+ // Publishing expects: https://aitoearn.s3.ap-southeast-1.amazonaws.com/ai/video/...+ const ossUrl = `${OSS_URL}${url}`+ const filename = `ai-generated-video.mp4`++ // Extract video info using proxy URL for preview (only for VideoGrabFrame)+ const proxyUrl = getOssUrl(url)+ const videoInfo = await VideoGrabFrame(proxyUrl, 0)++ // Note: we don't create local blob, just use network URL+ const videoFile: IVideoFile = {+ filename,+ videoUrl: ossUrl, // Full S3 URL+ size: 0, // No download means no size, set to 0+ file: new Blob(), // Placeholder, not actually used+ ossUrl: ossUrl, // Full S3 URL for publishing API+ ...videoInfo,+ }++ return videoFile+ } catch (error) {+ console.error('Video processing failed:', error)+ message.error(t('aiFeatures.videoProcessingFailed' as any))+ return null+ }+ }++ // Sync to editor+ const syncToEditor = useCallback(async (content: string, action?: AIAction) => {+ if (onSyncToEditor) {+ // Extract all image URLs and video links+ const imageMatches = content.match(/!\[.*?\]\(([^)]+)\)/g) || []+ const linkMatches = content.match(/\[.*?\]\(([^)]+)\)/g) || []++ // Separate image and video links+ const imageUrls: string[] = []+ let videoUrl: string | null = null++ imageMatches.forEach(match => {+ const urlMatch = match.match(/!\[.*?\]\(([^)]+)\)/)+ const url = urlMatch ? urlMatch[1] : null+ if (url) {+ // Support regular URLs and base64 format images+ imageUrls.push(url)+ }+ })++ // Check for video links (support Markdown and pure URL formats)+ linkMatches.forEach(match => {+ const urlMatch = match.match(/\[.*?\]\(([^)]+)\)/)+ const url = urlMatch ? urlMatch[1] : null+ if (url && (url.includes('.mp4') || url.includes('.webm') || url.includes('video'))) {+ videoUrl = url+ }+ })++ // If no Markdown video link found, try to match custom marker+ if (!videoUrl) {+ const videoTagMatch = content.match(/__VIDEO__(.*?)__VIDEO__/)+ if (videoTagMatch) {+ videoUrl = videoTagMatch[1]+ }+ }++ // If still not found, try to match pure URL+ if (!videoUrl) {+ const urlRegex = /(https?:\/\/[^\s]+\.(mp4|webm)|https?:\/\/[^\s]*video[^\s]*)/gi+ const urlMatches = content.match(urlRegex)+ if (urlMatches && urlMatches.length > 0) {+ videoUrl = urlMatches[0]+ }+ }++ // Download images+ let imageFiles: IImgFile[] = []+ if (imageUrls.length > 0) {+ message.loading({ content: t('aiFeatures.downloadingImages' as any), key: 'downloadMedia' })+ const downloadPromises = imageUrls.map((url, index) =>+ downloadImageAsImgFile(url, index)+ )+ const results = await Promise.all(downloadPromises)+ imageFiles = results.filter((file): file is IImgFile => file !== null)+ }++ // Download video+ let videoFile: IVideoFile | undefined+ if (videoUrl) {+ message.loading({ content: t('aiFeatures.downloadingVideo' as any), key: 'downloadMedia' })+ videoFile = await downloadVideoAsVideoFile(videoUrl) || undefined+ }++ if (imageUrls.length > 0 || videoUrl) {+ message.destroy('downloadMedia')+ }++ // Remove images and video links from markdown, keep only text content+ let textContent = content+ .replace(/!\[.*?\]\([^)]+\)/g, '') // Remove images+ .replace(/\[视频链接\]\([^)]+\)/g, '') // Remove Markdown video links+ .replace(/\[.*?\]\([^)]+\.(mp4|webm)[^)]*\)/gi, '') // Remove Markdown format videos+ .replace(/__VIDEO__.*?__VIDEO__/g, '') // Remove custom video markers++ // Remove pure URL format video links+ if (videoUrl) {+ textContent = textContent.replace(videoUrl, '')+ }++ textContent = textContent.trim()++ // Determine if content should be appended (hashtags generation)+ const shouldAppend = action === 'generateHashtags'++ // If there's video, only sync video (don't update text)+ // If there are images, only sync images (don't update text)+ // If neither, sync text content+ if (videoFile) {+ onSyncToEditor('', [], videoFile, shouldAppend)+ message.success(t('aiFeatures.videoSynced' as any))+ } else if (imageFiles.length > 0) {+ onSyncToEditor('', imageFiles, undefined, shouldAppend)+ message.success(t('aiFeatures.imagesSynced' as any))+ } else {+ onSyncToEditor(textContent, [], undefined, shouldAppend)+ message.success(shouldAppend ? t('aiFeatures.hashtagsAppended' as any) : t('aiFeatures.syncSuccess' as any))+ }+ }+ }, [onSyncToEditor, t, downloadImageAsImgFile, downloadVideoAsVideoFile])++ // Update syncToEditor ref+ useEffect(() => {+ syncToEditorRef.current = syncToEditor+ }, [syncToEditor])++ // Expose methods to parent component+ useImperativeHandle(ref, () => ({+ processText: (text: string, action: AIAction) => {+ setActiveAction(action)+ setInputValue(text)+ // Auto-send immediately, pass action parameter to avoid async state issues+ setTimeout(() => {+ sendMessage(text, action) // Pass action parameter+ }, 50)+ },+ }), [sendMessage])++ const actionButtons = [+ { action: 'shorten', icon: <CompressOutlined />, label: t('aiFeatures.shorten' as any) },+ { action: 'expand', icon: <ExpandOutlined />, label: t('aiFeatures.expand' as any) },+ { action: 'polish', icon: <EditOutlined />, label: t('aiFeatures.polish' as any) },+ { action: 'translate', icon: <TranslationOutlined />, label: t('aiFeatures.translate' as any) },+ { action: 'generateImage', icon: <PictureOutlined />, label: t('aiFeatures.generateImage' as any) },+ { action: 'generateVideo', icon: <VideoCameraOutlined />, label: t('aiFeatures.generateVideo' as any) },+ { action: 'generateHashtags', icon: <TagsOutlined />, label: t('aiFeatures.generateHashtags' as any) },+ ]++ // Cache Markdown component config to avoid creating new objects on every render+ const markdownComponents = useMemo(() => ({+ img: ({ node, ...props }: any) => {+ return <AIGeneratedImage src={props.src || ''} alt={props.alt} />+ },+ p: ({ node, ...props }: any) => <p {...props} style={{ margin: '4px 0', lineHeight: '1.6' }} dir="auto" />,+ code: ({ node, inline, className, children, ...props }: any) => {+ return inline+ ? <code style={{ background: '#f0f0f0', padding: '2px 6px', borderRadius: '3px', fontSize: '0.9em' }} {...props}>{children}</code>+ : <code style={{ display: 'block', background: '#f0f0f0', padding: '12px', borderRadius: '4px', overflowX: 'auto', fontSize: '0.9em', lineHeight: '1.5' }} {...props}>{children}</code>+ },+ a: ({ node, ...props }: any) => {+ const href = props.href || ''++ // Check if it's an audio file+ if (/\.(aac|mp3|opus|wav)$/.test(href)) {+ return (+ <figure style={{ margin: '8px 0' }}>+ <audio controls src={href}></audio>+ </figure>+ )+ }++ // Check if it's a video file+ if (/\.(3gp|3g2|webm|ogv|mpeg|mp4|avi)$/.test(href) || href.includes('video')) {+ return (+ <div style={{ margin: '8px 0' }}>+ <video+ controls+ style={{+ maxWidth: '100%',+ maxHeight: '400px',+ borderRadius: '8px',+ display: 'block'+ }}+ >+ <source src={getOssUrl(href)} />+ </video>+ </div>+ )+ }++ // Regular link+ const isInternal = /^\/#/i.test(href)+ const target = isInternal ? '_self' : props.target ?? '_blank'+ return <a {...props} target={target} style={{ color: '#1890ff', textDecoration: 'underline' }} rel="noopener noreferrer" />+ },+ ul: ({ node, ...props }: any) => <ul style={{ margin: '8px 0', paddingLeft: '20px' }} {...props} />,+ ol: ({ node, ...props }: any) => <ol style={{ margin: '8px 0', paddingLeft: '20px' }} {...props} />,+ li: ({ node, ...props }: any) => <li style={{ margin: '4px 0' }} {...props} />,+ h1: ({ node, ...props }: any) => <h1 style={{ fontSize: '1.5em', fontWeight: 'bold', margin: '12px 0 8px' }} {...props} />,+ h2: ({ node, ...props }: any) => <h2 style={{ fontSize: '1.3em', fontWeight: 'bold', margin: '12px 0 8px' }} {...props} />,+ h3: ({ node, ...props }: any) => <h3 style={{ fontSize: '1.1em', fontWeight: 'bold', margin: '8px 0 6px' }} {...props} />,+ blockquote: ({ node, ...props }: any) => (+ <blockquote style={{ borderLeft: '3px solid #e0e0e0', paddingLeft: '12px', margin: '8px 0', color: '#666' }} {...props} />+ ),+ }), [])return (<div className={styles.publishDialogAi} id="publishDialogAi"><h1>- <span>{t('writingAssistant')}</span>+ <span>{t('aiAssistant' as any)}</span><CloseCircleFilled onClick={onClose} /></h1>- <div className="publishDialogAi-wrapper">- <Chat />+ <div className="publishDialogAi-wrapper" style={{ padding: '0 12px', marginTop: '12px' }}>+ {/* Chat messages area */}+ <div+ ref={chatContainerRef}+ className="publishDialogAi-chat"+ style={{+ flex: '1 1 0',+ overflowY: 'auto',+ marginBottom: 12,+ padding: '12px',+ background: '#f5f5f5',+ borderRadius: '8px',+ }}+ >++ {messages.length === 0 ? (+ <div style={{+ textAlign: 'center',+ color: '#999',+ padding: '40px 20px',+ }}>+ {t('aiFeatures.emptyChat' as any)}+ </div>+ ) : (+ messages.map((msg, index) => (+ <MessageItem+ key={`${index}-${msg.content.substring(0, 20)}`}+ msg={msg}+ index={index}+ showRawContent={showRawContent}+ setShowRawContent={setShowRawContent}+ syncToEditor={syncToEditor}+ videoStatus={videoStatus}+ videoProgress={videoProgress}+ messagesLength={messages.length}+ t={t}+ markdownComponents={markdownComponents}+ />+ ))+ )}+ </div>++ {/* Action buttons area */}+ <div style={{+ display: 'flex',+ gap: 8,+ marginBottom: 8,+ padding: '8px',+ background: '#fafafa',+ borderRadius: '8px',+ justifyContent: 'space-between',+ alignItems: 'center',+ }}>+ <div style={{ display: 'flex', gap: 8, flex: 1, flexWrap: 'wrap' }}>+ {actionButtons.map(({ action, icon, label }) => (+ <Tooltip key={action} title={label}>+ <Button+ type={activeAction === action ? 'primary' : 'default'}+ icon={icon}+ onClick={() => handleActionClick(action as AIAction)}+ size="small"+ />+ </Tooltip>+ ))}+ </div>+ <Tooltip title={t('aiFeatures.settings' as any)}>+ <Button+ icon={<SettingOutlined />}+ onClick={() => setSettingsVisible(true)}+ size="small"+ />+ </Tooltip>+ </div>++ {/* Input area */}+ <div style={{ display: 'flex', gap: 8, marginBottom: 12 }}>+ <Input.TextArea+ value={inputValue}+ onChange={e => setInputValue(e.target.value)}+ placeholder={activeAction ? t('aiFeatures.inputPrompt' as any) : t('aiFeatures.emptyChat' as any)}+ rows={1}+ disabled={isProcessing}+ onPressEnter={(e) => {+ if (e.shiftKey) return // Shift+Enter for new line+ e.preventDefault()+ sendMessage()+ }}+ />+ <Button+ type="primary"+ icon={<SendOutlined />}+ onClick={() => sendMessage()}+ loading={isProcessing}+ disabled={isProcessing}+ >+ {t('aiFeatures.send' as any)}+ </Button>+ </div></div>++ {/* Settings modal */}+ <Modal+ title={t('aiFeatures.settings' as any)}+ open={settingsVisible}+ onCancel={() => setSettingsVisible(false)}+ footer={[+ <Button key="close" onClick={() => setSettingsVisible(false)}>+ {t('aiFeatures.close' as any)}+ </Button>,+ ]}+ >+ {/* Chat model selection */}+ <div style={{ marginBottom: 16 }}>+ <div style={{ marginBottom: 8, fontWeight: 'bold' }}>+ <EditOutlined style={{ marginRight: 8 }} />+ {t('aiFeatures.chatModel' as any)}+ </div>+ <Select+ value={selectedChatModel}+ onChange={(value) => {+ setSelectedChatModel(value)+ localStorage.setItem('ai_chat_model', value)+ message.success(t('aiFeatures.chatModelSaved' as any))+ }}+ style={{ width: '100%' }}+ placeholder={t('aiFeatures.selectChatModel' as any)}+ optionLabelProp="label"+ >+ {chatModels.map((model: any) => (+ <Option+ key={model.name}+ value={model.name}+ label={model.name}+ >+ <div style={{ padding: '4px 0' }}>+ <div style={{ fontWeight: 'bold', marginBottom: '4px' }}>{model.name}</div>+ {model.description && (+ <div style={{ fontSize: '12px', color: '#999', whiteSpace: 'normal', lineHeight: '1.4' }}>+ {model.description}+ </div>+ )}+ </div>+ </Option>+ ))}+ </Select>+ </div>++ {/* Image generation model selection */}+ <div style={{ marginBottom: 16 }}>+ <div style={{ marginBottom: 8, fontWeight: 'bold' }}>+ <PictureOutlined style={{ marginRight: 8 }} />+ {t('aiFeatures.imageModel' as any)}+ </div>+ <Select+ value={selectedImageModel}+ onChange={(value) => {+ setSelectedImageModel(value)+ localStorage.setItem('ai_image_model', value)+ message.success(t('aiFeatures.imageModelSaved' as any))+ }}+ style={{ width: '100%' }}+ placeholder={t('aiFeatures.selectImageModel' as any)}+ optionLabelProp="label"+ >+ {chatModels.map((model: any) => (+ <Option+ key={model.name}+ value={model.name}+ label={model.name}+ >+ <div style={{ padding: '4px 0' }}>+ <div style={{ fontWeight: 'bold', marginBottom: '4px' }}>{model.name}</div>+ {model.description && (+ <div style={{ fontSize: '12px', color: '#999', whiteSpace: 'normal', lineHeight: '1.4' }}>+ {model.description}+ </div>+ )}+ </div>+ </Option>+ ))}+ </Select>+ </div>++ {/* Video generation model selection */}+ <div style={{ marginBottom: 16 }}>+ <div style={{ marginBottom: 8, fontWeight: 'bold' }}>+ <VideoCameraOutlined style={{ marginRight: 8 }} />+ {t('aiFeatures.videoModel' as any)}+ </div>+ <Select+ value={selectedVideoModel}+ onChange={(value) => {+ setSelectedVideoModel(value)+ localStorage.setItem('ai_video_model', value)+ message.success(t('aiFeatures.videoModelSaved' as any))+ }}+ style={{ width: '100%' }}+ placeholder={t('aiFeatures.selectVideoModel' as any)}+ optionLabelProp="label"+ >+ {videoModels.map((model: any) => (+ <Option+ key={model.name}+ value={model.name}+ label={model.name}+ >+ <div style={{ padding: '4px 0' }}>+ <div style={{ fontWeight: 'bold', marginBottom: '4px' }}>{model.name}</div>+ {model.description && (+ <div style={{ fontSize: '12px', color: '#999', whiteSpace: 'normal', lineHeight: '1.4' }}>+ {model.description}+ </div>+ )}+ </div>+ </Option>+ ))}+ </Select>+ {selectedVideoModel && (() => {+ const model = videoModels.find((m: any) => m.name === selectedVideoModel)+ if (!model) return null++ let duration = 5+ let size = '720p'++ if (model?.channel === 'kling' && model?.pricing) {+ const durations = [...new Set(model.pricing.map((p: any) => p.duration))] as number[]+ const modes = [...new Set(model.pricing.map((p: any) => p.mode))] as string[]+ if (durations.length > 0) duration = durations[0]+ if (modes.length > 0) size = modes[0]+ } else {+ if (model?.durations?.length > 0) duration = model.durations[0]+ if (model?.resolutions?.length > 0) size = model.resolutions[0]+ }++ return (+ <div style={{ marginTop: 8, padding: '8px', background: '#f5f5f5', borderRadius: '4px', fontSize: '12px', color: '#666' }}>+ <div>{t('aiFeatures.defaultDuration' as any)}: {duration}{t('aiFeatures.seconds' as any)}</div>+ <div>{t('aiFeatures.defaultResolution' as any)}: {size}</div>+ </div>+ )+ })()}+ </div>+ </Modal></div>)},diff --git a/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.module.scss b/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.module.scssnew file mode 100644index 00000000..7dd3f485--- /dev/null+++ b/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.module.scss@@ -0,0 +1,29 @@+.textSelectionToolbar {+ background: white;+ border-radius: 8px;+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);+ padding: 4px;+ animation: fadeIn 0.2s ease-in-out;++ :global {+ .ant-btn {+ border-radius: 6px;++ &:hover {+ background: #f0f0f0;+ }+ }+ }+}++@keyframes fadeIn {+ from {+ opacity: 0;+ transform: translateX(-50%) translateY(-5px);+ }+ to {+ opacity: 1;+ transform: translateX(-50%) translateY(0);+ }+}+diff --git a/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.tsx b/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.tsxnew file mode 100644index 00000000..11483bc9--- /dev/null+++ b/project/web/src/components/PublishDialog/compoents/TextSelectionToolbar.tsx@@ -0,0 +1,165 @@+import type { CSSProperties } from 'react'+import { memo, useCallback, useEffect, useRef, useState } from 'react'+import { Button } from 'antd'+import {+ CompressOutlined,+ ExpandOutlined,+ EditOutlined,+ TranslationOutlined,+ TagsOutlined,+ PictureOutlined,+ VideoCameraOutlined,+} from '@ant-design/icons'+import { useTransClient } from '@/app/i18n/client'+import type { AIAction } from './PublishDialogAi'+import styles from './TextSelectionToolbar.module.scss'++export interface TextSelectionToolbarProps {+ // Container ref to limit selection range+ containerRef: React.RefObject<HTMLElement>+ // Callback after text selection+ onAction: (action: AIAction, selectedText: string) => void+}++const TextSelectionToolbar = memo(({ containerRef, onAction }: TextSelectionToolbarProps) => {+ const { t } = useTransClient('publish')+ const [visible, setVisible] = useState(false)+ const [position, setPosition] = useState<{ top: number; left: number }>({ top: 0, left: 0 })+ const [selectedText, setSelectedText] = useState('')+ const toolbarRef = useRef<HTMLDivElement>(null)++ const handleSelection = useCallback(() => {+ const selection = window.getSelection()+ const text = selection?.toString().trim()++ if (!text || text.length === 0) {+ setVisible(false)+ return+ }++ // Check if selected text is within specified container+ if (containerRef.current && selection && selection.rangeCount > 0) {+ const range = selection.getRangeAt(0)+ const container = containerRef.current++ if (!container.contains(range.commonAncestorContainer)) {+ setVisible(false)+ return+ }++ // Get selection position+ const rect = range.getBoundingClientRect()+ const containerRect = container.getBoundingClientRect()++ // Calculate toolbar position (relative to viewport)+ const top = rect.top - 50 // Show toolbar above selected text+ const left = rect.left + rect.width / 2 // Center align++ setPosition({ top, left })+ setSelectedText(text)+ setVisible(true)+ }+ }, [containerRef])++ const handleAction = useCallback((action: AIAction) => {+ if (selectedText) {+ onAction(action, selectedText)+ setVisible(false)+ // Clear selection+ window.getSelection()?.removeAllRanges()+ }+ }, [selectedText, onAction])++ useEffect(() => {+ const handleMouseUp = () => {+ setTimeout(handleSelection, 10)+ }++ const handleClickOutside = (e: MouseEvent) => {+ if (+ toolbarRef.current+ && !toolbarRef.current.contains(e.target as Node)+ && visible+ ) {+ setVisible(false)+ }+ }++ document.addEventListener('mouseup', handleMouseUp)+ document.addEventListener('click', handleClickOutside)++ return () => {+ document.removeEventListener('mouseup', handleMouseUp)+ document.removeEventListener('click', handleClickOutside)+ }+ }, [handleSelection, visible])++ if (!visible) return null++ const toolbarStyle: CSSProperties = {+ position: 'fixed',+ top: position.top,+ left: position.left,+ transform: 'translateX(-50%)',+ zIndex: 10000,+ }++ return (+ <div ref={toolbarRef} className={styles.textSelectionToolbar} style={toolbarStyle}>+ <Button.Group size="small">+ <Button+ icon={<CompressOutlined />}+ onClick={() => handleAction('shorten')}+ title={t('aiFeatures.shorten' as any)}+ >+ {t('aiFeatures.shorten' as any)}+ </Button>+ <Button+ icon={<ExpandOutlined />}+ onClick={() => handleAction('expand')}+ title={t('aiFeatures.expand' as any)}+ >+ {t('aiFeatures.expand' as any)}+ </Button>+ <Button+ icon={<EditOutlined />}+ onClick={() => handleAction('polish')}+ title={t('aiFeatures.polish' as any)}+ >+ {t('aiFeatures.polish' as any)}+ </Button>+ <Button+ icon={<TranslationOutlined />}+ onClick={() => handleAction('translate')}+ title={t('aiFeatures.translate' as any)}+ >+ {t('aiFeatures.translate' as any)}+ </Button>+ <Button+ icon={<TagsOutlined />}+ onClick={() => handleAction('generateHashtags')}+ title={t('aiFeatures.generateHashtags' as any)}+ >+ {t('aiFeatures.generateHashtags' as any)}+ </Button>+ <Button+ icon={<PictureOutlined />}+ onClick={() => handleAction('generateImage')}+ title={t('aiFeatures.generateImage' as any)}+ >+ {t('aiFeatures.generateImage' as any)}+ </Button>+ <Button+ icon={<VideoCameraOutlined />}+ onClick={() => handleAction('generateVideo')}+ title={t('aiFeatures.generateVideo' as any)}+ >+ {t('aiFeatures.generateVideo' as any)}+ </Button>+ </Button.Group>+ </div>+ )+})++export default TextSelectionToolbar+diff --git a/project/web/src/components/PublishDialog/index.tsx b/project/web/src/components/PublishDialog/index.tsxindex afdd0d4c..d3ce3b4f 100644--- a/project/web/src/components/PublishDialog/index.tsx+++ b/project/web/src/components/PublishDialog/index.tsx@@ -26,6 +26,7 @@ import { useWindowSize } from 'react-use'import { useShallow } from 'zustand/react/shallow'import { apiCreatePublish } from '@/api/plat/publish'import { toolsApi } from '@/api/tools'+import { getChatModels } from '@/api/ai'import {getDays,getUtcDays,@@ -51,13 +52,16 @@ import { useTransClient } from '@/app/i18n/client'import AvatarPlat from '@/components/AvatarPlat'import DownloadAppModal from '@/components/common/DownloadAppModal'import PlatParamsSetting from '@/components/PublishDialog/compoents/PlatParamsSetting'+import PublishDialogAi, { type AIAction, type IPublishDialogAiRef } from '@/components/PublishDialog/compoents/PublishDialogAi'+import TextSelectionToolbar from '@/components/PublishDialog/compoents/TextSelectionToolbar'import PublishDatePicker from '@/components/PublishDialog/compoents/PublishDatePicker'-import PublishDialogAi from '@/components/PublishDialog/compoents/PublishDialogAi'import PublishDialogPreview from '@/components/PublishDialog/compoents/PublishDialogPreview'import { usePublishManageUpload } from '@/components/PublishDialog/compoents/PublishManageUpload/usePublishManageUpload'+import { UploadTaskTypeEnum } from '@/components/PublishDialog/compoents/PublishManageUpload/publishManageUpload.enum'import PubParmasTextarea from '@/components/PublishDialog/compoents/PubParmasTextarea'import usePubParamsVerify from '@/components/PublishDialog/hooks/usePubParamsVerify'import { usePublishDialog } from '@/components/PublishDialog/usePublishDialog'+import type { IImgFile, IVideoFile } from '@/components/PublishDialog/publishDialog.type'import { usePublishDialogStorageStore } from '@/components/PublishDialog/usePublishDialogStorageStore'import { useAccountStore } from '@/store/account'import { generateUUID } from '@/utils'@@ -165,10 +169,17 @@ const PublishDialog = memo(const [showFacebookPagesModal, setShowFacebookPagesModal]= useState(false)const { t } = useTransClient('publish')- const { tasks, md5Cache } = usePublishManageUpload(+ // AI助手ref+ const aiAssistantRef = useRef<IPublishDialogAiRef>(null)+ // 聊天模型列表+ const [chatModels, setChatModels] = useState<any[]>([])+ // 中间内容区域ref,用于划词功能+ const contentAreaRef = useRef<HTMLDivElement>(null)+ const { tasks, md5Cache, enqueueUpload } = usePublishManageUpload(useShallow(state => ({tasks: state.tasks,md5Cache: state.md5Cache,+ enqueueUpload: state.enqueueUpload,})),)// 是否clear@@ -190,11 +201,17 @@ const PublishDialog = memo(const images = newPubItem.params.images// 视频匹配- if (video && !video.ossUrl) {- newPubItem.params.video!.ossUrl- = md5Cache[tasks[video.uploadTaskIds!.video!].md5!]?.ossUrl- newPubItem.params.video!.cover.ossUrl- = md5Cache[tasks[video.uploadTaskIds!.cover!].md5!]?.ossUrl+ if (video) {+ // 如果视频本身没有ossUrl,从上传任务中获取+ if (!video.ossUrl && video.uploadTaskIds?.video) {+ newPubItem.params.video!.ossUrl+ = md5Cache[tasks[video.uploadTaskIds.video].md5!]?.ossUrl+ }+ // 如果封面没有ossUrl,从上传任务中获取+ if (!video.cover.ossUrl && video.uploadTaskIds?.cover) {+ newPubItem.params.video!.cover.ossUrl+ = md5Cache[tasks[video.uploadTaskIds.cover].md5!]?.ossUrl+ }return newPubItem}// 图片匹配@@ -441,6 +458,26 @@ const PublishDialog = memo(useEffect(() => {if (open) {init(accounts, defaultAccountId)++ // 获取聊天模型列表(使用 sessionStorage 缓存)+ const cachedModels = sessionStorage.getItem('ai_chat_models')+ if (cachedModels) {+ try {+ setChatModels(JSON.parse(cachedModels))+ } catch (error) {+ console.error(t('messages.parseCachedChatModelsFailed' as any), error)+ }+ } else {+ // 如果没有缓存,则请求+ getChatModels().then((res: any) => {+ if (res?.code === 0 && res.data && Array.isArray(res.data)) {+ setChatModels(res.data)+ sessionStorage.setItem('ai_chat_models', JSON.stringify(res.data))+ }+ }).catch(error => {+ console.error(t('messages.getChatModelsFailed' as any), error)+ })+ }}else {isClear.current = true@@ -495,13 +532,10 @@ const PublishDialog = memo(const openLeftSide = useMemo(() => {if (!openLeft)return false- if (step === 0) {- return pubListChoosed.length !== 0- }- else {- return expandedPubItem !== undefined- }- }, [openLeft, step, pubListChoosed.length, expandedPubItem])+ // 如果用户主动打开了AI助手(openLeft=true),就保持打开状态+ // 不再依赖于 pubListChoosed 或 expandedPubItem 的状态+ return true+ }, [openLeft])useEffect(() => {setErrParamsMap(errParamsMap)@@ -556,6 +590,174 @@ const PublishDialog = memo(usePublishDialogStorageStore.getState().clearPubData()}, [pubListChoosed])+ // 处理划词操作+ const handleTextSelection = useCallback((action: AIAction, selectedText: string) => {+ // 只有当面板未打开时才设置,避免重复触发导致状态混乱+ if (!openLeft) {+ setOpenLeft(true)+ }+ // 等待面板打开动画完成后调用AI处理并自动发送+ setTimeout(() => {+ aiAssistantRef.current?.processText(selectedText, action)+ }, openLeft ? 100 : 500) // 如果已打开,减少延迟+ }, [openLeft, setOpenLeft])++ // AI内容同步到编辑器+ const handleSyncToEditor = useCallback(async (content: string, images?: IImgFile[], video?: IVideoFile, append?: boolean) => {+ console.log('父组件收到同步请求 - 内容:', content, '图片数量:', images?.length || 0, '视频:', video ? '有' : '无', '追加模式:', append)++ // 处理图片上传+ if (images && images.length > 0) {+ console.log('开始上传AI同步的图片')+ const uploadsWithImages: Array<{ image: IImgFile, promise: Promise<any>, cancel: () => void }> = []++ for (const image of images) {+ const handle = enqueueUpload({+ file: image.file,+ fileName: image.filename,+ type: UploadTaskTypeEnum.Image,+ })++ const imageWithTask: IImgFile = {+ ...image,+ uploadTaskId: handle.taskId,+ }++ uploadsWithImages.push({+ image: imageWithTask,+ promise: handle.promise,+ cancel: handle.cancel,+ })+ }++ // 使用带有 uploadTaskId 的图片+ images = uploadsWithImages.map(item => item.image)+ console.log('图片上传任务已创建:', images)+ }++ // 处理视频上传(AI生成的视频已经有ossUrl,只需要上传封面)+ if (video) {+ console.log('处理AI同步的视频,ossUrl:', video.ossUrl)++ // 如果视频已经有ossUrl(AI生成的),只需要上传封面+ if (video.ossUrl && !video.cover.ossUrl) {+ console.log('视频已有OSS地址,只上传封面')+ const coverHandle = enqueueUpload({+ file: video.cover.file,+ fileName: video.cover.filename,+ type: UploadTaskTypeEnum.Image,+ })++ video = {+ ...video,+ uploadTaskIds: {+ cover: coverHandle.taskId,+ },+ }+ }+ // 如果视频没有ossUrl(用户上传的),需要上传视频和封面+ else if (!video.ossUrl) {+ console.log('上传视频和封面')+ const videoHandle = enqueueUpload({+ file: video.file,+ fileName: video.filename,+ type: UploadTaskTypeEnum.Video,+ })++ const coverHandle = enqueueUpload({+ file: video.cover.file,+ fileName: video.cover.filename,+ type: UploadTaskTypeEnum.Image,+ })++ video = {+ ...video,+ uploadTaskIds: {+ video: videoHandle.taskId,+ cover: coverHandle.taskId,+ },+ }+ }+ console.log('视频处理完成:', video)+ }++ // 如果只有一个账号,直接更新+ if (pubListChoosed.length === 1) {+ const params: any = {}+ // 只有当 content 不为空字符串时才更新文案+ if (content) {+ // 如果是追加模式,将内容追加到现有文案后面+ if (append && pubListChoosed[0].params.des) {+ params.des = pubListChoosed[0].params.des + '\n' + content+ } else {+ params.des = content+ }+ }+ // 视频和图片不能同时存在+ if (video) {+ console.log('设置视频到单账号参数')+ params.video = video+ // 如果有视频,清空图片+ params.images = []+ } else if (images && images.length > 0) {+ console.log('设置图片到单账号参数')+ params.images = images+ }+ console.log('更新单账号参数:', params)+ setOnePubParams(params, pubListChoosed[0].account.id)+ }+ // 如果是多账号且在第一步,更新公共参数+ else if (pubListChoosed.length >= 2 && step === 0) {+ const params: any = {}+ // 只有当 content 不为空字符串时才更新文案+ if (content) {+ // 如果是追加模式,将内容追加到现有文案后面+ if (append && commonPubParams.des) {+ params.des = commonPubParams.des + '\n' + content+ } else {+ params.des = content+ }+ }+ // 视频和图片不能同时存在+ if (video) {+ console.log('设置视频到多账号公共参数')+ params.video = video+ // 如果有视频,清空图片+ params.images = []+ } else if (images && images.length > 0) {+ console.log('设置图片到多账号公共参数')+ params.images = images+ }+ console.log('更新多账号公共参数:', params)+ setAccountAllParams(params)+ }+ // 如果在第二步且有展开的项,更新该项+ else if (step === 1 && expandedPubItem) {+ const params: any = {}+ // 只有当 content 不为空字符串时才更新文案+ if (content) {+ // 如果是追加模式,将内容追加到现有文案后面+ if (append && expandedPubItem.params.des) {+ params.des = expandedPubItem.params.des + '\n' + content+ } else {+ params.des = content+ }+ }+ // 视频和图片不能同时存在+ if (video) {+ console.log('设置视频到展开项参数')+ params.video = video+ // 如果有视频,清空图片+ params.images = []+ } else if (images && images.length > 0) {+ console.log('设置图片到展开项参数')+ params.images = images+ }+ console.log('更新展开项参数:', params)+ setOnePubParams(params, expandedPubItem.account.id)+ }+ }, [pubListChoosed, step, expandedPubItem, setOnePubParams, setAccountAllParams, enqueueUpload])+const imperativeHandle: IPublishDialogRef = {setPubTime,}@@ -578,7 +780,12 @@ const PublishDialog = memo(classNames="left"unmountOnExit>- <PublishDialogAi onClose={() => setOpenLeft(false)} />+ <PublishDialogAi+ ref={aiAssistantRef}+ onClose={() => setOpenLeft(false)}+ onSyncToEditor={handleSyncToEditor}+ chatModels={chatModels}+ /></CSSTransition>)}@@ -590,7 +797,13 @@ const PublishDialog = memo(}}}>- <div className="publishDialog-con">+ {/* 划词工具栏 */}+ <TextSelectionToolbar+ containerRef={contentAreaRef}+ onAction={handleTextSelection}+ />++ <div className="publishDialog-con" ref={contentAreaRef}><div className="publishDialog-con-head"><span className="publishDialog-con-head-title">{t('title')}@@ -971,7 +1184,12 @@ const PublishDialog = memo(classNames="left"unmountOnExit>- <PublishDialogAi onClose={() => setOpenLeft(false)} />+ <PublishDialogAi+ ref={aiAssistantRef}+ onClose={() => setOpenLeft(false)}+ onSyncToEditor={handleSyncToEditor}+ chatModels={chatModels}+ /></CSSTransition>)}<CSSTransitiondiff --git a/project/web/src/components/PublishDialog/publishDialog.module.scss b/project/web/src/components/PublishDialog/publishDialog.module.scssindex 7e3e74d4..55614dfe 100644--- a/project/web/src/components/PublishDialog/publishDialog.module.scss+++ b/project/web/src/components/PublishDialog/publishDialog.module.scss@@ -168,15 +168,19 @@ $assistantWidth: 380px;padding: 15px 15px 0 15px;display: flex;justify-content: space-between;+ flex-shrink: 0;.anticon {cursor: pointer;font-size: 20px;}}.publishDialogAi-wrapper {- min-width: $assistantWidth;- height: 100%;+ min-width: 330px;+ flex: 1;min-height: 0;+ overflow: hidden;+ display: flex;+ flex-direction: column;}}}