@@ -73,6 +73,7 @@ func LookupBackgroundModel(r api.Registry, name string) BackgroundModel {
7373 if action == nil {
7474 return nil
7575 }
76+
7677 return & backgroundModel {* action }
7778}
7879
@@ -141,28 +142,7 @@ func NewBackgroundModel(name string, opts *BackgroundModelOptions, startFn Start
141142 return nil , err
142143 }
143144
144- if resp .Operation == nil {
145- return nil , core .NewError (core .FAILED_PRECONDITION , "background model did not produce an operation" )
146- }
147-
148- op := & ModelOperation {
149- Action : resp .Operation .Action ,
150- ID : resp .Operation .Id ,
151- Done : resp .Operation .Done ,
152- Metadata : resp .Operation .Metadata ,
153- }
154-
155- if resp .Operation .Error != nil {
156- op .Error = errors .New (resp .Operation .Error .Message )
157- }
158-
159- if resp .Operation .Output != nil {
160- if modelResp , ok := resp .Operation .Output .(* ModelResponse ); ok {
161- op .Output = modelResp
162- }
163- }
164-
165- return op , nil
145+ return modelOpFromResponse (resp )
166146 }
167147
168148 return & backgroundModel {* core .NewBackgroundAction (name , api .ActionTypeBackgroundModel , metadata , wrappedFn , checkFn , opts .Cancel )}
@@ -182,30 +162,7 @@ func GenerateOperation(ctx context.Context, r *registry.Registry, opts ...Genera
182162 return nil , err
183163 }
184164
185- if resp .Operation == nil {
186- return nil , core .NewError (core .FAILED_PRECONDITION , "model did not return an operation" )
187- }
188-
189- op := & ModelOperation {
190- Action : resp .Operation .Action ,
191- ID : resp .Operation .Id ,
192- Done : resp .Operation .Done ,
193- Metadata : resp .Operation .Metadata ,
194- }
195-
196- if resp .Operation .Error != nil {
197- op .Error = errors .New (resp .Operation .Error .Message )
198- }
199-
200- if op .Done {
201- if modelResp , ok := resp .Operation .Output .(* ModelResponse ); ok {
202- op .Output = modelResp
203- } else {
204- return nil , core .NewError (core .INTERNAL , "operation output is not a model response" )
205- }
206- }
207-
208- return op , nil
165+ return modelOpFromResponse (resp )
209166}
210167
211168// CheckModelOperation checks the status of a background model operation by looking up the model and calling its Check method.
@@ -220,6 +177,7 @@ func backgroundModelToModelFn(startFn StartModelOpFunc) ModelFunc {
220177 if err != nil {
221178 return nil , err
222179 }
180+
223181 return & ModelResponse {
224182 Operation : & Operation {
225183 Action : op .Action ,
@@ -233,3 +191,31 @@ func backgroundModelToModelFn(startFn StartModelOpFunc) ModelFunc {
233191 }, nil
234192 }
235193}
194+
195+ // modelOpFromResponse extracts a [ModelOperation] from a [ModelResponse].
196+ func modelOpFromResponse (resp * ModelResponse ) (* ModelOperation , error ) {
197+ if resp .Operation == nil {
198+ return nil , core .NewError (core .FAILED_PRECONDITION , "background model did not return an operation" )
199+ }
200+
201+ op := & ModelOperation {
202+ Action : resp .Operation .Action ,
203+ ID : resp .Operation .Id ,
204+ Done : resp .Operation .Done ,
205+ Metadata : resp .Operation .Metadata ,
206+ }
207+
208+ if resp .Operation .Error != nil {
209+ op .Error = errors .New (resp .Operation .Error .Message )
210+ }
211+
212+ if op .Done && resp .Operation .Output != nil {
213+ if modelResp , ok := resp .Operation .Output .(* ModelResponse ); ok {
214+ op .Output = modelResp
215+ } else {
216+ return nil , core .NewError (core .INTERNAL , "operation output is not a model response" )
217+ }
218+ }
219+
220+ return op , nil
221+ }
0 commit comments