Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233//! The models jig knows, as plain data.
const std = @import("std");const Usage = @import("provider.zig").Usage;
/// How much the model reasons before it answers.pub const Level = enum { off, low, medium, high, max };
/// The level a session starts with when the user picks none.pub const default_level: Level = .medium;
/// Prices in USD per million tokens.pub const Cost = struct { input: f64 = 0, output: f64 = 0, cache_read: f64 = 0, cache_write: f64 = 0,};
/// Who serves a model.pub const Provider = enum { workers_ai, fake };
pub const Entry = struct { /// The short name that a user types and a UI shows. name: []const u8, /// The name the provider knows the model by. id: []const u8, provider: Provider, /// What the provider calls each level. Null for a level the model does /// not support. levels: std.EnumArray(Level, ?[]const u8) = .initFill(null), /// In tokens. Zero when unknown. context_window: u32 = 0, /// The most tokens one reply may hold. max_output: u32, /// Null when the price is unknown. cost: ?Cost = null,};
pub const entries = [_]Entry{ .{ .name = "deepseek-v4.1-flash", .id = "@cf/deepseek-ai/deepseek-v4.1-flash-1017", .provider = .workers_ai, .levels = .init(.{ .off = "none", .low = "low", .medium = null, .high = "high", .max = "max" }), .context_window = 1_048_576, // The server default can be too small for a tool call that writes a file. .max_output = 16 * 1024, .cost = .{ .input = 0.30, .output = 1.20, .cache_read = 0.006 }, }, .{ .name = "kimi-k2.6", .id = "@cf/moonshotai/kimi-k2.6", .provider = .workers_ai, .levels = .init(.{ .off = "none", .low = null, .medium = null, .high = "high", .max = null }), .context_window = 262_144, .max_output = 16 * 1024, .cost = .{ .input = 0.95, .output = 4.00, .cache_read = 0.16 }, }, .{ .name = "gpt-oss-120b", .id = "@cf/openai/gpt-oss-120b", .provider = .workers_ai, .levels = .init(.{ .off = null, .low = "low", .medium = "medium", .high = "high", .max = null }), .context_window = 128_000, .max_output = 16 * 1024, // The provider lists no price for cached input. .cost = .{ .input = 0.35, .output = 0.75, .cache_read = 0.35 }, }, .{ .name = "fake", .id = "fake", .provider = .fake, .max_output = 0 },};
/// The names of all entries, joined with ", ".pub const names = blk: { var text: []const u8 = ""; for (entries, 0..) |entry, index| { text = text ++ (if (index == 0) "" else ", ") ++ entry.name; } break :blk text;};
/// The entry with this short name.pub fn find(name: []const u8) ?*const Entry { return &entries[indexOf(&entries, name) orelse return null];}
/// Where in `list` the entry with this short name is.pub fn indexOf(list: []const Entry, name: []const u8) ?usize { for (list, 0..) |entry, at| { if (std.mem.eql(u8, entry.name, name)) { return at; } } return null;}
/// The level nearest to `level` that the model supports: `level` itself, else/// the next higher one, else the next lower one. `off` for a model without/// reasoning.pub fn clamp(entry: *const Entry, level: Level) Level { const all = std.enums.values(Level); const start = @backingInt(level); for (all[start..]) |candidate| { if (entry.levels.get(candidate) != null) { return candidate; } } var index: usize = start; while (index > 0) { index -= 1; if (entry.levels.get(all[index]) != null) { return all[index]; } } return .off;}
/// Whether the model can reason at all.pub fn reasons(entry: *const Entry) bool { for (std.enums.values(Level)) |level| { if (level != .off and entry.levels.get(level) != null) { return true; } } return false;}
/// The next level after `level` that the model supports, and the first one/// after the last. `level` itself when the model supports no other.pub fn nextLevel(entry: *const Entry, level: Level) Level { const all = std.enums.values(Level); for (1..all.len) |step| { const candidate = all[(@backingInt(level) + step) % all.len]; if (entry.levels.get(candidate) != null) { return candidate; } } return level;}
/// What `usage` costs at these prices, in USD.pub fn cost(prices: Cost, usage: Usage) f64 { const per_million = 1_000_000.0; return (prices.input * @as(f64, @floatFromInt(usage.input)) + prices.output * @as(f64, @floatFromInt(usage.output)) + prices.cache_read * @as(f64, @floatFromInt(usage.cache_read)) + prices.cache_write * @as(f64, @floatFromInt(usage.cache_write))) / per_million;}
const testing = std.testing;
test "a model is found by its short name" { const entry = find("deepseek-v4.1-flash").?; try testing.expectEqualStrings("@cf/deepseek-ai/deepseek-v4.1-flash-1017", entry.id); try testing.expectEqual(Provider.workers_ai, entry.provider); try testing.expectEqual(Provider.fake, find("fake").?.provider);}
test "an unknown name and a provider id find nothing" { try testing.expectEqual(null, find("gpt")); try testing.expectEqual(null, find("@cf/deepseek-ai/deepseek-v4.1-flash-1017")); try testing.expectEqual(null, find(""));}
test "every name is listed once" { try testing.expectEqualStrings("deepseek-v4.1-flash, kimi-k2.6, gpt-oss-120b, fake", names); for (entries, 0..) |entry, index| { for (entries[index + 1 ..]) |other| { try testing.expect(!std.mem.eql(u8, entry.name, other.name)); } }}
test "the cost is each token count at its price per million" { const prices: Cost = .{ .input = 2, .output = 10, .cache_read = 1, .cache_write = 4 }; try testing.expectEqual(@as(f64, 0), cost(prices, .{})); try testing.expectApproxEqAbs(@as(f64, 2), cost(prices, .{ .input = 1_000_000 }), 1e-9); try testing.expectApproxEqAbs(@as(f64, 3.7), cost(prices, .{ .input = 1_000_000, .output = 100_000, .cache_read = 500_000, .cache_write = 50_000 }), 1e-9); try testing.expectApproxEqAbs(@as(f64, 0.000012), cost(prices, .{ .cache_read = 12 }), 1e-12);}
test "a level the model lacks becomes the next higher one, else the next lower one" { const entry: Entry = .{ .name = "m", .id = "m", .provider = .fake, .levels = .init(.{ .off = "none", .low = null, .medium = "mid", .high = null, .max = null }), .max_output = 0, }; try testing.expectEqual(Level.off, clamp(&entry, .off)); try testing.expectEqual(Level.medium, clamp(&entry, .low)); try testing.expectEqual(Level.medium, clamp(&entry, .medium)); try testing.expectEqual(Level.medium, clamp(&entry, .high)); try testing.expectEqual(Level.medium, clamp(&entry, .max));}
test "a model without reasoning is always off" { const entry: Entry = .{ .name = "m", .id = "m", .provider = .fake, .max_output = 0 }; for (std.enums.values(Level)) |level| { try testing.expectEqual(Level.off, clamp(&entry, level)); }}
test "the default level of each model is one it supports" { try testing.expectEqual(Level.high, clamp(find("deepseek-v4.1-flash").?, default_level)); try testing.expectEqual(Level.high, clamp(find("kimi-k2.6").?, default_level)); try testing.expectEqual(Level.medium, clamp(find("gpt-oss-120b").?, default_level)); // This model cannot turn reasoning off. try testing.expectEqual(Level.low, clamp(find("gpt-oss-120b").?, .off)); try testing.expectEqual(Level.off, clamp(find("fake").?, default_level));}
test "the next level skips what the model lacks and wraps around" { const entry: Entry = .{ .name = "m", .id = "m", .provider = .fake, .levels = .init(.{ .off = "none", .low = null, .medium = "mid", .high = null, .max = "max" }), .max_output = 0, }; try testing.expectEqual(Level.medium, nextLevel(&entry, .off)); try testing.expectEqual(Level.max, nextLevel(&entry, .medium)); try testing.expectEqual(Level.off, nextLevel(&entry, .max)); try testing.expect(reasons(&entry));
const plain: Entry = .{ .name = "p", .id = "p", .provider = .fake, .max_output = 0 }; try testing.expectEqual(Level.off, nextLevel(&plain, .off)); try testing.expect(!reasons(&plain));
const only_off: Entry = .{ .name = "o", .id = "o", .provider = .fake, .levels = .init(.{ .off = "none", .low = null, .medium = null, .high = null, .max = null }), .max_output = 0 }; try testing.expect(!reasons(&only_off));}