@aichatkit/storage-adapter
Version:
Base storage adapter for Hypermode ChatKit
111 lines (109 loc) • 3.65 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
StorageAdapter: () => StorageAdapter
});
module.exports = __toCommonJS(index_exports);
var StorageAdapter = class {
/**
* Set network callbacks for syncing with backend
* @param callbacks Network adapter callbacks
*/
setNetworkCallbacks(callbacks) {
this.callbacks = callbacks;
}
/**
* Adds a message to a conversation (convenience method)
* @param conversationId ID of the conversation
* @param message Message to add
* @returns Promise that resolves with the updated conversation or null if not found
*/
async addMessage(conversationId, message) {
const messageItem = {
id: message.id,
type: "message",
content: message.content,
role: message.role,
timestamp: message.timestamp
};
return this.addItem(conversationId, messageItem);
}
/**
* Get conversation history (messages only) - syncs with backend if callback available
* @param conversationId ID of the conversation
* @returns Promise with array of messages
*/
async getConversationHistory(conversationId) {
const items = await this.getConversationItems(conversationId);
return items.filter((item) => item.type === "message").map((item) => {
const msgItem = item;
return {
id: msgItem.id,
content: msgItem.content,
role: msgItem.role,
timestamp: msgItem.timestamp
};
});
}
/**
* Sync all conversations with backend to ensure consistency
* @returns Promise that resolves when sync is complete
*/
async syncAllConversationsWithBackend() {
var _a;
if (!((_a = this.callbacks) == null ? void 0 : _a.getConversationItems)) {
return Promise.resolve();
}
try {
const conversations = await this.getAllConversations();
for (const conversation of conversations) {
const agentId = await this.getConversationAgent(conversation.id);
if (agentId) {
try {
const backendItems = await this.callbacks.getConversationItems(agentId);
conversation.items = backendItems;
await this.saveConversation(conversation);
} catch (error) {
console.error(
`Failed to sync conversation ${conversation.id}:`,
error
);
}
}
}
} catch (error) {
console.error("Failed to sync conversations with backend:", error);
}
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) {
return Promise.resolve();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
StorageAdapter
});