| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "reasonix/internal/browser" |
| 7 | "reasonix/internal/browser/cdp" |
| 8 | "reasonix/internal/config" |
| 9 | ) |
| 10 | |
| 11 | // browserLaunchTimeout bounds the first attach: a browser that has not shown a |
| 12 | // DevTools endpoint by then is reported to the model instead of hanging a turn. |
| 13 | const browserLaunchTimeout = 45 * time.Second |
| 14 | |
| 15 | // browserBackend decides which browser a session drives, and returns the |
| 16 | // shutdown the controller owes it. A host that already brought one — the |
| 17 | // desktop shell's WebContentsViews or Serve's SSH broker — keeps it and stays |
| 18 | // responsible for its lifetime. Otherwise a configured CDP backend gives CLI, |
| 19 | // Serve, and headless sessions the same browser_* tools; nothing is launched |
| 20 | // until the first browser tool call. |
| 21 | // uploadRoots are the session's write roots: a page is untrusted, so a file |
| 22 | // input may only receive files this task already owns. |
| 23 | func browserBackend(host browser.Executor, cfg config.BrowserConfig, uploadRoots []string) (browser.Executor, func()) { |
| 24 | if host != nil { |
| 25 | return host, nil |
| 26 | } |
| 27 | if !cfg.Enabled { |
| 28 | return nil, nil |
| 29 | } |
| 30 | lazy := cdp.NewLazy(cdp.Options{ |
| 31 | Endpoint: cfg.Endpoint, |
| 32 | AllowRemoteEndpoint: cfg.AllowRemoteEndpoint, |
| 33 | ChromePath: cfg.ChromePath, |
| 34 | ChromeArgs: cfg.ChromeArgs, |
| 35 | UserDataDir: cfg.UserDataDir, |
| 36 | Headless: cfg.Headless, |
| 37 | LaunchTimeout: browserLaunchTimeout, |
| 38 | UploadRoots: uploadRoots, |
| 39 | }) |
| 40 | return lazy, lazy.Shutdown |
| 41 | } |
| 42 |