Skip to content

Repository files navigation

AgentRun Component

阿里云 AgentRun 智能体运行时组件,用于部署和管理智能体运行时实例。

简介

AgentRun Component 是 Serverless Devs 的一个组件,用于部署和管理阿里云 AgentRun 智能体运行时实例。智能体运行时提供代码执行、容器运行、网络访问、日志管理等核心能力。

功能特性

  • 部署模式:支持代码模式和容器模式
  • 代码管理:支持本地代码、ZIP 包、OSS 代码包
  • 版本管理:支持版本发布和管理
  • 端点管理:支持创建端点和灰度发布
  • 网络配置:支持 VPC、公网、混合网络
  • 日志查询:支持实时日志和历史日志查询
  • 实例管理:支持实例列表和命令执行
  • 并发控制:支持预留并发配置
  • 健康检查:支持自定义健康检查配置

前置要求

  • Serverless Devs 工具已安装(版本 >= 3.0.0)
  • 已配置阿里云访问凭证(Access Key)
  • 拥有 AgentRun 服务的使用权限

快速开始

1. 安装 Serverless Devs

npm install -g @serverless-devs/s

2. 配置访问凭证

s config add

按提示输入:

  • AccountID(阿里云账号 ID)
  • AccessKeyID
  • AccessKeySecret
  • 别名(如:default)

3. 初始化项目

# 创建项目目录
mkdir my-agent-project &&cd my-agent-project
# 创建代码目录
mkdir code
# 创建示例代码
cat > code/index.py << 'EOF'#!/usr/bin/env python3import jsonfrom http.server import HTTPServer, BaseHTTPRequestHandlerclass Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() response = {"message": "Hello from AgentRun!"} self.wfile.write(json.dumps(response).encode()) def log_message(self, format, *args): print(f"{self.address_string()} - {format % args}")if __name__ == '__main__': port = 8000 server = HTTPServer(('0.0.0.0', port), Handler) print(f"Server starting on port {port}...") server.serve_forever()EOF

4. 创建配置文件

创建 s.yaml

edition: 3.0.0name: my-agentrun-appaccess: defaultresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: my-first-agentdescription: "My first agent runtime"code:
src: ./codelanguage: python3.12command:
- python3
- index.pycpu: 1.0memory: 2048port: 8000instanceConcurrency: 10logConfig: autoendpoints:
- name: productiondescription: "Production endpoint"

5. 部署

s deploy

部署成功后,查看信息:

s info

YAML 配置详解

基本配置结构

edition: 3.0.0 # Serverless Devs 版本(必须 >= 3.0.0)name: agentrun-app # 应用名称access: default # 访问凭证别名vars: # 全局变量(可选)region: cn-hangzhouenv: productionresources:
my-agent: # 资源名称component: agentrun # 组件名称props: # 组件属性region: string # 地域agent: object # 智能体配置

Agent 配置详解

必填字段

字段类型说明示例
nameString智能体运行时名称,字母开头,支持字母、数字、下划线、中划线,1-128 字符my-runtime
codecustomContainerConfigObject代码配置或容器配置(二选一)见下文

可选字段

字段类型默认值说明
descriptionString-描述信息,最多 256 字符
cpuNumber1.0CPU 核数,范围:0.05-16
memoryNumber2048内存大小(MB),范围:128-32768
portNumber8000监听端口,范围:1-65535
instanceConcurrencyNumber10实例并发数,范围:1-200
sessionIdleTimeoutSecondsNumber3600会话空闲超时(秒),范围:60-86400
internetAccessBooleantrue是否允许公网访问
vpcConfigObject-VPC 配置
environmentVariablesObject-环境变量
roleString-RAM 角色 ARN
credentialNameString-访问凭证名称
logConfigObject/String-日志配置,可设置为 auto
protocolConfigurationObject-协议配置
healthCheckConfigurationObject-健康检查配置
endpointsArray-端点配置

代码配置(Code)

基本结构

