Skip to main content

Agent Management

version tip

This module has been added since v5.0.0.

Agent supports two underlying deployment types:

  • SCF (SCF): lightweight, pay-as-you-go, suitable for low concurrency scenarios.
  • TCBR (Cloud Run): containerized, suitable for high concurrency or scenarios requiring custom runtime environments.

Create an Agent

1. API Description

API feature: Create an Agent based on SCF (Cloud Function) in the current environment.

API declaration: app.agent.createAgent(params): Promise<ICreateAgentResponse>

2. Input Parameters

FieldRequiredTypeDescription
NameRequiredStringAgent name
RuntimeRequiredRuntimeVersionRuntime version, see the enumeration table below.
AgentIdNoStringAgent ID, auto-generated if not provided
envVariablesNoRecord<string,string>Environment variable key-value pairs
TimeoutNoNumberTimeout period (seconds), default 7200
MemorySizeNoNumberMemory size (MB), default 128, optional 64 or 128~3072 (in increments of 128)
InstallDependencyNoBooleanWhether to automatically install dependencies, default false
SessionConfigNoISessionConfigSession configuration, see description below
cwdNoStringLocal code directory, mutually exclusive with ZipFile
ignoreNoString | String[]File glob to ignore during packaging (only valid for cwd method)
deployModeNo'zip' | 'cos'Deployment method, default 'zip'; for large files, 'cos' is recommended
ZipFileNoStringBase64-encoded ZIP file content, mutually exclusive with cwd
CosBucketRegionNoStringCOS Bucket region (used only for direct upload)
TempCosObjectNameNoStringCOS temporary object name (used only for direct upload)

RuntimeVersion Enum

ValueDescription
Nodejs20.19Node.js 20.19
Nodejs18.15Node.js 18.15
Nodejs16.13Node.js 16.13
Nodejs14.18Node.js 14.18
Python3.10Python 3.10
Python3.9Python 3.9
Python3.7Python 3.7
Php8.0PHP 8.0
Php7.4PHP 7.4
Go1Go 1.x
Java11Java 11
Java8Java 8

ISessionConfig

FieldRequiredTypeDescription
SessionSourceNoStringSession source: 'HEADER' (default), 'COOKIE', 'QUERY_STRING'
SessionNameNoStringSession name, 5-40 characters, starting with a letter; default 'X-Session-Id'
MaximumConcurrencySessionPerInstanceNoNumberMaximum concurrent sessions per instance, 1-100, default 1
MaximumTTLInSecondsNoNumberMaximum session lifetime (seconds), default 21600
MaximumIdleTimeInSecondsNoNumberMaximum session idle time (seconds), default 1800
MaxConcurrencyNoNumberMaximum concurrency per instance, 1-100, default 50

3. Return Results

FieldTypeDescription
AgentIdStringCreated Agent ID
RequestIdStringUnique identifier of the request

4. Sample Code

import CloudBase from '@cloudbase/manager-node'

const app = CloudBase.init({
secretId: 'Your SecretId',
secretKey: 'Your SecretKey',
envId: 'Your envId'
})

async function main() {
// Method 1: Create via local code directory
const res = await app.agent.createAgent({
Name: 'my-agent',
Runtime: 'Nodejs18.15',
cwd: '/path/to/agent/code',
InstallDependency: true,
envVariables: {
OPENAI_API_KEY: 'sk-xxx'
}
})
console.log('Agent ID:', res.AgentId)
}

main()

Query Agent List

1. API Description

API feature: queries the Agent list in the current environment

API declaration: app.agent.describeAgentList(params?): Promise<IDescribeAgentListResponse>

2. Input Parameters

FieldRequiredTypeDescription
PageSizeNoNumberNumber of items per page, default: 20
PageNumberNoNumberPage number, default: 1
AgentIdNoStringSpecify Agent ID (used for querying a single one)

3. Return Results

FieldTypeDescription
AgentListArray<IAgentInfo>Agent list
TotalNumberTotal Agents
RequestIdStringUnique identifier of the request

IAgentInfo

FieldTypeDescription
AgentIdStringAgent ID
NameStringAgent name
AgentTypeStringAgent type: scf or tcbr
IntroductionStringIntroduction
AvatarStringAvatar URL
TagsString[]Tag list
CreateTimeStringCreation time
UpdateTimeStringUpdate time
StatusStringStatus
ServiceIdStringUnderlying service ID

4. Sample Code

async function main() {
const { AgentList, Total } = await app.agent.describeAgentList({ PageSize: 10 })
console.log(`Total ${Total} Agents`)
AgentList.forEach(a => console.log(a.AgentId, a.Name, a.AgentType))
}

Query Agent Details

1. API Description

API feature: query the details of a single Agent, and check whether the underlying resources (SCF / Cloud Run) are ready and available.

