Developing Functional Agents
This article introduces how to develop, debug, and deploy an AI agent based on functional cloud hosting.
The following is a simple Functional AI Agent
sample code:
const { BotRunner, BotCore } = require("@cloudbase/aiagent-framework");
const ANSWER = "Hello, I am an agent, but I can only say this one sentence.";
class MyBot extends BotCore {
async sendMessage() {
for (let i = 0; i < ANSWER.length; i++) {
// Send a message to the client
this.sseSender.send({ data: { content: ANSWER[i] } });
}
// Send complete
this.sseSender.end();
}
}
exports.main = (event, context) =>
BotRunner.run(event, context, new MyBot(context));
For a complete project example of this code, refer to the template: Github, Gitee.
Create a new folder, create index.js
and fill it with the above code, then run npm i @cloudbase/aiagent-framework
to install the required dependencies, and you can implement a simple functional agent.
The key to implementing an Agent
lies in implementing the methods defined in the IBot
interface. With the help of the BotCore
class, it can be implemented with just a few lines of code:
The above code implements a MyBot
class with the following features:
- Inherits from the
BotCore
class, gaining the ability to send SSE responses throughthis.sseSender
- Implements the
IBot
interface. As defined, we need to implement at least thesendMessage
method. The Agent we implemented here will always return a fixed response.
For the usage of the Cloud Development AI Agent Framework @cloudbase/aiagent-framework
, refer to: https://www.npmjs.com/package/@cloudbase/aiagent-framework
Local Development and Debugging of Agents
Create a package.json
file and write the following content:
{
"name": "function-agent-test",
"main": "index.js",
"scripts": {
"login": "tcb login",
"dev": "tcb cloudrun run --runMode=agent -w",
"deploy": "tcb cloudrun deploy"
},
"dependencies": {
"@cloudbase/aiagent-framework": "^1.0.0-beta.10"
},
"devDependencies": {
"@cloudbase/cli": "^2.6.16"
}
}
Install dependencies:
npm install
After installation, you can enter the following command to log in:
npm run login
Start the Agent Service
Run the following command to start the service:
npm run dev
Invoke the Agent Service
Access via Command Line Using cURL
curl 'http://127.0.0.1:3000/v1/aibot/bots/ibot-myBot-botTag/send-message' \
-H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' \
--data-raw '{"msg":"hi"}'
The running results are as follows:

Web Access Using @cloudbase/js-sdk
@cloudbase/js-sdk provides methods for client-side access to Agent interfaces. Refer to the CloudBase documentation for details.
Below is an HTML example using @cloudbase/js-sdk
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test Agent</title>
</head>
<body>
<p id="result"></p>
</body>
<script src="//static.cloudbase.net/cloudbase-js-sdk/2.9.0/cloudbase.full.js"></script>
<script>
const p = document.getElementById("result");
async function main() {
const app = cloudbase.init({
env: "your-env-id", // Fill in your CloudBase environment id
});
const auth = app.auth();
await auth.signInAnonymously();
const ai = app.ai();
const res = await ai.bot.sendMessage({
botId: "ibot-myBot-botTag",
msg: "hi",
});
for await (let x of res.textStream) {
console.log(x);
p.innerText += x;
}
}
main();
</script>
</html>
Start a local server to enable browser access to this HTML file. We also need to configure a proxy service to route requests to the local service. Taking whistle as an example, configure as follows:
/.*.api.tcloudbasegateway.com/([^S]*)/ http://localhost:3000/$1
Open the web page to view the invocation results:

Local Development and Deployment of Agents
Run the following command to deploy the agent:
npm run deploy
Select the environment and service name according to the prompts. The service name must follow the ibot-xxx
format.
Associate Cloud Run Service with Agent
If you have already created a Cloud Run service but haven't created the corresponding agent on the Cloud Development Platform, go to the Cloud Development Platform AI+ page to create one.
Click the + sign to create a Functional Agent, then fill in the corresponding form according to the format.
If the Cloud Run service is named ibot-testAgent
, the corresponding Functional Agent ID should be ibot-testAgent-123456
(the last segment is not used for associating with the Cloud Run service).
Cloud-Based Development and Debugging of Agents
Step.1: Create Functional Agent
Refer to the process described in the documentation https://docs.cloudbase.net/ai/cbrf-agent/quickstart to create a Functional Agent.
After creation, the corresponding agent can be viewed in the agent list.
After clicking 'Online Development', you can enter the online development page to develop agents based on Function-based Cloud Hosting.
Step.2: Develop and Debug Agent Code
- For how to develop, debug, and deploy agents in the cloud, refer to the documentation: https://docs.cloudbase.net/cbrf/how-to-develop-online
- For how to use the
@cloudbase/aiagent-framework
framework, refer to: https://www.npmjs.com/package/@cloudbase/aiagent-framework - For how to write agent code based on Function-based Cloud Hosting, refer to the function writing guide: https://docs.cloudbase.net/cbrf/how-to-writing-functions-code
More sample code can be found:
- Empty Agent sample Github
- Based on DeepSeek Agent sample Github
- Yuanqi Agent sample Github
- Based on Mastra Agent sample Github