@@ -4,14 +4,21 @@ import type {
44 ScheduledTask ,
55 ScheduledTaskUpsertInput ,
66} from "@t3tools/contracts" ;
7- import { resolveEnvironmentMachineKind } from "@t3tools/contracts" ;
7+ import {
8+ MAX_WEBHOOK_DELIVERY_AGE_MINUTES ,
9+ resolveEnvironmentMachineKind ,
10+ } from "@t3tools/contracts" ;
811import type { MenuAction } from "@react-native-menu/menu" ;
912import { DateTimePicker } from "@expo/ui/community/datetime-picker" ;
1013import {
1114 isAtomCommandInterrupted ,
1215 squashAtomCommandFailure ,
1316 type AtomCommandResult ,
1417} from "@t3tools/client-runtime/state/runtime" ;
18+ import {
19+ DEFAULT_WEBHOOK_PROMPT ,
20+ parseMaxDeliveryAge ,
21+ } from "@t3tools/client-runtime/scheduled-task-webhook" ;
1522import {
1623 useCallback ,
1724 useEffect ,
@@ -37,6 +44,9 @@ import type { ComposerEditorSelection } from "../../components/ComposerEditor";
3744import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView" ;
3845import { SegmentedControl } from "../../components/SegmentedControl" ;
3946import { ThemedSwitch } from "../../components/ThemedSwitch" ;
47+ import { webhookAddress } from "@t3tools/client-runtime/webhook-address" ;
48+ import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic" ;
49+ import { usePreparedConnection } from "../../state/session" ;
4050import { buildModelOptions } from "../../lib/modelOptions" ;
4151import { NativeStackScreenOptions } from "../../native/StackHeader" ;
4252import { useProjects , useEnvironmentServerConfig } from "../../state/entities" ;
@@ -87,6 +97,7 @@ const DAYS = [
8797] as const ;
8898
8999function describeSchedule ( task : ScheduledTask ) : string {
100+ if ( task . schedule . type === "webhook" ) return "On webhook" ;
90101 if ( task . schedule . type === "interval" ) return formatScheduledTaskInterval ( task . schedule . everyMs ) ;
91102 const days = task . schedule . weekdays ?. length ? repeatLabel ( task . schedule . weekdays ) : "Every day" ;
92103 return `${ days } at ${ formatTime ( task . schedule . timeOfDay ) } ` ;
@@ -127,7 +138,7 @@ function FormField(props: {
127138 readonly label : string ;
128139 readonly value : string ;
129140 readonly onChange : ( value : string ) => void ;
130- readonly keyboardType ?: "decimal-pad" ;
141+ readonly keyboardType ?: "decimal-pad" | "number-pad" ;
131142 readonly disabled ?: boolean ;
132143 readonly placeholder ?: string ;
133144 readonly borderTop ?: boolean ;
@@ -598,7 +609,24 @@ function TaskForm({
598609 environmentUnavailable
599610 )
600611 return ;
601- const schedule = scheduleFromDraft ( draft . schedule ) ;
612+ // Signatures are edited on desktop and web; send the task's current one,
613+ // not the copy taken when this form opened, so a newer edit survives.
614+ const liveTask = tasks . data ?. tasks . find ( ( task ) => task . id === draft . task ?. id ) ;
615+ const schedule = scheduleFromDraft (
616+ draft . schedule . mode === "webhook" && liveTask ?. schedule . type === "webhook"
617+ ? { ...draft . schedule , signature : liveTask . schedule . signature }
618+ : draft . schedule ,
619+ ) ;
620+ if (
621+ draft . schedule . mode === "webhook" &&
622+ parseMaxDeliveryAge ( draft . schedule . maxDeliveryAgeMinutes ) === undefined
623+ ) {
624+ Alert . alert (
625+ "Invalid age limit" ,
626+ `Enter whole minutes from 1 to ${ MAX_WEBHOOK_DELIVERY_AGE_MINUTES } , or leave it blank.` ,
627+ ) ;
628+ return ;
629+ }
602630 if (
603631 ! draft . title . trim ( ) ||
604632 ! draft . prompt . trim ( ) ||
@@ -810,12 +838,20 @@ function TaskForm({
810838 < SegmentedControl
811839 options = { [
812840 { value : "fixed_time" , label : "At a time" } ,
813- { value : "interval" , label : "Every interval" } ,
841+ { value : "interval" , label : "Interval" } ,
842+ { value : "webhook" , label : "On webhook" } ,
814843 ] }
815844 selected = { draft . schedule . mode }
816845 onSelect = { ( mode ) => {
817846 setTimePickerOpen ( false ) ;
818- setDraft ( { ...draft , schedule : { ...draft . schedule , mode } } ) ;
847+ setDraft ( {
848+ ...draft ,
849+ prompt :
850+ mode === "webhook" && ! draft . prompt . trim ( )
851+ ? DEFAULT_WEBHOOK_PROMPT
852+ : draft . prompt ,
853+ schedule : { ...draft . schedule , mode } ,
854+ } ) ;
819855 } }
820856 />
821857 </ View >
@@ -889,6 +925,32 @@ function TaskForm({
889925 } }
890926 />
891927 </ >
928+ ) : draft . schedule . mode === "webhook" ? (
929+ < >
930+ < WebhookScheduleDetails
931+ environmentId = { environmentId }
932+ // The live row, so a rotated URL shows up without reopening the form.
933+ // Once the list has loaded, a missing task is gone; don't keep showing its URL.
934+ task = {
935+ tasks . data
936+ ? ( tasks . data . tasks . find ( ( task ) => task . id === draft . task ?. id ) ?? null )
937+ : draft . task
938+ }
939+ signatureConfigured = { draft . schedule . signature !== null }
940+ disabled = { saving || environmentUnavailable }
941+ />
942+ < FormField
943+ label = "Skip requests older than (minutes)"
944+ value = { draft . schedule . maxDeliveryAgeMinutes }
945+ placeholder = "Run every request"
946+ keyboardType = "number-pad"
947+ disabled = { saving }
948+ borderTop
949+ onChange = { ( maxDeliveryAgeMinutes ) =>
950+ setDraft ( { ...draft , schedule : { ...draft . schedule , maxDeliveryAgeMinutes } } )
951+ }
952+ />
953+ </ >
892954 ) : (
893955 < >
894956 < FormField
@@ -944,6 +1006,100 @@ function TaskForm({
9441006 ) ;
9451007}
9461008
1009+ function WebhookScheduleDetails ( {
1010+ environmentId,
1011+ task,
1012+ signatureConfigured,
1013+ disabled,
1014+ } : {
1015+ readonly environmentId : EnvironmentId ;
1016+ readonly task : ScheduledTask | null ;
1017+ readonly signatureConfigured : boolean ;
1018+ readonly disabled : boolean ;
1019+ } ) {
1020+ const [ rotating , setRotating ] = useState ( false ) ;
1021+ const rotate = useAtomCommand ( serverEnvironment . rotateScheduledTaskWebhookToken , {
1022+ label : "scheduled task rotate webhook token" ,
1023+ reportFailure : false ,
1024+ } ) ;
1025+ const preparedConnection = usePreparedConnection ( environmentId ) ;
1026+ const httpBaseUrl =
1027+ preparedConnection . _tag === "Some" ? preparedConnection . value . httpBaseUrl : null ;
1028+ const webhook = task ?. schedule . type === "webhook" ? task . webhook : undefined ;
1029+ // Without T3 Connect, the path is resolved on the address this phone uses.
1030+ const resolved = webhook ? webhookAddress ( webhook , httpBaseUrl ) : null ;
1031+ return (
1032+ < View className = "gap-2 border-t border-border-subtle px-4 py-3" >
1033+ < Text className = "text-sm text-foreground-muted" >
1034+ {
1035+ "The prompt can use {{body.a.b}}, {{headers.name}}, {{query.name}}, {{body}} and {{request}}. The filled-in prompt is all the agent sees."
1036+ }
1037+ </ Text >
1038+ { task === null || resolved === null ? (
1039+ < Text className = "text-sm text-foreground-muted" > Save the task to get its webhook URL.</ Text >
1040+ ) : (
1041+ < >
1042+ < Pressable
1043+ accessibilityRole = "button"
1044+ accessibilityLabel = "Copy webhook URL"
1045+ accessibilityHint = "Copies the URL to the clipboard"
1046+ // A bare path is not something a sender can call, so only full URLs copy.
1047+ disabled = { ! resolved . copyable }
1048+ onPress = { ( ) => void tryCopyTextWithHaptic ( resolved . address ) }
1049+ className = "gap-1 active:opacity-70"
1050+ >
1051+ < Text className = "text-lg text-foreground" >
1052+ { resolved . copyable ? "Webhook URL" : "Webhook path" }
1053+ </ Text >
1054+ < Text className = "text-sm text-foreground-muted" numberOfLines = { 2 } selectable >
1055+ { resolved . address }
1056+ </ Text >
1057+ </ Pressable >
1058+ { resolved . note !== null ? (
1059+ < Text className = "text-sm text-foreground-muted" > { resolved . note } </ Text >
1060+ ) : null }
1061+ < Pressable
1062+ accessibilityRole = "button"
1063+ accessibilityState = { { disabled : disabled || rotating } }
1064+ disabled = { disabled || rotating }
1065+ onPress = { ( ) =>
1066+ Alert . alert ( "Rotate URL?" , "The current URL stops working immediately." , [
1067+ { text : "Cancel" , style : "cancel" } ,
1068+ {
1069+ text : "Rotate" ,
1070+ style : "destructive" ,
1071+ onPress : ( ) => {
1072+ setRotating ( true ) ;
1073+ void rotate ( { environmentId, input : { id : task . id } } ) . then ( ( result ) => {
1074+ setRotating ( false ) ;
1075+ if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
1076+ Alert . alert (
1077+ "Could not rotate URL" ,
1078+ String ( squashAtomCommandFailure ( result ) ) ,
1079+ ) ;
1080+ }
1081+ } ) ;
1082+ } ,
1083+ } ,
1084+ ] )
1085+ }
1086+ className = "min-h-11 justify-center active:opacity-70 disabled:opacity-50"
1087+ >
1088+ < Text className = "text-base text-danger-foreground" >
1089+ { rotating ? "Rotating…" : "Rotate URL" }
1090+ </ Text >
1091+ </ Pressable >
1092+ </ >
1093+ ) }
1094+ { signatureConfigured ? (
1095+ < Text className = "text-sm text-foreground-muted" >
1096+ Signature check configured on desktop/web.
1097+ </ Text >
1098+ ) : null }
1099+ </ View >
1100+ ) ;
1101+ }
1102+
9471103function EnvironmentTasks ( {
9481104 environment,
9491105 now,
@@ -1050,7 +1206,8 @@ function EnvironmentTasks({
10501206 actions = { [
10511207 { id : "edit" , title : "Edit" } ,
10521208 { id : "toggle" , title : task . enabled ? "Pause" : "Resume" } ,
1053- { id : "run" , title : "Run now" } ,
1209+ // A webhook task has no request to run without.
1210+ ...( task . schedule . type === "webhook" ? [ ] : [ { id : "run" , title : "Run now" } ] ) ,
10541211 { id : "delete" , title : "Delete" , attributes : { destructive : true } } ,
10551212 ] }
10561213 onPressAction = { ( { nativeEvent } ) => {
0 commit comments