xiaozhi-sdk 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl

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.

Potentially problematic release.


This version of xiaozhi-sdk might be problematic. Click here for more details.

xiaozhi_sdk/__init__.py CHANGED
@@ -1,13 +1,14 @@
1
1
  import asyncio
2
2
  import json
3
3
  import os
4
+ import re
4
5
  import uuid
5
6
  from collections import deque
6
- from typing import Any, Callable, Dict
7
+ from typing import Any, Callable, Dict, Optional
7
8
 
8
9
  import websockets
9
10
 
10
- from xiaozhi_sdk.config import INPUT_SERVER_AUDIO_SAMPLE_RATE, WSS_URL
11
+ from xiaozhi_sdk.config import INPUT_SERVER_AUDIO_SAMPLE_RATE
11
12
  from xiaozhi_sdk.iot import OtaDevice
12
13
  from xiaozhi_sdk.mcp import McpTool
13
14
  from xiaozhi_sdk.utils import get_wav_info, read_audio_file, setup_opus
@@ -19,26 +20,40 @@ from xiaozhi_sdk.opus import AudioOpus
19
20
  class XiaoZhiWebsocket(McpTool):
20
21
 
21
22
  def __init__(
22
- self, message_handler_callback=None, url=None, ota_url=None, audio_sample_rate=16000, audio_channels=1
23
+ self,
24
+ message_handler_callback: Optional[Callable] = None,
25
+ url: Optional[str] = None,
26
+ ota_url: Optional[str] = None,
27
+ audio_sample_rate: int = 16000,
28
+ audio_channels: int = 1,
23
29
  ):
24
30
  super().__init__()
25
- self.url = url or WSS_URL
31
+ self.url = url
26
32
  self.ota_url = ota_url
27
- self.audio_sample_rate = audio_sample_rate
28
33
  self.audio_channels = audio_channels
29
34
  self.audio_opus = AudioOpus(audio_sample_rate, audio_channels)
35
+
36
+ # 客户端标识
30
37
  self.client_id = str(uuid.uuid4())
31
- self.mac_addr = None
38
+ self.mac_addr: Optional[str] = None
39
+
40
+ # 回调函数
32
41
  self.message_handler_callback = message_handler_callback
33
42
 
43
+ # 连接状态
34
44
  self.hello_received = asyncio.Event()
35
45
  self.session_id = ""
36
- self.audio_queue = deque()
37
46
  self.websocket = None
38
- self.message_handler_task = None
39
- self.ota = None
47
+ self.message_handler_task: Optional[asyncio.Task] = None
48
+
49
+ # 输出音频
50
+ self.output_audio_queue: deque[bytes] = deque()
40
51
 
