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) symbolic_mode: bool = False glyph_contexts: list = field(default_factory=list) def op_LOAD_MODEL(ctx: XICContext, *args): """LOAD_MODEL : Load a .gx model file.""" if not args: raise ValueError("LOAD_MODEL requires a path argument") model_path = args[0] ctx.model_path = str(model_path) print(f"[XIC] Model loaded: {ctx.model_path}") def op_SET_MODE(ctx: XICContext, *args): """SET_MODE : Set execution mode (chat, eval, benchmark, symbolic).""" if not args: raise ValueError("SET_MODE requires a mode argument") ctx.mode = str(args[0]) if ctx.mode == "symbolic": ctx.symbolic_mode = True print(f"[XIC] Mode set to: {ctx.mode}") def op_SET_PARAM(ctx: XICContext, *args): """SET_PARAM : Set a parameter.""" if len(args) < 2: raise ValueError("SET_PARAM requires key and value arguments") key = str(args[0]) value = args[1] ctx.params[key] = value print(f"[XIC] Parameter {key} = {value}") def op_RUN_PROMPT(ctx: XICContext, *args): """RUN_PROMPT : Execute prompt against loaded model or symbolic cognition. Symbolic behavior (ctx.symbolic_mode=True): - Routes through symbolic pipeline (run_symbolic_pipeline). - Uses ctx.params["context"] for execution context. - If glyph_contexts is populated: passes glyph_ids for multi-glyph resonance - Stores full pipeline result in ctx._state["last_symbolic_pipeline"]. Compressed behavior (ctx.symbolic_mode=False): - Requires model_path to be set via LOAD_MODEL. - Routes to execute_gx() for compressed execution. - Stores result in ctx._state["last_result"]. """ if not args: raise ValueError("RUN_PROMPT requires a prompt argument") prompt = str(args[0]) if ctx.symbolic_mode: from glyphos.symbolic_pipeline import run_symbolic_pipeline # Check for multi-glyph resonance context glyph_ids = None if ctx.glyph_contexts: glyph_ids = list(ctx.glyph_contexts) print(f"[XIC-MULTI-GLYPH] RUN_PROMPT with {len(glyph_ids)} glyphs") pipeline_result = run_symbolic_pipeline( prompt=prompt, context=ctx.params.get("context"), glyph_ids=glyph_ids, ) print(f"[XIC-SYMBOLIC] {pipeline_result.output_text}") ctx._state["last_symbolic_result"] = pipeline_result.output_text ctx._state["last_symbolic_pipeline"] = pipeline_result return 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_STREAM(ctx: XICContext, *args): """STREAM : Execute and stream output line by line. Symbolic behavior (ctx.symbolic_mode=True): - Routes through symbolic pipeline. - If glyph_contexts is populated: passes glyph_ids for multi-glyph resonance - Streams output_text line by line with [XIC-STREAM] prefix. - Stores pipeline result in ctx._state["last_symbolic_pipeline"]. Compressed behavior (ctx.symbolic_mode=False): - Routes to execute_gx(). - Streams result line by line with [XIC-STREAM] prefix. - Stores result in ctx._state["last_result"]. """ if not args: raise ValueError("STREAM requires a prompt argument") prompt = str(args[0]) if ctx.symbolic_mode: from glyphos.symbolic_pipeline import run_symbolic_pipeline # Check for multi-glyph resonance context glyph_ids = None if ctx.glyph_contexts: glyph_ids = list(ctx.glyph_contexts) print(f"[XIC-MULTI-GLYPH] STREAM with {len(glyph_ids)} glyphs") pipeline_result = run_symbolic_pipeline( prompt=prompt, context=ctx.params.get("context"), glyph_ids=glyph_ids, ) for chunk in str(pipeline_result.output_text).split("\n"): if chunk.strip(): print(f"[XIC-STREAM] {chunk}") ctx._state["last_symbolic_result"] = pipeline_result.output_text ctx._state["last_symbolic_pipeline"] = pipeline_result return if not ctx.model_path: raise ValueError("No model loaded. Use LOAD_MODEL first.") try: exec_ctx = execute_gx( ctx.model_path, trace=ctx.params.get("trace", False), profile=ctx.params.get("profile", False), ) result_text = str(getattr(exec_ctx, "result", "OK")) for chunk in result_text.split("\n"): if chunk.strip(): print(f"[XIC-STREAM] {chunk}") ctx._state["last_result"] = exec_ctx except ExecutionError as e: print(f"[XIC] Execution error: {e}") raise except Exception as e: print(f"[XIC] Unexpected error: {e}") raise def op_CHAIN(ctx: XICContext, *args): """CHAIN