@aixnn/aixautots 0.6.4 → 0.6.5
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/dist/{automation-queue-BxrSIkG3.d.ts → automation-queue-aYRO9JLj.d.ts} +2 -2
- package/dist/chunk-7CN37G3W.js +102 -0
- package/dist/{chunk-JTFZSFFA.js → chunk-7H4CYM42.js} +414 -5
- package/dist/{chunk-M4DO7SSZ.js → chunk-D7J4EQTA.js} +1 -1
- package/dist/{chunk-B2BOVLI6.js → chunk-F76N3GOA.js} +1077 -313
- package/dist/{chunk-SAB5CH2K.js → chunk-FBWJUW4X.js} +578 -141
- package/dist/{chunk-T3QVYTOT.js → chunk-IFARWPMU.js} +364 -0
- package/dist/{chunk-UJO7BJUV.js → chunk-LMSN6Z3N.js} +668 -104
- package/dist/{chunk-Q63FQ7B5.js → chunk-LWTL4AIC.js} +254 -13
- package/dist/{chunk-YFF7VLNT.js → chunk-SKTL6ACT.js} +40 -2
- package/dist/{chunk-BYL2S2H6.js → chunk-X5KVYWUK.js} +4 -5
- package/dist/{chunk-NGW34POA.js → chunk-YWA47ARA.js} +318 -15
- package/dist/{config-CLi5IhmO.d.ts → config-B7t1Ln3w.d.ts} +177 -8
- package/dist/core/index.d.ts +21 -16
- package/dist/core/index.js +2 -4
- package/dist/{device-normalize-BaThgnH7.d.ts → device-normalize-CuGflfZA.d.ts} +1 -1
- package/dist/device-session-OFGN4KUD.js +1 -0
- package/dist/i18n/index.js +1 -1
- package/dist/{mirror-session-BP7Rd3Jk.d.ts → mirror-session-DK0OQvA3.d.ts} +11 -3
- package/dist/react/index.d.ts +10 -8
- package/dist/react/index.js +3 -6
- package/dist/{script-debugger-DtUGxK9i.d.ts → script-debugger-6hoLEwEz.d.ts} +1 -1
- package/dist/{task-agent-runner-DzV_OUZ7.d.ts → task-agent-runner-BwRW88rl.d.ts} +5 -3
- package/dist/{types-C2WXVcam.d.ts → types-BbtVr3QM.d.ts} +34 -3
- package/dist/ui/billing.d.ts +1 -1
- package/dist/ui/billing.js +5 -9
- package/dist/ui/device-create.d.ts +4 -3
- package/dist/ui/device-create.js +7 -9
- package/dist/ui/device-mirror.d.ts +16 -2
- package/dist/ui/device-mirror.js +6 -8
- package/dist/ui/devices.d.ts +5 -5
- package/dist/ui/devices.js +10 -13
- package/dist/ui/editor.d.ts +2 -2
- package/dist/ui/editor.js +7 -11
- package/dist/ui/index.d.ts +4 -4
- package/dist/ui/index.js +9 -15
- package/dist/ui/inspector.js +5 -9
- package/dist/ui/market.js +4 -7
- package/dist/ui/mirror.d.ts +3 -3
- package/dist/ui/primitives/index.d.ts +1 -1
- package/dist/ui/primitives/index.js +1 -2
- package/dist/ui/script-studio.js +6 -10
- package/dist/ui/scripts.d.ts +1 -1
- package/dist/ui/scripts.js +5 -9
- package/dist/ui/tasks.js +10 -13
- package/package.json +1 -1
- package/dist/chunk-A5VNENHD.js +0 -229
- package/dist/chunk-BUW45GM2.js +0 -341
- package/dist/chunk-H37NHOYU.js +0 -44
- package/dist/chunk-LAZ3RQ7X.js +0 -668
- package/dist/device-session-CVT7OY4H.js +0 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as RunEventKind, A as AixConfig, R as ResolvedConfig } from './config-
|
|
2
|
-
import { S as Script } from './types-
|
|
1
|
+
import { a as RunEventKind, A as AixConfig, R as ResolvedConfig } from './config-B7t1Ln3w.js';
|
|
2
|
+
import { S as Script } from './types-BbtVr3QM.js';
|
|
3
3
|
|
|
4
4
|
interface FlowSubmit {
|
|
5
5
|
/** Inline flow JSON (XOR scriptId). */
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useAixT } from './chunk-IFARWPMU.js';
|
|
2
|
+
import { Button } from './chunk-SKTL6ACT.js';
|
|
3
|
+
import { useClient } from './chunk-LWTL4AIC.js';
|
|
4
|
+
import { useSyncExternalStore, useState, useEffect } from 'react';
|
|
5
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
var sessions = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
function entryFor(client, nodeId, udid) {
|
|
9
|
+
const scope = client.cfg ?? client;
|
|
10
|
+
let map = sessions.get(scope);
|
|
11
|
+
if (!map) {
|
|
12
|
+
map = /* @__PURE__ */ new Map();
|
|
13
|
+
sessions.set(scope, map);
|
|
14
|
+
}
|
|
15
|
+
const key = JSON.stringify([nodeId, udid]);
|
|
16
|
+
let entry = map.get(key);
|
|
17
|
+
if (!entry) {
|
|
18
|
+
entry = { state: {}, listeners: /* @__PURE__ */ new Set() };
|
|
19
|
+
map.set(key, entry);
|
|
20
|
+
}
|
|
21
|
+
return entry;
|
|
22
|
+
}
|
|
23
|
+
function update(entry, patch) {
|
|
24
|
+
entry.state = { ...entry.state, ...patch };
|
|
25
|
+
entry.listeners.forEach((listener) => listener());
|
|
26
|
+
}
|
|
27
|
+
function IosDeviceDiagnostics({ nodeId, udid }) {
|
|
28
|
+
const client = useClient();
|
|
29
|
+
const t = useAixT();
|
|
30
|
+
const entry = entryFor(client, nodeId, udid);
|
|
31
|
+
const state = useSyncExternalStore((listener) => {
|
|
32
|
+
entry.listeners.add(listener);
|
|
33
|
+
return () => {
|
|
34
|
+
entry.listeners.delete(listener);
|
|
35
|
+
};
|
|
36
|
+
}, () => entry.state, () => entry.state);
|
|
37
|
+
const [opened, setOpened] = useState(false);
|
|
38
|
+
const run = async (action) => {
|
|
39
|
+
if (entry.state.busy || action !== "diagnostics" && (!udid || entry.state.uncertain || !entry.state.result)) return;
|
|
40
|
+
update(entry, { busy: action, error: void 0 });
|
|
41
|
+
try {
|
|
42
|
+
const result = await (action === "diagnostics" ? client.nodes.iosDiagnostics(nodeId, udid) : action === "prepare" ? client.nodes.iosWdaPrepare(nodeId, udid) : action === "start" ? client.nodes.iosWdaStart(nodeId, udid) : client.nodes.iosWdaStop(nodeId, udid));
|
|
43
|
+
if (udid && result.udid !== udid || !Array.isArray(result.stages)) throw new Error(t("iosDiag.invalid"));
|
|
44
|
+
const working = result.operation_in_progress === true || result.wda_state === "starting" || result.stages.some((stage) => stage.code === "operation_in_progress");
|
|
45
|
+
update(entry, { result, uncertain: working });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
update(entry, { error: error instanceof Error ? error.message : String(error), uncertain: true });
|
|
48
|
+
} finally {
|
|
49
|
+
update(entry, { busy: void 0 });
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (opened && !entry.state.busy) void run("diagnostics");
|
|
54
|
+
}, [opened, entry]);
|
|
55
|
+
const disabled = !!state.busy || state.uncertain || !state.result;
|
|
56
|
+
return /* @__PURE__ */ jsxs("div", { className: "mt-2 w-full border-t border-border pt-2", children: [
|
|
57
|
+
/* @__PURE__ */ jsx(Button, { size: "sm", className: "min-h-11", variant: "ghost", "aria-expanded": opened, onClick: () => setOpened((value) => !value), children: t(udid ? "iosDiag.title" : "iosDiag.nodeTitle") }),
|
|
58
|
+
opened && /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
59
|
+
/* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: t(udid ? "iosDiag.explanation" : "iosDiag.nodeExplanation") }),
|
|
60
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-wrap gap-2", children: [
|
|
61
|
+
/* @__PURE__ */ jsx(Button, { size: "sm", className: "min-h-11", variant: "secondary", disabled: !!state.busy, onClick: () => void run("diagnostics"), children: t("iosDiag.refresh") }),
|
|
62
|
+
udid && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
63
|
+
/* @__PURE__ */ jsx(Button, { size: "sm", className: "min-h-11", variant: "secondary", disabled, onClick: () => void run("prepare"), children: t("iosDiag.prepare") }),
|
|
64
|
+
/* @__PURE__ */ jsx(Button, { size: "sm", className: "min-h-11", variant: "secondary", disabled: disabled || state.result?.wda_state === "ready", onClick: () => void run("start"), children: t("iosDiag.start") }),
|
|
65
|
+
/* @__PURE__ */ jsx(Button, { size: "sm", className: "min-h-11", variant: "secondary", disabled: disabled || state.result?.wda_state === "stopped", onClick: () => void run("stop"), children: t("iosDiag.stop") })
|
|
66
|
+
] })
|
|
67
|
+
] }),
|
|
68
|
+
state.busy && /* @__PURE__ */ jsx("p", { role: "status", className: "text-xs", children: t(`iosDiag.busy.${state.busy}`) }),
|
|
69
|
+
state.uncertain && /* @__PURE__ */ jsx("p", { role: "status", className: "text-xs text-muted-foreground", children: t("iosDiag.uncertain") }),
|
|
70
|
+
state.error && /* @__PURE__ */ jsx("p", { role: "alert", className: "text-xs text-destructive", children: state.error }),
|
|
71
|
+
state.result && /* @__PURE__ */ jsx("ul", { className: "space-y-1 text-xs", "aria-label": t("iosDiag.stages"), children: state.result.stages.map((stage, index) => /* @__PURE__ */ jsxs("li", { className: "rounded bg-secondary p-2", children: [
|
|
72
|
+
/* @__PURE__ */ jsxs("span", { className: "font-medium", children: [
|
|
73
|
+
stage.id,
|
|
74
|
+
" \xB7 ",
|
|
75
|
+
t(`iosDiag.status.${stage.status}`)
|
|
76
|
+
] }),
|
|
77
|
+
/* @__PURE__ */ jsx("p", { children: stage.message })
|
|
78
|
+
] }, `${stage.id}:${index}`)) })
|
|
79
|
+
] })
|
|
80
|
+
] });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/ui/device-capabilities.ts
|
|
84
|
+
function isPhysicalIos(device) {
|
|
85
|
+
const meta = device.meta ?? {};
|
|
86
|
+
const spec = meta.spec;
|
|
87
|
+
const platform = meta.platform ?? spec?.platform ?? device.platform;
|
|
88
|
+
return device.id?.startsWith("ios-") === true || meta.kind === "ios-real" || platform === "ios-real" || ["ios", "vphone"].includes(String(platform)) && meta.provisioned !== true;
|
|
89
|
+
}
|
|
90
|
+
function iosPhysicalUnavailableReason(node) {
|
|
91
|
+
const os = node.reported_caps?.system?.os;
|
|
92
|
+
if (os !== "macos" && os !== "windows") return "iOS \u771F\u673A\u9700 Windows/macOS \u5BBF\u4E3B\uFF08\u7ECF go-ios \u9A71\u52A8\uFF09";
|
|
93
|
+
if (node.reported_caps?.ios_capable !== true) return "\u8282\u70B9\u5C1A\u672A\u4E0A\u62A5 iOS \u771F\u673A\u80FD\u529B\uFF0C\u8BF7\u68C0\u67E5\u8282\u70B9\u7248\u672C\u4E0E\u8FD0\u884C\u73AF\u5883";
|
|
94
|
+
if (node.effective_caps?.real_devices !== true) return "\u8282\u70B9\u672A\u542F\u7528\u771F\u673A\u63A5\u5165";
|
|
95
|
+
if (node.effective_caps?.ios !== true) return "\u8282\u70B9\u672A\u542F\u7528 iOS \u8BBE\u5907\u6743\u9650";
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
function canConnectPhysicalDevice(node, platform) {
|
|
99
|
+
return node.is_owner && node.status === "online" && (platform === "ios" ? iosPhysicalUnavailableReason(node) === null : node.effective_caps?.real_devices === true);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { IosDeviceDiagnostics, canConnectPhysicalDevice, iosPhysicalUnavailableReason, isPhysicalIos };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { joinUrl, AixAuthError, AixLocked, AixError, resolveConfig, ReconnectingSocket, AixTimeout, parseIncoming, isRetFrame, errorFromRet, isFridaEventFrame, isScriptletEventFrame, isStatusFrame, executionWsTicketUrl, isExecutionFrame, executionStreamEventsAfter } from './chunk-
|
|
1
|
+
import { joinUrl, AixAuthError, AixLocked, AixError, resolveConfig, ReconnectingSocket, AixTimeout, parseIncoming, isRetFrame, errorFromRet, isFridaEventFrame, isScriptletEventFrame, isStatusFrame, executionWsTicketUrl, isExecutionFrame, getIceServers, mintViewerToken, executionStreamEventsAfter } from './chunk-F76N3GOA.js';
|
|
2
2
|
|
|
3
3
|
// src/core/ws-access.ts
|
|
4
4
|
async function mintDeviceAccess(cfg, deviceId) {
|
|
@@ -233,9 +233,9 @@ var FlowRun = class _FlowRun {
|
|
|
233
233
|
const headers = { ...await resolved.auth.headers("caller"), "content-type": "application/json" };
|
|
234
234
|
const resp = await resolved.fetchFn(url, { method: "POST", headers, body: JSON.stringify(payload) });
|
|
235
235
|
if (!resp.ok) {
|
|
236
|
-
const
|
|
237
|
-
if (resp.status === 401 || resp.status === 403) throw new AixAuthError(`tasks ${resp.status}: ${
|
|
238
|
-
throw new AixError(`tasks ${resp.status}: ${
|
|
236
|
+
const t2 = await resp.text().catch(() => "");
|
|
237
|
+
if (resp.status === 401 || resp.status === 403) throw new AixAuthError(`tasks ${resp.status}: ${t2}`);
|
|
238
|
+
throw new AixError(`tasks ${resp.status}: ${t2}`);
|
|
239
239
|
}
|
|
240
240
|
const data = await resp.json();
|
|
241
241
|
const targets = (data.targets ?? []).map((x) => x.device_id);
|
|
@@ -407,4 +407,413 @@ var ScriptDebugger = class {
|
|
|
407
407
|
}
|
|
408
408
|
};
|
|
409
409
|
|
|
410
|
-
|
|
410
|
+
// src/core/automation-queue.ts
|
|
411
|
+
async function submitBatch(cfg, config) {
|
|
412
|
+
const { script, flow, deviceIds, expiresIn } = config;
|
|
413
|
+
const mode = config.mode ?? "round-robin";
|
|
414
|
+
if (script && flow !== void 0) {
|
|
415
|
+
throw new Error("submitBatch: pass exactly one of script or flow");
|
|
416
|
+
}
|
|
417
|
+
if (!script && flow === void 0) {
|
|
418
|
+
throw new Error("submitBatch: script or flow required");
|
|
419
|
+
}
|
|
420
|
+
if (deviceIds.length === 0) {
|
|
421
|
+
throw new Error("submitBatch: deviceIds required");
|
|
422
|
+
}
|
|
423
|
+
const rows = config.rows.length > 0 ? config.rows : [{}];
|
|
424
|
+
const taskIds = [];
|
|
425
|
+
for (let i = 0; i < rows.length; i++) {
|
|
426
|
+
const row = rows[i];
|
|
427
|
+
const targetIds = mode === "broadcast" ? deviceIds : [deviceIds[i % deviceIds.length]];
|
|
428
|
+
const fr = await FlowRun.submit(cfg, {
|
|
429
|
+
scriptId: script?.id,
|
|
430
|
+
flow,
|
|
431
|
+
deviceIds: targetIds,
|
|
432
|
+
params: row,
|
|
433
|
+
// 全量模式下后端按 Broadcast 在每台设备各跑一遍;轮询模式单目标无需 mode。
|
|
434
|
+
mode: mode === "broadcast" ? "Broadcast" : void 0,
|
|
435
|
+
expiresIn
|
|
436
|
+
});
|
|
437
|
+
fr.close();
|
|
438
|
+
taskIds.push(fr.taskId);
|
|
439
|
+
}
|
|
440
|
+
return taskIds;
|
|
441
|
+
}
|
|
442
|
+
var AutomationQueue = class {
|
|
443
|
+
constructor(cfg) {
|
|
444
|
+
this.cfg = cfg;
|
|
445
|
+
}
|
|
446
|
+
/** 展开并提交一个批次,返回 task id 数组。见 [`submitBatch`]。 */
|
|
447
|
+
submit(config) {
|
|
448
|
+
return submitBatch(this.cfg, config);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
// src/core/script-params.ts
|
|
453
|
+
var SCHEMA_KEY = "__schema__";
|
|
454
|
+
var VALID_TYPES = /* @__PURE__ */ new Set([
|
|
455
|
+
"text",
|
|
456
|
+
"textarea",
|
|
457
|
+
"number",
|
|
458
|
+
"select",
|
|
459
|
+
"boolean"
|
|
460
|
+
]);
|
|
461
|
+
function coerceParam(raw) {
|
|
462
|
+
if (!raw || typeof raw !== "object") return null;
|
|
463
|
+
const o = raw;
|
|
464
|
+
const key = typeof o.key === "string" ? o.key : "";
|
|
465
|
+
if (!key || key.startsWith("__")) return null;
|
|
466
|
+
const type = typeof o.type === "string" && VALID_TYPES.has(o.type) ? o.type : "text";
|
|
467
|
+
const p = { key, type };
|
|
468
|
+
if (typeof o.label === "string") p.label = o.label;
|
|
469
|
+
if (o.default != null) p.default = String(o.default);
|
|
470
|
+
if (Array.isArray(o.options)) p.options = o.options.map((x) => String(x));
|
|
471
|
+
if (o.randomable === true) p.randomable = true;
|
|
472
|
+
if (o.isTarget === true) p.isTarget = true;
|
|
473
|
+
return p;
|
|
474
|
+
}
|
|
475
|
+
function variablesToSchema(variables) {
|
|
476
|
+
const vars = variables ?? {};
|
|
477
|
+
const rawSchema = vars[SCHEMA_KEY];
|
|
478
|
+
if (Array.isArray(rawSchema)) {
|
|
479
|
+
const params = rawSchema.map(coerceParam).filter((p) => p !== null);
|
|
480
|
+
let seenTarget = false;
|
|
481
|
+
for (const p of params) {
|
|
482
|
+
if (p.isTarget) {
|
|
483
|
+
if (seenTarget) delete p.isTarget;
|
|
484
|
+
else seenTarget = true;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
return params;
|
|
488
|
+
}
|
|
489
|
+
return Object.entries(vars).filter(([k]) => !k.startsWith("__")).map(([k, v]) => ({ key: k, type: "text", default: String(v) }));
|
|
490
|
+
}
|
|
491
|
+
function schemaToVariables(schema, base) {
|
|
492
|
+
const out = {};
|
|
493
|
+
for (const [k, v] of Object.entries(base ?? {})) {
|
|
494
|
+
if (k === SCHEMA_KEY) continue;
|
|
495
|
+
if (k.startsWith("__")) out[k] = v;
|
|
496
|
+
}
|
|
497
|
+
out[SCHEMA_KEY] = schema;
|
|
498
|
+
for (const p of schema) out[p.key] = p.default ?? "";
|
|
499
|
+
return out;
|
|
500
|
+
}
|
|
501
|
+
function targetParam(schema) {
|
|
502
|
+
return schema.find((p) => p.isTarget);
|
|
503
|
+
}
|
|
504
|
+
function schemaDefaults(schema) {
|
|
505
|
+
return Object.fromEntries(schema.map((p) => [p.key, p.default ?? ""]));
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// src/core/mirror-session.ts
|
|
509
|
+
var SCRCPY_PROFILES = {
|
|
510
|
+
balanced: { max_size: 1280, bitrate: 6e6, fps: 60 },
|
|
511
|
+
low_latency: { max_size: 1024, bitrate: 4e6, fps: 30 },
|
|
512
|
+
high_quality: { max_size: 0, bitrate: 8e6, fps: 60 }
|
|
513
|
+
};
|
|
514
|
+
function qualityMessage(profile) {
|
|
515
|
+
return { t: "quality", ...SCRCPY_PROFILES[profile] };
|
|
516
|
+
}
|
|
517
|
+
function signalWsUrl(baseUrl) {
|
|
518
|
+
return joinUrl(baseUrl, "/ws/signal").replace(/^http/, "ws");
|
|
519
|
+
}
|
|
520
|
+
var MirrorSession = class _MirrorSession {
|
|
521
|
+
constructor(cfg, session, socket, iceServers, token) {
|
|
522
|
+
this.cfg = cfg;
|
|
523
|
+
this.session = session;
|
|
524
|
+
this.socket = socket;
|
|
525
|
+
this.iceServers = iceServers;
|
|
526
|
+
this.token = token;
|
|
527
|
+
this.pc = null;
|
|
528
|
+
this.input = null;
|
|
529
|
+
this.requestedAudio = null;
|
|
530
|
+
this.left = false;
|
|
531
|
+
this._stream = null;
|
|
532
|
+
this.peerId = null;
|
|
533
|
+
this.remotePeer = null;
|
|
534
|
+
this.remoteDescriptionReady = false;
|
|
535
|
+
this.pendingIce = [];
|
|
536
|
+
this.stateListeners = /* @__PURE__ */ new Set();
|
|
537
|
+
this.streamListeners = /* @__PURE__ */ new Set();
|
|
538
|
+
this.controlListeners = /* @__PURE__ */ new Set();
|
|
539
|
+
}
|
|
540
|
+
/** Start a viewer session. On WebSocket reconnect the socket re-joins
|
|
541
|
+
* automatically; the node then sends a fresh offer, which is paired with a
|
|
542
|
+
* fresh RTCPeerConnection (see the `offer` handler) — no consumer action
|
|
543
|
+
* needed to restore the video track. */
|
|
544
|
+
static async join(cfg, session) {
|
|
545
|
+
const resolved = "fetchFn" in cfg ? cfg : resolveConfig(cfg);
|
|
546
|
+
const iceServers = await getIceServers(resolved);
|
|
547
|
+
const token = await mintViewerToken(resolved, session);
|
|
548
|
+
let ms;
|
|
549
|
+
const socket = new ReconnectingSocket({
|
|
550
|
+
urlProvider: async () => signalWsUrl(resolved.baseUrl),
|
|
551
|
+
wsFactory: resolved.wsFactory,
|
|
552
|
+
onOpen: () => ms.sendSignal({ type: "join", session, role: "viewer", token }),
|
|
553
|
+
onMessage: (text) => ms.onSignal(text),
|
|
554
|
+
onClose: () => {
|
|
555
|
+
if (!ms.left) {
|
|
556
|
+
ms.resetPeer();
|
|
557
|
+
ms._stream = null;
|
|
558
|
+
ms.emitState("disconnected");
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
ms = new _MirrorSession(resolved, session, socket, iceServers, token);
|
|
563
|
+
await socket.start();
|
|
564
|
+
return ms;
|
|
565
|
+
}
|
|
566
|
+
get stream() {
|
|
567
|
+
return this._stream;
|
|
568
|
+
}
|
|
569
|
+
get connectionState() {
|
|
570
|
+
return this.left ? "closed" : this.pc?.connectionState ?? "connecting";
|
|
571
|
+
}
|
|
572
|
+
get controlReady() {
|
|
573
|
+
return !this.left && this.pc?.connectionState === "connected" && this.input?.readyState === "open";
|
|
574
|
+
}
|
|
575
|
+
onControlStateChange(cb) {
|
|
576
|
+
this.controlListeners.add(cb);
|
|
577
|
+
return () => this.controlListeners.delete(cb);
|
|
578
|
+
}
|
|
579
|
+
onConnectionStateChange(cb) {
|
|
580
|
+
this.stateListeners.add(cb);
|
|
581
|
+
return () => this.stateListeners.delete(cb);
|
|
582
|
+
}
|
|
583
|
+
onStream(cb) {
|
|
584
|
+
this.streamListeners.add(cb);
|
|
585
|
+
return () => this.streamListeners.delete(cb);
|
|
586
|
+
}
|
|
587
|
+
sendInput(msg) {
|
|
588
|
+
if (this.left) return;
|
|
589
|
+
if (msg.t === "audio") {
|
|
590
|
+
this.requestedAudio = msg.enabled;
|
|
591
|
+
this.flushAudio();
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
if (["disconnected", "failed", "closed"].includes(this.connectionState)) return;
|
|
595
|
+
if (this.input && this.input.readyState === "open") this.input.send(JSON.stringify(msg));
|
|
596
|
+
}
|
|
597
|
+
emitState(state) {
|
|
598
|
+
for (const cb of this.stateListeners) cb(state);
|
|
599
|
+
this.emitControl();
|
|
600
|
+
}
|
|
601
|
+
emitControl() {
|
|
602
|
+
for (const cb of this.controlListeners) cb(this.controlReady);
|
|
603
|
+
}
|
|
604
|
+
flushAudio() {
|
|
605
|
+
if (this.left || this.requestedAudio === null || this.input?.readyState !== "open") return;
|
|
606
|
+
try {
|
|
607
|
+
this.input.send(JSON.stringify({ t: "audio", enabled: this.requestedAudio }));
|
|
608
|
+
} catch {
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
/** Ask the node to switch this device's encode profile (restarts its capture
|
|
612
|
+
* pipeline; the video blanks briefly while it re-syncs on a keyframe). */
|
|
613
|
+
setQuality(profile) {
|
|
614
|
+
this.sendInput(qualityMessage(profile));
|
|
615
|
+
}
|
|
616
|
+
/** Current round-trip time (seconds) from the active ICE candidate pair, or
|
|
617
|
+
* null when stats are unavailable (no pc yet / mock without getStats). Used
|
|
618
|
+
* by the auto quality heuristic. */
|
|
619
|
+
async currentRtt() {
|
|
620
|
+
const pc = this.pc;
|
|
621
|
+
if (!pc?.getStats) return null;
|
|
622
|
+
try {
|
|
623
|
+
const report = await pc.getStats();
|
|
624
|
+
let rtt = null;
|
|
625
|
+
const visit = (s) => {
|
|
626
|
+
const stat = s;
|
|
627
|
+
if (stat?.type === "candidate-pair" && (stat.nominated || stat.state === "succeeded") && typeof stat.currentRoundTripTime === "number") rtt = stat.currentRoundTripTime;
|
|
628
|
+
};
|
|
629
|
+
const r = report;
|
|
630
|
+
if (typeof r?.forEach === "function") r.forEach(visit);
|
|
631
|
+
else if (Symbol.iterator in Object(report)) for (const s of report) visit(s);
|
|
632
|
+
return rtt;
|
|
633
|
+
} catch {
|
|
634
|
+
return null;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
sendSignal(msg) {
|
|
638
|
+
try {
|
|
639
|
+
this.socket.send(JSON.stringify(msg));
|
|
640
|
+
} catch {
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
ensurePc() {
|
|
644
|
+
if (this.pc) return this.pc;
|
|
645
|
+
const pc = this.cfg.rtcFactory({ iceServers: this.iceServers });
|
|
646
|
+
pc.ontrack = (ev) => {
|
|
647
|
+
if (this.left || this.pc !== pc) return;
|
|
648
|
+
if (ev.receiver && "playoutDelayHint" in ev.receiver) ev.receiver.playoutDelayHint = 0;
|
|
649
|
+
if (ev.streams && ev.streams[0]) {
|
|
650
|
+
this._stream = ev.streams[0];
|
|
651
|
+
for (const cb of this.streamListeners) cb(this._stream);
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
pc.onicecandidate = (ev) => {
|
|
655
|
+
if (!this.left && this.pc === pc && ev.candidate && this.remotePeer) this.sendSignal({ type: "ice", session: this.session, to: this.remotePeer, candidate: ev.candidate });
|
|
656
|
+
};
|
|
657
|
+
pc.ondatachannel = (ev) => {
|
|
658
|
+
if (!this.left && this.pc === pc && ev.channel.label === "control") this.attachInput(ev.channel);
|
|
659
|
+
};
|
|
660
|
+
pc.onconnectionstatechange = () => {
|
|
661
|
+
if (!this.left && this.pc === pc) this.emitState(pc.connectionState);
|
|
662
|
+
};
|
|
663
|
+
this.pc = pc;
|
|
664
|
+
return pc;
|
|
665
|
+
}
|
|
666
|
+
attachInput(dc) {
|
|
667
|
+
if (this.input) {
|
|
668
|
+
this.input.onopen = null;
|
|
669
|
+
this.input.onclose = null;
|
|
670
|
+
this.input.close();
|
|
671
|
+
}
|
|
672
|
+
this.input = dc;
|
|
673
|
+
dc.onopen = () => {
|
|
674
|
+
if (this.input === dc) {
|
|
675
|
+
this.flushAudio();
|
|
676
|
+
this.emitControl();
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
dc.onclose = () => {
|
|
680
|
+
if (this.input === dc) {
|
|
681
|
+
this.input = null;
|
|
682
|
+
this.emitControl();
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
this.flushAudio();
|
|
686
|
+
this.emitControl();
|
|
687
|
+
}
|
|
688
|
+
/** Drop the peer and its stream so a replacement cannot inherit frame/input readiness. */
|
|
689
|
+
resetPeer() {
|
|
690
|
+
if (this.input) {
|
|
691
|
+
this.input.onopen = null;
|
|
692
|
+
this.input.onclose = null;
|
|
693
|
+
}
|
|
694
|
+
try {
|
|
695
|
+
this.input?.close();
|
|
696
|
+
} catch {
|
|
697
|
+
}
|
|
698
|
+
try {
|
|
699
|
+
this.pc?.close();
|
|
700
|
+
} catch {
|
|
701
|
+
}
|
|
702
|
+
this.input = null;
|
|
703
|
+
this.pc = null;
|
|
704
|
+
this._stream = null;
|
|
705
|
+
this.remoteDescriptionReady = false;
|
|
706
|
+
this.pendingIce = [];
|
|
707
|
+
if (!this.left) this.emitState(this.connectionState);
|
|
708
|
+
}
|
|
709
|
+
async onSignal(text) {
|
|
710
|
+
if (this.left) return;
|
|
711
|
+
let m;
|
|
712
|
+
try {
|
|
713
|
+
m = JSON.parse(text);
|
|
714
|
+
} catch {
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
switch (m.type) {
|
|
718
|
+
case "welcome":
|
|
719
|
+
this.peerId = m.peer_id ?? null;
|
|
720
|
+
break;
|
|
721
|
+
case "peers":
|
|
722
|
+
break;
|
|
723
|
+
// node initiates the offer; viewer waits
|
|
724
|
+
case "offer":
|
|
725
|
+
if (m.sdp && m.from) {
|
|
726
|
+
if (this.pc) this.resetPeer();
|
|
727
|
+
this.remotePeer = m.from;
|
|
728
|
+
const pc = this.ensurePc();
|
|
729
|
+
await pc.setRemoteDescription({ type: "offer", sdp: m.sdp });
|
|
730
|
+
if (this.left || this.pc !== pc) return;
|
|
731
|
+
this.remoteDescriptionReady = true;
|
|
732
|
+
for (const candidate of this.pendingIce.splice(0)) {
|
|
733
|
+
await pc.addIceCandidate(candidate);
|
|
734
|
+
if (this.left || this.pc !== pc) return;
|
|
735
|
+
}
|
|
736
|
+
const answer = await pc.createAnswer();
|
|
737
|
+
if (this.left || this.pc !== pc) return;
|
|
738
|
+
await pc.setLocalDescription(answer);
|
|
739
|
+
if (this.left || this.pc !== pc) return;
|
|
740
|
+
this.sendSignal({ type: "answer", session: this.session, to: m.from, sdp: answer.sdp });
|
|
741
|
+
}
|
|
742
|
+
break;
|
|
743
|
+
case "ice":
|
|
744
|
+
if (m.candidate) {
|
|
745
|
+
if (!this.pc || !this.remoteDescriptionReady) this.pendingIce.push(m.candidate);
|
|
746
|
+
else await this.pc.addIceCandidate(m.candidate);
|
|
747
|
+
}
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
leave() {
|
|
752
|
+
this.left = true;
|
|
753
|
+
this.requestedAudio = null;
|
|
754
|
+
this.sendSignal({ type: "leave", session: this.session });
|
|
755
|
+
this.resetPeer();
|
|
756
|
+
this.socket.close();
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
// src/core/resources/device-normalize.ts
|
|
761
|
+
function metaStr(r, key) {
|
|
762
|
+
const v = r.meta?.[key];
|
|
763
|
+
return typeof v === "string" && v ? v : null;
|
|
764
|
+
}
|
|
765
|
+
function seenMs(r) {
|
|
766
|
+
return r.last_seen_at ? Date.parse(r.last_seen_at) || 0 : 0;
|
|
767
|
+
}
|
|
768
|
+
function toTransport(r) {
|
|
769
|
+
return {
|
|
770
|
+
external_id: r.external_id,
|
|
771
|
+
transport: metaStr(r, "transport"),
|
|
772
|
+
online: r.online === true,
|
|
773
|
+
node_id: r.node_id ?? null,
|
|
774
|
+
node_name: r.node_name ?? null,
|
|
775
|
+
last_seen_at: r.last_seen_at
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
function pickPrimary(group) {
|
|
779
|
+
const mostRecent = (rows) => rows.reduce((best, r) => seenMs(r) > seenMs(best) ? r : best);
|
|
780
|
+
const online = group.filter((r) => r.online === true);
|
|
781
|
+
if (!online.length) return mostRecent(group);
|
|
782
|
+
const provisioned = group.filter((r) => r.meta?.provisioned === true);
|
|
783
|
+
if (provisioned.length) return mostRecent(provisioned);
|
|
784
|
+
const usb = online.filter((r) => metaStr(r, "transport") === "usb");
|
|
785
|
+
return mostRecent(usb.length ? usb : online);
|
|
786
|
+
}
|
|
787
|
+
function normalizeDevices(rows) {
|
|
788
|
+
const serialnoSet = /* @__PURE__ */ new Set();
|
|
789
|
+
for (const r of rows) {
|
|
790
|
+
const sn = metaStr(r, "serialno");
|
|
791
|
+
if (sn) serialnoSet.add(sn);
|
|
792
|
+
}
|
|
793
|
+
const groups = /* @__PURE__ */ new Map();
|
|
794
|
+
for (const r of rows) {
|
|
795
|
+
const serialno = metaStr(r, "serialno");
|
|
796
|
+
let key;
|
|
797
|
+
if (serialno) {
|
|
798
|
+
key = `sn:${serialno}`;
|
|
799
|
+
} else if (serialnoSet.has(r.external_id)) {
|
|
800
|
+
key = `sn:${r.external_id}`;
|
|
801
|
+
} else {
|
|
802
|
+
key = `ext:${r.external_id}`;
|
|
803
|
+
}
|
|
804
|
+
const g = groups.get(key);
|
|
805
|
+
if (g) g.push(r);
|
|
806
|
+
else groups.set(key, [r]);
|
|
807
|
+
}
|
|
808
|
+
return [...groups.values()].map((group) => {
|
|
809
|
+
const primary = pickPrimary(group);
|
|
810
|
+
return {
|
|
811
|
+
...primary,
|
|
812
|
+
online: group.some((r) => r.online === true),
|
|
813
|
+
device_id: primary.external_id,
|
|
814
|
+
transports: group.map(toTransport)
|
|
815
|
+
};
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
export { AutomationQueue, DeviceChannel, DeviceTunnel, FlowRun, MirrorSession, SCHEMA_KEY, SCRCPY_PROFILES, ScriptDebugger, mintDeviceAccess, normalizeDevices, qualityMessage, schemaDefaults, schemaToVariables, submitBatch, targetParam, variablesToSchema, wsUrlFromAccess };
|