code:
src: string # 本地代码路径(目录或 ZIP)#ossBucketName: string # OSS 存储桶名称ossObjectName: string # OSS 对象名称language: string # 编程语言(必填)command: array # 运行命令(可选)checksum: string # CRC-64 校验值(可选)

支持的编程语言

  • python3.10
  • python3.12
  • nodejs18
  • nodejs20
  • java8
  • java11
  • custom

示例

本地目录

code:
src: ./codelanguage: python3.12command:
- python3
- main.py

本地 ZIP 文件

code:
src: ./code.ziplanguage: python3.12command:
- python3
- main.py

OSS 代码包

code:
ossBucketName: my-code-bucketossObjectName: agent-code.ziplanguage: python3.12command:
- python3
- main.pychecksum: "1234567890123456789"# 可选

容器配置(CustomContainerConfig)

基本结构

customContainerConfig:
image: string # 容器镜像地址(必填)command: array # 运行命令(可选)imageRegistryType: string # 镜像源类型(可选)acrInstanceId: string # ACR 实例 ID(可选)

镜像源类型

  • ACR - 阿里云容器镜像服务
  • ACREE - 阿里云容器镜像服务企业版
  • CUSTOM - 自定义镜像仓库

示例

基本容器配置

customContainerConfig:
image: registry.cn-hangzhou.aliyuncs.com/my-namespace/my-agent:latestcommand:
- python3
- app.py

ACR 企业版

customContainerConfig:
image: my-acr-instance.cn-hangzhou.cr.aliyuncs.com/namespace/image:tagimageRegistryType: ACREEacrInstanceId: cri-xxxxxxxxxxxxxcommand:
- python3
- app.py

VPC 配置

基本结构

vpcConfig:
vpcId: string # VPC ID(必填)vSwitchIds: string | array # 交换机 ID(必填,支持单个或多个)securityGroupId: string # 安全组 ID(必填)

网络模式

配置方式网络模式说明
vpcConfig
internetAccess: true 或不设置
公网仅公网访问
vpcConfig
internetAccess: false
私网仅 VPC 内网访问
vpcConfig
internetAccess: true 或不设置
混合VPC + 公网都可访问

示例

单个交换机

vpcConfig:
vpcId: vpc-bp1234567890abcdefvSwitchIds: vsw-bp1111111111111111securityGroupId: sg-bp1234567890abcdef

多个交换机(高可用)

vpcConfig:
vpcId: vpc-bp1234567890abcdefvSwitchIds:
- vsw-bp1111111111111111
- vsw-bp2222222222222222securityGroupId: sg-bp1234567890abcdef

仅内网访问

vpcConfig:
vpcId: vpc-bp1234567890abcdefvSwitchIds: [vsw-bp1111111111111111]securityGroupId: sg-bp1234567890abcdefinternetAccess: false

日志配置

方式 1:自动配置(推荐)

logConfig: auto

组件会自动创建 SLS 项目和日志库,首次部署后会提示替换为实际配置。

方式 2:手动配置

logConfig:
project: my-sls-project # SLS 项目名称logstore: agentrun-logs # SLS 日志库名称

协议配置

protocolConfiguration:
type: HTTP # 或 HTTPS

健康检查配置

healthCheckConfiguration:
httpGetUrl: /health # HTTP GET URL,默认 /healthinitialDelaySeconds: 30# 初始延迟(秒),默认 30periodSeconds: 10# 检查间隔(秒),默认 30timeoutSeconds: 3# 超时时间(秒),默认 3failureThreshold: 3# 失败阈值,默认 3successThreshold: 1# 成功阈值,默认 1

端点配置

在 YAML 中配置

endpoints:
- name: production # 端点名称(必填)version: 1# 目标版本,默认 LATESTdescription: "Production"# 描述(可选)
- name: stagingversion: LATESTdescription: "Staging environment"

注意:灰度发布的流量权重不在 YAML 中配置,需要通过命令行操作。


配置示例

1. 代码模式 - 最小配置

edition: 3.0.0name: minimal-agentaccess: defaultresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: my-agentcode:
src: ./codelanguage: python3.12command:
- python3
- main.py

2. 代码模式 - 完整配置

