Repository navigation
Expand file tree
/
Copy pathlimrun-profile.ts
More file actions
107 lines (103 loc) · 4.16 KB
/
Copy pathlimrun-profile.ts
File metadata and controls
107 lines (103 loc) · 4.16 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
import crypto from 'node:crypto';
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
import type { RemoteConfigProfile } from '@agent-device/remote/remote-config-schema';
import { AppError } from '@agent-device/kernel/errors';
import type { CliFlags } from '@agent-device/contracts/command';
import { type EnvMap } from '@agent-device/kernel/source-value';
import { rejectRefusedProviderProfileFields } from '@agent-device/contracts/provider-profile-fields';
import { LIMRUN_PROFILE_FIELDS } from '@agent-device/provider-limrun';
import { readMetroProfileFields } from './profile-fields.ts';
import { persistAndResolveGeneratedProfile } from './generated-config.ts';
import { resolveRequestedLeaseBackend } from '../commands/connection-lease-transaction.ts';
import {
limrunInstanceVariables,
limrunPlatformForLeaseBackend,
readLimrunCredentials,
} from '../../provider-limrun-credentials.ts';
import { buildConnectClientId } from './client-id.ts';
const DEFAULT_LIMRUN_TENANT = 'limrun';
export function resolveLimrunConnectProfile(options: {
flags: CliFlags;
stateDir: string;
cwd: string;
env: EnvMap;
}): { flags: CliFlags; remoteConfigPath: string } {
const { env } = options;
const profile = buildLimrunRemoteProfile({ flags: options.flags, cwd: options.cwd, env });
const credentials = readLimrunCredentials(env);
const platform = limrunPlatformForLeaseBackend(profile.leaseBackend) ?? 'android';
if (!credentials?.apiKey && !credentials?.instances?.[platform]) {
throw new AppError(
'INVALID_ARGS',
`connect limrun requires LIMRUN_API_KEY, or ${limrunInstanceVariables(platform).join(', ')} for an existing instance.`,
{
hint: 'Set the credentials in the environment before running agent-device connect limrun.',
},
);
}
return persistAndResolveGeneratedProfile({
stateDir: options.stateDir,
provider: 'limrun',
profile,
cwd: options.cwd,
env,
flags: options.flags,
});
}
function buildLimrunRemoteProfile(options: {
flags: CliFlags;
cwd: string;
env: EnvMap;
}): RemoteConfigProfile {
const flags = options.flags;
const leaseBackend = validateLimrunConnectFlags(flags);
const daemonPaths = resolveDaemonPaths(flags.stateDir, {
cwd: options.cwd,
env: options.env,
});
return {
stateDir: daemonPaths.baseDir,
daemonTransport: 'auto',
tenant: flags.tenant ?? DEFAULT_LIMRUN_TENANT,
runId: flags.runId ?? limrunRunId(daemonPaths.baseDir, flags.session),
sessionIsolation: 'tenant',
leaseBackend,
leaseProvider: 'limrun',
platform: flags.platform,
target: 'mobile',
session: flags.session,
// The capture opt-in is read at lease allocation, so it must survive into later commands.
providerNetworkLogs: flags.providerNetworkLogs,
providerNetworkHosts: flags.providerNetworkHosts,
...readMetroProfileFields(flags),
};
}
/**
* A named session reconnects under the same run. An unscoped connect gets a fresh session later in
* `resolveConnectContext`, so it gets a fresh run here rather than one shared by every unscoped call.
*/
function limrunRunId(stateDir: string, session: string | undefined): string {
const identity = session ?? crypto.randomUUID();
return `limrun-${buildConnectClientId('limrun', stateDir, identity)}`;
}
function validateLimrunConnectFlags(flags: CliFlags): 'android-instance' | 'ios-instance' {
rejectRefusedProviderProfileFields(flags, LIMRUN_PROFILE_FIELDS);
if (flags.platform !== 'android' && flags.platform !== 'ios') {
throw new AppError('INVALID_ARGS', 'connect limrun requires --platform ios or android.');
}
if (flags.device !== undefined) {
throw new AppError(
'INVALID_ARGS',
'connect limrun does not accept --device; Limrun selects a remote simulator or emulator.',
);
}
const leaseBackend = resolveRequestedLeaseBackend(flags);
const expectedLeaseBackend = flags.platform === 'ios' ? 'ios-instance' : 'android-instance';
if (leaseBackend !== expectedLeaseBackend) {
throw new AppError(
'INVALID_ARGS',
`connect limrun --platform ${flags.platform} requires --lease-backend ${expectedLeaseBackend}.`,
);
}
return expectedLeaseBackend;
}