Skip to content

solidworks_mcp.ui.prefab_trace_probe

solidworks_mcp.ui.prefab_trace_probe

Minimal Prefab probe app for tracing the UI startup and model-connect flow.

Attributes

API_ORIGIN module-attribute

API_ORIGIN = getenv('SOLIDWORKS_UI_API_ORIGIN', 'http://127.0.0.1:8766')

DEFAULT_ASSUMPTIONS module-attribute

DEFAULT_ASSUMPTIONS = 'Assume PETG, 0.4mm nozzle, 0.2mm layers, and 0.30mm mating clearance unless overridden.'

SESSION_ID_EXPR module-attribute

SESSION_ID_EXPR = "{{ session_id || 'prefab-dashboard' }}"

Functions

_hydrate_preview_from_result

_hydrate_preview_from_result() -> list[object]

Update preview-relevant trace_payload fields directly from preview/refresh POST result.

Avoids nested Fetch-in-on_success which is unreliable in prefab_ui 0.19.x. The POST to /api/ui/preview/refresh returns build_dashboard_state() whose top-level keys map 1:1 to trace_payload.state fields.

Returns:

Type Description
list[object]

list[object]: A list containing the resulting items.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _hydrate_preview_from_result() -> list[object]:
    """Update preview-relevant trace_payload fields directly from preview/refresh POST result.

    Avoids nested Fetch-in-on_success which is unreliable in prefab_ui 0.19.x. The POST to
    /api/ui/preview/refresh returns build_dashboard_state() whose top-level keys map 1:1 to
    trace_payload.state fields.

    Returns:
        list[object]: A list containing the resulting items.
    """
    return [
        SetState("trace_payload.state", RESULT),
        SetState("trace_payload.latest_message", RESULT.latest_message),
        SetState("trace_payload.latest_error_text", RESULT.latest_error_text),
        SetState("last_error", ""),
    ]

_hydrate_trace

_hydrate_trace() -> list[object]

Build internal hydrate trace.

Returns:

Type Description
list[object]

list[object]: A list containing the resulting items.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _hydrate_trace() -> list[object]:
    """Build internal hydrate trace.

    Returns:
        list[object]: A list containing the resulting items.
    """

    return [
        SetState("trace_payload", RESULT),
        SetState("last_error", ""),
    ]

_refresh_preview

_refresh_preview() -> Fetch

Single preview refresh that chains _refresh_trace() on completion.

Chaining ensures the trace payload (which contains preview_view_urls) is re-fetched after all 4 orientation PNGs have been written, not in parallel with the preview export. We intentionally skip the intermediate _hydrate_preview_from_result() call here: the deep-path SetState it performs replaces trace_payload.state with the preview/refresh result which may have stale or empty preview_view_urls if called before the orientation PNGs are stored in metadata. _refresh_trace() (GET /api/ui/debug/session) is the single source of truth — it runs after the export completes and returns the full trace payload including the correct preview_view_urls.

Returns:

Name Type Description
Fetch Fetch

The result produced by the operation.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _refresh_preview() -> Fetch:
    """Single preview refresh that chains _refresh_trace() on completion.

    Chaining ensures the trace payload (which contains preview_view_urls) is re-fetched
    *after* all 4 orientation PNGs have been written, not in parallel with the preview
    export.  We intentionally skip the intermediate _hydrate_preview_from_result() call
    here: the deep-path SetState it performs replaces trace_payload.state with the
    preview/refresh result which may have stale or empty preview_view_urls if called before
    the orientation PNGs are stored in metadata.  _refresh_trace() (GET
    /api/ui/debug/session) is the single source of truth — it runs after the export
    completes and returns the full trace payload including the correct preview_view_urls.

    Returns:
        Fetch: The result produced by the operation.
    """
    return Fetch.post(
        f"{API_ORIGIN}/api/ui/preview/refresh",
        body={"session_id": SESSION_ID_EXPR, "orientation": "isometric"},
        on_success=_refresh_trace(),
        on_error=_trace_error("preview refresh"),
    )

_refresh_trace

_refresh_trace() -> Fetch

Build internal refresh trace.

Returns:

Name Type Description
Fetch Fetch

