python+qwen的api实现类似openclaw个人电脑自动化操作代理智能体bfwclaw代码

代码语言:python

所属分类:其他

代码描述:python+qwen的api实现类似openclaw个人电脑自动化操作代理智能体bfwclaw代码,bfwclaw可以帮助你完成各种任务,比如::文件格式转换、办公自动化、数据处理和分析、浏览网页,爬信息、文件操作和管理、python编程、网络请求和AP调用、设置定时任务、自动化电脑点击输入等GUI操作、执行系统命令等,为你的电脑安装一个专业解决各种问题的电脑助手吧。

代码标签: python qwen api 个人 电脑 自动化 操作 代理 智能体 bfwclaw 代码 op

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

import os
import sys
import json
import time
import base64
import shutil
import platform
import threading
import subprocess
import webbrowser
import ctypes
import tempfile
import asyncio
import queue
from io import BytesIO
from typing import List, Optional

import pyautogui
import pyperclip
import uvicorn
from PIL import Image, ImageGrab
from openai import OpenAI
from apscheduler.schedulers.background import BackgroundScheduler

from fastapi import FastAPI, Request, File, UploadFile, Form
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, StreamingResponse
from starlette.middleware.sessions import SessionMiddleware
from pydantic import BaseModel

# ================= 目录初始化 =================
WORKSPACE_DIR = os.path.join(os.getcwd(), "workspace")
UPLOAD_DIR = os.path.join(WORKSPACE_DIR, "uploads")
DOWNLOAD_DIR = os.path.join(WORKSPACE_DIR, "downloads")
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(DOWNLOAD_DIR, exist_ok=True)

# ================= 核心修复:处理 Windows 缩放 =================
if platform.system() == "Windows":
    try: ctypes.windll.shcore.SetProcessDpiAwareness(1)
    except Exception:
        try: ctypes.windll.user32.SetProcessDPIAware()
        except Exception: pass

# ================= 基础配置 =================
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "sk-") 
BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"  
MODEL_NAME = "qwen3.5-plus"   
MODEL_INPUT_SIZE = 1120

client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL)
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5

scheduler = BackgroundScheduler()
scheduler.start()

ACCOUNT_USERNAME = "admin"
ACCOUNT_PASSWORD = "123456"

# ================= 消息推送与历史会话管理器 =================
event_queue = queue.Queue()

class UISessionManager:
    def __init__(self):
        self.sessions = []
        self.current_session_id = None
        self.new_session()

    def new_session(self):
        self.current_session_id = f"session_{int(time.time())}"
        self.sessions.append({
            "id": self.current_session_id,
            "title": "新任务会话",
            "messages": [{
                "role": "ai", 
                "text": "👋 <b>你好!我是 BfwClaw。</b><br><br>我能优先通过<b>编写 Python 代码</b>处理数据。如果需要操作桌面软件,我也能<b>截屏并模拟鼠标键盘</b>。<br><br>您可以点击下方的 📎 图标上传文件让我处理。"
            }]
        })

    def add_message(self, role, text):
        for s in self.sessions:
            if s["id"] == self.current_session_id:
                if role == "user" and s["title"] == "新任务会话":
                    clean_text = text.split("<br>")[0][:15] + "..."
                    s["title"] = clean_text
                s["messages"].append({"role": role, "text": text})
                break
                
    def switch_session(self, session_id):
        for s in self.sessions:
            if s["id"] == session_id:
                self.current_session_id = session_id
                return True
        return False

ui_manager = UISessionManager()

# ================= HTML / CSS 前端模板 =================
LOGIN_HTML = """
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>AI Desktop Agent - 登录</title>
<style>
    body { background: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; margin: 0; }
    .login-box { background: white; padding: 40px; border-radius: 12px; width: 320px; box-shadow: 0 8px 24px rgba(0,0,0,0.08); text-align: center; }
    h2 { margin-bottom: 25px; color: #1a1a1a; }
    input { width: 100%; padding: 14px; margin: 10px 0; border: 1px solid #e1e4e8; border-radius: 8px; box-sizing: border-box; outline: none; font-size: 15px;}
    input:focus { border-color: #0066ff; }
    button { width: 100%; padding: 14px; background: #0066ff; color: white; border: none; border-radius: 8px; cursor: pointer; font-size: 16px; margin-top: 15px; font-weight: 500;}
    .error { color: #dc3545; margin-top: 15px; display: none; font-size: 14px;}
</style></head>
<body>
    <div class="login-box">
        <h2>工作台登录</h2>
        <input type="text" id="username" placeholder="账号" />
        <input type=.........完整代码请登录后点击上方下载按钮下载查看

网友评论0