Skip to content
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2c18d7c
feat: knowledge base for long-term memory (issue #1099)
bitloi Feb 1, 2026
d07d875
fix: use os.environ in knowledge_base for testability; add unit and A…
bitloi Feb 1, 2026
f45ca7c
Merge origin/main into feature/knowledge-base-1099
bitloi Feb 1, 2026
ece99c9
fix: resolve chat_service.py conflict with main (keep KB integration)
bitloi Feb 1, 2026
852ab43
Merge upstream/main, resolve chat_service.py (keep KB integration)
bitloi Feb 1, 2026
f678309
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 1, 2026
3b9c0e4
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 1, 2026
3d10e6d
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 2, 2026
47f4f10
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 3, 2026
fbd480b
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 3, 2026
b7af46a
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 3, 2026
7216ce8
fix: resolve merge conflicts with main (router.py, chat_service.py)
bitloi Feb 4, 2026
4e2dcb2
Merge upstream/main into feature/knowledge-base-1099, resolve conflicts
bitloi Feb 4, 2026
c363187
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 4, 2026
3d2f4b6
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 4, 2026
d328676
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 4, 2026
b2fd959
PR feedback: rename to sqlite_toolkit, FTS5/BM25 search, add tool onl…
bitloi Feb 4, 2026
70cd6df
Remove knowledge base from developer agent for now (per review)
bitloi Feb 4, 2026
b4bfc4e
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 4, 2026
e195c47
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 5, 2026
dd804f2
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 5, 2026
606533f
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 5, 2026
06d19b1
Merge branch 'main' into feature/knowledge-base-1099
Wendong-Fan Feb 5, 2026
b4fba05
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 5, 2026
1b0f125
PR feedback: remove KB from chat context, rename tool to store_projec…
bitloi Feb 6, 2026
e134793
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 6, 2026
52d06b2
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 6, 2026
3086710
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 6, 2026
a275992
Merge upstream/main into feature/knowledge-base-1099
bitloi Feb 7, 2026
ba023f5
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi Feb 7, 2026
8d8b573
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 7, 2026
3564835
refactor(knowledge-base): switch from SQLite to markdown file-based m…
bitloi Feb 8, 2026
60367cb
Merge branch 'main' into feature/knowledge-base-1099
nitpicker55555 Feb 8, 2026
b2d810a
refactor(memory): index-only prompt, no dedicated tools (reviewer fee…
bitloi Feb 9, 2026
3bd0880
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi Feb 9, 2026
0971ae6
Address nitpicker55555 review: remove unused memory helpers, wire pro…
bitloi Feb 9, 2026
4cbdaf2
chore(backend): remove ruff from dev dependencies
bitloi Feb 9, 2026
7e7991e
Merge branch 'main' into feature/knowledge-base-1099
bitloi Feb 9, 2026
516caa2
Replace knowledge_base_toolkit with use_project_memory flag
bitloi Feb 9, 2026
d000b44
Merge branch 'feature/knowledge-base-1099' of https://github.com/bitl…
bitloi Feb 9, 2026
dafac62
Revert linter-only changes in router.py (review feedback)
bitloi Feb 9, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions backend/app/agent/toolkit/knowledge_base_toolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========

"""
Toolkit for long-term memory using markdown files (issue #1099).

Agents can both read and write to the project's memory file (.eigent/memory.md).
This provides a simple, file-based approach for persistent knowledge storage.
"""

from __future__ import annotations

import logging
import os
from typing import Final

from camel.toolkits.base import BaseToolkit
from camel.toolkits.function_tool import FunctionTool

from app.agent.toolkit.abstract_toolkit import AbstractToolkit
from app.component.environment import env
from app.utils.memory_file import append_memory, read_memory

logger = logging.getLogger(__name__)

_DEFAULT_WORKING_DIR: Final[str] = "~/.eigent"
_NO_MEMORY_MESSAGE: Final[str] = (
"No project memory exists yet. Use remember_this to save information."
)
_SUCCESS_MESSAGE: Final[str] = (
"Saved to project memory (.eigent/memory.md). "
"This will be available in future conversations."
)
_FAILURE_MESSAGE: Final[str] = "Failed to save to project memory. Please try again."


def _resolve_working_directory(working_directory: str | None) -> str:
"""Resolve and validate the working directory path."""
if working_directory is None:
working_directory = env("file_save_path", os.path.expanduser(_DEFAULT_WORKING_DIR))
resolved = os.path.expanduser(working_directory)
os.makedirs(resolved, exist_ok=True)
return resolved


class KnowledgeBaseToolkit(BaseToolkit, AbstractToolkit):
"""Toolkit for reading and writing project long-term memory.

Uses a simple markdown file (.eigent/memory.md) in the project's working
directory. Agents can both read existing memories and write new ones.
"""

def __init__(
self,
api_task_id: str,
working_directory: str | None = None,
agent_name: str | None = None,
timeout: float | None = None,
) -> None:
if not api_task_id or not api_task_id.strip():
raise ValueError("api_task_id cannot be empty")

super().__init__(timeout=timeout)
self.api_task_id = api_task_id.strip()
self.working_directory = _resolve_working_directory(working_directory)
self.agent_name = agent_name.strip() if agent_name else "agent"

logger.debug(
"KnowledgeBaseToolkit initialized",
extra={
"api_task_id": self.api_task_id,
"working_directory": self.working_directory,
"agent_name": self.agent_name,
},
)

def read_project_memory(self) -> str:
"""Read the project's long-term memory file.

Returns the content of .eigent/memory.md which contains facts,
preferences, and decisions that should persist across sessions.

Returns:
str: The memory file content, or a message if no memory exists yet.
"""
try:
content = read_memory(self.working_directory)
if content is None:
return _NO_MEMORY_MESSAGE
return content
except Exception as e:
logger.error(
f"Error reading project memory: {e}",
extra={"working_directory": self.working_directory},
)
return _NO_MEMORY_MESSAGE

def remember_this(self, content: str) -> str:
"""Save a fact or piece of information to the project's long-term memory.

Use this when the user or task establishes something that should be
remembered for future conversations (e.g. preferences, decisions,
project-specific facts). The content will be appended to .eigent/memory.md.

Args:
content (str): The information to remember (clear, self-contained text).

Returns:
str: Confirmation message indicating success or failure.
"""
if not content or not content.strip():
return "Cannot save empty content. Please provide information to remember."

try:
success = append_memory(self.working_directory, content)
if success:
logger.info(
"Memory saved successfully",
extra={
"api_task_id": self.api_task_id,
"content_length": len(content),
},
)
return _SUCCESS_MESSAGE
return _FAILURE_MESSAGE
except Exception as e:
logger.error(
f"Error saving to project memory: {e}",
extra={"working_directory": self.working_directory},
)
return _FAILURE_MESSAGE

def get_tools(self) -> list[FunctionTool]:
"""Return the list of tools provided by this toolkit."""
return [
FunctionTool(self.read_project_memory),
FunctionTool(self.remember_this),
]


def get_tools(
api_task_id: str,
working_directory: str | None = None,
agent_name: str | None = None,
) -> list[FunctionTool]:
"""Return the memory tools for use by an agent."""
return KnowledgeBaseToolkit(
api_task_id=api_task_id,
working_directory=working_directory,
agent_name=agent_name,
).get_tools()
2 changes: 2 additions & 0 deletions backend/app/agent/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from app.agent.toolkit.google_drive_mcp_toolkit import GoogleDriveMCPToolkit
from app.agent.toolkit.google_gmail_mcp_toolkit import GoogleGmailMCPToolkit
from app.agent.toolkit.image_analysis_toolkit import ImageAnalysisToolkit
from app.agent.toolkit.knowledge_base_toolkit import KnowledgeBaseToolkit
from app.agent.toolkit.lark_toolkit import LarkToolkit
from app.agent.toolkit.linkedin_toolkit import LinkedInToolkit
from app.agent.toolkit.mcp_search_toolkit import McpSearchToolkit
Expand Down Expand Up @@ -63,6 +64,7 @@ async def get_toolkits(tools: list[str], agent_name: str, api_task_id: str):
"google_drive_mcp_toolkit": GoogleDriveMCPToolkit,
"google_gmail_mcp_toolkit": GoogleGmailMCPToolkit,
"image_analysis_toolkit": ImageAnalysisToolkit,
"knowledge_base_toolkit": KnowledgeBaseToolkit,
"linkedin_toolkit": LinkedInToolkit,
"lark_toolkit": LarkToolkit,
"mcp_search_toolkit": McpSearchToolkit,
Expand Down
7 changes: 2 additions & 5 deletions backend/app/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
"""
Centralized router registration for the Eigent API.
All routers are explicitly registered here
for better visibility and maintainability.
All routers are explicitly registered here for better visibility and maintainability.
"""

import logging
Expand Down Expand Up @@ -79,9 +78,7 @@ def register_routers(app: FastAPI, prefix: str = "") -> None:
)
route_count = len(config["router"].routes)
logger.info(
f"Registered {config['tags'][0]} router:"
f" {route_count} routes -"
f" {config['description']}"
f"Registered {config['tags'][0]} router: {route_count} routes - {config['description']}"
)

logger.info(f"Total routers registered: {len(routers_config)}")
14 changes: 8 additions & 6 deletions backend/app/service/chat_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ def check_conversation_history_length(


def build_conversation_context(
task_lock: TaskLock, header: str = "=== CONVERSATION HISTORY ==="
task_lock: TaskLock,
header: str = "=== CONVERSATION HISTORY ===",
) -> str:
"""Build conversation context from task_lock history
with files listed only once at the end.
Expand All @@ -245,14 +246,13 @@ def build_conversation_context(
header: Header text for the context section

Returns:
Formatted context string with task history
and files listed once at the end
Formatted context string with task history and files listed once at the end
"""
context = ""
working_directories = set() # Collect all unique working directories

if task_lock.conversation_history:
context = f"{header}\n"
context += f"{header}\n"

for entry in task_lock.conversation_history:
if entry["role"] == "task_result":
Expand Down Expand Up @@ -553,7 +553,8 @@ async def step_solve(options: Chat, request: Request, task_lock: TaskLock):
"without workforce"
)
conv_ctx = build_conversation_context(
task_lock, header="=== Previous Conversation ==="
task_lock,
header="=== Previous Conversation ===",
)
simple_answer_prompt = (
f"{conv_ctx}"
Expand Down Expand Up @@ -1967,7 +1968,8 @@ async def question_confirm(
context_prompt = ""
if task_lock:
context_prompt = build_conversation_context(
task_lock, header="=== Previous Conversation ==="
task_lock,
header="=== Previous Conversation ===",
)

full_prompt = f"""{context_prompt}User Query: {prompt}
Expand Down
Loading