@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
49 lines • 2.09 kB
JavaScript
import { stallTimeout } from "../core/api/client.js";
import { ApiError } from "../core/api/errors.js";
import { md5Hex } from "../utils/hash.js";
import { resolveMem0Config } from "../vfs/mem0/config.js";
import { Accessor } from "./base.js";
const ENCODER = new TextEncoder();
export class Mem0Error extends ApiError {
constructor(status) {
super(`Mem0 request failed with status ${String(status)}`, status);
this.name = 'Mem0Error';
}
}
export class Mem0Accessor extends Accessor {
config;
constructor(config) {
super();
this.config = resolveMem0Config(config);
}
async request(method, endpoint, options = {}) {
const url = new URL(this.config.host + endpoint);
for (const [name, value] of Object.entries(options.params ?? {})) {
url.searchParams.set(name, String(value));
}
// Python drives mem0 through the official SDK; this is a hand-rolled
// fetch client, so it has to send what the SDK sends. `Mem0-User-ID` is
// the SDK's own client identifier, md5 of the API key
// (`mem0/client/main.py`: `hashlib.md5(self.api_key.encode()).hexdigest()`).
// It is an identifier the server expects, not a credential, so do not
// "fix" the hash away.
const headers = {
Authorization: `Token ${this.config.apiKey}`,
'Mem0-User-ID': md5Hex(ENCODER.encode(this.config.apiKey)),
};
const init = { method, headers };
if (options.json !== undefined) {
headers['Content-Type'] = 'application/json';
init.body = JSON.stringify(options.json);
}
const response = await stallTimeout(this.config.timeout)(url, init);
if (!response.ok)
throw new Mem0Error(response.status);
const payload = await response.json();
if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) {
throw new Error('Mem0 response must be a JSON object');
}
return payload;
}
}
//# sourceMappingURL=mem0.js.map