File size: 3,432 Bytes
a9fb7e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
from utils import WORKFLOW_SVG_DIAGRAM
from config import WORKFLOW_GENERATE_SYSTEM_PROMPT, WORKFLOW_EXECUTE_SYSTEM_PROMPT

def handle_workflow_generation(description):
    """处理“工作流执行”标签页的生成逻辑"""
    # 在真实应用中,这里会使用 WORKFLOW_GENERATE_SYSTEM_PROMPT
    # We use a mock SVG diagram from utils
    svg_diagram = WORKFLOW_SVG_DIAGRAM
    
    steps = ["Step 1: Plan", "Step 2: Execute", "Step 3: Review"]
    initial_state = {"current_step": 0, "steps": steps}
    initial_status = f"**当前节点**: {steps[0]}"
    initial_chatbot_message = [(None, f"工作流已生成。让我们开始第一步:‘{steps[0]}’。请提供规划所需的信息。 ")]

    return svg_diagram, initial_status, initial_chatbot_message, initial_state

def handle_workflow_chat(user_input, chat_history, state):
    """处理工作流的交互式聊天"""
    if not state or not state.get("steps"):
        return chat_history, state, "", gr.update(interactive=False)

    chat_history.append((user_input, None))
    
    current_step_index = state["current_step"]
    steps = state["steps"]
    
    thinking_message = "..."
    chat_history[-1] = (user_input, thinking_message)
    yield chat_history, state, "", gr.update(interactive=False)

    current_step_index += 1
    state["current_step"] = current_step_index

    if current_step_index < len(steps):
        next_step_name = steps[current_step_index]
        response = f"好的,已完成上一步。现在我们进行 ‘{next_step_name}’。请提供相关信息。"
        new_status = f"**当前节点**: {next_step_name}"
        interactive = True
    else:
        response = "所有步骤均已完成!工作流结束。"
        new_status = "**状态**: 已完成"
        interactive = False

    chat_history.append((None, response))
    
    yield chat_history, state, new_status, gr.update(interactive=interactive)

def create_workflow_tab():
    with gr.TabItem("工作流执行", id="workflow_tab"):
        gr.Markdown("<p align='center'>由 <strong>Ring 💍</strong> 模型驱动</p>")
        with gr.Row():
            with gr.Column(scale=1):
                workflow_description_input = gr.Textbox(lines=7, label="工作流描述", placeholder="Describe the steps of your workflow...")
                gr.Examples(
                    examples=["规划一次东京之旅", "新用户引导流程", "内容审批流程"],
                    label="示例提示",
                    inputs=[workflow_description_input]
                )
                generate_workflow_button = gr.Button("✨ 生成工作流")
                workflow_visualization_output = gr.HTML(label="工作流图示")
            with gr.Column(scale=1):
                workflow_status_output = gr.Markdown(label="节点状态")
                workflow_chatbot = gr.Chatbot(label="执行对话", height=400)
                workflow_chat_input = gr.Textbox(label="交互输入", placeholder="Your response...", interactive=False)

    return {
        "workflow_description_input": workflow_description_input,
        "generate_workflow_button": generate_workflow_button,
        "workflow_visualization_output": workflow_visualization_output,
        "workflow_status_output": workflow_status_output,
        "workflow_chatbot": workflow_chatbot,
        "workflow_chat_input": workflow_chat_input
    }