0.0.0-alpha.4 • Published 5 months ago

@aichatkit/storage-adapter v0.0.0-alpha.4

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

@aichatkit/storage-adapter

Base storage adapter abstract class for Hypermode ChatKit.

Installation

npm install @aichatkit/storage-adapter

Usage

This package provides the base abstract class for implementing storage adapters. You typically won't use this package directly, but rather create or use an implementation like @aichatkit/localstorage-adapter.

import { StorageAdapter } from '@aichatkit/storage-adapter';
import { Conversation, Message } from '@aichatkit/types';

// Example: Create a custom adapter implementation
class CustomStorageAdapter extends StorageAdapter {
  private conversations: Conversation[] = [];

  async saveConversation(conversation: Conversation): Promise<void> {
    const index = this.conversations.findIndex((c) => c.id === conversation.id);
    if (index >= 0) {
      this.conversations[index] = conversation;
    } else {
      this.conversations.push(conversation);
    }
  }

  async getConversation(id: string): Promise<Conversation | null> {
    return this.conversations.find((c) => c.id === id) || null;
  }

  async getAllConversations(): Promise<Conversation[]> {
    return this.conversations;
  }

  async deleteConversation(id: string): Promise<boolean> {
    const initialLength = this.conversations.length;
    this.conversations = this.conversations.filter((c) => c.id !== id);
    return initialLength !== this.conversations.length;
  }

  async addMessage(conversationId: string, message: Message): Promise<Conversation | null> {
    const conversation = await this.getConversation(conversationId);
    if (!conversation) return null;

    conversation.messages.push(message);
    await this.saveConversation(conversation);
    return conversation;
  }
}

License

MIT © Hypermode