|
| 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 | +export const NO_PLUGIN_CREDENTIALS: PluginInventoryCredentials = Object.freeze({ |
| 31 | + variables: Object.freeze([]), |
| 32 | + byBackend: Object.freeze({}), |
| 33 | +}); |
| 34 | + |
| 35 | +/** |
| 36 | + * The credential variable names a lease on `leaseBackend` reads for `provider`: the backend's own |
| 37 | + * list when the plugin declares one, otherwise its flat list. Undefined when no active plugin |
| 38 | + * claims the provider. |
| 39 | + */ |
| 40 | +export function pluginCredentialVariables( |
| 41 | + inventory: readonly PluginInventoryEntry[], |
| 42 | + provider: string, |
| 43 | + leaseBackend?: string, |
| 44 | +): readonly string[] | undefined { |
| 45 | + const entry = inventory.find((candidate) => candidate.provider === provider); |
| 46 | + if (entry?.state !== 'active') return undefined; |
| 47 | + const { variables, byBackend } = entry.credentials; |
| 48 | + return ( |
| 49 | + (leaseBackend !== undefined && Object.hasOwn(byBackend, leaseBackend) |
| 50 | + ? byBackend[leaseBackend] |
| 51 | + : undefined) ?? variables |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +/** Reads the inventory a daemon published, dropping anything that is not a well-formed entry. */ |
| 56 | +export function readPluginInventory(value: unknown): readonly PluginInventoryEntry[] { |
| 57 | + return Array.isArray(value) ? value.flatMap((entry) => readEntry(entry) ?? []) : []; |
| 58 | +} |
| 59 | + |
| 60 | +function readEntry(value: unknown): PluginInventoryEntry | undefined { |
| 61 | + if (!isRecord(value) || !isSource(value.source)) return undefined; |
| 62 | + const credentials = readCredentials(value.credentials); |
| 63 | + if (!credentials) return undefined; |
| 64 | + const identity = { |
| 65 | + ...(typeof value.provider === 'string' ? { provider: value.provider } : {}), |
| 66 | + source: value.source, |
| 67 | + ...(typeof value.package === 'string' ? { package: value.package } : {}), |
| 68 | + ...(typeof value.version === 'string' ? { version: value.version } : {}), |
| 69 | + credentials, |
| 70 | + }; |
| 71 | + if (value.state === 'active') return { ...identity, state: 'active' }; |
| 72 | + const error = readError(value.error); |
| 73 | + return value.state === 'failed' && error ? { ...identity, state: 'failed', error } : undefined; |
| 74 | +} |
| 75 | + |
| 76 | +function readCredentials(value: unknown): PluginInventoryCredentials | undefined { |
| 77 | + if (!isRecord(value)) return undefined; |
| 78 | + const variables = readNames(value.variables); |
| 79 | + const byBackend = value.byBackend === undefined ? {} : value.byBackend; |
| 80 | + if (!variables || !isRecord(byBackend)) return undefined; |
| 81 | + const entries = Object.entries(byBackend).map(([backend, names]) => [backend, readNames(names)]); |
| 82 | + if (entries.some(([, names]) => names === undefined)) return undefined; |
| 83 | + return { |
| 84 | + variables, |
| 85 | + byBackend: Object.fromEntries(entries) as Record<string, readonly string[]>, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function isSource(value: unknown): value is PluginSource { |
| 90 | + return value === 'builtin' || value === 'store' || value === 'embedder'; |
| 91 | +} |
| 92 | + |
| 93 | +function readNames(value: unknown): readonly string[] | undefined { |
| 94 | + return Array.isArray(value) && value.every((name) => typeof name === 'string') |
| 95 | + ? value |
| 96 | + : undefined; |
| 97 | +} |
| 98 | + |
| 99 | +function readError(value: unknown): PluginInventoryError | undefined { |
| 100 | + if (!isRecord(value) || typeof value.code !== 'string' || typeof value.message !== 'string') { |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + return { |
| 104 | + code: value.code, |
| 105 | + message: value.message, |
| 106 | + ...(typeof value.reason === 'string' ? { reason: value.reason } : {}), |
| 107 | + ...(typeof value.hint === 'string' ? { hint: value.hint } : {}), |
| 108 | + }; |
| 109 | +} |
0 commit comments