| 21 | ) |
| 22 | |
| 23 | func Load(ctx context.Context, source Source) (*latest.Config, error) { |
| 24 | data, err := source.Read(ctx) |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | |
| 29 | // Configurations may be authored in HCL as an alternative to YAML. |
| 30 | // Detect the format from the source name extension or, when no hint is |
| 31 | // available (OCI artifacts, etc.), from the content itself, then |
| 32 | // transparently convert to YAML for the rest of the pipeline. |
| 33 | if isHCLSource(source.Name(), data) { |
| 34 | data, err = hclconv.ToYAML(data, source.Name()) |
| 35 | if err != nil { |
| 36 | return nil, fmt.Errorf("parsing HCL config file: %w", err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | var raw struct { |
| 41 | Version string `yaml:"version,omitempty"` |
| 42 | } |
| 43 | if err := yaml.Unmarshal(data, &raw); err != nil { |
| 44 | return nil, fmt.Errorf("looking for version in config file\n%s", yaml.FormatError(err, true, true)) |
| 45 | } |
| 46 | raw.Version = cmp.Or(raw.Version, latest.Version) |
| 47 | |
| 48 | oldConfig, err := parseCurrentVersion(data, raw.Version) |
| 49 | if err != nil { |
| 50 | return nil, fmt.Errorf("parsing config file\n%s", yaml.FormatError(err, true, true)) |
| 51 | } |
| 52 | |
| 53 | config, err := migrateToLatestConfig(oldConfig, data) |
| 54 | if err != nil { |
| 55 | return nil, fmt.Errorf("migrating config: %w", err) |
| 56 | } |
| 57 | |
| 58 | config.Version = raw.Version |
| 59 | |
| 60 | if err := resolveInstructionFiles(&config, source); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | |
| 64 | if err := validateConfig(&config); err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | warnExpansionMismatches(ctx, slog.Default(), &config) |
| 69 | warnMaxTokensVsContextWindow(ctx, slog.Default(), &config) |
| 70 | |
| 71 | return &config, nil |
| 72 | } |
| 73 | |
| 74 | // resolveInstructionFiles replaces every agent's instruction_file reference |
| 75 | // with the file's contents, loaded relative to the config file's directory. |