dsh-plugin-image-workbench 1.1.5 → 1.1.7

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.
Files changed (173) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +5 -3
  3. package/README.md +7 -3
  4. package/client.js +241 -241
  5. package/cordis.patch.yml +14 -14
  6. package/host.js +51 -0
  7. package/install.mjs +333 -387
  8. package/package.json +3 -2
  9. package/scripts/fetch-style-images.mjs +104 -0
  10. package/scripts/vendor-snapshot.py +154 -154
  11. package/server/access.py +26 -26
  12. package/server/app.py +1371 -1329
  13. package/server/catalog.py +147 -147
  14. package/server/composer.py +391 -391
  15. package/server/drafts.py +158 -158
  16. package/server/history.py +575 -575
  17. package/server/imaging.py +172 -172
  18. package/server/pool.py +1335 -1335
  19. package/server/pool_admin.py +921 -921
  20. package/server/pool_service.py +1073 -1073
  21. package/server/requirements.txt +6 -6
  22. package/server/smoke_check.py +402 -402
  23. package/server/static/access.html +202 -202
  24. package/server/static/index.html +15 -0
  25. package/vendor/chatgpt2api/source/.env.example +82 -82
  26. package/vendor/chatgpt2api/source/.python-version +1 -1
  27. package/vendor/chatgpt2api/source/LICENSE +21 -21
  28. package/vendor/chatgpt2api/source/README.md +391 -391
  29. package/vendor/chatgpt2api/source/VERSION +1 -1
  30. package/vendor/chatgpt2api/source/api/__init__.py +2 -2
  31. package/vendor/chatgpt2api/source/api/accounts.py +537 -537
  32. package/vendor/chatgpt2api/source/api/ai.py +217 -217
  33. package/vendor/chatgpt2api/source/api/app.py +62 -62
  34. package/vendor/chatgpt2api/source/api/errors.py +46 -46
  35. package/vendor/chatgpt2api/source/api/image_inputs.py +309 -309
  36. package/vendor/chatgpt2api/source/api/image_tasks.py +120 -120
  37. package/vendor/chatgpt2api/source/api/support.py +134 -134
  38. package/vendor/chatgpt2api/source/api/system.py +365 -365
  39. package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -50
  40. package/vendor/chatgpt2api/source/config.default.json +108 -108
  41. package/vendor/chatgpt2api/source/main.py +9 -9
  42. package/vendor/chatgpt2api/source/pyproject.toml +27 -27
  43. package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -186
  44. package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -163
  45. package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -13
  46. package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -385
  47. package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -129
  48. package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -92
  49. package/vendor/chatgpt2api/source/services/account_service.py +1757 -1757
  50. package/vendor/chatgpt2api/source/services/auth_service.py +253 -253
  51. package/vendor/chatgpt2api/source/services/backup_service.py +671 -671
  52. package/vendor/chatgpt2api/source/services/config.py +667 -667
  53. package/vendor/chatgpt2api/source/services/content_filter.py +242 -242
  54. package/vendor/chatgpt2api/source/services/cpa_service.py +315 -315
  55. package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -260
  56. package/vendor/chatgpt2api/source/services/image_service.py +376 -376
  57. package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -405
  58. package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -76
  59. package/vendor/chatgpt2api/source/services/image_task_service.py +550 -550
  60. package/vendor/chatgpt2api/source/services/log_service.py +337 -337
  61. package/vendor/chatgpt2api/source/services/model_service.py +182 -182
  62. package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -267
  63. package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -2763
  64. package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -32
  65. package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -2
  66. package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -307
  67. package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -238
  68. package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -1644
  69. package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -124
  70. package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -19
  71. package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -316
  72. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -86
  73. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -42
  74. package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -53
  75. package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -453
  76. package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -160
  77. package/vendor/chatgpt2api/source/services/proxy_service.py +648 -648
  78. package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -5
  79. package/vendor/chatgpt2api/source/services/storage/base.py +38 -38
  80. package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -194
  81. package/vendor/chatgpt2api/source/services/storage/factory.py +104 -104
  82. package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -183
  83. package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -95
  84. package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -531
  85. package/vendor/chatgpt2api/source/utils/helper.py +469 -469
  86. package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -346
  87. package/vendor/chatgpt2api/source/utils/log.py +110 -110
  88. package/vendor/chatgpt2api/source/utils/pkce.py +19 -19
  89. package/vendor/chatgpt2api/source/utils/pow.py +207 -207
  90. package/vendor/chatgpt2api/source/utils/sentinel.py +159 -159
  91. package/vendor/chatgpt2api/source/utils/turnstile.py +184 -184
  92. package/vendor/chatgpt2api/source/uv.lock +904 -904
  93. package/vendor/chatgpt2api/source/web/bun.lock +1501 -1501
  94. package/vendor/chatgpt2api/source/web/components.json +24 -24
  95. package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -17
  96. package/vendor/chatgpt2api/source/web/next.config.ts +40 -40
  97. package/vendor/chatgpt2api/source/web/package.json +58 -58
  98. package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -5
  99. package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -925
  100. package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -1307
  101. package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -224
  102. package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -444
  103. package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -9
  104. package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -9
  105. package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -172
  106. package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -169
  107. package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -39
  108. package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -59
  109. package/vendor/chatgpt2api/source/web/src/app/globals.css +230 -230
  110. package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -542
  111. package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -554
  112. package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -176
  113. package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -1799
  114. package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -732
  115. package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -50
  116. package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -101
  117. package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -356
  118. package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -30
  119. package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -341
  120. package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -447
  121. package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -71
  122. package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -491
  123. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -108
  124. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -169
  125. package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -185
  126. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -379
  127. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -127
  128. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -200
  129. package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -12
  130. package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -886
  131. package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -81
  132. package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -422
  133. package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -135
  134. package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -1069
  135. package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -49
  136. package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -24
  137. package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -434
  138. package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -46
  139. package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -18
  140. package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -14
  141. package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -263
  142. package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -256
  143. package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -41
  144. package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -59
  145. package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -46
  146. package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -61
  147. package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -27
  148. package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -267
  149. package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -132
  150. package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -13
  151. package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -19
  152. package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -24
  153. package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -149
  154. package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -38
  155. package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -175
  156. package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -143
  157. package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -29
  158. package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -91
  159. package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -269
  160. package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -18
  161. package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -127
  162. package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -6
  163. package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -85
  164. package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -926
  165. package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -26
  166. package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -25
  167. package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -93
  168. package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -89
  169. package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -6
  170. package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -112
  171. package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -33
  172. package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -318
  173. package/vendor/chatgpt2api/source/web/tsconfig.json +42 -42
