solidworks_mcp.ui.services.llm_service¶
solidworks_mcp.ui.services.llm_service ¶
LLM orchestration service for the Prefab CAD assistant dashboard.
Wraps all pydantic-ai calls for clarification, family inspection, and Go orchestration. This module is the only place that imports pydantic-ai or subprocess for token retrieval.
Security note
_ensure_provider_credentials uses subprocess.run(["gh", "auth", "token"]).
TODO: security review — subprocess token extraction. Consider replacing with a¶
credential-store or environment-variable-only approach.¶
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.'
Classes¶
CheckpointCandidate ¶
Bases: BaseModel
One suggested execution checkpoint.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
Short human-readable label. |
allowed_tools |
list[str]
|
MCP tool names allowed at this checkpoint. |
rationale |
str
|
Reasoning for including this step. |
execution |
dict[str, Any]
|
Optional structured execution hints used by checkpoint runner. |
ClarificationResponse ¶
Bases: BaseModel
LLM response for goal clarification.
Attributes:
| Name | Type | Description |
|---|---|---|
normalized_brief |
str
|
Concise manufacturing-ready description of the design goal. |
questions |
list[str]
|
Follow-up questions that unblock the next modelling step. |
FamilyInspection ¶
Bases: BaseModel
LLM response for feature-family classification.
Attributes:
| Name | Type | Description |
|---|---|---|
family |
str
|
Detected SolidWorks feature family name. |
confidence |
Literal['low', 'medium', 'high']
|
Model confidence level. |
evidence |
list[str]
|
Supporting evidence lines. |
warnings |
list[str]
|
Contradictory or low-confidence warnings. |
checkpoints |
list[CheckpointCandidate]
|
Suggested checkpoint plan for human review. |
RecoverableFailure ¶
Bases: BaseModel
Fallback stub used when pydantic-ai is not installed.
Functions¶
_build_agent_model ¶
Construct and return the pydantic-ai model object for the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Provider-qualified model name. |
required |
local_endpoint
|
str | None
|
Base URL used when |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Either an |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When the local-model path is requested but pydantic-ai's OpenAI provider is not installed. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
_coerce_explicit_feature_order_plan ¶
Coerce checkpoints to an explicit feature sequence when the goal provides one.
Source code in src/solidworks_mcp/ui/services/llm_service.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
_ensure_provider_credentials ¶
Verify that the required provider credential is present, raising otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Provider-qualified model name (e.g. |
required |
local_endpoint
|
str | None
|
Optional base URL for a local LLM server. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When the required credential is missing. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
_extract_blind_depth_mm ¶
Extract blind-depth numeric value from phrases like blind 10.0 mm.
Source code in src/solidworks_mcp/ui/services/llm_service.py
_extract_explicit_feature_order ¶
Extract feature order tokens from goals that include an explicit A -> B -> C sequence.
Source code in src/solidworks_mcp/ui/services/llm_service.py
_family_for_feature_order ¶
Infer family from explicit feature sequence, preferring deterministic extrusion flows.
Source code in src/solidworks_mcp/ui/services/llm_service.py
_parse_json_blob ¶
Parse a JSON string into a dict, returning an empty dict on failure.
Source code in src/solidworks_mcp/ui/services/llm_service.py
_resolve_model_name ¶
Return the resolved model name, falling back to the environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
explicit_model
|
str | None
|
Caller-supplied model string (may be |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Model name string to pass to pydantic-ai. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
_run_structured_agent
async
¶
_run_structured_agent(*, system_prompt: str, user_prompt: str, result_type: type[BaseModel], model_name: str | None = None, local_endpoint: str | None = None) -> BaseModel | RecoverableFailure
Run a pydantic-ai agent and return a structured result or a RecoverableFailure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
System-level instructions for the agent. |
required |
user_prompt
|
str
|
User turn containing the planning request. |
required |
result_type
|
type[BaseModel]
|
Pydantic model class expected as the structured output. |
required |
model_name
|
str | None
|
Optional provider-qualified model override. |
None
|
local_endpoint
|
str | None
|
Optional base URL for local LLM servers. |
None
|
Returns:
| Type | Description |
|---|---|
BaseModel | RecoverableFailure
|
Either an instance of |
BaseModel | RecoverableFailure
|
why the agent call failed and how to recover. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | |
_tools_for_feature_name ¶
Map a feature token to an executable tool subset used by checkpoint execution.
Source code in src/solidworks_mcp/ui/services/llm_service.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_evidence_link ¶
insert_evidence_link(*, session_id: str, source_type: str, source_id: str, checkpoint_id: int | None = None, relevance_score: float | None = None, rationale: str | None = None, payload_json: str | None = None, db_path: Path | None = None) -> None
Insert one evidence row used by planning/classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session id value. |
required |
source_type
|
str
|
The source type value. |
required |
source_id
|
str
|
The source id value. |
required |
checkpoint_id
|
int | None
|
The checkpoint id value. Defaults to None. |
None
|
relevance_score
|
float | None
|
The relevance score value. Defaults to None. |
None
|
rationale
|
str | None
|
The rationale value. Defaults to None. |
None
|
payload_json
|
str | None
|
The payload 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
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
inspect_family
async
¶
inspect_family(session_id: str, user_goal: str, *, db_path: Path | None = None, model_name: str | None = None) -> dict[str, Any]
Run LLM-backed family classification and suggested checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Dashboard session identifier. |
required |
user_goal
|
str
|
Free-text description of the design intent. |
required |
db_path
|
Path | None
|
Optional SQLite path override. |
None
|
model_name
|
str | None
|
Optional provider-qualified model override. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Full dashboard state payload. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | |
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_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
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
replace_plan_checkpoints ¶
replace_plan_checkpoints(*, session_id: str, checkpoints: list[dict[str, Any]], db_path: Path | None = None) -> None
Replace all session checkpoints with a new ordered checkpoint list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session id value. |
required |
checkpoints
|
list[dict[str, Any]]
|
Replacement checkpoint records. |
required |
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
request_clarifications
async
¶
request_clarifications(session_id: str, user_goal: str, *, user_answer: str = '', db_path: Path | None = None, model_name: str | None = None) -> dict[str, Any]
Generate focused follow-up questions for the current design goal using LLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Dashboard session identifier. |
required |
user_goal
|
str
|
Free-text description of the design intent. |
required |
user_answer
|
str
|
Any previous answers the user has already provided. |
''
|
db_path
|
Path | None
|
Optional SQLite path override. |
None
|
model_name
|
str | None
|
Optional provider-qualified model override. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Full dashboard state payload. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
run_go_orchestration
async
¶
run_go_orchestration(session_id: str, *, user_goal: str, assumptions_text: str | None = None, user_answer: str = '', db_path: Path | None = None, api_origin: str = DEFAULT_API_ORIGIN) -> dict[str, Any]
Run a single end-to-end pass: approve brief, update preferences, clarify, inspect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Dashboard session identifier. |
required |
user_goal
|
str
|
Free-text description of the design intent. |
required |
assumptions_text
|
str | None
|
Optional manufacturing assumptions to persist. |
None
|
user_answer
|
str
|
Any previous answers the user has already provided. |
''
|
db_path
|
Path | None
|
Optional SQLite path override. |
None
|
api_origin
|
str
|
Base URL of the running FastAPI server. |
DEFAULT_API_ORIGIN
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Full dashboard state payload. |
Source code in src/solidworks_mcp/ui/services/llm_service.py
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 | |
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
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. |