MCPcopy
hub / github.com/StructuredLabs/preswald / wrapper

Function wrapper

preswald/engine/render_tracking.py:56–135  ·  view source on GitHub ↗
(*args, **kwargs)

Source from the content-addressed store, hash-verified

54
55 @wraps(func)
56 def wrapper(*args, **kwargs):
57 # Extract a callsite hint for ID generation if not explicitly provided
58 if "callsite_hint" not in kwargs:
59 stack = inspect.stack()
60 for frame in stack:
61 if "preswald" not in frame.filename:
62 kwargs["callsite_hint"] = f"{frame.filename}:{frame.lineno}"
63 break
64
65 # Resolve component ID and corresponding atom name
66 if "component_id" in kwargs:
67 component_id = kwargs["component_id"]
68 atom_name = generate_stable_atom_name_from_component_id(component_id)
69 logger.debug(f"[with_render_tracking] Using provided component_id {component_id}:{atom_name}")
70 else:
71 identifier = kwargs.get("identifier")
72 component_id = generate_stable_id(component_type, callsite_hint=kwargs["callsite_hint"], identifier=identifier)
73 atom_name = generate_stable_atom_name_from_component_id(component_id)
74 kwargs["component_id"] = component_id
75 logger.debug(f"[with_render_tracking] Generated component_id {component_id}:{atom_name}")
76
77 service = PreswaldService.get_instance()
78 if not service.is_reactivity_enabled:
79 result = func(*args, **kwargs)
80
81 # Extract the component dict from result
82 component = getattr(result, "_preswald_component", None)
83 if not component and isinstance(result, dict) and "id" in result:
84 component = result
85
86 if not component:
87 logger.warning(f"[{component_type}] No component metadata found {func.__name__=}")
88 return result
89
90 return_value = result.value if isinstance(result, ComponentReturn) else result
91
92 component['shouldRender'] = service.should_render(component_id, component)
93 # Skip DAG logic, but still respect RenderBuffer diffing
94 if service.should_render(component_id, component):
95 service.append_component(component)
96 else:
97 logger.info(f"[{component_type}] Fallback: No changes detected. Skipping append {component_id=}")
98
99 return return_value
100
101 # Run the component function within the atom context (if not already active)
102 current_atom = service._workflow._current_atom
103 if current_atom:
104 context = contextlib.nullcontext()
105 else:
106 context = service.active_atom(atom_name)
107
108 with context:
109 # Call the user-defined component function
110 result = func(*args, **kwargs)
111
112 # Extract the component dict from result
113 component = getattr(result, "_preswald_component", None)

Callers

nothing calls this directly

Calls 7

generate_stable_idFunction · 0.90
append_componentMethod · 0.80
active_atomMethod · 0.80
get_instanceMethod · 0.45
should_renderMethod · 0.45

Tested by

no test coverage detected