The result produced by the operation.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _refresh_trace() -> Fetch:
    """Build internal refresh trace.

    Returns:
        Fetch: The result produced by the operation.
    """

    return Fetch.get(
        f"{API_ORIGIN}/api/ui/debug/session",
        params={"session_id": SESSION_ID_EXPR},
        on_success=_hydrate_trace(),
        on_error=_trace_error("debug trace fetch"),
    )

_reset_probe_session

_reset_probe_session() -> Fetch

Build internal reset probe session.

Returns:

Name Type Description
Fetch Fetch

The result produced by the operation.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _reset_probe_session() -> Fetch:
    """Build internal reset probe session.

    Returns:
        Fetch: The result produced by the operation.
    """

    return Fetch.post(
        f"{API_ORIGIN}/api/ui/workflow/select",
        body={
            "session_id": SESSION_ID_EXPR,
            "workflow_mode": "unselected",
        },
        on_success=[
            SetState("last_action", "reset-session"),
            SetState("feature_target_text", ""),
            SetState("last_error", ""),
            SetState("checklist_result", "Session reset completed."),
            _refresh_trace(),
            ShowToast("Session reset completed.", variant="success"),
        ],
        on_error=[
            SetState("checklist_result", "Session reset failed."),
            *_trace_error("reset session"),
        ],
    )

_result_state

_result_state(_key: str, fallback: str = '') -> str

Build internal result state.

Parameters:

Name Type Description Default
_key str

The key value.

required
fallback str

The fallback value. Defaults to "".

''

Returns:

Name Type Description
str str

The resulting text value.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _result_state(_key: str, fallback: str = "") -> str:
    """Build internal result state.

    Args:
        _key (str): The key value.
        fallback (str): The fallback value. Defaults to "".

    Returns:
        str: The resulting text value.
    """

    return fallback

_run_checklist

_run_checklist() -> Fetch

Build internal run checklist.

Returns:

Name Type Description
Fetch Fetch

The result produced by the operation.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _run_checklist() -> Fetch:
    """Build internal run checklist.

    Returns:
        Fetch: The result produced by the operation.
    """

    return Fetch.get(
        f"{API_ORIGIN}/api/health",
        on_success=[
            SetState("health_response", RESULT),
            Fetch.get(
                f"{API_ORIGIN}/api/ui/debug/session",
                params={"session_id": SESSION_ID_EXPR},
                on_success=[
                    *_hydrate_trace(),
                    Fetch.post(
                        f"{API_ORIGIN}/api/ui/workflow/select",
                        body={
                            "session_id": SESSION_ID_EXPR,
                            "workflow_mode": "edit_existing",
                        },
                        on_success=[
                            SetState("last_action", "run-checklist"),
                            SetState("checklist_result", "Checklist run completed."),
                            _refresh_trace(),
                            ShowToast(
                                "Checklist run completed. Check the UI log file for the request chain.",
                                variant="success",
                            ),
                        ],
                        on_error=[
                            SetState(
                                "checklist_result",
                                "Checklist failed at step 3 (workflow select).",
                            ),
                            *_trace_error("checklist workflow select"),
                        ],
                    ),
                ],
                on_error=[
                    SetState(
                        "checklist_result",
                        "Checklist failed at step 2 (trace snapshot).",
                    ),
                    *_trace_error("checklist trace snapshot"),
                ],
            ),
        ],
        on_error=[
            SetState("checklist_result", "Checklist failed at step 1 (health)."),
            *_trace_error("checklist health"),
        ],
    )

_trace_error

_trace_error(step: str) -> list[object]

Build internal trace error.

Parameters:

Name Type Description Default
step str

The step value.

required

Returns:

Type Description
list[object]

list[object]: A list containing the resulting items.

Source code in src/solidworks_mcp/ui/prefab_trace_probe.py
def _trace_error(step: str) -> list[object]:
    """Build internal trace error.

    Args:
        step (str): The step value.

    Returns:
        list[object]: A list containing the resulting items.
    """

    return [
        SetState("last_error", f"{step}: {ERROR}"),
        ShowToast(f"{step}: {ERROR}", variant="error"),
    ]