Files
makelore/electron/coding-plugins/default-installation.ts

47 lines
2.3 KiB
TypeScript

import type { AccountBinding, MarketplaceLibrarySnapshot } from './account-plugin-cache';
import { MarketplaceClientError, type MarketplaceClient } from './marketplace-client';
import { CHILD_FRIENDLY_REPLIES_PLUGIN_ID, CHILD_FRIENDLY_REPLIES_BUNDLED_RELEASE_ID } from '../../shared/coding-plugins';
import { isMakeLoreVersionCompatible } from './release-descriptor';
export const DEFAULT_CHILD_REPLIES_PLUGIN_ID = CHILD_FRIENDLY_REPLIES_PLUGIN_ID;
/** This code-owned Skill ships with the client; only its account acquisition is remote. */
export async function installDefaultChildReplies(options: {
marketplace: MarketplaceClient;
clientVersion: string;
binding: AccountBinding | null;
library: MarketplaceLibrarySnapshot;
onInstalled(): Promise<void>;
}): Promise<MarketplaceLibrarySnapshot> {
let library = options.library;
const binding = options.binding;
if (!binding || library.stale) return library;
const assertAccount = () => {
const current = options.marketplace.getCurrentAccountBinding();
if (current?.accountKey !== binding.accountKey || current?.epoch !== binding.epoch) {
throw new MarketplaceClientError('marketplace_account_changed', 409, 'Marketplace account changed');
}
};
const pluginId = DEFAULT_CHILD_REPLIES_PLUGIN_ID;
const entry = library.items.find((item) => item.pluginId === pluginId);
if (entry) return library; // Includes the account's durable removal receipt.
try {
assertAccount();
const detail = await options.marketplace.readDetail(pluginId);
assertAccount();
const release = detail.stableRelease;
if (detail.stale || detail.runtimeStatus !== 'enabled' || detail.acquisition !== 'free'
|| release?.deliveryKind !== 'bundled' || release.releaseId !== CHILD_FRIENDLY_REPLIES_BUNDLED_RELEASE_ID
|| !isMakeLoreVersionCompatible(options.clientVersion, release.minMakeloreVersion, release.maxMakeloreVersion)) return library;
library = await options.marketplace.acquire(pluginId);
assertAccount();
await options.onInstalled();
} catch (error) {
assertAccount();
if (error instanceof MarketplaceClientError && error.code === 'marketplace_account_changed') throw error;
// An unavailable default must not hide the rest of the Library. A later
// session/list refresh retries; no local success or removal receipt is invented.
}
return library;
}