Storage
How Trayn persists memories and how to use custom storage backends.
StorageAdapter Interface
interface StorageAdapter {
putJson(key: string, data: unknown): Promise<void>;
getJson<T>(key: string): Promise<T | null>;
putBinary(key: string, data: Buffer | Uint8Array, contentType: string): Promise<void>;
getBinary(key: string): Promise<Buffer | null>;
listPrefix(prefix: string): Promise<string[]>;
deleteKey(key: string): Promise<void>;
deletePrefix(prefix: string): Promise<void>;
}Default
The SDK ships with a cloud storage adapter that the harness configures automatically using your Trayn account credentials. No additional infrastructure keys are required.
Custom Storage Example
import type { StorageAdapter } from "@trayn/memory";
import { setStorage } from "@trayn/memory";
class FileSystemStorage implements StorageAdapter {
private baseDir: string;
constructor(baseDir: string) {
this.baseDir = baseDir;
}
async putJson(key: string, data: unknown): Promise<void> {
const path = `${this.baseDir}/${key}`;
await fs.mkdir(dirname(path), { recursive: true });
await fs.writeFile(path, JSON.stringify(data, null, 2));
}
async getJson<T>(key: string): Promise<T | null> {
const path = `${this.baseDir}/${key}`;
try {
const content = await fs.readFile(path, "utf8");
return JSON.parse(content) as T;
} catch {
return null;
}
}
// ... implement remaining methods
}
setStorage(new FileSystemStorage("./training-data"));Dependency Injection
import { setStorage, getStorage } from "@trayn/memory";
// Set a custom storage backend
setStorage(myStorage);
// Use storage
const data = await getStorage().getJson("my-key");getStorage() falls back to the built-in cloud storage adapter if no custom storage is configured. You only need setStorage() when replacing the default backend.