wechat_ai/agent/main.go

91 lines
2.7 KiB
Go
Raw 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.

package main
import (
"fmt"
"os"
)
func main() {
if err := runOnce(); err != nil {
logError(err.Error())
os.Exit(1)
}
}
func runOnce() error {
logInfo("Go RPA agent demo 启动")
config, err := loadConfig("config.toml")
if err != nil {
return fmt.Errorf("读取 config.toml 失败:%w", err)
}
logInfo("已读取 config.toml模型=" + config.Volcengine.Model)
regionsPath := appDataPath(config, config.Agent.RegionsPath)
screenshotPath := appDataPath(config, config.Agent.ScreenshotPath)
annotation, err := loadAnnotation(regionsPath)
if err != nil {
return fmt.Errorf("读取标注文件失败:%w", err)
}
summaries := summarizeRegions(annotation.Regions)
summaries, inferenceWarnings := inferMissingRegionTypes(summaries)
logInfo(fmt.Sprintf("已读取标注文件regions=%d", len(summaries)))
for _, warning := range inferenceWarnings {
logWarning(warning)
}
if err := validateRequiredRegions(summaries); err != nil {
return err
}
for _, summary := range summaries {
logThink(fmt.Sprintf("区域识别:%s center=[%.1f, %.1f]", summary.Type, summary.CenterScreen[0], summary.CenterScreen[1]))
}
imageDataURL, err := loadImageDataURL(screenshotPath)
if err != nil {
return fmt.Errorf("读取截图失败:%w", err)
}
logInfo("已读取截图 WeChat.jpg")
prompt, err := buildPrompt(summaries)
if err != nil {
return fmt.Errorf("构造 prompt 失败:%w", err)
}
logThink("正在请求豆包多模态模型识别聊天内容并生成任务计划")
rawResponse, err := requestLLM(config, prompt, imageDataURL)
if err != nil {
return fmt.Errorf("请求 LLM 失败:%w", err)
}
task, _, err := parseTaskFromLLM(rawResponse)
if err != nil {
_ = saveRawResponse(config, rawResponse)
return fmt.Errorf("解析 LLM task JSON 失败,已保存 latest_task_raw.txt%w", err)
}
task = finalizeTask(task, summaries, config, regionsPath, screenshotPath)
logJSON("summary", fmt.Sprintf(
"LLM 摘要:联系人=%s最新消息=%s回复=%sconfidence=%.2f",
task.LLMSummary.ContactName,
task.LLMSummary.LatestUserMessage,
task.LLMSummary.ReplyText,
task.Confidence,
))
logTask(fmt.Sprintf("已生成任务 %stype=%sactions=%ddry_run=%v", task.TaskID, task.TaskType, len(task.Actions), config.Agent.DryRun))
for _, action := range task.Actions {
logTask(fmt.Sprintf("动作记录:%s %s status=%s target=%s", action.ActionID, action.Name, action.Status, action.TargetRegion))
}
latestPath, historyPath, err := saveTaskFiles(task, config)
if err != nil {
return fmt.Errorf("保存 task 文件失败:%w", err)
}
logInfo("已保存 latest task: " + latestPath)
logInfo("已保存 history task: " + historyPath)
logInfo("Go RPA agent demo 完成,未真实执行鼠标键盘")
return nil
}