@@ -28,23 +28,20 @@ pub const Model = struct {
2828 oauth: bool = false,
2929 };
3030
31+/// How a provider proves who the user is. Everything else about it — name,
32+/// environment keys, API root, models — comes from the models.dev document.
33+const Credential = enum { env, chatgpt };
34+
3135 const Builtin = struct {
3236 id: []const u8,
33- env_keys: []const []const u8,
34- session_header: ?[]const u8,
37+ credential: Credential,
38+ session_header: ?[]const u8 = null,
3539 };
3640
3741 const builtins = [_]Builtin{
38- .{
39- .id = "opencode-go",
40- .env_keys = &.{"OPENCODE_API_KEY"},
41- .session_header = "x-opencode-session",
42- },
43- .{
44- .id = "opencode",
45- .env_keys = &.{"OPENCODE_API_KEY"},
46- .session_header = "x-opencode-session",
47- },
42+ .{ .id = "opencode", .credential = .env, .session_header = "x-opencode-session" },
43+ .{ .id = "opencode-go", .credential = .env, .session_header = "x-opencode-session" },
44+ .{ .id = "openai", .credential = .chatgpt },
4845 };
4946
5047 const ladder = [_][]const u8{ "off", "minimal", "low", "medium", "high", "xhigh", "max" };
@@ -127,17 +124,25 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
127124 err.* = "model metadata unavailable";
128125 return null;
129126 };
127+ const p = db.provider(provider_id) orelse {
128+ err.* = unknownProvider(a, provider_id);
129+ return null;
130+ };
130131 const info = db.info(provider_id, model_id) orelse {
131132 err.* = unknownModel(a, model_id, provider_id);
132133 return null;
133134 };
135+ if (b.credential == .chatgpt and !auth.available(a)) {
136+ err.* = notSignedIn(a, provider_id);
137+ return null;
138+ }
134139 return Model{
135140 .id = model_id,
136141 .name = info.name,
137142 .api = info.api,
138143 .provider = provider_id,
139144 .base_url = info.base_url,
140- .api_key = firstEnv(b.env_keys),
145+ .api_key = if (b.credential == .env) firstEnv(p.env) else null,
141146 .effort = clampEffort(info.reasoning, info.effort, cfg.thinking_effort),
142147 .supports_images = info.images,
143148 .context_window = info.context,
@@ -147,6 +152,7 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
147152 .cost_output = info.cost_output,
148153 .cost_cache_read = info.cost_cache_read,
149154 .session_header = b.session_header,
155+ .oauth = b.credential == .chatgpt,
150156 };
151157 }
152158
@@ -154,8 +160,7 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
154160 for (p.models) |m| {
155161 if (!std.mem.eql(u8, m.id, model_id)) continue;
156162 if (m.oauth and !auth.available(a)) {
157- err.* = std.fmt.allocPrint(a, "provider \"{s}\" is not signed in; run `mini login`", .{provider_id}) catch
158- "not signed in; run `mini login`";
163+ err.* = notSignedIn(a, provider_id);
159164 return null;
160165 }
161166 return Model{
@@ -181,7 +186,7 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
181186 return null;
182187 }
183188
184- err.* = std.fmt.allocPrint(a, "unknown provider \"{s}\"", .{provider_id}) catch "unknown provider";
189+ err.* = unknownProvider(a, provider_id);
185190 return null;
186191 }
187192
@@ -193,15 +198,31 @@ fn headerPairs(a: std.mem.Allocator, headers: ?std.json.ArrayHashMap([]const u8)
193198 return out.toOwnedSlice(a) catch &.{};
194199 }
195200
196-const ProviderEntry = struct { id: []const u8, name: []const u8, key_present: bool };
201+const ProviderEntry = struct { id: []const u8, name: []const u8, ready: bool };
202+
203+/// A built-in provider is usable when its credential is on hand: the first of
204+/// the provider's own environment keys, or the sign-in with ChatGPT record.
205+fn usable(b: Builtin, a: std.mem.Allocator, p: modelsdev.Provider) bool {
206+ return switch (b.credential) {
207+ .env => firstEnv(p.env) != null,
208+ .chatgpt => auth.available(a),
209+ };
210+}
197211
198212 pub fn providers(a: std.mem.Allocator, cfg: *const config.Config) []ProviderEntry {
199213 var out: std.ArrayList(ProviderEntry) = .empty;
214+ var db: ?modelsdev.Db = modelsdev.Db.open(a) catch null;
200215 for (builtins) |b| {
201- out.append(a, .{ .id = b.id, .name = b.id, .key_present = firstEnv(b.env_keys) != null }) catch {};
216+ // Name, environment keys and every model fact come from the document.
217+ const p = (if (db) |*d| d.provider(b.id) else null) orelse continue;
218+ out.append(a, .{
219+ .id = b.id,
220+ .name = if (p.name.len > 0) p.name else b.id,
221+ .ready = usable(b, a, p),
222+ }) catch {};
202223 }
203224 for (cfg.custom_providers) |p| {
204- out.append(a, .{ .id = p.id, .name = p.name orelse p.id, .key_present = p.envKeys.len == 0 or firstEnv(p.envKeys) != null }) catch {};
225+ out.append(a, .{ .id = p.id, .name = p.name orelse p.id, .ready = p.envKeys.len == 0 or firstEnv(p.envKeys) != null }) catch {};
205226 }
206227 return out.toOwnedSlice(a) catch &.{};
207228 }
@@ -220,28 +241,54 @@ pub fn listing(
220241 if (custom(cfg, provider_id)) |p| return customListing(a, p);
221242 const b = builtin(provider_id) orelse return error.UnknownProvider;
222243 var db = modelsdev.Db.open(a) catch return error.ModelMetadataUnavailable;
244+ const p = db.provider(provider_id) orelse return error.UnknownProvider;
223245 const base = db.baseUrl(provider_id) orelse return error.UnknownProvider;
224- const key = firstEnv(b.env_keys) orelse return error.MissingApiKey;
246+ const key = switch (b.credential) {
247+ .env => firstEnv(p.env) orelse return error.MissingApiKey,
248+ .chatgpt => auth.token(cancel) orelse return error.NotSignedIn,
249+ };
225250
226251 const url = try std.fmt.allocPrint(a, "{s}/models", .{base});
227252 const authorization = try std.fmt.allocPrint(a, "Bearer {s}", .{key});
228253 const body = try http.get(a, url, &.{.{ .name = "Authorization", .value = authorization }}, cancel);
229254
230- const List = struct {
231- data: []const struct { id: []const u8 },
232- };
233- const parsed = try std.json.parseFromSliceLeaky(List, a, body, .{ .ignore_unknown_fields = true });
234-
255+ const parsed = try std.json.parseFromSliceLeaky(Listed, a, body, .{ .ignore_unknown_fields = true });
235256 var out: std.ArrayList(Entry) = .empty;
236- for (parsed.data) |model| {
237- const info = db.info(provider_id, model.id) orelse continue;
238- try out.append(a, .{ .id = model.id, .name = info.name });
257+ for (parsed.items()) |item| {
258+ const id = item.offer() orelse continue;
259+ const info = db.info(provider_id, id) orelse continue;
260+ try out.append(a, .{ .id = id, .name = info.name });
239261 }
240262 const items = try out.toOwnedSlice(a);
241263 std.mem.sort(Entry, items, {}, entryLess);
242264 return items;
243265 }
244266
267+/// Providers decide the shape of their model list: OpenAI-compatible gateways
268+/// answer `data[].id`, the ChatGPT plan answers `models[].slug` with a
269+/// `visibility` that hides internal models.
270+const Listed = struct {
271+ data: ?[]const Item = null,
272+ models: ?[]const Item = null,
273+
274+ const Item = struct {
275+ id: ?[]const u8 = null,
276+ slug: ?[]const u8 = null,
277+ visibility: ?[]const u8 = null,
278+
279+ fn offer(self: Item) ?[]const u8 {
280+ if (self.visibility) |v| {
281+ if (!std.mem.eql(u8, v, "list")) return null;
282+ }
283+ return self.id orelse self.slug;
284+ }
285+ };
286+
287+ fn items(self: Listed) []const Item {
288+ return self.data orelse self.models orelse &.{};
289+ }
290+};
291+
245292 fn entryLess(_: void, a: Entry, b: Entry) bool {
246293 return std.mem.order(u8, a.id, b.id) == .lt;
247294 }
@@ -290,3 +337,12 @@ pub fn clampNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id:
290337 fn unknownModel(a: std.mem.Allocator, model_id: []const u8, provider_id: []const u8) []const u8 {
291338 return std.fmt.allocPrint(a, "unknown model \"{s}\" for provider \"{s}\"", .{ model_id, provider_id }) catch "unknown model";
292339 }
340+
341+fn unknownProvider(a: std.mem.Allocator, provider_id: []const u8) []const u8 {
342+ return std.fmt.allocPrint(a, "unknown provider \"{s}\"", .{provider_id}) catch "unknown provider";
343+}
344+
345+fn notSignedIn(a: std.mem.Allocator, provider_id: []const u8) []const u8 {
346+ return std.fmt.allocPrint(a, "provider \"{s}\" is not signed in; run `mini login`", .{provider_id}) catch
347+ "not signed in; run `mini login`";
348+}