Repository navigation
Expand file tree
/
Copy pathsession-state.ts
More file actions
109 lines (96 loc) · 4.06 KB
/
Copy pathsession-state.ts
File metadata and controls
109 lines (96 loc) · 4.06 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
import type { DaemonPlatformServices } from '../platform-services.ts';
import { isApplePlatform } from '@agent-device/kernel/device';
import type { DaemonRequest, DaemonResponse } from '../daemon-request.ts';
import { SessionStore } from '../session-store.ts';
import {
hasExplicitSessionFlag,
requireSessionOrExplicitSelector,
resolveCommandDevice,
selectorTargetsSessionDevice,
} from '../session-device-resolution.ts';
import type { BindDeviceRuntime, InspectDeviceRuntimeFacts } from '../request-runtime-binding.ts';
import type { RuntimeCommandHandlerParams } from '../session-runtime-admission.ts';
import { errorResponse } from '@agent-device/kernel/contracts';
import { handleBootCommand } from './session-boot.ts';
import { handleShutdownCommand } from './session-shutdown.ts';
import { appleSessionAppStateResponse } from './session-appstate-apple-session.ts';
import { queryDeviceRuntimeAppState } from './session-appstate-device-runtime.ts';
const IOS_APPSTATE_SESSION_REQUIRED_MESSAGE =
'iOS appstate requires an active session on the target device. Run open first (for example: open --session sim --platform ios --device "<name>" <app>).';
const MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE =
'macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").';
async function handleAppStateCommand(params: RuntimeCommandHandlerParams): Promise<DaemonResponse> {
const { req, sessionName, sessionStore } = params;
const session = sessionStore.get(sessionName);
const flags = req.flags ?? {};
const normalizedPlatform = flags.platform;
if (!session && hasExplicitSessionFlag(flags)) {
const message =
normalizedPlatform === 'ios'
? `No active session "${sessionName}". Run open with --session ${sessionName} first.`
: `No active session "${sessionName}". Run open with --session ${sessionName} first, or omit --session to query by device selector.`;
return errorResponse('SESSION_NOT_FOUND', message);
}
const guard = requireSessionOrExplicitSelector('appstate', session, flags);
if (guard) return guard;
const shouldUseSessionStateForApple =
isApplePlatform(session?.device.platform) && selectorTargetsSessionDevice(flags, session);
const targetsIos = normalizedPlatform === 'ios';
const targetsMacOs = normalizedPlatform === 'macos';
if (targetsIos && !shouldUseSessionStateForApple) {
return errorResponse('SESSION_NOT_FOUND', IOS_APPSTATE_SESSION_REQUIRED_MESSAGE);
}
if (targetsMacOs && !shouldUseSessionStateForApple) {
return errorResponse('SESSION_NOT_FOUND', MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE);
}
if (shouldUseSessionStateForApple && session) {
return await appleSessionAppStateResponse(params, session);
}
const device = await resolveCommandDevice({
session,
flags,
platformServices: params.platformServices,
});
return await queryDeviceRuntimeAppState(params, device);
}
export async function handleSessionStateCommands(params: {
req: DaemonRequest;
sessionName: string;
sessionStore: SessionStore;
inspectFacts?: InspectDeviceRuntimeFacts;
platformServices: DaemonPlatformServices;
bindDevice?: BindDeviceRuntime;
}): Promise<DaemonResponse | null> {
const { req, sessionName, sessionStore } = params;
if (req.command === 'boot') {
return await handleBootCommand({
req,
sessionName,
sessionStore,
inspectFacts: params.inspectFacts,
platformServices: params.platformServices,
bindDevice: params.bindDevice,
});
}
if (req.command === 'shutdown') {
return await handleShutdownCommand({
req,
sessionName,
sessionStore,
inspectFacts: params.inspectFacts,
platformServices: params.platformServices,
bindDevice: params.bindDevice,
});
}
if (req.command === 'appstate') {
return await handleAppStateCommand({
req,
sessionName,
sessionStore,
inspectFacts: params.inspectFacts,
platformServices: params.platformServices,
bindDevice: params.bindDevice,
});
}
return null;
}