2026-05-21 01:01:10 -04:00
|
|
|
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)
|
2026-05-21 01:19:40 -04:00
|
|
|
symbolic_mode: bool = False
|
2026-05-21 01:01:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_LOAD_MODEL(ctx: XICContext, *args):
|
|
|
|
|
"""LOAD_MODEL <path>: 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):
|
2026-05-21 01:19:40 -04:00
|
|
|
"""SET_MODE <mode>: Set execution mode (chat, eval, benchmark, symbolic)."""
|
2026-05-21 01:01:10 -04:00
|
|
|
if not args:
|
|
|
|
|
raise ValueError("SET_MODE requires a mode argument")
|
|
|
|
|
ctx.mode = str(args[0])
|
2026-05-21 01:19:40 -04:00
|
|
|
if ctx.mode == "symbolic":
|
|
|
|
|
ctx.symbolic_mode = True
|
2026-05-21 01:01:10 -04:00
|
|
|
print(f"[XIC] Mode set to: {ctx.mode}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_SET_PARAM(ctx: XICContext, *args):
|
|
|
|
|
"""SET_PARAM <key> <value>: 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):
|
2026-05-21 01:19:40 -04:00
|
|
|
"""RUN_PROMPT <prompt>: Execute prompt against loaded model or symbolic cognition.
|
2026-05-21 01:01:10 -04:00
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
Symbolic behavior (ctx.symbolic_mode=True):
|
|
|
|
|
- Routes through symbolic pipeline (run_symbolic_pipeline).
|
|
|
|
|
- Uses ctx.params["context"] for execution context.
|
|
|
|
|
- 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"].
|
2026-05-21 01:01:10 -04:00
|
|
|
"""
|
|
|
|
|
if not args:
|
|
|
|
|
raise ValueError("RUN_PROMPT requires a prompt argument")
|
|
|
|
|
|
2026-05-21 01:19:40 -04:00
|
|
|
prompt = str(args[0])
|
|
|
|
|
|
|
|
|
|
if ctx.symbolic_mode:
|
2026-05-21 01:27:49 -04:00
|
|
|
from glyphos.symbolic_pipeline import run_symbolic_pipeline
|
|
|
|
|
pipeline_result = run_symbolic_pipeline(
|
|
|
|
|
prompt=prompt,
|
|
|
|
|
context=ctx.params.get("context")
|
|
|
|
|
)
|
|
|
|
|
print(f"[XIC-SYMBOLIC] {pipeline_result.output_text}")
|
|
|
|
|
ctx._state["last_symbolic_result"] = pipeline_result.output_text
|
|
|
|
|
ctx._state["last_symbolic_pipeline"] = pipeline_result
|
2026-05-21 01:19:40 -04:00
|
|
|
return
|
|
|
|
|
|
2026-05-21 01:01:10 -04:00
|
|
|
if not ctx.model_path:
|
|
|
|
|
raise ValueError("No model loaded. Use LOAD_MODEL first.")
|
|
|
|
|
|
|
|
|
|
try:
|
2026-05-21 01:23:48 -04:00
|
|
|
execution_context = execute_gx(
|
|
|
|
|
ctx.model_path,
|
|
|
|
|
trace=ctx.params.get("trace", False),
|
|
|
|
|
profile=ctx.params.get("profile", False)
|
|
|
|
|
)
|
2026-05-21 01:01:10 -04:00
|
|
|
|
|
|
|
|
print(f"[XIC] Execution complete")
|
2026-05-21 01:19:40 -04:00
|
|
|
print(f"[XIC] Result: {getattr(execution_context, 'result', 'OK')}")
|
2026-05-21 01:01:10 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 01:19:40 -04:00
|
|
|
def op_STREAM(ctx: XICContext, *args):
|
2026-05-21 01:23:48 -04:00
|
|
|
"""STREAM <prompt>: Execute and stream output line by line.
|
|
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
Symbolic behavior (ctx.symbolic_mode=True):
|
|
|
|
|
- Routes through symbolic pipeline.
|
|
|
|
|
- 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"].
|
2026-05-21 01:23:48 -04:00
|
|
|
"""
|
2026-05-21 01:19:40 -04:00
|
|
|
if not args:
|
|
|
|
|
raise ValueError("STREAM requires a prompt argument")
|
|
|
|
|
prompt = str(args[0])
|
|
|
|
|
|
|
|
|
|
if ctx.symbolic_mode:
|
2026-05-21 01:27:49 -04:00
|
|
|
from glyphos.symbolic_pipeline import run_symbolic_pipeline
|
|
|
|
|
pipeline_result = run_symbolic_pipeline(
|
|
|
|
|
prompt=prompt,
|
|
|
|
|
context=ctx.params.get("context")
|
|
|
|
|
)
|
|
|
|
|
for chunk in str(pipeline_result.output_text).split("\n"):
|
2026-05-21 01:19:40 -04:00
|
|
|
if chunk.strip():
|
|
|
|
|
print(f"[XIC-STREAM] {chunk}")
|
2026-05-21 01:27:49 -04:00
|
|
|
ctx._state["last_symbolic_result"] = pipeline_result.output_text
|
|
|
|
|
ctx._state["last_symbolic_pipeline"] = pipeline_result
|
2026-05-21 01:19:40 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if not ctx.model_path:
|
|
|
|
|
raise ValueError("No model loaded. Use LOAD_MODEL first.")
|
|
|
|
|
|
2026-05-21 01:23:48 -04:00
|
|
|
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
|
2026-05-21 01:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_CHAIN(ctx: XICContext, *args):
|
|
|
|
|
"""CHAIN <label>: Mark start of a named chain; passes context forward."""
|
|
|
|
|
if not args:
|
|
|
|
|
raise ValueError("CHAIN requires a label argument")
|
|
|
|
|
label = str(args[0])
|
|
|
|
|
ctx.params["chain_label"] = label
|
|
|
|
|
print(f"[XIC-CHAIN] Entering chain: {label}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_CALL_GLYPH(ctx: XICContext, *args):
|
2026-05-21 01:27:49 -04:00
|
|
|
"""CALL_GLYPH <glyph_id> <payload>: Invoke glyph-aware cognition.
|
|
|
|
|
|
|
|
|
|
Routes through symbolic pipeline with explicit glyph_id parameter.
|
|
|
|
|
The glyph_id is propagated into the pipeline context and used for
|
|
|
|
|
glyph-aware symbolic transformations in the LAIN layer.
|
|
|
|
|
|
|
|
|
|
Stores result with key "glyph_{glyph_id}" containing:
|
|
|
|
|
- output_text: Final text from cognition
|
|
|
|
|
- fused_symbol: Fused symbolic representation (if produced)
|
|
|
|
|
- steps: List of symbolic pipeline steps
|
|
|
|
|
"""
|
2026-05-21 01:19:40 -04:00
|
|
|
if not args:
|
|
|
|
|
raise ValueError("CALL_GLYPH requires glyph_id argument")
|
|
|
|
|
glyph_id = str(args[0])
|
|
|
|
|
payload = str(args[1]) if len(args) > 1 else ""
|
|
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
from glyphos.symbolic_pipeline import run_symbolic_pipeline
|
2026-05-21 01:19:40 -04:00
|
|
|
glyph_context = dict(ctx.params.get("context", {}))
|
|
|
|
|
glyph_context["glyph_id"] = glyph_id
|
2026-05-21 01:27:49 -04:00
|
|
|
|
|
|
|
|
pipeline_result = run_symbolic_pipeline(
|
|
|
|
|
prompt=payload,
|
|
|
|
|
context=glyph_context,
|
|
|
|
|
glyph_id=glyph_id,
|
|
|
|
|
)
|
|
|
|
|
print(f"[XIC-GLYPH] {pipeline_result.output_text}")
|
|
|
|
|
ctx._state[f"glyph_{glyph_id}"] = {
|
|
|
|
|
"output_text": pipeline_result.output_text,
|
|
|
|
|
"fused_symbol": pipeline_result.fused_symbol,
|
|
|
|
|
"steps": [{"name": s.name, "kind": s.kind, "payload": str(s.payload)[:100]}
|
|
|
|
|
for s in pipeline_result.steps],
|
|
|
|
|
}
|
2026-05-21 01:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_SET_CONTEXT(ctx: XICContext, *args):
|
|
|
|
|
"""SET_CONTEXT <key> <value>: Set symbolic/cognitive context key."""
|
|
|
|
|
if len(args) < 2:
|
|
|
|
|
raise ValueError("SET_CONTEXT requires key and value")
|
|
|
|
|
if "context" not in ctx.params:
|
|
|
|
|
ctx.params["context"] = {}
|
2026-05-21 01:23:48 -04:00
|
|
|
key = str(args[0])
|
|
|
|
|
value = args[1]
|
|
|
|
|
ctx.params["context"][key] = value
|
|
|
|
|
print(f"[XIC] Context {key} = {value}")
|
2026-05-21 01:19:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def op_LOG(ctx: XICContext, *args):
|
|
|
|
|
"""LOG <message>: Structured log from XIC program."""
|
|
|
|
|
message = str(args[0]) if args else ""
|
|
|
|
|
print(f"[XIC-LOG] {message}")
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 01:01:10 -04:00
|
|
|
# Operation dispatch table
|
|
|
|
|
OP_TABLE = {
|
|
|
|
|
"LOAD_MODEL": op_LOAD_MODEL,
|
|
|
|
|
"SET_MODE": op_SET_MODE,
|
|
|
|
|
"SET_PARAM": op_SET_PARAM,
|
2026-05-21 01:19:40 -04:00
|
|
|
"SET_CONTEXT": op_SET_CONTEXT,
|
2026-05-21 01:01:10 -04:00
|
|
|
"RUN_PROMPT": op_RUN_PROMPT,
|
2026-05-21 01:19:40 -04:00
|
|
|
"STREAM": op_STREAM,
|
|
|
|
|
"CHAIN": op_CHAIN,
|
|
|
|
|
"CALL_GLYPH": op_CALL_GLYPH,
|
|
|
|
|
"LOG": op_LOG,
|
2026-05-21 01:01:10 -04:00
|
|
|
}
|