79113
Ma
mini-coding-agent / files
mini-coding-agent / src / models_dev.zig
4.2 KB
1const std = @import("std"); 2 3pub const raw = @embedFile("models_dev.json"); 4 5pub const Info = struct { 6 name: []const u8, 7 api: []const u8, 8 base_url: []const u8, 9 images: bool, 10 reasoning: bool, 11 context: u64, 12 max_output: u64, 13 effort: []const []const u8, 14 budget_tokens: bool, 15 cost_input: f64, 16 cost_output: f64, 17 cost_cache_read: f64, 18}; 19 20const wires = [_][2][]const u8{ 21 .{ "@ai-sdk/openai-compatible", "openai-completions" }, 22 .{ "@ai-sdk/openai", "openai-responses" }, 23 .{ "@ai-sdk/anthropic", "anthropic-messages" }, 24 .{ "@ai-sdk/google", "google-generative-ai" }, 25 .{ "@openrouter/ai-sdk-provider", "openai-completions" }, 26}; 27 28fn wire(npm: []const u8) ?[]const u8 { 29 for (wires) |w| { 30 if (std.mem.eql(u8, w[0], npm)) return w[1]; 31 } 32 return null; 33} 34 35const Effort = struct { type: []const u8 = "", values: []const []const u8 = &.{} }; 36const Limit = struct { context: u64 = 0, output: u64 = 0 }; 37const Cost = struct { input: f64 = 0, output: f64 = 0, cache_read: f64 = 0 }; 38const Package = struct { npm: []const u8 = "", api: []const u8 = "" }; 39 40const Model = struct { 41 name: []const u8 = "", 42 attachment: bool = false, 43 reasoning: bool = false, 44 reasoning_options: []const Effort = &.{}, 45 limit: Limit = .{}, 46 cost: Cost = .{}, 47 provider: ?Package = null, 48}; 49 50pub const Provider = struct { 51 api: []const u8 = "", 52 npm: []const u8 = "", 53 name: []const u8 = "", 54 env: []const []const u8 = &.{}, 55 models: std.json.ArrayHashMap(Model) = .{}, 56}; 57 58const File = std.json.ArrayHashMap(Provider); 59 60const wire_dirs = [_][]const u8{ "openai", "anthropic", "google" }; 61 62pub const Db = struct { 63 a: std.mem.Allocator, 64 file: File, 65 66 pub fn open(a: std.mem.Allocator) !Db { 67 return .{ .a = a, .file = try std.json.parseFromSliceLeaky(File, a, raw, .{ .ignore_unknown_fields = true }) }; 68 } 69 70 pub fn provider(self: Db, provider_id: []const u8) ?Provider { 71 return self.file.map.get(provider_id); 72 } 73 74 pub fn baseUrl(self: Db, provider_id: []const u8) ?[]const u8 { 75 const p = self.file.map.get(provider_id) orelse return null; 76 const api = std.mem.trimEnd(u8, p.api, "/"); 77 if (api.len == 0) return null; 78 const version_at = std.mem.lastIndexOfScalar(u8, api, '/') orelse return api; 79 const root_at = std.mem.lastIndexOfScalar(u8, api[0..version_at], '/') orelse return api; 80 if (!isWireDir(api[root_at + 1 .. version_at])) return api; 81 return std.fmt.allocPrint(self.a, "{s}{s}", .{ api[0..root_at], api[version_at..] }) catch null; 82 } 83 84 pub fn info(self: Db, provider_id: []const u8, model_id: []const u8) ?Info { 85 const p = self.file.map.get(provider_id) orelse return null; 86 if (p.api.len == 0) return null; 87 const m = p.models.map.get(model_id) orelse return null; 88 return .{ 89 .name = if (m.name.len > 0) m.name else model_id, 90 .api = wire(modelNpm(m, p)) orelse return null, 91 .base_url = std.mem.trimEnd(u8, modelApi(m, p), "/"), 92 .images = m.attachment, 93 .reasoning = m.reasoning, 94 .context = m.limit.context, 95 .max_output = m.limit.output, 96 .effort = effortOf(m), 97 .budget_tokens = hasOption(m, "budget_tokens"), 98 .cost_input = m.cost.input, 99 .cost_output = m.cost.output, 100 .cost_cache_read = m.cost.cache_read, 101 }; 102 } 103}; 104 105fn isWireDir(seg: []const u8) bool { 106 for (wire_dirs) |w| { 107 if (std.mem.eql(u8, w, seg)) return true; 108 } 109 return false; 110} 111 112fn modelNpm(m: Model, p: Provider) []const u8 { 113 if (m.provider) |pkg| { 114 if (pkg.npm.len > 0) return pkg.npm; 115 } 116 return p.npm; 117} 118 119fn modelApi(m: Model, p: Provider) []const u8 { 120 if (m.provider) |pkg| { 121 if (pkg.api.len > 0) return pkg.api; 122 } 123 return p.api; 124} 125 126fn effortOf(m: Model) []const []const u8 { 127 for (m.reasoning_options) |opt| { 128 if (std.mem.eql(u8, opt.type, "effort")) return opt.values; 129 } 130 return &.{}; 131} 132 133fn hasOption(m: Model, kind: []const u8) bool { 134 for (m.reasoning_options) |opt| { 135 if (std.mem.eql(u8, opt.type, kind)) return true; 136 } 137 return false; 138}