- _reply_lang zapamiętuje język ostatniej wiadomości pilota; głos wybierany z niego (pl=gosi, en=ryan), a nie z tekstu odpowiedzi (krótkie polskie odpowiedzi często nie mają diakrytyków). - _detect_lang rozszerzony o listy częstych słów PL/EN jako fallback.
1195 lines
47 KiB
Python
1195 lines
47 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
XCoPilot - lokalny co-pilot (drugi pilot / AI) dla X-Plane 12 (llama.cpp)
|
|
|
|
Podlacza sie do lokalnego serwera llama.cpp (OpenAI-compat /v1/chat/completions),
|
|
czyta kontekst lotu z datarefow, a co-pilot ODPOWIADA STRUKTURALNYM JSON-em z
|
|
lista akcji, ktore wtyczka wykonuje NA DATAREFACH / KOMENDACH X-Plane.
|
|
|
|
W odroznieniu od XSayATC (ATC tylko gada), co-pilot realnie STERUJE samolotem:
|
|
podnosi/chowa podwozie, klapy, ustawia autopilota, transponder itd.
|
|
|
|
Kluczowa idea: plik controls.json mapuje NAZWY AKCJI ("gear_up", "set_heading")
|
|
na konkretne datarefy/komendy dla danego samolotu (per-Aircraft match po nazwie
|
|
ACF). AI widzi liste dostepnych akcji i wydaje polecenia uzywajac tylko tych
|
|
nazw - dzieki czemu nie zgaduje nazw datarefow i dziala dla kazdego samolotu.
|
|
|
|
Wymaga: XPPython3 (Python 3.12 + imgui + requests).
|
|
|
|
Konfiguracja: config.json (ustawienia LLM / glos) + controls.json (mapowanie
|
|
akcji na datarefy) obok tego pliku.
|
|
|
|
WAZNE (bezpieczenstwo watkow):
|
|
- Odczyty ORAZ zapisy datarefow (xp.getData*, xp.setData*, xp.command*) MUSZA
|
|
dzialac na glownym watku X-Plane.
|
|
- Watek roboczy robi WYLACZNIE siec (requests) i parsowanie JSON-a. Wynik
|
|
(lista akcji + tekst) trafia do kolejki, a glowna petla (flight loop) je
|
|
wykonuje bezpiecznie.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import threading
|
|
import subprocess
|
|
import time
|
|
|
|
from XPPython3 import xp
|
|
from XPPython3.xp_imgui import Window as ImguiWindow
|
|
|
|
import imgui
|
|
import requests
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Konfiguracja i domyslne wartosci
|
|
# --------------------------------------------------------------------------
|
|
|
|
CONFIG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
|
|
CONTROLS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "controls.json")
|
|
|
|
DEFAULT_CONFIG = {
|
|
"base_url": "http://localhost:8081", # adres serwera llama.cpp
|
|
"model": "", # puste = pobierze pierwszy z /v1/models
|
|
"temperature": 0.3, # nizsza = bardziej deterministyczne akcje
|
|
"max_tokens": 512,
|
|
"system_prompt": (
|
|
"You are a calm, friendly and competent co-pilot (first officer) flying with "
|
|
"the pilot in X-Plane 12. You speak naturally with the pilot in their language: "
|
|
"if the pilot writes or speaks Polish, answer in Polish; if English, answer in "
|
|
"English. You can actually FLY the aircraft: you issue actions that the simulator "
|
|
"executes on datarefs and commands. "
|
|
"You have the pilot's current flight context (position, altitude, speed, heading, "
|
|
"autopilot, gear, flaps, transponder, etc.) and a list of AVAILABLE CONTROLS "
|
|
"(named actions you may use) with their meaning. "
|
|
"IMPORTANT OUTPUT FORMAT - always answer with ONE JSON object and nothing else:\n"
|
|
"{\n"
|
|
" \"say\": \"your natural spoken reply to the pilot\",\n"
|
|
" \"actions\": [\n"
|
|
" {\"control\": \"gear_up\"},\n"
|
|
" {\"control\": \"set_heading\", \"value\": 270}\n"
|
|
" ]\n"
|
|
"}\n"
|
|
"- \"say\" is what you SAY to the pilot: a short, natural, conversational "
|
|
"sentence or two, written exactly as you would speak it (no JSON, no dataref "
|
|
"paths, no technical listing). Confirm what you are doing, e.g. \"Jasne, chowam "
|
|
"podwozie i ustawiam kurs dwa siedem zero.\"\n"
|
|
"- \"actions\" is the list of control actions to execute. Use only names from "
|
|
"the AVAILABLE CONTROLS list; add \"value\" only for controls that need a "
|
|
"parameter.\n"
|
|
"- Never invent control names or dataref paths. Use only names from the list.\n"
|
|
"- If no action is needed (just a question or small talk), set \"actions\": [].\n"
|
|
"- If you cannot safely do what the pilot asks, say so naturally in \"say\" and "
|
|
"set \"actions\": [].\n"
|
|
"- Never put raw dataref paths or JSON syntax inside \"say\".\n"
|
|
"Keep replies short, calm and natural, like a real first officer."
|
|
),
|
|
"include_flight_context": True,
|
|
"no_think": True, # dodaje /no_think do promptu (modele Qwen3 z reasoningiem)
|
|
"window_always_on_top": True,
|
|
# --- Mapowanie akcji -> datarefy ---
|
|
"controls_file": "", # puste = controls.json obok wtyczki
|
|
# Pozwol AI uzywac surowych datarefow/komend bezposrednio w JSON
|
|
# ({"dref": "sim/...", "value": v} / {"command": "sim/..."}). Ryzykowne -
|
|
# AI moze wymyslic bledna sciezke. Zalecane: False (tylko nazwane akcje).
|
|
"allow_raw_actions": False,
|
|
# --- Glos (whisper / piper) ---
|
|
"voice_enabled": True,
|
|
"voice_ptt_mode": "hold", # "hold" | "toggle"
|
|
"whisper_cli": "/usr/bin/whisper-cli",
|
|
"whisper_model": "/home/pali112/models/ggml-small.bin",
|
|
"whisper_language": "auto",
|
|
"record_cmd": ["pw-record", "--format=s16", "--rate=16000", "--channels=1"],
|
|
"piper": "/opt/piper-tts/piper",
|
|
"piper_model": "", # stary klucz (fallback); normalnie uzywa _pl / _en ponizej
|
|
"piper_model_pl": "/home/pali112/models/pl_PL-gosia-medium.onnx",
|
|
"piper_model_en": "/home/pali112/models/en_US-ryan-high.onnx",
|
|
"play_cmd": ["pw-play"],
|
|
"tts_enabled": True,
|
|
}
|
|
|
|
# Dataref -> (nazwa kontekstu, mnoznik, jednostka, czy_int)
|
|
CONTEXT_DATAREFS = [
|
|
("sim/flightmodel/position/latitude", "Latitude", 1.0, "deg", False),
|
|
("sim/flightmodel/position/longitude", "Longitude", 1.0, "deg", False),
|
|
("sim/flightmodel/misc/h_ind", "Alt (baro)", 1.0, "ft", False),
|
|
("sim/flightmodel/position/elevation", "Alt (MSL)", 1.0, "ft", False),
|
|
("sim/flightmodel/position/y_agl", "AGL", 3.28084, "ft", False),
|
|
("sim/flightmodel/position/indicated_airspeed", "IAS", 1.0, "kt", False),
|
|
("sim/flightmodel/position/groundspeed", "GS", 1.943844, "kt", False),
|
|
("sim/flightmodel/position/vh_ind_fpm", "VS", 1.0, "fpm", False),
|
|
("sim/flightmodel/position/mag_psi", "HDG (mag)", 1.0, "deg", False),
|
|
("sim/flightmodel/position/true_psi", "HDG (true)", 1.0, "deg", False),
|
|
("sim/flightmodel/position/true_theta", "Pitch", 1.0, "deg", False),
|
|
("sim/flightmodel/position/phi", "Roll", 1.0, "deg", False),
|
|
("sim/cockpit/radios/transponder_code", "Squawk", 1.0, "", True),
|
|
("sim/cockpit/radios/com1_freq_hz", "COM1", 0.01, "MHz", True),
|
|
("sim/cockpit/radios/com2_freq_hz", "COM2", 0.01, "MHz", True),
|
|
# Autopilot / auto-throttle
|
|
("sim/cockpit2/autopilot/autopilot_on", "AP on", 1.0, "", True),
|
|
("sim/cockpit2/autopilot/autothrottle_on", "ATHR on", 1.0, "", True),
|
|
("sim/cockpit2/autopilot/heading_dial_deg_mag_pilot", "AP HDG", 1.0, "deg", False),
|
|
("sim/cockpit2/autopilot/altitude_dial_ft", "AP ALT", 1.0, "ft", False),
|
|
("sim/cockpit2/autopilot/airspeed_dial_kts_mach", "AP SPD", 1.0, "kt", False),
|
|
]
|
|
|
|
TAILNUM_DATAREF = "sim/aircraft/view/acf_tailnum"
|
|
ACF_PATH_DATAREF = "sim/aircraft/view/acf_path"
|
|
ACF_ICAO_DATAREF = "sim/aircraft/view/acf_ICAO"
|
|
VR_ENABLED_DATAREF = "sim/graphics/VR/enabled"
|
|
|
|
# Typy datarefow XPLM (bitfield)
|
|
XPLM_TYPE_INT = 1
|
|
XPLM_TYPE_FLOAT = 2
|
|
XPLM_TYPE_DOUBLE = 4
|
|
XPLM_TYPE_FLOAT_ARRAY = 8
|
|
XPLM_TYPE_INT_ARRAY = 16
|
|
XPLM_TYPE_DATA = 32
|
|
|
|
# Wykrywanie jezyka (TTS): znaki diakrytyczne + czeste slowa.
|
|
POLISH_CHARS = set("ąćęłńóśźż")
|
|
POLISH_WORDS = {
|
|
"się", "nie", "jest", "to", "że", "na", "co", "jak", "ale", "dla", "po", "do",
|
|
"w", "z", "tak", "będzie", "będziemy", "możesz", "proszę", "dziękuję", "cześć",
|
|
"siema", "siemanko", "dobrze", "ok", "jasne", "rozmawiać", "rozmawiajmy", "ty",
|
|
"ja", "mi", "ci", "tego", "też", "już", "teraz", "wszystko", "może", "dzięki",
|
|
"witaj", "hej", "czym", "pomóc", "pomogę", "wykonuję", "ustawiam", "chowam",
|
|
"wysuwam", "włączam", "wyłączam", "nastaw", "ustaw", "podwozie", "klapy",
|
|
}
|
|
ENGLISH_WORDS = {
|
|
"the", "and", "you", "i", "to", "is", "a", "of", "in", "it", "that", "we",
|
|
"can", "please", "ok", "okay", "hi", "hello", "yes", "no", "what", "do", "this",
|
|
"for", "on", "will", "would", "good", "thanks", "thank", "speak", "talk", "now",
|
|
"let", "us", "me", "your", "my", "are", "be", "set", "heading", "gear",
|
|
"flaps", "altitude", "speed",
|
|
}
|
|
|
|
|
|
class XCoPilot:
|
|
def __init__(self):
|
|
self.config = self._load_config()
|
|
self.controls = {"controls": {}, "state": {}} # aktualne mapowanie (po matchu)
|
|
self._controls_raw = {} # surowy controls.json
|
|
self._controls_mtime = 0.0
|
|
|
|
self.datarefs = {} # name -> dataref object (CONTEXT_DATAREFS)
|
|
self._resolved_datarefs = False
|
|
self._tailnum_ref = None
|
|
self._acf_path_ref = None
|
|
self._acf_icao_ref = None
|
|
self._vr_ref = None
|
|
|
|
self._cmd_cache = {} # command name -> command ref
|
|
|
|
# Stan rozmowy
|
|
self.messages = [] # {"role": ..., "text": ...}
|
|
self.activity = [] # log wykonanych akcji (do okna)
|
|
self.pending_text = ""
|
|
self.status = "Gotowy."
|
|
self.streaming = False
|
|
|
|
self._model_cache = None
|
|
self._stop_event = threading.Event()
|
|
self._worker = None
|
|
|
|
# Kolejka akcji (wypelnia watek roboczy, oproznia glowny watek)
|
|
self._pending_actions = [] # lista dictow {"control"/"dref"/"command", "value", ...}
|
|
self._pending_say = None # tekst do TTS (z "say")
|
|
|
|
# Okna ImGui
|
|
self._imgui_window = None
|
|
self._config_window = None
|
|
|
|
# Menu
|
|
self._menu = None
|
|
self._menu_cfg_idx = None
|
|
|
|
# VR
|
|
self._last_vr = None
|
|
|
|
# Glos (STT / TTS)
|
|
self._recording = False
|
|
self._record_proc = None
|
|
self._ptt_toggled = False
|
|
self._pending_send = None # tekst ze STT do wyslania na glownym watku
|
|
self._speak_lock = threading.Lock()
|
|
self._reply_lang = None # jezyk ostatniej wypowiedzi pilota ('pl'/'en')
|
|
|
|
# Cache aktualnego samolotu (acf path / icao)
|
|
self._acf_cache = None # (path, icao, ts)
|
|
|
|
# ---------------------------- Config ----------------------------
|
|
def _load_config(self):
|
|
cfg = dict(DEFAULT_CONFIG)
|
|
try:
|
|
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
if isinstance(data, dict):
|
|
cfg.update(data)
|
|
except Exception:
|
|
pass
|
|
return cfg
|
|
|
|
def _save_config(self):
|
|
try:
|
|
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
|
json.dump(self.config, f, ensure_ascii=False, indent=2)
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad zapisu config.json: {e}")
|
|
|
|
def _controls_path(self):
|
|
p = self.config.get("controls_file", "")
|
|
return p or CONTROLS_FILE
|
|
|
|
# ---------------------------- Mapowanie akcji (controls.json) ----------------------------
|
|
def _load_controls(self, force=False):
|
|
"""Wczytuje controls.json i wybiera mapowanie dla aktualnego samolotu."""
|
|
path = self._controls_path()
|
|
try:
|
|
mtime = os.path.getmtime(path)
|
|
except Exception:
|
|
mtime = 0.0
|
|
if not force and self._controls_raw and mtime == self._controls_mtime:
|
|
self._select_controls()
|
|
return
|
|
try:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
raw = json.load(f)
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad wczytywania controls.json ({path}): {e}")
|
|
raw = {}
|
|
self._controls_raw = raw if isinstance(raw, dict) else {}
|
|
self._controls_mtime = mtime
|
|
self._select_controls()
|
|
|
|
def _select_controls(self):
|
|
"""Wyznacza aktywne mapowanie: default + (opcjonalny) override per-aircraft."""
|
|
default = self._controls_raw.get("default", {}) or {}
|
|
base_controls = dict(default.get("controls", {}) or {})
|
|
base_state = dict(default.get("state", {}) or {})
|
|
|
|
path, icao = self._current_acf()
|
|
chosen = None
|
|
for ac in self._controls_raw.get("aircraft", []) or []:
|
|
if not isinstance(ac, dict):
|
|
continue
|
|
match = (ac.get("match") or "").lower()
|
|
if not match:
|
|
continue
|
|
haystack = ((path or "") + " " + (icao or "")).lower()
|
|
if match in haystack:
|
|
chosen = ac
|
|
break
|
|
|
|
if chosen:
|
|
# override: nadpisuje/rozszerza default (per-klucz)
|
|
for k, v in (chosen.get("controls", {}) or {}).items():
|
|
base_controls[k] = v
|
|
for k, v in (chosen.get("state", {}) or {}).items():
|
|
base_state[k] = v
|
|
|
|
self.controls = {"controls": base_controls, "state": base_state}
|
|
self._matched_acf = chosen.get("match", "default") if chosen else "default"
|
|
|
|
def _current_acf(self, force=False):
|
|
"""Zwraca (path, icao) aktualnego samolotu (cache 5 s)."""
|
|
now = time.time()
|
|
if not force and self._acf_cache is not None:
|
|
p, i, ts = self._acf_cache
|
|
if now - ts < 5.0:
|
|
return p, i
|
|
path = self._read_string(self._acf_path_ref) or ""
|
|
icao = self._read_string(self._acf_icao_ref) or ""
|
|
self._acf_cache = (path, icao, now)
|
|
return path, icao
|
|
|
|
# ---------------------------- Datarefs ----------------------------
|
|
def _ensure_datarefs(self):
|
|
if self._resolved_datarefs:
|
|
return
|
|
for name, _l, _m, _u, _i in CONTEXT_DATAREFS:
|
|
self.datarefs[name] = xp.findDataRef(name)
|
|
self._tailnum_ref = xp.findDataRef(TAILNUM_DATAREF)
|
|
self._acf_path_ref = xp.findDataRef(ACF_PATH_DATAREF)
|
|
self._acf_icao_ref = xp.findDataRef(ACF_ICAO_DATAREF)
|
|
self._vr_ref = xp.findDataRef(VR_ENABLED_DATAREF)
|
|
self._resolved_datarefs = True
|
|
|
|
def _get_dataref(self, name, is_float=True):
|
|
d = self.datarefs.get(name)
|
|
if d is None:
|
|
return None
|
|
try:
|
|
if is_float:
|
|
return xp.getDataf(d)
|
|
return xp.getDatai(d)
|
|
except Exception:
|
|
return None
|
|
|
|
def _read_string(self, dref):
|
|
if dref is None:
|
|
return None
|
|
try:
|
|
return xp.getDatas(dref)
|
|
except Exception:
|
|
return None
|
|
|
|
def _ref_type_bits(self, ref):
|
|
try:
|
|
return int(xp.getDataRefTypes(ref))
|
|
except Exception:
|
|
return 0
|
|
|
|
def _find_dref(self, path):
|
|
try:
|
|
return xp.findDataRef(path)
|
|
except Exception:
|
|
return None
|
|
|
|
def _find_cmd(self, path):
|
|
if path in self._cmd_cache:
|
|
return self._cmd_cache[path]
|
|
try:
|
|
ref = xp.findCommand(path)
|
|
except Exception:
|
|
ref = None
|
|
self._cmd_cache[path] = ref
|
|
return ref
|
|
|
|
# ---------------------------- Kontekst lotu (TYLKO glowny watek) ----------------------------
|
|
def _collect_context(self):
|
|
"""Zbiera kontekst lotu + liste dostepnych akcji. TYLKO glowny watek."""
|
|
ctx = {}
|
|
lines = []
|
|
|
|
tail = self._read_string(self._tailnum_ref)
|
|
if tail and tail.strip():
|
|
ctx["Callsign"] = tail.strip()
|
|
lines.append("Callsign: " + tail.strip())
|
|
|
|
path, icao = self._current_acf()
|
|
if path:
|
|
ctx["Aircraft"] = os.path.basename(path)
|
|
lines.append("Aircraft: " + os.path.basename(path))
|
|
|
|
for name, label, mult, unit, is_int in CONTEXT_DATAREFS:
|
|
v = self._get_dataref(name, is_float=not is_int)
|
|
if v is None:
|
|
continue
|
|
if is_int:
|
|
val = int(v)
|
|
ctx[label] = val
|
|
lines.append(f"{label}: {val}")
|
|
else:
|
|
val = v * mult
|
|
ctx[label] = val
|
|
unit_s = (" " + unit) if unit else ""
|
|
lines.append(f"{label}: {val:.1f}{unit_s}")
|
|
|
|
# Stan z mapowania (state: nazwa -> dataref)
|
|
state_lines = self._collect_state()
|
|
if state_lines:
|
|
lines.append("Control state:")
|
|
lines.extend(state_lines)
|
|
ctx["ControlState"] = state_lines
|
|
|
|
# Lista dostepnych akcji (z opisami) — kluczowa dla AI
|
|
avail = self._available_controls_desc()
|
|
if avail:
|
|
lines.append("AVAILABLE CONTROLS (use these exact names):")
|
|
lines.extend(avail)
|
|
ctx["AvailableControls"] = avail
|
|
|
|
return ctx, "\n".join(lines)
|
|
|
|
def _collect_state(self):
|
|
"""Czyta datarefy stanu z mapowania 'state'. Zwraca liste linii."""
|
|
out = []
|
|
for name, dref_path in self.controls.get("state", {}).items():
|
|
if not dref_path:
|
|
continue
|
|
ref = self._find_dref(dref_path)
|
|
if ref is None:
|
|
continue
|
|
bits = self._ref_type_bits(ref)
|
|
try:
|
|
if bits & XPLM_TYPE_DATA:
|
|
v = xp.getDatas(ref)
|
|
out.append(f" {name}: {v}")
|
|
elif bits & (XPLM_TYPE_INT | XPLM_TYPE_INT_ARRAY):
|
|
out.append(f" {name}: {int(xp.getDatai(ref))}")
|
|
else:
|
|
out.append(f" {name}: {float(xp.getDataf(ref)):.3f}")
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad odczytu stanu {name} ({dref_path}): {e}")
|
|
return out
|
|
|
|
def _available_controls_desc(self):
|
|
"""Zwraca liste linii opisujacych dostepne akcje (nazwa + opis + czy wymaga value)."""
|
|
out = []
|
|
for name, ctrl in self.controls.get("controls", {}).items():
|
|
if not isinstance(ctrl, dict):
|
|
continue
|
|
desc = ctrl.get("desc", "")
|
|
needs_value = self._control_needs_value(ctrl)
|
|
suffix = " (requires value)" if needs_value else ""
|
|
out.append(f" {name}{suffix}: {desc}")
|
|
return out
|
|
|
|
def _control_needs_value(self, ctrl):
|
|
for op in ctrl.get("ops", []) or []:
|
|
v = op.get("value")
|
|
if isinstance(v, str) and "{value}" in v:
|
|
return True
|
|
return False
|
|
|
|
# ---------------------------- Wykonywanie akcji (TYLKO glowny watek) ----------------------------
|
|
def _execute_action(self, action):
|
|
"""Wykonuje pojedyncza akcje z JSON-a. Zwraca opis (string) lub None.
|
|
|
|
Obslugiwane formy:
|
|
{"control": "nazwa", "value": v} -> mapowanie controls.json
|
|
{"dref": "sim/...", "value": v} -> surowy zapis (gdy allow_raw_actions)
|
|
{"command": "sim/..."} -> surowa komenda (gdy allow_raw_actions)
|
|
"""
|
|
if "control" in action:
|
|
return self._execute_named(action.get("control"), action.get("value"))
|
|
if action.get("dref") and self.config.get("allow_raw_actions", False):
|
|
return self._execute_set_dref(action["dref"], action.get("value"))
|
|
if action.get("command") and self.config.get("allow_raw_actions", False):
|
|
ref = self._find_cmd(action["command"])
|
|
if ref is None:
|
|
return f"command not found: {action['command']}"
|
|
try:
|
|
xp.commandOnce(ref)
|
|
except Exception as e:
|
|
return f"command error: {e}"
|
|
return f"cmd {action['command']}"
|
|
return None
|
|
|
|
def _execute_named(self, name, value):
|
|
ctrl = self.controls.get("controls", {}).get(name)
|
|
if not isinstance(ctrl, dict):
|
|
return f"unknown control: {name}"
|
|
ops = ctrl.get("ops", []) or []
|
|
results = []
|
|
for op in ops:
|
|
if not isinstance(op, dict):
|
|
continue
|
|
if op.get("cmd"):
|
|
ref = self._find_cmd(op["cmd"])
|
|
if ref is None:
|
|
results.append(f"{op['cmd']} (not found)")
|
|
continue
|
|
try:
|
|
xp.commandOnce(ref)
|
|
results.append(op["cmd"])
|
|
except Exception as e:
|
|
results.append(f"{op['cmd']} err:{e}")
|
|
elif op.get("set"):
|
|
v = self._resolve_op_value(op["value"], value)
|
|
r = self._execute_set_dref(op["set"], v)
|
|
results.append(r or op["set"])
|
|
elif op.get("toggle"):
|
|
r = self._execute_toggle_dref(op["toggle"])
|
|
results.append(r or op["toggle"])
|
|
return "; ".join(results) if results else f"{name} (no ops)"
|
|
|
|
def _resolve_op_value(self, op_value, action_value):
|
|
"""Op value moze byc literalem albo szablonem '{value}'."""
|
|
if isinstance(op_value, str) and "{value}" in op_value:
|
|
return action_value
|
|
return op_value
|
|
|
|
def _execute_set_dref(self, dref_path, value):
|
|
ref = self._find_dref(dref_path)
|
|
if ref is None:
|
|
return f"dref not found: {dref_path}"
|
|
bits = self._ref_type_bits(ref)
|
|
try:
|
|
if bits & XPLM_TYPE_DATA:
|
|
xp.setDatas(ref, str(value))
|
|
return f"{dref_path}={value}"
|
|
if bits & (XPLM_TYPE_INT | XPLM_TYPE_INT_ARRAY):
|
|
iv = int(round(float(value)))
|
|
xp.setDatai(ref, iv)
|
|
return f"{dref_path}={iv}"
|
|
fv = float(value)
|
|
xp.setDataf(ref, fv)
|
|
return f"{dref_path}={fv}"
|
|
except Exception as e:
|
|
return f"{dref_path} err:{e}"
|
|
|
|
def _execute_toggle_dref(self, dref_path):
|
|
ref = self._find_dref(dref_path)
|
|
if ref is None:
|
|
return f"dref not found: {dref_path}"
|
|
bits = self._ref_type_bits(ref)
|
|
try:
|
|
if bits & (XPLM_TYPE_INT | XPLM_TYPE_INT_ARRAY):
|
|
cur = int(xp.getDatai(ref))
|
|
nv = 0 if cur else 1
|
|
xp.setDatai(ref, nv)
|
|
return f"{dref_path}->{nv}"
|
|
cur = float(xp.getDataf(ref))
|
|
nv = 0.0 if cur >= 0.5 else 1.0
|
|
xp.setDataf(ref, nv)
|
|
return f"{dref_path}->{nv}"
|
|
except Exception as e:
|
|
return f"{dref_path} err:{e}"
|
|
|
|
# ---------------------------- Komunikacja z LLM ----------------------------
|
|
def _resolve_model(self):
|
|
if self._model_cache:
|
|
return self._model_cache
|
|
if self.config.get("model"):
|
|
self._model_cache = self.config["model"]
|
|
return self._model_cache
|
|
try:
|
|
r = requests.get(self.config["base_url"].rstrip("/") + "/v1/models", timeout=5)
|
|
r.raise_for_status()
|
|
models = (r.json().get("data") or [])
|
|
if models:
|
|
self._model_cache = models[0].get("id")
|
|
return self._model_cache
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Nie mozna pobrac modelu: {e}")
|
|
return None
|
|
|
|
def _build_messages(self, user_text, flight_ctx_text):
|
|
msgs = []
|
|
sp = self.config.get("system_prompt", "")
|
|
if self.config.get("no_think", True) and sp and "/no_think" not in sp:
|
|
sp = sp + "\n/no_think"
|
|
msgs.append({"role": "system", "content": sp})
|
|
|
|
if self.config.get("include_flight_context", True) and flight_ctx_text:
|
|
msgs.append({"role": "system", "content":
|
|
"Pilot's current flight context:\n" + flight_ctx_text})
|
|
|
|
for m in self.messages[-12:]:
|
|
msgs.append({"role": m["role"], "content": m["text"]})
|
|
|
|
msgs.append({"role": "user", "content": user_text})
|
|
return msgs
|
|
|
|
def _request(self, messages):
|
|
url = self.config["base_url"].rstrip("/") + "/v1/chat/completions"
|
|
model = self._resolve_model() or self.config.get("model") or "model"
|
|
payload = {
|
|
"model": model,
|
|
"messages": messages,
|
|
"temperature": self.config.get("temperature", 0.3),
|
|
"max_tokens": self.config.get("max_tokens", 512),
|
|
"stream": True,
|
|
}
|
|
return requests.post(url, json=payload, stream=True, timeout=(10, 120))
|
|
|
|
def _run_stream(self, user_text, flight_ctx_text):
|
|
"""Watek roboczy: TYLKO siec + parsowanie. Bez xp.getData*/setData*."""
|
|
self.streaming = True
|
|
self.status = "Co-pilot mysli..."
|
|
|
|
self.messages.append({"role": "user", "text": user_text})
|
|
self.messages.append({"role": "assistant", "text": ""})
|
|
|
|
try:
|
|
messages = self._build_messages(user_text, flight_ctx_text)
|
|
resp = self._request(messages)
|
|
resp.raise_for_status()
|
|
|
|
buf = ""
|
|
for raw in resp.iter_lines(decode_unicode=True):
|
|
if self._stop_event.is_set():
|
|
break
|
|
if not raw or not raw.startswith("data:"):
|
|
continue
|
|
data = raw[len("data:"):].strip()
|
|
if data == "[DONE]":
|
|
break
|
|
try:
|
|
chunk = json.loads(data)
|
|
except Exception:
|
|
continue
|
|
choices = chunk.get("choices") or []
|
|
if not choices:
|
|
continue
|
|
delta = choices[0].get("delta") or {}
|
|
content = delta.get("content")
|
|
if content:
|
|
buf += content
|
|
self.messages[-1]["text"] = buf
|
|
|
|
final = self.messages[-1]["text"].strip()
|
|
if not final:
|
|
final = "(brak odpowiedzi)"
|
|
self.messages[-1]["text"] = final
|
|
# Sparsuj JSON, przygotuj akcje i tekst do powiedzenia.
|
|
say, actions = self._parse_response(final)
|
|
self._pending_actions = actions
|
|
self._pending_say = say
|
|
# W oknie pokaz naturalna wypowiedz co-pilota, nie surowy JSON.
|
|
if say:
|
|
self.messages[-1]["text"] = say
|
|
self.status = "Gotowy."
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad zapytania: {e}")
|
|
self.status = f"Blad: {e}"
|
|
if self.messages and self.messages[-1]["role"] == "assistant":
|
|
if not self.messages[-1]["text"]:
|
|
self.messages[-1]["text"] = f"(blad: {e})"
|
|
finally:
|
|
self.streaming = False
|
|
|
|
def _parse_response(self, text):
|
|
"""Wyciaga z odpowiedzi (tekst + JSON) pare (say, actions)."""
|
|
# Tolerancyjne wyciagniecie pierwszego obiektu JSON
|
|
obj = self._extract_json(text)
|
|
if obj is None:
|
|
# Nie udalo sie sparsowac — traktuj calosc jako zwykla wypowiedz,
|
|
# bez akcji.
|
|
return text, []
|
|
say = obj.get("say") if isinstance(obj, dict) else None
|
|
actions = obj.get("actions") if isinstance(obj, dict) else None
|
|
if not isinstance(actions, list):
|
|
actions = []
|
|
# Jesli brak "say", zbuduj krotki opis z akcji (fallback do TTS/okna).
|
|
if not say:
|
|
say = self._describe_actions(actions) or "OK."
|
|
return say, actions
|
|
|
|
def _extract_json(self, text):
|
|
"""Zwraca pierwszy obiekt JSON w tekscie (albo None). Obsluguje code fence."""
|
|
# Zdejmij ewentualny code fence ```json ... ```
|
|
t = text.strip()
|
|
if t.startswith("```"):
|
|
lines = t.split("\n")
|
|
if lines and lines[0].startswith("```"):
|
|
lines = lines[1:]
|
|
if lines and lines[-1].strip() == "```":
|
|
lines = lines[:-1]
|
|
t = "\n".join(lines).strip()
|
|
# Proba bezposredniego parsowania calosci
|
|
try:
|
|
return json.loads(t)
|
|
except Exception:
|
|
pass
|
|
# Znajdz pierwszy '{' i pasujacy '}' (balans nawiasow)
|
|
start = t.find("{")
|
|
if start < 0:
|
|
return None
|
|
depth = 0
|
|
in_str = False
|
|
esc = False
|
|
for i in range(start, len(t)):
|
|
c = t[i]
|
|
if in_str:
|
|
if esc:
|
|
esc = False
|
|
elif c == "\\":
|
|
esc = True
|
|
elif c == '"':
|
|
in_str = False
|
|
continue
|
|
if c == '"':
|
|
in_str = True
|
|
elif c == "{":
|
|
depth += 1
|
|
elif c == "}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
try:
|
|
return json.loads(t[start:i + 1])
|
|
except Exception:
|
|
return None
|
|
return None
|
|
|
|
def _describe_actions(self, actions):
|
|
if not actions:
|
|
return ""
|
|
parts = []
|
|
for a in actions:
|
|
if isinstance(a, dict):
|
|
name = a.get("control") or a.get("dref") or a.get("command") or "?"
|
|
if a.get("value") is not None:
|
|
parts.append(f"{name}={a['value']}")
|
|
else:
|
|
parts.append(str(name))
|
|
else:
|
|
parts.append(str(a))
|
|
return "Wykonuje: " + ", ".join(parts)
|
|
|
|
def send(self, text):
|
|
"""Wywolywane na glownym watku (przycisk / PTT)."""
|
|
text = (text or "").strip()
|
|
if not text or self.streaming:
|
|
return
|
|
# Zapamietaj jezyk wypowiedzi PILOTA — to on okresla glos odpowiedzi.
|
|
self._reply_lang = self._detect_lang(text)
|
|
self._dispatch(text)
|
|
|
|
def _dispatch(self, user_text):
|
|
if self.streaming:
|
|
return
|
|
self.streaming = True
|
|
|
|
# Odswiez mapowanie (mozesz edytowac controls.json w locie).
|
|
self._load_controls()
|
|
|
|
flight_ctx_text = None
|
|
if self.config.get("include_flight_context", True):
|
|
try:
|
|
_ctx, flight_ctx_text = self._collect_context()
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad zbierania kontekstu: {e}")
|
|
flight_ctx_text = None
|
|
|
|
self._stop_event.clear()
|
|
self._worker = threading.Thread(
|
|
target=self._run_stream, args=(user_text, flight_ctx_text), daemon=True)
|
|
self._worker.start()
|
|
|
|
def stop(self):
|
|
self._stop_event.set()
|
|
|
|
# ---------------------------- Wykonywanie akcji na glownym watku ----------------------------
|
|
def _process_pending_actions(self):
|
|
"""Oproznia kolejke akcji (wywolywane z flight loop na glownym watku)."""
|
|
if not self._pending_actions:
|
|
return
|
|
actions = self._pending_actions
|
|
self._pending_actions = []
|
|
for a in actions:
|
|
try:
|
|
desc = self._execute_action(a)
|
|
except Exception as e:
|
|
desc = f"blad akcji: {e}"
|
|
if desc:
|
|
self.activity.append(desc)
|
|
xp.log(f"[XCoPilot] akcja: {desc}")
|
|
# ogranicz log do ostatnich 200 wpisow
|
|
if len(self.activity) > 200:
|
|
self.activity = self.activity[-200:]
|
|
|
|
def _process_pending_say(self):
|
|
"""Czyta tekst do powiedzenia (TTS). Wywolywane na glownym watku."""
|
|
if self._pending_say is None:
|
|
return
|
|
say = self._pending_say
|
|
self._pending_say = None
|
|
if say and self.config.get("tts_enabled", True):
|
|
threading.Thread(target=self._speak, args=(say,), daemon=True).start()
|
|
|
|
# ---------------------------- Glos (STT / TTS) ----------------------------
|
|
def _voice_enabled(self):
|
|
return bool(self.config.get("voice_enabled", True))
|
|
|
|
def _start_recording(self):
|
|
if self._recording:
|
|
return
|
|
import tempfile
|
|
self._record_file = tempfile.mktemp(suffix=".wav", prefix="xcopilot_")
|
|
cmd = list(self.config.get("record_cmd", ["pw-record", "--format=s16", "--rate=16000", "--channels=1"]))
|
|
cmd.append(self._record_file)
|
|
try:
|
|
self._record_proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
self._recording = True
|
|
self.status = "Nagrywanie... (pusc PTT aby wyslac)"
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad nagrywania: {e}")
|
|
self._record_proc = None
|
|
|
|
def _stop_recording_and_send(self):
|
|
if not self._recording:
|
|
return
|
|
self._recording = False
|
|
if self._record_proc is not None:
|
|
try:
|
|
self._record_proc.send_signal(2) # SIGINT -> domkniecie WAV
|
|
try:
|
|
self._record_proc.wait(timeout=3)
|
|
except Exception:
|
|
self._record_proc.kill()
|
|
self._record_proc.wait(timeout=2)
|
|
except Exception:
|
|
pass
|
|
self._record_proc = None
|
|
|
|
wav = getattr(self, "_record_file", None)
|
|
if not wav or not os.path.exists(wav):
|
|
self.status = "Blad: brak nagrania"
|
|
return
|
|
self.status = "Transkrybuje (whisper)..."
|
|
threading.Thread(target=self._transcribe_and_send, args=(wav,), daemon=True).start()
|
|
|
|
def _transcribe_and_send(self, wav):
|
|
text = self._transcribe(wav)
|
|
try:
|
|
os.remove(wav)
|
|
except Exception:
|
|
pass
|
|
if text:
|
|
self._pending_send = text
|
|
else:
|
|
self.status = "Nie rozpoznano mowy."
|
|
|
|
def _transcribe(self, wav_path):
|
|
import tempfile
|
|
out_file = tempfile.mktemp(suffix="", prefix="xcopilot_stt_")
|
|
cli = self.config.get("whisper_cli", "/usr/bin/whisper-cli")
|
|
model = self.config.get("whisper_model", "")
|
|
lang = self.config.get("whisper_language", "auto")
|
|
cmd = [cli, "-m", model, "-l", lang, "-otxt", "-of", out_file, "-f", wav_path, "-np"]
|
|
try:
|
|
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=60)
|
|
txt_file = out_file + ".txt"
|
|
if os.path.exists(txt_file):
|
|
with open(txt_file, "r", encoding="utf-8") as f:
|
|
text = f.read().strip()
|
|
try:
|
|
os.remove(txt_file)
|
|
except Exception:
|
|
pass
|
|
return text
|
|
return ""
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad whisper: {e}")
|
|
return ""
|
|
|
|
def _clean_env(self):
|
|
env = dict(os.environ)
|
|
env.pop("LD_PRELOAD", None)
|
|
env.pop("LD_LIBRARY_PATH", "")
|
|
env["LD_LIBRARY_PATH"] = "/opt/piper-tts"
|
|
return env
|
|
|
|
def _speak(self, text):
|
|
if not self.config.get("tts_enabled", True):
|
|
return
|
|
text = (text or "").strip()
|
|
if not text:
|
|
return
|
|
if not self._speak_lock.acquire(blocking=False):
|
|
return
|
|
try:
|
|
# Jezyk: preferuj jezyk ostatniej wypowiedzi PILOTA (niezawodny),
|
|
# fallback na detekcje samego tekstu do przeczytania.
|
|
lang = getattr(self, "_reply_lang", None) or self._detect_lang(text)
|
|
self._speak_locked(text, lang)
|
|
finally:
|
|
self._speak_lock.release()
|
|
|
|
def _detect_lang(self, text):
|
|
"""Wykrywa jezyk: 'pl' lub 'en' (znaki diakrytyczne + lista czestych slow)."""
|
|
t = (text or "").lower()
|
|
if any(ch in POLISH_CHARS for ch in t):
|
|
return "pl"
|
|
words = re.findall(r"[a-ząćęłńóśźż]+", t)
|
|
if not words:
|
|
return "en"
|
|
pl = sum(1 for w in words if w in POLISH_WORDS)
|
|
en = sum(1 for w in words if w in ENGLISH_WORDS)
|
|
if pl > en:
|
|
return "pl"
|
|
if en > pl:
|
|
return "en"
|
|
return "en"
|
|
|
|
def _speak_locked(self, text, lang):
|
|
piper = self.config.get("piper", "/opt/piper-tts/piper")
|
|
# Wybierz glos wg jezyka: pl -> gosia, en -> ryan.
|
|
if lang == "pl":
|
|
model = self.config.get("piper_model_pl") or self.config.get("piper_model", "")
|
|
else:
|
|
model = self.config.get("piper_model_en") or self.config.get("piper_model", "")
|
|
if not model:
|
|
xp.log("[XCoPilot] TTS: brak modelu piper (piper_model_pl/_en).")
|
|
return
|
|
import tempfile
|
|
wav = tempfile.mktemp(suffix=".wav", prefix="xcopilot_tts_")
|
|
env = self._clean_env()
|
|
try:
|
|
p = subprocess.Popen(
|
|
[piper, "-m", model, "-f", wav],
|
|
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
|
|
env=env)
|
|
p.communicate(input=text.encode("utf-8"), timeout=60)
|
|
if os.path.exists(wav) and os.path.getsize(wav) > 0:
|
|
play_cmd = list(self.config.get("play_cmd", ["pw-play"]))
|
|
subprocess.run(play_cmd + [wav], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=60, env=env)
|
|
try:
|
|
os.remove(wav)
|
|
except Exception:
|
|
pass
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad TTS: {e}")
|
|
|
|
# ---------------------------- VR ----------------------------
|
|
def _vr_flight_loop(self, lastCall, elapsed, counter, refCon):
|
|
"""Co 1s: VR, wykonanie akcji, TTS, podniesienie STT (glowny watek)."""
|
|
vr = self._get_vr_enabled()
|
|
if vr != self._last_vr:
|
|
self._last_vr = vr
|
|
self._apply_vr_mode(vr)
|
|
# Wykonaj zakolejkowane akcje (datarefy! musza byc na glownym watku)
|
|
try:
|
|
self._process_pending_actions()
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad wykonania akcji: {e}")
|
|
# Powiedz na glos, co co-pilot zrobil
|
|
try:
|
|
self._process_pending_say()
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad TTS dispatch: {e}")
|
|
# Podnies tekst ze STT
|
|
if self._pending_send is not None:
|
|
txt = self._pending_send
|
|
self._pending_send = None
|
|
self.send(txt)
|
|
return 1.0
|
|
|
|
def _get_vr_enabled(self):
|
|
if self._vr_ref is None:
|
|
return 0
|
|
try:
|
|
return xp.getDatai(self._vr_ref)
|
|
except Exception:
|
|
return 0
|
|
|
|
def _apply_vr_mode(self, vr):
|
|
for win in (self._imgui_window, self._config_window):
|
|
if win is None or win.windowID is None:
|
|
continue
|
|
try:
|
|
if vr:
|
|
xp.setWindowPositioningMode(win.windowID, xp.WindowVR, -1)
|
|
xp.setWindowGeometryVR(win.windowID, 800, 900)
|
|
else:
|
|
xp.setWindowPositioningMode(win.windowID, xp.WindowPositionFree, -1)
|
|
except Exception as e:
|
|
xp.log(f"[XCoPilot] Blad ustawiania VR: {e}")
|
|
|
|
# ---------------------------- Okno ImGui ----------------------------
|
|
def _draw(self, windowID, refCon):
|
|
imgui.text("XCoPilot - AI co-pilot")
|
|
imgui.separator()
|
|
|
|
avail_h = imgui.get_content_region_available()[1]
|
|
chat_h = max(60.0, avail_h - 260.0)
|
|
with imgui.begin_child("chat", 0, chat_h, border=True):
|
|
for m in self.messages:
|
|
if m["role"] == "user":
|
|
imgui.text_colored("Pilot: " + m["text"], 0.5, 0.8, 1.0, 1.0)
|
|
else:
|
|
imgui.text_wrapped("Co-pilot: " + m["text"])
|
|
if self.streaming:
|
|
imgui.text_colored("...", 0.9, 0.9, 0.3, 1.0)
|
|
imgui.set_scroll_here_y(1.0)
|
|
|
|
changed, self.pending_text = imgui.input_text(
|
|
"##msg", self.pending_text, 512,
|
|
flags=imgui.INPUT_TEXT_ENTER_RETURNS_TRUE)
|
|
imgui.same_line()
|
|
if imgui.button("Wyslij (PTT)") or (changed and self.pending_text.strip()):
|
|
self.send(self.pending_text)
|
|
self.pending_text = ""
|
|
imgui.same_line()
|
|
if imgui.button("Stop"):
|
|
self.stop()
|
|
|
|
imgui.text(self.status)
|
|
imgui.separator()
|
|
|
|
# Aktualny samolot + match
|
|
path, _ = self._current_acf()
|
|
imgui.text("Samolot: " + (os.path.basename(path) if path else "?"))
|
|
imgui.text("Mapping: " + getattr(self, "_matched_acf", "default"))
|
|
if imgui.button("Przeladuj controls.json"):
|
|
self._load_controls(force=True)
|
|
self.status = "controls.json przeladowany."
|
|
|
|
imgui.separator()
|
|
# Log wykonanych akcji
|
|
imgui.text("Wykonane akcje:")
|
|
with imgui.begin_child("activity", 0, 100.0, border=True):
|
|
for ln in self.activity[-50:]:
|
|
imgui.text_wrapped(ln)
|
|
|
|
def _draw_config(self, windowID, refCon):
|
|
imgui.text("Konfiguracja XCoPilot")
|
|
imgui.separator()
|
|
|
|
_, self.config["base_url"] = imgui.input_text("Base URL", self.config["base_url"], 256)
|
|
_, self.config["model"] = imgui.input_text("Model (puste=auto)", self.config["model"], 512)
|
|
_, self.config["temperature"] = imgui.slider_float("Temperature", self.config["temperature"], 0.0, 2.0, "%.2f")
|
|
_, self.config["max_tokens"] = imgui.slider_int("Max tokens", self.config["max_tokens"], 64, 4096)
|
|
_, self.config["no_think"] = imgui.checkbox("Tryb /no_think (Qwen3)", self.config["no_think"])
|
|
_, self.config["include_flight_context"] = imgui.checkbox("Dolacz kontekst lotu", self.config["include_flight_context"])
|
|
_, self.config["allow_raw_actions"] = imgui.checkbox(
|
|
"Pozwol AI na surowe datarefy/komendy (ryzykowne)", self.config["allow_raw_actions"])
|
|
|
|
imgui.separator()
|
|
imgui.text("Mapowanie akcji:")
|
|
_, self.config["controls_file"] = imgui.input_text(
|
|
"Plik controls.json", self.config["controls_file"], 512)
|
|
|
|
imgui.separator()
|
|
imgui.text("Glos (whisper / piper):")
|
|
_, self.config["voice_enabled"] = imgui.checkbox("Glos wlaczony (STT)", self.config["voice_enabled"])
|
|
_, self.config["tts_enabled"] = imgui.checkbox("Odczytywanie odpowiedzi (TTS)", self.config["tts_enabled"])
|
|
_, self.config["whisper_model"] = imgui.input_text("Model whisper", self.config["whisper_model"], 512)
|
|
_, self.config["piper_model_pl"] = imgui.input_text(
|
|
"Glos PL (gosia)", self.config["piper_model_pl"], 512)
|
|
_, self.config["piper_model_en"] = imgui.input_text(
|
|
"Glos EN (ryan)", self.config["piper_model_en"], 512)
|
|
|
|
imgui.separator()
|
|
imgui.text("System prompt (osobowosc i format JSON):")
|
|
_, self.config["system_prompt"] = imgui.input_text_multiline(
|
|
"##sysprompt", self.config["system_prompt"], 4096, width=0, height=120)
|
|
|
|
imgui.separator()
|
|
if imgui.button("Zapisz"):
|
|
self._save_config()
|
|
self._model_cache = None
|
|
self.status = "Konfiguracja zapisana."
|
|
|
|
imgui.separator()
|
|
imgui.text("Kontekst lotu (podglad):")
|
|
try:
|
|
_, ctx_text = self._collect_context()
|
|
imgui.text_wrapped(ctx_text)
|
|
except Exception as e:
|
|
imgui.text(f"(blad: {e})")
|
|
|
|
# ---------------------------- Cykl zycia pluginu ----------------------------
|
|
def XPluginStart(self):
|
|
self._ensure_datarefs()
|
|
self._load_controls(force=True)
|
|
|
|
self._cmd = xp.createCommand("xcopilot/ptt", "XCoPilot push-to-talk")
|
|
xp.registerCommandHandler(self._cmd, self._ptt_handler, 1, None)
|
|
self._cmd_toggle = xp.createCommand("xcopilot/toggle_window", "XCoPilot toggle window")
|
|
xp.registerCommandHandler(self._cmd_toggle, self._toggle_handler, 1, None)
|
|
|
|
self._menu = xp.createMenu("XCoPilot", None, 0, self._menu_handler, None)
|
|
xp.appendMenuItemWithCommand(self._menu, "Pokaz/ukryj okno", self._cmd_toggle)
|
|
self._menu_cfg_idx = xp.appendMenuItem(self._menu, "Konfiguracja", "xcopilot_config")
|
|
|
|
self._imgui_window = ImguiWindow(
|
|
left=100, top=400, right=600, bottom=100,
|
|
visible=0, draw=self._draw, refCon=None, decoration=1, layer=1)
|
|
self._imgui_window.setTitle("XCoPilot")
|
|
|
|
self._config_window = ImguiWindow(
|
|
left=150, top=500, right=700, bottom=120,
|
|
visible=0, draw=self._draw_config, refCon=None, decoration=1, layer=1)
|
|
self._config_window.setTitle("XCoPilot - Konfiguracja")
|
|
|
|
for win in (self._imgui_window, self._config_window):
|
|
try:
|
|
xp.setWindowResizingLimits(win.windowID, 200, 100, 2000, 2000)
|
|
except Exception:
|
|
pass
|
|
|
|
xp.registerFlightLoopCallback(self._vr_flight_loop, 1.0, None)
|
|
self._last_vr = self._get_vr_enabled()
|
|
self._apply_vr_mode(self._last_vr)
|
|
|
|
return "XCoPilot", "pali112.xcopilot", "AI co-pilot (llama.cpp + datarefy)"
|
|
|
|
def XPluginStop(self):
|
|
self.stop()
|
|
try:
|
|
xp.unregisterFlightLoopCallback(self._vr_flight_loop, None)
|
|
except Exception:
|
|
pass
|
|
for win in (self._config_window, self._imgui_window):
|
|
if win is not None:
|
|
try:
|
|
win.delete()
|
|
except Exception:
|
|
pass
|
|
self._config_window = None
|
|
self._imgui_window = None
|
|
|
|
def XPluginEnable(self):
|
|
return 1
|
|
|
|
def XPluginDisable(self):
|
|
pass
|
|
|
|
def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
|
|
pass
|
|
|
|
# ---------------------------- Handlery ----------------------------
|
|
def _ptt_handler(self, cmdRef, phase, refCon):
|
|
if self._voice_enabled():
|
|
mode = self.config.get("voice_ptt_mode", "hold")
|
|
if mode == "toggle":
|
|
if phase == xp.CommandBegin:
|
|
if self._recording:
|
|
self._stop_recording_and_send()
|
|
else:
|
|
self._start_recording()
|
|
return 0
|
|
else:
|
|
if phase == xp.CommandBegin:
|
|
self._start_recording()
|
|
return 0
|
|
elif phase == xp.CommandEnd:
|
|
self._stop_recording_and_send()
|
|
return 0
|
|
return 1
|
|
if phase == xp.CommandBegin:
|
|
text = self.pending_text.strip()
|
|
if text:
|
|
self.send(text)
|
|
self.pending_text = ""
|
|
return 0
|
|
return 1
|
|
|
|
def _toggle_handler(self, cmdRef, phase, refCon):
|
|
if phase == xp.CommandBegin:
|
|
self._toggle_main_window()
|
|
return 0
|
|
return 1
|
|
|
|
def _menu_handler(self, menuRef, itemRef):
|
|
if itemRef == "xcopilot_config":
|
|
self._toggle_config_window()
|
|
|
|
def _toggle_main_window(self):
|
|
if self._imgui_window is not None and self._imgui_window.windowID is not None:
|
|
cur = xp.getWindowIsVisible(self._imgui_window.windowID)
|
|
xp.setWindowIsVisible(self._imgui_window.windowID, 0 if cur else 1)
|
|
|
|
def _toggle_config_window(self):
|
|
if self._config_window is not None and self._config_window.windowID is not None:
|
|
cur = xp.getWindowIsVisible(self._config_window.windowID)
|
|
xp.setWindowIsVisible(self._config_window.windowID, 0 if cur else 1)
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Punkt wejscia XPPython3
|
|
# --------------------------------------------------------------------------
|
|
|
|
_plugin = XCoPilot()
|
|
|
|
|
|
class PythonInterface:
|
|
def XPluginStart(self):
|
|
return _plugin.XPluginStart()
|
|
|
|
def XPluginStop(self):
|
|
_plugin.XPluginStop()
|
|
|
|
def XPluginEnable(self):
|
|
return _plugin.XPluginEnable()
|
|
|
|
def XPluginDisable(self):
|
|
_plugin.XPluginDisable()
|
|
|
|
def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
|
|
_plugin.XPluginReceiveMessage(inFromWho, inMessage, inParam)
|