Files
Kronic-Krusaders/repair_context_wiring.py
T

68 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Repair v8 chat->context resolver wiring without replacing existing sources."""
import argparse
import ast
import datetime
from pathlib import Path
import re
import shutil
import sys
p = argparse.ArgumentParser()
p.add_argument('--root', default='/home/admin/mediaserver')
p.add_argument('--check', action='store_true')
a = p.parse_args()
root = Path(a.root).expanduser().resolve()
server = root / 'stream_server.py'
context = root / 'ai_command_context.py'
if not server.is_file() or not context.is_file():
sys.exit('ERROR: stream_server.py or ai_command_context.py missing. No changes.')
s = server.read_text()
ctx = context.read_text()
if 'def resolve(' not in ctx or 'window_id=' not in ctx:
sys.exit('ERROR: unexpected ai_command_context.py API. No changes.')
context_import = 'from ai_command_context import resolve as resolve_ai_media'
legacy_import = 'from ai_command_media_extended import resolve as resolve_ai_media'
if context_import not in s and legacy_import not in s:
sys.exit('ERROR: unknown resolver import. No changes.')
if context_import in s and re.search(r"resolve_ai_media\s*\([\s\S]{0,750}?window_id\s*=\s*window_id", s):
print("Already wired: chat handler passes window_id to v8 context resolver.")
sys.exit(0)
pattern = re.compile(r'(?P<prefix>response\s*=\s*(?:None\s+if\s+route\[.mode.\]\s*==\s*.remote.\s+else\s+)?resolve_ai_media\s*\()(?P<args>.*?iptv_list_channels=iptv_list_channels,\s*)(?P<close>\))', re.S)
matches = list(pattern.finditer(s))
if len(matches) != 1:
sys.exit(f'ERROR: expected one chat resolver call, found {len(matches)}. No changes.')
m = matches[0]
args = m.group('args')
if 'window_id=' in args and context_import in s:
print('Already wired: chat handler passes window_id to v8 context resolver.')
sys.exit(0)
if 'window_id=' in args:
sys.exit('ERROR: call has window_id but import is not context resolver. No changes.')
if args.count('iptv_list_channels=iptv_list_channels') != 1:
sys.exit('ERROR: resolver arguments unexpected. No changes.')
if not re.search(r'\bwindow_id\b', s[:m.start()]):
sys.exit('ERROR: window_id variable not initialized before resolver call. No changes.')
if context_import in s and legacy_import in s:
sys.exit('ERROR: both resolver imports exist. No changes.')
newargs = re.sub(r'(iptv_list_channels=iptv_list_channels\s*,?)', lambda match: match.group(1).rstrip().rstrip(',') + ',\n window_id=window_id,', args, count=1)
updated = s[:m.start('args')] + newargs + s[m.end('args'):]
if legacy_import in updated:
if updated.count(legacy_import) != 1:
sys.exit('ERROR: duplicate legacy imports. No changes.')
updated = updated.replace(legacy_import, context_import, 1)
try:
ast.parse(updated, filename=str(server))
except SyntaxError as exc:
sys.exit(f'ERROR: patch failed Python parse: {exc}. No changes.')
print('CHECK OK: resolved chat call and v8 context import; syntax valid.')
if a.check:
sys.exit(0)
backup = root / ('backup-ai-context-wiring-' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S-%f'))
backup.mkdir()
shutil.copy2(server, backup / 'stream_server.py')
server.write_text(updated)
print('UPDATED:', server)
print('BACKUP:', backup)
print('Restart: sudo systemctl restart mediaserver.service')