69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
import sys
|
||
|
|
from typing import List, Optional
|
||
|
|
|
||
|
|
# Support both package imports and direct script execution.
|
||
|
|
if __package__ in (None, ""):
|
||
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||
|
|
|
||
|
|
from core_agent.compression import RollingContextCompressor
|
||
|
|
from core_agent.memory import SimpleMemoryStore
|
||
|
|
from core_agent.providers.base import AgentProvider
|
||
|
|
from core_agent.session import AgentRunResult, ConversationSession
|
||
|
|
|
||
|
|
|
||
|
|
class CoreAgent:
|
||
|
|
"""Factory-style facade around a reusable multi-turn conversation session."""
|
||
|
|
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
*,
|
||
|
|
provider: AgentProvider,
|
||
|
|
workspace: str | Path,
|
||
|
|
skill_dirs: Optional[List[str | Path]] = None,
|
||
|
|
tool_registry=None,
|
||
|
|
dispatcher=None,
|
||
|
|
system_prompt: Optional[str] = None,
|
||
|
|
max_iterations: int = 12,
|
||
|
|
max_history_turns: int = 5,
|
||
|
|
memory_store: Optional[SimpleMemoryStore] = None,
|
||
|
|
context_compressor: Optional[RollingContextCompressor] = None,
|
||
|
|
) -> None:
|
||
|
|
self.provider = provider
|
||
|
|
self.workspace = Path(workspace).resolve()
|
||
|
|
self.skill_dirs = list(skill_dirs or [self.workspace / "skills"])
|
||
|
|
self.tool_registry = tool_registry
|
||
|
|
self.dispatcher = dispatcher
|
||
|
|
self.system_prompt = system_prompt
|
||
|
|
self.max_iterations = max_iterations
|
||
|
|
self.max_history_turns = max_history_turns
|
||
|
|
self.memory_store = memory_store or SimpleMemoryStore(self.workspace / ".core_agent" / "memory.json")
|
||
|
|
self.context_compressor = context_compressor or RollingContextCompressor()
|
||
|
|
|
||
|
|
def run(self, user_message: str, *, active_skills: Optional[List[str]] = None) -> AgentRunResult:
|
||
|
|
session = self.new_session(active_skills=active_skills)
|
||
|
|
return session.ask(user_message)
|
||
|
|
|
||
|
|
def new_session(self, *, active_skills: Optional[List[str]] = None) -> ConversationSession:
|
||
|
|
return ConversationSession(
|
||
|
|
provider=self.provider,
|
||
|
|
workspace=self.workspace,
|
||
|
|
skill_dirs=self.skill_dirs,
|
||
|
|
tool_registry=self.tool_registry,
|
||
|
|
dispatcher=self.dispatcher,
|
||
|
|
system_prompt=self.system_prompt,
|
||
|
|
max_iterations=self.max_iterations,
|
||
|
|
max_history_turns=self.max_history_turns,
|
||
|
|
memory_store=self.memory_store,
|
||
|
|
context_compressor=self.context_compressor,
|
||
|
|
active_skills=active_skills,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
from core_agent.chat_cli import main
|
||
|
|
|
||
|
|
main()
|