Bỏ qua, đến nội dung

Tham chiếu API — AI

Tổng quan Resources

Plugin AI đăng ký các resource sau thông qua app.resourceManager:

ResourceMô tảQuyền
llmServicesQuản lý LLM providersAdmin (pm.ai.llm-services)
aiBotsQuản lý AI botsAdmin (pm.ai.ai-bots)
aiConversationsPhiên hội thoạiNgười dùng đã đăng nhập
aiMessagesTin nhắn trong phiênThông qua conversation
aiToolsĐịnh nghĩa AI toolsAdmin (quản lý), loggedIn (xem)
aiSkillsRAG skills / knowledge connectorsAdmin (quản lý), loggedIn (xem)
aiSettingsCấu hình AI toàn cụcAdmin (pm.ai.ai-settings)
aiMcpClientsKết nối MCP serversAdmin (pm.ai.mcp-settings)
aiContextDatasourcesNguồn dữ liệu ngữ cảnhloggedIn (xem)
aiFilesFiles đính kèm trong chatloggedIn (tạo)
aiResource tổng hợp (listModels, v.v.)Tuỳ action

LLM Services

Tạo LLM service

typescript
await agent.resource('llmServices').create({
  values: {
    name: 'openai-main',
    provider: 'openai',
    apiKey: 'sk-...',
    baseUrl: 'https://api.openai.com/v1',
    defaultModel: 'gpt-4o',
  },
});

Lấy danh sách providers

typescript
const response = await agent.resource('llmServices').list();
// response.body.data = [{ id, name, provider, defaultModel, ... }]

Liệt kê tất cả model khả dụng

typescript
const response = await agent.resource('ai').listAllEnabledModels();
// Trả về danh sách model từ tất cả provider đã kích hoạt

AI Bots

Tạo bot

typescript
await agent.resource('aiBots').create({
  values: {
    name: 'Support Bot',
    username: 'support-bot',
    systemPrompt: 'Bạn là trợ lý hỗ trợ khách hàng chuyên nghiệp...',
    llmServiceId: 1,
    model: 'gpt-4o',
  },
});

Cập nhật bot

typescript
await agent.resource('aiBots').update({
  filterByTk: botId,
  values: {
    systemPrompt: 'System prompt mới...',
    model: 'gpt-4o-mini',
  },
});

Lấy danh sách bot theo user

typescript
// Chỉ trả về bots mà user hiện tại có quyền truy cập (theo role)
const response = await agent.resource('aiBots').listByUser();

Cập nhật prompt cá nhân

typescript
// User có thể tuỳ chỉnh prompt riêng cho bot
await agent.resource('aiBots').updateUserPrompt({
  filterByTk: botUsername,
  values: { userPrompt: 'Trả lời bằng tiếng Việt...' },
});

Conversations & Messages

Tạo conversation

typescript
const conv = await agent.resource('aiConversations').create({
  values: { botId: 1 },
});
const conversationId = conv.body.data.id;

Gửi message

typescript
await agent.resource('aiMessages').create({
  values: {
    conversationId: conversationId,
    content: 'Làm sao để đặt hàng?',
    role: 'user',
  },
});

Lấy lịch sử chat

typescript
const messages = await agent.resource('aiMessages').list({
  filter: { conversationId: conversationId },
  sort: ['createdAt'],
});

Huỷ cuộc hội thoại đang xử lý

Plugin hỗ trợ huỷ qua sync message:

typescript
// Server-side: gửi sync message để abort conversation
plugin.handleSyncMessage({
  type: 'aiBots:abortConversation',
  payload: { sessionId: 'xxx' },
});

MCP Clients

Thêm MCP server

typescript
await agent.resource('aiMcpClients').create({
  values: {
    name: 'My MCP Server',
    url: 'http://localhost:3001',
    description: 'Server cung cấp tools bổ sung',
  },
});

AI Settings

Lấy cấu hình công khai

typescript
// Endpoint cho user đã đăng nhập (không cần admin)
const settings = await agent.resource('aiSettings').publicGet();

Database Collections

CollectionMô tảGhi chú
llmServicesCấu hình LLM provider (API key, base URL, model)Dữ liệu nhạy cảm
aiBotsĐịnh nghĩa bot (system prompt, model, tools)Liên kết M2M với roles
aiConversationsPhiên chatThuộc về user
aiMessagesTin nhắn trong phiênThuộc về conversation
aiToolsĐịnh nghĩa AI toolsĐăng ký động hoặc tĩnh
aiSkillsRAG skills, datasource connectorsGán cho bot
aiSettingsCấu hình AI toàn cụcMột bản ghi duy nhất
aiMcpClientsMCP server connectionsCung cấp tools động
aiContextDatasourceNguồn dữ liệu ngữ cảnhCho work context
aiFilesFiles đính kèm trong chatLưu qua file-manager
aiToolMessagesLog các lần gọi toolDebugging
lc-checkpointsLangChain checkpoints (agent state)Tự dọn sau 48h
lc-checkpoint-blobsBlob data cho checkpoints
lc-checkpoint-writesGhi checkpoint
users-ai-botsLiên kết user ↔ bot (M2M)Phân quyền
roles (mở rộng)Thêm field allowNewAiBotDefault true

Giới hạn kỹ thuật

Tham sốGiá trịGhi chú
LLM response timeout60 giây (mặc định)Tuỳ thuộc provider
File đính kèmTối đa 10MBCấu hình qua aiSettings
Checkpoint retention48 giờCron job dọn lúc 2:00 AM
Rate limitTheo LLM providerKhông giới hạn phía plugin