Coze智能体开发实战-单Agent综合实战

添加Webhooks

第1步: 创建gitee账号,步骤省。

第2步:某仓库 - 管理 - 添加webHook

第3步:填写URL、勾选pull,不设密码

  • URL 可以是 http 或 https 协议,也支持 http + IP 地址 + 端口的格式,如 http://127.0.0.1:8080/project,但要是外网可直接访问的地址,内网地址无内网穿透则无法使用。
  • 密码 为了保证安全以及识别数据来源,建议设置一个密码,Gitee 将会在 Post 数据中携带这个密码,不设密码可以不填。

第4步:index.php源码

php 复制代码
<?php 
// 步骤1:检查仓库是否存在,若不存在则克隆
function cloneRepoIfNotExists($repoUrl, $localPath) {
    if (!is_dir($localPath)) { 
        $command = "git clone $repoUrl $localPath";
        exec($command, $output, $returnCode);
        if ($returnCode === 0) {
            return true;
        } else {
            echo "Failed to clone repository: " . implode("\n", $output) . "\n";
            return false;
        }
    }
    return true;
}

// 步骤2:拉取最新代码
function gitPull($localPath) {
    chdir($localPath);
    $command = "git pull";
    exec($command, $output, $returnCode);
    if ($returnCode === 0) {
        return true;
    } else {
        echo "Failed to pull the latest code: " . implode("\n", $output) . "\n";
        return false;
    }
}

// 步骤3:获取增量代码
function getIncrementlCode($oldCommit, $newCommit, $localPath) {
    chdir($localPath);
    // 执行 git diff 命令获取增量代码
    $command = "git diff $oldCommit $newCommit";
    exec($command, $output, $returnCode);
    if ($returnCode === 0) {
        // 将输出合并为一个字符串
        $incrementalCode = implode("\n", $output);
        return $incrementalCode;
    } else {
        echo "Failed to get incremental code: " . implode("\n", $output) . "\n";
        return null;
    }
}

// 步骤4:改写curl代码为PHP调用接口的方式
function callApi($incrementalCode) {
    $url = 'https://api.coze.cn/v1/workflow/run';
    $token = '你自己的coze令牌';
    $workflowId = '你自己的工作流ID';

    $data = [
        "parameters" => [
            "input" => $incrementalCode
        ],
        "workflow_id" => $workflowId
    ];

    $dataJson = json_encode($data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: Bearer $token",
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 修正:curl_getinfo 用于获取 HTTP 状态码

    curl_close($ch);

    return [
        'http_code' => $httpCode,
        'response' => $response
    ];
}

// 步骤5: 处理 Gitee Webhook请求
// 假设Gitee Webhook 发送的是JSON数据
$inputJSON = file_get_contents('php://input'); // 修正:file_get_content -> file_get_contents
$input = json_decode($inputJSON, TRUE);

// 检查是否为 push 事件
if ($input && isset($input['ref']) && isset($input['before']) && isset($input['after']) && isset($input['repository']['git_http_url'])) {
    $oldCommit = $input['before'];
    $newCommit = $input['after'];
    $repoUrl = $input['repository']['git_http_url'];
    $repoName = basename($repoUrl, '.git');
    $localRepoPath = __DIR__ . '/' . $repoName;

    // 检查并克隆仓库
    if (cloneRepoIfNotExists($repoUrl, $localRepoPath)) {
        // 执行 git pull 操作
        if (gitPull($localRepoPath)) {
            // 获取增量代码
            $incrementalCode = getIncrementlCode($oldCommit, $newCommit, $localRepoPath);

            if ($incrementalCode) {
                // 调用 API 并传递增量代码
                $result = callApi($incrementalCode);
            }
        }
    }
}

代码审查工作流

原理:提交代码后,通过Webhooks,调用工作流检查代码是否符合规范,如果不符合,推送消息给企业微信机器人。

第1步:工作空间 - 资源库 - "+资源" - 工作流

第2步:连接"大模型"

第3步:测试节点

第4步:连接"结束"节点

第5步:试运行

第6步:发布

第7步:工作空间 - 资源库 - 工作流

第8步:单击"check_python_code"名称

复制workflow_id的值,替换index.php的$workflowId的值

第9步:让外部应用访问我们的工作流

第10步:扣子API - 授权 - 个人访问令牌 - 添加新令牌

第11步:弹出对话框里,一定要复制好令牌。因为只显示一次。替换index.php里$token的值。

相关推荐
lypzcgf4 小时前
Coze源码分析-资源库-编辑工作流-前端源码-核心流程/API/总结
前端·工作流·coze·coze源码分析·智能体平台·ai应用平台·agent平台
lypzcgf4 小时前
Coze源码分析-资源库-编辑工作流-前端源码-核心组件
前端·工作流·coze·coze源码分析·智能体平台·agent平台
lypzcgf1 天前
Coze源码分析-资源库-编辑插件-后端源码-领域/数据访问层
系统架构·插件·coze·coze源码分析·智能体平台·ai应用平台·agent平台
lypzcgf2 天前
Coze源码分析-资源库-编辑插件-后端源码-安全与错误处理
安全·插件·coze·coze源码分析·智能体平台·ai应用平台·agent平台
后端小肥肠2 天前
Coze+liblib 强强联合!阿容容治愈插画、灵魂画手素描、火柴人漫画,一键生成不翻车
人工智能·aigc·coze
lypzcgf3 天前
Coze源码分析-资源库-编辑插件-后端源码-核心技术与总结
系统架构·插件·coze·coze源码分析·智能体平台·ai应用平台·agent平台
lypzcgf4 天前
Coze源码分析-资源库-编辑插件-后端源码-IDL/API/应用服务层
系统架构·插件·coze·coze源码分析·智能体平台·ai应用平台·agent平台
袁庭新4 天前
不懂编程也能开发出自己的扣子AI智能体与工作流
agent·coze
lypzcgf5 天前
Coze源码分析-资源库-删除数据库-后端源码-领域服务/数据访问层
数据库·go·coze·coze源码分析·智能体平台·ai应用平台·agent平台
lypzcgf6 天前
Coze源码分析-资源库-删除数据库-后端源码-流程/核心技术/总结
数据库·go·coze·coze源码分析·智能体平台·ai应用平台·agent平台