NewClient creates a new Copilot runtime client with the given options. If options is nil, default options are used (spawns the bundled runtime over stdio). The client is not connected after creation; call [Client.Start] to connect, or simply call [Client.CreateSession]/[Client.ResumeSession], which
(options *ClientOptions)
| 178 | // Connection: copilot.URIConnection{URL: "localhost:8080"}, |
| 179 | // }) |
| 180 | func NewClient(options *ClientOptions) *Client { |
| 181 | opts := ClientOptions{} |
| 182 | |
| 183 | client := &Client{ |
| 184 | options: opts, |
| 185 | state: stateDisconnected, |
| 186 | sessions: make(map[string]*Session), |
| 187 | actualHost: "localhost", |
| 188 | isExternalServer: false, |
| 189 | useStdio: true, |
| 190 | } |
| 191 | |
| 192 | if options != nil { |
| 193 | opts = *options |
| 194 | } |
| 195 | |
| 196 | // Resolve the connection. nil defaults to an empty StdioConnection. |
| 197 | connection := opts.Connection |
| 198 | if connection == nil { |
| 199 | connection = StdioConnection{} |
| 200 | } |
| 201 | switch conn := connection.(type) { |
| 202 | case StdioConnection: |
| 203 | client.useStdio = true |
| 204 | client.cliPath = conn.Path |
| 205 | if len(conn.Args) > 0 { |
| 206 | client.cliArgs = append([]string{}, conn.Args...) |
| 207 | } |
| 208 | case TCPConnection: |
| 209 | client.useStdio = false |
| 210 | client.cliPath = conn.Path |
| 211 | if len(conn.Args) > 0 { |
| 212 | client.cliArgs = append([]string{}, conn.Args...) |
| 213 | } |
| 214 | client.port = conn.Port |
| 215 | client.tcpConnectionToken = conn.ConnectionToken |
| 216 | case URIConnection: |
| 217 | if conn.URL == "" { |
| 218 | panic("URIConnection requires a non-empty URL") |
| 219 | } |
| 220 | host, port := parseCLIURL(conn.URL) |
| 221 | client.actualHost = host |
| 222 | client.actualPort = port |
| 223 | client.isExternalServer = true |
| 224 | client.useStdio = false |
| 225 | client.tcpConnectionToken = conn.ConnectionToken |
| 226 | default: |
| 227 | panic(fmt.Sprintf("unknown RuntimeConnection type: %T", connection)) |
| 228 | } |
| 229 | |
| 230 | // Validate auth options when connecting to an external runtime. |
| 231 | if client.isExternalServer && (opts.GitHubToken != "" || opts.UseLoggedInUser != nil) { |
| 232 | panic("GitHubToken and UseLoggedInUser cannot be used with URIConnection (external runtime manages its own auth)") |
| 233 | } |
| 234 | |
| 235 | // Default Env to current environment if not set |
| 236 | if opts.Env == nil { |
| 237 | opts.Env = os.Environ() |
searching dependent graphs…