Skip to content

Commit 6eede0f

Browse files
feat(web): start from an automation in the new-thread picker (#371)
The new-thread Start-from picker gains an Automations tab listing the project's scheduled tasks. Enter runs the task as Run now does and opens the thread it ran in (the run-now result now carries that threadId); mod+Enter opens the task's edit form. A pinned "New automation from draft" row opens the new-task form prefilled with the composer's text, project, model, runtime and workspace, noting what a text prompt could not keep. Mobile's Start-from menu gets the same Automation source. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1a3f02e commit 6eede0f

25 files changed

Lines changed: 1118 additions & 78 deletions

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

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import {
2727
parseMaxDeliveryAge,
2828
} from "@t3tools/client-runtime/scheduled-task-webhook";
2929
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
30-
import { useFocusEffect, useNavigation, usePreventRemove } from "@react-navigation/native";
30+
import {
31+
type StaticScreenProps,
32+
useFocusEffect,
33+
useNavigation,
34+
usePreventRemove,
35+
} from "@react-navigation/native";
3136
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
3237
import { AppState, Platform, Pressable, TextInput as RNTextInput, View } from "react-native";
3338
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -75,8 +80,11 @@ import { useScheduledTaskEditor } from "./scheduled-task-editor";
7580
import { scheduledTaskEditorSessionAtom } from "./scheduled-task-editor-state";
7681
import { appAtomRegistry } from "../../state/atom-registry";
7782
import {
83+
describeSchedule,
7884
formatNextScheduledTaskRun,
79-
formatScheduledTaskInterval,
85+
formatTime,
86+
repeatLabel,
87+
SCHEDULE_DAYS,
8088
} from "./scheduledTaskPresentation";
8189

8290
type ScheduledTaskRoutes = {
@@ -86,31 +94,6 @@ type ScheduledTaskRoutes = {
8694
SettingsScheduledTaskBranch: undefined;
8795
};
8896

89-
const DAYS = [
90-
{ index: 1, label: "Mon" },
91-
{ index: 2, label: "Tue" },
92-
{ index: 3, label: "Wed" },
93-
{ index: 4, label: "Thu" },
94-
{ index: 5, label: "Fri" },
95-
{ index: 6, label: "Sat" },
96-
{ index: 0, label: "Sun" },
97-
] as const;
98-
99-
function describeSchedule(task: ScheduledTask): string {
100-
if (task.schedule.type === "webhook") return "On webhook";
101-
if (task.schedule.type === "interval") return formatScheduledTaskInterval(task.schedule.everyMs);
102-
const days = task.schedule.weekdays?.length ? repeatLabel(task.schedule.weekdays) : "Every day";
103-
return `${days} at ${formatTime(task.schedule.timeOfDay)}`;
104-
}
105-
106-
function formatTime(value: string): string {
107-
const [hours, minutes] = value.split(":").map(Number);
108-
if (!Number.isInteger(hours) || !Number.isInteger(minutes)) return value;
109-
const time = new Date();
110-
time.setHours(hours ?? 9, minutes ?? 0, 0, 0);
111-
return time.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
112-
}
113-
11497
function timePickerValue(value: string): Date {
11598
const [hours, minutes] = value.split(":").map(Number);
11699
const time = new Date();
@@ -123,17 +106,6 @@ function timePickerValue(value: string): Date {
123106
return time;
124107
}
125108

126-
function repeatLabel(weekdays: ReadonlyArray<number>): string {
127-
const days = new Set(weekdays);
128-
if (days.size === 7) return "Every day";
129-
if (days.size === 5 && [1, 2, 3, 4, 5].every((day) => days.has(day))) return "Weekdays";
130-
return (
131-
DAYS.filter((day) => days.has(day.index))
132-
.map((day) => day.label)
133-
.join(", ") || "Choose days"
134-
);
135-
}
136-
137109
function FormField(props: {
138110
readonly label: string;
139111
readonly value: string;
@@ -365,10 +337,13 @@ export function SettingsScheduledTasksRouteScreen() {
365337
);
366338
}
367339

368-
export function SettingsScheduledTaskNewRouteScreen() {
340+
/** `prefilled` keeps the editor session a caller has already started, such as from a draft. */
341+
export function SettingsScheduledTaskNewRouteScreen({
342+
route,
343+
}: StaticScreenProps<{ readonly prefilled?: boolean } | undefined>) {
369344
const { resetEditor } = useScheduledTaskEditor();
370345
const { availableTargets } = useSettingsEnvironmentFilter();
371-
const initialized = useRef(false);
346+
const initialized = useRef(route.params?.prefilled === true);
372347
useLayoutEffect(() => {
373348
if (initialized.current || availableTargets.length === 0) return;
374349
initialized.current = true;
@@ -511,6 +486,7 @@ function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string }
511486
onChangeSelection={setSelection}
512487
disabled={saving}
513488
voiceInput={voiceInput}
489+
note={editor.note}
514490
/>
515491
}
516492
setDraft={(draft) => {
@@ -913,7 +889,7 @@ function TaskForm({
913889
title: "Weekdays",
914890
state: repeatLabel(draft.schedule.weekdays) === "Weekdays" ? "on" : undefined,
915891
},
916-
...DAYS.map((day) => ({
892+
...SCHEDULE_DAYS.map((day) => ({
917893
id: String(day.index),
918894
title: day.label,
919895
attributes: { keepsMenuPresented: true },
@@ -925,11 +901,11 @@ function TaskForm({
925901
onSelect={(id) => {
926902
const weekdays =
927903
id === "every_day"
928-
? DAYS.map((day) => day.index)
904+
? SCHEDULE_DAYS.map((day) => day.index)
929905
: id === "weekdays"
930906
? [1, 2, 3, 4, 5]
931907
: (() => {
932-
const day = DAYS.find((item) => String(item.index) === id);
908+
const day = SCHEDULE_DAYS.find((item) => String(item.index) === id);
933909
if (!day) return draft.schedule.weekdays;
934910
return draft.schedule.weekdays.includes(day.index)
935911
? draft.schedule.weekdays.filter((index) => index !== day.index)

‎apps/mobile/src/features/settings/components/ScheduledTaskPromptField.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export function ScheduledTaskPromptField(props: {
1919
readonly onChangeSelection: (selection: ComposerEditorSelection) => void;
2020
readonly disabled: boolean;
2121
readonly voiceInput: ReturnType<typeof useVoiceInputController>;
22+
/** Says what a prefilled prompt could not keep from where it came from. */
23+
readonly note?: string | null | undefined;
2224
}) {
2325
const voice = props.voiceInput;
2426
const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);
@@ -78,6 +80,7 @@ export function ScheduledTaskPromptField(props: {
7880
</View>
7981
</ComposerDictationToolbar>
8082
) : null}
83+
{props.note ? <Text className="text-sm text-foreground-muted">{props.note}</Text> : null}
8184
</View>
8285
);
8386
}

‎apps/mobile/src/features/settings/scheduled-task-editor-state.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export type ScheduledTaskEditor = {
88
readonly environmentId: EnvironmentId;
99
readonly environmentLabel: string;
1010
readonly draft: ScheduledTaskDraft;
11+
/** Set when the form was prefilled from a composer draft that held more than text. */
12+
readonly note?: string | null;
1113
};
1214

1315
type ScheduledTaskEditorSession = {

‎apps/mobile/src/features/settings/scheduledTaskDraft.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
type ScheduledTask,
1010
} from "@t3tools/contracts";
1111
import {
12+
type ComposerTaskSource,
13+
draftFromComposer,
1214
scheduledTaskDefaultModel,
1315
createDraft,
1416
editDraft,
@@ -370,3 +372,50 @@ describe("scheduled task model defaults", () => {
370372
).toBeNull();
371373
});
372374
});
375+
376+
describe("draftFromComposer", () => {
377+
const source = {
378+
projectId: ProjectId.make("project-a"),
379+
prompt: "Summarise yesterday's errors ",
380+
sideContextCount: 0,
381+
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
382+
runtimeMode: "approval-required",
383+
workspaceMode: "local",
384+
branch: "main",
385+
worktreePath: null,
386+
startFromOrigin: false,
387+
} satisfies ComposerTaskSource;
388+
389+
it("carries the prompt, project, model, runtime and project checkout", () => {
390+
const { draft, note } = draftFromComposer(source);
391+
expect(note).toBeNull();
392+
expect(draft).toMatchObject({
393+
task: null,
394+
prompt: "Summarise yesterday's errors",
395+
projectId: "project-a",
396+
modelSelection: source.modelSelection,
397+
modelSelectionIsExplicit: true,
398+
runtimeMode: "approval-required",
399+
workspace: "root",
400+
});
401+
});
402+
403+
it("maps a new worktree to its base branch and a checked-out worktree to its path", () => {
404+
expect(
405+
draftFromComposer({
406+
...source,
407+
workspaceMode: "worktree",
408+
branch: "develop",
409+
startFromOrigin: true,
410+
}).draft,
411+
).toMatchObject({ workspace: "worktree", baseRef: "develop", startFromOrigin: true });
412+
expect(draftFromComposer({ ...source, worktreePath: "/wt/pr-12" }).draft).toMatchObject({
413+
workspace: "existing_worktree",
414+
checkoutPath: "/wt/pr-12",
415+
});
416+
});
417+
418+
it("notes attachments a text prompt cannot carry", () => {
419+
expect(draftFromComposer({ ...source, sideContextCount: 1 }).note).not.toBeNull();
420+
});
421+
});

‎apps/mobile/src/features/settings/scheduledTaskDraft.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
} from "@t3tools/contracts";
1010

1111
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
12+
import { scheduledTaskPromptFromComposer } from "@t3tools/client-runtime/scheduled-task-picker";
1213
import { parseMaxDeliveryAge } from "@t3tools/client-runtime/scheduled-task-webhook";
1314
import {
1415
resolveProjectSettings,
@@ -225,3 +226,46 @@ export function editDraft(task: ScheduledTask): ScheduledTaskDraft {
225226
runtimeMode: task.runtimeMode,
226227
};
227228
}
229+
230+
/** What a new thread's composer holds, to start a scheduled task from. */
231+
export interface ComposerTaskSource {
232+
readonly projectId: ProjectId;
233+
readonly prompt: string;
234+
/** Attachments and context held beside the text, which a task's prompt cannot carry. */
235+
readonly sideContextCount: number;
236+
readonly modelSelection: ModelSelection | null;
237+
readonly runtimeMode: RuntimeMode;
238+
readonly workspaceMode: "local" | "worktree";
239+
readonly branch: string | null;
240+
readonly worktreePath: string | null;
241+
readonly startFromOrigin: boolean;
242+
}
243+
244+
/** A new-task form prefilled from a composer draft, with a note on what text could not keep. */
245+
export function draftFromComposer(source: ComposerTaskSource): {
246+
readonly draft: ScheduledTaskDraft;
247+
readonly note: string | null;
248+
} {
249+
const { prompt, note } = scheduledTaskPromptFromComposer(source.prompt, source.sideContextCount);
250+
const base = createDraft(source.projectId, source.modelSelection);
251+
const workspace: Partial<ScheduledTaskDraft> =
252+
source.worktreePath !== null
253+
? { workspace: "existing_worktree", checkoutPath: source.worktreePath }
254+
: source.workspaceMode === "worktree"
255+
? {
256+
workspace: "worktree",
257+
...(source.branch ? { baseRef: source.branch } : {}),
258+
startFromOrigin: source.startFromOrigin,
259+
}
260+
: { workspace: "root" };
261+
return {
262+
draft: {
263+
...base,
264+
...workspace,
265+
prompt,
266+
modelSelectionIsExplicit: source.modelSelection !== null,
267+
runtimeMode: source.runtimeMode,
268+
},
269+
note,
270+
};
271+
}

‎apps/mobile/src/features/settings/scheduledTaskPresentation.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ScheduledTask } from "@t3tools/contracts";
2+
13
const MINUTE = 60_000;
24
const HOUR = 60 * MINUTE;
35
const DAY = 24 * HOUR;
@@ -55,3 +57,39 @@ export function formatNextScheduledTaskRun(nextRunAt: string, now: number): stri
5557
});
5658
return `Next run ${date} at ${time}`;
5759
}
60+
61+
export const SCHEDULE_DAYS = [
62+
{ index: 1, label: "Mon" },
63+
{ index: 2, label: "Tue" },
64+
{ index: 3, label: "Wed" },
65+
{ index: 4, label: "Thu" },
66+
{ index: 5, label: "Fri" },
67+
{ index: 6, label: "Sat" },
68+
{ index: 0, label: "Sun" },
69+
] as const;
70+
71+
export function describeSchedule(task: Pick<ScheduledTask, "schedule">): string {
72+
if (task.schedule.type === "webhook") return "On webhook";
73+
if (task.schedule.type === "interval") return formatScheduledTaskInterval(task.schedule.everyMs);
74+
const days = task.schedule.weekdays?.length ? repeatLabel(task.schedule.weekdays) : "Every day";
75+
return `${days} at ${formatTime(task.schedule.timeOfDay)}`;
76+
}
77+
78+
export function formatTime(value: string): string {
79+
const [hours, minutes] = value.split(":").map(Number);
80+
if (!Number.isInteger(hours) || !Number.isInteger(minutes)) return value;
81+
const time = new Date();
82+
time.setHours(hours ?? 9, minutes ?? 0, 0, 0);
83+
return time.toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
84+
}
85+
86+
export function repeatLabel(weekdays: ReadonlyArray<number>): string {
87+
const days = new Set(weekdays);
88+
if (days.size === 7) return "Every day";
89+
if (days.size === 5 && [1, 2, 3, 4, 5].every((day) => days.has(day))) return "Weekdays";
90+
return (
91+
SCHEDULE_DAYS.filter((day) => days.has(day.index))
92+
.map((day) => day.label)
93+
.join(", ") || "Choose days"
94+
);
95+
}

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import { Menu } from "../../components/ui/Menu";
7575
import { gitHubIssueEnvironment } from "../../state/githubIssues";
7676
import { linearEnvironment } from "../../state/linear";
7777
import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
78+
import { useStartFromAutomationPicker } from "./StartFromAutomationSheet";
7879
import { useStartFromPullRequestPicker } from "./StartFromPullRequestSheet";
7980
import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip";
8081
import { composerStripAttachments } from "../../lib/composerImages";
@@ -1059,6 +1060,29 @@ export function NewTaskDraftScreen(props: {
10591060
}
10601061
: null,
10611062
);
1063+
const startFromAutomationPicker = useStartFromAutomationPicker(
1064+
selectedProject
1065+
? {
1066+
environmentId: selectedProject.environmentId,
1067+
environmentLabel:
1068+
flow.environments.find(
1069+
(environment) => environment.environmentId === selectedProject.environmentId,
1070+
)?.environmentLabel ?? "Environment",
1071+
projectId: selectedProject.id,
1072+
readSource: () => ({
1073+
projectId: selectedProject.id,
1074+
prompt: flow.prompt,
1075+
sideContextCount: flow.attachments.length,
1076+
modelSelection: flow.selectedModel,
1077+
runtimeMode: flow.runtimeMode,
1078+
workspaceMode: flow.workspaceMode,
1079+
branch: flow.selectedBranchName,
1080+
worktreePath: flow.selectedWorktreePath,
1081+
startFromOrigin: flow.startFromOrigin,
1082+
}),
1083+
}
1084+
: null,
1085+
);
10621086
const checkoutPicker = useCheckoutPicker(
10631087
flow.draftKey && selectedProject
10641088
? {
@@ -1721,15 +1745,17 @@ export function NewTaskDraftScreen(props: {
17211745
{ id: "pull-request", title: "Pull request", image: PullRequestGlyph.pullRequest },
17221746
{ id: "issue", title: "Linear issue", image: "ticket" },
17231747
{ id: "github-issue", title: "GitHub issue", image: "smallcircle.filled.circle" },
1748+
{ id: "automation", title: "Automation", image: "calendar.badge.clock" },
17241749
]}
17251750
onPressAction={({ nativeEvent }) => {
17261751
if (nativeEvent.event === "pull-request") startFromPullRequestPicker.open();
17271752
else if (nativeEvent.event === "issue") startFromIssuePicker.open();
17281753
else if (nativeEvent.event === "github-issue") startFromGitHubIssuePicker.open();
1754+
else if (nativeEvent.event === "automation") startFromAutomationPicker.open();
17291755
}}
17301756
>
17311757
<Pressable
1732-
accessibilityLabel="Start from a pull request or issue"
1758+
accessibilityLabel="Start from a pull request, issue, or automation"
17331759
accessibilityRole="button"
17341760
className="size-11 items-center justify-center rounded-xl active:bg-subtle disabled:opacity-45"
17351761
disabled={isComposerInteractionLocked}
@@ -1999,6 +2025,7 @@ export function NewTaskDraftScreen(props: {
19992025
{notionPagePicker.sheet}
20002026
{granolaNotePicker.sheet}
20012027
{startFromPullRequestPicker.sheet}
2028+
{startFromAutomationPicker.sheet}
20022029
{checkoutPicker.sheet}
20032030
{repositoryPicker.sheet}
20042031
</View>

0 commit comments

Comments
 (0)