@@ -163,6 +163,53 @@ func ensureStandaloneRunnerAvailable(ctx context.Context, printer standalone.Sta
163163 return inspectStandaloneRunner (container ), nil
164164}
165165
166+ // withStandaloneRunner wraps a command's RunE to ensure the standalone runner
167+ // is available before executing the command. This is a no-op in unsupported
168+ // contexts (e.g., Docker Desktop) or if automatic installations have been disabled.
169+ func withStandaloneRunner (cmd * cobra.Command ) * cobra.Command {
170+ if cmd .RunE == nil {
171+ return cmd
172+ }
173+ originalRunE := cmd .RunE
174+ cmd .RunE = func (cmd * cobra.Command , args []string ) error {
175+ if _ , err := ensureStandaloneRunnerAvailable (cmd .Context (), asPrinter (cmd ), false ); err != nil {
176+ return fmt .Errorf ("unable to initialize standalone model runner: %w" , err )
177+ }
178+ return originalRunE (cmd , args )
179+ }
180+ return cmd
181+ }
182+
183+ // getStandaloneRunner returns the standalone runner info by finding the controller container.
184+ // This is useful for commands that need runner details after withStandaloneRunner has run.
185+ // Returns nil for non-standalone contexts (e.g., Docker Desktop).
186+ func getStandaloneRunner (ctx context.Context ) (* standaloneRunner , error ) {
187+ // Only standalone contexts have a runner container to inspect.
188+ engineKind := modelRunner .EngineKind ()
189+ standaloneSupported := engineKind == types .ModelRunnerEngineKindMoby ||
190+ engineKind == types .ModelRunnerEngineKindCloud
191+ if ! standaloneSupported {
192+ return nil , nil
193+ }
194+
195+ if dockerCLI == nil {
196+ return nil , nil
197+ }
198+
199+ dockerClient , err := desktop .DockerClientForContext (dockerCLI , dockerCLI .CurrentContext ())
200+ if err != nil {
201+ return nil , fmt .Errorf ("failed to create Docker client: %w" , err )
202+ }
203+ containerID , _ , ctr , err := standalone .FindControllerContainer (ctx , dockerClient )
204+ if err != nil {
205+ return nil , fmt .Errorf ("unable to find standalone model runner: %w" , err )
206+ }
207+ if containerID == "" {
208+ return nil , nil
209+ }
210+ return inspectStandaloneRunner (ctr ), nil
211+ }
212+
166213// runnerOptions holds common configuration for install/start/reinstall commands
167214type runnerOptions struct {
168215 port uint16
0 commit comments