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 = {
storagePrefix?: string;
storage?: Storage;
storage?: {
prefix?: string;
customInstance?: Storage;
};
maxSize?: number;
};
@ -42,14 +44,15 @@ export class LocalHistory {
* - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH.
*/
public constructor(opts: LocalHistoryOptions = {}) {
const { storagePrefix, storage, maxSize } = opts;
const { storage, maxSize } = opts;
const { prefix, customInstance } = storage ?? {};
this.maxSize = maxSize ?? DEFAULT_MAX_LENGTH;
if (storage) {
this.storage = storage;
log.info("Using explicit storage");
} else if (storagePrefix) {
this.storage = new Storage(storagePrefix);
log.info("Creating storage for prefix", storagePrefix);
if (customInstance) {
this.storage = customInstance;
log.info("Using custom storage instance", { customInstance });
} else if (prefix) {
this.storage = new Storage(prefix);
log.info("Creating storage with prefix", { prefix });
} else {
this.storage = undefined;
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 {
messageId: entry.messageId,
retrievalHint: entry.retrievalHint
@ -68,7 +68,9 @@ export class MessageSerializer {
};
}
public static deserializeCausalEntry(entry: StoredCausalEntry): HistoryEntry {
private static deserializeCausalEntry(
entry: StoredCausalEntry
): HistoryEntry {
return {
messageId: entry.messageId,
retrievalHint: entry.retrievalHint