Skip to content

Commit

Permalink
Support setting callbacks on env change; Move debug level set in env
Browse files Browse the repository at this point in the history
  • Loading branch information
nitanmarcel authored and trufae committed Sep 29, 2024
1 parent 71b1b26 commit bb80d4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
13 changes: 13 additions & 0 deletions r2ai/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Iterable, Callable

class R2AiEnv(dict):
def __init__(self):
self._callbacks = {}

def add_callback(self, key: str, callback: Callable):
self._callbacks[key] = callback

def __setitem__(self, __key, __value) -> None:
if __key in self._callbacks:
self._callbacks[__key](__value)
return super().__setitem__(__key, __value)
6 changes: 5 additions & 1 deletion r2ai/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rich.rule import Rule
from signal import signal, SIGINT

from .env import R2AiEnv
from .large import Large
from .utils import merge_deltas
from .message_block import MessageBlock
Expand Down Expand Up @@ -606,7 +607,7 @@ def __init__(self):
self.auto_run = False
self.model = get_default_model()
self.last_model = ""
self.env = {}
self.env = R2AiEnv()
self.openai_client = None
self.anthropic_client = None
self.groq_client = None
Expand Down Expand Up @@ -648,6 +649,7 @@ def __init__(self):
self.env["http.path"] = ""
self.env["http.verbose"] = "true" # not used yet
self.env["http.chatctx"] = "false"
self.env["debug_level"] = "1"
if have_rlang:
self.env["chat.live"] = "false"
else:
Expand All @@ -658,6 +660,8 @@ def __init__(self):
self.env["chat.bubble"] = "false"
self.env["chat.reply"] = "false"
self.env["chat.code"] = "true"

self.env.add_callback("debug_level", lambda val: LOGGER.setLevel(int(val) * 10))

# No active block to start
# (blocks are visual representation of messages on the terminal)
Expand Down
14 changes: 2 additions & 12 deletions r2ai/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ def r2_cmd(x):
r2ai -w ([port]) start webserver (curl -D hello http://localhost:8000)
r2ai -W ([port]) start webserver in background
r2ai -VV visual mode
r2ai -W ([port]) start webserver in background
r2ai -V (num) set log level for this session
0: NOTSET, 1: DEBUG, 2: INFO,
3: WARNING, 4: ERROR, 5: CRITICAL
r2ai -V get current log level"""
r2ai -W ([port]) start webserver in background"""

def myprint(msg, file=None):
global print_buffer
Expand Down Expand Up @@ -449,7 +445,7 @@ def runline(ai, usertext):
elif usertext.startswith("' "):
if not autoai:
autoai = Interpreter()
autoai.auto_run = True
autoai.auto_run = True

autoai.chat(usertext[2:])

Expand All @@ -458,12 +454,6 @@ def runline(ai, usertext):
print("r2 is not available", file=sys.stderr)
else:
builtins.print(r2_cmd(usertext[1:]))
elif usertext.startswith("-V"):
arguments = usertext.split()
if len(arguments) > 1:
LOGGER.setLevel(int(arguments[-1]) * 10)
else:
print("{0:.0f}".format(LOGGER.level / 10))
elif usertext.startswith("-"):
print(f"Unknown flag '{usertext}'. See 'r2ai -h' for help", file=sys.stderr)
else:
Expand Down

0 comments on commit bb80d4c

Please sign in to comment.