File size: 2,236 Bytes
9fc4259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
# app.py

import gradio as gr
from agents.programmer import ProgrammerAgent
from agents.debugger import DebuggerAgent
from agents.base_agent import ACPMessage

# Initialize agents
codebot = ProgrammerAgent()
bugbot = DebuggerAgent()

# Chat history
conversation = []

def start_conversation():
    global conversation
    conversation = []  # reset

    # Initial message from BugBot
    msg = bugbot.create_message(
        receiver=codebot.name,
        performative="request",
        content="Can you write a Python function to reverse a list?"
    )
    conversation.append(str(msg))

    # CodeBot responds
    reply = codebot.receive_message(msg)
    conversation.append(str(reply))

    return "\n\n".join(conversation)

def continue_conversation():
    if len(conversation) < 2:
        return "Please start the conversation first."

    # Last reply came from CodeBot β†’ BugBot now responds
    last_msg = conversation[-1]
    parsed = parse_acp_string(last_msg)
    reply = bugbot.receive_message(parsed)
    conversation.append(str(reply))

    # CodeBot replies again
    reply2 = codebot.receive_message(reply)
    conversation.append(str(reply2))

    return "\n\n".join(conversation)

def parse_acp_string(message_str: str) -> ACPMessage:
    """
    Convert "[PERFORMATIVE] sender β†’ receiver: content"
    back to an ACPMessage object
    """
    try:
        parts = message_str.split("] ", 1)
        performative = parts[0][1:].lower()
        meta, content = parts[1].split(": ", 1)
        sender, receiver = [s.strip() for s in meta.split("β†’")]
        return ACPMessage(sender, receiver, performative, content.strip())
    except Exception:
        return ACPMessage("Unknown", "Unknown", "inform", "Failed to parse message.")

# Gradio interface
with gr.Blocks(title="BotTalks") as demo:
    gr.Markdown("# πŸ€– BotTalks: Programmer vs Debugger\nAgent Communication Protocol Chat")

    chatbox = gr.Textbox(label="Conversation", lines=20)

    with gr.Row():
        start_btn = gr.Button("Start Conversation πŸš€")
        next_btn = gr.Button("Next Turn πŸ”")

    start_btn.click(start_conversation, outputs=chatbox)
    next_btn.click(continue_conversation, outputs=chatbox)

# Run app
demo.launch()