nonebot-plugin-group-chat-memory 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (13) hide show
  1. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/PKG-INFO +3 -1
  2. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/README.md +2 -0
  3. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/api.py +38 -7
  4. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/handlers.py +6 -3
  5. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/pyproject.toml +1 -1
  6. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/LICENSE +0 -0
  7. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/__init__.py +0 -0
  8. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/config.py +0 -0
  9. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/context.py +0 -0
  10. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/memory.py +0 -0
  11. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/models.py +0 -0
  12. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/replies.py +0 -0
  13. {nonebot_plugin_group_chat_memory-0.1.1 → nonebot_plugin_group_chat_memory-0.1.2}/nonebot_plugin_group_chat_memory/store.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot-plugin-group-chat-memory
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: 为 NoneBot2 群聊提供共享长期记忆、点名对话和按群概率回复
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -85,6 +85,8 @@ COMMAND_START=["/"]
85
85
 
86
86
  聊天地址填到 API 根路径(通常包含 `/v1`),插件分别请求其下的 `/chat/completions` 和 `/embeddings`。默认聊天模型标识是 `deepseek-v4.1-flash`,但**模型标识必须以实际服务商支持的值为准**。这四项保存到 SQLite,不再读取同名环境变量,更新后立即生效且重启后保留。使用 `/模型配置` 可查询脱敏状态。
87
87
 
88
+ 使用 OpenCode Go 直连 API 时,地址是 `https://opencode.ai/inference/go/openai/v1`,模型是 `deepseek-v4.1-flash`。插件会发送自己的 User-Agent,并对 OpenCode 请求附带按群稳定的 `x-opencode-session`,避免被网关当作通用 SDK 流量。
89
+
88
90
  ## 其余环境配置字段
89
91
 
90
92
  四个模型运行时字段不再通过 env 设置。其余以下字段可写在 NoneBot `.env` 中;集合类值使用 JSON 格式。
@@ -59,6 +59,8 @@ COMMAND_START=["/"]
59
59
 
60
60
  聊天地址填到 API 根路径(通常包含 `/v1`),插件分别请求其下的 `/chat/completions` 和 `/embeddings`。默认聊天模型标识是 `deepseek-v4.1-flash`,但**模型标识必须以实际服务商支持的值为准**。这四项保存到 SQLite,不再读取同名环境变量,更新后立即生效且重启后保留。使用 `/模型配置` 可查询脱敏状态。
61
61
 
62
+ 使用 OpenCode Go 直连 API 时,地址是 `https://opencode.ai/inference/go/openai/v1`,模型是 `deepseek-v4.1-flash`。插件会发送自己的 User-Agent,并对 OpenCode 请求附带按群稳定的 `x-opencode-session`,避免被网关当作通用 SDK 流量。
63
+
62
64
  ## 其余环境配置字段
63
65
 
64
66
  四个模型运行时字段不再通过 env 设置。其余以下字段可写在 NoneBot `.env` 中;集合类值使用 JSON 格式。
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
  import asyncio
4
4
  import math
5
5
  from typing import Any
6
+ from urllib.parse import urlparse
6
7
 
7
8
  import httpx
8
9
 
@@ -11,6 +12,7 @@ from .context import build_chat_messages
11
12
  from .models import ChatContext
12
13
 
13
14
  CHAT_RETRY_DELAYS: tuple[float, ...] = (0.5, 1.0)
15
+ CLIENT_USER_AGENT = "nonebot-plugin-group-chat-memory/0.1.2"
14
16
 
15
17
 
16
18
  class ModelAPIError(RuntimeError):
@@ -24,9 +26,16 @@ class EmbeddingAPIError(ModelAPIError):
24
26
  class ChatAPIError(ModelAPIError):
25
27
  """聊天请求失败或响应不可用。"""
26
28
 
27
- def __init__(self, message: str, *, retryable: bool = False) -> None:
29
+ def __init__(
30
+ self,
31
+ message: str,
32
+ *,
33
+ retryable: bool = False,
34
+ status_code: int | None = None,
35
+ ) -> None:
28
36
  super().__init__(message)
29
37
  self.retryable = retryable
38
+ self.status_code = status_code
30
39
 
31
40
 
32
41
  def _make_client(timeout: float) -> httpx.AsyncClient:
@@ -39,7 +48,19 @@ def _endpoint(base_url: str, path: str) -> str:
39
48
 
40
49
 
41
50
  def _auth_headers(api_key: str) -> dict[str, str]:
