Skip to content

Commit e6090f3

Browse files
committed
Merge remote-tracking branch 'github/main' into takeover-27231
2 parents 49ba555 + b50d8a1 commit e6090f3

34 files changed

Lines changed: 1368 additions & 187 deletions

‎apps/desktop/src/preview/DesktopBrowserHost.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,7 @@ export const make = Effect.gen(function* () {
302302
if (tab.relay) return tab.relay;
303303
const { webContents, debugger: debuggee } = tab.debuggee;
304304
const send = (method: string, params: Record<string, unknown>, sessionId?: string) =>
305-
sessionId === undefined
306-
? sendPageCommand(webContents, debuggee, method, params)
307-
: debuggee.sendCommand(method, params, sessionId);
305+
sendPageCommand(webContents, debuggee, method, params, sessionId);
308306
const detachedOwners = new Set<string>();
309307
/** Viewers and recordings stream; the agent's own sessions never do. */
310308
const streamingOwners = new Set<string>();

‎apps/desktop/src/preview/GuestInput.test.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as NodeVM from "node:vm";
2+
13
import { describe, expect, it, vi } from "vite-plus/test";
24

35
import { sendPageCommand, toKeyboardInputEvents } from "./GuestInput.ts";
@@ -42,6 +44,149 @@ describe("preview guest input", () => {
4244
expect(guest.sendCommand).not.toHaveBeenCalledWith("Input.insertText", expect.anything());
4345
});
4446

47+
it.each([
48+
{
49+
sessionId: undefined,
50+
method: "Input.dispatchMouseEvent",
51+
type: "mousePressed",
52+
outcome: "restore",
53+
},
54+
{
55+
sessionId: "child-frame",
56+
method: "Input.dispatchMouseEvent",
57+
type: "mousePressed",
58+
outcome: "restore",
59+
},
60+
{
61+
sessionId: undefined,
62+
method: "Input.dispatchTouchEvent",
63+
type: "touchStart",
64+
outcome: "restore",
65+
},
66+
{
67+
sessionId: undefined,
68+
method: "Input.dispatchMouseEvent",
69+
type: "mousePressed",
70+
outcome: "other-field",
71+
},
72+
{
73+
sessionId: undefined,
74+
method: "Input.dispatchMouseEvent",
75+
type: "mousePressed",
76+
outcome: "removed",
77+
},
78+
{
79+
sessionId: undefined,
80+
method: "Input.dispatchMouseEvent",
81+
type: "mousePressed",
82+
outcome: "failure",
83+
},
84+
])(
85+
"preserves host focus for $method in $sessionId with $outcome",
86+
async ({ sessionId, method, type, outcome }) => {
87+
const document = { activeElement: {} as object };
88+
const composer = {
89+
isConnected: true,
90+
focus: () => {
91+
document.activeElement = composer;
92+
},
93+
};
94+
const preview = { tagName: "WEBVIEW", getWebContentsId: () => 7 };
95+
const otherField = { tagName: "INPUT" };
96+
document.activeElement = composer;
97+
const context = NodeVM.createContext({ document });
98+
const host = {
99+
isDestroyed: () => false,
100+
executeJavaScript: async (script: string) => NodeVM.runInContext(script, context),
101+
};
102+
const guest = { id: 7, focusedFrame: null, hostWebContents: host };
103+
const debuggee = {
104+
sendCommand: async () => {
105+
document.activeElement = outcome === "other-field" ? otherField : preview;
106+
if (outcome === "removed") composer.isConnected = false;
107+
if (outcome === "failure") throw new Error("Pointer failed");
108+
return {};
109+
},
110+
};
111+
const result = sendPageCommand(
112+
guest as unknown as Electron.WebContents,
113+
debuggee as unknown as Electron.Debugger,
114+
method,
115+
{ type, x: 10, y: 10, button: "left" },
116+
sessionId,
117+
);
118+
if (outcome === "failure") await expect(result).rejects.toThrow("Pointer failed");
119+
else await result;
120+
expect(document.activeElement).toBe(
121+
outcome === "other-field" ? otherField : outcome === "removed" ? preview : composer,
122+
);
123+
expect(Object.keys(context)).toEqual(["document"]);
124+
},
125+
);
126+
127+
it.each([
128+
{
129+
method: "Input.dispatchMouseEvent",
130+
press: "mousePressed",
131+
release: "mouseReleased",
132+
failPress: false,
133+
},
134+
{
135+
method: "Input.dispatchTouchEvent",
136+
press: "touchStart",
137+
release: "touchEnd",
138+
failPress: false,
139+
},
140+
{
141+
method: "Input.dispatchMouseEvent",
142+
press: "mousePressed",
143+
release: "mouseReleased",
144+
failPress: true,
145+
},
146+
])(
147+
"keeps $method releases behind presses waiting for host focus, failed press: $failPress",
148+
async ({ method, press, release, failPress }) => {
149+
const captureStarted = Promise.withResolvers<void>();
150+
const finishCapture = Promise.withResolvers<void>();
151+
const events: Array<unknown> = [];
152+
const guest = {
153+
id: 7,
154+
focusedFrame: null,
155+
hostWebContents: {
156+
isDestroyed: () => false,
157+
executeJavaScript: async () => {
158+
captureStarted.resolve();
159+
await finishCapture.promise;
160+
},
161+
},
162+
};
163+
const debuggee = {
164+
sendCommand: async (_method: string, params: Record<string, unknown>) => {
165+
events.push(params["type"]);
166+
if (failPress && params["type"] === press) throw new Error("Press failed");
167+
return {};
168+
},
169+
};
170+
const send = (type: string) =>
171+
sendPageCommand(
172+
guest as unknown as Electron.WebContents,
173+
debuggee as unknown as Electron.Debugger,
174+
method,
175+
{ type },
176+
);
177+
const pressed = send(press);
178+
await captureStarted.promise;
179+
const released = send(release);
180+
finishCapture.resolve();
181+
const results = await Promise.allSettled([pressed, released]);
182+
expect(results.map((result) => result.status)).toEqual([
183+
failPress ? "rejected" : "fulfilled",
184+
"fulfilled",
185+
]);
186+
expect(events).toEqual([press, release]);
187+
},
188+
);
189+
45190
it("refuses input when a cross-site frame holds the unfocused page's focus", async () => {
46191
const guest = makeGuest({ focused: false, focusedDocument: false });
47192
await expect(guest.send("Input.insertText", { text: "secret" })).rejects.toThrow(

‎apps/desktop/src/preview/GuestInput.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,78 @@ const evaluate = async (debuggee: Electron.Debugger, expression: string) => {
9292
return reply.result?.value;
9393
};
9494

95+
let focusSequence = 0;
96+
97+
/** A CDP pointer press focuses the guest's native view even with focus emulation enabled. */
98+
const sendPointerPress = async (
99+
webContents: Electron.WebContents,
100+
debuggee: Electron.Debugger,
101+
method: string,
102+
params: Record<string, unknown>,
103+
sessionId: string | undefined,
104+
) => {
105+
const host = webContents.hostWebContents;
106+
if (!host || host.isDestroyed() || webContents.focusedFrame) {
107+
return debuggee.sendCommand(method, params, sessionId);
108+
}
109+
const key = JSON.stringify(`__t3PreviewFocus${++focusSequence}`);
110+
await host.executeJavaScript(`(() => {
111+
const active = document.activeElement;
112+
if (!active || active === document.body || active.tagName === "WEBVIEW") return;
113+
globalThis[${key}] = () => {
114+
delete globalThis[${key}];
115+
const current = document.activeElement;
116+
if (active.isConnected && current?.tagName === "WEBVIEW" &&
117+
current.getWebContentsId() === ${webContents.id}) active.focus({ preventScroll: true });
118+
};
119+
})()`);
120+
try {
121+
return await debuggee.sendCommand(method, params, sessionId);
122+
} finally {
123+
if (!host.isDestroyed()) {
124+
await host.executeJavaScript(`globalThis[${key}]?.()`);
125+
}
126+
}
127+
};
128+
129+
const pointerCommands = new WeakMap<Electron.WebContents, Promise<unknown>>();
130+
131+
/** Focus capture crosses processes; later pointer events must not overtake the press. */
132+
const sendPointerCommand = async (
133+
webContents: Electron.WebContents,
134+
debuggee: Electron.Debugger,
135+
method: string,
136+
params: Record<string, unknown>,
137+
sessionId: string | undefined,
138+
) => {
139+
const previous = pointerCommands.get(webContents) ?? Promise.resolve();
140+
const current = previous
141+
.catch(() => {})
142+
.then(() =>
143+
params["type"] === "mousePressed" || params["type"] === "touchStart"
144+
? sendPointerPress(webContents, debuggee, method, params, sessionId)
145+
: debuggee.sendCommand(method, params, sessionId),
146+
);
147+
pointerCommands.set(webContents, current);
148+
try {
149+
return await current;
150+
} finally {
151+
if (pointerCommands.get(webContents) === current) pointerCommands.delete(webContents);
152+
}
153+
};
154+
95155
/** Sends one command to the guest's page, keeping reloads and input inside it. */
96156
export const sendPageCommand = async (
97157
webContents: Electron.WebContents,
98158
debuggee: Electron.Debugger,
99159
method: string,
100160
params: Record<string, unknown>,
161+
sessionId?: string,
101162
): Promise<unknown> => {
163+
if (method === "Input.dispatchMouseEvent" || method === "Input.dispatchTouchEvent") {
164+
return sendPointerCommand(webContents, debuggee, method, params, sessionId);
165+
}
166+
if (sessionId !== undefined) return debuggee.sendCommand(method, params, sessionId);
102167
// Electron's debugger routes Page.reload on a webview to its embedder window.
103168
if (method === "Page.reload") {
104169
if (params["ignoreCache"] === true) webContents.reloadIgnoringCache();

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ const DAYS = [
109109

110110
function describeSchedule(task: ScheduledTask): string {
111111
if (task.schedule.type === "webhook") return "On webhook";
112+
if (task.schedule.type === "once") {
113+
const runAt = new Date(task.schedule.runAt);
114+
return `Once, ${formatRunAtDate(runAt)} at ${formatRunAtTime(runAt)}`;
115+
}
112116
if (task.schedule.type === "interval") return formatScheduledTaskInterval(task.schedule.everyMs);
113117
const days = task.schedule.weekdays?.length ? repeatLabel(task.schedule.weekdays) : "Every day";
114118
return `${days} at ${formatTime(task.schedule.timeOfDay)}`;
@@ -122,6 +126,14 @@ function formatTime(value: string): string {
122126
return time.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
123127
}
124128

129+
function formatRunAtDate(value: Date): string {
130+
return value.toLocaleDateString([], { month: "short", day: "numeric", year: "numeric" });
131+
}
132+
133+
function formatRunAtTime(value: Date): string {
134+
return value.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
135+
}
136+
125137
function timePickerValue(value: string): Date {
126138
const [hours, minutes] = value.split(":").map(Number);
127139
const time = new Date();
@@ -687,6 +699,7 @@ function TaskForm({
687699
});
688700
const submissionPending = useRef(false);
689701
const [timePickerOpen, setTimePickerOpen] = useState(false);
702+
const [runAtPicker, setRunAtPicker] = useState<"date" | "time" | null>(null);
690703
const taskMissing =
691704
draft.task !== null &&
692705
tasks.data !== null &&
@@ -701,6 +714,9 @@ function TaskForm({
701714
}
702715
};
703716

717+
const offersOnce =
718+
config?.environment.capabilities.oneTimeSchedules === true ||
719+
draft.task?.schedule.type === "once";
704720
const save = async () => {
705721
if (
706722
!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||
@@ -728,6 +744,13 @@ function TaskForm({
728744
);
729745
return;
730746
}
747+
if (draft.schedule.mode === "once" && !offersOnce) {
748+
Dialog.alert(
749+
"Once is not available here",
750+
"This environment runs an older T3 Code. Update it to schedule a one-time task, or pick another schedule.",
751+
);
752+
return;
753+
}
731754
const parsedFilters = parseWebhookFilterDrafts(draft.schedule.filters);
732755
if (draft.schedule.mode === "webhook" && "error" in parsedFilters) {
733756
Dialog.alert("Invalid filter", parsedFilters.error);
@@ -964,19 +987,31 @@ function TaskForm({
964987
variant="card"
965988
options={[
966989
{ value: "fixed_time", label: "At a time" },
990+
// Older servers reject one-time schedules on save.
991+
...(offersOnce || draft.schedule.mode === "once"
992+
? [{ value: "once" as const, label: "Once" }]
993+
: []),
967994
{ value: "interval", label: "Interval" },
968995
{ value: "webhook", label: "On webhook" },
969996
]}
970997
selected={draft.schedule.mode}
971998
onSelect={(mode) => {
972999
setTimePickerOpen(false);
1000+
setRunAtPicker(null);
9731001
setDraft({
9741002
...draft,
9751003
prompt:
9761004
mode === "webhook" && !draft.prompt.trim()
9771005
? DEFAULT_WEBHOOK_PROMPT
9781006
: draft.prompt,
979-
schedule: { ...draft.schedule, mode },
1007+
schedule: {
1008+
...draft.schedule,
1009+
mode,
1010+
runAt:
1011+
mode === "once" && !draft.schedule.runAt
1012+
? new Date(Math.ceil(Date.now() / 3_600_000) * 3_600_000).toISOString()
1013+
: draft.schedule.runAt,
1014+
},
9801015
});
9811016
}}
9821017
/>
@@ -1058,6 +1093,53 @@ function TaskForm({
10581093
}}
10591094
/>
10601095
</>
1096+
) : draft.schedule.mode === "once" ? (
1097+
(["date", "time"] as const).map((field) => {
1098+
const runAt = new Date(draft.schedule.runAt);
1099+
const label = field === "date" ? formatRunAtDate(runAt) : formatRunAtTime(runAt);
1100+
return (
1101+
<View key={field}>
1102+
<Pressable
1103+
accessibilityRole="button"
1104+
accessibilityLabel={`${field === "date" ? "Date" : "Time"}, ${label}`}
1105+
onPress={() => setRunAtPicker((open) => (open === field ? null : field))}
1106+
className="min-h-14 flex-row items-center gap-3 border-t border-border-subtle px-4 py-3 active:opacity-70"
1107+
>
1108+
<Text textRole="body" className="text-foreground">
1109+
{field === "date" ? "Date" : "Time"}
1110+
</Text>
1111+
<Text
1112+
textRole="footnote"
1113+
className="min-w-0 flex-1 text-right text-foreground-muted"
1114+
>
1115+
{label}
1116+
</Text>
1117+
<SymbolView
1118+
name="chevron.right"
1119+
size={14}
1120+
tintColorClassName="accent-chevron"
1121+
type="monochrome"
1122+
/>
1123+
</Pressable>
1124+
{runAtPicker === field ? (
1125+
<DateTimePicker
1126+
value={runAt}
1127+
mode={field}
1128+
display={Platform.OS === "ios" ? "spinner" : "default"}
1129+
onDismiss={() => setRunAtPicker(null)}
1130+
onValueChange={(_, selected) => {
1131+
// Android shows a dialog that closes on pick; iOS stays inline.
1132+
if (Platform.OS === "android") setRunAtPicker(null);
1133+
setDraft({
1134+
...draft,
1135+
schedule: { ...draft.schedule, runAt: selected.toISOString() },
1136+
});
1137+
}}
1138+
/>
1139+
) : null}
1140+
</View>
1141+
);
1142+
})
10611143
) : draft.schedule.mode === "webhook" ? (
10621144
<>
10631145
<WebhookScheduleDetails

0 commit comments

Comments
 (0)