import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; export const CHATGPT_MARKETPLACE_NAME = "personal"; export const CHATGPT_PLUGIN_NAME = "pony"; export const CHATGPT_PERSONAL_MARKETPLACE_PATH = path.join( ".agents", "plugins", "marketplace.json", ); interface MarketplacePlugin { name: string; [key: string]: unknown; } interface PersonalMarketplace { name: string; interface?: Record; plugins: MarketplacePlugin[]; [key: string]: unknown; } export interface ChatGptSetupResult { changed: boolean; marketplaceName: string; marketplacePath: string; pluginId: string; version: string; } function isObject(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function parseMarketplace(contents: string, marketplacePath: string) { let value: unknown; try { value = JSON.parse(contents); } catch { throw new Error( `ChatGPT marketplace at ${marketplacePath} contains invalid JSON. Repair it before running setup again.`, ); } if ( !isObject(value) || typeof value.name !== "string" || !value.name.trim() || !Array.isArray(value.plugins) || (value.interface !== undefined && !isObject(value.interface)) ) { throw new Error( `ChatGPT marketplace at ${marketplacePath} has an unsupported structure. Pony left it unchanged.`, ); } if ( !value.plugins.every( (plugin) => isObject(plugin) && typeof plugin.name === "string" && plugin.name.trim().length > 0, ) ) { throw new Error( `ChatGPT marketplace at ${marketplacePath} contains an invalid plugin entry. Pony left it unchanged.`, ); } return value as PersonalMarketplace; } function ponyMarketplaceEntry(version: string): MarketplacePlugin { return { name: CHATGPT_PLUGIN_NAME, source: { source: "npm", package: "@whitespace/pony", version, registry: "https://registry.npmjs.org", }, policy: { installation: "AVAILABLE", authentication: "ON_INSTALL", }, category: "Productivity", }; } async function readMarketplace( marketplacePath: string, ): Promise { try { return parseMarketplace( await readFile(marketplacePath, "utf8"), marketplacePath, ); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; return { name: CHATGPT_MARKETPLACE_NAME, interface: { displayName: "Personal" }, plugins: [], }; } } async function writeMarketplace( marketplacePath: string, marketplace: PersonalMarketplace, ): Promise { await mkdir(path.dirname(marketplacePath), { recursive: true }); const temporaryPath = `${marketplacePath}.${process.pid}.tmp`; const contents = `${JSON.stringify(marketplace, null, 2)}\n`; try { await writeFile(temporaryPath, contents, "utf8"); try { await rename(temporaryPath, marketplacePath); } catch (error) { const code = (error as NodeJS.ErrnoException).code; if (code !== "EEXIST" && code !== "EPERM") throw error; // Windows may reject replacing an existing destination with rename. await writeFile(marketplacePath, contents, "utf8"); await rm(temporaryPath, { force: true }); } } catch (error) { await rm(temporaryPath, { force: true }); throw error; } } /** * Makes Pony discoverable through ChatGPT's supported personal marketplace. * Installation remains an explicit user action in ChatGPT's Plugins UI. */ export async function setupChatGptPlugin( version: string, marketplacePath: string, ): Promise { const marketplace = await readMarketplace(marketplacePath); const entry = ponyMarketplaceEntry(version); const ponyIndexes = marketplace.plugins.flatMap((plugin, index) => plugin.name === CHATGPT_PLUGIN_NAME ? [index] : [], ); if (ponyIndexes.length > 1) { throw new Error( `ChatGPT marketplace at ${marketplacePath} contains multiple Pony entries. Pony left it unchanged.`, ); } const existingIndex = ponyIndexes[0]; const changed = existingIndex === undefined || JSON.stringify(marketplace.plugins[existingIndex]) !== JSON.stringify(entry); if (changed) { if (existingIndex === undefined) { marketplace.plugins.push(entry); } else { marketplace.plugins[existingIndex] = entry; } await writeMarketplace(marketplacePath, marketplace); } return { changed, marketplaceName: marketplace.name, marketplacePath, pluginId: `${CHATGPT_PLUGIN_NAME}@${marketplace.name}`, version, }; }