edition: 3.0.0name: complete-agentaccess: defaultvars:
region: cn-hangzhouenv: productionresources:
my-agent:
component: agentrunprops:
region: ${vars.region}agent:
# 基本信息name: ${vars.env}-agentdescription: "Production agent runtime"# 代码配置code:
src: ./codelanguage: python3.12command:
- python3
- main.py# 资源配置cpu: 2.0memory: 4096port: 8000instanceConcurrency: 50sessionIdleTimeoutSeconds: 7200# 网络配置vpcConfig:
vpcId: vpc-bp1234567890abcdefvSwitchIds:
- vsw-bp1111111111111111
- vsw-bp2222222222222222securityGroupId: sg-bp1234567890abcdefinternetAccess: true# 环境变量environmentVariables:
ENVIRONMENT: ${vars.env}LOG_LEVEL: infoAPI_KEY: ${env(API_KEY)}# 执行角色role: acs:ram::123456789:role/AliyunAgentRunRole# 凭证名称credentialName: my-credential# 日志配置logConfig:
project: ${vars.env}-logslogstore: agentrun-runtime# 协议配置protocolConfiguration:
type: HTTP# 健康检查healthCheckConfiguration:
httpGetUrl: /healthperiodSeconds: 30timeoutSeconds: 3# 端点配置endpoints:
- name: productionversion: 1description: "Production endpoint"
- name: stagingversion: LATESTdescription: "Staging endpoint"

3. 容器模式

edition: 3.0.0name: container-agentaccess: defaultresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: my-container-agentdescription: "Container-based agent"# 容器配置customContainerConfig:
image: registry.cn-hangzhou.aliyuncs.com/my-ns/my-agent:v1.0command:
- python3
- app.pyimageRegistryType: ACR# 资源配置cpu: 2.0memory: 4096port: 9000# 环境变量environmentVariables:
MODEL_NAME: qwen-maxAPI_KEY: ${env(API_KEY)}# 日志配置logConfig: auto

4. VPC 内网访问

edition: 3.0.0name: vpc-agentaccess: defaultresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: my-vpc-agentcode:
src: ./codelanguage: python3.12command:
- python3
- main.py# VPC 配置vpcConfig:
vpcId: vpc-bp1234567890abcdefvSwitchIds: vsw-bp1111111111111111securityGroupId: sg-bp1234567890abcdef# 禁用公网访问internetAccess: falselogConfig: auto

命令说明

deploy - 部署

部署智能体运行时到云端:

# 基本部署
s deploy
# 显示详细日志
s deploy --debug
# 自动确认(跳过提示)
s deploy -y

info - 查询信息

查看已部署的智能体运行时详细信息:

s info

输出示例:

agent: id: 1062cdd0-042e-407b-8a3f-234370c2c68c
arn: acs:agentrun:cn-hangzhou:1583208943291465:runtimes/1062cdd0-042e-407b-8a3f-234370c2c68c
name: my-runtime
description: My Agent Runtime
artifactType: Code
status: READY
resources: cpu: 1
memory: 2048
port: 8000
timestamps: createdAt: 2025-01-10T07:07:33Z
lastUpdatedAt: 2025-01-10T08:32:03Z
region: cn-hangzhou
endpoints: - id: b8db2461-bae3-4a80-aa66-1e2ccbbb7fb6
name: production
url: https://xxx.agentrun-data.cn-hangzhou.aliyuncs.com/.../invocations
version: LATEST
status: READY
description: Production endpoint

build - 构建代码包

使用 FC3 组件构建代码包(支持依赖安装):

# 基本构建
s build
# 使用自定义 Dockerfile
s build --dockerfile Dockerfile
# 显示详细日志
s build --debug

remove - 删除

删除智能体运行时实例及所有关联资源:

# 交互式删除(会提示确认)
s remove
# 自动确认删除
s remove -y
# 显示详细日志
s remove --debug

logs - 日志查询

查询智能体运行时的日志:

