import { untrackAction, renameAction } from '../../lib/actions' import { parseUrl } from '../../lib/util' import { t } from '../../lib/i18n' import * as settings from '../../settings' import type { InstallationRecord } from '../../installations' import type { SourcePlugin, FieldOption, ActionResult, ActionTools, LaunchCommand } from '../../types/sources' export interface UrlSourceConfig { id: string labelKey: string descKey: string category: 'remote' | 'cloud' defaultUrl: string transformUrl?: (url: string) => string editableUrl?: boolean includeUntrack?: boolean } export function createUrlSource(config: UrlSourceConfig): SourcePlugin { const { id, labelKey, descKey, category, defaultUrl, transformUrl, editableUrl = false, includeUntrack = false } = config return { id, get label() { return t(labelKey) }, get description() { return t(descKey) }, category, hasConsole: false, skipInstall: true, get fields() { return [ { id: 'url', label: t('remote.comfyuiUrl'), type: 'text' as const, defaultValue: defaultUrl } ] }, getDefaults() { return { launchMode: 'window', browserPartition: 'shared' } }, buildInstallation( selections: Record ): Record { const url = selections.url?.value || defaultUrl const parsed = parseUrl(url) return { ...(category === 'remote' ? { version: 'remote' } : {}), remoteUrl: parsed ? parsed.href : url, launchMode: 'window', browserPartition: 'shared' } }, getListPreview(installation: InstallationRecord): string | null { return (installation.remoteUrl as string) || null }, getLaunchCommand(installation: InstallationRecord): LaunchCommand | null { const rawUrl = (installation.remoteUrl as string) || defaultUrl const finalUrl = transformUrl ? transformUrl(rawUrl) : rawUrl const parsed = parseUrl(finalUrl) if (!parsed) return null return { remote: true, url: parsed.href, host: parsed.hostname, port: parsed.port } }, getListActions(installation: InstallationRecord): Record[] { return [ { id: 'launch', label: t('actions.connect'), style: 'primary', enabled: installation.status === 'installed', showProgress: true, progressTitle: t('actions.connecting'), cancellable: true } ] }, getDetailSections(installation: InstallationRecord): Record[] { const urlField: Record = editableUrl ? { id: 'remoteUrl', label: t('remote.url'), value: (installation.remoteUrl as string) || '—', editable: true, requiresRestart: true } : { label: t('remote.url'), value: (installation.remoteUrl as string) || '—' } const actions: Record[] = [ { id: 'launch', label: t('actions.connect'), style: 'primary', enabled: installation.status === 'installed', showProgress: true, progressTitle: t('actions.connecting'), cancellable: true } ] if (category !== 'cloud') { actions.push(renameAction(installation.name)) } if (includeUntrack) { actions.push(untrackAction()) } return [ { tab: 'status', title: t('remote.connectionInfo'), fields: [ { label: t('common.installMethod'), value: installation.sourceLabel as string }, urlField, { label: t('remote.added'), value: new Date(installation.createdAt).toLocaleDateString() } ] }, { tab: 'settings', title: t('common.launchSettings'), fields: [ { id: 'browserPartition', label: t('common.browserPartition'), value: (installation.browserPartition as string) || 'shared', editable: true, editType: 'select', options: [ { value: 'shared', label: t('common.partitionShared') }, { value: 'unique', label: t('common.partitionUnique') } ], tooltip: t('tooltips.browserPartition') }, { id: 'autoDownloadOutputs', label: t('common.autoDownloadOutputs'), value: (installation.autoDownloadOutputs as boolean | undefined) ?? true, editable: true, editType: 'boolean', refreshSection: true, tooltip: t('tooltips.autoDownloadOutputs') }, ...((installation.autoDownloadOutputs as boolean | undefined) !== false ? [ { id: 'useSharedOutputDir', label: t('common.useSharedOutputDir'), value: (installation.useSharedOutputDir as boolean | undefined) ?? true, editable: true, editType: 'boolean', refreshSection: true, nested: true, tooltip: t('tooltips.useSharedOutputDir') }, ...((installation.useSharedOutputDir as boolean | undefined) === false ? [ { id: 'outputDir', label: t('media.outputDir'), value: (installation.outputDir as string | undefined) || settings.defaults.outputDir, editable: true, editType: 'path', browseOnly: true, nested: true, tooltip: t('tooltips.outputDirPerInstall') } ] : []) ] : []) ] }, { title: 'Actions', pinBottom: true, actions } ] }, probeInstallation(_dirPath: string): Record | null { return null }, async handleAction( actionId: string, _installation: InstallationRecord, _actionData: Record | undefined, _tools: ActionTools ): Promise { return { ok: false, message: `Action "${actionId}" not yet implemented.` } } } }