| 19 | var agentSessionURLRegexp = regexp.MustCompile(fmt.Sprintf("^/agent-sessions/(%s)$", uuidPattern)) |
| 20 | |
| 21 | func CapiClientFunc(f *cmdutil.Factory) func() (capi.CapiClient, error) { |
| 22 | return func() (capi.CapiClient, error) { |
| 23 | cfg, err := f.Config() |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | |
| 28 | httpClient, err := f.HttpClient() |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | authCfg := cfg.Authentication() |
| 34 | host, _ := authCfg.DefaultHost() |
| 35 | token, _ := authCfg.ActiveToken(host) |
| 36 | |
| 37 | cachedClient := api.NewCachedHTTPClient(httpClient, time.Minute*10) |
| 38 | capiBaseURL, err := resolveCapiURL(cachedClient, host) |
| 39 | if err != nil { |
| 40 | return nil, fmt.Errorf("failed to resolve Copilot API URL: %w", err) |
| 41 | } |
| 42 | |
| 43 | return capi.NewCAPIClient(httpClient, token, host, capiBaseURL), nil |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // resolveCapiURL queries the GitHub API for the Copilot API endpoint URL. |
| 48 | func resolveCapiURL(httpClient *http.Client, host string) (string, error) { |