@gsb-core/ai-assistant
Version:
GSB AI Assistant Core Package
153 lines • 6.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GsbAiChatService = void 0;
/**
* GSB AI Chat Service
*
* Provides functionality for interacting with AI chat models through the GSB backend.
* This service integrates with the 'aiChat' serverless function to process AI chat requests.
*
* Key features:
* - Template-based prompt generation using nunjucks (handled by backend)
* - Multi-provider support (OpenAI, Azure, Anthropic, HuggingFace)
* - Chat history management
* - Context-aware conversations
*/
const core_1 = require("@gsb-core/core");
/**
* AI Chat Service
*
* This service acts as a client for the GSB backend AI chat functionality.
* It communicates with the 'aiChat' serverless function to process templates,
* manage chat history, and generate AI responses.
*/
class GsbAiChatService {
/**
* Get the singleton instance of the AI Chat Service
*
* @param token Authentication token for API calls
* @returns The singleton instance
*/
static getInstance(useCache = true) {
if (!GsbAiChatService.instance) {
GsbAiChatService.instance = new GsbAiChatService(useCache);
}
return GsbAiChatService.instance;
}
/**
* Constructor
*
* @param token Authentication token for API calls
*/
constructor(useCache = true) {
this.entityService = core_1.GsbEntityService.getInstance(useCache);
}
/**
* Send a message to the AI chat and get a response
*
* This method calls the 'aiChat' serverless function which:
* 1. Gets or creates a chat session
* 2. Processes the template using nunjucks (if configured)
* 3. Stores the user message
* 4. Generates an AI response using the configured LLM provider
* 5. Stores the AI response in the chat history
* 6. Returns the AI response
* @param prompt User's message/prompt
* @param llmConfId LLM configuration ID (from a saved LlmConfiguration entity)
* @param chatId Existing chat ID (if continuing a conversation) or undefined for a new chat
* @param data Context data containing the entity,entityDefinition,entity_id and other information for template processing, matches GsbWorkflowInstance
* @param options Additional options like testMode, system prompts, etc. matches GsbWorkflowInstance.prms
* @returns Response object containing the AI message and updated chat
*
* @example
* ```typescript
* // Start a new chat
* const result = await aiChatService.chat(
* "What's the status of our project?",
* "llm-config-id", // ID of saved LlmConfiguration
* undefined, // New chat
* { entity: projectEntity, entityDefinition: projectEntityDefinition, entity_id: projectEntityId }
* );
*
* // Continue the conversation
* const followUp = await aiChatService.chat(
* "What should be our next steps?",
* "llm-config-id",
* result.chat.id, // Use existing chat ID
* { entity: projectEntity, entityDefinition: projectEntityDefinition, entity_id: projectEntityId }
* );
* ```
*/
async chat({ prompt, llmConfId, chatId, data, options }, token, tenantCode) {
try {
// Call the serverless function with all necessary parameters
const response = await this.entityService.runWfFunction({
function: {
name: 'aiChat',
},
instance: {
...data,
prms: {
prompt,
llmConfId,
chatId,
testMode: (options === null || options === void 0 ? void 0 : options.testMode) || false,
...options
},
}
}, token, tenantCode);
// Handle errors from the serverless function
if (!response || response.error) {
throw new Error((response === null || response === void 0 ? void 0 : response.error) || 'Unknown error in AI chat function');
}
const respData = response.response;
// Return the message and chat information
return {
message: respData.message,
chat: respData.chat || { id: respData.chatId }
};
}
catch (error) {
console.error('Error in GsbAiChatService.chat:', error);
throw error;
}
}
/**
* Get a chat by ID
*
* @param chatId The chat ID
* @returns The chat entity
*/
async getChat(chatId, token, tenantCode) {
const req = new core_1.QueryParams("GsbAiChat").include(p => p.messages).self.pickEntity(chatId).type(core_1.QueryType.Full);
const response = await this.entityService.get(req, token, tenantCode);
return response;
}
/**
* Get all chats
*
* @returns Array of chat entities
*/
async getChats(queryParams, token, tenantCode) {
let req = new core_1.QueryParams("GsbAiChat").select(p => p.id).select(p => p.title).select(p => p.createDate);
if (queryParams)
Object.assign(req, queryParams);
const response = await this.entityService.query(req, token, tenantCode);
return response.entities;
}
/**
* Get messages for a specific chat
*
* @param chatId The chat ID
* @returns Array of message entities
*/
async getChatMessages(chatId, queryParams, token, tenantCode) {
let req = new core_1.QueryParams("GsbAiMessage").select(p => p.id).select(p => p.content).select(p => p.createDate);
if (queryParams)
Object.assign(req, queryParams);
const response = await this.entityService.query(req, token, tenantCode);
return response.entities;
}
}
exports.GsbAiChatService = GsbAiChatService;
//# sourceMappingURL=gsb-ai-chat.service.js.map