agent-device-x86sim 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +192 -2
- package/ax-hook/libx86sim-ax.dylib +0 -0
- package/dist/cli.js +127 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +226 -0
- package/dist/start-server-C84E8KRm.js +1182 -0
- package/dist/system-DMZAX-z7.js +203 -0
- package/package.json +59 -3
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { access, constants, readFile, readdir, readlink, realpath } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { createConnection } from "node:net";
|
|
5
|
+
//#region src/cli/install.ts
|
|
6
|
+
const BOOT_SCRIPT = "cli/libexec/arm64/boot-a64.sh";
|
|
7
|
+
/** Where x86Sim copies itself and keeps phones, as its AppImage decides it. */
|
|
8
|
+
const x86simDataDir = (env, home) => env.X86SIM_DATA || path.join(env.XDG_DATA_HOME || path.join(home, ".local", "share"), "x86sim");
|
|
9
|
+
/** Where x86Sim keeps its scratch files. */
|
|
10
|
+
const x86simCacheDir = (env, home) => env.X86SIM_CACHE || path.join(env.XDG_CACHE_HOME || path.join(home, ".cache"), "x86sim");
|
|
11
|
+
const versionParts = (version) => version.split(/[.-]/).map((part) => /^\d+$/.test(part) ? Number(part) : -1);
|
|
12
|
+
/** Compares versions part by part. A part that is not a number, such as `pre8`, sorts below a missing one. */
|
|
13
|
+
function compareVersions(a, b) {
|
|
14
|
+
const left = versionParts(a);
|
|
15
|
+
const right = versionParts(b);
|
|
16
|
+
for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
|
|
17
|
+
const difference = (left[index] ?? 0) - (right[index] ?? 0);
|
|
18
|
+
if (difference !== 0) return difference;
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
const newestVersion = (versions) => [...versions].sort(compareVersions).at(-1);
|
|
23
|
+
/**
|
|
24
|
+
* x86Sim's AppImage copies its payload into the data folder, and phones run from there after
|
|
25
|
+
* the launcher exits. This runs the copy the way the AppImage's `AppRun` would, with the same
|
|
26
|
+
* environment.
|
|
27
|
+
*/
|
|
28
|
+
function dataDirInstall(parts) {
|
|
29
|
+
const app = path.join(parts.dataDir, "app", parts.version);
|
|
30
|
+
const env = {
|
|
31
|
+
X86SIM_DARLING_MODE: parts.darlingMode,
|
|
32
|
+
X86SIM_ROOT: parts.dataDir,
|
|
33
|
+
X86SIM_SCRATCH: parts.cacheDir,
|
|
34
|
+
X86SIM_APP: app,
|
|
35
|
+
X86SIM_VERSION: parts.version,
|
|
36
|
+
X86SIM_DARLING_ID: parts.darlingId,
|
|
37
|
+
X86SIM_PYLIB: path.join(app, "pylib"),
|
|
38
|
+
A64_PREFIX_HEAL: "0",
|
|
39
|
+
PATH: `${path.join(app, "bin")}:${parts.path}`
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
label: `x86Sim ${parts.version} in ${parts.dataDir}`,
|
|
43
|
+
dataDir: parts.dataDir,
|
|
44
|
+
cli: (args) => ({
|
|
45
|
+
command: path.join(app, "cli", "x86sim"),
|
|
46
|
+
args: [...args],
|
|
47
|
+
env
|
|
48
|
+
}),
|
|
49
|
+
boot: () => ({
|
|
50
|
+
command: "/bin/sh",
|
|
51
|
+
args: ["-c", BOOT_SCRIPT],
|
|
52
|
+
env,
|
|
53
|
+
cwd: parts.dataDir
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** An AppImage the user named, which runs everything through its own entry point. */
|
|
58
|
+
function appImageInstall(appImage, dataDir) {
|
|
59
|
+
return {
|
|
60
|
+
label: appImage,
|
|
61
|
+
dataDir,
|
|
62
|
+
cli: (args) => ({
|
|
63
|
+
command: appImage,
|
|
64
|
+
args: ["x86sim", ...args],
|
|
65
|
+
env: {}
|
|
66
|
+
}),
|
|
67
|
+
boot: () => ({
|
|
68
|
+
command: appImage,
|
|
69
|
+
args: [
|
|
70
|
+
"shell",
|
|
71
|
+
"-c",
|
|
72
|
+
BOOT_SCRIPT
|
|
73
|
+
],
|
|
74
|
+
env: {}
|
|
75
|
+
})
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/cli/discovery.ts
|
|
80
|
+
const QUICK_START_URL = "https://github.com/x86sim/x86sim#quick-start";
|
|
81
|
+
const DEVICE_PREFIX = "device-a64-";
|
|
82
|
+
/** x86Sim has not been set up on this machine. Callers that can run without it catch this one. */
|
|
83
|
+
var NotSetUpError = class extends Error {};
|
|
84
|
+
/**
|
|
85
|
+
* x86Sim as `--appimage` or `$X86SIM_APPIMAGE` names it, else the newest copy in the data
|
|
86
|
+
* folder. Running an AppImage copies its payload there, whatever folder the AppImage is in.
|
|
87
|
+
*/
|
|
88
|
+
async function findX86Sim(flag, environment) {
|
|
89
|
+
const { env, home, cwd } = environment;
|
|
90
|
+
const dataDir = x86simDataDir(env, home);
|
|
91
|
+
const given = flag ?? env.X86SIM_APPIMAGE;
|
|
92
|
+
if (given) {
|
|
93
|
+
const file = path.resolve(cwd, given);
|
|
94
|
+
if (!await environment.isExecutable(file)) throw new Error(`The x86Sim AppImage ${file} is missing or not executable. Run: chmod +x ${file}`);
|
|
95
|
+
return appImageInstall(file, dataDir);
|
|
96
|
+
}
|
|
97
|
+
const appDir = path.join(dataDir, "app");
|
|
98
|
+
const complete = [];
|
|
99
|
+
for (const version of await environment.listDir(appDir)) if ((await environment.listDir(path.join(appDir, version))).includes(".complete")) complete.push(version);
|
|
100
|
+
const version = newestVersion(complete);
|
|
101
|
+
if (!version) throw new NotSetUpError(`x86Sim is not set up. Follow ${QUICK_START_URL}`);
|
|
102
|
+
const mode = env.X86SIM_DARLING_MODE;
|
|
103
|
+
const link = await environment.readLink(path.join(dataDir, "darling-prefix"));
|
|
104
|
+
return dataDirInstall({
|
|
105
|
+
dataDir,
|
|
106
|
+
cacheDir: x86simCacheDir(env, home),
|
|
107
|
+
version,
|
|
108
|
+
darlingId: (await environment.readText(path.join(appDir, version, "DARLING_ID")))?.trim() ?? "",
|
|
109
|
+
darlingMode: mode === "system" || mode === "userns" ? mode : link?.includes("darling-userns") ? "userns" : "system",
|
|
110
|
+
path: env.PATH ?? ""
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
async function listPhones(dataDir, environment) {
|
|
114
|
+
const varDir = path.join(dataDir, "var");
|
|
115
|
+
const folders = (await environment.listDir(varDir)).filter((name) => name.startsWith(DEVICE_PREFIX)).sort();
|
|
116
|
+
return await Promise.all(folders.map(async (folder) => ({
|
|
117
|
+
folder,
|
|
118
|
+
path: path.join(varDir, folder),
|
|
119
|
+
name: (await environment.readText(path.join(varDir, folder, "name")))?.trim() || folder
|
|
120
|
+
})));
|
|
121
|
+
}
|
|
122
|
+
const quoted = (phones) => phones.map((phone) => `"${phone.name}"`).join(", ");
|
|
123
|
+
const pick = (phone) => `agent-device connect x86sim --device "${phone.name}"`;
|
|
124
|
+
/**
|
|
125
|
+
* The phone to use. `--device` names it, by the name the launcher shows or by its folder, or is
|
|
126
|
+
* the folder's path. Without it, the phone that runs wins, else the only phone.
|
|
127
|
+
*/
|
|
128
|
+
async function findPhone(flag, dataDir, environment) {
|
|
129
|
+
const phones = await listPhones(dataDir, environment);
|
|
130
|
+
if (flag?.includes("/")) {
|
|
131
|
+
const folder = path.resolve(environment.cwd, flag);
|
|
132
|
+
if (!(await environment.listDir(folder)).includes("sim.env")) throw new Error(`${folder} is not a phone folder, because it has no sim.env.`);
|
|
133
|
+
return {
|
|
134
|
+
folder: path.basename(folder),
|
|
135
|
+
path: folder,
|
|
136
|
+
name: path.basename(folder)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (flag) {
|
|
140
|
+
const lower = flag.toLowerCase();
|
|
141
|
+
const matches = phones.filter((phone) => phone.folder === flag || phone.name.toLowerCase() === lower);
|
|
142
|
+
const [only] = matches;
|
|
143
|
+
if (only && matches.length === 1) return only;
|
|
144
|
+
if (only) throw new Error(`More than one phone is named "${flag}". Use a folder name: ${matches.map((p) => p.folder).join(", ")}.`);
|
|
145
|
+
throw new Error(phones.length === 0 ? `No phone found in ${path.join(dataDir, "var")}. Create one on the Setup page of the x86Sim launcher. ${QUICK_START_URL}` : `No phone is named "${flag}". The phones are ${quoted(phones)}.`);
|
|
146
|
+
}
|
|
147
|
+
const [first] = phones;
|
|
148
|
+
if (!first) throw new Error(`No phone found in ${path.join(dataDir, "var")}. Create one on the Setup page of the x86Sim launcher. ${QUICK_START_URL}`);
|
|
149
|
+
const running = [];
|
|
150
|
+
for (const phone of phones) if (await environment.isRunning(phone.path)) running.push(phone);
|
|
151
|
+
const [firstRunning] = running;
|
|
152
|
+
if (firstRunning && running.length === 1) return firstRunning;
|
|
153
|
+
if (phones.length === 1) return first;
|
|
154
|
+
throw new Error(`${running.length > 1 ? "More than one phone runs" : "There is more than one phone and none runs"}. The phones are ${quoted(running.length > 1 ? running : phones)}. Pick one with ${pick(running[0] ?? first)}.`);
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/cli/running.ts
|
|
158
|
+
const PROBE_TIMEOUT_MS = 5e3;
|
|
159
|
+
/** Whether the phone in this folder runs, which is whether its simhid socket accepts a connection. */
|
|
160
|
+
async function isPhoneRunning(device) {
|
|
161
|
+
try {
|
|
162
|
+
const socketPath = await realpath(path.join(device, "data", "simhid.sock"));
|
|
163
|
+
return await new Promise((resolve) => {
|
|
164
|
+
const socket = createConnection(socketPath);
|
|
165
|
+
const done = (answer) => {
|
|
166
|
+
socket.destroy();
|
|
167
|
+
resolve(answer);
|
|
168
|
+
};
|
|
169
|
+
socket.setTimeout(PROBE_TIMEOUT_MS, () => done(false));
|
|
170
|
+
socket.once("connect", () => done(true));
|
|
171
|
+
socket.once("error", () => done(false));
|
|
172
|
+
});
|
|
173
|
+
} catch {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/cli/system.ts
|
|
179
|
+
/** The machine this process runs on. */
|
|
180
|
+
function systemEnvironment() {
|
|
181
|
+
return {
|
|
182
|
+
env: process.env,
|
|
183
|
+
home: homedir(),
|
|
184
|
+
cwd: process.cwd(),
|
|
185
|
+
listDir: (dir) => readdir(dir).catch(() => []),
|
|
186
|
+
readText: (file) => readFile(file, "utf8").catch(() => void 0),
|
|
187
|
+
readLink: (file) => readlink(file).catch(() => void 0),
|
|
188
|
+
isExecutable: (file) => access(file, constants.X_OK).then(() => true, () => false),
|
|
189
|
+
isRunning: isPhoneRunning
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/** The x86Sim and the phone on this machine, or an error that names the fix. */
|
|
193
|
+
async function discoverTarget(flags, environment = systemEnvironment()) {
|
|
194
|
+
const x86sim = await findX86Sim(flags.appimage, environment);
|
|
195
|
+
const phone = await findPhone(flags.device, x86sim.dataDir, environment);
|
|
196
|
+
return {
|
|
197
|
+
x86sim,
|
|
198
|
+
device: phone.path,
|
|
199
|
+
deviceName: phone.name
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
export { isPhoneRunning as n, NotSetUpError as r, discoverTarget as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-device-x86sim",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Run an iOS simulator on Linux through x86Sim and drive it with agent-device. One package: the agent-device plugin and the `agent-device-x86sim` command.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent-device",
|
|
7
|
+
"agent-device-plugin",
|
|
8
|
+
"ai-agents",
|
|
9
|
+
"ios",
|
|
10
|
+
"ios-simulator",
|
|
11
|
+
"linux",
|
|
12
|
+
"webdriver",
|
|
13
|
+
"x86sim"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/wobsoriano/agent-device-x86sim#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/wobsoriano/agent-device-x86sim/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/wobsoriano/agent-device-x86sim.git",
|
|
23
|
+
"directory": "packages/plugin"
|
|
24
|
+
},
|
|
25
|
+
"bin": {
|
|
26
|
+
"agent-device-x86sim": "./dist/cli.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"ax-hook/libx86sim-ax.dylib"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"exports": {
|
|
34
|
+
"./agent-device": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"import": "./dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.20.5",
|
|
41
|
+
"agent-device": "0.21.35",
|
|
42
|
+
"typescript": "^7.0.2",
|
|
43
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.3.3",
|
|
44
|
+
"vite-plus": "0.3.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"agent-device": ">=0.21.35"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"agent-device": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=22.18.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "vp pack",
|
|
59
|
+
"dev": "vp pack --watch",
|
|
60
|
+
"test": "vp test"
|
|
61
|
+
}
|
|
6
62
|
}
|