| | |
| | | # -*- coding: utf-8 -*- |
| | | # -*- coding: utf-8 -*- |
| | | """ |
| | | 核心编排器 / 工作流引擎 |
| | | 管理六阶段状态机,协调各智能体执行,支持用户在任意阶段介入 |
| | |
| | | import os |
| | | import threading |
| | | import time |
| | | import copy |
| | | from datetime import datetime |
| | | from enum import Enum |
| | | from typing import Any, Callable, Dict, List, Optional |
| | |
| | | REFERENCE_GENERATION = "reference_generation" |
| | | VIDEO_GENERATION = "video_generation" |
| | | POST_PRODUCTION = "post_production" |
| | | COMPLETED = "session_completed" |
| | | COMPLETED = "completed" |
| | | |
| | | |
| | | STAGE_ORDER = [ |
| | |
| | | """工作流状态""" |
| | | |
| | | # 状态说明: |
| | | # - idle: 新建会话,还没有任何数据,也没有在运行 |
| | | # - pending: 新建会话,还没有任何数据,也没有在运行 |
| | | # - running: 会话正在运行 |
| | | # - waiting_in_stage: 会话在某一阶段内等待用户介入(如选择角色、选择图片等) |
| | | # - stage_completed: 会话完成了某一阶段,等待用户确定开始下一阶段 |
| | | # - session_completed: 会话全部完成 |
| | | # - waiting: 会话在某一阶段内等待用户介入(如选择角色、选择图片等) |
| | | # - completed: 会话完成了某一阶段,等待用户确定开始下一阶段 |
| | | # - completed: 会话全部完成 |
| | | # - stopped: 用户手动停止 |
| | | # - error: 执行中遇到错误 |
| | | |
| | | def __init__(self, session_id: str): |
| | | self.session_id = session_id |
| | | self.current_stage: WorkflowStage = WorkflowStage.INIT |
| | | self.status: str = "idle" |
| | | self.stages_completed: List[str] = [] # 已完成的阶段列表 |
| | | self.status: Dict[str, str] = { |
| | | stage.value: "pending" for stage in WorkflowStage if stage != WorkflowStage.INIT and stage != WorkflowStage.COMPLETED |
| | | } |
| | | self.artifacts: Dict[str, Any] = {} |
| | | self.error: Optional[str] = None |
| | | self.started_at: Optional[datetime] = None |
| | |
| | | "current_stage": self.current_stage.value, |
| | | "status": self.status, |
| | | "error": self.error, |
| | | "stages_completed": self.stages_completed, |
| | | "artifacts": self.artifacts, |
| | | "meta": self.meta, |
| | | "updated_at": self.updated_at, |
| | |
| | | |
| | | # 从磁盘数据恢复 WorkflowState |
| | | state = WorkflowState(session_id=session_id) |
| | | state.status = data.get('status', 'idle') |
| | | |
| | | stage_str = data.get('current_stage') |
| | | state.current_stage = WorkflowStage(stage_str) if stage_str else WorkflowStage.INIT |
| | | state.stages_completed = data.get('stages_completed', []) |
| | | |
| | | loaded_status = data.get('status') |
| | | if isinstance(loaded_status, str): |
| | | stages_completed = data.get('stages_completed', []) |
| | | for stage in WorkflowStage: |
| | | if stage != WorkflowStage.INIT and stage != WorkflowStage.COMPLETED: |
| | | if stage.value in stages_completed: |
| | | state.status[stage.value] = "completed" |
| | | elif stage.value == state.current_stage.value: |
| | | state.status[stage.value] = loaded_status |
| | | else: |
| | | state.status[stage.value] = "pending" |
| | | elif isinstance(loaded_status, dict): |
| | | state.status = loaded_status |
| | | |
| | | state.artifacts = data.get('artifacts', {}) |
| | | state.meta = data.get('meta', {}) |
| | | state.error = data.get('error') |
| | | state.updated_at = data.get('updated_at', 0) |
| | | |
| | | # 缓存到内存 |
| | |
| | | def stop_session(self, session_id: str): |
| | | self.get_stop_event(session_id).set() |
| | | state = self.get_state(session_id) |
| | | if state and state.status == "running": |
| | | state.status = "stopped" |
| | | if state and state.status.get(state.current_stage.value) == "running": |
| | | state.status[state.current_stage.value] = "stopped" |
| | | state.error = None # 清除错误,因为是主动停止 |
| | | state.updated_at = datetime.now() |
| | | self.save_session_to_disk(session_id) |
| | |
| | | except ValueError: |
| | | pass |
| | | return None |
| | | |
| | | # ──────────── 跨阶段同步逻辑 ──────────── |
| | | def _sync_artifacts_cross_stages(self, state: WorkflowState, stage: WorkflowStage, payload: Dict): |
| | | """ |
| | | 跨阶段数据同步钩子:当某个阶段产生新数据时,自动推送到后续阶段。 |
| | | 例如:剧本续写产生的新角色/分镜,自动同步到 Stage 2 和 Stage 3。 |
| | | """ |
| | | if not isinstance(payload, dict): |
| | | return |
| | | |
| | | # 案例 1: 剧本续写确认 (Script Confirmation) |
| | | if stage == WorkflowStage.SCRIPT_GENERATION: |
| | | # 获取合并后的角色、场景和剧集 |
| | | new_chars = payload.get("new_characters", []) |
| | | new_settings = payload.get("new_settings", []) |
| | | new_episodes = payload.get("new_episodes", []) |
| | | # 如果没有新增剧集数据,无需同步到 Stage 2 和 3 |
| | | if not new_episodes: |
| | | return |
| | | |
| | | # (A) 同步到第二阶段 (角色设计) |
| | | if new_chars or new_settings: |
| | | # 注意:WorkflowStage.CHARACTER_DESIGN 是 Enum,这里需要使用 .value |
| | | char_stage_key = WorkflowStage.CHARACTER_DESIGN.value |
| | | char_art = state.artifacts.get(char_stage_key) |
| | | if not isinstance(char_art, dict): |
| | | char_art = {"characters": [], "settings": [], "version": 1} |
| | | |
| | | existing_chars = char_art.get("characters", []) |
| | | for nc in new_chars: |
| | | if not any(c.get("id") == nc.get("character_id") for c in existing_chars): |
| | | existing_chars.append({ |
| | | "id": nc.get("character_id"), "name": nc.get("name"), "description": nc.get("description"), |
| | | "selected": "", "versions": [] |
| | | }) |
| | | char_art["characters"] = existing_chars |
| | | |
| | | existing_sets = char_art.get("settings", []) |
| | | for ns in new_settings: |
| | | if not any(s.get("id") == ns.get("setting_id") for s in existing_sets): |
| | | existing_sets.append({ |
| | | "id": ns.get("setting_id"), "name": ns.get("name"), "description": ns.get("description"), |
| | | "selected": "", "versions": [] |
| | | }) |
| | | char_art["settings"] = existing_sets |
| | | state.artifacts[char_stage_key] = char_art |
| | | |
| | | # (B) 同步到第三阶段 (分镜设计) |
| | | if new_episodes: |
| | | story_stage_key = WorkflowStage.STORYBOARD.value |
| | | story_art = state.artifacts.get(story_stage_key) |
| | | if not isinstance(story_art, dict): |
| | | story_art = {"episodes": [], "version": 1} |
| | | |
| | | existing_eps = story_art.get("episodes", []) |
| | | for ne in new_episodes: |
| | | ep_num = ne.get("episode_number") |
| | | if not any(e.get("episode_number") == ep_num for e in existing_eps): |
| | | existing_eps.append({ |
| | | "episode_number": ep_num, |
| | | "episode_title": ne.get("act_title") or f"第{ep_num}集", |
| | | "segments": [] |
| | | }) |
| | | existing_eps.sort(key=lambda x: x.get("episode_number", 0)) |
| | | story_art["episodes"] = existing_eps |
| | | state.artifacts[story_stage_key] = story_art |
| | | |
| | | # 案例 2: 分镜生成或修改同步到第四、第五阶段 (Storyboard -> Ref/Video) |
| | | if stage == WorkflowStage.STORYBOARD: |
| | | episodes = payload.get("episodes", []) if isinstance(payload, dict) else payload |
| | | if not isinstance(episodes, list): |
| | | return |
| | | |
| | | all_sync_clips = [] |
| | | for ep in episodes: |
| | | if not isinstance(ep, dict): continue |
| | | ep_n = ep.get("episode_number", 0) |
| | | for s_i, seg in enumerate(ep.get("segments", []), 1): |
| | | if not isinstance(seg, dict): continue |
| | | seg_id = seg.get("segment_id", f"seg_{ep_n:02d}_{s_i:02d}") |
| | | |
| | | # 汇总 segment 级别的描述和时长 |
| | | shots = seg.get("shots", []) |
| | | desc_video = " ".join([sh.get("plot") or sh.get("content") or "" for sh in shots]).strip() |
| | | desc_ref = " ".join([sh.get("visual_prompt") or sh.get("plot") or sh.get("content") or "" for sh in shots]).strip() |
| | | total_dur = seg.get("total_duration") or sum([sh.get("duration", 0) for sh in shots]) or 10 |
| | | |
| | | all_sync_clips.append({ |
| | | "segment_id": seg_id, |
| | | "desc_video": desc_video, |
| | | "desc_ref": desc_ref, |
| | | "duration": total_dur, |
| | | "episode": ep_n, |
| | | "index": s_i, |
| | | "name": f"第{ep_n}集-片段{s_i}" |
| | | }) |
| | | |
| | | if not all_sync_clips: |
| | | return |
| | | |
| | | # (A) 同步到第四阶段 (参考图生成) |
| | | ref_stage_key = WorkflowStage.REFERENCE_GENERATION.value |
| | | ref_art = state.artifacts.get(ref_stage_key) |
| | | if not isinstance(ref_art, dict): |
| | | ref_art = {"scenes": [], "version": 1} |
| | | |
| | | existing_scenes = ref_art.get("scenes", []) |
| | | for c_info in all_sync_clips: |
| | | id = c_info["segment_id"] |
| | | idx = next((i for i, s in enumerate(existing_scenes) if s.get("id") == id), -1) |
| | | if idx == -1: |
| | | existing_scenes.append({ |
| | | "id": id, |
| | | "name": c_info["name"], |
| | | "index": c_info["index"], |
| | | "description": c_info["desc_ref"], |
| | | "selected": "", |
| | | "versions": [], |
| | | "status": "pending", |
| | | "episode": c_info["episode"] |
| | | }) |
| | | else: |
| | | # 更新已有记录 |
| | | existing_scenes[idx]["description"] = c_info["desc_ref"] |
| | | existing_scenes[idx]["episode"] = c_info["episode"] |
| | | existing_scenes[idx]["index"] = c_info["index"] |
| | | existing_scenes[idx]["name"] = c_info["name"] |
| | | |
| | | # 排序:确保片段显示顺序正确 (按 id 排序,例如 seg_01_01 < seg_07_01) |
| | | existing_scenes.sort(key=lambda x: x.get("id", "")) |
| | | ref_art["scenes"] = existing_scenes |
| | | state.artifacts[ref_stage_key] = ref_art |
| | | |
| | | # (B) 同步到第五阶段 (视频生成) |
| | | video_stage_key = WorkflowStage.VIDEO_GENERATION.value |
| | | video_art = state.artifacts.get(video_stage_key) |
| | | if not isinstance(video_art, dict): |
| | | video_art = {"clips": [], "version": 1} |
| | | |
| | | existing_clips = video_art.get("clips", []) |
| | | for c_info in all_sync_clips: |
| | | id = c_info["segment_id"] |
| | | idx = next((i for i, c in enumerate(existing_clips) if c.get("id") == id), -1) |
| | | if idx == -1: |
| | | existing_clips.append({ |
| | | "id": id, |
| | | "name": c_info["name"], |
| | | "index": c_info["index"], |
| | | "description": c_info["desc_video"], |
| | | "duration": c_info["duration"], |
| | | "selected": "", |
| | | "versions": [], |
| | | "status": "pending", |
| | | "episode": c_info["episode"] |
| | | }) |
| | | else: |
| | | existing_clips[idx]["description"] = c_info["desc_video"] |
| | | existing_clips[idx]["duration"] = c_info["duration"] |
| | | existing_clips[idx]["episode"] = c_info["episode"] |
| | | existing_clips[idx]["index"] = c_info["index"] |
| | | existing_clips[idx]["name"] = c_info["name"] |
| | | |
| | | # 排序:确保片段显示顺序正确 |
| | | existing_clips.sort(key=lambda x: x.get("id", "")) |
| | | video_art["clips"] = existing_clips |
| | | state.artifacts[video_stage_key] = video_art |
| | | |
| | | def _recalculate_all_statuses(self, state: WorkflowState): |
| | | """ |
| | | 根据各阶段 artifacts 的数据完整性重新计算 status 字典。 |
| | | 逻辑: |
| | | - 如果 artifacts[stage] 不存在: pending |
| | | - 如果存在且包含核心列表(characters/scenes/clips等): |
| | | - 如果列表项存在 selected 为空的情况: waiting |
| | | - 如果列表项全部已选择(或不需要选择): completed |
| | | """ |
| | | for stage in WorkflowStage: |
| | | if stage in [WorkflowStage.INIT, WorkflowStage.COMPLETED]: |
| | | continue |
| | | s_val = stage.value |
| | | # 如果当前阶段正在运行,不自动覆盖其为 completed/waiting (除非它目前是空) |
| | | current_s_status = state.status.get(s_val, "pending") |
| | | if current_s_status == "running" or current_s_status == "error": |
| | | continue |
| | | |
| | | art = state.artifacts.get(s_val) |
| | | if not art or not isinstance(art, dict): |
| | | state.status[s_val] = "pending" |
| | | continue |
| | | |
| | | # 检查是否有待处理的“空数据占位” |
| | | has_pending = False |
| | | |
| | | if s_val == "character_design": |
| | | chars = art.get("characters", []) |
| | | sets = art.get("settings", []) |
| | | if any(not c.get("selected") for c in chars) or any(not s.get("selected") for s in sets): |
| | | has_pending = True |
| | | elif s_val == "storyboard": |
| | | # 检查分镜阶段:如果存在剧集(episode)但其 segments 为空,视为 waiting |
| | | episodes = art.get("episodes", []) |
| | | if not episodes or any(not ep.get("segments") for ep in episodes): |
| | | has_pending = True |
| | | elif s_val == "reference_generation": |
| | | scenes = art.get("scenes", []) |
| | | if not scenes or any(not s.get("selected") for s in scenes): |
| | | has_pending = True |
| | | elif s_val == "video_generation": |
| | | clips = art.get("clips", []) |
| | | if not clips or any(not c.get("selected") for c in clips): |
| | | has_pending = True |
| | | |
| | | if has_pending: |
| | | state.status[s_val] = "waiting" |
| | | else: |
| | | # 已经有数据且没有 pending 项,标记为完成 |
| | | state.status[s_val] = "completed" |
| | | |
| | | async def execute_stage(self, |
| | | state: WorkflowState, |
| | |
| | | agent.set_progress_callback(wrapped_progress_callback) |
| | | |
| | | state.current_stage = stage |
| | | state.status = "running" |
| | | state.status[stage.value] = "running" |
| | | state.updated_at = datetime.now() |
| | | |
| | | try: |
| | | result = await agent.process(input_data, intervention=intervention) |
| | | state.artifacts[stage.value] = result.get("payload") |
| | | if not isinstance(result, dict): |
| | | logger.error(f"[execute_stage] Agent {stage.value} returned non-dict result: {type(result)}") |
| | | result = {"payload": result} |
| | | |
| | | payload = result.get("payload", {}) |
| | | requires_intervention = result.get("requires_intervention", False) |
| | | |
| | | # 第三阶段修改分镜时:同步更新第四、第五阶段的相关内容 |
| | | if stage.value == "storyboard" and intervention and "modified_storyboard" in intervention: |
| | | modified_shots = intervention["modified_storyboard"] |
| | | if isinstance(modified_shots, list): |
| | | shot_durations = {s.get('shot_id'): s.get('duration', 10) |
| | | for s in modified_shots if s.get('shot_id')} |
| | | shot_visual_prompts = {s.get('shot_id'): s.get('visual_prompt', '') |
| | | for s in modified_shots if s.get('shot_id')} |
| | | shot_plots = {s.get('shot_id'): s.get('plot', '') |
| | | for s in modified_shots if s.get('shot_id')} |
| | | |
| | | # 1. 同步 duration 到第五阶段 clips |
| | | video_art = state.artifacts.get('video_generation', {}) |
| | | if isinstance(video_art, dict) and 'clips' in video_art: |
| | | for clip in video_art['clips']: |
| | | shot_id = clip.get('id') |
| | | if shot_id in shot_durations: |
| | | clip['duration'] = shot_durations[shot_id] |
| | | if shot_id in shot_plots: |
| | | clip['description'] = shot_plots[shot_id] |
| | | |
| | | # 2. 同步 visual_prompt 到第四阶段 scenes (description) |
| | | ref_art = state.artifacts.get('reference_generation', {}) |
| | | if isinstance(ref_art, dict) and 'scenes' in ref_art: |
| | | for scene in ref_art['scenes']: |
| | | shot_id = scene.get('id') |
| | | if shot_id in shot_visual_prompts: |
| | | scene['description'] = shot_visual_prompts[shot_id] |
| | | |
| | | # 3. 新增分镜:同步到第四、第五阶段 |
| | | existing_shot_ids = {clip.get('id') for clip in video_art.get('clips', [])} if isinstance(video_art, dict) else set() |
| | | existing_scene_ids = {scene.get('id') for scene in ref_art.get('scenes', [])} if isinstance(ref_art, dict) else set() |
| | | |
| | | for shot in modified_shots: |
| | | shot_id = shot.get('shot_id') |
| | | if not shot_id: |
| | | continue |
| | | # 新增到第五阶段 |
| | | if shot_id not in existing_shot_ids: |
| | | if isinstance(video_art, dict): |
| | | video_art.setdefault('clips', []).append({ |
| | | 'id': shot_id, |
| | | 'name': f"镜头{shot_id.split('_')[-1]}", |
| | | 'description': shot.get('plot', ''), |
| | | 'duration': shot.get('duration', 10), |
| | | 'selected': '', |
| | | 'versions': [], |
| | | 'status': 'pending' |
| | | }) |
| | | # 新增到第四阶段 |
| | | if shot_id not in existing_scene_ids: |
| | | if isinstance(ref_art, dict): |
| | | ref_art.setdefault('scenes', []).append({ |
| | | 'id': shot_id, |
| | | 'name': f"场景{shot_id.split('_')[-2]}", |
| | | 'description': shot.get('visual_prompt', ''), |
| | | 'selected': '', |
| | | 'versions': [], |
| | | 'status': 'pending' |
| | | }) |
| | | |
| | | logger.info(f"Synced stage 3 modifications: {len(shot_durations)} durations, {len(shot_visual_prompts)} prompts") |
| | | # 根据阶段类型处理数据持久化和同步逻辑 |
| | | if stage == WorkflowStage.SCRIPT_GENERATION: |
| | | if requires_intervention: |
| | | # 【续写预览状态】直接保存 payload 以保留 new_episodes 供前端显示。 |
| | | # 此时千万不要跨阶段同步(避免向第二、三阶段注入用户未确认的数据)。 |
| | | state.artifacts[stage.value] = copy.deepcopy(payload) |
| | | else: |
| | | # 【确定续写 / 正常生成状态】 |
| | | # 先同步增量数据到第二、三阶段 |
| | | self._sync_artifacts_cross_stages(state, stage, payload) |
| | | # 然后清理第一阶段内部的临时增量字段并保存 |
| | | clean_art = copy.deepcopy(payload) |
| | | for key in ["new_episodes", "new_characters", "new_settings", "sequel_idea"]: |
| | | clean_art.pop(key, None) |
| | | state.artifacts[stage.value] = clean_art |
| | | else: |
| | | # 其他阶段正常执行跨阶段同步和赋值 |
| | | self._sync_artifacts_cross_stages(state, stage, payload) |
| | | state.artifacts[stage.value] = payload |
| | | |
| | | # 调试日志 |
| | | logger.info(f"[execute_stage] stage={stage.value}, intervention={intervention is not None}, requires_intervention={result.get('requires_intervention')}, stage_completed={result.get('stage_completed')}") |
| | | |
| | | # 状态转换逻辑: |
| | | # - stage_completed=True: 阶段已完成,等待用户确认进入下一阶段 |
| | | # - requires_intervention=True: 阶段内需要用户介入(如选择图片等) |
| | | # - 其他(running):阶段正在执行中 |
| | | if result.get("stage_completed"): |
| | | # 阶段真正完成,标记到已完成的列表 |
| | | if stage.value not in state.stages_completed: |
| | | state.stages_completed.append(stage.value) |
| | | # 如果是最后一个阶段,设置为 session_completed |
| | | if stage == WorkflowStage.POST_PRODUCTION: |
| | | state.status = "session_completed" |
| | | else: |
| | | state.status = "stage_completed" |
| | | # 重新计算所有阶段的状态(基于 artifacts 里的数据完整性) |
| | | self._recalculate_all_statuses(state) |
| | | |
| | | # 如果 result 明确标记了完成且不是干预,则可能需要覆盖为 completed (除非 recalculate 认为是 waiting) |
| | | if result.get("stage_completed") and not result.get("requires_intervention"): |
| | | # 如果 recalculate 没把它设为 waiting,就设为 completed |
| | | if state.status.get(stage.value) != "waiting": |
| | | state.status[stage.value] = "completed" |
| | | elif result.get("requires_intervention"): |
| | | # 阶段内需要用户介入,等待用户选择 |
| | | state.status = "waiting_in_stage" |
| | | else: |
| | | # 阶段正在执行中(中间步骤),保持 running |
| | | state.status = "running" |
| | | state.status[stage.value] = "waiting" |
| | | |
| | | state.updated_at = datetime.now() |
| | | # 立即保存状态到磁盘,确保前端能获取到最新状态 |
| | |
| | | return result |
| | | |
| | | except Exception as e: |
| | | state.status = "error" |
| | | state.status[stage.value] = "error" |
| | | state.error = str(e) |
| | | state.updated_at = datetime.now() |
| | | # 确保保存错误状态 |
| | | self.save_session_to_disk(state.session_id) |
| | | raise |
| | | |
| | | async def handle_intervention(self, |
| | |
| | | input_data = current_artifact if isinstance(current_artifact, dict) else {} |
| | | input_data.update(modifications) |
| | | |
| | | return await self.execute_stage(state, stage_enum, input_data, intervention=modifications) |
| | | res = await self.execute_stage(state, stage_enum, input_data, intervention=modifications) |
| | | # 处理完干预后再次校验状态 |
| | | self._recalculate_all_statuses(state) |
| | | self.save_session_to_disk(session_id) |
| | | return res |
| | | |
| | | async def continue_workflow(self, session_id: str) -> Dict: |
| | | state = self.sessions[session_id] |
| | | logger.info(f"[continue_workflow] session={session_id}, current_stage={state.current_stage}, status={state.status}") |
| | | |
| | | # 检查当前阶段是否已完成 |
| | | current_stage_str = state.current_stage.value if hasattr(state.current_stage, 'value') else str(state.current_stage) |
| | | # 检查当前状态是否已完成 |
| | | current_stage_str = state.current_stage.value if hasattr(state.current_stage, "value") else str(state.current_stage) |
| | | |
| | | # 在继续之前,先重新扫描一次状态,确保最新的选择已被计入 |
| | | self._recalculate_all_statuses(state) |
| | | |
| | | # 如果当前状态是 running,说明阶段还在执行中,不能继续 |
| | | if state.status == "running": |
| | | if state.status.get(current_stage_str) == "running": |
| | | return { |
| | | "status": "waiting", |
| | | "openclaw": f"当前阶段({current_stage_str})还在执行中,请等待完成后再调用 /continue。", |
| | | "message": f"当前阶段({current_stage_str})还在执行中,请等待完成后再调用 /continue。", |
| | | "current_status": state.status, |
| | | "current_status": "running", |
| | | } |
| | | |
| | | # 状态转换逻辑: |
| | | # - waiting_in_stage 或 stage_completed: 用户确认后直接进入下一阶段 |
| | | # 注意:只有当阶段真正完成(waiting_in_stage 或 stage_completed)时才允许继续 |
| | | # - waiting 或 completed: 用户确认后直接进入下一阶段 |
| | | # 如果是 waiting,可能需要阻止进入下一阶段,除非业务允许强制通过 |
| | | # 这里我们遵循用户逻辑:如果有空数据,显示为 waiting,用户需要解决它才能真正 completed。 |
| | | if state.status.get(current_stage_str) == "waiting": |
| | | # 如果是 waiting 状态,通常不应该自动跳到下一阶段 |
| | | # 但如果用户点击了“继续”,可能是想补全或者强制进入 |
| | | pass |
| | | # 注意:只有当阶段真正完成(waiting 或 completed)时才允许继续 |
| | | |
| | | if state.status == "waiting_in_stage" or state.status == "stage_completed": |
| | | # 用户确认后标记阶段完成 |
| | | if current_stage_str not in state.stages_completed: |
| | | state.stages_completed.append(current_stage_str) |
| | | |
| | | current_status = state.status.get(current_stage_str) |
| | | if current_status == "waiting" or current_status == "completed": |
| | | # 直接进入下一阶段 |
| | | state.status = "running" |
| | | state.status[current_stage_str] = "completed" |
| | | next_stage = self._get_next_stage(state.current_stage) |
| | | |
| | | if not next_stage: |
| | | state.status = "session_completed" |
| | | state.status[current_stage_str] = "completed" |
| | | self.save_session_to_disk(state.session_id) |
| | | return {"status": "session_completed"} |
| | | return {"status": "completed", "session_id": state.session_id, "status_map": state.status} |
| | | |
| | | self.save_session_to_disk(state.session_id) |
| | | return {"status": "ready", "next_stage": next_stage.value} |
| | | return {"status": "ready", "next_stage": next_stage.value, "session_id": state.session_id, "status_map": state.status} |
| | | |
| | | # 其他状态(如 idle, stopped, error, session_completed)不允许继续 |
| | | # 其他状态(如 pending, stopped, error, completed)不允许继续 |
| | | return { |
| | | "status": "error", |
| | | "openclaw": f"当前状态 {state.status} 不允许继续,请检查会话状态。", |
| | | "openclaw": f"当前状态 {current_status} 不允许继续,请检查会话状态。", |
| | | "message": f"当前状态不允许继续", |
| | | "current_status": state.status, |
| | | "current_status": current_status, |
| | | } |
| | | |
| | | # ──────────── 会话持久化 ──────────── |
| | |
| | | |
| | | path = os.path.join(self._session_dir, f"{session_id}.json") |
| | | data: Dict[str, Any] = {} |
| | | state = self.sessions.get(session_id) |
| | | |
| | | # 1. 准备基础数据 |
| | | if os.path.exists(path): |
| | | try: |
| | | with open(path, 'r', encoding='utf-8') as f: |
| | | data = json.load(f) |
| | | except (json.JSONDecodeError, Exception): |
| | | # 文件损坏,忽略旧数据 |
| | | pass |
| | | |
| | | data["session_id"] = session_id |
| | | if meta: |
| | | for k, v in meta.items(): |
| | | data[k] = v |
| | | if "created_at" not in data: |
| | | data["created_at"] = time.time() |
| | | data["updated_at"] = time.time() |
| | | state = self.sessions.get(session_id) |
| | | |
| | | # 2. 将内存中的最新 state 合并到 data 中 |
| | | if state: |
| | | data["current_stage"] = state.current_stage.value |
| | | data["status"] = state.status |
| | | data["stages_completed"] = state.stages_completed |
| | | # 这里的 state.artifacts 应该是已经经过 _sync_artifacts_cross_stages 处理的最新的内存对象 |
| | | data["artifacts"] = state.artifacts |
| | | data["error"] = state.error |
| | | # datetime 对象需要转换为时间戳 |
| | | data["updated_at"] = state.updated_at.timestamp() if isinstance(state.updated_at, datetime) else state.updated_at |
| | | # 保存元数据(包含模型配置) |
| | | data["updated_at"] = state.updated_at.timestamp() if isinstance(state.updated_at, datetime) else time.time() |
| | | |
| | | # 保存元数据(将 meta 中的键展开到根目录下) |
| | | if state.meta: |
| | | for k, v in state.meta.items(): |
| | | if v is not None: |
| | | # 转换并保持原始类型(尤其是布尔值) |
| | | if isinstance(v, str) and v.lower() == 'true': |
| | | data[k] = True |
| | | elif isinstance(v, str) and v.lower() == 'false': |
| | | data[k] = False |
| | | else: |
| | | data[k] = v |
| | | else: |
| | | data["updated_at"] = time.time() |
| | | |
| | | # 原子写入:先写临时文件,再重命名 |
| | | # 3. 原子写入:先写临时文件,再重命名 |
| | | dir_path = os.path.dirname(path) |
| | | fd, tmp_path = tempfile.mkstemp(dir=dir_path, suffix='.json') |
| | | try: |
| | | with os.fdopen(fd, 'w', encoding='utf-8') as f: |
| | | json.dump(data, f, ensure_ascii=False, indent=2) |
| | | shutil.move(tmp_path, path) |
| | | except Exception: |
| | | # 写入失败,删除临时文件 |
| | | logger.info(f"[Orchestrator] Session {session_id} saved successfully.") |
| | | except Exception as e: |
| | | if os.path.exists(tmp_path): |
| | | os.remove(tmp_path) |
| | | logger.error(f"[Orchestrator] Failed to save session {session_id}: {e}") |
| | | raise |
| | | |
| | | def _load_sessions_from_disk(self): |
| | |
| | | state.current_stage = WorkflowStage(data.get("current_stage", "init")) |
| | | except ValueError: |
| | | state.current_stage = WorkflowStage.INIT |
| | | # 旧版本兼容:状态名称转换 |
| | | old_status = data.get("status", "idle") |
| | | if old_status == "waiting_intervention": |
| | | state.status = "waiting_in_stage" |
| | | elif old_status == "completed": |
| | | state.status = "session_completed" |
| | | else: |
| | | |
| | | # 旧版本兼容:状态名称转换及迁移 |
| | | old_status = data.get("status", "pending") |
| | | if isinstance(old_status, str): |
| | | if old_status == "waiting_intervention": |
| | | old_status = "waiting" |
| | | elif old_status == "completed": |
| | | old_status = "completed" |
| | | |
| | | stages_completed = data.get("stages_completed", []) |
| | | for stage in WorkflowStage: |
| | | if stage != WorkflowStage.INIT and stage != WorkflowStage.COMPLETED: |
| | | if stage.value in stages_completed: |
| | | state.status[stage.value] = "completed" |
| | | elif stage.value == state.current_stage.value: |
| | | state.status[stage.value] = old_status |
| | | else: |
| | | state.status[stage.value] = "pending" |
| | | elif isinstance(old_status, dict): |
| | | state.status = old_status |
| | | |
| | | state.stages_completed = data.get("stages_completed", []) |
| | | state.artifacts = data.get("artifacts", {}) |
| | | state.error = data.get("error") |
| | | state.updated_at = data.get("updated_at", 0) |
| | |
| | | "idea": data.get("idea", ""), |
| | | "style": data.get("style", ""), |
| | | "date": data.get("updated_at", 0), |
| | | "stages": data.get("stages_completed", []), |
| | | "status": data.get("status", {}), |
| | | }) |
| | | except Exception: |
| | | continue |