feat(events): Add EventEmitter and instrument ai_client.py
This commit is contained in:
@@ -19,6 +19,7 @@ from pathlib import Path
|
|||||||
import file_cache
|
import file_cache
|
||||||
import mcp_client
|
import mcp_client
|
||||||
import google.genai
|
import google.genai
|
||||||
|
from events import EventEmitter
|
||||||
|
|
||||||
_provider: str = "gemini"
|
_provider: str = "gemini"
|
||||||
_model: str = "gemini-2.5-flash"
|
_model: str = "gemini-2.5-flash"
|
||||||
@@ -27,6 +28,9 @@ _max_tokens: int = 8192
|
|||||||
|
|
||||||
_history_trunc_limit: int = 8000
|
_history_trunc_limit: int = 8000
|
||||||
|
|
||||||
|
# Global event emitter for API lifecycle events
|
||||||
|
events = EventEmitter()
|
||||||
|
|
||||||
def set_model_params(temp: float, max_tok: int, trunc_limit: int = 8000):
|
def set_model_params(temp: float, max_tok: int, trunc_limit: int = 8000):
|
||||||
global _temperature, _max_tokens, _history_trunc_limit
|
global _temperature, _max_tokens, _history_trunc_limit
|
||||||
_temperature = temp
|
_temperature = temp
|
||||||
|
|||||||
14
events.py
Normal file
14
events.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
class EventEmitter:
|
||||||
|
def __init__(self):
|
||||||
|
self._listeners = {}
|
||||||
|
|
||||||
|
def on(self, event_name, callback):
|
||||||
|
if event_name not in self._listeners:
|
||||||
|
self._listeners[event_name] = []
|
||||||
|
self._listeners[event_name].append(callback)
|
||||||
|
|
||||||
|
def emit(self, event_name, *args, **kwargs):
|
||||||
|
if event_name in self._listeners:
|
||||||
|
for callback in self._listeners[event_name]:
|
||||||
|
callback(*args, **kwargs)
|
||||||
20
tests/test_api_events.py
Normal file
20
tests/test_api_events.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
import ai_client
|
||||||
|
|
||||||
|
def test_ai_client_event_emitter_exists():
|
||||||
|
# This should fail initially because 'events' won't exist on ai_client
|
||||||
|
assert hasattr(ai_client, 'events')
|
||||||
|
assert ai_client.events is not None
|
||||||
|
|
||||||
|
def test_event_emission():
|
||||||
|
# We'll expect these event names based on the spec
|
||||||
|
mock_callback = MagicMock()
|
||||||
|
ai_client.events.on("request_start", mock_callback)
|
||||||
|
|
||||||
|
# Trigger something that should emit the event (once implemented)
|
||||||
|
# For now, we just test the emitter itself if we were to call it manually
|
||||||
|
ai_client.events.emit("request_start", payload={"model": "test"})
|
||||||
|
|
||||||
|
mock_callback.assert_called_once_with(payload={"model": "test"})
|
||||||
Reference in New Issue
Block a user