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
feat(bot):add multi read tool, opt loop memory, fix agent memory search, update README (#895)
yeshion23333b12a0b05aaa3
diff --git a/bot/README.md b/bot/README.mdindex 615b3dd3d..9ba3e0ba1 100644--- a/bot/README.md+++ b/bot/README.md@@ -173,6 +173,8 @@ All configurations are under the `bot` field in `ov.conf`, with default values f- `ov_server`: OpenViking Server configuration.- If not configured, the OpenViking server information configured in `ov.conf` is used by default- If you don't use the locally started OpenViking Server, you can configure the url and the corresponding root user's API Key here+ - root_api_key: In a multi-tenant scenario, the API KEY must have root privileges; otherwise, the bot cannot automatically register multiple OpenViking users, which is used to implement memory isolation.+ - account_id: Defaults to default, which is the account ID of OpenViking. All users under an OpenViking account share resources.- `channels`: Message platform configuration, see [Message Platform Configuration](bot/docs/CHANNEL.md) for details```jsondiff --git a/bot/README_CN.md b/bot/README_CN.mdindex 84651c469..d9b130687 100644--- a/bot/README_CN.md+++ b/bot/README_CN.md@@ -8,7 +8,7 @@Vikingbot 深度集成 OpenViking,提供强大的知识管理和记忆检索能力:- **本地/远程双模式**:支持本地存储(`~/.openviking/data/`)和远程服务器模式-- **7 个专用 Agent 工具**:资源管理、语义搜索、正则搜索、通配符搜索、记忆搜索+- **7 个专用 Agent 工具**:资源管理、语义搜索、正则搜索、通配符搜索、记忆提交- **三级内容访问**:L0(摘要)、L1(概览)、L2(完整内容)- **会话记忆自动提交**:对话历史自动保存到 OpenViking- **模型配置**:从 OpenViking 配置(`vlm` 部分)读取,无需在 bot 配置中单独设置 provider@@ -176,6 +176,8 @@ bot将连接远程的OpenViking服务器,使用前需启动Openviking Server- ov_server:OpenViking Server 配置。- 不配置,默认使用`ov.conf`中配置的OpenViking server信息- 若不使用本地启动的OpenViking Server,可在此配置url和对应的root user的API Key+ - root_api_key: 多租户场景API KEY必须有root权限,否则bot无法自动注册多个OpenViking用户,用于实现memory的隔离+ - account_id: 默认default,ov的账号ID,OpenViking account下所有user共享resources- channels:消息平台配置,详见 [消息平台配置](bot/docs/CHANNEL.md)```jsondiff --git a/bot/eval/locomo/judge.py b/bot/eval/locomo/judge.pyindex bd76528ef..ae93cc8e0 100644--- a/bot/eval/locomo/judge.py+++ b/bot/eval/locomo/judge.py@@ -88,7 +88,7 @@ async def main():)parser.add_argument("--input",- default="./result/locomo_qa_result.csv",+ default="./result/locomo_qa_result_only_sys_memory.csv",help="Path to QA result csv file, default: ./result/locomo_qa_result.csv",)parser.add_argument(diff --git a/bot/eval/locomo/run_eval.py b/bot/eval/locomo/run_eval.pyindex a5f580a6e..1799aec49 100644--- a/bot/eval/locomo/run_eval.py+++ b/bot/eval/locomo/run_eval.py@@ -5,12 +5,39 @@ import timeimport csvimport osimport re+import threading+from concurrent.futures import ThreadPoolExecutor, as_completed+++def load_csv_qa(input_path: str, count: int | None = None) -> list[dict]:+ """从CSV文件加载QA数据,取sample_id和question字段"""+ qa_list = []+ with open(input_path, "r", encoding="utf-8", newline="") as f:+ reader = csv.DictReader(f)+ for row in reader:+ qa_list.append(+ {+ "sample_id": row.get("sample_id", ""),+ "question": row.get("question", ""),+ "answer": row.get("answer", ""),+ "category": "",+ "evidence": [],+ }+ )++ if count is not None:+ qa_list = qa_list[:count]+ return qa_listdef load_locomo_qa(input_path: str, sample_index: int | None = None, count: int | None = None) -> list[dict]:- """加载LoCoMo数据集的QA部分,逻辑同原eval.py"""+ """加载LoCoMo数据集的QA部分,支持JSON和CSV格式"""+ if input_path.lower().endswith(".csv"):+ return load_csv_qa(input_path, count)++ # 原有JSON格式处理逻辑with open(input_path, "r", encoding="utf-8") as f:data = json.load(f)@@ -40,8 +67,8 @@ def load_locomo_qa(return qa_list-def run_vikingbot_chat(question: str) -> tuple[str, dict, float]:- """执行vikingbot chat命令,返回回答、token使用情况、耗时(秒)"""+def run_vikingbot_chat(question: str) -> tuple[str, dict, float, int, list]:+ """执行vikingbot chat命令,返回回答、token使用情况、耗时(秒)、迭代次数、使用的工具列表"""input = f"Answer the question directly: {question}"cmd = ["vikingbot", "chat", "-m", input, "-e"]start_time = time.time()@@ -59,15 +86,21 @@ def run_vikingbot_chat(question: str) -> tuple[str, dict, float]:"token_usage", {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0})time_cost = resp_json.get("time_cost", time_cost)+ iteration = resp_json.get("iteration", 0)+ tools_used_names = resp_json.get("tools_used_names", [])except (json.JSONDecodeError, ValueError) as e:response = f"[PARSE ERROR] {output}"token_usage = {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0}- return response, token_usage, time_cost+ iteration = 0+ tools_used_names = []+ return response, token_usage, time_cost, iteration, tools_used_namesexcept subprocess.CalledProcessError as e:return (f"[CMD ERROR] {e.stderr}",{"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},0,+ 0,+ [],)except subprocess.TimeoutExpired:time_cost = 0@@ -75,6 +108,8 @@ def run_vikingbot_chat(question: str) -> tuple[str, dict, float]:"[TIMEOUT]",{"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},time_cost,+ 0,+ [],)@@ -111,6 +146,9 @@ def main():parser.add_argument("--count", type=int, default=None, help="Number of QA questions to run, default all")+ parser.add_argument(+ "--threads", type=int, default=5, help="Number of concurrent threads, default: 5"+ )args = parser.parse_args()# 确保输出目录存在@@ -134,10 +172,15 @@ def main():"response","token_usage","time_cost",+ "iteration",+ "tools_used_names","result",]# 打开CSV文件,不存在则创建写表头,存在则追加file_exists = os.path.exists(args.output)+ # 创建线程锁,确保多线程写文件安全+ write_lock = threading.Lock()+with open(args.output, "a+", encoding="utf-8", newline="") as f:writer = csv.DictWriter(f, fieldnames=fieldnames)if not file_exists:@@ -145,15 +188,18 @@ def main():f.flush()processed_count = len(processed_questions)- for idx, qa_item in enumerate(qa_list, 1):- question = qa_item["question"]- if question in processed_questions:- print(f"Skipping {idx}/{total}: already processed")- continue+ # 过滤掉已经处理过的问题+ remaining_qa = [qa for qa in qa_list if qa["question"] not in processed_questions]+ remaining_count = len(remaining_qa)+ print(f"Starting evaluation with {args.threads} concurrent threads, {remaining_count} questions to process")+ def process_qa(qa_item, idx, total_count):+ """单个QA处理函数,供多线程调用"""+ question = qa_item["question"]answer = qa_item["answer"]- print(f"Processing {idx}/{total}: {question[:60]}...")- response, token_usage, time_cost = run_vikingbot_chat(question)+ print(f"Processing {idx}/{total_count}: {question[:60]}...")++ response, token_usage, time_cost, iteration, tools_used_names = run_vikingbot_chat(question)row = {"sample_id": qa_item["sample_id"],@@ -162,13 +208,34 @@ def main():"response": response,"token_usage": json.dumps(token_usage, ensure_ascii=False),"time_cost": round(time_cost, 2),+ "iteration": iteration,+ "tools_used_names": json.dumps(tools_used_names, ensure_ascii=False),"result": "",}- writer.writerow(row)- f.flush()- processed_questions.add(question)- processed_count += 1- print(f"Completed {processed_count}/{total}, time cost: {round(time_cost, 2)}s")++ # 线程安全的文件写入+ with write_lock:+ nonlocal processed_count+ writer.writerow(row)+ f.flush()+ processed_questions.add(question)+ processed_count += 1+ print(f"Completed {processed_count}/{total}, time cost: {round(time_cost, 2)}s")+ return True++ # 使用线程池处理+ with ThreadPoolExecutor(max_workers=args.threads) as executor:+ # 提交所有任务+ futures = []+ for idx, qa_item in enumerate(remaining_qa, 1):+ futures.append(executor.submit(process_qa, qa_item, idx, remaining_count))++ # 等待所有任务完成+ for future in as_completed(futures):+ try:+ future.result()+ except Exception as e:+ print(f"Error processing QA item: {str(e)}")print(f"Evaluation completed, results saved to {args.output}")diff --git a/bot/eval/locomo/stat_judge_result.py b/bot/eval/locomo/stat_judge_result.pyindex 1c4ece6f2..2d7ebd8d6 100644--- a/bot/eval/locomo/stat_judge_result.py+++ b/bot/eval/locomo/stat_judge_result.py@@ -8,7 +8,7 @@ def main():parser = argparse.ArgumentParser(description="Statistics for judge result csv")parser.add_argument("--input",- default="./result/judge_result.csv",+ default="./result/locomo_qa_result_only_sys_memory.csv",help="Path to judge result csv file, default: ./result/judge_result.csv",)args = parser.parse_args()@@ -24,6 +24,7 @@ def main():total_completion_tokens = 0total_tokens = 0valid_rows = 0+ total_iteration = 0with open(args.input, "r", encoding="utf-8", newline="") as f:reader = csv.DictReader(f)@@ -36,6 +37,7 @@ def main():elif result == "WRONG":wrong += 1+ total_iteration += int(row.get("iteration", "0"))# 统计耗时time_cost = row.get("time_cost", "")if time_cost:@@ -67,6 +69,7 @@ def main():f"Wrong: {wrong}",f"Accuracy: {accuracy:.2%}",f"\nAverage time cost: {avg_time:.2f}s",+ f"\nAverage iteration: {total_iteration / valid_rows if valid_rows > 0 else 0.0:.2f}",f"\nToken usage:",f" Total prompt tokens: {total_prompt_tokens}",f" Total completion tokens: {total_completion_tokens}",diff --git a/bot/vikingbot/agent/context.py b/bot/vikingbot/agent/context.pyindex 9fb016918..70ea74823 100644--- a/bot/vikingbot/agent/context.py+++ b/bot/vikingbot/agent/context.py@@ -149,7 +149,7 @@ Skills with available="false" need dependencies installed first - you can try inreturn "\n\n---\n\n".join(parts)async def _build_user_memory(- self, session_key: SessionKey, current_message: str, history: list[dict[str, Any]]+ self, session_key: SessionKey, current_message: str, sender_id: str) -> str:"""Build the system prompt from bootstrap files, memory, and skills.@@ -170,7 +170,7 @@ Skills with available="false" need dependencies installed first - you can try in# Viking agent memorystart = _time.time()viking_memory = await self.memory.get_viking_memory_context(- current_message=current_message, workspace_id=workspace_id+ current_message=current_message, workspace_id=workspace_id, sender_id=sender_id)cost = round(_time.time() - start, 2)logger.info(@@ -178,7 +178,9 @@ Skills with available="false" need dependencies installed first - you can try in)if viking_memory:parts.append(- f"## Your memories about the current conversation. If you need to know more details, please use the tools.\n{viking_memory}"+ f"## Long term memory about this conversation.\n"+ f"You do not need to use tool to search again:\n"+ f"{viking_memory}")return "\n\n---\n\n".join(parts)@@ -215,8 +217,6 @@ You have access to tools that allow you to:You have two workspaces:1. Local workspace: {workspace_display}2. OpenViking workspace: managed via OpenViking tools-- Long-term memory: using user_memory_search tool to search memory-- History log: tow types, a. using user_memory_search tool to search history; b. memory/HISTORY.md (grep-searchable)- Custom skills: {workspace_display}/skills/{{skill-name}}/SKILL.mdIMPORTANT: When responding to direct questions or conversations, reply directly with your text response.@@ -226,8 +226,7 @@ For normal conversation, just respond with text - do not call the message tool.Always be helpful, accurate, and concise. When using tools, think step by step: what you know, what you need, and why you chose this tool.## Memory-- Remember important facts: using openviking_memory_commit tool to commit-- Recall past events: prioritize using user_memory_search tool to search history"""+- Remember important facts: using openviking_memory_commit tool to commit"""def _load_bootstrap_files(self) -> str:"""Load all bootstrap files from workspace."""@@ -273,7 +272,7 @@ Always be helpful, accurate, and concise. When using tools, think step by step:messages.extend(history)# User- user_info = await self._build_user_memory(session_key, current_message, history)+ user_info = await self._build_user_memory(session_key, current_message, self._sender_id)messages.append({"role": "user", "content": user_info})# Current message (with optional image attachments)diff --git a/bot/vikingbot/agent/loop.py b/bot/vikingbot/agent/loop.pyindex c34ab6e8b..ea2395674 100644--- a/bot/vikingbot/agent/loop.py+++ b/bot/vikingbot/agent/loop.py@@ -217,7 +217,7 @@ class AgentLoop:session_key: SessionKey,publish_events: bool = True,sender_id: str | None = None,- ) -> tuple[str | None, list[dict], dict[str, int]]:+ ) -> tuple[str | None, list[dict], dict[str, int], int]:"""Run the core agent loop: call LLM, execute tools, repeat until done.@@ -363,7 +363,7 @@ class AgentLoop:else:final_content = "I've completed processing but have no response to give."- return final_content, tools_used, token_usage+ return final_content, tools_used, token_usage, iteration@trace(name="process_message",@@ -512,7 +512,7 @@ class AgentLoop:# logger.info(f"New messages: {messages}")# Run agent loop- final_content, tools_used, token_usage = await self._run_agent_loop(+ final_content, tools_used, token_usage, iteration = await self._run_agent_loop(messages=messages,session_key=session_key,publish_events=True,@@ -532,13 +532,18 @@ class AgentLoop:await self.sessions.save(session)time_cost = round(time.time() - start_time, 2)+ if tools_used is not None:+ tools_used_names = [tool["tool_name"] for tool in tools_used]+ else:+ tools_used_names = []return OutboundMessage(session_key=msg.session_key,content=final_content,metadata=msg.metadata,token_usage=token_usage,- time_cost=time_cost- or {}, # Pass through for channel-specific needs (e.g. Slack thread_ts)+ time_cost=time_cost,+ iteration=iteration,+ tools_used_names=tools_used_names)finally:long_running_notified = True@@ -565,7 +570,7 @@ class AgentLoop:)# Run agent loop (no events published)- final_content, tools_used, token_usage = await self._run_agent_loop(+ final_content, tools_used, token_usage, iteration = await self._run_agent_loop(messages=messages,session_key=msg.session_key,publish_events=False,@@ -724,8 +729,9 @@ Respond with ONLY valid JSON, no markdown fences."""allow_from.append(self.config.ov_server.admin_user_id)for channel in self.config.channels_config.get_all_channels():if channel.channel_key() == msg.session_key.channel_key():- if channel.allow_from:- allow_from.extend(channel.allow_from)+ allow_cmd = getattr(channel, 'allow_cmd_from', [])+ if allow_cmd:+ allow_from.extend(allow_cmd)break# If channel not found or sender not in allow_from list, ignore messagediff --git a/bot/vikingbot/agent/memory.py b/bot/vikingbot/agent/memory.pyindex 3a4864645..bfe0ed4d7 100644--- a/bot/vikingbot/agent/memory.py+++ b/bot/vikingbot/agent/memory.py@@ -28,10 +28,12 @@ class MemoryStore:user_memories = []for idx, memory in enumerate(result, start=1):user_memories.append(- f"{idx}. {getattr(memory, 'abstract', '')}; "- f"uri: {getattr(memory, 'uri', '')}; "- f"isDir: {getattr(memory, 'is_leaf', False)}; "- f"related score: {getattr(memory, 'score', 0.0)}"+ f"<memory index=\"{idx}\">\n"+ f" <abstract>{getattr(memory, 'abstract', '')}</abstract>\n"+ f" <uri>{getattr(memory, 'uri', '')}</uri>\n"+ f" <is_leaf>{getattr(memory, 'is_leaf', False)}</is_leaf>\n"+ f" <score>{getattr(memory, 'score', 0.0)}</score>\n"+ f"</memory>")return "\n".join(user_memories)return ""@@ -47,11 +49,13 @@ class MemoryStore:long_term = self.read_long_term()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:+ async def get_viking_memory_context(self, current_message: str, workspace_id: str, sender_id: str) -> str:try:+ config = load_config().ov_server+ admin_user_id = config.admin_user_id+ user_id = sender_id if config.mode == "remote" else admin_user_idclient = await VikingClient.create(agent_id=workspace_id)- admin_user_id = load_config().ov_server.admin_user_id- result = await client.search_memory(current_message, user_id=admin_user_id, limit=3)+ result = await client.search_memory(query=current_message, user_id=user_id, agent_user_id=admin_user_id, limit=5)if not result:return ""user_memory = self._parse_viking_memory(result["user_memory"])@@ -69,4 +73,4 @@ class MemoryStore:result = await client.read_user_profile(user_id)if not result:return ""- return result+ return result\ No newline at end of filediff --git a/bot/vikingbot/agent/tools/factory.py b/bot/vikingbot/agent/tools/factory.pyindex b85a43d68..6c846b4d4 100644--- a/bot/vikingbot/agent/tools/factory.py+++ b/bot/vikingbot/agent/tools/factory.py@@ -7,12 +7,11 @@ from vikingbot.agent.tools.filesystem import ReadFileTool, WriteFileTool, EditFifrom vikingbot.agent.tools.image import ImageGenerationToolfrom vikingbot.agent.tools.message import MessageToolfrom vikingbot.agent.tools.ov_file import (- VikingReadTool,VikingListTool,VikingSearchTool,VikingGrepTool,VikingGlobTool,- VikingSearchUserMemoryTool,+ VikingMultiReadTool,VikingMemoryCommitTool,VikingAddResourceTool,)@@ -87,12 +86,11 @@ def register_default_tools(# Open Viking toolsif include_viking_tools:- registry.register(VikingReadTool())+ registry.register(VikingMultiReadTool())registry.register(VikingListTool())registry.register(VikingSearchTool())registry.register(VikingGrepTool())registry.register(VikingGlobTool())- registry.register(VikingSearchUserMemoryTool())registry.register(VikingMemoryCommitTool())if not config.read_only:registry.register(VikingAddResourceTool())diff --git a/bot/vikingbot/agent/tools/ov_file.py b/bot/vikingbot/agent/tools/ov_file.pyindex 167b3cf03..a48ad477d 100644--- a/bot/vikingbot/agent/tools/ov_file.py+++ b/bot/vikingbot/agent/tools/ov_file.py@@ -1,14 +1,18 @@"""OpenViking file system tools: read, write, list, search resources."""+import asynciofrom abc import ABCfrom pathlib import Path-from typing import Any, Optional+from typing import Any, Optional, Union+import jsonimport httpxfrom loguru import loggerfrom vikingbot.agent.tools.base import Tool, ToolContextfrom vikingbot.openviking_mount.ov_server import VikingClient+from vikingbot.providers.litellm_provider import LiteLLMProvider+from vikingbot.config.loader import load_configclass OVFileTool(Tool, ABC):@@ -21,48 +25,6 @@ class OVFileTool(Tool, ABC):self._client = await VikingClient.create(tool_context.workspace_id)return self._client--class VikingReadTool(OVFileTool):- """Tool to read content from Viking resources."""-- @property- def name(self) -> str:- return "openviking_read"-- @property- def description(self) -> str:- return "Read content from OpenViking resources at different levels (abstract, overview, or full content)."-- @property- def parameters(self) -> dict[str, Any]:- return {- "type": "object",- "properties": {- "uri": {- "type": "string",- "description": "The Viking file's URI to read from (e.g., viking://resources/path/123.md)",- },- "level": {- "type": "string",- "description": "Reading level: 'abstract' (L0 summary), 'overview' (L1 overview), or 'read' (L2 full content)",- "enum": ["abstract", "overview", "read"],- "default": "abstract",- },- },- "required": ["uri"],- }-- async def execute(- self, tool_context: ToolContext, uri: str, level: str = "abstract", **kwargs: Any- ) -> str:- try:- client = await self._get_client(tool_context)- content = await client.read_content(uri, level=level)- return content- except Exception as e:- return f"Error reading from Viking: {str(e)}"--class VikingListTool(OVFileTool):"""Tool to list Viking resources."""@@ -126,7 +88,7 @@ class VikingSearchTool(OVFileTool):@propertydef description(self) -> str:- return "Search for resources in OpenViking using a query."+ return "Search for resources in OpenViking using a query"@propertydef parameters(self) -> dict[str, Any]:@@ -151,7 +113,8 @@ class VikingSearchTool(OVFileTool):) -> str:try:client = await self._get_client(tool_context)- results = await client.search(query, target_uri=target_uri)+ search_client = getattr(client, 'admin_user_client', client)+ results = await search_client.search(query, target_uri=target_uri)if not results:return f"No results found for query: {query}"@@ -231,7 +194,7 @@ class VikingGrepTool(OVFileTool):@propertydef description(self) -> str:- return "Search Viking resources using regex patterns (like grep)."+ return "Search Viking resources using regex patterns (like grep). Supports multiple patterns to search concurrently."@propertydef parameters(self) -> dict[str, Any]:@@ -243,8 +206,9 @@ class VikingGrepTool(OVFileTool):"description": "The whole Viking URI to search within (e.g., viking://resources/)",},"pattern": {- "type": "string",- "description": "Regex pattern to search for",+ "type": "array",+ "items": {"type": "string"},+ "description": "Regex pattern or array of regex patterns to search for",},"case_insensitive": {"type": "boolean",@@ -259,36 +223,70 @@ class VikingGrepTool(OVFileTool):self,tool_context: "ToolContext",uri: str,- pattern: str,+ pattern: Union[str, list[str]],case_insensitive: bool = False,**kwargs: Any,) -> str:try:client = await self._get_client(tool_context)- result = await client.grep(uri, pattern, case_insensitive=case_insensitive)-- if isinstance(result, dict):- matches = result.get("matches", [])- count = result.get("count", 0)- else:- matches = getattr(result, "matches", [])- count = getattr(result, "count", 0)-- if not matches:- return f"No matches found for pattern: {pattern}"-- result_lines = [f"Found {count} match{'es' if count != 1 else ''}:"]- for match in matches:- if isinstance(match, dict):- match_uri = match.get("uri", "unknown")- line = match.get("line", "?")- content = match.get("content", "")- else:- match_uri = getattr(match, "uri", "unknown")- line = getattr(match, "line", "?")- content = getattr(match, "content", "")- result_lines.append(f"\n📄 {match_uri}:{line}")- result_lines.append(f" {content}")+ patterns = [pattern] if isinstance(pattern, str) else pattern++ # Limit concurrent requests to avoid overwhelming the server and memory+ max_concurrent = 10+ semaphore = asyncio.Semaphore(max_concurrent)++ async def run_grep(p: str) -> tuple[str, list[Any]]:+ async with semaphore:+ try:+ result = await client.grep(uri, p, case_insensitive=case_insensitive)+ if isinstance(result, dict):+ matches = result.get("matches", [])+ else:+ matches = getattr(result, "matches", [])+ return (p, matches)+ except Exception as e:+ logger.warning(f"Error searching for pattern '{p}': {e}")+ return (p, [])++ tasks = [run_grep(p) for p in patterns]+ results = await asyncio.gather(*tasks)++ # Merge results by URI+ merged_results: dict[str, list[tuple[int, str, str]]] = {}+ total_matches = 0++ for p, matches in results:+ if not matches:+ continue+ total_matches += len(matches)+ for match in matches:+ if isinstance(match, dict):+ match_uri = match.get("uri", "unknown")+ line = match.get("line", "?")+ content = match.get("content", "")+ else:+ match_uri = getattr(match, "uri", "unknown")+ line = getattr(match, "line", "?")+ content = getattr(match, "content", "")++ if match_uri not in merged_results:+ merged_results[match_uri] = []+ merged_results[match_uri].append((line, content, p))++ if not merged_results:+ pattern_str = ", ".join(f"'{p}'" for p in patterns)+ return f"No matches found for patterns: {pattern_str}"++ # Format output+ result_lines = [f"Found {total_matches} match{'es' if total_matches != 1 else ''} across {len(patterns)} pattern{'s' if len(patterns) != 1 else ''}:"]++ for match_uri, matches in merged_results.items():+ # Sort matches by line number+ matches.sort(key=lambda x: int(x[0]) if str(x[0]).isdigit() else 0)+ result_lines.append(f"\n📄 {match_uri}")+ for line, content, pattern_name in matches:+ result_lines.append(f" Line {line} (pattern: '{pattern_name}'):")+ result_lines.append(f" {content}")return "\n".join(result_lines)except Exception as e:@@ -431,3 +429,276 @@ class VikingMemoryCommitTool(OVFileTool):except Exception as e:logger.exception(f"Error processing message: {e}")return f"Error committing to Viking: {str(e)}"++class VikingUserProfileTool(OVFileTool):+ """Tool to commit messages to OpenViking session."""++ @property+ def name(self) -> str:+ return "openviking_user_profile_read"++ @property+ def description(self) -> str:+ return "Read user's profile details from OpenViking."++ @property+ def parameters(self) -> dict[str, Any]:+ return {}++ async def execute(+ self,+ tool_context: ToolContext,+ messages: list[dict[str, Any]],+ **kwargs: Any,+ ) -> str:+ try:+ if not tool_context.sender_id:+ return "Error committed, sender_id is required."+ client = await self._get_client(tool_context)+ session_id = tool_context.session_key.safe_name()+ profile = await client.read_user_profile(tool_context.sender_id, session_id)+ return f"{profile}"+ except Exception as e:+ logger.exception(f"Error processing message: {e}")+ return f"Error read user profile: {str(e)}"+++class VikingSearchUserMemoryToolV2(OVFileTool):+ """Tool to search Viking user memories with enhanced query expansion and ranking."""++ def __init__(self):+ super().__init__()+ self._llm_provider = None+ self._config = load_config()++ @property+ def name(self) -> str:+ return "user_memory_search_v2"++ @property+ def description(self) -> str:+ return "Search for user memories in OpenViking using a query with enhanced query expansion and relevance ranking. Do not use the openviking_search tool to retrieve similar queries again."++ @property+ def parameters(self) -> dict[str, Any]:+ return {+ "type": "object",+ "properties": {+ "query": {"type": "string", "description": "The search query"},+ "top_k": {"type": "integer", "description": "Number of top results to return", "default": 10},+ },+ "required": ["query"],+ }++ async def _get_llm_provider(self):+ """Get or initialize the LLM provider."""+ if self._llm_provider is None:+ llm_config = self._config.agents+ self._llm_provider = LiteLLMProvider(+ api_key=llm_config.api_key,+ api_base=llm_config.api_base,+ default_model=llm_config.model,+ extra_headers=llm_config.extra_headers if llm_config else None,+ provider_name=llm_config.provider,+ )+ return self._llm_provider++ async def _generate_search_queries(self, original_query: str) -> list[str]:+ """Generate multiple search queries from the original query using LLM."""+ llm = await self._get_llm_provider()++ system_prompt = """You are an expert in generating search queries. Based on the user's original query, split it into 3–5 different query phrases to retrieve the user's personal memories.+The generated queries should cover the intent of the original query from different perspectives, including synonyms, related concepts, various expressions, etc., and be as concise as possible.+Please return only a JSON array with no extra content. Example:+["Query 1", "Query 2", "Query 3"]"""++ messages = [+ {"role": "system", "content": system_prompt},+ {"role": "user", "content": f"query: {original_query}\n"}+ ]++ try:+ response = await llm.chat(messages, temperature=0.3, max_tokens=512)+ content = response.content.strip()+ # 尝试解析JSON+ try:+ queries = json.loads(content)+ if isinstance(queries, list) and all(isinstance(q, str) for q in queries):+ # 去重并保留原始查询+ queries = list(set([original_query] + queries))+ return queries[:5] # 最多5个查询+ except json.JSONDecodeError:+ # 如果解析失败,尝试提取数组部分+ import re+ match = re.search(r'\[.*\]', content, re.DOTALL)+ if match:+ try:+ queries = json.loads(match.group())+ if isinstance(queries, list) and all(isinstance(q, str) for q in queries):+ queries = list(set([original_query] + queries))+ return queries[:5]+ except:+ pass++ # 如果所有解析都失败,返回原始查询+ return [original_query]++ except Exception as e:+ logger.warning(f"Failed to generate search queries: {e}")+ return [original_query]++ async def execute(self, tool_context: ToolContext, query: str, top_k: int = 10, **kwargs: Any) -> str:+ try:+ # 1. 生成扩展查询列表+ search_queries = await self._generate_search_queries(query)+ logger.info(f"Generated search queries: {search_queries}")++ # 2. 并发调用搜索接口+ client = await self._get_client(tool_context)+ user_id = tool_context.sender_id++ if not user_id:+ return "Error: sender_id is required for memory search."++ # 检查用户是否存在+ user_exists = await client._check_user_exists(user_id)+ if not user_exists:+ return f"No user found for id: {user_id}"++ uri_user_memory = f"viking://user/{user_id}/memories/"++ # 并发搜索+ search_tasks = [+ client.search(q, target_uri=uri_user_memory)+ for q in search_queries+ ]+ search_results = await asyncio.gather(*search_tasks, return_exceptions=True)++ # 3. 合并结果并去重+ all_memories = {}+ for result in search_results:+ if isinstance(result, Exception):+ logger.warning(f"Search failed: {result}")+ continue++ memories = result.get("memories", []) + result.get("resources", [])+ for memory in memories:+ uri = memory.get("uri", "")+ if not uri:+ continue++ score = memory.get("score", 0.0)+ if uri not in all_memories or score > all_memories[uri].get("score", 0.0):+ all_memories[uri] = memory++ # 4. 按相似度得分排序+ sorted_memories = sorted(+ all_memories.values(),+ key=lambda x: x.get("score", 0.0),+ reverse=True+ )[:top_k]++ # 5. 构造结果摘要+ if not sorted_memories:+ return f"No results found for query: {query}"++ result_lines = [f"Found {len(sorted_memories)} relevant memories:"]+ for i, memory in enumerate(sorted_memories, 1):+ title = memory.get("title", memory.get("uri", "Untitled"))+ abstract = memory.get("abstract", "")+ score = memory.get("score", 0.0)+ uri = memory.get("uri", "")++ result_lines.append(f"\n{i}. {title} (score: {score:.4f})")+ if abstract:+ result_lines.append(f" Abstract: {abstract}")+ result_lines.append(f" URI: {uri}")++ return "\n".join(result_lines)++ except Exception as e:+ logger.exception(f"Error in VikingSearchUserMemoryToolV2: {e}")+ return f"Error searching Viking user memory: {str(e)}"+++class VikingMultiReadTool(OVFileTool):+ """Tool to read content from multiple Viking resources concurrently."""++ @property+ def name(self) -> str:+ return "openviking_multi_read"++ @property+ def description(self) -> str:+ return "Read full content from multiple OpenViking resources concurrently. Returns complete content for all URIs with no truncation."++ @property+ def parameters(self) -> dict[str, Any]:+ return {+ "type": "object",+ "properties": {+ "uris": {+ "type": "array",+ "items": {"type": "string"},+ "description": "List of Viking file URIs to read from (e.g., [\"viking://resources/path/123.md\", \"viking://resources/path/456.md\"])",+ },+ },+ "required": ["uris"],+ }++ async def execute(+ self,+ tool_context: ToolContext,+ uris: list[str],+ **kwargs: Any,+ ) -> str:+ level = "read" # 默认获取完整内容+ try:+ if not uris:+ return "Error: No URIs provided."++ client = await self._get_client(tool_context)+ max_concurrent = 10+ semaphore = asyncio.Semaphore(max_concurrent)++ async def read_single_uri(uri: str) -> dict:+ async with semaphore:+ try:+ content = await client.read_content(uri, level=level)+ return {+ "uri": uri,+ "content": content,+ "success": True,+ }+ except Exception as e:+ logger.warning(f"Error reading from {uri}: {e}")+ return {+ "uri": uri,+ "content": f"Error reading from Viking: {str(e)}",+ "success": False,+ }++ # 并发读取所有URI+ read_tasks = [read_single_uri(uri) for uri in uris]+ results = await asyncio.gather(*read_tasks)++ # 构建结果+ result_lines = [f"Multi-read results for {len(uris)} resources (level: {level}):"]++ for i, result in enumerate(results, 1):+ uri = result["uri"]+ content = result["content"]+ success = result["success"]++ result_lines.append(f"\n--- START OF {uri} ---")+ if success:+ result_lines.append(content)+ else:+ result_lines.append(f"ERROR: {content}")+ result_lines.append(f"--- END OF {uri} ---")++ return "\n".join(result_lines)++ except Exception as e:+ logger.exception(f"Error in VikingMultiReadTool: {e}")+ return f"Error multi-reading Viking resources: {str(e)}"\ No newline at end of filediff --git a/bot/vikingbot/bus/events.py b/bot/vikingbot/bus/events.pyindex 147c0eab1..b14b67ecb 100644--- a/bot/vikingbot/bus/events.py+++ b/bot/vikingbot/bus/events.py@@ -42,6 +42,8 @@ class OutboundMessage:metadata: dict[str, Any] = field(default_factory=dict)token_usage: dict[str, int] = field(default_factory=dict)time_cost: float = field(default_factory=float)+ iteration: int = field(default_factory=int)+ tools_used_names: list[str] = field(default_factory=list)@propertydef channel(self) -> str:diff --git a/bot/vikingbot/channels/single_turn.py b/bot/vikingbot/channels/single_turn.pyindex 7c1673b8f..f618b816c 100644--- a/bot/vikingbot/channels/single_turn.py+++ b/bot/vikingbot/channels/single_turn.py@@ -95,10 +95,13 @@ class SingleTurnChannel(BaseChannel):"""Send a message - store final response for later retrieval."""if msg.is_normal_message:if self._eval:+ content = msg.content.replace('"', "'") if msg.content else ""output = {- "text": msg.content,+ "text": content,"token_usage": msg.token_usage,"time_cost": msg.time_cost,+ "iteration": msg.iteration,+ "tools_used_names": msg.tools_used_names,}msg.content = json.dumps(output, ensure_ascii=False)self._last_response = msg.contentdiff --git a/bot/vikingbot/cli/commands.py b/bot/vikingbot/cli/commands.pyindex 119db4f4f..594cdc0be 100644--- a/bot/vikingbot/cli/commands.py+++ b/bot/vikingbot/cli/commands.py@@ -1,5 +1,14 @@"""CLI commands for vikingbot."""+import warnings+# Ignore Pydantic V1 compatibility warning with Python 3.14+ from volcenginesdkarkruntime+warnings.filterwarnings(+ "ignore",+ message="Core Pydantic V1 functionality isn't compatible with Python 3.14 or greater.",+ category=UserWarning,+ module="volcenginesdkarkruntime._compat"+)+import asyncioimport jsonimport os@@ -573,21 +582,9 @@ def chat(bus = MessageBus()config = ensure_config(path)_init_bot_data(config)- session_manager = SessionManager(config.bot_data_path)-- is_single_turn = message is not None- # Use unified default session ID- if session_id is None:- session_id = get_or_create_machine_id()- cron = prepare_cron(bus, quiet=is_single_turn)- channels = prepare_agent_channel(config, bus, message, session_id, markdown, logs, eval, sender)- agent_loop = prepare_agent_loop(- config, bus, session_manager, cron, quiet=is_single_turn, eval=eval- )logger.remove()-- log_file = get_data_dir() / f"vikingbot.debug.{os.getpid()}.log"+ log_file = get_data_dir() /"log" / f"vikingbot.debug.{os.getpid()}.log"logger.add(log_file,level="DEBUG",@@ -603,6 +600,18 @@ def chat(else:logger.add(sys.stderr, level="ERROR")+ session_manager = SessionManager(config.bot_data_path)++ is_single_turn = message is not None+ # Use unified default session ID+ if session_id is None:+ session_id = get_or_create_machine_id()+ cron = prepare_cron(bus, quiet=is_single_turn)+ channels = prepare_agent_channel(config, bus, message, session_id, markdown, logs, eval, sender)+ agent_loop = prepare_agent_loop(+ config, bus, session_manager, cron, quiet=is_single_turn, eval=eval+ )+async def run():if is_single_turn:# Single-turn mode: run channels and agent, exit after responsediff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.pyindex 78948bbbc..0b5a885ae 100644--- a/bot/vikingbot/config/schema.py+++ b/bot/vikingbot/config/schema.py@@ -115,7 +115,8 @@ class FeishuChannelConfig(BaseChannelConfig):app_secret: str = ""encrypt_key: str = ""verification_token: str = ""- allow_from: list[str] = Field(default_factory=list) ## 允许更新Agent对话的Feishu用户ID列表+ allow_from: list[str] = Field(default_factory=list)+ allow_cmd_from: list[str] = Field(default_factory=list) ## 允许执行命令的Feishu用户ID列表thread_require_mention: bool = Field(default=True, description="话题群模式下是否需要@才响应:默认True=所有消息必须@才响应;False=新话题首条消息无需@,后续回复必须@")def channel_id(self) -> str:diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.pyindex ba43b67d9..bf36fdeae 100644--- a/bot/vikingbot/openviking_mount/ov_server.py+++ b/bot/vikingbot/openviking_mount/ov_server.py@@ -22,6 +22,7 @@ class VikingClient:self.client = ov.AsyncHTTPClient(url=openviking_config.server_url)self.agent_id = "default"self.account_id = "default"+ self.user_id = "default"self.admin_user_id = "default"self._apikey_manager = Noneelse:@@ -55,6 +56,14 @@ class VikingClient:user_exists = await self._check_user_exists(self.admin_user_id)if not user_exists:await self._initialize_user(self.admin_user_id, role="admin")+ admin_user_api_key = await self._get_or_create_user_apikey(self.admin_user_id)+ if admin_user_api_key:+ self.admin_user_client = ov.AsyncHTTPClient(+ url=self.openviking_config.server_url,+ api_key=admin_user_api_key,+ agent_id=self.agent_id,+ )+ await self.admin_user_client.initialize()@classmethodasync def create(cls, agent_id: Optional[str] = None):@@ -289,7 +298,7 @@ class VikingClient:return Noneasync def search_memory(- self, query: str, user_id: str, limit: int = 10+ self, query: str, user_id: str, agent_user_id: str, limit: int = 10) -> dict[str, list[Any]]:"""通过上下文消息,检索viking 的user、Agent memory。@@ -313,7 +322,7 @@ class VikingClient:target_uri=uri_user_memory,limit=limit,)- agent_space_name = self.get_agent_space_name(user_id)+ agent_space_name = self.get_agent_space_name(agent_user_id)uri_agent_memory = f"viking://agent/{agent_space_name}/memories/"agent_memory = await self.client.find(query=query,@@ -462,7 +471,12 @@ async def main_test():async def account_test():- client = ov.AsyncHTTPClient(url="http://localhost:1933", api_key="test")++ client = ov.AsyncHTTPClient(+ url="http://localhost:1933",+ api_key="",+ agent_id="shared",+ )await client.initialize()# res = await client.admin_list_users("eval")@@ -470,7 +484,8 @@ async def account_test():# res = await client.admin_remove_user("default", "admin")# res = await client.admin_list_accounts()# res = await client.admin_create_account("eval", "default")- res = await client.admin_register_user("default", "test_root", "root")+ res = await client.search("123")+print(res)