This commit is contained in:
yumoqing 2026-05-22 16:53:10 +08:00
parent 410ab1d2e4
commit ef4d854424
2 changed files with 33 additions and 45 deletions

View File

@ -46,9 +46,9 @@ async def checkCustomerBalance(llmid, userid, userorgid, catelogid=None):
debug(f'self orgid user') debug(f'self orgid user')
return True return True
balance = 0.00 balance = 0.00
apikey = await get_user_tpac_apikey(userid) tpac = await get_user_tpac(lu.userid)
if apikey: if tpac:
balance = await get_tpac_balance(apikey, userid) balance = await get_tpac_balance(tpac, userid)
else: else:
async with get_sor_context(env, 'accounting') as sor: async with get_sor_context(env, 'accounting') as sor:
balance = await getCustomerBalance(sor, userorgid) balance = await getCustomerBalance(sor, userorgid)
@ -154,11 +154,6 @@ where a.id=${llmid}$
} }
await sor.U('llmusage', ns) await sor.U('llmusage', ns)
async def update_llmusage(ns):
env = ServerEnv()
async with get_sor_context(env, 'llmage') as sor:
await sor.U('llmusage', ns)
async def get_accounting_llmusages(luid=None): async def get_accounting_llmusages(luid=None):
env = ServerEnv() env = ServerEnv()
lus = [] lus = []
@ -233,9 +228,10 @@ async def backend_accounting():
for lu in lus: for lu in lus:
try: try:
debug(f'backend_accounting(): {lu.id=} handleing...') debug(f'backend_accounting(): {lu.id=} handleing...')
apikey = await get_user_tpac_apikey(lu.userid) # apikey = await get_user_tpac_apikey(lu.userid)
if apikey: tpac = await get_user_tpac(lu.userid)
await tpac_accounting(apikey, lu.userid, lu.llmid, lu.amount, lu.usages) if tpac:
await tpac_accounting(tpac, lu.userid, lu.llmid, lu.amount, lu.usages, lu.id)
else: else:
await llm_accounting(lu) await llm_accounting(lu)
except Exception as e: except Exception as e:

View File

@ -16,65 +16,57 @@ from ahserver.filestorage import FileStorage
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.streamhttpclient import StreamHttpClient from appPublic.streamhttpclient import StreamHttpClient
async def get_user_tpac_apikey(userid): async def update_llmusage(ns):
env = ServerEnv()
async with get_sor_context(env, 'llmage') as sor:
await sor.U('llmusage', ns)
async def get_user_tpac(userid)
env = ServerEnv() env = ServerEnv()
config = getConfig() config = getConfig()
if not config.tpac: async with get_sor_context(env, 'rbac') as sor:
return None recs = await sor.R('users', {'id': userid})
apikey = await env.get_user_dapp_apikey(config.tpac.dappid, userid) if recs:
if apikey is None: tpac = config.tpacs.get(recs[0].sync_from)
return None return tpac
return apikey return None
async def get_tpac_balance(apikey, userid): async def get_tpac_balance(tpac, userid):
config = getConfig() url = tpac.get_user_balance_url
if apikey is None:
return None
url = config.tpac.get_user_balance_url
hc = StreamHttpClient() hc = StreamHttpClient()
try: try:
b = await hc.request('GET', url, params={"apikey": apikey, 'userid': userid}) b = await hc.request('GET', url, params={'userid': userid})
if b: if b:
d = json.loads(b.decode('utf-8')) d = json.loads(b.decode('utf-8'))
if d['status'] == 'ok': if d['status'] == 'ok':
return d['balance'] return d['balance']
exception(f'{url=}, {userid=}, {apikey=}, {b} error') exception(f'{url=}, {userid=}, {b} error')
return None return None
except Exception as e: except Exception as e:
exception(f'{url=}, {userid=}, {apikey=}, error:{e}') exception(f'{url=}, {userid=}, error:{e}')
return None return None
async def tpac_accounting(apikey, userid, llmid, amount, usage): async def tpac_accounting(tpac, userid, llmid, amount, usage, luid):
if apikey is None: url = tpac.accounting_url
return hc = StreamHttpClient()
config = getConfig()
url = config.tpac.accounting_url
d = { d = {
'apikey': apikey,
'userid': userid, 'userid': userid,
'llmid': llmid, 'llmid': llmid,
'amount': amount, 'amount': amount,
'usage': usage 'usage': usage
} }
hc = StreamHttpClient()
status = 'failed' status = 'failed'
try: try:
b = await hc.request('POST', url, data=d) b = await hc.request('POST', url, data=d)
d = json.loads(b.decode('utf-8')) d = json.loads(b.decode('utf-8'))
if d['status'] == 'ok': if d['status'] == 'ok':
status = 'accounted' debug(f'{d=}'
exception(f'{apikey=}, {userid=}, {llmid=}, {amount=}, {usage=} tpac accounting error') await update_llmusage({'id': luid, 'accounting_status': 'accounted'})
return
raise Exception(f'{d} tpac accounting error')
except Exception as e: except Exception as e:
exception(f'{apikey=}, {userid=}, {llmid=}, {amount=}, {usage=} tpac accounting error:{e}') exception(f'{userid=}, {llmid=}, {amount=}, {usage=} tpac accounting error:{e}')
env = ServerEnv() raise e
async with get_sor_context(env, 'llmage') as sor:
await sor.U('llmusage', {
'id': llmid,
'accounting_status': status
})
return
exception(f'{apikey=}, {userid=}, {llmid=}, {amount=}, {usage=} tpac_accounting error:update llmusage error')
async def append_new_llmoutput(webpath, output): async def append_new_llmoutput(webpath, output):
fs = FileStorage() fs = FileStorage()