11import sys
2- import os
32from abc import ABC , abstractmethod
43from pathlib import Path
54import logging
65
76from aider .coders import Coder
87from aider .models import Model
98from aider .io import InputOutput
10- from tenacity import retry , wait_exponential , RetryCallState , retry_if_exception_type
9+ import re
1110
1211
13- class APIError (Exception ):
14- def __init__ (self , status_code : int , message : str ):
15- self .status_code = status_code
16- self .message = message
17- super ().__init__ (f"API Error: { status_code } - { message } " )
18-
19- def handle_logging (logging_name : str , log_file : Path ):
12+ def handle_logging (logging_name : str , log_file : Path ) -> None :
13+ """Handle logging for agent"""
2014 logger = logging .getLogger (logging_name )
2115 logger .setLevel (logging .INFO )
2216 logger .propagate = False
@@ -26,17 +20,32 @@ def handle_logging(logging_name: str, log_file: Path):
2620 )
2721 logger .addHandler (logger_handler )
2822
23+
24+ class AgentReturn (ABC ):
25+ def __init__ (self , log_file : Path ):
26+ self .log_file = log_file
27+ self .last_cost = self .get_money_cost ()
28+
29+ def get_money_cost (self ) -> float :
30+ """Get accumulated money cost from log file"""
31+ last_cost = 0.0
32+ with open (self .log_file , "r" ) as file :
33+ for line in file :
34+ if "Tokens:" in line and "Cost:" in line :
35+ match = re .search (
36+ r"Cost: \$\d+\.\d+ message, \$(\d+\.\d+) session" , line
37+ )
38+ if match :
39+ last_cost = float (match .group (1 ))
40+ return last_cost
41+
42+
2943class Agents (ABC ):
30- def __init__ (self , max_iteration : int , retry_if_api_error_codes : tuple [ int , ...] = ( 429 , 503 , 529 ) ):
44+ def __init__ (self , max_iteration : int ):
3145 self .max_iteration = max_iteration
3246
33- # error code 429 is rate limit exceeded for openai and anthropic
34- # error code 503 is service overloaded for openai
35- # error code 529 is service overloaded for anthropic
36- self .retry_if_api_error_codes = retry_if_api_error_codes
37-
3847 @abstractmethod
39- def run (self ) -> None :
48+ def run (self ) -> AgentReturn :
4049 """Start agent"""
4150 raise NotImplementedError
4251
@@ -46,84 +55,81 @@ def __init__(self, max_iteration: int, model_name: str):
4655 super ().__init__ (max_iteration )
4756 self .model = Model (model_name )
4857
49- @retry (
50- wait = wait_exponential (multiplier = 1 , min = 4 , max = 10 ),
51- retry = retry_if_exception_type (APIError )
52- )
5358 def run (
5459 self ,
5560 message : str ,
5661 test_cmd : str ,
5762 lint_cmd : str ,
5863 fnames : list [str ],
5964 log_dir : Path ,
60- ) -> None :
65+ ) -> AgentReturn :
6166 """Start aider agent"""
62- try :
63- if test_cmd :
64- auto_test = True
65- else :
66- auto_test = False
67- if lint_cmd :
68- auto_lint = True
69- else :
70- auto_lint = False
71- log_dir = log_dir .resolve ()
72- log_dir .mkdir (parents = True , exist_ok = True )
73- input_history_file = log_dir / ".aider.input.history"
74- chat_history_file = log_dir / ".aider.chat.history.md"
75-
76- print (
77- f"check { os .path .abspath (chat_history_file )} for prompts and lm generations" ,
78- file = sys .stderr ,
79- )
80- # Set up logging
81- log_file = log_dir / "aider.log"
82- logging .basicConfig (
83- filename = log_file ,
84- level = logging .INFO ,
85- format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ,
86- )
67+ if test_cmd :
68+ auto_test = True
69+ else :
70+ auto_test = False
71+ if lint_cmd :
72+ auto_lint = True
73+ else :
74+ auto_lint = False
75+ log_dir = log_dir .resolve ()
76+ log_dir .mkdir (parents = True , exist_ok = True )
77+ input_history_file = log_dir / ".aider.input.history"
78+ chat_history_file = log_dir / ".aider.chat.history.md"
8779
88- # Redirect print statements to the log file
89- sys .stdout = open (log_file , "a" )
90- sys .stderr = open (log_file , "a" )
80+ # Set up logging
81+ log_file = log_dir / "aider.log"
82+ logging .basicConfig (
83+ filename = log_file ,
84+ level = logging .INFO ,
85+ format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ,
86+ )
9187
92- # Configure httpx and backoff logging
93- handle_logging ( "httpx" , log_file )
94- handle_logging ( "backoff" , log_file )
88+ # Redirect print statements to the log file
89+ sys . stdout = open ( log_file , "a" )
90+ sys . stderr = open ( log_file , "a" )
9591
96- io = InputOutput (
97- yes = True ,
98- input_history_file = input_history_file ,
99- chat_history_file = chat_history_file ,
100- )
101- coder = Coder .create (
102- main_model = self .model ,
103- fnames = fnames ,
104- auto_lint = auto_lint ,
105- auto_test = auto_test ,
106- lint_cmds = {"python" : lint_cmd },
107- test_cmd = test_cmd ,
108- io = io ,
92+ # Configure httpx and backoff logging
93+ handle_logging ("httpx" , log_file )
94+ handle_logging ("backoff" , log_file )
95+
96+ io = InputOutput (
97+ yes = True ,
98+ input_history_file = input_history_file ,
99+ chat_history_file = chat_history_file ,
100+ )
101+ coder = Coder .create (
102+ main_model = self .model ,
103+ fnames = fnames ,
104+ auto_lint = auto_lint ,
105+ auto_test = auto_test ,
106+ lint_cmds = {"python" : lint_cmd },
107+ test_cmd = test_cmd ,
108+ io = io ,
109+ )
110+ coder .max_reflection = self .max_iteration
111+ coder .stream = True
112+
113+ # Run the agent
114+ # coder.run(message)
115+
116+ #### TMP
117+ import time
118+ import random
119+
120+ time .sleep (random .random () * 5 )
121+ n = random .random () / 10
122+ with open (log_file , "a" ) as f :
123+ f .write (
124+ f"> Tokens: 33k sent, 1.3k received. Cost: $0.12 message, ${ n } session. \n "
109125 )
110- coder .max_reflection = self .max_iteration
111- coder .stream = False
112-
113- # Run the agent
114- raise Exception ("test" )
115- coder .run (message )
116-
117- except Exception as e :
118- # If the exception is related to API errors, raise an APIError
119- if hasattr (e , 'status_code' ) and e .status_code in self .retry_if_api_error_codes :
120- raise APIError (e .status_code , str (e ))
121- # For other exceptions, re-raise them
122- raise
123- finally :
124- # Close redirected stdout and stderr
125- sys .stdout .close ()
126- sys .stderr .close ()
127- # Restore original stdout and stderr
128- sys .stdout = sys .__stdout__
129- sys .stderr = sys .__stderr__
126+ #### TMP
127+
128+ # Close redirected stdout and stderr
129+ sys .stdout .close ()
130+ sys .stderr .close ()
131+ # Restore original stdout and stderr
132+ sys .stdout = sys .__stdout__
133+ sys .stderr = sys .__stderr__
134+
135+ return AgentReturn (log_file )
0 commit comments