阿里云 AgentRun 智能体运行时组件,用于部署和管理智能体运行时实例。
AgentRun Component 是 Serverless Devs 的一个组件,用于部署和管理阿里云 AgentRun 智能体运行时实例。智能体运行时提供代码执行、容器运行、网络访问、日志管理等核心能力。
- ✅ 部署模式:支持代码模式和容器模式
- ✅ 代码管理:支持本地代码、ZIP 包、OSS 代码包
- ✅ 版本管理:支持版本发布和管理
- ✅ 端点管理:支持创建端点和灰度发布
- ✅ 网络配置:支持 VPC、公网、混合网络
- ✅ 日志查询:支持实时日志和历史日志查询
- ✅ 实例管理:支持实例列表和命令执行
- ✅ 并发控制:支持预留并发配置
- ✅ 健康检查:支持自定义健康检查配置
- Serverless Devs 工具已安装(版本 >= 3.0.0)
- 已配置阿里云访问凭证(Access Key)
- 拥有 AgentRun 服务的使用权限
npm install -g @serverless-devs/ss config add按提示输入:
- AccountID(阿里云账号 ID)
- AccessKeyID
- AccessKeySecret
- 别名(如:default)
# 创建项目目录
mkdir my-agent-project && cd my-agent-project
# 创建代码目录
mkdir code
# 创建示例代码
cat > code/index.py << 'EOF'
#!/usr/bin/env python3
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
class 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创建 s.yaml:
edition: 3.0.0
name: my-agentrun-app
access: default
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: my-first-agent
description: "My first agent runtime"
code:
src: ./code
language: python3.12
command:
- python3
- index.py
cpu: 1.0
memory: 2048
port: 8000
instanceConcurrency: 10
logConfig: auto
endpoints:
- name: production
description: "Production endpoint"s deploy部署成功后,查看信息:
s infoedition: 3.0.0 # Serverless Devs 版本(必须 >= 3.0.0)
name: agentrun-app # 应用名称
access: default # 访问凭证别名
vars: # 全局变量(可选)
region: cn-hangzhou
env: production
resources:
my-agent: # 资源名称
component: agentrun # 组件名称
props: # 组件属性
region: string # 地域
agent: object # 智能体配置| 字段 | 类型 | 说明 | 示例 |
|---|---|---|---|
name |
String | 智能体运行时名称,字母开头,支持字母、数字、下划线、中划线,1-128 字符 | my-runtime |
code 或 customContainerConfig |
Object | 代码配置或容器配置(二选一) | 见下文 |
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
description |
String | - | 描述信息,最多 256 字符 |
cpu |
Number | 1.0 | CPU 核数,范围:0.05-16 |
memory |
Number | 2048 | 内存大小(MB),范围:128-32768 |
port |
Number | 8000 | 监听端口,范围:1-65535 |
instanceConcurrency |
Number | 10 | 实例并发数,范围:1-200 |
sessionIdleTimeoutSeconds |
Number | 3600 | 会话空闲超时(秒),范围:60-86400 |
internetAccess |
Boolean | true | 是否允许公网访问 |
vpcConfig |
Object | - | VPC 配置 |
environmentVariables |
Object | - | 环境变量 |
role |
String | - | RAM 角色 ARN |
credentialName |
String | - | 访问凭证名称 |
logConfig |
Object/String | - | 日志配置,可设置为 auto |
protocolConfiguration |
Object | - | 协议配置 |
healthCheckConfiguration |
Object | - | 健康检查配置 |
endpoints |
Array | - | 端点配置 |
code:
src: string # 本地代码路径(目录或 ZIP)
# 或
ossBucketName: string # OSS 存储桶名称
ossObjectName: string # OSS 对象名称
language: string # 编程语言(必填)
command: array # 运行命令(可选)
checksum: string # CRC-64 校验值(可选)python3.10python3.12nodejs18nodejs20java8java11custom
code:
src: ./code
language: python3.12
command:
- python3
- main.pycode:
src: ./code.zip
language: python3.12
command:
- python3
- main.pycode:
ossBucketName: my-code-bucket
ossObjectName: agent-code.zip
language: python3.12
command:
- python3
- main.py
checksum: "1234567890123456789" # 可选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:latest
command:
- python3
- app.pycustomContainerConfig:
image: my-acr-instance.cn-hangzhou.cr.aliyuncs.com/namespace/image:tag
imageRegistryType: ACREE
acrInstanceId: cri-xxxxxxxxxxxxx
command:
- python3
- app.pyvpcConfig:
vpcId: string # VPC ID(必填)
vSwitchIds: string | array # 交换机 ID(必填,支持单个或多个)
securityGroupId: string # 安全组 ID(必填)| 配置方式 | 网络模式 | 说明 |
|---|---|---|
无 vpcConfiginternetAccess: true 或不设置 |
公网 | 仅公网访问 |
有 vpcConfiginternetAccess: false |
私网 | 仅 VPC 内网访问 |
有 vpcConfiginternetAccess: true 或不设置 |
混合 | VPC + 公网都可访问 |
vpcConfig:
vpcId: vpc-bp1234567890abcdef
vSwitchIds: vsw-bp1111111111111111
securityGroupId: sg-bp1234567890abcdefvpcConfig:
vpcId: vpc-bp1234567890abcdef
vSwitchIds:
- vsw-bp1111111111111111
- vsw-bp2222222222222222
securityGroupId: sg-bp1234567890abcdefvpcConfig:
vpcId: vpc-bp1234567890abcdef
vSwitchIds: [vsw-bp1111111111111111]
securityGroupId: sg-bp1234567890abcdef
internetAccess: falselogConfig: auto组件会自动创建 SLS 项目和日志库,首次部署后会提示替换为实际配置。
logConfig:
project: my-sls-project # SLS 项目名称
logstore: agentrun-logs # SLS 日志库名称protocolConfiguration:
type: HTTP # 或 HTTPShealthCheckConfiguration:
httpGetUrl: /health # HTTP GET URL,默认 /health
initialDelaySeconds: 30 # 初始延迟(秒),默认 30
periodSeconds: 10 # 检查间隔(秒),默认 30
timeoutSeconds: 3 # 超时时间(秒),默认 3
failureThreshold: 3 # 失败阈值,默认 3
successThreshold: 1 # 成功阈值,默认 1endpoints:
- name: production # 端点名称(必填)
version: 1 # 目标版本,默认 LATEST
description: "Production" # 描述(可选)
- name: staging
version: LATEST
description: "Staging environment"注意:灰度发布的流量权重不在 YAML 中配置,需要通过命令行操作。
edition: 3.0.0
name: minimal-agent
access: default
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: my-agent
code:
src: ./code
language: python3.12
command:
- python3
- main.pyedition: 3.0.0
name: complete-agent
access: default
vars:
region: cn-hangzhou
env: production
resources:
my-agent:
component: agentrun
props:
region: ${vars.region}
agent:
# 基本信息
name: ${vars.env}-agent
description: "Production agent runtime"
# 代码配置
code:
src: ./code
language: python3.12
command:
- python3
- main.py
# 资源配置
cpu: 2.0
memory: 4096
port: 8000
instanceConcurrency: 50
sessionIdleTimeoutSeconds: 7200
# 网络配置
vpcConfig:
vpcId: vpc-bp1234567890abcdef
vSwitchIds:
- vsw-bp1111111111111111
- vsw-bp2222222222222222
securityGroupId: sg-bp1234567890abcdef
internetAccess: true
# 环境变量
environmentVariables:
ENVIRONMENT: ${vars.env}
LOG_LEVEL: info
API_KEY: ${env(API_KEY)}
# 执行角色
role: acs:ram::123456789:role/AliyunAgentRunRole
# 凭证名称
credentialName: my-credential
# 日志配置
logConfig:
project: ${vars.env}-logs
logstore: agentrun-runtime
# 协议配置
protocolConfiguration:
type: HTTP
# 健康检查
healthCheckConfiguration:
httpGetUrl: /health
periodSeconds: 30
timeoutSeconds: 3
# 端点配置
endpoints:
- name: production
version: 1
description: "Production endpoint"
- name: staging
version: LATEST
description: "Staging endpoint"edition: 3.0.0
name: container-agent
access: default
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: my-container-agent
description: "Container-based agent"
# 容器配置
customContainerConfig:
image: registry.cn-hangzhou.aliyuncs.com/my-ns/my-agent:v1.0
command:
- python3
- app.py
imageRegistryType: ACR
# 资源配置
cpu: 2.0
memory: 4096
port: 9000
# 环境变量
environmentVariables:
MODEL_NAME: qwen-max
API_KEY: ${env(API_KEY)}
# 日志配置
logConfig: autoedition: 3.0.0
name: vpc-agent
access: default
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: my-vpc-agent
code:
src: ./code
language: python3.12
command:
- python3
- main.py
# VPC 配置
vpcConfig:
vpcId: vpc-bp1234567890abcdef
vSwitchIds: vsw-bp1111111111111111
securityGroupId: sg-bp1234567890abcdef
# 禁用公网访问
internetAccess: false
logConfig: auto部署智能体运行时到云端:
# 基本部署
s deploy
# 显示详细日志
s deploy --debug
# 自动确认(跳过提示)
s deploy -y查看已部署的智能体运行时详细信息:
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
使用 FC3 组件构建代码包(支持依赖安装):
# 基本构建
s build
# 使用自定义 Dockerfile
s build --dockerfile Dockerfile
# 显示详细日志
s build --debug删除智能体运行时实例及所有关联资源:
# 交互式删除(会提示确认)
s remove
# 自动确认删除
s remove -y
# 显示详细日志
s remove --debug查询智能体运行时的日志:
# 查询最近 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"管理智能体运行时的实例:
# 列出所有实例
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"管理预留并发配置:
# 查询当前并发配置
s concurrency get
# 设置预留并发
s concurrency put --reserved-concurrency 10
# 删除并发配置
s concurrency remove
# 删除时跳过确认
s concurrency remove -y管理智能体运行时的版本:
# 列出所有版本
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
管理智能体运行时的访问端点:
# 列出所有端点
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支持多种方式设置环境变量:
environmentVariables:
ENV: production
DEBUG: "false"
PORT: "8000"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创建 .env 文件:
API_KEY=your_api_key
DB_PASSWORD=your_password
DB_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)}部署和管理 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 内的资源,执行角色需要相应的网络权限。
推荐的项目结构:
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
使用不同的配置文件管理多环境:
s.dev.yaml - 开发环境:
edition: 3.0.0
name: dev-agent
access: default
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: dev-agent
code:
src: ./code
language: python3.12
command: [python3, main.py]
cpu: 1.0
memory: 2048
logConfig: autos.prod.yaml - 生产环境:
edition: 3.0.0
name: prod-agent
access: prod-account
resources:
my-agent:
component: agentrun
props:
region: cn-hangzhou
agent:
name: prod-agent
code:
src: ./code
language: python3.12
command: [python3, main.py]
cpu: 2.0
memory: 4096
vpcConfig:
vpcId: vpc-prod
vSwitchIds: [vsw-prod-1, vsw-prod-2]
securityGroupId: sg-prod
internetAccess: false
logConfig:
project: prod-logs
logstore: agentrun部署时指定配置文件:
# 开发环境
s deploy -t s.dev.yaml
# 生产环境
s deploy -t s.prod.yaml完整的灰度发布流程:
# 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在代码中实现健康检查端点:
Python 示例:
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.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: /health
initialDelaySeconds: 10
periodSeconds: 30
timeoutSeconds: 3
failureThreshold: 3
successThreshold: 1- ✅ 使用环境变量:不要在配置文件中硬编码敏感信息
- ✅ 使用 .gitignore:忽略
.env文件 - ✅ 生产环境使用 VPC:启用内网隔离
- ✅ 最小权限原则:为不同环境使用不同的 RAM 角色
- ✅ 启用健康检查:及时发现服务异常
- ✅ 配置日志服务:便于问题排查
- ✅ 使用版本管理:支持快速回滚
A: 确保在 props 下配置了 agent 字段:
props:
region: cn-hangzhou
agent: # ← 必须有这一层
name: my-agent
code: ...A: 直接修改 s.yaml 后再次执行 s deploy,组件会自动检测并更新现有实例。
A: 部署成功后执行以下命令查看:
s info
# 或
s endpoint listA: 不需要,直接 s deploy 即可更新代码。
A: 使用 tail 模式:
s logs --tailA: 首先获取实例 ID,然后执行命令:
# 获取实例 ID
s instance list
# 执行命令
s instance exec --instance-id c-xxxx --cmd "ls -lh"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"A: 使用数组格式:
vpcConfig:
vSwitchIds:
- vsw-id-1
- vsw-id-2A: 自动创建 SLS 日志配置,首次部署后会提示实际的 project 和 logstore 名称。
A: 使用 endpoint 切换版本:
# 查看可用版本
s version list
# 切换到旧版本
s endpoint publish --endpoint-name production --target-version 1如果您想参与组件开发或调试:
git clone https://github.com/devsapp/agentrun.git
cd agentrunnpm installnpm run watch在 test/ 目录创建测试配置:
edition: 3.0.0
name: test-app
access: default
resources:
test-agent:
component: ${path('../dist')} # 指向本地构建目录
props:
region: cn-hangzhou
agent:
name: test-agent
code:
src: ./code
language: python3.12
command: [python3, main.py]执行测试:
cd test
s deploy --debugnpm run formatnpm run build- ✨ 首次发布
- ✨ 支持代码模式和容器模式
- ✨ 支持版本管理和端点管理
- ✨ 支持日志查询和实例管理
- ✨ 支持并发配置
- ✨ 支持 VPC 网络配置
- ✨ 支持健康检查配置
- ✨ 完善文档和示例
MIT License
感谢使用 AgentRun Component!
如有问题,欢迎随时反馈。