Repository navigation
Expand file tree
/
Copy pathplatform-runtime.ts
More file actions
201 lines (187 loc) · 7.44 KB
/
Copy pathplatform-runtime.ts
File metadata and controls
201 lines (187 loc) · 7.44 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import type {
ProviderDeviceInventorySource,
ProviderDeviceRuntime,
} from '@agent-device/contracts/device';
import type { AppLogSessionArtifacts } from '@agent-device/contracts/app-log-runtime';
import type { OwnedProcessRecordWriter } from '@agent-device/contracts/platform-runtime-host';
import type { AppStateRuntimeResult } from '@agent-device/contracts/app-state-runtime';
import type { DeviceShutdownRuntimeDependencies } from '@agent-device/contracts/device-shutdown-runtime';
import {
type ComposedDeviceInventoryGateways,
createPlatformModuleRegistry,
} from '@agent-device/contracts/platform-module';
import type { DeviceRuntimeGateway } from '@agent-device/contracts/platform-runtime';
import type {
PlatformProviderRequestContext,
RequestPlatformProviderScope,
RequestPlatformProviders,
} from '@agent-device/contracts/platform-providers';
import type {
PlatformRuntimeModule,
PlatformRuntimeOperations,
} from '@agent-device/contracts/platform-runtime-operations';
import {
inventoryModule as appleInventoryModule,
loadShutdownRuntime as loadAppleShutdownRuntime,
runtimeModule as appleRuntimeModule,
} from '@agent-device/platform-apple';
import {
createAndroidObservationAdapter as createPackageAndroidObservationAdapter,
createAndroidInventoryModule,
createAndroidRuntimeModule,
readAndroidAppStateWithExecutor,
loadShutdownRuntime as loadAndroidShutdownRuntime,
} from '@agent-device/platform-android';
import {
createHarmonyInventoryModule,
runtimeModule as harmonyosRuntimeModule,
} from '@agent-device/platform-harmonyos';
import {
inventoryModule as vegaInventoryModule,
runtimeModule as vegaRuntimeModule,
} from '@agent-device/platform-vega';
import {
captureLinuxSurfaceSnapshot,
inventoryModule as linuxInventoryModule,
runtimeModule as linuxRuntimeModule,
} from '@agent-device/platform-linux';
import {
inventoryModule as webInventoryModule,
runtimeModule as webRuntimeModule,
} from '@agent-device/platform-web';
import {
createComposedPlatformRuntimeGateway,
type PlatformRuntimeProviderRegistration,
} from './platform-runtime-gateway.ts';
import { createComposedDeviceInventoryGateways } from './platform-runtime-device-inventory.ts';
import { createAndroidObservationHost } from './platform-runtime-android-observation-host.ts';
import type { RequestPlatformProviderOptions } from './platform-runtime/request-providers.ts';
export type {
AppleRunnerProviderResolver,
AppleRunnerScreenRecordingTransportResolver,
PlatformProviderResolvers,
} from './platform-runtime/request-providers.ts';
export async function getAndroidAppStateWithAdb(
adb: Parameters<typeof readAndroidAppStateWithExecutor>[0],
signal?: AbortSignal,
): Promise<AppStateRuntimeResult> {
return await readAndroidAppStateWithExecutor(adb, signal);
}
const androidInventoryModule = createAndroidInventoryModule({
sdkRoots: configuredValues(process.env.ANDROID_SDK_ROOT, process.env.ANDROID_HOME),
});
/** One root-composed Android observer shared by every daemon request. */
export const androidObservation = createPackageAndroidObservationAdapter(
createAndroidObservationHost(),
);
const shutdownLoaders = Object.freeze({
apple: async (dependencies: Pick<DeviceShutdownRuntimeDependencies, 'appleTools'>) =>
await loadAppleShutdownRuntime(dependencies),
android: async (dependencies: Pick<DeviceShutdownRuntimeDependencies, 'commands'>) =>
await loadAndroidShutdownRuntime(dependencies),
});
const harmonyosInventoryModule = createHarmonyInventoryModule({
hdcSdkPath: process.env.HDC_SDK_PATH,
devecoSdkHome: process.env.DEVECO_SDK_HOME,
commandLineToolsHome: process.env.HARMONYOS_COMMAND_LINE_TOOLS,
});
export const platformModuleRegistry = createPlatformModuleRegistry([
appleInventoryModule,
androidInventoryModule,
harmonyosInventoryModule,
vegaInventoryModule,
linuxInventoryModule,
webInventoryModule,
]);
export function createPlatformDeviceInventoryGateways(
provider?: ProviderDeviceInventorySource,
): ComposedDeviceInventoryGateways {
return createComposedDeviceInventoryGateways({
registry: platformModuleRegistry,
loadHost: async () => {
const { createDeviceInventoryHost } = await import('./platform-runtime-host.ts');
return createDeviceInventoryHost();
},
provider,
});
}
const androidRuntimeModule = createAndroidRuntimeModule({
// Evaluating the root's adb host module binds the process-wide port exactly once.
bindAdbHost: async () => {
await import('./platform-runtime-android-adb-host.ts');
},
});
/** The root composition registry shared by the gateway and bounded host-contract fixtures. */
export const platformRuntimeModules: ReadonlyMap<Platform, PlatformRuntimeModule> = new Map<
Platform,
PlatformRuntimeModule
>([
['apple', appleRuntimeModule],
['android', androidRuntimeModule],
['harmonyos', harmonyosRuntimeModule],
['vega', vegaRuntimeModule],
['linux', linuxRuntimeModule],
['web', webRuntimeModule],
]);
type Platform = PlatformRuntimeModule['family'];
export function createPlatformRuntimeGateway(
options: Readonly<{
providerRuntimes?: readonly ProviderDeviceRuntime[];
providerModules?: readonly PlatformRuntimeProviderRegistration[];
resolveSessionArtifacts(sessionId: string): AppLogSessionArtifacts;
sessionsDir: string;
ownedProcesses?: OwnedProcessRecordWriter;
assertShutdownAllowed?: () => void;
}>,
): DeviceRuntimeGateway<PlatformRuntimeOperations> {
return createComposedPlatformRuntimeGateway({
modules: platformRuntimeModules,
loadHost: async () => {
const { createPlatformRuntimeHost, createSnapshotRuntimeHost, loadMacOsSurfaceSnapshot } =
await import('./platform-runtime-operation-host.ts');
return createPlatformRuntimeHost({
sessionsDir: options.sessionsDir,
resolveSessionArtifacts: options.resolveSessionArtifacts,
shutdownLoaders,
assertShutdownAllowed: options.assertShutdownAllowed,
snapshot: createSnapshotRuntimeHost({
linux: captureLinuxSurfaceSnapshot,
macos: loadMacOsSurfaceSnapshot,
}),
ownedProcesses: options.ownedProcesses,
});
},
providerRuntimes: options.providerRuntimes,
providerModules: options.providerModules,
});
}
/**
* The canonical root owns request-provider composition as well as device-runtime composition.
* Its private submodule stays unevaluated until a request actually enters a provider scope, so
* importing the runtime registry does not load provider or plugin implementations eagerly.
*/
export function createRequestPlatformProviders(
options: RequestPlatformProviderOptions = {},
): RequestPlatformProviders {
let composed: Promise<RequestPlatformProviders> | undefined;
const resolveComposed = (): Promise<RequestPlatformProviders> => {
composed ??= import('./platform-runtime/request-providers.ts').then(
({ createComposedRequestPlatformProviders }) =>
createComposedRequestPlatformProviders(options),
);
return composed;
};
return Object.freeze({
hasConfiguredResolvers: Object.values(options.providers ?? {}).some(Boolean),
run: async <T>(
context: PlatformProviderRequestContext,
task: (scope: RequestPlatformProviderScope) => Promise<T>,
): Promise<T> => await (await resolveComposed()).run(context, task),
});
}
function configuredValues(...values: Array<string | undefined>): string[] {
return values.flatMap((value) => {
const configured = value?.trim();
return configured ? [configured] : [];
});
}