feat: replace synchronous HTTP calls with async client and add background task tracking

This commit is contained in:
2025-11-06 15:44:41 +00:00
parent 99d7ec53d5
commit e018cff131
19 changed files with 531 additions and 1771 deletions
-5
View File
@@ -43,11 +43,6 @@ from pr.tools.memory import (
from pr.tools.patch import apply_patch, create_diff
from pr.tools.python_exec import python_exec
from pr.tools.web import http_fetch, web_search, web_search_news
from pr.tools.context_modifier import (
modify_context_add,
modify_context_replace,
modify_context_delete,
)
__all__ = [
"add_knowledge_entry",
+34 -9
View File
@@ -1,3 +1,4 @@
import asyncio
import os
from typing import Any, Dict, List
@@ -16,15 +17,39 @@ def _create_api_wrapper():
tools_definition = get_tools_definition() if use_tools else []
def api_wrapper(messages, temperature=None, max_tokens=None, **kwargs):
return call_api(
messages=messages,
model=model,
api_url=api_url,
api_key=api_key,
use_tools=use_tools,
tools_definition=tools_definition,
verbose=False,
)
try:
# Try to get the current event loop
loop = asyncio.get_running_loop()
# If we're in an event loop, use run_coroutine_threadsafe
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
future = asyncio.run_coroutine_threadsafe(
call_api(
messages=messages,
model=model,
api_url=api_url,
api_key=api_key,
use_tools=use_tools,
tools_definition=tools_definition,
verbose=False,
),
loop,
)
return future.result()
except RuntimeError:
# No event loop running, use asyncio.run
return asyncio.run(
call_api(
messages=messages,
model=model,
api_url=api_url,
api_key=api_key,
use_tools=use_tools,
tools_definition=tools_definition,
verbose=False,
)
)
return api_wrapper
+14 -7
View File
@@ -1,18 +1,21 @@
import os
from typing import Optional
CONTEXT_FILE = '/home/retoor/.local/share/rp/.rcontext.txt'
CONTEXT_FILE = "/home/retoor/.local/share/rp/.rcontext.txt"
def _read_context() -> str:
if not os.path.exists(CONTEXT_FILE):
raise FileNotFoundError(f"Context file {CONTEXT_FILE} not found.")
with open(CONTEXT_FILE, 'r') as f:
with open(CONTEXT_FILE, "r") as f:
return f.read()
def _write_context(content: str):
with open(CONTEXT_FILE, 'w') as f:
with open(CONTEXT_FILE, "w") as f:
f.write(content)
def modify_context_add(new_content: str, position: Optional[str] = None) -> str:
"""
Add new content to the .rcontext.txt file.
@@ -25,13 +28,14 @@ def modify_context_add(new_content: str, position: Optional[str] = None) -> str:
if position and position in current:
# Insert before the position
parts = current.split(position, 1)
updated = parts[0] + new_content + '\n\n' + position + parts[1]
updated = parts[0] + new_content + "\n\n" + position + parts[1]
else:
# Append at the end
updated = current + '\n\n' + new_content
updated = current + "\n\n" + new_content
_write_context(updated)
return f"Added: {new_content[:100]}... (full addition applied). Consequences: Enhances functionality as requested."
def modify_context_replace(old_content: str, new_content: str) -> str:
"""
Replace old content with new content in .rcontext.txt.
@@ -47,6 +51,7 @@ def modify_context_replace(old_content: str, new_content: str) -> str:
_write_context(updated)
return f"Replaced: '{old_content[:50]}...' with '{new_content[:50]}...'. Consequences: Changes behavior as specified; verify for unintended effects."
def modify_context_delete(content_to_delete: str, confirmed: bool = False) -> str:
"""
Delete content from .rcontext.txt, but only if confirmed.
@@ -56,10 +61,12 @@ def modify_context_delete(content_to_delete: str, confirmed: bool = False) -> st
confirmed: Must be True to proceed with deletion.
"""
if not confirmed:
raise PermissionError(f"Deletion not confirmed. To delete '{content_to_delete[:50]}...', you must explicitly confirm. Are you sure? This may affect system behavior permanently.")
raise PermissionError(
f"Deletion not confirmed. To delete '{content_to_delete[:50]}...', you must explicitly confirm. Are you sure? This may affect system behavior permanently."
)
current = _read_context()
if content_to_delete not in current:
raise ValueError(f"Content to delete not found: {content_to_delete[:50]}...")
updated = current.replace(content_to_delete, '', 1)
updated = current.replace(content_to_delete, "", 1)
_write_context(updated)
return f"Deleted: '{content_to_delete[:50]}...'. Consequences: Removed specified content; system may lose referenced rules or guidelines."