Skip to content

Commit 7e1f1f9

Browse files
authored
Merge pull request #4 from Raven95676/dev
撤回部分更改
2 parents 23faaf9 + 29fe27b commit 7e1f1f9

File tree

7 files changed

+29
-48
lines changed

7 files changed

+29
-48
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ AstrBot 的 lorebook 插件,支持自定义触发器、变量、逻辑、占
1313

1414
建议在使用前先阅读[YAML 入门教程](https://www.runoob.com/w3cnote/yaml-intro.html)
1515

16-
## 近期更改(0.1.1 - 0.1.2
16+
## 近期更改(0.1.1 - 0.1.4
1717

18-
- 优化处理器和解析器性能
1918
- 实现作用域继承与变量值占位符解析
2019
- 添加非逻辑
2120
- 按会话、人格隔离lorebook

core/handlers/logic_handler.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from functools import lru_cache
2-
31
# 定义支持的比较运算符及其对应的函数
42
OPERATORS = [
53
("==", lambda x, y: x == y), # 等于
@@ -77,7 +75,6 @@ def handle_logic_oper(self, function: str, args: list[str]) -> str:
7775
case _:
7876
return "未知逻辑操作"
7977

80-
@lru_cache(maxsize=128)
8178
def _eval_cond(self, condition: str) -> bool:
8279
"""处理条件表达式
8380
@@ -135,7 +132,6 @@ def _eval_cond(self, condition: str) -> bool:
135132
# 如果没有匹配到任何运算符,将非空条件视为真
136133
return bool(condition)
137134

138-
@lru_cache(maxsize=256)
139135
def _try_numeric_conversion(self, value: str) -> int | float | str:
140136
"""尝试将值转换为数值类型
141137

core/handlers/random_handler.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import random
22
import re
3-
from functools import lru_cache
43

54
from astrbot.api import logger
65

@@ -57,7 +56,6 @@ def handle_random_oper(self, args: list[str]) -> str:
5756
except Exception as e:
5857
return f"参数无效: {e}"
5958

60-
@lru_cache(maxsize=128)
6159
def _is_num(self, s: str) -> bool:
6260
"""检查字符串是否可以转换为整数(包括负数)
6361

core/handlers/time_handler.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from datetime import datetime
2-
from functools import lru_cache
32
from typing import Any
43

5-
from dateutil import relativedelta # type: ignore
4+
from dateutil import relativedelta
65

76
# 定义不同时间格式的格式化字符串
87
TIME_FORMATS = {
@@ -145,38 +144,6 @@ def _adjust_time(self, delta_input: Any, positive: bool = True) -> str:
145144
case _:
146145
return "时间增量输入类型无效"
147146

148-
@lru_cache(maxsize=64)
149-
def _format_time_difference(self, seconds: float, is_past: bool) -> str:
150-
"""格式化时间差异为人性化字符串
151-
152-
Args:
153-
seconds: 时间差异(秒)
154-
is_past: 是否是过去时间
155-
156-
Returns:
157-
人性化的时间差异描述
158-
"""
159-
suffix = "前" if is_past else "后"
160-
seconds = abs(seconds)
161-
162-
if seconds < 60:
163-
return "刚刚"
164-
elif seconds < 3600: # 1小时内
165-
minutes = int(seconds / 60)
166-
return f"{minutes}分钟{suffix}"
167-
elif seconds < 86400: # 24小时内
168-
hours = int(seconds / 3600)
169-
return f"{hours}小时{suffix}"
170-
elif seconds < 2592000: # 约30天内
171-
days = int(seconds / 86400)
172-
return f"{days}{suffix}"
173-
elif seconds < 31536000: # 1年内
174-
months = int(seconds / 2592000)
175-
return f"{months}个月{suffix}"
176-
else:
177-
years = int(seconds / 31536000)
178-
return f"{years}{suffix}"
179-
180147
def _get_idle_duration(self, times: dict[str, datetime]) -> str:
181148
"""计算并返回人性化的空闲时间字符串
182149
@@ -192,6 +159,29 @@ def _get_idle_duration(self, times: dict[str, datetime]) -> str:
192159

193160
# 确定是过去还是未来
194161
is_past = idle_seconds > 0
162+
suffix = "前" if is_past else "后"
163+
164+
idle_seconds = abs(idle_seconds)
195165

196-
# 使用可缓存的辅助方法来格式化时间差异
197-
return self._format_time_difference(idle_seconds, is_past)
166+
if is_past:
167+
delta = relativedelta.relativedelta(times["before"], times["after"])
168+
else:
169+
delta = relativedelta.relativedelta(times["after"], times["before"])
170+
171+
if idle_seconds < 60:
172+
return "刚刚"
173+
elif idle_seconds < 3600: # 1小时内
174+
minutes = delta.minutes
175+
return f"{minutes}分钟{suffix}"
176+
elif idle_seconds < 86400: # 24小时内
177+
hours = delta.hours
178+
return f"{hours}小时{suffix}"
179+
elif idle_seconds < 2592000: # 约30天内
180+
days = delta.days
181+
return f"{days}{suffix}"
182+
elif idle_seconds < 31536000: # 1年内
183+
months = delta.months + delta.years * 12
184+
return f"{months}个月{suffix}"
185+
else:
186+
years = delta.years
187+
return f"{years}{suffix}"

core/parser.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import re
44
from collections import deque
55
from datetime import datetime
6-
from functools import lru_cache
76
from typing import Any
87

98
from kwmatcher import AhoMatcher
@@ -204,7 +203,6 @@ def replace_match(match, phase):
204203

205204
return text
206205

207-
@lru_cache(maxsize=128)
208206
def _split_args(self, args_str: str) -> list[str]:
209207
"""切分参数字符串,支持引号保护。
210208

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .core.parser import LoreParser # type: ignore
1616

1717

18-
@register("astrbot_plugin_lorebook_lite", "Raven95676", "lorebook插件", "0.1.3")
18+
@register("astrbot_plugin_lorebook_lite", "Raven95676", "lorebook插件", "0.1.4")
1919
class LorePlugin(Star):
2020
"""Lorebook插件,用于根据预设规则处理聊天内容并修改LLM请求"""
2121

metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: astrbot-plugin-lorebook-lite
22
desc: Astrbot 轻量世界书插件
33
help:
4-
version: v0.1.3
4+
version: v0.1.4
55
author: Raven95676
66
repo: https://github.com/Raven95676/astrbot_plugin_lorebook_lite

0 commit comments

Comments
 (0)