from dataclasses import dataclass, field from typing import Dict, Any, Optional from runtime_executor.runner import execute_gx, ExecutionError @dataclass class XICContext: model_path: Optional[str] = None mode: str = "chat" params: Dict[str, Any] = field(default_factory=dict) _state: Dict[str, Any] = field(default_factory=dict) @property def glyph_contexts(self) -> list: return self._state.get("glyph_stack", []) def op_LOAD_MODEL(ctx: XICContext, *args): if not args: raise ValueError("LOAD_MODEL requires a path argument") ctx.model_path = str(args[0]) print(f"[XIC] Model loaded: {ctx.model_path}") def op_SET_MODE(ctx: XICContext, *args): if not args: raise ValueError("SET_MODE requires a mode argument") ctx.mode = str(args[0]) print(f"[XIC] Mode set to: {ctx.mode}") def op_SET_PARAM(ctx: XICContext, *args): if len(args) < 2: raise ValueError("SET_PARAM requires key and value arguments") ctx.params[str(args[0])] = args[1] print(f"[XIC] Parameter {args[0]} = {args[1]}") def op_RUN_PROMPT(ctx: XICContext, *args): if not args: raise ValueError("RUN_PROMPT requires a prompt argument") if not ctx.model_path: raise ValueError("No model loaded. Use LOAD_MODEL first.") try: execution_context = execute_gx( ctx.model_path, trace=ctx.params.get("trace", False), profile=ctx.params.get("profile", False), ) print(f"[XIC] Execution complete") print(f"[XIC] Result: {getattr(execution_context, 'result', 'OK')}") ctx._state["last_result"] = execution_context except ExecutionError as e: print(f"[XIC] Execution error: {e}") raise except Exception as e: print(f"[XIC] Unexpected error: {e}") raise def op_SET_CONTEXT(ctx: XICContext, *args): if len(args) < 2: raise ValueError("SET_CONTEXT requires key and value arguments") ctx._state[str(args[0])] = args[1] print(f"[XIC] Context {args[0]} = {args[1]}") def op_PUSH_GLYPH_CONTEXT(ctx: XICContext, *args): if not args: raise ValueError("PUSH_GLYPH_CONTEXT requires a glyph argument") glyph_uri = str(args[0]) stack = ctx._state.setdefault("glyph_stack", []) max_glyphs = ctx.params.get("max_resonance_glyphs", 10) guardrails = ctx.params.get("enable_resonance_guardrails", False) if guardrails and len(stack) >= max_glyphs: print(f"[XIC] Guardrail blocked glyph push: {glyph_uri} (max {max_glyphs})") return if glyph_uri not in stack: stack.append(glyph_uri) print(f"[XIC] Pushed glyph context: {glyph_uri}") else: print(f"[XIC] Duplicate glyph ignored: {glyph_uri}") def op_CHAIN(ctx: XICContext, *args): if not args: raise ValueError("CHAIN requires a target symbol argument") target = str(args[0]) if target not in ctx._state.get("_program", {}).get("symbols", {}): raise ValueError(f"Unknown symbol: {target}") ctx._state["chain_target"] = target print(f"[XIC] Chaining to symbol: {target}") def op_LOG(ctx: XICContext, *args): if not args: raise ValueError("LOG requires a message argument") message = str(args[0]) print(f"[XIC] LOG: {message}") def op_CLEAR_GLYPH_CONTEXT(ctx: XICContext, *args): ctx._state["glyph_stack"] = [] print(f"[XIC] Cleared glyph context") def op_CALL_GLYPH(ctx: XICContext, *args): if len(args) < 2: raise ValueError("CALL_GLYPH requires glyph_id and prompt arguments") glyph_id = str(args[0]) prompt = str(args[1]) glyph_ids = ctx._state.get("glyph_stack", []) try: from glyphos.symbolic_pipeline import run_symbolic_pipeline result = run_symbolic_pipeline( prompt=prompt, glyph_id=glyph_id, glyph_ids=glyph_ids if glyph_ids else None, context=dict(ctx.params.get("context", {})), ) ctx._state["last_pipeline_result"] = result print(f"[XIC] Glyph {glyph_id} called with prompt: {prompt[:40]}...") except Exception as e: print(f"[XIC] CALL_GLYPH error: {e}") def op_GET_GLYPH_RESONANCE(ctx: XICContext, *args): result = ctx._state.get("last_pipeline_result") if result and hasattr(result, 'fused_symbol') and result.fused_symbol: res = result.fused_symbol.resonance_map print(f"[XIC] Global Resonance: {res.global_resonance_score:.3f}") print(f"[XIC] Glyphs Engaged: {len(res.resonances)}") else: print("[XIC] No resonance data available") def op_NOP(ctx: XICContext, *args): pass OP_TABLE = { "LOAD_MODEL": op_LOAD_MODEL, "SET_MODE": op_SET_MODE, "SET_PARAM": op_SET_PARAM, "RUN_PROMPT": op_RUN_PROMPT, "SET_CONTEXT": op_SET_CONTEXT, "PUSH_GLYPH_CONTEXT": op_PUSH_GLYPH_CONTEXT, "CLEAR_GLYPH_CONTEXT": op_CLEAR_GLYPH_CONTEXT, "CALL_GLYPH": op_CALL_GLYPH, "GET_GLYPH_RESONANCE": op_GET_GLYPH_RESONANCE, "NOP": op_NOP, "CHAIN": op_CHAIN, "LOG": op_LOG, }