Files

23 lines
1.2 KiB
Python

import json,pathlib,sys,os
path=pathlib.Path(sys.argv[1]);models=json.loads(pathlib.Path(sys.argv[2]).read_text());d=json.loads(path.read_text())
ids=[k for k in d.get('provider',{}) if k in ('athena-deck','athena_deck')]
if len(ids)!=1:raise SystemExit('Expected one existing Athena provider; no change')
pid=ids[0];provider=d['provider'][pid];old=provider.get('models',{});renames={}
for new_id,row in models.items():
old_id=next((k for k,v in old.items()if k==new_id or v.get('name')==row['name']),None)
if old_id:
kept=dict(old[old_id]);kept.update(row);models[new_id]=kept
renames[pid+'/'+old_id]=pid+'/'+new_id
provider['models']=models
def remap(x):
if isinstance(x,dict):
for k,v in x.items():
if k in ('model','small_model')and isinstance(v,str)and v in renames:x[k]=renames[v]
else:remap(v)
elif isinstance(x,list):
for v in x:remap(v)
remap(d)
tmp=path.with_name(path.name+'.profiles.tmp');tmp.write_text(json.dumps(d,indent=2,ensure_ascii=False)+'\n');os.chmod(tmp,path.stat().st_mode&0o777);tmp.replace(path)
check=json.loads(path.read_text());assert set(check['provider'][pid]['models'])==set(models)
print(json.dumps({'updated':str(path),'provider':pid,'models':len(models),'credentials_unchanged':True}))