pycoze 0.1.203__py3-none-any.whl → 0.1.205__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
pycoze/bot/bot.py CHANGED
@@ -1,72 +1,72 @@
1
- from langchain_core.messages import HumanMessage, AIMessage
2
- from .agent import INPUT_MESSAGE, INTERRUPT_MESSAGE, output, CHAT_DATA, clear_chat_data
3
- from .agent_chat import agent_chat
1
+ import signal
2
+ import sys
4
3
  import json
5
- import threading
6
- import queue
7
- import time
4
+ from .agent import INPUT_MESSAGE, output, CHAT_DATA, clear_chat_data
5
+ from .agent_chat import agent_chat
6
+ from multiprocessing import Process, Event
8
7
  import asyncio
8
+ from langchain_core.messages import HumanMessage, AIMessage
9
9
 
10
+ # 用于标记中断请求
11
+ interrupt_flag = False
10
12
 
11
- class PeekableQueue(queue.Queue):
12
- def peek(self):
13
- try:
14
- return self._get()
15
- except queue.Empty:
16
- return None
13
+ # 信号处理函数
14
+ def handle_interrupt(signum, frame):
15
+ global interrupt_flag
16
+ interrupt_flag = True
17
+ print("Interrupt signal received. Waiting for the current operation to complete...")
17
18
 
18
- def chat(bot_setting_file: str):
19
- history = []
20
- input_queue = PeekableQueue()
19
+ # 设置信号处理器
20
+ signal.signal(signal.SIGINT, handle_interrupt)
21
21
 
22
- def input_thread(input_queue: PeekableQueue):
23
- while True:
22
+ def read_input():
23
+ while True:
24
+ try:
24
25
  input_text = input()
25
- if input_text == INTERRUPT_MESSAGE or input_text.startswith(INPUT_MESSAGE):
26
- input_queue.put(input_text)
26
+ if input_text.startswith(INPUT_MESSAGE):
27
+ yield input_text
27
28
  else:
28
29
  raise ValueError("Invalid message")
30
+ except EOFError: # 如果输入流结束,则退出循环
31
+ break
32
+
33
+ def chat(bot_setting_file: str):
34
+ history = []
35
+ for input_text in read_input():
36
+ try:
37
+ message = json.loads(input_text[len(INPUT_MESSAGE):])
38
+ history.append(HumanMessage(message["content"]))
39
+ clear_chat_data()
40
+
41
+ # 创建一个事件来控制子进程的退出
42
+ should_exit = Event()
29
43
 
44
+ # 使用进程来运行 agent_chat
45
+ agent_chat_process = Process(target=asyncio.run, args=(agent_chat(bot_setting_file, history, should_exit),))
46
+ agent_chat_process.start()
30
47
 
31
- input_thread_instance = threading.Thread(target=input_thread, args=(input_queue,))
32
- input_thread_instance.start()
48
+ # 检查是否收到了中断信号
49
+ while agent_chat_process.is_alive():
50
+ if interrupt_flag:
51
+ should_exit.set() # 设置退出标志
52
+ break
53
+ time.sleep(0.1) # 每隔 0.1 秒检查一次
33
54
 
34
- try:
35
- while True:
36
- if not input_queue.empty():
37
- input_text = input_queue.get()
38
- if input_text == INTERRUPT_MESSAGE:
39
- continue
40
- try:
41
- message = json.loads(input_text[len(INPUT_MESSAGE):])
42
- history.append(HumanMessage(message["content"]))
43
- clear_chat_data()
44
-
45
- # 为每个线程创建一个独立的退出标志
46
- should_exit = threading.Event()
47
- agent_chat_thread = threading.Thread(target=asyncio.run, args=(agent_chat(bot_setting_file, history, should_exit),))
48
- agent_chat_thread.start()
49
-
50
- next_input_text = ""
51
- while agent_chat_thread.is_alive():
52
- if not input_queue.empty():
53
- next_input_text = input_queue.peek()
54
- if next_input_text == INTERRUPT_MESSAGE:
55
- history.append(AIMessage(content=CHAT_DATA["info"]))
56
- should_exit.set() # 设置退出标志
57
- break
58
- time.sleep(0.1) # 每隔 0.1 秒检查一次
59
- if next_input_text != INTERRUPT_MESSAGE:
60
- history.append(AIMessage(content=CHAT_DATA["output"]))
61
- except json.JSONDecodeError:
62
- print("Invalid JSON format in input message.")
63
- except KeyError:
64
- print("Missing 'content' key in input message.")
65
- except Exception as e:
66
- print(f"An error occurred: {e}")
67
- finally:
68
- input_thread_instance.join()
55
+ # 确保子进程已经退出
56
+ agent_chat_process.join()
69
57
 