@@ -1,365 +1,365 @@
1
- from __future__ import annotations
2
-
3
- from urllib.parse import quote
4
-
5
- from fastapi import APIRouter, Header, HTTPException, Query, Request
6
- from fastapi.concurrency import run_in_threadpool
7
- from fastapi.responses import HTMLResponse, Response, StreamingResponse
8
- from pydantic import BaseModel, ConfigDict
9
-
10
- from api.support import require_admin, require_identity, resolve_image_base_url
11
- from services.backup_service import BackupError, backup_service
12
- from services.config import config
13
- from services.image_service import (
14
- compress_images,
15
- delete_images,
16
- delete_to_target,
17
- download_images_zip,
18
- get_image_download_response,
19
- get_image_response,
20
- get_thumbnail_response,
21
- list_images,
22
- storage_stats,
23
- )
24
- from services.image_storage_service import ImageStorageError, image_storage_service
25
- from services.image_tags_service import delete_tag, get_all_tags, set_tags
26
- from services.log_service import log_service
27
- from services.proxy_service import proxy_settings, test_clearance, test_proxy
28
-
29
-
30
- class SettingsUpdateRequest(BaseModel):
31
- model_config = ConfigDict(extra="allow")
32
-
33
-
34
- class ProxyTestRequest(BaseModel):
35
- url: str = ""
36
-
37
-
38
- class ClearanceTestRequest(BaseModel):
39
- target_url: str = "https://chatgpt.com"
40
-
41
-
42
- class ImageDeleteRequest(BaseModel):
43
- paths: list[str] = []
44
- start_date: str = ""
45
- end_date: str = ""
46
- all_matching: bool = False
47
-
48
- class ImageDownloadRequest(BaseModel):
49
- paths: list[str]
50
-
51
- class ImageTagsRequest(BaseModel):
52
- path: str
53
- tags: list[str]
54
-
55
- class LogDeleteRequest(BaseModel):
56
- ids: list[str] = []
57
- class BackupDeleteRequest(BaseModel):
58
- key: str = ""
59
-
60
-
61
- def create_router(app_version: str) -> APIRouter:
62
- router = APIRouter()
63
-
64
- @router.post("/auth/login")
65
- async def login(authorization: str | None = Header(default=None)):
66
- identity = require_identity(authorization)
67
- return {
68
- "ok": True,
69
- "version": app_version,
70
- "role": identity.get("role"),
71
- "subject_id": identity.get("id"),
72
- "name": identity.get("name"),
73
- }
74
-
75
- @router.get("/version")
76
- async def get_version():
77
- return {"version": app_version}
78
-
79
- @router.get("/api/settings")
80
- async def get_settings(authorization: str | None = Header(default=None)):
81
- require_admin(authorization)
82
- return {"config": config.get()}
83
-
84
- @router.get("/api/third-party-apps")
85
- async def get_third_party_apps(authorization: str | None = Header(default=None)):
86
- require_identity(authorization)
87
- return {"third_party_apps": config.get_third_party_apps_settings()}
88
-
89
- @router.post("/api/settings")
90
- async def save_settings(body: SettingsUpdateRequest, authorization: str | None = Header(default=None)):
91
- require_admin(authorization)
92
- try:
93
- return {"config": config.update(body.model_dump(mode="python"))}
94
- except ValueError as exc:
95
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
96
-
97
- @router.get("/api/images")
98
- async def get_images(request: Request, start_date: str = "", end_date: str = "", authorization: str | None = Header(default=None)):
99
- require_admin(authorization)
100
- return list_images(resolve_image_base_url(request), start_date=start_date.strip(), end_date=end_date.strip())
101
-
102
- @router.get("/images/{image_path:path}", include_in_schema=False)
103
- async def get_image(image_path: str):
104
- return get_image_response(image_path)
105
-
106
- @router.get("/image-thumbnails/{image_path:path}", include_in_schema=False)
107
- async def get_image_thumbnail(image_path: str):
108
- return get_thumbnail_response(image_path)
109
-
110
- @router.post("/api/images/delete")
111
- async def delete_images_endpoint(body: ImageDeleteRequest, authorization: str | None = Header(default=None)):
112
- require_admin(authorization)
113
- return delete_images(body.paths, start_date=body.start_date.strip(), end_date=body.end_date.strip(), all_matching=body.all_matching)
114
-
115
- @router.post("/api/images/download")
116
- async def download_images_endpoint(body: ImageDownloadRequest, authorization: str | None = Header(default=None)):
117
- require_admin(authorization)
118
- buf = download_images_zip(body.paths)
119
- return StreamingResponse(
120
- buf,
121
- media_type="application/zip",
122
- headers={"Content-Disposition": 'attachment; filename="images.zip"'},
123
- )
124
-
125
- @router.get("/api/images/download/{image_path:path}")
126
- async def download_single_image_endpoint(image_path: str, authorization: str | None = Header(default=None)):
127
- require_admin(authorization)
128
- return get_image_download_response(image_path)
129
-
130
- @router.get("/api/logs")
131
- async def get_logs(type: str = "", start_date: str = "", end_date: str = "", authorization: str | None = Header(default=None)):
132
- require_admin(authorization)
133
- return {"items": log_service.list(type=type.strip(), start_date=start_date.strip(), end_date=end_date.strip())}
134
-
135
- @router.post("/api/logs/delete")
136
- async def delete_logs(body: LogDeleteRequest, authorization: str | None = Header(default=None)):
137
- require_admin(authorization)
138
- return log_service.delete(body.ids)
139
-
140
- @router.post("/api/proxy/test")
141
- async def test_proxy_endpoint(body: ProxyTestRequest, authorization: str | None = Header(default=None)):
142
- require_admin(authorization)
143
- return {"result": await run_in_threadpool(test_proxy, (body.url or "").strip())}
144
-
145
- @router.get("/api/proxy/runtime")
146
- async def get_proxy_runtime_endpoint(authorization: str | None = Header(default=None)):
147
- require_admin(authorization)
148
- return {
149
- "runtime": config.get_public_proxy_runtime_settings(),
150
- "status": proxy_settings.get_runtime_status(),
151
- }
152
-
153
- @router.post("/api/proxy/runtime")
154
- async def save_proxy_runtime_endpoint(body: SettingsUpdateRequest, authorization: str | None = Header(default=None)):
155
- require_admin(authorization)
156
- try:
157
- config.update({"proxy_runtime": body.model_dump(mode="python")})
158
- except ValueError as exc:
159
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
160
- return {
161
- "runtime": config.get_public_proxy_runtime_settings(),
162
- "status": proxy_settings.get_runtime_status(),
163
- }
164
-
165
- @router.post("/api/proxy/clearance/test")
166
- async def test_proxy_clearance_endpoint(body: ClearanceTestRequest, authorization: str | None = Header(default=None)):
167
- require_admin(authorization)
168
- return {"result": await run_in_threadpool(test_clearance, body.target_url)}
169
-
170
- @router.get("/api/storage/info")
171
- async def get_storage_info(authorization: str | None = Header(default=None)):
172
- require_admin(authorization)
173
- storage = config.get_storage_backend()
174
- return {
175
- "backend": storage.get_backend_info(),
176
- "health": storage.health_check(),
177
- }
178
-
179
- @router.post("/api/backup/test")
180
- async def test_backup_connection(authorization: str | None = Header(default=None)):
181
- require_admin(authorization)
182
- try:
183
- return {"result": await run_in_threadpool(backup_service.test_connection)}
184
- except BackupError as exc:
185
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
186
-
187
- @router.post("/api/image-storage/test")
188
- async def test_image_storage_endpoint(authorization: str | None = Header(default=None)):
189
- require_admin(authorization)
190
- return {"result": await run_in_threadpool(image_storage_service.test_webdav)}
191
-
192
- @router.post("/api/image-storage/sync")
193
- async def sync_image_storage_endpoint(authorization: str | None = Header(default=None)):
194
- require_admin(authorization)
195
- try:
196
- return {"result": await run_in_threadpool(image_storage_service.sync_all)}
197
- except ImageStorageError as exc:
198
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
199
-
200
- @router.get("/api/backups")
201
- async def get_backups(authorization: str | None = Header(default=None)):
202
- require_admin(authorization)
203
- try:
204
- return {
205
- "items": await run_in_threadpool(backup_service.list_backups),
206
- "state": backup_service.get_status(),
207
- "settings": backup_service.get_settings(),
208
- }
209
- except BackupError as exc:
210
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
211
-
212
- @router.post("/api/backups/run")
213
- async def run_backup_endpoint(authorization: str | None = Header(default=None)):
214
- require_admin(authorization)
215
- try:
216
- return {"result": await run_in_threadpool(backup_service.run_backup)}
217
- except BackupError as exc:
218
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
219
-
220
- @router.post("/api/backups/delete")
221
- async def delete_backup_endpoint(body: BackupDeleteRequest, authorization: str | None = Header(default=None)):
222
- require_admin(authorization)
223
- try:
224
- await run_in_threadpool(backup_service.delete_backup, body.key)
225
- return {"ok": True}
226
- except BackupError as exc:
227
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
228
-
229
- @router.get("/api/backups/detail")
230
- async def get_backup_detail(key: str = "", authorization: str | None = Header(default=None)):
231
- require_admin(authorization)
232
- try:
233
- return {"item": await run_in_threadpool(backup_service.get_backup_detail, key)}
234
- except BackupError as exc:
235
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
236
-
237
- @router.get("/api/backups/download")
238
- async def download_backup_endpoint(key: str = "", authorization: str | None = Header(default=None)):
239
- require_admin(authorization)
240
- try:
241
- item = await run_in_threadpool(backup_service.download_backup, key)
242
- except BackupError as exc:
243
- raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
244
- filename = str(item.get("name") or "backup.bin")
245
- quoted = quote(filename)
246
- headers = {
247
- "Content-Disposition": f"attachment; filename*=UTF-8''{quoted}",
248
- "Content-Length": str(int(item.get("size") or 0)),
249
- }
250
- return Response(
251
- content=bytes(item.get("payload") or b""),
252
- media_type=str(item.get("content_type") or "application/octet-stream"),
253
- headers=headers,
254
- )
255
-
256
-
257
- @router.get("/api/images/tags")
258
- async def list_image_tags(authorization: str | None = Header(default=None)):
259
- require_admin(authorization)
260
- return {"tags": get_all_tags()}
261
-
262
- @router.post("/api/images/tags")
263
- async def update_image_tags(body: ImageTagsRequest, authorization: str | None = Header(default=None)):
264
- require_admin(authorization)
265
- rel = body.path.strip().lstrip("/")
266
- if not rel:
267
- raise HTTPException(status_code=400, detail={"error": "path is required"})
268
- tags = set_tags(rel, body.tags)
269
- return {"ok": True, "tags": tags}
270
-
271
- @router.delete("/api/images/tags/{tag}")
272
- async def delete_image_tag(tag: str, authorization: str | None = Header(default=None)):
273
- require_admin(authorization)
274
- count = delete_tag(tag)
275
- return {"ok": True, "removed_from": count}
276
-
277
- @router.get("/api/images/storage")
278
- async def get_image_storage(authorization: str | None = Header(default=None)):
279
- require_admin(authorization)
280
- return storage_stats()
281
-
282
- @router.post("/api/images/storage/compress")
283
- async def compress_all_images(authorization: str | None = Header(default=None)):
284
- require_admin(authorization)
285
- return await run_in_threadpool(compress_images)
286
-
287
- @router.post("/api/images/storage/cleanup-to-target")
288
- async def cleanup_to_target(
289
- target_free_mb: int = 500,
290
- dry_run: bool = False,
291
- authorization: str | None = Header(default=None),
292
- ):
293
- require_admin(authorization)
294
- return await run_in_threadpool(delete_to_target, target_free_mb, dry_run)
295
-
296
- @router.get("/health", response_model=None)
297
- async def health_dashboard(format: str = Query(default="html")):
298
- from services.account_service import account_service as acct_svc
299
- stats = acct_svc.get_stats()
300
- storage = config.get_storage_backend()
301
- storage_health = storage.health_check()
302
- healthy = stats["active"] > 0
303
-
304
- stats_json = {
305
- "status": "ok" if healthy else "degraded",
306
- "healthy": healthy,
307
- "version": app_version,
308
- "storage": {"backend": storage.get_backend_info(), "health": storage_health},
309
- "proxy_runtime": proxy_settings.get_runtime_status(),
310
- "accounts": stats,
311
- }
312
- if format == "json":
313
- return stats_json
314
- return HTMLResponse(f"""<!DOCTYPE html>
315
- <html lang="zh">
316
- <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
317
- <title>号池健康监控 - chatgpt2api</title>
318
- <style>
319
- *{{margin:0;padding:0;box-sizing:border-box}}
320
- body{{font-family:system-ui,-apple-system,sans-serif;background:#0f1117;color:#e2e8f0;min-height:100vh}}
321
- .header{{background:#1a1d27;border-bottom:1px solid #2a2d3a;padding:16px 24px;display:flex;justify-content:space-between;align-items:center}}
322
- .header h1{{font-size:20px}}
323
- .status-dot{{display:inline-block;width:10px;height:10px;border-radius:50%;margin-right:8px}}
324
- .status-ok{{background:#22c55e;box-shadow:0 0 8px #22c55e88}}
325
- .status-degraded{{background:#f59e0b;box-shadow:0 0 8px #f59e0b88}}
326
- .container{{max-width:960px;margin:0 auto;padding:24px}}
327
- .cards{{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px;margin-bottom:24px}}
328
- .card{{background:#1a1d27;border:1px solid #2a2d3a;border-radius:10px;padding:16px}}
329
- .card .value{{font-size:28px;font-weight:700;margin:4px 0}}
330
- .card .label{{font-size:13px;color:#94a3b8}}
331
- .green{{color:#22c55e}}.yellow{{color:#f59e0b}}.red{{color:#ef4444}}.blue{{color:#6c63ff}}
332
- table{{width:100%;border-collapse:collapse;background:#1a1d27;border:1px solid #2a2d3a;border-radius:10px;overflow:hidden}}
333
- th{{background:#242836;font-weight:600;text-align:left;padding:10px 12px;font-size:12px;color:#94a3b8;text-transform:uppercase}}
334
- td{{padding:8px 12px;border-top:1px solid #2a2d3a;font-size:14px}}tr:hover td{{background:rgba(108,99,255,.05)}}
335
- .api-url{{font-family:monospace;font-size:12px;color:#6c63ff}}
336
- .refresh{{font-size:12px;color:#64748b;text-align:center;margin-top:24px}}
337
- </style>
338
- <meta http-equiv="refresh" content="30">
339
- </head>
340
- <body>
341
- <div class="header">
342
- <h1><span class="status-dot {'status-ok' if healthy else 'status-degraded'}"></span>号池健康监控</h1>
343
- <div style="font-size:13px;color:#94a3b8">v{app_version} · 30s 自动刷新</div>
344
- </div>
345
- <div class="container">
346
- <div class="cards">
347
- <div class="card"><div class="label">号池状态</div><div class="value {'green' if healthy else 'yellow'}">{'正常' if healthy else '异常'}</div></div>
348
- <div class="card"><div class="label">当前账号</div><div class="value blue">{stats['total']}</div></div>
349
- <div class="card"><div class="label">累计入库</div><div class="value">{stats['cumulative_total']}</div></div>
350
- <div class="card"><div class="label">可用账号</div><div class="value green">{stats['active']}</div></div>
351
- <div class="card"><div class="label">剩余额度</div><div class="value">{stats['total_quota']}</div></div>
352
- <div class="card"><div class="label">限流</div><div class="value yellow">{stats['limited']}</div></div>
353
- <div class="card"><div class="label">异常</div><div class="value red">{stats['abnormal']}</div></div>
354
- <div class="card"><div class="label">禁用</div><div class="value">{stats['disabled']}</div></div>
355
- <div class="card"><div class="label">成功/失败</div><div class="value">{stats['total_success']}<span style="font-size:18px;color:#94a3b8">/</span><span class="red">{stats['total_fail']}</span></div></div>
356
- </div>
357
- <h2 style="margin-bottom:12px;font-size:16px">账号类型分布</h2>
358
- <table>
359
- <tr><th>类型</th><th>数量</th></tr>
360
- {''.join(f'<tr><td>{t}</td><td>{c}</td></tr>' for t,c in sorted(stats['by_type'].items()))}
361
- </table>
362
- <div class="refresh">JSON: <span class="api-url">/health?format=json</span></div>
363
- </div></body></html>""")
364
-
365
- return router
1
+ from __future__ import annotations
2
+
3
+ from urllib.parse import quote
4
+
5
+ from fastapi import APIRouter, Header, HTTPException, Query, Request
6
+ from fastapi.concurrency import run_in_threadpool
7
+ from fastapi.responses import HTMLResponse, Response, StreamingResponse
8
+ from pydantic import BaseModel, ConfigDict
9
+
10
+ from api.support import require_admin, require_identity, resolve_image_base_url
11
+ from services.backup_service import BackupError, backup_service
12
+ from services.config import config
13
+ from services.image_service import (
14
+ compress_images,
15
+ delete_images,
16
+ delete_to_target,
17
+ download_images_zip,
18
+ get_image_download_response,
19
+ get_image_response,
20
+ get_thumbnail_response,
21
+ list_images,
22
+ storage_stats,
23
+ )
24
+ from services.image_storage_service import ImageStorageError, image_storage_service
25
+ from services.image_tags_service import delete_tag, get_all_tags, set_tags
26
+ from services.log_service import log_service
27
+ from services.proxy_service import proxy_settings, test_clearance, test_proxy
28
+
29
+
30
+ class SettingsUpdateRequest(BaseModel):
31
+ model_config = ConfigDict(extra="allow")
32
+
33
+
34
+ class ProxyTestRequest(BaseModel):
35
+ url: str = ""
36
+
37
+
38
+ class ClearanceTestRequest(BaseModel):
39
+ target_url: str = "https://chatgpt.com"
40
+
41
+
42
+ class ImageDeleteRequest(BaseModel):
43
+ paths: list[str] = []
44
+ start_date: str = ""
45
+ end_date: str = ""
46
+ all_matching: bool = False
47
+
48
+ class ImageDownloadRequest(BaseModel):
49
+ paths: list[str]
50
+
51
+ class ImageTagsRequest(BaseModel):
52
+ path: str
53
+ tags: list[str]
54
+
55
+ class LogDeleteRequest(BaseModel):
56
+ ids: list[str] = []
57
+ class BackupDeleteRequest(BaseModel):
58
+ key: str = ""
59
+
60
+
61
+ def create_router(app_version: str) -> APIRouter:
62
+ router = APIRouter()
63
+
64
+ @router.post("/auth/login")
65
+ async def login(authorization: str | None = Header(default=None)):
66
+ identity = require_identity(authorization)
67
+ return {
68
+ "ok": True,
69
+ "version": app_version,
70
+ "role": identity.get("role"),
71
+ "subject_id": identity.get("id"),
72
+ "name": identity.get("name"),
73
+ }
74
+
75
+ @router.get("/version")
76
+ async def get_version():
77
+ return {"version": app_version}
78
+
79
+ @router.get("/api/settings")
80
+ async def get_settings(authorization: str | None = Header(default=None)):
81
+ require_admin(authorization)
82
+ return {"config": config.get()}
83
+
84
+ @router.get("/api/third-party-apps")
85
+ async def get_third_party_apps(authorization: str | None = Header(default=None)):
86
+ require_identity(authorization)
87
+ return {"third_party_apps": config.get_third_party_apps_settings()}
88
+
89
+ @router.post("/api/settings")
90
+ async def save_settings(body: SettingsUpdateRequest, authorization: str | None = Header(default=None)):
91
+ require_admin(authorization)
92
+ try:
93
+ return {"config": config.update(body.model_dump(mode="python"))}
94
+ except ValueError as exc:
95
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
96
+
97
+ @router.get("/api/images")
98
+ async def get_images(request: Request, start_date: str = "", end_date: str = "", authorization: str | None = Header(default=None)):
99
+ require_admin(authorization)
100
+ return list_images(resolve_image_base_url(request), start_date=start_date.strip(), end_date=end_date.strip())
101
+
102
+ @router.get("/images/{image_path:path}", include_in_schema=False)
103
+ async def get_image(image_path: str):
104
+ return get_image_response(image_path)
105
+
106
+ @router.get("/image-thumbnails/{image_path:path}", include_in_schema=False)
107
+ async def get_image_thumbnail(image_path: str):
108
+ return get_thumbnail_response(image_path)
109
+
110
+ @router.post("/api/images/delete")
111
+ async def delete_images_endpoint(body: ImageDeleteRequest, authorization: str | None = Header(default=None)):
112
+ require_admin(authorization)
113
+ return delete_images(body.paths, start_date=body.start_date.strip(), end_date=body.end_date.strip(), all_matching=body.all_matching)
114
+
115
+ @router.post("/api/images/download")
116
+ async def download_images_endpoint(body: ImageDownloadRequest, authorization: str | None = Header(default=None)):
117
+ require_admin(authorization)
118
+ buf = download_images_zip(body.paths)
119
+ return StreamingResponse(
120
+ buf,
121
+ media_type="application/zip",
122
+ headers={"Content-Disposition": 'attachment; filename="images.zip"'},
123
+ )
124
+
125
+ @router.get("/api/images/download/{image_path:path}")
126
+ async def download_single_image_endpoint(image_path: str, authorization: str | None = Header(default=None)):
127
+ require_admin(authorization)
128
+ return get_image_download_response(image_path)
129
+
130
+ @router.get("/api/logs")
131
+ async def get_logs(type: str = "", start_date: str = "", end_date: str = "", authorization: str | None = Header(default=None)):
132
+ require_admin(authorization)
133
+ return {"items": log_service.list(type=type.strip(), start_date=start_date.strip(), end_date=end_date.strip())}
134
+
135
+ @router.post("/api/logs/delete")
136
+ async def delete_logs(body: LogDeleteRequest, authorization: str | None = Header(default=None)):
137
+ require_admin(authorization)
138
+ return log_service.delete(body.ids)
139
+
140
+ @router.post("/api/proxy/test")
141
+ async def test_proxy_endpoint(body: ProxyTestRequest, authorization: str | None = Header(default=None)):
142
+ require_admin(authorization)
143
+ return {"result": await run_in_threadpool(test_proxy, (body.url or "").strip())}
144
+
145
+ @router.get("/api/proxy/runtime")
146
+ async def get_proxy_runtime_endpoint(authorization: str | None = Header(default=None)):
147
+ require_admin(authorization)
148
+ return {
149
+ "runtime": config.get_public_proxy_runtime_settings(),
150
+ "status": proxy_settings.get_runtime_status(),
151
+ }
152
+
153
+ @router.post("/api/proxy/runtime")
154
+ async def save_proxy_runtime_endpoint(body: SettingsUpdateRequest, authorization: str | None = Header(default=None)):
155
+ require_admin(authorization)
156
+ try:
157
+ config.update({"proxy_runtime": body.model_dump(mode="python")})
158
+ except ValueError as exc:
159
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
160
+ return {
161
+ "runtime": config.get_public_proxy_runtime_settings(),
162
+ "status": proxy_settings.get_runtime_status(),
163
+ }
164
+
165
+ @router.post("/api/proxy/clearance/test")
166
+ async def test_proxy_clearance_endpoint(body: ClearanceTestRequest, authorization: str | None = Header(default=None)):
167
+ require_admin(authorization)
168
+ return {"result": await run_in_threadpool(test_clearance, body.target_url)}
169
+
170
+ @router.get("/api/storage/info")
171
+ async def get_storage_info(authorization: str | None = Header(default=None)):
172
+ require_admin(authorization)
173
+ storage = config.get_storage_backend()
174
+ return {
175
+ "backend": storage.get_backend_info(),
176
+ "health": storage.health_check(),
177
+ }
178
+
179
+ @router.post("/api/backup/test")
180
+ async def test_backup_connection(authorization: str | None = Header(default=None)):
181
+ require_admin(authorization)
182
+ try:
183
+ return {"result": await run_in_threadpool(backup_service.test_connection)}
184
+ except BackupError as exc:
185
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
186
+
187
+ @router.post("/api/image-storage/test")
188
+ async def test_image_storage_endpoint(authorization: str | None = Header(default=None)):
189
+ require_admin(authorization)
190
+ return {"result": await run_in_threadpool(image_storage_service.test_webdav)}
191
+
192
+ @router.post("/api/image-storage/sync")
193
+ async def sync_image_storage_endpoint(authorization: str | None = Header(default=None)):
194
+ require_admin(authorization)
195
+ try:
196
+ return {"result": await run_in_threadpool(image_storage_service.sync_all)}
197
+ except ImageStorageError as exc:
198
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
199
+
200
+ @router.get("/api/backups")
201
+ async def get_backups(authorization: str | None = Header(default=None)):
202
+ require_admin(authorization)
203
+ try:
204
+ return {
205
+ "items": await run_in_threadpool(backup_service.list_backups),
206
+ "state": backup_service.get_status(),
207
+ "settings": backup_service.get_settings(),
208
+ }
209
+ except BackupError as exc:
210
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
211
+
212
+ @router.post("/api/backups/run")
213
+ async def run_backup_endpoint(authorization: str | None = Header(default=None)):
214
+ require_admin(authorization)
215
+ try:
216
+ return {"result": await run_in_threadpool(backup_service.run_backup)}
217
+ except BackupError as exc:
218
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
219
+
220
+ @router.post("/api/backups/delete")
221
+ async def delete_backup_endpoint(body: BackupDeleteRequest, authorization: str | None = Header(default=None)):
222
+ require_admin(authorization)
223
+ try:
224
+ await run_in_threadpool(backup_service.delete_backup, body.key)
225
+ return {"ok": True}
226
+ except BackupError as exc:
227
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
228
+
229
+ @router.get("/api/backups/detail")
230
+ async def get_backup_detail(key: str = "", authorization: str | None = Header(default=None)):
231
+ require_admin(authorization)
232
+ try:
233
+ return {"item": await run_in_threadpool(backup_service.get_backup_detail, key)}
234
+ except BackupError as exc:
235
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
236
+
237
+ @router.get("/api/backups/download")
238
+ async def download_backup_endpoint(key: str = "", authorization: str | None = Header(default=None)):
239
+ require_admin(authorization)
240
+ try:
241
+ item = await run_in_threadpool(backup_service.download_backup, key)
242
+ except BackupError as exc:
243
+ raise HTTPException(status_code=400, detail={"error": str(exc)}) from exc
244
+ filename = str(item.get("name") or "backup.bin")
245
+ quoted = quote(filename)
246
+ headers = {
247
+ "Content-Disposition": f"attachment; filename*=UTF-8''{quoted}",
248
+ "Content-Length": str(int(item.get("size") or 0)),
249
+ }
250
+ return Response(
251
+ content=bytes(item.get("payload") or b""),
252
+ media_type=str(item.get("content_type") or "application/octet-stream"),
253
+ headers=headers,
254
+ )
255
+
256
+
257
+ @router.get("/api/images/tags")
258
+ async def list_image_tags(authorization: str | None = Header(default=None)):
259
+ require_admin(authorization)
260
+ return {"tags": get_all_tags()}
261
+
262
+ @router.post("/api/images/tags")
263
+ async def update_image_tags(body: ImageTagsRequest, authorization: str | None = Header(default=None)):
264
+ require_admin(authorization)
265
+ rel = body.path.strip().lstrip("/")
266
+ if not rel:
267
+ raise HTTPException(status_code=400, detail={"error": "path is required"})
268
+ tags = set_tags(rel, body.tags)
269
+ return {"ok": True, "tags": tags}
270
+
271
+ @router.delete("/api/images/tags/{tag}")
272
+ async def delete_image_tag(tag: str, authorization: str | None = Header(default=None)):
273
+ require_admin(authorization)
274
+ count = delete_tag(tag)
275
+ return {"ok": True, "removed_from": count}
276
+
277
+ @router.get("/api/images/storage")
278
+ async def get_image_storage(authorization: str | None = Header(default=None)):
279
+ require_admin(authorization)
280
+ return storage_stats()
281
+
282
+ @router.post("/api/images/storage/compress")
283
+ async def compress_all_images(authorization: str | None = Header(default=None)):
284
+ require_admin(authorization)
285
+ return await run_in_threadpool(compress_images)
286
+
287
+ @router.post("/api/images/storage/cleanup-to-target")
288
+ async def cleanup_to_target(
289
+ target_free_mb: int = 500,
290
+ dry_run: bool = False,
291
+ authorization: str | None = Header(default=None),
292
+ ):
293
+ require_admin(authorization)
294
+ return await run_in_threadpool(delete_to_target, target_free_mb, dry_run)
295
+
296
+ @router.get("/health", response_model=None)
297
+ async def health_dashboard(format: str = Query(default="html")):
298
+ from services.account_service import account_service as acct_svc
299
+ stats = acct_svc.get_stats()
300
+ storage = config.get_storage_backend()
301
+ storage_health = storage.health_check()
302
+ healthy = stats["active"] > 0
303
+
304
+ stats_json = {
305
+ "status": "ok" if healthy else "degraded",
306
+ "healthy": healthy,
307
+ "version": app_version,
308
+ "storage": {"backend": storage.get_backend_info(), "health": storage_health},
309
+ "proxy_runtime": proxy_settings.get_runtime_status(),
310
+ "accounts": stats,
311
+ }
312
+ if format == "json":
313
+ return stats_json
314
+ return HTMLResponse(f"""<!DOCTYPE html>
315
+ <html lang="zh">
316
+ <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
317
+ <title>号池健康监控 - chatgpt2api</title>
318
+ <style>
319
+ *{{margin:0;padding:0;box-sizing:border-box}}
320
+ body{{font-family:system-ui,-apple-system,sans-serif;background:#0f1117;color:#e2e8f0;min-height:100vh}}
321
+ .header{{background:#1a1d27;border-bottom:1px solid #2a2d3a;padding:16px 24px;display:flex;justify-content:space-between;align-items:center}}
322
+ .header h1{{font-size:20px}}
323
+ .status-dot{{display:inline-block;width:10px;height:10px;border-radius:50%;margin-right:8px}}
324
+ .status-ok{{background:#22c55e;box-shadow:0 0 8px #22c55e88}}
325
+ .status-degraded{{background:#f59e0b;box-shadow:0 0 8px #f59e0b88}}
326
+ .container{{max-width:960px;margin:0 auto;padding:24px}}
327
+ .cards{{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px;margin-bottom:24px}}
328
+ .card{{background:#1a1d27;border:1px solid #2a2d3a;border-radius:10px;padding:16px}}
329
+ .card .value{{font-size:28px;font-weight:700;margin:4px 0}}
330
+ .card .label{{font-size:13px;color:#94a3b8}}
331
+ .green{{color:#22c55e}}.yellow{{color:#f59e0b}}.red{{color:#ef4444}}.blue{{color:#6c63ff}}
332
+ table{{width:100%;border-collapse:collapse;background:#1a1d27;border:1px solid #2a2d3a;border-radius:10px;overflow:hidden}}
333
+ th{{background:#242836;font-weight:600;text-align:left;padding:10px 12px;font-size:12px;color:#94a3b8;text-transform:uppercase}}
334
+ td{{padding:8px 12px;border-top:1px solid #2a2d3a;font-size:14px}}tr:hover td{{background:rgba(108,99,255,.05)}}
335
+ .api-url{{font-family:monospace;font-size:12px;color:#6c63ff}}
336
+ .refresh{{font-size:12px;color:#64748b;text-align:center;margin-top:24px}}
337
+ </style>
338
+ <meta http-equiv="refresh" content="30">
339
+ </head>
340
+ <body>
341
+ <div class="header">
342
+ <h1><span class="status-dot {'status-ok' if healthy else 'status-degraded'}"></span>号池健康监控</h1>
343
+ <div style="font-size:13px;color:#94a3b8">v{app_version} · 30s 自动刷新</div>
344
+ </div>
345
+ <div class="container">
346
+ <div class="cards">
347
+ <div class="card"><div class="label">号池状态</div><div class="value {'green' if healthy else 'yellow'}">{'正常' if healthy else '异常'}</div></div>
348
+ <div class="card"><div class="label">当前账号</div><div class="value blue">{stats['total']}</div></div>
349
+ <div class="card"><div class="label">累计入库</div><div class="value">{stats['cumulative_total']}</div></div>
350
+ <div class="card"><div class="label">可用账号</div><div class="value green">{stats['active']}</div></div>
351
+ <div class="card"><div class="label">剩余额度</div><div class="value">{stats['total_quota']}</div></div>
352
+ <div class="card"><div class="label">限流</div><div class="value yellow">{stats['limited']}</div></div>
353
+ <div class="card"><div class="label">异常</div><div class="value red">{stats['abnormal']}</div></div>
354
+ <div class="card"><div class="label">禁用</div><div class="value">{stats['disabled']}</div></div>
355
+ <div class="card"><div class="label">成功/失败</div><div class="value">{stats['total_success']}<span style="font-size:18px;color:#94a3b8">/</span><span class="red">{stats['total_fail']}</span></div></div>
356
+ </div>
357
+ <h2 style="margin-bottom:12px;font-size:16px">账号类型分布</h2>
358
+ <table>
359
+ <tr><th>类型</th><th>数量</th></tr>
360
+ {''.join(f'<tr><td>{t}</td><td>{c}</td></tr>' for t,c in sorted(stats['by_type'].items()))}
361
+ </table>
362
+ <div class="refresh">JSON: <span class="api-url">/health?format=json</span></div>
363
+ </div></body></html>""")
364
+
365
+ return router