draftgo-cli 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,239 +0,0 @@
1
- # DraftGo 项目上下文
2
-
3
- > 由 draftgo-init 自动生成。重新初始化:`/draftgo init`
4
-
5
- ## 架构认知(必读)
6
-
7
- **DraftGo 不是传统 SPA,是"数据库驱动的页面资产运行时":**
8
- - 壳层(Vue 3 + Vite)只负责 runtime 编排,源码在 `frontend/src/core/`
9
- - 业务页面 HTML 存在数据库 `page.value.html`,运行在 `iframe.srcdoc`
10
- - 导航栏 HTML 存在数据库 `navigation.html`,由壳层按需加载
11
- - 页面与壳层通过 `window.parent.App` API 通信
12
-
13
- **技术栈:**
14
- - 后端:FastAPI 0.115.0 + SQLAlchemy 2.0.36 + Pydantic 2.10.0 + MySQL + Redis
15
- - 前端:Vue 3 + Vite + Tailwind CSS(本地 `/assets/tailwindcss.js`)
16
-
17
- ## 连接信息
18
- - 服务器:https://dev.draftgo.cn
19
- - Token:存储于 `.draftgo/config.json`(已加入 .gitignore)
20
-
21
- ## 开发规范
22
- → 前端页面开发:读取 `.draftgo/rules/frontend.md`
23
- → 页面排错指南:读取 `.draftgo/rules/debugging-syntax.md`(页面功能不执行时必读!)
24
- - 任何开发,必读!
25
-
26
- ## 开发禁区(违反必报错)
27
-
28
- - ❌ 禁止境外 CDN(googleapis/jsdelivr/cdnjs/unpkg)→ ✅ 允许国内镜像(npmmirror.com/staticfile.net)
29
- - ❌ 禁止 `App()` 写法(App 是对象不是函数)→ ✅ 正确:`const App = window.parent?.App`
30
- - ❌ 禁止 `const App = () => window.parent?.App` → ✅ 去掉箭头函数
31
- - ❌ 禁止 `window.location.search` 读参数 → ✅ 用 `window.__DG_ROUTE_CONTEXT__.query`
32
- - ❌ 禁止 `App?.user?.role` 判断管理员 → `App.user` 不存在!✅ 用 `App.isAdmin` 或 `App.currentUser?.role_code`
33
- - ❌ 禁止 `navigate('/login')` 退出 → ✅ 用 `window.location.href = '/login'`
34
- - ❌ 禁止 `window.alert/confirm/prompt` → ✅ 用 `App.toast()` / `App.confirm()` / `App.showModal()`
35
- - `App.showSuccess(msg)` 成功提示(绿色 3s)— 推荐用于操作成功反馈
36
- - `App.showError(msg)` 错误提示(红色 4s)— 推荐用于失败反馈
37
- - `App.showWarning(msg)` 警告提示(黄色 3s)— 推荐用于提醒
38
- - `App.showInfo(msg)` 信息提示(蓝色 3s)— 推荐用于中性信息
39
- - `App.toast(msg, type)` 通用方法 — type: success/error/warning/info
40
- - `App.confirm(msg, title?)` 确认弹窗 — 玻璃蒙层 + 弹跳动画 + ⚠ 图标,双按钮,返回 Promise\<boolean\>
41
- - `App.showModal(msg, title?)` 信息模态框 — 玻璃蒙层 + 弹跳动画 + i 图标,单按钮,替代 alert()
42
- - `App.showLoading()` / `App.hideLoading()` — 全局 loading 玻璃蒙层,手动开关
43
- - 详见 `.draftgo/rules/frontend.md` App API 章节
44
- - ⚠️ 尽量避免硬编码颜色 → 优先用 `var(--dg-accent)` 等 token 以适配主题切换
45
-
46
- ## 主题切换适配
47
-
48
- DraftGo 支持多套配色方案 + 亮暗模式,通过 CSS 变量实现。
49
-
50
- **概念分层:**
51
- | 概念 | 存储键 | 取值 |
52
- |---|---|---|
53
- | 显示模式 | `dg_theme` | `'light'` \| `'dark'` \| `'system'` |
54
- | 配色方案 | `dg_color_scheme` | `'dark-gray-white'` \| `'deep-blue-white'` \| `'orange-white'` \| `'custom'` |
55
-
56
- **内置配色方案:**
57
- - `dark-gray-white` 深灰白(默认)— 冷调深灰,干净利落
58
- - `deep-blue-white` 深蓝白 — 专业稳重,企业风
59
- - `orange-white` 橙白 — 温暖醒目,创意风
60
-
61
- **颜色 Token(优先使用):**
62
- - `--dg-bg-base` / `--dg-bg-page` / `--dg-bg-surface` — 背景层级
63
- - `--dg-text-primary` / `--dg-text-secondary` / `--dg-text-muted` — 文字层级
64
- - `--dg-accent` / `--dg-accent-hover` / `--dg-accent-subtle` — 主题色
65
- - `--dg-border` / `--dg-success` / `--dg-error` / `--dg-warning` — 功能色
66
-
67
- **App API 切换配色:**
68
- ```javascript
69
- App.setColorScheme('deep-blue-white'); // 切换为预设方案
70
- App.setColorScheme('custom', customVarsObject); // 应用自定义配色
71
- ```
72
-
73
- **硬编码颜色的场景(允许但需注意):**
74
- - 品牌色(如 Logo 固定色)
75
- - 数据可视化图表(需保证对比度)
76
- - 第三方组件库强制要求
77
-
78
- **硬编码时的要求:**
79
- - 同时提供亮色/暗色两套值,通过 `[data-theme="dark"]` 选择器切换
80
- - 确保对比度符合 WCAG AA 标准(文字至少 4.5:1)
81
-
82
- ## 本地开发流程
83
-
84
- **原则:有功能修改就读本地数据,自主决策是否调用 API 完成高质量修改。**
85
-
86
- 1. **开发时**:读写 `.draftgo/` 本地文件
87
- - 查元数据:读 `index.json`
88
- - 查/改 HTML:按 `html_file` 字段路径读对应 `.html` 文件
89
- 2. **修改后**:运行同步脚本推送到服务器(见下方"同步命令")
90
- 3. **同步成功**:本地与服务器数据一致
91
-
92
- **本地数据目录:**
93
-
94
- | 目录 | 内容 |
95
- |---|---|
96
- | pages/ | index.json(无 html)+ 若干 .html |
97
- | navigations/ | index.json(无 html)+ 若干 .html |
98
- | roles/ | index.json |
99
- | users/ | index.json |
100
- | db_meta/ | index.json |
101
- | aihub/ | index.json |
102
- | system_config/ | index.json |
103
- | iteration/ | YYYY-MM-DD.txt(迭代日志) |
104
- | error/ | YYYY-MM-DD.txt(错误日志) |
105
-
106
- ## 数据库 Schema(关键字段)
107
-
108
- | 表 | 关键字段 | 说明 |
109
- |---|---|---|
110
- | `page` | `id`, `title`, `route`, `permission`, `value` (JSON: `{html: "..."}`) | value.html 存页面完整 HTML |
111
- | `navigation` | `id`, `code`, `name`, `html`, `order`, `status` | html 字段存导航栏完整 HTML |
112
- | `user` | `id`, `username`, `email`, `role_id`, `status` | role_id 关联 role 表 |
113
- | `role` | `id`, `code`, `name`, `permissions` (JSON) | admin 角色拥有所有权限 |
114
- | `db_meta` | `id`, `type`, `fields` (JSON: `[{name, type, required}]`) | 动态表结构定义 |
115
- | `db` | `id`, `type`, `data` (JSON), `userid`, `status`, `created_at`, `updated_at` | 动态数据存储(含自动时间戳) |
116
- | `aihub` | `id`, `name`, `type`, `config` (JSON), `status` | AI 服务配置 |
117
- | `sys_config` | `config_key`, `config_value`, `value_type`, `category`, `is_sensitive` | 系统配置 KV |
118
-
119
- ## API 速查与示例
120
-
121
- | 模块 | 端点 |
122
- |---|---|
123
- | 认证 | POST /api/auth/login, /register, /logout, /refresh, /forgot-password, /reset-password |
124
- | 微信认证 | POST /api/auth/wechat/mp/oauth, /wechat/mini/login, /wechat/mp/qr/create · GET /wechat/mp/qr/poll |
125
- | 用户 | GET/PUT /api/users/me · GET/POST /api/users · GET/PUT/DELETE /users/{id} · POST /users/{id}/ban, /unban |
126
- | 角色 | GET/POST /api/roles · GET/PUT/DELETE /roles/{id} · POST /roles/{id}/assign/{uid} · DELETE /roles/{id}/revoke/{uid} |
127
- | 页面 | GET/POST /api/pages/ · GET/PUT/DELETE /pages/{id} · POST /pages/{id}/reset-system · GET /pages/by-route · GET /pages/public/{route} |
128
- | 导航栏 | GET/POST /api/navigations · GET /navigations/{code} · PUT/DELETE /navigations/{id} · POST /navigations/{id}/reset-system |
129
- | 系统配置 | GET /api/system/config · GET /system/category/{category} · GET/PUT/DELETE /system/{key} · POST /system/ · POST /system/config/ensure |
130
- | 备份恢复 | GET/POST /api/system/backup · POST /system/restore · POST /system/reset · POST /system/backup/selective · POST /system/restore/selective · GET /system/backup/logs |
131
- | 通知公告 | GET/POST /api/notices · GET/PUT/DELETE /notices/{id} |
132
- | 反馈 | GET/POST /api/feedback · GET /feedback/updates · GET/PUT/DELETE /feedback/{id} · DELETE /feedback/batch |
133
- | 日志 | GET /api/logs · GET /logs/{id} |
134
- | 智能体 | GET /api/agents · GET /agents/logs · POST /agents/{id}/chat · POST /agents/{id}/preview-chat |
135
- | 动态DB | GET/POST /api/db/{type} · GET/PUT/DELETE /db/{type}/{id} |
136
- | DB Meta | GET/POST /api/db-meta · GET /db-meta/{type} · PUT/DELETE /db-meta/{id} |
137
- | ⚠️ DB Meta 查询 | GET /db-meta/{type} 用 **type**(如 `patient_profile`),不是 id。用 id 查会返回 "元数据不存在"。PUT/DELETE 才用 id。 |
138
- | AIHub | GET/POST /api/aihub · GET/PUT/DELETE /aihub/{id} · PATCH /aihub/{id}/basic · POST /aihub/{id}/sync · POST /aihub/discover · GET /aihub/types |
139
- | AI推理 | GET /api/v1/models · POST /v1/chat/completions |
140
- | 文件上传 | POST /api/upload |
141
- | 通知测试 | POST /api/system/notifications/test-email, /test-sms · GET /system/notifications/logs |
142
-
143
- ### 高频 API 示例
144
-
145
- **创建页面(需 admin):**
146
- ```
147
- POST /api/pages/
148
- Body: { "title": "新页面", "route": "/new", "permission": "public", "value": {"html": "<html>...</html>"} }
149
- Response: { "id": 123, "title": "新页面", "route": "/new", ... }
150
- ```
151
-
152
- **更新页面 HTML(需 admin):**
153
- ```
154
- PUT /api/pages/123
155
- Body: { "value": {"html": "<html>更新后...</html>"} }
156
- ```
157
-
158
- **重置系统页面(需 admin):**
159
- ```
160
- POST /api/pages/123/reset-system
161
- Body: 空
162
- 说明:从 backend/init/pages/ 重新加载该页面
163
- ```
164
-
165
- **创建动态数据(普通用户自动绑定 userid):**
166
- ```
167
- POST /api/db/patient
168
- Body: { "data": {"name": "张三", "age": 30} }
169
- Response: { "id": 456, "type": "patient", "data": {...}, "userid": 当前用户id, "status": 1, "created_at": "2026-01-01T12:00:00", "updated_at": "2026-01-01T12:00:00" }
170
- ```
171
-
172
- **查询动态数据(带分页):**
173
- ```
174
- GET /api/db/patient?page=1&page_size=20
175
- Response: { "items": [{ "id": 456, "type": "patient", "data": {...}, "userid": 1, "status": 1, "created_at": "...", "updated_at": "..." }], "total": 100, "page": 1, "page_size": 20 }
176
- ```
177
-
178
- ## URL 参数读取
179
-
180
- 页面运行在 `iframe.srcdoc` 中,`window.location.search` 不可靠。框架通过 `<script data-dg-route-bridge>` 注入路由上下文。
181
-
182
- ```javascript
183
- // 标准三阶回落(推荐)
184
- const routeContext =
185
- window.__DG_ROUTE_CONTEXT__
186
- || window.__DG_GET_ROUTE_CONTEXT__?.()
187
- || window.parent?.App?.getCurrentRouteContext?.()
188
- || { query: {} };
189
- const query = routeContext.query || {};
190
-
191
- // 使用示例
192
- // 路由 /doctor/prescription-estimate?patientId=42&visitId=7
193
- const patientId = query.patientId; // "42"
194
- const visitId = query.visitId; // "7"
195
- ```
196
-
197
- 禁止:`new URLSearchParams(window.location.search)` — iframe 中取不到壳层 URL。
198
-
199
- ## 常见问题排查
200
-
201
- | 问题 | 排查步骤 |
202
- |---|---|
203
- | 同步失败 | 1. 检查 `.draftgo/config.json` token 是否有效<br>2. 检查服务器连接(ping/curl)<br>3. 查看 `.draftgo/error/YYYY-MM-DD.txt` 错误日志 |
204
- | 页面加载空白 | 1. 检查 `permission` 字段与当前用户角色是否匹配<br>2. 检查路由是否正确(`/api/pages/by-route?route=/xxx`)<br>3. 浏览器控制台查看 JS 错误 |
205
- | API 返回 401 | Token 过期,前端会自动用 refresh token 刷新,无需手动处理 |
206
- | API 返回 403 | 权限不足,检查当前用户角色是否有对应权限 |
207
- | DB Meta GET 返回"元数据不存在" | 你用了 id,应该用 **type**(如 `/api/db-meta/patient_profile`)。PUT/DELETE 才用 id。 |
208
- | 颜色不生效 | 检查是否用了 `var(--dg-*)` 而非硬编码 hex/rgb |
209
- | URL 参数读取失败 | 检查是否用了 `window.__DG_ROUTE_CONTEXT__.query` 而非 `window.location.search` |
210
- | 退出登录后导航栏未更新 | 检查是否用了 `window.location.href = '/login'` 而非 `navigate('/login')` |
211
-
212
- ## 错误日志规范
213
-
214
- **遇到任何开发/运行时错误,必须自动追加到 `.draftgo/error/YYYY-MM-DD.txt`:**
215
-
216
- 格式:`[HH:MM:SS] [错误类型] 描述`
217
-
218
- 示例:
219
- ```
220
- [14:23:45] [同步失败] pages/123 同步到服务器失败:401 Unauthorized
221
- [14:30:12] [API错误] GET /api/db/patient 返回 500:数据库连接超时
222
- [15:10:33] [页面错误] admin-users.html 第 42 行:App is not a function
223
- ```
224
-
225
- 操作流程:
226
- 1. 捕获错误后,获取当前日期和时间
227
- 2. 检查 `.draftgo/error/YYYY-MM-DD.txt` 是否存在
228
- 3. 存在则 Read 后追加新行;不存在则直接 Write
229
- 4. 无需告知用户(静默执行)
230
-
231
- ## 同步命令
232
-
233
- > **禁止用 npx/npm/curl 同步。必须用 Python 脚本:**
234
- > `python .draftgo/skill/scripts/draftgo_sync.py pages <page_id>`
235
- > `python .draftgo/skill/scripts/draftgo_sync.py nav <nav_id>`
236
-
237
- - `/draftgo sync pages` — 触发 sync skill(不是 CLI 命令)
238
- - `/draftgo sync nav` — 触发 sync skill(不是 CLI 命令)
239
- - `/draftgo init` — 触发 init skill(不是 CLI 命令)