跳转至

Trae 与 Claude Sonnet 4 带来的惊喜!

04

从未感受过的震撼!

平时使用 IDE 的 AI 插件时,都是使用自动补全和自动生成 commit 提交信息。

01

从来没试过全程交管给 AI 写代码。

今天找了个小项目尝试了一下,让我大为震惊!

1. 项目介绍

Kubernetes 控制器:为了实现将 Pod 的 IP 直接挂载到 负载均衡 后端。

项目代码: sync-pod-to-clb-python

1.1. 运行环境

03

腾讯云 > 容器服务 > 应用场景

2. 过程

2.1. 开局就是排队 🥴

02

Trae

2.2. 代码生成

项目代码: sync-pod-to-clb-go

2.2.1. 首次(震惊)

一次就完成了所有功能。

2.2.2. 不足(三处 bug)

DescribeTargetsResponse 结构错误

tencent.go
// type DescribeTargetsResponse struct {
//  Listeners []Listener `json:"Listeners"`
// }

ctrl + c 无法退出主进程

main.go
select {
    case <-ctx.Done():
        watcher.Stop()
        return ctx.Err()
    default:
}

Dockerfile 一阶段构建错误

Dockerfile
# 复制文件
COPY go.mod go.sum ./

# 安装依赖
RUN go mod download

2.3. 优化(结构)

将客户端独立封装

tencent.go
func NewTencentClient() (*TencentClient, error) {
    secretID := os.Getenv("CLOUD_TENCENT_SECRET_ID")
    secretKey := os.Getenv("CLOUD_TENCENT_SECRET_KEY")
    region := os.Getenv("TENCENT_REGION")

    if secretID == "" || secretKey == "" {
        return nil, fmt.Errorf("CLOUD_TENCENT_SECRET_ID and CLOUD_TENCENT_SECRET_KEY must be set")
    }

    if region == "" {
        region = "ap-beijing" // 默认区域
    }

    credential := common.NewCredential(secretID, secretKey)
    cpf := profile.NewClientProfile()
    cpf.HttpProfile.Endpoint = "clb.tencentcloudapi.com"

    client, err := clb.NewClient(credential, region, cpf)
    if err != nil {
        return nil, fmt.Errorf("failed to create tencent client: %v", err)
    }

    return &TencentClient{
        client: client,
        region: region,
    }, nil
}

2.4. 后续改动(不再赘述)

  1. 将使用 kube-config 配置文件替换为 rbac 权限
  2. ctrl + c 退出主进程

3. 震惊的点

  1. 一次解决95%的问题
  2. 代码生成的质量非常高
  3. 第三方包(腾讯云SDK)的替换很准确
  4. 生成的代码可直接用于生产环境
  5. 测试用例的编写也非常准确
  6. 迁移说明很完善

4. 不足

  1. 排队时间长
  2. 上下文限制(老生常谈),很容易出现(复现时出现)

5. 结束

一次就完成了所有功能,虽然项目代码量小、结构相对简单,但对于我来说节省了很多时间和精力。

对于小项目换编程语言真的很适合。

当信任基础与普及程度形成良性循环时,便会产生强大的协同效应。 -- 里德·霍夫曼