refactor: cleaner API for storage in LocalHistoryOptions

This commit is contained in:
Danish Arora 2025-12-10 15:17:18 -05:00
parent 8a749c453a
commit e396c3b755
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 16 additions and 11 deletions

View File

@ -21,8 +21,10 @@ export const DEFAULT_MAX_LENGTH = 10_000;
*/ */
export type LocalHistoryOptions = { export type LocalHistoryOptions = {
storagePrefix?: string; storage?: {
storage?: Storage; prefix?: string;
customInstance?: Storage;
};
maxSize?: number; maxSize?: number;
}; };
@ -42,14 +44,15 @@ export class LocalHistory {
* - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH. * - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH.
*/ */
public constructor(opts: LocalHistoryOptions = {}) { public constructor(opts: LocalHistoryOptions = {}) {
const { storagePrefix, storage, maxSize } = opts; const { storage, maxSize } = opts;
const { prefix, customInstance } = storage ?? {};
this.maxSize = maxSize ?? DEFAULT_MAX_LENGTH; this.maxSize = maxSize ?? DEFAULT_MAX_LENGTH;
if (storage) { if (customInstance) {
this.storage = storage; this.storage = customInstance;
log.info("Using explicit storage"); log.info("Using custom storage instance", { customInstance });
} else if (storagePrefix) { } else if (prefix) {
this.storage = new Storage(storagePrefix); this.storage = new Storage(prefix);
log.info("Creating storage for prefix", storagePrefix); log.info("Creating storage with prefix", { prefix });
} else { } else {
this.storage = undefined; this.storage = undefined;
log.info("Using in-memory storage"); log.info("Using in-memory storage");

View File

@ -59,7 +59,7 @@ export class MessageSerializer {
} }
} }
public static serializeCausalEntry(entry: HistoryEntry): StoredCausalEntry { private static serializeCausalEntry(entry: HistoryEntry): StoredCausalEntry {
return { return {
messageId: entry.messageId, messageId: entry.messageId,
retrievalHint: entry.retrievalHint retrievalHint: entry.retrievalHint
@ -68,7 +68,9 @@ export class MessageSerializer {
}; };
} }
public static deserializeCausalEntry(entry: StoredCausalEntry): HistoryEntry { private static deserializeCausalEntry(
entry: StoredCausalEntry
): HistoryEntry {
return { return {
messageId: entry.messageId, messageId: entry.messageId,
retrievalHint: entry.retrievalHint retrievalHint: entry.retrievalHint