histoire
Version:
A fast story book powered by Vite
68 lines (67 loc) • 3.33 kB
JavaScript
import { createAgentBinaryCache } from './binary-cache.js';
import { createAgentDiscovery } from './discovery.js';
import { createAgentRegistryIcons } from './registry-icons.js';
import { createAgentRegistry } from './registry.js';
/**
* Manager-scoped registry preparation and discovery retain independent process ownership.
* @param options Manager options.
* @param sanitize Secret redaction.
* @param announced Receives commands from fresh discovery probes.
*/
export function createAgentServices(options, sanitize, announced) {
const registry = createAgentRegistry(options.registry);
const binaries = createAgentBinaryCache(registry.cacheDir);
const icons = createAgentRegistryIcons({ cacheDir: registry.cacheDir, fetch: options.registry?.fetch, iconUrl: registry.iconUrl });
const discovery = createAgentDiscovery({ root: options.root, timeout: options.handshakeTimeoutMs ?? 10_000, sanitize, commandsWait: options.commandsWaitMs, announced });
let controller = new AbortController();
/** Resolve managed distribution without exposing archive URLs to browser. */
async function prepare(preset, environment) {
const signal = controller.signal;
if (!preset.registryId)
return { preset, environment };
const distribution = await abortablePreparation(registry.distribution(preset.registryId), signal);
signal.throwIfAborted();
const command = distribution.kind === 'binary' ? await binaries.prepare(preset.registryId, distribution, signal) : distribution.command;
signal.throwIfAborted();
return { preset: { ...preset, command, args: distribution.args }, environment: { ...distribution.env, ...environment } };
}
return {
/** A forced catalog refresh invalidates capabilities tied to previous adapter versions. */
async registry(force = false) {
const result = await registry.load(force);
if (force)
discovery.retire();
return result;
},
prepare,
/** Same-origin SVG logo of a registry agent. */
icon: icons.get,
/** Coalesced probes use exact configured command/environment/model identity. */
discover: (preset, environment, force = false) => discovery.discover(preset, environment, () => prepare(preset, environment), force),
/** Abort preparing launches synchronously before preferences enter control lane. */
retire() {
controller.abort(new Error('Agent runtime closed'));
controller = new AbortController();
discovery.retire();
},
/** No temporary agent survives context shutdown. */
async close() {
controller.abort(new Error('Agent runtime closed'));
await discovery.close();
},
};
}
/** Stop awaiting shared catalog fetch immediately when this preparation loses authority. */
async function abortablePreparation(operation, signal) {
signal.throwIfAborted();
let abort;
try {
return await Promise.race([operation, new Promise((_, reject) => {
abort = () => reject(signal.reason);
signal.addEventListener('abort', abort, { once: true });
})]);
}
finally {
signal.removeEventListener('abort', abort);
}
}