| 433 | } |
| 434 | |
| 435 | func (p *Server) newAdvisorRuntime( |
| 436 | ctx context.Context, |
| 437 | chat database.Chat, |
| 438 | advisorCfg codersdk.AdvisorConfig, |
| 439 | fallbackModel fantasy.LanguageModel, |
| 440 | fallbackCallConfig codersdk.ChatModelCallConfig, |
| 441 | providerKeys chatprovider.ProviderAPIKeys, |
| 442 | modelOpts modelBuildOptions, |
| 443 | logger slog.Logger, |
| 444 | ) (*chatadvisor.Runtime, error) { |
| 445 | advisorModel, advisorCallConfig, err := p.resolveAdvisorModelOverride( |
| 446 | ctx, |
| 447 | chat, |
| 448 | advisorCfg, |
| 449 | fallbackModel, |
| 450 | fallbackCallConfig, |
| 451 | providerKeys, |
| 452 | modelOpts, |
| 453 | logger, |
| 454 | ) |
| 455 | if err != nil { |
| 456 | return nil, err |
| 457 | } |
| 458 | |
| 459 | maxUsesPerRun := advisorCfg.MaxUsesPerRun |
| 460 | switch { |
| 461 | case maxUsesPerRun == 0: |
| 462 | // Advisor config treats 0 as unlimited, but the runtime |
| 463 | // requires a positive bound. maxChatSteps is the |
| 464 | // effective upper bound because advisor can run at most |
| 465 | // once per loop step. |
| 466 | maxUsesPerRun = maxChatSteps |
| 467 | case maxUsesPerRun < 0: |
| 468 | logger.Warn( |
| 469 | ctx, |
| 470 | "invalid advisor max uses per run, continuing without advisor", |
| 471 | slog.F("max_uses_per_run", maxUsesPerRun), |
| 472 | ) |
| 473 | return nil, nil //nolint:nilnil // Nil runtime with nil error means advisor is skipped for this turn. |
| 474 | } |
| 475 | |
| 476 | maxOutputTokens := advisorCfg.MaxOutputTokens |
| 477 | if maxOutputTokens <= 0 { |
| 478 | maxOutputTokens = defaultAdvisorMaxOutputTokens |
| 479 | } |
| 480 | |
| 481 | advisorCallConfig.MaxOutputTokens = ptr.Ref(maxOutputTokens) |
| 482 | providerOptions := chatprovider.ProviderOptionsFromChatModelConfig( |
| 483 | advisorModel, |
| 484 | advisorCallConfig.ProviderOptions, |
| 485 | ) |
| 486 | |
| 487 | rt, err := chatadvisor.NewRuntime(chatadvisor.RuntimeConfig{ |
| 488 | Model: advisorModel, |
| 489 | ModelConfig: advisorCallConfig, |
| 490 | ProviderOptions: providerOptions, |
| 491 | MaxUsesPerRun: maxUsesPerRun, |
| 492 | MaxOutputTokens: maxOutputTokens, |