|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import json |
5 | 6 | import logging |
6 | 7 | from pathlib import Path |
7 | 8 |
|
|
11 | 12 | from app.agent.moderator_modes import ( |
12 | 13 | PRESET_MODES, |
13 | 14 | load_moderator_mode_config, |
| 15 | + reload_moderator_modes, |
14 | 16 | save_moderator_mode_config, |
15 | 17 | ) |
16 | 18 | from app.core.config import get_moderator_modes_dir, get_workspace_base |
17 | | -from app.core.libs_service import get_cached_modes_meta, list_assignable_items |
| 19 | +from app.core.libs_service import get_cached_modes_meta, invalidate_libs_cache, list_assignable_items |
18 | 20 | from app.models.schemas import ( |
19 | 21 | GenerateModeratorModeRequest, |
20 | 22 | GenerateModeratorModeResponse, |
21 | 23 | ModeratorModeConfig, |
22 | 24 | ModeratorModeInfo, |
23 | 25 | SetModeratorModeRequest, |
| 26 | + ShareModeratorModeRequest, |
24 | 27 | ) |
25 | 28 | from app.models.store import get_topic |
26 | 29 |
|
@@ -198,3 +201,81 @@ async def generate_moderator_mode_endpoint(topic_id: str, req: GenerateModerator |
198 | 201 | "custom_prompt": custom_prompt, |
199 | 202 | "config": config, |
200 | 203 | } |
| 204 | + |
| 205 | + |
| 206 | +@router.post("/topics/{topic_id}/moderator-mode/share", response_model=dict) |
| 207 | +def share_moderator_mode_to_platform(topic_id: str, req: ShareModeratorModeRequest): |
| 208 | + """Share topic's custom moderator mode to platform library (topiclab_shared source).""" |
| 209 | + topic = get_topic(topic_id) |
| 210 | + if not topic: |
| 211 | + raise HTTPException(status_code=404, detail="Topic not found") |
| 212 | + |
| 213 | + ws_base = get_workspace_base() |
| 214 | + ws_path = ws_base / "topics" / topic_id |
| 215 | + config = load_moderator_mode_config(ws_path) |
| 216 | + |
| 217 | + if config.get("mode_id") != "custom" or not config.get("custom_prompt"): |
| 218 | + raise HTTPException( |
| 219 | + status_code=400, |
| 220 | + detail="Topic must use a custom moderator mode with custom_prompt to share" |
| 221 | + ) |
| 222 | + |
| 223 | + # Reject if mode_id is built-in (default source) |
| 224 | + existing = PRESET_MODES.get(req.mode_id) |
| 225 | + if existing and existing.get("source") == "default": |
| 226 | + raise HTTPException( |
| 227 | + status_code=409, |
| 228 | + detail=f"Mode '{req.mode_id}' conflicts with built-in mode; choose a different id" |
| 229 | + ) |
| 230 | + |
| 231 | + modes_dir = get_moderator_modes_dir() |
| 232 | + shared_dir = modes_dir / "topiclab_shared" |
| 233 | + shared_dir.mkdir(parents=True, exist_ok=True) |
| 234 | + |
| 235 | + prompt_file_name = f"{req.mode_id}.md" |
| 236 | + (shared_dir / prompt_file_name).write_text(config["custom_prompt"], encoding="utf-8") |
| 237 | + |
| 238 | + name = req.name or req.mode_id.replace("_", " ").title() |
| 239 | + description = req.description or f"User-shared moderator mode: {name}" |
| 240 | + |
| 241 | + meta_path = shared_dir / "meta.json" |
| 242 | + if meta_path.exists(): |
| 243 | + meta = json.loads(meta_path.read_text(encoding="utf-8")) |
| 244 | + else: |
| 245 | + meta = { |
| 246 | + "common_sections": "moderator_common.md", |
| 247 | + "categories": { |
| 248 | + "topiclab": { |
| 249 | + "id": "topiclab", |
| 250 | + "name": "TopicLab", |
| 251 | + "description": "User-shared moderator modes from frontend", |
| 252 | + } |
| 253 | + }, |
| 254 | + "modes": {}, |
| 255 | + } |
| 256 | + # Ensure topiclab_shared is registered in main meta.json |
| 257 | + main_meta_path = modes_dir / "meta.json" |
| 258 | + main_meta = json.loads(main_meta_path.read_text(encoding="utf-8")) |
| 259 | + main_meta.setdefault("sources", {})["topiclab_shared"] = { |
| 260 | + "id": "topiclab_shared", |
| 261 | + "name": "TopicLab-共享", |
| 262 | + "description": "User-shared moderator modes from frontend", |
| 263 | + } |
| 264 | + main_meta_path.write_text(json.dumps(main_meta, ensure_ascii=False, indent=2), encoding="utf-8") |
| 265 | + meta.setdefault("modes", {})[req.mode_id] = { |
| 266 | + "id": req.mode_id, |
| 267 | + "source": "topiclab_shared", |
| 268 | + "name": name, |
| 269 | + "description": description, |
| 270 | + "category": "topiclab", |
| 271 | + "num_rounds": config.get("num_rounds", 5), |
| 272 | + "convergence_strategy": "User-defined flow", |
| 273 | + "prompt_file": prompt_file_name, |
| 274 | + "summary_scope": "key findings, consensus, disagreements", |
| 275 | + } |
| 276 | + meta_path.write_text(json.dumps(meta, ensure_ascii=False, indent=2), encoding="utf-8") |
| 277 | + |
| 278 | + reload_moderator_modes() |
| 279 | + invalidate_libs_cache() |
| 280 | + |
| 281 | + return {"message": "Moderator mode shared to platform successfully", "mode_id": req.mode_id} |
0 commit comments