@@ -47,8 +47,8 @@ func init() {
4747 f .Bool ("include-cost" , false , "Include cost info in response" )
4848 f .String ("preset" , "" , "Named preset to apply" )
4949 f .Bool ("dry-run" , false , "Print the API request without executing" )
50- f .Int ("poll-interval" , 5 , "Polling interval in seconds for async results" )
51- f .Int ("timeout" , 600 , "Maximum wait time in seconds for video generation" )
50+ f .Duration ("poll-interval" , defaultPollInterval , "Polling interval for async results" )
51+ f .Duration ("timeout" , defaultVideoGenerationTimeout , "Maximum wait time for video generation" )
5252 f .Duration ("download-timeout" , defaultVideoDownloadTimeout , "timeout to use when downloading video inference results" )
5353
5454 videoInferenceCmd .RegisterFlagCompletionFunc ("output-format" , func (cmd * cobra.Command , args []string , toComplete string ) ([]cobra.Completion , cobra.ShellCompDirective ) { //nolint:errcheck,gosec
@@ -119,8 +119,8 @@ func runVideoInference(cmd *cobra.Command, args []string) error {
119119 dryRun , _ := cmd .Flags ().GetBool ("dry-run" )
120120 sourcePath , _ := cmd .Flags ().GetString ("source" )
121121 sourceLastPath , _ := cmd .Flags ().GetString ("source-last" )
122- pollInterval , _ := cmd .Flags ().GetInt ("poll-interval" )
123- timeout , _ := cmd .Flags ().GetInt ("timeout" )
122+ pollInterval , _ := cmd .Flags ().GetDuration ("poll-interval" )
123+ timeout , _ := cmd .Flags ().GetDuration ("timeout" )
124124 downloadTimeout , _ := cmd .Flags ().GetDuration ("download-timeout" )
125125
126126 // Build request
@@ -192,12 +192,12 @@ func runVideoInference(cmd *cobra.Command, args []string) error {
192192 // Submit the video generation task
193193 s := output .NewSpinner (" Submitting video generation task..." )
194194 s .Start ()
195- defer s .Stop ()
196195
197196 client := api .NewClient (key , config .GetBaseURL (), flagVerbose )
198197
199198 submitResults , err := client .VideoInference (context .Background (), req )
200199 if err != nil {
200+ s .Stop ()
201201 if api .IsAuthError (err ) {
202202 output .Error ("Authentication failed. Run 'runware auth login' to set your API key." )
203203 return err
@@ -213,48 +213,39 @@ func runVideoInference(cmd *cobra.Command, args []string) error {
213213 taskUUID = submitResults [0 ].TaskUUID
214214 }
215215
216- deadline := time .Now ().Add (time .Duration (timeout ) * time .Second )
217- interval := time .Duration (pollInterval ) * time .Second
218- var results []api.VideoInferenceResult
216+ pollCtx , cancel := context .WithTimeout (cmd .Context (), timeout )
217+ defer cancel ()
219218
220- for time .Now ().Before (deadline ) {
221- rawData , err := client .GetResponse (context .Background (), taskUUID )
222- if err != nil {
223- // getResponse may return an error if results aren't ready yet — keep polling
224- if flagVerbose {
225- fmt .Fprintf (os .Stderr , "Poll: %s\n " , err ) //nolint:errcheck,gosec
226- }
227- time .Sleep (interval )
228- continue
229- }
230-
231- // Parse results
232- for _ , raw := range rawData {
219+ results , err := api .PollResults (
220+ pollCtx ,
221+ client ,
222+ taskUUID ,
223+ pollInterval ,
224+ flagVerbose ,
225+ func (raw json.RawMessage ) (api.VideoInferenceResult , bool ) {
233226 var r api.VideoInferenceResult
234227 if err := json .Unmarshal (raw , & r ); err != nil {
235- continue
236- }
237- // Check if we have a video URL (indicates completion)
238- if r .VideoURL != "" || r .MediaURL != "" {
239- results = append (results , r )
228+ return r , false
240229 }
241- }
242-
243- if len ( results ) > 0 {
244- break
245- }
246-
247- time . Sleep ( interval )
230+ return r , r . VideoURL != "" || r . MediaURL != ""
231+ },
232+ )
233+ if err != nil {
234+ s . Stop ()
235+ output . Error ( "Video generation failed" )
236+ return err
248237 }
249238
250239 if len (results ) == 0 {
240+ s .Stop ()
251241 output .Error ("Video generation timed out or returned no results" )
252- return fmt .Errorf ("no video results after %ds " , timeout )
242+ return fmt .Errorf ("no video results after %v " , timeout )
253243 }
254244
255245 // JSON/YAML output
256246 format := output .ParseFormat (getFormat ())
257247 if format != output .FormatTable {
248+ s .Stop ()
258249 return output .Print (format , results , nil , nil )
259250 }
260251
@@ -277,9 +268,7 @@ func runVideoInference(cmd *cobra.Command, args []string) error {
277268 headers = append (headers , tableHeaderCost )
278269 }
279270
280- // Manually stop the spinner to ensure clean output of results.
281271 s .Stop ()
282-
283272 return output .Print (format , results , headers , rows )
284273 }
285274
@@ -328,7 +317,6 @@ func runVideoInference(cmd *cobra.Command, args []string) error {
328317 return fmt .Errorf ("all %d video downloads failed" , len (results ))
329318 }
330319
331- // Manually stop the spinner to ensure clean output of results.
332320 s .Stop ()
333321
334322 if err := output .Print (format , results , headers , rows ); err != nil {
0 commit comments