qingxu99 commited on
Commit
77a2d62
1 Parent(s): c43e22b

捕获缺少依赖时的异常

Browse files
request_llm/bridge_stackclaude.py CHANGED
@@ -6,70 +6,71 @@ import importlib
6
  import logging
7
  import time
8
  from toolbox import get_conf
9
- from slack_sdk.errors import SlackApiError
10
- from slack_sdk.web.async_client import AsyncWebClient
11
  import asyncio
12
-
13
-
14
- """
15
- ========================================================================
16
- 第一部分:Slack API Client
17
- https://github.com/yokonsan/claude-in-slack-api
18
- ========================================================================
19
- """
20
  load_message = "正在加载Claude组件,请稍候..."
21
 
 
 
 
 
 
 
 
22
 
23
- class SlackClient(AsyncWebClient):
24
- """SlackClient类用于与Slack API进行交互,实现消息发送、接收等功能。
25
 
26
- 属性:
27
- - CHANNEL_ID:str类型,表示频道ID。
28
 
29
- 方法:
30
- - open_channel():异步方法。通过调用conversations_open方法打开一个频道,并将返回的频道ID保存在属性CHANNEL_ID中。
31
- - chat(text: str):异步方法。向已打开的频道发送一条文本消息。
32
- - get_slack_messages():异步方法。获取已打开频道的最新消息并返回消息列表,目前不支持历史消息查询。
33
- - get_reply():异步方法。循环监听已打开频道的消息,如果收到"Typing…_"结尾的消息说明Claude还在继续输出,否则结束循环。
34
 
35
- """
36
- CHANNEL_ID = None
 
 
 
37
 
38
- async def open_channel(self):
39
- response = await self.conversations_open(users=get_conf('SLACK_CLAUDE_BOT_ID')[0])
40
- self.CHANNEL_ID = response["channel"]["id"]
41
 
42
- async def chat(self, text):
43
- if not self.CHANNEL_ID:
44
- raise Exception("Channel not found.")
45
 
46
- resp = await self.chat_postMessage(channel=self.CHANNEL_ID, text=text)
47
- self.LAST_TS = resp["ts"]
 
48
 
49
- async def get_slack_messages(self):
50
- try:
51
- # TODO:暂时不支持历史消息,因为在同一个频道里存在多人使用时历史消息渗透问题
52
- resp = await self.conversations_history(channel=self.CHANNEL_ID, oldest=self.LAST_TS, limit=1)
53
- msg = [msg for msg in resp["messages"]
54
- if msg.get("user") == get_conf('SLACK_CLAUDE_BOT_ID')[0]]
55
- return msg
56
- except (SlackApiError, KeyError) as e:
57
- raise RuntimeError(f"获取Slack消息失败。")
58
-
59
- async def get_reply(self):
60
- while True:
61
- slack_msgs = await self.get_slack_messages()
62
- if len(slack_msgs) == 0:
63
- await asyncio.sleep(0.5)
64
- continue
65
-
66
- msg = slack_msgs[-1]
67
- if msg["text"].endswith("Typing…_"):
68
- yield False, msg["text"]
69
- else:
70
- yield True, msg["text"]
71
- break
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  """
75
  ========================================================================
@@ -87,8 +88,9 @@ class ClaudeHandle(Process):
87
  self.success = True
88
  self.local_history = []
89
  self.check_dependency()
90
- self.start()
91
- self.threadLock = threading.Lock()
 
92
 
93
  def check_dependency(self):
94
  try:
@@ -97,7 +99,7 @@ class ClaudeHandle(Process):
97
  self.info = "依赖检测通过,等待Claude响应。注意目前不能多人同时调用Claude接口(有线程锁),否则将导致每个人的Claude问询历史互相渗透。调用Claude时,会自动使用已配置的代理。"
98
  self.success = True
99
  except:
100
- self.info = "缺少的依赖,如果要使用Claude,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_claude.txt`安装Claude的依赖。"
101
  self.success = False
102
 
103
  def ready(self):
 
6
  import logging
7
  import time
8
  from toolbox import get_conf
 
 
9
  import asyncio
 
 
 
 
 
 
 
 
10
  load_message = "正在加载Claude组件,请稍候..."
11
 
12
+ try:
13
+ """
14
+ ========================================================================
15
+ 第一部分:Slack API Client
16
+ https://github.com/yokonsan/claude-in-slack-api
17
+ ========================================================================
18
+ """
19
 
20
+ from slack_sdk.errors import SlackApiError
21
+ from slack_sdk.web.async_client import AsyncWebClient
22
 
23
+ class SlackClient(AsyncWebClient):
24
+ """SlackClient类用于与Slack API进行交互,实现消息发送、接收等功能。
25
 
