Files
Athena-Deck/integrations/openwebui/install_pcm_streaming.py

48 lines
3.2 KiB
Python

"""Version-checked OpenWebUI 0.11.4 call-mode streaming patch. No secrets read.
Copy this directory to the container and run this file; restart OpenWebUI.
Reapply after image recreation. Original files receive one-time backups.
"""
import gzip,json
from pathlib import Path
ROOT=Path(__file__).parent
def browser_patch(source):
if 'window.athenaPCM.getAudio' in source:return source
old='const ye=await g0(localStorage.token,ve(),ue).catch(ct=>null);if(ye){const ct=await ye.blob(),Ct=URL.createObjectURL(ct);Ue.set(ue,new Audio(Ct))}'
new='const ye=await window.athenaPCM.getAudio(localStorage.token,ve(),ue).catch(ct=>{_t.error(`${ct}`);return null});if(ye)Ue.set(ue,ye)'
if source.count(old)!=1 or source.count('he=ue=>n()?new Promise')!=1 or source.count('De=async()=>{c(K,!1)')!=1:
raise RuntimeError('Call implementation changed; review the patch before installation.')
return source.replace(old,new).replace('he=ue=>n()?new Promise','he=ue=>ue?.athenaPCM?window.athenaPCM.play(ue,i()?.audio?.tts?.playbackRate??1):n()?new Promise').replace('De=async()=>{c(K,!1)','De=async()=>{window.athenaPCM.stop();c(K,!1)')
def main():
if json.loads(Path('/app/package.json').read_text()).get('version')!='0.11.4':raise RuntimeError('Reviewed for OpenWebUI 0.11.4 only.')
backend=Path('/app/backend/open_webui/routers/audio.py');original=backend.read_text()
addition=(ROOT/'pcm_route.txt').read_text()
updated=original if 'async def athena_pcm_speech(' in original else original+addition
compile(updated,str(backend),'exec')
assets=list(Path('/app/build/_app/immutable/chunks').glob('*.js'))
matching=[p for p in assets if 'he=ue=>n()?new Promise' in p.read_text() or 'window.athenaPCM.getAudio' in p.read_text()]
if len(matching)!=1:raise RuntimeError('Expected exactly one call-mode asset.')
asset=matching[0];browser_original=asset.read_text();browser_updated=browser_patch(browser_original)
# Validate every target before writing any of them.
index=Path('/app/build/index.html');html=index.read_text()
if '</head>' not in html:raise RuntimeError('Expected a complete HTML head.')
if any(Path(str(p)+'.br').exists() for p in (asset,index)):
import brotli
for path,text in ((backend,updated),(asset,browser_updated)):
backup=path.with_name(path.name+'.athena-pcm.bak')
if not backup.exists():backup.write_text(path.read_text())
path.write_text(text)
if Path(str(asset)+'.gz').exists():Path(str(asset)+'.gz').write_bytes(gzip.compress(browser_updated.encode()))
if Path(str(asset)+'.br').exists():Path(str(asset)+'.br').write_bytes(brotli.compress(browser_updated.encode()))
Path('/app/build/athena-pcm-stream.js').write_text((ROOT/'athena_pcm.js').read_text())
if '/athena-pcm-stream.js' not in html:
backup=index.with_name('index.html.athena-pcm.bak')
if not backup.exists():backup.write_text(html)
html=html.replace('</head>','<script src="/athena-pcm-stream.js"></script></head>');index.write_text(html)
if Path(str(index)+'.gz').exists():Path(str(index)+'.gz').write_bytes(gzip.compress(html.encode()))
if Path(str(index)+'.br').exists():
import brotli
Path(str(index)+'.br').write_bytes(brotli.compress(html.encode()))
print('Athena call-mode PCM streaming installed; restart OpenWebUI and reload the browser.')
if __name__=='__main__':main()