| 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 | |
| 36 | cachedClient := api.NewCachedHTTPClient(httpClient, time.Minute*10) |
| 37 | capiBaseURL, err := resolveCapiURL(cachedClient, host) |
| 38 | if err != nil { |
| 39 | return nil, fmt.Errorf("failed to resolve Copilot API URL: %w", err) |
| 40 | } |
| 41 | |
| 42 | // The CAPI client authenticates by setting the token header itself, which bypasses the base HTTP client's |
| 43 | // auto-refresh. Pass authCfg so the CAPI transport can refresh an expiring token on each request. |
| 44 | return capi.NewCAPIClient(httpClient, authCfg, host, capiBaseURL), nil |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // resolveCapiURL queries the GitHub API for the Copilot API endpoint URL. |
| 49 | func resolveCapiURL(httpClient *http.Client, host string) (string, error) { |