41
- async def send_hello(self, aec: bool):
52
+ # OTA设备
53
+ self.ota: Optional[OtaDevice] = None
54
+
55
+ async def _send_hello(self, aec: bool) -> None:
56
+ """发送hello消息"""
42
57
  hello_message = {
43
58
  "type": "hello",
44
59
  "version": 1,
@@ -54,61 +69,109 @@ class XiaoZhiWebsocket(McpTool):
54
69
  await self.websocket.send(json.dumps(hello_message))
55
70
  await asyncio.wait_for(self.hello_received.wait(), timeout=10.0)
56
71
 
57
- async def start_listen(self):
72
+ async def _start_listen(self) -> None:
73
+ """开始监听"""
74
+
58
75
  listen_message = {"session_id": self.session_id, "type": "listen", "state": "start", "mode": "realtime"}
59
76
  await self.websocket.send(json.dumps(listen_message))
60
77
 
61
- async def set_mcp_tool_callback(self, tool_func: Dict[str, Callable[..., Any]]):
62
- self.tool_func = tool_func
63
-
64
- async def activate_iot_device(self, ota_info):
65
- if ota_info.get("activation"):
66
- await self.send_demo_audio()
67
- challenge = ota_info["activation"]["challenge"]
68
- await asyncio.sleep(3)
69
- for _ in range(10):
70
- if await self.ota.check_activate(challenge):
71
- break
72
- await asyncio.sleep(3)
78
+ async def _activate_iot_device(self, ota_info: Dict[str, Any]) -> None:
79
+ """激活IoT设备"""
80
+ if not ota_info.get("activation"):
81
+ return
73
82
 
74
- async def init_connection(self, mac_addr: str, aec: bool = False):
75
- self.mac_addr = mac_addr
76
- self.ota = OtaDevice(self.mac_addr, self.client_id, self.ota_url)
77
- ota_info = await self.ota.activate_device()
83
+ if not self.ota:
84
+ return
78
85
 
79
- headers = {
80
- "Authorization": "Bearer test-token",
81
- "Protocol-Version": "1",
82
- "Device-Id": mac_addr,
83
- "Client-Id": self.client_id,
84
- }
86
+ await self._send_demo_audio()
87
+ challenge = ota_info["activation"]["challenge"]
88
+ await asyncio.sleep(3)
85
89
 
86
- self.websocket = await websockets.connect(uri=self.url, additional_headers=headers)
87
- self.message_handler_task = asyncio.create_task(self.message_handler())
88
- await self.send_hello(aec)
89
- await self.start_listen()
90
- asyncio.create_task(self.activate_iot_device(ota_info))
90
+ for _ in range(10):
91
+ if await self.ota.check_activate(challenge):
92
+ break
93
+ await asyncio.sleep(3)
91
94
 
92
- async def send_demo_audio(self):
95
+ async def _send_demo_audio(self) -> None:
96
+ """发送演示音频"""
93
97
  current_dir = os.path.dirname(os.path.abspath(__file__))
94
- wav_path = os.path.join(current_dir, "../file/greet.wav")
95
- framerate, nchannels = get_wav_info(wav_path)
96
- audio_opus = AudioOpus(framerate, nchannels)
98
+ wav_path = os.path.join(current_dir, "../file/audio/greet.wav")
99
+ framerate, channels = get_wav_info(wav_path)
100
+ audio_opus = AudioOpus(framerate, channels)
97
101
 
98
102
  for pcm_data in read_audio_file(wav_path):
99
103
  opus_data = await audio_opus.pcm_to_opus(pcm_data)
100
104
  await self.websocket.send(opus_data)
101
105
  await self.send_silence_audio()
102
106
 
103
- async def send_silence_audio(self, duration_seconds: float = 1.2):
104
- # 发送 静音数据
107
+ async def send_silence_audio(self, duration_seconds: float = 1.2) -> None:
108
+ """发送静音音频"""
105
109
  frames_count = int(duration_seconds * 1000 / 60)
106
110
  pcm_frame = b"\x00\x00" * int(INPUT_SERVER_AUDIO_SAMPLE_RATE / 1000 * 60)
107
111
 
108
112
  for _ in range(frames_count):
109
113
  await self.send_audio(pcm_frame)
110
114
 
111
- async def send_audio(self, pcm: bytes):
115
+ async def _handle_websocket_message(self, message: Any) -> None:
116
+ """处理接受到的WebSocket消息"""
117
+ if isinstance(message, bytes):
118
+ pcm_array = await self.audio_opus.opus_to_pcm(message)
119
+ self.output_audio_queue.extend(pcm_array)
120
+ else:
121
+ data = json.loads(message)
122
+ message_type = data["type"]
123
+
124
+ if message_type == "hello":
125
+ self.hello_received.set()
126
+ self.session_id = data["session_id"]
127
+ elif message_type == "mcp":
128
+ await self.mcp(data)
129
+ elif self.message_handler_callback:
130
+ await self.message_handler_callback(data)
131
+
132
+ async def _message_handler(self) -> None:
133
+ """消息处理器"""
134
+ try:
135
+ async for message in self.websocket:
136
+ await self._handle_websocket_message(message)
137
+ except websockets.ConnectionClosed:
138
+ if self.message_handler_callback:
139
+ await self.message_handler_callback(
140
+ {"type": "websocket", "state": "close", "source": "sdk.message_handler"}
141
+ )
142
+
143
+ async def set_mcp_tool_callback(self, tool_func: Dict[str, Callable[..., Any]]) -> None:
144
+ """设置MCP工具回调函数"""
145
+ self.tool_func = tool_func
146
+
147
+ async def init_connection(self, mac_addr: str, aec: bool = False) -> None:
148
+ """初始化连接"""
149
+ # 校验MAC地址格式 XX:XX:XX:XX:XX:XX
150
+ mac_pattern = r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$"
151
+ if not re.match(mac_pattern, mac_addr):
152
+ raise ValueError(f"无效的MAC地址格式: {mac_addr}。正确格式应为 XX:XX:XX:XX:XX:XX")
153
+
154
+ self.mac_addr = mac_addr.lower()
155
+ self.ota = OtaDevice(self.mac_addr, self.client_id, self.ota_url)
156
+ ota_info = await self.ota.activate_device()
157
+
158
+ headers = {
159
+ "Authorization": "Bearer {}".format(ota_info["websocket"]["token"]),
160
+ "Protocol-Version": "1",
161
+ "Device-Id": self.mac_addr,
162
+ "Client-Id": self.client_id,
163
+ }
164
+ self.url = self.url or ota_info["websocket"]["url"]
165
+ self.websocket = await websockets.connect(uri=self.url, additional_headers=headers)
166
+ self.message_handler_task = asyncio.create_task(self._message_handler())
167
+
168
+ await self._send_hello(aec)
169
+ await self._start_listen()
170
+ asyncio.create_task(self._activate_iot_device(ota_info))
171
+ await asyncio.sleep(0.5)
172
+
173
+ async def send_audio(self, pcm: bytes) -> None:
174
+ """发送音频数据"""
112
175
  if not self.websocket:
113
176
  return
114
177
 
@@ -119,34 +182,13 @@ class XiaoZhiWebsocket(McpTool):
119
182
  elif state in [websockets.protocol.State.CLOSED, websockets.protocol.State.CLOSING]:
120
183
  if self.message_handler_callback:
121
184
  await self.message_handler_callback({"type": "websocket", "state": "close", "source": "sdk.send_audio"})
185
+ self.websocket = None
122
186
  await asyncio.sleep(0.5)
123
187
  else:
124
188
  await asyncio.sleep(0.1)
125
189
 
126
- async def message_handler(self):
127
- try:
128
- async for message in self.websocket:
129
- if isinstance(message, bytes):
130
- pcm_array = await self.audio_opus.opus_to_pcm(message)
131
- self.audio_queue.extend(pcm_array)
132
- else:
133
- data = json.loads(message)
134
- message_type = data["type"]
135
-
136
- if message_type == "hello":
137
- self.hello_received.set()
138
- self.session_id = data["session_id"]
139
- elif message_type == "mcp":
140
- await self.mcp(data)
141
- elif self.message_handler_callback:
142
- await self.message_handler_callback(data)
143
- except websockets.ConnectionClosed:
144
- if self.message_handler_callback:
145
- await self.message_handler_callback(
146
- {"type": "websocket", "state": "close", "source": "sdk.message_handler"}
147
- )
148
-
149
- async def close(self):
190
+ async def close(self) -> None:
191
+ """关闭连接"""
150
192
  if self.message_handler_task and not self.message_handler_task.done():
151
193
  self.message_handler_task.cancel()
152
194
  try:
xiaozhi_sdk/__main__.py CHANGED
@@ -1,8 +1,9 @@
1
1
  import argparse
2
2
  import asyncio
3
- import re
3
+ import logging
4
4
  import time
5
5
  from collections import deque
6
+ from typing import Optional
6
7
 
7
8
  import numpy as np
8
9
  import sounddevice as sd
@@ -10,84 +11,90 @@ import sounddevice as sd
10
11
  from xiaozhi_sdk import XiaoZhiWebsocket
11
12
  from xiaozhi_sdk.config import INPUT_SERVER_AUDIO_SAMPLE_RATE
12
13
 
13
- input_audio: deque[bytes] = deque()
14
+ # 配置logging
15
+ logging.basicConfig(
16
+ level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
17
+ )
18
+ logger = logging.getLogger("xiaozhi_sdk")
14
19
 
15
- is_play_audio = False
20
+ # 全局状态
21
+ input_audio_buffer: deque[bytes] = deque()
22
+ is_playing_audio = False
16
23
 
17
24
 
18
- async def message_handler_callback(message):
19
- print("message received:", message)
25
+ async def handle_message(message):
26
+ """处理接收到的消息"""
27
+ logger.info("message received: %s", message)
20
28
 
21
29
 
22
- async def assistant_audio_play(audio_queue):
23
- global is_play_audio
24
- # 创建一个持续播放的流
30
+ async def play_assistant_audio(audio_queue: deque[bytes]):
31
+ """播放音频流"""
32
+ global is_playing_audio
33
+
25
34
  stream = sd.OutputStream(samplerate=INPUT_SERVER_AUDIO_SAMPLE_RATE, channels=1, dtype=np.int16)
26
35
  stream.start()
27
- last_time = None
36
+ last_audio_time = None
28
37
 
29
38
  while True:
30
-
31
39
  if not audio_queue:
32
40
  await asyncio.sleep(0.01)
33
- if last_time and time.time() - last_time > 1:
34
- is_play_audio = False
41
+ if last_audio_time and time.time() - last_audio_time > 1:
42
+ is_playing_audio = False
35
43
  continue
36
44
 
37
- is_play_audio = True
45
+ is_playing_audio = True
38
46
  pcm_data = audio_queue.popleft()
39
47
  stream.write(pcm_data)
40
- last_time = time.time()
48
+ last_audio_time = time.time()
49
+
41
50
 
51
+ class XiaoZhiClient:
52
+ """小智客户端类"""
42
53
 
43
- class Client:
44
- def __init__(self, mac_address, url=None, ota_url=None):
54
+ def __init__(self, mac_address: str, url: Optional[str] = None, ota_url: Optional[str] = None):
45
55
  self.mac_address = mac_address
46
- self.xiaozhi = None
56
+ self.xiaozhi: Optional[XiaoZhiWebsocket] = None
47
57
  self.url = url
48
58
  self.ota_url = ota_url
49
59
 
50
60
  async def start(self):
51
- self.xiaozhi = XiaoZhiWebsocket(message_handler_callback, url=self.url, ota_url=self.ota_url)
61
+ """启动客户端连接"""
62
+ self.xiaozhi = XiaoZhiWebsocket(handle_message, url=self.url, ota_url=self.ota_url)
52
63
  await self.xiaozhi.init_connection(self.mac_address, aec=False)
53
- asyncio.create_task(assistant_audio_play(self.xiaozhi.audio_queue))
64
+ asyncio.create_task(play_assistant_audio(self.xiaozhi.output_audio_queue))
54
65
 
55
- def callback_func(self, indata, frames, time, status):
56
- pcm = (indata.flatten() * 32767).astype(np.int16).tobytes()
57
- input_audio.append(pcm)
66
+ def audio_callback(self, indata, frames, time, status):
67
+ """音频输入回调函数"""
68
+ pcm_data = (indata.flatten() * 32767).astype(np.int16).tobytes()
69
+ input_audio_buffer.append(pcm_data)
58
70
 
59
- async def process_audio(self):
71
+ async def process_audio_input(self):
72
+ """处理音频输入"""
60
73
  while True:
61
- if not input_audio:
74
+ if not input_audio_buffer:
62
75
  await asyncio.sleep(0.02)
63
76
  continue
64
- pcm = input_audio.popleft()
65
- if not is_play_audio:
66
- await self.xiaozhi.send_audio(pcm)
67
77
 
68
-
69
- def mac_address(string):
70
- """验证是否为有效的MAC地址"""
71
- if re.fullmatch(r"([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}", string):
72
- return string
73
- else:
74
- raise argparse.ArgumentTypeError(f"无效的MAC地址格式: '{string}'")
78
+ pcm_data = input_audio_buffer.popleft()
79
+ if not is_playing_audio:
80
+ await self.xiaozhi.send_audio(pcm_data)
75
81
 
76
82
 
77
83
  async def main():
78
- parser = argparse.ArgumentParser(description="这是一个小智SDK。")
79
- parser.add_argument("device", type=mac_address, help="你的小智设备的MAC地址 (格式: XX:XX:XX:XX:XX:XX)")
80
- parser.add_argument("--url", help="小智服务 websocket 地址")
81
- parser.add_argument("--ota_url", help="小智 OTA 地址")
84
+ """主函数"""
85
+ parser = argparse.ArgumentParser(description="小智SDK客户端")
86
+ parser.add_argument("device", help="小智设备的MAC地址 (格式: XX:XX:XX:XX:XX:XX)")
87
+ parser.add_argument("--url", help="小智服务websocket地址")
88
+ parser.add_argument("--ota_url", help="小智OTA地址")
82
89
 
83
90
  args = parser.parse_args()
84
- client = Client(args.device, args.url, args.ota_url)
91
+ logger.info("Recording... Press Ctrl+C to stop.")
92
+
93
+ client = XiaoZhiClient(args.device, args.url, args.ota_url)
85
94
  await client.start()
86
- await asyncio.sleep(2)
87
95
 
88
- with sd.InputStream(callback=client.callback_func, channels=1, samplerate=16000, blocksize=960):
89
- print("Recording... Press Ctrl+C to stop.")
90
- await client.process_audio() # 持续处理音频
96
+ with sd.InputStream(callback=client.audio_callback, channels=1, samplerate=16000, blocksize=960):
97
+ await client.process_audio_input()
91
98
 
92
99
 
93
100
  if __name__ == "__main__":
xiaozhi_sdk/config.py CHANGED
@@ -1,5 +1,3 @@
1
1
  INPUT_SERVER_AUDIO_SAMPLE_RATE = 16000
2
2
 
3
- WSS_URL = "wss://api.tenclass.net/xiaozhi/v1/"
4
3
  OTA_URL = "https://api.tenclass.net/xiaozhi/ota/"
5
- VL_URL = "http://api.xiaozhi.me/mcp/vision/explain"
xiaozhi_sdk/iot.py CHANGED
@@ -1,52 +1,76 @@
1
+ import hashlib
2
+ import hmac
1
3
  import json
4
+ from typing import Any, Dict, Optional
2
5
 
3
6
  import aiohttp
4
7
 
5
8
  from xiaozhi_sdk.config import OTA_URL
6
9
 
7
- USER_AGENT = "XiaoXhi-SDK/1.0"
10
+ # 常量定义
11
+ DEFAULT_APPLICATION_VERSION = "1.0.0"
12
+ BOARD_TYPE = "xiaozhi-sdk-box"
13
+ USER_AGENT = "xiaozhi-sdk/{}".format(DEFAULT_APPLICATION_VERSION)
14
+ BOARD_NAME = "xiaozhi-sdk-{}".format(DEFAULT_APPLICATION_VERSION)
8
15
 
9
16
 
10
- class OtaDevice(object):
17
+ class OtaDevice:
18
+ """
19
+ OTA设备管理类
11
20
 
12
- def __init__(self, mac_addr: str, client_id: str, ota_url: str, serial_number: str = ""):
21
+ 用于处理设备的激活和挑战验证操作。
22
+
23
+ Attributes:
24
+ ota_url (str): OTA服务器URL
25
+ mac_addr (str): 设备MAC地址
26
+ client_id (str): 客户端ID
27
+ serial_number (str): 设备序列号
28
+ """
29
+
30
+ def __init__(self, mac_addr: str, client_id: str, ota_url: Optional[str] = None, serial_number: str = "") -> None:
13
31
  self.ota_url = ota_url or OTA_URL
14
32
  self.mac_addr = mac_addr
15
33
  self.client_id = client_id
16
34
  self.serial_number = serial_number
17
35
 
18
- async def activate_device(self):
19
- header = {
36
+ def _get_base_headers(self) -> Dict[str, str]:
37
+ return {
20
38
  "user-agent": USER_AGENT,
21
39
  "Device-Id": self.mac_addr,
22
40
  "Client-Id": self.client_id,
23
41
  "Content-Type": "application/json",
24
- "serial-number": self.serial_number,
25
42
  }
43
+
44
+ async def activate_device(self) -> Dict[str, Any]:
45
+ headers = self._get_base_headers()
46
+ headers["serial-number"] = self.serial_number
47
+
26
48
  payload = {
27
- "application": {"version": "1.0.0"},
49
+ "application": {"version": DEFAULT_APPLICATION_VERSION},
28
50
  "board": {
29
- "type": "xiaozhi-sdk-box",
30
- "name": "xiaozhi-sdk-main",
51
+ "type": BOARD_TYPE,
52
+ "name": BOARD_NAME,
31
53
  },
32
54
  }
55
+
33
56
  async with aiohttp.ClientSession() as session:
34
- async with session.post(self.ota_url, headers=header, data=json.dumps(payload)) as response:
35
- data = await response.json()
36
- return data
57
+ async with session.post(self.ota_url, headers=headers, data=json.dumps(payload)) as response:
58
+ response.raise_for_status()
59
+ return await response.json()
60
+
61
+ async def check_activate(self, challenge: str, license_key: str = "") -> bool:
62
+ url = f"{self.ota_url}/activate"
63
+ headers = self._get_base_headers()
64
+
65
+ hmac_instance = hmac.new(license_key.encode(), challenge.encode(), hashlib.sha256)
66
+ hmac_result = hmac_instance.hexdigest()
37
67
 
38
- async def check_activate(self, challenge: str):
39
- url = self.ota_url + "/activate"
40
- header = {
41
- "user-agent": USER_AGENT,
42
- "Device-Id": self.mac_addr,
43
- "Client-Id": self.client_id,
44
- "Content-Type": "application/json",
45
- }
46
68
  payload = {
47
69
  "serial_number": self.serial_number,
48
70
  "challenge": challenge,
71
+ "hmac": hmac_result
49
72
  }
73
+
50
74
  async with aiohttp.ClientSession() as session:
51
- async with session.post(url, headers=header, data=json.dumps(payload)) as response:
75
+ async with session.post(url, headers=headers, data=json.dumps(payload)) as response:
52
76
  return response.status == 200
xiaozhi_sdk/mcp.py CHANGED
@@ -1,16 +1,19 @@
1
1
  import json
2
+ import logging
2
3
 
3
4
  import requests
4
5
 
5
- from xiaozhi_sdk.config import VL_URL
6
6
  from xiaozhi_sdk.data import mcp_initialize_payload, mcp_tool_conf, mcp_tools_payload
7
7
 
8
+ logger = logging.getLogger("xiaozhi_sdk")
9
+
8
10
 
9
11
  class McpTool(object):
10
12
 
11
13
  def __init__(self):
12
14
  self.session_id = ""
13
- self.vl_token = ""
15
+ self.explain_url = ""
16
+ self.explain_token = ""
14
17
  self.websocket = None
15
18
  self.tool_func = {}
16
19
 
@@ -30,48 +33,68 @@ class McpTool(object):
30
33
  )
31
34
 
32
35
  async def analyze_image(self, img_byte: bytes, question: str = "这张图片里有什么?"):
33
- headers = {"Authorization": f"Bearer {self.vl_token}"}
36
+ headers = {"Authorization": f"Bearer {self.explain_token}"}
34
37
  files = {"file": ("camera.jpg", img_byte, "image/jpeg")}
35
38
  payload = {"question": question}
36
-
37
- response = requests.post(VL_URL, files=files, data=payload, headers=headers)
38
- return response.json()
39
+ try:
40
+ response = requests.post(self.explain_url, files=files, data=payload, headers=headers, timeout=5)
41
+ res_json = response.json()
42
+ except Exception as e:
43
+ return "网络异常", True
44
+ if res_json.get("error"):
45
+ return res_json, True
46
+ return res_json, False
39
47
 
40
48
  async def mcp_tool_call(self, mcp_json: dict):
41
49
  tool_name = mcp_json["params"]["name"]
42
50
  tool_func = self.tool_func[tool_name]
51
+ try:
52
+ tool_res, is_error = tool_func(mcp_json["params"]["arguments"])
53
+ except Exception as e:
54
+ logger.error("tool_func error: %s", e)
55
+ return
43
56
 
44
57
  if tool_name == "take_photo":
45
- res = await self.analyze_image(tool_func(None), mcp_json["params"]["arguments"]["question"])
46
- else:
47
- res = tool_func(mcp_json["params"]["arguments"])
58
+ tool_res, is_error = await self.analyze_image(tool_res, mcp_json["params"]["arguments"]["question"])
48
59
 
49
- content = json.dumps(res, ensure_ascii=False)
50
- return self._build_response(mcp_json["id"], content)
60
+ content = json.dumps(tool_res, ensure_ascii=False)
61
+ return self._build_response(mcp_json["id"], content, is_error)
51
62
 
52
63
  async def mcp(self, data: dict):
53
64
  payload = data["payload"]
54
65
  method = payload["method"]
55
66
 
56
67
  if method == "initialize":
57
- self.vl_token = payload["params"]["capabilities"]["vision"]["token"]
68
+ self.explain_url = payload["params"]["capabilities"]["vision"]["url"]
69
+ self.explain_token = payload["params"]["capabilities"]["vision"]["token"]
70
+
58
71
  mcp_initialize_payload["id"] = payload["id"]
59
72
  await self.websocket.send(self.get_mcp_json(mcp_initialize_payload))
60
73
 
74
+ elif method == "notifications/initialized":
75
+ # print("\nMCP 工具初始化")
76
+ pass
77
+
78
+ elif method == "notifications/cancelled":
79
+ logger.error("MCP 工具加载失败")
80
+
61
81
  elif method == "tools/list":
62
82
  mcp_tools_payload["id"] = payload["id"]
83
+ tool_list = []
63
84
  for name, func in self.tool_func.items():
64
85
  if func:
86
+ tool_list.append(name)
65
87
  mcp_tool_conf[name]["name"] = name
66
88
  mcp_tools_payload["result"]["tools"].append(mcp_tool_conf[name])
67
-
68
89
  await self.websocket.send(self.get_mcp_json(mcp_tools_payload))
90
+ if tool_list:
91
+ logger.info("MCP 加载成功,当前可用工具列表为:%s", tool_list)
69
92
 
70
93
  elif method == "tools/call":
71
- print("tools/call", payload)
72
94
  tool_name = payload["params"]["name"]
73
95
  if not self.tool_func.get(tool_name):
74
96
  raise Exception("Tool not found")
75
-
76
97
  mcp_res = await self.mcp_tool_call(payload)
77
98
  await self.websocket.send(mcp_res)
99
+ else:
100
+ logger.warning("unknown method %s: %s", method, payload)
xiaozhi_sdk/utils.py CHANGED
@@ -1,6 +1,7 @@
1
1
  import ctypes.util
2
- import wave
2
+ import os
3
3
  import platform
4
+ import wave
4
5
 
5
6
 
6
7
  def get_wav_info(file_path):
@@ -27,26 +28,28 @@ def read_audio_file(file_path):
27
28
 
28
29
 
29
30
  def setup_opus():
31
+
30
32
  def fake_find_library(name):
33
+ current_dir = os.path.dirname(os.path.abspath(__file__))
31
34
  if name == "opus":
32
35
  system = platform.system().lower()
33
36
  machine = platform.machine().lower()
34
-
37
+
35
38
  # 检测架构
36
- if machine in ['x86_64', 'amd64', 'x64']:
37
- arch = 'x64'
38
- elif machine in ['arm64', 'aarch64']:
39
- arch = 'arm64'
39
+ if machine in ["x86_64", "amd64", "x64"]:
40
+ arch = "x64"
41
+ elif machine in ["arm64", "aarch64"]:
42
+ arch = "arm64"
40
43
  else:
41
44
  # 默认使用x64作为回退
42
- arch = 'x64'
43
-
45
+ arch = "x64"
46
+
44
47
  if system == "darwin": # macOS
45
- return f"./libs/macos/{arch}/libopus.dylib"
48
+ return f"{current_dir}/../file/opus/macos-{arch}-libopus.dylib"
46
49
  elif system == "windows": # Windows
47
- return f"./libs/windows/{arch}/opus.dll"
50
+ return f"{current_dir}/../file/opus/windows-{arch}-opus.dll"
48
51
  elif system == "linux": # Linux
49
- return f"./libs/linux/{arch}/libopus.so"
52
+ return f"{current_dir}/../file/opus/linux-{arch}-libopus.so"
50
53
  else:
51
54
  # 默认情况,尝试系统查找
52
55
  return ctypes.util.find_library(name)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xiaozhi-sdk
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: 一个用于连接和控制小智智能设备的Python SDK,支持实时音频通信、MCP工具集成和设备管理功能。
5
5
  Home-page: https://github.com/dairoot/xiaozhi-sdk
6
6
  Author: dairoot
@@ -79,7 +79,7 @@ options:
79
79
  #### 连接设备(需要提供 MAC 地址)
80
80
 
81
81
  ```bash
82
- python -m xiaozhi_sdk 00:11:22:33:44:55
82
+ python -m xiaozhi_sdk 00:22:44:66:88:00
83
83
  ```
84
84
 
85
85
  ### 2. 编程使用
@@ -0,0 +1,22 @@
1
+ file/audio/greet.wav,sha256=F60kKKFVQZyYh67_-9AJHMviuquSWHHqwGQewUSOAFg,32720
2
+ file/audio/say_hello.wav,sha256=RGo2MDUF7npGmjFPT4III0ibf7dIZ1c47jijrF0Yjaw,34146
3
+ file/audio/take_photo.wav,sha256=_DNWg31Q8NIxN3eUS4wBC7mn4MZCWLCNPuKfKPv1ojQ,51412
4
+ file/image/leijun.jpg,sha256=plhBvnB4O21RjLwH-HjNq0jH4Msy5ppA_IDWe5ieNg4,70814
5
+ file/opus/linux-arm64-libopus.so,sha256=D2H5VDUomaYuLetejCvLwCgf-iAVP0isg1yGwfsuvEE,493032
6
+ file/opus/linux-x64-libopus.so,sha256=FmXJqkxLpDzNFOHYkmOzmsp1hP0eIS5b6x_XfOs-IQA,623008
7
+ file/opus/macos-arm64-libopus.dylib,sha256=H7wXwkrGwb-hesMMZGFxWb0Ri1Y4m5GWiKsd8CfOhE8,357584
8
+ file/opus/macos-x64-libopus.dylib,sha256=MqyL_OjwSACF4Xs_-KrGbcScy4IEprr5Rlkk3ddZye8,550856
9
+ file/opus/windows-x86_64-opus.dll,sha256=kLfhioMvbJhOgNMAldpWk3DCZqC5Xd70LRbHnACvAnw,463360
10
+ xiaozhi_sdk/__init__.py,sha256=w_qhmG3_KPZM3753ovJibgeRRzMlwymwvMj0xMsWK5g,7251
11
+ xiaozhi_sdk/__main__.py,sha256=aidncdraH8Zyvhmp2f_upMLnUL9LG1XwFf6NBd7Wnzo,3175
12
+ xiaozhi_sdk/config.py,sha256=mpjWWklTI2bw4zY3ZWCYvqvpfZSoF5iM7ubAP9y_8cM,90
13
+ xiaozhi_sdk/data.py,sha256=ST9ks_B23iUToacccDqa49LjdWRkvxtrxbplhVKlpqw,2527
14
+ xiaozhi_sdk/iot.py,sha256=4KW6NNA3W5gjZYXgaujkpVPONGZ3nzMB3HOmZHS3waU,2432
15
+ xiaozhi_sdk/mcp.py,sha256=Q1f9FJu7kEcLK_RHTSInV824kgRKp_uwEp_-rsRYUGE,3734
16
+ xiaozhi_sdk/opus.py,sha256=4O-kz-PcUVmpa27Vju6jv-sbwywuAXFvVL23R1-vv5o,2104
17
+ xiaozhi_sdk/utils.py,sha256=5qHAiI5Nrzeka3TofMPhAVmMovEJJa6QSrKcDM0OF4g,1703
18
+ xiaozhi_sdk-0.0.2.dist-info/licenses/LICENSE,sha256=Vwgps1iODKl43cAtME_0dawTjAzNW-O2BWiN5BHggww,1085
19
+ xiaozhi_sdk-0.0.2.dist-info/METADATA,sha256=lETEmKz2mqb87k17NCWdnRHfih5xmsGAZH9A7EAiOtM,2390
20
+ xiaozhi_sdk-0.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ xiaozhi_sdk-0.0.2.dist-info/top_level.txt,sha256=nBpue4hU5Ykm5CtYPsAdxSa_yqbtZsIT_gF_EkBaJPM,12
22
+ xiaozhi_sdk-0.0.2.dist-info/RECORD,,
@@ -1,22 +0,0 @@
1
- file/greet.wav,sha256=F60kKKFVQZyYh67_-9AJHMviuquSWHHqwGQewUSOAFg,32720
2
- file/leijun.jpg,sha256=plhBvnB4O21RjLwH-HjNq0jH4Msy5ppA_IDWe5ieNg4,70814
3
- file/say_hello.wav,sha256=RGo2MDUF7npGmjFPT4III0ibf7dIZ1c47jijrF0Yjaw,34146
4
- file/take_photo.wav,sha256=_DNWg31Q8NIxN3eUS4wBC7mn4MZCWLCNPuKfKPv1ojQ,51412
5
- libs/linux/arm64/libopus.so,sha256=D2H5VDUomaYuLetejCvLwCgf-iAVP0isg1yGwfsuvEE,493032
6
- libs/linux/x64/libopus.so,sha256=FmXJqkxLpDzNFOHYkmOzmsp1hP0eIS5b6x_XfOs-IQA,623008
7
- libs/macos/arm64/libopus.dylib,sha256=H7wXwkrGwb-hesMMZGFxWb0Ri1Y4m5GWiKsd8CfOhE8,357584
8
- libs/macos/x64/libopus.dylib,sha256=MqyL_OjwSACF4Xs_-KrGbcScy4IEprr5Rlkk3ddZye8,550856
9
- libs/win/x86_64/opus.dll,sha256=kLfhioMvbJhOgNMAldpWk3DCZqC5Xd70LRbHnACvAnw,463360
10
- xiaozhi_sdk/__init__.py,sha256=OxmYqKsXg0vcHrr5HzbsG3jJOGjhqeGMxfONkAkTD1I,6023
11
- xiaozhi_sdk/__main__.py,sha256=LyEt1-9Nk4MGMLSOyjgxiRau_-WyMxC-syE5PWMYAcA,2889
12
- xiaozhi_sdk/config.py,sha256=q4e_xmYzUB4_E5h-YftsyAhfeBSapwYD-ogx9ps1fIQ,189
13
- xiaozhi_sdk/data.py,sha256=ST9ks_B23iUToacccDqa49LjdWRkvxtrxbplhVKlpqw,2527
14
- xiaozhi_sdk/iot.py,sha256=hw2UJAMdY41AARSh7l3XTkHzV1NUiQC3YQBWTR3YSqk,1697
15
- xiaozhi_sdk/mcp.py,sha256=jvXICyZ4BAdpyCIBzw9q40JjQrzi562NQdU9-vwWQJw,2786
16
- xiaozhi_sdk/opus.py,sha256=4O-kz-PcUVmpa27Vju6jv-sbwywuAXFvVL23R1-vv5o,2104
17
- xiaozhi_sdk/utils.py,sha256=3o2wHRCG3dHcnn9_jbRzl1patgU1I2JTJJaTNb9EUys,1591
18
- xiaozhi_sdk-0.0.1.dist-info/licenses/LICENSE,sha256=Vwgps1iODKl43cAtME_0dawTjAzNW-O2BWiN5BHggww,1085
19
- xiaozhi_sdk-0.0.1.dist-info/METADATA,sha256=8g3Q-kcBWqymax4PYY0ypgVTcf_9Vl03JYlzkeWtWgs,2390
20
- xiaozhi_sdk-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
- xiaozhi_sdk-0.0.1.dist-info/top_level.txt,sha256=nBpue4hU5Ykm5CtYPsAdxSa_yqbtZsIT_gF_EkBaJPM,12
22
- xiaozhi_sdk-0.0.1.dist-info/RECORD,,
File without changes
File without changes
File without changes
File without changes