26
+ 属性:
27
+ - CHANNEL_ID:str类型,表示频道ID
 
 
 
28
 
29
+ 方法:
30
+ - open_channel():异步方法。通过调用conversations_open方法打开一个频道,并将返回的频道ID保存在属性CHANNEL_ID中。
31
+ - chat(text: str):异步方法。向已打开的频道发送一条文本消息。
32
+ - get_slack_messages():异步方法。获取已打开频道的最新消息并返回消息列表,目前不支持历史消息查询。
33
+ - get_reply():异步方法。循环监听已打开频道的消息,如果收到"Typing…_"结尾的消息说明Claude还在继续输出,否则结束循环。
34
 
35
+ """
36
+ CHANNEL_ID = None
 
37
 
38
+ async def open_channel(self):
39
+ response = await self.conversations_open(users=get_conf('SLACK_CLAUDE_BOT_ID')[0])
40
+ self.CHANNEL_ID = response["channel"]["id"]
41
 
42
+ async def chat(self, text):
43
+ if not self.CHANNEL_ID:
44
+ raise Exception("Channel not found.")
45
 
46
+ resp = await self.chat_postMessage(channel=self.CHANNEL_ID, text=text)
47
+ self.LAST_TS = resp["ts"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+ async def get_slack_messages(self):
50
+ try:
51
+ # TODO:暂时不支持历史消息,因为在同一个频道里存在多人使用时历史消息渗透问题
52
+ resp = await self.conversations_history(channel=self.CHANNEL_ID, oldest=self.LAST_TS, limit=1)
53
+ msg = [msg for msg in resp["messages"]
54
+ if msg.get("user") == get_conf('SLACK_CLAUDE_BOT_ID')[0]]
55
+ return msg
56
+ except (SlackApiError, KeyError) as e:
57
+ raise RuntimeError(f"获取Slack消息失败。")
58
+
59
+ async def get_reply(self):
60
+ while True:
61
+ slack_msgs = await self.get_slack_messages()
62
+ if len(slack_msgs) == 0:
63
+ await asyncio.sleep(0.5)
64
+ continue
65
+
66
+ msg = slack_msgs[-1]
67
+ if msg["text"].endswith("Typing…_"):
68
+ yield False, msg["text"]
69
+ else:
70
+ yield True, msg["text"]
71
+ break
72
+ except:
73
+ pass
74
 
75
  """
76
  ========================================================================
 
88
  self.success = True
89
  self.local_history = []
90
  self.check_dependency()
91
+ if self.success:
92
+ self.start()
93
+ self.threadLock = threading.Lock()
94
 
95
  def check_dependency(self):
96
  try:
 
99
  self.info = "依赖检测通过,等待Claude响应。注意目前不能多人同时调用Claude接口(有线程锁),否则将导致每个人的Claude问询历史互相渗透。调用Claude时,会自动使用已配置的代理。"
100
  self.success = True
101
  except:
102
+ self.info = "缺少的依赖,如果要使用Claude,除了基础的pip依赖以外,您还需要运行`pip install -r request_llm/requirements_slackclaude.txt`安装Claude的依赖。"
103
  self.success = False
104
 
105
  def ready(self):
request_llm/{requirements_claude.txt → requirements_slackclaude.txt} RENAMED
File without changes