Files
Fay/core/content_db.py
T
xszyou ca63d30824 自然进化
2025年10月15日,Fay升级全新的工具调用机制,实现ai ide级的工具调用稳定性。(参考openai codex cli的实现逻辑):
1. 工具调用逻辑由ReAct替换为更稳定的workflow;
2. 为mcp服务补充一个编辑功能;
3. 提供logseq mcp服务器,并作为fay的默认配置之一;
4. 工具调用的文本支持了流式输出;
5. 非富了工具调用时的think日志;
6. 优化agent、llm、工具调用的prompt。
7. 可用的mcp工具列表单独维护,不在nlp时更新;
8. 优化日程管理工具;
9. 修复了若干已知bug。
2025-10-15 19:57:31 +08:00

189 lines
6.0 KiB
Python

import sqlite3
import time
import threading
import functools
from utils import util
def synchronized(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return wrapper
__content_tb = None
def new_instance():
global __content_tb
if __content_tb is None:
__content_tb = Content_Db()
__content_tb.init_db()
return __content_tb
class Content_Db:
def __init__(self) -> None:
self.lock = threading.Lock()
# 初始化数据库
def init_db(self):
conn = sqlite3.connect('memory/fay.db')
conn.text_factory = str
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS T_Msg
(id INTEGER PRIMARY KEY AUTOINCREMENT,
type CHAR(10),
way CHAR(10),
content TEXT NOT NULL,
createtime INT,
username TEXT DEFAULT 'User',
uid INT);''')
# 对话采纳记录表
c.execute('''CREATE TABLE IF NOT EXISTS T_Adopted
(id INTEGER PRIMARY KEY AUTOINCREMENT,
msg_id INTEGER UNIQUE,
adopted_time INT,
FOREIGN KEY(msg_id) REFERENCES T_Msg(id));''')
conn.commit()
conn.close()
# 添加对话
@synchronized
def add_content(self, type, way, content, username='User', uid=0):
conn = sqlite3.connect("memory/fay.db")
conn.text_factory = str
cur = conn.cursor()
try:
cur.execute("INSERT INTO T_Msg (type, way, content, createtime, username, uid) VALUES (?, ?, ?, ?, ?, ?)",
(type, way, content, int(time.time()), username, uid))
conn.commit()
last_id = cur.lastrowid
except Exception as e:
util.log(1, "请检查参数是否有误: {}".format(e))
conn.close()
return 0
conn.close()
return last_id
# 更新对话内容
@synchronized
def update_content(self, msg_id, content):
"""
更新指定ID的消息内容
:param msg_id: 消息ID
:param content: 新的内容
:return: 是否更新成功
"""
conn = sqlite3.connect("memory/fay.db")
conn.text_factory = str
cur = conn.cursor()
try:
cur.execute("UPDATE T_Msg SET content = ? WHERE id = ?", (content, msg_id))
conn.commit()
affected_rows = cur.rowcount
except Exception as e:
util.log(1, f"更新消息内容失败: {e}")
conn.close()
return False
conn.close()
return affected_rows > 0
# 根据ID查询对话记录
@synchronized
def get_content_by_id(self, msg_id):
conn = sqlite3.connect("memory/fay.db")
conn.text_factory = str
cur = conn.cursor()
cur.execute("SELECT * FROM T_Msg WHERE id = ?", (msg_id,))
record = cur.fetchone()
conn.close()
return record
# 添加对话采纳记录
@synchronized
def adopted_message(self, msg_id):
conn = sqlite3.connect('memory/fay.db')
conn.text_factory = str
cur = conn.cursor()
# 检查消息ID是否存在
cur.execute("SELECT 1 FROM T_Msg WHERE id = ?", (msg_id,))
if cur.fetchone() is None:
util.log(1, "消息ID不存在")
conn.close()
return False
try:
cur.execute("INSERT INTO T_Adopted (msg_id, adopted_time) VALUES (?, ?)", (msg_id, int(time.time())))
conn.commit()
except sqlite3.IntegrityError:
util.log(1, "该消息已被采纳")
conn.close()
return False
conn.close()
return True
# 获取对话内容
@synchronized
def get_list(self, way, order, limit, uid=0):
conn = sqlite3.connect("memory/fay.db")
conn.text_factory = str
cur = conn.cursor()
where_uid = ""
if int(uid) != 0:
where_uid = f" AND T_Msg.uid = {uid} "
base_query = f"""
SELECT T_Msg.type, T_Msg.way, T_Msg.content, T_Msg.createtime,
datetime(T_Msg.createtime, 'unixepoch', 'localtime') AS timetext,
T_Msg.username,T_Msg.id,
CASE WHEN T_Adopted.msg_id IS NOT NULL THEN 1 ELSE 0 END AS is_adopted
FROM T_Msg
LEFT JOIN T_Adopted ON T_Msg.id = T_Adopted.msg_id
WHERE 1 {where_uid}
"""
if way == 'all':
query = base_query + f" ORDER BY T_Msg.id {order} LIMIT ?"
cur.execute(query, (limit,))
elif way == 'notappended':
query = base_query + f" AND T_Msg.way != 'appended' ORDER BY T_Msg.id {order} LIMIT ?"
cur.execute(query, (limit,))
else:
query = base_query + f" AND T_Msg.way = ? ORDER BY T_Msg.id {order} LIMIT ?"
cur.execute(query, (way, limit))
list = cur.fetchall()
conn.close()
return list
@synchronized
def get_recent_messages_by_user(self, username='User', limit=30):
conn = sqlite3.connect("memory/fay.db")
conn.text_factory = str
cur = conn.cursor()
cur.execute(
"""
SELECT type, content
FROM T_Msg
WHERE username = ?
ORDER BY id DESC
LIMIT ?
""",
(username, limit),
)
rows = cur.fetchall()
conn.close()
rows.reverse()
return rows
@synchronized
def get_previous_user_message(self, msg_id):
conn = sqlite3.connect("memory/fay.db")
cur = conn.cursor()
cur.execute("""
SELECT id, type, way, content, createtime, datetime(createtime, 'unixepoch', 'localtime') AS timetext, username
FROM T_Msg
WHERE id < ? AND type != 'fay'
ORDER BY id DESC
LIMIT 1
""", (msg_id,))
record = cur.fetchone()
conn.close()
return record