@aichatkit/storage-adapter
Version:
Base storage adapter for Hypermode ChatKit
120 lines (104 loc) • 3.83 kB
text/typescript
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Conversation, Message } from '@aichatkit/types'
export interface StorageAdapterCallbacks {
/**
* Callback to get conversation history from network
* @param agentId The ID of the agent
* @returns Promise with array of messages
*/
getConversationHistory?: (agentId: string) => Promise<Message[]>
/**
* Callback to clear conversation history on network
* @param agentId The ID of the agent
* @returns Promise that resolves when cleared
*/
clearConversationHistory?: (agentId: string) => Promise<void>
}
export abstract class StorageAdapter {
protected callbacks?: StorageAdapterCallbacks
/**
* Set network callbacks for syncing with backend
* @param callbacks Network adapter callbacks
*/
setNetworkCallbacks(callbacks: StorageAdapterCallbacks): void {
this.callbacks = callbacks
}
/**
* Saves a conversation to storage
* @param conversation Conversation to save
* @returns Promise that resolves when the operation is complete
*/
abstract saveConversation(conversation: Conversation): Promise<void>
/**
* Retrieves a conversation from storage by ID
* @param id ID of the conversation to retrieve
* @returns Promise that resolves with the conversation or null if not found
*/
abstract getConversation(id: string): Promise<Conversation | null>
/**
* Retrieves all conversations from storage
* @returns Promise that resolves with an array of conversations
*/
abstract getAllConversations(): Promise<Conversation[]>
/**
* Deletes a conversation from storage
* @param id ID of the conversation to delete
* @returns Promise that resolves with a boolean indicating success
*/
abstract deleteConversation(id: string): Promise<boolean>
/**
* Adds a message to a conversation
* @param conversationId ID of the conversation
* @param message Message to add
* @returns Promise that resolves with the updated conversation or null if not found
*/
abstract addMessage(
conversationId: string,
message: Message,
): Promise<Conversation | null>
/**
* Get conversation history - syncs with backend if callback available
* @param conversationId ID of the conversation
* @returns Promise with array of messages
*/
abstract getConversationHistory(conversationId: string): Promise<Message[]>
/**
* Clear conversation history - syncs with backend if callback available
* @param conversationId ID of the conversation
* @returns Promise that resolves when history is cleared
*/
abstract clearConversationHistory(conversationId: string): Promise<void>
/**
* Store agent ID for a conversation
* @param conversationId ID of the conversation
* @param agentId ID of the agent
* @returns Promise that resolves when stored
*/
abstract setConversationAgent(
conversationId: string,
agentId: string,
): Promise<void>
/**
* Get agent ID for a conversation
* @param conversationId ID of the conversation
* @returns Promise with agent ID or null if not found
*/
abstract getConversationAgent(conversationId: string): Promise<string | null>
/**
* Sync all conversations with backend to ensure consistency
* This should be called on app initialization to get latest state
* @returns Promise that resolves when sync is complete
*/
async syncAllConversationsWithBackend?(): Promise<void> {
// Optional method - implementations can override this
return Promise.resolve()
}
/**
* Optional method to initialize the adapter
* @param config Optional configuration object
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async initialize(config?: Record<string, any>): Promise<void> {
return Promise.resolve()
}
}