AdvisorConfig returns the deployment-wide advisor configuration. The underlying site-config row changes on the order of hours or days, so this cache saves a per-turn DB round trip on chats that reference the advisor. Parse errors and lookup errors are surfaced to the caller; callers that prefer sile
(ctx context.Context)
| 445 | // advisor. Parse errors and lookup errors are surfaced to the caller; |
| 446 | // callers that prefer silent fallback handle that at the call site. |
| 447 | func (c *chatConfigCache) AdvisorConfig(ctx context.Context) (codersdk.AdvisorConfig, error) { |
| 448 | if config, ok := c.cachedAdvisorConfig(); ok { |
| 449 | return config, nil |
| 450 | } |
| 451 | |
| 452 | generation := c.advisorConfigGenerationSnapshot() |
| 453 | config, err := singleflightDoChan( |
| 454 | ctx, |
| 455 | &c.advisorConfigFetches, |
| 456 | fmt.Sprintf("%d:advisor", generation), |
| 457 | func() (codersdk.AdvisorConfig, error) { |
| 458 | if cached, ok := c.cachedAdvisorConfig(); ok { |
| 459 | return cached, nil |
| 460 | } |
| 461 | |
| 462 | raw, err := c.db.GetChatAdvisorConfig(c.ctx) |
| 463 | if err != nil { |
| 464 | return codersdk.AdvisorConfig{}, err |
| 465 | } |
| 466 | var cfg codersdk.AdvisorConfig |
| 467 | if err := json.Unmarshal([]byte(raw), &cfg); err != nil { |
| 468 | return codersdk.AdvisorConfig{}, err |
| 469 | } |
| 470 | c.storeAdvisorConfig(generation, cfg) |
| 471 | return cfg, nil |
| 472 | }, |
| 473 | ) |
| 474 | if err != nil { |
| 475 | return codersdk.AdvisorConfig{}, err |
| 476 | } |
| 477 | return config, nil |
| 478 | } |
| 479 | |
| 480 | func (c *chatConfigCache) cachedAdvisorConfig() (codersdk.AdvisorConfig, bool) { |
| 481 | c.mu.RLock() |