Repository navigation
Expand file tree
/
Copy pathprovider-device-runtimes.ts
More file actions
130 lines (119 loc) · 4.76 KB
/
Copy pathprovider-device-runtimes.ts
File metadata and controls
130 lines (119 loc) · 4.76 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { DefaultCloudWebDriverProviderRuntimeEnv } from '@agent-device/provider-webdriver';
import type { ProviderDeviceRuntime } from '@agent-device/contracts/device';
import type { LIMRUN_PROVIDER } from '@agent-device/provider-limrun';
import type {
PlatformRuntimeHost,
PlatformRuntimeOwner,
PlatformRuntimeProviderModule,
} from '@agent-device/contracts/platform-runtime-operations';
import type { PlatformRuntimeProviderRegistration } from './platform-runtime-gateway.ts';
import { providerWebDriver } from './provider-webdriver.ts';
import { readLimrunCredentials } from './provider-limrun-credentials.ts';
export type DefaultProviderDeviceRuntimeEnv = DefaultCloudWebDriverProviderRuntimeEnv &
NodeJS.ProcessEnv;
export const DEFAULT_PROVIDER_RUNTIME_REQUIRED_IDS = [
...providerWebDriver.providerIds,
'limrun' satisfies typeof LIMRUN_PROVIDER,
] as const;
export type DefaultProviderRuntimeComposition = Readonly<{
runtimes: readonly ProviderDeviceRuntime[];
platformModules: readonly PlatformRuntimeProviderRegistration[];
}>;
/**
* Extracts eager provider-owner metadata without loading a provider implementation. Both the
* daemon composition and integration harnesses use this one classification so a provider-owned
* device can never silently select a local lifecycle runtime merely because its registration was
* forgotten at a call site.
*/
export function createProviderPlatformRuntimeRegistrations(
runtimes: readonly ProviderDeviceRuntime[],
): readonly PlatformRuntimeProviderRegistration[] {
return Object.freeze(
runtimes.flatMap((runtime) =>
hasPlatformRuntimeModule(runtime)
? [{ runtime, module: providerPlatformRuntimeModule(runtime) }]
: [],
),
);
}
export async function createDaemonProviderRuntimeComposition(
env: DefaultProviderDeviceRuntimeEnv = process.env,
): Promise<DefaultProviderRuntimeComposition> {
const bundled = await createDefaultProviderRuntimeComposition(env);
try {
const { loadProviderPlugins } = await import('./plugins/load.ts');
const plugins = await loadProviderPlugins(env, DEFAULT_PROVIDER_RUNTIME_REQUIRED_IDS);
return Object.freeze({
runtimes: Object.freeze([...bundled.runtimes, ...plugins.map(({ runtime }) => runtime)]),
platformModules: Object.freeze([
...bundled.platformModules,
...plugins.map(({ runtime, platformModule }) => ({ runtime, module: platformModule })),
]),
});
} catch (error) {
await Promise.allSettled(bundled.runtimes.map(async (runtime) => await runtime.shutdown()));
throw error;
}
}
export async function createDefaultProviderRuntimeComposition(
env: DefaultProviderDeviceRuntimeEnv = process.env,
): Promise<DefaultProviderRuntimeComposition> {
const runtimes = providerWebDriver.createDefaultRuntimes(env);
const platformModules = [...createProviderPlatformRuntimeRegistrations(runtimes)];
const limrunCredentials = readLimrunCredentials(env);
if (!limrunCredentials) {
return Object.freeze({ runtimes, platformModules: Object.freeze(platformModules) });
}
const [limrunRuntime, dependencies] = await Promise.all([
import('@agent-device/provider-limrun'),
import('./sdk/limrun-runtime-dependencies.ts'),
]);
const registration = limrunRuntime.createLimrunRuntime(
limrunCredentials,
dependencies.createLimrunRuntimeDependencies(),
{ includePlatformModule: true },
);
return Object.freeze({
runtimes: Object.freeze([...runtimes, registration.runtime]),
platformModules: Object.freeze([
...platformModules,
{ runtime: registration.runtime, module: registration.platformModule },
]),
});
}
type ProviderRuntimeWithPlatformModule = ProviderDeviceRuntime &
Readonly<{
owner: PlatformRuntimeProviderModule['owner'];
loadRuntime(host: PlatformRuntimeHost): Promise<PlatformRuntimeOwner>;
}>;
function hasPlatformRuntimeModule(
runtime: ProviderDeviceRuntime,
): runtime is ProviderRuntimeWithPlatformModule {
if (!('owner' in runtime) || !('loadRuntime' in runtime)) return false;
const owner = runtime.owner;
return (
typeof runtime.loadRuntime === 'function' &&
isProviderRuntimeOwner(owner) &&
owner.provider === runtime.provider
);
}
function isProviderRuntimeOwner(owner: unknown): owner is PlatformRuntimeProviderModule['owner'] {
return (
typeof owner === 'object' &&
owner !== null &&
'kind' in owner &&
owner.kind === 'provider-runtime' &&
'provider' in owner &&
typeof owner.provider === 'string' &&
'instance' in owner &&
typeof owner.instance === 'string'
);
}
function providerPlatformRuntimeModule(
runtime: ProviderRuntimeWithPlatformModule,
): PlatformRuntimeProviderModule {
return Object.freeze({
owner: runtime.owner,
loadRuntime: async (host) => await runtime.loadRuntime(host),
});
}