Files
skill-template/llm-manager/scripts/engines/kimi.py
2026-04-04 10:35:02 +08:00

49 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import asyncio
from .base import BaseEngine
class KimiEngine(BaseEngine):
async def generate(self, prompt: str) -> str:
await self.page.goto("https://kimi.moonshot.cn/")
await self.page.wait_for_load_state("networkidle")
# 登录检测:等待输入框出现,超时则未登录
try:
editor = self.page.locator(".chat-input-editor").first
await editor.wait_for(state="visible", timeout=10000)
except Exception:
return "ERROR:REQUIRE_LOGIN"
# 输入提示词insert_text 避免换行符被误触发)
await editor.click()
await self.page.keyboard.insert_text(prompt)
await asyncio.sleep(0.5)
# 点击发送按钮
await self.page.locator(".send-button-container").first.click()
print("💡 [llm-manager/kimi] 已发送提示词,等待 Kimi 生成响应...")
# 等待 2 秒让发送按钮进入"生成中"状态
await asyncio.sleep(2)
# 等待 Send SVG 图标重新出现(表示生成完毕)
try:
send_icon = self.page.locator('.send-button-container svg[name="Send"]')
await send_icon.wait_for(state="visible", timeout=150000)
except Exception:
return "ERROR:Kimi 生成超时150秒未见完成标志可能网络卡住或内容被拦截。"
# 稳妥起见多等 2 秒,让复制按钮完全渲染
await asyncio.sleep(2)
# 点击最后一条回复的复制按钮,通过剪贴板取完整文本
try:
copy_btn = self.page.locator('svg[name="Copy"]').last
await copy_btn.click()
await asyncio.sleep(0.5)
result = await self.page.evaluate("navigator.clipboard.readText()")
return result.strip()
except Exception as e:
return f"ERROR:抓取 Kimi 内容时异常:{e}"