Skip to content

Agent Zero AI framework

projects continued

frdel0e1aa85890c4
diff --git a/agent.py b/agent.py
index c1478cd95..594dc37bc 100644
--- a/agent.py
+++ b/agent.py
@@ -11,7 +11,7 @@ from enum import Enum
import uuid
import models
-from python.helpers import extract_tools, files, errors, history, tokens
+from python.helpers import extract_tools, files, errors, history, tokens, context as context_helper
from python.helpers import dirty_json
from python.helpers.print_style import PrintStyle
@@ -55,12 +55,22 @@ class AgentContext:
last_message: datetime | None = None,
data: dict | None = None,
output_data: dict | None = None,
+ set_current: bool = False,
):
- # build context
+ # initialize context
self.id = id or AgentContext.generate_id()
+ existing = self._contexts.get(self.id, None)
+ if existing:
+ AgentContext.remove(self.id)
+ self._contexts[self.id] = self
+ if set_current:
+ AgentContext.set_current(self.id)
+
+ # initialize state
self.name = name
self.config = config
self.log = log or Log.Log()
+ self.log.context = self
self.agent0 = agent0 or Agent(0, self.config, self)
self.paused = paused
self.streaming_agent = streaming_agent
@@ -73,15 +83,32 @@ class AgentContext:
self.data = data or {}
self.output_data = output_data or {}
- existing = self._contexts.get(self.id, None)
- if existing:
- AgentContext.remove(self.id)
- self._contexts[self.id] = self
+
@staticmethod
def get(id: str):
return AgentContext._contexts.get(id, None)
+ @staticmethod
+ def use(id: str):
+ context = AgentContext.get(id)
+ if context:
+ AgentContext.set_current(id)
+ else:
+ AgentContext.set_current("")
+ return context
+
+ @staticmethod
+ def current():
+ ctxid = context_helper.get_context_data("agent_context_id","")
+ if not ctxid:
+ return None
+ return AgentContext.get(ctxid)
+
+ @staticmethod
+ def set_current(ctxid: str):
+ context_helper.set_context_data("agent_context_id", ctxid)
+
@staticmethod
def first():
if not AgentContext._contexts:
@@ -123,6 +150,10 @@ class AgentContext:
# recursive is not used now, prepared for context hierarchy
self.data[key] = value
+ def get_output_data(self, key: str, recursive: bool = True):
+ # recursive is not used now, prepared for context hierarchy
+ return self.output_data.get(key, None)
+
def set_output_data(self, key: str, value: Any, recursive: bool = True):
# recursive is not used now, prepared for context hierarchy
self.output_data[key] = value
@@ -688,7 +719,7 @@ class Agent:
response, _reasoning = await call_data["model"].unified_call(
system_message=call_data["system"],
user_message=call_data["message"],
- response_callback=stream_callback,
+ response_callback=stream_callback if call_data["callback"] else None,
rate_limiter_callback=self.rate_limiter_callback if not call_data["background"] else None,
)
diff --git a/models.py b/models.py
index 12c36afb4..469925e49 100644
--- a/models.py
+++ b/models.py
@@ -487,6 +487,7 @@ class LiteLLMChatWrapper(SimpleChatModel):
call_kwargs: dict[str, Any] = {**self.kwargs, **kwargs}
max_retries: int = int(call_kwargs.pop("a0_retry_attempts", 2))
retry_delay_s: float = float(call_kwargs.pop("a0_retry_delay_seconds", 1.5))
+ stream = reasoning_callback is not None or response_callback is not None or tokens_callback is not None
# results
result = ChatGenerationResult()
@@ -499,41 +500,52 @@ class LiteLLMChatWrapper(SimpleChatModel):
_completion = await acompletion(
model=self.model_name,
messages=msgs_conv,
- stream=True,
+ stream=stream,
**call_kwargs,
)
- # iterate over chunks
- async for chunk in _completion: # type: ignore
- got_any_chunk = True
- # parse chunk
- parsed = _parse_chunk(chunk)
+ if stream:
+ # iterate over chunks
+ async for chunk in _completion: # type: ignore
+ got_any_chunk = True
+ # parse chunk
+ parsed = _parse_chunk(chunk)
+ output = result.add_chunk(parsed)
+
+ # collect reasoning delta and call callbacks
+ if output["reasoning_delta"]:
+ if reasoning_callback:
+ await reasoning_callback(output["reasoning_delta"], result.reasoning)
+ if tokens_callback:
+ await tokens_callback(
+ output["reasoning_delta"],
+ approximate_tokens(output["reasoning_delta"]),
+ )
+ # Add output tokens to rate limiter if configured
+ if limiter:
+ limiter.add(output=approximate_tokens(output["reasoning_delta"]))
+ # collect response delta and call callbacks
+ if output["response_delta"]:
+ if response_callback:
+ await response_callback(output["response_delta"], result.response)
+ if tokens_callback:
+ await tokens_callback(
+ output["response_delta"],
+ approximate_tokens(output["response_delta"]),
+ )
+ # Add output tokens to rate limiter if configured
+ if limiter:
+ limiter.add(output=approximate_tokens(output["response_delta"]))
+
+ # non-stream response
+ else:
+ parsed = _parse_chunk(_completion)
output = result.add_chunk(parsed)
-
- # collect reasoning delta and call callbacks
- if output["reasoning_delta"]:
- if reasoning_callback:
- await reasoning_callback(output["reasoning_delta"], result.reasoning)
- if tokens_callback:
- await tokens_callback(
- output["reasoning_delta"],
- approximate_tokens(output["reasoning_delta"]),
- )
- # Add output tokens to rate limiter if configured
- if limiter:
- limiter.add(output=approximate_tokens(output["reasoning_delta"]))
- # collect response delta and call callbacks
- if output["response_delta"]:
- if response_callback:
- await response_callback(output["response_delta"], result.response)
- if tokens_callback:
- await tokens_callback(
- output["response_delta"],
- approximate_tokens(output["response_delta"]),
- )
- # Add output tokens to rate limiter if configured
- if limiter:
+ if limiter:
+ if output["response_delta"]:
limiter.add(output=approximate_tokens(output["response_delta"]))
+ if output["reasoning_delta"]:
+ limiter.add(output=approximate_tokens(output["reasoning_delta"]))
# Successful completion of stream
return result.response, result.reasoning
@@ -804,6 +816,10 @@ def _parse_chunk(chunk: Any) -> ChatChunk:
delta.get("reasoning_content", "")
if isinstance(delta, dict)
else getattr(delta, "reasoning_content", "")
+ ) or (
+ message.get("reasoning_content", "")
+ if isinstance(message, dict)
+ else getattr(message, "reasoning_content", "")
)
return ChatChunk(reasoning_delta=reasoning_delta, response_delta=response_delta)
diff --git a/python/api/api_log_get.py b/python/api/api_log_get.py
index c09fdfdc0..8111dbea5 100644
--- a/python/api/api_log_get.py
+++ b/python/api/api_log_get.py
@@ -32,7 +32,7 @@ class ApiLogGet(ApiHandler):
return Response('{"error": "context_id is required"}', status=400, mimetype="application/json")
# Get context
- context = AgentContext.get(context_id)
+ context = AgentContext.use(context_id)
if not context:
return Response('{"error": "Context not found"}', status=404, mimetype="application/json")
diff --git a/python/api/api_message.py b/python/api/api_message.py
index 4d3248c60..385d556dd 100644
--- a/python/api/api_message.py
+++ b/python/api/api_message.py
@@ -68,12 +68,13 @@ class ApiMessage(ApiHandler):
# Get or create context
if context_id:
- context = AgentContext.get(context_id)
+ context = AgentContext.use(context_id)
if not context:
return Response('{"error": "Context not found"}', status=404, mimetype="application/json")
else:
config = initialize_agent()
context = AgentContext(config=config, type=AgentContextType.USER)
+ AgentContext.use(context.id)
context_id = context.id
# Update chat lifetime
diff --git a/python/api/api_reset_chat.py b/python/api/api_reset_chat.py
index b497adb1e..bf0a10f8a 100644
--- a/python/api/api_reset_chat.py
+++ b/python/api/api_reset_chat.py
@@ -35,7 +35,7 @@ class ApiResetChat(ApiHandler):
)
# Check if context exists
- context = AgentContext.get(context_id)
+ context = AgentContext.use(context_id)
if not context:
return Response(
'{"error": "Chat context not found"}',
diff --git a/python/api/api_terminate_chat.py b/python/api/api_terminate_chat.py
index 3d6bcb4b4..e746d84c5 100644
--- a/python/api/api_terminate_chat.py
+++ b/python/api/api_terminate_chat.py
@@ -35,7 +35,7 @@ class ApiTerminateChat(ApiHandler):
)
# Check if context exists
- context = AgentContext.get(context_id)
+ context = AgentContext.use(context_id)
if not context:
return Response(
'{"error": "Chat context not found"}',
diff --git a/python/api/chat_create.py b/python/api/chat_create.py
new file mode 100644
index 000000000..f73f3416d
--- /dev/null
+++ b/python/api/chat_create.py
@@ -0,0 +1,32 @@
+from python.helpers.api import ApiHandler, Input, Output, Request, Response
+
+
+from python.helpers import projects, guids
+from agent import AgentContext
+
+
+class CreateChat(ApiHandler):
+ async def process(self, input: Input, request: Request) -> Output:
+ current_ctxid = input.get("current_context", "") # current context id
+ new_ctxid = input.get("new_context", guids.generate_id()) # given or new guid
+
+ # context instance - get or create
+ current_context = AgentContext.get(current_ctxid)
+
+ # get/create new context
+ new_context = self.use_context(new_ctxid)
+
+ # copy selected data from current to new context
+ if current_context:
+ current_data_1 = current_context.get_data(projects.CONTEXT_DATA_KEY_PROJECT)
+ if current_data_1:
+ new_context.set_data(projects.CONTEXT_DATA_KEY_PROJECT, current_data_1)
+ current_data_2 = current_context.get_output_data(projects.CONTEXT_DATA_KEY_PROJECT)
+ if current_data_2:
+ new_context.set_output_data(projects.CONTEXT_DATA_KEY_PROJECT, current_data_2)
+
+ return {
+ "ok": True,
+ "ctxid": new_context.id,
+ "message": "Context created.",
+ }
diff --git a/python/api/chat_export.py b/python/api/chat_export.py
index 2817a4ea8..a82be6483 100644
--- a/python/api/chat_export.py
+++ b/python/api/chat_export.py
@@ -8,7 +8,7 @@ class ExportChat(ApiHandler):
if not ctxid:
raise Exception("No context id provided")
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
content = persist_chat.export_json_chat(context)
return {
"message": "Chats exported.",
diff --git a/python/api/chat_remove.py b/python/api/chat_remove.py
index a0b186e14..c7b75f02a 100644
--- a/python/api/chat_remove.py
+++ b/python/api/chat_remove.py
@@ -8,7 +8,7 @@ class RemoveChat(ApiHandler):
async def process(self, input: Input, request: Request) -> Output:
ctxid = input.get("context", "")
- context = AgentContext.get(ctxid)
+ context = AgentContext.use(ctxid)
if context:
# stop processing any tasks
context.reset()
diff --git a/python/api/chat_reset.py b/python/api/chat_reset.py
index 5086aacc1..a5ce1284e 100644
--- a/python/api/chat_reset.py
+++ b/python/api/chat_reset.py
@@ -9,7 +9,7 @@ class Reset(ApiHandler):
ctxid = input.get("context", "")
# context instance - get or create
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
context.reset()
persist_chat.save_tmp_chat(context)
persist_chat.remove_msg_files(ctxid)
diff --git a/python/api/ctx_window_get.py b/python/api/ctx_window_get.py
index 16a4438b7..46573cb60 100644
--- a/python/api/ctx_window_get.py
+++ b/python/api/ctx_window_get.py
@@ -6,7 +6,7 @@ from python.helpers import tokens
class GetCtxWindow(ApiHandler):
async def process(self, input: Input, request: Request) -> Output:
ctxid = input.get("context", [])
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
agent = context.streaming_agent or context.agent0
window = agent.get_data(agent.DATA_NAME_CTX_WINDOW)
if not window or not isinstance(window, dict):
diff --git a/python/api/get_work_dir_files.py b/python/api/get_work_dir_files.py
index 1783e3d8a..13cd428d4 100644
--- a/python/api/get_work_dir_files.py
+++ b/python/api/get_work_dir_files.py
@@ -1,6 +1,6 @@
from python.helpers.api import ApiHandler, Request, Response
from python.helpers.file_browser import FileBrowser
-from python.helpers import runtime
+from python.helpers import runtime, files
class GetWorkDirFiles(ApiHandler):
@@ -15,7 +15,7 @@ class GetWorkDirFiles(ApiHandler):
# current_path = "work_dir"
# else:
# current_path = "root"
- current_path = "root"
+ current_path = "/a0"
# browser = FileBrowser()
# result = browser.get_files(current_path)
diff --git a/python/api/history_get.py b/python/api/history_get.py
index 7ff40e3fe..608a523ec 100644
--- a/python/api/history_get.py
+++ b/python/api/history_get.py
@@ -4,7 +4,7 @@ from python.helpers.api import ApiHandler, Request, Response
class GetHistory(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
ctxid = input.get("context", [])
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
agent = context.streaming_agent or context.agent0
history = agent.history.output_text()
size = agent.history.get_tokens()
diff --git a/python/api/import_knowledge.py b/python/api/import_knowledge.py
index fd1b26542..bfc25b649 100644
--- a/python/api/import_knowledge.py
+++ b/python/api/import_knowledge.py
@@ -13,7 +13,7 @@ class ImportKnowledge(ApiHandler):
if not ctxid:
raise Exception("No context id provided")
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
file_list = request.files.getlist("files[]")
KNOWLEDGE_FOLDER = files.get_abs_path(memory.get_custom_knowledge_subdir_abs(context.agent0), "main")
diff --git a/python/api/memory_dashboard.py b/python/api/memory_dashboard.py
index a494227c8..d1275fe41 100644
--- a/python/api/memory_dashboard.py
+++ b/python/api/memory_dashboard.py
@@ -1,8 +1,9 @@
from python.helpers.api import ApiHandler, Request, Response
-from python.helpers.memory import Memory
+from python.helpers.memory import Memory, get_existing_memory_subdirs, get_context_memory_subdir
from python.helpers import files
from models import ModelConfig, ModelType
from langchain_core.documents import Document
+from agent import AgentContext
class MemoryDashboard(ApiHandler):
@@ -113,21 +114,13 @@ class MemoryDashboard(ApiHandler):
# Fallback to default if no context available
return {"success": True, "memory_subdir": "default"}
- # Import AgentContext here to avoid circular imports
- from agent import AgentContext
-
- # Get the context and extract memory subdirectory
- context = AgentContext.get(context_id)
- if (
- context
- and hasattr(context, "config")
- and hasattr(context.config, "memory_subdir")
- ):
- memory_subdir = context.config.memory_subdir or "default"
- return {"success": True, "memory_subdir": memory_subdir}
- else:
+ context = AgentContext.use(context_id)
+ if not context:
return {"success": True, "memory_subdir": "default"}
+ memory_subdir = get_context_memory_subdir(context)
+ return {"success": True, "memory_subdir": memory_subdir or "default"}
+
except Exception:
return {
"success": True, # Still success, just fallback to default
@@ -138,12 +131,7 @@ class MemoryDashboard(ApiHandler):
"""Get available memory subdirectories."""
try:
# Get subdirectories from memory folder
- subdirs = files.get_subdirectories("memory", exclude="embeddings")
-
- # Ensure 'default' is always available
- if "default" not in subdirs:
- subdirs.insert(0, "default")
-
+ subdirs = get_existing_memory_subdirs()
return {"success": True, "subdirs": subdirs}
except Exception as e:
return {
diff --git a/python/api/message.py b/python/api/message.py
index 2f88f4784..d3c18d47c 100644
--- a/python/api/message.py
+++ b/python/api/message.py
@@ -53,7 +53,7 @@ class Message(ApiHandler):
message = text
# Obtain agent context
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
# Store attachments in agent data
# context.agent0.set_data("attachments", attachment_paths)
diff --git a/python/api/nudge.py b/python/api/nudge.py
index 64683da07..558734cdf 100644
--- a/python/api/nudge.py
+++ b/python/api/nudge.py
@@ -6,7 +6,7 @@ class Nudge(ApiHandler):
if not ctxid:
raise Exception("No context id provided")
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
context.nudge()
msg = "Process reset, agent nudged."
diff --git a/python/api/pause.py b/python/api/pause.py
index e4b20ecfb..47b444e80 100644
--- a/python/api/pause.py
+++ b/python/api/pause.py
@@ -8,7 +8,7 @@ class Pause(ApiHandler):
ctxid = input.get("context", "")
# context instance - get or create
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid)
context.paused = paused
diff --git a/python/api/poll.py b/python/api/poll.py
index a17987f13..eb021c2f2 100644
--- a/python/api/poll.py
+++ b/python/api/poll.py
@@ -20,7 +20,7 @@ class Poll(ApiHandler):
# context instance - get or create only if ctxid is provided
if ctxid:
- context = self.get_context(ctxid)
+ context = self.use_context(ctxid, create_if_not_exists=False)
else:
context = None
@@ -106,6 +106,7 @@ class Poll(ApiHandler):
# data from this server
return {
+ "deselect_chat": ctxid and not context,
"context": context.id if context else "",
"contexts": ctxs,
"tasks": tasks,
diff --git a/python/api/projects.py b/python/api/projects.py
index a214c0567..6d96a0e76 100644
--- a/python/api/projects.py
+++ b/python/api/projects.py
@@ -5,24 +5,26 @@ from python.helpers import projects
class Projects(ApiHandler):
async def process(self, input: Input, request: Request) -> Output:
action = input.get("action", "")
+ ctxid = input.get("context_id", None)
+
+ if ctxid:
+ _context = self.use_context(ctxid)
try:
- if action == "list-active":
+ if action == "list":
data = self.get_active_projects_list()
- elif action == "list-archive":
- data = self.get_archived_projects_list()
elif action == "load":
- data = self.load_project(input.get("path", None))
+ data = self.load_project(input.get("name", None))
elif action == "create":
data = self.create_project(input.get("project", None))
elif action == "update":
data = self.update_project(input.get("project", None))
elif action == "delete":
- data = self.delete_project(input.get("path", None))
+ data = self.delete_project(input.get("name", None))
elif action == "activate":
- data = self.activate_project(input.get("context_id", None), input.get("path", None))
+ data = self.activate_project(ctxid, input.get("name", None))
elif action == "deactivate":
- data = self.deactivate_project(input.get("context_id", None))
+ data = self.deactivate_project(ctxid)
else:
raise Exception("Invalid action")
@@ -39,41 +41,38 @@ class Projects(ApiHandler):
def get_active_projects_list(self):
return projects.get_active_projects_list()
- def get_archived_projects_list(self):
- return projects.get_archived_projects_list()
-
def create_project(self, project: dict|None):
if project is None:
raise Exception("Project data is required")
data = projects.BasicProjectData(**project)
- path = projects.create_project(project["name"], data)
- return projects.load_edit_project_data(path)
+ name = projects.create_project(project["name"], data)
+ return projects.load_edit_project_data(name)
- def load_project(self, path: str|None):
- if path is None:
- raise Exception("Project path is required")
- return projects.load_edit_project_data(path)
+ def load_project(self, name: str|None):
+ if name is None:
+ raise Exception("Project name is required")
+ return projects.load_edit_project_data(name)
def update_project(self, project: dict|None):
if project is None:
raise Exception("Project data is required")
- data = projects.BasicProjectData(**project)
- path = projects.update_project(project["path"], data)
- return projects.load_edit_project_data(path)
+ data = projects.EditProjectData(**project)
+ name = projects.update_project(project["name"], data)
+ return projects.load_edit_project_data(name)
- def delete_project(self, path: str|None):
- if path is None:
- raise Exception("Project path is required")
- return projects.delete_project(path)
+ def delete_project(self, name: str|None):
+ if name is None:
+ raise Exception("Project name is required")
+ return projects.delete_project(name)
- def activate_project(self, context_id: str|None, path: str|None):
- if context_id is None:
+ def activate_project(self, context_id: str|None, name: str|None):
+ if not context_id:
raise Exception("Context ID is required")
- if path is None:
- raise Exception("Project path is required")
- return projects.activate_project(context_id, path)
+ if not name:
+ raise Exception("Project name is required")
+ return projects.activate_project(context_id, name)
def deactivate_project(self, context_id: str|None):
- if context_id is None:
+ if not context_id:
raise Exception("Context ID is required")
- return projects.deactivate_project(context_id)
+ return projects.deactivate_project(context_id)
\ No newline at end of file
diff --git a/python/api/scheduler_task_delete.py b/python/api/scheduler_task_delete.py
index 59e718799..f9b52ac03 100644
--- a/python/api/scheduler_task_delete.py
+++ b/python/api/scheduler_task_delete.py
@@ -30,7 +30,7 @@ class SchedulerTaskDelete(ApiHandler):
context = None
if task.context_id:
- context = self.get_context(task.context_id)
+ context = self.use_context(task.context_id)
# If the task is running, update its state to IDLE first
if task.state == TaskState.RUNNING:
diff --git a/python/api/synthesize.py b/python/api/synthesize.py
index 071af8760..87f93eeaa 100644
--- a/python/api/synthesize.py
+++ b/python/api/synthesize.py
@@ -7,9 +7,11 @@ from python.helpers import runtime, settings, kokoro_tts
class Synthesize(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
text = input.get("text", "")
- # ctxid = input.get("ctxid", "")
+ ctxid = input.get("ctxid", "")
- # context = self.get_context(ctxid)
+ if ctxid:
+ context = self.use_context(ctxid)
+
# if not await kokoro_tts.is_downloaded():
# context.log.log(type="info", content="Kokoro TTS model is currently being initialized, please wait...")
diff --git a/python/api/transcribe.py b/python/api/transcribe.py
index 8276a70a6..5abb9a083 100644
--- a/python/api/transcribe.py
+++ b/python/api/transcribe.py
@@ -5,9 +5,11 @@ from python.helpers import runtime, settings, whisper
class Transcribe(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
audio = input.get("audio")
- # ctxid = input.get("ctxid", "")
+ ctxid = input.get("ctxid", "")
+
+ if ctxid:
+ context = self.use_context(ctxid)
- # context = self.get_context(ctxid)
# if not await whisper.is_downloaded():
# context.log.log(type="info", content="Whisper STT model is currently being initialized, please wait...")
diff --git a/python/extensions/error_format/_10_mask_errors.py b/python/extensions/error_format/_10_mask_errors.py
index e685fdbbc..f90cf7773 100644
--- a/python/extensions/error_format/_10_mask_errors.py
+++ b/python/extensions/error_format/_10_mask_errors.py
@@ -1,5 +1,5 @@
from python.helpers.extension import Extension
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_secrets_manager
class MaskErrorSecrets(Extension):
@@ -10,7 +10,7 @@ class MaskErrorSecrets(Extension):
if not msg:
return
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# Mask the error message
if "message" in msg:
diff --git a/python/extensions/hist_add_before/_10_mask_content.py b/python/extensions/hist_add_before/_10_mask_content.py
index 279c81b14..a59006e62 100644
--- a/python/extensions/hist_add_before/_10_mask_content.py
+++ b/python/extensions/hist_add_before/_10_mask_content.py
@@ -1,4 +1,5 @@
from python.helpers.extension import Extension
+from python.helpers.secrets import get_secrets_manager
class MaskHistoryContent(Extension):
@@ -10,8 +11,7 @@ class MaskHistoryContent(Extension):
return
try:
- from python.helpers.secrets import SecretsManager
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# Mask the content before adding to history
content_data["content"] = self._mask_content(content_data["content"], secrets_mgr)
diff --git a/python/extensions/reasoning_stream_chunk/_10_mask_stream.py b/python/extensions/reasoning_stream_chunk/_10_mask_stream.py
index aef15624a..07459e64c 100644
--- a/python/extensions/reasoning_stream_chunk/_10_mask_stream.py
+++ b/python/extensions/reasoning_stream_chunk/_10_mask_stream.py
@@ -1,4 +1,5 @@
from python.helpers.extension import Extension
+from python.helpers.secrets import get_secrets_manager
class MaskReasoningStreamChunk(Extension):
@@ -10,8 +11,7 @@ class MaskReasoningStreamChunk(Extension):
return
try:
- from python.helpers.secrets import SecretsManager
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# Initialize filter if not exists
filter_key = "_reason_stream_filter"
diff --git a/python/extensions/response_stream_chunk/_10_mask_stream.py b/python/extensions/response_stream_chunk/_10_mask_stream.py
index fb52c174a..fe7eb3dd7 100644
--- a/python/extensions/response_stream_chunk/_10_mask_stream.py
+++ b/python/extensions/response_stream_chunk/_10_mask_stream.py
@@ -1,6 +1,6 @@
from python.helpers.extension import Extension
-from python.helpers.secrets import SecretsManager
from agent import Agent, LoopData
+from python.helpers.secrets import get_secrets_manager
class MaskResponseStreamChunk(Extension):
@@ -13,8 +13,7 @@ class MaskResponseStreamChunk(Extension):
return
try:
- from python.helpers.secrets import SecretsManager
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# Initialize filter if not exists
filter_key = "_resp_stream_filter"
diff --git a/python/extensions/system_prompt/_10_system_prompt.py b/python/extensions/system_prompt/_10_system_prompt.py
index b4af2cfbd..9a17c0ad9 100644
--- a/python/extensions/system_prompt/_10_system_prompt.py
+++ b/python/extensions/system_prompt/_10_system_prompt.py
@@ -58,9 +58,9 @@ def get_mcp_tools_prompt(agent: Agent):
def get_secrets_prompt(agent: Agent):
try:
# Use lazy import to avoid circular dependencies
- from python.helpers.secrets import SecretsManager
+ from python.helpers.secrets import get_secrets_manager
- secrets_manager = SecretsManager.get_instance()
+ secrets_manager = get_secrets_manager(agent.context)
secrets = secrets_manager.get_secrets_for_prompt()
vars = get_settings()["variables"]
return agent.read_prompt("agent.system.secrets.md", secrets=secrets, vars=vars)
@@ -71,9 +71,9 @@ def get_secrets_prompt(agent: Agent):
def get_project_prompt(agent: Agent):
result = agent.read_prompt("agent.system.projects.main.md")
- project_path = agent.context.get_data(projects.CONTEXT_DATA_KEY_PROJECT_PATH)
- if project_path:
- project_vars = projects.build_system_prompt_vars(project_path)
+ project_name = agent.context.get_data(projects.CONTEXT_DATA_KEY_PROJECT)
+ if project_name:
+ project_vars = projects.build_system_prompt_vars(project_name)
result += "\n\n" + agent.read_prompt(
"agent.system.projects.active.md", **project_vars
)
diff --git a/python/extensions/system_prompt/_20_behaviour_prompt.py b/python/extensions/system_prompt/_20_behaviour_prompt.py
index 5c451eba7..1650e9ee8 100644
--- a/python/extensions/system_prompt/_20_behaviour_prompt.py
+++ b/python/extensions/system_prompt/_20_behaviour_prompt.py
@@ -11,7 +11,7 @@ class BehaviourPrompt(Extension):
system_prompt.insert(0, prompt) #.append(prompt)
def get_custom_rules_file(agent: Agent):
- return memory.get_memory_subdir_abs(agent) + f"/behaviour.md"
+ return files.get_abs_path(memory.get_memory_subdir_abs(agent), "behaviour.md")
def read_rules(agent: Agent):
rules_file = get_custom_rules_file(agent)
diff --git a/python/extensions/tool_execute_after/_10_mask_secrets.py b/python/extensions/tool_execute_after/_10_mask_secrets.py
index 1055364c4..ae2cdc4ef 100644
--- a/python/extensions/tool_execute_after/_10_mask_secrets.py
+++ b/python/extensions/tool_execute_after/_10_mask_secrets.py
@@ -1,5 +1,5 @@
from python.helpers.extension import Extension
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_secrets_manager
from python.helpers.tool import Response
@@ -8,5 +8,5 @@ class MaskToolSecrets(Extension):
async def execute(self, response: Response | None = None, **kwargs):
if not response:
return
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
response.message = secrets_mgr.mask_values(response.message)
diff --git a/python/extensions/tool_execute_before/_10_unmask_secrets.py b/python/extensions/tool_execute_before/_10_unmask_secrets.py
index 347d67d1b..902581229 100644
--- a/python/extensions/tool_execute_before/_10_unmask_secrets.py
+++ b/python/extensions/tool_execute_before/_10_unmask_secrets.py
@@ -1,5 +1,5 @@
from python.helpers.extension import Extension
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_secrets_manager
class UnmaskToolSecrets(Extension):
@@ -10,7 +10,7 @@ class UnmaskToolSecrets(Extension):
if not tool_args:
return
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# Unmask placeholders in args for actual tool execution
for k, v in tool_args.items():
diff --git a/python/extensions/util_model_call_before/_10_mask_secrets.py b/python/extensions/util_model_call_before/_10_mask_secrets.py
index beeffe950..df23ff1f6 100644
--- a/python/extensions/util_model_call_before/_10_mask_secrets.py
+++ b/python/extensions/util_model_call_before/_10_mask_secrets.py
@@ -1,5 +1,5 @@
from python.helpers.extension import Extension
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_secrets_manager
class MaskToolSecrets(Extension):
@@ -8,7 +8,7 @@ class MaskToolSecrets(Extension):
# model call data
call_data:dict = kwargs.get("call_data", {})
- secrets_mgr = SecretsManager.get_instance()
+ secrets_mgr = get_secrets_manager(self.agent.context)
# mask system and user message
if system:=call_data.get("system"):
diff --git a/python/helpers/api.py b/python/helpers/api.py
index 4af0693b7..6a40e1347 100644
--- a/python/helpers/api.py
+++ b/python/helpers/api.py
@@ -80,14 +80,18 @@ class ApiHandler:
return Response(response=error, status=500, mimetype="text/plain")
# get context to run agent zero in
- def get_context(self, ctxid: str):
+ def use_context(self, ctxid: str, create_if_not_exists: bool = True):
with self.thread_lock:
if not ctxid:
first = AgentContext.first()
if first:
+ AgentContext.use(first.id)
return first
- return AgentContext(config=initialize_agent())
- got = AgentContext.get(ctxid)
+ context = AgentContext(config=initialize_agent(), set_current=True)
+ return context
+ got = AgentContext.use(ctxid)
if got:
return got
- return AgentContext(config=initialize_agent(), id=ctxid)
+ if create_if_not_exists:
+ context = AgentContext(config=initialize_agent(), id=ctxid, set_current=True)
+ return context
diff --git a/python/helpers/context.py b/python/helpers/context.py
new file mode 100644
index 000000000..2dc5609f0
--- /dev/null
+++ b/python/helpers/context.py
@@ -0,0 +1,46 @@
+from contextvars import ContextVar
+from typing import Any, TypeVar, cast, Optional, Dict
+
+T = TypeVar("T")
+
+# no mutable default — None is safe
+_context_data: ContextVar[Optional[Dict[str, Any]]] = ContextVar("_context_data", default=None)
+
+
+def _ensure_context() -> Dict[str, Any]:
+ """Make sure a context dict exists, and return it."""
+ data = _context_data.get()
+ if data is None:
+ data = {}
+ _context_data.set(data)
+ return data
+
+
+def set_context_data(key: str, value: Any):
+ """Set context data for the current async/task context."""
+ data = _ensure_context()
+ if data.get(key) == value:
+ return
+ data[key] = value
+ _context_data.set(data)
+
+
+def delete_context_data(key: str):
+ """Delete a key from the current async/task context."""
+ data = _ensure_context()
+ if key in data:
+ del data[key]
+ _context_data.set(data)
+
+
+def get_context_data(key: Optional[str] = None, default: T = None) -> T:
+ """Get a key from the current context, or the full dict if key is None."""
+ data = _ensure_context()
+ if key is None:
+ return cast(T, data)
+ return cast(T, data.get(key, default))
+
+
+def clear_context_data():
+ """Completely clear the context dict."""
+ _context_data.set({})
diff --git a/python/helpers/files.py b/python/helpers/files.py
index e72ee8b1f..3bcd93a04 100644
--- a/python/helpers/files.py
+++ b/python/helpers/files.py
@@ -14,6 +14,7 @@ import importlib
import importlib.util
import inspect
import glob
+import mimetypes
class VariablesPlugin(ABC):
@@ -22,7 +23,9 @@ class VariablesPlugin(ABC):
pass
-def load_plugin_variables(file: str, backup_dirs: list[str] | None = None) -> dict[str, Any]:
+def load_plugin_variables(
+ file: str, backup_dirs: list[str] | None = None
+) -> dict[str, Any]:
if not file.endswith(".md"):
return {}
@@ -38,11 +41,14 @@ def load_plugin_variables(file: str, backup_dirs: list[str] | None = None) -> di
plugin_file = None
if plugin_file and exists(plugin_file):
-
+
from python.helpers import extract_tools
- classes = extract_tools.load_classes_from_file(plugin_file, VariablesPlugin, one_per_file=False)
+
+ classes = extract_tools.load_classes_from_file(
+ plugin_file, VariablesPlugin, one_per_file=False
+ )
for cls in classes:
- return cls().get_variables(file, backup_dirs) # type: ignore < abstract class here is ok, it is always a subclass
+ return cls().get_variables(file, backup_dirs) # type: ignore < abstract class here is ok, it is always a subclass
# load python code and extract variables variables from it
# module = None
@@ -70,10 +76,13 @@ def load_plugin_variables(file: str, backup_dirs: list[str] | None = None) -> di
# return cls[1]().get_variables() # type: ignore
return {}
+
from python.helpers.strings import sanitize_string
-def parse_file(_filename: str, _directories: list[str] | None = None, _encoding="utf-8", **kwargs):
+def parse_file(
+ _filename: str, _directories: list[str] | None = None, _encoding="utf-8", **kwargs
+):
if _directories is None:
_directories = []
@@ -84,7 +93,7 @@ def parse_file(_filename: str, _directories: list[str] | None = None, _encoding=
with open(absolute_path, "r", encoding=_encoding) as f:
# content = remove_code_fences(f.read())
content = f.read()
-
+
is_json = is_full_json_template(content)
content = remove_code_fences(content)
variables = load_plugin_variables(absolute_path, _directories) or {} # type: ignore
@@ -99,12 +108,16 @@ def parse_file(_filename: str, _directories: list[str] | None = None, _encoding=
# Process include statements
content = process_includes(
# here we use kwargs, the plugin variables are not inherited
- content, _directories, **kwargs
+ content,
+ _directories,
+ **kwargs,
)
return content
-def read_prompt_file(_file: str, _directories: list[str] | None = None, _encoding="utf-8", **kwargs):
+def read_prompt_file(
+ _file: str, _directories: list[str] | None = None, _encoding="utf-8", **kwargs
+):
if _directories is None:
_directories = []
@@ -131,13 +144,15 @@ def read_prompt_file(_file: str, _directories: list[str] | None = None, _encodin
# Process include statements
content = process_includes(
# here we use kwargs, the plugin variables are not inherited
- content, _directories, **kwargs
+ content,
+ _directories,
+ **kwargs,
)
return content
-def read_file(relative_path:str, encoding="utf-8"):
+def read_file(relative_path: str, encoding="utf-8"):
# Try to get the absolute path for the file from the original directory or backup directories
absolute_path = get_abs_path(relative_path)
@@ -146,7 +161,7 @@ def read_file(relative_path:str, encoding="utf-8"):
return f.read()
-def read_file_bin(relative_path:str):
+def read_file_bin(relative_path: str):
# Try to get the absolute path for the file from the original directory or backup directories
absolute_path = get_abs_path(relative_path)
@@ -248,6 +263,7 @@ def find_file_in_dirs(_filename: str, _directories: list[str]):
f"File '{_filename}' not found in any of the provided directories."
)
+
def get_unique_filenames_in_dirs(dir_paths: list[str], pattern: str = "*"):
# returns absolute paths for unique filenames, priority by order in dir_paths
seen = set()
@@ -263,6 +279,7 @@ def get_unique_filenames_in_dirs(dir_paths: list[str], pattern: str = "*"):
result.sort(key=lambda path: os.path.basename(path))
return result
+
def remove_code_fences(text):
# Pattern to match code fences with optional language specifier
pattern = r"(```|~~~)(.*?\n)(.*?)(\1)"
@@ -334,6 +351,7 @@ def delete_dir(relative_path: str):
# suppress all errors - we're ensuring no errors propagate
pass
+
def move_dir(old_path: str, new_path: str):
# rename/move the directory from old_path to new_path (both relative)
abs_old = get_abs_path(old_path)
@@ -345,6 +363,7 @@ def move_dir(old_path: str, new_path: str):
except Exception:
pass # suppress all errors, keep behavior consistent
+
# move dir safely, remove with number if needed
def move_dir_safe(src, dst, rename_format="{name}_{number}"):
base_dst = dst
@@ -355,6 +374,7 @@ def move_dir_safe(src, dst, rename_format="{name}_{number}"):
move_dir(src, dst)
return dst
+
# create dir safely, add number if needed
def create_dir_safe(dst, rename_format="{name}_{number}"):
base_dst = dst
@@ -365,10 +385,12 @@ def create_dir_safe(dst, rename_format="{name}_{number}"):
create_dir(dst)
return dst
+
def create_dir(relative_path: str):
abs_path = get_abs_path(relative_path)
os.makedirs(abs_path, exist_ok=True)
+
def list_files(relative_path: str, filter: str = "*"):
abs_path = get_abs_path(relative_path)
if not os.path.exists(abs_path):
@@ -385,18 +407,22 @@ def get_abs_path(*relative_paths):
"Convert relative paths to absolute paths based on the base directory."
return os.path.join(get_base_dir(), *relative_paths)
-def deabsolute_path(path:str):
+
+def deabsolute_path(path: str):
"Convert absolute paths to relative paths based on the base directory."
return os.path.relpath(path, get_base_dir())
-def fix_dev_path(path:str):
+
+def fix_dev_path(path: str):
"On dev environment, convert /a0/... paths to local absolute paths"
from python.helpers.runtime import is_development
+
if is_development():
if path.startswith("/a0/"):
path = path.replace("/a0/", "")
return get_abs_path(path)
+
def exists(*relative_paths):
path = get_abs_path(*relative_paths)
return os.path.exists(path)
@@ -470,4 +496,29 @@ def move_file(relative_path: str, new_path: str):
def safe_file_name(filename: str) -> str:
# Replace any character that's not alphanumeric, dash, underscore, or dot with underscore
- return re.sub(r'[^a-zA-Z0-9-._]', '_', filename)
+ return re.sub(r"[^a-zA-Z0-9-._]", "_", filename)
+
+
+def read_text_files_in_dir(
+ dir_path: str, max_size: int = 1024 * 1024
+) -> dict[str, str]:
+
+ abs_path = get_abs_path(dir_path)
+ if not os.path.exists(abs_path):
+ return {}
+ result = {}
+ for file_path in [os.path.join(abs_path, f) for f in os.listdir(abs_path)]:
+ try:
+ if not os.path.isfile(file_path):
+ continue
+ if os.path.getsize(file_path) > max_size:
+ continue
+ mime, _ = mimetypes.guess_type(file_path)
+ if mime is not None and not mime.startswith("text"):
+ continue
+ # Check if file is binary by reading a small chunk
+ content = read_file(file_path)
+ result[os.path.basename(file_path)] = content
+ except Exception:
+ continue
+ return result
diff --git a/python/helpers/log.py b/python/helpers/log.py
index 098862a95..068e85499 100644
--- a/python/helpers/log.py
+++ b/python/helpers/log.py
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field
import json
-from typing import Any, Literal, Optional, Dict, TypeVar
+from typing import Any, Literal, Optional, Dict, TypeVar, TYPE_CHECKING
T = TypeVar("T")
import uuid
@@ -8,6 +8,11 @@ from collections import OrderedDict # Import OrderedDict
from python.helpers.strings import truncate_text_by_ratio
import copy
from typing import TypeVar
+from python.helpers.secrets import get_secrets_manager
+
+
+if TYPE_CHECKING:
+ from agent import AgentContext
T = TypeVar("T")
@@ -107,24 +112,7 @@ def _truncate_content(text: str | None) -> str:
return truncated
-def _mask_recursive(obj: T) -> T:
- """Recursively mask secrets in nested objects."""
- try:
- from python.helpers.secrets import SecretsManager
-
- secrets_mgr = SecretsManager.get_instance()
- if isinstance(obj, str):
- return secrets_mgr.mask_values(obj)
- elif isinstance(obj, dict):
- return {k: _mask_recursive(v) for k, v in obj.items()} # type: ignore
- elif isinstance(obj, list):
- return [_mask_recursive(item) for item in obj] # type: ignore
- else:
- return obj
- except Exception as _e:
- # If masking fails, return original object
- return obj
@dataclass
@@ -195,6 +183,7 @@ class LogItem:
class Log:
def __init__(self):
+ self.context: "AgentContext|None" = None # set from outside
self.guid: str = str(uuid.uuid4())
self.updates: list[int] = []
self.logs: list[LogItem] = []
@@ -250,23 +239,23 @@ class Log:
# adjust all content before processing
if heading is not None:
- heading = _mask_recursive(heading)
+ heading = self._mask_recursive(heading)
heading = _truncate_heading(heading)
item.heading = heading
if content is not None:
- content = _mask_recursive(content)
+ content = self._mask_recursive(content)
content = _truncate_content(content)
item.content = content
if kvps is not None:
kvps = OrderedDict(copy.deepcopy(kvps))
- kvps = _mask_recursive(kvps)
+ kvps = self._mask_recursive(kvps)
kvps = _truncate_value(kvps)
item.kvps = kvps
elif item.kvps is None:
item.kvps = OrderedDict()
if kwargs:
kwargs = copy.deepcopy(kwargs)
- kwargs = _mask_recursive(kwargs)
+ kwargs = self._mask_recursive(kwargs)
item.kvps.update(kwargs)
if type is not None:
@@ -285,7 +274,7 @@ class Log:
self._update_progress_from_item(item)
def set_progress(self, progress: str, no: int = 0, active: bool = True):
- progress = _mask_recursive(progress)
+ progress = self._mask_recursive(progress)
progress = _truncate_progress(progress)
self.progress = progress
if not no:
@@ -324,3 +313,28 @@ class Log:
item.heading,
(item.no if item.update_progress == "persistent" else -1),
)
+
+ def _mask_recursive(self, obj: T) -> T:
+ """Recursively mask secrets in nested objects."""
+ try:
+ from agent import AgentContext
+ secrets_mgr = get_secrets_manager(self.context or AgentContext.current())
+
+ # debug helper to identify context mismatch
+ self_id = self.context.id if self.context else None
+ current_ctx = AgentContext.current()
+ current_id = current_ctx.id if current_ctx else None
+ if self_id != current_id:
+ print(f"Context ID mismatch: {self_id} != {current_id}")
+
+ if isinstance(obj, str):
+ return secrets_mgr.mask_values(obj)
+ elif isinstance(obj, dict):
+ return {k: self._mask_recursive(v) for k, v in obj.items()} # type: ignore
+ elif isinstance(obj, list):
+ return [self._mask_recursive(item) for item in obj] # type: ignore
+ else:
+ return obj
+ except Exception as _e:
+ # If masking fails, return original object
+ return obj
\ No newline at end of file
diff --git a/python/helpers/memory.py b/python/helpers/memory.py
index 9e7fb5eb4..bc3f1cf20 100644
--- a/python/helpers/memory.py
+++ b/python/helpers/memory.py
@@ -28,7 +28,7 @@ from langchain_core.documents import Document
from python.helpers import knowledge_import
from python.helpers.log import Log, LogItem
from enum import Enum
-from agent import Agent
+from agent import Agent, AgentContext
import models
import logging
from simpleeval import simple_eval
@@ -63,7 +63,7 @@ class Memory:
@staticmethod
async def get(agent: Agent):
- memory_subdir = agent.config.memory_subdir or "default"
+ memory_subdir = get_agent_memory_subdir(agent)
if Memory.index.get(memory_subdir) is None:
log_item = agent.context.log.log(
type="util",
@@ -136,7 +136,7 @@ class Memory:
em_dir = files.get_abs_path(
"memory/embeddings"
) # just caching, no need to parameterize
- db_dir = Memory._abs_db_dir(memory_subdir)
+ db_dir = abs_db_dir(memory_subdir)
# make sure embeddings and database directories exist
os.makedirs(db_dir, exist_ok=True)
@@ -249,7 +249,7 @@ class Memory:
log_item.update(heading="Preloading knowledge...")
# db abs path
- db_dir = Memory._abs_db_dir(memory_subdir)
+ db_dir = abs_db_dir(memory_subdir)
# Load the index file if it exists
index_path = files.get_abs_path(db_dir, "knowledge_import.json")
@@ -418,7 +418,7 @@ class Memory:
@staticmethod
def _save_db_file(db: MyFaiss, memory_subdir: str):
- abs_dir = Memory._abs_db_dir(memory_subdir)
+ abs_dir = abs_db_dir(memory_subdir)
db.save_local(folder_path=abs_dir)
@staticmethod
@@ -446,10 +446,6 @@ class Memory:
) # float precision can cause values like 1.0000000596046448
return res
- @staticmethod
- def _abs_db_dir(memory_subdir: str) -> str:
- return files.get_abs_path("memory", memory_subdir)
-
@staticmethod
def format_docs_plain(docs: list[Document]) -> list[str]:
result = []
@@ -466,10 +462,6 @@ class Memory:
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
-def get_memory_subdir_abs(agent: Agent) -> str:
- return files.get_abs_path("memory", agent.config.memory_subdir or "default")
-
-
def get_custom_knowledge_subdir_abs(agent: Agent) -> str:
for dir in agent.config.knowledge_subdirs:
if dir != "default":
@@ -480,3 +472,56 @@ def get_custom_knowledge_subdir_abs(agent: Agent) -> str:
def reload():
# clear the memory index, this will force all DBs to reload
Memory.index = {}
+
+
+def abs_db_dir(memory_subdir: str) -> str:
+ # patch for projects, this way we don't need to re-work the structure of memory subdirs
+ if memory_subdir.startswith("projects/"):
+ from python.helpers.projects import get_project_meta_folder
+
+ return files.get_abs_path(get_project_meta_folder(memory_subdir[9:]), "memory")
+ # standard subdirs
+ return files.get_abs_path("memory", memory_subdir)
+
+
+def get_memory_subdir_abs(agent: Agent) -> str:
+ subdir = get_agent_memory_subdir(agent)
+ return abs_db_dir(subdir)
+
+
+def get_agent_memory_subdir(agent: Agent) -> str:
+ # if project is active, use project memory subdir
+ return get_context_memory_subdir(agent.context)
+
+
+def get_context_memory_subdir(context: AgentContext) -> str:
+ # if project is active, use project memory subdir
+ from python.helpers.projects import get_context_memory_subdir as get_project_memory_subdir
+
+ memory_subdir = get_project_memory_subdir(context)
+ if memory_subdir:
+ return memory_subdir
+
+ # no project, regular memory subdir
+ return context.config.memory_subdir or "default"
+
+def get_existing_memory_subdirs() -> list[str]:
+ try:
+ from python.helpers.projects import get_project_meta_folder, get_projects_parent_folder
+ # Get subdirectories from memory folder
+ subdirs = files.get_subdirectories("memory", exclude="embeddings")
+
+ project_subdirs = files.get_subdirectories(get_projects_parent_folder())
+ for project_subdir in project_subdirs:
+ if files.exists(get_project_meta_folder(project_subdir), "memory", "index.faiss"):
+ subdirs.append(f"projects/{project_subdir}")
+
+ # Ensure 'default' is always available
+ if "default" not in subdirs:
+ subdirs.insert(0, "default")
+
+ return subdirs
+ except Exception as e:
+ PrintStyle.error(f"Failed to get memory subdirectories: {str(e)}")
+ return ["default"]
+
\ No newline at end of file
diff --git a/python/helpers/print_style.py b/python/helpers/print_style.py
index 6d6e5f4f5..188697c86 100644
--- a/python/helpers/print_style.py
+++ b/python/helpers/print_style.py
@@ -95,9 +95,10 @@ class PrintStyle:
# Automatically mask secrets in all print output
try:
- from python.helpers.secrets import SecretsManager
- secrets_mgr = SecretsManager.get_instance()
- text = secrets_mgr.mask_values(text)
+ if not hasattr(self, "secrets_mgr"):
+ from python.helpers.secrets import get_secrets_manager
+ self.secrets_mgr = get_secrets_manager()
+ text = self.secrets_mgr.mask_values(text)
except Exception:
# If masking fails, proceed without masking to avoid breaking functionality
pass
diff --git a/python/helpers/projects.py b/python/helpers/projects.py
index ea8861c55..39549b1bc 100644
--- a/python/helpers/projects.py
+++ b/python/helpers/projects.py
@@ -1,78 +1,73 @@
import os
-from typing import TypedDict
+from typing import Literal, TypedDict, TYPE_CHECKING
from python.helpers import files, dirty_json, persist_chat
from python.helpers.print_style import PrintStyle
+
+if TYPE_CHECKING:
+ from agent import AgentContext
+
PROJECTS_PARENT_DIR = "usr/projects"
-PROJECTS_ARCHIVE_DIR = "usr/projects-archived"
PROJECT_META_DIR = ".a0proj"
PROJECT_INSTRUCTIONS_DIR = "instructions"
PROJECT_HEADER_FILE = "project.json"
-CONTEXT_DATA_KEY_PROJECT_PATH = "project_path"
-CONTEXT_DATA_KEY_PROJECT_COLOR = "project_color"
-CONTEXT_DATA_KEY_PROJECT_NAME = "project_name"
+CONTEXT_DATA_KEY_PROJECT = "project"
class BasicProjectData(TypedDict):
- title: str | None
- description: str | None
- instructions: str | None
- color: str | None
+ title: str
+ description: str
+ instructions: str
+ color: str
+ memory: Literal["own", "global"] # in the future we can add cutom and point to another existing folder
+
class EditProjectData(BasicProjectData):
name: str
- path: str
+ instruction_files_count: int
+ variables: str
+ secrets: str
def get_projects_parent_folder():
return files.get_abs_path(PROJECTS_PARENT_DIR)
-def get_projects_archive_folder():
- return files.get_abs_path(PROJECTS_ARCHIVE_DIR)
-
-
def get_project_folder(name: str):
return files.get_abs_path(get_projects_parent_folder(), name)
+def get_project_meta_folder(name: str):
+ return files.get_abs_path(get_project_folder(name), PROJECT_META_DIR)
-def get_archived_project_folder(name: str):
- return files.get_abs_path(get_projects_archive_folder(), name)
-
-
-def archive_project(name: str):
- return files.move_dir_safe(
- get_project_folder(name), get_archived_project_folder(name), rename_format="{name}_{number}"
- )
-
-
-def unarchive_project(name: str):
- return files.move_dir_safe(
- get_archived_project_folder(name), get_project_folder(name), rename_format="{name}_{number}"
- )
-
-def delete_project(path: str):
- files.delete_dir(path)
- deactivate_project_in_chats(path)
- return path
+def delete_project(name: str):
+ abs_path = files.get_abs_path(PROJECTS_PARENT_DIR, name)
+ files.delete_dir(abs_path)
+ deactivate_project_in_chats(name)
+ return name
def create_project(name: str, data: BasicProjectData):
- new_path = files.create_dir_safe(get_project_folder(name), rename_format="{name}_{number}")
+ abs_path = files.create_dir_safe(
+ files.get_abs_path(PROJECTS_PARENT_DIR, name), rename_format="{name}_{number}"
+ )
+ files.create_dir(
+ files.get_abs_path(abs_path, PROJECT_META_DIR, PROJECT_INSTRUCTIONS_DIR)
+ )
data = _normalizeBasicData(data)
- save_project_files(new_path, data)
- return new_path
+ save_project_header(name, data)
+ return name
-def load_project_header(path: str):
- header: dict = dirty_json.parse(
- files.read_file(files.get_abs_path(path, PROJECT_META_DIR, PROJECT_HEADER_FILE))
- ) # type: ignore
- header["path"] = path
+def load_project_header(name: str):
+ abs_path = files.get_abs_path(
+ PROJECTS_PARENT_DIR, name, PROJECT_META_DIR, PROJECT_HEADER_FILE
+ )
+ header: dict = dirty_json.parse(files.read_file(abs_path)) # type: ignore
+ header["name"] = name
return header
@@ -82,61 +77,96 @@ def _normalizeBasicData(data: BasicProjectData):
description=data.get("description", ""),
instructions=data.get("instructions", ""),
color=data.get("color", ""),
+ memory=data.get("memory", "own"),
)
+def _normalizeEditData(data: EditProjectData):
+ return EditProjectData(
+ name=data.get("name", ""),
+ title=data.get("title", ""),
+ description=data.get("description", ""),
+ instructions=data.get("instructions", ""),
+ variables=data.get("variables", ""),
+ color=data.get("color", ""),
+ instruction_files_count=data.get("instruction_files_count", 0),
+ secrets=data.get("secrets", ""),
+ memory=data.get("memory", "own"),
+ )
-def update_project(path: str, data: BasicProjectData):
- current: BasicProjectData = load_edit_project_data(path) # type: ignore
- current.update(_normalizeBasicData(data))
- save_project_files(path, current)
- reactivate_project_in_chats(path)
- return path
-
-
-def load_edit_project_data(path: str) -> BasicProjectData:
- data = BasicProjectData(
- **dirty_json.parse(
- files.read_file(
- files.get_abs_path(path, PROJECT_META_DIR, PROJECT_HEADER_FILE)
- )
- ) # type: ignore
+def _edit_data_to_basic_data(data: EditProjectData):
+ return _normalizeBasicData(data)
+
+def _basic_data_to_edit_data(data: BasicProjectData):
+ return _normalizeEditData(data) # type: ignore
+
+
+def update_project(name: str, data: EditProjectData):
+ # merge with current state
+ current = load_edit_project_data(name)
+ current.update(data)
+ current = _normalizeEditData(current)
+
+ # save header data
+ header = _edit_data_to_basic_data(current)
+ save_project_header(name, header)
+
+ # save secrets
+ save_project_variables(name, current["variables"])
+ save_project_secrets(name, current["secrets"])
+
+ reactivate_project_in_chats(name)
+ return name
+
+def load_basic_project_data(name: str) -> BasicProjectData:
+ data = BasicProjectData(**load_project_header(name))
+ normalized = _normalizeBasicData(data)
+ return normalized
+
+
+def load_edit_project_data(name: str) -> EditProjectData:
+ data = load_basic_project_data(name)
+ additional_instructions = get_additional_instructions_files(
+ name
+ ) # for additional info
+ variables = load_project_variables(name)
+ secrets = load_project_secrets_masked(name)
+ data = EditProjectData(
+ **data,
+ name=name,
+ instruction_files_count=len(additional_instructions),
+ variables=variables,
+ secrets=secrets,
)
- data = _normalizeBasicData(data)
- data = EditProjectData(**data, name=os.path.basename(path), path=path)
- return data # type: ignore
+ data = _normalizeEditData(data)
+ return data
-def save_project_files(path: str, data: BasicProjectData):
+def save_project_header(name: str, data: BasicProjectData):
# save project header file
header = dirty_json.stringify(data)
- files.write_file(
- files.get_abs_path(path, PROJECT_META_DIR, PROJECT_HEADER_FILE), header
+ abs_path = files.get_abs_path(
+ PROJECTS_PARENT_DIR, name, PROJECT_META_DIR, PROJECT_HEADER_FILE
)
+ files.write_file(abs_path, header)
+
def get_active_projects_list():
return _get_projects_list(get_projects_parent_folder())
-def get_archived_projects_list():
- return _get_projects_list(get_projects_archive_folder())
-
-
def _get_projects_list(parent_dir):
projects = []
# folders in project directory
for name in os.listdir(parent_dir):
try:
- path = os.path.join(parent_dir, name)
- if os.path.isdir(path):
-
- project_data = load_edit_project_data(path)
-
+ abs_path = os.path.join(parent_dir, name)
+ if os.path.isdir(abs_path):
+ project_data = load_basic_project_data(name)
projects.append(
{
"name": name,
- "path": path,
"title": project_data.get("title", ""),
"description": project_data.get("description", ""),
"color": project_data.get("color", ""),
@@ -146,24 +176,24 @@ def _get_projects_list(parent_dir):
PrintStyle.error(f"Error loading project {name}: {str(e)}")
# sort projects by name
-
projects.sort(key=lambda x: x["name"])
return projects
-def activate_project(context_id: str, path: str):
+def activate_project(context_id: str, name: str):
from agent import AgentContext
- data = load_edit_project_data(path)
+ data = load_edit_project_data(name)
context = AgentContext.get(context_id)
if context is None:
raise Exception("Context not found")
- name = str(data.get("title", data.get("name", data.get("path", ""))))
- name = name[:22] + "..." if len(name) > 25 else name
- context.set_data(CONTEXT_DATA_KEY_PROJECT_PATH, path)
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_PATH, path)
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_COLOR, data.get("color", ""))
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_NAME, name)
+ display_name = str(data.get("title", name))
+ display_name = display_name[:22] + "..." if len(display_name) > 25 else display_name
+ context.set_data(CONTEXT_DATA_KEY_PROJECT, name)
+ context.set_output_data(
+ CONTEXT_DATA_KEY_PROJECT,
+ {"name": name, "title": display_name, "color": data.get("color", "")},
+ )
# persist
persist_chat.save_tmp_chat(context)
@@ -175,37 +205,87 @@ def deactivate_project(context_id: str):
context = AgentContext.get(context_id)
if context is None:
raise Exception("Context not found")
- context.set_data(CONTEXT_DATA_KEY_PROJECT_PATH, None)
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_PATH, None)
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_COLOR, None)
- context.set_output_data(CONTEXT_DATA_KEY_PROJECT_NAME, None)
+ context.set_data(CONTEXT_DATA_KEY_PROJECT, None)
+ context.set_output_data(CONTEXT_DATA_KEY_PROJECT, None)
# persist
persist_chat.save_tmp_chat(context)
-def reactivate_project_in_chats(path: str):
+def reactivate_project_in_chats(name: str):
from agent import AgentContext
for context in AgentContext.all():
- if context.get_data(CONTEXT_DATA_KEY_PROJECT_PATH) == path:
- activate_project(context.id, path)
+ if context.get_data(CONTEXT_DATA_KEY_PROJECT) == name:
+ activate_project(context.id, name)
persist_chat.save_tmp_chat(context)
-def deactivate_project_in_chats(path: str):
+
+def deactivate_project_in_chats(name: str):
from agent import AgentContext
for context in AgentContext.all():
- if context.get_data(CONTEXT_DATA_KEY_PROJECT_PATH) == path:
+ if context.get_data(CONTEXT_DATA_KEY_PROJECT) == name:
deactivate_project(context.id)
persist_chat.save_tmp_chat(context)
-def build_system_prompt_vars(project_path: str):
- project_data = load_edit_project_data(project_path)
+
+def build_system_prompt_vars(name: str):
+ project_data = load_basic_project_data(name)
+ main_instructions = project_data.get("instructions", "") or ""
+ additional_instructions = get_additional_instructions_files(name)
+ complete_instructions = (
+ main_instructions
+ + "\n\n".join(
+ additional_instructions[k] for k in sorted(additional_instructions)
+ )
+ ).strip()
return {
- "project_path": project_path,
"project_name": project_data.get("title", ""),
"project_description": project_data.get("description", ""),
- "project_instructions": project_data.get("instructions", ""),
+ "project_instructions": complete_instructions or "",
}
+
+def get_additional_instructions_files(name: str):
+ instructions_folder = files.get_abs_path(
+ get_project_folder(name), PROJECT_META_DIR, PROJECT_INSTRUCTIONS_DIR
+ )
+ return files.read_text_files_in_dir(instructions_folder)
+
+def get_context_project_name(context: "AgentContext") -> str | None:
+ return context.get_data(CONTEXT_DATA_KEY_PROJECT)
+
+def load_project_variables(name: str):
+ try:
+ abs_path = files.get_abs_path(
+ get_project_meta_folder(name), "variables.env"
+ )
+ return files.read_file(abs_path)
+ except Exception:
+ return ""
+
+def save_project_variables(name: str, variables: str):
+ abs_path = files.get_abs_path(
+ get_project_meta_folder(name), "variables.env"
+ )
+ files.write_file(abs_path, variables)
+
+def load_project_secrets_masked(name:str, merge_with_global=False):
+ from python.helpers import secrets
+ mgr = secrets.get_project_secrets_manager(name, merge_with_global)
+ return mgr.get_masked_secrets()
+
+def save_project_secrets(name: str, secrets: str):
+ from python.helpers.secrets import get_project_secrets_manager
+ secrets_manager = get_project_secrets_manager(name)
+ secrets_manager.save_secrets_with_merge(secrets)
+
+def get_context_memory_subdir(context: "AgentContext") -> str | None:
+ # if a project is active and has memory isolation set, return the project memory subdir
+ project_name = get_context_project_name(context)
+ if project_name:
+ project_data = load_basic_project_data(project_name)
+ if project_data["memory"] == "own":
+ return "projects/" + project_name
+ return None # no memory override
\ No newline at end of file
diff --git a/python/helpers/secrets.py b/python/helpers/secrets.py
index a4b58c51c..53ee85d9b 100644
--- a/python/helpers/secrets.py
+++ b/python/helpers/secrets.py
@@ -4,27 +4,32 @@ import time
import os
from io import StringIO
from dataclasses import dataclass
-from typing import Dict, Optional, List, Literal, Set, Callable
+from typing import Dict, Optional, List, Literal, Set, Callable, Tuple, TYPE_CHECKING
from dotenv.parser import parse_stream
from python.helpers.errors import RepairableException
from python.helpers import files
+if TYPE_CHECKING:
+ from agent import AgentContext
+
# New alias-based placeholder format §§secret(KEY)
ALIAS_PATTERN = r"§§secret\(([A-Za-z_][A-Za-z0-9_]*)\)"
+DEFAULT_SECRETS_FILE = "tmp/secrets.env"
+
def alias_for_key(key: str, placeholder: str = "§§secret({key})") -> str:
# Return alias string for given key in upper-case
key = key.upper()
return placeholder.format(key=key)
+
@dataclass
class EnvLine:
raw: str
type: Literal["pair", "comment", "blank", "other"]
key: Optional[str] = None
value: Optional[str] = None
- key_part: Optional[str] = None # original left side including whitespace up to '='
inline_comment: Optional[str] = (
None # preserves trailing inline comment including leading spaces and '#'
)
@@ -118,44 +123,55 @@ class StreamingSecretsFilter:
class SecretsManager:
- SECRETS_FILE = "tmp/secrets.env"
PLACEHOLDER_PATTERN = ALIAS_PATTERN
MASK_VALUE = "***"
- _instance: Optional["SecretsManager"] = None
+ _instances: Dict[Tuple[str, ...], "SecretsManager"] = {}
_secrets_cache: Optional[Dict[str, str]] = None
_last_raw_text: Optional[str] = None
@classmethod
- def get_instance(cls) -> "SecretsManager":
- if cls._instance is None:
- cls._instance = cls()
- return cls._instance
-
- def __init__(self):
+ def get_instance(cls, *secrets_files: str) -> "SecretsManager":
+ if not secrets_files:
+ secrets_files = (DEFAULT_SECRETS_FILE,)
+ key = tuple(secrets_files)
+ if key not in cls._instances:
+ cls._instances[key] = cls(*secrets_files)
+ return cls._instances[key]
+
+ def __init__(self, *files: str):
self._lock = threading.RLock()
- # instance-level override for secrets file
- self._secrets_file_rel = self.SECRETS_FILE
-
- def set_secrets_file(self, relative_path: str):
- """Override the relative secrets file location (useful for tests)."""
- with self._lock:
- self._secrets_file_rel = relative_path
- self.clear_cache()
+ # instance-level list of secrets files
+ self._files: Tuple[str, ...] = tuple(files) if files else (DEFAULT_SECRETS_FILE,)
+ self._raw_snapshots: Dict[str, str] = {}
+ self._secrets_cache = None
+ self._last_raw_text = None
def read_secrets_raw(self) -> str:
"""Read raw secrets file content from local filesystem (same system)."""
- try:
- content = files.read_file(self._secrets_file_rel)
- self._last_raw_text = content
- return content
- except Exception:
- self._last_raw_text = ""
- return ""
+ parts: List[str] = []
+ self._raw_snapshots = {}
+
+ for path in self._files:
+ try:
+ content = files.read_file(path)
+ except Exception:
+ content = ""
+
+ self._raw_snapshots[path] = content
+ parts.append(content)
+
+ combined = "\n".join(parts)
+ self._last_raw_text = combined
+ return combined
def _write_secrets_raw(self, content: str):
"""Write raw secrets file content to local filesystem."""
- files.write_file(self._secrets_file_rel, content)
+ if len(self._files) != 1:
+ raise RuntimeError(
+ "Saving secrets content is only supported for a single secrets file"
+ )
+ files.write_file(self._files[0], content)
def load_secrets(self) -> Dict[str, str]:
"""Load secrets from file, return key-value dict"""
@@ -163,29 +179,27 @@ class SecretsManager:
if self._secrets_cache is not None:
return self._secrets_cache
- secrets: Dict[str, str] = {}
- try:
- content = self.read_secrets_raw()
- # keep raw snapshot for future save merge without reading again
- self._last_raw_text = content
- if content:
- secrets = self.parse_env_content(content)
- except Exception as e:
- # On unexpected failure, keep empty cache rather than crash
- secrets = {}
-
- self._secrets_cache = secrets
- return secrets
+ combined_raw = self.read_secrets_raw()
+ merged_secrets = (
+ self.parse_env_content(combined_raw) if combined_raw else {}
+ )
+
+ # Only track the first file's raw text for single-file setups
+ if len(self._files) != 1:
+ self._last_raw_text = None
+
+ self._secrets_cache = merged_secrets
+ return merged_secrets
def save_secrets(self, secrets_content: str):
"""Save secrets content to file and update cache"""
+ if len(self._files) != 1:
+ raise RuntimeError(
+ "Saving secrets is disabled when multiple files are configured"
+ )
with self._lock:
- # Ensure write to local filesystem (UTF-8)
self._write_secrets_raw(secrets_content)
- # Update cache
- self._secrets_cache = self.parse_env_content(secrets_content)
- # Update raw snapshot
- self._last_raw_text = secrets_content
+ self._invalidate_all_caches()
def save_secrets_with_merge(self, submitted_content: str):
"""Merge submitted content with existing file preserving comments, order and supporting deletion.
@@ -193,13 +207,19 @@ class SecretsManager:
- Keys present in existing but omitted from submitted are deleted.
- New keys with non-masked values are appended at the end.
"""
+ if len(self._files) != 1:
+ raise RuntimeError(
+ "Merging secrets is disabled when multiple files are configured"
+ )
with self._lock:
# Prefer in-memory snapshot to avoid disk reads during save
+ primary_path = self._files[0]
if self._last_raw_text is not None:
existing_text = self._last_raw_text
else:
try:
- existing_text = self.read_secrets_raw()
+ existing_text = files.read_file(primary_path)
+ self._raw_snapshots[primary_path] = existing_text
except Exception as e:
# If read fails and submitted contains masked values, abort to avoid losing values/comments
if self.MASK_VALUE in submitted_content:
@@ -210,7 +230,8 @@ class SecretsManager:
existing_text = ""
merged_lines = self._merge_env(existing_text, submitted_content)
merged_text = self._serialize_env_lines(merged_lines)
- self.save_secrets(merged_text)
+ self._write_secrets_raw(merged_text)
+ self._invalidate_all_caches()
def get_keys(self) -> List[str]:
"""Get list of secret keys"""
@@ -219,7 +240,7 @@ class SecretsManager:
def get_secrets_for_prompt(self) -> str:
"""Get formatted string of secret keys for system prompt"""
- content = self._last_raw_text or self.read_secrets_raw()
+ content = self.read_secrets_raw()
if not content:
return ""
@@ -251,9 +272,7 @@ class SecretsManager:
return secrets[key]
else:
available_keys = ", ".join(secrets.keys())
- error_msg = (
- f"Secret placeholder '{alias_for_key(key)}' not found in secrets store.\n"
- )
+ error_msg = f"Secret placeholder '{alias_for_key(key)}' not found in secrets store.\n"
error_msg += f"Available secrets: {available_keys}"
raise RepairableException(error_msg)
@@ -276,7 +295,9 @@ class SecretsManager:
return result
- def mask_values(self, text: str, min_length: int = 4, placeholder: str = "§§secret({key})") -> str:
+ def mask_values(
+ self, text: str, min_length: int = 4, placeholder: str = "§§secret({key})"
+ ) -> str:
"""Replace actual secret values with placeholders in text"""
if not text:
return text
@@ -295,18 +316,21 @@ class SecretsManager:
def get_masked_secrets(self) -> str:
"""Get content with values masked for frontend display (preserves comments and unrecognized lines)"""
- if not (content:=self.read_secrets_raw()):
+ content = self.read_secrets_raw()
+ if not content:
return ""
# Parse content for known keys using python-dotenv
secrets_map = self.parse_env_content(content)
env_lines = self.parse_env_lines(content)
+
# Replace values with mask for keys present
for ln in env_lines:
if ln.type == "pair" and ln.key is not None:
ln.key = ln.key.upper()
if ln.key in secrets_map and secrets_map[ln.key] != "":
ln.value = self.MASK_VALUE
+
return self._serialize_env_lines(env_lines)
def parse_env_content(self, content: str) -> Dict[str, str]:
@@ -325,6 +349,13 @@ class SecretsManager:
"""Clear the secrets cache"""
with self._lock:
self._secrets_cache = None
+ self._raw_snapshots = {}
+ self._last_raw_text = None
+
+ @classmethod
+ def _invalidate_all_caches(cls):
+ for instance in cls._instances.values():
+ instance.clear_cache()
# ---------------- Internal helpers for parsing/merging ----------------
@@ -342,9 +373,7 @@ class SecretsManager:
# Fallback to composed key_part if original not available
if "=" in line_text:
left, right = line_text.split("=", 1)
- key_part = left
else:
- key_part = binding.key
right = ""
# Try to extract inline comment by scanning right side to comment start, respecting quotes
in_single = False
@@ -376,7 +405,6 @@ class SecretsManager:
type="pair",
key=binding.key,
value=binding.value or "",
- key_part=key_part,
inline_comment=inline_comment,
)
)
@@ -404,11 +432,15 @@ class SecretsManager:
out: List[str] = []
for ln in lines:
if ln.type == "pair" and ln.key is not None:
- left_raw = ln.key_part if ln.key_part is not None else ln.key
+ left_raw = ln.key
left = left_raw.upper()
val = ln.value if ln.value is not None else ""
comment = ln.inline_comment or ""
- formatted_key = key_formatter(left) if key_formatter else f"{key_delimiter}{left}{key_delimiter}"
+ formatted_key = (
+ key_formatter(left)
+ if key_formatter
+ else f"{key_delimiter}{left}{key_delimiter}"
+ )
val_part = f'="{val}"' if with_values else ""
comment_part = f" {comment}" if with_comments and comment else ""
out.append(f"{formatted_key}{val_part}{comment_part}")
@@ -455,11 +487,10 @@ class SecretsManager:
existing_val = existing_pairs[key].value or ""
merged.append(
EnvLine(
- raw=f"{(sub.key_part or key)}={existing_val}",
+ raw=f"{key}={existing_val}",
type="pair",
key=key,
value=existing_val,
- key_part=sub.key_part or key,
inline_comment=sub.inline_comment,
)
)
@@ -471,3 +502,40 @@ class SecretsManager:
merged.append(sub)
return merged
+
+
+def get_secrets_manager(context: "AgentContext|None" = None) -> SecretsManager:
+ from python.helpers import projects
+
+ # default secrets file
+ secret_files = [DEFAULT_SECRETS_FILE]
+
+ # use AgentContext from contextvars if no context provided
+ if not context:
+ from agent import AgentContext
+ context = AgentContext.current()
+
+ # merged with project secrets if active
+ if context:
+ project = projects.get_context_project_name(context)
+ if project:
+ secret_files.append(files.get_abs_path(projects.get_project_meta_folder(project), "secrets.env"))
+
+ return SecretsManager.get_instance(*secret_files)
+
+def get_project_secrets_manager(project_name: str, merge_with_global: bool = False) -> SecretsManager:
+ from python.helpers import projects
+
+ # default secrets file
+ secret_files = []
+
+ if merge_with_global:
+ secret_files.append(DEFAULT_SECRETS_FILE)
+
+ # merged with project secrets if active
+ secret_files.append(files.get_abs_path(projects.get_project_meta_folder(project_name), "secrets.env"))
+
+ return SecretsManager.get_instance(*secret_files)
+
+def get_default_secrets_manager() -> SecretsManager:
+ return SecretsManager.get_instance()
\ No newline at end of file
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index 3c6d826b8..d882de94c 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -11,7 +11,7 @@ from python.helpers import runtime, whisper, defer, git
from . import files, dotenv
from python.helpers.print_style import PrintStyle
from python.helpers.providers import get_providers
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_default_secrets_manager
from python.helpers import dirty_json
@@ -1110,7 +1110,7 @@ def convert_out(settings: Settings) -> SettingsOutput:
# Secrets section
secrets_fields: list[SettingsField] = []
- secrets_manager = SecretsManager.get_instance()
+ secrets_manager = get_default_secrets_manager()
try:
secrets = secrets_manager.get_masked_secrets()
except Exception:
@@ -1417,10 +1417,9 @@ def _write_sensitive_settings(settings: Settings):
set_root_password(settings["root_password"])
# Handle secrets separately - merge with existing preserving comments/order and support deletions
- secrets_manager = SecretsManager.get_instance()
+ secrets_manager = get_default_secrets_manager()
submitted_content = settings["secrets"]
secrets_manager.save_secrets_with_merge(submitted_content)
- secrets_manager.clear_cache() # Clear cache to reload secrets
diff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.py
index 20254685b..2b94960e3 100644
--- a/python/helpers/vector_db.py
+++ b/python/helpers/vector_db.py
@@ -14,6 +14,7 @@ from langchain_community.vectorstores.utils import (
DistanceStrategy,
)
from langchain.embeddings import CacheBackedEmbeddings
+from simpleeval import simple_eval
from agent import Agent
@@ -140,7 +141,7 @@ def cosine_normalizer(val: float) -> float:
def get_comparator(condition: str):
def comparator(data: dict[str, Any]):
try:
- result = eval(condition, {}, data)
+ result = simple_eval(condition, {}, data)
return result
except Exception as e:
# PrintStyle.error(f"Error evaluating condition: {e}")
diff --git a/python/tools/behaviour_adjustment.py b/python/tools/behaviour_adjustment.py
index 735a39dfa..e9c60bb05 100644
--- a/python/tools/behaviour_adjustment.py
+++ b/python/tools/behaviour_adjustment.py
@@ -52,7 +52,7 @@ async def update_behaviour(agent: Agent, log_item: LogItem, adjustments: str):
def get_custom_rules_file(agent: Agent):
- return memory.get_memory_subdir_abs(agent) + f"/behaviour.md"
+ return files.get_abs_path(memory.get_memory_subdir_abs(agent), "behaviour.md")
def read_rules(agent: Agent):
diff --git a/python/tools/browser_agent.py b/python/tools/browser_agent.py
index 949543a52..6d5f085b2 100644
--- a/python/tools/browser_agent.py
+++ b/python/tools/browser_agent.py
@@ -9,7 +9,7 @@ from python.helpers import files, defer, persist_chat, strings
from python.helpers.browser_use import browser_use # type: ignore[attr-defined]
from python.helpers.print_style import PrintStyle
from python.helpers.playwright import ensure_playwright_binary
-from python.helpers.secrets import SecretsManager
+from python.helpers.secrets import get_secrets_manager
from python.extensions.message_loop_start._10_iteration_no import get_iter_no
from pydantic import BaseModel
import uuid
@@ -153,7 +153,7 @@ class State:
try:
- secrets_manager = SecretsManager.get_instance()
+ secrets_manager = get_secrets_manager(self.agent.context)
secrets_dict = secrets_manager.load_secrets()
self.use_agent = browser_use.Agent(
@@ -216,7 +216,7 @@ class BrowserAgent(Tool):
self.guid = self.agent.context.generate_id() # short random id
reset = str(reset).lower().strip() == "true"
await self.prepare_state(reset=reset)
- message = SecretsManager.get_instance().mask_values(message, placeholder="<secret>{key}</secret>") # mask any potential passwords passed from A0 to browser-use to browser-use format
+ message = get_secrets_manager(self.agent.context).mask_values(message, placeholder="<secret>{key}</secret>") # mask any potential passwords passed from A0 to browser-use to browser-use format
task = self.state.start_task(message) if self.state else None
# wait for browser agent to finish and update progress with timeout
@@ -394,7 +394,7 @@ class BrowserAgent(Tool):
def _mask(self, text: str) -> str:
try:
- return SecretsManager.get_instance().mask_values(text or "")
+ return get_secrets_manager(self.agent.context).mask_values(text or "")
except Exception as e:
return text or ""
diff --git a/tests/rate_limiter_test.py b/tests/rate_limiter_test.py
index 6ab180e6b..ac0aa52fe 100644
--- a/tests/rate_limiter_test.py
+++ b/tests/rate_limiter_test.py
@@ -3,8 +3,8 @@ import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import models
-provider = "openai"
-name = "gpt-4.1-mini"
+provider = "openrouter"
+name = "deepseek/deepseek-r1"
model = models.get_chat_model(
provider=provider,
diff --git a/webui/components/chat/top-section/chat-top-store.js b/webui/components/chat/top-section/chat-top-store.js
new file mode 100644
index 000000000..8dd47b000
--- /dev/null
+++ b/webui/components/chat/top-section/chat-top-store.js
@@ -0,0 +1,12 @@
+import { createStore } from "/js/AlpineStore.js";
+
+// define the model object holding data and functions
+const model = {
+ connected: false,
+};
+
+// convert it to alpine store
+const store = createStore("chatTop", model);
+
+// export for use in other files
+export { store };
diff --git a/webui/components/chat/top-section/chat-top.html b/webui/components/chat/top-section/chat-top.html
new file mode 100644
index 000000000..179495d6c
--- /dev/null
+++ b/webui/components/chat/top-section/chat-top.html
@@ -0,0 +1,43 @@
+<html>
+
+<head>
+ <title>Example component or modal</title>
+
+ <!-- Import the alpine store -->
+ <script type="module">
+ import { store } from "/components/chat/top-section/chat-top-store.js";
+ </script>
+</head>
+
+<body>
+
+ <!-- This construct of x-data + x-if is used to ensure the component is only rendered when the store is available -->
+ <div x-data>
+ <template x-if="$store.chatTop">
+
+ <!-- Time and Date -->
+ <div id="time-date-container">
+ <div id="time-date"></div>
+ <div class="status-icon">
+ <svg viewBox="0 0 30 30">
+ <!-- Connected State (filled circle) -->
+ <circle class="connected-circle" cx="15" cy="15" r="8"
+ x-bind:fill="$store.chatTop.connected ? '#00c340' : 'none'" x-bind:opacity="$store.chatTop.connected ? 1 : 0" />
+
+ <!-- Disconnected State (outline circle) -->
+ <circle class="disconnected-circle" cx="15" cy="15" r="12" fill="none" stroke="#e40138"
+ stroke-width="3" x-bind:opacity="$store.chatTop.connected ? 0 : 1" />
+ </svg>
+ </div>
+ <!-- Notification Toggle positioned next to time-date -->
+ <x-component path="notifications/notification-icons.html"></x-component>
+ <!-- Project Selector -->
+ <x-component path="projects/project-selector.html"></x-component>
+ </div>
+
+ </template>
+ </div>
+
+</body>
+
+</html>
\ No newline at end of file
diff --git a/webui/components/modals/file-browser/file-browser-store.js b/webui/components/modals/file-browser/file-browser-store.js
index 61402d428..3764f7215 100644
--- a/webui/components/modals/file-browser/file-browser-store.js
+++ b/webui/components/modals/file-browser/file-browser-store.js
@@ -24,31 +24,36 @@ const model = {
},
// --- Public API (called from button/link) --------------------------------
- async open() {
+ async open(path = "") {
if (this.isLoading) return; // Prevent double-open
-
this.isLoading = true;
this.error = null;
this.history = [];
-
+
try {
// Open modal FIRST (immediate UI feedback)
- this.closePromise = window.openModal('modals/file-browser/file-browser.html');
-
- // Setup cleanup on modal close
- if (this.closePromise && typeof this.closePromise.then === 'function') {
- this.closePromise.then(() => {
- this.destroy();
- });
- }
-
+ this.closePromise = window.openModal(
+ "modals/file-browser/file-browser.html"
+ );
+
+ // // Setup cleanup on modal close
+ // if (this.closePromise && typeof this.closePromise.then === "function") {
+ // this.closePromise.then(() => {
+ // this.destroy();
+ // });
+ // }
+
// Use stored initial path or default
- const path = this.initialPath || this.browser.currentPath || "$WORK_DIR";
+ path = path || this.initialPath || this.browser.currentPath || "$WORK_DIR";
this.browser.currentPath = path;
-
+
// Fetch files
await this.fetchFiles(this.browser.currentPath);
-
+
+ // await modal close
+ await this.closePromise;
+ this.destroy();
+
} catch (error) {
console.error("File browser error:", error);
this.error = error?.message || "Failed to load files";
@@ -128,7 +133,9 @@ const model = {
async fetchFiles(path = "") {
this.isLoading = true;
try {
- const response = await fetchApi(`/get_work_dir_files?path=${encodeURIComponent(path)}`);
+ const response = await fetchApi(
+ `/get_work_dir_files?path=${encodeURIComponent(path)}`
+ );
if (response.ok) {
const data = await response.json();
this.browser.entries = data.data.entries;
@@ -139,7 +146,10 @@ const model = {
this.browser.entries = [];
}
} catch (e) {
- window.toastFrontendError("Error fetching files: " + e.message, "File Browser Error");
+ window.toastFrontendError(
+ "Error fetching files: " + e.message,
+ "File Browser Error"
+ );
this.browser.entries = [];
} finally {
this.isLoading = false;
@@ -147,7 +157,9 @@ const model = {
},
async navigateToFolder(path) {
- if (this.browser.currentPath !== path) this.history.push(this.browser.currentPath);
+ if(!path.startsWith("/")) path = "/" + path;
+ if (this.browser.currentPath !== path)
+ this.history.push(this.browser.currentPath);
await this.fetchFiles(path);
},
@@ -165,20 +177,32 @@ const model = {
const resp = await fetchApi("/delete_work_dir_file", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ path: file.path, currentPath: this.browser.currentPath }),
+ body: JSON.stringify({
+ path: file.path,
+ currentPath: this.browser.currentPath,
+ }),
});
if (resp.ok) {
- this.browser.entries = this.browser.entries.filter((e) => e.path !== file.path);
+ this.browser.entries = this.browser.entries.filter(
+ (e) => e.path !== file.path
+ );
alert("File deleted successfully.");
} else {
alert(`Error deleting file: ${await resp.text()}`);
}
} catch (e) {
- window.toastFrontendError("Error deleting file: " + e.message, "File Delete Error");
+ window.toastFrontendError(
+ "Error deleting file: " + e.message,
+ "File Delete Error"
+ );
}
},
async handleFileUpload(event) {
+ return store._handleFileUpload(event); // bind to model to ensure correct context
+ },
+
+ async _handleFileUpload(event) {
try {
const files = event.target.files;
if (!files.length) return;
@@ -186,27 +210,38 @@ const model = {
formData.append("path", this.browser.currentPath);
for (let f of files) {
const ext = f.name.split(".").pop().toLowerCase();
- if (!["zip", "tar", "gz", "rar", "7z"].includes(ext) && f.size > 100 * 1024 * 1024) {
+ if (
+ !["zip", "tar", "gz", "rar", "7z"].includes(ext) &&
+ f.size > 100 * 1024 * 1024
+ ) {
alert(`File ${f.name} exceeds 100MB limit.`);
continue;
}
formData.append("files[]", f);
}
- const resp = await fetchApi("/upload_work_dir_files", { method: "POST", body: formData });
+ const resp = await fetchApi("/upload_work_dir_files", {
+ method: "POST",
+ body: formData,
+ });
if (resp.ok) {
const data = await resp.json();
this.browser.entries = data.data.entries;
this.browser.currentPath = data.data.current_path;
this.browser.parentPath = data.data.parent_path;
if (data.failed && data.failed.length) {
- const msg = data.failed.map((f) => `${f.name}: ${f.error}`).join("\n");
+ const msg = data.failed
+ .map((f) => `${f.name}: ${f.error}`)
+ .join("\n");
alert(`Some files failed to upload:\n${msg}`);
}
} else {
alert(await resp.text());
}
} catch (e) {
- window.toastFrontendError("Error uploading files: " + e.message, "File Upload Error");
+ window.toastFrontendError(
+ "Error uploading files: " + e.message,
+ "File Upload Error"
+ );
} finally {
event.target.value = ""; // reset input so same file can be reselected
}
@@ -233,12 +268,14 @@ window.openFileLink = async function (path) {
}
if (resp.is_dir) {
// Set initial path and open via store
- store.initialPath = resp.abs_path;
- await store.open();
+ await store.open(resp.abs_path);
} else {
store.downloadFile({ path: resp.abs_path, name: resp.file_name });
}
} catch (e) {
- window.toastFrontendError("Error opening file: " + e.message, "File Open Error");
+ window.toastFrontendError(
+ "Error opening file: " + e.message,
+ "File Open Error"
+ );
}
};
diff --git a/webui/components/modals/file-browser/file-browser.html b/webui/components/modals/file-browser/file-browser.html
index 5551e8dfa..b9689fb53 100644
--- a/webui/components/modals/file-browser/file-browser.html
+++ b/webui/components/modals/file-browser/file-browser.html
@@ -211,6 +211,7 @@
/* Path Navigator Styles */
.path-navigator {
+ overflow: hidden;
display: flex;
align-items: center;
gap: 24px;
diff --git a/webui/components/notifications/notification-store.js b/webui/components/notifications/notification-store.js
index 60534f291..260d53ed5 100644
--- a/webui/components/notifications/notification-store.js
+++ b/webui/components/notifications/notification-store.js
@@ -664,7 +664,8 @@ const model = {
title = "Connection Error",
display_time = 8,
group = "",
- priority = defaultPriority
+ priority = defaultPriority,
+ frontendOnly = false
) {
return await this.addFrontendToast(
NotificationType.ERROR,
diff --git a/webui/components/projects/project-create.html b/webui/components/projects/project-create.html
index f47a0c684..93137d6d9 100644
--- a/webui/components/projects/project-create.html
+++ b/webui/components/projects/project-create.html
@@ -13,7 +13,7 @@
<div>
<div class="project-detail">
- <x-component path="projects/project-basic-data-form.html">
+ <x-component path="projects/project-edit-basic-data.html">
</x-component>
<div class="buttons-right">
diff --git a/webui/components/projects/project-basic-data-form.html b/webui/components/projects/project-edit-basic-data.html
similarity index 81%
rename from webui/components/projects/project-basic-data-form.html
rename to webui/components/projects/project-edit-basic-data.html
index e0da32eb1..6ef3dc2b2 100644
--- a/webui/components/projects/project-basic-data-form.html
+++ b/webui/components/projects/project-edit-basic-data.html
@@ -11,18 +11,20 @@
<div x-data>
<template x-if="$store.projects && $store.projects.selectedProject">
<div>
- <!-- <div class="project-detail-header">
- <div class="projects-project-card-title">Project details</div>
- <div class="project-path" x-text="$store.projects.selectedProject.path"></div>
- </div> -->
<template x-if="!$store.projects.selectedProject._meta.creating">
<div class="projects-form-group">
<label class="projects-form-label">Folder name</label>
<span class="projects-form-description">Project files and settings are located in
- <strong>/a0/usr/projects/<span
- x-text="$store.projects.selectedProject.name"></span></strong></span>
- <input class="projects-form-input projects-disabled" type="text" x-model="$store.projects.selectedProject.name" disabled>
+ <strong><span
+ x-text="$store.projects.getSelectedAbsPath()"></span></strong></span>
+ <div class="projects-input-with-button-wrapper">
+ <input class="projects-form-input projects-disabled" type="text" x-model="$store.projects.selectedProject.name" disabled>
+ <button class="button icon-button" @click="$store.projects.browseSelected()">
+ <span class="icon material-symbols-outlined">folder</span>
+ <span>Browse</span>
+ </button>
+ </div>
</div>
</template>
@@ -147,6 +149,24 @@
.projects-disabled {
opacity: 0.7;
}
+
+ .projects-input-with-button-wrapper {
+ display: flex;
+ gap: 0.5em;
+ align-items: center;
+ }
+
+ .projects-input-with-button-wrapper input {
+ flex-grow: 1;
+ }
+
+ @media (max-width: 640px) {
+ .projects-input-with-button-wrapper {
+ flex-direction: column;
+ align-items: stretch;
+ }
+ }
+
</style>
</html>
\ No newline at end of file
diff --git a/webui/components/projects/project-edit-instructions.html b/webui/components/projects/project-edit-instructions.html
new file mode 100644
index 000000000..d3accb760
--- /dev/null
+++ b/webui/components/projects/project-edit-instructions.html
@@ -0,0 +1,58 @@
+<html>
+
+<head>
+ <title>Create a new project</title>
+ <script type="module">
+ import { store } from "/components/projects/projectsStore.js";
+ </script>
+</head>
+
+<body>
+ <div x-data>
+ <template x-if="$store.projects && $store.projects.selectedProject">
+ <div>
+ <!-- <div class="project-detail-header">
+ <div class="projects-project-card-title">Project details</div>
+ <div class="project-path" x-text="$store.projects.selectedProject.path"></div>
+ </div> -->
+
+ <div class="projects-form-group">
+ <label class="projects-form-label">Description</label>
+ <span class="projects-form-description">Describe the project. This helps both you and the agent
+ understand the project context.</span>
+ <textarea class="projects-form-textarea" x-model="$store.projects.selectedProject.description"
+ rows="5" placeholder="Enter project description"></textarea>
+ </div>
+
+ <div class="projects-form-group">
+ <label class="projects-form-label">Instructions</label>
+ <span class="projects-form-description">Provide specific instructions for the agent related to this
+ project.</span>
+ <textarea class="projects-form-textarea"
+ x-model="$store.projects.selectedProject.instructions" rows="15"
+ placeholder="Enter project instructions"></textarea>
+ </div>
+
+ <div class="projects-form-group">
+ <label class="projects-form-label">Instruction files</label>
+ <div class="projects-input-with-button-wrapper">
+ <span class="projects-form-description">Additional instruction files in <strong><span
+ x-text="$store.projects.getSelectedAbsPath('.a0proj','instructions')"></span></strong>. Only text files are currently supported (txt, md, yaml, no PDFs or images).</span>
+ <span class="projects-form-description">Currently there are <strong><span
+ x-text="$store.projects.selectedProject.instruction_files_count || 0"></span></strong> instruction files.</span>
+ <button class="button icon-button" x-on:click="$store.projects.browseInstructionFiles()">
+ <span class="icon material-symbols-outlined">folder</span>
+ <span>Browse</span>
+ </button>
+ </div>
+ </div>
+ </div>
+
+
+ </template>
+ </div>
+</body>
+<style>
+</style>
+
+</html>
\ No newline at end of file
diff --git a/webui/components/projects/project-edit-secrets.html b/webui/components/projects/project-edit-secrets.html
new file mode 100644
index 000000000..76c066750
--- /dev/null
+++ b/webui/components/projects/project-edit-secrets.html
@@ -0,0 +1,53 @@
+<html>
+
+<head>
+ <title>Create a new project</title>
+ <script type="module">
+ import { store } from "/components/projects/projectsStore.js";
+ </script>
+</head>
+
+<body>
+ <div x-data>
+ <template x-if="$store.projects && $store.projects.selectedProject">
+ <div>
+ <!-- <div class="project-detail-header">
+ <div class="projects-project-card-title">Project details</div>
+ <div class="project-path" x-text="$store.projects.selectedProject.path"></div>
+ </div> -->
+
+ <div class="projects-form-group">
+ <label class="projects-form-label">Non-sensitive variables</label>
+ <span class="projects-form-description">tore non-sensitive variables in .env format e.g.
+ EMAIL_IMAP_SERVER="imap.gmail.com", one item per line. You can use comments starting with # to
+ add descriptions for the agent. See <a
+ href="javascript:openModal('settings/secrets/example-vars.html')">example</a>.
+ <br>
+ These variables are visible to LLMs and in chat history, they are not being masked.</span>
+ <textarea class="projects-form-textarea" x-model="$store.projects.selectedProject.variables"
+ rows="10" placeholder="Enter project variables"></textarea>
+ </div>
+
+ <div class="projects-form-group">
+ <label class="projects-form-label">Sensitive variables</label>
+ <span class="projects-form-description">
+ Store secrets and credentials in .env format e.g. EMAIL_PASSWORD="s3cret-p4$$w0rd", one item per
+ line. You can use comments starting with # to add descriptions for the agent. See <a
+ href="javascript:openModal('settings/secrets/example-secrets.html')">example</a>.<br>These
+ variables are not visile to LLMs and in chat history, they are being masked. ⚠️ only values with
+ length &gt;= 4 are being masked to prevent false positives.
+ </span>
+ <textarea class="projects-form-textarea"
+ x-model="$store.projects.selectedProject.secrets" rows="10"
+ placeholder="Enter project secrets"></textarea>
+ </div>
+ </div>
+
+
+ </template>
+ </div>
+</body>
+<style>
+</style>
+
+</html>
\ No newline at end of file
diff --git a/webui/components/projects/project-edit.html b/webui/components/projects/project-edit.html
index e68049a88..ebdbce629 100644
--- a/webui/components/projects/project-edit.html
+++ b/webui/components/projects/project-edit.html
@@ -15,11 +15,14 @@
<div class="buttons-container" style="margin-top: 1em; margin-bottom: 2em;">
<div class="buttons-left">
- <button type="button" class="button cancel" @click="$store.projects.deleteProjectAndCloseModal()">Delete</button>
+ <button type="button" class="button cancel"
+ @click="$store.projects.deleteProjectAndCloseModal()">Delete</button>
</div>
<div class="buttons-right">
- <button type="button" class="button cancel" @click="$store.projects.cancelEdit()">Cancel</button>
- <button type="button" class="button confirm" @click="$store.projects.confirmEdit()">Save</button>
+ <button type="button" class="button cancel"
+ @click="$store.projects.cancelEdit()">Cancel</button>
+ <button type="button" class="button confirm"
+ @click="$store.projects.confirmEdit()">Save</button>
</div>
</div>
@@ -27,7 +30,7 @@
<div class="project-detail-header">
<span class="projects-project-card-title">Basic info</span>
</div>
- <x-component path="projects/project-basic-data-form.html">
+ <x-component path="projects/project-edit-basic-data.html">
</x-component>
</div>
@@ -35,17 +38,28 @@
<div class="project-detail-header">
<span class="projects-project-card-title">Instructions</span>
</div>
- <x-component path="projects/project-instructions-form.html">
+ <x-component path="projects/project-edit-instructions.html">
+ </x-component>
+ </div>
+
+ <div class="project-detail">
+ <div class="project-detail-header">
+ <span class="projects-project-card-title">Secrets</span>
+ </div>
+ <x-component path="projects/project-edit-secrets.html">
</x-component>
</div>
<div class="buttons-container" style="margin-top: 2em;">
<div class="buttons-left">
- <button type="button" class="button cancel" @click="$store.projects.deleteProjectAndCloseModal()">Delete</button>
+ <button type="button" class="button cancel"
+ @click="$store.projects.deleteProjectAndCloseModal()">Delete</button>
</div>
<div class="buttons-right">
- <button type="button" class="button cancel" @click="$store.projects.cancelEdit()">Cancel</button>
- <button type="button" class="button confirm" @click="$store.projects.confirmEdit()">Save</button>
+ <button type="button" class="button cancel"
+ @click="$store.projects.cancelEdit()">Cancel</button>
+ <button type="button" class="button confirm"
+ @click="$store.projects.confirmEdit()">Save</button>
</div>
</div>
diff --git a/webui/components/projects/project-instructions-form.html b/webui/components/projects/project-instructions-form.html
deleted file mode 100644
index f0d1e63c7..000000000
--- a/webui/components/projects/project-instructions-form.html
+++ /dev/null
@@ -1,41 +0,0 @@
-<html>
-
-<head>
- <title>Create a new project</title>
- <script type="module">
- import { store } from "/components/projects/projectsStore.js";
- </script>
-</head>
-
-<body>
- <div x-data>
- <template x-if="$store.projects && $store.projects.selectedProject">
- <div>
- <!-- <div class="project-detail-header">
- <div class="projects-project-card-title">Project details</div>
- <div class="project-path" x-text="$store.projects.selectedProject.path"></div>
- </div> -->
-
- <div class="projects-form-group">
- <label class="projects-form-label">Description</label>
- <span class="projects-form-description">Describe the project. This helps both you and the agent understand the project context.</span>
- <textarea class="projects-form-textarea" x-model="$store.projects.selectedProject.description"
- rows="5" placeholder="Enter project description"></textarea>
- </div>
-
- <div class="projects-form-group">
- <label class="projects-form-label">Instructions</label>
- <span class="projects-form-description">Provide specific instructions for the agent related to this project.</span>
- <textarea class="projects-form-textarea" style="min-height: 25em;" x-model="$store.projects.selectedProject.instructions"
- rows="5" placeholder="Enter project instructions"></textarea>
- </div>
- </div>
-
-
- </template>
- </div>
-</body>
-<style>
-</style>
-
-</html>
\ No newline at end of file
diff --git a/webui/components/projects/project-list.html b/webui/components/projects/project-list.html
index c654c2699..9b1ecf889 100644
--- a/webui/components/projects/project-list.html
+++ b/webui/components/projects/project-list.html
@@ -16,16 +16,16 @@
instructions and files.
You can create projects for your tasks and switch between them easily.</p>
<div class="projects-list-header">
- <div x-show="$store.chats.selectedContext?.project_path" class="active-project-display">
+ <div x-show="$store.chats.selectedContext?.project" class="active-project-display">
<span style="display: inline-flex; align-items: center; gap: 0.5em;">
<span>Active:</span>
- <span class="project-color-ball" :style="$store.chats.selectedContext.project_color ? { backgroundColor: $store.chats.selectedContext.project_color } : { border: '1px solid var(--color-border)' }"></span>
- <strong x-text="$store.chats.selectedContext?.project_name"></strong>
+ <span class="project-color-ball" :style="$store.chats.selectedContext?.project?.color ? { backgroundColor: $store.chats.selectedContext?.project?.color } : { border: '1px solid var(--color-border)' }"></span>
+ <strong x-text="$store.chats.selectedContext?.project?.name"></strong>
</span>
<button type="button" class="button cancel icon-button" title="Deactivate" @click="$store.projects.deactivateProject()">
<span class="icon material-symbols-outlined">close</span>
</button>
- <button type="button" class="button icon-button" title="Edit" @click="$store.projects.openEditModal($store.chats.selectedContext?.project_path)">
+ <button type="button" class="button icon-button" title="Edit" @click="$store.projects.openEditModal($store.chats.selectedContext?.project?.name)">
<span class="icon material-symbols-outlined">edit</span>
</button>
</div>
@@ -36,7 +36,7 @@
</div>
<template :key="project.name" x-for="project in $store.projects.projectList">
- <div class="projects-project-card" :class="{ 'projects-active': project.path === $store.chats.selectedContext?.project_path }">
+ <div class="projects-project-card" :class="{ 'projects-active': project.name === $store.chats.selectedContext?.project?.name }">
<div class="projects-project-card-header" style="display: flex; align-items: start; justify-content: space-between; gap: 1em;">
<div>
<div class="projects-project-card-title">
@@ -46,20 +46,20 @@
<div class="projects-project-card-name">/<span x-text="project.name"></span></div>
</div>
<div class="projects-project-card-actions" style="display: flex; gap: 0.5em;">
- <template x-if="project.path === $store.chats.selectedContext?.project_path">
+ <template x-if="project.name === $store.chats.selectedContext?.project?.name">
<button type="button" class="button cancel" title="Deactivate" style="width:9em" @click="$store.projects.deactivateProject()">
<span class="icon material-symbols-outlined">close</span> Deactivate
</button>
</template>
- <template x-if="project.path !== $store.chats.selectedContext?.project_path">
- <button type="button" class="button confirm" title="Activate" style="width:9em" @click="$store.projects.activateProject(project.path)">
+ <template x-if="project.name !== $store.chats.selectedContext?.project?.name">
+ <button type="button" class="button confirm" title="Activate" style="width:9em" @click="$store.projects.activateProject(project.name)">
<span class="icon material-symbols-outlined">play_arrow</span> Activate
</button>
</template>
- <button type="button" class="button icon-button" title="Edit" @click="$store.projects.openEditModal(project.path)">
+ <button type="button" class="button icon-button" title="Edit" @click="$store.projects.openEditModal(project.name)">
<span class="icon material-symbols-outlined">edit</span>
</button>
- <button type="button" class="button cancel icon-button" title="Delete" @click="$store.projects.deleteProject(project.path)">
+ <button type="button" class="button cancel icon-button" title="Delete" @click="$store.projects.deleteProject(project.name)">
<span class="icon material-symbols-outlined">delete</span>
</button>
</div>
diff --git a/webui/components/projects/project-selector.html b/webui/components/projects/project-selector.html
index ccdae79a5..deea9cb3a 100644
--- a/webui/components/projects/project-selector.html
+++ b/webui/components/projects/project-selector.html
@@ -11,13 +11,13 @@
<div x-data>
<template x-if="$store.projects && $store.chats">
<div>
- <template x-if="$store.chats.selectedContext?.project_name">
+ <template x-if="$store.chats.selectedContext?.project?.name">
<button @click="$store.projects.openProjectsModal()" type="button" class="button project-button">
- <span class="project-color-ball" :style="$store.chats.selectedContext.project_color ? { backgroundColor: $store.chats.selectedContext.project_color } : { border: '1px solid var(--color-border)' }"></span>
- <span x-text="$store.chats.selectedContext?.project_name"></span>
+ <span class="project-color-ball" :style="$store.chats.selectedContext.project.color ? { backgroundColor: $store.chats.selectedContext.project.color } : { border: '1px solid var(--color-border)' }"></span>
+ <span x-text="$store.chats.selectedContext?.project?.name"></span>
</button>
</template>
- <template x-if="!$store.chats.selectedContext?.project_name">
+ <template x-if="!$store.chats.selectedContext?.project?.name">
<button @click="$store.projects.openProjectsModal()" type="button" class="button project-button">
No project</button>
</template>
diff --git a/webui/components/projects/projectsStore.js b/webui/components/projects/projectsStore.js
index 60812f817..9978c8ae3 100644
--- a/webui/components/projects/projectsStore.js
+++ b/webui/components/projects/projectsStore.js
@@ -3,6 +3,7 @@ import * as api from "/js/api.js";
import * as modals from "/js/modals.js";
import * as notifications from "/components/notifications/notification-store.js";
import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
+import { store as browserStore } from "/components/modals/file-browser/file-browser-store.js";
const listModal = "projects/project-list.html";
const createModal = "projects/project-create.html";
@@ -48,16 +49,17 @@ const model = {
],
_toFolderName(str) {
- if (!str) return '';
+ if (!str) return "";
// a helper function to convert title to a folder safe name
- const s = str.normalize('NFD') // remove all diacritics and replace it with the latin character
- .replace(/[\u0300-\u036f]/g, '')
+ const s = str
+ .normalize("NFD") // remove all diacritics and replace it with the latin character
+ .replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
- .replace(/[^a-z0-9\s-]/g, '_') // replace all special symbols with _
- .replace(/\s+/g, '_') // replace spaces with _
- .replace(/_{2,}/g, '_') // condense multiple underscores into 1
- .replace(/^-+|-+$/g, '') // remove any leading and trailing underscores
- .replace(/^_+|_+$/g, '');
+ .replace(/[^a-z0-9\s-]/g, "_") // replace all special symbols with _
+ .replace(/\s+/g, "_") // replace spaces with _
+ .replace(/_{2,}/g, "_") // condense multiple underscores into 1
+ .replace(/^-+|-+$/g, "") // remove any leading and trailing underscores
+ .replace(/^_+|_+$/g, "");
return s;
},
@@ -72,8 +74,8 @@ const model = {
this.selectedProject = null;
},
- async openEditModal(projectPath) {
- this.selectedProject = await this._createEditProjectData(projectPath);
+ async openEditModal(name) {
+ this.selectedProject = await this._createEditProjectData(name);
await modals.openModal(editModal);
this.selectedProject = null;
},
@@ -92,7 +94,7 @@ const model = {
const project = await this.saveSelectedProject(true);
await this.loadProjectsList();
await modals.closeModal(createModal);
- await this.openEditModal(project.path);
+ await this.openEditModal(project.name);
},
async confirmEdit() {
@@ -101,12 +103,12 @@ const model = {
await modals.closeModal(editModal);
},
- async activateProject(projectPath) {
+ async activateProject(name) {
try {
await api.callJsonApi("projects", {
action: "activate",
context_id: chatsStore.getSelectedChatId(),
- path: projectPath,
+ name: name,
});
notifications.toastFrontendSuccess(
"Project activated successfully",
@@ -159,11 +161,11 @@ const model = {
},
async deleteProjectAndCloseModal() {
- await this.deleteProject(this.selectedProject.path);
+ await this.deleteProject(this.selectedProject.name);
await modals.closeModal(editModal);
},
- async deleteProject(projectPath) {
+ async deleteProject(name) {
// show confirmation dialog before proceeding
const confirmed = window.confirm(
"Are you sure you want to permanently delete this project? This action is irreversible and ALL FILES will be deleted."
@@ -172,7 +174,7 @@ const model = {
try {
const response = await api.callJsonApi("projects", {
action: "delete",
- path: projectPath,
+ name: name,
});
if (response.ok) {
notifications.toastFrontendSuccess(
@@ -211,7 +213,7 @@ const model = {
this.loading = true;
try {
const response = await api.callJsonApi("projects", {
- action: "list-active",
+ action: "list",
});
this.projectList = response.data || [];
} catch (error) {
@@ -281,14 +283,13 @@ const model = {
};
},
- async _createEditProjectData(projectPath) {
+ async _createEditProjectData(name) {
const projectData = (
await api.callJsonApi("projects", {
action: "load",
- path: projectPath,
+ name: name,
})
).data;
- // const project = this.projectList.find((p) => p.path === projectPath);
return {
_meta: {
creating: false,
@@ -296,6 +297,30 @@ const model = {
...projectData,
};
},
+
+ async browseSelected(...relPath) {
+ const path = this.getSelectedAbsPath(...relPath);
+ return await browserStore.open(path);
+ },
+
+ async browseInstructionFiles() {
+ await this.browseSelected(".a0proj", "instructions");
+ try {
+ const newData = await this._createEditProjectData(
+ this.selectedProject.name
+ );
+ this.selectedProject.instruction_files_count =
+ newData.instruction_files_count;
+ } catch (error) {
+ //pass
+ }
+ },
+
+ getSelectedAbsPath(...relPath) {
+ return ["/a0/usr/projects", this.selectedProject.name, ...relPath]
+ .join("/")
+ .replace(/\/+/g, "/");
+ },
};
// convert it to alpine store
diff --git a/webui/components/sidebar/chats/chats-list.html b/webui/components/sidebar/chats/chats-list.html
index c8b5077e3..f0daa14ad 100644
--- a/webui/components/sidebar/chats/chats-list.html
+++ b/webui/components/sidebar/chats/chats-list.html
@@ -1,9 +1,11 @@
<html>
+
<head>
<script type="module">
import { store } from "/components/sidebar/chats/chats-store.js";
</script>
</head>
+
<body>
<div x-data>
<template x-if="$store.chats">
@@ -13,10 +15,11 @@
<template x-for="context in $store.chats.contexts" :key="context.id">
<li>
<div :class="{'chat-container': true, 'chat-selected': context.id === $store.chats.selected}">
- <div class="chat-stripe" :style="'background-color: ' + (context.project_color || 'transparent')"></div>
+ <div class="chat-stripe" :style="'background-color: ' + (context.project?.color || 'transparent')">
+ </div>
<div class="chat-list-button" @click="$store.chats.selectChat(context.id)">
<span class="chat-name" :title="context.name ? context.name : 'Chat #' + context.no"
- x-text="context.name ? context.name : 'Chat #' + context.no"></span>
+ x-text="context.name ? context.name : 'Chat #' + context.no"></span>
</div>
<button class="edit-button" @click.stop="$store.chats.killChat(context.id)">X</button>
</div>
@@ -37,6 +40,7 @@
flex-shrink: 0;
z-index: 10;
}
+
/* Chats list container and items */
.chats-list-container {
display: flex;
@@ -45,23 +49,27 @@
min-height: 0;
margin-top: var(--spacing-md);
}
-
+
.chats-config-list {
flex: 1;
min-height: 0;
overflow: scroll;
scroll-behavior: smooth;
max-height: 100%;
- scrollbar-width: none; /* Firefox */
- -ms-overflow-style: none; /* IE/Edge */
+ scrollbar-width: none;
+ /* Firefox */
+ -ms-overflow-style: none;
+ /* IE/Edge */
}
+
/* Hide scrollbar for Chrome/Safari/Webkit */
.chats-config-list::-webkit-scrollbar {
display: none;
}
-
- .chat-container {
- position: relative; /* Anchor for the stripe */
+
+ .chat-container {
+ position: relative;
+ /* Anchor for the stripe */
display: flex;
align-items: center;
justify-content: space-between;
@@ -69,9 +77,11 @@
min-height: 40px;
border-radius: 4px;
transition: background-color 0.2s ease-in-out;
- overflow: hidden; /* Important for containing the stripe's corners */
+ overflow: hidden;
+ /* Important for containing the stripe's corners */
}
- .chat-container:hover {
+
+ .chat-container:hover {
background-color: rgba(255, 255, 255, 0.03);
}
@@ -80,7 +90,8 @@
align-items: center;
flex-grow: 1;
cursor: pointer;
- padding: 8px 8px 8px 10px; /* Top/Right/Bottom/Left - Left padding for stripe */
+ padding: 8px 8px 8px 10px;
+ /* Top/Right/Bottom/Left - Left padding for stripe */
overflow: hidden;
}
@@ -90,7 +101,8 @@
top: 0;
width: 5px;
height: 100%;
- border-radius: 0; /* No rounded corners */
+ border-radius: 0;
+ /* No rounded corners */
}
.chat-name {
@@ -103,34 +115,57 @@
.edit-button {
flex-shrink: 0;
margin-right: 8px;
- background-color: transparent;
- border: 1px solid var(--color-border);
- border-radius: 0.1875rem;
- color: var(--color-primary);
- cursor: pointer;
- padding: 0.125rem 0.5rem;
- transition: all var(--transition-speed) ease-in-out;
- width: 2rem;
+ background-color: transparent;
+ border: 1px solid var(--color-border);
+ border-radius: 0.1875rem;
+ color: var(--color-primary);
+ cursor: pointer;
+ padding: 0.125rem 0.5rem;
+ transition: all var(--transition-speed) ease-in-out;
+ width: 2rem;
height: 2rem;
}
- .edit-button:hover {
- border-color: var(--color-primary);
- background-color: #32455690;
+
+ .edit-button:hover {
+ border-color: var(--color-primary);
+ background-color: #32455690;
+ }
+
+ .edit-button:active {
+ background-color: #131a2090;
+ color: rgba(253, 253, 253, 0.35);
+ }
+
+ .empty-list-message {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ height: 100px;
+ color: var(--color-secondary);
+ text-align: center;
+ opacity: 0.7;
+ font-style: italic;
}
- .edit-button:active {
- background-color: #131a2090;
- color: rgba(253, 253, 253, 0.35);
+
+ .light-mode .empty-list-message {
+ color: var(--color-secondary-light);
}
-
- .empty-list-message { display: flex; justify-content: center; align-items: center; height: 100px; color: var(--color-secondary); text-align: center; opacity: 0.7; font-style: italic; }
- .light-mode .empty-list-message { color: var(--color-secondary-light); }
-
+
/* Selected chat accent */
- .chat-selected { font-weight:bold; background-color: var(--color-border) 0.2; }
+ .chat-selected {
+ background-color: rgba(var(--color-border-rgb, 50, 50, 50), 0.5);
+ }
+
+ .chat-selected .chat-name {
+ font-size: var(--font-size-normal);
+ font-weight: bold;
+ }
+
/* .chat-list-button.font-bold { position: relative; background-color: var(--color-border) 0.05; } */
/* .chat-list-button.font-bold::before { content: ""; position: absolute; left: 0; top: 0; height: 100%; width: 3px; background-color: var(--color-border); border-top-left-radius: 3px; border-bottom-left-radius: 3px; } */
/* .light-mode .chat-list-button.font-bold { background-color: var(--color-border) 0.05; } */
/* .light-mode .chat-list-button.font-bold::before { background-color: var(--color-border); } */
</style>
</body>
-</html>
+
+</html>
\ No newline at end of file
diff --git a/webui/components/sidebar/chats/chats-store.js b/webui/components/sidebar/chats/chats-store.js
index 9ca8a7cf2..3feb3d103 100644
--- a/webui/components/sidebar/chats/chats-store.js
+++ b/webui/components/sidebar/chats/chats-store.js
@@ -139,14 +139,26 @@ const model = {
// Create new chat
async newChat() {
try {
- if (globalThis.newContext) {
- globalThis.newContext();
- }
- if (globalThis.updateAfterScroll) {
- globalThis.updateAfterScroll();
+
+ // first create a new chat on the backend
+ const response = await sendJsonData("/chat_create", {
+ current_context: this.selected
+ });
+
+ if (response.ok) {
+ this.selectChat(response.ctxid);
+ return;
}
- // UX: scroll-to-top
- requestAnimationFrame(() => this._scrollChatsToTop());
+
+
+ // if (globalThis.newContext) {
+ // globalThis.newContext();
+ // }
+ // if (globalThis.updateAfterScroll) {
+ // globalThis.updateAfterScroll();
+ // }
+ // // UX: scroll-to-top
+ // requestAnimationFrame(() => this._scrollChatsToTop());
} catch (e) {
if (globalThis.toastFetchError) {
globalThis.toastFetchError("Error creating new chat", e);
diff --git a/webui/index.html b/webui/index.html
index ad400dd49..0ae2d1d62 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -149,25 +149,9 @@
<!-- Right Panel (Message History and Input Section) -->
<div id="right-panel" class="panel" >
- <!-- Time and Date -->
- <div id="time-date-container">
- <div id="time-date"></div>
- <div class="status-icon" x-data="{ connected: true }">
- <svg viewBox="0 0 30 30">
- <!-- Connected State (filled circle) -->
- <circle class="connected-circle" cx="15" cy="15" r="8"
- x-bind:fill="connected ? '#00c340' : 'none'" x-bind:opacity="connected ? 1 : 0" />
-
- <!-- Disconnected State (outline circle) -->
- <circle class="disconnected-circle" cx="15" cy="15" r="12" fill="none" stroke="#e40138"
- stroke-width="3" x-bind:opacity="connected ? 0 : 1" />
- </svg>
- </div>
- <!-- Notification Toggle positioned next to time-date -->
- <x-component path="notifications/notification-icons.html"></x-component>
- <!-- Project Selector -->
- <x-component path="projects/project-selector.html"></x-component>
- </div>
+
+ <!-- top section with time, status etc. -->
+ <x-component path="chat/top-section/chat-top.html"></x-component>
<!-- Welcome Screen Component -->
<div x-data x-show="$store.welcomeStore && $store.welcomeStore.isVisible">
diff --git a/webui/index.js b/webui/index.js
index 281651663..aeb693094 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -9,6 +9,7 @@ import { store as preferencesStore } from "/components/sidebar/bottom/preference
import { store as inputStore } from "/components/chat/input/input-store.js";
import { store as chatsStore } from "/components/sidebar/chats/chats-store.js";
import { store as tasksStore } from "/components/sidebar/tasks/tasks-store.js";
+import { store as chatTopStore } from "/components/chat/top-section/chat-top-store.js";
globalThis.fetchApi = api.fetchApi; // TODO - backward compatibility for non-modular scripts, remove once refactored to alpine
@@ -29,7 +30,6 @@ let autoScroll = true;
let context = null;
globalThis.resetCounter = 0; // Used by stores and getChatBasedId
let skipOneSpeech = false;
-let connectionStatus = undefined; // undefined = not checked yet, true = connected, false = disconnected
// Sidebar toggle logic is now handled by sidebar-store.js
@@ -153,7 +153,16 @@ export function updateChatInput(text) {
console.log("Updated chat input value:", chatInputEl.value);
}
-function updateUserTime() {
+async function updateUserTime() {
+
+ let userTimeElement = document.getElementById("time-date");
+
+ while(!userTimeElement){
+ await sleep(100);
+ userTimeElement = document.getElementById("time-date");
+ }
+
+
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
@@ -171,7 +180,6 @@ function updateUserTime() {
const dateString = now.toLocaleDateString(undefined, options);
// Update the HTML
- const userTimeElement = document.getElementById("time-date");
userTimeElement.innerHTML = `${timeString}<br><span id="user-date">${dateString}</span>`;
}
@@ -227,20 +235,21 @@ function generateGUID() {
}
function getConnectionStatus() {
- return connectionStatus;
+ return chatTopStore.connected;
}
globalThis.getConnectionStatus = getConnectionStatus;
function setConnectionStatus(connected) {
- connectionStatus = connected;
- // Broadcast connection status without touching Alpine directly
- try {
- window.dispatchEvent(
- new CustomEvent("connection-status", { detail: { connected } })
- );
- } catch (_e) {
- // no-op
- }
+ chatTopStore.connected = connected;
+ // connectionStatus = connected;
+ // // Broadcast connection status without touching Alpine directly
+ // try {
+ // window.dispatchEvent(
+ // new CustomEvent("connection-status", { detail: { connected } })
+ // );
+ // } catch (_e) {
+ // // no-op
+ // }
}
let lastLogVersion = 0;
diff --git a/webui/js/modals.js b/webui/js/modals.js
index 770e1967c..b147a25f7 100644
--- a/webui/js/modals.js
+++ b/webui/js/modals.js
@@ -9,13 +9,6 @@ const backdrop = document.createElement("div");
backdrop.className = "modal-backdrop";
backdrop.style.display = "none";
backdrop.style.backdropFilter = "blur(5px)";
-
-// Make sure we only close when clicking directly on the backdrop, not its children
-backdrop.addEventListener("click", (event) => {
- if (event.target === backdrop) {
- closeModal();
- }
-});
document.body.appendChild(backdrop);
// Function to update z-index for all modals and backdrop
@@ -55,12 +48,16 @@ function createModalElement(path) {
newModal.className = "modal";
newModal.path = path; // save name to the object
- // Add click handler to the modal element to close when clicking outside content
- newModal.addEventListener("click", (event) => {
- // Only close if clicking directly on the modal container, not its content
- if (event.target === newModal) {
+ // Add click handlers to only close modal if both mousedown and mouseup are on the modal container
+ let mouseDownTarget = null;
+ newModal.addEventListener("mousedown", (event) => {
+ mouseDownTarget = event.target;
+ });
+ newModal.addEventListener("mouseup", (event) => {
+ if (event.target === newModal && mouseDownTarget === newModal) {
closeModal();
}
+ mouseDownTarget = null;
});