|
| 1 | +import { isRecord } from '@agent-device/kernel/record'; |
| 2 | + |
| 3 | +export type PluginSource = 'builtin' | 'store' | 'embedder'; |
| 4 | + |
| 5 | +export type PluginInventoryError = Readonly<{ |
| 6 | + code: string; |
| 7 | + message: string; |
| 8 | + reason?: string; |
| 9 | + hint?: string; |
| 10 | +}>; |
| 11 | + |
| 12 | +export type PluginInventoryCredentials = Readonly<{ |
| 13 | + variables: readonly string[]; |
| 14 | + byBackend: Readonly<Record<string, readonly string[]>>; |
| 15 | +}>; |
| 16 | + |
| 17 | +type PluginInventoryIdentity = Readonly<{ |
| 18 | + provider?: string; |
| 19 | + source: PluginSource; |
| 20 | + package?: string; |
| 21 | + version?: string; |
| 22 | + credentials: PluginInventoryCredentials; |
| 23 | +}>; |
| 24 | + |
| 25 | +/** One plugin as the daemon sees it: published in `daemon.json` and shown by `daemon status`. */ |
| 26 | +export type PluginInventoryEntry = |
| 27 | + | (PluginInventoryIdentity & Readonly<{ state: 'active'; error?: undefined }>) |
| 28 | + | (PluginInventoryIdentity & Readonly<{ state: 'failed'; error: PluginInventoryError }>); |
| 29 | + |
| 30 | +/** One step of a plugin's cleanup: it completed, or it threw this typed error. */ |
| 31 | +export type PluginCleanupStep = |
| 32 | + | Readonly<{ status: 'ok' }> |
| 33 | + | Readonly<{ status: 'failed'; error: PluginInventoryError }>; |
| 34 | + |
| 35 | +/** |
| 36 | + * What closing the registry did for one registered plugin. A plugin without `dispose` has nothing to |
| 37 | + * dispose, which reads as `ok`. |
| 38 | + */ |
| 39 | +export type PluginCleanupOutcome = Readonly<{ |
| 40 | + provider: string; |
| 41 | + source: PluginSource; |
| 42 | + shutdown: PluginCleanupStep; |
| 43 | + dispose: PluginCleanupStep; |
| 44 | +}>; |
| 45 | + |
| 46 | +export const NO_PLUGIN_CREDENTIALS: PluginInventoryCredentials = Object.freeze({ |
| 47 | + variables: Object.freeze([]), |
| 48 | + byBackend: Object.freeze({}), |
| 49 | +}); |
| 50 | + |
| 51 | +/** |
| 52 | + * The credential variable names a lease on `leaseBackend` reads for `provider`: the backend's own |
| 53 | + * list when the plugin declares one, otherwise its flat list. Undefined when no active plugin |
| 54 | + * claims the provider. |
| 55 | + */ |
| 56 | +export function pluginCredentialVariables( |
| 57 | + inventory: readonly PluginInventoryEntry[], |
| 58 | + provider: string, |
| 59 | + leaseBackend?: string, |
| 60 | +): readonly string[] | undefined { |
| 61 | + const entry = inventory.find((candidate) => candidate.provider === provider); |
| 62 | + if (entry?.state !== 'active') return undefined; |
| 63 | + const { variables, byBackend } = entry.credentials; |
| 64 | + return ( |
| 65 | + (leaseBackend !== undefined && Object.hasOwn(byBackend, leaseBackend) |
| 66 | + ? byBackend[leaseBackend] |
| 67 | + : undefined) ?? variables |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +/** Reads the inventory a daemon published, dropping anything that is not a well-formed entry. */ |
| 72 | +export function readPluginInventory(value: unknown): readonly PluginInventoryEntry[] { |
| 73 | + return Array.isArray(value) ? value.flatMap((entry) => readEntry(entry) ?? []) : []; |
| 74 | +} |
| 75 | + |
| 76 | +function readEntry(value: unknown): PluginInventoryEntry | undefined { |
| 77 | + if (!isRecord(value) || !isSource(value.source)) return undefined; |
| 78 | + const credentials = readCredentials(value.credentials); |
| 79 | + if (!credentials) return undefined; |
| 80 | + const identity = { |
| 81 | + ...(typeof value.provider === 'string' ? { provider: value.provider } : {}), |
| 82 | + source: value.source, |
| 83 | + ...(typeof value.package === 'string' ? { package: value.package } : {}), |
| 84 | + ...(typeof value.version === 'string' ? { version: value.version } : {}), |
| 85 | + credentials, |
| 86 | + }; |
| 87 | + if (value.state === 'active') return { ...identity, state: 'active' }; |
| 88 | + const error = readError(value.error); |
| 89 | + return value.state === 'failed' && error ? { ...identity, state: 'failed', error } : undefined; |
| 90 | +} |
| 91 | + |
| 92 | +function readCredentials(value: unknown): PluginInventoryCredentials | undefined { |
| 93 | + if (!isRecord(value)) return undefined; |
| 94 | + const variables = readNames(value.variables); |
| 95 | + const byBackend = value.byBackend === undefined ? {} : value.byBackend; |
| 96 | + if (!variables || !isRecord(byBackend)) return undefined; |
| 97 | + const entries = Object.entries(byBackend).map(([backend, names]) => [backend, readNames(names)]); |
| 98 | + if (entries.some(([, names]) => names === undefined)) return undefined; |
| 99 | + return { |
| 100 | + variables, |
| 101 | + byBackend: Object.fromEntries(entries) as Record<string, readonly string[]>, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +function isSource(value: unknown): value is PluginSource { |
| 106 | + return value === 'builtin' || value === 'store' || value === 'embedder'; |
| 107 | +} |
| 108 | + |
| 109 | +function readNames(value: unknown): readonly string[] | undefined { |
| 110 | + return Array.isArray(value) && value.every((name) => typeof name === 'string') |
| 111 | + ? value |
| 112 | + : undefined; |
| 113 | +} |
| 114 | + |
| 115 | +function readError(value: unknown): PluginInventoryError | undefined { |
| 116 | + if (!isRecord(value) || typeof value.code !== 'string' || typeof value.message !== 'string') { |
| 117 | + return undefined; |
| 118 | + } |
| 119 | + return { |
| 120 | + code: value.code, |
| 121 | + message: value.message, |
| 122 | + ...(typeof value.reason === 'string' ? { reason: value.reason } : {}), |
| 123 | + ...(typeof value.hint === 'string' ? { hint: value.hint } : {}), |
| 124 | + }; |
| 125 | +} |
0 commit comments