58
+ # 如果没有收到中断信号,记录输出
59
+ if not interrupt_flag:
60
+ history.append(AIMessage(content=CHAT_DATA["output"]))
61
+
62
+ # 重置中断标志
63
+ interrupt_flag = False
64
+ except json.JSONDecodeError:
65
+ print("Invalid JSON format in input message.")
66
+ except KeyError:
67
+ print("Missing 'content' key in input message.")
68
+ except Exception as e:
69
+ print(f"An error occurred: {e}")
70
70
 
71
71
 
72
72
  def get_chat_response(bot_setting_file: str, input_text: str):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.203
3
+ Version: 0.1.205
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -6,7 +6,7 @@ pycoze/automation/browser/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJW
6
6
  pycoze/automation/browser/edge_driver_manager.py,sha256=gpgseunph5owZH6EskSYthuhey2SU3UP204gY0yIcuI,3022
7
7
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
8
8
  pycoze/bot/agent_chat.py,sha256=hr0jGTd-lvLSe4vCcvaN2rKhB7BMBcVBtI1FpVa6ngs,3016
9
- pycoze/bot/bot.py,sha256=CCnBmxtmpmz7aLR_MIIiOuKR9IACo5P4uSkQXb5d0KU,3061
9
+ pycoze/bot/bot.py,sha256=oLdI9ihPEV3rz6WM30kBJDz55pC8wX90wh3R1I7f_TY,2683
10
10
  pycoze/bot/agent/__init__.py,sha256=3wE8_FFQS8j2BY-g9Cr-onV0POEvDRZaw_NCzpqrNus,265
11
11
  pycoze/bot/agent/agent.py,sha256=Ueste4xWvkQFk5Giklv4c-kcxiPL-D-KafzZ4DONosY,3613
12
12
  pycoze/bot/agent/assistant.py,sha256=3iLxnRvf_ia0cP-FHK5Fv4ylltlnzPq1KscRCFYqjkc,1147
@@ -28,8 +28,8 @@ pycoze/utils/__init__.py,sha256=Gi5EnrWZGMD2JRejgV4c_VLCXyvA2wwBFI_niDF5MUE,110
28
28
  pycoze/utils/arg.py,sha256=GtfGbMTMdaK75Fwh6MpUe1pCA5X6Ep4LFG7a72YrzjI,525
29
29
  pycoze/utils/env.py,sha256=W04lhvTHhAAC6EldP6kk2xrctqtu8K6kl1vDLZDNeh8,561
30
30
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
31
- pycoze-0.1.203.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
- pycoze-0.1.203.dist-info/METADATA,sha256=0avX2C5q7v5RHgIqf0ZMJOEj3cA5Fl37d67c6vzmvFc,726
33
- pycoze-0.1.203.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
- pycoze-0.1.203.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
- pycoze-0.1.203.dist-info/RECORD,,
31
+ pycoze-0.1.205.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
+ pycoze-0.1.205.dist-info/METADATA,sha256=ch2R45VIsF0XiCOo8lkFGOb8NIRwBix8GcbJz-pFq4Y,726
33
+ pycoze-0.1.205.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
+ pycoze-0.1.205.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
+ pycoze-0.1.205.dist-info/RECORD,,