# 查询最近 20 分钟日志
s logs
# 实时日志(类似 tail -f)
s logs --tail
# 指定时间范围
s logs --start-time "2024-01-01 00:00:00" --end-time "2024-01-01 23:59:59"# 根据请求 ID 过滤
s logs --request-id 1-63f9c123-xxxx
# 根据实例 ID 过滤
s logs --instance-id c-63f9c123-xxxx
# 关键字搜索
s logs --search "error"# 只看失败日志
s logs --type fail
# 高亮匹配文本
s logs --search "error" --match "error"

instance - 实例管理

管理智能体运行时的实例:

# 列出所有实例
s instance list
# 在实例中执行命令
s instance exec --instance-id c-xxxx --cmd "ls -lh"# 进入交互式 shell
s instance exec --instance-id c-xxxx
# 使用自定义 shell
s instance exec --instance-id c-xxxx --shell /bin/sh
# 指定工作目录
s instance exec --instance-id c-xxxx --workdir /app --cmd "pwd"

concurrency - 并发配置

管理预留并发配置:

# 查询当前并发配置
s concurrency get
# 设置预留并发
s concurrency put --reserved-concurrency 10
# 删除并发配置
s concurrency remove
# 删除时跳过确认
s concurrency remove -y

version - 版本管理

管理智能体运行时的版本:

# 列出所有版本
s version list
# 发布新版本
s version publish --description "Version 1.0"# 发布版本(自动生成描述)
s version publish

输出示例:

✅ Found 2 version(s)
version description lastUpdatedAt ------- ---------------- ---------------------------
1 Version 1.0 2024-01-01T10:00:00Z LATEST Auto published 2024-01-02T15:30:00Z 

endpoint - 端点管理

管理智能体运行时的访问端点:

# 列出所有端点
s endpoint list
# 获取端点详情
s endpoint get --endpoint-name production
# 创建/更新端点(指向 LATEST)
s endpoint publish --endpoint-name production --description "Production endpoint"# 创建/更新端点(指向特定版本)
s endpoint publish --endpoint-name prod-v1 --target-version 1 --description "Production v1"# 灰度发布(80% v2, 20% v1)
s endpoint publish --endpoint-name canary \
--target-version 2 \
--canary-version 1 \
--weight 0.2 \
--description "Canary deployment"# 删除端点
s endpoint remove --endpoint-name staging
# 删除时跳过确认
s endpoint remove --endpoint-name staging -y

输出示例:

✅ Found 3 endpoint(s)
name url traffic status ---------- ------------------------------------------- ------------------------- ------
production https://xxx.agentrun-data.cn-hangzhou... 100% → v2 READY canary https://xxx.agentrun-data.cn-hangzhou... 80% → v2, 20% → v1 READY staging https://xxx.agentrun-data.cn-hangzhou... 100% → vLATEST READY 

完整工作流示例

场景:从开发到生产的完整流程

# 1. 开发环境部署
s deploy
# 2. 查看部署信息
s info
# 3. 查看实时日志
s logs --tail
# 4. 测试通过后,发布版本 1
s version publish --description "First production version"# 5. 创建生产端点(指向版本 1)
s endpoint publish --endpoint-name production --target-version 1 --description "Production endpoint"# 6. 开发新功能,再次部署(更新 LATEST)# 修改代码后
s deploy
# 7. 创建灰度端点测试新版本(10% 流量到 LATEST)
s endpoint publish --endpoint-name canary \
--target-version 1 \
--canary-version LATEST \
--weight 0.1 \
--description "Canary test"# 8. 观察日志,验证新版本
s logs --tail
# 9. 验证通过后,逐步提高灰度流量
s endpoint publish --endpoint-name canary \
--target-version 1 \
--canary-version LATEST \
--weight 0.5
# 10. 最终发布版本 2
s version publish --description "Second production version"# 11. 全量切换到版本 2
s endpoint publish --endpoint-name production --target-version 2
# 12. 删除灰度端点
s endpoint remove --endpoint-name canary -y
# 13. 查看最终状态
s info
s endpoint list

环境变量

支持多种方式设置环境变量:

1. 直接配置

environmentVariables:
ENV: productionDEBUG: "false"PORT: "8000"

