Files

63 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Safe minimal Kronic v8 installer. No Balroc files or JS overwritten."""
from __future__ import annotations
import argparse
import datetime
import pathlib
import py_compile
import shutil
import sys
p=argparse.ArgumentParser(description='Install Kronic AI media conversational context v8')
p.add_argument('root',nargs='?',default='/home/admin/mediaserver')
p.add_argument('--check',action='store_true')
a=p.parse_args()
root=pathlib.Path(a.root).expanduser().resolve()
server=root/'stream_server.py'
oldmodule=root/'ai_command_media_extended.py'
newmodule=root/'ai_command_context.py'
source=pathlib.Path(__file__).parent/'BLADE-A/home/admin/mediaserver/ai_command_context.py'
for file in (server,oldmodule,root/'ai_command_media.py',source):
if not file.is_file():sys.exit(f'Missing required source {file}. Nothing changed.')
if '_explicit_youtube_request' not in oldmodule.read_text():
sys.exit('Requires YouTube routing v7 in ai_command_media_extended.py. Nothing changed.')
if newmodule.is_file() and 'Per-user, per-AI-window media follow-ups' not in newmodule.read_text():
sys.exit('ai_command_context.py already exists but is not this module. Nothing changed.')
s=server.read_text()
if 'from ai_command_context import resolve as resolve_ai_media' in s:
print('Conversation routing already installed; no patch necessary.')
sys.exit(0)
old='from ai_command_media_extended import resolve as resolve_ai_media'
if s.count(old)!=1:
sys.exit(f'Expected a single v4-v7 resolver import, found {s.count(old)}. Nothing changed.')
start=s.find('response = None if route[\'mode\'] == \'remote\' else resolve_ai_media(')
if start<0:
# Support pre-v5 branch, but leave remote routing alone.
start=s.find('response = resolve_ai_media(')
if start<0:sys.exit('Cannot identify AI media call site; nothing changed.')
end=s.find('\n )',start)
if end<0:sys.exit('AI media resolver call layout differs; nothing changed.')
call=s[start:end]
if call.count('iptv_list_channels=iptv_list_channels,')!=1:
sys.exit('Cannot locate media keyword arguments; nothing changed.')
if 'window_id=' in call:sys.exit('window_id already passed; refusing duplicate change.')
patched=s.replace(old,'from ai_command_context import resolve as resolve_ai_media',1)
# Exact call signature changed without touching the user/account auth, remote selector, or frontend.
patched=patched.replace(call,call.replace('iptv_list_channels=iptv_list_channels,','iptv_list_channels=iptv_list_channels,\n window_id=window_id,'),1)
py_compile.compile(str(source),doraise=True)
import ast
ast.parse(patched,filename=str(server))
if a.check:
print('CHECK OK: server patch and new module compatible; no files changed.')
sys.exit(0)
backup=root/('backup-ai-context-v8-'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
backup.mkdir(parents=True,exist_ok=False)
shutil.copy2(server,backup/'stream_server.py')
if newmodule.is_file():shutil.copy2(newmodule,backup/'ai_command_context.py')
shutil.copy2(source,newmodule)
server.write_text(patched)
print('Installed:',newmodule)
print('Updated:',server)
print('Backup:',backup)
print('Restart: sudo systemctl restart mediaserver.service')