fix(price-sync): keep live Friendli entries

This commit is contained in:
siyoon 2026-09-17 19:58:44 +09:00
parent dc3dcd9e5d
commit 4d2314d9cf
2 changed files with 44 additions and 9 deletions

View file

@ -165,6 +165,12 @@ def transform_friendli_data(data: list, local_data: dict) -> dict[str, dict[str,
return transformed
# Synchronize local data with remote data
def _friendli_catalog_keys(data: list | None) -> frozenset[str]:
if not data:
return frozenset()
return frozenset(f"{FRIENDLI_PROVIDER}/{model['id']}" for model in data if "id" in model)
def _stale_friendli_keys(
local_keys: frozenset[str],
friendli_keys_in_remote: frozenset[str],
@ -325,22 +331,21 @@ def main():
vercel_data = transform_vercel_ai_gateway_data(vercel_data)
friendli_data = asyncio.run(fetch_data(FRIENDLI_API_URL))
friendli_fetch_failed = friendli_data is None
friendli_catalog_keys = _friendli_catalog_keys(friendli_data)
friendli_data = transform_friendli_data(friendli_data, local_data)
# Combine both datasets
all_remote_data = {**openrouter_data, **vercel_data, **friendli_data}
# Deletion only runs when the Friendli catalog itself was fetched: if the
# catalog is unreachable, absence proves nothing and deleting entries would
# wipe live models. OpenRouter/Vercel rows never qualify for deletion; their
# gateways can delist rows users still route to
# Deletion only runs when Friendli returns a non-empty catalog. Catalog rows
# with invalid pricing still prove the model is live, so deletion compares
# against raw catalog ids instead of transformed priced rows
catalog_dropped_keys: frozenset[str] = (
frozenset()
if friendli_fetch_failed
if not friendli_catalog_keys
else _stale_friendli_keys(
local_keys=frozenset(local_data or {}),
friendli_keys_in_remote=frozenset(friendli_data),
friendli_keys_in_remote=friendli_catalog_keys,
)
)

View file

@ -140,6 +140,27 @@ def test_transform_skips_rows_without_valid_token_prices_so_priced_local_entries
assert local["friendliai/zai-org/GLM-Test"]["output_cost_per_token"] == 5e-07
def test_sync_keeps_live_friendli_entry_when_catalog_row_has_invalid_pricing(sync_module):
local = {
"friendliai/zai-org/GLM-Test": {
"litellm_provider": "friendliai",
"input_cost_per_token": 1.5e-07,
"output_cost_per_token": 5e-07,
}
}
raw_catalog = [_reasoning_model(pricing={})]
remote = sync_module.transform_friendli_data(raw_catalog, local)
dropped = sync_module._stale_friendli_keys(
local_keys=frozenset(local),
friendli_keys_in_remote=sync_module._friendli_catalog_keys(raw_catalog),
)
sync_module.sync_local_data_with_remote(
local, remote, replace_keys=frozenset(remote), catalog_dropped_keys=dropped
)
assert "friendliai/zai-org/GLM-Test" in local
assert local["friendliai/zai-org/GLM-Test"]["input_cost_per_token"] == 1.5e-07
def test_transform_keeps_zero_priced_rows(sync_module):
free_model = _reasoning_model(pricing={"input": "0", "output": "0"})
entry = sync_module.transform_friendli_data([free_model], {})["friendliai/zai-org/GLM-Test"]
@ -254,8 +275,17 @@ def test_sync_keeps_all_entries_when_friendli_fetch_fails_or_is_empty(sync_modul
}
for failed_fetch in (None, []):
remote = sync_module.transform_friendli_data(failed_fetch, local)
assert remote == {}
sync_module.sync_local_data_with_remote(local, remote, replace_keys=frozenset(remote))
catalog_keys = sync_module._friendli_catalog_keys(failed_fetch)
dropped = (
frozenset()
if not catalog_keys
else sync_module._stale_friendli_keys(
local_keys=frozenset(local), friendli_keys_in_remote=catalog_keys
)
)
sync_module.sync_local_data_with_remote(
local, remote, replace_keys=frozenset(remote), catalog_dropped_keys=dropped
)
assert "friendliai/zai-org/GLM-Test" in local
assert "openrouter/kept" in local