mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-08-22 07:03:33 +00:00
update
This commit is contained in:
parent
c0dbb5f35d
commit
8ab8e6d591
3 changed files with 49 additions and 2 deletions
|
|
@ -5,10 +5,36 @@ from fastapi import APIRouter, FastAPI
|
|||
from dotenv import load_dotenv
|
||||
import importlib
|
||||
from typing import Any, overload
|
||||
import threading
|
||||
|
||||
# Thread-local storage for user-specific environment
|
||||
_thread_local = threading.local()
|
||||
|
||||
# Default global environment path
|
||||
default_env_path = os.path.join(os.path.expanduser("~"), ".eigent", ".env")
|
||||
load_dotenv(dotenv_path=default_env_path)
|
||||
|
||||
|
||||
env_path = os.path.join(os.path.expanduser("~"), ".eigent", ".env")
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
def set_user_env_path(env_path: str | None = None):
|
||||
"""
|
||||
Set user-specific environment path for current thread.
|
||||
If env_path is None, uses default global environment.
|
||||
"""
|
||||
if env_path and os.path.exists(env_path):
|
||||
_thread_local.env_path = env_path
|
||||
# Load user-specific environment variables
|
||||
load_dotenv(dotenv_path=env_path, override=True)
|
||||
else:
|
||||
# Clear thread-local env_path to fall back to global
|
||||
if hasattr(_thread_local, 'env_path'):
|
||||
delattr(_thread_local, 'env_path')
|
||||
|
||||
|
||||
def get_current_env_path() -> str:
|
||||
"""
|
||||
Get current environment path (either user-specific or default).
|
||||
"""
|
||||
return getattr(_thread_local, 'env_path', default_env_path)
|
||||
|
||||
|
||||
@overload
|
||||
|
|
@ -24,6 +50,20 @@ def env(key: str, default: Any) -> Any: ...
|
|||
|
||||
|
||||
def env(key: str, default=None):
|
||||
"""
|
||||
Get environment variable.
|
||||
First checks thread-local user-specific environment,
|
||||
then falls back to global environment.
|
||||
"""
|
||||
# If we have a user-specific environment path, try to reload it to get latest values
|
||||
if hasattr(_thread_local, 'env_path') and os.path.exists(_thread_local.env_path):
|
||||
# Temporarily load user-specific env to get the latest value
|
||||
from dotenv import dotenv_values
|
||||
user_env_values = dotenv_values(_thread_local.env_path)
|
||||
if key in user_env_values:
|
||||
return user_env_values[key] or default
|
||||
|
||||
# Fall back to global environment
|
||||
return os.getenv(key, default)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ from app.service.task import (
|
|||
create_task_lock,
|
||||
get_task_lock,
|
||||
)
|
||||
from app.component.environment import set_user_env_path
|
||||
|
||||
|
||||
router = APIRouter(tags=["chat"])
|
||||
|
|
@ -33,6 +34,9 @@ chat_logger = traceroot.get_logger('chat_controller')
|
|||
async def post(data: Chat, request: Request):
|
||||
chat_logger.info(f"Starting new chat session for task_id: {data.task_id}, user: {data.email}")
|
||||
task_lock = create_task_lock(data.task_id)
|
||||
|
||||
# Set user-specific environment path for this thread
|
||||
set_user_env_path(data.env_path)
|
||||
load_dotenv(dotenv_path=data.env_path)
|
||||
|
||||
# logger.debug(f"start chat: {data.model_dump_json()}")
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ from app.service.task import (
|
|||
task_locks,
|
||||
)
|
||||
import asyncio
|
||||
from app.component.environment import set_user_env_path
|
||||
|
||||
|
||||
router = APIRouter(tags=["task"])
|
||||
|
|
@ -49,6 +50,8 @@ def take_control(id: str, data: TakeControl):
|
|||
|
||||
@router.post("/task/{id}/add-agent", name="add new agent")
|
||||
def add_agent(id: str, data: NewAgent):
|
||||
# Set user-specific environment path for this thread
|
||||
set_user_env_path(data.env_path)
|
||||
load_dotenv(dotenv_path=data.env_path)
|
||||
asyncio.run(get_task_lock(id).put_queue(ActionNewAgent(**data.model_dump())))
|
||||
return Response(status_code=204)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue