#!/usr/bin/env python3 # Strata Next native picker profile v1 """Attach an existing launcher to an explicit ready native deployment.""" import json import os from pathlib import Path import re import stat from urllib.parse import urlsplit def load(path): try: fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW) except FileNotFoundError: return None except OSError: raise RuntimeError('Cannot read private native-picker.json') from None try: with os.fdopen(fd, 'rb') as source: info = os.fstat(source.fileno()) if (not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & 0o077): raise ValueError('file permissions') raw = source.read(16385) if len(raw) > 16384: raise ValueError('size') def unique(pairs): result = {} for key, value in pairs: if key in result: raise ValueError('duplicate key') result[key] = value return result config = json.loads(raw, object_pairs_hook=unique) if (not isinstance(config, dict) or set(config) != {'schema_version', 'mode', 'url', 'model', 'key_file'} or type(config['schema_version']) is not int or config['schema_version'] != 1 or config['mode'] != 'attach'): raise ValueError('schema') url = config['url'] if not isinstance(url, str) or not url.isascii() or any(ord(c) <= 32 or ord(c) == 127 for c in url): raise ValueError('url') parsed = urlsplit(url) if (parsed.scheme != 'http' or parsed.hostname not in {'127.0.0.1', 'localhost', '::1'} or parsed.username or parsed.password or parsed.path or parsed.query or parsed.fragment or parsed.port is None or not 1 <= parsed.port <= 65535): raise ValueError('loopback URL') if not isinstance(config['model'], str) or not re.fullmatch(r'[A-Za-z0-9_.-]{1,180}', config['model']): raise ValueError('model') key = config['key_file'] if key is not None and (not isinstance(key, str) or not Path(key).is_absolute() or any(ord(c) < 32 or ord(c) == 127 for c in key)): raise ValueError('key path') return config except (OSError, ValueError, TypeError, RecursionError): raise RuntimeError('Invalid native-picker.json: use private schema 1 attach profile with loopback URL, model and key_file') from None def attach(profile, *, validate, discover, configure, atomic): config = load(profile / 'native-picker.json') if config is None: return False target = dict(backend='strata-native', url=config['url'], key_file=config['key_file'], managed=False) validate(target) try: state = discover(target) except (OSError, ValueError, KeyError, RuntimeError): raise RuntimeError('Configured native deployment is unavailable; connection unchanged') from None if (state.get('backend') != 'strata-native' or state.get('model') != config['model'] or type(state.get('context')) is not int or state['context'] != 262144 or type(state.get('parallel')) is not int or state['parallel'] != 1): raise RuntimeError('Configured native deployment has a different model or layout; connection unchanged') configure(state) atomic(profile / 'connection.json', json.dumps(target, indent=2) + '\n') print('Підключено обраний native runtime · 1 × 256К · Responses API. Запускай qwen-codex.') return True