getCompactionModelForAgent resolves the dedicated compaction (summary generation) model for an agent, if any. It returns the model named by the `compaction_model` field of the first of the agent's configured models that sets it, or nil when none do. It mirrors getTitleModelForAgent: the value may be
(ctx context.Context, cfg *latest.Config, a *latest.AgentConfig, runConfig *config.RuntimeConfig, providerRegistry *provider.Registry)
| 611 | // sets it, or nil when none do. It mirrors getTitleModelForAgent: the value may |
| 612 | // be a named model from the models section or an inline "provider/model" spec. |
| 613 | func getCompactionModelForAgent(ctx context.Context, cfg *latest.Config, a *latest.AgentConfig, runConfig *config.RuntimeConfig, providerRegistry *provider.Registry) (provider.Provider, error) { |
| 614 | var compactionRef string |
| 615 | for name := range strings.SplitSeq(a.Model, ",") { |
| 616 | if modelCfg, ok := cfg.Models[name]; ok && modelCfg.CompactionModel != "" { |
| 617 | compactionRef = modelCfg.CompactionModel |
| 618 | break |
| 619 | } |
| 620 | } |
| 621 | if compactionRef == "" { |
| 622 | return nil, nil |
| 623 | } |
| 624 | |
| 625 | modelsStore, modelsStoreErr := runConfig.ModelsDevStore() |
| 626 | |
| 627 | modelCfg, exists := cfg.Models[compactionRef] |
| 628 | if !exists { |
| 629 | parsed, err := latest.ParseModelRef(compactionRef) |
| 630 | if err != nil { |
| 631 | return nil, fmt.Errorf("compaction model '%s' not found in configuration and is not a valid provider/model format", compactionRef) |
| 632 | } |
| 633 | modelCfg = parsed |
| 634 | } |
| 635 | modelCfg.Name = compactionRef |
| 636 | |
| 637 | maxTokens := &defaultMaxTokens |
| 638 | if modelCfg.MaxTokens != nil { |
| 639 | maxTokens = modelCfg.MaxTokens |
| 640 | } else if modelsStoreErr == nil { |
| 641 | m, err := modelsStore.GetModel(ctx, modelsdev.NewID(modelCfg.Provider, modelCfg.Model)) |
| 642 | if err == nil { |
| 643 | maxTokens = &m.Limit.Output |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | opts := []options.Opt{ |
| 648 | options.WithGateway(runConfig.ModelsGateway), |
| 649 | options.WithStructuredOutput(a.StructuredOutput), |
| 650 | options.WithProviders(cfg.Providers), |
| 651 | } |
| 652 | if maxTokens != nil { |
| 653 | opts = append(opts, options.WithMaxTokens(*maxTokens)) |
| 654 | } |
| 655 | if modelsStoreErr == nil { |
| 656 | opts = append(opts, options.WithModelsDevStore(modelsStore)) |
| 657 | } |
| 658 | |
| 659 | model, err := providerRegistry.NewWithModels(ctx, &modelCfg, cfg.Models, runConfig.EnvProvider(), opts...) |
| 660 | if err != nil { |
| 661 | return nil, fmt.Errorf("failed to create compaction model '%s': %w", compactionRef, err) |
| 662 | } |
| 663 | return model, nil |
| 664 | } |
| 665 | |
| 666 | // getToolsForAgent returns the tool definitions for an agent based on its |
| 667 | // configuration. Toolset instructions support ${...} JavaScript placeholders |
no test coverage detected