Repository navigation
Expand file tree
/
Copy pathmanifest.ts
More file actions
70 lines (66 loc) · 2.33 KB
/
Copy pathmanifest.ts
File metadata and controls
70 lines (66 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import fs from 'node:fs';
import path from 'node:path';
import { AppError } from '@agent-device/kernel/errors';
const PROVIDER_PLUGIN_API_VERSION = 1;
export type PluginManifest = {
name: string;
version: string;
agentDevicePlugin: { apiVersion: number; provider: string; entry: string };
};
export function assertUniquePluginProviders(
plugins: readonly PluginManifest[],
reserved: readonly string[],
): void {
const providers = new Set(reserved);
for (const plugin of plugins) {
const provider = plugin.agentDevicePlugin.provider;
if (providers.has(provider))
throw new AppError('INVALID_ARGS', `Duplicate provider plugin: ${provider}`);
providers.add(provider);
}
}
export function readPluginManifest(directory: string): PluginManifest {
let manifest: PluginManifest;
try {
const file = path.join(directory, 'package.json');
manifest = JSON.parse(fs.readFileSync(file, 'utf8')) as PluginManifest;
} catch (error) {
throw new AppError(
'INVALID_ARGS',
`Cannot read plugin manifest: ${directory}`,
{},
error as Error,
);
}
const declaration = manifest?.agentDevicePlugin;
if (
typeof manifest?.name !== 'string' ||
typeof manifest?.version !== 'string' ||
!declaration ||
declaration.apiVersion !== PROVIDER_PLUGIN_API_VERSION ||
typeof declaration.provider !== 'string' ||
!/^[a-z][a-z0-9-]*$/.test(declaration.provider) ||
typeof declaration.entry !== 'string' ||
!declaration.entry.startsWith('./')
) {
throw new AppError('INVALID_ARGS', 'Package does not declare a compatible provider plugin', {
reason: 'incompatible_plugin',
apiVersion: PROVIDER_PLUGIN_API_VERSION,
hint: `Install a plugin release supporting agentDevicePlugin.apiVersion ${PROVIDER_PLUGIN_API_VERSION}.`,
});
}
resolvePluginEntry(directory, declaration.entry);
return manifest as PluginManifest;
}
export function resolvePluginEntry(directory: string, entry: string): string {
try {
const root = fs.realpathSync(directory);
const resolved = fs.realpathSync(path.resolve(directory, entry));
if (resolved.startsWith(`${root}${path.sep}`) && fs.statSync(resolved).isFile())
return resolved;
} catch {}
throw new AppError('INVALID_ARGS', 'Plugin entry must be a readable file inside its package', {
directory,
entry,
});
}