solidworks_mcp.ui.services._utils¶
solidworks_mcp.ui.services._utils ¶
Shared utility functions for the Prefab CAD assistant dashboard services.
This module provides pure utility functions with no service-level dependencies. All functions are stateless and accept their inputs as arguments.
Design principles applied: - Single Responsibility: each function does exactly one thing. - No side effects: these helpers never write to the database directly. - No circular imports: this module is the lowest layer; it imports only from the database and schema layers, never from sibling service modules.
Attributes¶
DEFAULT_API_ORIGIN
module-attribute
¶
DEFAULT_USER_GOAL
module-attribute
¶
DEFAULT_USER_GOAL = 'Design a printable mounting component with documented constraints and fastener strategy.'
SUPPORTED_MODEL_UPLOAD_SUFFIXES
module-attribute
¶
_DEFAULT_CONTEXT_DIR
module-attribute
¶
_DEFAULT_PREVIEW_DIR
module-attribute
¶
_DEFAULT_UPLOADED_MODEL_DIR
module-attribute
¶
Classes¶
HTMLTextExtractor ¶
Bases: HTMLParser
Minimal HTML-to-plain-text extractor.
Strips tags and collects visible text content. Script, style, and nav elements are suppressed entirely.
Source code in src/solidworks_mcp/ui/services/_utils.py
Functions¶
_looks_like_path_token ¶
Return True if token looks like a filesystem path rather than a feature name.
Source code in src/solidworks_mcp/ui/services/_utils.py
_trace_json_default ¶
context_file_path ¶
context_file_path(session_id: str, *, context_name: str | None = None, context_dir: Path | None = None) -> Path
Return the canonical path for a context snapshot JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Session identifier used as the default filename. |
required |
context_name
|
str | None
|
Optional override name (will be slugified). |
None
|
context_dir
|
Path | None
|
Override directory for context files. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
|
Source code in src/solidworks_mcp/ui/services/_utils.py
default_model_for_profile ¶
Return the default model name for the given provider and profile tier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
Provider string ( |
required |
profile
|
str
|
Profile tier ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
Provider-qualified model name string. |
Source code in src/solidworks_mcp/ui/services/_utils.py
ensure_context_dir ¶
Create and return the dashboard context snapshot directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_dir
|
Path | None
|
Override directory; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Resolved |
Source code in src/solidworks_mcp/ui/services/_utils.py
ensure_preview_dir ¶
Create and return the preview image directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preview_dir
|
Path | None
|
Override directory; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Resolved |
Source code in src/solidworks_mcp/ui/services/_utils.py
ensure_uploaded_model_dir ¶
Create and return the uploaded-model staging directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upload_dir
|
Path | None
|
Override directory; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Resolved |
Source code in src/solidworks_mcp/ui/services/_utils.py
feature_grounding_warning_text ¶
feature_grounding_warning_text(*, active_model_path: str, feature_target_text: str, feature_tree_count: int) -> str
Return a warning string when feature grounding cannot proceed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
active_model_path
|
str
|
Currently attached model path. |
required |
feature_target_text
|
str
|
Raw feature target refs from UI. |
required |
feature_tree_count
|
int
|
Number of rows in the current feature tree snapshot. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Warning message, or |
Source code in src/solidworks_mcp/ui/services/_utils.py
feature_target_status ¶
feature_target_status(features: list[dict[str, Any]], feature_target_text: str | None) -> tuple[str, list[str], list[str]]
Compute matched / missing feature target status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
list[dict[str, Any]]
|
Feature tree rows from the active snapshot. |
required |
feature_target_text
|
str | None
|
Raw feature target input from the UI. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, list[str], list[str]]
|
Three-tuple of (status_message, matched_names, missing_names). |
Source code in src/solidworks_mcp/ui/services/_utils.py
filter_docs_text ¶
Filter plain-text docs content to lines most relevant to query.
Returns up to max_chars characters of the most relevant lines, scored
by how many query tokens they contain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Full plain-text docs content. |
required |
query
|
str
|
Space-separated keyword query. |
required |
max_chars
|
int
|
Maximum characters to return. |
4000
|
Returns:
| Type | Description |
|---|---|
str
|
Filtered and truncated text snippet. |
Source code in src/solidworks_mcp/ui/services/_utils.py
get_design_session ¶
Return one session row as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session id value. |
required |
db_path
|
Path | None
|
The db path value. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
dict[str, Any] | None: A dictionary containing the resulting values. |
Source code in src/solidworks_mcp/agents/history_db.py
insert_tool_call_record ¶
insert_tool_call_record(*, session_id: str, tool_name: str, checkpoint_id: int | None = None, run_id: str | None = None, input_json: str | None = None, output_json: str | None = None, success: bool = True, latency_ms: float | None = None, db_path: Path | None = None) -> None
Insert one tool call execution record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session id value. |
required |
tool_name
|
str
|
The tool name value. |
required |
checkpoint_id
|
int | None
|
The checkpoint id value. Defaults to None. |
None
|
run_id
|
str | None
|
The run id value. Defaults to None. |
None
|
input_json
|
str | None
|
The input json value. Defaults to None. |
None
|
output_json
|
str | None
|
The output json value. Defaults to None. |
None
|
success
|
bool
|
The success value. Defaults to True. |
True
|
latency_ms
|
float | None
|
The latency ms value. Defaults to None. |
None
|
db_path
|
Path | None
|
The db path value. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None. |
Source code in src/solidworks_mcp/agents/history_db.py
is_url_reference ¶
Return True when source_path is an http/https URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
str
|
Raw source path string from the UI. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/solidworks_mcp/ui/services/_utils.py
materialize_uploaded_model ¶
materialize_uploaded_model(session_id: str, uploaded_files: list[dict[str, Any]] | None, *, upload_dir: Path | None = None) -> Path
Decode a base64-encoded uploaded model file and write it to the staging directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Dashboard session identifier used as a staging subdirectory. |
required |
uploaded_files
|
list[dict[str, Any]] | None
|
List of file payload dicts, each with |
required |
upload_dir
|
Path | None
|
Override for the upload staging directory. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path pointing at the decoded file on disk. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When no file is provided, the name is missing, the suffix is unsupported, or the data field is not valid base64. |
Source code in src/solidworks_mcp/ui/services/_utils.py
merge_metadata ¶
merge_metadata(session_id: str, *, db_path: Path | None = None, user_goal: str | None = None, **updates: Any) -> dict[str, Any]
Read session metadata, merge updates into it, and write it back.
Implements the optimistic read-modify-write pattern used across all service functions that need to update one or more metadata keys without overwriting unrelated keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Target session identifier. |
required |
db_path
|
Path | None
|
Optional override for the SQLite database path. |
None
|
user_goal
|
str | None
|
When provided, also updates the |
None
|
**updates
|
Any
|
Arbitrary key-value pairs to merge into metadata. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The merged metadata dict after the write. |
Source code in src/solidworks_mcp/ui/services/_utils.py
normalize_feature_targets ¶
Parse a comma- or newline-separated feature target string into a list.
Strips @ prefixes and filters out tokens that look like file paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_target_text
|
str | None
|
Raw input such as |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of normalised feature name strings. |
Source code in src/solidworks_mcp/ui/services/_utils.py
normalize_model_name_for_provider ¶
normalize_model_name_for_provider(model_name: str | None, *, provider: str | None, profile: str | None = None) -> str
Normalise a free-form model name into a provider-qualified routing string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str | None
|
Raw model name from UI state, may be None or unqualified. |
required |
provider
|
str | None
|
Explicit provider override ( |
required |
profile
|
str | None
|
Profile tier used as fallback when model_name is empty. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Provider-qualified model name (e.g. |
Source code in src/solidworks_mcp/ui/services/_utils.py
normalize_workflow_mode ¶
Normalise a raw workflow mode string to a known value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow_mode
|
str | None
|
Raw mode string from UI state. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/solidworks_mcp/ui/services/_utils.py
parse_json_blob ¶
Parse a JSON string into a dict, returning an empty dict on any failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
str | None
|
Raw JSON string, or |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed dict, or |
Source code in src/solidworks_mcp/ui/services/_utils.py
persist_ui_action ¶
persist_ui_action(session_id: str, *, tool_name: str, db_path: Path | None = None, metadata_updates: dict[str, Any] | None = None, user_goal: str | None = None, input_payload: dict[str, Any] | None = None, output_payload: dict[str, Any] | None = None, output_metadata: bool = False, success: bool = True, checkpoint_id: int | None = None) -> dict[str, Any]
Persist metadata updates and a matching tool-call audit record atomically.
Combines :func:merge_metadata and insert_tool_call_record so callers
can update session state and write an audit entry in a single call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Target session identifier. |
required |
tool_name
|
str
|
Logical name for the audit record (e.g. |
required |
db_path
|
Path | None
|
Optional override for the SQLite database path. |
None
|
metadata_updates
|
dict[str, Any] | None
|
Key-value pairs to merge into session metadata. |
None
|
user_goal
|
str | None
|
When provided, also updates the |
None
|
input_payload
|
dict[str, Any] | None
|
Dict serialised as the |
None
|
output_payload
|
dict[str, Any] | None
|
Dict serialised as the |
None
|
output_metadata
|
bool
|
When |
False
|
success
|
bool
|
Whether the action succeeded. |
True
|
checkpoint_id
|
int | None
|
Optional FK to the associated plan checkpoint. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The merged metadata dict after the write. |
Source code in src/solidworks_mcp/ui/services/_utils.py
provider_from_model_name ¶
Infer the provider prefix from a provider-qualified model name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Model name such as |
required |
Returns:
| Type | Description |
|---|---|
str
|
Provider string: |
str
|
|
Source code in src/solidworks_mcp/ui/services/_utils.py
provider_has_credentials ¶
Check whether the required credentials exist for the given model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Provider-qualified model name. |
required |
local_endpoint
|
str | None
|
Local API endpoint URL (required for |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/solidworks_mcp/ui/services/_utils.py
read_reference_source ¶
Read the text content of a local file (PDF, markdown, text).
PDF extraction requires the optional pypdf package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
Path
|
Local file path to read. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Extracted plain text content. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When a PDF is supplied but |
Source code in src/solidworks_mcp/ui/services/_utils.py
read_reference_url ¶
Fetch content from a URL and return (text, label).
Handles HTML (stripped), PDF (requires pypdf), and plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_url
|
str
|
http/https URL to fetch. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
Tuple of (plain_text_content, label) where label is derived from the URL path. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When a PDF is served but |
Source code in src/solidworks_mcp/ui/services/_utils.py
safe_context_name ¶
Normalise a context name to a safe filesystem slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_name
|
str | None
|
User-supplied context name, may be None. |
required |
session_id
|
str
|
Fallback identifier. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Alphanumeric + hyphen slug, never empty. |
Source code in src/solidworks_mcp/ui/services/_utils.py
sanitize_model_path_text ¶
Strip surrounding quotes from a model path string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Raw model path value from UI state. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Cleaned path string, or |
Source code in src/solidworks_mcp/ui/services/_utils.py
sanitize_preview_viewer_url ¶
Validate and return a preview viewer URL, or "" if it looks wrong.
Rejects URLs pointing at unexpected origins or session IDs to prevent open-redirect-style issues in the embedded viewer iframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Raw URL string from session metadata. |
required |
session_id
|
str
|
Expected session ID segment in the path. |
required |
api_origin
|
str
|
Allowed API origin (scheme + host + port). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Validated URL string, or |
Source code in src/solidworks_mcp/ui/services/_utils.py
sanitize_ui_text ¶
Return a clean string from value, using fallback for empty/invalid inputs.
Strips template placeholders such as {{ field }}, bare quotes, and
pydantic-ai expression strings that sometimes leak into UI state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Arbitrary input (str, None, etc.). |
required |
fallback
|
str
|
Value to return when value is empty or invalid. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
Cleaned string, or fallback. |
Source code in src/solidworks_mcp/ui/services/_utils.py
trace_json ¶
Serialise value to a pretty-printed JSON string for operator trace panels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Any JSON-serialisable value. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Indented JSON string. |
Source code in src/solidworks_mcp/ui/services/_utils.py
trace_session_row ¶
Return a copy of session_row with the bulky metadata_json field removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_row
|
dict[str, Any] | None
|
Raw session row dict from the database. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Filtered dict suitable for debug display. |
Source code in src/solidworks_mcp/ui/services/_utils.py
trace_tool_records ¶
Return the last ten tool-call records as lean trace dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
list[dict[str, Any]]
|
Full list of tool-call records for the session. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
Trimmed list of trace-friendly dicts (id, tool_name, success, …). |
Source code in src/solidworks_mcp/ui/services/_utils.py
upsert_design_session ¶
upsert_design_session(*, session_id: str, user_goal: str, source_mode: str = 'model', accepted_family: str | None = None, status: str = 'active', current_checkpoint_index: int = 0, metadata_json: str | None = None, db_path: Path | None = None) -> None
Create or update one interactive design session row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session id value. |
required |
user_goal
|
str
|
The user goal value. |
required |
source_mode
|
str
|
The source mode value. Defaults to "model". |
'model'
|
accepted_family
|
str | None
|
The accepted family value. Defaults to None. |
None
|
status
|
str
|
The status value. Defaults to "active". |
'active'
|
current_checkpoint_index
|
int
|
The current checkpoint index value. Defaults to 0. |
0
|
metadata_json
|
str | None
|
The metadata json value. Defaults to None. |
None
|
db_path
|
Path | None
|
The db path value. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None. |
Source code in src/solidworks_mcp/agents/history_db.py
workflow_copy ¶
Return display copy for the workflow selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow_mode
|
str
|
Normalised workflow mode string. |
required |
active_model_path
|
str | None
|
Currently attached model path, if any. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str, str, str]
|
Three-tuple of (label, guidance_text, flow_header_text). |