package provider import "core:hash" import "core:math" import "core:strings" import "core:time" // All integers and IEEE-754 bits are little endian, independent of ABI. CATALOG_BINARY_MAGIC :: "MEMMODEL" CATALOG_BINARY_VERSION :: 1 CATALOG_BINARY_MAX_MODELS :: 100_000 CATALOG_BINARY_MAX_STRING :: 1024 * 1024 CATALOG_BINARY_MAX_VARIANTS :: 1024 Catalog_Binary_Reader :: struct { data: []u8, pos: int, ok: bool, } catalog_binary_uint :: proc(out: ^[dynamic]u8, value: u64, width: int) { for i in 0 ..< width do append(out, u8((value >> uint(8 * i)) & 255)) } catalog_binary_string :: proc(out: ^[dynamic]u8, value: string) { catalog_binary_uint(out, u64(len(value)), 4) append(out, ..transmute([]u8)value) } catalog_binary_read_uint :: proc(r: ^Catalog_Binary_Reader, width: int) -> u64 { if !r.ok || width > len(r.data) - r.pos { r.ok = false return 0 } value: u64 for i in 0 ..< width do value |= u64(r.data[r.pos + i]) << uint(8 * i) r.pos += width return value } // Borrow until the whole record has been validated; the caller then clones. catalog_binary_read_string :: proc(r: ^Catalog_Binary_Reader) -> string { n := catalog_binary_read_uint(r, 4) if !r.ok || n > CATALOG_BINARY_MAX_STRING || n > u64(len(r.data) - r.pos) { r.ok = false return "" } value := string(r.data[r.pos:r.pos + int(n)]) r.pos += int(n) return value } // Exact, length-delimited registry identity (no hash collision or persisted // procedure addresses). Availability/selection do not affect catalog_parse. // Custom callbacks must supply a versioned semantic identity or caching is off. catalog_cache_identity :: proc(registry: ^Provider_Registry) -> [dynamic]u8 { if registry == nil do return nil identity: [dynamic]u8 for registration in registry.registrations { if registration.catalog_id == "" do continue if (registration.catalog_model_filter != nil || registration.catalog_model_overrides != nil) && registration.catalog_cache_policy == "" { delete(identity) return nil } catalog_binary_string(&identity, string(registration.id)) catalog_binary_string(&identity, registration.catalog_id) catalog_binary_string(&identity, registration.catalog_npm) catalog_binary_string(&identity, registration.catalog_cache_policy) catalog_binary_uint(&identity, u64(int(registration.catalog_model_filter != nil)), 4) catalog_binary_uint(&identity, u64(int(registration.catalog_model_overrides != nil)), 4) } return identity } catalog_binary_encode :: proc( models: []Model_Info, registry: ^Provider_Registry, now: time.Time, ) -> [dynamic]u8 { identity := catalog_cache_identity(registry) defer delete(identity) if len(identity) == 0 || len(identity) > CATALOG_BINARY_MAX_STRING || len(models) == 0 || len(models) > CATALOG_BINARY_MAX_MODELS { return nil } out: [dynamic]u8 append(&out, ..transmute([]u8)string(CATALOG_BINARY_MAGIC)) catalog_binary_uint(&out, CATALOG_BINARY_VERSION, 4) catalog_binary_uint(&out, u64(time.to_unix_nanoseconds(now)), 8) catalog_binary_string(&out, string(identity[:])) catalog_binary_uint(&out, u64(len(models)), 4) for model in models { values := [?]string { string(model.ref.provider), model.ref.id, model.name, model.family, model.release_date, } for value in values { if len(value) > CATALOG_BINARY_MAX_STRING { delete(out) return nil } catalog_binary_string(&out, value) } for value in ([]int{model.context_length, model.input_limit, model.output_limit}) { catalog_binary_uint(&out, u64(value), 8) } for value in ([]f64{model.cost.input, model.cost.output, model.cost.cache_read, model.cost.cache_write}) { catalog_binary_uint(&out, transmute(u64)value, 8) } flags := u64(int(model.has_cost)) | u64(int(model.attachment)) << 1 | u64(int(model.reasoning)) << 2 | u64(int(model.temperature)) << 3 | u64(int(model.tool_call)) << 4 catalog_binary_uint(&out, flags, 4) catalog_binary_uint(&out, u64(model.status), 4) if len(model.variants) > CATALOG_BINARY_MAX_VARIANTS { delete(out) return nil } catalog_binary_uint(&out, u64(len(model.variants)), 4) for variant in model.variants { if len(variant) > CATALOG_BINARY_MAX_STRING { delete(out) return nil } catalog_binary_string(&out, variant) } if len(out) > MODELS_DEV_MAX_RESPONSE_BYTES - 8 { delete(out) return nil } } catalog_binary_uint(&out, hash.fnv64a(out[:]), 8) return out } catalog_binary_decode :: proc( data: []u8, registry: ^Provider_Registry, now: time.Time, ) -> [dynamic]Model_Info { models, _ := catalog_binary_decode_matching(data, registry, now, nil) return models } // A nil target decodes the entire catalog. With a target, validate every // record but own only the requested model; valid absent models are cache hits. catalog_binary_decode_matching :: proc( data: []u8, registry: ^Provider_Registry, now: time.Time, target: ^Model_Ref, ) -> ([dynamic]Model_Info, bool) { if len(data) < 36 || len(data) > MODELS_DEV_MAX_RESPONSE_BYTES || string(data[:8]) != CATALOG_BINARY_MAGIC { return nil, false } checksum_reader := Catalog_Binary_Reader { data = data[len(data) - 8:], ok = true, } if hash.fnv64a(data[:len(data) - 8]) != catalog_binary_read_uint(&checksum_reader, 8) do return nil, false r := Catalog_Binary_Reader { data = data[:len(data) - 8], pos = 8, ok = true, } if catalog_binary_read_uint(&r, 4) != CATALOG_BINARY_VERSION do return nil, false stamp := catalog_binary_read_uint(&r, 8) current := time.to_unix_nanoseconds(now) if current < 0 || stamp > u64(current) || u64(current) - stamp >= u64(MODELS_DEV_CACHE_TTL) { return nil, false } identity := catalog_cache_identity(registry) defer delete(identity) if len(identity) == 0 || catalog_binary_read_string(&r) != string(identity[:]) do return nil, false count := catalog_binary_read_uint(&r, 4) // Minimum record size is 88 bytes, before string contents/variants. if !r.ok || count == 0 || count > CATALOG_BINARY_MAX_MODELS || count > u64((len(r.data) - r.pos) / 88) { return nil, false } models: [dynamic]Model_Info success := false defer {if !success do catalog_models_destroy(&models)} previous_provider, previous_id: string for _ in 0 ..< int(count) { provider_id := catalog_binary_read_string(&r) id := catalog_binary_read_string(&r) name := catalog_binary_read_string(&r) family := catalog_binary_read_string(&r) release := catalog_binary_read_string(&r) limits: [3]int for &limit in limits { value := catalog_binary_read_uint(&r, 8) if value > 0x7fff_ffff_ffff_ffff do return nil, false limit = int(value) } costs: [4]f64 for &cost in costs { value := catalog_binary_read_uint(&r, 8) cost = transmute(f64)value if math.is_nan(cost) || math.is_inf(cost) || cost < 0 do return nil, false } flags := catalog_binary_read_uint(&r, 4) status := catalog_binary_read_uint(&r, 4) variants := catalog_binary_read_uint(&r, 4) if !r.ok || !catalog_id_valid(provider_id) || !catalog_id_valid(id) || name == "" || limits[0] <= 0 || limits[2] <= 0 || flags > 31 || status > u64(Model_Status.Deprecated) || variants > CATALOG_BINARY_MAX_VARIANTS || variants > u64((len(r.data) - r.pos) / 4) { return nil, false } if !registry_known(registry, Provider_Id(provider_id)) do return nil, false if previous_provider > provider_id || (previous_provider == provider_id && previous_id >= id) { return nil, false } previous_provider, previous_id = provider_id, id selected := target == nil || string(target.provider) == provider_id && target.id == id if selected { model := Model_Info { ref = {Provider_Id(strings.clone(provider_id)), strings.clone(id)}, name = strings.clone(name), family = strings.clone(family), release_date = strings.clone(release), context_length = limits[0], input_limit = limits[1], output_limit = limits[2], cost = {costs[0], costs[1], costs[2], costs[3]}, has_cost = flags & 1 != 0, attachment = flags & 2 != 0, reasoning = flags & 4 != 0, temperature = flags & 8 != 0, tool_call = flags & 16 != 0, status = Model_Status(status), } append(&models, model) } for _ in 0 ..< int(variants) { variant := catalog_binary_read_string(&r) if !r.ok || !catalog_variant_valid(variant) do return nil, false if selected do append(&models[len(models) - 1].variants, strings.clone(variant)) } } if !r.ok || r.pos != len(r.data) do return nil, false success = true return models, true }