myagent-ai 1.9.1 → 1.9.2

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 (2) hide show
  1. package/core/llm.py +28 -20
  2. package/package.json +1 -1
package/core/llm.py CHANGED
@@ -649,28 +649,36 @@ class LLMClient:
649
649
  logger.error(f"流式 LLM 调用失败: {e}")
650
650
 
651
651
  async def _stream_openai(self, kwargs: dict) -> AsyncGenerator[str, None]:
652
- """OpenAI / 兼容接口 (含 Zhipu) 流式调用"""
653
- loop = asyncio.get_running_loop()
654
-
655
- def _create_stream():
656
- return self._client.chat.completions.create(**kwargs)
657
-
658
- stream = await loop.run_in_executor(None, _create_stream)
652
+ """OpenAI / 兼容接口 (含 Zhipu) 流式调用
659
653
 
660
- # 使用迭代器在 executor 中逐步获取
661
- def _next_chunk(it):
662
- try:
663
- return next(it)
664
- except StopIteration:
665
- return None
654
+ 支持两种客户端:
655
+ - AsyncOpenAI: 使用 async for 直接异步迭代
656
+ - 同步 OpenAI: 在 executor 中同步迭代
657
+ """
658
+ # 判断客户端类型,选择合适的流式迭代方式
659
+ is_async = hasattr(self._client, '__aenter__')
660
+
661
+ if is_async:
662
+ # AsyncOpenAI: 直接异步迭代
663
+ stream = await self._client.chat.completions.create(**kwargs)
664
+ async for chunk in stream:
665
+ if chunk.choices and chunk.choices[0].delta.content:
666
+ yield chunk.choices[0].delta.content
667
+ else:
668
+ # 同步 OpenAI: 在 executor 中迭代
669
+ loop = asyncio.get_running_loop()
666
670
 
667
- iterator = iter(stream)
668
- while True:
669
- chunk = await loop.run_in_executor(None, _next_chunk, iterator)
670
- if chunk is None:
671
- break
672
- if chunk.choices and chunk.choices[0].delta.content:
673
- yield chunk.choices[0].delta.content
671
+ def _create_and_iterate():
672
+ stream = self._client.chat.completions.create(**kwargs)
673
+ results = []
674
+ for chunk in stream:
675
+ if chunk.choices and chunk.choices[0].delta.content:
676
+ results.append(chunk.choices[0].delta.content)
677
+ return results
678
+
679
+ chunks = await loop.run_in_executor(None, _create_and_iterate)
680
+ for content in chunks:
681
+ yield content
674
682
 
675
683
  async def _stream_anthropic(
676
684
  self, messages: List[Message], kwargs: dict
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myagent-ai",
3
- "version": "1.9.1",
3
+ "version": "1.9.2",
4
4
  "description": "本地桌面端执行型AI助手 - Open Interpreter 风格 | Local Desktop Execution-Oriented AI Assistant",
5
5
  "main": "main.py",
6
6
  "bin": {