2. 引用外部环境变量

environmentVariables:
API_KEY: ${env(API_KEY)}DB_PASSWORD: ${env(DB_PASSWORD)}DB_HOST: ${env(DB_HOST)}

使用前先设置:

export API_KEY=your_api_key
export DB_PASSWORD=your_password
export DB_HOST=127.0.0.1
s deploy

3. 使用 .env 文件

创建 .env 文件:

API_KEY=your_api_keyDB_PASSWORD=your_passwordDB_HOST=127.0.0.1

.gitignore 中忽略:

.env

s.yaml 中引用:

environmentVariables:
API_KEY: ${env(API_KEY)}DB_PASSWORD: ${env(DB_PASSWORD)}DB_HOST: ${env(DB_HOST)}

权限配置

所需的 RAM 权限

部署和管理 AgentRun 需要以下权限:

{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": [
"agentrun:CreateAgentRuntime",
"agentrun:UpdateAgentRuntime",
"agentrun:DeleteAgentRuntime",
"agentrun:GetAgentRuntime",
"agentrun:ListAgentRuntimes",
"agentrun:PublishRuntimeVersion",
"agentrun:ListAgentRuntimeVersions",
"agentrun:CreateAgentRuntimeEndpoint",
"agentrun:UpdateAgentRuntimeEndpoint",
"agentrun:DeleteAgentRuntimeEndpoint",
"agentrun:GetAgentRuntimeEndpoint",
"agentrun:ListAgentRuntimeEndpoints"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"fc:GetFunction",
"fc:GetTempBucketToken"
],
"Resource": "*"
}
]
}

执行角色权限

如果配置了日志服务,执行角色需要以下权限:

{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": [
"log:PostLogStoreLogs"
],
"Resource": "*"
}
]
}

如果访问 VPC 内的资源,执行角色需要相应的网络权限。


最佳实践

1. 项目结构

推荐的项目结构:

my-agent-project/
├── .env # 环境变量(不要提交到 Git)
├── .gitignore # Git 忽略文件
├── s.yaml # Serverless Devs 配置
├── s.dev.yaml # 开发环境配置
├── s.prod.yaml # 生产环境配置
├── code/ # 代码目录
│ ├── main.py
│ ├── requirements.txt
│ ├── utils/
│ └── ...
├── tests/ # 测试代码
└── README.md

.gitignore 内容:

.env
.s/
node_modules/
*.pyc
__pycache__/
.DS_Store

2. 多环境管理

使用不同的配置文件管理多环境:

s.dev.yaml - 开发环境:

edition: 3.0.0name: dev-agentaccess: defaultresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: dev-agentcode:
src: ./codelanguage: python3.12command: [python3, main.py]cpu: 1.0memory: 2048logConfig: auto

s.prod.yaml - 生产环境:

edition: 3.0.0name: prod-agentaccess: prod-accountresources:
my-agent:
component: agentrunprops:
region: cn-hangzhouagent:
name: prod-agentcode:
src: ./codelanguage: python3.12command: [python3, main.py]cpu: 2.0memory: 4096vpcConfig:
vpcId: vpc-prodvSwitchIds: [vsw-prod-1, vsw-prod-2]securityGroupId: sg-prodinternetAccess: falselogConfig:
project: prod-logslogstore: agentrun

部署时指定配置文件:

# 开发环境
s deploy -t s.dev.yaml
# 生产环境
s deploy -t s.prod.yaml

3. 灰度发布流程

完整的灰度发布流程:

# 1. 部署新版本(更新 LATEST)
s deploy
# 2. 发布新版本
s version publish --description "New feature: xxx"# 3. 创建灰度端点(10% 流量)
s endpoint publish --endpoint-name canary \
--target-version 1 \
--canary-version 2 \
--weight 0.1 \
--description "Canary: 10% traffic"# 4. 观察指标和日志
s logs --tail
# 5. 逐步提高流量(50%)
s endpoint publish --endpoint-name canary \
--target-version 1 \
--canary-version 2 \
--weight 0.5
# 6. 全量切换
s endpoint publish --endpoint-name production --target-version 2
# 7. 清理灰度端点
s endpoint remove --endpoint-name canary -y

