File size: 10,108 Bytes
011bd45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import asyncio
from dataclasses import dataclass, field
from agents import Runner, RunHooks
import threading
from typing import Any, Dict, Optional, List
import traceback
import time
from datetime import datetime
import numpy as np
import gradio as gr

from .config import logger, env
@dataclass
class RunnerStep:
    """Log entry for a single Runner step"""
    timestamp: str
    step_type: str
    agent_name: str
    turn_number: int
    details: Dict[str, Any] = field(default_factory=dict)
    duration_ms: Optional[float] = None

    def __str__(self) -> str:
        return f"[{self.timestamp}][T{self.turn_number}][{self.step_type}]: {self.details}"

@dataclass
class Message:
    role: str
    content: str
    mode: str
    metadata: Dict[str, Any] = field(default_factory=dict)

    @classmethod
    def user(cls, content: str) -> "Message":
        return cls("user", content, '')
    
    @classmethod
    def system(cls, content: str) -> "Message":
        return cls("system", content, '')

    @classmethod
    def tool(cls, content: str, **kwargs) -> "Message":
        return cls("assistant", content, 'tool', kwargs)
    
    @classmethod
    def assistant(cls, content: str, mode='') -> "Message":
        return cls("assistant", content, mode)
    
    @classmethod
    def tts(cls, content: str) -> "Message":
        return cls("assistant", content, 'tts')
    
    def to_dict(self) -> Dict[str, Any]:
        result = {"role": self.role, "content": self.content}
        if self.mode == "tool":
            metadata = self.metadata.copy()
            if title := metadata.get("title"):
                metadata["title"] = title.title()
            result["metadata"] = metadata
        return result


@dataclass
class Snapshot:
    sender: str
    data: Any
    status: str = 'done'
    
    @property
    def gr(self):
        if isinstance(self.data, np.ndarray):
            return gr.Image(self.data)
        return self.data


class RunnerLoggerHooks(RunHooks):
    """Custom hooks to log every step of the Runner"""
    
    def __init__(self, memory_instance):
        super().__init__()
        self.memory = memory_instance
        self.current_turn = 0
        self.turn_start_time = None
    
    async def on_agent_start(self, context, agent):
        self.current_turn += 1
        self.turn_start_time = time.time()
        self.memory.snapshots.append(Snapshot(
            sender='agent',
            data='Deciding tools',
            status='pending'
        ))
        step = RunnerStep(
            timestamp=datetime.now().isoformat(),
            step_type="turn_start",
            agent_name=agent.name,
            turn_number=self.current_turn,
            details={"message": f"Starting turn {self.current_turn} with agent {agent.name}"}
        )
        self.memory.log_runner_step(step)
    
    async def on_agent_end(self, context, agent, result):
        if self.turn_start_time:
            duration = (time.time() - self.turn_start_time) * 1000
        else:
            duration = None
            
        step = RunnerStep(
            timestamp=datetime.now().isoformat(),
            step_type="agent_call",
            agent_name=agent.name,
            turn_number=self.current_turn,
            details={"message": f"Agent {agent.name} completed", "result_type": type(result).__name__},
            duration_ms=duration
        )
        self.memory.log_runner_step(step)
    
    async def on_tool_start(self, context, agent, tool_call):
        tool_name = getattr(tool_call, 'name', 'unknown')
        tool_args = None
        for attr in ['arguments', 'args', 'function', 'parameters']:
            if hasattr(tool_call, attr):
                tool_args = getattr(tool_call, attr)
                break
        self.memory.snapshots.append(Snapshot(
            sender='agent',
            status='pending',
            data=f'Calling **{tool_name}**'
        ))
        step = RunnerStep(
            timestamp=datetime.now().isoformat(),
            step_type="tool_call",
            agent_name=agent.name,
            turn_number=self.current_turn,
            details={
                "tool_name": tool_name,
                "tool_args": tool_args,
                "message": f"Calling tool {tool_name}"
            }
        )
        self.memory.log_runner_step(step)
    
    async def on_tool_end(self, context, agent, tool_call, result):
        # Handle different tool_call object attributes safely
        tool_name = getattr(tool_call, 'name', 'unknown')
        
        step = RunnerStep(
            timestamp=datetime.now().isoformat(),
            step_type="tool_result",
            agent_name=agent.name,
            turn_number=self.current_turn,
            details={
                "tool_name": tool_name,
                "result_length": len(str(result)) if result else 0,
                "message": f"Tool {tool_name} completed"
            }
        )
        self.memory.log_runner_step(step)


class Chat:
    def __init__(self):
        self.history = [] 

    def append(self, message: Message):
        self.history.append(message)
    
    @property
    def messages(self):
        return [i.to_dict() for i in self.history]


class Memory:
    def __init__(self, agent, limit: int = 200) -> None:
        self.limit: int = limit
        self.frames: list[Any] = []  
        self.snapshots: list[Any] = []      
        self.inputs: list[Any] = [] 
        self.chat = Chat()

        self.runner_steps: List[RunnerStep] = []
        self.step_limit: int = 1000  # Keep last 1000 steps
        self.logger_hooks: Optional[RunnerLoggerHooks] = None

        self._chat_q: asyncio.Queue[Any] = asyncio.Queue()
        self._input_q: asyncio.Queue[Any] = asyncio.Queue()
        self._loop: Optional[asyncio.AbstractEventLoop] = None
        self.is_waiting: bool = False
        self.is_running: bool = False
        self._last_frame_time: float = 0
        self.setup(agent)

    def log_runner_step(self, step: RunnerStep) -> None:
        """Log a runner step and maintain the step history limit"""
        self.runner_steps.append(step)
        logger.debug(f"[ 🛠️ ]{step}")
        while len(self.runner_steps) > self.step_limit:
            self.runner_steps.pop(0)

    def enqueue(self, data: Any) -> None:
        current_time = time.time()
        if  current_time-self._last_frame_time > 1.0 / env.fps:
            self._last_frame_time = current_time
            self.frames.append(data)
            while len(self.frames) > self.limit:
                self.frames.pop(0)
        return self.snapshots.pop(0) if self.snapshots else None
    
    def receive(self, text: str) -> None:
        self.chat.append(Message.user(text))
        self._loop.call_soon_threadsafe(self._chat_q.put_nowait, text)


    def setup(self, agent) -> None:
        """Bind *agent* and spawn the background monitor threads."""
        self.v_agent = agent
        self.logger_hooks = RunnerLoggerHooks(self)
        def _runner() -> None:
            self._loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self._loop)
            try:
                self._loop.create_task(self._monitor_chat())
                self._loop.run_forever()
            finally:
                self._loop.close()

        threading.Thread(target=_runner, daemon=True).start()
    
    async def _monitor_chat(self) -> None:
        """Process incoming chat messages, respecting the waiting gate."""
        while True:
            text = await self._chat_q.get()
            logger.debug(f"Processing: {text}")
            start_step = RunnerStep(
                timestamp=datetime.now().isoformat(),
                step_type="processing_start",
                agent_name=getattr(self.v_agent, 'name', 'unknown'),
                turn_number=0,
                details={"user_input": text}
            )
            self.log_runner_step(start_step)
            
            try:
                self.is_running = True
                result = await Runner.run(
                    starting_agent=self.v_agent,
                    input=text,
                    context=self,
                    hooks=self.logger_hooks  # Add our custom hooks here
                )
                
                self.is_running = False
                
                # Log successful completion
                success_step = RunnerStep(
                    timestamp=datetime.now().isoformat(),
                    step_type="final_output",
                    agent_name=getattr(self.v_agent, 'name', 'unknown'),
                    turn_number=self.logger_hooks.current_turn if self.logger_hooks else 0,
                    details={
                        "output_type": type(result.final_output).__name__,
                        "output_preview": str(result.final_output)[:100] + "..." if len(str(result.final_output)) > 100 else str(result.final_output)
                    }
                )
                self.log_runner_step(success_step)
                
            except Exception as exc:  # noqa: BLE001
                self.is_running = False
                full_traceback = traceback.format_exc()
                logger.debug(f"Error in _monitor_chat: {exc}\n{full_traceback}")
                
                # Log the error
                error_step = RunnerStep(
                    timestamp=datetime.now().isoformat(),
                    step_type="error",
                    agent_name=getattr(self.v_agent, 'name', 'unknown'),
                    turn_number=self.logger_hooks.current_turn if self.logger_hooks else 0,
                    details={
                        "error_type": type(exc).__name__,
                        "error_message": str(exc),
                        "traceback": full_traceback
                    }
                )
                self.log_runner_step(error_step)
                continue
            final = result.final_output.split('</think>', 1)[-1]
            self.chat.append(Message.assistant(final))
            await asyncio.sleep(0)