Start starts the CLI server (if not using an external server) and establishes a connection. If connecting to an external server (via URIConnection), only establishes the connection. Otherwise, spawns the CLI server process and then connects. This method is called automatically when creating a sess
(ctx context.Context)
| 342 | // } |
| 343 | // // Now ready to create sessions |
| 344 | func (c *Client) Start(ctx context.Context) error { |
| 345 | c.startStopMux.Lock() |
| 346 | defer c.startStopMux.Unlock() |
| 347 | |
| 348 | if c.state == stateConnected { |
| 349 | return nil |
| 350 | } |
| 351 | |
| 352 | c.state = stateConnecting |
| 353 | |
| 354 | // Only start CLI server process if not connecting to external server |
| 355 | if !c.isExternalServer { |
| 356 | if err := c.startCLIServer(ctx); err != nil { |
| 357 | c.process = nil |
| 358 | c.state = stateError |
| 359 | return err |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Connect to the server |
| 364 | if err := c.connectToServer(ctx); err != nil { |
| 365 | killErr := c.killProcess() |
| 366 | c.state = stateError |
| 367 | return errors.Join(err, killErr) |
| 368 | } |
| 369 | |
| 370 | // Verify protocol version compatibility |
| 371 | if err := c.verifyProtocolVersion(ctx); err != nil { |
| 372 | killErr := c.killProcess() |
| 373 | c.state = stateError |
| 374 | return errors.Join(err, killErr) |
| 375 | } |
| 376 | |
| 377 | // If a session filesystem provider was configured, register it. |
| 378 | if c.options.SessionFS != nil { |
| 379 | req := &rpc.SessionFSSetProviderRequest{ |
| 380 | InitialCwd: c.options.SessionFS.InitialWorkingDirectory, |
| 381 | SessionStatePath: c.options.SessionFS.SessionStatePath, |
| 382 | Conventions: c.options.SessionFS.Conventions, |
| 383 | } |
| 384 | if c.options.SessionFS.Capabilities != nil { |
| 385 | sqlite := c.options.SessionFS.Capabilities.Sqlite |
| 386 | req.Capabilities = &rpc.SessionFSSetProviderCapabilities{ |
| 387 | Sqlite: &sqlite, |
| 388 | } |
| 389 | } |
| 390 | _, err := c.RPC.SessionFS.SetProvider(ctx, req) |
| 391 | if err != nil { |
| 392 | killErr := c.killProcess() |
| 393 | c.state = stateError |
| 394 | return errors.Join(err, killErr) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // If a request handler was configured, register as the inference provider. |
| 399 | if c.options.RequestHandler != nil { |
| 400 | if _, err := c.RPC.LlmInference.SetProvider(ctx); err != nil { |
| 401 | killErr := c.killProcess() |