Repository navigation
Expand file tree
/
Copy pathsession-appstate.ts
More file actions
95 lines (85 loc) · 3.64 KB
/
Copy pathsession-appstate.ts
File metadata and controls
95 lines (85 loc) · 3.64 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
import { isApplePlatform } from '@agent-device/kernel/device';
import { errorResponse } from '@agent-device/kernel/contracts';
import {
hasExplicitSessionFlag,
requireSessionOrExplicitSelector,
resolveCommandDevice,
selectorTargetsSessionDevice,
} from '../session-device-resolution.ts';
import type { RuntimeCommandHandlerParams } from '../session-runtime-admission.ts';
import type { DaemonResponse } from '../daemon-request.ts';
import {
appleSessionAppStateResponse,
appleSessionRequiredRefusal,
type AppStateSessionRecord,
} from './session-appstate-apple-session.ts';
import { queryDeviceRuntimeAppState } from './session-appstate-device-runtime.ts';
function appStateSessionNotFoundResponse(
sessionName: string,
platform: string | undefined,
): DaemonResponse {
if (platform === 'ios') {
return errorResponse(
'SESSION_NOT_FOUND',
`No active session "${sessionName}". Run open with --session ${sessionName} first.`,
);
}
return errorResponse(
'SESSION_NOT_FOUND',
`No active session "${sessionName}". Run open with --session ${sessionName} first, or omit --session to query by device selector.`,
);
}
/**
* Has this appstate request an answerable target, and which authority answers it: the Apple
* session it points at, or the device runtime the selector resolves to. The apple-session branch
* carries the session itself, so answering it can never re-read the store for a session that has
* gone missing between admission and response. Every way the request can be refused without a
* device answers here.
*/
type AppStateSessionStoreEntry = ReturnType<RuntimeCommandHandlerParams['sessionStore']['get']>;
type AppStateTargetDecision =
| { refusal: DaemonResponse }
| { answeredBy: 'apple-session'; session: AppStateSessionRecord }
| { answeredBy: 'device-runtime'; session: AppStateSessionStoreEntry };
function admitAppStateTarget(params: RuntimeCommandHandlerParams): AppStateTargetDecision {
const { req, sessionName, sessionStore } = params;
const session = sessionStore.get(sessionName);
const flags = req.flags ?? {};
const normalizedPlatform = flags.platform;
if (!session && hasExplicitSessionFlag(flags)) {
return { refusal: appStateSessionNotFoundResponse(sessionName, normalizedPlatform) };
}
const guard = requireSessionOrExplicitSelector('appstate', session, flags);
if (guard) return { refusal: guard };
const useSessionStateForApple =
isApplePlatform(session?.device.platform) && selectorTargetsSessionDevice(flags, session);
if (normalizedPlatform === 'ios' && !useSessionStateForApple) {
return { refusal: appleSessionRequiredRefusal('ios') };
}
if (normalizedPlatform === 'macos' && !useSessionStateForApple) {
return { refusal: appleSessionRequiredRefusal('macos') };
}
if (useSessionStateForApple && session) {
return { answeredBy: 'apple-session' as const, session };
}
return { answeredBy: 'device-runtime' as const, session };
}
/**
* Which app is in the foreground: the Apple session's record and runner answer for their own
* devices; every other target is resolved and answered by its device runtime.
*/
export async function handleAppStateCommand(
params: RuntimeCommandHandlerParams,
): Promise<DaemonResponse> {
const target = admitAppStateTarget(params);
if ('refusal' in target) return target.refusal;
if (target.answeredBy === 'apple-session') {
return await appleSessionAppStateResponse(params, target.session);
}
const device = await resolveCommandDevice({
session: target.session,
flags: params.req.flags ?? {},
platformServices: params.platformServices,
});
return await queryDeviceRuntimeAppState(params, device);
}