API declaration: app.agent.describeAgent(agentId): Promise<IDescribeAgentResponse>

2. Input Parameters

FieldRequiredTypeDescription
agentIdYesStringAgent ID

3. Return Results

FieldTypeDescription
AgentInfoIAgentInfoBasic information of the Agent, null when not present
IsReadyBooleanwhether the Agent is ready and available
NotReadyReasonStringReason description when unavailable
RequestIdStringUnique identifier of the request

4. Sample Code

async function main() {
const detail = await app.agent.describeAgent('agent-xxx')
if (detail.IsReady) {
console.log('Agent is ready')
} else {
console.log('Unavailable reason:', detail.NotReadyReason)
}
}

Update an Agent

1. API Description

API feature: Updates the Agent's code or configuration, automatically invoking the corresponding logic based on the Agent type (SCF / TCBR).

API declaration: app.agent.updateAgent(params): Promise<IUpdateScfAgentResponse>

2. Input Parameters

FieldRequiredTypeDescription
AgentIdYesStringAgent ID
cwdNoStringLocal code directory (mutually exclusive with ZipFile)
ZipFileNoStringBase64-encoded ZIP file content (mutually exclusive with cwd)
envVariablesNoRecord<string,string>Environment variable key-value pairs
RuntimeNoRuntimeVersionRuntime version
TimeoutNoNumberTimeout period (seconds)
MemorySizeNoNumberMemory size (MB)
InstallDependencyNoBooleanwhether to automatically install dependencies
IgnoreNoString | String[]Files to ignore during packaging (file glob)

3. Return Results

FieldTypeDescription
RequestIdStringUnique identifier of the request
elapsedTimeNumberTotal elapsed time for update (ms)
messageStringResult message
detailsString[]Details of each step

4. Sample Code

async function main() {
const res = await app.agent.updateAgent({
AgentId: 'agent-xxx',
cwd: '/path/to/new/code',
envVariables: { API_KEY: 'new-key' }
})
console.log(`Update completed, elapsed time ${res.elapsedTime}ms`)
}

Delete the Agent

1. API Description

API feature: deletes the specified Agent and attempts to delete the underlying SCF or Cloud Run resources.

API declaration: app.agent.deleteAgent(params): Promise<IDeleteAgentResponse>

2. Input Parameters

FieldRequiredTypeDescription
AgentIdYesStringAgent ID

3. Return Results

FieldTypeDescription
RequestIdStringUnique identifier of the request
AgentDeletedBooleanwhether the Agent record was successfully deleted
ResourceResultObject | undefinedResult of underlying resource deletion; see below for details

ResourceResult

FieldTypeDescription
TypeStringResource type: scf or tcbr
NameStringResource name (i.e. AgentId)
DeletedBooleanwhether the underlying resources were successfully deleted
ErrorStringError message when deletion fails
CleanupHintStringManual cleanup instructions when deletion fails
Caution

If the underlying resources fail to be deleted (ResourceResult.Deleted = false), the Agent record has been deleted, but the SCF or Cloud Run service still exists. Manually clean them up according to CleanupHint.

4. Sample Code

async function main() {
const res = await app.agent.deleteAgent({ AgentId: 'agent-xxx' })
if (res.AgentDeleted) {
if (res.ResourceResult?.Deleted) {
console.log('Agent and underlying resources have been deleted')
} else {
console.warn('Agent has been deleted, but underlying resources deletion failed:', res.ResourceResult?.CleanupHint)
}
}
}

Obtain Agent Logs

1. API Description

API feature: Obtains the Agent's runtime logs, automatically routing to the corresponding log source based on the Agent type (SCF function logs / CLS logs)

API declaration: app.agent.getAgentLogs(params): Promise<any>

Response Structure Description
  • SCF Agent: Returns an array of function logs (IFunctionLogDetailRes[])
  • TCBR Agent: Returns the CLS log structure (ISearchClsLogResponse, same as app.log.searchClsLog)

2. Input Parameters

FieldRequiredTypeDescription
AgentIdYesStringAgent ID
offsetNoNumberOffset, default 0 (SCF Agent only)
limitNoNumberNumber of returned logs, default 10, maximum 100
startTimeNoStringStart time, format: YYYY-MM-DD HH:mm:ss
endTimeNoStringEnd time, format: YYYY-MM-DD HH:mm:ss
requestIdNoStringFilter by RequestId (valid for SCF Agent only)

3. Sample Code

async function main() {
const logs = await app.agent.getAgentLogs({
AgentId: 'agent-xxx',
limit: 20,
startTime: '2025-01-01 00:00:00',
endTime: '2025-01-01 23:59:59'
})
console.log(JSON.stringify(logs, null, 2))
}