4. 健康检查最佳实践

在代码中实现健康检查端点:

Python 示例:

fromhttp.serverimportHTTPServer, BaseHTTPRequestHandlerimportjsonclassHandler(BaseHTTPRequestHandler):
defdo_GET(self):
ifself.path=='/health':
# 健康检查逻辑self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
response= {"status": "healthy", "version": "1.0"}
self.wfile.write(json.dumps(response).encode())
else:
# 业务逻辑pass

在 YAML 中配置:

healthCheckConfiguration:
httpGetUrl: /healthinitialDelaySeconds: 10periodSeconds: 30timeoutSeconds: 3failureThreshold: 3successThreshold: 1

5. 安全建议

  • 使用环境变量:不要在配置文件中硬编码敏感信息
  • 使用 .gitignore:忽略 .env 文件
  • 生产环境使用 VPC:启用内网隔离
  • 最小权限原则:为不同环境使用不同的 RAM 角色
  • 启用健康检查:及时发现服务异常
  • 配置日志服务:便于问题排查
  • 使用版本管理:支持快速回滚

常见问题

Q1: 部署时报错 "agent configuration is required"?

A: 确保在 props 下配置了 agent 字段:

props:
region: cn-hangzhouagent: # ← 必须有这一层name: my-agentcode: ...

Q2: 如何更新已部署的运行时?

A: 直接修改 s.yaml 后再次执行 s deploy,组件会自动检测并更新现有实例。

Q3: 端点的 URL 在哪里查看?

A: 部署成功后执行以下命令查看:

s info
#
s endpoint list

Q4: 代码更新后是否需要删除重建?

A: 不需要,直接 s deploy 即可更新代码。

Q5: 如何查看实时日志?

A: 使用 tail 模式:

s logs --tail

Q6: 如何在实例中执行命令?

A: 首先获取实例 ID,然后执行命令:

# 获取实例 ID
s instance list
# 执行命令
s instance exec --instance-id c-xxxx --cmd "ls -lh"

Q7: 灰度发布如何配置?

A: 使用 endpoint publish 命令:

s endpoint publish --endpoint-name canary \
--target-version 2 \
--canary-version 1 \
--weight 0.2 \
--description "20% to v1, 80% to v2"

Q8: 如何配置多个交换机?

A: 使用数组格式:

vpcConfig:
vSwitchIds:
- vsw-id-1
- vsw-id-2

Q9: logConfig: auto 是什么意思?

A: 自动创建 SLS 日志配置,首次部署后会提示实际的 project 和 logstore 名称。

Q10: 如何快速回滚到之前的版本?

A: 使用 endpoint 切换版本:

# 查看可用版本
s version list
# 切换到旧版本
s endpoint publish --endpoint-name production --target-version 1

开发调试

如果您想参与组件开发或调试:

1. 克隆仓库

git clone https://github.com/devsapp/agentrun.git
cd agentrun

2. 安装依赖

npm install

3. 启动开发模式

npm run watch

4. 测试

test/ 目录创建测试配置:

edition: 3.0.0name: test-appaccess: defaultresources:
test-agent:
component: ${path('../dist')} # 指向本地构建目录props:
region: cn-hangzhouagent:
name: test-agentcode:
src: ./codelanguage: python3.12command: [python3, main.py]

执行测试:

cdtest
s deploy --debug

5. 代码格式化

npm run format

6. 构建发布

npm run build

更新日志

v1.0.0 (2025-01-01)

  • ✨ 首次发布
  • ✨ 支持代码模式和容器模式
  • ✨ 支持版本管理和端点管理
  • ✨ 支持日志查询和实例管理
  • ✨ 支持并发配置
  • ✨ 支持 VPC 网络配置
  • ✨ 支持健康检查配置
  • ✨ 完善文档和示例

相关链接


许可证

MIT License


感谢使用 AgentRun Component!

如有问题,欢迎随时反馈。

About

Agentrun Component

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages