session_state
Return a structured JSON snapshot of the active build session, including geometry metrics, named objects, snapshots, and variable summaries, to confirm current state after resets or multi-step builds.
Instructions
Return a structured JSON snapshot of the current session: current_shape metrics, all named objects with geometry stats, snapshot names, and a variables summary of the Python namespace (type + volume for shapes, type + length for collections, type + value for scalars). Use this to orient after a reset, restore, or multi-step build to confirm what geometry and variables are active.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The main handler function for the 'session_state' tool. It collects current_shape metrics and named object diagnostics via _collect(), adds snapshot names, computes a variables summary of the Python namespace, and returns the result as a JSON string.
def session_state(session) -> str: state = _collect(session.current_shape, session.objects) state["snapshots"] = list(session.snapshots.keys()) state["variables"] = _namespace_summary(session.namespace) return json.dumps(state, indent=2) - _namespace_summary() iterates the user's namespace, filtering out builtins, imported build123d symbols, and private names, then returns a dict with type info (and volume for Shapes, length for collections, value for scalars).
def _namespace_summary(namespace: dict) -> dict: global _BUILD123D_NAMES if _BUILD123D_NAMES is None: _BUILD123D_NAMES = _build123d_public_names() _shape_cls: type | None = None try: from build123d import Shape _shape_cls = Shape except ImportError: pass result = {} for name, val in namespace.items(): if name.startswith("_") or name in _SKIP: continue if _is_imported_symbol(val): continue if name in _BUILD123D_NAMES: continue try: typ = type(val).__name__ if _shape_cls is not None and isinstance(val, _shape_cls): try: result[name] = {"type": typ, "volume": round(val.volume, 4)} # type: ignore[attr-defined] except Exception: result[name] = {"type": typ} elif isinstance(val, (list, tuple)): result[name] = {"type": typ, "length": len(val)} elif isinstance(val, dict): result[name] = {"type": "dict", "length": len(val)} elif isinstance(val, bool): result[name] = {"type": "bool", "value": val} elif isinstance(val, (int, float)): result[name] = {"type": typ, "value": val} elif isinstance(val, str): result[name] = {"type": "str", "value": val[:80]} elif callable(val): result[name] = {"type": "function"} else: result[name] = {"type": typ} except Exception: pass return result - _is_imported_symbol() utility to detect if a namespace value is an imported class/function/module from build123d/cadquery/OCP (not a user-created value).
def _is_imported_symbol(val) -> bool: """Return True if val is a class/function/module imported from build123d, not a user value.""" if isinstance(val, types.ModuleType): return True mod = getattr(val, '__module__', '') or '' if mod.startswith('build123d') or mod.startswith('cadquery') or mod.startswith('OCP'): if isinstance(val, type) or (callable(val) and not isinstance(val, type)): return True return False - _build123d_public_names() helper that introspects the build123d module to get the set of all public names, used to filter them out of the variable summary.
def _build123d_public_names() -> set[str]: try: import build123d return set(dir(build123d)) except ImportError: return set() - _collect() is imported from diff.py. It takes current_shape and objects dict and produces geometry diagnostics (volume, faces, edges, vertices, bbox) for each.
def _collect(current_shape, objects: dict) -> dict: result: dict = {"current_shape": None, "objects": {}} if current_shape is not None: try: result["current_shape"] = _shape_diag(current_shape) except Exception as e: result["current_shape"] = {"error": str(e)} for name, shape in objects.items(): try: result["objects"][name] = _shape_diag(shape) except Exception as e: result["objects"][name] = {"error": str(e)} return result