Compare commits

...

11 Commits

Author SHA1 Message Date
“ydy0615” caf1ac1c01 refactor: replace Kokoro-82M with suno/bark for TTS, update HF cache path, and add model warmup on startup. 2026-04-06 13:40:41 +08:00
ydy0615 7985fe9641 feat(tts): add api endpoints and optimization for apple silicon
Introduce a comprehensive TTS/ASR module that:
- Adds /v1/tts-asr/config, /status, /warmup, /tts, /asr endpoints with detailed JSON responses
- Implements Apple‑Silicon detection, device selection (MPS/CUDA/CPU), and memory limiting logic
- Supports selectable model size, quantization, and offline mode via environment variables
- Adds robust audio validation and multi‑path resampling fallback
- Provides new README sections for API usage, device detection, and performance benchmarking
- Includes a full testing suite: unit tests, integration tests, macOS simulation and performance reports
- Updates backend dependencies and CI scripts
- Adds new front‑end views and components for Univer editor integration

All changes are backward compatible; new features are exposed through environment variables and new API routes.
2026-04-06 11:14:09 +08:00
ydy0615 c70cb2a9f0 refactor(ui): add context menu and file content viewer to Docs view
Introduce ContextMenu.vue and FileContent.vue components for interactive file operations
and file preview.

Update FileTree to support root drop, integrate the new components into DocsView,
and refresh i18n strings for file actions.

Refactor MilkdownEditor to embed TTS menu and player.
2026-04-05 23:30:01 +08:00
ydy0615 01b132266a feat(ui): add file explorer, TTS UI, views and routing
Add a file tree UI and corresponding composable for local file management.
Introduce TTS menu and player components for voice synthesis integration.
Add new EditorView and DocsView routes and update SettingsPanel view switching.
Enhance Mermaid plugin with improved styling and action buttons.
2026-04-05 23:22:00 +08:00
ydy0615 818baa349a modified: backend/llm.py
modified:   src/components/MilkdownEditor.vue
	modified:   src/utils/config.js
	modified:   src/utils/i18n.js
2026-04-05 15:10:23 +08:00
ydy0615 9293d48c1b modified: src/components/SettingsPanel.vue 2026-04-05 14:14:21 +08:00
ydy0615 68ed783d6c feat: LLM 应用网页开发及内联建议功能实现 2026-04-05 13:42:29 +08:00
ydy0615 9904b9bd78 feat: 批量上传支持及prompt优化
- 支持多文件批量上传,一次最多10个
- 新增json/toml/yaml格式支持
- 优化inline补全prompt结构,增加边界决策指南
- size计算包含doc_block内容长度
- 超限时显示警告tooltip
2026-04-05 11:40:56 +08:00
ydy0615 7ed199aaf1 style: 简化文档卡片样式,优化布局间距 2026-04-05 10:16:16 +08:00
ydy0615 9ff51ac2f3 feat(plugin): add document export, doc‑block, and TTS/ASR support
Adds a DocBlock component that renders embedded documents, new export buttons for DOCX
and PDF, and updates the file‑upload picker to accept *.txt, *.docx, *.pptx, and *.pdf.
Introduces a DOCX→PDF conversion bridge in the backend and new /tts and /asr
endpoints that expose TTS and speech‑recognition functionality.  The README is
rewritten to describe the new features and clean up legacy documentation.  All
changes are backward‑compatible and do not introduce breaking API changes.
2026-04-04 23:56:18 +08:00
ydy0615 be4000b774 chore: 更新项目配置和依赖,优化前后端代码 2026-04-04 20:05:40 +08:00
65 changed files with 18102 additions and 1955 deletions
+9
View File
@@ -0,0 +1,9 @@
{
"shortcuts": [
{
"label": "Run",
"command": "npm run dev",
"icon": "play"
}
]
}
+4 -3
View File
@@ -1,3 +1,4 @@
VITE_API_BASE_URL=http://149.104.29.239:8001 VITE_API_BASE_URL=
VITE_API_URL=http://149.104.29.239:8001/v1/completions VITE_API_URL=
VITE_OCR_URL=http://149.104.29.239:8001/v1/ocr VITE_OCR_URL=
VITE_CONVERT_URL=
+7 -1
View File
@@ -1,4 +1,4 @@
# Logs # Logs
logs logs
*.log *.log
npm-debug.log* npm-debug.log*
@@ -39,3 +39,9 @@ env/
*.njsproj *.njsproj
*.sln *.sln
*.sw? *.sw?
# IDE directories
.kilocode/
.kilo/
.codex/
+52
View File
@@ -0,0 +1,52 @@
# 导出按钮缺失修复计划
## 问题分析
当前 `action-buttons` 区域只有以下按钮可见:
- 上传文件
- 导入 Markdown
- 导出 Markdown
- 上传图片
- AI 切换按钮
**缺失功能**DOCX 和 PDF 导出按钮
## 调查结果
1. ✅ 翻译文件中已存在 `exportDocx``exportPdf` 键名(src/utils/i18n.js
2. ❌ 模板中**完全缺失**这两个按钮的 HTML 代码
3. ❓ 导出功能后端已实现,前端只需要添加调用接口的按钮
4. ✅ 相关 CSS 样式已存在,按钮外观无需额外调整
## 实施计划
### 1. 添加 UI 按钮
`src/components/MilkdownEditor.vue:79` 之后添加两个新按钮:
- DOCX 导出按钮
- PDF 导出按钮
按钮位置:
```
导出 Markdown → 导出 DOCX → 导出 PDF → 上传图片
```
### 2. 实现前端导出功能
使用已安装的依赖库:
- `docx` 库:用于 DOCX 导出
- `html2pdf.js` 库:用于 PDF 导出
需要添加的函数:
```javascript
const exportDocx = async () => {
// 使用 docx 库实现导出
}
const exportPdf = async () => {
// 使用 html2pdf.js 实现导出
}
```
### 3. 按钮图标
- DOCX:使用文档图标
- PDF:使用 PDF 专用图标
### 4. 状态管理
添加加载状态和错误处理,与现有按钮保持一致风格
+65
View File
@@ -0,0 +1,65 @@
# rules.md
在构建这个LLM应用网页时,你需要基于VUE3开发。我需要前端只运行渲染和数据回传,后端负责llm api调用,类似copilet的auto inline suggustions实现和数据解析。
# **重要** : 在回复用户消息时,一定要使用中文
## 指导原则
- 不要擅自用npm或者yarn运行网页,你既看不到网页的内容,也无法阻止命令暂停。但是,你可以用npm run build检查代码。
- 应该保证代码效率,不多定义变量,不写冗余注释,把降低延迟放在第一位。
- 每次完成任务前都要反复阅读检查代码,确保代码准确无误。
- 尽量不要搜索关键字,而是了解代码结构后查询整个问题代码明确问题所在。
- @/milkdown-docs/ 代表milkdown的最新官方文档,不要修改,涉及到前端编辑器的指令时要核对官方文档。
# 仓库指南
## 语言约定
项目文档、日志、错误提示以及对外返回的文字信息统一使用 **中文**。前端 UI 默认展示中文,若需多语言支持请在相应模块实现。
## 项目结构 \& 模块组织
```
backend/ # FastAPI 后端(Python
├─ main.py # API 入口
├─ llm.py # LLM 包装工具
├─ prompt.py # Prompt 构建辅助
└─ tests/ # pytest 测试套件
public/ # 前端静态资源
src/ # 前端源码(Vite + React
dist/ # 构建产出(生成文件)
```
生产代码主要位于 `backend/`Python)和 `src/`(JS/TS)。测试文件与被测模块并置。
## 构建、测试、开发命令
| 命令 | 说明 |
|----------------------------------------------|--------------------------------------------------|
| `npm install` | 安装前端依赖 |
| `npm run dev` | 启动 Vite 开发服务器 |
| `uvicorn backend.main:app --reload` | 本地运行 FastAPI 服务 |
| `pytest` | 运行 Python 测试套件 |
| `npm run build` | 生成生产环境构建产物至 `dist/` |
## 编码风格 \& 命名约定
- **Python**:使用 4 空格缩进,`snake_case` 命名函数/变量,`PascalCase` 命名类。提交前请使用 `ruff`/`black` 格式化。
- **JavaScript/TypeScript**:使用 2 空格缩进,`camelCase` 命名变量/函数,`PascalCase` 命名 React 组件。使用 `eslint``prettier` 检查。
- 文件名采用全小写加短横线,例如 `my-module.py``my-component.tsx`
## 测试指南
- 后端使用 **pytest**,测试文件放在对应模块目录下,命名为 `test_<module>.py`
- 目标覆盖率 ≥ 80%`pytest --cov=backend`)。
- 在虚拟环境中运行:`pip install -r backend/requirements.txt && pytest`
## 提交 \& Pull Request 规范
- 提交信息遵循 **Conventional Commits**`feat:` 新功能、`fix:` 修复、`docs:` 文档、`refactor:` 重构等。
- PR 必须包含:
- 与提交信息匹配的标题。
- 关联的 Issue(如 `Fixes #123`)。
- UI 变更或 API 示例的截图/示例。
- 所有 CI 检查(代码检查、测试、类型检查)均通过。
## 安全 \& 配置建议
- 敏感信息请放入 `.env` 并确保已在 `.gitignore` 中。
- 按照 `backend/main.py` 中的实现,对上传文件的大小和类型进行校验,防止滥用。
- 定期审计依赖安全(`npm audit``pip-audit`)。
+110 -227
View File
@@ -1,264 +1,147 @@
# LLM in Text - 智能写作助手 # LLM in Text - 智能写作助手
基于 Vue3 和 FastAPI 的智能 Markdown 编辑器,集成大语言模型(LLM)实时补全建议功能,提供类似 GitHub Copilot 的 Ghost Text 体验 基于 Vue3 和 FastAPI 的智能 Markdown 编辑器,集成大语言模型(LLM)实时补全建议功能。
## 功能特性 ## 功能特性
### Markdown 编辑器 ### Markdown 编辑器
- 基于 Milkdown Crepe 的所见即所得编辑体验 - 基于 Milkdown Crepe 的所见即所得编辑体验
- 支持完整 Markdown 语法和 LaTeX 公式 - 支持 Markdown 语法和 LaTeX 公式
- 支持 Mermaid 图表渲染
- 导入/导出 Markdown 文件 - 导入/导出 Markdown 文件
- 导出 DOCX 和 PDF 格式
### AI 智能补全 ### AI 智能补全
- 实时生成文本补全建议(灰色显示) - 实时生成文本补全建议(灰色显示)
- 流式响应,低延迟体验 - 流式响应,低延迟体验
- 多种交互方式: - 多种交互方式:Tab接受、Esc拒绝、点击接受
- **Tab 键**:接受建议
- **Esc 键**:拒绝建议
- **点击灰色文本**:接受建议
- **继续输入**:自动拒绝建议
### AI 开关控制 ### 文档处理
- 右下角 AI 开关按钮 - OCR 图片识别:上传图片自动识别文字
- 白色 = AI 启用,黑色 = AI 禁用 - 文档转换:PDF、DOCX、PPTX、TXT 转 Markdown
- 禁用时自动清除灰色文本并停止 API 调用 - 文档块嵌入:可折叠的文档预览块
- 智能大小限制:32KB自动禁用AI
### 设置面板
- 外观主题:亮色/暗色/跟随系统
- 背景模式:默认/暖色/阅读灯/自定义图片
- 模型智能:低/中/高思考级别
- 隐私控制:隐私模式防止发送IP
- 多语言界面:中英日韩德法
### 语音功能
- TTS文字转语音(macOS优化,支持Apple Silicon M1/M2/M3
- STT语音转文字(支持多种模型大小和量化)
- 自动设备检测(MPS/CUDA/CPU智能切换)
- 离线模式支持(模型缓存检查)
## 技术架构 ## 技术架构
```mermaid 前端: Vue3 + Vite + Milkdown + ProseMirror
flowchart TB 后端: FastAPI + Python + Ollama
subgraph Frontend["前端 (Vue3 + Vite)"]
A[App.vue] --> B[MilkdownEditor.vue]
B --> C[Crepe Editor]
C --> D[ProseMirror]
D --> E[copilotPlugin.ts]
E --> F[copilotGhostMark]
E --> G[api.js]
end
subgraph Backend["后端 (FastAPI + Python)"]
H[main.py<br/>FastAPI Server] --> I[prompt.py<br/>Prompt 构建]
H --> J[llm.py<br/>Ollama 调用]
J --> K[Ollama API]
end
G -->|POST /v1/completions<br/>SSE 流式响应| H
K -->|LLM 响应| J
```
## 项目结构
```
llm-in-text/
├── src/
│ ├── components/
│ │ └── MilkdownEditor.vue # 主编辑器组件
│ ├── plugins/
│ │ ├── copilotPlugin.ts # ProseMirror AI 补全插件
│ │ ├── types.ts # 类型定义
│ │ └── index.ts # 插件导出
│ ├── utils/
│ │ ├── api.js # API 调用封装
│ │ ├── config.js # 配置文件
│ │ └── ocrCache.js # OCR 缓存管理
│ ├── App.vue
│ └── main.js
├── backend/
│ ├── main.py # FastAPI 服务器
│ ├── llm.py # LLM API 调用
│ ├── prompt.py # Prompt 构建
│ └── requirements.txt
└── README.md
```
## 快速开始 ## 快速开始
### 环境要求 环境: Node.js 18+、Python 3.8+、Ollama
- Node.js 18+
- Python 3.8+
- Ollama 服务(或其他兼容 OpenAI API 的服务)
### 安装 安装:
- 前端: npm install
- 后端: pip install -r backend/requirements.txt
```bash 启动:
# 前端 - 后端: python backend/main.py (端口8001)
npm install - 前端: npm run dev (端口5173)
# 后端 ## API接口
cd backend
pip install -r requirements.txt
```
### 配置 - POST /v1/completions 流式补全建议
- POST /v1/ocr 图片文字识别
- POST /v1/convert 文档转换
- POST /v1/completions/cancel 取消请求
- GET /v1/tts-asr/status TTS/ASR模型状态
- GET /v1/tts-asr/config TTS/ASR配置信息
- POST /v1/tts-asr/warmup 模型预热
- POST /v1/tts-asr/tts 文字转语音
- POST /v1/tts-asr/asr 语音转文字
`backend/.env` 中配置: ## TTS/ASR环境变量配置
```env 支持以下环境变量来配置TTS/ASR模块:
OLLAMA_MODEL=gpt-oss:20b
OLLAMA_HOST=http://localhost:11434
```
### 启动 | 变量名 | 说明 | 默认值 |
|--------|------|--------|
| `TTS_ASR_DEVICE` | 设备选择 (auto/mps/cuda/cpu) | auto |
| `TTS_ASR_MODEL_SIZE` | ASR模型大小 (tiny/base/small/medium/large/turbo) | auto |
| `TTS_ASR_QUANTIZE` | 是否使用INT8量化 (true/false) | false |
| `TTS_ASR_OFFLINE_MODE` | 离线模式,仅使用缓存模型 (true/false) | false |
| `TTS_ASR_WARMUP` | 启动时预热模型 (true/false) | true |
| `TTS_ASR_WARMUP_TIMEOUT` | 预热超时时间(秒) | 120 |
| `TTS_ASR_IDLE_TIMEOUT` | 空闲卸载时间(秒,0=不卸载) | 0 |
| `TTS_ASR_MPS_MEMORY_LIMIT_MB` | MPS内存限制(MB) | 8192 |
```bash **Apple Silicon优化建议**:
# 后端(端口 8000 - 系统自动检测Apple Silicon并推荐使用`small`模型
cd backend - MPS内存限制默认为系统内存的60%
python main.py - 建议使用`small``medium`模型以获得更好的性能
- 可通过`TTS_ASR_MODEL_SIZE=medium`手动指定模型大小
# 前端(端口 5173
npm run dev
```
访问 http://localhost:5173
## API 接口
### POST /v1/completions
流式获取补全建议
**请求:**
```json
{
"prefix": "# Title\n\nContent ",
"suffix": "",
"languageId": "markdown"
}
```
**响应(SSE):**
```
data: {"content": "here"}
data: {"content": "here is"}
data: {"done": true}
```
## 核心实现 ## 核心实现
### 后端设计 ### 后端
- main.py: FastAPI服务器、SSE流式响应
- llm.py: 异步Ollama调用、超时控制
- prompt.py: 7条Prompt规则
- tts_asr.py: macOS/Apple Silicon优化的TTS/ASR处理
- 自动检测Apple Silicon (M1/M2/M3)
- MPS/CUDA/CPU智能降级
- 支持多种Whisper模型大小
- INT8量化支持
- 离线模式支持
- 健壮的音频重采样
#### main.py - FastAPI 服务器 ### 前端
- 定义 `/v1/completions` 端点 - copilotPlugin.ts: ProseMirror Mark系统
- 使用 `StreamingResponse` 返回 SSE 流式响应 - 关键函数: scheduleFetch、insertGhostText
- CORS 配置允许跨域请求 - Pinia Store状态管理
#### llm.py - LLM 调用封装
- 使用 `ollama.AsyncClient` 异步调用
- 支持 `think='high'` 思考模式
- 返回 `content``thinking` 字段
#### prompt.py - Prompt 工程
精心设计的 Prompt 模板,包含 7 条核心规则:
| 规则 | 说明 |
|------|------|
| RULE #1 | 无缝连接 - 不重复 suffix 内容,避免"复读机"错误 |
| RULE #2 | 空白处理 - 避免双空格,正确对接标点 |
| RULE #3 | 缩进对齐 - 匹配当前缩进级别和类型 |
| RULE #4 | 列表维护 - 识别并继续任务列表、有序列表、无序列表 |
| RULE #5 | 语法闭合 - 自动闭合未完成的 Markdown 语法 |
| RULE #6 | 输出格式 - 仅输出续写文本,无解释无注释 |
| RULE #7 | 必须输出 - 始终提供有用的续写建议 |
### 前端设计
#### ProseMirror Mark 系统
使用 ProseMirror 的 Mark 系统实现灰色建议文本:
```typescript
// 定义 ghost mark
export const copilotGhostMark = $markSchema('copilot_ghost', () => ({
excludes: '_',
inclusive: true,
toDOM: () => ['span', {
'data-copilot-ghost': '',
class: 'copilot-ghost-text'
}, 0]
}))
// CSS 样式
.copilot-ghost-text {
color: #999;
opacity: 0.6;
}
```
#### copilotPlugin 核心逻辑
```mermaid
flowchart LR
A[用户输入] --> B{文档变化?}
B -->|是| C[清除旧建议]
C --> D[防抖 1000ms]
D --> E[发送 API 请求]
E --> F[收到建议]
F --> G[插入 Ghost Text]
G --> H{用户操作}
H -->|Tab| I[接受建议<br/>移除 mark]
H -->|Esc| J[拒绝建议<br/>删除文本]
H -->|点击 Ghost| I
H -->|继续输入| J
```
#### 关键函数
| 函数 | 作用 |
|------|------|
| `scheduleFetch` | 防抖调度 API 请求 |
| `insertGhostText` | 插入带 mark 的建议文本 |
| `acceptSuggestion` | Tab 接受建议 |
| `rejectSuggestion` | Esc 拒绝建议 |
| `clearGhostText` | 清除当前建议 |
### 数据流
```mermaid
sequenceDiagram
participant U as 用户
participant E as Editor (ProseMirror)
participant P as copilotPlugin
participant A as api.js
participant B as Backend
participant L as LLM
U->>E: 输入文本
E->>P: view.update()
P->>P: 清除旧建议
P->>P: 防抖 1000ms
P->>A: fetchSuggestion(prefix, suffix)
A->>B: POST /v1/completions
B->>B: build_prompt()
B->>L: ollama.chat()
L-->>B: {content, thinking}
B-->>A: SSE stream
A-->>P: suggestion text
P->>E: insertGhostText()
E-->>U: 显示灰色建议
alt Tab 键
U->>P: Tab
P->>E: acceptSuggestion()
E-->>U: 建议变为正常文本
else Esc 键
U->>P: Esc
P->>E: rejectSuggestion()
E-->>U: 建议消失
else 继续输入
U->>E: 输入其他字符
E->>P: handleKeyDown()
P->>E: clearGhostText()
end
```
## 设计亮点 ## 设计亮点
1. **前后端分离**:前端只负责渲染和数据回传,后端负责 LLM 调用、Prompt 构建和数据解析 1. 前后端分离
2. **低延迟优化**:防抖机制 (1000ms) + SSE 流式响应 + AbortController 取消过期请求 2. 低延迟优化:防抖+SSE+AbortController
3. **ProseMirror Mark 系统**:与编辑器状态完美集成,支持 Undo/Redo 3. ProseMirror Mark系统
4. **多种交互方式**:Tab/Esc/点击/输入,用户体验友好 4. 多种交互方式
5. **智能大小限制**:文档超过 32KB 自动禁用 AI 功能 5. 智能大小限制
6. 隐私保护
7. 多语言支持
8. 主题定制
9. 文档处理
10. 语音功能
## 开发指南
代码风格: Python(4空格,snake_case) JS/TS(2空格,camelCase)
测试: pytest
构建: npm run build
### 运行测试
项目提供完整的测试套件,包括单元测试、集成测试和macOS环境模拟测试:
```bash
# 快速运行单元测试
python backend/tests/run_tests.py unit
# 运行集成测试(需要启动后端服务)
python backend/tests/run_tests.py integration
# 运行macOS环境模拟测试(在非Mac环境测试)
python backend/tests/run_tests.py simulate
# 运行所有测试
python backend/tests/run_tests.py all
```
详细测试说明请参考: [测试指南](backend/tests/TESTING_GUIDE.md)
## 许可证 ## 许可证
+234
View File
@@ -0,0 +1,234 @@
# TTS/ASR模块修复完成总结
## 修复概览
本次修复彻底重构了`backend/tts_asr.py`,针对macOS和Apple Silicon (M1/M2/M3)进行了全面优化,并提供了完整的测试套件。
## 修复日期
**完成时间**: 2026-04-06
## 修改文件清单
### 核心修改
-`backend/tts_asr.py` - 主要重构(~1150行)
-`backend/requirements.txt` - 添加新依赖
-`README.md` - 更新文档
### 测试脚本(新增)
-`backend/tests/test_tts_asr_unit.py` - 单元测试
-`backend/tests/test_tts_asr_integration.py` - 集成测试
-`backend/tests/simulate_macos.py` - macOS环境模拟工具
-`backend/tests/run_tests.py` - 测试运行器
-`backend/tests/quick_verify.py` - 快速验证脚本
### 文档(新增)
-`backend/TTS_ASR_MACOS_FIX.md` - 详细修复说明
-`backend/tests/TESTING_GUIDE.md` - 测试指南
## 核心改进汇总
### 1. 设备检测系统(DeviceCapabilities
**改进前**:
- 简单的MPS/CUDA检测
- 缺少内存管理
- 无Apple Silicon特殊处理
**改进后**:
- `DeviceCapabilities`数据类,结构化存储设备信息
- 全面的MPS/CUDA可用性测试(1000x1000矩阵运算)
- Apple Silicon自动识别(Darwin + arm64
- 动态内存管理(MPS内存限制为系统内存的60%)
- 智能设备降级策略
### 2. 模型加载优化
**改进前**:
- 固定使用large-v3-turbo模型
- 无内存优化选项
- 缺少离线模式支持
**改进后**:
- 6种模型大小可选(tiny/base/small/medium/large/turbo
- Apple Silicon自动推荐`small`模型
- INT8量化支持(减少内存占用)
- 离线模式(检查模型缓存)
- 环境变量驱动的配置
### 3. 音频处理鲁棒性
**改进前**:
- librosa.resample无回退
- 缺少音频验证
**改进后**:
- `_validate_audio_data()`: 完整的音频数据验证
- `_resample_audio_robust()`: 多重回退重采样
- librosa.resample → torchaudio → NumPy线性插值
- 所有音频操作都有完整的错误处理
### 4. 环境变量配置
| 变量名 | 说明 | 默认值 |
|--------|------|--------|
| `TTS_ASR_DEVICE` | 设备选择 | `auto` |
| `TTS_ASR_MODEL_SIZE` | ASR模型大小 | `auto` |
| `TTS_ASR_QUANTIZE` | INT8量化 | `false` |
| `TTS_ASR_OFFLINE_MODE` | 离线模式 | `false` |
| `TTS_ASR_WARMUP` | 启动预热 | `true` |
| `TTS_ASR_WARMUP_TIMEOUT` | 预热超时(秒) | `120` |
| `TTS_ASR_IDLE_TIMEOUT` | 空闲卸载(秒) | `0` |
| `TTS_ASR_MPS_MEMORY_LIMIT_MB` | MPS内存限制 | `8192` |
### 5. 新增API端点
- `GET /v1/tts-asr/config`: 获取完整配置信息
- 增强`/v1/tts-asr/status`: 包含设备能力、模型大小等
- 增强`/v1/tts-asr/warmup`: 返回详细预热结果
## 测试套件概览
### 单元测试(test_tts_asr_unit.py
覆盖8个测试类,共20+测试用例:
- `TestAppleSiliconDetection`: Apple Silicon检测
- `TestEnvironmentVariables`: 环境变量解析
- `TestModelSizeSelection`: 模型大小选择
- `TestAudioValidation`: 音频验证
- `TestAudioResampling`: 音频重采样
- `TestDeviceCapabilities`: 设备能力
- `TestModelCacheCheck`: 模型缓存
- `TestRequestResponseModels`: API模型
### 集成测试(test_tts_asr_integration.py
需要运行后端服务,测试完整API流程:
- 配置端点测试
- 状态端点测试
- 预热端点测试
- TTS功能测试
- ASR功能测试
- API密钥验证
- 长文本处理
- 性能基准测试
### macOS模拟测试(simulate_macos.py
在非macOS环境下模拟Apple Silicon环境:
- Apple Silicon环境模拟
- MPS设备模拟
- CUDA设备模拟
- 内存管理测试
- 完整环境变量测试
## 使用建议
### Apple Silicon推荐配置
**8GB内存**:
```bash
export TTS_ASR_MODEL_SIZE=small
export TTS_ASR_MPS_MEMORY_LIMIT_MB=4096
```
**16GB+内存**:
```bash
export TTS_ASR_MODEL_SIZE=medium
export TTS_ASR_MPS_MEMORY_LIMIT_MB=8192
```
**内存紧张**:
```bash
export TTS_ASR_MODEL_SIZE=tiny
export TTS_ASR_QUANTIZE=true
```
### 快速开始
```bash
# 1. 安装依赖
pip install -r backend/requirements.txt
# 2. 快速验证
python backend/tests/quick_verify.py
# 3. 运行单元测试
pytest backend/tests/test_tts_asr_unit.py -v
# 4. macOS模拟测试
python backend/tests/simulate_macos.py --full-simulation
# 5. 启动后端服务
python backend/main.py
# 6. 运行集成测试(另一终端)
python backend/tests/test_tts_asr_integration.py
```
## 向后兼容性
所有改动保持100%向后兼容:
- ✅ 现有API端点未改变
- ✅ 默认行为与原版一致
- ✅ 新功能通过环境变量启用
- ✅ 无需修改现有代码
## 已知限制
1. **MPS float16**: 默认使用float32以避免潜在问题
2. **8-bit量化**: 仅在CPU和CUDA环境支持
3. **Core ML**: 预留扩展点但未实现
## 性能影响
- **Apple Silicon**: 推荐使用small模型,性能更稳定
- **MPS内存**: 自动限制为系统内存的60%,避免OOM
- **模型加载**: 支持预热和空闲卸载,优化内存使用
## 故障排查
### 模型加载失败
1. 检查网络连接
2. 关闭离线模式: `export TTS_ASR_OFFLINE_MODE=false`
3. 使用预热端点: `POST /v1/tts-asr/warmup`
### MPS内存不足
1. 使用更小模型: `export TTS_ASR_MODEL_SIZE=tiny`
2. 启用量化: `export TTS_ASR_QUANTIZE=true`
3. 降低内存限制: `export TTS_ASR_MPS_MEMORY_LIMIT_MB=4096`
### 音频处理失败
1. 检查音频格式(支持WAV
2. 确保采样率≥8000Hz
3. 查看详细日志
## 未来改进方向
1. 集成Core ML作为备选推理后端
2. 支持torch.compile (PyTorch 2.0+)
3. 实现模型下载进度显示
4. 添加更多音频格式支持
## 验证状态
✅ 所有文件已创建
✅ 核心函数已实现
✅ 环境变量已配置
✅ 测试脚本已编写
✅ 文档已更新
## 联系方式
如有问题,请参考:
- [修复详细说明](./TTS_ASR_MACOS_FIX.md)
- [测试指南](./tests/TESTING_GUIDE.md)
- [README更新](../README.md)
---
**修复完成确认**: 所有TTS/ASR模块修复已完成,代码已全面重构并优化,测试套件完整,文档齐全。
+319
View File
@@ -0,0 +1,319 @@
# TTS/ASR macOS适配修复说明
## 修复概述
本次修复彻底重构了`backend/tts_asr.py`,针对macOS和Apple Silicon (M1/M2/M3)进行了全面优化。
## 主要改进
### 1. 增强的设备检测 (`_detect_device_capabilities`)
**改进前问题**:
- 简单的张量乘法测试不足以验证MPS设备实际可用性
- 缺少内存限制检测
- Apple Silicon没有特殊处理
**改进后**:
- 使用`DeviceCapabilities`结构化存储设备信息
- 更全面的MPS测试(1000x1000矩阵运算)
- 自动检测Apple Silicon并调整内存限制
- 根据系统内存动态设置MPS内存阈值(默认60%)
- 支持设备能力降级(MPS→CPU, CUDA→CPU
**验证方法**:
```python
# 在Python环境中测试
from tts_asr import _detect_device_capabilities
caps = _detect_device_capabilities()
print(f"Device: {caps.device}")
print(f"MPS Available: {caps.mps_available}")
print(f"Apple Silicon: {_is_apple_silicon()}")
```
### 2. 模型大小选择和量化支持
**新增环境变量**:
- `TTS_ASR_MODEL_SIZE`: 选择Whisper模型大小
- `tiny`: 最小模型,最快但准确度较低
- `base`: 基础模型,平衡性能和准确度
- `small`: 推荐用于Apple Silicon
- `medium`: 中等模型
- `large`: 大模型,最高准确度
- `turbo`: large-v3-turbo (原默认模型)
- `auto`: 自动选择(Apple Silicon默认small
- `TTS_ASR_QUANTIZE`: 启用INT8量化(减少内存占用)
**Apple Silicon优化**:
- 自动检测并推荐`small`模型
- 考虑MPS内存限制选择合适模型
**验证方法**:
```python
# 查看推荐的模型大小
from tts_asr import _get_recommended_model_size
print(_get_recommended_model_size()) # Apple Silicon: "small"
```
### 3. 离线模式支持
**新增环境变量**:
- `TTS_ASR_OFFLINE_MODE`: 启用离线模式
- 启动前检查模型是否已缓存
- 缓存不存在时优雅失败而非崩溃
**验证方法**:
```bash
# 启用离线模式
export TTS_ASR_OFFLINE_MODE=true
python backend/main.py
# 检查模型缓存
python -c "from tts_asr import _check_model_cached; print(_check_model_cached('openai/whisper-small'))"
```
### 4. 健壮的音频处理
**改进前问题**:
- `librosa.resample`失败时无回退
- 缺少音频数据验证
**改进后**:
- `_validate_audio_data()`: 验证音频数据有效性
- `_resample_audio_robust()`: 多重回退重采样
1. 优先使用`librosa.resample`
2. 回退到`torchaudio.transforms.Resample`
3. 最后使用NumPy线性插值
**验证方法**:
```python
import numpy as np
from tts_asr import _resample_audio_robust
# 测试重采样
audio = np.random.randn(16000).astype(np.float32)
resampled = _resample_audio_robust(audio, 16000, 48000)
print(f"Original: {len(audio)}, Resampled: {len(resampled)}")
```
### 5. 改进的错误处理和降级
**降级路径**:
```
MPS推理失败 → 标记MPS不可用 → 清理MPS缓存 → 降级到CPU
CUDA推理失败 → 标记CUDA不可用 → 清理CUDA缓存 → 降级到CPU
```
**日志改进**:
- 详细记录设备检测过程
- 明确标注降级原因
- 显示模型大小、量化状态、离线模式等配置
### 6. 新增API端点
**GET /v1/tts-asr/config**:
```json
{
"environment": {
"TTS_ASR_DEVICE": "auto",
"TTS_ASR_MODEL_SIZE": "auto",
"TTS_ASR_QUANTIZE": false,
"TTS_ASR_OFFLINE_MODE": false,
...
},
"device": {
"current": "mps",
"mps_available": true,
"cuda_available": false,
"is_apple_silicon": true,
"mps_memory_limit_mb": 8192
},
"model": {
"tts": "hexgrad/Kokoro-82M",
"asr_current_size": "small",
"asr_recommended_size": "small",
"available_sizes": ["tiny", "base", "small", "medium", "large", "turbo"]
}
}
```
## 环境变量完整列表
| 变量名 | 说明 | 默认值 | 示例 |
|--------|------|--------|------|
| `TTS_ASR_DEVICE` | 设备选择 | `auto` | `mps`, `cuda`, `cpu` |
| `TTS_ASR_MODEL_SIZE` | ASR模型大小 | `auto` | `tiny`, `base`, `small`, `medium`, `large`, `turbo` |
| `TTS_ASR_QUANTIZE` | INT8量化 | `false` | `true`, `false` |
| `TTS_ASR_OFFLINE_MODE` | 离线模式 | `false` | `true`, `false` |
| `TTS_ASR_WARMUP` | 启动预热 | `true` | `true`, `false` |
| `TTS_ASR_WARMUP_TIMEOUT` | 预热超时(秒) | `120` | `60`, `180` |
| `TTS_ASR_IDLE_TIMEOUT` | 空闲卸载(秒) | `0` | `300`, `600` |
| `TTS_ASR_MPS_MEMORY_LIMIT_MB` | MPS内存限制(MB) | `8192` | `4096`, `16384` |
## macOS使用建议
### 推荐配置
**Apple Silicon (M1/M2/M3) 8GB内存**:
```bash
export TTS_ASR_MODEL_SIZE=small
export TTS_ASR_MPS_MEMORY_LIMIT_MB=4096
```
**Apple Silicon (M1/M2/M3) 16GB+内存**:
```bash
export TTS_ASR_MODEL_SIZE=medium
export TTS_ASR_MPS_MEMORY_LIMIT_MB=8192
```
**内存紧张时**:
```bash
export TTS_ASR_MODEL_SIZE=tiny
export TTS_ASR_QUANTIZE=true
```
### 性能优化建议
1. **首次运行**: 建议不使用离线模式,让模型自动下载
2. **后续运行**: 启用离线模式避免网络延迟
```bash
export TTS_ASR_OFFLINE_MODE=true
```
3. **长期运行服务**: 设置空闲超时自动卸载模型
```bash
export TTS_ASR_IDLE_TIMEOUT=600 # 10分钟后卸载
```
4. **调试模式**: 查看详细设备检测日志
```python
import logging
logging.getLogger("tts_asr").setLevel(logging.DEBUG)
```
## 验证步骤(非Mac环境)
由于你不在Mac环境下,可以使用以下方法验证代码逻辑:
### 1. 代码静态检查
```bash
# 检查Python语法
python -m py_compile backend/tts_asr.py
# 检查导入
python -c "import backend.tts_asr"
```
### 2. 单元测试模拟
```python
# 模拟Apple Silicon环境
import os
import platform
# 模拟Darwin/arm64
original_system = platform.system
original_machine = platform.machine
def mock_system():
return "Darwin"
def mock_machine():
return "arm64"
platform.system = mock_system
platform.machine = mock_machine
# 测试Apple Silicon检测
from tts_asr import _is_apple_silicon
assert _is_apple_silicon() == True
# 恢复原始函数
platform.system = original_system
platform.machine = original_machine
```
### 3. 环境变量测试
```python
import os
os.environ['TTS_ASR_MODEL_SIZE'] = 'small'
os.environ['TTS_ASR_QUANTIZE'] = 'true'
# 重新加载模块
import importlib
import backend.tts_asr
importlib.reload(backend.tts_asr)
from backend.tts_asr import TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE
assert TTS_ASR_MODEL_SIZE == 'small'
assert TTS_ASR_QUANTIZE == True
```
### 4. API端点测试(需要运行服务)
```bash
# 启动服务
python backend/main.py
# 测试配置端点(需要API Key)
curl -X GET "http://localhost:8001/v1/tts-asr/config" \
-H "X-API-Key: your-secret-key-here"
# 测试状态端点
curl -X GET "http://localhost:8001/v1/tts-asr/status" \
-H "X-API-Key: your-secret-key-here"
```
## 依赖更新
已在`backend/requirements.txt`中添加:
- `psutil`: 系统内存检测
- `torchaudio`: 音频重采样备选方案
安装新依赖:
```bash
pip install -r backend/requirements.txt
```
## 向后兼容性
所有改动保持向后兼容:
- 现有API端点未改变
- 默认行为与原版一致
- 新功能通过环境变量启用
## 已知限制
1. **MPS float16**: 在某些操作上可能不稳定,代码默认使用float32
2. **8-bit量化**: 仅在CPU和CUDA环境支持,MPS不支持
3. **Core ML**: 预留了扩展点但未实现(需要额外依赖)
## 未来改进方向
1. 集成Core ML作为备选推理后端
2. 支持torch.compile (PyTorch 2.0+)
3. 实现模型自动下载的进度显示
4. 添加更多音频格式支持
## 问题排查
### 模型加载失败
1. 检查网络连接
2. 尝试关闭离线模式: `export TTS_ASR_OFFLINE_MODE=false`
3. 查看详细日志: 设置`logging.getLogger("tts_asr").setLevel(logging.DEBUG)`
### MPS内存不足
1. 使用更小的模型: `export TTS_ASR_MODEL_SIZE=tiny`
2. 启用量化: `export TTS_ASR_QUANTIZE=true`
3. 降低内存限制: `export TTS_ASR_MPS_MEMORY_LIMIT_MB=4096`
### 音频处理失败
1. 检查音频格式(支持WAV
2. 确保音频采样率≥8000Hz
3. 查看日志中的详细错误信息
---
**修复完成日期**: 2026-04-06
**修改文件**:
- `backend/tts_asr.py` (主要重构)
- `backend/requirements.txt` (添加依赖)
- `README.md` (更新文档)
+82
View File
@@ -0,0 +1,82 @@
# API Benchmarking Report (2026-04-05 23:55:38)
**Base URL:** `https://api.imageteach.tech:8002`
## Executive Summary
| Task | Success Rate | Avg TTFB | Avg Latency | P95 Latency | TPS | RPS |
| :--- | :--- | :--- | :--- | :--- | :--- | :--- |
| Completion-Short | 100.0% | 7519.5ms | 7520.1ms | 14075.8ms | 63.9 | 0.58 |
| Completion-Normal | 70.0% | 9184.3ms | 9184.8ms | 14619.5ms | 100.5 | 0.14 |
| Completion-Long | 100.0% | 22419.4ms | 22419.8ms | 39618.0ms | 852.5 | 0.21 |
| OCR-Concurrent | 0.0% | 0.0ms | 0.0ms | 0.0ms | 0.0 | 5.49 |
| TTS-Concurrent | 0.0% | 0.0ms | 0.0ms | 0.0ms | 0.0 | 11.27 |
| ASR-Concurrent | 0.0% | 0.0ms | 0.0ms | 0.0ms | 0.0 | 7.54 |
| Convert-Concurrent | 100.0% | 377.9ms | 378.6ms | 1017.7ms | 26.4 | 5.28 |
## Stability & Context Analysis
Detailed analysis of how context length affects TTFB and overall performance.
### Completion-Short Details
- **Total Samples:** 10
- **Duration:** 17.36s
### Completion-Normal Details
- **Total Samples:** 10
- **Duration:** 70.66s
- **Top Errors:**
- `[504]` <html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>openresty</center>
</body>
</html>
- `[504]` <html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>openresty</center>
</body>
</html>
- `[504]` <html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>openresty</center>
</body>
</html>
### Completion-Long Details
- **Total Samples:** 10
- **Duration:** 47.09s
### OCR-Concurrent Details
- **Total Samples:** 10
- **Duration:** 1.82s
- **Top Errors:**
- `[500]` {"error":"model runner has unexpectedly stopped, this may be due to resource limitations or an internal error, check ollama server logs for details (status code: 500)"}
- `[500]` {"error":"model runner has unexpectedly stopped, this may be due to resource limitations or an internal error, check ollama server logs for details (status code: 500)"}
+20
View File
@@ -0,0 +1,20 @@
const path = require('path')
const { convert } = require('docx2pdf-converter')
function main() {
const inputPath = process.argv[2]
const outputPath = process.argv[3]
if (!inputPath || !outputPath) {
throw new Error('缺少 DOCX 或 PDF 路径')
}
convert(path.resolve(inputPath), path.resolve(outputPath))
}
try {
main()
} catch (error) {
console.error(error instanceof Error ? error.message : String(error))
process.exit(1)
}
+23 -37
View File
@@ -6,39 +6,22 @@ from datetime import datetime
import ollama import ollama
from dotenv import load_dotenv from dotenv import load_dotenv
from prompts import get_vlm_ocr_prompt
load_dotenv() load_dotenv()
OLLAMA_MODEL = os.getenv('OLLAMA_MODEL', 'gpt-oss:20b') OLLAMA_MODEL = os.getenv('OLLAMA_MODEL', 'gpt-oss:20b')
OLLAMA_HOST = os.getenv('OLLAMA_HOST', 'http://192.168.0.120:11434') OLLAMA_HOST = os.getenv('OLLAMA_HOST', 'http://localhost:11434')
VLM_MODEL = os.getenv('VLM_MODEL', 'qwen3-vl:30b') VLM_MODEL = os.getenv('VLM_MODEL', 'qwen3-vl:30b')
# Timeouts in seconds (10 minutes for large model loading)
COMPLETION_TIMEOUT = 600
OCR_TIMEOUT = 600
CONVERT_TIMEOUT = 600
client = ollama.AsyncClient(host=OLLAMA_HOST) client = ollama.AsyncClient(host=OLLAMA_HOST)
logger = logging.getLogger("llm") logger = logging.getLogger("llm")
VLM_OCR_CONTEXT_PROMPT = """You are an OCR and visual-context extractor for markdown writing assistance.
Your output will be embedded inside an HTML comment as hidden context for a text-completion model.
Requirements:
- Keep output compact: maximum 120 words.
- Use plain text only (no markdown code fences).
- Never output <!-- or -->.
- Do not invent unreadable text; mark uncertain characters with ?.
- Preserve original script for recognized text (do not forcibly translate).
Return exactly this format:
TEXT:
<exact transcription of visible text; use " | " for line breaks; write "(none)" if no readable text>
KEY_DETAILS:
- <3-5 short factual bullets about relevant objects/layout>
LANGUAGE:
<dominant language(s) in visible text, e.g. English / Chinese / Mixed>
SUMMARY:
<one short sentence, <= 20 words>"""
def _extract_message(response) -> tuple[str, str]: def _extract_message(response) -> tuple[str, str]:
content = "" content = ""
@@ -58,10 +41,10 @@ def _extract_message(response) -> tuple[str, str]:
async def call_ollama( async def call_ollama(
prompt: str, prompt: str,
*, *,
system_prompt: str = None, system_prompt: str | None = None,
tag: str = "default", tag: str = "default",
temperature: float = 0.7, temperature: float = 0.7,
thinking: str = None, thinking: str | None = None,
) -> dict: ) -> dict:
""" """
调用 Ollama API 并返回 content 和 thinking。 调用 Ollama API 并返回 content 和 thinking。
@@ -97,7 +80,7 @@ async def call_ollama(
if thinking: if thinking:
kwargs["think"] = thinking kwargs["think"] = thinking
response = await client.chat(**kwargs) response = await asyncio.wait_for(client.chat(**kwargs), timeout=COMPLETION_TIMEOUT)
except asyncio.CancelledError: except asyncio.CancelledError:
elapsed_ms = (time.perf_counter() - start) * 1000 elapsed_ms = (time.perf_counter() - start) * 1000
end_dt = datetime.now() end_dt = datetime.now()
@@ -156,15 +139,18 @@ async def call_vlm_ocr(image_bytes: bytes, language: str = 'auto') -> str:
) )
try: try:
response = await client.chat( response = await asyncio.wait_for(
model=VLM_MODEL, client.chat(
messages=[{ model=VLM_MODEL,
'role': 'user', messages=[{
'content': VLM_OCR_CONTEXT_PROMPT, 'role': 'user',
'images': [image_bytes] 'content': get_vlm_ocr_prompt(),
}], 'images': [image_bytes]
stream=False, }],
options={'temperature': 0.3} stream=False,
options={'temperature': 0.3}
),
timeout=OCR_TIMEOUT
) )
except Exception: except Exception:
elapsed_ms = (time.perf_counter() - start) * 1000 elapsed_ms = (time.perf_counter() - start) * 1000
+92 -34
View File
@@ -1,15 +1,17 @@
import asyncio import asyncio
import base64 import base64
import json
import logging import logging
import os import os
import re
import shutil
import subprocess
import tempfile import tempfile
import uuid import uuid
from typing import Optional from typing import Optional
from fastapi import FastAPI, HTTPException, Request, Security from fastapi import FastAPI, HTTPException, Request, Security, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse from fastapi.responses import JSONResponse, Response
from fastapi.security import APIKeyHeader from fastapi.security import APIKeyHeader
from pydantic import BaseModel from pydantic import BaseModel
@@ -24,8 +26,26 @@ logging.basicConfig(
) )
logger = logging.getLogger("api") logger = logging.getLogger("api")
_markitdown_instance = None
def _get_markitdown():
global _markitdown_instance
if _markitdown_instance is None:
_markitdown_instance = markitdown.MarkItDown()
return _markitdown_instance
app = FastAPI() app = FastAPI()
@app.on_event("startup")
async def startup_event():
logger.info("Starting blocking preload for TTS and ASR models...")
try:
from tts_asr import _warmup_all
await _warmup_all()
except Exception as e:
logger.warning(f"Failed to initiate model warmup: {e}")
ACTIVE_COMPLETIONS: dict[str, asyncio.Task] = {} ACTIVE_COMPLETIONS: dict[str, asyncio.Task] = {}
ACTIVE_COMPLETIONS_LOCK = asyncio.Lock() ACTIVE_COMPLETIONS_LOCK = asyncio.Lock()
@@ -81,6 +101,32 @@ class ConvertRequest(BaseModel):
filename: str = "document.pdf" filename: str = "document.pdf"
ALLOWED_CONVERT_EXTENSIONS = {".txt", ".docx", ".pptx", ".pdf"}
IMAGE_MARKDOWN_RE = re.compile(r"!\[[^\]]*]\([^)]+\)")
IMAGE_HTML_RE = re.compile(r"<img\b[^>]*>", re.IGNORECASE)
def _convert_docx_to_pdf(input_path: str, output_path: str) -> None:
node_executable = shutil.which("node")
if not node_executable:
raise RuntimeError("未找到 Node.js,无法转换 DOCX 为 PDF")
bridge_path = os.path.join(os.path.dirname(__file__), "docx2pdf_bridge.cjs")
if not os.path.exists(bridge_path):
raise RuntimeError("缺少 DOCX 转 PDF 桥接脚本")
result = subprocess.run(
[node_executable, bridge_path, input_path, output_path],
cwd=os.path.dirname(os.path.dirname(__file__)),
capture_output=True,
text=True,
)
if result.returncode != 0:
error_text = (result.stderr or result.stdout or "DOCX 转 PDF 失败").strip()
raise RuntimeError(error_text)
def _preview(text: str, limit: int = 80) -> str: def _preview(text: str, limit: int = 80) -> str:
value = (text or "").replace("\n", "\\n") value = (text or "").replace("\n", "\\n")
if len(value) <= limit: if len(value) <= limit:
@@ -88,8 +134,12 @@ def _preview(text: str, limit: int = 80) -> str:
return value[:limit] + "..." return value[:limit] + "..."
def _sse_payload(payload: dict) -> str: def _sanitize_converted_markdown(text: str) -> str:
return f"data: {json.dumps(payload)}\n\n" value = (text or "").replace("\r\n", "\n").replace("\r", "\n")
value = IMAGE_MARKDOWN_RE.sub("", value)
value = IMAGE_HTML_RE.sub("", value)
value = re.sub(r"\n{3,}", "\n\n", value)
return value.strip()
def get_client_ip(request: Request) -> str: def get_client_ip(request: Request) -> str:
@@ -149,11 +199,10 @@ async def create_completion(request: Request, req: CompletionRequest, api_key: s
) )
) )
async with ACTIVE_COMPLETIONS_LOCK: existing = ACTIVE_COMPLETIONS.get(request_id)
existing = ACTIVE_COMPLETIONS.get(request_id) if existing and not existing.done():
if existing and not existing.done(): existing.cancel()
existing.cancel() ACTIVE_COMPLETIONS[request_id] = inference_task
ACTIVE_COMPLETIONS[request_id] = inference_task
result = await inference_task result = await inference_task
content = result["content"] or "" content = result["content"] or ""
@@ -167,26 +216,17 @@ async def create_completion(request: Request, req: CompletionRequest, api_key: s
_preview(content, 120), _preview(content, 120),
) )
async def generate(): return JSONResponse(content={"content": content, "request_id": request_id})
yield _sse_payload({"content": content})
yield _sse_payload({"done": True})
return StreamingResponse(generate(), media_type="text/event-stream")
except asyncio.CancelledError: except asyncio.CancelledError:
logger.info("[%s] /v1/completions cancelled request_id=%s", request_tag, request_id) logger.info("[%s] /v1/completions cancelled request_id=%s", request_tag, request_id)
return JSONResponse(content={"cancelled": True, "request_id": request_id}, status_code=499)
async def cancelled():
yield _sse_payload({"cancelled": True, "request_id": request_id, "done": True})
return StreamingResponse(cancelled(), media_type="text/event-stream")
except Exception as e: except Exception as e:
logger.exception("[%s] /v1/completions failed request_id=%s: %s", request_tag, request_id, e) logger.exception("[%s] /v1/completions failed request_id=%s: %s", request_tag, request_id, e)
return JSONResponse(content={"error": str(e)}, status_code=500) return JSONResponse(content={"error": str(e)}, status_code=500)
finally: finally:
async with ACTIVE_COMPLETIONS_LOCK: active = ACTIVE_COMPLETIONS.get(request_id)
active = ACTIVE_COMPLETIONS.get(request_id) if active is not None and active is inference_task:
if active is not None and active is inference_task: ACTIVE_COMPLETIONS.pop(request_id, None)
ACTIVE_COMPLETIONS.pop(request_id, None)
@app.post("/v1/completions/cancel") @app.post("/v1/completions/cancel")
@@ -253,7 +293,7 @@ async def ocr_image(request: OCRRequest, api_key: str = Security(get_api_key)):
@app.post("/v1/convert") @app.post("/v1/convert")
async def convert_to_markdown(request: ConvertRequest, api_key: str = Security(get_api_key)): async def convert_to_markdown(request: ConvertRequest, api_key: str = Security(get_api_key)):
"""将文件转换为Markdown格式""" """Convert file to markdown"""
request_id = str(uuid.uuid4())[:8] request_id = str(uuid.uuid4())[:8]
try: try:
@@ -264,23 +304,33 @@ async def convert_to_markdown(request: ConvertRequest, api_key: str = Security(g
len(request.file or ""), len(request.file or ""),
) )
# 解码Base64文件内容 # Decode base64
file_bytes = base64.b64decode(request.file) file_bytes = base64.b64decode(request.file)
logger.info("[%s] /v1/convert decoded file_bytes=%d", request_id, len(file_bytes)) logger.info("[%s] /v1/convert decoded file_bytes=%d", request_id, len(file_bytes))
# 获取文件扩展名 # Get file extension
ext = os.path.splitext(request.filename)[1].lower() ext = os.path.splitext(request.filename)[1].lower()
# 创建临时文件 if ext not in ALLOWED_CONVERT_EXTENSIONS:
raise ValueError("仅支持 txt、docx、pptx、pdf 格式")
if ext == ".txt":
markdown_text = _sanitize_converted_markdown(file_bytes.decode("utf-8", errors="ignore"))
return {
"markdown": markdown_text,
"filename": request.filename
}
# Create temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp: with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
tmp.write(file_bytes) tmp.write(file_bytes)
tmp_path = tmp.name tmp_path = tmp.name
try: try:
# 使用MarkItDown转换为Markdown # Convert using MarkItDown
md = markitdown.MarkItDown() md = _get_markitdown()
result = md.convert(tmp_path) result = await asyncio.to_thread(md.convert, tmp_path)
markdown_text = result.text_content markdown_text = _sanitize_converted_markdown(result.text_content)
logger.info( logger.info(
"[%s] /v1/convert success text_chars=%d text_preview='%s'", "[%s] /v1/convert success text_chars=%d text_preview='%s'",
@@ -294,7 +344,7 @@ async def convert_to_markdown(request: ConvertRequest, api_key: str = Security(g
"filename": request.filename "filename": request.filename
} }
finally: finally:
# 清理临时文件 # Clean up temporary file
if os.path.exists(tmp_path): if os.path.exists(tmp_path):
os.unlink(tmp_path) os.unlink(tmp_path)
@@ -302,8 +352,16 @@ async def convert_to_markdown(request: ConvertRequest, api_key: str = Security(g
logger.exception("[%s] /v1/convert failed: %s", request_id, e) logger.exception("[%s] /v1/convert failed: %s", request_id, e)
return JSONResponse(content={"error": str(e)}, status_code=500) return JSONResponse(content={"error": str(e)}, status_code=500)
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001) uvicorn.run(app, host="0.0.0.0", port=8001)
# TTS and ASR routes (lazy loaded to avoid heavy import on startup)
def _register_tts_asr_routes():
from tts_asr import register_tts_asr_routes
register_tts_asr_routes(app)
_register_tts_asr_routes()
+60 -305
View File
@@ -1,6 +1,15 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
import re import re
from typing import Tuple from typing import Protocol, Tuple, runtime_checkable
from prompts import get_language_guidance_map, get_system_prompt_template, get_inline_examples
@runtime_checkable
class UserPreferences(Protocol):
language: str
currency: str
timezone: str
def _get_current_datetime(timezone_pref: str = "auto") -> str: def _get_current_datetime(timezone_pref: str = "auto") -> str:
@@ -214,304 +223,36 @@ def _canonical_language_id(language_id: str) -> str:
return LANGUAGE_SYNONYMS.get(safe, safe) return LANGUAGE_SYNONYMS.get(safe, safe)
_JS_LANGS = {"javascript", "typescript"}
_CODE_LANGS = {"python", "go", "rust", "java", "kotlin", "swift", "ruby", "php", "lua", "c", "cpp", "csharp", "r", "matlab", "dart"}
def _language_guidance(language_id: str) -> str: def _language_guidance(language_id: str) -> str:
canonical = _canonical_language_id(language_id) canonical = _canonical_language_id(language_id)
if canonical == "markdown": if canonical == "markdown":
return "" return ""
if canonical == "mermaid": guidance_map = get_language_guidance_map()
return """ guidance = guidance_map.get(canonical)
Language-specific guidance (mermaid): if guidance:
- Output valid Mermaid syntax only. return guidance
- Prefer concise, syntactically correct diagram statements. if canonical in _JS_LANGS:
- Avoid prose unless the user prompt explicitly requires it.""" return guidance_map.get("_js_code", "").replace("{lang}", canonical)
if canonical == "latex": if canonical in _CODE_LANGS:
return """ return guidance_map.get("_generic_code", "").replace("{lang}", canonical)
Language-specific guidance (latex): return guidance_map.get("_generic_code", "").replace("{lang}", canonical)
- Output LaTeX math content only when completing LaTeX.
- If CURSOR_IN_FENCED_CODE_BLOCK=true and CURSOR_FENCE_LANGUAGE is latex/tex/katex:
- Output raw LaTeX lines only.
- Do not wrap with $ or $$."""
if canonical == "json":
return """
Language-specific guidance (json):
- Output strict JSON only (no comments, no trailing commas).
- Ensure valid quotes and braces."""
if canonical == "yaml":
return """
Language-specific guidance (yaml):
- Output valid YAML only.
- Use consistent indentation and avoid tabs."""
if canonical == "toml":
return """
Language-specific guidance (toml):
- Output valid TOML only.
- Keep key types consistent."""
if canonical == "ini":
return """
Language-specific guidance (ini):
- Output valid INI only.
- Keep section headers and key=value pairs consistent."""
if canonical == "sql":
return """
Language-specific guidance (sql):
- Output a single, valid SQL statement unless context requires multiple.
- Prefer ANSI SQL when dialect is unclear."""
if canonical == "bash":
return """
Language-specific guidance (bash):
- Output POSIX-compatible shell when possible.
- Avoid interactive prompts or destructive commands unless requested."""
if canonical == "powershell":
return """
Language-specific guidance (powershell):
- Output valid PowerShell commands.
- Avoid destructive commands unless explicitly requested."""
if canonical == "html":
return """
Language-specific guidance (html):
- Output valid HTML only.
- Keep markup minimal and well-formed."""
if canonical == "css":
return """
Language-specific guidance (css):
- Output valid CSS only.
- Use concise, readable selectors."""
if canonical == "diff":
return """
Language-specific guidance (diff):
- Output a unified diff only.
- Ensure @@ hunk headers and +/- lines are consistent."""
if canonical == "regex":
return """
Language-specific guidance (regex):
- Output the regex pattern only.
- Avoid delimiters unless explicitly requested."""
if canonical in {"javascript", "typescript"}:
return f"""
Language-specific guidance ({canonical}):
- Output valid {canonical} code.
- Prefer modern syntax and avoid prose unless comments are needed."""
if canonical in {"python", "go", "rust", "java", "kotlin", "swift", "ruby", "php", "lua", "c", "cpp", "csharp", "r", "matlab", "dart"}:
return f"""
Language-specific guidance ({canonical}):
- Output valid {canonical} code.
- Avoid prose unless context clearly expects comments or docstrings."""
if canonical == "text":
return """
Language-specific guidance (text):
- Output plain text only.
- Avoid markdown formatting unless explicitly asked."""
if canonical == "xml":
return """
Language-specific guidance (xml):
- Output well-formed XML only.
- Ensure matching tags and proper escaping."""
if canonical == "dockerfile":
return """
Language-specific guidance (dockerfile):
- Output valid Dockerfile instructions only.
- Keep layers minimal and ordered logically."""
if canonical == "makefile":
return """
Language-specific guidance (makefile):
- Output valid Makefile syntax only.
- Use tabs for recipe lines."""
return f"""
Language-specific guidance ({canonical}):
- Output valid {canonical} code.
- Avoid prose unless context clearly expects comments or docstrings."""
def build_inline_system_prompt(language_id: str = "markdown") -> str: def build_inline_system_prompt(language_id: str = "markdown") -> str:
safe_language_id = _canonical_language_id(language_id) safe_language_id = _canonical_language_id(language_id)
language_guidance = _language_guidance(safe_language_id) language_guidance = _language_guidance(safe_language_id)
template = get_system_prompt_template()
system_prompt = f"""You are an inline completion engine for a {safe_language_id} editor with ghost-text suggestions. system_prompt = template.replace("{language_id}", safe_language_id)
Return only the insertion text that should be placed between PREFIX and SUFFIX.
Hard constraints you must follow:
1) Output-only contract:
- Output insertion text only.
- No explanations, no meta labels, no wrapper quotes around the whole answer.
2) Strict math formatting (KaTeX):
- If you output any math expression, it must be strict KaTeX-compatible math.
- Every formula must be wrapped with either $...$ (inline) or $$...$$ (block).
- Never output bare formulas without $ or $$ wrappers.
- Exception: If CURSOR_IN_FENCED_CODE_BLOCK=true and CURSOR_FENCE_LANGUAGE is latex/tex/katex,
output raw LaTeX without $ or $$ wrappers.
3) Strict code formatting:
- Read CURSOR_IN_FENCED_CODE_BLOCK from the user prompt.
- If CURSOR_IN_FENCED_CODE_BLOCK=true:
- You are already inside a fenced code block.
- Never output triple backticks.
- Output code lines only.
- If CURSOR_IN_FENCED_CODE_BLOCK=false:
- Any code output must be in a fenced code block with a language tag:
```{{language}}
...
```
- Do not output code snippets as inline backticks.
- Choose the language tag from context (no default fallback tag instruction).
4) Mermaid-specific completion rules:
- Read CURSOR_FENCE_LANGUAGE and MERMAID_CONTEXT from the user prompt.
- If CURSOR_FENCE_LANGUAGE=mermaid:
- Output Mermaid statements only.
- Never output triple backticks.
- Never output prose explanations.
- If CURSOR_IN_FENCED_CODE_BLOCK=false and MERMAID_CONTEXT=true:
- Output a complete Mermaid fenced block:
```mermaid
...
```
- Keep Mermaid syntax valid and concise.
- Never mix Mermaid code and explanatory narration in one output.
5) Boundary newline repair:
- Read PREFIX_ENDS_WITH_NEWLINE and SUFFIX_STARTS_WITH_NEWLINE from the user prompt.
- Carefully reason about whether OUTPUT should start or end with a newline.
- If PREFIX lacks a required boundary newline, add it at OUTPUT start.
- If SUFFIX lacks a required boundary newline, add it at OUTPUT end.
- Ensure PREFIX + OUTPUT + SUFFIX is structurally natural.
6) Context stitching:
- Do not repeat text that already appears at the start of SUFFIX.
- Preserve nearby language, tone, punctuation, indentation, and markdown structure.
- Continue existing structures naturally (lists, tables, block quotes, headings).
7) OCR safety:
- PREFIX may include hidden OCR metadata tags like <OCR:...>.
- Never output any OCR tag.
- Never output OCR tag fragments such as <OCR:...>."""
if language_guidance: if language_guidance:
system_prompt = f"{system_prompt.rstrip()}\n{language_guidance.strip()}" system_prompt = f"{system_prompt.rstrip()}\n{language_guidance.strip()}"
return system_prompt.strip() return system_prompt.strip()
INLINE_EXAMPLES = """[EX01] Prose continuation _INLINE_EXAMPLES = get_inline_examples()
<PREFIX>The quick brown fox </PREFIX>
<SUFFIX>jumps over the lazy dog.</SUFFIX>
Expected OUTPUT:
moved quietly and then
[EX02] Avoid repeating suffix beginning
<PREFIX>Our launch plan starts with </PREFIX>
<SUFFIX>phase one, followed by phase two.</SUFFIX>
Expected OUTPUT:
careful internal testing before
[EX03] Continue markdown checklist
<PREFIX>## TODO
- [ ] Buy milk
- [ ] </PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
Write release notes and share draft with team
[EX04] Cursor outside code block, code must use fenced block
CURSOR_IN_FENCED_CODE_BLOCK=false
<PREFIX>Parse this JSON payload in Python:</PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
```python
import json
data = json.loads(payload)
```
[EX05] Cursor inside fenced code block, do not output fences
CURSOR_IN_FENCED_CODE_BLOCK=true
<PREFIX>```python
def add(a, b):
return </PREFIX>
<SUFFIX>
```</SUFFIX>
Expected OUTPUT:
a + b
[EX06] Inline math must use $...$
<PREFIX>The derivative of x^2 is </PREFIX>
<SUFFIX>.</SUFFIX>
Expected OUTPUT:
$2x$
[EX07] Block math must use $$...$$
<PREFIX>We can write the Gaussian integral as:</PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
$$
\\int_{-\\infty}^{\\infty} e^{-x^2}\\,dx = \\sqrt{\\pi}
$$
[EX08] Prefix misses boundary newline; add newline at output start
PREFIX_ENDS_WITH_NEWLINE=false
<PREFIX>Deployment steps:</PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
- Build artifact
- Deploy service
[EX09] Suffix misses boundary newline; add newline at output end
SUFFIX_STARTS_WITH_NEWLINE=false
<PREFIX>Summary paragraph complete.</PREFIX>
<SUFFIX>## Next Section</SUFFIX>
Expected OUTPUT:
[EX10] OCR metadata exists but must never be emitted
<PREFIX>![whiteboard](img.png) <OCR:equation y = mx + b>
The relationship is </PREFIX>
<SUFFIX>.</SUFFIX>
Expected OUTPUT:
$y = mx + b$
[EX11] Continue markdown table with correct row shape
<PREFIX>| Name | Score |
| --- | --- |
| Alice | 92 |
| Bob | </PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
88 |
[EX12] Mixed text + math + code in one insertion
CURSOR_IN_FENCED_CODE_BLOCK=false
<PREFIX>Use the area formula and provide a tiny JS helper.</PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
The area is $A = \\pi r^2$.
```javascript
const area = (r) => Math.PI * r * r;
```
[EX13] Cursor inside mermaid fence: no backticks, mermaid lines only
CURSOR_IN_FENCED_CODE_BLOCK=true
CURSOR_FENCE_LANGUAGE=mermaid
<PREFIX>```mermaid
flowchart TD
A[Start] --> </PREFIX>
<SUFFIX>
```</SUFFIX>
Expected OUTPUT:
B{Valid?}
B -->|Yes| C[Done]
[EX14] Mermaid context outside fence: return full mermaid block
CURSOR_IN_FENCED_CODE_BLOCK=false
MERMAID_CONTEXT=true
<PREFIX>Please provide a simple release pipeline diagram.</PREFIX>
<SUFFIX></SUFFIX>
Expected OUTPUT:
```mermaid
flowchart LR
Build --> Test --> Deploy
```"""
def build_completion_prompts( def build_completion_prompts(
@@ -520,7 +261,7 @@ def build_completion_prompts(
language_id: str = "markdown", language_id: str = "markdown",
location: str = "", location: str = "",
thinking_level: str = "low", thinking_level: str = "low",
preferences: object = None, preferences: UserPreferences | None = None,
) -> Tuple[str, str]: ) -> Tuple[str, str]:
safe_language_id = _canonical_language_id(language_id) safe_language_id = _canonical_language_id(language_id)
recent_prefix, recent_suffix = _prepare_context(prefix, suffix) recent_prefix, recent_suffix = _prepare_context(prefix, suffix)
@@ -551,35 +292,49 @@ def build_completion_prompts(
preferences_instruction = f"\nUser Preferences:\n{preferences_instruction}" preferences_instruction = f"\nUser Preferences:\n{preferences_instruction}"
user_prompt = f"""Current time: {current_time}{location_info}{preferences_instruction} user_prompt = f"""Current time: {current_time}{location_info}{preferences_instruction}
Reasoning hint: {thinking_level} Reasoning level: {thinking_level}
Editor language id: {safe_language_id} Editor language: {safe_language_id}
Completion state flags: === STATE FLAGS ===
- CURSOR_IN_FENCED_CODE_BLOCK: {"true" if cursor_in_fenced_code_block else "false"} - CURSOR_IN_FENCED_CODE_BLOCK: {"true" if cursor_in_fenced_code_block else "false"}
- CURSOR_FENCE_LANGUAGE: {cursor_fence_language} - CURSOR_FENCE_LANGUAGE: {cursor_fence_language}
- MERMAID_CONTEXT: {"true" if mermaid_context else "false"} - MERMAID_CONTEXT: {"true" if mermaid_context else "false"}
- PREFIX_ENDS_WITH_NEWLINE: {"true" if prefix_ends_with_newline else "false"} - PREFIX_ENDS_WITH_NEWLINE: {"true" if prefix_ends_with_newline else "false"}
- SUFFIX_STARTS_WITH_NEWLINE: {"true" if suffix_starts_with_newline else "false"} - SUFFIX_STARTS_WITH_NEWLINE: {"true" if suffix_starts_with_newline else "false"}
Task: === TASK ===
- Produce the best insertion text at the cursor between PREFIX and SUFFIX. Produce the best insertion text between PREFIX and SUFFIX.
- Keep insertion meaningful and non-empty. Requirements:
- Keep insertion concise unless structure requires more content. - Non-empty and meaningful
- Concise unless structure needs more
- Follows markdown rules in system prompt
Context notes: === BOUNDARY DECISION GUIDE ===
- PREFIX may include OCR metadata after image markdown, e.g. ![alt](url) <OCR:description>.
- OCR metadata is hidden context and must never be copied into output.
- Preserve local style and formatting.
Decision policy: Step 1: Check PREFIX_ENDS_WITH_NEWLINE
- Prioritize seamless join: PREFIX + OUTPUT + SUFFIX must read naturally. If false, ask: "Does output need to start on a new line?"
- Do not repeat SUFFIX-leading text. - YES if PREFIX ends with: ":", "steps:", "items:", heading text, or complete sentence before heading
- If uncertain, prefer a complete short phrase/sentence with clear meaning. - If YES: start output with \\n
Comprehensive examples: Step 2: Check SUFFIX_STARTS_WITH_NEWLINE
{INLINE_EXAMPLES} If false, ask: "Does output need to end with a newline?"
- YES if SUFFIX starts with: heading (##), new paragraph, or list marker
- If YES: end output with \\n
Now produce the insertion. Step 3: Choose newline type
- Use \\n\\n for: new paragraphs, before headings, starting lists
- Use \\n for: continuing within blocks, list items, table cells
- Exception: inside code fences, use \\n freely
=== CONTEXT NOTES ===
- OCR metadata (e.g., <OCR:description>) is hidden context, never copy to output
- Match PREFIX tone, style, and indentation
- Do not repeat text from SUFFIX beginning
=== EXAMPLES BY CATEGORY ===
{_INLINE_EXAMPLES}
=== NOW COMPLETE THE TASK ===
<PREFIX> <PREFIX>
{recent_prefix} {recent_prefix}
@@ -601,7 +356,7 @@ def build_prompt(
language_id: str = "markdown", language_id: str = "markdown",
location: str = "", location: str = "",
thinking_level: str = "low", thinking_level: str = "low",
preferences: object = None, preferences: UserPreferences | None = None,
) -> str: ) -> str:
""" """
Backward-compatible helper. Returns only the user prompt body. Backward-compatible helper. Returns only the user prompt body.
+44
View File
@@ -0,0 +1,44 @@
import json
from pathlib import Path
from typing import Any
_PROMPTS_DIR = Path(__file__).parent
class PromptManager:
_instance = None
_data: dict[str, Any] = {}
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._load_all()
return cls._instance
def _load_all(self):
for json_file in _PROMPTS_DIR.glob("*.json"):
key = json_file.stem
with open(json_file, "r", encoding="utf-8") as f:
self._data[key] = json.load(f)
def get(self, key: str, default: Any = None) -> Any:
return self._data.get(key, default)
_prompts = PromptManager()
def get_system_prompt_template() -> str:
return _prompts.get("system_prompt", {}).get("template", "")
def get_language_guidance_map() -> dict[str, str]:
return _prompts.get("language_guidance", {})
def get_inline_examples() -> str:
return _prompts.get("inline_examples", {}).get("content", "")
def get_vlm_ocr_prompt() -> str:
return _prompts.get("vlm_ocr", {}).get("prompt", "")
+3
View File
@@ -0,0 +1,3 @@
{
"content": "=== CATEGORY A: PROSE CONTINUATION ===\n\n[EX01] Simple prose continuation\n<PREFIX>The quick brown fox </PREFIX>\n<SUFFIX>jumps over the lazy dog.</SUFFIX>\nExpected OUTPUT:\nmoved quietly and then\n\n[EX02] Avoid repeating suffix\n<PREFIX>Our launch plan starts with </PREFIX>\n<SUFFIX>phase one, followed by phase two.</SUFFIX>\nExpected OUTPUT:\ncareful internal testing before\nWRONG: phase one starts with (repeats suffix)\n\n=== CATEGORY B: MARKDOWN STRUCTURES ===\n\n[EX03] Continue checklist\n<PREFIX>## TODO\n- [ ] Buy milk\n- [ ] </PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\nWrite release notes and share draft with team\n\n[EX04] Start list after header (PREFIX lacks newline)\nPREFIX_ENDS_WITH_NEWLINE=false\n<PREFIX>Deployment steps:</PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n\n- Build artifact\n- Deploy service\n\n[EX05] Continue table row\n<PREFIX>| Name | Score |\n| --- | --- |\n| Alice | 92 |\n| Bob | </PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n88 |\n\n[EX06] Start new paragraph\n<PREFIX>First paragraph ends.</PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n\nSecond paragraph starts.\nWRONG: Second paragraph starts. (missing leading \\n\\n)\n\n[EX07] Add newline before heading\nPREFIX_ENDS_WITH_NEWLINE=false\n<PREFIX>End of previous section.</PREFIX>\n<SUFFIX>## Next Heading</SUFFIX>\nExpected OUTPUT:\n\nWRONG: (would join with heading without separation)\n\n=== CATEGORY C: CODE BLOCKS ===\n\n[EX08] Outside fence: wrap code in fence\nCURSOR_IN_FENCED_CODE_BLOCK=false\n<PREFIX>Parse this JSON payload in Python:</PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n```python\nimport json\ndata = json.loads(payload)\n```\nWRONG: import json\\ndata = json.loads(payload) (no fence)\n\n[EX09] Inside fence: output code only\nCURSOR_IN_FENCED_CODE_BLOCK=true\n<PREFIX>```python\ndef add(a, b):\nreturn </PREFIX>\n<SUFFIX>\n```</SUFFIX>\nExpected OUTPUT:\na + b\nWRONG: ```python\\nreturn a + b\\n``` (duplicate fences)\n\n[EX10] Code inside fence uses single newline\nCURSOR_IN_FENCED_CODE_BLOCK=true\n<PREFIX>```python\ndef hello():</PREFIX>\n<SUFFIX>\n```</SUFFIX>\nExpected OUTPUT:\nprint(\"Hello\")\nreturn True\n(Note: single \\n between code lines, no markdown rules)\n\n=== CATEGORY D: MATH ===\n\n[EX11] Inline math\n<PREFIX>The derivative of x^2 is </PREFIX>\n<SUFFIX>.</SUFFIX>\nExpected OUTPUT:\n$2x$\nWRONG: 2x (bare formula)\n\n[EX12] Block math\n<PREFIX>We can write the Gaussian integral as:</PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n$$\n\\int_{-\\infty}^{\\infty} e^{-x^2}\\,dx = \\sqrt{\\pi}\n$$\nWRONG: \\int... (bare formula without $$)\n\n=== CATEGORY E: MERMAID ===\n\n[EX13] Inside mermaid fence\nCURSOR_FENCE_LANGUAGE=mermaid\nCURSOR_IN_FENCED_CODE_BLOCK=true\n<PREFIX>```mermaid\nflowchart TD\nA[Start] --> </PREFIX>\n<SUFFIX>\n```</SUFFIX>\nExpected OUTPUT:\nB{Valid?}\nB -->|Yes| C[Done]\nWRONG: ```mermaid\\nB{Valid?}... (duplicate fence)\n\n[EX14] Outside fence with mermaid context\nCURSOR_IN_FENCED_CODE_BLOCK=false\nMERMAID_CONTEXT=true\n<PREFIX>Please provide a simple release pipeline diagram.</PREFIX>\n<SUFFIX></SUFFIX>\nExpected OUTPUT:\n```mermaid\nflowchart LR\nBuild --> Test --> Deploy\n```\n\n=== CATEGORY F: OCR METADATA ===\n\n[EX15] Use OCR as context, never output\n<PREFIX>![whiteboard](img.png) <OCR:equation y = mx + b>\nThe relationship is </PREFIX>\n<SUFFIX>.</SUFFIX>\nExpected OUTPUT:\n$y = mx + b$\nWRONG: <OCR:equation y = mx + b> (OCR tag in output)"
}
+21
View File
@@ -0,0 +1,21 @@
{
"mermaid": "\nLanguage-specific guidance (mermaid):\n- Output valid Mermaid syntax only.\n- Prefer concise, syntactically correct diagram statements.\n- Avoid prose unless the user prompt explicitly requires it.",
"latex": "\nLanguage-specific guidance (latex):\n- Output LaTeX math content only when completing LaTeX.\n- If CURSOR_IN_FENCED_CODE_BLOCK=true and CURSOR_FENCE_LANGUAGE is latex/tex/katex:\n- Output raw LaTeX lines only.\n- Do not wrap with $ or $$.",
"json": "\nLanguage-specific guidance (json):\n- Output strict JSON only (no comments, no trailing commas).\n- Ensure valid quotes and braces.",
"yaml": "\nLanguage-specific guidance (yaml):\n- Output valid YAML only.\n- Use consistent indentation and avoid tabs.",
"toml": "\nLanguage-specific guidance (toml):\n- Output valid TOML only.\n- Keep key types consistent.",
"ini": "\nLanguage-specific guidance (ini):\n- Output valid INI only.\n- Keep section headers and key=value pairs consistent.",
"sql": "\nLanguage-specific guidance (sql):\n- Output a single, valid SQL statement unless context requires multiple.\n- Prefer ANSI SQL when dialect is unclear.",
"bash": "\nLanguage-specific guidance (bash):\n- Output POSIX-compatible shell when possible.\n- Avoid interactive prompts or destructive commands unless requested.",
"powershell": "\nLanguage-specific guidance (powershell):\n- Output valid PowerShell commands.\n- Avoid destructive commands unless explicitly requested.",
"html": "\nLanguage-specific guidance (html):\n- Output valid HTML only.\n- Keep markup minimal and well-formed.",
"css": "\nLanguage-specific guidance (css):\n- Output valid CSS only.\n- Use concise, readable selectors.",
"diff": "\nLanguage-specific guidance (diff):\n- Output a unified diff only.\n- Ensure @@ hunk headers and +/- lines are consistent.",
"regex": "\nLanguage-specific guidance (regex):\n- Output the regex pattern only.\n- Avoid delimiters unless explicitly requested.",
"text": "\nLanguage-specific guidance (text):\n- Output plain text only.\n- Avoid markdown formatting unless explicitly asked.",
"xml": "\nLanguage-specific guidance (xml):\n- Output well-formed XML only.\n- Ensure matching tags and proper escaping.",
"dockerfile": "\nLanguage-specific guidance (dockerfile):\n- Output valid Dockerfile instructions only.\n- Keep layers minimal and ordered logically.",
"makefile": "\nLanguage-specific guidance (makefile):\n- Output valid Makefile syntax only.\n- Use tabs for recipe lines.",
"_generic_code": "\nLanguage-specific guidance ({lang}):\n- Output valid {lang} code.\n- Avoid prose unless context clearly expects comments or docstrings.",
"_js_code": "\nLanguage-specific guidance ({lang}):\n- Output valid {lang} code.\n- Prefer modern syntax and avoid prose unless comments are needed."
}
+3
View File
@@ -0,0 +1,3 @@
{
"template": "You are an inline completion engine for a {language_id} editor with ghost-text suggestions.\n\nReturn only the insertion text that should be placed between PREFIX and SUFFIX.\n\nCORE PRINCIPLE: Output insertion text only. No explanations, no meta labels, no wrapper quotes.\n\nPRIORITY 1: CONTEXT AWARENESS (Read these flags from user prompt)\n- CURSOR_IN_FENCED_CODE_BLOCK: Are you inside a code fence?\n- CURSOR_FENCE_LANGUAGE: What language is the current fence?\n- PREFIX_ENDS_WITH_NEWLINE: Does prefix end with newline?\n- SUFFIX_STARTS_WITH_NEWLINE: Does suffix start with newline?\n- MERMAID_CONTEXT: Is this a Mermaid diagram context?\n\nPRIORITY 2: SPECIALIZED CONTENT RULES\n\n2.1 Code Block Handling:\nIf CURSOR_IN_FENCED_CODE_BLOCK=true:\n- You are inside a code fence\n- Output code lines ONLY (no triple backticks)\n- Use single \\n for code line separation\n\nIf CURSOR_IN_FENCED_CODE_BLOCK=false and code needed:\n- Wrap code in fenced block with language tag:\n```{language}\ncode here\n```\n- Never use inline backticks for code snippets\n\n2.2 Math Formatting (KaTeX):\n- Inline math: wrap with $...$\n- Block math: wrap with $$...$$\n- Never output bare formulas\n- Exception: inside latex/tex/katex fence, output raw LaTeX\n\n2.3 Mermaid Diagrams:\nIf CURSOR_FENCE_LANGUAGE=mermaid:\n- Output Mermaid syntax ONLY\n- No backticks, no explanations\n\nIf MERMAID_CONTEXT=true and outside fence:\n- Output complete fenced block:\n```mermaid\ndiagram syntax\n```\n\nPRIORITY 3: MARKDOWN STRUCTURE\n\n3.1 Newline Semantics:\n- Single \\n: soft break (same paragraph, renders as space or <br>)\n- Double \\n\\n: hard break (new paragraph/block)\n- Use \\n\\n for: new paragraphs, before headings, starting lists/tables\n- Use \\n for: continuation within blocks (list items, table cells)\n- Exception: inside code blocks, use \\n freely for code lines\n\n3.2 Boundary Management:\nCheck PREFIX_ENDS_WITH_NEWLINE and SUFFIX_STARTS_WITH_NEWLINE:\n- If PREFIX lacks needed newline: start OUTPUT with \\n\n- If SUFFIX lacks needed newline: end OUTPUT with \\n\n- Common cases requiring leading \\n:\n* Starting a list after \"Steps:\"\n* Creating new paragraph after text\n* Adding heading after paragraph\n- Common cases requiring trailing \\n:\n* Before new heading\n* End of section\n\n3.3 Context Stitching:\n- Never repeat text from SUFFIX beginning\n- Match PREFIX tone, style, indentation\n- Continue structures: lists, tables, quotes, headings\n\nPRIORITY 4: HIDDEN CONTEXT\n- OCR metadata like <OCR:...> is hidden context\n- Never copy OCR tags to output\n- Use OCR content as semantic hint only"
}
+3
View File
@@ -0,0 +1,3 @@
{
"prompt": "You are an OCR and visual-context extractor for markdown writing assistance.\n\nYour output will be embedded inside an HTML comment as hidden context for a text-completion model.\n\nRequirements:\n- Keep output compact: maximum 120 words.\n- Use plain text only (no markdown code fences).\n- Never output <!-- or -->.\n- Do not invent unreadable text; mark uncertain characters with ?.\n- Preserve original script for recognized text (do not forcibly translate).\n\nReturn exactly this format:\n\nTEXT:\n<exact transcription of visible text; use \" | \" for line breaks; write \"(none)\" if no readable text>\n\nKEY_DETAILS:\n- <3-5 short factual bullets about relevant objects/layout>\n\nLANGUAGE:\n<dominant language(s) in visible text, e.g. English / Chinese / Mixed>\n\nSUMMARY:\n<one short sentence, <= 20 words>"
}
+10
View File
@@ -10,3 +10,13 @@ python-docx
python-pptx python-pptx
openpyxl openpyxl
pypdf pypdf
# TTS and ASR dependencies
torch
transformers
soundfile
numpy
accelerate
librosa
psutil
torchaudio
+295
View File
@@ -0,0 +1,295 @@
import asyncio
import base64
import json
import logging
import time
import argparse
import uuid
import sys
import statistics
import os
from datetime import datetime
from typing import List, Dict, Any, Optional, Tuple
import httpx
from pydantic import BaseModel
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stdout)]
)
logger = logging.getLogger("api_benchmarker")
# Constants
DEFAULT_BASE_URL = "http://localhost:8001"
DEFAULT_API_KEY = "your-secret-key-here"
CHARS_PER_TOKEN = 4
# Data Generators
def get_dummy_base64_image():
return "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
def get_dummy_base64_audio():
# A bit longer dummy audio to pass validation (44 bytes header + some data)
return "UklGRigAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQAAAAAAA" + "A" * 100 + "=="
def generate_context_text(tokens: int) -> str:
"""Generate synthetic text of approximately 'tokens' tokens."""
base_phrase = "The quick brown fox jumps over the lazy dog. "
repeat_count = (tokens * CHARS_PER_TOKEN) // len(base_phrase) + 1
return (base_phrase * repeat_count)[:tokens * CHARS_PER_TOKEN]
# Metric Models
class RequestMetric(BaseModel):
task_name: str
endpoint: str
status_code: int
ttfb_ms: float
total_ms: float
success: bool
tokens: int
error: Optional[str] = None
class BenchStats:
def __init__(self, name: str):
self.name = name
self.metrics: List[RequestMetric] = []
self.start_time = 0.0
self.end_time = 0.0
def add(self, m: RequestMetric):
self.metrics.append(m)
def get_summary(self) -> Dict[str, Any]:
if not self.metrics:
return {}
total = len(self.metrics)
successes = [m for m in self.metrics if m.success]
success_count = len(successes)
fail_count = total - success_count
total_latencies = [m.total_ms for m in successes] if successes else [0]
ttfb_latencies = [m.ttfb_ms for m in successes] if successes else [0]
duration = self.end_time - self.start_time
total_tokens = sum(m.tokens for m in successes)
return {
"name": self.name,
"total_requests": total,
"success_rate": (success_count / total) * 100 if total > 0 else 0,
"avg_latency": statistics.mean(total_latencies),
"p50_latency": statistics.median(total_latencies),
"p95_latency": sorted(total_latencies)[int(len(total_latencies)*0.95)] if total_latencies else 0,
"avg_ttfb": statistics.mean(ttfb_latencies),
"tps": total_tokens / duration if duration > 0 else 0,
"rps": total / duration if duration > 0 else 0,
"duration": duration
}
# Benchmarking Engine
class ApiBenchmarker:
def __init__(self, base_url: str, api_key: str):
self.base_url = base_url
self.api_key = api_key
self.headers = {"X-API-Key": api_key}
self.semaphores = {
"completions": asyncio.Semaphore(5),
"ocr": asyncio.Semaphore(2),
"convert": asyncio.Semaphore(2),
"tts-asr": asyncio.Semaphore(3)
}
self.results: Dict[str, BenchStats] = {}
async def _execute_request(self, client: httpx.AsyncClient, name: str, method: str, path: str, **kwargs) -> RequestMetric:
url = f"{self.base_url}{path}"
start = time.perf_counter()
ttfb = 0.0
tokens_count = 0
# Estimate input + output tokens (mock for output)
if "json" in kwargs:
input_text = str(kwargs["json"].get("prefix", "")) + str(kwargs["json"].get("text", ""))
tokens_count += len(input_text) // CHARS_PER_TOKEN
try:
async with client.stream(method, url, **kwargs) as response:
ttfb = (time.perf_counter() - start) * 1000
body = await response.aread()
total_ms = (time.perf_counter() - start) * 1000
success = 200 <= response.status_code < 300
error_msg = None
if not success:
error_msg = body.decode(errors="ignore")[:200]
else:
# Estimate output tokens from response content
try:
resp_json = json.loads(body)
content = resp_json.get("content", "") or resp_json.get("text", "") or resp_json.get("markdown", "")
tokens_count += len(content) // CHARS_PER_TOKEN
except:
pass
return RequestMetric(
task_name=name,
endpoint=path,
status_code=response.status_code,
ttfb_ms=ttfb,
total_ms=total_ms,
success=success,
tokens=tokens_count,
error=error_msg
)
except Exception as e:
total_ms = (time.perf_counter() - start) * 1000
return RequestMetric(
task_name=name,
endpoint=path,
status_code=0,
ttfb_ms=ttfb or total_ms,
total_ms=total_ms,
success=False,
tokens=tokens_count,
error=str(e)
)
async def run_task(self, client: httpx.AsyncClient, task_type: str, name: str, iterations: int):
if name not in self.results:
self.results[name] = BenchStats(name)
stats = self.results[name]
stats.start_time = time.perf_counter()
sem = self.semaphores.get(task_type, self.semaphores["completions"])
async def worker():
async with sem:
if task_type == "completions":
# Stability Test Variation
prefix_len = 100
if "Normal" in name: prefix_len = 1000
if "Long" in name: prefix_len = 4000
metric = await self._execute_request(client, name, "POST", "/v1/completions", json={
"prefix": generate_context_text(prefix_len),
"suffix": "End of document.",
"model_thinking": "low"
})
elif task_type == "ocr":
metric = await self._execute_request(client, name, "POST", "/v1/ocr", json={
"image": get_dummy_base64_image(),
"filename": "bench.png"
})
elif task_type == "convert":
metric = await self._execute_request(client, name, "POST", "/v1/convert", json={
"file": base64.b64encode(b"Performance test data").decode(),
"filename": "bench.txt"
})
elif task_type == "tts":
metric = await self._execute_request(client, name, "POST", "/v1/tts-asr/tts", json={
"text": "This is a performance benchmark for the text to speech engine.",
"voice": "v2/en_speaker_6",
"format": "wav"
})
elif task_type == "asr":
metric = await self._execute_request(client, name, "POST", "/v1/tts-asr/asr", json={
"audio_base64": get_dummy_base64_audio(),
"language": "en"
})
else:
metric = await self._execute_request(client, name, "GET", "/v1/tts-asr/status")
stats.add(metric)
tasks = [worker() for _ in range(iterations)]
await asyncio.gather(*tasks)
stats.end_time = time.perf_counter()
def generate_report(self, output_file: str):
report = []
report.append(f"# API Benchmarking Report ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})")
report.append(f"\n**Base URL:** `{self.base_url}`")
# Summary Table
report.append("\n## Executive Summary")
report.append("| Task | Success Rate | Avg TTFB | Avg Latency | P95 Latency | TPS | RPS |")
report.append("| :--- | :--- | :--- | :--- | :--- | :--- | :--- |")
for name, stats in self.results.items():
s = stats.get_summary()
if not s: continue
report.append(f"| {s['name']} | {s['success_rate']:.1f}% | {s['avg_ttfb']:.1f}ms | {s['avg_latency']:.1f}ms | {s['p95_latency']:.1f}ms | {s['tps']:.1f} | {s['rps']:.2f} |")
# Stability Analysis
report.append("\n## Stability & Context Analysis")
report.append("Detailed analysis of how context length affects TTFB and overall performance.")
# Details per category
for name, stats in self.results.items():
s = stats.get_summary()
if not s: continue
report.append(f"\n### {name} Details")
report.append(f"- **Total Samples:** {s['total_requests']}")
report.append(f"- **Duration:** {s['duration']:.2f}s")
failures = [m for m in stats.metrics if not m.success]
if failures:
report.append(f"- **Top Errors:**")
for f in failures[:3]:
report.append(f" - `[{f.status_code}]` {f.error}")
with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(report))
logger.info(f"Report generated: {output_file}")
async def main():
parser = argparse.ArgumentParser(description="Advanced LLM API Benchmarker")
parser.add_argument("--url", default=DEFAULT_BASE_URL, help="Base URL")
parser.add_argument("--key", default=DEFAULT_API_KEY, help="API Key")
parser.add_argument("--c-comp", type=int, default=5, help="Completion Concurrency")
parser.add_argument("--c-ocr", type=int, default=2, help="OCR Concurrency")
parser.add_argument("--c-audio", type=int, default=2, help="TTS/ASR Concurrency")
parser.add_argument("--iters", type=int, default=10, help="Iterations per test suite")
parser.add_argument("--output", default="api_performance_report.md", help="Output report file")
args = parser.parse_args()
bench = ApiBenchmarker(args.url, args.key)
bench.semaphores["completions"] = asyncio.Semaphore(args.c_comp)
bench.semaphores["ocr"] = asyncio.Semaphore(args.c_ocr)
bench.semaphores["tts-asr"] = asyncio.Semaphore(args.c_audio)
async with httpx.AsyncClient(headers=bench.headers, timeout=120.0) as client:
logger.info("Starting Benchmark Suites...")
# Suite 1: Stability - Completion Contexts
logger.info("Running Stability Suite (Short Context)...")
await bench.run_task(client, "completions", "Completion-Short", args.iters)
logger.info("Running Stability Suite (Normal Context)...")
await bench.run_task(client, "completions", "Completion-Normal", args.iters)
logger.info("Running Stability Suite (Long Context)...")
await bench.run_task(client, "completions", "Completion-Long", args.iters)
# Suite 2: Functional Concurrency
logger.info("Running OCR Concurrency Suite...")
await bench.run_task(client, "ocr", "OCR-Concurrent", args.iters)
logger.info("Running TTS Concurrency Suite...")
await bench.run_task(client, "tts", "TTS-Concurrent", args.iters)
logger.info("Running ASR Concurrency Suite...")
await bench.run_task(client, "asr", "ASR-Concurrent", args.iters)
logger.info("Running File Transformation Suite...")
await bench.run_task(client, "convert", "Convert-Concurrent", args.iters)
bench.generate_report(args.output)
print(f"\nBenchmark Complete! View the report at: {args.output}")
if __name__ == "__main__":
asyncio.run(main())
+453
View File
@@ -0,0 +1,453 @@
# TTS/ASR 测试指南
本文档提供完整的测试脚本使用说明,包括单元测试、集成测试和macOS环境模拟测试。
## 测试脚本概览
| 脚本 | 位置 | 用途 | 需要后端服务 |
|------|------|------|--------------|
| `test_tts_asr_unit.py` | `backend/tests/` | 单元测试(设备检测、模型选择、音频处理) | 否 |
| `test_tts_asr_integration.py` | `backend/tests/` | 集成测试(API端点、完整流程) | 是 |
| `simulate_macos.py` | `backend/tests/` | macOS环境模拟(在非Mac环境测试) | 否 |
## 快速开始
### 1. 单元测试(推荐首先运行)
单元测试不需要实际运行模型或后端服务,测试代码逻辑:
```bash
# 使用pytest运行(推荐)
pytest backend/tests/test_tts_asr_unit.py -v
# 直接运行
python backend/tests/test_tts_asr_unit.py
# 运行特定测试类
pytest backend/tests/test_tts_asr_unit.py::TestAppleSiliconDetection -v
# 运行特定测试方法
pytest backend/tests/test_tts_asr_unit.py::TestAppleSiliconDetection::test_is_apple_silicon_on_darwin_arm64 -v
```
### 2. macOS环境模拟测试
在非macOS环境下模拟Apple Silicon环境:
```bash
# 运行完整模拟测试套件
python backend/tests/simulate_macos.py --full-simulation
# 仅模拟Apple Silicon环境并进入交互模式
python backend/tests/simulate_macos.py --apple-silicon
# 模拟特定设备
python backend/tests/simulate_macos.py --device mps
python backend/tests/simulate_macos.py --device cuda
# 运行特定测试
python backend/tests/simulate_macos.py --test device # 设备检测
python backend/tests/simulate_macos.py --test memory # 内存管理
python backend/tests/simulate_macos.py --test model # 模型选择
python backend/tests/simulate_macos.py --test audio # 音频处理
python backend/tests/simulate_macos.py --test env # 环境变量
```
### 3. 集成测试
集成测试需要运行后端服务:
```bash
# 1. 启动后端服务(终端1
python backend/main.py
# 2. 运行集成测试(终端2
# 运行所有测试
python backend/tests/test_tts_asr_integration.py
# 运行特定测试
python backend/tests/test_tts_asr_integration.py --test config # 配置端点
python backend/tests/test_tts_asr_integration.py --test status # 状态端点
python backend/tests/test_tts_asr_integration.py --test warmup # 预热测试
python backend/tests/test_tts_asr_integration.py --test tts # TTS测试
python backend/tests/test_tts_asr_integration.py --test asr # ASR测试
python backend/tests/test_tts_asr_integration.py --test perf # 性能测试
# 自定义API地址
python backend/tests/test_tts_asr_integration.py --url http://localhost:8001 --key your-api-key
```
## 详细测试说明
### 单元测试详解
#### TestAppleSiliconDetection
测试Apple Silicon检测功能:
- `test_is_apple_silicon_on_darwin_arm64`: 在Darwin/arm64环境检测
- `test_is_apple_silicon_on_windows`: 在Windows环境不应检测到
- `test_is_apple_silicon_on_linux`: 在Linux环境不应检测到
#### TestEnvironmentVariables
测试环境变量解析:
- `test_default_environment_values`: 验证默认值
- `test_custom_environment_values`: 验证自定义值
#### TestModelSizeSelection
测试模型大小选择:
- `test_whisper_model_sizes_mapping`: 模型大小映射验证
- `test_recommended_model_size_explicit`: 显式指定大小
- `test_invalid_model_size_falls_back`: 无效大小回退
#### TestAudioValidation
测试音频验证:
- `test_validate_empty_audio`: 空音频验证
- `test_validate_valid_wav_header`: 有效WAV头验证
- `test_validate_invalid_audio`: 无效音频验证
#### TestAudioResampling
测试音频重采样:
- `test_resample_same_rate`: 相同采样率
- `test_resample_different_rate`: 不同采样率重采样
- `test_resample_downsample`: 下采样
#### TestDeviceCapabilities
测试设备能力检测:
- `test_device_capabilities_dataclass`: 数据类验证
- `test_device_capabilities_with_mps`: MPS设备能力
#### TestModelCacheCheck
测试模型缓存检查:
- `test_cache_check_non_offline_mode`: 非离线模式
- `test_cache_check_offline_mode_missing`: 离线模式缺失模型
#### TestRequestResponseModels
测试API模型:
- `test_tts_request_model`: TTS请求模型
- `test_asr_request_model`: ASR请求模型
- `test_model_status_model`: 状态模型
### 集成测试详解
#### TTSASRIntegrationTest
主要集成测试:
- `test_01_config_endpoint`: 配置端点测试
- `test_02_status_endpoint`: 状态端点测试
- `test_03_warmup_endpoint`: 预热端点测试
- `test_04_tts_endpoint_basic`: TTS基本功能测试
- `test_05_asr_endpoint_basic`: ASR基本功能测试
- `test_06_api_key_validation`: API密钥验证测试
- `test_07_tts_long_text`: TTS长文本测试
#### PerformanceTest
性能测试:
- `test_tts_latency`: TTS延迟测试
### macOS模拟测试详解
#### MacOSSimulator类
提供以下模拟功能:
- `simulate_apple_silicon()`: 模拟Darwin/arm64环境
- `simulate_mps_device()`: 模拟MPS设备可用
- `simulate_cuda_device()`: 模拟CUDA设备可用
- `cleanup()`: 清理模拟环境
#### 独立测试函数
- `test_device_detection_on_apple_silicon()`: Apple Silicon设备检测
- `test_memory_management()`: 内存管理测试
- `test_model_size_selection()`: 模型大小选择测试
- `test_audio_processing()`: 音频处理测试
- `test_environment_variables()`: 环境变量测试
## 测试覆盖率
### 单元测试覆盖的功能
- [x] Apple Silicon检测逻辑
- [x] 环境变量解析和默认值
- [x] 模型大小选择和推荐
- [x] 音频数据验证
- [x] 音频重采样(多回退方案)
- [x] 设备能力检测数据结构
- [x] 模型缓存检查
- [x] API请求/响应模型
### 集成测试覆盖的功能
- [x] 配置端点(`/v1/tts-asr/config`
- [x] 状态端点(`/v1/tts-asr/status`
- [x] 预热端点(`/v1/tts-asr/warmup`
- [x] TTS端点(`/v1/tts-asr/tts`
- [x] ASR端点(`/v1/tts-asr/asr`
- [x] API密钥验证
- [x] 长文本处理
- [x] 性能基准测试
### macOS模拟测试覆盖的场景
- [x] Apple Silicon环境模拟
- [x] MPS设备模拟
- [x] CUDA设备模拟
- [x] 系统内存模拟
- [x] 完整环境变量测试
## 常见测试场景
### 场景1: 开发时快速验证
```bash
# 快速单元测试
pytest backend/tests/test_tts_asr_unit.py -v --tb=short
# macOS模拟(完整)
python backend/tests/simulate_macos.py --full-simulation
```
### 场景2: 验证特定配置
```bash
# 设置环境变量后测试
export TTS_ASR_MODEL_SIZE=small
export TTS_ASR_QUANTIZE=true
# 运行测试
python backend/tests/simulate_macos.py --test model
```
### 场景3: API功能验证
```bash
# 启动服务
python backend/main.py
# 测试配置端点
python backend/tests/test_tts_asr_integration.py --test config
# 测试TTS功能
python backend/tests/test_tts_asr_integration.py --test tts
# 测试ASR功能
python backend/tests/test_tts_asr_integration.py --test asr
```
### 场景4: 性能基准测试
```bash
# 启动服务
python backend/main.py
# 运行性能测试
python backend/tests/test_tts_asr_integration.py --test perf
```
## 测试输出解读
### 成功示例
```
test_is_apple_silicon_on_darwin_arm64 ... ok
test_is_apple_silicon_on_windows ... ok
test_is_apple_silicon_on_linux ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.005s
OK
```
### 失败示例
```
test_device_detection_on_apple_silicon ... FAIL
======================================================================
FAIL: test_device_detection_on_apple_silicon
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_tts_asr_unit.py", line 45, in test_is_apple_silicon_on_darwin_arm64
self.assertTrue(_is_apple_silicon())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 tests in 0.002s
FAILED (failures=1)
```
## 持续集成配置
### GitHub Actions示例
```yaml
name: TTS/ASR Tests
on: [push, pull_request]
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
pip install -r backend/requirements.txt
pip install pytest
- name: Run unit tests
run: pytest backend/tests/test_tts_asr_unit.py -v
- name: Run macOS simulation
run: python backend/tests/simulate_macos.py --full-simulation
```
### pytest配置
创建 `pytest.ini`:
```ini
[pytest]
testpaths = backend/tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts = -v --tb=short
```
## 故障排查
### 问题1: 导入错误
```
ModuleNotFoundError: No module named 'backend'
```
**解决方案**:
```bash
# 确保在项目根目录运行
cd /path/to/llm-in-text
# 或设置PYTHONPATH
export PYTHONPATH="${PYTHONPATH}:$(pwd)"
```
### 问题2: 后端服务连接失败
```
✗ 无法连接到服务: [Errno 111] Connection refused
```
**解决方案**:
```bash
# 确保后端服务正在运行
python backend/main.py
# 检查端口
lsof -i :8001
# 或使用自定义URL
python backend/tests/test_tts_asr_integration.py --url http://localhost:8001
```
### 问题3: 模型未加载
```
⚠ TTS失败(可能是模型未加载)
```
**解决方案**:
这是预期行为,表示模型需要时间下载。可以:
1. 等待模型下载完成
2. 使用预热端点: `POST /v1/tts-asr/warmup`
3. 启用离线模式(如果模型已下载)
### 问题4: 测试超时
```
httpx.ReadTimeout: timed out
```
**解决方案**:
```bash
# 增加超时时间
export TEST_TIMEOUT=300.0
# 或在测试脚本中修改
TEST_TIMEOUT = 300.0 # 5分钟
```
## 最佳实践
1. **开发时**: 频繁运行单元测试
```bash
pytest backend/tests/test_tts_asr_unit.py -v --tb=short
```
2. **提交前**: 运行完整测试套件
```bash
pytest backend/tests/test_tts_asr_unit.py -v
python backend/tests/simulate_macos.py --full-simulation
```
3. **部署前**: 运行集成测试
```bash
python backend/tests/test_tts_asr_integration.py
```
4. **调试时**: 使用详细输出
```bash
pytest backend/tests/test_tts_asr_unit.py -v -s --tb=long
```
## 测试报告
生成测试覆盖率报告:
```bash
# 安装coverage
pip install pytest-cov
# 运行并生成报告
pytest backend/tests/test_tts_asr_unit.py --cov=backend.tts_asr --cov-report=html
# 查看报告
open htmlcov/index.html
```
## 相关文档
- [TTS/ASR修复说明](./TTS_ASR_MACOS_FIX.md)
- [环境变量配置](../README.md#ttsasr环境变量配置)
- [API文档](../README.md#api接口)
---
**更新日期**: 2026-04-06
**维护者**: 项目开发团队
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import base64
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from backend.tts_asr import _tts_sync_with_retry
async def play_audio():
print("生成测试音频中,请稍候...")
test_text = "这是一段用以测试新语音模型音质的中文合成音频。"
try:
audio_bytes, sr = await _tts_sync_with_retry(test_text, rate=1.0)
# 保存到本地文件
wav_path = os.path.join(os.path.dirname(__file__), "test_audio.wav")
with open(wav_path, "wb") as f:
f.write(audio_bytes)
print(f"音频已生成并保存到: {wav_path}")
print("正在尝试在 macOS 上播放...")
# Mac OS 的播放命令
os.system(f"afplay '{wav_path}'")
print("播放完成。")
except Exception as e:
import traceback
traceback.print_exc()
print(f"音频生成失败: {str(e)}")
if __name__ == "__main__":
asyncio.run(play_audio())
+188
View File
@@ -0,0 +1,188 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
快速验证脚本
验证TTS/ASR模块修复是否正确应用
运行方式:
python backend/tests/quick_verify.py
"""
import os
import sys
from pathlib import Path
# 设置控制台编码
if sys.platform == 'win32':
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# 确保可以导入backend模块
script_path = Path(__file__).resolve()
project_root = script_path.parent.parent.parent
sys.path.insert(0, str(project_root))
print(f"项目根目录: {project_root}")
print(f"脚本路径: {script_path}")
def check_file_exists(filepath: str, description: str) -> bool:
"""检查文件是否存在"""
full_path = project_root / filepath
exists = full_path.exists()
status = "[OK]" if exists else "[FAIL]"
print(f"{status} {description}: {filepath} (完整路径: {full_path})")
return exists
def check_function_exists(module_name: str, function_name: str) -> bool:
"""检查函数是否存在"""
try:
module = __import__(module_name, fromlist=[function_name])
exists = hasattr(module, function_name)
status = "[OK]" if exists else "[FAIL]"
print(f"{status} 函数存在: {module_name}.{function_name}")
return exists
except Exception as e:
print(f"[FAIL] 导入失败: {module_name} - {e}")
return False
def check_environment_variable(var_name: str, expected_default: str) -> bool:
"""检查环境变量默认值"""
try:
# 清除可能存在的环境变量
original_value = os.environ.get(var_name)
if var_name in os.environ:
del os.environ[var_name]
# 重新导入模块
if 'backend.tts_asr' in sys.modules:
del sys.modules['backend.tts_asr']
from backend.tts_asr import (
TTS_ASR_DEVICE, TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE,
TTS_ASR_OFFLINE_MODE, TTS_ASR_WARMUP, TTS_ASR_WARMUP_TIMEOUT,
TTS_ASR_IDLE_TIMEOUT, TTS_ASR_MPS_MEMORY_LIMIT_MB
)
var_map = {
'TTS_ASR_DEVICE': TTS_ASR_DEVICE,
'TTS_ASR_MODEL_SIZE': TTS_ASR_MODEL_SIZE,
'TTS_ASR_QUANTIZE': TTS_ASR_QUANTIZE,
'TTS_ASR_OFFLINE_MODE': TTS_ASR_OFFLINE_MODE,
'TTS_ASR_WARMUP': TTS_ASR_WARMUP,
'TTS_ASR_WARMUP_TIMEOUT': TTS_ASR_WARMUP_TIMEOUT,
'TTS_ASR_IDLE_TIMEOUT': TTS_ASR_IDLE_TIMEOUT,
'TTS_ASR_MPS_MEMORY_LIMIT_MB': TTS_ASR_MPS_MEMORY_LIMIT_MB,
}
actual_value = var_map.get(var_name)
if var_name == 'TTS_ASR_MODEL_SIZE':
expected = 'auto'
elif var_name == 'TTS_ASR_QUANTIZE':
expected = False
elif var_name == 'TTS_ASR_OFFLINE_MODE':
expected = False
elif var_name == 'TTS_ASR_WARMUP':
expected = True
elif var_name == 'TTS_ASR_WARMUP_TIMEOUT':
expected = 120
elif var_name == 'TTS_ASR_IDLE_TIMEOUT':
expected = 0
elif var_name == 'TTS_ASR_MPS_MEMORY_LIMIT_MB':
expected = 8192
else:
expected = expected_default
matches = actual_value == expected
status = "[OK]" if matches else "[FAIL]"
print(f"{status} 环境变量默认值: {var_name} = {actual_value} (预期: {expected})")
return matches
except Exception as e:
print(f"[FAIL] 检查环境变量失败: {var_name} - {e}")
return False
def main():
print("="*70)
print("TTS/ASR模块快速验证")
print("="*70)
checks = []
# 1. 检查文件
print("\n[1] 文件检查")
print("-"*70)
checks.append(check_file_exists("backend/tts_asr.py", "主模块文件"))
checks.append(check_file_exists("backend/tests/test_tts_asr_unit.py", "单元测试"))
checks.append(check_file_exists("backend/tests/test_tts_asr_integration.py", "集成测试"))
checks.append(check_file_exists("backend/tests/simulate_macos.py", "macOS模拟工具"))
checks.append(check_file_exists("backend/tests/TESTING_GUIDE.md", "测试指南"))
checks.append(check_file_exists("backend/TTS_ASR_MACOS_FIX.md", "修复文档"))
# 2. 检查核心函数
print("\n[2] 核心函数检查")
print("-"*70)
checks.append(check_function_exists("backend.tts_asr", "_is_apple_silicon"))
checks.append(check_function_exists("backend.tts_asr", "_detect_device_capabilities"))
checks.append(check_function_exists("backend.tts_asr", "_get_recommended_model_size"))
checks.append(check_function_exists("backend.tts_asr", "_validate_audio_data"))
checks.append(check_function_exists("backend.tts_asr", "_resample_audio_robust"))
checks.append(check_function_exists("backend.tts_asr", "_check_model_cached"))
# 3. 检查数据类
print("\n[3] 数据类检查")
print("-"*70)
checks.append(check_function_exists("backend.tts_asr", "DeviceCapabilities"))
checks.append(check_function_exists("backend.tts_asr", "ModelStatus"))
# 4. 检查环境变量
print("\n[4] 环境变量默认值检查")
print("-"*70)
checks.append(check_environment_variable("TTS_ASR_DEVICE", "auto"))
checks.append(check_environment_variable("TTS_ASR_MODEL_SIZE", "auto"))
checks.append(check_environment_variable("TTS_ASR_QUANTIZE", "false"))
checks.append(check_environment_variable("TTS_ASR_OFFLINE_MODE", "false"))
# 5. 检查常量
print("\n[5] 常量检查")
print("-"*70)
try:
from backend.tts_asr import WHISPER_MODEL_SIZES, APPLE_SILICON_DEFAULT_SIZE
expected_sizes = ['tiny', 'base', 'small', 'medium', 'large', 'turbo']
sizes_match = list(WHISPER_MODEL_SIZES.keys()) == expected_sizes
status = "[OK]" if sizes_match else "[FAIL]"
print(f"{status} WHISPER_MODEL_SIZES: {list(WHISPER_MODEL_SIZES.keys())}")
checks.append(sizes_match)
size_match = APPLE_SILICON_DEFAULT_SIZE == 'small'
status = "[OK]" if size_match else "[FAIL]"
print(f"{status} APPLE_SILICON_DEFAULT_SIZE: {APPLE_SILICON_DEFAULT_SIZE}")
checks.append(size_match)
except Exception as e:
print(f"[FAIL] 常量检查失败: {e}")
checks.extend([False, False])
# 汇总结果
print("\n" + "="*70)
print("验证结果")
print("="*70)
total = len(checks)
passed = sum(checks)
print(f"通过: {passed}/{total}")
if all(checks):
print("\n[SUCCESS] 所有验证通过!TTS/ASR模块修复已正确应用。")
return 0
else:
print("\n[FAILED] 部分验证失败,请检查上述错误。")
return 1
if __name__ == '__main__':
sys.exit(main())
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TTS/ASR测试运行器
便捷地运行各种测试组合
运行方式:
python backend/tests/run_tests.py --help
python backend/tests/run_tests.py unit
python backend/tests/run_tests.py integration
python backend/tests/run_tests.py simulate
python backend/tests/run_tests.py all
"""
import argparse
import os
import subprocess
import sys
from pathlib import Path
def run_command(cmd: list, cwd: str = None) -> int:
"""运行命令并返回退出码"""
print(f"\n执行: {' '.join(cmd)}")
print("-" * 70)
result = subprocess.run(cmd, cwd=cwd)
return result.returncode
def run_unit_tests(verbose: bool = False) -> int:
"""运行单元测试"""
print("\n" + "="*70)
print("运行单元测试")
print("="*70)
cmd = ['pytest', 'backend/tests/test_tts_asr_unit.py']
if verbose:
cmd.append('-v')
return run_command(cmd)
def run_integration_tests(test_type: str = None, url: str = None, key: str = None) -> int:
"""运行集成测试"""
print("\n" + "="*70)
print("运行集成测试")
print("="*70)
cmd = ['python', 'backend/tests/test_tts_asr_integration.py']
if test_type:
cmd.extend(['--test', test_type])
if url:
cmd.extend(['--url', url])
if key:
cmd.extend(['--key', key])
return run_command(cmd)
def run_simulation(test_type: str = None) -> int:
"""运行macOS模拟测试"""
print("\n" + "="*70)
print("运行macOS环境模拟测试")
print("="*70)
if test_type == 'full':
cmd = ['python', 'backend/tests/simulate_macos.py', '--full-simulation']
elif test_type:
cmd = ['python', 'backend/tests/simulate_macos.py', '--test', test_type]
else:
cmd = ['python', 'backend/tests/simulate_macos.py', '--full-simulation']
return run_command(cmd)
def run_all_tests(url: str = None, key: str = None) -> int:
"""运行所有测试"""
print("\n" + "="*70)
print("运行完整测试套件")
print("="*70)
results = []
# 1. 单元测试
print("\n[1/3] 单元测试")
results.append(("单元测试", run_unit_tests(verbose=True)))
# 2. macOS模拟测试
print("\n[2/3] macOS模拟测试")
results.append(("macOS模拟", run_simulation(test_type='full')))
# 3. 集成测试(如果服务可用)
print("\n[3/3] 集成测试")
print("注意: 集成测试需要后端服务运行中")
response = input("是否继续运行集成测试? [y/N]: ")
if response.lower() == 'y':
results.append(("集成测试", run_integration_tests(url=url, key=key)))
else:
print("跳过集成测试")
results.append(("集成测试", 0))
# 汇总结果
print("\n" + "="*70)
print("测试结果汇总")
print("="*70)
total_passed = 0
for name, code in results:
status = "✓ 通过" if code == 0 else "✗ 失败"
print(f"{name}: {status}")
if code == 0:
total_passed += 1
print("\n" + "-"*70)
print(f"总计: {total_passed}/{len(results)} 测试套件通过")
print("="*70)
return 0 if all(code == 0 for _, code in results) else 1
def main():
parser = argparse.ArgumentParser(
description='TTS/ASR测试运行器',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
# 运行单元测试
python backend/tests/run_tests.py unit
# 运行集成测试
python backend/tests/run_tests.py integration
# 运行macOS模拟测试
python backend/tests/run_tests.py simulate
# 运行所有测试
python backend/tests/run_tests.py all
# 运行特定集成测试
python backend/tests/run_tests.py integration --test config
# 运行特定模拟测试
python backend/tests/run_tests.py simulate --test device
"""
)
subparsers = parser.add_subparsers(dest='command', help='测试类型')
# 单元测试
unit_parser = subparsers.add_parser('unit', help='运行单元测试')
unit_parser.add_argument('-v', '--verbose', action='store_true', help='详细输出')
# 集成测试
integration_parser = subparsers.add_parser('integration', help='运行集成测试')
integration_parser.add_argument('--test', choices=[
'config', 'status', 'warmup', 'tts', 'asr', 'perf'
], help='运行特定测试')
integration_parser.add_argument('--url', default='http://localhost:8001', help='API URL')
integration_parser.add_argument('--key', default='your-secret-key-here', help='API密钥')
# macOS模拟测试
simulate_parser = subparsers.add_parser('simulate', help='运行macOS模拟测试')
simulate_parser.add_argument('--test', choices=[
'device', 'memory', 'model', 'audio', 'env', 'full'
], help='运行特定测试')
# 所有测试
all_parser = subparsers.add_parser('all', help='运行所有测试')
all_parser.add_argument('--url', default='http://localhost:8001', help='API URL')
all_parser.add_argument('--key', default='your-secret-key-here', help='API密钥')
args = parser.parse_args()
# 确保在项目根目录
project_root = Path(__file__).parent.parent.parent
os.chdir(project_root)
if args.command == 'unit':
return run_unit_tests(verbose=args.verbose)
elif args.command == 'integration':
return run_integration_tests(
test_type=args.test,
url=args.url,
key=args.key
)
elif args.command == 'simulate':
return run_simulation(test_type=args.test)
elif args.command == 'all':
return run_all_tests(url=args.url, key=args.key)
else:
parser.print_help()
return 0
if __name__ == '__main__':
sys.exit(main())
+505
View File
@@ -0,0 +1,505 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
macOS环境模拟测试工具
在非macOS环境下模拟Apple Silicon环境进行测试
运行方式:
python backend/tests/simulate_macos.py --help
python backend/tests/simulate_macos.py --device mps
python backend/tests/simulate_macos.py --apple-silicon
python backend/tests/simulate_macos.py --full-simulation
"""
import argparse
import importlib
import os
import platform
import sys
from unittest.mock import patch
import numpy as np
class MacOSSimulator:
"""macOS环境模拟器"""
def __init__(self):
self.original_platform_system = platform.system
self.original_platform_machine = platform.machine
self.patches = []
def simulate_apple_silicon(self):
"""模拟Apple Silicon环境"""
print("\n" + "="*70)
print("模拟 Apple Silicon 环境")
print("="*70)
# 模拟Darwin系统和arm64架构
self.patches.append(patch('platform.system', return_value='Darwin'))
self.patches.append(patch('platform.machine', return_value='arm64'))
for p in self.patches:
p.start()
print("✓ 平台: Darwin (macOS)")
print("✓ 架构: arm64 (Apple Silicon)")
def simulate_mps_device(self):
"""模拟MPS设备可用"""
print("\n" + "="*70)
print("模拟 MPS 设备")
print("="*70)
# 创建模拟的torch.backends.mps
mock_mps = type('MockMPS', (), {
'is_available': lambda: True,
'is_built': lambda: True,
'empty_cache': lambda: None
})()
mock_backends = type('MockBackends', (), {
'mps': mock_mps
})()
# 模拟torch模块
mock_torch = type('MockTorch', (), {
'backends': mock_backends,
'mps': mock_mps,
'randn': lambda *args, **kwargs: np.random.randn(*args),
'mm': lambda a, b: np.dot(a, b),
'empty_cache': lambda: None
})()
self.patches.append(patch('torch', mock_torch))
self.patches.append(patch('torch.backends.mps.is_available', return_value=True))
self.patches.append(patch('torch.backends.mps.is_built', return_value=True))
for p in self.patches[-3:]:
p.start()
print("✓ MPS 可用: True")
print("✓ MPS 已编译: True")
def simulate_cuda_device(self):
"""模拟CUDA设备可用"""
print("\n" + "="*70)
print("模拟 CUDA 设备")
print("="*70)
mock_cuda = type('MockCUDA', (), {
'is_available': lambda: True,
'device_count': lambda: 1,
'get_device_properties': lambda n: type('Props', (), {'total_memory': 8*1024*1024*1024})(),
'empty_cache': lambda: None
})()
self.patches.append(patch('torch.cuda', mock_cuda))
self.patches.append(patch('torch.cuda.is_available', return_value=True))
for p in self.patches[-2:]:
p.start()
print("✓ CUDA 可用: True")
print("✓ GPU 数量: 1")
print("✓ 显存: 8 GB")
def cleanup(self):
"""清理所有补丁"""
for p in self.patches:
p.stop()
self.patches.clear()
print("\n✓ 已清理模拟环境")
def test_device_detection_on_apple_silicon():
"""测试Apple Silicon设备检测"""
print("\n测试1: Apple Silicon 设备检测")
print("-"*70)
simulator = MacOSSimulator()
try:
simulator.simulate_apple_silicon()
simulator.simulate_mps_device()
# 设置环境变量
os.environ['TTS_ASR_DEVICE'] = 'auto'
os.environ['TTS_ASR_MODEL_SIZE'] = 'auto'
# 重新导入模块以应用模拟
if 'backend.tts_asr' in sys.modules:
del sys.modules['backend.tts_asr']
from backend.tts_asr import (
_is_apple_silicon,
_detect_device_capabilities,
_get_recommended_model_size
)
# 测试Apple Silicon检测
assert _is_apple_silicon(), "应该检测到Apple Silicon"
print("✓ Apple Silicon 检测: 通过")
# 测试设备能力检测
caps = _detect_device_capabilities()
print(f"✓ 设备: {caps.device}")
print(f"✓ MPS 可用: {caps.mps_available}")
print(f"✓ 推荐模型大小: {caps.recommended_model_size}")
# 测试模型大小推荐
recommended_size = _get_recommended_model_size()
assert recommended_size in ['small', 'tiny', 'base'], \
f"Apple Silicon应推荐小模型,但推荐了 {recommended_size}"
print(f"✓ 推荐模型大小: {recommended_size}")
print("\n✓ 测试通过")
return True
except Exception as e:
print(f"\n✗ 测试失败: {e}")
import traceback
traceback.print_exc()
return False
finally:
simulator.cleanup()
def test_memory_management():
"""测试内存管理"""
print("\n测试2: 内存管理")
print("-"*70)
simulator = MacOSSimulator()
try:
simulator.simulate_apple_silicon()
simulator.simulate_mps_device()
# 模拟系统内存
import psutil
original_virtual_memory = psutil.virtual_memory
def mock_virtual_memory():
mock_mem = type('MockMemory', (), {
'total': 16 * 1024 * 1024 * 1024 # 16GB
})()
return mock_mem
self.patches.append(patch('psutil.virtual_memory', mock_virtual_memory))
from backend.tts_asr import _get_system_memory_mb, TTS_ASR_MPS_MEMORY_LIMIT_MB
mem_mb = _get_system_memory_mb()
print(f"✓ 系统内存: {mem_mb} MB")
# 计算预期的MPS内存限制(60%
expected_limit = int(mem_mb * 0.6)
print(f"✓ 预期MPS限制: {expected_limit} MB (60%)")
print(f"✓ 配置MPS限制: {TTS_ASR_MPS_MEMORY_LIMIT_MB} MB")
print("\n✓ 测试通过")
return True
except Exception as e:
print(f"\n✗ 测试失败: {e}")
import traceback
traceback.print_exc()
return False
finally:
simulator.cleanup()
def test_model_size_selection():
"""测试模型大小选择"""
print("\n测试3: 模型大小选择")
print("-"*70)
test_cases = [
('auto', 'Apple Silicon默认'),
('tiny', '最小模型'),
('small', '推荐模型'),
('medium', '中等模型'),
('large', '大模型'),
('turbo', 'turbo模型'),
]
from backend.tts_asr import WHISPER_MODEL_SIZES, _get_recommended_model_size
for size, desc in test_cases:
os.environ['TTS_ASR_MODEL_SIZE'] = size
# 重新加载模块
if 'backend.tts_asr' in sys.modules:
del sys.modules['backend.tts_asr']
from backend.tts_asr import _get_recommended_model_size
if size == 'auto':
# 自动选择
recommended = _get_recommended_model_size()
print(f"{desc}: {recommended}")
else:
# 显式选择
os.environ['TTS_ASR_MODEL_SIZE'] = size
result = _get_recommended_model_size()
assert result == size, f"应该返回 {size},但返回了 {result}"
print(f"{desc}: {size} -> {WHISPER_MODEL_SIZES[size]}")
print("\n✓ 测试通过")
return True
def test_audio_processing():
"""测试音频处理"""
print("\n测试4: 音频处理")
print("-"*70)
from backend.tts_asr import (
_validate_audio_data,
_resample_audio_robust
)
# 测试音频验证
test_cases = [
(b'', False, "空数据"),
(b'short', False, "太短"),
(b'RIFF' + b'\x00' * 40, True, "有效WAV头"),
]
for data, expected, desc in test_cases:
result = _validate_audio_data(data)
assert result == expected, f"{desc}: 预期 {expected},得到 {result}"
print(f"✓ 音频验证 ({desc}): {'通过' if result == expected else '失败'}")
# 测试重采样
audio_16k = np.sin(np.linspace(0, 2*np.pi, 16000)).astype(np.float32)
# 16k -> 48k
audio_48k = _resample_audio_robust(audio_16k, 16000, 48000)
assert len(audio_48k) == 48000, f"48kHz音频长度错误: {len(audio_48k)}"
print(f"✓ 重采样 (16k -> 48k): 长度 {len(audio_16k)} -> {len(audio_48k)}")
# 48k -> 16k
audio_back = _resample_audio_robust(audio_48k, 48000, 16000)
assert len(audio_back) == 16000, f"16kHz音频长度错误: {len(audio_back)}"
print(f"✓ 重采样 (48k -> 16k): 长度 {len(audio_48k)} -> {len(audio_back)}")
print("\n✓ 测试通过")
return True
def test_environment_variables():
"""测试环境变量"""
print("\n测试5: 环境变量配置")
print("-"*70)
# 清理环境变量
env_vars = [
'TTS_ASR_DEVICE', 'TTS_ASR_MODEL_SIZE', 'TTS_ASR_QUANTIZE',
'TTS_ASR_OFFLINE_MODE', 'TTS_ASR_WARMUP', 'TTS_ASR_WARMUP_TIMEOUT',
'TTS_ASR_IDLE_TIMEOUT', 'TTS_ASR_MPS_MEMORY_LIMIT_MB'
]
original_values = {}
for var in env_vars:
original_values[var] = os.environ.get(var)
if var in os.environ:
del os.environ[var]
try:
# 测试默认值
from backend.tts_asr import (
TTS_ASR_DEVICE, TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE,
TTS_ASR_OFFLINE_MODE, TTS_ASR_WARMUP, TTS_ASR_WARMUP_TIMEOUT,
TTS_ASR_IDLE_TIMEOUT, TTS_ASR_MPS_MEMORY_LIMIT_MB
)
defaults = {
'TTS_ASR_DEVICE': 'auto',
'TTS_ASR_MODEL_SIZE': 'auto',
'TTS_ASR_QUANTIZE': False,
'TTS_ASR_OFFLINE_MODE': False,
'TTS_ASR_WARMUP': True,
'TTS_ASR_WARMUP_TIMEOUT': 120,
'TTS_ASR_IDLE_TIMEOUT': 0,
'TTS_ASR_MPS_MEMORY_LIMIT_MB': 8192,
}
for var, expected in defaults.items():
actual = locals()[var]
assert actual == expected, f"{var}: 预期 {expected},得到 {actual}"
print(f"{var} = {actual}")
# 测试自定义值
print("\n自定义配置测试:")
os.environ['TTS_ASR_MODEL_SIZE'] = 'small'
os.environ['TTS_ASR_QUANTIZE'] = 'true'
os.environ['TTS_ASR_OFFLINE_MODE'] = 'true'
os.environ['TTS_ASR_MPS_MEMORY_LIMIT_MB'] = '4096'
# 重新加载
if 'backend.tts_asr' in sys.modules:
del sys.modules['backend.tts_asr']
from backend.tts_asr import (
TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE,
TTS_ASR_OFFLINE_MODE, TTS_ASR_MPS_MEMORY_LIMIT_MB
)
assert TTS_ASR_MODEL_SIZE == 'small'
assert TTS_ASR_QUANTIZE == True
assert TTS_ASR_OFFLINE_MODE == True
assert TTS_ASR_MPS_MEMORY_LIMIT_MB == 4096
print(f"✓ TTS_ASR_MODEL_SIZE = {TTS_ASR_MODEL_SIZE}")
print(f"✓ TTS_ASR_QUANTIZE = {TTS_ASR_QUANTIZE}")
print(f"✓ TTS_ASR_OFFLINE_MODE = {TTS_ASR_OFFLINE_MODE}")
print(f"✓ TTS_ASR_MPS_MEMORY_LIMIT_MB = {TTS_ASR_MPS_MEMORY_LIMIT_MB}")
print("\n✓ 测试通过")
return True
finally:
# 恢复原始值
for var, value in original_values.items():
if value is not None:
os.environ[var] = value
elif var in os.environ:
del os.environ[var]
def run_full_simulation():
"""运行完整模拟测试"""
print("\n" + "="*70)
print("完整macOS环境模拟测试")
print("="*70)
results = []
# 运行所有测试
results.append(("设备检测", test_device_detection_on_apple_silicon()))
results.append(("内存管理", test_memory_management()))
results.append(("模型选择", test_model_size_selection()))
results.append(("音频处理", test_audio_processing()))
results.append(("环境变量", test_environment_variables()))
# 汇总结果
print("\n" + "="*70)
print("测试结果汇总")
print("="*70)
for name, passed in results:
status = "✓ 通过" if passed else "✗ 失败"
print(f"{name}: {status}")
total = len(results)
passed = sum(1 for _, p in results if p)
print("\n" + "-"*70)
print(f"总计: {passed}/{total} 测试通过")
print("="*70)
return all(p for _, p in results)
def main():
parser = argparse.ArgumentParser(
description='macOS环境模拟测试工具',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
# 运行完整模拟测试
python backend/tests/simulate_macos.py --full-simulation
# 仅模拟Apple Silicon环境
python backend/tests/simulate_macos.py --apple-silicon
# 仅模拟MPS设备
python backend/tests/simulate_macos.py --device mps
# 仅模拟CUDA设备
python backend/tests/simulate_macos.py --device cuda
"""
)
parser.add_argument(
'--full-simulation',
action='store_true',
help='运行完整模拟测试'
)
parser.add_argument(
'--apple-silicon',
action='store_true',
help='模拟Apple Silicon环境'
)
parser.add_argument(
'--device',
choices=['mps', 'cuda'],
help='模拟特定设备'
)
parser.add_argument(
'--test',
choices=['device', 'memory', 'model', 'audio', 'env'],
help='运行特定测试'
)
args = parser.parse_args()
# 确保可以导入backend模块
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
if args.full_simulation:
success = run_full_simulation()
sys.exit(0 if success else 1)
if args.apple_silicon:
simulator = MacOSSimulator()
try:
simulator.simulate_apple_silicon()
simulator.simulate_mps_device()
print("\n环境已模拟,按Ctrl+D退出")
print("在Python环境中可以使用:")
print(" from backend.tts_asr import _is_apple_silicon")
print(" print(_is_apple_silicon()) # 应该返回 True")
# 进入交互模式
import code
code.interact(local=locals())
finally:
simulator.cleanup()
if args.device:
simulator = MacOSSimulator()
try:
if args.device == 'mps':
simulator.simulate_mps_device()
elif args.device == 'cuda':
simulator.simulate_cuda_device()
print("\n设备已模拟")
import code
code.interact(local=locals())
finally:
simulator.cleanup()
if args.test:
test_func = {
'device': test_device_detection_on_apple_silicon,
'memory': test_memory_management,
'model': test_model_size_selection,
'audio': test_audio_processing,
'env': test_environment_variables,
}
success = test_func[args.test]()
sys.exit(0 if success else 1)
# 默认运行完整测试
if not any([args.full_simulation, args.apple_silicon, args.device, args.test]):
parser.print_help()
if __name__ == '__main__':
main()
Binary file not shown.
+395
View File
@@ -0,0 +1,395 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TTS/ASR模块集成测试
测试API端点和完整流程(需要运行后端服务)
运行方式:
# 方式1: 使用pytest
pytest backend/tests/test_tts_asr_integration.py -v -s
# 方式2: 直接运行
python backend/tests/test_tts_asr_integration.py
# 方式3: 测试特定端点
python backend/tests/test_tts_asr_integration.py --test config
"""
import asyncio
import base64
import json
import os
import sys
import time
import unittest
from typing import Optional
import httpx
# 配置
API_BASE_URL = os.environ.get('API_BASE_URL', 'http://localhost:8001')
API_KEY = os.environ.get('API_KEY', 'your-secret-key-here')
TEST_TIMEOUT = 120.0 # 2分钟超时
class TTSASRIntegrationTest(unittest.TestCase):
"""TTS/ASR集成测试"""
@classmethod
def setUpClass(cls):
"""测试类初始化"""
cls.client = httpx.Client(timeout=TEST_TIMEOUT)
cls.headers = {'X-API-Key': API_KEY}
# 检查服务是否运行
try:
response = cls.client.get(f'{API_BASE_URL}/v1/tts-asr/status', headers=cls.headers)
if response.status_code == 200:
cls.service_available = True
print(f"\n✓ 服务可用: {API_BASE_URL}")
else:
cls.service_available = False
print(f"\n✗ 服务返回非200状态码: {response.status_code}")
except Exception as e:
cls.service_available = False
print(f"\n✗ 无法连接到服务: {e}")
print(f" 请确保后端服务正在运行: python backend/main.py")
@classmethod
def tearDownClass(cls):
"""测试类清理"""
cls.client.close()
def setUp(self):
"""每个测试前的检查"""
if not self.service_available:
self.skipTest("后端服务不可用")
def test_01_config_endpoint(self):
"""测试配置端点"""
response = self.client.get(
f'{API_BASE_URL}/v1/tts-asr/config',
headers=self.headers
)
self.assertEqual(response.status_code, 200)
config = response.json()
# 验证配置结构
self.assertIn('environment', config)
self.assertIn('device', config)
self.assertIn('model', config)
self.assertIn('status', config)
# 验证环境变量配置
env = config['environment']
self.assertIn('TTS_ASR_DEVICE', env)
self.assertIn('TTS_ASR_MODEL_SIZE', env)
self.assertIn('TTS_ASR_QUANTIZE', env)
# 验证设备信息
device = config['device']
self.assertIn('current', device)
self.assertIn('mps_available', device)
self.assertIn('cuda_available', device)
self.assertIn('is_apple_silicon', device)
# 验证模型信息
model = config['model']
self.assertIn('tts', model)
self.assertIn('asr_current_size', model)
self.assertIn('available_sizes', model)
print(f"\n配置信息:")
print(f" 设备: {device['current']}")
print(f" Apple Silicon: {device['is_apple_silicon']}")
print(f" MPS可用: {device['mps_available']}")
print(f" ASR模型大小: {model['asr_current_size']}")
def test_02_status_endpoint(self):
"""测试状态端点"""
response = self.client.get(
f'{API_BASE_URL}/v1/tts-asr/status',
headers=self.headers
)
self.assertEqual(response.status_code, 200)
status = response.json()
# 验证状态结构
self.assertIn('tts_loaded', status)
self.assertIn('asr_loaded', status)
self.assertIn('device', status)
self.assertIn('offline_mode', status)
self.assertIn('quantize_enabled', status)
print(f"\n状态信息:")
print(f" TTS已加载: {status['tts_loaded']}")
print(f" ASR已加载: {status['asr_loaded']}")
print(f" 设备: {status['device']}")
print(f" 离线模式: {status['offline_mode']}")
print(f" 量化启用: {status['quantize_enabled']}")
def test_03_warmup_endpoint(self):
"""测试预热端点"""
print("\n开始模型预热(可能需要几分钟)...")
start_time = time.time()
response = self.client.post(
f'{API_BASE_URL}/v1/tts-asr/warmup',
headers=self.headers
)
elapsed = time.time() - start_time
self.assertEqual(response.status_code, 200)
result = response.json()
self.assertIn('tts_warmup', result)
self.assertIn('asr_warmup', result)
self.assertIn('device', result)
print(f"\n预热完成 (耗时: {elapsed:.2f}秒):")
print(f" TTS预热: {'成功' if result['tts_warmup'] else '失败'}")
print(f" ASR预热: {'成功' if result['asr_warmup'] else '失败'}")
# 警告:预热失败不一定是错误(可能模型未下载)
if not result['tts_warmup'] or not result['asr_warmup']:
print("\n⚠ 警告: 预热失败可能是因为模型未下载")
print(" 请确保网络连接正常,或使用已下载的模型")
def test_04_tts_endpoint_basic(self):
"""测试TTS基本功能"""
# 简单的中文文本
test_text = "这是一个测试"
response = self.client.post(
f'{API_BASE_URL}/v1/tts-asr/tts',
headers=self.headers,
json={
'text': test_text,
'voice': 'af_bella',
'rate': 1.0,
'format': 'wav'
}
)
# 检查响应
if response.status_code == 500:
error = response.json()
print(f"\n⚠ TTS失败(可能是模型未加载): {error.get('detail', 'Unknown error')}")
self.skipTest("TTS模型未加载或不可用")
self.assertEqual(response.status_code, 200)
result = response.json()
# 验证响应结构
self.assertIn('audio_base64', result)
self.assertIn('format', result)
self.assertIn('duration_ms', result)
# 验证音频数据
audio_data = base64.b64decode(result['audio_base64'])
self.assertGreater(len(audio_data), 0)
self.assertGreater(result['duration_ms'], 0)
print(f"\nTTS测试成功:")
print(f" 输入文本: {test_text}")
print(f" 音频大小: {len(audio_data)} bytes")
print(f" 时长: {result['duration_ms']} ms")
def test_05_asr_endpoint_basic(self):
"""测试ASR基本功能"""
# 创建一个简单的静音WAV文件(1秒,16kHz,单声道)
sample_rate = 16000
duration = 1.0
samples = int(sample_rate * duration)
# 生成静音数据
import numpy as np
silence = np.zeros(samples, dtype=np.int16)
# 创建WAV文件字节流
import io
import wave
wav_buffer = io.BytesIO()
with wave.open(wav_buffer, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(silence.tobytes())
audio_bytes = wav_buffer.getvalue()
audio_base64 = base64.b64encode(audio_bytes).decode()
# 发送ASR请求
response = self.client.post(
f'{API_BASE_URL}/v1/tts-asr/asr',
headers=self.headers,
json={
'audio_base64': audio_base64,
'language': 'zh-CN'
}
)
# 检查响应
if response.status_code == 500:
error = response.json()
print(f"\n⚠ ASR失败(可能是模型未加载): {error.get('detail', 'Unknown error')}")
self.skipTest("ASR模型未加载或不可用")
self.assertEqual(response.status_code, 200)
result = response.json()
# 验证响应结构
self.assertIn('text', result)
self.assertIn('language', result)
print(f"\nASR测试成功:")
print(f" 识别文本: '{result['text']}'")
print(f" 语言: {result['language']}")
print(f" 注意: 静音音频应该返回空文本")
def test_06_api_key_validation(self):
"""测试API密钥验证"""
# 使用错误的API密钥
wrong_headers = {'X-API-Key': 'wrong-api-key'}
response = self.client.get(
f'{API_BASE_URL}/v1/tts-asr/status',
headers=wrong_headers
)
# 应该返回403 Forbidden
self.assertEqual(response.status_code, 403)
print(f"\n✓ API密钥验证正常:错误密钥被拒绝")
def test_07_tts_long_text(self):
"""测试TTS长文本处理"""
# 较长的文本
long_text = "这是一段较长的测试文本,用于测试TTS系统对长文本的处理能力。" * 3
response = self.client.post(
f'{API_BASE_URL}/v1/tts-asr/tts',
headers=self.headers,
json={
'text': long_text,
'voice': 'af_bella',
'rate': 1.0,
'format': 'wav'
},
timeout=60.0 # 长文本需要更长超时
)
if response.status_code == 500:
self.skipTest("TTS模型未加载或不可用")
self.assertEqual(response.status_code, 200)
result = response.json()
print(f"\n长文本TTS测试成功:")
print(f" 输入长度: {len(long_text)} 字符")
print(f" 音频大小: {len(base64.b64decode(result['audio_base64']))} bytes")
print(f" 时长: {result['duration_ms']} ms")
class PerformanceTest(unittest.TestCase):
"""性能测试"""
@classmethod
def setUpClass(cls):
cls.client = httpx.Client(timeout=TEST_TIMEOUT)
cls.headers = {'X-API-Key': API_KEY}
try:
response = cls.client.get(f'{API_BASE_URL}/v1/tts-asr/status', headers=cls.headers)
cls.service_available = response.status_code == 200
except:
cls.service_available = False
@classmethod
def tearDownClass(cls):
cls.client.close()
def setUp(self):
if not self.service_available:
self.skipTest("后端服务不可用")
def test_tts_latency(self):
"""测试TTS延迟"""
test_text = "测试延迟"
latencies = []
for i in range(3):
start = time.time()
response = self.client.post(
f'{API_BASE_URL}/v1/tts-asr/tts',
headers=self.headers,
json={'text': test_text}
)
elapsed = time.time() - start
if response.status_code == 200:
latencies.append(elapsed)
if latencies:
avg_latency = sum(latencies) / len(latencies)
print(f"\nTTS延迟测试:")
print(f" 平均延迟: {avg_latency:.3f}")
print(f" 最小延迟: {min(latencies):.3f}")
print(f" 最大延迟: {max(latencies):.3f}")
def run_tests(test_type: Optional[str] = None):
"""运行测试"""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
if test_type == 'config':
suite.addTest(TTSASRIntegrationTest('test_01_config_endpoint'))
elif test_type == 'status':
suite.addTest(TTSASRIntegrationTest('test_02_status_endpoint'))
elif test_type == 'warmup':
suite.addTest(TTSASRIntegrationTest('test_03_warmup_endpoint'))
elif test_type == 'tts':
suite.addTest(TTSASRIntegrationTest('test_04_tts_endpoint_basic'))
elif test_type == 'asr':
suite.addTest(TTSASRIntegrationTest('test_05_asr_endpoint_basic'))
elif test_type == 'perf':
suite.addTests(loader.loadTestsFromTestCase(PerformanceTest))
else:
# 运行所有测试
suite.addTests(loader.loadTestsFromTestCase(TTSASRIntegrationTest))
suite.addTests(loader.loadTestsFromTestCase(PerformanceTest))
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
return result.wasSuccessful()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='TTS/ASR集成测试')
parser.add_argument('--test', choices=[
'config', 'status', 'warmup', 'tts', 'asr', 'perf'
], help='运行特定测试')
parser.add_argument('--url', default=API_BASE_URL, help='API基础URL')
parser.add_argument('--key', default=API_KEY, help='API密钥')
args = parser.parse_args()
# 更新配置
API_BASE_URL = args.url
API_KEY = args.key
print("=" * 70)
print("TTS/ASR 集成测试")
print("=" * 70)
print(f"API URL: {API_BASE_URL}")
print(f"测试类型: {args.test or '全部'}")
print("=" * 70)
success = run_tests(args.test)
sys.exit(0 if success else 1)
+373
View File
@@ -0,0 +1,373 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TTS/ASR模块单元测试
测试核心功能,无需实际运行模型
运行方式:
pytest backend/tests/test_tts_asr_unit.py -v
python backend/tests/test_tts_asr_unit.py
"""
import os
import sys
import unittest
from unittest.mock import Mock, MagicMock, patch
import tempfile
import numpy as np
# 确保可以导入backend和tts_asr模块
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
class TestAppleSiliconDetection(unittest.TestCase):
"""测试Apple Silicon检测功能"""
def test_is_apple_silicon_on_darwin_arm64(self):
"""测试在Darwin/arm64环境下检测Apple Silicon"""
with patch('platform.system', return_value='Darwin'), \
patch('platform.machine', return_value='arm64'):
# 需要重新导入以应用mock
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import _is_apple_silicon
self.assertTrue(_is_apple_silicon())
def test_is_apple_silicon_on_windows(self):
"""测试在Windows环境下不是Apple Silicon"""
with patch('platform.system', return_value='Windows'), \
patch('platform.machine', return_value='AMD64'):
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import _is_apple_silicon
self.assertFalse(_is_apple_silicon())
def test_is_apple_silicon_on_linux(self):
"""测试在Linux环境下不是Apple Silicon"""
with patch('platform.system', return_value='Linux'), \
patch('platform.machine', return_value='x86_64'):
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import _is_apple_silicon
self.assertFalse(_is_apple_silicon())
class TestEnvironmentVariables(unittest.TestCase):
"""测试环境变量解析"""
def test_default_environment_values(self):
"""测试默认环境变量值"""
# 清除可能存在的环境变量
env_vars = [
'TTS_ASR_DEVICE', 'TTS_ASR_MODEL_SIZE', 'TTS_ASR_QUANTIZE',
'TTS_ASR_OFFLINE_MODE', 'TTS_ASR_WARMUP', 'TTS_ASR_WARMUP_TIMEOUT',
'TTS_ASR_IDLE_TIMEOUT', 'TTS_ASR_MPS_MEMORY_LIMIT_MB'
]
# 保存原始值
original_values = {}
for var in env_vars:
original_values[var] = os.environ.get(var)
if var in os.environ:
del os.environ[var]
try:
# 重新加载模块以应用默认值
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import (
TTS_ASR_DEVICE, TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE,
TTS_ASR_OFFLINE_MODE, TTS_ASR_WARMUP, TTS_ASR_WARMUP_TIMEOUT,
TTS_ASR_IDLE_TIMEOUT, TTS_ASR_MPS_MEMORY_LIMIT_MB
)
self.assertEqual(TTS_ASR_DEVICE, 'auto')
self.assertEqual(TTS_ASR_MODEL_SIZE, 'auto')
self.assertFalse(TTS_ASR_QUANTIZE)
self.assertFalse(TTS_ASR_OFFLINE_MODE)
self.assertTrue(TTS_ASR_WARMUP)
self.assertEqual(TTS_ASR_WARMUP_TIMEOUT, 120)
self.assertEqual(TTS_ASR_IDLE_TIMEOUT, 0)
self.assertEqual(TTS_ASR_MPS_MEMORY_LIMIT_MB, 8192)
finally:
# 恢复原始值
for var, value in original_values.items():
if value is not None:
os.environ[var] = value
elif var in os.environ:
del os.environ[var]
def test_custom_environment_values(self):
"""测试自定义环境变量值"""
os.environ['TTS_ASR_DEVICE'] = 'cpu'
os.environ['TTS_ASR_MODEL_SIZE'] = 'small'
os.environ['TTS_ASR_QUANTIZE'] = 'true'
os.environ['TTS_ASR_OFFLINE_MODE'] = 'true'
try:
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import (
TTS_ASR_DEVICE, TTS_ASR_MODEL_SIZE, TTS_ASR_QUANTIZE,
TTS_ASR_OFFLINE_MODE
)
self.assertEqual(TTS_ASR_DEVICE, 'cpu')
self.assertEqual(TTS_ASR_MODEL_SIZE, 'small')
self.assertTrue(TTS_ASR_QUANTIZE)
self.assertTrue(TTS_ASR_OFFLINE_MODE)
finally:
# 清理环境变量
for var in ['TTS_ASR_DEVICE', 'TTS_ASR_MODEL_SIZE',
'TTS_ASR_QUANTIZE', 'TTS_ASR_OFFLINE_MODE']:
if var in os.environ:
del os.environ[var]
class TestModelSizeSelection(unittest.TestCase):
"""测试模型大小选择逻辑"""
def test_whisper_model_sizes_mapping(self):
"""测试Whisper模型大小映射"""
from backend.tts_asr import WHISPER_MODEL_SIZES
expected_sizes = ['tiny', 'base', 'small', 'medium', 'large', 'turbo']
self.assertEqual(list(WHISPER_MODEL_SIZES.keys()), expected_sizes)
# 验证模型ID格式
for size, model_id in WHISPER_MODEL_SIZES.items():
self.assertTrue(model_id.startswith('openai/whisper'))
self.assertIn(size, model_id)
def test_recommended_model_size_explicit(self):
"""测试显式指定的模型大小"""
os.environ['TTS_ASR_MODEL_SIZE'] = 'medium'
try:
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import _get_recommended_model_size
size = _get_recommended_model_size()
self.assertEqual(size, 'medium')
finally:
if 'TTS_ASR_MODEL_SIZE' in os.environ:
del os.environ['TTS_ASR_MODEL_SIZE']
def test_invalid_model_size_falls_back(self):
"""测试无效模型大小回退到自动选择"""
os.environ['TTS_ASR_MODEL_SIZE'] = 'invalid_size'
try:
import importlib
import backend.tts_asr as tts_asr_module
importlib.reload(tts_asr_module)
from backend.tts_asr import _get_recommended_model_size, WHISPER_MODEL_SIZES
# 应该回退到推荐大小而不崩溃
size = _get_recommended_model_size()
self.assertIn(size, WHISPER_MODEL_SIZES.keys())
finally:
if 'TTS_ASR_MODEL_SIZE' in os.environ:
del os.environ['TTS_ASR_MODEL_SIZE']
class TestAudioValidation(unittest.TestCase):
"""测试音频验证功能"""
def test_validate_empty_audio(self):
"""测试空音频数据验证"""
from backend.tts_asr import _validate_audio_data
self.assertFalse(_validate_audio_data(b''))
self.assertFalse(_validate_audio_data(b'short'))
def test_validate_valid_wav_header(self):
"""测试有效WAV头部验证"""
from backend.tts_asr import _validate_audio_data
# 创建一个最小的有效WAV头部(44字节)
valid_wav_header = b'RIFF' + b'\x00' * 40
self.assertTrue(_validate_audio_data(valid_wav_header))
def test_validate_invalid_audio(self):
"""测试无效音频数据验证"""
from backend.tts_asr import _validate_audio_data
# 小于最小WAV头部大小
invalid_audio = b'RIFF' + b'\x00' * 30
self.assertFalse(_validate_audio_data(invalid_audio))
class TestAudioResampling(unittest.TestCase):
"""测试音频重采样功能"""
def test_resample_same_rate(self):
"""测试相同采样率(无需重采样)"""
from backend.tts_asr import _resample_audio_robust
audio = np.random.randn(16000).astype(np.float32)
resampled = _resample_audio_robust(audio, 16000, 16000)
# 应该返回原始音频
np.testing.assert_array_almost_equal(audio, resampled)
def test_resample_different_rate(self):
"""测试不同采样率重采样"""
from backend.tts_asr import _resample_audio_robust
# 创建1秒的音频,从16kHz重采样到48kHz
audio_16k = np.sin(np.linspace(0, 2*np.pi, 16000)).astype(np.float32)
audio_48k = _resample_audio_robust(audio_16k, 16000, 48000)
# 检查长度变化
expected_length = int(len(audio_16k) * 48000 / 16000)
self.assertEqual(len(audio_48k), expected_length)
def test_resample_downsample(self):
"""测试下采样"""
from backend.tts_asr import _resample_audio_robust
# 从48kHz下采样到16kHz
audio_48k = np.sin(np.linspace(0, 2*np.pi, 48000)).astype(np.float32)
audio_16k = _resample_audio_robust(audio_48k, 48000, 16000)
expected_length = int(len(audio_48k) * 16000 / 48000)
self.assertEqual(len(audio_16k), expected_length)
class TestDeviceCapabilities(unittest.TestCase):
"""测试设备能力检测"""
def test_device_capabilities_dataclass(self):
"""测试DeviceCapabilities数据类"""
from backend.tts_asr import DeviceCapabilities
caps = DeviceCapabilities(
device='cpu',
mps_available=False,
cuda_available=False
)
self.assertEqual(caps.device, 'cpu')
self.assertFalse(caps.mps_available)
self.assertFalse(caps.cuda_available)
self.assertEqual(caps.recommended_model_size, 'large') # 默认值
def test_device_capabilities_with_mps(self):
"""测试MPS设备能力"""
from backend.tts_asr import DeviceCapabilities
caps = DeviceCapabilities(
device='mps',
mps_available=True,
mps_memory_limit_mb=8192,
recommended_model_size='small'
)
self.assertEqual(caps.device, 'mps')
self.assertTrue(caps.mps_available)
self.assertEqual(caps.mps_memory_limit_mb, 8192)
self.assertEqual(caps.recommended_model_size, 'small')
class TestModelCacheCheck(unittest.TestCase):
"""测试模型缓存检查"""
@patch('backend.tts_asr.TTS_ASR_OFFLINE_MODE', False)
def test_cache_check_non_offline_mode(self):
"""测试非离线模式下缓存检查总是返回True"""
from backend.tts_asr import _check_model_cached
# 非离线模式应该总是返回True
result = _check_model_cached('any/model')
self.assertTrue(result)
@patch('backend.tts_asr.TTS_ASR_OFFLINE_MODE', True)
def test_cache_check_offline_mode_missing(self):
"""测试离线模式下缺失模型的处理"""
from backend.tts_asr import _check_model_cached
# 模拟缓存路径
with patch('huggingface_hub.constants.HF_HUB_CACHE', '/nonexistent/path'):
result = _check_model_cached('nonexistent/model')
# 应该返回False(模型未缓存)
self.assertFalse(result)
class TestRequestResponseModels(unittest.TestCase):
"""测试请求/响应数据模型"""
def test_tts_request_model(self):
"""测试TTS请求模型"""
from backend.tts_asr import TTSRequest
req = TTSRequest(text="测试文本")
self.assertEqual(req.text, "测试文本")
self.assertEqual(req.voice, "af_bella") # 默认值
self.assertEqual(req.rate, 1.0) # 默认值
self.assertEqual(req.format, "wav") # 默认值
def test_asr_request_model(self):
"""测试ASR请求模型"""
from backend.tts_asr import ASRRequest
req = ASRRequest(audio_base64="dGVzdA==")
self.assertEqual(req.audio_base64, "dGVzdA==")
self.assertEqual(req.language, "zh-CN") # 默认值
def test_model_status_model(self):
"""测试ModelStatus模型"""
from backend.tts_asr import ModelStatus
status = ModelStatus(
tts_loaded=False,
asr_loaded=False,
device='cpu'
)
self.assertFalse(status.tts_loaded)
self.assertFalse(status.asr_loaded)
self.assertEqual(status.device, 'cpu')
self.assertIsNone(status.tts_last_used)
self.assertIsNone(status.asr_last_used)
def run_tests():
"""运行所有测试"""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# 添加所有测试类
suite.addTests(loader.loadTestsFromTestCase(TestAppleSiliconDetection))
suite.addTests(loader.loadTestsFromTestCase(TestEnvironmentVariables))
suite.addTests(loader.loadTestsFromTestCase(TestModelSizeSelection))
suite.addTests(loader.loadTestsFromTestCase(TestAudioValidation))
suite.addTests(loader.loadTestsFromTestCase(TestAudioResampling))
suite.addTests(loader.loadTestsFromTestCase(TestDeviceCapabilities))
suite.addTests(loader.loadTestsFromTestCase(TestModelCacheCheck))
suite.addTests(loader.loadTestsFromTestCase(TestRequestResponseModels))
# 运行测试
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
return result.wasSuccessful()
if __name__ == '__main__':
# 直接运行时执行测试
success = run_tests()
sys.exit(0 if success else 1)
+56
View File
@@ -0,0 +1,56 @@
import asyncio
import base64
import os
import sys
# 确保能找到backend模块
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from backend.tts_asr import _tts_sync_with_retry, _load_asr_pipeline_with_retry, _get_asr_pipeline
async def verify_tts_asr_cross():
print("====================================")
print(" 开始严格交叉验证: TTS 生成 -> ASR 解析")
print("====================================")
test_text = "苹果设备支持离线大模型运算"
print(f"\n[1] 正在调用 TTS 引擎 (suno/bark-small)...")
print(f"目标文本: '{test_text}'")
try:
# TTS生成
audio_bytes, sr = await _tts_sync_with_retry(test_text, rate=1.0)
print(f"-> TTS 成功生成音频数据,大小: {len(audio_bytes)} Bytes, 采样率: {sr}Hz")
except Exception as e:
print(f"-> TTS 失败: {str(e)}")
sys.exit(1)
print("\n[2] 正在调用 ASR 引擎 (Whisper)...")
try:
loaded = await _load_asr_pipeline_with_retry()
if not loaded:
print("-> ASR 核心加载失败!")
sys.exit(1)
print("-> ASR 加载成功,开始解析音频...")
# 将生成的wav bytes传递给ASR进行语音识别
asr_pipeline = _get_asr_pipeline()
result = asr_pipeline(audio_bytes, generate_kwargs={"task": "transcribe"})
recognized_text = result.get('text', '')
print(f"-> ASR 识别结果: '{recognized_text.strip()}'")
if len(recognized_text.strip()) > 0:
print("\n结论: ✅ 验证成功!TTS和ASR模块功能链路闭环完成。")
else:
print("\n结论: ❌ ASR输出为空字符,闭环失败。")
sys.exit(1)
except Exception as e:
import traceback
traceback.print_exc()
print(f"-> ASR 分析阶段失败: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(verify_tts_asr_cross())
+1155
View File
File diff suppressed because it is too large Load Diff
+6792 -871
View File
File diff suppressed because it is too large Load Diff
+13 -1
View File
@@ -6,16 +6,27 @@
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"preview": "vite preview" "preview": "vite preview",
"test": "echo 'No tests configured yet'",
"check": "npm run build"
}, },
"dependencies": { "dependencies": {
"@blocknote/xl-docx-exporter": "^0.47.3",
"@milkdown/core": "^7.18.0", "@milkdown/core": "^7.18.0",
"@milkdown/crepe": "^7.18.0", "@milkdown/crepe": "^7.18.0",
"@milkdown/kit": "^7.18.0", "@milkdown/kit": "^7.18.0",
"@milkdown/theme-nord": "^7.18.0", "@milkdown/theme-nord": "^7.18.0",
"@milkdown/vue": "^7.18.0", "@milkdown/vue": "^7.18.0",
"@univerjs/preset-docs-core": "^0.20.0",
"@univerjs/preset-sheets-core": "^0.20.0",
"@univerjs/presets": "^0.20.0",
"@univerjs/slides": "^0.20.0",
"@univerjs/slides-ui": "^0.20.0",
"docx": "^9.6.0", "docx": "^9.6.0",
"docx-preview": "^0.3.7",
"docx2pdf-converter": "^2.1.1",
"html2pdf.js": "^0.14.0", "html2pdf.js": "^0.14.0",
"jspdf": "^4.2.1",
"katex": "^0.16.9", "katex": "^0.16.9",
"markdown-it": "^13.0.0", "markdown-it": "^13.0.0",
"markdown-it-math": "^3.0.2", "markdown-it-math": "^3.0.2",
@@ -27,6 +38,7 @@
}, },
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue": "^6.0.1", "@vitejs/plugin-vue": "^6.0.1",
"@vue/language-server": "^3.2.6",
"vite": "^7.2.4" "vite": "^7.2.4"
} }
} }
+3 -21
View File
@@ -1,19 +1,10 @@
<script setup> <script setup>
import { computed, defineAsyncComponent, ref } from 'vue' import { computed } from 'vue'
import { useSettingsStore } from './stores/settings' import { useSettingsStore } from './stores/settings'
import SettingsPanel from './components/SettingsPanel.vue' import SettingsPanel from './components/SettingsPanel.vue'
const MilkdownEditor = defineAsyncComponent(() => import('./components/MilkdownEditor.vue'))
const markdown = ref('')
const emit = defineEmits(['update:markdown'])
const settings = useSettingsStore() const settings = useSettingsStore()
function onChange(markdownValue) {
markdown.value = markdownValue
emit('update:markdown', markdownValue)
}
const appStyle = computed(() => { const appStyle = computed(() => {
const style = {} const style = {}
if (settings.backgroundType === 'warm') { if (settings.backgroundType === 'warm') {
@@ -52,9 +43,7 @@ const backgroundStyle = computed(() => {
<div class="app-shell" :style="appStyle"> <div class="app-shell" :style="appStyle">
<div v-if="settings.backgroundType === 'image'" class="app-bg-layer" :style="backgroundStyle"></div> <div v-if="settings.backgroundType === 'image'" class="app-bg-layer" :style="backgroundStyle"></div>
<div class="editor-container"> <router-view />
<MilkdownEditor @update:markdown="onChange" />
</div>
<SettingsPanel /> <SettingsPanel />
</div> </div>
@@ -64,17 +53,10 @@ const backgroundStyle = computed(() => {
.app-shell { .app-shell {
position: relative; position: relative;
width: 100%; width: 100%;
height: 100%; height: 100vh;
background: var(--app-bg); background: var(--app-bg);
color: var(--app-text); color: var(--app-text);
transition: background 0.3s, color 0.3s; transition: background 0.3s, color 0.3s;
isolation: isolate; isolation: isolate;
} }
.editor-container {
position: relative;
z-index: 1;
height: 100%;
}
</style> </style>
+164
View File
@@ -0,0 +1,164 @@
<script setup>
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
const props = defineProps({
visible: { type: Boolean, default: false },
x: { type: Number, default: 0 },
y: { type: Number, default: 0 },
node: { type: Object, default: null },
canPaste: { type: Boolean, default: false }
})
const emit = defineEmits(['close', 'rename', 'delete', 'copy', 'cut', 'paste', 'new-file', 'new-folder'])
const menuRef = ref(null)
const menuStyle = ref({})
function handleClickOutside(event) {
if (menuRef.value && !menuRef.value.contains(event.target)) {
emit('close')
}
}
function handleKeydown(event) {
if (event.key === 'Escape') {
emit('close')
}
}
watch(() => props.visible, async (val) => {
if (val) {
await nextTick()
const menu = menuRef.value
if (menu) {
const rect = menu.getBoundingClientRect()
const vw = window.innerWidth
const vh = window.innerHeight
menuStyle.value = {
left: props.x + rect.width > vw ? vw - rect.width - 8 : props.x,
top: props.y + rect.height > vh ? vh - rect.height - 8 : props.y
}
}
}
})
onMounted(() => {
document.addEventListener('mousedown', handleClickOutside)
document.addEventListener('keydown', handleKeydown)
})
onUnmounted(() => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keydown', handleKeydown)
})
</script>
<template>
<Teleport to="body">
<div
v-if="visible"
ref="menuRef"
class="context-menu"
:style="{ left: `${menuStyle.left || x}px`, top: `${menuStyle.top || y}px` }"
>
<template v-if="node">
<button v-if="node.type === 'folder'" class="context-menu-item" @click="emit('new-file', node.id)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M3.75 1.5a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V4.664a.25.25 0 00-.073-.177l-2.914-2.914a.25.25 0 00-.177-.073H3.75zM3 1.75C3 .784 3.784 0 4.75 0h5.339c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0113 16H4.75A1.75 1.75 0 013 14.25V1.75z"/><path d="M8.5 4V1.5H10a.5.5 0 01.5.5v1.5a.5.5 0 01-.5.5H9a.5.5 0 01-.5-.5zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
新建文件
</button>
<button v-if="node.type === 'folder'" class="context-menu-item" @click="emit('new-folder', node.id)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M.5 2.5A1.5 1.5 0 012 1h3.5a.5.5 0 01.354.146l1.5 1.5a.5.5 0 00.354.146H13a1.5 1.5 0 011.5 1.5v7.5a1.5 1.5 0 01-1.5 1.5H2a1.5 1.5 0 01-1.5-1.5v-7.5zM6 2v1.5h4.5V2H6zm-2 5a.5.5 0 01.5-.5h5a.5.5 0 010 1h-5a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h5a.5.5 0 000-1h-5z"/></svg>
新建文件夹
</button>
<div v-if="node.type === 'folder'" class="context-menu-divider"></div>
<button class="context-menu-item" @click="emit('copy', node.id)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25zM5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25z"/></svg>
复制
</button>
<button class="context-menu-item" @click="emit('cut', node.id)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M4.455.752a1.523 1.523 0 012.173.043l2.84 3.076a3.052 3.052 0 01.524.669h3.258a.75.75 0 01.643 1.137l-2.55 4.25a.75.75 0 01-1.286-.784L11.5 6.75h-2.5a3.052 3.052 0 01-.524.669L5.632 10.49a1.523 1.523 0 01-2.173.043l-.93-.93a.75.75 0 111.06-1.06l.93.93 2.845-3.076a1.55 1.55 0 000-2.134L4.52 1.683l-.93.93a.75.75 0 01-1.06-1.06l.93-.93.995.995z"/></svg>
剪切
</button>
<button v-if="canPaste" class="context-menu-item" @click="emit('paste', node.type === 'folder' ? node.id : null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M4.75 1.5a.25.25 0 00-.25.25v.59c0 .396.316.717.707.717h5.586c.39 0 .707-.32.707-.716v-.591a.25.25 0 00-.25-.25H4.75zm6.543-.75a1.75 1.75 0 011.75 1.75v.59c0 .396-.107.767-.293 1.086l1.293 1.293a.75.75 0 010 1.061l-1.293 1.293c.186.32.293.69.293 1.087v.59a1.75 1.75 0 01-1.75 1.75H4.75a1.75 1.75 0 01-1.75-1.75v-.59c0-.396.107-.767.293-1.087L2 5.53a.75.75 0 010-1.06l1.293-1.294A2.048 2.048 0 013 2.09v-.59A1.75 1.75 0 014.75 0h6.543zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
粘贴
</button>
<div class="context-menu-divider"></div>
<button class="context-menu-item danger" @click="emit('delete', node.id)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M6.5 1.75a.25.25 0 01.25-.25h2.5a.25.25 0 01.25.25V3h-3V1.75zm4.5 0V3h2.25a.75.75 0 010 1.5H2.75a.75.75 0 010-1.5H5V1.75C5 .784 5.784 0 6.75 0h2.5C10.216 0 11 .784 11 1.75zM4.496 6.675a.75.75 0 10-1.492.15l.66 6.6A1.75 1.75 0 005.41 15h5.18a1.75 1.75 0 001.746-1.578l.66-6.6a.75.75 0 00-1.492-.149l-.66 6.6a.25.25 0 01-.249.227H5.41a.25.25 0 01-.249-.227l-.66-6.6z"/></svg>
删除
</button>
</template>
<template v-else>
<button class="context-menu-item" @click="emit('new-file', null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M3.75 1.5a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V4.664a.25.25 0 00-.073-.177l-2.914-2.914a.25.25 0 00-.177-.073H3.75zM3 1.75C3 .784 3.784 0 4.75 0h5.339c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0113 16H4.75A1.75 1.75 0 013 14.25V1.75z"/><path d="M8.5 4V1.5H10a.5.5 0 01.5.5v1.5a.5.5 0 01-.5.5H9a.5.5 0 01-.5-.5zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
新建文件
</button>
<button class="context-menu-item" @click="emit('new-folder', null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M.5 2.5A1.5 1.5 0 012 1h3.5a.5.5 0 01.354.146l1.5 1.5a.5.5 0 00.354.146H13a1.5 1.5 0 011.5 1.5v7.5a1.5 1.5 0 01-1.5 1.5H2a1.5 1.5 0 01-1.5-1.5v-7.5zM6 2v1.5h4.5V2H6zm-2 5a.5.5 0 01.5-.5h5a.5.5 0 010 1h-5a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h5a.5.5 0 000-1h-5z"/></svg>
新建文件夹
</button>
<button v-if="canPaste" class="context-menu-item" @click="emit('paste', null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M4.75 1.5a.25.25 0 00-.25.25v.59c0 .396.316.717.707.717h5.586c.39 0 .707-.32.707-.716v-.591a.25.25 0 00-.25-.25H4.75zm6.543-.75a1.75 1.75 0 011.75 1.75v.59c0 .396-.107.767-.293 1.086l1.293 1.293a.75.75 0 010 1.061l-1.293 1.293c.186.32.293.69.293 1.087v.59a1.75 1.75 0 01-1.75 1.75H4.75a1.75 1.75 0 01-1.75-1.75v-.59c0-.396.107-.767.293-1.087L2 5.53a.75.75 0 010-1.06l1.293-1.294A2.048 2.048 0 013 2.09v-.59A1.75 1.75 0 014.75 0h6.543zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
粘贴
</button>
</template>
</div>
</Teleport>
</template>
<style scoped>
.context-menu {
position: fixed;
z-index: 100000;
min-width: 200px;
background: var(--panel-bg);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: 8px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
padding: 4px;
animation: contextMenuIn 0.12s ease-out;
}
@keyframes contextMenuIn {
from { opacity: 0; transform: scale(0.95); }
to { opacity: 1; transform: scale(1); }
}
.context-menu-item {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
padding: 6px 10px;
border: none;
background: none;
color: var(--app-text);
font-size: 13px;
cursor: pointer;
border-radius: 4px;
text-align: left;
}
.context-menu-item:hover {
background: var(--focus-ring);
color: #fff;
}
.context-menu-item.danger {
color: var(--danger-text);
}
.context-menu-item.danger:hover {
background: var(--danger-text);
color: #fff;
}
.context-menu-divider {
height: 1px;
background: var(--panel-border);
margin: 4px 0;
}
</style>
+345
View File
@@ -0,0 +1,345 @@
<template>
<section class="doc-card" :class="{ 'is-collapsed': collapsedState }">
<header class="doc-card__header">
<div class="doc-card__badge">{{ typeLabel }}</div>
<div class="doc-card__meta">
<div class="doc-card__name">{{ docName }}</div>
<div class="doc-card__time">{{ displayTime }}</div>
</div>
<div class="doc-card__actions">
<button type="button" class="doc-card__btn" :title="collapsedState ? '展开文件' : '折叠文件'" @click="toggleCollapse">
<svg v-if="collapsedState" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="9 18 15 12 9 6"/>
</svg>
<svg v-else width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="6 9 12 15 18 9"/>
</svg>
</button>
<button type="button" class="doc-card__btn doc-card__btn--danger" title="删除文件" @click="props.onDelete?.()">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M3 6h18"/>
<path d="M8 6V4h8v2"/>
<path d="M19 6l-1 14H6L5 6"/>
<path d="M10 11v6"/>
<path d="M14 11v6"/>
</svg>
</button>
</div>
</header>
<div v-show="!collapsedState" class="doc-card__body">
<div ref="editorRoot" class="doc-card__editor"></div>
</div>
</section>
</template>
<script setup>
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { replaceAll } from '@milkdown/kit/utils'
import { Crepe } from '@milkdown/crepe'
import { editorViewCtx } from '@milkdown/kit/core'
import { copilotPlugin, copilotConfigCtx, copilotGhostMark, setCopilotEnabled, clearGhostSuggestion } from '../plugins/copilotPlugin'
import { fetchSuggestion } from '../utils/api.js'
const props = defineProps({
docType: { type: String, default: 'txt' },
docName: { type: String, default: 'document.txt' },
uploadTime: { type: String, default: '' },
content: { type: String, default: '' },
collapsed: { type: Boolean, default: false },
resolveSuggestionRequest: { type: Function, default: null },
onUpdateContent: { type: Function, default: null },
onUpdateCollapsed: { type: Function, default: null },
onDelete: { type: Function, default: null },
})
const editorRoot = ref(null)
const collapsedState = ref(Boolean(props.collapsed))
const currentContent = ref(props.content || '')
let crepe = null
let syncTimer = null
let syncingExternal = false
const typeLabel = computed(() => {
if (props.docType === 'docx') return 'DOCX'
if (props.docType === 'pptx') return 'PPTX'
if (props.docType === 'pdf') return 'PDF'
return 'TXT'
})
const displayTime = computed(() => {
if (!props.uploadTime) return '刚上传'
const date = new Date(props.uploadTime)
if (Number.isNaN(date.getTime())) return '刚上传'
return date.toLocaleString('zh-CN', { hour12: false })
})
const toggleCollapse = () => {
collapsedState.value = !collapsedState.value
props.onUpdateCollapsed?.(collapsedState.value)
}
const syncContent = () => {
if (!crepe) return
if (syncTimer) clearTimeout(syncTimer)
syncTimer = setTimeout(async () => {
if (!crepe || syncingExternal) return
const markdown = await crepe.getMarkdown()
currentContent.value = markdown
props.onUpdateContent?.(markdown)
}, 120)
}
const syncExternalContent = async (nextValue) => {
const value = nextValue || ''
if (!crepe) {
currentContent.value = value
return
}
if (value === currentContent.value) return
syncingExternal = true
try {
crepe.editor.action(replaceAll(value))
currentContent.value = value
} finally {
syncingExternal = false
}
}
watch(() => props.content, (nextValue) => {
void syncExternalContent(nextValue)
})
watch(() => props.collapsed, (nextValue) => {
collapsedState.value = Boolean(nextValue)
})
onMounted(async () => {
if (!editorRoot.value) return
crepe = new Crepe({
root: editorRoot.value,
defaultValue: props.content || '',
features: {
[Crepe.Feature.Latex]: true,
[Crepe.Feature.ImageBlock]: true,
[Crepe.Feature.Table]: true,
[Crepe.Feature.ListCheck]: true,
},
config: {
showLineNumber: false,
},
})
crepe.editor.config((ctx) => {
ctx.set(copilotConfigCtx.key, {
fetchSuggestion: async (prefix, suffix, languageId, signal) => {
const payload = props.resolveSuggestionRequest
? await props.resolveSuggestionRequest({ prefix, suffix, languageId })
: { prefix, suffix, languageId, blocked: false }
if (payload?.blocked) return ''
return fetchSuggestion(payload?.prefix ?? prefix, payload?.suffix ?? suffix, payload?.languageId ?? languageId, signal)
},
debounceMs: 900,
})
})
crepe.editor.use(copilotConfigCtx)
crepe.editor.use(copilotGhostMark)
crepe.editor.use(copilotPlugin)
await crepe.create()
crepe.on((listener) => {
listener.updated(() => {
syncContent()
})
})
crepe.editor.action((ctx) => {
const view = ctx.get(editorViewCtx)
setCopilotEnabled(view, true)
})
})
onUnmounted(() => {
if (syncTimer) {
clearTimeout(syncTimer)
syncTimer = null
}
if (crepe) {
crepe.editor.action((ctx) => {
const view = ctx.get(editorViewCtx)
clearGhostSuggestion(view)
})
crepe.destroy()
crepe = null
}
})
</script>
<style scoped>
.doc-card {
width: 100%;
max-width: 100%;
margin: 8px 0;
border-radius: 12px;
border: 1px solid rgba(59, 130, 246, 0.12);
background: rgba(255, 255, 255, 0.8);
box-shadow: 0 2px 8px rgba(59, 130, 246, 0.06), 0 1px 3px rgba(0, 0, 0, 0.04);
overflow: hidden;
backdrop-filter: blur(10px);
position: relative;
}
:root[data-theme='dark'] .doc-card {
background: rgba(26, 30, 39, 0.8);
border-color: rgba(96, 165, 250, 0.15);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.2);
}
.doc-card__header {
display: grid;
grid-template-columns: auto minmax(0, 1fr) auto;
gap: 10px;
align-items: center;
padding: 8px 12px;
border-bottom: 1px solid rgba(59, 130, 246, 0.1);
background: rgba(255, 255, 255, 0.8);
}
:root[data-theme='dark'] .doc-card__header {
background: rgba(26, 30, 39, 0.8);
border-bottom-color: rgba(96, 165, 250, 0.15);
}
.doc-card__badge {
min-width: 48px;
padding: 4px 10px;
border-radius: 999px;
background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%);
color: #fff;
font-size: 10px;
font-weight: 600;
letter-spacing: 0.08em;
text-align: center;
box-shadow: 0 2px 6px rgba(59, 130, 246, 0.2);
}
.doc-card__meta {
min-width: 0;
}
.doc-card__name {
color: #1e293b;
font-size: 13px;
font-weight: 600;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
:root[data-theme='dark'] .doc-card__name {
color: #e5e7eb;
}
.doc-card__time {
margin-top: 2px;
color: #64748b;
font-size: 10px;
}
:root[data-theme='dark'] .doc-card__time {
color: #aeb6c5;
}
.doc-card__actions {
display: flex;
gap: 4px;
}
.doc-card__btn {
width: 26px;
height: 26px;
border: 1px solid rgba(59, 130, 246, 0.12);
border-radius: 8px;
background: rgba(255, 255, 255, 0.5);
color: #64748b;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.15s ease;
}
:root[data-theme='dark'] .doc-card__btn {
background: rgba(34, 40, 52, 0.5);
border-color: rgba(96, 165, 250, 0.15);
color: #aeb6c5;
}
.doc-card__btn:hover {
background: rgba(59, 130, 246, 0.1);
border-color: rgba(59, 130, 246, 0.25);
color: #3b82f6;
}
.doc-card__btn--danger:hover {
background: rgba(239, 68, 68, 0.1);
border-color: rgba(239, 68, 68, 0.2);
color: #ef4444;
}
.doc-card__body {
padding: 8px 10px;
background: rgba(248, 250, 252, 0.8);
}
:root[data-theme='dark'] .doc-card__body {
background: rgba(18, 22, 30, 0.8);
}
.doc-card__editor {
min-height: 48px;
border-radius: 8px;
border: 1px solid rgba(59, 130, 246, 0.08);
background: rgba(255, 255, 255, 0.8);
overflow: hidden;
}
:root[data-theme='dark'] .doc-card__editor {
background: rgba(26, 30, 39, 0.8);
border-color: rgba(96, 165, 250, 0.12);
}
.doc-card__editor :deep(.milkdown) {
background: transparent !important;
}
.doc-card__editor :deep(.milkdown__main),
.doc-card__editor :deep(.milkdown__editor) {
margin: 0 !important;
padding: 0 !important;
}
.doc-card__editor :deep(.ProseMirror) {
min-height: 0;
padding: 10px 12px 12px !important;
font-size: 13px !important;
line-height: 1.6;
}
.doc-card__editor :deep(.ProseMirror > *:last-child) {
margin-bottom: 0;
}
.doc-card__editor :deep(.ProseMirror p:first-child) {
margin-top: 0;
}
.doc-card__editor :deep(.milkdown__toolbar),
.doc-card__editor :deep(.milkdown__menu),
.doc-card__editor :deep(.milkdown__statusbar),
.doc-card__editor :deep(.milkdown-slate-toolbar),
.doc-card__editor :deep(.milkdown-bubble-menu) {
display: none !important;
}
</style>
+216
View File
@@ -0,0 +1,216 @@
<template>
<div class="doc-block" :class="{ collapsed: isCollapsed }">
<!-- 深色条文件头 -->
<div class="doc-header">
<!-- 最左边文件类型icon -->
<div class="doc-icon">
<!-- PDF icon -->
<svg v-if="docType === 'pdf'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
<polyline points="14 2 14 8 20 8"/>
<path d="M9 15v-2h6v2"/>
<path d="M12 13v4"/>
</svg>
<!-- Word icon -->
<svg v-else-if="docType === 'doc'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
<polyline points="14 2 14 8 20 8"/>
<path d="M16 13H8"/>
<path d="M16 17H8"/>
<path d="M10 9H8"/>
</svg>
<!-- PPT icon -->
<svg v-else-if="docType === 'ppt'" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="2" y="3" width="20" height="14" rx="2"/>
<path d="M8 21h8"/>
<path d="M12 17v4"/>
</svg>
<!-- TXT icon -->
<svg v-else width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
<polyline points="14 2 14 8 20 8"/>
<path d="M16 13H8"/>
<path d="M16 17H8"/>
</svg>
</div>
<!-- 中间文件名 -->
<div class="doc-name">{{ docName }}</div>
<!-- 最右边下载按钮 + 折叠按钮 -->
<div class="doc-actions">
<button @click="downloadDoc" class="action-btn" title="下载文档">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="7 10 12 15 17 10"/>
<line x1="12" y1="15" x2="12" y2="3"/>
</svg>
</button>
<button @click="toggleCollapse" class="action-btn collapse-btn" :title="isCollapsed ? '展开' : '折叠'">
<svg v-if="isCollapsed" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="9 18 15 12 9 6"/>
</svg>
<svg v-else width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="6 9 12 15 18 9"/>
</svg>
</button>
</div>
</div>
<!-- 浅色块文档内容非折叠状态显示 -->
<div class="doc-content" v-show="!isCollapsed">
<pre>{{ content }}</pre>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
docType: {
type: String,
default: 'text'
},
docName: {
type: String,
default: 'document.txt'
},
uploadTime: {
type: String,
default: ''
},
content: {
type: String,
default: ''
}
})
const isCollapsed = ref(false)
const toggleCollapse = () => {
isCollapsed.value = !isCollapsed.value
}
const downloadDoc = () => {
const blob = new Blob([props.content], { type: 'text/plain;charset=utf-8' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = props.docName
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(url)
}
</script>
<style scoped>
.doc-block {
margin: 8px 0;
border-radius: 10px;
overflow: hidden;
background: rgba(255, 255, 255, 0.8);
backdrop-filter: blur(12px);
border: 1px solid rgba(59, 130, 246, 0.15);
box-shadow: 0 2px 8px rgba(59, 130, 246, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
}
:root[data-theme='dark'] .doc-block {
background: rgba(26, 30, 39, 0.8);
border-color: rgba(96, 165, 250, 0.15);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
}
.doc-block.collapsed .doc-content {
display: none;
}
.doc-header {
display: flex;
align-items: center;
padding: 6px 10px;
background: rgba(255, 255, 255, 0.8);
border-bottom: 1px solid rgba(59, 130, 246, 0.12);
gap: 8px;
}
:root[data-theme='dark'] .doc-header {
background: rgba(26, 30, 39, 0.8);
border-bottom-color: rgba(96, 165, 250, 0.15);
}
.doc-icon {
display: flex;
align-items: center;
justify-content: center;
color: #3b82f6;
}
.doc-name {
flex: 1;
font-size: 13px;
font-weight: 500;
color: #1e293b;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
:root[data-theme='dark'] .doc-name {
color: #e5e7eb;
}
.doc-actions {
display: flex;
align-items: center;
gap: 2px;
}
.action-btn {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
padding: 0;
border: none;
background: transparent;
color: #64748b;
cursor: pointer;
border-radius: 6px;
opacity: 0.75;
}
.action-btn:hover {
background: rgba(59, 130, 246, 0.1);
color: #3b82f6;
opacity: 1;
}
.doc-content {
padding: 8px 10px;
background: rgba(248, 250, 252, 0.8);
max-height: 240px;
overflow-y: auto;
}
:root[data-theme='dark'] .doc-content {
background: rgba(18, 22, 30, 0.8);
}
.doc-content pre {
margin: 0;
padding: 0;
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Mono', monospace;
font-size: 12px;
line-height: 1.5;
color: #334155;
white-space: pre-wrap;
word-break: break-word;
}
:root[data-theme='dark'] .doc-content pre {
color: #cbd5e1;
}
</style>
+448
View File
@@ -0,0 +1,448 @@
<script setup>
import { computed } from 'vue'
const props = defineProps({
node: { type: Object, default: null },
breadcrumb: { type: Array, default: () => [] },
rootNodes: { type: Array, default: () => [] },
getFileIcon: { type: Function, default: () => 'file' }
})
const emit = defineEmits(['navigate'])
const fileExt = computed(() => {
if (!props.node || props.node.type !== 'file') return ''
const parts = props.node.name.split('.')
return parts.length > 1 ? parts.pop().toLowerCase() : ''
})
const isMarkdown = computed(() => {
return fileExt.value === 'md' || fileExt.value === 'markdown'
})
const isText = computed(() => {
const textExts = ['txt', 'json', 'js', 'ts', 'css', 'html', 'py', 'vue', 'xml', 'yaml', 'yml', 'csv', 'log', 'sql', 'toml', 'ini', 'cfg', 'conf', 'sh', 'bat']
return textExts.includes(fileExt.value) || isMarkdown.value
})
const isRoot = computed(() => !props.node)
const isFolder = computed(() => props.node && props.node.type === 'folder')
const folderItems = computed(() => {
if (!isRoot.value && !isFolder.value) return []
const items = isRoot.value ? props.rootNodes : (props.node.children || [])
return [...items].sort((a, b) => {
if (a.type !== b.type) return a.type === 'folder' ? -1 : 1
return a.name.localeCompare(b.name)
})
})
function navigateTo(id) {
emit('navigate', id)
}
function navigateUp() {
if (isRoot.value) return
emit('navigate', props.node.parentId || null)
}
function formatDate(timestamp) {
if (!timestamp) return ''
const date = new Date(timestamp)
const diff = Date.now() - date.getTime()
if (diff < 60000) return '刚刚'
if (diff < 3600000) return `${Math.floor(diff/60000)}分钟前`
if (diff < 86400000) return `${Math.floor(diff/3600000)}小时前`
if (diff < 30 * 86400000) return `${Math.floor(diff/86400000)}天前`
return date.toLocaleDateString()
}
</script>
<template>
<div class="file-content">
<div v-if="breadcrumb.length > 0" class="breadcrumb">
<template v-for="(item, index) in breadcrumb" :key="item.id">
<span
v-if="item.type === 'folder'"
class="breadcrumb-item"
@click="navigateTo(item.id)"
>{{ item.name }}</span>
<span v-else class="breadcrumb-item breadcrumb-current">{{ item.name }}</span>
<span v-if="index < breadcrumb.length - 1" class="breadcrumb-sep">/</span>
</template>
</div>
<div v-if="isRoot || isFolder" class="content-directory-view">
<div class="directory-list">
<div class="directory-header">
<div class="col-name">名称</div>
<div class="col-date">更新时间</div>
</div>
<div v-if="!isRoot" class="directory-row" @click="navigateUp">
<span class="col-icon">
<span class="icon-folder"></span>
</span>
<div class="col-name name-folder">..</div>
<div class="col-date"></div>
</div>
<div
v-for="item in folderItems"
:key="item.id"
class="directory-row"
@click="navigateTo(item.id)"
>
<span class="col-icon">
<span v-if="item.type==='folder'" class="icon-folder"></span>
<span v-else :class="['icon-file', `icon-${getFileIcon(item.name)}`]"></span>
</span>
<div class="col-name" :class="item.type === 'folder' ? 'name-folder' : 'name-file'">{{ item.name }}</div>
<div class="col-date" :title="new Date(item.updatedAt).toLocaleString()">{{ formatDate(item.updatedAt) }}</div>
</div>
<div v-if="folderItems.length === 0" class="directory-empty">
<svg viewBox="0 0 24 24" width="48" height="48" stroke="currentColor" stroke-width="1" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path>
</svg>
<p>此文件夹为空</p>
</div>
</div>
</div>
<div v-else-if="isMarkdown" class="content-markdown">
<div class="markdown-body" v-html="renderMarkdown(node.content || '')"></div>
</div>
<div v-else-if="isText" class="content-text">
<pre class="text-content">{{ node.content || '' }}</pre>
</div>
<div v-else class="content-unsupported">
<svg viewBox="0 0 24 24" width="48" height="48" stroke="currentColor" stroke-width="1" fill="none" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" y1="8" x2="12" y2="12"></line>
<line x1="12" y1="16" x2="12.01" y2="16"></line>
</svg>
<p>暂不支持预览此文件类型</p>
<p class="file-ext">.{{ fileExt }}</p>
</div>
</div>
</template>
<script>
function renderMarkdown(text) {
if (!text) return ''
let html = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/^### (.+)$/gm, '<h3>$1</h3>')
.replace(/^## (.+)$/gm, '<h2>$1</h2>')
.replace(/^# (.+)$/gm, '<h1>$1</h1>')
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.+?)\*/g, '<em>$1</em>')
.replace(/`(.+?)`/g, '<code>$1</code>')
.replace(/^\> (.+)$/gm, '<blockquote>$1</blockquote>')
.replace(/^\- (.+)$/gm, '<li>$1</li>')
.replace(/^(\d+)\. (.+)$/gm, '<li>$1. $2</li>')
.replace(/\n\n/g, '</p><p>')
.replace(/\n/g, '<br>')
return `<p>${html}</p>`
}
</script>
<style scoped>
.file-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
min-width: 0;
}
.breadcrumb {
display: flex;
align-items: center;
gap: 4px;
padding: 8px 16px;
font-size: 13px;
color: var(--muted-text);
border-bottom: 1px solid var(--panel-border);
flex-shrink: 0;
}
.breadcrumb-item {
cursor: pointer;
color: var(--focus-ring);
}
.breadcrumb-item:hover {
text-decoration: underline;
}
.breadcrumb-current {
color: var(--app-text);
cursor: default;
}
.breadcrumb-current:hover {
text-decoration: none;
}
.breadcrumb-sep {
color: var(--muted-text);
}
.content-unsupported {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
color: var(--muted-text);
padding: 32px;
}
.content-unsupported svg {
opacity: 0.4;
}
.content-directory-view {
flex: 1;
padding: 24px 32px;
overflow-y: auto;
background: var(--app-bg);
}
.directory-list {
max-width: 900px;
margin: 0 auto;
border: 1px solid var(--panel-border);
border-radius: 8px;
overflow: hidden;
background: var(--panel-bg);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
.directory-header {
display: flex;
align-items: center;
padding: 12px 16px;
background: var(--ghost-code-bg);
border-bottom: 1px solid var(--panel-border);
font-size: 13px;
font-weight: 600;
color: var(--muted-text);
}
.directory-row {
display: flex;
align-items: center;
padding: 10px 16px;
border-bottom: 1px solid var(--panel-border);
cursor: pointer;
transition: background 0.15s ease;
font-size: 14px;
}
.directory-row:last-child {
border-bottom: none;
}
.directory-row:hover {
background: var(--ghost-code-bg);
}
.col-icon {
width: 24px;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
margin-right: 8px;
}
.col-name {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: var(--app-text);
}
.name-folder {
font-weight: 500;
color: var(--focus-ring);
}
.directory-row:hover .name-folder {
text-decoration: underline;
}
.col-date {
width: 120px;
flex-shrink: 0;
text-align: right;
color: var(--muted-text);
font-size: 13px;
}
.directory-empty {
padding: 48px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 16px;
color: var(--muted-text);
font-size: 14px;
}
.directory-empty svg {
opacity: 0.3;
}
/* File Icons (Reused from FileTree) */
.icon-file,
.icon-folder {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
flex-shrink: 0;
}
.icon-folder::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%2354aeff' d='M0 2.5A1.5 1.5 0 011.5 1h2.793a.5.5 0 01.353.146l1.5 1.5a.5.5 0 00.354.146H13.5A1.5 1.5 0 0115 4.5v7.5a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12v-9.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
[data-theme='dark'] .icon-folder::before {
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%2358a6ff' d='M0 2.5A1.5 1.5 0 011.5 1h2.793a.5.5 0 01.353.146l1.5 1.5a.5.5 0 00.354.146H13.5A1.5 1.5 0 0115 4.5v7.5a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12v-9.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.icon-markdown::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%236e7781' d='M14.85 3H1.15C.52 3 0 3.52 0 4.15v7.69C0 12.48.52 13 1.15 13h13.69c.64 0 1.15-.52 1.15-1.15V4.15C16 3.52 15.48 3 14.85 3zM9 11H7.5V8.5L6.25 10l-1.25-1.5V11H3.5V5H5l1.25 1.5L7.5 5H9v6zm4-2.5c0 .28-.22.5-.5.5h-1v1c0 .28-.22.5-.5.5s-.5-.22-.5-.5v-1h-1c-.28 0-.5-.22-.5-.5s.22-.5.5-.5h1v-1c0-.28.22-.5.5-.5s.5.22.5.5v1h1c.28 0 .5.22.5.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.icon-text::before,
.icon-json::before,
.icon-file::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%236e7781' d='M3.75 1.5a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V4.664a.25.25 0 00-.073-.177l-2.914-2.914a.25.25 0 00-.177-.073H3.75zM3 1.75C3 .784 3.784 0 4.75 0h5.339c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0113 16H4.75A1.75 1.75 0 013 14.25V1.75z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.file-ext {
font-family: monospace;
font-size: 0.85rem;
background: var(--ghost-code-bg);
padding: 4px 8px;
border-radius: 4px;
}
.content-markdown {
flex: 1;
overflow-y: auto;
padding: 24px 32px;
}
.markdown-body {
max-width: 800px;
margin: 0 auto;
line-height: 1.7;
color: var(--app-text);
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
margin-top: 24px;
margin-bottom: 12px;
font-weight: 600;
line-height: 1.3;
}
.markdown-body h1 { font-size: 1.75rem; border-bottom: 1px solid var(--panel-border); padding-bottom: 8px; }
.markdown-body h2 { font-size: 1.4rem; border-bottom: 1px solid var(--panel-border); padding-bottom: 6px; }
.markdown-body h3 { font-size: 1.15rem; }
.markdown-body strong { font-weight: 600; }
.markdown-body em { font-style: italic; }
.markdown-body code {
background: var(--code-inline-bg);
padding: 2px 6px;
border-radius: 4px;
font-size: 0.9em;
font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
color: var(--code-text);
}
.markdown-body blockquote {
border-left: 3px solid var(--focus-ring);
padding-left: 16px;
margin: 12px 0;
color: var(--muted-text);
}
.markdown-body li {
margin-left: 20px;
list-style: disc;
}
.content-text {
flex: 1;
overflow: auto;
padding: 16px;
}
.text-content {
margin: 0;
padding: 16px;
background: var(--code-block-bg);
border: 1px solid var(--code-block-border);
border-radius: 8px;
font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
font-size: 13px;
line-height: 1.6;
color: var(--code-text);
white-space: pre-wrap;
word-break: break-word;
overflow: auto;
max-width: 100%;
}
.content-markdown::-webkit-scrollbar,
.content-text::-webkit-scrollbar {
width: 8px;
height: 8px;
}
.content-markdown::-webkit-scrollbar-thumb,
.content-text::-webkit-scrollbar-thumb {
background: var(--scrollbar-thumb);
border-radius: 4px;
}
.content-markdown::-webkit-scrollbar-thumb:hover,
.content-text::-webkit-scrollbar-thumb:hover {
background: var(--scrollbar-thumb-hover);
}
</style>
+586
View File
@@ -0,0 +1,586 @@
<script setup>
import { ref } from 'vue'
defineProps({
nodes: { type: Array, required: true },
selectedId: { type: String, default: null },
expandedIds: { type: Set, required: true },
clipboard: { type: Object, default: null },
getFileIcon: { type: Function, required: true }
})
const emit = defineEmits([
'select', 'toggle', 'create-file', 'create-folder',
'rename', 'remove', 'copy', 'cut', 'paste',
'context-menu', 'drop', 'drag-start', 'drag-over'
])
const renameId = ref(null)
const renameValue = ref('')
const creatingInFolder = ref(null)
const creatingType = ref(null)
const creatingName = ref('')
function startRename(node) {
renameId.value = node.id
renameValue.value = node.name
}
function finishRename(node) {
if (renameId.value === node.id && renameValue.value.trim()) {
emit('rename', node.id, renameValue.value.trim())
}
renameId.value = null
renameValue.value = ''
}
function cancelRename() {
renameId.value = null
renameValue.value = ''
}
function startCreate(parentId, type) {
creatingInFolder.value = parentId
creatingType.value = type
creatingName.value = ''
}
function finishCreate() {
if (creatingInFolder.value !== undefined && creatingName.value.trim()) {
if (creatingType.value === 'file') {
emit('create-file', creatingInFolder.value, creatingName.value.trim())
} else {
emit('create-folder', creatingInFolder.value, creatingName.value.trim())
}
}
creatingInFolder.value = null
creatingType.value = null
creatingName.value = ''
}
function cancelCreate() {
creatingInFolder.value = null
creatingType.value = null
creatingName.value = ''
}
function handleContextMenu(event, node) {
event.preventDefault()
event.stopPropagation()
emit('context-menu', event.clientX, event.clientY, node)
}
function handleDrop(event, targetNode) {
event.preventDefault()
event.stopPropagation()
const draggedId = event.dataTransfer.getData('text/plain')
if (draggedId) {
emit('drop', draggedId, targetNode ? targetNode.id : null)
}
}
function handleDropRoot(event) {
event.preventDefault()
const draggedId = event.dataTransfer.getData('text/plain')
if (draggedId) {
emit('drop', draggedId, null)
}
}
function isClipped(id) {
return props.clipboard && props.clipboard.node && props.clipboard.node.id === id
}
function getIconClass(type, name) {
if (type === 'folder') return 'icon-folder'
return `icon-file icon-${props.getFileIcon(name)}`
}
</script>
<template>
<div class="file-tree">
<div class="tree-header">
<span class="tree-title">文件</span>
<div class="tree-header-actions">
<button class="header-action-btn" title="新建文件" @click="emit('create-file', null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M3.75 1.5a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V4.664a.25.25 0 00-.073-.177l-2.914-2.914a.25.25 0 00-.177-.073H3.75zM3 1.75C3 .784 3.784 0 4.75 0h5.339c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0113 16H4.75A1.75 1.75 0 013 14.25V1.75z"/><path d="M8.5 4V1.5H10a.5.5 0 01.5.5v1.5a.5.5 0 01-.5.5H9a.5.5 0 01-.5-.5zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
</button>
<button class="header-action-btn" title="新建文件夹" @click="emit('create-folder', null)">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M.5 2.5A1.5 1.5 0 012 1h3.5a.5.5 0 01.354.146l1.5 1.5a.5.5 0 00.354.146H13a1.5 1.5 0 011.5 1.5v7.5a1.5 1.5 0 01-1.5 1.5H2a1.5 1.5 0 01-1.5-1.5v-7.5zM6 2v1.5h4.5V2H6zm-2 5a.5.5 0 01.5-.5h5a.5.5 0 010 1h-5a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h5a.5.5 0 000-1h-5z"/></svg>
</button>
</div>
</div>
<div
class="tree-content"
@drop="handleDropRoot"
@dragover="(e) => e.preventDefault()"
>
<template v-if="nodes.length === 0">
<div class="tree-empty">
<p>暂无文件</p>
<div class="tree-empty-actions">
<button @click="startCreate(null, 'file')">+ 新建文件</button>
<button @click="startCreate(null, 'folder')">+ 新建文件夹</button>
</div>
</div>
</template>
<template v-for="node in nodes" :key="node.id">
<TreeNodeItem
:node="node"
:level="0"
:selected-id="selectedId"
:expanded-ids="expandedIds"
:clipboard="clipboard"
:get-icon-class="getIconClass"
:rename-id="renameId"
:rename-value="renameValue"
:creating-in-folder="creatingInFolder"
:creating-type="creatingType"
:creating-name="creatingName"
@select="(id) => emit('select', id)"
@toggle="(id) => emit('toggle', id)"
@start-rename="startRename"
@finish-rename="finishRename"
@cancel-rename="cancelRename"
@update:rename-value="(val) => renameValue = val"
@start-create="startCreate"
@finish-create="finishCreate"
@cancel-create="cancelCreate"
@update:creating-name="(val) => creatingName = val"
@context-menu="handleContextMenu"
@drop="handleDrop"
@drag-start="(e, id) => emit('drag-start', e, id)"
@drag-over="(e, id) => emit('drag-over', e, id)"
/>
</template>
</div>
</div>
</template>
<script>
import { h } from 'vue'
export const TreeNodeItem = {
name: 'TreeNodeItem',
props: {
node: { type: Object, required: true },
level: { type: Number, required: true },
selectedId: { type: String, default: null },
expandedIds: { type: Set, required: true },
clipboard: { type: Object, default: null },
getIconClass: { type: Function, required: true },
renameId: { type: String, default: null },
renameValue: { type: String, default: '' },
creatingInFolder: { type: String, default: null },
creatingType: { type: String, default: null },
creatingName: { type: String, default: '' }
},
emits: [
'select', 'toggle', 'start-rename', 'finish-rename', 'cancel-rename',
'start-create', 'finish-create', 'cancel-create',
'context-menu', 'drop', 'drag-start', 'drag-over'
],
setup(props, { emit }) {
function isSelected() {
return props.node.id === props.selectedId
}
function isExpanded() {
return props.expandedIds.has(props.node.id)
}
function isClipped() {
return props.clipboard && props.clipboard.node && props.clipboard.node.id === props.node.id
}
function isRenaming() {
return props.renameId === props.node.id
}
function isCreating() {
return props.creatingInFolder === props.node.id
}
function handleContextMenu(event) {
event.preventDefault()
event.stopPropagation()
emit('context-menu', event.clientX, event.clientY, props.node)
}
function handleDrop(event) {
event.preventDefault()
event.stopPropagation()
const draggedId = event.dataTransfer.getData('text/plain')
if (draggedId) {
emit('drop', draggedId, props.node)
}
}
function handleDragStart(event) {
event.dataTransfer.setData('text/plain', props.node.id)
emit('drag-start', event, props.node.id)
}
function handleDragOver(event) {
event.preventDefault()
emit('drag-over', event, props.node.id)
}
return () => {
const node = props.node
const children = node.type === 'folder' ? (node.children || []) : []
const paddingLeft = `${props.level * 16 + 8}px`
const nodeVNode = h('div', {
class: ['tree-node', { selected: isSelected(), clipped: isClipped() }],
style: { paddingLeft },
onClick: () => emit('select', node.id),
onContextmenu: handleContextMenu,
onDragstart: handleDragStart,
onDragover: handleDragOver,
onDrop: handleDrop,
draggable: 'true'
}, [
node.type === 'folder'
? h('span', {
class: 'chevron',
onClick: (e) => { e.stopPropagation(); emit('toggle', node.id) }
}, isExpanded()
? h('svg', { viewBox: '0 0 16 16', width: 16, height: 16, fill: 'currentColor' }, h('path', { d: 'M5 6l3 3 3-3z' }))
: h('svg', { viewBox: '0 0 16 16', width: 16, height: 16, fill: 'currentColor' }, h('path', { d: 'M6 5l3 3-3 3z' }))
)
: h('span', { class: 'chevron-placeholder' }),
h('span', { class: props.getIconClass(node.type, node.name) }),
isRenaming()
? h('input', {
class: 'rename-input',
value: props.renameValue,
onInput: (e) => { emit('update:rename-value', e.target.value) },
onKeydown: (e) => { if (e.key === 'Enter') emit('finish-rename', node); if (e.key === 'Escape') emit('cancel-rename') },
onBlur: () => emit('finish-rename', node),
autofocus: true
})
: h('span', { class: 'node-name' }, node.name),
h('span', { class: 'node-actions' }, [
node.type === 'folder' ? [
h('button', {
class: 'action-btn',
title: '新建文件',
onClick: (e) => { e.stopPropagation(); emit('start-create', node.id, 'file') }
}, h('svg', { viewBox: '0 0 16 16', width: 14, height: 14, fill: 'currentColor' }, h('path', { d: 'M8 2a5.53 5.53 0 00-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2zm2.354 6H9.698v.656h.656v.688H9.698v.656H9.042v-.656H8.386v-.688h.656V8h-.656V7.344h.656V6.688h.656V7.344h.656v.656zM8 3a4.69 4.69 0 013.293 1.342c.612.586 1.038 1.32 1.143 2.122.16.99.478 1.89 1.39 2.136C15.346 9.02 16 10.37 16 11.773 16 13.17 14.902 14 12.687 14H3.781C2.108 14 1 12.902 1 11.318c0-1.343.96-2.494 2.234-2.652.105-.612.53-1.346 1.143-1.932A4.69 4.69 0 018 3z' }))),
h('button', {
class: 'action-btn',
title: '新建文件夹',
onClick: (e) => { e.stopPropagation(); emit('start-create', node.id, 'folder') }
}, h('svg', { viewBox: '0 0 16 16', width: 14, height: 14, fill: 'currentColor' }, h('path', { d: 'M.75 3a.75.75 0 01.75-.75h4.59c.3 0 .584.12.793.332l.967.968H13.5a.75.75 0 010 1.5H7.19L6.22 4.08A.25.25 0 006.043 4H1.5A.75.75 0 01.75 3zM1 6.75A.75.75 0 011.75 6h12.5a.75.75 0 010 1.5H1.75A.75.75 0 011 6.75zm0 3a.75.75 0 01.75-.75h4.5a.75.75 0 010 1.5h-4.5a.75.75 0 01-.75-.75z' })))
] : null
].filter(Boolean))
].filter(Boolean))
const createVNode = isCreating()
? h('div', {
class: ['tree-node', 'tree-node-new'],
style: { paddingLeft: `${(props.level + 1) * 16 + 8}px` }
}, [
h('span', { class: 'chevron-placeholder' }),
h('span', { class: `icon-file ${props.creatingType === 'folder' ? 'icon-folder' : 'icon-file'}` }),
h('input', {
class: 'rename-input',
value: props.creatingName,
placeholder: props.creatingType === 'file' ? '文件名.md' : '文件夹名',
onInput: (e) => { emit('update:creating-name', e.target.value) },
onKeydown: (e) => { if (e.key === 'Enter') emit('finish-create'); if (e.key === 'Escape') emit('cancel-create') },
onBlur: () => emit('finish-create'),
autofocus: true
})
])
: null
const childrenVNodes = (node.type === 'folder' && isExpanded())
? children.map(child => h(TreeNodeItem, {
node: child,
level: props.level + 1,
selectedId: props.selectedId,
expandedIds: props.expandedIds,
clipboard: props.clipboard,
getIconClass: props.getIconClass,
renameId: props.renameId,
renameValue: props.renameValue,
creatingInFolder: props.creatingInFolder,
creatingType: props.creatingType,
creatingName: props.creatingName,
onSelect: (id) => emit('select', id),
onToggle: (id) => emit('toggle', id),
onStartRename: (n) => emit('start-rename', n),
onFinishRename: (n) => emit('finish-rename', n),
onCancelRename: () => emit('cancel-rename'),
onStartCreate: (id, type) => emit('start-create', id, type),
onFinishCreate: () => emit('finish-create'),
onCancelCreate: () => emit('cancel-create'),
onContextMenu: (x, y, n) => emit('context-menu', x, y, n),
onDrop: (id, n) => emit('drop', id, n),
onDragStart: (e, id) => emit('drag-start', e, id),
onDragOver: (e, id) => emit('drag-over', e, id)
}))
: []
return [nodeVNode, createVNode, ...childrenVNodes].filter(Boolean)
}
}
}
</script>
<style scoped>
.file-tree {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
background: var(--app-bg);
overflow: hidden;
}
.tree-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 12px;
border-bottom: 1px solid var(--panel-border);
min-height: 40px;
flex-shrink: 0;
}
.tree-title {
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
color: var(--muted-text);
letter-spacing: 0.05em;
}
.tree-header-actions {
display: flex;
gap: 4px;
}
.header-action-btn {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
padding: 0;
border: none;
background: none;
color: var(--muted-text);
cursor: pointer;
border-radius: 4px;
}
.header-action-btn:hover {
background: var(--ghost-code-bg);
color: var(--app-text);
}
.tree-content {
flex: 1;
overflow-y: auto;
padding: 4px 0;
min-height: 0;
}
.tree-empty {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 32px 16px;
color: var(--muted-text);
font-size: 13px;
text-align: center;
gap: 12px;
}
.tree-empty p {
margin: 0;
}
.tree-empty-actions {
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
}
.tree-empty-actions button {
padding: 6px 12px;
border: 1px dashed var(--panel-border);
background: none;
color: var(--muted-text);
cursor: pointer;
border-radius: 6px;
font-size: 12px;
}
.tree-empty-actions button:hover {
border-color: var(--focus-ring);
color: var(--focus-ring);
background: var(--ghost-code-bg);
}
.tree-node {
display: flex;
align-items: center;
gap: 4px;
padding: 2px 8px;
cursor: pointer;
font-size: 13px;
line-height: 28px;
white-space: nowrap;
user-select: none;
position: relative;
}
.tree-node:hover {
background: var(--ghost-code-bg);
}
.tree-node.selected {
background: var(--focus-ring);
color: #fff;
}
.tree-node.selected .chevron,
.tree-node.selected .node-actions,
.tree-node.selected .action-btn {
color: #fff;
}
.tree-node.clipped {
opacity: 0.5;
}
.tree-node-new {
background: var(--ghost-code-bg);
border: 1px dashed var(--focus-ring);
margin: 1px 4px;
border-radius: 4px;
}
.chevron {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
color: var(--muted-text);
cursor: pointer;
flex-shrink: 0;
}
.chevron:hover {
color: var(--app-text);
}
.chevron-placeholder {
width: 16px;
flex-shrink: 0;
}
.icon-file,
.icon-folder {
display: flex;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
flex-shrink: 0;
}
.icon-folder::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%2354aeff' d='M0 2.5A1.5 1.5 0 011.5 1h2.793a.5.5 0 01.353.146l1.5 1.5a.5.5 0 00.354.146H13.5A1.5 1.5 0 0115 4.5v7.5a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12v-9.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
[data-theme='dark'] .icon-folder::before {
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%2358a6ff' d='M0 2.5A1.5 1.5 0 011.5 1h2.793a.5.5 0 01.353.146l1.5 1.5a.5.5 0 00.354.146H13.5A1.5 1.5 0 0115 4.5v7.5a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12v-9.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.icon-markdown::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%236e7781' d='M14.85 3H1.15C.52 3 0 3.52 0 4.15v7.69C0 12.48.52 13 1.15 13h13.69c.64 0 1.15-.52 1.15-1.15V4.15C16 3.52 15.48 3 14.85 3zM9 11H7.5V8.5L6.25 10l-1.25-1.5V11H3.5V5H5l1.25 1.5L7.5 5H9v6zm4-2.5c0 .28-.22.5-.5.5h-1v1c0 .28-.22.5-.5.5s-.5-.22-.5-.5v-1h-1c-.28 0-.5-.22-.5-.5s.22-.5.5-.5h1v-1c0-.28.22-.5.5-.5s.5.22.5.5v1h1c.28 0 .5.22.5.5z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.icon-text::before,
.icon-json::before,
.icon-file::before {
content: '';
display: block;
width: 16px;
height: 16px;
background: url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill='%236e7781' d='M3.75 1.5a.25.25 0 00-.25.25v12.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V4.664a.25.25 0 00-.073-.177l-2.914-2.914a.25.25 0 00-.177-.073H3.75zM3 1.75C3 .784 3.784 0 4.75 0h5.339c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0113 16H4.75A1.75 1.75 0 013 14.25V1.75z'/%3E%3C/svg%3E") no-repeat center;
background-size: contain;
}
.node-name {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
}
.rename-input {
flex: 1;
padding: 2px 6px;
border: 1px solid var(--focus-ring);
border-radius: 4px;
background: var(--app-bg);
color: var(--app-text);
font-size: 13px;
outline: none;
height: 22px;
}
.node-actions {
display: flex;
gap: 2px;
opacity: 0;
transition: opacity 0.15s;
}
.tree-node:hover .node-actions {
opacity: 1;
}
.action-btn {
display: flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
padding: 0;
border: none;
background: none;
color: var(--muted-text);
cursor: pointer;
border-radius: 4px;
}
.action-btn:hover {
background: var(--panel-border);
color: var(--app-text);
}
.tree-content::-webkit-scrollbar {
width: 8px;
}
.tree-content::-webkit-scrollbar-thumb {
background: var(--scrollbar-thumb);
border-radius: 4px;
}
.tree-content::-webkit-scrollbar-thumb:hover {
background: var(--scrollbar-thumb-hover);
}
</style>
+1 -1
View File
@@ -16,7 +16,7 @@ const props = defineProps({
}) })
const md = new MarkdownIt({ const md = new MarkdownIt({
html: true, html: false,
linkify: true, linkify: true,
typographer: true typographer: true
}) })
File diff suppressed because it is too large Load Diff
+82 -1
View File
@@ -1,10 +1,16 @@
<script setup> <script setup>
import { ref, watch, computed, onMounted, onUnmounted } from 'vue' import { ref, watch, computed, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useSettingsStore } from '../stores/settings' import { useSettingsStore } from '../stores/settings'
import { useTheme } from '../composables/useTheme' import { useTheme } from '../composables/useTheme'
import packageJson from '../../package.json'
const store = useSettingsStore() const store = useSettingsStore()
const { setTheme } = useTheme() const { setTheme } = useTheme()
const router = useRouter()
const route = useRoute()
const VERSION = packageJson.version || '0.0.0'
const isOpen = ref(false) const isOpen = ref(false)
let systemThemeMediaQuery = null let systemThemeMediaQuery = null
@@ -126,6 +132,11 @@ const handleImageUpload = (event) => {
} }
const t = (key) => store.t[key] const t = (key) => store.t[key]
const switchView = (view) => {
router.push(view === 'editor' ? '/' : '/docs')
closePanel()
}
</script> </script>
<template> <template>
@@ -194,6 +205,38 @@ const t = (key) => store.t[key]
</div> </div>
</section> </section>
<!-- View Switch -->
<section class="settings-section">
<h3>{{ t('view') || '视图' }}</h3>
<div class="view-switch">
<button
class="view-btn"
:class="{ active: route.path === '/' }"
@click="switchView('editor')"
>
<svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 20h9"></path>
<path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
</svg>
<span>{{ t('editor') || '编辑器' }}</span>
</button>
<button
class="view-btn"
:class="{ active: route.path === '/docs' }"
@click="switchView('docs')"
>
<svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
<span>{{ t('docs') || '文档' }}</span>
</button>
</div>
</section>
<!-- Model Section --> <!-- Model Section -->
<section class="settings-section"> <section class="settings-section">
<h3>{{ t('modelIntelligence') }}</h3> <h3>{{ t('modelIntelligence') }}</h3>
@@ -272,7 +315,7 @@ const t = (key) => store.t[key]
<div class="about-card"> <div class="about-card">
<h4>llm-in-text</h4> <h4>llm-in-text</h4>
<p>A smart Markdown editor with local LLM intelligence.</p> <p>A smart Markdown editor with local LLM intelligence.</p>
<p class="version">v0.1.0-beta</p> <p class="version">v{{ VERSION }}</p>
</div> </div>
</section> </section>
</div> </div>
@@ -316,6 +359,7 @@ const t = (key) => store.t[key]
backdrop-filter: blur(20px); backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
border-right: 1px solid var(--panel-border); border-right: 1px solid var(--panel-border);
border-radius: 0 8px 8px 0;
box-shadow: var(--panel-shadow); box-shadow: var(--panel-shadow);
z-index: 10000; z-index: 10000;
transform: translateX(-100%); transform: translateX(-100%);
@@ -346,8 +390,11 @@ const t = (key) => store.t[key]
/* Mobile Fullscreen */ /* Mobile Fullscreen */
@media (max-width: 640px) { @media (max-width: 640px) {
.settings-panel { .settings-panel {
top: 0;
bottom: 0;
width: 100%; width: 100%;
border-right: none; border-right: none;
border-radius: 0;
} }
} }
@@ -381,6 +428,7 @@ const t = (key) => store.t[key]
flex: 1; flex: 1;
overflow-y: auto; overflow-y: auto;
padding: 20px; padding: 20px;
min-height: 0;
} }
.settings-section { .settings-section {
@@ -559,5 +607,38 @@ const t = (key) => store.t[key]
font-size: 0.8rem; font-size: 0.8rem;
opacity: 0.7; opacity: 0.7;
} }
.view-switch {
display: flex;
gap: 8px;
}
.view-btn {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 10px 12px;
border-radius: 8px;
border: 1px solid var(--panel-border);
background: var(--ghost-code-bg);
color: var(--muted-text);
cursor: pointer;
font-size: 0.9rem;
transition: all 0.2s;
}
.view-btn:hover {
color: var(--app-text);
border-color: var(--focus-ring);
}
.view-btn.active {
background: var(--app-bg);
color: var(--app-text);
border-color: var(--focus-ring);
font-weight: 600;
}
</style> </style>
+100
View File
@@ -0,0 +1,100 @@
<template>
<Teleport to="body">
<Transition name="tts-menu-fade">
<div
v-if="visible"
class="tts-menu"
:style="menuStyle"
@mousedown.stop
>
<button class="tts-menu__btn" @click="$emit('speak')" :disabled="loading">
<svg v-if="!loading" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 5L6 9H2v6h4l5 4V5z"/>
<path d="M15.54 8.46a5 5 0 0 1 0 7.07"/>
<path d="M19.07 4.93a10 10 0 0 1 0 14.14"/>
</svg>
<svg v-else class="tts-menu__spinner" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 12a9 9 0 1 1-6.219-8.56"/>
</svg>
{{ loading ? '生成中...' : '朗读' }}
</button>
</div>
</Transition>
</Teleport>
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps({
visible: { type: Boolean, default: false },
x: { type: Number, default: 0 },
y: { type: Number, default: 0 },
loading: { type: Boolean, default: false },
})
defineEmits(['speak'])
const menuStyle = computed(() => ({
left: `${props.x}px`,
top: `${props.y}px`,
transform: 'translate(-50%, -100%)',
}))
</script>
<style scoped>
.tts-menu {
position: fixed;
z-index: 100000;
pointer-events: auto;
}
.tts-menu__btn {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 14px;
border: 1px solid var(--panel-border);
border-radius: 20px;
background: var(--panel-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
color: var(--app-text);
font-size: 13px;
font-weight: 500;
cursor: pointer;
box-shadow: var(--panel-shadow);
white-space: nowrap;
transition: all 0.15s ease;
}
.tts-menu__btn:hover:not(:disabled) {
background: var(--btn-hover-bg);
color: var(--btn-hover-fg);
border-color: var(--btn-hover-bg);
}
.tts-menu__btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.tts-menu__spinner {
animation: tts-spin 1s linear infinite;
}
@keyframes tts-spin {
to { transform: rotate(360deg); }
}
.tts-menu-fade-enter-active,
.tts-menu-fade-leave-active {
transition: opacity 0.15s ease, transform 0.15s ease;
}
.tts-menu-fade-enter-from,
.tts-menu-fade-leave-to {
opacity: 0;
transform: translate(-50%, calc(-100% + 4px));
}
</style>
+432
View File
@@ -0,0 +1,432 @@
<template>
<Teleport to="body">
<Transition name="tts-player-slide">
<div v-if="visible" class="tts-player">
<div class="tts-player__inner">
<div class="tts-player__controls">
<button class="tts-player__btn" @click="skipBackward" title="快退5秒">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M1 4v6h6"/><path d="M11 12a7 7 0 0 0-7-7"/><path d="M11 12l-3-3"/>
<text x="14" y="16" font-size="8" fill="currentColor" stroke="none">5</text>
</svg>
</button>
<button class="tts-player__btn tts-player__btn--primary" @click="togglePlay" :title="isPlaying ? '暂停' : '播放'">
<svg v-if="!isPlaying" width="20" height="20" viewBox="0 0 24 24" fill="currentColor" stroke="none">
<polygon points="5 3 19 12 5 21 5 3"/>
</svg>
<svg v-else width="20" height="20" viewBox="0 0 24 24" fill="currentColor" stroke="none">
<rect x="6" y="4" width="4" height="16"/><rect x="14" y="4" width="4" height="16"/>
</svg>
</button>
<button class="tts-player__btn" @click="skipForward" title="快进5秒">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M23 4v6h-6"/><path d="M13 12a7 7 0 0 1 7-7"/><path d="M13 12l3-3"/>
<text x="4" y="16" font-size="8" fill="currentColor" stroke="none">5</text>
</svg>
</button>
</div>
<div class="tts-player__progress">
<span class="tts-player__time">{{ currentTimeStr }}</span>
<div class="tts-player__bar" @click="seek">
<div class="tts-player__bar-fill" :style="{ width: progressPercent + '%' }">
<div class="tts-player__bar-thumb"></div>
</div>
</div>
<span class="tts-player__time">{{ durationStr }}</span>
</div>
<div class="tts-player__settings">
<div class="tts-player__volume">
<button class="tts-player__btn" @click="toggleMute" :title="isMuted ? '取消静音' : '静音'">
<svg v-if="!isMuted && volume > 0.5" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 5L6 9H2v6h4l5 4V5z"/><path d="M19.07 4.93a10 10 0 0 1 0 14.14"/><path d="M15.54 8.46a5 5 0 0 1 0 7.07"/>
</svg>
<svg v-else-if="!isMuted" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 5L6 9H2v6h4l5 4V5z"/><path d="M15.54 8.46a5 5 0 0 1 0 7.07"/>
</svg>
<svg v-else width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 5L6 9H2v6h4l5 4V5z"/><line x1="23" y1="9" x2="17" y2="15"/><line x1="17" y1="9" x2="23" y2="15"/>
</svg>
</button>
<input type="range" class="tts-player__slider" v-model.number="volume" min="0" max="1" step="0.05" title="音量">
</div>
<select class="tts-player__speed-select" v-model.number="playbackRate" title="播放速度">
<option value="0.5">0.5x</option>
<option value="0.75">0.75x</option>
<option value="1">1x</option>
<option value="1.25">1.25x</option>
<option value="1.5">1.5x</option>
<option value="2">2x</option>
</select>
<button class="tts-player__btn" @click="downloadAudio" title="下载音频">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/>
</svg>
</button>
<button class="tts-player__btn tts-player__btn--close" @click="$emit('close')" title="关闭">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
</div>
</div>
<audio ref="audioRef" :src="audioSrc" @timeupdate="onTimeUpdate" @ended="onEnded" @loadedmetadata="onLoadedMetadata"></audio>
</div>
</Transition>
</Teleport>
</template>
<script setup>
import { ref, computed, watch, onUnmounted } from 'vue'
const props = defineProps({
visible: { type: Boolean, default: false },
audioBase64: { type: String, default: '' },
format: { type: String, default: 'wav' },
durationMs: { type: Number, default: 0 },
})
const emit = defineEmits(['close'])
const audioRef = ref(null)
const isPlaying = ref(false)
const currentTime = ref(0)
const duration = ref(0)
const volume = ref(1)
const isMuted = ref(false)
const playbackRate = ref(1)
let blobUrl = ''
const audioSrc = computed(() => {
if (!props.audioBase64) return ''
if (blobUrl) URL.revokeObjectURL(blobUrl)
const byteChars = atob(props.audioBase64)
const byteNumbers = new Array(byteChars.length)
for (let i = 0; i < byteChars.length; i++) {
byteNumbers[i] = byteChars.charCodeAt(i)
}
const byteArray = new Uint8Array(byteNumbers)
const blob = new Blob([byteArray], { type: `audio/${props.format}` })
blobUrl = URL.createObjectURL(blob)
return blobUrl
})
const currentTimeStr = computed(() => formatTime(currentTime.value))
const durationStr = computed(() => formatTime(duration.value))
const progressPercent = computed(() => {
if (!duration.value) return 0
return Math.min(100, (currentTime.value / duration.value) * 100)
})
function formatTime(seconds) {
if (!seconds || !isFinite(seconds)) return '0:00'
const mins = Math.floor(seconds / 60)
const secs = Math.floor(seconds % 60)
return `${mins}:${secs.toString().padStart(2, '0')}`
}
function togglePlay() {
const audio = audioRef.value
if (!audio) return
if (isPlaying.value) {
audio.pause()
} else {
audio.play()
}
}
function skipBackward() {
const audio = audioRef.value
if (!audio) return
audio.currentTime = Math.max(0, audio.currentTime - 5)
}
function skipForward() {
const audio = audioRef.value
if (!audio) return
audio.currentTime = Math.min(duration.value, audio.currentTime + 5)
}
function seek(event) {
const audio = audioRef.value
if (!audio || !duration.value) return
const rect = event.currentTarget.getBoundingClientRect()
const ratio = (event.clientX - rect.left) / rect.width
audio.currentTime = Math.max(0, Math.min(duration.value, ratio * duration.value))
}
function toggleMute() {
isMuted.value = !isMuted.value
if (audioRef.value) {
audioRef.value.muted = isMuted.value
}
}
function downloadAudio() {
if (!blobUrl) return
const a = document.createElement('a')
a.href = blobUrl
a.download = `tts-${Date.now()}.${props.format}`
a.click()
}
function onTimeUpdate() {
const audio = audioRef.value
if (!audio) return
currentTime.value = audio.currentTime
}
function onLoadedMetadata() {
const audio = audioRef.value
if (!audio) return
duration.value = audio.duration || (props.durationMs / 1000)
audio.volume = volume.value
audio.playbackRate = playbackRate.value
audio.play()
}
function onEnded() {
isPlaying.value = false
currentTime.value = 0
}
watch(isPlaying, (val) => {
const audio = audioRef.value
if (!audio) return
if (val) {
audio.play().catch(() => { isPlaying.value = false })
} else {
audio.pause()
}
})
watch(volume, (val) => {
if (audioRef.value) {
audioRef.value.volume = val
isMuted.value = val === 0
}
})
watch(playbackRate, (val) => {
if (audioRef.value) {
audioRef.value.playbackRate = val
}
})
watch(() => props.visible, (val) => {
if (val) {
isPlaying.value = true
currentTime.value = 0
} else {
if (audioRef.value) {
audioRef.value.pause()
audioRef.value.currentTime = 0
}
isPlaying.value = false
}
})
onUnmounted(() => {
if (audioRef.value) {
audioRef.value.pause()
}
if (blobUrl) {
URL.revokeObjectURL(blobUrl)
blobUrl = ''
}
})
</script>
<style scoped>
.tts-player {
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 99999;
padding: 8px 16px;
background: var(--panel-bg);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
border-top: 1px solid var(--panel-border);
box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.1);
}
.tts-player__inner {
display: flex;
align-items: center;
gap: 12px;
max-width: 1200px;
margin: 0 auto;
}
.tts-player__controls {
display: flex;
align-items: center;
gap: 4px;
flex-shrink: 0;
}
.tts-player__btn {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border: none;
border-radius: 50%;
background: transparent;
color: var(--app-text);
cursor: pointer;
transition: all 0.15s ease;
}
.tts-player__btn:hover {
background: rgba(128, 128, 128, 0.15);
}
.tts-player__btn--primary {
width: 36px;
height: 36px;
background: var(--btn-hover-bg);
color: var(--btn-hover-fg);
}
.tts-player__btn--primary:hover {
filter: brightness(1.1);
}
.tts-player__btn--close:hover {
background: rgba(220, 38, 38, 0.15);
color: var(--danger-text);
}
.tts-player__progress {
flex: 1;
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
}
.tts-player__time {
font-size: 11px;
color: var(--muted-text);
font-variant-numeric: tabular-nums;
flex-shrink: 0;
min-width: 36px;
}
.tts-player__bar {
flex: 1;
height: 4px;
background: rgba(128, 128, 128, 0.2);
border-radius: 2px;
cursor: pointer;
position: relative;
transition: height 0.1s ease;
}
.tts-player__bar:hover {
height: 6px;
}
.tts-player__bar:hover .tts-player__bar-thumb {
opacity: 1;
}
.tts-player__bar-fill {
height: 100%;
background: linear-gradient(90deg, var(--btn-hover-bg), #60a5fa);
border-radius: 2px;
position: relative;
transition: width 0.1s linear;
}
.tts-player__bar-thumb {
position: absolute;
right: -5px;
top: 50%;
transform: translateY(-50%);
width: 10px;
height: 10px;
background: #fff;
border-radius: 50%;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
opacity: 0;
transition: opacity 0.1s ease;
}
.tts-player__settings {
display: flex;
align-items: center;
gap: 8px;
flex-shrink: 0;
}
.tts-player__volume {
display: flex;
align-items: center;
gap: 4px;
}
.tts-player__slider {
width: 60px;
height: 4px;
-webkit-appearance: none;
appearance: none;
background: rgba(128, 128, 128, 0.2);
border-radius: 2px;
outline: none;
cursor: pointer;
}
.tts-player__slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 12px;
height: 12px;
background: var(--btn-hover-bg);
border-radius: 50%;
cursor: pointer;
}
.tts-player__slider::-moz-range-thumb {
width: 12px;
height: 12px;
background: var(--btn-hover-bg);
border-radius: 50%;
border: none;
cursor: pointer;
}
.tts-player__speed-select {
padding: 2px 6px;
border: 1px solid var(--panel-border);
border-radius: 4px;
background: transparent;
color: var(--app-text);
font-size: 11px;
cursor: pointer;
outline: none;
}
.tts-player__speed-select:hover {
border-color: var(--btn-hover-bg);
}
.tts-player-slide-enter-active,
.tts-player-slide-leave-active {
transition: transform 0.25s ease, opacity 0.2s ease;
}
.tts-player-slide-enter-from,
.tts-player-slide-leave-to {
transform: translateY(100%);
opacity: 0;
}
</style>
+504
View File
@@ -0,0 +1,504 @@
<template>
<div class="univer-editor-container">
<!-- 工具栏 -->
<div class="univer-toolbar">
<div class="toolbar-left">
<span class="doc-name" :title="documentInfo?.name || ''">
{{ documentInfo?.name || '未命名文档' }}
</span>
<span class="doc-format" v-if="documentInfo?.format">
{{ getFormatLabel(documentInfo.format) }}
</span>
</div>
<div class="toolbar-right">
<button class="toolbar-btn" @click="handleImport" :title="t('import') || '导入文件'">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="17 8 12 3 7 8"/>
<line x1="12" y1="3" x2="12" y2="15"/>
</svg>
<span>{{ t('import') || '导入' }}</span>
</button>
<button class="toolbar-btn" @click="handleExport" :title="t('export') || '导出文件'" :disabled="!hasDocument">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="7 10 12 15 17 10"/>
<line x1="12" y1="15" x2="12" y2="3"/>
</svg>
<span>{{ t('export') || '导出' }}</span>
</button>
<button class="toolbar-btn" @click="handleSaveSnapshot" :title="t('saveSnapshot') || '保存快照'" :disabled="!editorInstance">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/>
<polyline points="17 21 17 13 7 13 7 21"/>
<polyline points="7 3 7 8 15 8"/>
</svg>
<span>{{ t('saveSnapshot') || '快照' }}</span>
</button>
<button class="toolbar-btn back-btn" @click="handleBack" :title="t('backToEditor') || '返回编辑器'">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 12H5M12 19l-7-7 7-7"/>
</svg>
<span>{{ t('back') || '返回' }}</span>
</button>
</div>
</div>
<!-- 编辑器容器 -->
<div ref="editorContainer" class="univer-editor-body">
<!-- 空状态提示 -->
<div v-if="!editorInstance" class="empty-state">
<div class="empty-icon">
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
<polyline points="14 2 14 8 20 8"/>
<line x1="16" y1="13" x2="8" y2="13"/>
<line x1="16" y1="17" x2="8" y2="17"/>
<polyline points="10 9 9 9 8 9"/>
</svg>
</div>
<p class="empty-text">{{ t('selectOfficeFile') || '请选择 Office 文件开始编辑' }}</p>
<p class="empty-hint">{{ t('supportedFormats') || '支持 DOCX、XLSX、PPTX 格式' }}</p>
</div>
</div>
<!-- 导入文件输入 -->
<input
ref="fileInput"
type="file"
:accept="acceptTypes"
@change="handleFileChange"
style="display: none"
/>
<!-- 导出格式选择对话框 -->
<Teleport to="body">
<div v-if="showExportDialog" class="export-dialog-overlay" @click.self="showExportDialog = false">
<div class="export-dialog">
<h3>{{ t('selectExportFormat') || '选择导出格式' }}</h3>
<div class="export-options">
<button
v-for="format in exportFormats"
:key="format.value"
class="export-option"
@click="confirmExport(format.value)"
>
<span class="format-icon">{{ format.icon }}</span>
<span class="format-label">{{ format.label }}</span>
</button>
</div>
<button class="cancel-btn" @click="showExportDialog = false">
{{ t('cancel') || '取消' }}
</button>
</div>
</div>
</Teleport>
</div>
</template>
<script setup>
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useOfficeStore } from '../stores/office'
import { useSettingsStore } from '../stores/settings'
import { useTheme } from '../composables/useTheme'
import { createUniverEditor, OfficeFormat, OfficePresetType } from '../services/univerBridge'
import { isOfficeFile, getOfficeFormat, getFormatDisplayName } from '../services/officeDetection'
const emit = defineEmits(['back', 'document-loaded', 'document-changed'])
const router = useRouter()
const officeStore = useOfficeStore()
const settings = useSettingsStore()
const { isDark } = useTheme()
const t = (key) => settings.t[key]
const editorContainer = ref(null)
const fileInput = ref(null)
const editorInstance = ref(null)
const showExportDialog = ref(false)
const acceptTypes = '.docx,.xlsx,.pptx'
const hasDocument = computed(() => officeStore.hasDocument)
const documentInfo = computed(() => officeStore.documentInfo)
const exportFormats = computed(() => {
const currentFormat = officeStore.currentFormat
if (currentFormat === OfficeFormat.XLSX) {
return [
{ value: 'xlsx', label: 'Excel (.xlsx)', icon: '📊' },
{ value: 'xlsx_snapshot', label: '快照 (JSON)', icon: '💾' }
]
}
return [
{ value: 'docx', label: 'Word (.docx)', icon: '📄' },
{ value: 'snapshot', label: '快照 (JSON)', icon: '💾' }
]
})
/**
* 初始化编辑器
*/
async function initEditor(format) {
if (!editorContainer.value) return
try {
if (editorInstance.value) {
await editorInstance.value.destroy()
}
editorInstance.value = createUniverEditor()
await editorInstance.value.init(editorContainer.value, {
format: format || OfficeFormat.DOCX,
locale: settings.language === 'zh-CN' ? 'zh-CN' : 'en-US',
theme: isDark.value ? 'dark' : 'light'
})
//
editorInstance.value.onChange((event) => {
officeStore.markAsChanged()
emit('document-changed', event)
})
} catch (error) {
console.error('初始化 Univer 编辑器失败:', error)
}
}
/**
* 处理导入
*/
function handleImport() {
fileInput.value?.click()
}
/**
* 处理文件选择
*/
async function handleFileChange(event) {
const file = event.target.files?.[0]
if (!file) return
if (!isOfficeFile(file)) {
alert(t('invalidOfficeFormat') || '请选择有效的 Office 文件 (DOCX/XLSX/PPTX)')
event.target.value = ''
return
}
const format = getOfficeFormat(file)
const bytes = await file.arrayBuffer()
officeStore.setCurrentDocument(file, bytes)
//
await initEditor(format)
emit('document-loaded', {
name: file.name,
format,
size: file.size
})
event.target.value = ''
}
/**
* 处理导出
*/
function handleExport() {
showExportDialog.value = true
}
/**
* 确认导出
*/
async function confirmExport(format) {
showExportDialog.value = false
if (!editorInstance.value) return
try {
const snapshot = await editorInstance.value.exportSnapshot()
const json = JSON.stringify(snapshot, null, 2)
downloadFile(json, `${officeStore.currentFileName || 'document'}.json`, 'application/json')
} catch (error) {
console.error('导出失败:', error)
alert(t('exportFailed') || '导出失败,请重试')
}
}
/**
* 保存快照
*/
async function handleSaveSnapshot() {
if (!editorInstance.value) return
try {
const snapshot = await editorInstance.value.exportSnapshot()
officeStore.setSnapshot(snapshot)
// localStorage
const key = `univer_snapshot_${Date.now()}`
localStorage.setItem(key, JSON.stringify({
name: officeStore.currentFileName,
format: officeStore.currentFormat,
snapshot: snapshot,
savedAt: new Date().toISOString()
}))
alert(t('snapshotSaved') || '快照已保存')
} catch (error) {
console.error('保存快照失败:', error)
}
}
/**
* 返回编辑器
*/
function handleBack() {
emit('back')
router.push('/')
}
/**
* 下载文件
*/
function downloadFile(content, filename, mimeType) {
const blob = new Blob([content], { type: mimeType })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(url)
}
/**
* 获取格式标签
*/
function getFormatLabel(format) {
return getFormatDisplayName(format, settings.language)
}
/**
* 监听主题变化
*/
watch(isDark, async (newVal) => {
if (editorInstance.value) {
// Univer
await initEditor(officeStore.currentFormat)
}
})
/**
* 监听语言变化
*/
watch(() => settings.language, async (newVal) => {
if (editorInstance.value) {
await initEditor(officeStore.currentFormat)
}
})
onMounted(async () => {
//
if (officeStore.currentFormat) {
await initEditor(officeStore.currentFormat)
}
})
onBeforeUnmount(async () => {
if (editorInstance.value) {
await editorInstance.value.destroy()
editorInstance.value = null
}
})
defineExpose({
initEditor,
editorInstance
})
</script>
<style scoped>
.univer-editor-container {
display: flex;
flex-direction: column;
width: 100%;
height: 100vh;
background: var(--app-bg);
color: var(--app-text);
}
.univer-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 16px;
background: var(--panel-bg);
border-bottom: 1px solid var(--panel-border);
flex-shrink: 0;
}
.toolbar-left {
display: flex;
align-items: center;
gap: 12px;
}
.doc-name {
font-size: 14px;
font-weight: 500;
max-width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.doc-format {
font-size: 12px;
color: var(--muted-text);
padding: 2px 8px;
background: var(--ghost-code-bg);
border-radius: 4px;
}
.toolbar-right {
display: flex;
align-items: center;
gap: 8px;
}
.toolbar-btn {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
border: 1px solid var(--panel-border);
border-radius: 6px;
background: var(--app-bg);
color: var(--app-text);
font-size: 13px;
cursor: pointer;
transition: all 0.2s;
}
.toolbar-btn:hover:not(:disabled) {
background: var(--btn-hover-bg);
border-color: var(--focus-ring);
}
.toolbar-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.back-btn {
margin-left: 8px;
border-color: var(--focus-ring);
color: var(--focus-ring);
}
.univer-editor-body {
flex: 1;
overflow: hidden;
position: relative;
}
.empty-state {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: var(--muted-text);
}
.empty-icon {
margin-bottom: 16px;
opacity: 0.5;
}
.empty-text {
font-size: 16px;
margin-bottom: 8px;
}
.empty-hint {
font-size: 13px;
opacity: 0.7;
}
.export-dialog-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 10000;
}
.export-dialog {
background: var(--panel-bg);
padding: 24px;
border-radius: 12px;
border: 1px solid var(--panel-border);
min-width: 320px;
}
.export-dialog h3 {
margin: 0 0 16px;
font-size: 16px;
text-align: center;
}
.export-options {
display: flex;
flex-direction: column;
gap: 8px;
margin-bottom: 16px;
}
.export-option {
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px;
border: 1px solid var(--panel-border);
border-radius: 8px;
background: var(--app-bg);
cursor: pointer;
transition: all 0.2s;
}
.export-option:hover {
background: var(--btn-hover-bg);
border-color: var(--focus-ring);
}
.format-icon {
font-size: 24px;
}
.format-label {
font-size: 14px;
}
.cancel-btn {
width: 100%;
padding: 10px;
border: 1px solid var(--panel-border);
border-radius: 6px;
background: transparent;
color: var(--muted-text);
cursor: pointer;
}
.cancel-btn:hover {
background: var(--ghost-code-bg);
}
</style>
+383
View File
@@ -0,0 +1,383 @@
import { ref, computed, watch } from 'vue'
const STORAGE_KEY = 'llm-in-text-file-system'
const MAX_FILE_SIZE = 50 * 1024 * 1024 // 50MB
const MAX_FILES = 100
const MAX_FOLDERS = 50
function generateId() {
return Date.now().toString(36) + Math.random().toString(36).slice(2, 9)
}
function countFilesAndFolders(nodes) {
let files = 0
let folders = 0
function traverse(items) {
for (const item of items) {
if (item.type === 'folder') {
folders++
traverse(item.children || [])
} else {
files++
}
}
}
traverse(nodes)
return { files, folders }
}
function findNode(nodes, id) {
for (const node of nodes) {
if (node.id === id) return node
if (node.type === 'folder') {
const found = findNode(node.children || [], id)
if (found) return found
}
}
return null
}
function findParent(nodes, id, parent = null) {
for (const node of nodes) {
if (node.id === id) return parent
if (node.type === 'folder') {
const found = findParent(node.children || [], id, node)
if (found !== undefined) return found
}
}
return undefined
}
function removeNode(nodes, id) {
return nodes.filter(n => n.id !== id).map(n => {
if (n.type === 'folder') {
return { ...n, children: removeNode(n.children || [], id) }
}
return n
})
}
function getPath(nodes, id, path = []) {
for (const node of nodes) {
if (node.id === id) return [...path, node]
if (node.type === 'folder') {
const found = getPath(node.children || [], id, [...path, node])
if (found) return found
}
}
return null
}
export function useFileSystem() {
const tree = ref([])
const selectedId = ref(null)
const expandedIds = ref(new Set())
const clipboard = ref(null) // { mode: 'copy' | 'cut', node: {...} }
const contextMenu = ref(null) // { x, y, node }
const error = ref(null)
function load() {
try {
const stored = localStorage.getItem(STORAGE_KEY)
if (stored) {
tree.value = JSON.parse(stored)
} else {
// 创建示例文件和文件夹
const welcomeId = generateId()
const folderId = generateId()
tree.value = [
{
id: folderId,
name: '示例文件夹',
type: 'folder',
children: [],
parentId: null,
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: welcomeId,
name: '欢迎使用.md',
type: 'file',
content: '# 欢迎使用文件系统\n\n这是一个类似 GitHub 风格的文件浏览器。\n\n## 功能\n\n- ✅ 文件夹展开/折叠\n- ✅ 文件选中高亮\n- ✅ 拖拽移动\n- ✅ 右键菜单\n- ✅ 重命名\n- ✅ 新建/删除\n\n点击左侧的文件或文件夹来查看内容。\n',
parentId: null,
createdAt: Date.now(),
updatedAt: Date.now()
}
]
save()
}
} catch {
tree.value = []
}
}
function save() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(tree.value))
} catch {
error.value = '存储空间不足'
}
}
watch(tree, save, { deep: true })
function createFile(parentId, name, content = '') {
const { files } = countFilesAndFolders(tree.value)
if (files >= MAX_FILES) {
error.value = `文件数量已达上限(${MAX_FILES}个)`
return false
}
if (new Blob([content]).size > MAX_FILE_SIZE) {
error.value = '文件大小不能超过 50MB'
return false
}
const newFile = {
id: generateId(),
name,
type: 'file',
content,
parentId,
createdAt: Date.now(),
updatedAt: Date.now()
}
if (!parentId) {
tree.value.push(newFile)
} else {
const parent = findNode(tree.value, parentId)
if (parent && parent.type === 'folder') {
parent.children = parent.children || []
parent.children.push(newFile)
}
}
error.value = null
return true
}
function createFolder(parentId, name) {
const { folders } = countFilesAndFolders(tree.value)
if (folders >= MAX_FOLDERS) {
error.value = `文件夹数量已达上限(${MAX_FOLDERS}个)`
return false
}
const newFolder = {
id: generateId(),
name,
type: 'folder',
children: [],
parentId,
createdAt: Date.now(),
updatedAt: Date.now()
}
if (!parentId) {
tree.value.push(newFolder)
} else {
const parent = findNode(tree.value, parentId)
if (parent && parent.type === 'folder') {
parent.children = parent.children || []
parent.children.push(newFolder)
}
}
error.value = null
return true
}
function rename(id, newName) {
const node = findNode(tree.value, id)
if (node) {
node.name = newName
node.updatedAt = Date.now()
error.value = null
return true
}
return false
}
function remove(id) {
tree.value = removeNode(tree.value, id)
if (selectedId.value === id) selectedId.value = null
error.value = null
}
function select(id) {
selectedId.value = id
}
function toggleFolder(id) {
const node = findNode(tree.value, id)
if (!node || node.type !== 'folder') return
const set = new Set(expandedIds.value)
if (set.has(id)) {
set.delete(id)
} else {
set.add(id)
}
expandedIds.value = set
}
function copy(id) {
const node = findNode(tree.value, id)
if (node) {
clipboard.value = { mode: 'copy', node: JSON.parse(JSON.stringify(node)) }
}
}
function cut(id) {
const node = findNode(tree.value, id)
if (node) {
clipboard.value = { mode: 'cut', node: JSON.parse(JSON.stringify(node)) }
}
}
function paste(targetParentId) {
if (!clipboard.value) return
const { mode, node } = clipboard.value
if (mode === 'cut') {
const oldParent = findParent(tree.value, node.id)
if (oldParent) {
oldParent.children = (oldParent.children || []).filter(c => c.id !== node.id)
} else {
tree.value = tree.value.filter(n => n.id !== node.id)
}
node.parentId = targetParentId || null
if (targetParentId) {
const target = findNode(tree.value, targetParentId)
if (target && target.type === 'folder') {
target.children = target.children || []
target.children.push(node)
}
} else {
tree.value.push(node)
}
clipboard.value = null
} else {
const { files, folders } = countFilesAndFolders(tree.value)
function countInNode(n) {
let f = 0, fl = 0
if (n.type === 'folder') {
fl = 1
for (const c of (n.children || [])) {
const sub = countInNode(c)
f += sub.f
fl += sub.fl
}
} else {
f = 1
}
return { f, fl }
}
const counts = countInNode(node)
if (files + counts.f > MAX_FILES) {
error.value = `文件数量将达上限`
return
}
if (folders + counts.fl > MAX_FOLDERS) {
error.value = `文件夹数量将达上限`
return
}
function cloneWithNewIds(n) {
const clone = { ...n, id: generateId(), createdAt: Date.now(), updatedAt: Date.now() }
if (clone.type === 'folder') {
clone.children = (n.children || []).map(c => cloneWithNewIds(c))
}
return clone
}
const cloned = cloneWithNewIds(node)
cloned.parentId = targetParentId || null
if (targetParentId) {
const target = findNode(tree.value, targetParentId)
if (target && target.type === 'folder') {
target.children = target.children || []
target.children.push(cloned)
}
} else {
tree.value.push(cloned)
}
}
error.value = null
}
function canPaste() {
return clipboard.value !== null
}
function clearClipboard() {
clipboard.value = null
}
function getSelectedNode() {
return selectedId.value ? findNode(tree.value, selectedId.value) : null
}
function getBreadcrumbPath(id) {
return getPath(tree.value, id) || []
}
function showContextMenu(x, y, node) {
contextMenu.value = { x, y, node }
}
function hideContextMenu() {
contextMenu.value = null
}
function getExtension(name) {
const parts = name.split('.')
return parts.length > 1 ? parts.pop().toLowerCase() : ''
}
function getFileIcon(name) {
const ext = getExtension(name)
const iconMap = {
md: 'markdown',
txt: 'text',
json: 'json',
js: 'javascript',
ts: 'typescript',
css: 'css',
html: 'html',
py: 'python',
vue: 'vue',
xml: 'xml',
yaml: 'yaml',
yml: 'yaml',
csv: 'csv',
log: 'log',
sql: 'sql'
}
return iconMap[ext] || 'file'
}
return {
tree,
selectedId,
expandedIds,
clipboard,
contextMenu,
error,
load,
createFile,
createFolder,
rename,
remove,
select,
toggleFolder,
copy,
cut,
paste,
canPaste,
clearClipboard,
getSelectedNode,
getBreadcrumbPath,
showContextMenu,
hideContextMenu,
getFileIcon,
getExtension,
MAX_FILE_SIZE,
MAX_FILES,
MAX_FOLDERS
}
}
+3 -1
View File
@@ -1,5 +1,6 @@
import { createApp } from 'vue' import { createApp } from 'vue'
import App from './App.vue' import App from './App.vue'
import router from './router'
import './style.css' import './style.css'
import { createPinia } from 'pinia' import { createPinia } from 'pinia'
@@ -8,9 +9,10 @@ import '@milkdown/crepe/theme/frame.css'
const app = createApp(App) const app = createApp(App)
app.use(createPinia()) app.use(createPinia())
app.use(router)
app.mount('#app') app.mount('#app')
if (import.meta.env.PROD && 'serviceWorker' in navigator) { if (import.meta.env.PROD && 'serviceWorker' in navigator && false) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js').catch(() => { navigator.serviceWorker.register('/sw.js').catch(() => {
// Service worker registration failed, silently ignore // Service worker registration failed, silently ignore
+48 -8
View File
@@ -9,6 +9,7 @@ import { getOcrCache, OCR_SIZE_LIMIT, extractTextFromOCR } from '../utils/ocrCac
const COPILOT_PLUGIN_KEY = new PluginKey('milkdown-copilot') const COPILOT_PLUGIN_KEY = new PluginKey('milkdown-copilot')
const DEBOUNCE_MS = 1000 const DEBOUNCE_MS = 1000
const SIZE_LIMIT = OCR_SIZE_LIMIT const SIZE_LIMIT = OCR_SIZE_LIMIT
const DOC_SIZE_LIMIT = 32 * 1024 // 文档块32KB限制
const IMAGE_NODE_TYPES = new Set(['image', 'image-block', 'imageBlock']) const IMAGE_NODE_TYPES = new Set(['image', 'image-block', 'imageBlock'])
interface CopilotState { interface CopilotState {
@@ -330,6 +331,31 @@ function buildOcrContextForRequest(doc: ProseNode, cursorPos: number): string {
return `\n\n${lines.join('\n')}` return `\n\n${lines.join('\n')}`
} }
// 从markdown中提取文档块内容用于AI补全上下文
function extractDocBlocksFromMarkdown(markdown: string): string {
const lines: string[] = []
// 使用正则表达式匹配文档块
// <doc_type="pdf" doc_name="xxx" upload_time="xxx">content</doc_end>
const docBlockRegex = /<doc_type="(\w+)"\s+doc_name="([^"]+)"\s+upload_time="([^"]+)">([\s\S]*?)<\/doc_end>/g
let match
while ((match = docBlockRegex.exec(markdown)) !== null) {
const docType = match[1]
const docName = match[2]
const content = match[4].trim()
if (content) {
// 将文档内容格式化为上下文,限制长度
const truncatedContent = content.length > 500 ? content.substring(0, 500) + '...' : content
lines.push(`<doc_type="${docType}" doc_name="${docName}">\n${truncatedContent}\n</doc_end>`)
}
}
if (lines.length === 0) return ''
return `\n\n-- 已上传文档内容 --\n${lines.join('\n\n')}`
}
function doFetchSuggestion( function doFetchSuggestion(
view: EditorView, view: EditorView,
runtime: CopilotRuntime, runtime: CopilotRuntime,
@@ -379,7 +405,6 @@ function scheduleFetch(view: EditorView, runtime: CopilotRuntime, pos: number) {
const doc = view.state.doc const doc = view.state.doc
const schema = view.state.schema const schema = view.state.schema
const baseSize = doc.content.size
const serializer = runtime.ctx.get(serializerCtx) const serializer = runtime.ctx.get(serializerCtx)
let prefixMarkdown = '' let prefixMarkdown = ''
@@ -400,12 +425,21 @@ function scheduleFetch(view: EditorView, runtime: CopilotRuntime, pos: number) {
suffixMarkdown = doc.textBetween(pos, doc.content.size, '\n', '\n') suffixMarkdown = doc.textBetween(pos, doc.content.size, '\n', '\n')
} }
const requestPrefix = `${prefixMarkdown}${buildOcrContextForRequest(doc, pos)}` // 构建上下文:OCR内容 + 上传文档内容
const totalTextLen = (prefixMarkdown + suffixMarkdown).length const ocrContext = buildOcrContextForRequest(doc, pos)
const ocrContextLen = requestPrefix.length - prefixMarkdown.length
const totalWithOcr = totalTextLen + ocrContextLen
const overLimit = totalWithOcr > SIZE_LIMIT // 从markdown中提取文档块内容用于AI补全上下文
const docContext = extractDocBlocksFromMarkdown(prefixMarkdown + suffixMarkdown)
// 组合所有上下文到prefix前面
const fullPrefixWithContext = `${ocrContext}${docContext}\n\n${prefixMarkdown}`
const totalTextLen = (prefixMarkdown + suffixMarkdown).length
const contextLen = fullPrefixWithContext.length - prefixMarkdown.length
const totalWithContext = totalTextLen + contextLen
// 使用32KB限制(文档上下文)
const overLimit = totalWithContext > DOC_SIZE_LIMIT
if (overLimit) { if (overLimit) {
setCopilotEnabled(view, false) setCopilotEnabled(view, false)
@@ -422,9 +456,10 @@ function scheduleFetch(view: EditorView, runtime: CopilotRuntime, pos: number) {
runtime.requestSeq = requestSeq runtime.requestSeq = requestSeq
const requestDocVersion = runtime.docVersion const requestDocVersion = runtime.docVersion
// 使用包含文档上下文的prefix
runtime.debounceTimer = setTimeout(() => { runtime.debounceTimer = setTimeout(() => {
runtime.debounceTimer = null runtime.debounceTimer = null
doFetchSuggestion(view, runtime, pos, requestPrefix, suffixMarkdown, requestSeq, requestDocVersion) doFetchSuggestion(view, runtime, pos, fullPrefixWithContext, suffixMarkdown, requestSeq, requestDocVersion)
}, debounceMs) }, debounceMs)
} }
@@ -708,7 +743,12 @@ export function interruptCopilot(view: EditorView): void {
} }
export function checkSizeLimit(view: EditorView): { size: number; overLimit: boolean } { export function checkSizeLimit(view: EditorView): { size: number; overLimit: boolean } {
const size = view.state.doc.content.size let size = view.state.doc.content.size
view.state.doc.descendants((node) => {
if (node.type.name === 'doc_block' && node.attrs.content) {
size += String(node.attrs.content).length
}
})
return { size, overLimit: size > SIZE_LIMIT } return { size, overLimit: size > SIZE_LIMIT }
} }
+249
View File
@@ -0,0 +1,249 @@
import { createApp, reactive } from 'vue'
import { serializerCtx } from '@milkdown/kit/core'
import { $node, $remark, $view } from '@milkdown/kit/utils'
import type { Node as ProseNode, Schema } from '@milkdown/prose/model'
import type { EditorView, NodeView } from '@milkdown/prose/view'
import DocBlockCrepe from '../components/DocBlockCrepe.vue'
import {
DOC_BLOCK_FENCE_LANG,
DOC_BLOCK_NODE_TYPE,
DOC_CONTEXT_LIMIT,
buildLegacyDocBlock,
buildDocContextFence,
normalizeDocType,
parseLegacyDocBlock,
parseDocBlockValue,
stripDocBlockMarkdown,
} from '../utils/docBlock.js'
function serializeRangeToMarkdown(
doc: ProseNode,
from: number,
to: number,
schema: Schema,
serializer: (content: ProseNode) => string
): string {
if (from >= to) return ''
const slice = doc.slice(from, to)
if (slice.content.size <= 0) return ''
const sliceDoc = schema.topNodeType.createAndFill(undefined, slice.content)
return sliceDoc ? serializer(sliceDoc) : doc.textBetween(from, to, '\n', '\n')
}
function buildDocContext(doc: ProseNode, excludePos?: number) {
const blocks: string[] = []
doc.descendants((node, pos) => {
if (node.type.name !== DOC_BLOCK_NODE_TYPE) return true
if (excludePos !== undefined && pos === excludePos) return false
blocks.push(
buildDocContextFence({
docType: node.attrs.docType,
content: node.attrs.content,
})
)
return false
})
return blocks.join('\n\n')
}
class DocBlockNodeView implements NodeView {
node: ProseNode
view: EditorView
getPos: () => number | undefined
dom: HTMLElement
app: ReturnType<typeof createApp> | null = null
props: Record<string, any>
serializer: (content: ProseNode) => string
constructor(node: ProseNode, view: EditorView, getPos: () => number | undefined, serializer: (content: ProseNode) => string) {
this.node = node
this.view = view
this.getPos = getPos
this.serializer = serializer
this.dom = document.createElement('div')
this.dom.className = 'doc-block-node-view'
this.props = reactive({
docType: node.attrs.docType,
docName: node.attrs.docName,
uploadTime: node.attrs.uploadTime,
content: node.attrs.content,
collapsed: node.attrs.collapsed,
onUpdateContent: (content: string) => this.updateAttrs({ content }),
onUpdateCollapsed: (collapsed: boolean) => this.updateAttrs({ collapsed }),
onDelete: () => this.deleteNode(),
resolveSuggestionRequest: (payload: { prefix: string; suffix: string; languageId: string }) => this.resolveSuggestionRequest(payload),
})
this.mount()
}
mount() {
this.app = createApp(DocBlockCrepe, this.props)
this.app.mount(this.dom)
}
getPosValue() {
const pos = this.getPos()
return typeof pos === 'number' ? pos : undefined
}
updateAttrs(patch: Record<string, any>) {
const pos = this.getPosValue()
if (pos === undefined) return
const nextAttrs = { ...this.node.attrs, ...patch }
this.view.dispatch(this.view.state.tr.setNodeMarkup(pos, undefined, nextAttrs))
}
deleteNode() {
const pos = this.getPosValue()
if (pos === undefined) return
const tr = this.view.state.tr.delete(pos, pos + this.node.nodeSize).scrollIntoView()
this.view.dispatch(tr)
this.view.focus()
}
resolveSuggestionRequest(payload: { prefix: string; suffix: string; languageId: string }) {
const pos = this.getPosValue()
if (pos === undefined) return payload
const doc = this.view.state.doc
const schema = this.view.state.schema
const before = stripDocBlockMarkdown(serializeRangeToMarkdown(doc, 0, pos, schema, this.serializer))
const after = stripDocBlockMarkdown(serializeRangeToMarkdown(doc, pos + this.node.nodeSize, doc.content.size, schema, this.serializer))
const docContext = buildDocContext(doc, pos)
const mergedPrefix = [docContext, before, payload.prefix].filter(Boolean).join('\n\n')
const mergedSuffix = [payload.suffix, after].filter(Boolean).join('\n\n')
if (mergedPrefix.length + mergedSuffix.length > DOC_CONTEXT_LIMIT) {
return {
prefix: mergedPrefix.slice(0, DOC_CONTEXT_LIMIT),
suffix: '',
languageId: payload.languageId,
blocked: true,
}
}
return {
prefix: mergedPrefix,
suffix: mergedSuffix,
languageId: payload.languageId,
blocked: false,
}
}
update(node: ProseNode) {
if (node.type !== this.node.type) return false
this.node = node
this.props.docType = node.attrs.docType
this.props.docName = node.attrs.docName
this.props.uploadTime = node.attrs.uploadTime
this.props.content = node.attrs.content
this.props.collapsed = node.attrs.collapsed
return true
}
stopEvent(event: Event) {
const target = event.target as Node | null
return Boolean(target && this.dom.contains(target))
}
ignoreMutation() {
return true
}
destroy() {
this.app?.unmount()
this.app = null
}
}
function visitChildren(node: any, visitor: (child: any) => any) {
if (!node || !Array.isArray(node.children)) return
node.children = node.children.map((child: any) => {
const next = visitor(child)
if (next && next !== child) return next
visitChildren(child, visitor)
return child
})
}
export const docBlockRemark = $remark('docBlockRemark', () => () => {
return (tree: any) => {
visitChildren(tree, (node) => {
if (node?.type === 'code' && node.lang === DOC_BLOCK_FENCE_LANG) {
return {
type: 'docBlock',
value: String(node.value || ''),
sourceType: 'code',
}
}
if (node?.type === 'html' && typeof node.value === 'string' && node.value.includes('<doc_type=')) {
return {
type: 'docBlock',
value: String(node.value || ''),
sourceType: 'html',
}
}
return node
})
}
})
export const docBlockNode = $node(DOC_BLOCK_NODE_TYPE, () => ({
group: 'block',
atom: true,
isolating: true,
selectable: true,
draggable: false,
marks: '',
attrs: {
docType: { default: 'txt' },
docName: { default: 'document.txt' },
uploadTime: { default: '' },
content: { default: '' },
collapsed: { default: false },
},
parseDOM: [
{
tag: 'div[data-doc-block="true"]',
getAttrs: (dom) => ({
docType: normalizeDocType((dom as HTMLElement).getAttribute('data-doc-type') || ''),
docName: (dom as HTMLElement).getAttribute('data-doc-name') || 'document.txt',
uploadTime: (dom as HTMLElement).getAttribute('data-doc-upload-time') || '',
collapsed: ((dom as HTMLElement).getAttribute('data-doc-collapsed') || '') === 'true',
content: '',
}),
},
],
toDOM: (node) => [
'div',
{
'data-doc-block': 'true',
'data-doc-type': node.attrs.docType,
'data-doc-name': node.attrs.docName,
'data-doc-upload-time': node.attrs.uploadTime,
'data-doc-collapsed': String(Boolean(node.attrs.collapsed)),
},
],
parseMarkdown: {
match: (node) => node.type === 'docBlock',
runner: (state, node, type) => {
const attrs = node.sourceType === 'code'
? parseDocBlockValue(String(node.value || ''))
: parseLegacyDocBlock(String(node.value || ''))
if (!attrs) return
state.addNode(type, attrs)
},
},
toMarkdown: {
match: (node) => node.type.name === DOC_BLOCK_NODE_TYPE,
runner: (state, node) => {
state.addNode('html', undefined, buildLegacyDocBlock(node.attrs))
},
},
}))
export const docBlockView = $view(docBlockNode, (ctx) => {
const serializer = ctx.get(serializerCtx)
return (node, view, getPos) => new DocBlockNodeView(node, view, getPos, serializer)
})
export function buildDocContextFromDoc(doc: ProseNode, excludePos?: number) {
return buildDocContext(doc, excludePos)
}
+88 -21
View File
@@ -1,4 +1,4 @@
import { codeBlockConfig } from '@milkdown/kit/component/code-block' import { codeBlockConfig } from '@milkdown/kit/component/code-block'
import mermaid from 'mermaid' import mermaid from 'mermaid'
let mermaidReadyTheme = '' let mermaidReadyTheme = ''
@@ -24,14 +24,37 @@ function ensureMermaid() {
const theme = getMermaidTheme() const theme = getMermaidTheme()
if (mermaidReadyTheme === theme) return if (mermaidReadyTheme === theme) return
const dark = window.matchMedia?.('(prefers-color-scheme: dark)').matches const dark = theme === 'dark'
mermaid.initialize({ mermaid.initialize({
startOnLoad: false, startOnLoad: false,
theme: theme || (dark ? 'dark' : 'default'), theme: dark ? 'dark' : 'base',
securityLevel: 'loose', securityLevel: 'loose',
fontFamily: 'inherit', fontFamily: 'inherit',
flowchart: { flowchart: { htmlLabels: false },
htmlLabels: false, themeVariables: dark ? {
primaryColor: '#1e2d45',
primaryTextColor: '#c9d6e8',
primaryBorderColor: '#3b5278',
lineColor: '#5a7aa8',
secondaryColor: '#162236',
tertiaryColor: '#0f1926',
edgeLabelBackground: '#1a2a40',
clusterBkg: '#111e2e',
titleColor: '#c9d6e8',
nodeBorder: '#3b5278',
mainBkg: '#1e2d45',
} : {
primaryColor: '#e8f0fe',
primaryTextColor: '#1e3a5f',
primaryBorderColor: '#93b4d9',
lineColor: '#4a7cb5',
secondaryColor: '#dbeafe',
tertiaryColor: '#f0f7ff',
edgeLabelBackground: '#f0f7ff',
clusterBkg: '#f5f8ff',
titleColor: '#1e3a5f',
nodeBorder: '#93b4d9',
mainBkg: '#e8f0fe',
}, },
}) })
mermaidReadyTheme = theme mermaidReadyTheme = theme
@@ -69,17 +92,26 @@ function makeMermaidFilename() {
} }
function buildMermaidPreviewMarkup(code: string, token: number) { function buildMermaidPreviewMarkup(code: string, token: number) {
const encoded = encodeMermaidCode(code) // 剥离首尾的 ```mermaid 或 ``` 标识符,防止其被误认为图表节点
const cleanCode = code
.replace(/^```[a-z]*\s*\n?/i, '')
.replace(/\n?```\s*$/i, '')
.trim()
const encoded = encodeMermaidCode(cleanCode)
const filename = makeMermaidFilename() const filename = makeMermaidFilename()
const zoomSvg = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/><line x1="11" y1="8" x2="11" y2="14"/><line x1="8" y1="11" x2="14" y2="11"/></svg>`
const dlSvg = `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>`
return ` return `
<div class="mermaid-block" data-mermaid-code="${encoded}" data-mermaid-token="${token}"> <div class="mermaid-block" data-mermaid-code="${encoded}" data-mermaid-token="${token}">
<div class="mermaid-controls"> <div class="mermaid-controls">
<button type="button" class="mermaid-action-btn" data-mermaid-action="zoom" disabled>Zoom</button> <button type="button" class="mermaid-action-btn mermaid-zoom-btn" data-mermaid-action="zoom" disabled aria-label="放大查看">${zoomSvg}</button>
<button type="button" class="mermaid-action-btn" data-mermaid-action="download" data-mermaid-filename="${filename}" disabled>Download PNG</button> <button type="button" class="mermaid-action-btn mermaid-download-btn" data-mermaid-action="download" data-mermaid-filename="${filename}" disabled aria-label="下载图表">${dlSvg}<span></span></button>
</div> </div>
<div class="mermaid-inner"> <div class="mermaid-inner">
<div class="mermaid-loading">...</div> <div class="mermaid-loading"><span></span><span></span><span></span></div>
</div> </div>
</div>`.trim() </div>`.trim()
} }
@@ -102,7 +134,6 @@ function setMermaidActionsState(block: HTMLElement, payload: MermaidImagePayload
} }
if (action === 'download') { if (action === 'download') {
node.textContent = 'Download PNG'
if (payload) { if (payload) {
node.removeAttribute('disabled') node.removeAttribute('disabled')
node.setAttribute('data-mermaid-url', payload.downloadUrl || payload.previewUrl) node.setAttribute('data-mermaid-url', payload.downloadUrl || payload.previewUrl)
@@ -130,6 +161,7 @@ function setMermaidActionsState(block: HTMLElement, payload: MermaidImagePayload
} }
function getSvgSize(svg: string) { function getSvgSize(svg: string) {
// 优先读取 viewBox (取第三、四个值作为宽高)
const viewBox = svg.match(/viewBox\s*=\s*["']\s*[-\d.]+\s+[-\d.]+\s+([-\d.]+)\s+([-\d.]+)\s*["']/i) const viewBox = svg.match(/viewBox\s*=\s*["']\s*[-\d.]+\s+[-\d.]+\s+([-\d.]+)\s+([-\d.]+)\s*["']/i)
if (viewBox) { if (viewBox) {
const width = Number(viewBox[1]) const width = Number(viewBox[1])
@@ -139,14 +171,16 @@ function getSvgSize(svg: string) {
} }
} }
const widthAttr = svg.match(/width\s*=\s*["']([-\d.]+)(px)?["']/i) // 次选读取数字像素属性
const heightAttr = svg.match(/height\s*=\s*["']([-\d.]+)(px)?["']/i) const widthAttr = svg.match(/\bwidth\s*=\s*["']([\d.]+)(?:px)?["']/i)
const width = widthAttr ? Number(widthAttr[1]) : 960 const heightAttr = svg.match(/\bheight\s*=\s*["']([\d.]+)(?:px)?["']/i)
const height = heightAttr ? Number(heightAttr[1]) : 540 const attrW = widthAttr ? Number(widthAttr[1]) : 0
return { const attrH = heightAttr ? Number(heightAttr[1]) : 0
width: Number.isFinite(width) && width > 0 ? width : 960, if (attrW > 0 && attrH > 0) {
height: Number.isFinite(height) && height > 0 ? height : 540, return { width: attrW, height: attrH }
} }
return { width: 960, height: 540 }
} }
function svgToDataUrl(svg: string) { function svgToDataUrl(svg: string) {
@@ -159,6 +193,38 @@ function stripExternalSvgResources(svg: string) {
.replace(/url\(\s*["']?https?:\/\/[^"')]*["']?\s*\)/gi, 'none') .replace(/url\(\s*["']?https?:\/\/[^"')]*["']?\s*\)/gi, 'none')
} }
/** 给 SVG viewBox 四周加 padding,同步更新 width/height 并注入字体样式防止截断 */
function padSvgViewBox(svg: string, pad = 48): string {
let result = svg
// 注入显式字体样式,确保测量与渲染一致
const styleInject = `
<style>
svg { font-family: 'Inter', system-ui, sans-serif !important; }
.node text, .edgeLabel text { font-family: 'Inter', system-ui, sans-serif !important; }
</style>`
if (result.includes('</style>')) {
result = result.replace('</style>', ` svg { font-family: 'Inter', system-ui, sans-serif !important; }\n .node text, .edgeLabel text { font-family: 'Inter', system-ui, sans-serif !important; }\n</style>`)
} else {
result = result.replace(/>/, `>${styleInject}`)
}
// 匹配 viewBox
const vbMatch = result.match(/viewBox\s*=\s*["']\s*([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)\s*["']/i)
if (vbMatch) {
const x = parseFloat(vbMatch[1]) - pad
const y = parseFloat(vbMatch[2]) - pad
const w = parseFloat(vbMatch[3]) + pad * 2
const h = parseFloat(vbMatch[4]) + pad * 2
result = result.replace(vbMatch[0], `viewBox="${x} ${y} ${w} ${h}"`)
// 覆盖外层 width/height 为实际像素值
result = result.replace(/\bwidth\s*=\s*["'][^"']*["']/i, `width="${w}"`)
result = result.replace(/\bheight\s*=\s*["'][^"']*["']/i, `height="${h}"`)
}
return result
}
function getRasterDpr(width: number, height: number) { function getRasterDpr(width: number, height: number) {
const rawDpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1 const rawDpr = typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1
const baseDpr = Math.min(3, Math.max(1, rawDpr)) const baseDpr = Math.min(3, Math.max(1, rawDpr))
@@ -196,7 +262,8 @@ async function rasterizeSvgToPngDataUrl(svg: string, width: number, height: numb
} }
async function svgToImageDataUrl(svg: string): Promise<MermaidImagePayload> { async function svgToImageDataUrl(svg: string): Promise<MermaidImagePayload> {
const fallback = getSvgSize(svg) const paddedSvg = padSvgViewBox(svg)
const fallback = getSvgSize(paddedSvg)
const sourceWidth = fallback.width const sourceWidth = fallback.width
const sourceHeight = fallback.height const sourceHeight = fallback.height
@@ -208,8 +275,8 @@ async function svgToImageDataUrl(svg: string): Promise<MermaidImagePayload> {
width = Math.max(1, Math.round(width * scale)) width = Math.max(1, Math.round(width * scale))
height = Math.max(1, Math.round(height * scale)) height = Math.max(1, Math.round(height * scale))
const normalizedSvg = stripExternalSvgResources(svg) const normalizedSvg = stripExternalSvgResources(paddedSvg)
const candidates = normalizedSvg === svg ? [svg] : [svg, normalizedSvg] const candidates = normalizedSvg === paddedSvg ? [paddedSvg] : [paddedSvg, normalizedSvg]
let lastError: unknown = null let lastError: unknown = null
for (const candidate of candidates) { for (const candidate of candidates) {
@@ -270,7 +337,7 @@ async function renderMermaidBlock(block: HTMLElement, token: number): Promise<vo
const encodedCode = block.getAttribute('data-mermaid-code') || '' const encodedCode = block.getAttribute('data-mermaid-code') || ''
const code = decodeMermaidCode(encodedCode).trim() || 'graph TD\nA-->B' const code = decodeMermaidCode(encodedCode).trim() || 'graph TD\nA-->B'
inner.innerHTML = '<div class="mermaid-loading">...</div>' inner.innerHTML = '<div class="mermaid-loading"><span></span><span></span><span></span></div>'
setMermaidActionsState(block, null) setMermaidActionsState(block, null)
try { try {
+12 -2
View File
@@ -3,8 +3,18 @@ import { createRouter, createWebHistory } from 'vue-router'
const routes = [ const routes = [
{ {
path: '/', path: '/',
name: 'Home', name: 'Editor',
component: () => import('../App.vue') component: () => import('../views/EditorView.vue')
},
{
path: '/docs',
name: 'Docs',
component: () => import('../views/DocsView.vue')
},
{
path: '/univer',
name: 'Univer',
component: () => import('../views/UniverView.vue')
} }
] ]
+135
View File
@@ -0,0 +1,135 @@
/**
* Office 文件类型检测工具
*/
import { OfficeFormat, OfficePresetType } from './univerBridge'
/**
* 支持的 Office 文件扩展名
*/
export const SUPPORTED_EXTENSIONS = {
[OfficeFormat.DOCX]: ['.docx'],
[OfficeFormat.XLSX]: ['.xlsx'],
[OfficeFormat.PPTX]: ['.pptx']
}
/**
* MIME 类型映射
*/
export const MIME_TYPES = {
[OfficeFormat.DOCX]: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
[OfficeFormat.XLSX]: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
[OfficeFormat.PPTX]: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
}
/**
* 检测文件是否为 Office 文件
*/
export function isOfficeFile(file) {
if (!file) return false
const filename = file.name?.toLowerCase() || ''
const type = file.type?.toLowerCase() || ''
// 检查扩展名
for (const [format, exts] of Object.entries(SUPPORTED_EXTENSIONS)) {
if (exts.some(ext => filename.endsWith(ext))) {
return true
}
}
// 检查 MIME 类型
for (const [format, mime] of Object.entries(MIME_TYPES)) {
if (type === mime) {
return true
}
}
return false
}
/**
* 获取文件的 Office 格式
*/
export function getOfficeFormat(file) {
if (!file) return null
const filename = file.name?.toLowerCase() || ''
const type = file.type?.toLowerCase() || ''
// 检查扩展名
for (const [format, exts] of Object.entries(SUPPORTED_EXTENSIONS)) {
if (exts.some(ext => filename.endsWith(ext))) {
return format
}
}
// 检查 MIME 类型
for (const [format, mime] of Object.entries(MIME_TYPES)) {
if (type === mime) {
return format
}
}
return null
}
/**
* 获取文件图标类型
*/
export function getOfficeIcon(format) {
switch (format) {
case OfficeFormat.DOCX:
return 'doc'
case OfficeFormat.XLSX:
return 'xls'
case OfficeFormat.PPTX:
return 'ppt'
default:
return 'file'
}
}
/**
* 获取格式显示名称
*/
export function getFormatDisplayName(format, locale = 'zh-CN') {
const names = {
'zh-CN': {
[OfficeFormat.DOCX]: 'Word 文档',
[OfficeFormat.XLSX]: 'Excel 表格',
[OfficeFormat.PPTX]: 'PowerPoint 演示文稿'
},
'en-US': {
[OfficeFormat.DOCX]: 'Word Document',
[OfficeFormat.XLSX]: 'Excel Spreadsheet',
[OfficeFormat.PPTX]: 'PowerPoint Presentation'
}
}
return names[locale]?.[format] || format?.toUpperCase() || '未知格式'
}
/**
* 获取对应的 Preset 类型
*/
export function getPresetTypeByFormat(format) {
switch (format) {
case OfficeFormat.DOCX:
case OfficeFormat.PPTX:
return OfficePresetType.DOCS
case OfficeFormat.XLSX:
return OfficePresetType.SHEETS
default:
return null
}
}
export default {
isOfficeFile,
getOfficeFormat,
getOfficeIcon,
getFormatDisplayName,
getPresetTypeByFormat,
SUPPORTED_EXTENSIONS,
MIME_TYPES
}
+265
View File
@@ -0,0 +1,265 @@
/**
* Univer 编辑器桥接服务
* 封装 Univer 的初始化加载导出等操作
*/
import { createUniver, LocaleType, merge } from '@univerjs/presets'
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
// 导入样式
import '@univerjs/preset-docs-core/lib/index.css'
import '@univerjs/preset-sheets-core/lib/index.css'
// 导入语言包
import DocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'
import SheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import DocsCoreZhCN from '@univerjs/preset-docs-core/locales/zh-CN'
import SheetsCoreZhCN from '@univerjs/preset-sheets-core/locales/zh-CN'
export const OfficeFormat = {
DOCX: 'docx',
XLSX: 'xlsx',
PPTX: 'pptx'
}
export const OfficePresetType = {
DOCS: 'docs',
SHEETS: 'sheets',
SLIDES: 'slides'
}
/**
* 根据文件扩展名判断 Office 格式
*/
export function detectOfficeFormat(filename) {
const ext = filename?.toLowerCase().split('.').pop() || ''
if (ext === 'docx') return OfficeFormat.DOCX
if (ext === 'xlsx') return OfficeFormat.XLSX
if (ext === 'pptx') return OfficeFormat.PPTX
return null
}
/**
* 根据格式获取对应的 Preset 类型
*/
export function getPresetType(format) {
switch (format) {
case OfficeFormat.DOCX:
return OfficePresetType.DOCS
case OfficeFormat.XLSX:
return OfficePresetType.SHEETS
case OfficeFormat.PPTX:
return OfficePresetType.SLIDES
default:
return null
}
}
/**
* 创建 Univer 实例
*/
export async function createUniverInstance(container, options = {}) {
const {
format = OfficeFormat.DOCX,
locale = 'zh-CN',
theme = 'light'
} = options
const localeType = locale === 'zh-CN' ? LocaleType.ZH_CN : LocaleType.EN_US
const locales = locale === 'zh-CN'
? { [LocaleType.ZH_CN]: merge(DocsCoreZhCN, SheetsCoreZhCN) }
: { [LocaleType.EN_US]: merge(DocsCoreEnUS, SheetsCoreEnUS) }
const presets = []
// 根据格式添加对应的 Preset
if (format === OfficeFormat.DOCX || format === OfficeFormat.PPTX) {
presets.push(UniverDocsCorePreset({
container,
theme: theme === 'dark' ? 'dark' : 'default'
}))
}
if (format === OfficeFormat.XLSX) {
presets.push(UniverSheetsCorePreset({
container,
theme: theme === 'dark' ? 'dark' : 'default'
}))
}
// 默认使用 Docs 作为兜底
if (presets.length === 0) {
presets.push(UniverDocsCorePreset({
container,
theme: theme === 'dark' ? 'dark' : 'default'
}))
}
const { univer, univerAPI } = createUniver({
locale: localeType,
locales,
presets,
collaboration: false // 纯前端模式,不启用协作
})
return { univer, univerAPI }
}
/**
* Univer 编辑器实例包装类
*/
export class UniverEditorInstance {
constructor() {
this.univer = null
this.univerAPI = null
this.container = null
this.currentFormat = null
}
/**
* 初始化编辑器
*/
async init(container, options = {}) {
if (this.univer) {
await this.destroy()
}
this.container = container
this.currentFormat = options.format || OfficeFormat.DOCX
const result = await createUniverInstance(container, {
format: this.currentFormat,
...options
})
this.univer = result.univer
this.univerAPI = result.univerAPI
// 创建初始文档
if (this.currentFormat === OfficeFormat.XLSX) {
this.univerAPI.createWorkbook({})
} else {
this.univerAPI.createUniverDoc({})
}
return this
}
/**
* 从字节数组加载文档
*/
async loadFromBytes(bytes, format) {
if (!this.univerAPI) {
throw new Error('Univer 实例未初始化')
}
// 注意:纯前端模式下,Univer 不支持直接从 DOCX/XLSX/PPTX 字节流加载
// 这里需要使用快照模式或后端服务来解析
// 当前实现为占位,实际需要配合快照格式
console.warn('纯前端模式暂不支持从 DOCX/XLSX/PPTX 字节流加载,请使用快照模式')
return false
}
/**
* 导出为快照数据
*/
async exportSnapshot() {
if (!this.univerAPI) {
throw new Error('Univer 实例未初始化')
}
const activeDoc = this.univerAPI.getActiveDocument()
const activeSheet = this.univerAPI.getActiveWorkbook()
if (activeSheet) {
return {
type: OfficePresetType.SHEETS,
format: OfficeFormat.XLSX,
data: activeSheet.getSnapshot()
}
}
if (activeDoc) {
return {
type: OfficePresetType.DOCS,
format: OfficeFormat.DOCX,
data: activeDoc.getSnapshot()
}
}
return null
}
/**
* 从快照数据导入
*/
async importSnapshot(snapshot) {
if (!this.univerAPI || !snapshot?.data) {
throw new Error('无效的快照数据')
}
// 快照数据可以直接用于恢复文档状态
// 具体实现取决于 Univer API
console.log('导入快照:', snapshot.type, snapshot.format)
return true
}
/**
* 监听文档变化
*/
onChange(callback) {
if (!this.univerAPI) return
// Univer API 的事件监听
this.univerAPI.addEvent(this.univerAPI.Event.CommandExecuted, (event) => {
callback({
type: 'command',
data: event
})
})
}
/**
* 销毁实例
*/
async destroy() {
if (this.univer) {
this.univer.dispose()
this.univer = null
this.univerAPI = null
this.container = null
this.currentFormat = null
}
}
/**
* 获取当前格式
*/
getFormat() {
return this.currentFormat
}
/**
* 检查是否已初始化
*/
isInitialized() {
return this.univer !== null && this.univerAPI !== null
}
}
/**
* 创建 Univer 编辑器实例
*/
export function createUniverEditor() {
return new UniverEditorInstance()
}
export default {
createUniverInstance,
createUniverEditor,
detectOfficeFormat,
getPresetType,
OfficeFormat,
OfficePresetType,
UniverEditorInstance
}
+138
View File
@@ -0,0 +1,138 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { OfficeFormat, OfficePresetType } from '../services/univerBridge'
export const useOfficeStore = defineStore('office', () => {
// 当前文档状态
const currentFileName = ref('')
const currentFormat = ref(null)
const currentFileSize = ref(0)
const currentBytes = ref(null)
// 快照模式
const isSnapshotMode = ref(true) // 默认启用快照模式
const currentSnapshot = ref(null)
// 编辑状态
const isEditing = ref(false)
const hasUnsavedChanges = ref(false)
// 视图状态
const activeView = ref('milkdown') // 'milkdown' | 'univer'
// 计算属性
const hasDocument = computed(() => {
return currentFileName.value && currentFormat.value
})
const documentInfo = computed(() => {
if (!hasDocument.value) return null
return {
name: currentFileName.value,
format: currentFormat.value,
size: currentFileSize.value,
isSnapshot: isSnapshotMode.value
}
})
/**
* 设置当前文档
*/
function setCurrentDocument(file, bytes) {
if (!file) {
clearCurrentDocument()
return
}
currentFileName.value = file.name || '未命名'
currentFormat.value = getFormatFromFileName(file.name)
currentFileSize.value = file.size || 0
currentBytes.value = bytes
hasUnsavedChanges.value = false
}
/**
* 清除当前文档
*/
function clearCurrentDocument() {
currentFileName.value = ''
currentFormat.value = null
currentFileSize.value = 0
currentBytes.value = null
currentSnapshot.value = null
hasUnsavedChanges.value = false
}
/**
* 设置快照数据
*/
function setSnapshot(snapshot) {
currentSnapshot.value = snapshot
hasUnsavedChanges.value = false
}
/**
* 标记有未保存的更改
*/
function markAsChanged() {
hasUnsavedChanges.value = true
}
/**
* 切换视图
*/
function switchView(view) {
activeView.value = view
}
/**
* 切换快照模式
*/
function toggleSnapshotMode() {
isSnapshotMode.value = !isSnapshotMode.value
}
return {
// 状态
currentFileName,
currentFormat,
currentFileSize,
currentBytes,
isSnapshotMode,
currentSnapshot,
isEditing,
hasUnsavedChanges,
activeView,
// 计算属性
hasDocument,
documentInfo,
// 方法
setCurrentDocument,
clearCurrentDocument,
setSnapshot,
markAsChanged,
switchView,
toggleSnapshotMode
}
})
/**
* 从文件名获取格式
*/
function getFormatFromFileName(filename) {
const ext = filename?.toLowerCase().split('.').pop() || ''
switch (ext) {
case 'docx':
return OfficeFormat.DOCX
case 'xlsx':
return OfficeFormat.XLSX
case 'pptx':
return OfficeFormat.PPTX
default:
return null
}
}
export default useOfficeStore
+1 -1
View File
@@ -13,7 +13,7 @@ export const useSettingsStore = defineStore('settings', () => {
const debounceMs = ref(1000) // 1000 - 5000 const debounceMs = ref(1000) // 1000 - 5000
// 3. Privacy // 3. Privacy
const privacyMode = ref(false) const privacyMode = ref(true)
// 4. Preferences // 4. Preferences
const language = ref('auto') const language = ref('auto')
+233 -75
View File
@@ -37,16 +37,21 @@
--toggle-moon: #475569; --toggle-moon: #475569;
--ghost-text: #7d8796; --ghost-text: #7d8796;
--ghost-code-bg: rgba(15, 23, 42, 0.06); --ghost-code-bg: rgba(15, 23, 42, 0.06);
--mermaid-max-height: 420px; --code-inline-bg: rgba(15, 23, 42, 0.06);
--code-block-bg: #f8fafc;
--code-block-border: rgba(148, 163, 184, 0.28);
--code-text: #0f172a;
--mermaid-max-height: 480px;
--mermaid-mobile-max-height: 320px; --mermaid-mobile-max-height: 320px;
--mermaid-action-bg: linear-gradient(180deg, #ffffff 0%, #f4f7fc 100%); --mermaid-glass-bg: rgba(255, 255, 255, 0.55);
--mermaid-action-hover-bg: linear-gradient(180deg, #ffffff 0%, #e9f2ff 100%); --mermaid-glass-border: rgba(180, 200, 230, 0.5);
--mermaid-action-fg: #1f2937; --mermaid-glass-border-hover: rgba(59, 130, 246, 0.45);
--mermaid-action-border: #cfd8e6; --mermaid-glass-shadow: 0 4px 24px rgba(15, 23, 42, 0.08), 0 1px 4px rgba(15, 23, 42, 0.05);
--mermaid-action-shadow: 0 1px 2px rgba(15, 23, 42, 0.08); --mermaid-glass-shadow-hover: 0 8px 32px rgba(59, 130, 246, 0.12), 0 2px 8px rgba(15, 23, 42, 0.08);
--mermaid-action-shadow-hover: 0 4px 10px rgba(37, 99, 235, 0.16); --mermaid-btn-bg: rgba(255, 255, 255, 0.8);
--mermaid-action-disabled-bg: rgba(148, 163, 184, 0.18); --mermaid-btn-hover-bg: rgba(239, 246, 255, 0.95);
--mermaid-action-disabled-fg: #9aa4b2; --mermaid-btn-fg: #374151;
--mermaid-btn-border: rgba(180, 200, 230, 0.6);
--crepe-color-background: #ffffff; --crepe-color-background: #ffffff;
--crepe-color-on-background: #000000; --crepe-color-on-background: #000000;
@@ -95,14 +100,21 @@
--toggle-moon: #e2e8f0; --toggle-moon: #e2e8f0;
--ghost-text: #95a0b4; --ghost-text: #95a0b4;
--ghost-code-bg: rgba(226, 232, 240, 0.12); --ghost-code-bg: rgba(226, 232, 240, 0.12);
--mermaid-action-bg: linear-gradient(180deg, #30394b 0%, #242c3a 100%); --code-inline-bg: rgba(30, 41, 59, 0.92);
--mermaid-action-hover-bg: linear-gradient(180deg, #3a4760 0%, #2a3445 100%); --code-block-bg: rgba(15, 23, 42, 0.92);
--mermaid-action-fg: #e5e7eb; --code-block-border: rgba(71, 85, 105, 0.55);
--mermaid-action-border: #3e4a61; --code-text: #e2e8f0;
--mermaid-action-shadow: 0 1px 2px rgba(2, 6, 23, 0.45); --mermaid-max-height: 480px;
--mermaid-action-shadow-hover: 0 5px 12px rgba(2, 6, 23, 0.55); --mermaid-mobile-max-height: 320px;
--mermaid-action-disabled-bg: rgba(82, 93, 110, 0.3); --mermaid-glass-bg: rgba(20, 28, 44, 0.6);
--mermaid-action-disabled-fg: #9aa4b2; --mermaid-glass-border: rgba(60, 80, 120, 0.4);
--mermaid-glass-border-hover: rgba(96, 165, 250, 0.45);
--mermaid-glass-shadow: 0 4px 24px rgba(0, 0, 0, 0.3), 0 1px 4px rgba(0, 0, 0, 0.2);
--mermaid-glass-shadow-hover: 0 8px 32px rgba(96, 165, 250, 0.15), 0 2px 8px rgba(0, 0, 0, 0.3);
--mermaid-btn-bg: rgba(40, 52, 75, 0.85);
--mermaid-btn-hover-bg: rgba(55, 70, 100, 0.95);
--mermaid-btn-fg: #c9d6e8;
--mermaid-btn-border: rgba(70, 95, 140, 0.55);
--crepe-color-background: #1a1a1a; --crepe-color-background: #1a1a1a;
--crepe-color-on-background: #e6e6e6; --crepe-color-on-background: #e6e6e6;
@@ -210,114 +222,158 @@ body {
/* ── Mermaid diagram blocks ─────────────────────────────────────────── */ /* ── Mermaid diagram blocks ─────────────────────────────────────────── */
.mermaid-block { .mermaid-block {
position: relative;
display: block; display: block;
width: fit-content; width: 100%;
max-width: 100%; max-width: 100%;
margin: 1em auto; margin: 1.25em 0;
padding: 12px; padding: 0;
background: var(--crepe-color-surface, #f7f7f7); background: var(--mermaid-glass-bg);
border: 1px solid var(--panel-border, #d7deea); border: 1px solid var(--mermaid-glass-border);
border-radius: 10px; border-radius: 16px;
transition: border-color 160ms ease, box-shadow 160ms ease, background-color 160ms ease; box-shadow: var(--mermaid-glass-shadow);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
transition: border-color 240ms ease, box-shadow 240ms ease;
overflow: hidden;
} }
.mermaid-block:hover { .mermaid-block:hover {
border-color: var(--focus-ring, #3b82f6); border-color: var(--mermaid-glass-border-hover);
box-shadow: var(--mermaid-glass-shadow-hover);
} }
/* 悬浮控制栏:右上角,hover 时渐现 */
.mermaid-controls { .mermaid-controls {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
display: flex; display: flex;
justify-content: flex-end; align-items: center;
gap: 8px; gap: 6px;
margin-bottom: 10px; opacity: 0;
flex-wrap: wrap; transform: translateY(-4px);
transition: opacity 200ms ease, transform 200ms ease;
pointer-events: none;
}
.mermaid-block:hover .mermaid-controls {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
} }
.mermaid-action-btn { .mermaid-action-btn {
appearance: none; appearance: none;
border: 1px solid var(--mermaid-action-border); border: 1px solid var(--mermaid-btn-border);
border-radius: 999px; background: var(--mermaid-btn-bg);
padding: 6px 12px; color: var(--mermaid-btn-fg);
font-size: 12px;
font-weight: 600;
letter-spacing: 0.01em;
line-height: 1.2;
background: var(--mermaid-action-bg);
color: var(--mermaid-action-fg);
cursor: pointer; cursor: pointer;
box-shadow: var(--mermaid-action-shadow); display: inline-flex;
transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease, background 160ms ease; align-items: center;
justify-content: center;
gap: 5px;
font-size: 12px;
font-weight: 500;
line-height: 1;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
box-shadow: 0 1px 4px rgba(0,0,0,0.15), inset 0 1px 0 rgba(255,255,255,0.1);
transition: background 160ms ease, border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}
.mermaid-zoom-btn {
width: 30px;
height: 30px;
padding: 0;
border-radius: 50%;
}
.mermaid-download-btn {
height: 30px;
padding: 0 12px;
border-radius: 999px;
} }
.mermaid-action-btn:hover:not([disabled]) { .mermaid-action-btn:hover:not([disabled]) {
background: var(--mermaid-btn-hover-bg);
border-color: var(--focus-ring); border-color: var(--focus-ring);
background: var(--mermaid-action-hover-bg); box-shadow: 0 0 0 3px rgba(59,130,246,0.18), 0 2px 8px rgba(0,0,0,0.2);
box-shadow: var(--mermaid-action-shadow-hover);
transform: translateY(-1px); transform: translateY(-1px);
} }
.mermaid-action-btn:active:not([disabled]) { .mermaid-action-btn:active:not([disabled]) {
transform: translateY(0); transform: translateY(0);
box-shadow: 0 1px 4px rgba(0,0,0,0.15);
} }
.mermaid-action-btn[disabled] { .mermaid-action-btn[disabled] {
background: var(--mermaid-action-disabled-bg); opacity: 0.35;
color: var(--mermaid-action-disabled-fg);
cursor: not-allowed; cursor: not-allowed;
pointer-events: none;
} }
.mermaid-inner { .mermaid-inner {
display: block; display: block;
max-width: 100%; width: 100%;
max-height: var(--mermaid-max-height); max-height: var(--mermaid-max-height);
overflow: auto; overflow: auto;
border-radius: 8px; padding: 20px;
padding: 8px; background: transparent;
background: color-mix(in srgb, var(--crepe-color-background, #fff) 88%, transparent);
}
.mermaid-inner::-webkit-scrollbar {
width: 8px;
height: 8px;
} }
.mermaid-inner::-webkit-scrollbar { width: 6px; height: 6px; }
.mermaid-inner::-webkit-scrollbar-thumb { .mermaid-inner::-webkit-scrollbar-thumb {
background-color: var(--scrollbar-thumb); background-color: var(--scrollbar-thumb);
border-radius: 4px; border-radius: 3px;
}
.mermaid-inner::-webkit-scrollbar-thumb:hover {
background-color: var(--scrollbar-thumb-hover);
} }
.mermaid-image { .mermaid-image {
display: block; display: block;
width: auto; width: auto;
height: auto; height: auto;
max-width: none; max-width: 100%;
margin: 0 auto; margin: 0 auto;
border-radius: 6px;
} }
.mermaid-inner svg { .mermaid-inner svg { display: block; margin: 0 auto; }
display: block;
}
/* 三点弹跳加载动画 */
.mermaid-loading { .mermaid-loading {
padding: 24px; display: flex;
text-align: center; align-items: center;
font-size: 1.4em; justify-content: center;
color: var(--muted-text, #6b7280); gap: 6px;
letter-spacing: 0.2em; padding: 36px;
animation: mermaid-pulse 1.2s ease-in-out infinite;
} }
@keyframes mermaid-pulse { .mermaid-loading span {
0%, 100% { opacity: 0.4; } display: inline-block;
50% { opacity: 1; } width: 7px;
height: 7px;
border-radius: 50%;
background: var(--muted-text, #6b7280);
animation: mermaid-bounce 1.2s ease-in-out infinite;
}
.mermaid-loading span:nth-child(2) { animation-delay: 0.2s; }
.mermaid-loading span:nth-child(3) { animation-delay: 0.4s; }
@keyframes mermaid-bounce {
0%, 80%, 100% { transform: scale(0.55); opacity: 0.35; }
40% { transform: scale(1); opacity: 1; }
} }
.mermaid-error { .mermaid-error {
margin: 12px;
padding: 12px 16px; padding: 12px 16px;
margin: 0; background: rgba(220, 38, 38, 0.08);
background: color-mix(in srgb, var(--danger-text, #dc2626) 8%, transparent);
border: 1px solid var(--danger-text, #dc2626); border: 1px solid var(--danger-text, #dc2626);
border-radius: 6px; border-radius: 8px;
color: var(--danger-text, #dc2626); color: var(--danger-text, #dc2626);
font-size: 12px; font-size: 12px;
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Mono', monospace; font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Mono', monospace;
@@ -325,20 +381,122 @@ body {
word-break: break-word; word-break: break-word;
} }
:root[data-theme='dark'] .milkdown .katex { :root .milkdown .katex {
color: var(--crepe-color-on-background);
}
:root[data-theme='dark'] .milkdown .katex .mord,
:root[data-theme='dark'] .milkdown .katex .mbin,
:root[data-theme='dark'] .milkdown .katex .mrel,
:root[data-theme='dark'] .milkdown .katex .minner {
color: var(--crepe-color-on-background);
}
:root[data-theme='dark'] .milkdown .katex .mord.text {
color: var(--crepe-color-on-background); color: var(--crepe-color-on-background);
} }
:root[data-theme='dark'] .milkdown .cm-editor, :root[data-theme='dark'] .milkdown .cm-editor,
:root[data-theme='dark'] .milkdown .cm-scroller { :root[data-theme='dark'] .milkdown .cm-scroller {
background-color: color-mix(in srgb, var(--crepe-color-surface-low) 86%, transparent); background-color: var(--code-block-bg);
color: var(--crepe-color-on-surface); color: var(--code-text);
} }
:root[data-theme='dark'] .milkdown .cm-gutters { :root[data-theme='dark'] .milkdown .cm-gutters {
background-color: color-mix(in srgb, var(--crepe-color-surface-low) 86%, transparent); background-color: var(--code-block-bg);
color: var(--crepe-color-on-surface-variant); color: var(--crepe-color-on-surface-variant);
border-right-color: var(--panel-border); border-right: 1px solid var(--code-block-border);
}
:root[data-theme='dark'] .milkdown .cm-activeLine {
background-color: rgba(255, 255, 255, 0.05);
}
:root[data-theme='dark'] .milkdown .cm-activeLineGutter {
background-color: rgba(255, 255, 255, 0.08);
}
:root[data-theme='dark'] .milkdown .cm-selectionBackground,
:root[data-theme='dark'] .milkdown .cm-selectionBackground::selection {
background-color: rgba(59, 130, 246, 0.25);
}
:root[data-theme='dark'] .milkdown .cm-cursor {
border-left-color: var(--crepe-color-on-background);
}
:root .milkdown .ProseMirror pre,
:root .milkdown .ProseMirror code {
background: var(--code-inline-bg);
color: var(--code-text);
border: 1px solid var(--code-block-border);
}
:root .milkdown .ProseMirror pre {
padding: 12px 16px;
border-radius: 8px;
background: var(--code-block-bg);
}
:root .milkdown .ProseMirror pre code {
background: none;
border: none;
padding: 0;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.comment,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.prolog,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.doctype,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.cdata {
color: #6b7280;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.punctuation {
color: #9ca3af;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.property,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.tag,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.boolean,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.number,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.constant,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.symbol,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.deleted {
color: #f472b6;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.selector,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.attr-name,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.string,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.char,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.builtin,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.inserted {
color: #a78bfa;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.operator,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.entity,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.url,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .language-css .token.string,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .style .token.string {
color: #60a5fa;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.atrule,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.attr-value,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.keyword {
color: #34d399;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.function,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.class-name {
color: #fbbf24;
}
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.regex,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.important,
:root[data-theme='dark'] .milkdown .ProseMirror pre code .token.variable {
color: #fb923c;
} }
@media (max-width: 768px) { @media (max-width: 768px) {
+34 -58
View File
@@ -1,10 +1,6 @@
import { API_URL } from './config.js' import { API_URL, API_KEY, TTS_URL, TTS_STATUS_URL } from './config.js'
import { useSettingsStore } from '../stores/settings' import { useSettingsStore } from '../stores/settings'
const API_KEY = 'your-secret-key-here'
let cachedIP = null
function generateRequestId() { function generateRequestId() {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID() return crypto.randomUUID()
@@ -46,20 +42,6 @@ async function sendCancelRequest(cancelUrl, requestId, reason) {
} }
} }
async function getClientIP() {
if (cachedIP) return cachedIP
try {
const controller = new AbortController()
setTimeout(() => controller.abort(), 3000)
const res = await fetch('https://api.ipify.org?format=json', { signal: controller.signal })
const data = await res.json()
cachedIP = data.ip
return cachedIP
} catch {
return null
}
}
export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl = API_URL) { export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl = API_URL) {
let normalizedLanguageId = 'markdown' let normalizedLanguageId = 'markdown'
if (typeof languageId === 'string' && languageId.trim()) { if (typeof languageId === 'string' && languageId.trim()) {
@@ -89,16 +71,10 @@ export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl
try { try {
const settings = useSettingsStore() const settings = useSettingsStore()
const clientIP = await getClientIP()
const headers = { const headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-API-Key': API_KEY,
'X-Request-Id': requestId, 'X-Request-Id': requestId,
} 'X-API-Key': API_KEY,
// Only send IP if privacy mode is OFF
if (clientIP && !settings.privacyMode) {
headers['X-Client-IP'] = clientIP
} }
const body = { const body = {
@@ -126,38 +102,8 @@ export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl
throw new Error(`HTTP ${res.status}: ${errorText}`) throw new Error(`HTTP ${res.status}: ${errorText}`)
} }
const reader = res.body?.getReader() const data = await res.json()
if (!reader) { return data.content || ''
throw new Error('No reader available')
}
let text = ''
let buffer = ''
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += new TextDecoder().decode(value)
const lines = buffer.split('\n')
buffer = lines.pop() || ''
for (const line of lines) {
if (!line.startsWith('data: ')) continue
const jsonStr = line.slice(6).trim()
if (!jsonStr) continue
try {
const data = JSON.parse(jsonStr)
if (data.content) {
text += data.content
}
if (data.done || data.error) break
} catch (e) {
// skip invalid lines
}
}
}
return text
} catch (e) { } catch (e) {
if (e.name === 'AbortError') { if (e.name === 'AbortError') {
// ignore abort // ignore abort
@@ -170,3 +116,33 @@ export async function fetchSuggestion(prefix, suffix, languageId, signal, apiUrl
} }
} }
} }
export async function fetchTTS(text, voice = 'af_bella', rate = 1.0, apiUrl = TTS_URL) {
const res = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': API_KEY,
},
body: JSON.stringify({ text, voice, rate, format: 'wav' }),
})
if (!res.ok) {
const errorText = await res.text()
throw new Error(`TTS HTTP ${res.status}: ${errorText}`)
}
return res.json()
}
export async function fetchTTSStatus(apiUrl = TTS_STATUS_URL) {
const res = await fetch(apiUrl, {
headers: { 'X-API-Key': API_KEY },
})
if (!res.ok) {
throw new Error(`TTS Status HTTP ${res.status}`)
}
return res.json()
}
+8 -1
View File
@@ -1,5 +1,12 @@
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://api.imageteach.tech:8002' export const DEBUG = import.meta.env.DEV
const DEFAULT_API_BASE_URL = import.meta.env.DEV ? '' : 'https://api.imageteach.tech:8002'
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || DEFAULT_API_BASE_URL
export const API_URL = import.meta.env.VITE_API_URL || `${API_BASE_URL}/v1/completions` export const API_URL = import.meta.env.VITE_API_URL || `${API_BASE_URL}/v1/completions`
export const OCR_URL = import.meta.env.VITE_OCR_URL || `${API_BASE_URL}/v1/ocr` export const OCR_URL = import.meta.env.VITE_OCR_URL || `${API_BASE_URL}/v1/ocr`
export const CONVERT_URL = import.meta.env.VITE_CONVERT_URL || `${API_BASE_URL}/v1/convert` export const CONVERT_URL = import.meta.env.VITE_CONVERT_URL || `${API_BASE_URL}/v1/convert`
export const EXPORT_PDF_URL = import.meta.env.VITE_EXPORT_PDF_URL || '/v1/export/pdf'
export const TTS_URL = import.meta.env.VITE_TTS_URL || `${API_BASE_URL}/v1/tts-asr/tts`
export const TTS_STATUS_URL = import.meta.env.VITE_TTS_STATUS_URL || `${API_BASE_URL}/v1/tts-asr/status`
export const API_KEY = import.meta.env.VITE_API_KEY || 'your-secret-key-here'
+1 -3
View File
@@ -1,7 +1,5 @@
import { CONVERT_URL } from './config.js' import { CONVERT_URL } from './config.js'
const API_KEY = 'your-secret-key-here'
function readFileAsBase64(file) { function readFileAsBase64(file) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const reader = new FileReader() const reader = new FileReader()
@@ -25,7 +23,7 @@ export async function convertFileToMarkdown(file) {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-API-Key': API_KEY, 'X-API-Key': 'your-secret-key-here',
}, },
body: JSON.stringify({ body: JSON.stringify({
file: base64, file: base64,
+194
View File
@@ -0,0 +1,194 @@
export const DOC_BLOCK_NODE_TYPE = 'doc_block'
export const DOC_BLOCK_FENCE_LANG = 'llm-file'
export const DOC_CONTEXT_LIMIT = 32 * 1024
const IMAGE_MD_RE = /!\[[^\]]*]\([^)]+\)/g
const IMAGE_HTML_RE = /<img\b[^>]*>/gi
const HEADER_SEPARATOR = '\n---\n'
export function normalizeDocType(value = '') {
const lower = String(value || '').trim().toLowerCase()
if (lower === 'txt' || lower === 'text' || lower === 'plain') return 'txt'
if (lower === 'json') return 'json'
if (lower === 'toml') return 'toml'
if (lower === 'yaml' || lower === 'yml') return 'yaml'
if (lower === 'doc' || lower === 'docx' || lower === 'word') return 'docx'
if (lower === 'ppt' || lower === 'pptx' || lower === 'powerpoint') return 'pptx'
if (lower === 'pdf') return 'pdf'
return 'txt'
}
export function getDocTypeFromFilename(name = '') {
const lower = String(name || '').toLowerCase()
if (lower.endsWith('.docx')) return 'docx'
if (lower.endsWith('.pptx')) return 'pptx'
if (lower.endsWith('.pdf')) return 'pdf'
if (lower.endsWith('.json')) return 'json'
if (lower.endsWith('.toml')) return 'toml'
if (lower.endsWith('.yaml') || lower.endsWith('.yml')) return 'yaml'
return 'txt'
}
export function isSupportedDocFile(file) {
if (!file) return false
const name = String(file.name || '').toLowerCase()
const type = String(file.type || '').toLowerCase()
return (
name.endsWith('.txt') ||
name.endsWith('.json') ||
name.endsWith('.toml') ||
name.endsWith('.yaml') ||
name.endsWith('.yml') ||
name.endsWith('.docx') ||
name.endsWith('.pptx') ||
name.endsWith('.pdf') ||
type === 'text/plain' ||
type === 'application/json' ||
type === 'text/yaml' ||
type === 'text/x-yaml' ||
type === 'application/x-yaml' ||
type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
type === 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ||
type === 'application/pdf'
)
}
export function sanitizeDocContent(markdown = '') {
return String(markdown || '')
.replace(/\r\n?/g, '\n')
.replace(IMAGE_MD_RE, '')
.replace(IMAGE_HTML_RE, '')
.replace(/\n{3,}/g, '\n\n')
.trim()
}
function quoteMeta(value = '') {
return JSON.stringify(String(value ?? ''))
}
function parseMetaLine(line = '') {
const idx = line.indexOf(':')
if (idx < 0) return null
const key = line.slice(0, idx).trim()
const rawValue = line.slice(idx + 1).trim()
if (!key) return null
try {
return [key, JSON.parse(rawValue)]
} catch {
return [key, rawValue]
}
}
function pickFence(content = '') {
const matches = String(content || '').match(/`{3,}/g) || []
const maxLen = matches.reduce((max, item) => Math.max(max, item.length), 2)
return '`'.repeat(maxLen + 1)
}
export function buildDocBlockValue(attrs = {}) {
const docType = normalizeDocType(attrs.docType)
const docName = String(attrs.docName || `document.${docType}`)
const uploadTime = String(attrs.uploadTime || new Date().toISOString())
const collapsed = Boolean(attrs.collapsed)
const content = sanitizeDocContent(attrs.content || '')
return [
`type: ${quoteMeta(docType)}`,
`name: ${quoteMeta(docName)}`,
`uploadTime: ${quoteMeta(uploadTime)}`,
`collapsed: ${collapsed ? 'true' : 'false'}`,
'---',
content,
].join('\n')
}
export function parseDocBlockValue(raw = '') {
const normalized = String(raw || '').replace(/\r\n?/g, '\n')
const separatorIndex = normalized.indexOf(HEADER_SEPARATOR)
const headerText = separatorIndex >= 0 ? normalized.slice(0, separatorIndex) : ''
const bodyText = separatorIndex >= 0 ? normalized.slice(separatorIndex + HEADER_SEPARATOR.length) : normalized
const attrs = {
docType: 'txt',
docName: 'document.txt',
uploadTime: '',
collapsed: false,
content: sanitizeDocContent(bodyText),
}
for (const line of headerText.split('\n')) {
const parsed = parseMetaLine(line)
if (!parsed) continue
const [key, value] = parsed
if (key === 'type') attrs.docType = normalizeDocType(value)
if (key === 'name' && value) attrs.docName = String(value)
if (key === 'uploadTime' && value) attrs.uploadTime = String(value)
if (key === 'collapsed') attrs.collapsed = value === true || value === 'true'
}
if (!attrs.docName) attrs.docName = `document.${attrs.docType}`
return attrs
}
export function buildDocBlockMarkdown(attrs = {}) {
const value = buildDocBlockValue(attrs)
const fence = pickFence(value)
return `${fence}${DOC_BLOCK_FENCE_LANG}\n${value}\n${fence}`
}
export function buildDocContextFence(attrs = {}) {
const docType = normalizeDocType(attrs.docType)
const content = sanitizeDocContent(attrs.content || '')
const fence = pickFence(content)
return `${fence}${docType}\n${content}\n${fence}`
}
export function buildLegacyDocBlock(attrs = {}) {
const docType = normalizeDocType(attrs.docType)
const docName = String(attrs.docName || `document.${docType}`)
const uploadTime = String(attrs.uploadTime || new Date().toISOString())
const content = sanitizeDocContent(attrs.content || '')
return `<doc_type="${docType}" doc_name="${docName}" upload_time="${uploadTime}" collapsed="${Boolean(attrs.collapsed)}">\n${content}\n</doc_end>`
}
export function parseLegacyDocBlock(raw = '') {
const match = String(raw || '').match(/^<doc_type="([^"]+)"\s+doc_name="([^"]+)"\s+upload_time="([^"]+)"(?:\s+collapsed="([^"]+)")?>\n?([\s\S]*?)\n?<\/doc_end>$/)
if (!match) return null
return {
docType: normalizeDocType(match[1]),
docName: match[2] || 'document.txt',
uploadTime: match[3] || '',
collapsed: match[4] === 'true',
content: sanitizeDocContent(match[5] || ''),
}
}
export function transformDocBlockMarkdownForClipboard(markdown = '') {
const pattern = /(^|\n)(`{3,})llm-file[^\n]*\n([\s\S]*?)\n\2(?=\n|$)/g
const replacedFence = String(markdown || '').replace(pattern, (full, prefix, _fence, value) => {
const attrs = parseDocBlockValue(value)
return `${prefix}${buildDocContextFence(attrs)}`
})
return replacedFence.replace(/<doc_type="[^"]+"\s+doc_name="[^"]+"\s+upload_time="[^"]+"(?:\s+collapsed="[^"]+")?>[\s\S]*?<\/doc_end>/g, (full) => {
const attrs = parseLegacyDocBlock(full)
return attrs ? buildDocContextFence(attrs) : full
})
}
export function stripDocBlockMarkdown(markdown = '') {
const pattern = /(^|\n)(`{3,})llm-file[^\n]*\n[\s\S]*?\n\2(?=\n|$)/g
return String(markdown || '').replace(pattern, '$1').replace(/\n{3,}/g, '\n\n').trim()
}
export function transformLegacyDocBlocksForExport(markdown = '') {
return String(markdown || '').replace(/<doc_type="[^"]+"\s+doc_name="[^"]+"\s+upload_time="[^"]+"(?:\s+collapsed="[^"]+")?>[\s\S]*?<\/doc_end>/g, (full) => {
const attrs = parseLegacyDocBlock(full)
return attrs ? buildDocBlockMarkdown(attrs) : full
})
}
export function transformSpecialDocBlocksToLegacy(markdown = '') {
const pattern = /(^|\n)(`{3,})llm-file[^\n]*\n([\s\S]*?)\n\2(?=\n|$)/g
return String(markdown || '').replace(pattern, (full, prefix, _fence, value) => {
const attrs = parseDocBlockValue(value)
return `${prefix}${buildLegacyDocBlock(attrs)}`
})
}
+244 -15
View File
@@ -33,20 +33,63 @@ export const translations = {
exportMd: 'Export Markdown', exportMd: 'Export Markdown',
exportDocx: 'Export DOCX', exportDocx: 'Export DOCX',
exportPdf: 'Export PDF', exportPdf: 'Export PDF',
upload: 'Upload',
uploadImg: 'Upload Image', uploadImg: 'Upload Image',
uploadFile: 'Upload File', uploadFile: 'Upload File',
uploadDoc: 'Upload Document',
uploadDocTypeWarning: 'Only txt, json, toml, yaml, docx, pptx, pdf formats are supported.',
uploadDocSizeWarning: 'File size cannot exceed 10MB.',
uploadDocInBlockWarning: 'Cannot insert document inside an existing document block. Please move cursor outside.',
uploadDocError: 'Document conversion failed:',
uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.', uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.',
uploadMdTypeWarning: 'Only Markdown (.md) files and image files are supported.', uploadMdTypeWarning: 'Only Markdown (.md) files and image files are supported.',
uploadFileError: 'File upload failed.', uploadFileError: 'File upload failed.',
uploadConvertError: 'File conversion failed.', uploadConvertError: 'File conversion failed.',
enableAI: 'Enable AI', uploadBatchLimit: 'Maximum 10 files at once',
uploadSizeLimit: 'File exceeds 50MB limit',
uploading: 'Uploading files...',
enableAI: 'Enable AI',
disableAI: 'Disable AI', disableAI: 'Disable AI',
insertUrl: 'Insert Image from URL', insertUrl: 'Insert Image from URL',
insert: 'Insert', insert: 'Insert',
cancel: 'Cancel', cancel: 'Cancel',
imgTooLarge: 'Image too large', imgTooLarge: 'Image too large',
docTooLarge: 'Document too large, AI disabled', docTooLarge: 'Document too large, AI disabled',
initialMarkdown: '# Welcome to LLM-IN-TEXT\n\nAn instant LLM system\n\nStart your creative work below...' initialMarkdown: '# Welcome to LLM-IN-TEXT\n\nAn instant LLM system\n\nStart your creative work below...',
view: 'View',
editor: 'Editor',
docs: 'Docs',
docsManagement: 'Document Management',
docsEmptyDesc: 'Document management interface is under development...',
files: 'Files',
noFiles: '暂无文件',
newFile: '新建文件',
newFolder: '新建文件夹',
untitledFile: 'untitled.md',
untitledFolder: '新建文件夹',
rename: '重命名',
delete: '删除',
copy: '复制',
cut: '剪切',
paste: '粘贴',
confirmDelete: '确认删除',
confirmDeleteDesc: '确定要删除',
confirmDeleteFolderDesc: '此操作将删除文件夹内的所有内容。',
confirmDeleteFileDesc: '此操作不可撤销。',
cancel: '取消',
rootDir: '根目录',
expandSidebar: '展开侧边栏',
collapseSidebar: '收起侧边栏',
fileLimitReached: '文件数量已达上限',
folderLimitReached: '文件夹数量已达上限',
fileSizeLimit: '文件大小不能超过 50MB',
storageError: '存储空间不足',
selectFileToView: '选择一个文件以查看内容',
folderContains: '包含',
items: '个项目',
unsupportedPreview: '暂不支持预览此文件类型',
fileNamePlaceholder: '文件名.md',
folderNamePlaceholder: '文件夹名'
}, },
zh: { zh: {
settings: '设置', settings: '设置',
@@ -82,20 +125,63 @@ export const translations = {
exportMd: '导出 Markdown', exportMd: '导出 Markdown',
exportDocx: '导出 DOCX', exportDocx: '导出 DOCX',
exportPdf: '导出 PDF', exportPdf: '导出 PDF',
upload: '上传',
uploadImg: '上传图片', uploadImg: '上传图片',
uploadFile: '上传文件', uploadFile: '上传文件',
uploadDoc: '上传文档',
uploadDocTypeWarning: '仅支持 txt、json、toml、yaml、docx、pptx、pdf 格式的文档',
uploadDocSizeWarning: '文件大小不能超过 10MB',
uploadDocInBlockWarning: '无法在现有文档块内插入新文档,请将光标移到文档外部',
uploadDocError: '文档转换失败:',
uploadFileTypeWarning: '不支持的文件类型。仅支持 doc/docx/ppt/pptx/pdf/zip、图片、txt/json。', uploadFileTypeWarning: '不支持的文件类型。仅支持 doc/docx/ppt/pptx/pdf/zip、图片、txt/json。',
uploadMdTypeWarning: '仅支持 Markdown.md)和图片文件。', uploadMdTypeWarning: '仅支持 Markdown.md)和图片文件。',
uploadFileError: '文件上传失败', uploadFileError: '文件上传失败',
uploadConvertError: '文件转换失败', uploadConvertError: '文件转换失败',
enableAI: '启用 AI', uploadBatchLimit: '一次最多上传10个文件',
uploadSizeLimit: '文件超过50MB限制',
uploading: '正在上传文件...',
enableAI: '启用 AI',
disableAI: '禁用 AI', disableAI: '禁用 AI',
insertUrl: '通过 URL 插入图片', insertUrl: '通过 URL 插入图片',
insert: '插入', insert: '插入',
cancel: '取消', cancel: '取消',
imgTooLarge: '图片过大', imgTooLarge: '图片过大',
docTooLarge: '文档过大,AI已禁用', docTooLarge: '文档过大,AI已禁用',
initialMarkdown: '# 欢迎使用 LLM-IN-TEXT\n\n即时可用的 LLM 系统\n\n在下方开始创作吧...' initialMarkdown: '# 欢迎使用 LLM-IN-TEXT\n\n即时可用的 LLM 系统\n\n在下方开始创作吧...',
view: '视图',
editor: '编辑器',
docs: '文档',
docsManagement: '文档管理',
docsEmptyDesc: '文档管理界面开发中...',
files: '文件',
noFiles: '暂无文件',
newFile: '新建文件',
newFolder: '新建文件夹',
untitledFile: '未命名.md',
untitledFolder: '新建文件夹',
rename: '重命名',
delete: '删除',
copy: '复制',
cut: '剪切',
paste: '粘贴',
confirmDelete: '确认删除',
confirmDeleteDesc: '确定要删除',
confirmDeleteFolderDesc: '此操作将删除文件夹内的所有内容。',
confirmDeleteFileDesc: '此操作不可撤销。',
cancel: '取消',
rootDir: '根目录',
expandSidebar: '展开侧边栏',
collapseSidebar: '收起侧边栏',
fileLimitReached: '文件数量已达上限',
folderLimitReached: '文件夹数量已达上限',
fileSizeLimit: '文件大小不能超过 50MB',
storageError: '存储空间不足',
selectFileToView: '选择一个文件以查看内容',
folderContains: '包含',
items: '个项目',
unsupportedPreview: '暂不支持预览此文件类型',
fileNamePlaceholder: '文件名.md',
folderNamePlaceholder: '文件夹名'
}, },
ja: { ja: {
settings: '設定', settings: '設定',
@@ -131,20 +217,58 @@ export const translations = {
exportMd: 'Markdownをエクスポート', exportMd: 'Markdownをエクスポート',
exportDocx: 'DOCXをエクスポート', exportDocx: 'DOCXをエクスポート',
exportPdf: 'PDFをエクスポート', exportPdf: 'PDFをエクスポート',
upload: 'アップロード',
uploadImg: '画像をアップロード', uploadImg: '画像をアップロード',
uploadFile: 'Upload File', uploadFile: 'Upload File',
uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.', uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.',
uploadMdTypeWarning: 'Only Markdown (.md) files and image files are supported.', uploadMdTypeWarning: 'Only Markdown (.md) files and image files are supported.',
uploadFileError: 'File upload failed.', uploadFileError: 'File upload failed.',
uploadConvertError: 'File conversion failed.', uploadConvertError: 'File conversion failed.',
enableAI: 'AIを有効化', uploadBatchLimit: 'Maximum 10 files at once',
uploadSizeLimit: 'File exceeds 50MB limit',
uploading: 'Uploading files...',
enableAI: 'AIを有効化',
disableAI: 'AIを無効化', disableAI: 'AIを無効化',
insertUrl: 'URLから画像を挿入', insertUrl: 'URLから画像を挿入',
insert: '挿入', insert: '挿入',
cancel: 'キャンセル', cancel: 'キャンセル',
imgTooLarge: '画像が大きすぎます', imgTooLarge: '画像が大きすぎます',
docTooLarge: 'ドキュメントが大きすぎます、AI無効', docTooLarge: 'ドキュメントが大きすぎます、AI無効',
initialMarkdown: '# LLM-IN-TEXTへようこそ\n\nすぐに使えるLLMシステム\n\n下から創作を始めましょう...' initialMarkdown: '# LLM-IN-TEXTへようこそ\n\nすぐに使えるLLMシステム\n\n下から創作を始めましょう...',
view: 'ビュー',
editor: 'エディター',
docs: 'ドキュメント',
docsManagement: 'ドキュメント管理',
docsEmptyDesc: 'ドキュメント管理画面は開発中です...',
files: 'ファイル',
noFiles: 'ファイルはありません',
newFile: '新規ファイル',
newFolder: '新規フォルダー',
untitledFile: '無題.md',
untitledFolder: '新しいフォルダー',
rename: '名前を変更',
delete: '削除',
copy: 'コピー',
cut: '切り取り',
paste: '貼り付け',
confirmDelete: '削除の確認',
confirmDeleteDesc: '本当に削除しますか',
confirmDeleteFolderDesc: 'フォルダー内のすべてのコンテンツが削除されます。',
confirmDeleteFileDesc: 'この操作は元に戻せません。',
cancel: 'キャンセル',
rootDir: 'ルートディレクトリ',
expandSidebar: 'サイドバーを展開',
collapseSidebar: 'サイドバーを折りたたむ',
fileLimitReached: 'ファイル数の上限に達しました',
folderLimitReached: 'フォルダー数の上限に達しました',
fileSizeLimit: 'ファイルサイズは50MBを超えられません',
storageError: 'ストレージが不足しています',
selectFileToView: 'ファイルを選択して内容を表示',
folderContains: '含む',
items: '項目',
unsupportedPreview: 'このファイルタイプはプレビューに対応していません',
fileNamePlaceholder: 'ファイル名.md',
folderNamePlaceholder: 'フォルダー名'
}, },
ko: { ko: {
settings: '설정', settings: '설정',
@@ -180,6 +304,7 @@ export const translations = {
exportMd: 'Markdown 내보내기', exportMd: 'Markdown 내보내기',
exportDocx: 'DOCX 내보내기', exportDocx: 'DOCX 내보내기',
exportPdf: 'PDF 내보내기', exportPdf: 'PDF 내보내기',
upload: '업로드',
uploadImg: '이미지 업로드', uploadImg: '이미지 업로드',
uploadFile: 'Upload File', uploadFile: 'Upload File',
uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.', uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.',
@@ -193,7 +318,41 @@ export const translations = {
cancel: '취소', cancel: '취소',
imgTooLarge: '이미지가 너무 큽니다', imgTooLarge: '이미지가 너무 큽니다',
docTooLarge: '문서가 너무 큽니다, AI 비활성화됨', docTooLarge: '문서가 너무 큽니다, AI 비활성화됨',
initialMarkdown: '# LLM-IN-TEXT에 오신 것을 환영합니다\n\n즉시 사용할 수 있는 LLM 시스템\n\n아래에서 창작을 시작하세요...' initialMarkdown: '# LLM-IN-TEXT에 오신 것을 환영합니다\n\n즉시 사용할 수 있는 LLM 시스템\n\n아래에서 창작을 시작하세요...',
view: '보기',
editor: '에디터',
docs: '문서',
docsManagement: '문서 관리',
docsEmptyDesc: '문서 관리 화면은 개발 중입니다...',
files: '파일',
noFiles: '파일이 없습니다',
newFile: '새 파일',
newFolder: '새 폴더',
untitledFile: '제목 없음.md',
untitledFolder: '새 폴더',
rename: '이름 변경',
delete: '삭제',
copy: '복사',
cut: '잘라내기',
paste: '붙여넣기',
confirmDelete: '삭제 확인',
confirmDeleteDesc: '정말 삭제하시겠습니까',
confirmDeleteFolderDesc: '폴더의 모든 콘텐츠가 삭제됩니다.',
confirmDeleteFileDesc: '이 작업은 취소할 수 없습니다.',
cancel: '취소',
rootDir: '루트 디렉토리',
expandSidebar: '사이드바 펼치기',
collapseSidebar: '사이드바 접기',
fileLimitReached: '파일 수上限에 도달했습니다',
folderLimitReached: '폴더 수上限에 도달했습니다',
fileSizeLimit: '파일 크기는 50MB를 초과할 수 없습니다',
storageError: '저장 공간이 부족합니다',
selectFileToView: '파일을 선택하여 내용 보기',
folderContains: '포함',
items: '항목',
unsupportedPreview: '이 파일 유형은 미리보기를 지원하지 않습니다',
fileNamePlaceholder: '파일명.md',
folderNamePlaceholder: '폴더명'
}, },
de: { de: {
settings: 'Einstellungen', settings: 'Einstellungen',
@@ -229,6 +388,7 @@ export const translations = {
exportMd: 'Markdown exportieren', exportMd: 'Markdown exportieren',
exportDocx: 'DOCX exportieren', exportDocx: 'DOCX exportieren',
exportPdf: 'PDF exportieren', exportPdf: 'PDF exportieren',
upload: 'Hochladen',
uploadImg: 'Bild hochladen', uploadImg: 'Bild hochladen',
uploadFile: 'Upload File', uploadFile: 'Upload File',
uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.', uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.',
@@ -242,7 +402,41 @@ export const translations = {
cancel: 'Abbrechen', cancel: 'Abbrechen',
imgTooLarge: 'Bild zu groß', imgTooLarge: 'Bild zu groß',
docTooLarge: 'Dokument zu groß, KI deaktiviert', docTooLarge: 'Dokument zu groß, KI deaktiviert',
initialMarkdown: '# Willkommen bei LLM-IN-TEXT\n\nEin sofort verfügbares LLM-System\n\nStarten Sie Ihre kreative Arbeit unten...' initialMarkdown: '# Willkommen bei LLM-IN-TEXT\n\nEin sofort verfügbares LLM-System\n\nStarten Sie Ihre kreative Arbeit unten...',
view: 'Ansicht',
editor: 'Editor',
docs: 'Dokumente',
docsManagement: 'Dokumentenverwaltung',
docsEmptyDesc: 'Die Dokumentenverwaltung ist in Entwicklung...',
files: 'Dateien',
noFiles: 'Keine Dateien',
newFile: 'Neue Datei',
newFolder: 'Neuer Ordner',
untitledFile: 'Unbenannt.md',
untitledFolder: 'Neuer Ordner',
rename: 'Umbenennen',
delete: 'Löschen',
copy: 'Kopieren',
cut: 'Ausschneiden',
paste: 'Einfügen',
confirmDelete: 'Löschen bestätigen',
confirmDeleteDesc: 'Möchten Sie wirklich löschen',
confirmDeleteFolderDesc: 'Dies entfernt alle Inhalte im Ordner.',
confirmDeleteFileDesc: 'Diese Aktion kann nicht rückgängig gemacht werden.',
cancel: 'Abbrechen',
rootDir: 'Stammverzeichnis',
expandSidebar: 'Seitenleiste erweitern',
collapseSidebar: 'Seitenleiste einklappen',
fileLimitReached: 'Maximale Dateianzahl erreicht',
folderLimitReached: 'Maximale Ordneranzahl erreicht',
fileSizeLimit: 'Dateigröße darf 50MB nicht überschreiten',
storageError: 'Speicherplatz unzureichend',
selectFileToView: 'Datei auswählen zum Anzeigen',
folderContains: 'Enthält',
items: 'Elemente',
unsupportedPreview: 'Dieser Dateityp wird nicht in der Vorschau unterstützt',
fileNamePlaceholder: 'Dateiname.md',
folderNamePlaceholder: 'Ordnername'
}, },
fr: { fr: {
settings: 'Paramètres', settings: 'Paramètres',
@@ -278,6 +472,7 @@ export const translations = {
exportMd: 'Exporter Markdown', exportMd: 'Exporter Markdown',
exportDocx: 'Exporter DOCX', exportDocx: 'Exporter DOCX',
exportPdf: 'Exporter PDF', exportPdf: 'Exporter PDF',
upload: 'Télécharger',
uploadImg: 'Télécharger image', uploadImg: 'Télécharger image',
uploadFile: 'Upload File', uploadFile: 'Upload File',
uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.', uploadFileTypeWarning: 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.',
@@ -291,6 +486,40 @@ export const translations = {
cancel: 'Annuler', cancel: 'Annuler',
imgTooLarge: 'Image trop grande', imgTooLarge: 'Image trop grande',
docTooLarge: 'Document trop grand, IA désactivée', docTooLarge: 'Document trop grand, IA désactivée',
initialMarkdown: '# Bienvenue sur LLM-IN-TEXT\n\nUn système LLM instantané\n\nCommencez votre création ci-dessous...' initialMarkdown: '# Bienvenue sur LLM-IN-TEXT\n\nUn système LLM instantané\n\nCommencez votre création ci-dessous...',
view: 'Vue',
editor: 'Éditeur',
docs: 'Documents',
docsManagement: 'Gestion des documents',
docsEmptyDesc: 'L\'interface de gestion des documents est en développement...',
files: 'Fichiers',
noFiles: 'Aucun fichier',
newFile: 'Nouveau fichier',
newFolder: 'Nouveau dossier',
untitledFile: 'Sans titre.md',
untitledFolder: 'Nouveau dossier',
rename: 'Renommer',
delete: 'Supprimer',
copy: 'Copier',
cut: 'Couper',
paste: 'Coller',
confirmDelete: 'Confirmer la suppression',
confirmDeleteDesc: 'Êtes-vous sûr de vouloir supprimer',
confirmDeleteFolderDesc: 'Cela supprimera tout le contenu du dossier.',
confirmDeleteFileDesc: 'Cette action est irréversible.',
cancel: 'Annuler',
rootDir: 'Répertoire racine',
expandSidebar: 'Développer la barre latérale',
collapseSidebar: 'Réduire la barre latérale',
fileLimitReached: 'Nombre maximum de fichiers atteint',
folderLimitReached: 'Nombre maximum de dossiers atteint',
fileSizeLimit: 'La taille du fichier ne peut pas dépasser 50 Mo',
storageError: 'Espace de stockage insuffisant',
selectFileToView: 'Sélectionnez un fichier pour afficher le contenu',
folderContains: 'Contient',
items: 'éléments',
unsupportedPreview: 'Ce type de fichier n\'est pas pris en charge pour l\'aperçu',
fileNamePlaceholder: 'Nom du fichier.md',
folderNamePlaceholder: 'Nom du dossier'
} }
} }
+554
View File
@@ -0,0 +1,554 @@
<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useFileSystem } from '../composables/useFileSystem'
import { useOfficeStore } from '../stores/office'
import { isOfficeFile, getOfficeFormat } from '../services/officeDetection'
import FileTree from '../components/FileTree.vue'
import FileContent from '../components/FileContent.vue'
import ContextMenu from '../components/ContextMenu.vue'
const router = useRouter()
const fs = useFileSystem()
const officeStore = useOfficeStore()
const sidebarCollapsed = ref(false)
const confirmDialog = ref(null)
onMounted(() => {
fs.load()
})
const selectedNode = computed(() => fs.getSelectedNode())
const breadcrumb = computed(() => {
if (!fs.selectedId.value) return []
return fs.getBreadcrumbPath(fs.selectedId.value)
})
function handleCreateFile(parentId) {
const name = 'untitled.md'
fs.createFile(parentId, name)
}
function handleCreateFolder(parentId) {
const name = '新建文件夹'
fs.createFolder(parentId, name)
}
function handleRename(id, newName) {
fs.rename(id, newName)
}
function handleDelete(id) {
const node = fs.tree.value.find(n => n.id === id) || findNode(fs.tree.value, id)
if (node) {
confirmDialog.value = {
id,
name: node.name,
type: node.type
}
}
}
function confirmDelete() {
if (confirmDialog.value) {
fs.remove(confirmDialog.value.id)
confirmDialog.value = null
}
}
function cancelDelete() {
confirmDialog.value = null
}
function findNode(nodes, id) {
for (const node of nodes) {
if (node.id === id) return node
if (node.type === 'folder') {
const found = findNode(node.children || [], id)
if (found) return found
}
}
return null
}
function handleContextMenu(x, y, node) {
fs.showContextMenu(x, y, node)
}
function handleDrop(draggedId, targetParentId) {
if (draggedId === targetParentId) return
//
let targetNode = null
if (targetParentId) {
targetNode = findNode(fs.tree.value, targetParentId)
if (!targetNode || targetNode.type !== 'folder') return
}
//
const draggedNode = findNode(fs.tree.value, draggedId)
if (!draggedNode) return
if (targetParentId && isDescendant(draggedNode, targetParentId)) return
// 使
fs.cut(draggedId)
fs.paste(targetParentId)
}
function isDescendant(node, targetId) {
if (node.type !== 'folder') return false
for (const child of (node.children || [])) {
if (child.id === targetId) return true
if (isDescendant(child, targetId)) return true
}
return false
}
function findParent(nodes, id, parent = null) {
for (const node of nodes) {
if (node.id === id) return parent
if (node.type === 'folder') {
const found = findParent(node.children || [], id, node)
if (found !== undefined) return found
}
}
return undefined
}
function handleDragStart(event, id) {
event.dataTransfer.setData('text/plain', id)
}
function handleDragOver(event) {
event.preventDefault()
}
// Univer
function openInUniver() {
router.push('/univer')
}
// Office
const isSelectedOfficeFile = computed(() => {
if (!selectedNode.value || selectedNode.value.type === 'folder') return false
return isOfficeFile({ name: selectedNode.value.name })
})
</script>
<template>
<div class="docs-view">
<div class="docs-layout">
<div v-show="!sidebarCollapsed" class="docs-sidebar">
<FileTree
:nodes="fs.tree.value"
:selected-id="fs.selectedId.value"
:expanded-ids="fs.expandedIds.value"
:clipboard="fs.clipboard.value"
:get-file-icon="fs.getFileIcon"
@select="fs.select"
@toggle="fs.toggleFolder"
@create-file="handleCreateFile"
@create-folder="handleCreateFolder"
@rename="handleRename"
@remove="handleDelete"
@copy="fs.copy"
@cut="fs.cut"
@paste="fs.paste"
@context-menu="handleContextMenu"
@drop="handleDrop"
@drag-start="handleDragStart"
@drag-over="handleDragOver"
/>
</div>
<div class="docs-main">
<div class="docs-toolbar">
<button class="sidebar-toggle" @click="sidebarCollapsed = !sidebarCollapsed" :title="sidebarCollapsed ? '展开侧边栏' : '收起侧边栏'">
<svg v-if="!sidebarCollapsed" viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M4.5 3.5a.5.5 0 00-.707.707L6.586 7l-2.793 2.793a.5.5 0 10.707.707l3-3a.5.5 0 000-.707l-3-3z"/><path d="M9.5 3.5a.5.5 0 01.707.707L7.414 7l2.793 2.793a.5.5 0 01-.707.707l-3-3a.5.5 0 010-.707l3-3z"/></svg>
<svg v-else viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M11.5 3.5a.5.5 0 01.707.707L9.414 7l2.793 2.793a.5.5 0 01-.707.707l-3-3a.5.5 0 010-.707l3-3z"/><path d="M4.5 3.5a.5.5 0 00-.707.707L6.586 7l-2.793 2.793a.5.5 0 10.707.707l3-3a.5.5 0 000-.707l-3-3z"/></svg>
</button>
<button class="editor-toggle" @click="router.push('/')" title="返回编辑器">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor">
<path d="M2 2.5A1.5 1.5 0 013.5 1h9A1.5 1.5 0 0114 2.5v11a1.5 1.5 0 01-1.5 1.5h-9A1.5 1.5 0 012 13.5v-11z"/>
<path fill="var(--app-bg)" d="M4 4h8v1H4zm0 2h8v1H4zm0 2h6v1H4zm0 2h8v1H4zm0 2h5v1H4z"/>
</svg>
<span class="toggle-label">编辑器</span>
</button>
<div class="breadcrumb-bar">
<span
class="breadcrumb-link"
:class="{ 'breadcrumb-current': breadcrumb.length === 0 }"
@click="fs.select(null)"
>根目录</span>
<span v-if="breadcrumb.length > 0" class="breadcrumb-sep">/</span>
<template v-for="(item, index) in breadcrumb" :key="item.id">
<span
v-if="index < breadcrumb.length - 1"
class="breadcrumb-link"
@click="fs.select(item.id)"
>{{ item.name }}</span>
<span v-else class="breadcrumb-current">{{ item.name }}</span>
<span v-if="index < breadcrumb.length - 1" class="breadcrumb-sep">/</span>
</template>
</div>
<div class="toolbar-actions">
<button v-if="isSelectedOfficeFile" class="toolbar-btn office-btn" @click="openInUniver" title="在 Univer 中编辑">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><rect x="7" y="11" width="8" height="6" rx="1" stroke-width="1.5"/><circle cx="9" cy="13" r="0.8" fill="currentColor"/><path d="M7 16l2-2 2 2" stroke-width="1.5"/></svg>
编辑 Office
</button>
<button v-if="fs.canPaste()" class="toolbar-btn" @click="fs.paste(selectedNode && selectedNode.type === 'folder' ? selectedNode.id : null)" title="粘贴">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M4.75 1.5a.25.25 0 00-.25.25v.59c0 .396.316.717.707.717h5.586c.39 0 .707-.32.707-.716v-.591a.25.25 0 00-.25-.25H4.75zm6.543-.75a1.75 1.75 0 011.75 1.75v.59c0 .396-.107.767-.293 1.086l1.293 1.293a.75.75 0 010 1.061l-1.293 1.293c.186.32.293.69.293 1.087v.59a1.75 1.75 0 01-1.75 1.75H4.75a1.75 1.75 0 01-1.75-1.75v-.59c0-.396.107-.767.293-1.087L2 5.53a.75.75 0 010-1.06l1.293-1.294A2.048 2.048 0 013 2.09v-.59A1.75 1.75 0 014.75 0h6.543zM6 8.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm.5 2.5a.5.5 0 000 1h3a.5.5 0 000-1h-3z"/></svg>
粘贴
</button>
</div>
</div>
<FileContent
:node="selectedNode"
:breadcrumb="breadcrumb"
:root-nodes="fs.tree.value"
:get-file-icon="fs.getFileIcon"
@navigate="fs.select"
/>
</div>
</div>
<ContextMenu
:visible="!!fs.contextMenu.value"
:x="fs.contextMenu.value?.x || 0"
:y="fs.contextMenu.value?.y || 0"
:node="fs.contextMenu.value?.node || null"
:can-paste="fs.canPaste()"
@close="fs.hideContextMenu()"
@rename="(id) => { fs.hideContextMenu(); }"
@delete="(id) => { fs.hideContextMenu(); handleDelete(id) }"
@copy="(id) => { fs.hideContextMenu(); fs.copy(id) }"
@cut="(id) => { fs.hideContextMenu(); fs.cut(id) }"
@paste="(parentId) => { fs.hideContextMenu(); fs.paste(parentId) }"
@new-file="(parentId) => { fs.hideContextMenu(); handleCreateFile(parentId) }"
@new-folder="(parentId) => { fs.hideContextMenu(); handleCreateFolder(parentId) }"
/>
<Teleport to="body">
<div v-if="confirmDialog" class="confirm-overlay">
<div class="confirm-dialog">
<div class="confirm-icon">
<svg viewBox="0 0 24 24" width="32" height="32" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" y1="8" x2="12" y2="12"></line>
<line x1="12" y1="16" x2="12.01" y2="16"></line>
</svg>
</div>
<h3>确认删除</h3>
<p>确定要删除 <strong>{{ confirmDialog.name }}</strong> {{ confirmDialog.type === 'folder' ? '此操作将删除文件夹内的所有内容。' : '此操作不可撤销。' }}</p>
<div class="confirm-actions">
<button class="btn-cancel" @click="cancelDelete">取消</button>
<button class="btn-delete" @click="confirmDelete">删除</button>
</div>
</div>
</div>
</Teleport>
<Teleport to="body">
<div v-if="fs.error" class="error-toast">
<div class="error-content">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M8 0a8 8 0 100 16A8 8 0 008 0zm3.78 11.22a.75.75 0 01-1.06 0L8 8.56 5.28 11.28a.75.75 0 01-1.06-1.06L6.94 7.5 4.22 4.78a.75.75 0 011.06-1.06L8 6.44l2.72-2.72a.75.75 0 111.06 1.06L9.06 7.5l2.72 2.72a.75.75 0 010 1.06z"/></svg>
<span>{{ fs.error }}</span>
<button class="error-close" @click="fs.error = null">&times;</button>
</div>
</div>
</Teleport>
</div>
</template>
<style scoped>
.docs-view {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.docs-layout {
display: flex;
flex: 1;
overflow: hidden;
}
.docs-sidebar {
width: 280px;
min-width: 200px;
max-width: 400px;
height: 100%;
border-right: 1px solid var(--panel-border);
flex-shrink: 0;
overflow: hidden;
}
.docs-main {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
min-width: 0;
}
.docs-toolbar {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
border-bottom: 1px solid var(--panel-border);
min-height: 44px;
flex-shrink: 0;
}
.sidebar-toggle {
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
padding: 0;
border: 1px solid var(--panel-border);
background: var(--app-bg);
color: var(--muted-text);
cursor: pointer;
border-radius: 6px;
flex-shrink: 0;
}
.sidebar-toggle:hover {
color: var(--app-text);
border-color: var(--focus-ring);
}
.editor-toggle {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 12px;
border: 1px solid var(--panel-border);
background: var(--app-bg);
color: var(--muted-text);
cursor: pointer;
border-radius: 6px;
font-size: 13px;
flex-shrink: 0;
transition: all 0.2s ease;
}
.editor-toggle:hover {
color: var(--focus-ring);
border-color: var(--focus-ring);
background: var(--ghost-code-bg);
}
.toggle-label {
font-weight: 500;
}
.breadcrumb-bar {
flex: 1;
display: flex;
align-items: center;
gap: 4px;
font-size: 13px;
overflow: hidden;
white-space: nowrap;
}
.breadcrumb-link {
color: var(--focus-ring);
cursor: pointer;
}
.breadcrumb-link:hover {
text-decoration: underline;
}
.breadcrumb-current {
color: var(--app-text);
font-weight: 500;
}
.breadcrumb-sep {
color: var(--muted-text);
}
.breadcrumb-root {
color: var(--muted-text);
}
.toolbar-actions {
display: flex;
gap: 4px;
flex-shrink: 0;
}
.toolbar-btn {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 12px;
border: 1px solid var(--panel-border);
background: var(--app-bg);
color: var(--app-text);
cursor: pointer;
border-radius: 6px;
font-size: 13px;
}
.toolbar-btn:hover {
border-color: var(--focus-ring);
color: var(--focus-ring);
}
.toolbar-btn.office-btn {
border-color: var(--focus-ring);
color: var(--focus-ring);
background: rgba(59, 130, 246, 0.08);
}
.toolbar-btn.office-btn:hover {
background: rgba(59, 130, 246, 0.15);
}
.confirm-overlay {
position: fixed;
inset: 0;
background: var(--overlay-bg);
display: flex;
align-items: center;
justify-content: center;
z-index: 100001;
animation: fadeIn 0.15s ease;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.confirm-dialog {
background: var(--panel-bg);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid var(--panel-border);
border-radius: 12px;
box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
padding: 24px;
max-width: 400px;
width: 90%;
text-align: center;
animation: slideUp 0.2s ease;
}
@keyframes slideUp {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: translateY(0); }
}
.confirm-icon {
color: var(--danger-text);
margin-bottom: 12px;
}
.confirm-dialog h3 {
margin: 0 0 8px;
font-size: 1.1rem;
}
.confirm-dialog p {
margin: 0 0 20px;
color: var(--muted-text);
font-size: 0.9rem;
line-height: 1.5;
}
.confirm-dialog p strong {
color: var(--app-text);
}
.confirm-actions {
display: flex;
gap: 8px;
justify-content: center;
}
.btn-cancel,
.btn-delete {
padding: 8px 20px;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
border: 1px solid var(--panel-border);
font-weight: 500;
}
.btn-cancel {
background: var(--app-bg);
color: var(--app-text);
}
.btn-cancel:hover {
background: var(--ghost-code-bg);
}
.btn-delete {
background: var(--danger-text);
color: #fff;
border-color: var(--danger-text);
}
.btn-delete:hover {
opacity: 0.9;
}
.error-toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%);
z-index: 100002;
animation: slideUp 0.2s ease;
}
.error-content {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 16px;
background: var(--danger-text);
color: #fff;
border-radius: 8px;
font-size: 13px;
box-shadow: 0 4px 12px rgba(220, 38, 38, 0.3);
}
.error-close {
background: none;
border: none;
color: #fff;
font-size: 18px;
cursor: pointer;
padding: 0 4px;
line-height: 1;
}
.error-close:hover {
opacity: 0.8;
}
@media (max-width: 768px) {
.docs-sidebar {
position: fixed;
left: 0;
top: 0;
bottom: 0;
z-index: 9000;
box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
}
}
</style>
+66
View File
@@ -0,0 +1,66 @@
<script setup>
import { ref } from 'vue'
import { defineAsyncComponent } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const MilkdownEditor = defineAsyncComponent(() => import('../components/MilkdownEditor.vue'))
const markdown = ref('')
</script>
<template>
<div class="editor-view">
<div class="editor-toolbar">
<button class="docs-toggle" @click="router.push('/docs')" title="切换到文档模式">
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor">
<path d="M0 2.5A1.5 1.5 0 011.5 1h2.793a.5.5 0 01.353.146l1.5 1.5a.5.5 0 00.354.146H13.5A1.5 1.5 0 0115 4.5v7.5a1.5 1.5 0 01-1.5 1.5h-11A1.5 1.5 0 011 12v-9.5z"/>
</svg>
<span class="toggle-label">文档</span>
</button>
</div>
<MilkdownEditor v-model:markdown="markdown" />
</div>
</template>
<style scoped>
.editor-view {
position: relative;
z-index: 1;
height: 100%;
display: flex;
flex-direction: column;
}
.editor-toolbar {
position: absolute;
top: 8px;
right: 16px;
z-index: 10;
}
.docs-toggle {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
border: 1px solid var(--panel-border);
background: var(--app-bg);
color: var(--muted-text);
cursor: pointer;
border-radius: 6px;
font-size: 13px;
transition: all 0.2s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.docs-toggle:hover {
color: var(--focus-ring);
border-color: var(--focus-ring);
background: var(--ghost-code-bg);
}
.toggle-label {
font-weight: 500;
}
</style>
+46
View File
@@ -0,0 +1,46 @@
<template>
<div class="univer-view">
<UniverEditor
ref="editorRef"
@back="handleBack"
@document-loaded="handleDocumentLoaded"
@document-changed="handleDocumentChanged"
/>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useOfficeStore } from '../stores/office'
import UniverEditor from '../components/UniverEditor.vue'
const router = useRouter()
const officeStore = useOfficeStore()
const editorRef = ref(null)
function handleBack() {
router.push('/')
}
function handleDocumentLoaded(doc) {
console.log('文档已加载:', doc)
}
function handleDocumentChanged(event) {
console.log('文档已更改:', event)
}
onMounted(() => {
// univer
officeStore.switchView('univer')
})
</script>
<style scoped>
.univer-view {
width: 100%;
height: 100vh;
overflow: hidden;
}
</style>
+20
View File
@@ -0,0 +1,20 @@
import asyncio
from backend.tts_asr import _tts_sync_with_retry, _load_asr_pipeline_with_retry, _asr_pipeline
import base64
async def main():
text = "早上好"
print(f"Testing TTS with text: {text}")
audio_bytes, sr = await _tts_sync_with_retry(text, rate=1.0)
print(f"TTS generated {len(audio_bytes)} bytes of audio.")
print("Testing ASR...")
await _load_asr_pipeline_with_retry()
asr = _asr_pipeline
# Needs to process audio_bytes. ASR expects float32 numpy array or bytes?
# the pipeline takes bytes or dict with raw array
result = asr(audio_bytes)
print("ASR output:", result)
asyncio.run(main())
+1 -1
View File
@@ -8,7 +8,7 @@ export default defineConfig({
port: 5173, port: 5173,
proxy: { proxy: {
'/v1': { '/v1': {
target: 'http://localhost:8001', target: 'https://api.imageteach.tech:8002',
changeOrigin: true changeOrigin: true
} }
} }