Pattern · Agents
NewAgent Run SwiftUI Block
A long-running agent task live: state, elapsed time, current activity, steps, tokens and cost, Pause and Stop, then its result.
The status section for an agent working in the background. It shows the state, a ticking clock, what the agent is doing now, a segmented step bar and the tokens and cost so far, with Pause, Resume and a confirmed Stop, and Notify me when done through the system permission. It settles into a summary with the outputs to open, or the reason and Retry when it fails.
- SwiftUI
- iOS 17.0+
- agent-run
$199 once, every piece included. See how installing works · Sign in
01
What's included
SwiftUI source you own, built on Apple frameworks only.
- Queued, Running, Paused, Needs input, Finished and Failed in one tinted chip
- Elapsed time ticking in the brand's figures, with the activity changing in place
- A segmented step bar and tokens and cost so far
- Pause, Resume and Stop with a confirmation, and Notify me when done
- Settles into a summary with outputs and Open, or the reason with Retry
02
Implementation notes
How it behaves, what it handles, and the decisions behind it.
A surface card shows the run's state as a tinted chip (Queued, Running, Paused, Needs input, Finished or Failed) with the step it's on, the task's title, and the elapsed time in the brand's figures, ticking each second while it runs and holding while paused. Under it, the agent's current activity changes in place beside a small spinner, a segmented bar lights a segment per finished step with the running one half lit, and a hairline separates the tokens and cost used so far. Pause and Resume run your closures with progress in the button, and Stop asks "Stop this run?" before it runs yours. Notify me when done asks for notification permission the first time it is turned on and, when notifications are off, says so with Open Settings. When the agent needs an answer, its question replaces the activity with Respond. On finish the block settles into the result: the time, tokens and cost, a short summary, the outputs with Open, and the primary action opening the first. On failure the reason shows with Retry. nil shows grey shapes in the block's own layout.
Notes
AgentRunBlock(run:notify:onPause:onResume:onStop:onRetry:onRespond:onOpen:)takes oneRun. It is a section: no navigation stack, background, padding or scroll view of its own, so place it in your own column.AgentRunBlock.Runhas atitle, aState, the currentactivity, thestepfrom 1 and thestepCount, theelapsedseconds banked beforeresumedAt, optionaltokensandcost(withcurrencyCode), and thesummaryandoutputsit shows when finished.elapsed(at:)gives the running total;Run.sampleis the expenses run from the preview.Run.Stateis.queued(position:),.running,.paused,.needsInput(question),.finishedor.failed(reason). Stopping is a failure with your reason ("You stopped the run on step 5.").Run.Outputhas a title, an optional detail, a symbol and an optionalurl. Open usesopenURLunless you passonOpen; an output with neither shows no Open.- Pass a new
Runwhenever the agent reports progress. KeepresumedAtset while running so the clock ticks on its own between updates. onPauseandonResumeshow Pause and Resume;onStopshows Stop, behind a confirmation;onRetryshows Retry on failure;onRespondshows Respond when the agent needs input. Leaving one out hides its button. A thrown error shows itserrorDescriptionunder the buttons;CancellationError()goes back quietly.notifybinds Notify me when done; nil hides the row. Turning it on asksUNUserNotificationCenterfor permission when it hasn't been asked, and stays off if it's refused. The block checks again when the app becomes active. Schedule the notification yourself when the run finishes.- Haptics mark success when the run finishes, a warning when it needs input and an error when it fails.
- VoiceOver reads the state chip, the elapsed time in words, the activity as "Now: …", the steps as "4 of 9 done", and announces pause, resume and stop. Pause and Stop stack at accessibility sizes.
- Reduce Motion turns every change into a short fade.
AgentRunBlockExampleat the end ofAgentRunBlock.swiftis the screen in the preview: a run reconciling a month of expenses that advances a step every 1.6 seconds, pauses and resumes, stops after confirming, and finishes with three outputs. PasswrapsInNavigationStack: falseto push it in your own stack.
Theming
- Reads
SPThemefrom@Environment(\.spTheme)and requires thedesign-systemitem. - Cards are
color.surfacewithradius.lg, 16 point padding andSPHairlinedividers inseparator; groups sit 24 points apart. - The elapsed time is the one display moment: the brand's face (
type.face) at 34 points with monospaced digits,type.numeralWeightcapped at semibold, intype.figureDesignwhen the face isn't bundled. The title istype.headline, the activitytype.subhead, figurestype.subheadwith monospaced digits, labelstype.caption. - The chip is
infowhile running,warningwhen it needs input,successwhen finished anddangerwhen failed, each on a 14% tint, ortextSecondaryonfillwhen queued or paused. Step segments aretextPrimaryonfill. - Pause and Resume are the secondary button on
fill; Stop is onfillwithdangertext. Respond, Retry and Open are theaccentwithaccentOntext, one at a time. - Changes animate with
motion.smoothand buttons withmotion.snappy; every animation becomes a short fade under Reduce Motion.
Requirements
- Notify me when done uses
UNUserNotificationCenter; no capability or Info.plist key is needed to ask.
03
Keep building
Pieces that work well next to this one.