dsh-brain/lib/cloudsync.js

48 lines
1.8 KiB
JavaScript

/**
* Cloud-sync bundle — the remote side of the memory feature.
*
* This module owns the concrete transport: one cloud client per connection,
* with the package settings sidecar as the fallback for any field the
* connection left empty. The memory bundle consumes the `MemoryRemote`
* contract and never sees this file — the composition root injects the
* factory, so swapping the transport never touches the tools or the
* Mnemon provider.
*/
import { CloudClient } from "./api.js";
import { SettingsStore } from "./store.js";
/** Build a factory over live settings closures (the composition root's path). */
export function createCloudMemoryRemoteFactory(deps) {
let shared;
return {
current: () => {
shared ??= new CloudClient({
baseUrl: () => deps.settings().baseUrl,
token: () => deps.memoryToken(),
timeoutMs: () => deps.settings().requestTimeoutMs,
});
return shared;
},
forConnection: (connection) => new CloudClient({
baseUrl: () => connection.endpoint ?? deps.settings().baseUrl,
token: () => connection.token ?? deps.memoryToken(),
timeoutMs: () => connection.timeoutMs ?? deps.settings().requestTimeoutMs,
}),
};
}
/**
* The standalone Mnemon-provider path: the same factory, backed lazily by
* the settings sidecar so a provider constructed before any configuration
* still resolves against live settings at call time.
*/
export function createSidecarMemoryRemoteFactory() {
let store;
const ensure = () => {
store ??= new SettingsStore({});
return store;
};
return createCloudMemoryRemoteFactory({
settings: () => ensure().settings(),
memoryToken: () => ensure().memoryToken(),
});
}