龙虾读取session历史消息

龙虾读取历史消息:

def _load(self, key: str) -> Session | None:

"""Load a session from disk."""

path = self._get_session_path(key)

if not path.exists():

legacy_path = self._get_legacy_session_path(key)

if legacy_path.exists():

try:

shutil.move(str(legacy_path), str(path))

logger.info("Migrated session {} from legacy path", key)

except Exception:

logger.exception("Failed to migrate session {}", key)

print('----------------------path--------------------------------------')

print(path)

print('----------------------path--------------------------------------')

if not path.exists():

return None

try:

messages = []

metadata = {}

created_at = None

last_consolidated = 0

with open(path, encoding="utf-8") as f:

for line in f:

line = line.strip()

if not line:

continue

data = json.loads(line)

if data.get("_type") == "metadata":

metadata = data.get("metadata", {})

created_at = datetime.fromisoformat(data["created_at"]) if data.get("created_at") else None

last_consolidated = data.get("last_consolidated", 0)

else:

messages.append(data)

print('------------------a---------------------------')

print(messages)

print('------------------load_messages---------------------------')

return Session(

key=key,

messages=messages,

created_at=created_at or datetime.now(),

metadata=metadata,

last_consolidated=last_consolidated

)

except Exception as e:

logger.warning("Failed to load session {}: {}", key, e)

return None

------------------load_messages---------------------------

