Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
1. add token use; (#733)
yeshion23333a88927b4a107
diff --git a/bot/vikingbot/agent/context.py b/bot/vikingbot/agent/context.pyindex 0af9b0aa6..9fb016918 100644--- a/bot/vikingbot/agent/context.py+++ b/bot/vikingbot/agent/context.py@@ -272,14 +272,14 @@ Always be helpful, accurate, and concise. When using tools, think step by step:if not self._eval:messages.extend(history)+ # User+ user_info = await self._build_user_memory(session_key, current_message, history)+ messages.append({"role": "user", "content": user_info})+# Current message (with optional image attachments)user_content = self._build_user_content(current_message, media)messages.append({"role": "user", "content": user_content})- # User- user_info = await self._build_user_memory(session_key, current_message, history)- messages.append({"role": "system", "content": user_info})-return messagesdef _build_user_content(self, text: str, media: list[str] | None) -> str | list[dict[str, Any]]:diff --git a/bot/vikingbot/agent/loop.py b/bot/vikingbot/agent/loop.pyindex b72dd98f0..87d29d394 100644--- a/bot/vikingbot/agent/loop.py+++ b/bot/vikingbot/agent/loop.py@@ -129,11 +129,6 @@ class AgentLoop:self._running = Falseself._register_default_tools()- self._token_usage = {- "prompt_tokens": 0,- "completion_tokens": 0,- "total_tokens": 0,- }async def _publish_thinking_event(self, session_key: SessionKey, event_type: OutboundEventType, content: str@@ -221,7 +216,7 @@ class AgentLoop:session_key: SessionKey,publish_events: bool = True,sender_id: str | None = None,- ) -> tuple[str | None, list[dict]]:+ ) -> tuple[str | None, list[dict], dict[str, int]]:"""Run the core agent loop: call LLM, execute tools, repeat until done.@@ -236,6 +231,11 @@ class AgentLoop:iteration = 0final_content = Nonetools_used: list[dict] = []+ token_usage = {+ "prompt_tokens": 0,+ "completion_tokens": 0,+ "total_tokens": 0,+ }while iteration < self.max_iterations:iteration += 1@@ -257,9 +257,9 @@ class AgentLoop:)if response.usage:cur_token = response.usage- self._token_usage["prompt_tokens"] += cur_token["prompt_tokens"]- self._token_usage["completion_tokens"] += cur_token["completion_tokens"]- self._token_usage["total_tokens"] += cur_token["total_tokens"]+ token_usage["prompt_tokens"] += cur_token["prompt_tokens"]+ token_usage["completion_tokens"] += cur_token["completion_tokens"]+ token_usage["total_tokens"] += cur_token["total_tokens"]if publish_events and response.reasoning_content:await self.bus.publish_outbound(@@ -362,7 +362,7 @@ class AgentLoop:else:final_content = "I've completed processing but have no response to give."- return final_content, tools_used+ return final_content, tools_used, token_usage@trace(name="process_message",@@ -489,7 +489,7 @@ class AgentLoop:# logger.info(f"New messages: {messages}")# Run agent loop- final_content, tools_used = await self._run_agent_loop(+ final_content, tools_used, token_usage = await self._run_agent_loop(messages=messages,session_key=session_key,publish_events=True,@@ -503,7 +503,8 @@ class AgentLoop:# Save to session (include tool names so consolidation sees what happened)session.add_message("user", msg.content, sender_id=msg.sender_id)session.add_message(- "assistant", final_content, tools_used=tools_used if tools_used else None+ "assistant", final_content, tools_used=tools_used if tools_used else None, token_usage=token_usage,+ sender_id=msg.sender_id,)await self.sessions.save(session)@@ -512,7 +513,7 @@ class AgentLoop:session_key=msg.session_key,content=final_content,metadata=msg.metadata,- token_usage=self._token_usage,+ token_usage=token_usage,time_cost=time_costor {}, # Pass through for channel-specific needs (e.g. Slack thread_ts))@@ -541,7 +542,7 @@ class AgentLoop:)# Run agent loop (no events published)- final_content, tools_used = await self._run_agent_loop(+ final_content, tools_used, token_usage = await self._run_agent_loop(messages=messages,session_key=msg.session_key,publish_events=False,diff --git a/bot/vikingbot/agent/memory.py b/bot/vikingbot/agent/memory.pyindex e7817839d..8e47adcd7 100644--- a/bot/vikingbot/agent/memory.py+++ b/bot/vikingbot/agent/memory.py@@ -48,21 +48,25 @@ class MemoryStore:return f"## Long-term Memory\n{long_term}" if long_term else ""async def get_viking_memory_context(self, current_message: str, workspace_id: str) -> str:- client = await VikingClient.create(agent_id=workspace_id)- admin_user_id = load_config().ov_server.admin_user_id- start = time.time()- result = await client.search_memory(current_message, user_id=admin_user_id, limit=3)- cost = round(time.time() - start, 2)- if not result:- logger.info(f"[USER_MEMORY]: search failed. cost {cost}")+ try:+ client = await VikingClient.create(agent_id=workspace_id)+ admin_user_id = load_config().ov_server.admin_user_id+ start = time.time()+ result = await client.search_memory(current_message, user_id=admin_user_id, limit=3)+ cost = round(time.time() - start, 2)+ if not result:+ logger.info(f"[USER_MEMORY]: search failed. cost {cost}")+ return ""+ user_memory = self._parse_viking_memory(result["user_memory"])+ agent_memory = self._parse_viking_memory(result["agent_memory"])+ logger.info(f"[USER_MEMORY]: search success. res: {user_memory[:100]}. cost {cost}")+ return (+ f"### user memories:\n{user_memory}\n"+ f"### agent memories:\n{agent_memory}"+ )+ except Exception as e:+ logger.error(f"[USER_MEMORY]: search failed. {e}")return ""- user_memory = self._parse_viking_memory(result["user_memory"])- agent_memory = self._parse_viking_memory(result["agent_memory"])- logger.info(f"[USER_MEMORY]: search success. res: {user_memory[:100]}. cost {cost}")- return (- f"### user memories:\n{user_memory}\n"- f"### agent memories:\n{agent_memory}"- )async def get_viking_user_profile(self, workspace_id: str, user_id: str) -> str:client = await VikingClient.create(agent_id=workspace_id)diff --git a/bot/vikingbot/session/manager.py b/bot/vikingbot/session/manager.pyindex 32087f682..971f39f42 100644--- a/bot/vikingbot/session/manager.py+++ b/bot/vikingbot/session/manager.py@@ -29,12 +29,14 @@ class Session:metadata: dict[str, Any] = field(default_factory=dict)def add_message(- self, role: str, content: str, sender_id: str | None = None, **kwargs: Any+ self, role: str, content: str, sender_id: str | None = None, token_usage: dict[str, Any] = None, **kwargs: Any) -> None:"""Add a message to the session."""msg = {"role": role, "content": content, "timestamp": datetime.now().isoformat(), **kwargs}if sender_id is not None:msg["sender_id"] = sender_id+ if token_usage is not None:+ msg["token_usage"] = token_usageself.messages.append(msg)self.updated_at = datetime.now()