|
1 | 1 | import type { DaemonPlatformServices } from '../platform-services.ts'; |
2 | | -import { asAppError, isRequestCanceledError } from '@agent-device/kernel/errors'; |
3 | | -import { appStateUse } from '@agent-device/contracts/platform-runtime-operations'; |
4 | | -import { |
5 | | - isApplePlatform, |
6 | | - isIosFamily, |
7 | | - isMacOs, |
8 | | - publicPlatformString, |
9 | | - type DeviceInfo, |
10 | | -} from '@agent-device/kernel/device'; |
| 2 | +import { isApplePlatform } from '@agent-device/kernel/device'; |
11 | 3 | import type { DaemonRequest, DaemonResponse } from '../daemon-request.ts'; |
12 | 4 | import { SessionStore } from '../session-store.ts'; |
13 | | -import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; |
14 | | -import type { AppleApplicationState } from '@agent-device/kernel/snapshot'; |
15 | | -import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation'; |
16 | 5 | import { |
17 | 6 | hasExplicitSessionFlag, |
18 | 7 | requireSessionOrExplicitSelector, |
19 | 8 | resolveCommandDevice, |
20 | 9 | selectorTargetsSessionDevice, |
21 | 10 | } from '../session-device-resolution.ts'; |
22 | 11 | import type { BindDeviceRuntime, InspectDeviceRuntimeFacts } from '../request-runtime-binding.ts'; |
23 | | -import { admitRuntimeUse } from '../runtime-admission.ts'; |
24 | 12 | import type { RuntimeCommandHandlerParams } from '../session-runtime-admission.ts'; |
25 | 13 | import { errorResponse } from '@agent-device/kernel/contracts'; |
26 | 14 | import { handleBootCommand } from './session-boot.ts'; |
27 | 15 | import { handleShutdownCommand } from './session-shutdown.ts'; |
| 16 | +import { appleSessionAppStateResponse } from './session-appstate-apple-session.ts'; |
| 17 | +import { queryDeviceRuntimeAppState } from './session-appstate-device-runtime.ts'; |
28 | 18 |
|
29 | 19 | const IOS_APPSTATE_SESSION_REQUIRED_MESSAGE = |
30 | 20 | 'iOS appstate requires an active session on the target device. Run open first (for example: open --session sim --platform ios --device "<name>" <app>).'; |
31 | 21 | const MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE = |
32 | 22 | 'macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").'; |
33 | 23 |
|
34 | | -/** |
35 | | - * The session app's state as a live runner reads it, when this device's owner admits the read; |
36 | | - * nothing otherwise, so the session record alone answers and no state is invented. The owner never |
37 | | - * starts a runner for it. A runner that cannot answer right now (busy, mid-restart) leaves the |
38 | | - * session answer as it was and says so in the log; a cancelled request stays cancelled. |
39 | | - */ |
40 | | -async function readAppleSessionAppState( |
41 | | - params: RuntimeCommandHandlerParams, |
42 | | - session: Readonly<{ device: DeviceInfo; appBundleId?: string }>, |
43 | | -): Promise<AppleApplicationState | undefined> { |
44 | | - if (!session.appBundleId) return undefined; |
45 | | - const admitted = await admitRuntimeUse({ |
46 | | - command: 'appstate', |
47 | | - device: session.device, |
48 | | - use: appStateUse, |
49 | | - inspectFacts: params.inspectFacts, |
50 | | - platformServices: params.platformServices, |
51 | | - bindDevice: params.bindDevice, |
52 | | - }); |
53 | | - if (admitted.type === 'response') return undefined; |
54 | | - try { |
55 | | - const read = await admitted.runtime.operations.appState({ appBundleId: session.appBundleId }); |
56 | | - return read.applicationState; |
57 | | - } catch (error) { |
58 | | - if (isRequestCanceledError(error)) throw error; |
59 | | - emitDiagnostic({ |
60 | | - level: 'warn', |
61 | | - phase: 'apple_appstate_runner_read_failed', |
62 | | - data: { code: asAppError(error).code, message: asAppError(error).message }, |
63 | | - }); |
64 | | - return undefined; |
65 | | - } |
66 | | -} |
67 | | - |
68 | 24 | async function handleAppStateCommand(params: RuntimeCommandHandlerParams): Promise<DaemonResponse> { |
69 | 25 | const { req, sessionName, sessionStore } = params; |
70 | 26 | const session = sessionStore.get(sessionName); |
@@ -95,96 +51,15 @@ async function handleAppStateCommand(params: RuntimeCommandHandlerParams): Promi |
95 | 51 | } |
96 | 52 |
|
97 | 53 | if (shouldUseSessionStateForApple && session) { |
98 | | - const appName = session.appName ?? session.appBundleId; |
99 | | - if (!session.appName && !session.appBundleId) { |
100 | | - if ( |
101 | | - isMacOs(session.device) && |
102 | | - session.surface && |
103 | | - session.surface !== 'app' && |
104 | | - session.surface !== 'frontmost-app' |
105 | | - ) { |
106 | | - return { |
107 | | - ok: true, |
108 | | - data: { |
109 | | - platform: publicPlatformString(session.device), |
110 | | - appName: session.surface, |
111 | | - appBundleId: session.appBundleId, |
112 | | - source: 'session', |
113 | | - surface: session.surface, |
114 | | - }, |
115 | | - }; |
116 | | - } |
117 | | - |
118 | | - const sessionPlatform = isMacOs(session.device) ? 'macOS' : 'iOS'; |
119 | | - return errorResponse( |
120 | | - 'COMMAND_FAILED', |
121 | | - `No foreground app is tracked for this ${sessionPlatform} session. Open an app in the session, then retry appstate.`, |
122 | | - ); |
123 | | - } |
124 | | - |
125 | | - const state = await readAppleSessionAppState(params, session); |
126 | | - return { |
127 | | - ok: true, |
128 | | - data: { |
129 | | - platform: publicPlatformString(session.device), |
130 | | - appName: appName ?? 'unknown', |
131 | | - appBundleId: session.appBundleId, |
132 | | - source: state ? 'runner' : 'session', |
133 | | - ...(state ? { state } : {}), |
134 | | - surface: session.surface ?? 'app', |
135 | | - ...(isIosFamily(session.device) |
136 | | - ? { |
137 | | - device_udid: session.device.id, |
138 | | - ios_simulator_device_set: session.device.simulatorSetPath ?? null, |
139 | | - } |
140 | | - : {}), |
141 | | - }, |
142 | | - }; |
| 54 | + return await appleSessionAppStateResponse(params, session); |
143 | 55 | } |
144 | 56 |
|
145 | 57 | const device = await resolveCommandDevice({ |
146 | 58 | session, |
147 | 59 | flags, |
148 | 60 | platformServices: params.platformServices, |
149 | 61 | }); |
150 | | - if (isIosFamily(device)) { |
151 | | - return errorResponse('SESSION_NOT_FOUND', IOS_APPSTATE_SESSION_REQUIRED_MESSAGE); |
152 | | - } |
153 | | - if (isMacOs(device)) { |
154 | | - return errorResponse('SESSION_NOT_FOUND', MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE); |
155 | | - } |
156 | | - const admitted = await admitRuntimeUse({ |
157 | | - command: 'appstate', |
158 | | - device, |
159 | | - use: appStateUse, |
160 | | - inspectFacts: params.inspectFacts, |
161 | | - platformServices: params.platformServices, |
162 | | - bindDevice: params.bindDevice, |
163 | | - unavailableResponse: (unavailable) => |
164 | | - errorResponse( |
165 | | - 'UNSUPPORTED_OPERATION', |
166 | | - device.platform === 'web' |
167 | | - ? 'appstate is not supported on web.' |
168 | | - : 'appstate is not supported on this device', |
169 | | - undefined, |
170 | | - unavailable.hint ? { hint: unavailable.hint } : undefined, |
171 | | - ), |
172 | | - }); |
173 | | - if (admitted.type === 'response') return admitted.response; |
174 | | - const runtime = admitted.runtime; |
175 | | - await runtime.operations.ensureReady({ |
176 | | - serial: flags.serial, |
177 | | - androidSerialAllowlist: resolveAndroidSerialAllowlistForAppState(flags.androidDeviceAllowlist), |
178 | | - }); |
179 | | - const state = await runtime.operations.appState(); |
180 | | - return { |
181 | | - ok: true, |
182 | | - data: { |
183 | | - platform: publicPlatformString(device), |
184 | | - package: state.package, |
185 | | - activity: state.activity, |
186 | | - }, |
187 | | - }; |
| 62 | + return await queryDeviceRuntimeAppState(params, device); |
188 | 63 | } |
189 | 64 |
|
190 | 65 | export async function handleSessionStateCommands(params: { |
@@ -232,8 +107,3 @@ export async function handleSessionStateCommands(params: { |
232 | 107 |
|
233 | 108 | return null; |
234 | 109 | } |
235 | | - |
236 | | -function resolveAndroidSerialAllowlistForAppState(value: string | undefined): string[] | undefined { |
237 | | - const allowlist = resolveAndroidSerialAllowlist(value); |
238 | | - return allowlist ? [...allowlist].sort() : undefined; |
239 | | -} |
0 commit comments