@@ -24,9 +24,6 @@ pub const Model = struct {
2424 thinking: Thinking = .budget,
2525 };
2626
27-/// A built-in provider is one whose models.dev entry the agent knows how to
28-/// request: name, environment keys, API root and models all come from the
29-/// document.
3027 const Builtin = struct {
3128 id: []const u8,
3229 session_header: ?[]const u8 = null,
@@ -97,6 +94,15 @@ fn custom(cfg: *const config.Config, id: []const u8) ?*const config.CustomProvid
9794 return null;
9895 }
9996
97+fn customModel(cfg: *const config.Config, provider_id: []const u8, model_id: []const u8) ?*const config.CustomModel {
98+ if (builtin(provider_id) != null) return null;
99+ const p = custom(cfg, provider_id) orelse return null;
100+ for (p.models) |*m| {
101+ if (std.mem.eql(u8, m.id, model_id)) return m;
102+ }
103+ return null;
104+}
105+
100106 fn firstEnv(keys: []const []const u8) ?[]const u8 {
101107 for (keys) |key| {
102108 if (platform.getEnv(key)) |value| {
@@ -145,27 +151,26 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
145151 }
146152
147153 if (custom(cfg, provider_id)) |p| {
148- for (p.models) |m| {
149- if (!std.mem.eql(u8, m.id, model_id)) continue;
150- return Model{
151- .id = m.id,
152- .api = @tagName(m.api),
153- .provider = provider_id,
154- .base_url = m.baseUrl,
155- .api_key = firstEnv(p.envKeys),
156- .effort = clampEffort(m.reasoning, m.effort, cfg.thinking_effort),
157- .supports_images = m.images,
158- .context_window = m.context,
159- .max_tokens = m.maxOutput,
160- .cost_input = m.cost[0],
161- .cost_output = m.cost[1],
162- .cost_cache_read = m.cost[2],
163- .session_header = null,
164- .headers = headerPairs(a, p.headers),
165- };
166- }
167- err.* = unknownModel(a, model_id, provider_id);
168- return null;
154+ const m = customModel(cfg, provider_id, model_id) orelse {
155+ err.* = unknownModel(a, model_id, provider_id);
156+ return null;
157+ };
158+ return Model{
159+ .id = m.id,
160+ .api = @tagName(m.api),
161+ .provider = provider_id,
162+ .base_url = m.baseUrl,
163+ .api_key = firstEnv(p.envKeys),
164+ .effort = clampEffort(m.reasoning, m.effort, cfg.thinking_effort),
165+ .supports_images = m.images,
166+ .context_window = m.context,
167+ .max_tokens = m.maxOutput,
168+ .cost_input = m.cost[0],
169+ .cost_output = m.cost[1],
170+ .cost_cache_read = m.cost[2],
171+ .session_header = null,
172+ .headers = headerPairs(a, p.headers),
173+ };
169174 }
170175
171176 err.* = unknownProvider(a, provider_id);
@@ -182,7 +187,6 @@ fn headerPairs(a: std.mem.Allocator, headers: ?std.json.ArrayHashMap([]const u8)
182187
183188 const ProviderEntry = struct { id: []const u8, name: []const u8, ready: bool };
184189
185-/// A built-in provider is usable when its first environment key is present.
186190 fn usable(p: modelsdev.Provider) bool {
187191 return firstEnv(p.env) != null;
188192 }
@@ -191,7 +195,6 @@ pub fn providers(a: std.mem.Allocator, cfg: *const config.Config) []ProviderEntr
191195 var out: std.ArrayList(ProviderEntry) = .empty;
192196 var db: ?modelsdev.Db = modelsdev.Db.open(a) catch null;
193197 for (builtins) |b| {
194- // Name, environment keys and every model fact come from the document.
195198 const p = (if (db) |*d| d.provider(b.id) else null) orelse continue;
196199 out.append(a, .{
197200 .id = b.id,
@@ -207,9 +210,6 @@ pub fn providers(a: std.mem.Allocator, cfg: *const config.Config) []ProviderEntr
207210
208211 pub const Entry = struct { id: []const u8, name: []const u8 };
209212
210-/// The models a provider offers. Built-ins are asked over the wire
211-/// (authenticated, so the provider decides what is enabled); a model with no
212-/// models.dev metadata is skipped. Custom providers list what they declare.
213213 pub fn listing(
214214 a: std.mem.Allocator,
215215 cfg: *const config.Config,
@@ -239,7 +239,6 @@ pub fn listing(
239239 return items;
240240 }
241241
242-/// The model list shape of an OpenAI-compatible gateway: `data[].id`.
243242 const Listed = struct {
244243 data: ?[]const Item = null,
245244
@@ -260,34 +259,20 @@ fn customListing(a: std.mem.Allocator, p: *const config.CustomProvider) ![]Entry
260259 return items;
261260 }
262261
262+fn levels(reasoning: bool, effort: []const []const u8) []const []const u8 {
263+ if (!reasoning) return &.{"off"};
264+ return if (effort.len == 0) &ladder else effort;
265+}
266+
263267 pub fn supportedLevels(a: std.mem.Allocator, cfg: *const config.Config, provider_id: []const u8, model_id: []const u8) []const []const u8 {
264- if (builtin(provider_id) == null) {
265- if (custom(cfg, provider_id)) |p| {
266- for (p.models) |m| {
267- if (!std.mem.eql(u8, m.id, model_id)) continue;
268- if (!m.reasoning) return &.{"off"};
269- if (m.effort.len == 0) return &ladder;
270- return m.effort;
271- }
272- }
273- return &ladder;
274- }
268+ if (customModel(cfg, provider_id, model_id)) |m| return levels(m.reasoning, m.effort);
275269 var db = modelsdev.Db.open(a) catch return &ladder;
276270 const info = db.info(provider_id, model_id) orelse return &ladder;
277- if (!info.reasoning) return &.{"off"};
278- if (info.effort.len == 0) return &ladder;
279- return info.effort;
271+ return levels(info.reasoning, info.effort);
280272 }
281273
282274 pub fn clampNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id: []const u8, model_id: []const u8, desired: []const u8) []const u8 {
283- if (builtin(provider_id) == null) {
284- if (custom(cfg, provider_id)) |p| {
285- for (p.models) |m| {
286- if (std.mem.eql(u8, m.id, model_id)) return clampEffort(m.reasoning, m.effort, desired);
287- }
288- }
289- return desired;
290- }
275+ if (customModel(cfg, provider_id, model_id)) |m| return clampEffort(m.reasoning, m.effort, desired);
291276 var db = modelsdev.Db.open(a) catch return desired;
292277 const info = db.info(provider_id, model_id) orelse return desired;
293278 return clampEffort(info.reasoning, info.effort, desired);