42
- return {"Authorization": f"Bearer {api_key.strip()}"}
51
+ return {
52
+ "Authorization": f"Bearer {api_key.strip()}",
53
+ "User-Agent": CLIENT_USER_AGENT,
54
+ }
55
+
56
+
57
+ def _chat_headers(
58
+ base_url: str, api_key: str, session_id: str | None
59
+ ) -> dict[str, str]:
60
+ headers = _auth_headers(api_key)
61
+ if session_id and urlparse(base_url).hostname == "opencode.ai":
62
+ headers["x-opencode-session"] = session_id
63
+ return headers
43
64
 
44
65
 
45
66
  class EmbeddingClient:
@@ -105,7 +126,9 @@ class ChatClient:
105
126
  self._config = config
106
127
  self._retry_delays = tuple(retry_delays)
107
128
 
108
- async def complete(self, context: ChatContext, current_text: str) -> str:
129
+ async def complete(
130
+ self, context: ChatContext, current_text: str, *, session_id: str | None = None
131
+ ) -> str:
109
132
  """按上下文请求一条回复;瞬时失败按配置次数重试,最终失败抛出 ChatAPIError。"""
110
133
  self._config.require_chat_config()
111
134
  if not current_text.strip():
@@ -118,7 +141,7 @@ class ChatClient:
118
141
  attempts = len(self._retry_delays) + 1
119
142
  for attempt in range(attempts):
120
143
  try:
121
- response = await self._post(payload)
144
+ response = await self._post(payload, session_id)
122
145
  except ChatAPIError as exc:
123
146
  if not exc.retryable or attempt + 1 >= attempts:
124
147
  raise
@@ -127,9 +150,16 @@ class ChatClient:
127
150
  return self._parse(response)
128
151
  raise ChatAPIError("聊天请求重试次数已用尽。")
129
152
 
130
- async def _post(self, payload: dict[str, Any]) -> httpx.Response:
131
- url = _endpoint(self._config.group_chat_memory_chat_base_url, "chat/completions")
132
- headers = _auth_headers(self._config.group_chat_memory_chat_api_key.get_secret_value())
153
+ async def _post(
154
+ self, payload: dict[str, Any], session_id: str | None
155
+ ) -> httpx.Response:
156
+ base_url = self._config.group_chat_memory_chat_base_url
157
+ url = _endpoint(base_url, "chat/completions")
158
+ headers = _chat_headers(
159
+ base_url,
160
+ self._config.group_chat_memory_chat_api_key.get_secret_value(),
161
+ session_id,
162
+ )
133
163
  timeout = self._config.group_chat_memory_chat_timeout
134
164
  try:
135
165
  async with _make_client(timeout) as client:
@@ -142,6 +172,7 @@ class ChatClient:
142
172
  raise ChatAPIError(
143
173
  f"聊天服务返回 HTTP {response.status_code}。",
144
174
  retryable=response.status_code == 429 or response.status_code >= 500,
175
+ status_code=response.status_code,
145
176
  )
146
177
  return response
147
178
 
@@ -21,7 +21,7 @@ from nonebot.adapters.onebot.v11 import (
21
21
  from nonebot.params import CommandArg
22
22
  from nonebot.permission import SUPERUSER
23
23
 
24
- from .api import ChatClient, EmbeddingClient
24
+ from .api import ChatAPIError, ChatClient, EmbeddingClient
25
25
  from .config import get_settings
26
26
  from .context import ContextBuilder
27
27
  from .memory import MemoryWriter
@@ -79,9 +79,12 @@ async def reply(group_id: int, message: MemoryMessage) -> None:
79
79
  bot = _bots[group_id]
80
80
  try:
81
81
  context = await context_builder.build(group_id, message.text)
82
- response = await chat_client.complete(context, message.text)
82
+ response = await chat_client.complete(
83
+ context, message.text, session_id=f"group:{group_id}"
84
+ )
83
85
  except Exception as exc:
84
- logger.error("群 %s 的聊天回复失败:%s", group_id, type(exc).__name__)
86
+ status = f" HTTP {exc.status_code}" if isinstance(exc, ChatAPIError) else ""
87
+ logger.error("群 %s 的聊天回复失败:%s%s", group_id, type(exc).__name__, status)
85
88
  await bot.send_group_msg(group_id=group_id, message=GENERIC_REPLY_ERROR)
86
89
  return
87
90
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nonebot-plugin-group-chat-memory"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "为 NoneBot2 群聊提供共享长期记忆、点名对话和按群概率回复"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "TonyLiangP2010405" }]