Overview
Provides Cloud Development AI integration capabilities, enabling rapid access to large language models and AI agents.
Basic Usage Examples
Publishable Key can be generated in Cloud Development Platform/API Key Configuration
Type Declaration
function ai(): AI;
Return Value
Returns the newly created AI instance.
import cloudbase from "@cloudbase/js-sdk";
// Initialization
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // region, defaults to Shanghai
accessKey: "", // Enter the generated Publishable Key
});
// If the accessKey is filled in, this step is not required
await app.auth.signInAnonymously();
const ai = app.ai();
// Basic Text Generation Example
async function generateText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});
console.log("Generated text:", result.text);
console.log("Consumed tokens:", result.usage);
}
// Streaming Text Generation Example
async function streamText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "what is 1+1" }],
});
for await (let chunk of result.textStream) {
console.log("Received text chunk:", chunk);
}
}
// Agent Chat Example
async function chatWithAgent() {
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
msg: "Hello, please introduce yourself.",
history: [],
});
for await (let text of res.textStream) {
console.log("Agent reply:", text);
}
}
AI
Class used to create AI models.
createModel
function createModel(model: string): ChatModel;
Create the specified AI model.
- Create a new AI model instance
- Return a model instance that implements the ChatModel abstract class
- This instance provides capabilities related to AI-generated text.
参数
Model identifier, such as 'hunyuan-exp', 'hunyuan-lite', etc.
返回
A model instance that implements the ChatModel abstract class, providing capabilities related to AI-generated text
示例
// Create Hunyuan exp model
const model = ai.createModel("hunyuan-exp");
// Create Hunyuan lite model
const liteModel = ai.createModel("hunyuan-lite");
// Create multiple model instances for different scenarios
const expModel = ai.createModel("hunyuan-exp"); // Experience edition with full functionality
const liteModel = ai.createModel("hunyuan-lite"); // Lite edition with fast response
const proModel = ai.createModel("hunyuan-pro"); // Pro edition with enhanced performance
// Select different models based on requirements
async function generateWithModel(scenario: string, content: string) {
let selectedModel;
switch (scenario) {
case "creative":
selectedModel = expModel;
break;
case "quick":
selectedModel = liteModel;
break;
case "professional":
selectedModel = proModel;
break;
default:
selectedModel = liteModel;
}
const result = await selectedModel.generateText({
model: scenario,
messages: [{ role: "user", content: content }],
});
return result.text;
}
bot
An instance of the Bot class is mounted, which aggregates a series of methods for interacting with the Agent. For details, refer to the Bot class documentation.
Usage Examples
const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
registerFunctionTool
function registerFunctionTool(functionTool: FunctionTool): void;
Register function tools. When making large model calls, you can inform the large model of the available function tools. If the large model's response is parsed as a tool call, the corresponding function tool will be automatically invoked.
参数
Definition of the function tool to be registered
返回
No return value
示例
// Define a tool to get weather
const getWeatherTool = {
name: "get_weather",
description: "Returns the weather information for a specific city. Invocation example: get_weather({city: 'Beijing'})",
fn: ({ city }) => `The weather in ${city} is: clear and crisp autumn weather!`,
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City to query",
},
},
required: ["city"],
},
};
// Register a tool
ai.registerFunctionTool(getWeatherTool);
// Use tools for conversation
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: [getWeatherTool],
messages: [
{
role: "user",
content: "Please tell me the weather conditions in Beijing",
},
],
});
console.log(result.text);
// Multi-tool Integrated System
class ToolIntegrationSystem {
private registeredTools: Map<string, FunctionTool> = new Map();
// Register multiple tools
registerTools(tools: FunctionTool[]): void {
tools.forEach((tool) => {
ai.registerFunctionTool(tool);
this.registeredTools.set(tool.name, tool);
console.log(`✓ Tool registration successful: ${tool.name}`);
});
}
// Get all available tools
getAvailableTools(): FunctionTool[] {
return Array.from(this.registeredTools.values());
}
// Intelligent Conversation Assistant
async smartAssistant(question: string): Promise<string> {
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: this.getAvailableTools(),
messages: [
{
role: "user",
content: question,
},
],
});
return result.text;
}
// Tool Usage Statistics
getToolUsageStats(): Map<string, number> {
const stats = new Map<string, number>();
// In practical applications, this can log tool invocation counts
return stats;
}
}
// Define multiple tools
const tools = [
{
name: "get_weather",
description: "Retrieves city weather information",
fn: ({ city }) => `Weather in ${city}: Sunny, 25℃`,
parameters: {
type: "object",
properties: { city: { type: "string", description: "City name" } },
required: ["city"],
},
},
{
name: "calculate",
description: "Performs mathematical calculations",
fn: ({ expression }) => `Calculation result: ${eval(expression)}`,
parameters: {
type: "object",
properties: { expression: { type: "string", description: "Mathematical expression" } },
required: ["expression"],
},
},
{
name: "search_info",
description: "Searches for information",
fn: ({ keyword }) => `Search results: information about ${keyword}`,
parameters: {
type: "object",
properties: { keyword: { type: "string", description: "Search keyword" } },
required: ["keyword"],
},
},
];
// Usage Examples
const toolSystem = new ToolIntegrationSystem();
toolSystem.registerTools(tools);
// Intelligent Conversation
const answer = await toolSystem.smartAssistant(
"Calculate the result of 15*8+20 and tell me the weather in Beijing
);
console.log("Assistant answer:", answer);
// Dynamic Tool Management System
class DynamicToolManager {
private toolRegistry: Map<string, FunctionTool> = new Map();
// Dynamic Tool Registration
registerTool(tool: FunctionTool): void {
ai.registerFunctionTool(tool);
this.toolRegistry.set(tool.name, tool);
console.log(`Tool registration: ${tool.name}`);
}
// Dynamic Tool Uninstallation
unregisterTool(toolName: string): boolean {
if (this.toolRegistry.has(toolName)) {
this.toolRegistry.delete(toolName);
console.log(`Tool uninstallation: ${toolName}`);
return true;
}
return false;
}
// Dynamically select tools based on the context
async contextualAssistant(
context: string,
question: string
): Promise<string> {
const relevantTools = this.selectRelevantTools(context, question);
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: relevantTools,
messages: [
{
role: "system",
content: `Current context: ${context}`,
},
{
role: "user",
content: question,
},
],
});
return result.text;
}
// Select relevant tools based on the context
private selectRelevantTools(
context: string,
question: string
): FunctionTool[] {
const tools = Array.from(this.toolRegistry.values());
// Simple keyword matching algorithm
const keywords = [...context.split(" "), ...question.split(" ")];
return tools.filter((tool) => {
return keywords.some((keyword) =>
tool.description.toLowerCase().includes(keyword.toLowerCase())
);
});
}
// Hot update tool
updateTool(toolName: string, updatedTool: FunctionTool): boolean {
if (this.toolRegistry.has(toolName)) {
this.unregisterTool(toolName);
this.registerTool(updatedTool);
console.log(`Tool update: ${toolName}`);
return true;
}
return false;
}
// Get tool list
listTools(): string[] {
return Array.from(this.toolRegistry.keys());
}
}
// Usage Examples
const toolManager = new DynamicToolManager();
// Register initial tools
toolManager.registerTool({
name: "weather",
description: "Retrieves weather information",
fn: ({ city }) => `${city} weather: sunny`,
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
});
// Dynamically add new tools
toolManager.registerTool({
name: "translate",
description: "Translates text",
fn: ({ text, to }) => `${text} translated to ${to}: sample translation result`,
parameters: {
type: "object",
properties: {
text: { type: "string" },
to: { type: "string" },
},
required: ["text", "to"],
},
});
// Context-aware conversation
const answer = await toolManager.contextualAssistant(
"weather and translation related problems",
"Translate 'Hello' into English and tell me the weather in Shanghai"
);
console.log("Intelligent Answer:", answer);
// View available tools
console.log("Available tools:", toolManager.listTools());
ChatModel
This abstract class describes the interface provided by the AI text generation model class.
generateText
function generateText(data: BaseChatModelInput): Promise<{
rawResponses: Array<unknown>;
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
error?: unknown;
}>;
Invoking large models to generate text.
- Send a message to the large model and retrieve the generated text response
- Supports complete dialogue context management
- Return detailed invocation information and token consumption statistics
参数
Large model input parameters, including model configuration and message content
返回
Generated text response from the large model
示例
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});
console.log("Generated text:", result.text);
console.log("Consumed tokens:", result.usage);
const model = ai.createModel("hunyuan-exp");
// Multi-turn conversation example
const messages = [
{ role: "user", content: "Hello, I'd like to learn about ancient Chinese literature." },
{
role: "assistant",
content:
"Ancient Chinese literature has a long and rich history, from the Book of Songs and Chu Ci to Tang poetry and Song Ci, all possessing profound cultural connotations.",
},
{ role: "user", content: "Could you introduce the characteristics of Tang poetry?" },
];
const result = await model.generateText({
model: "hunyuan-lite",
messages: messages,
temperature: 0.7,
topP: 0.9,
});
console.log("Introduction to Tang poetry:", result.text);
// Register a weather query tool
ai.registerFunctionTool({
name: "get_weather",
description: "Queries city weather information",
fn: ({ city }) => `${city} weather is: sunny, 25℃`,
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City name to query",
},
},
required: ["city"],
},
});
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Please tell me the weather conditions in Beijing" }],
tools: [getWeatherTool],
toolChoice: "auto",
});
console.log("Large model response:", result.text);
streamText
function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;
Streaming invocation of large models to generate text.
- During streaming invocation, the generated text and other response data will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
- Streaming invocation of large models to generate text, supporting real-time retrieval of incremental content
参数
Large model input parameters, including model configuration and message content
返回
Results of streaming text generation, including text streams and data streams
示例
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});
// Get text stream
for await (let chunk of result.textStream) {
console.log("Received text chunk:", chunk);
}
// 1
// Add
// 1
// Result
// Is
// 2
// .
// Get data stream
for await (let data of result.dataStream) {
console.log("Received data block:", data);
}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// Display streaming content in real time on the web page
async function displayStreamingText(question: string) {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: question }],
});
const outputElement = document.getElementById("output");
outputElement.innerHTML = "";
for await (let chunk of result.textStream) {
outputElement.innerHTML += chunk;
// Scroll to the latest content
outputElement.scrollTop = outputElement.scrollHeight;
}
}
// Usage Examples
displayStreamingText("Please provide a detailed introduction to the development history of Tang poetry");
// Monitor streaming generation progress
async function monitorStreamProgress(question: string) {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: question }],
});
let totalChunks = 0;
let startTime = Date.now();
for await (let chunk of result.textStream) {
totalChunks++;
console.log(`No.${totalChunks} text chunk: ${chunk}`);
}
const endTime = Date.now();
console.log(
`Generation completed, total ${totalChunks} text chunks, took ${endTime - startTime}ms`
);
// Get complete message and token statistics
const messages = await result.messages;
const usage = await result.usage;
console.log("Complete conversation:", messages);
console.log("Token consumption:", usage);
}
Bot
A class for interacting with the Agent.
get
function get(props: { botId: string }): Promise<BotInfo>;
Retrieve information about an Agent.
- Retrieve detailed Agent information based on Agent ID.
- Return complete Agent information including basic configuration, welcome message, avatar, etc.
参数
Parameters for retrieving Agent information
返回
Detailed information of the Agent
示例
const res = await ai.bot.get({ botId: "botId-xxx" });
console.log("Agent information:", res);
// Load Agent configuration on application startup
async function initializeAgent(botId: string) {
try {
const agentInfo = await ai.bot.get({ botId });
// Configure application interface
document.title = `${agentInfo.name} - AI Assistant`;
// Display welcome message
const welcomeElement = document.getElementById("welcome");
welcomeElement.innerHTML = `
<div class="agent-header">
<img src="${agentInfo.avatar}" alt="${agentInfo.name}" class="avatar" />
<h2>${agentInfo.name}</h2>
<p>${agentInfo.introduction}</p>
</div>
`;
// Set chat background
if (agentInfo.background) {
document.body.style.backgroundImage = `url(${agentInfo.background})`;
}
return agentInfo;
} catch (error) {
console.error("Failed to load Agent configuration:", error);
throw error;
}
}
// Usage Examples
initializeAgent("bot-27973647");
// Manage configurations of multiple Agents
class AgentManager {
private agents: Map<string, BotInfo> = new Map();
async loadAgent(botId: string) {
if (this.agents.has(botId)) {
return this.agents.get(botId);
}
const agentInfo = await ai.bot.get({ botId });
this.agents.set(botId, agentInfo);
return agentInfo;
}
getAgentList() {
return Array.from(this.agents.values());
}
async switchAgent(botId: string) {
const agentInfo = await this.loadAgent(botId);
// Update interface display
this.updateUI(agentInfo);
return agentInfo;
}
private updateUI(agentInfo: BotInfo) {
// Update interface elements
console.log("Switch to Agent:", agentInfo.name);
}
}
// Usage Examples
const agentManager = new AgentManager();
await agentManager.loadAgent("bot-27973647");
await agentManager.loadAgent("bot-27973648");
list
function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
}): Promise<AgentListResult>;
Batch retrieval of information for multiple Agents.
- Query and filter the available Agent list.
- Support paginated queries and conditional filtering
- Return Agent's basic information and configuration details
- Applicable for building applications such as Agent selectors and Agent management interfaces.
参数
Parameters for querying the Agent list
返回
Agent list query result
示例
const agentList = await ai.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});
console.log("Total Agents:", agentList.total);
console.log("Agent list:", agentList.botList);
// Filter Agents by conditions
async function searchAgents(keyword: string, page: number = 1) {
const result = await ai.bot.list({
pageNumber: page,
pageSize: 20,
name: keyword,
enable: true,
information: keyword,
introduction: keyword,
});
console.log(`Found ${result.total} matching Agents`);
result.botList.forEach((agent) => {
console.log(`- ${agent.name}: ${agent.introduction}`);
});
return result;
}
// Usage Examples
await searchAgents("translation"); // Search for translation-related Agents
await searchAgents("customer service", 2); // Search for customer service related Agents, page 2
// Build Agent selector interface
class AgentSelector {
private currentPage: number = 1;
private pageSize: number = 12;
async loadAgents(keyword: string = "") {
const result = await ai.bot.list({
pageNumber: this.currentPage,
pageSize: this.pageSize,
name: keyword,
enable: true,
information: keyword,
introduction: keyword,
});
this.displayAgents(result.botList);
this.updatePagination(result.total);
return result;
}
private displayAgents(agents: any[]) {
const container = document.getElementById("agent-list");
container.innerHTML = "";
agents.forEach((agent) => {
const agentCard = document.createElement("div");
agentCard.className = "agent-card";
agentCard.innerHTML = `
<img src="${agent.avatar}" alt="${agent.name}" class="agent-avatar" />
<div class="agent-info">
<h3>${agent.name}</h3>
<p>${agent.introduction}</p>
<button onclick="selectAgent('${agent.botId}')">Select</button>
</div>
`;
container.appendChild(agentCard);
});
}
private updatePagination(total: number) {
const pagination = document.getElementById("pagination");
const totalPages = Math.ceil(total / this.pageSize);
pagination.innerHTML = `
<button onclick="prevPage()" ${
this.currentPage <= 1 ? "disabled" : ""
}>Previous</button>
<span>Page ${this.currentPage} of ${totalPages}</span>
<button onclick="nextPage()" ${
this.currentPage >= totalPages ? "disabled" : ""
}>Next</button>
`;
}
async nextPage() {
this.currentPage++;
await this.loadAgents();
}
async prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
await this.loadAgents();
}
}
selectAgent(botId: string) {
console.log("Selected Agent ID:", botId);
// Start a conversation with the selected Agent
this.startChat(botId);
}
private startChat(botId: string) {
// Implement the logic to start a conversation with the Agent
console.log("Start conversation with Agent:", botId);
}
}
// Usage Examples
const selector = new AgentSelector();
await selector.loadAgents(); // Load the first page of Agents
// Search for specific types of Agents
await selector.loadAgents("Writing Assistant");
sendMessage
function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
Converse with the Agent.
- Response will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
- Supports multi-turn conversation context management
- Returns streaming responses, supporting real-time retrieval of Agent replies
- Applicable for building applications such as chatbots and intelligent assistants.
参数
Parameters for sending messages
返回
Results of streaming conversation, including text streams and data streams
示例
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "You are Li Bai.", role: "user" }],
msg: "Hello",
});
// Get text stream
for await (let text of res.textStream) {
console.log("Agent reply:", text);
}
// Get data stream
for await (let data of res.dataStream) {
console.log("Detailed data:", data);
}
// Multi-turn conversation example
class ChatSession {
private history: Array<{ role: string; content: string }> = [];
async sendMessage(botId: string, message: string) {
const res = await ai.bot.sendMessage({
botId: botId,
history: this.history,
msg: message,
});
let fullResponse = "";
// Display reply in real time
for await (let chunk of res.textStream) {
fullResponse += chunk;
this.updateDisplay(fullResponse);
}
// Record conversation history
this.history.push({ role: "user", content: message });
this.history.push({ role: "assistant", content: fullResponse });
return fullResponse;
}
private updateDisplay(text: string) {
const chatArea = document.getElementById("chat-area");
chatArea.innerHTML = text;
chatArea.scrollTop = chatArea.scrollHeight;
}
clearHistory() {
this.history = [];
}
getHistory() {
return this.history;
}
}
// Usage Examples
const chatSession = new ChatSession();
await chatSession.sendMessage("botId-xxx", "Hello, please introduce yourself.");
await chatSession.sendMessage("botId-xxx", "Can you help me write a poem?");
// Build a real-time chat application
class ChatApp {
private botId: string;
private conversationId: string;
constructor(botId: string) {
this.botId = botId;
this.setupEventListeners();
}
private setupEventListeners() {
const sendBtn = document.getElementById("send-btn");
const input = document.getElementById("message-input") as HTMLInputElement;
sendBtn.addEventListener("click", () => {
this.sendMessage(input.value);
input.value = "";
});
input.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
this.sendMessage(input.value);
input.value = "";
}
});
}
async sendMessage(message: string) {
if (!message.trim()) return;
// Display user message
this.displayMessage("user", message);
try {
const res = await ai.bot.sendMessage({
botId: this.botId,
history: this.getChatHistory(),
msg: message,
});
// Display streaming response
const assistantDiv = this.displayMessage("assistant", "");
for await (let chunk of res.textStream) {
assistantDiv.innerHTML += chunk;
assistantDiv.scrollIntoView({ behavior: "smooth" });
}
} catch (error) {
this.displayMessage("error", "Failed to send message: " + error.message);
}
}
private displayMessage(role: string, content: string): HTMLDivElement {
const chatContainer = document.getElementById("chat-container");
const messageDiv = document.createElement("div");
messageDiv.className = `message ${role}`;
messageDiv.innerHTML = content;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
return messageDiv;
}
private getChatHistory() {
const messages = document.querySelectorAll(".message");
const history = [];
for (let message of messages) {
const role = message.classList.contains("user") ? "user" : "assistant";
const content = message.innerHTML;
history.push({ role, content });
}
return history;
}
}
// Usage Examples
const chatApp = new ChatApp("botId-xxx");
getChatRecords
function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
}): Promise<ChatRecordsResult>;
Retrieve chat history.
- Retrieve the chat history of the specified Agent.
- Support paginated queries and sorting
- Return complete conversation history information
- Suitable for building chat history viewing features
参数
parameters for retrieving chat history
返回
Chat history query result
示例
const records = await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
console.log("Total records count:", records.total);
console.log("Record list:", records.recordList);
// Query paginated chat records
class ChatHistoryManager {
private botId: string;
private currentPage: number = 1;
private pageSize: number = 20;
constructor(botId: string) {
this.botId = botId;
}
async loadPage(pageNumber: number) {
const records = await ai.bot.getChatRecords({
botId: this.botId,
pageNumber: pageNumber,
pageSize: this.pageSize,
sort: "desc", // in descending order by time
});
this.currentPage = pageNumber;
return records;
}
async nextPage() {
return this.loadPage(this.currentPage + 1);
}
async previousPage() {
if (this.currentPage > 1) {
return this.loadPage(this.currentPage - 1);
}
return null;
}
getTotalPages(totalRecords: number) {
return Math.ceil(totalRecords / this.pageSize);
}
}
// Usage Examples
const historyManager = new ChatHistoryManager("botId-xxx");
const firstPage = await historyManager.loadPage(1);
console.log("Page 1 records:", firstPage.recordList);
const secondPage = await historyManager.nextPage();
console.log("Page 2 records:", secondPage.recordList);
// Build chat history viewing interface
class ChatHistoryViewer {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupUI();
}
private setupUI() {
const historyContainer = document.getElementById("history-container");
const prevBtn = document.getElementById("prev-btn");
const nextBtn = document.getElementById("next-btn");
const pageInfo = document.getElementById("page-info");
prevBtn.addEventListener("click", () => this.previousPage());
nextBtn.addEventListener("click", () => this.nextPage());
}
async loadHistory(pageNumber: number = 1) {
const records = await ai.bot.getChatRecords({
botId: this.botId,
pageNumber: pageNumber,
pageSize: 10,
sort: "desc",
});
this.displayRecords(records.recordList);
this.updatePageInfo(pageNumber, records.total);
}
private displayRecords(records: any[]) {
const container = document.getElementById("records-container");
container.innerHTML = "";
records.forEach((record) => {
const recordDiv = document.createElement("div");
recordDiv.className = "chat-record";
recordDiv.innerHTML = `
<div class="record-header">
<span class="role">${record.role}</span>
<span class="time">${record.createTime}</span>
</div>
<div class="content">${record.content}</div>
`;
container.appendChild(recordDiv);
});
}
private updatePageInfo(currentPage: number, total: number) {
const pageInfo = document.getElementById("page-info");
const totalPages = Math.ceil(total / 10);
pageInfo.textContent = `Page ${currentPage} of ${totalPages} pages, Total ${total} records`;
}
async nextPage() {
const currentPage = parseInt(
document.getElementById("page-info").textContent.match(/Page (\d+)/)[1]
);
await this.loadHistory(currentPage + 1);
}
async previousPage() {
const currentPage = parseInt(
document.getElementById("page-info").textContent.match(/Page (\d+)/)[1]
);
if (currentPage > 1) {
await this.loadHistory(currentPage - 1);
}
}
}
// Usage Examples
const historyViewer = new ChatHistoryViewer("botId-xxx");
await historyViewer.loadHistory(1);
sendFeedback
function sendFeedback(props: {
userFeedback: IUserFeedback;
botId: string;
}): Promise<void>;
Send feedback for a specific chat message.
- Evaluate and provide feedback for specified chat messages.
- Supports various feedback methods such as rating, commenting, and tagging.
- Help improve the quality of Agent responses
- Suitable for building user feedback systems
参数
Parameters for sending feedback
返回
No return value
示例
const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "Excellent",
rating: 5,
tags: ["Graceful"],
aiAnswer: "Fallen petals are scattered in profusion",
input: "Give me an idiom",
type: "upvote",
},
botId: "botId-xxx",
});
console.log("Feedback sent successfully");
// Build a user feedback system
class FeedbackSystem {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupFeedbackUI();
}
private setupFeedbackUI() {
const feedbackButtons = document.querySelectorAll(".feedback-btn");
feedbackButtons.forEach((button) => {
button.addEventListener("click", (e) => {
const recordId = e.target.dataset.recordId;
const type = e.target.dataset.type; // upvote or downvote
this.showFeedbackDialog(recordId, type);
});
});
}
private showFeedbackDialog(recordId: string, type: string) {
const dialog = document.getElementById("feedback-dialog");
const submitBtn = document.getElementById("submit-feedback");
const ratingInput = document.getElementById("rating") as HTMLInputElement;
const commentInput = document.getElementById(
"comment"
) as HTMLTextAreaElement;
const tagsInput = document.getElementById("tags") as HTMLInputElement;
dialog.style.display = "block";
submitBtn.onclick = async () => {
await this.submitFeedback(recordId, type, {
rating: parseInt(ratingInput.value),
comment: commentInput.value,
tags: tagsInput.value.split(",").map((tag) => tag.trim()),
});
dialog.style.display = "none";
};
}
async submitFeedback(
recordId: string,
type: string,
feedback: {
rating: number;
comment: string;
tags: string[];
}
) {
try {
await ai.bot.sendFeedback({
userFeedback: {
botId: this.botId,
recordId: recordId,
comment: feedback.comment,
rating: feedback.rating,
tags: feedback.tags,
aiAnswer: this.getCurrentAnswer(),
input: this.getCurrentQuestion(),
type: type,
},
botId: this.botId,
});
this.showSuccessMessage("Thank you for your feedback!");
} catch (error) {
Failed to send feedback: " + error.message
}
}
private getCurrentAnswer(): string {
// Get the answer content of the current conversation
const answerElement = document.querySelector(
".assistant-message:last-child"
);
return answerElement?.textContent || "";
}
private getCurrentQuestion(): string {
// Get the question content of the current conversation
const questionElement = document.querySelector(".user-message:last-child");
return questionElement?.textContent || "";
}
private showSuccessMessage(message: string) {
const notification = document.createElement("div");
notification.className = "notification success";
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 3000);
}
private showErrorMessage(message: string) {
const notification = document.createElement("div");
notification.className = "notification error";
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 5000);
}
}
// Usage Examples
const feedbackSystem = new FeedbackSystem("botId-xxx");
// Batch process user feedback
class BatchFeedbackProcessor {
private botId: string;
private feedbackQueue: Array<{
recordId: string;
type: string;
rating: number;
comment: string;
tags: string[];
}> = [];
constructor(botId: string) {
this.botId = botId;
this.startProcessing();
}
addFeedback(
recordId: string,
type: string,
rating: number,
comment: string,
tags: string[]
) {
this.feedbackQueue.push({ recordId, type, rating, comment, tags });
}
private async startProcessing() {
setInterval(async () => {
if (this.feedbackQueue.length > 0) {
await this.processBatch();
}
}, 5000); // Process every 5 seconds
}
private async processBatch() {
const batch = this.feedbackQueue.splice(0, 10); // Process up to 10 feedback items per batch
const promises = batch.map((feedback) => {
return ai.bot.sendFeedback({
userFeedback: {
botId: this.botId,
recordId: feedback.recordId,
comment: feedback.comment,
rating: feedback.rating,
tags: feedback.tags,
aiAnswer: "", // Needs to be obtained based on actual circumstances
input: "", // Needs to be obtained based on actual circumstances
type: feedback.type,
},
botId: this.botId,
});
});
try {
await Promise.all(promises);
console.log(`Successfully processed ${batch.length} feedback items`);
} catch (error) {
console.error("Batch processing feedback failed:", error);
// Requeue failed feedback
this.feedbackQueue.unshift(...batch);
}
}
getQueueSize() {
return this.feedbackQueue.length;
}
}
// Usage Examples
const feedbackProcessor = new BatchFeedbackProcessor("botId-xxx");
// Add multiple feedback items
feedbackProcessor.addFeedback("record-1", "upvote", 5, "The answer is excellent", [
"Accurate",
"Detailed",
]);
feedbackProcessor.addFeedback("record-2", "downvote", 2, "The answer is not accurate enough", [
"Inaccurate",
]);
console.log("Current queue size:", feedbackProcessor.getQueueSize());
getFeedback
function getFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
}): Promise<FeedbackResult>;
Retrieve existing feedback.
- Query the user feedback records of the specified Agent.
- Support multiple filter conditions: time range, score range, user filtering, etc.
- Return paginated feedback results and statistics
- Suitable for building feedback analysis and management systems
参数
Parameters for querying feedback information
返回
Feedback query results
示例
const res = await ai.bot.getFeedback({
botId: "botId-xxx",
from: 0,
to: 0,
maxRating: 4,
minRating: 3,
pageNumber: 1,
pageSize: 10,
sender: "user-a",
senderFilter: "include",
type: "upvote",
});
console.log("Total Feedback:", res.total);
console.log("Feedback List:", res.feedbackList);
// Build a feedback analysis system
class FeedbackAnalyzer {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async analyzeFeedback(timeRange: { from: number; to: number }) {
const feedback = await ai.bot.getFeedback({
botId: this.botId,
from: timeRange.from,
to: timeRange.to,
minRating: 1,
maxRating: 5,
pageNumber: 1,
pageSize: 1000, // Obtain sufficient data for analysis
sender: "",
senderFilter: "include",
type: "",
});
return this.generateReport(feedback.feedbackList);
}
private generateReport(feedbackList: any[]) {
const report = {
total: feedbackList.length,
upvotes: feedbackList.filter((f) => f.type === "upvote").length,
downvotes: feedbackList.filter((f) => f.type === "downvote").length,
averageRating: this.calculateAverageRating(feedbackList),
commonTags: this.getCommonTags(feedbackList),
ratingDistribution: this.getRatingDistribution(feedbackList),
topComments: this.getTopComments(feedbackList),
};
return report;
}
private calculateAverageRating(feedbackList: any[]): number {
const ratings = feedbackList.map((f) => f.rating).filter((r) => r > 0);
return ratings.length > 0
? ratings.reduce((a, b) => a + b, 0) / ratings.length
: 0;
}
private getCommonTags(feedbackList: any[]): string[] {
const tagCounts = new Map<string, number>();
feedbackList.forEach((feedback) => {
feedback.tags?.forEach((tag) => {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
});
});
return Array.from(tagCounts.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map((entry) => entry[0]);
}
private getRatingDistribution(feedbackList: any[]): Record<number, number> {
const distribution = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
feedbackList.forEach((feedback) => {
if (feedback.rating >= 1 && feedback.rating <= 5) {
distribution[feedback.rating]++;
}
});
return distribution;
}
private getTopComments(feedbackList: any[]): any[] {
return feedbackList
.filter((f) => f.comment && f.comment.length > 0)
.sort((a, b) => b.rating - a.rating)
.slice(0, 5);
}
}
// Usage Examples
const analyzer = new FeedbackAnalyzer("botId-xxx");
const report = await analyzer.analyzeFeedback({
from: Date.now() - 7 * 24 * 60 * 60 * 1000, // Last 7 days
to: Date.now(),
});
console.log("Feedback Analysis Report:", report);
// Build a feedback management interface
class FeedbackManager {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupUI();
}
private setupUI() {
const filterForm = document.getElementById("feedback-filter-form");
const resultsContainer = document.getElementById("feedback-results");
const exportBtn = document.getElementById("export-feedback");
filterForm.addEventListener("submit", (e) => {
e.preventDefault();
this.loadFeedback(this.getFilterValues());
});
exportBtn.addEventListener("click", () => {
this.exportFeedback();
});
}
private getFilterValues() {
const formData = new FormData(
document.getElementById("feedback-filter-form")
);
return {
type: formData.get("type") as string,
minRating: parseInt(formData.get("minRating") as string),
maxRating: parseInt(formData.get("maxRating") as string),
from: this.parseDate(formData.get("fromDate") as string),
to: this.parseDate(formData.get("toDate") as string),
pageNumber: 1,
pageSize: 50,
};
}
private parseDate(dateString: string): number {
return dateString ? new Date(dateString).getTime() : 0;
}
async loadFeedback(filters: any) {
try {
const res = await ai.bot.getFeedback({
botId: this.botId,
...filters,
sender: "",
senderFilter: "include",
});
this.displayResults(res.feedbackList, res.total);
} catch (error) {
this.showError("Failed to load feedback: " + error.message);
}
}
private displayResults(feedbackList: any[], total: number) {
const container = document.getElementById("feedback-results");
container.innerHTML = "";
const summary = document.createElement("div");
summary.className = "feedback-summary";
summary.innerHTML = `Found a total of ${total} feedback records`;
container.appendChild(summary);
feedbackList.forEach((feedback) => {
const feedbackCard = this.createFeedbackCard(feedback);
container.appendChild(feedbackCard);
});
}
private createFeedbackCard(feedback: any): HTMLDivElement {
const card = document.createElement("div");
card.className = `feedback-card ${feedback.type}`;
card.innerHTML = `
<div class="feedback-header">
<span class="type ${feedback.type}">${
feedback.type === "upvote" ? "👍" : "👎"
}</span>
<span class="rating">Rating: ${feedback.rating}/5</span>
<span class="time">${new Date(
feedback.createTime
).toLocaleString()}</span>
</div>
<div class="feedback-content">
<div class="question"><strong>Question:</strong> ${feedback.input}</div>
<div class="answer"><strong>Answer:</strong> ${feedback.aiAnswer}</div>
<div class="comment"><strong>Comment:</strong> ${feedback.comment}</div>
<div class="tags"><strong>Tag:</strong> ${
feedback.tags?.join(", ") || "None"
}</div>
</div>
`;
return card;
}
private async exportFeedback() {
const res = await ai.bot.getFeedback({
botId: this.botId,
from: 0,
to: Date.now(),
minRating: 1,
maxRating: 5,
pageNumber: 1,
pageSize: 10000,
sender: "",
senderFilter: "include",
type: "",
});
const csv = this.convertToCSV(res.feedbackList);
this.downloadCSV(csv, `feedback_${this.botId}_${Date.now()}.csv`);
}
private convertToCSV(feedbackList: any[]): string {
const headers = ["Type", "Rating", "Question", "Answer", "Comment", "Tag", "Time"];
const rows = feedbackList.map((f) => [
f.type,
f.rating,
f.input,
f.aiAnswer,
f.comment,
f.tags?.join(";"),
new Date(f.createTime).toLocaleString(),
]);
return [headers, ...rows]
.map((row) => row.map((cell) => `"${cell}"`).join(","))
.join("\n");
}
private downloadCSV(csv: string, filename: string) {
const blob = new Blob([csv], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
private showError(message: string) {
const errorDiv = document.createElement("div");
errorDiv.className = "error-message";
errorDiv.textContent = message;
document.getElementById("feedback-results").appendChild(errorDiv);
}
}
// Usage Examples
const feedbackManager = new FeedbackManager("botId-xxx");
uploadFiles
function uploadFiles(props: {
botId: string;
fileList: Array<{
fileId: string;
fileName: string;
type: "file";
}>;
}): Promise<void>;
Upload files from cloud storage to the Agent for document-based chatting.
- Supports uploading files from cloud storage to the specified Agent
- Uploaded files can be used for document chat functionality.
- Supports batch uploading of multiple files.
- Applicable for building application scenarios such as document Q&A and file analysis.
参数
File upload parameters
返回
No return value
示例
// Upload a single file to the specified Agent.
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://document.docx",
fileName: "document.docx",
type: "file",
},
],
});
console.log("File uploaded successfully");
// Batch upload multiple files
async function uploadAndChat(
botId: string,
files: Array<{ fileId: string; fileName: string }>
) {
// Upload a file
await ai.bot.uploadFiles({
botId: botId,
fileList: files.map((file) => ({
...file,
type: "file" as const,
})),
});
console.log("File upload completed, starting document chat");
// Chat based on uploaded files
const res = await ai.bot.sendMessage({
botId: botId,
msg: "Please analyze the main content of these documents",
files: files.map((f) => f.fileId),
});
// Stream responses
for await (let text of res.textStream) {
console.log(text);
}
}
// Usage Examples
await uploadAndChat("your-bot-id", [
{ fileId: "cloud://report.pdf", fileName: "Quarterly Report.pdf" },
{ fileId: "cloud://data.xlsx", fileName: "Data Spreadsheet.xlsx" },
]);
// Build a document Q&A system
class DocumentQASystem {
private botId: string;
private uploadedFiles: string[] = [];
constructor(botId: string) {
this.botId = botId;
}
async uploadDocuments(files: Array<{ fileId: string; fileName: string }>) {
await ai.bot.uploadFiles({
botId: this.botId,
fileList: files.map((file) => ({
...file,
type: "file" as const,
})),
});
this.uploadedFiles.push(...files.map((f) => f.fileId));
console.log(`Uploaded ${files.length} files`);
}
async askQuestion(question: string): Promise<string> {
const res = await ai.bot.sendMessage({
botId: this.botId,
msg: question,
files: this.uploadedFiles,
});
let fullAnswer = "";
for await (let text of res.textStream) {
fullAnswer += text;
}
return fullAnswer;
}
async interactiveChat() {
console.log("Document Q&A System is now running. Type 'exit' to quit");
while (true) {
const question = await this.getUserInput("Please enter your question: ");
if (question.toLowerCase() === "exit") {
console.log("Goodbye!");
break;
}
console.log("Thinking...");
const answer = await this.askQuestion(question);
console.log("Answer:", answer);
console.log("---");
}
}
private getUserInput(prompt: string): Promise<string> {
return new Promise((resolve) => {
// In practical applications, this could be a browser input field or command line input
const input = prompt;
resolve(input);
});
}
}
// Usage Examples
const qaSystem = new DocumentQASystem("doc-bot-id");
// Upload documents
await qaSystem.uploadDocuments([
{ fileId: "cloud://manual.pdf", fileName: "Product Manual.pdf" },
{ fileId: "cloud://faq.docx", fileName: "FAQ.docx" },
]);
// Start interactive Q&A
await qaSystem.interactiveChat();
getRecommendQuestions
function getRecommendQuestions(props: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
Generating recommended questions based on conversation context to intelligently generate relevant question suggestions.
- Intelligently recommend relevant questions based on current conversation content and historical records
- Supports streaming responses for real-time retrieval of recommended questions
- Applicable for building applications such as intelligent conversational assistants and chatbots.
- Help users discover more related topics to enhance the conversation experience
参数
Parameters for retrieving recommended questions
返回
streaming recommended questions
示例
// Retrieve recommended questions
const res = await ai.bot.getRecommendQuestions({
botId: "botId-xxx",
name: "Smart Assistant",
introduction: "I am an intelligent conversation assistant",
agentSetting: "Professional and friendly AI assistant",
msg: "Hello, I'd like to learn about artificial intelligence.",
history: [
{ content: "Who are you?", role: "user" },
{ content: "I am an intelligent assistant that can help you answer various questions", role: "assistant" },
],
});
// Retrieve recommended questions via streaming
for await (let question of res.textStream) {
console.log("Recommended questions:", question);
}
// Build an intelligent dialogue assistant to automatically recommend related questions.
class SmartChatAssistant {
private botId: string;
private conversationHistory: Array<{ role: string; content: string }> = [];
constructor(botId: string) {
this.botId = botId;
}
async sendMessage(message: string): Promise<string> {
// Add user message to history
this.conversationHistory.push({ role: "user", content: message });
// Send a message and get the answer
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
history: this.conversationHistory,
});
let assistantReply = "";
for await (let text of response.textStream) {
assistantReply += text;
}
// Add assistant's answer to history
this.conversationHistory.push({
role: "assistant",
content: assistantReply,
});
return assistantReply;
}
async getRecommendedQuestions(): Promise<string[]> {
if (this.conversationHistory.length === 0) {
return [];
}
const latestMessage =
this.conversationHistory[this.conversationHistory.length - 1].content;
const res = await ai.bot.getRecommendQuestions({
botId: this.botId,
name: "Smart Assistant",
introduction: "Intelligent Dialogue Assistant",
agentSetting: "",
msg: latestMessage,
history: this.conversationHistory,
});
const questions: string[] = [];
for await (let question of res.textStream) {
questions.push(question);
}
return questions;
}
async interactiveChat() {
console.log(
"Smart Assistant is running. Enter 'exit' to quit, enter 'suggest' to get recommended questions."
);
while (true) {
const userInput = await this.getUserInput("Please enter: ");
if (userInput.toLowerCase() === "exit") {
console.log("Goodbye!");
break;
}
if (userInput.toLowerCase() === "suggest") {
console.log("Recommending related questions for you...");
const questions = await this.getRecommendedQuestions();
console.log("Recommended questions:");
questions.forEach((q, i) => console.log(`${i + 1}. ${q}`));
continue;
}
console.log("Thinking...");
const reply = await this.sendMessage(userInput);
console.log("Assistant answer:", reply);
// Automatically recommend related questions
console.log("\nYou may also want to know:");
const questions = await this.getRecommendedQuestions();
questions.slice(0, 3).forEach((q, i) => console.log(`${i + 1}. ${q}`));
console.log("---");
}
}
private getUserInput(prompt: string): Promise<string> {
return new Promise((resolve) => {
// In practical applications, this could be a browser input field
const input = prompt;
resolve(input);
});
}
}
// Usage Examples
const assistant = new SmartChatAssistant("smart-bot-id");
await assistant.interactiveChat();
// Application of recommended questions in e-commerce customer service scenarios
class EcommerceCustomerService {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async handleCustomerQuery(
query: string
): Promise<{ answer: string; suggestions: string[] }> {
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: query,
history: [],
});
let answer = "";
for await (let text of response.textStream) {
answer += text;
}
// Retrieve related recommended questions
const recommendRes = await ai.bot.getRecommendQuestions({
botId: this.botId,
name: "E-commerce Customer Service",
introduction: "Professional E-commerce Customer Service Assistant",
agentSetting: "Specializes in answering e-commerce related questions",
msg: query,
history: [
{ role: "user", content: query },
{ role: "assistant", content: answer },
],
});
const suggestions: string[] = [];
for await (let suggestion of recommendRes.textStream) {
suggestions.push(suggestion);
}
return { answer, suggestions };
}
// Handling common e-commerce scenarios
async handleProductInquiry(productName: string) {
const query = `I want to learn about the ${productName} product`;
const result = await this.handleCustomerQuery(query);
console.log("Customer Service Answer:", result.answer);
console.log("\nYou may also want to know:");
result.suggestions.forEach((suggestion, index) => {
console.log(`${index + 1}. ${suggestion}`);
});
return result;
}
async handleOrderInquiry(orderId: string) {
const query = `Query the status of order ${orderId}`;
const result = await this.handleCustomerQuery(query);
console.log("Order Status:", result.answer);
console.log("\nRelated Services:");
result.suggestions.forEach((suggestion, index) => {
console.log(`${index + 1}. ${suggestion}`);
});
return result;
}
}
// Usage Examples
const customerService = new EcommerceCustomerService("ecommerce-bot-id");
// Handle product inquiries
await customerService.handleProductInquiry("iPhone 15");
// Handle order inquiries
await customerService.handleOrderInquiry("ORD20231226001");
createConversation
function createConversation(props: {
botId: string;
title?: string;
}): Promise<IConversation>;
Create a new conversation with the Agent to establish an independent conversation session.
- Create a new conversation session for the specified Agent
- Supports custom conversation titles for easy management and identification
- Each conversation session independently stores conversation history
- Suitable for multi-user, multi-scenario conversation management requirements
参数
Parameters for creating a conversation
返回
Created conversation information
示例
// Create a new conversation session
const conversation = await ai.bot.createConversation({
botId: "botId-xxx",
title: "Technical Consultation Conversation",
});
console.log("Conversation created successfully:", conversation.conversationId);
console.log("Conversation title:", conversation.title);
// Multi-user Conversation Management System
class ConversationManager {
private botId: string;
private userConversations: Map<string, string> = new Map(); // userId -> conversationId
constructor(botId: string) {
this.botId = botId;
}
async createUserConversation(
userId: string,
title?: string
): Promise<string> {
const conversation = await ai.bot.createConversation({
botId: this.botId,
title: title || `${userId}'s conversation`,
});
this.userConversations.set(userId, conversation.conversationId);
console.log(`Created conversation for user ${userId}: ${conversation.conversationId}`);
return conversation.conversationId;
}
async getUserConversation(userId: string): Promise<string | null> {
return this.userConversations.get(userId) || null;
}
async sendMessageToUser(userId: string, message: string): Promise<string> {
let conversationId = await this.getUserConversation(userId);
if (!conversationId) {
conversationId = await this.createUserConversation(userId);
}
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
conversationId: conversationId,
});
let reply = "";
for await (let text of response.textStream) {
reply += text;
}
return reply;
}
async getConversationHistory(userId: string) {
const conversationId = await this.getUserConversation(userId);
if (!conversationId) {
return [];
}
const conversation = await ai.bot.getConversation({
botId: this.botId,
conversationId: conversationId,
});
return conversation.history || [];
}
}
// Usage Examples
const manager = new ConversationManager("support-bot-id");
// Create a conversation for the user
await manager.createUserConversation("user123", "Technical Support Conversation");
// Send a message
const reply = await manager.sendMessageToUser("user123", "There is an issue with my account");
console.log("Assistant reply:", reply);
// Get conversation history
const history = await manager.getConversationHistory("user123");
console.log("Conversation history:", history);
// Multi-topic Conversation Management System
class MultiTopicConversationManager {
private botId: string;
private topicConversations: Map<string, string> = new Map(); // topic -> conversationId
constructor(botId: string) {
this.botId = botId;
}
async createTopicConversation(topic: string): Promise<string> {
const conversation = await ai.bot.createConversation({
botId: this.botId,
title: `Discussion on ${topic}`,
});
this.topicConversations.set(topic, conversation.conversationId);
console.log(`Creating topic conversation: ${topic} -> ${conversation.conversationId}`);
return conversation.conversationId;
}
async switchTopic(topic: string, message: string): Promise<string> {
let conversationId = this.topicConversations.get(topic);
if (!conversationId) {
conversationId = await this.createTopicConversation(topic);
}
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
conversationId: conversationId,
});
let reply = "";
for await (let text of response.textStream) {
reply += text;
}
return reply;
}
async getTopicSummary(topic: string): Promise<string> {
const conversationId = this.topicConversations.get(topic);
if (!conversationId) {
return "No conversation records available for this topic";
}
const conversation = await ai.bot.getConversation({
botId: this.botId,
conversationId: conversationId,
});
const history = conversation.history || [];
const messageCount = history.length;
const lastMessage =
history.length > 0 ? history[history.length - 1].content : "None";
return `Topic: ${topic}\nConversation count: ${messageCount}\nLast message: ${lastMessage}`;
}
listTopics(): string[] {
return Array.from(this.topicConversations.keys());
}
}
// Usage Examples
const topicManager = new MultiTopicConversationManager("multi-topic-bot-id");
// Switch conversations between different topics
const techReply = await topicManager.switchTopic(
"Technical issues",
"How to fix this bug?"
);
console.log("Technical issues reply:", techReply);
const businessReply = await topicManager.switchTopic(
"Business Cooperation",
"We would like to discuss cooperation"
);
console.log("Business Cooperation reply:", businessReply);
// Return to technical issues to continue the conversation
const techReply2 = await topicManager.switchTopic(
"Technical issues",
"Are there any other solutions?"
);
console.log("Technical issues continuation:", techReply2);
// View topic summary
console.log("All topics:", topicManager.listTopics());
const summary = await topicManager.getTopicSummary("Technical issues");
console.log("Technical issues summary:\n", summary);
getConversation
function getConversation(props: {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}): Promise<ConversationListResult>;
Retrieve the Agent's conversation list, supporting pagination and conditional filtering.
- Query all conversation records of the specified Agent.
- Support pagination to facilitate the management of large volumes of conversations.
- Filter default or all conversations.
- Suitable for scenarios such as conversation management and historical record viewing
参数
Parameters for retrieving the conversation list
返回
Conversation list query result
示例
// Get conversation list
const result = await ai.bot.getConversation({
botId: "botId-xxx",
pageSize: 10,
pageNumber: 1,
isDefault: false,
});
console.log(`Total ${result.total} conversations`);
result.conversations.forEach((conversation, index) => {
console.log(
`${index + 1}. ${conversation.title} (${conversation.messageCount} messages)`
);
});
// Conversation Management System
class ConversationManagementSystem {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async getAllConversations(): Promise<IConversation[]> {
let allConversations: IConversation[] = [];
let pageNumber = 1;
const pageSize = 50;
while (true) {
const result = await ai.bot.getConversation({
botId: this.botId,
pageSize: pageSize,
pageNumber: pageNumber,
isDefault: false,
});
allConversations = allConversations.concat(result.conversations);
if (result.conversations.length < pageSize) {
break;
}
pageNumber++;
}
return allConversations;
}
async searchConversations(keyword: string): Promise<IConversation[]> {
const allConversations = await this.getAllConversations();
return allConversations.filter((conversation) =>
conversation.title.toLowerCase().includes(keyword.toLowerCase())
);
}
async getRecentConversations(days: number = 7): Promise<IConversation[]> {
const allConversations = await this.getAllConversations();
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
return allConversations.filter((conversation) => {
const updateTime = new Date(conversation.updateTime);
return updateTime >= cutoffDate;
});
}
async getConversationStats(): Promise<{
total: number;
recent: number;
averageMessages: number;
}> {
const allConversations = await this.getAllConversations();
const recentConversations = await this.getRecentConversations(7);
const totalMessages = allConversations.reduce(
(sum, conv) => sum + conv.messageCount,
0
);
const averageMessages =
allConversations.length > 0 ? totalMessages / allConversations.length : 0;
return {
total: allConversations.length,
recent: recentConversations.length,
averageMessages: Math.round(averageMessages),
};
}
async exportConversationList(): Promise<string> {
const allConversations = await this.getAllConversations();
const csv = [
"Conversation ID,Title,Creation Time,Last Update Time,Message Count",
...allConversations.map(
(conv) =>
`${conv.conversationId},"${conv.title}",${conv.createTime},${conv.updateTime},${conv.messageCount}`
),
].join("\n");
return csv;
}
}
// Usage Examples
const manager = new ConversationManagementSystem("management-bot-id");
// Get conversation statistics
const stats = await manager.getConversationStats();
console.log(`Total conversations: ${stats.total}`);
console.log(`Conversations in the last 7 days: ${stats.recent}`);
console.log(`Average Messages: ${stats.averageMessages}`);
// Search conversations
const searchResults = await manager.searchConversations("Technology");
console.log(`Found ${searchResults.length} technical conversations`);
// Export conversation list
const csvData = await manager.exportConversationList();
console.log("Conversation list export completed");
// Batch Conversation Analysis Tool
class ConversationAnalyzer {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async analyzeConversationPatterns(): Promise<{
timeDistribution: { hour: number; count: number }[];
messageLengthStats: { avgLength: number; maxLength: number };
topicDistribution: Map<string, number>;
}> {
const allConversations = await this.getAllConversations();
// Time Distribution Analysis
const timeDistribution = Array.from({ length: 24 }, (_, hour) => ({
hour,
count: allConversations.filter((conv) => {
const hour = new Date(conv.createTime).getHours();
return hour === hour;
}).length,
}));
// Message Length Statistics
const messageLengths = allConversations.map((conv) => conv.messageCount);
const avgLength =
messageLengths.reduce((sum, len) => sum + len, 0) / messageLengths.length;
const maxLength = Math.max(...messageLengths);
// Topic Distribution Analysis (Based on Title Keywords)
const topicDistribution = new Map<string, number>();
const topics = ["Technology", "Business", "Customer Service", "Product", "Other"];
topics.forEach((topic) => {
const count = allConversations.filter((conv) =>
conv.title.includes(topic)
).length;
topicDistribution.set(topic, count);
});
return {
timeDistribution,
messageLengthStats: { avgLength, maxLength },
topicDistribution,
};
}
async getConversationTrends(): Promise<{
dailyTrends: { date: string; count: number }[];
weeklyTrends: { week: string; count: number }[];
}> {
const allConversations = await this.getAllConversations();
// Group by date
const dailyCounts = new Map<string, number>();
allConversations.forEach((conv) => {
const date = new Date(conv.createTime).toISOString().split("T")[0];
dailyCounts.set(date, (dailyCounts.get(date) || 0) + 1);
});
const dailyTrends = Array.from(dailyCounts.entries())
.map(([date, count]) => ({
date,
count,
}))
.sort((a, b) => a.date.localeCompare(b.date));
// Group by week
const weeklyCounts = new Map<string, number>();
allConversations.forEach((conv) => {
const date = new Date(conv.createTime);
const weekStart = new Date(date);
weekStart.setDate(date.getDate() - date.getDay());
const weekKey = weekStart.toISOString().split("T")[0];
weeklyCounts.set(weekKey, (weeklyCounts.get(weekKey) || 0) + 1);
});
const weeklyTrends = Array.from(weeklyCounts.entries())
.map(([week, count]) => ({
week,
count,
}))
.sort((a, b) => a.week.localeCompare(b.week));
return { dailyTrends, weeklyTrends };
}
private async getAllConversations(): Promise<IConversation[]> {
let allConversations: IConversation[] = [];
let pageNumber = 1;
const pageSize = 100;
while (true) {
const result = await ai.bot.getConversation({
botId: this.botId,
pageSize: pageSize,
pageNumber: pageNumber,
isDefault: false,
});
allConversations = allConversations.concat(result.conversations);
if (result.conversations.length < pageSize) {
break;
}
pageNumber++;
}
return allConversations;
}
}
// Usage Examples
const analyzer = new ConversationAnalyzer("analytics-bot-id");
// Analyze conversation patterns
const patterns = await analyzer.analyzeConversationPatterns();
console.log("Conversation time distribution:", patterns.timeDistribution);
console.log("Message Length Statistics:", patterns.messageLengthStats);
console.log("Topic Distribution:", patterns.topicDistribution);
// Get conversation trends
const trends = await analyzer.getConversationTrends();
console.log("Daily Trends:", trends.dailyTrends);
console.log("Weekly Trends:", trends.weeklyTrends);
deleteConversation
function deleteConversation(props: {
botId: string;
conversationId: string;
}): Promise<void>;
Delete the specified conversation session and clear the conversation history.
- Permanently delete the specified conversation session.
- Clear all message records in the conversation
- Free up storage space and optimize system performance
- Suitable for scenarios such as conversation management and data cleanup
参数
Parameters for deleting a conversation
返回
Deletion operation completed, no return value
示例
// Delete the specified conversation
await ai.bot.deleteConversation({
botId: "botId-xxx",
conversationId: "conv-123",
});
console.log("Conversation deleted successfully");
// Batch Conversation Cleanup Tool
class ConversationCleaner {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async deleteOldConversations(daysThreshold: number = 30): Promise<number> {
const allConversations = await this.getAllConversations();
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - daysThreshold);
const oldConversations = allConversations.filter((conversation) => {
const updateTime = new Date(conversation.updateTime);
return updateTime < cutoffDate;
});
console.log(
`Found ${oldConversations.length} old conversations exceeding ${daysThreshold} days.`
);
// Batch delete old conversations
let deletedCount = 0;
for (const conversation of oldConversations) {
try {
await ai.bot.deleteConversation({
botId: this.botId,
conversationId: conversation.conversationId,
});
deletedCount++;
console.log(`Deleted conversation: ${conversation.title}`);
} catch (error) {
console.error(`Failed to delete conversation: ${conversation.title}`, error);
}
}
console.log(`Successfully deleted ${deletedCount} old conversations`);
return deletedCount;
}
async deleteEmptyConversations(): Promise<number> {
const allConversations = await this.getAllConversations();
const emptyConversations = allConversations.filter(
(conversation) => conversation.messageCount === 0
);
console.log(`Found ${emptyConversations.length} empty conversations`);
let deletedCount = 0;
for (const conversation of emptyConversations) {
try {
await ai.bot.deleteConversation({
botId: this.botId,
conversationId: conversation.conversationId,
});
deletedCount++;
} catch (error) {
console.error(`Failed to delete empty conversation: ${conversation.title}`, error);
}
}
console.log(`Successfully deleted ${deletedCount} empty conversations`);
return deletedCount;
}
async deleteConversationsByTitle(keyword: string): Promise<number> {
const allConversations = await this.getAllConversations();
const matchingConversations = allConversations.filter((conversation) =>
conversation.title.toLowerCase().includes(keyword.toLowerCase())
);
console.log(
`Found ${matchingConversations.length} conversations containing "${keyword}"`
);
let deletedCount = 0;
for (const conversation of matchingConversations) {
try {
await ai.bot.deleteConversation({
botId: this.botId,
conversationId: conversation.conversationId,
});
deletedCount++;
console.log(`Deleted: ${conversation.title}`);
} catch (error) {
console.error(`Failed to delete conversation: ${conversation.title}`, error);
}
}
return deletedCount;
}
private async getAllConversations(): Promise<IConversation[]> {
let allConversations: IConversation[] = [];
let pageNumber = 1;
const pageSize = 100;
while (true) {
const result = await ai.bot.getConversation({
botId: this.botId,
pageSize: pageSize,
pageNumber: pageNumber,
isDefault: false,
});
allConversations = allConversations.concat(result.conversations);
if (result.conversations.length < pageSize) {
break;
}
pageNumber++;
}
return allConversations;
}
}
// Usage Examples
const cleaner = new ConversationCleaner("cleanup-bot-id");
// Delete conversations older than 30 days
const oldDeleted = await cleaner.deleteOldConversations(30);
console.log(`Cleaned up ${oldDeleted} old conversations`);
// Delete empty conversations
const emptyDeleted = await cleaner.deleteEmptyConversations();
console.log(`Cleaned up ${emptyDeleted} empty conversations`);
// Delete conversations containing specific keywords
const keywordDeleted = await cleaner.deleteConversationsByTitle("test1");
console.log(`Cleaned up ${keywordDeleted} test1 conversations`);
// User Data Management System
class UserDataManager {
private botId: string;
private userConversations: Map<string, string[]> = new Map(); // userId -> conversationIds
constructor(botId: string) {
this.botId = botId;
}
async deleteUserConversations(userId: string): Promise<number> {
const userConversationIds = this.userConversations.get(userId) || [];
console.log(`Deleted ${userConversationIds.length} conversations for user ${userId}`);
let deletedCount = 0;
for (const conversationId of userConversationIds) {
try {
await ai.bot.deleteConversation({
botId: this.botId,
conversationId: conversationId,
});
deletedCount++;
console.log(`Deleted conversation: ${conversationId}`);
} catch (error) {
console.error(`Failed to delete conversation: ${conversationId}`, error);
}
}
// Remove user conversation records from memory
this.userConversations.delete(userId);
console.log(`User ${userId}'s data cleanup completed, ${deletedCount} conversations deleted`);
return deletedCount;
}
async deleteAllUserData(): Promise<number> {
const allUserIds = Array.from(this.userConversations.keys());
let totalDeleted = 0;
console.log(`Start cleaning data for ${allUserIds.length} users`);
for (const userId of allUserIds) {
const deletedCount = await this.deleteUserConversations(userId);
totalDeleted += deletedCount;
}
console.log(`Cleaned up a total of ${totalDeleted} conversations`);
return totalDeleted;
}
async deleteUserDataByCondition(
condition: (userId: string, conversationIds: string[]) => boolean
): Promise<number> {
const usersToDelete: string[] = [];
for (const [userId, conversationIds] of this.userConversations.entries()) {
if (condition(userId, conversationIds)) {
usersToDelete.push(userId);
}
}
console.log(`Found ${usersToDelete.length} eligible users`);
let totalDeleted = 0;
for (const userId of usersToDelete) {
const deletedCount = await this.deleteUserConversations(userId);
totalDeleted += deletedCount;
}
return totalDeleted;
}
// Add user conversation records
addUserConversation(userId: string, conversationId: string) {
if (!this.userConversations.has(userId)) {
this.userConversations.set(userId, []);
}
this.userConversations.get(userId)!.push(conversationId);
}
// Get user conversation statistics
getUserStats(): { totalUsers: number; totalConversations: number } {
let totalConversations = 0;
for (const conversationIds of this.userConversations.values()) {
totalConversations += conversationIds.length;
}
return {
totalUsers: this.userConversations.size,
totalConversations: totalConversations,
};
}
}
// Usage Examples
const dataManager = new UserDataManager("data-management-bot-id");
// Add user conversation records
dataManager.addUserConversation("user1", "conv-123");
dataManager.addUserConversation("user1", "conv-456");
dataManager.addUserConversation("user2", "conv-789");
// View statistics
const stats = dataManager.getUserStats();
console.log(`User count: ${stats.totalUsers}, Conversation count: ${stats.totalConversations}`);
// Delete specific user data
const deletedCount = await dataManager.deleteUserConversations("user1");
console.log(`Deleted ${deletedCount} conversations for user1`);
// Conditional deletion: Delete users whose conversation count exceeds the threshold
const conditionDeleted = await dataManager.deleteUserDataByCondition(
(userId, conversationIds) => conversationIds.length > 5
);
console.log(`Deleted ${conditionDeleted} user data with conversation count exceeding 5`);
speechToText
function speechToText(props: {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}): Promise<SpeechToTextResult>;
Convert speech files to text, supporting multiple audio formats and language recognition.
- Supports multiple audio formats: MP3, WAV, AAC, etc.
- Supports multiple language recognition engines
- Can process local or remote audio files
- Suitable for scenarios such as voice assistants, meeting minutes, voice notes, etc.
参数
Speech-to-text parameters
返回
ASR result
示例
// Convert speech files to text
const result = await ai.bot.speechToText({
botId: "speech-bot-id",
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: "https://example.com/audio.mp3",
});
console.log("Recognition result:", result.text);
console.log("Confidence:", result.confidence);
console.log("Audio duration:", result.duration, "seconds");
console.log("Recognized language:", result.language);
// Voice Assistant Application
class VoiceAssistant {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async processVoiceCommand(audioUrl: string): Promise<{
text: string;
response: string;
action?: string;
}> {
// Speech to Text
const speechResult = await ai.bot.speechToText({
botId: this.botId,
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: audioUrl,
});
console.log("ASR result:", speechResult.text);
// Generate a reply based on the recognition result
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: speechResult.text,
});
let assistantReply = "";
for await (let text of response.textStream) {
assistantReply += text;
}
// Analyze whether a specific operation needs to be executed
const action = this.analyzeCommand(speechResult.text);
return {
text: speechResult.text,
response: assistantReply,
action: action,
};
}
async createVoiceNote(audioUrl: string, title?: string): Promise<string> {
const result = await ai.bot.speechToText({
botId: this.botId,
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: audioUrl,
});
const noteContent = `
Voice Notes - ${title || new Date().toLocaleString()}
Audio duration: ${result.duration} seconds
Recognition confidence: ${Math.round(result.confidence * 100)}%
Content:
${result.text}
`;
console.log("Voice Notes created successfully:");
console.log(noteContent);
return noteContent;
}
async batchTranscribe(
audioFiles: Array<{ url: string; name: string }>
): Promise<Map<string, string>> {
const results = new Map<string, string>();
console.log(`Starting batch conversion of ${audioFiles.length} audio files`);
for (const file of audioFiles) {
try {
const result = await ai.bot.speechToText({
botId: this.botId,
engSerViceType: "16k_zh",
voiceFormat: this.getFileFormat(file.url),
url: file.url,
});
results.set(file.name, result.text);
console.log(`✓ ${file.name}: Converted successfully`);
} catch (error) {
console.error(`✗ ${file.name}: Conversion failed`, error);
results.set(file.name, "Conversion failed");
}
}
console.log("Batch conversion completed");
return results;
}
private analyzeCommand(text: string): string | undefined {
const commands = {
Weather: "weather",
Time: "time",
Play: "play",
Stop: "stop",
Search: "search",
};
for (const [keyword, action] of Object.entries(commands)) {
if (text.includes(keyword)) {
return action;
}
}
return undefined;
}
private getFileFormat(url: string): string {
const extension = url.split(".").pop()?.toLowerCase();
return extension === "wav" ? "wav" : "mp3";
}
}
// Usage Examples
const voiceAssistant = new VoiceAssistant("voice-bot-id");
// Process voice commands
const commandResult = await voiceAssistant.processVoiceCommand(
"voice-command.mp3"
);
console.log("Voice command:", commandResult.text);
console.log("Assistant reply:", commandResult.response);
if (commandResult.action) {
console.log("Execute action:", commandResult.action);
}
// Create voice notes
const note = await voiceAssistant.createVoiceNote(
"meeting-recording.mp3",
"meeting minutes"
);
// Batch convert audio files
const audioFiles = [
{ url: "audio1.mp3", name: "Interview Recording 1" },
{ url: "audio2.wav", name: "Meeting Recording 2" },
{ url: "audio3.mp3", name: "Lecture Recording 3" },
];
const transcriptions = await voiceAssistant.batchTranscribe(audioFiles);
// Intelligent Meeting Minutes System
class MeetingTranscriptionSystem {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async transcribeMeeting(
audioUrl: string,
participants: string[] = []
): Promise<{
transcript: string;
summary: string;
actionItems: string[];
speakerAnalysis: Map<string, number>;
}> {
// Speech to Text
const speechResult = await ai.bot.speechToText({
botId: this.botId,
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: audioUrl,
});
console.log("Meeting recording conversion completed, duration:", speechResult.duration, "seconds");
// Generate meeting summary
const summaryPrompt = `Please generate a summary for the following meeting content, extracting key decisions and action items:\n\n${speechResult.text}`;
const summaryResponse = await ai.bot.sendMessage({
botId: this.botId,
msg: summaryPrompt,
});
let summary = "";
for await (let text of summaryResponse.textStream) {
summary += text;
}
// Extract action items
const actionPrompt = `Extract specific action items and responsible persons from the following meeting content:\n\n${speechResult.text}`;
const actionResponse = await ai.bot.sendMessage({
botId: this.botId,
msg: actionPrompt,
});
let actionText = "";
for await (let text of actionResponse.textStream) {
actionText += text;
}
const actionItems = actionText
.split("\n")
.filter((item) => item.trim().length > 0);
// Analyze speech distribution (Simple version)
const speakerAnalysis = this.analyzeSpeakerDistribution(
speechResult.text,
participants
);
return {
transcript: speechResult.text,
summary: summary,
actionItems: actionItems,
speakerAnalysis: speakerAnalysis,
};
}
async generateMeetingMinutes(
audioUrl: string,
meetingTitle: string,
participants: string[]
): Promise<string> {
const meetingData = await this.transcribeMeeting(audioUrl, participants);
const minutes = `
Meeting Minutes
========
Meeting Title: ${meetingTitle}
Meeting Duration: ${
meetingData.transcript.length > 0
? "approximately " + Math.ceil(meetingData.transcript.length / 200) + " minutes"
: "Unknown"
}
Participants: ${participants.join(", ")}
Meeting Summary
--------
${meetingData.summary}
Meeting Minutes
--------
${meetingData.transcript}
Action Items
------
${meetingData.actionItems
.map((item, index) => `${index + 1}. ${item}`)
.join("\n")}
Speech Statistics
--------
${Array.from(meetingData.speakerAnalysis.entries())
.map(([speaker, count]) => `${speaker}: ${count} utterances`)
.join("\n")}
`;
console.log("Meeting minutes generation completed");
return minutes;
}
private analyzeSpeakerDistribution(
transcript: string,
participants: string[]
): Map<string, number> {
const distribution = new Map<string, number>();
// Initialize count for each participant
participants.forEach((participant) => {
distribution.set(participant, 0);
});
// Simple analysis: Count the occurrences of participant names
participants.forEach((participant) => {
const regex = new RegExp(participant, "gi");
const count = (transcript.match(regex) || []).length;
distribution.set(participant, count);
});
return distribution;
}
}
// Usage Examples
const meetingSystem = new MeetingTranscriptionSystem("meeting-bot-id");
// Generate meeting minutes
const minutes = await meetingSystem.generateMeetingMinutes(
"meeting-recording.mp3",
"Quarterly Planning Meeting",
["Zhang San", "Li Si", "Wang Wu"]
);
console.log(minutes);
// Direct meeting transcription
const meetingData = await meetingSystem.transcribeMeeting("team-meeting.mp3");
console.log("Meeting Summary:", meetingData.summary);
console.log("Action Items:", meetingData.actionItems);
textToSpeech
function textToSpeech(props: {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;
Convert text to speech, supporting multiple voice types and timbre options.
- Supports multiple voice types and timbre options
- Adjustable parameters such as speech rate and pitch
- Support segmented conversion of long texts
- Suitable for scenarios such as voice broadcasting, audiobooks, voice assistants, etc.
参数
Text-to-speech parameters
返回
Text-to-speech result
示例
// Convert text to speech
const result = await ai.bot.textToSpeech({
botId: "tts-bot-id",
voiceType: 1, // standard female voice
text: "Hello, I am an AI voice assistant, pleased to assist you.",
});
console.log("TTS completed:");
console.log("Audio URL:", result.audioUrl);
console.log("Audio duration:", result.duration, "seconds");
console.log("File format:", result.format);
console.log("File size:", result.size, "bytes");
// Intelligent Voice Assistant
class VoiceAssistant {
private botId: string;
private voiceType: number;
constructor(botId: string, voiceType: number = 1) {
this.botId = botId;
this.voiceType = voiceType;
}
async speakText(text: string): Promise<string> {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: this.voiceType,
text: text,
});
console.log(`TTS completed: ${result.duration} seconds`);
// In practical applications, audio can be played here
this.playAudio(result.audioUrl);
return result.audioUrl;
}
async createAudioBook(
chapters: Array<{ title: string; content: string }>
): Promise<Map<string, string>> {
const audioFiles = new Map<string, string>();
console.log(`Starting production of audiobook with a total of ${chapters.length} chapters`);
for (const [index, chapter] of chapters.entries()) {
console.log(`Synthesizing Chapter ${index + 1}: ${chapter.title}`);
try {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: this.voiceType,
text: chapter.content,
});
audioFiles.set(chapter.title, result.audioUrl);
console.log(`✓ Chapter ${index + 1} synthesis completed, duration: ${result.duration} seconds`);
} catch (error) {
console.error(`✗ Chapter ${index + 1} synthesis failed`, error);
}
}
console.log("Audiobook production completed");
return audioFiles;
}
async generateInteractiveStory(
storyTitle: string,
storyContent: string[]
): Promise<{
audioFiles: string[];
totalDuration: number;
}> {
const audioFiles: string[] = [];
let totalDuration = 0;
console.log(`Generating interactive story: ${storyTitle}`);
for (const [index, paragraph] of storyContent.entries()) {
console.log(`Synthesizing Segment ${index + 1}`);
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: this.voiceType,
text: paragraph,
});
audioFiles.push(result.audioUrl);
totalDuration += result.duration;
console.log(`✓ Segment ${index + 1} synthesis completed, duration: ${result.duration} seconds`);
}
console.log(`Story generation completed, total duration: ${totalDuration} seconds`);
return { audioFiles, totalDuration };
}
async createVoiceAnnouncement(
message: string,
repeat: number = 1
): Promise<string[]> {
const announcements: string[] = [];
console.log(`Generate voice broadcast, repeat ${repeat} times`);
for (let i = 0; i < repeat; i++) {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: this.voiceType,
text: message,
});
announcements.push(result.audioUrl);
console.log(`✓ ${i + 1}th broadcast generation completed`);
}
return announcements;
}
private playAudio(audioUrl: string): void {
// In practical applications, audio playback logic can be implemented here
console.log("Playing audio:", audioUrl);
// Example: Play using HTML5 Audio API
// const audio = new Audio(audioUrl);
// audio.play();
}
// Set voice type
setVoiceType(voiceType: number): void {
this.voiceType = voiceType;
console.log(`Voice type has been set to: ${voiceType}`);
}
// Get supported voice types
getSupportedVoices(): Array<{ id: number; name: string }> {
return [
{ id: 1, name: "Standard Female Voice" },
{ id: 2, name: "Standard Male Voice" },
{ id: 3, name: "Emotional Female Voice" },
{ id: 4, name: "Emotional Male Voice" },
{ id: 5, name: "Child Voice" },
{ id: 6, name: "Elderly Voice" },
];
}
}
// Usage Examples
const voiceAssistant = new VoiceAssistant("voice-bot-id", 1);
// Simple Voice Broadcast
const audioUrl = await voiceAssistant.speakText("Welcome to the Voice Assistant System!");
// Create Audiobook
const chapters = [
{ title: "Chapter 1", content: "This is the beginning of a story..." },
{ title: "Chapter 2", content: "The story continues to develop..." },
{ title: "Chapter 3", content: "the climax part of the story..." },
];
const audioBook = await voiceAssistant.createAudioBook(chapters);
// Generate Interactive Story
const storyContent = [
"Once upon a time there was a small village...",
"In the village lived a wise old man...",
"The old man taught the villagers a lot of knowledge...",
];
const story = await voiceAssistant.generateInteractiveStory(
"Wise Old Man",
storyContent
);
// Create voice broadcast
const announcements = await voiceAssistant.createVoiceAnnouncement(
"Please note that the system is about to undergo maintenance.",
3
);
// Multilingual TTS System
class MultiLanguageVoiceSystem {
private botId: string;
private languageVoices: Map<string, number> = new Map();
constructor(botId: string) {
this.botId = botId;
this.initializeLanguageVoices();
}
private initializeLanguageVoices(): void {
this.languageVoices.set("zh-CN", 1); // Chinese - standard female voice
this.languageVoices.set("en-US", 2); // English - standard male voice
this.languageVoices.set("ja-JP", 3); // Japanese - emotional female voice
this.languageVoices.set("ko-KR", 4); // Korean - emotional male voice
this.languageVoices.set("fr-FR", 1); // French - standard female voice
this.languageVoices.set("de-DE", 2); // German - standard male voice
}
async synthesizeMultilingualText(
texts: Map<string, string>
): Promise<Map<string, string>> {
const results = new Map<string, string>();
console.log(`Start synthesizing multilingual texts, total ${texts.size} languages`);
for (const [language, text] of texts.entries()) {
const voiceType = this.languageVoices.get(language) || 1;
console.log(`Synthesizing ${language} text`);
try {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: voiceType,
text: text,
});
results.set(language, result.audioUrl);
console.log(`✓ ${language} synthesis completed, duration: ${result.duration} seconds`);
} catch (error) {
console.error(`✗ ${language} synthesis failed`, error);
results.set(language, "Synthesis failed");
}
}
return results;
}
async createInternationalAnnouncement(
message: string
): Promise<Map<string, string>> {
const translations = await this.translateMessage(message);
return this.synthesizeMultilingualText(translations);
}
async generateLanguageLearningAudio(
language: string,
phrases: string[]
): Promise<Array<{ phrase: string; audioUrl: string }>> {
const learningAudios: Array<{ phrase: string; audioUrl: string }> = [];
const voiceType = this.languageVoices.get(language) || 1;
console.log(`Generating ${language} learning audio, total ${phrases.length} phrases`);
for (const phrase of phrases) {
try {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: voiceType,
text: phrase,
});
learningAudios.push({
phrase: phrase,
audioUrl: result.audioUrl,
});
console.log(`✓ Phrase "${phrase}" audio generation completed`);
} catch (error) {
console.error(`✗ Phrase "${phrase}" audio generation failed`, error);
}
}
return learningAudios;
}
async createTourGuideAudio(
language: string,
attractions: Array<{ name: string; description: string }>
): Promise<Map<string, string>> {
const guideAudios = new Map<string, string>();
const voiceType = this.languageVoices.get(language) || 1;
console.log(
`Generating ${language} language tour audio, total ${attractions.length} attractions`
);
for (const attraction of attractions) {
const description = `${attraction.name}:${attraction.description}`;
try {
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: voiceType,
text: description,
});
guideAudios.set(attraction.name, result.audioUrl);
console.log(`✓ Attraction "${attraction.name}" tour audio generation completed`);
} catch (error) {
console.error(`✗ Attraction "${attraction.name}" tour audio generation failed`, error);
}
}
return guideAudios;
}
private async translateMessage(
message: string
): Promise<Map<string, string>> {
// In practical applications, here we can call the translation API
const translations = new Map<string, string>();
translations.set("zh-CN", message); // Chinese
translations.set("en-US", "Hello, this is an announcement."); // English
translations.set("ja-JP", "こんにちは,これはアナウンスです."); // Japanese
translations.set("ko-KR", "안녕하세요, 공지사항입니다."); // Korean
return translations;
}
// Add new language support
addLanguageSupport(languageCode: string, voiceType: number): void {
this.languageVoices.set(languageCode, voiceType);
console.log(`Add language support: ${languageCode} -> voice type ${voiceType}`);
}
// Get the list of supported languages
getSupportedLanguages(): string[] {
return Array.from(this.languageVoices.keys());
}
}
// Usage Examples
const multiLangSystem = new MultiLanguageVoiceSystem("multilang-bot-id");
// Multilingual Text Synthesis
const multilingualTexts = new Map([
["zh-CN", "Welcome to the multilingual voice system"],
["en-US", "Welcome to the multilingual voice system"],
["ja-JP", "Welcome to the multilingual voice system"],
]);
const audioResults = await multiLangSystem.synthesizeMultilingualText(
multilingualTexts
);
// International Announcement Broadcast
const announcements = await multiLangSystem.createInternationalAnnouncement(
"System Maintenance Notice
);
// Language Learning Audio
const englishPhrases = ["Hello", "How are you?", "Thank you", "Goodbye"];
const learningAudios = await multiLangSystem.generateLanguageLearningAudio(
"en-US",
englishPhrases
);
// Tour Guide Audio
const attractions = [
{ name: "The Forbidden City", description: "Ancient Chinese palace complex, UNESCO World Heritage site" },
{ name: "The Great Wall", description: "Ancient Chinese military defense project, world wonder" },
];
const guideAudios = await multiLangSystem.createTourGuideAudio(
"zh-CN",
attractions
);
getTextToSpeechResult
function getTextToSpeechResult(props: {
botId: string;
taskId: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;
Get the result of a text-to-speech task, query the completion status of the TTS task and the generated audio file.
- Query the status and result of asynchronous TTS tasks
- Retrieve generated audio file information
- Supports preview mode and production mode
- Suitable for asynchronous scenarios such as batch TTS and long-text processing
参数
Parameters for retrieving TTS results
返回
TTS task result
示例
// Query TTS task result
const result = await ai.bot.getTextToSpeechResult({
botId: "tts-bot-id",
taskId: "task-123456",
});
console.log("Task Status:", result.status);
console.log("Task Progress:", result.progress, "%");
if (result.status === "completed") {
console.log("Audio URL:", result.audioUrl);
console.log("Audio duration:", result.duration, "seconds");
console.log("File format:", result.format);
console.log("File size:", result.size, "bytes");
} else if (result.status === "processing") {
console.log("The task is still being processed, please try again later.");
} else if (result.status === "failed") {
console.log("Task processing failed");
}
// Batch TTS Task Monitoring System
class BatchTTSMonitor {
private botId: string;
private taskQueue: Map<string, { text: string; createTime: Date }> =
new Map();
constructor(botId: string) {
this.botId = botId;
}
// Add task to queue
addTask(taskId: string, text: string): void {
this.taskQueue.set(taskId, {
text: text,
createTime: new Date(),
});
console.log(`Task ${taskId} has been added to the queue`);
}
// Monitor task progress
async monitorTaskProgress(
taskId: string,
maxWaitTime: number = 300
): Promise<TextToSpeechResult> {
const startTime = Date.now();
const checkInterval = 5000; // Check every 5 seconds
console.log(`Starting progress monitoring for task ${taskId}`);
while (true) {
const result = await ai.bot.getTextToSpeechResult({
botId: this.botId,
taskId: taskId,
});
console.log(
`Task ${taskId} Status: ${result.status}, Progress: ${result.progress}%`
);
if (result.status === "completed") {
console.log(`Task ${taskId} completed, audio duration: ${result.duration} seconds`);
this.taskQueue.delete(taskId);
return result;
}
if (result.status === "failed") {
console.error(`Task ${taskId} processing failed`);
this.taskQueue.delete(taskId);
throw new Error(`TTS task failed: ${taskId}`);
}
// Check for timeout
if (Date.now() - startTime > maxWaitTime * 1000) {
console.error(`Task ${taskId} monitoring timed out`);
throw new Error(`TTS task timeout: ${taskId}`);
}
// Wait for a while and then retry
await this.sleep(checkInterval);
}
}
// Batch monitor all tasks
async monitorAllTasks(): Promise<Map<string, TextToSpeechResult>> {
const results = new Map<string, TextToSpeechResult>();
const taskIds = Array.from(this.taskQueue.keys());
console.log(`Start monitoring ${taskIds.length} tasks`);
for (const taskId of taskIds) {
try {
const result = await this.monitorTaskProgress(taskId);
results.set(taskId, result);
} catch (error) {
console.error(`Task ${taskId} monitoring failed:`, error);
}
}
console.log(`Task monitoring completed, succeeded: ${results.size}/${taskIds.length}`);
return results;
}
// Get task statistics
getTaskStats(): {
total: number;
completed: number;
processing: number;
failed: number;
} {
const stats = {
total: this.taskQueue.size,
completed: 0,
processing: 0,
failed: 0,
};
// In practical applications, this can perform statistics based on task status
return stats;
}
// Clean up completed tasks
cleanupCompletedTasks(): void {
const completedTaskIds: string[] = [];
// In practical applications, this can clean up records of completed tasks.
console.log(`Cleaned up ${completedTaskIds.length} completed tasks`);
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}
// Usage Examples
const monitor = new BatchTTSMonitor("batch-tts-bot-id");
// Add task to monitoring queue
monitor.addTask("task-001", "This is the first TTS task");
monitor.addTask("task-002", "This is the second TTS task");
monitor.addTask("task-003", "This is the third TTS task");
// Monitor a single task
const result = await monitor.monitorTaskProgress("task-001");
console.log("Task result:", result.audioUrl);
// Batch monitor all tasks
const allResults = await monitor.monitorAllTasks();
// View task statistics
const stats = monitor.getTaskStats();
console.log("Task Statistics:", stats);
// Long Text TTS System
class LongTextTTSProcessor {
private botId: string;
private voiceType: number;
constructor(botId: string, voiceType: number = 1) {
this.botId = botId;
this.voiceType = voiceType;
}
// Segment and process long texts
async processLongText(
longText: string,
maxSegmentLength: number = 500
): Promise<string[]> {
const segments = this.splitText(longText, maxSegmentLength);
const audioUrls: string[] = [];
console.log(`Start processing long text, total ${segments.length} segments`);
for (const [index, segment] of segments.entries()) {
console.log(`Processing segment ${index + 1}/${segments.length}`);
try {
// Submit TTS task
const result = await ai.bot.textToSpeech({
botId: this.botId,
voiceType: this.voiceType,
text: segment,
});
// Wait for task completion
const finalResult = await this.waitForTaskCompletion(result.taskId);
audioUrls.push(finalResult.audioUrl);
console.log(
`✓ Segment ${index + 1} processed, duration: ${finalResult.duration} seconds`
);
} catch (error) {
console.error(`✗ Segment ${index + 1} processing failed`, error);
}
}
console.log(`Long text processing completed, ${audioUrls.length} audio files generated`);
return audioUrls;
}
// Wait for task completion
private async waitForTaskCompletion(
taskId: string,
maxWaitTime: number = 600
): Promise<TextToSpeechResult> {
const startTime = Date.now();
const checkInterval = 3000; // Check every 3 seconds
while (true) {
const result = await ai.bot.getTextToSpeechResult({
botId: this.botId,
taskId: taskId,
});
if (result.status === "completed") {
return result;
}
if (result.status === "failed") {
throw new Error(`TTS task failed: ${taskId}`);
}
// Check for timeout
if (Date.now() - startTime > maxWaitTime * 1000) {
throw new Error(`TTS task timeout: ${taskId}`);
}
console.log(`Task ${taskId} progress: ${result.progress}%`);
await this.sleep(checkInterval);
}
}
// Text segmentation
private splitText(text: string, maxLength: number): string[] {
const segments: string[] = [];
let currentSegment = "";
const sentences = text.split(/[。!?.!?]/);
for (const sentence of sentences) {
const trimmedSentence = sentence.trim();
if (!trimmedSentence) continue;
if (currentSegment.length + trimmedSentence.length + 1 <= maxLength) {
currentSegment += (currentSegment ? "." : "") + trimmedSentence;
} else {
if (currentSegment) {
segments.push(currentSegment + ".");
}
currentSegment = trimmedSentence;
}
}
if (currentSegment) {
segments.push(currentSegment + ".");
}
return segments;
}
// Merge audio files (specialized audio processing tools are required in practical applications)
async mergeAudioFiles(audioUrls: string[]): Promise<string> {
console.log(`Starting to merge ${audioUrls.length} audio files`);
// In practical applications, the audio merging API can be called here
// Here returns the first audio URL as an example
return audioUrls[0];
}
// Generate Audiobook
async createAudioBook(
bookTitle: string,
chapters: string[]
): Promise<Map<string, string[]>> {
const bookAudios = new Map<string, string[]>();
console.log(`Starting production of audiobook: ${bookTitle}, total ${chapters.length} chapters`);
for (const [index, chapter] of chapters.entries()) {
console.log(`Processing chapter ${index + 1}`);
const chapterAudios = await this.processLongText(chapter);
bookAudios.set(`Chapter ${index + 1}`, chapterAudios);
console.log(
`✓ Chapter ${index + 1} processed, generated ${chapterAudios.length} audio segments`
);
}
console.log(`Audiobook production completed: ${bookTitle}`);
return bookAudios;
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}
// Usage Examples
const longTextProcessor = new LongTextTTSProcessor("longtext-bot-id", 1);
// Process long text
const longText = "This is a long text content that needs to be split into multiple paragraphs for TTS...";
const audioSegments = await longTextProcessor.processLongText(longText);
// Create Audiobook
const bookChapters = [
"Chapter 1: The Beginning of the Story...",
"Chapter 2: Plot Development...",
"Chapter 3: The Climax Part...",
"Chapter 4: Conclusion...",
];
const audioBook = await longTextProcessor.createAudioBook(
"My Story",
bookChapters
);
// Merge audio files
const mergedAudio = await longTextProcessor.mergeAudioFiles(audioSegments);
console.log("Merged audio:", mergedAudio);
Complete Type Definitions
IBotCreateConversation
interface IBotCreateConversation {
botId: string;
title?: string;
}
IBotGetConversation
interface IBotGetConversation {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}
IBotDeleteConversation
interface IBotDeleteConversation {
botId: string;
conversationId: string;
}
IBotSpeechToText
interface IBotSpeechToText {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}
IBotTextToSpeech
interface IBotTextToSpeech {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}
IBotGetTextToSpeechResult
interface IBotGetTextToSpeechResult {
botId: string;
taskId: string;
isPreview?: boolean;
}
BaseChatModelInput
interface BaseChatModelInput {
model: string;
messages: Array<ChatModelMessage>;
temperature?: number;
topP?: number;
tools?: Array<FunctionTool>;
toolChoice?: "none" | "auto" | "custom";
maxSteps?: number;
onStepFinish?: (prop: IOnStepFinish) => unknown;
}
| BaseChatModelInput Property Name | Type | Description |
|---|---|---|
| model | string | Model name. |
| messages | Array<ChatModelMessage> | Message list. |
| temperature | number | Sampling temperature, which controls the randomness of the output. |
| topP | number | Temperature sampling, where the model considers tokens within the top_p probability mass. |
| tools | Array<FunctionTool> | List of tools available for large models. |
| toolChoice | string | Specifies the method for large models to select tools. |
| maxSteps | number | Maximum number of requests to the large model. |
| onStepFinish | (prop: IOnStepFinish) => unknown | The callback function triggered when a request to the large model is completed. |
BotInfo
interface BotInfo {
botId: string;
name: string;
introduction: string;
agentSetting: string;
welcomeMessage: string;
avatar: string;
background: string;
tags: Array<string>;
isNeedRecommend: boolean;
knowledgeBase: Array<string>;
type: string;
initQuestions: Array<string>;
enable: true;
}
IUserFeedback
interface IUserFeedback {
recordId: string;
type: string;
botId: string;
comment: string;
rating: number;
tags: Array<string>;
input: string;
aiAnswer: string;
}
ChatModelMessage
type ChatModelMessage =
| UserMessage
| SystemMessage
| AssistantMessage
| ToolMessage;
UserMessage
type UserMessage = {
role: "user";
content: string;
};
SystemMessage
type SystemMessage = {
role: "system";
content: string;
};
AssistantMessage
type AssistantMessage = {
role: "assistant";
content?: string;
tool_calls?: Array<ToolCall>;
};
ToolMessage
type ToolMessage = {
role: "tool";
tool_call_id: string;
content: string;
};
ToolCall
export type ToolCall = {
id: string;
type: string;
function: { name: string; arguments: string };
};
FunctionTool
Tool definition type.
type FunctionTool = {
name: string;
description: string;
fn: CallableFunction;
parameters: object;
};
| FunctionTool Property Name | Type | Description |
|---|---|---|
| name | string | Tool name. |
| description | string | Description of the tool. A clear tool description helps the large model understand the tool's purpose. |
| fn | CallableFunction | The tool's execution function. When the AI SDK parses that the large model's response requires calling this tool, it invokes this function and returns the result to the large model. |
| parameters | object | The input parameters for the tool's execution function, which must be defined using JSON Schema format. |
IOnStepFinish
The type of input parameters for the callback function triggered after the large model responds.
interface IOnStepFinish {
messages: Array<ChatModelMessage>;
text?: string;
toolCall?: ToolCall;
toolResult?: unknown;
finishReason?: string;
stepUsage?: Usage;
totalUsage?: Usage;
}
| IOnStepFinish Property Name | Type | Description |
|---|---|---|
| messages | Array<ChatModelMessage> | List of all messages up to the current step. |
| text | string | The text of the current response. |
| toolCall | ToolCall | The tool called by the current response. |
| toolResult | unknown | The corresponding tool call result. |
| finishReason | string | Reason for termination of large model inference. |
| stepUsage | Usage | The tokens consumed by the current step. |
| totalUsage | Usage | The total tokens consumed up to the current step. |
Usage
type Usage = {
completion_tokens: number;
prompt_tokens: number;
total_tokens: number;
};
IConversation
Agent session.
interface IConversation {
id: string;
envId: string;
ownerUin: string;
userId: string;
conversationId: string;
title: string;
startTime: string; // date-time format
createTime: string;
updateTime: string;
}