Skip to main content

BotCore

The base class that provides the fundamental features of function-type Agents. Subclass this class to quickly implement a function-type Agent.

context

Cloud Function context. For details, see Function-based Cloud Run Functions | Input Parameters event and context.

botId

string

botId, that is, the id of the function-type Agent.

botTag

string

The format of the botId for function-type Agents is ibot-xxx-botTag, separated by -, and consists of three segments; we call the third segment botTag.

sseSender

interface SSESender {
send(prop: { data: { content: string } }): void;
end(): void;
}

An object that streams SSE messages. It provides two methods:

  • send() sends an SSE message, where the prop.data.content field contains the Agent's text output.
  • end() ends SSE and sends a closing message. When all content has been sent, this method can be called to end the SSE connection.

Usage Example

Here is a sample that streams a string character by character to the client:

class MyBot extends BotCore {
async sendMessage() {
const ANSWER = "This is a piece of content for streaming.";

for (let i = 0; i < ANSWER.length; i++) {
// Send a message to the client
this.sseSender.send({ data: { content: ANSWER[i] } });
}
// Send completed
this.sseSender.end();
}
}

tools

tools: AITools

BotCore provides some tools for external use, enabling users to quickly implement basic features using tools.

chatRecord

type ChatRecordDataModel = DataModelMethods<{
/** Asynchronous reply content */
async_reply?: string;
/** Whether to reply asynchronously */
need_async_reply?: boolean;
/** Request id */
trace_id?: string;
/** Reply message id */
reply?: string;
/** Replied message id */
reply_to?: string;
/** Original message content */
origin_msg?: string;
/** Source */
trigger_src?: string;
/** Session */
conversation?: string;
/** Sender */
sender?: string;
/** Recommended Questions */
recommend_questions?: string[];
/** Event type */
event?: string;
/** Image URL */
image?: string;
/** Content */
content?: string;
/** Message type */
type?: string;
/** Role */
role?: string;
/** Conversation Record ID */
record_id?: string;
/** Agent ID */
bot_id?: string;
}>;

The chat record data model provides methods such as add, delete, update, and query. For details, refer to Data Model | SDK Type Declarations | DataModelMethods.

tip

The BotCore framework encapsulates methods such as createRecordPair, getChatRecords, and getHistoryMessages on top of this data model, which can cater to requirements in most scenarios. For more granular needs, you may manipulate the chat record data model directly via this property.

Usage Example

Below is an example of obtaining a chat record list:

class MyBot extends BotCore {
getRecordList() {
return this.chatRecord.list({
pageSize: 10,
});
}
}

More usage examples for the data model can be viewed at Data Model | SDK Type Declarations | DataModelMethods.

createRecordPair()

interface CreateRecordPair {
({ userContent: string }): Promise<{
updateBotRecord: ({ content: string }) => Promise<void>;
}>;
}

Create a pair of chat records for 'User - Agent', store them in the data model, and provide a method in the return value to update the Agent's chat records.

In Agent conversation scenarios, chat records occur in pairs. After a user sends a message, the Agent is expected to respond to that specific message.

This method requires the input of the user's chat content and will create the following in the data model:

  • The user's chat record, with content being the passed-in chat message
  • The Agent's chat record, with content being empty

Developers can obtain the updateBotRecord function from the return value. After collecting the complete Agent chat content, they can call this function to update the Agent's chat records.

Usage Example

The following example will implement the Agent by conversing with a large model and store both the user's message records and the large model's responses in the data model.

import OpenAI from "openai";

const client = new OpenAI();

class MyBot extends BotCore {
sendMessage({ msg }) {
// Create a pair of 'User - Agent' chat records
const { updateBotRecord } = await this.createRecordPair({
userContent: msg,
});

const chatCompletion = await client.chat.completions.create({
messages: [{ role: "user", content: "Say this is a test" }],
model: "gpt-4o",
stream: true,
});

// Create a variable to store the Agent's messages
let content = "";
for await (const chunk of stream) {
const chunkContent = chunk.choices[0]?.delta?.content || "";
// Send an SSE message to the client
this.sseSender.send({ data: { content: chunkContent } });
// Accumulate message fragments into the content variable
content += chunkContent;
}

// End SSE transmission
this.sseSender.end();
// Update Agent chat records
await updateBotRecord({ content });
}
}

getHistoryMessages()

interface GetHistoryMessages {
(option?: { size?: number; removeLastUser?: boolean }): Promise<
Array<{
role: "user" | "assistant";
content: string;
}>
>;
}

Historical messages are obtained from the chat record data model. The return value is an array of chat records that has been specially processed to ensure that:

  1. The array starts with the user's chat records.
  2. In the array, user and Agent chat records alternate, forming a question-and-answer pattern.
  3. The array ends with the Agent's chat records (by default).
  4. The content field of each chat record is not empty

Parameters

Input Parameters

ParameterTypeDescription
option.sizenumberThe number of chat records to obtain. This value corresponds to the number of data items retrieved from the chat record data model. Due to filtering applied in this method, the actual number of returned chat records may be less than the specified value.
option.removeLastUserbooleanWhether to ensure that the array ends with the Agent's chat records.

Usage Example

The following example will implement the Agent by conversing with a large model. When initiating a call, it will read the historical messages between the user and the Agent from the data model and pass them together to the large model.

import OpenAI from "openai";

const client = new OpenAI();

class MyBot extends BotCore {
sendMessage({ msg }) {
// Obtain historical messages
const history = await this.getHistoryMessage();

const chatCompletion = await client.chat.completions.create({
messages: [
...history, // Add historical messages to the request parameters
{ role: "user", content: "Say this is a test" },
],
model: "gpt-4o",
stream: true,
});

for await (const chunk of stream) {
const chunkContent = chunk.choices[0]?.delta?.content || "";
// Send an SSE message to the client
this.sseSender.send({ data: { content: chunkContent } });
}

// End SSE transmission
this.sseSender.end();
}
}

getChatRecords()

Obtain chat records from the data model, which is the default implementation of getChatRecords defined in the IBot interface provided by BotCore.