contextual-agent-sdk
Version:
SDK for building AI agents with seamless voice-text context switching
133 lines • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.conversationManager = exports.ConversationManager = void 0;
const uuid_1 = require("uuid");
class ConversationManager {
conversations = new Map();
createConversation(systemPrompt) {
const conversation = {
id: (0, uuid_1.v4)(),
messages: [],
systemPrompt,
createdAt: new Date(),
updatedAt: new Date(),
metadata: {}
};
if (systemPrompt) {
conversation.messages.push({
id: (0, uuid_1.v4)(),
role: 'system',
content: systemPrompt,
timestamp: new Date()
});
}
this.conversations.set(conversation.id, conversation);
return conversation;
}
getConversation(conversationId) {
return this.conversations.get(conversationId);
}
addMessage(conversationId, message) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
const fullMessage = {
...message,
id: (0, uuid_1.v4)(),
timestamp: new Date()
};
conversation.messages.push(fullMessage);
conversation.updatedAt = new Date();
return fullMessage;
}
addToolCalls(conversationId, toolCalls) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
const toolCallMessage = {
id: (0, uuid_1.v4)(),
role: 'assistant',
content: '',
timestamp: new Date()
};
toolCallMessage.metadata = { toolCalls };
conversation.messages.push(toolCallMessage);
conversation.updatedAt = new Date();
}
addToolResults(conversationId, toolResults) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
for (const { callId, result } of toolResults) {
const toolMessage = {
id: (0, uuid_1.v4)(),
role: 'user',
content: JSON.stringify(result),
timestamp: new Date(),
metadata: {
isToolResult: true,
toolCallId: callId,
toolResult: result
}
};
conversation.messages.push(toolMessage);
}
conversation.updatedAt = new Date();
}
getConversationHistory(conversationId, includeSystem = true) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
if (includeSystem) {
return [...conversation.messages];
}
return conversation.messages.filter(msg => msg.role !== 'system');
}
clearConversation(conversationId) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
const systemMessage = conversation.messages.find(msg => msg.role === 'system');
conversation.messages = systemMessage ? [systemMessage] : [];
conversation.updatedAt = new Date();
}
deleteConversation(conversationId) {
this.conversations.delete(conversationId);
}
getAllConversationIds() {
return Array.from(this.conversations.keys());
}
getConversationStats(conversationId) {
const conversation = this.conversations.get(conversationId);
if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}
const stats = {
messageCount: conversation.messages.length,
userMessages: 0,
assistantMessages: 0,
toolCalls: 0,
duration: Date.now() - conversation.createdAt.getTime()
};
for (const message of conversation.messages) {
if (message.role === 'user') {
stats.userMessages++;
}
else if (message.role === 'assistant') {
stats.assistantMessages++;
if (message.metadata?.toolCalls) {
stats.toolCalls += message.metadata.toolCalls.length;
}
}
}
return stats;
}
}
exports.ConversationManager = ConversationManager;
exports.conversationManager = new ConversationManager();
//# sourceMappingURL=ConversationManager.js.map