{'role': 'user', 'content': '明天杭州什么天气', 'timestamp': '2026-04-06T19:31:20.198388'}, {'role': 'assistant', 'content': '我来帮你查询明天杭州的天气。让我使用天气技能来获取这个信息。', 'tool_calls': \[{'id': '16WO8yjBg', 'type': 'function', 'function': {'name': 'read_file', 'arguments': '{"path": "/root/nanobot/nanobot/skills/weather/SKILL.md"}'}}\], 'timestamp': '2026-04-06T19:31:20.198403'}, {'role': 'tool', 'tool_call_id': '16WO8yjBg', 'name': 'read_file', 'content': '1\| ---\\n2\| name: weather\\n3\| description: Get current weather and forecasts (no API key required).\\n4\| homepage: https://wttr.in/:help\\n5\| metadata: {"nanobot":{"emoji":"🌤️","requires":{"bins":\["curl"\]}}}\\n6\| ---\\n7\| \\n8\| # Weather\\n9\| \\n10\| Two free services, no API keys needed.\\n11\| \\n12\| ## wttr.in (primary)\\n13\| \\n14\| Quick one-liner:\\n15\| \`\`\`bash\\n16\| curl -s "wttr.in/London?format=3"\\n17\| # Output: London: ⛅️ +8°C\\n18\| \`\`\`\\n19\| \\n20\| Compact format:\\n21\| \`\`\`bash\\n22\| curl -s "wttr.in/London?format=%l:+%c+%t+%h+%w"\\n23\| # Output: London: ⛅️ +8°C 71% ↙5km/h\\n24\| \`\`\`\\n25\| \\n26\| Full forecast:\\n27\| \`\`\`bash\\n28\| curl -s "wttr.in/London?T"\\n29\| \`\`\`\\n30\| \\n31\| Format codes: \`%c\` condition · \`%t\` temp · \`%h\` humidity · \`%w\` wind · \`%l\` location · \`%m\` moon\\n32\| \\n33\| Tips:\\n34\| - URL-encode spaces: \`wttr.in/New+York\`\\n35\| - Airport codes: \`wttr.in/JFK\`\\n36\| - Units: \`?m\` (metric) \`?u\` (USCS)\\n37\| - Today only: \`?1\` · Current only: \`?0\`\\n38\| - PNG: \`curl -s "wttr.in/Berlin.png" -o /tmp/weather.png\`\\n39\| \\n40\| ## Open-Meteo (fallback, JSON)\\n41\| \\n42\| Free, no key, good for programmatic use:\\n43\| \`\`\`bash\\n44\| curl -s "https://api.open-meteo.com/v1/forecast?latitude=51.5\&longitude=-0.12\¤t_weather=true"\\n45\| \`\`\`\\n46\| \\n47\| Find coordinates for a city, then query. Returns JSON with temp, windspeed, weathercode.\\n48\| \\n49\| Docs: https://open-meteo.com/en/docs\\n\\n(End of file --- 49 lines total)', 'timestamp': '2026-04-06T19:31:20.198407'}, {'role': 'assistant', 'content': '现在我来查询明天杭州的天气。根据技能说明,我可以使用wttr.in服务来获取天气预报。让我获取明天(1天)的天气预报:', 'tool_calls': \[{'id': 'Fzpy7THFU', 'type': 'function', 'function': {'name': 'exec', 'arguments': '{"command": "curl -s \\\\"wttr.in/Hangzhou?1\&T\\\\""}'}}\], 'timestamp': '2026-04-06T19:31:20.198410'}, {'role': 'tool', 'tool_call_id': 'Fzpy7THFU', 'name': 'exec', 'content': 'Weather report: hangzhou\\n\\n _\`/"".-. Patchy rain nearby\\n ,\\\\_( ). +24(26) °C \\n /(___(__) ↓ 19 km/h \\n ' ' ' ' 10 km \\n ' ' ' ' 0.0 mm \\n ┌─────────────┐ \\n┌──────────────────────────────┬───────────────────────┤ Mon 06 Apr ├───────────────────────┬──────────────────────────────┐\\n│ Morning │ Noon └──────┬──────┘ Evening │ Night │\\n├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤\\n│ \\\\ / Sunny │ \\\\ / Partly Cloudy │ _\`/"".-. Patchy rain ne...│ _\`/"".-. Patchy rain ne...│\\n│ .-. 21 °C │ _ /"".-. 22 °C │ ,\\\\_( ). +24(18) °C │ ,\\\\_( ). 15 °C │\\n│ ― ( ) ― → 4-6 km/h │ \\\\_( ). ↓ 6-9 km/h │ /(___(__) ↓ 19-27 km/h │ /(___(__) ↓ 12-16 km/h │\\n│ \`-' 10 km │ /(___(__) 10 km │ ' ' ' ' 10 km │ ' ' ' ' 10 km │\\n│ / \\\\ 0.0 mm \| 0% │ 0.0 mm \| 0% │ ' ' ' ' 0.0 mm \| 74% │ ' ' ' ' 0.0 mm \| 60% │\\n└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘\\nLocation: 杭州市, 江干区 (Jianggan), 杭州市 Hangzhou, 浙江省, 中国 \[30.2489468,120.2052547\]\\n\\nFollow @igor_chubin for wttr.in updates\\n\\n\\nExit code: 0', 'timestamp': '2026-04-06T19:31:20.198412'}, {'role': 'assistant', 'content': '让我再获取更详细的明天天气预报,使用格式化的查询:', 'tool_calls': \[{'id': 'W2CUZhcck', 'type': 'function', 'function': {'name': 'exec', 'arguments': '{"command": "curl -s \\\\"wttr.in/Hangzhou?format=%l:+%c+%t+%h+%w\&1\\\\""}'}}\], 'timestamp': '2026-04-06T19:31:20.198414'}, {'role': 'tool', 'tool_call_id': 'W2CUZhcck', 'name': 'exec', 'content': 'hangzhou: 🌦 +26°C 48% ↙19km/h\\n\\nExit code: 0', 'timestamp': '2026-04-06T19:31:20.198417'}, {'role': 'assistant', 'content': '让我获取更详细的3天预报,这样可以看到明天的具体天气情况:', 'tool_calls': \[{'id': 'b2ON3sbZH', 'type': 'function', 'function': {'name': 'exec', 'arguments': '{"command": "curl -s \\\\"wttr.in/Hangzhou?2\&T\\\\""}'}}\], 'timestamp': '2026-04-06T19:31:20.198422'}, {'role': 'tool', 'tool_call_id': 'b2ON3sbZH', 'name': 'exec', 'content': 'Weather report: hangzhou\\n\\n _\`/"".-. Patchy rain nearby\\n ,\\\\_( ). +24(26) °C \\n /(___(__) ↓ 19 km/h \\n ' ' ' ' 10 km \\n ' ' ' ' 0.0 mm \\n ┌─────────────┐ \\n┌──────────────────────────────┬───────────────────────┤ Mon 06 Apr ├───────────────────────┬──────────────────────────────┐\\n│ Morning │ Noon └──────┬──────┘ Evening │ Night │\\n├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤\\n│ \\\\ / Sunny │ \\\\ / Partly Cloudy │ _\`/"".-. Patchy rain ne...│ _\`/"".-. Patchy rain ne...│\\n│ .-. 21 °C │ _ /"".-. 22 °C │ ,\\\\_( ). +24(18) °C │ ,\\\\_( ). 15 °C │\\n│ ― ( ) ― → 4-6 km/h │ \\\\_( ). ↓ 6-9 km/h │ /(___(__) ↓ 19-27 km/h │ /(___(__) ↓ 12-16 km/h │\\n│ \`-' 10 km │ /(___(__) 10 km │ ' ' ' ' 10 km │ ' ' ' ' 10 km │\\n│ / \\\\ 0.0 mm \| 0% │ 0.0 mm \| 0% │ ' ' ' ' 0.0 mm \| 74% │ ' ' ' ' 0.0 mm \| 60% │\\n└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘\\n ┌─────────────┐ \\n┌──────────────────────────────┬───────────────────────┤ Tue 07 Apr ├───────────────────────┬──────────────────────────────┐\\n│ Morning │ Noon └──────┬──────┘ Evening │ Night │\\n├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤\\n│ \\\\ / Sunny │ \\\\ / Sunny │ \\\\ / Partly Cloudy │ \\\\ / Partly Cloudy │\\n│ .-. 16 °C │ .-. 18 °C │ _ /"".-. +14(13) °C │ _ /"".-. +13(11) °C │\\n│ ― ( ) ― ↙ 10-12 km/h │ ― ( ) ― ← 17-20 km/h │ \\\\_( ). ↙ 14-20 km/h │ \\\\_( ). ↙ 12-17 km/h │\\n│ \`-' 10 km │ \`-' 10 km │ /(___(__) 10 km │ /(___(__) 10 km │\\n│ / \\\\ 0.0 mm \| 0% │ / \\\\ 0.0 mm \| 0% │ 0.0 mm \| 0% │ 0.0 mm \| 0% │\\n└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘\\nLocation: 杭州市, 江干区 (Jianggan), 杭州市 Hangzhou, 浙江省, 中国 \[30.2489468,120.2052547\]\\n\\nFollow @igor_chubin for wttr.in updates\\n\\n\\nExit code: 0', 'timestamp': '2026-04-06T19:31:20.198426'}, {'role': 'assistant', 'content': '根据天气预报,明天(4月7日,星期二)杭州的天气情况如下:\\n\\n## 明天杭州天气预报 🌤️\\n\\n\*\*整体天气:\*\* 晴天转多云\\n\*\*温度范围:\*\* 13°C - 18°C\\n\\n### 详细时段预报:\\n\\n\*\*早上(Morning):\*\*\\n- ☀️ 晴天\\n- 温度:16°C\\n- 风向:西南风 10-12 km/h\\n- 能见度:10 km\\n- 降水概率:0%\\n\\n\*\*中午(Noon):\*\*\\n- ☀️ 晴天\\n- 温度:18°C\\n- 风向:西风 17-20 km/h\\n- 能见度:10 km\\n- 降水概率:0%\\n\\n\*\*傍晚(Evening):\*\*\\n- ⛅ 局部多云\\n- 温度:14°C(体感13°C)\\n- 风向:西南风 14-20 km/h\\n- 能见度:10 km\\n- 降水概率:0%\\n\\n\*\*夜间(Night):\*\*\\n- ⛅ 局部多云\\n- 温度:13°C(体感11°C)\\n- 风向:西南风 12-17 km/h\\n- 能见度:10 km\\n- 降水概率:0%\\n\\n### 出行建议:\\n明天杭州天气不错,全天晴朗到多云,没有降水。温度适中,早晚温差约5°C,建议穿薄外套。风力中等,适合户外活动。', 'timestamp': '2026-04-06T19:31:20.198428'}

------------------load_messages---------------------------

相关推荐
better_liang2 小时前
每日Java面试场景题知识点之-分布式事务
java·微服务·seata·分布式事务·一致性·saga·tcc
莹宝思密达2 小时前
地图显示西安经济开发区边界线-2023.12
前端·vue.js·数据可视化
kvo7f2JTy2 小时前
JAVA 设计模式
java·开发语言·设计模式
仍然.2 小时前
多线程---阻塞队列收尾和线程池
java·开发语言·算法
TDengine (老段)3 小时前
TDengine IDMP 可视化 —— 饼图
大数据·数据库·人工智能·物联网·时序数据库·tdengine·涛思数据
认真的薛薛3 小时前
GPU运维:vllm启动大模型参数解析
运维·数据库·vllm
lizhongxuan3 小时前
LLM Wiki:让大模型替你打理知识库的完整指南
前端·后端·面试
鱼鳞_3 小时前
Java学习笔记_Day22
java·笔记·学习
刘~浪地球3 小时前
Redis 从入门到精通(九):事务详解
数据库·redis·缓存