fast-agent-mcp 0.4.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fast_agent/__init__.py +183 -0
- fast_agent/acp/__init__.py +19 -0
- fast_agent/acp/acp_aware_mixin.py +304 -0
- fast_agent/acp/acp_context.py +437 -0
- fast_agent/acp/content_conversion.py +136 -0
- fast_agent/acp/filesystem_runtime.py +427 -0
- fast_agent/acp/permission_store.py +269 -0
- fast_agent/acp/server/__init__.py +5 -0
- fast_agent/acp/server/agent_acp_server.py +1472 -0
- fast_agent/acp/slash_commands.py +1050 -0
- fast_agent/acp/terminal_runtime.py +408 -0
- fast_agent/acp/tool_permission_adapter.py +125 -0
- fast_agent/acp/tool_permissions.py +474 -0
- fast_agent/acp/tool_progress.py +814 -0
- fast_agent/agents/__init__.py +85 -0
- fast_agent/agents/agent_types.py +64 -0
- fast_agent/agents/llm_agent.py +350 -0
- fast_agent/agents/llm_decorator.py +1139 -0
- fast_agent/agents/mcp_agent.py +1337 -0
- fast_agent/agents/tool_agent.py +271 -0
- fast_agent/agents/workflow/agents_as_tools_agent.py +849 -0
- fast_agent/agents/workflow/chain_agent.py +212 -0
- fast_agent/agents/workflow/evaluator_optimizer.py +380 -0
- fast_agent/agents/workflow/iterative_planner.py +652 -0
- fast_agent/agents/workflow/maker_agent.py +379 -0
- fast_agent/agents/workflow/orchestrator_models.py +218 -0
- fast_agent/agents/workflow/orchestrator_prompts.py +248 -0
- fast_agent/agents/workflow/parallel_agent.py +250 -0
- fast_agent/agents/workflow/router_agent.py +353 -0
- fast_agent/cli/__init__.py +0 -0
- fast_agent/cli/__main__.py +73 -0
- fast_agent/cli/commands/acp.py +159 -0
- fast_agent/cli/commands/auth.py +404 -0
- fast_agent/cli/commands/check_config.py +783 -0
- fast_agent/cli/commands/go.py +514 -0
- fast_agent/cli/commands/quickstart.py +557 -0
- fast_agent/cli/commands/serve.py +143 -0
- fast_agent/cli/commands/server_helpers.py +114 -0
- fast_agent/cli/commands/setup.py +174 -0
- fast_agent/cli/commands/url_parser.py +190 -0
- fast_agent/cli/constants.py +40 -0
- fast_agent/cli/main.py +115 -0
- fast_agent/cli/terminal.py +24 -0
- fast_agent/config.py +798 -0
- fast_agent/constants.py +41 -0
- fast_agent/context.py +279 -0
- fast_agent/context_dependent.py +50 -0
- fast_agent/core/__init__.py +92 -0
- fast_agent/core/agent_app.py +448 -0
- fast_agent/core/core_app.py +137 -0
- fast_agent/core/direct_decorators.py +784 -0
- fast_agent/core/direct_factory.py +620 -0
- fast_agent/core/error_handling.py +27 -0
- fast_agent/core/exceptions.py +90 -0
- fast_agent/core/executor/__init__.py +0 -0
- fast_agent/core/executor/executor.py +280 -0
- fast_agent/core/executor/task_registry.py +32 -0
- fast_agent/core/executor/workflow_signal.py +324 -0
- fast_agent/core/fastagent.py +1186 -0
- fast_agent/core/logging/__init__.py +5 -0
- fast_agent/core/logging/events.py +138 -0
- fast_agent/core/logging/json_serializer.py +164 -0
- fast_agent/core/logging/listeners.py +309 -0
- fast_agent/core/logging/logger.py +278 -0
- fast_agent/core/logging/transport.py +481 -0
- fast_agent/core/prompt.py +9 -0
- fast_agent/core/prompt_templates.py +183 -0
- fast_agent/core/validation.py +326 -0
- fast_agent/event_progress.py +62 -0
- fast_agent/history/history_exporter.py +49 -0
- fast_agent/human_input/__init__.py +47 -0
- fast_agent/human_input/elicitation_handler.py +123 -0
- fast_agent/human_input/elicitation_state.py +33 -0
- fast_agent/human_input/form_elements.py +59 -0
- fast_agent/human_input/form_fields.py +256 -0
- fast_agent/human_input/simple_form.py +113 -0
- fast_agent/human_input/types.py +40 -0
- fast_agent/interfaces.py +310 -0
- fast_agent/llm/__init__.py +9 -0
- fast_agent/llm/cancellation.py +22 -0
- fast_agent/llm/fastagent_llm.py +931 -0
- fast_agent/llm/internal/passthrough.py +161 -0
- fast_agent/llm/internal/playback.py +129 -0
- fast_agent/llm/internal/silent.py +41 -0
- fast_agent/llm/internal/slow.py +38 -0
- fast_agent/llm/memory.py +275 -0
- fast_agent/llm/model_database.py +490 -0
- fast_agent/llm/model_factory.py +388 -0
- fast_agent/llm/model_info.py +102 -0
- fast_agent/llm/prompt_utils.py +155 -0
- fast_agent/llm/provider/anthropic/anthropic_utils.py +84 -0
- fast_agent/llm/provider/anthropic/cache_planner.py +56 -0
- fast_agent/llm/provider/anthropic/llm_anthropic.py +796 -0
- fast_agent/llm/provider/anthropic/multipart_converter_anthropic.py +462 -0
- fast_agent/llm/provider/bedrock/bedrock_utils.py +218 -0
- fast_agent/llm/provider/bedrock/llm_bedrock.py +2207 -0
- fast_agent/llm/provider/bedrock/multipart_converter_bedrock.py +84 -0
- fast_agent/llm/provider/google/google_converter.py +466 -0
- fast_agent/llm/provider/google/llm_google_native.py +681 -0
- fast_agent/llm/provider/openai/llm_aliyun.py +31 -0
- fast_agent/llm/provider/openai/llm_azure.py +143 -0
- fast_agent/llm/provider/openai/llm_deepseek.py +76 -0
- fast_agent/llm/provider/openai/llm_generic.py +35 -0
- fast_agent/llm/provider/openai/llm_google_oai.py +32 -0
- fast_agent/llm/provider/openai/llm_groq.py +42 -0
- fast_agent/llm/provider/openai/llm_huggingface.py +85 -0
- fast_agent/llm/provider/openai/llm_openai.py +1195 -0
- fast_agent/llm/provider/openai/llm_openai_compatible.py +138 -0
- fast_agent/llm/provider/openai/llm_openrouter.py +45 -0
- fast_agent/llm/provider/openai/llm_tensorzero_openai.py +128 -0
- fast_agent/llm/provider/openai/llm_xai.py +38 -0
- fast_agent/llm/provider/openai/multipart_converter_openai.py +561 -0
- fast_agent/llm/provider/openai/openai_multipart.py +169 -0
- fast_agent/llm/provider/openai/openai_utils.py +67 -0
- fast_agent/llm/provider/openai/responses.py +133 -0
- fast_agent/llm/provider_key_manager.py +139 -0
- fast_agent/llm/provider_types.py +34 -0
- fast_agent/llm/request_params.py +61 -0
- fast_agent/llm/sampling_converter.py +98 -0
- fast_agent/llm/stream_types.py +9 -0
- fast_agent/llm/usage_tracking.py +445 -0
- fast_agent/mcp/__init__.py +56 -0
- fast_agent/mcp/common.py +26 -0
- fast_agent/mcp/elicitation_factory.py +84 -0
- fast_agent/mcp/elicitation_handlers.py +164 -0
- fast_agent/mcp/gen_client.py +83 -0
- fast_agent/mcp/helpers/__init__.py +36 -0
- fast_agent/mcp/helpers/content_helpers.py +352 -0
- fast_agent/mcp/helpers/server_config_helpers.py +25 -0
- fast_agent/mcp/hf_auth.py +147 -0
- fast_agent/mcp/interfaces.py +92 -0
- fast_agent/mcp/logger_textio.py +108 -0
- fast_agent/mcp/mcp_agent_client_session.py +411 -0
- fast_agent/mcp/mcp_aggregator.py +2175 -0
- fast_agent/mcp/mcp_connection_manager.py +723 -0
- fast_agent/mcp/mcp_content.py +262 -0
- fast_agent/mcp/mime_utils.py +108 -0
- fast_agent/mcp/oauth_client.py +509 -0
- fast_agent/mcp/prompt.py +159 -0
- fast_agent/mcp/prompt_message_extended.py +155 -0
- fast_agent/mcp/prompt_render.py +84 -0
- fast_agent/mcp/prompt_serialization.py +580 -0
- fast_agent/mcp/prompts/__init__.py +0 -0
- fast_agent/mcp/prompts/__main__.py +7 -0
- fast_agent/mcp/prompts/prompt_constants.py +18 -0
- fast_agent/mcp/prompts/prompt_helpers.py +238 -0
- fast_agent/mcp/prompts/prompt_load.py +186 -0
- fast_agent/mcp/prompts/prompt_server.py +552 -0
- fast_agent/mcp/prompts/prompt_template.py +438 -0
- fast_agent/mcp/resource_utils.py +215 -0
- fast_agent/mcp/sampling.py +200 -0
- fast_agent/mcp/server/__init__.py +4 -0
- fast_agent/mcp/server/agent_server.py +613 -0
- fast_agent/mcp/skybridge.py +44 -0
- fast_agent/mcp/sse_tracking.py +287 -0
- fast_agent/mcp/stdio_tracking_simple.py +59 -0
- fast_agent/mcp/streamable_http_tracking.py +309 -0
- fast_agent/mcp/tool_execution_handler.py +137 -0
- fast_agent/mcp/tool_permission_handler.py +88 -0
- fast_agent/mcp/transport_tracking.py +634 -0
- fast_agent/mcp/types.py +24 -0
- fast_agent/mcp/ui_agent.py +48 -0
- fast_agent/mcp/ui_mixin.py +209 -0
- fast_agent/mcp_server_registry.py +89 -0
- fast_agent/py.typed +0 -0
- fast_agent/resources/examples/data-analysis/analysis-campaign.py +189 -0
- fast_agent/resources/examples/data-analysis/analysis.py +68 -0
- fast_agent/resources/examples/data-analysis/fastagent.config.yaml +41 -0
- fast_agent/resources/examples/data-analysis/mount-point/WA_Fn-UseC_-HR-Employee-Attrition.csv +1471 -0
- fast_agent/resources/examples/mcp/elicitations/elicitation_account_server.py +88 -0
- fast_agent/resources/examples/mcp/elicitations/elicitation_forms_server.py +297 -0
- fast_agent/resources/examples/mcp/elicitations/elicitation_game_server.py +164 -0
- fast_agent/resources/examples/mcp/elicitations/fastagent.config.yaml +35 -0
- fast_agent/resources/examples/mcp/elicitations/fastagent.secrets.yaml.example +17 -0
- fast_agent/resources/examples/mcp/elicitations/forms_demo.py +107 -0
- fast_agent/resources/examples/mcp/elicitations/game_character.py +65 -0
- fast_agent/resources/examples/mcp/elicitations/game_character_handler.py +256 -0
- fast_agent/resources/examples/mcp/elicitations/tool_call.py +21 -0
- fast_agent/resources/examples/mcp/state-transfer/agent_one.py +18 -0
- fast_agent/resources/examples/mcp/state-transfer/agent_two.py +18 -0
- fast_agent/resources/examples/mcp/state-transfer/fastagent.config.yaml +27 -0
- fast_agent/resources/examples/mcp/state-transfer/fastagent.secrets.yaml.example +15 -0
- fast_agent/resources/examples/researcher/fastagent.config.yaml +61 -0
- fast_agent/resources/examples/researcher/researcher-eval.py +53 -0
- fast_agent/resources/examples/researcher/researcher-imp.py +189 -0
- fast_agent/resources/examples/researcher/researcher.py +36 -0
- fast_agent/resources/examples/tensorzero/.env.sample +2 -0
- fast_agent/resources/examples/tensorzero/Makefile +31 -0
- fast_agent/resources/examples/tensorzero/README.md +56 -0
- fast_agent/resources/examples/tensorzero/agent.py +35 -0
- fast_agent/resources/examples/tensorzero/demo_images/clam.jpg +0 -0
- fast_agent/resources/examples/tensorzero/demo_images/crab.png +0 -0
- fast_agent/resources/examples/tensorzero/demo_images/shrimp.png +0 -0
- fast_agent/resources/examples/tensorzero/docker-compose.yml +105 -0
- fast_agent/resources/examples/tensorzero/fastagent.config.yaml +19 -0
- fast_agent/resources/examples/tensorzero/image_demo.py +67 -0
- fast_agent/resources/examples/tensorzero/mcp_server/Dockerfile +25 -0
- fast_agent/resources/examples/tensorzero/mcp_server/entrypoint.sh +35 -0
- fast_agent/resources/examples/tensorzero/mcp_server/mcp_server.py +31 -0
- fast_agent/resources/examples/tensorzero/mcp_server/pyproject.toml +11 -0
- fast_agent/resources/examples/tensorzero/simple_agent.py +25 -0
- fast_agent/resources/examples/tensorzero/tensorzero_config/system_schema.json +29 -0
- fast_agent/resources/examples/tensorzero/tensorzero_config/system_template.minijinja +11 -0
- fast_agent/resources/examples/tensorzero/tensorzero_config/tensorzero.toml +35 -0
- fast_agent/resources/examples/workflows/agents_as_tools_extended.py +73 -0
- fast_agent/resources/examples/workflows/agents_as_tools_simple.py +50 -0
- fast_agent/resources/examples/workflows/chaining.py +37 -0
- fast_agent/resources/examples/workflows/evaluator.py +77 -0
- fast_agent/resources/examples/workflows/fastagent.config.yaml +26 -0
- fast_agent/resources/examples/workflows/graded_report.md +89 -0
- fast_agent/resources/examples/workflows/human_input.py +28 -0
- fast_agent/resources/examples/workflows/maker.py +156 -0
- fast_agent/resources/examples/workflows/orchestrator.py +70 -0
- fast_agent/resources/examples/workflows/parallel.py +56 -0
- fast_agent/resources/examples/workflows/router.py +69 -0
- fast_agent/resources/examples/workflows/short_story.md +13 -0
- fast_agent/resources/examples/workflows/short_story.txt +19 -0
- fast_agent/resources/setup/.gitignore +30 -0
- fast_agent/resources/setup/agent.py +28 -0
- fast_agent/resources/setup/fastagent.config.yaml +65 -0
- fast_agent/resources/setup/fastagent.secrets.yaml.example +38 -0
- fast_agent/resources/setup/pyproject.toml.tmpl +23 -0
- fast_agent/skills/__init__.py +9 -0
- fast_agent/skills/registry.py +235 -0
- fast_agent/tools/elicitation.py +369 -0
- fast_agent/tools/shell_runtime.py +402 -0
- fast_agent/types/__init__.py +59 -0
- fast_agent/types/conversation_summary.py +294 -0
- fast_agent/types/llm_stop_reason.py +78 -0
- fast_agent/types/message_search.py +249 -0
- fast_agent/ui/__init__.py +38 -0
- fast_agent/ui/console.py +59 -0
- fast_agent/ui/console_display.py +1080 -0
- fast_agent/ui/elicitation_form.py +946 -0
- fast_agent/ui/elicitation_style.py +59 -0
- fast_agent/ui/enhanced_prompt.py +1400 -0
- fast_agent/ui/history_display.py +734 -0
- fast_agent/ui/interactive_prompt.py +1199 -0
- fast_agent/ui/markdown_helpers.py +104 -0
- fast_agent/ui/markdown_truncator.py +1004 -0
- fast_agent/ui/mcp_display.py +857 -0
- fast_agent/ui/mcp_ui_utils.py +235 -0
- fast_agent/ui/mermaid_utils.py +169 -0
- fast_agent/ui/message_primitives.py +50 -0
- fast_agent/ui/notification_tracker.py +205 -0
- fast_agent/ui/plain_text_truncator.py +68 -0
- fast_agent/ui/progress_display.py +10 -0
- fast_agent/ui/rich_progress.py +195 -0
- fast_agent/ui/streaming.py +774 -0
- fast_agent/ui/streaming_buffer.py +449 -0
- fast_agent/ui/tool_display.py +422 -0
- fast_agent/ui/usage_display.py +204 -0
- fast_agent/utils/__init__.py +5 -0
- fast_agent/utils/reasoning_stream_parser.py +77 -0
- fast_agent/utils/time.py +22 -0
- fast_agent/workflow_telemetry.py +261 -0
- fast_agent_mcp-0.4.7.dist-info/METADATA +788 -0
- fast_agent_mcp-0.4.7.dist-info/RECORD +261 -0
- fast_agent_mcp-0.4.7.dist-info/WHEEL +4 -0
- fast_agent_mcp-0.4.7.dist-info/entry_points.txt +7 -0
- fast_agent_mcp-0.4.7.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This simplified implementation directly converts between MCP types and PromptMessageExtended.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from mcp import ClientSession
|
|
8
|
+
from mcp.types import CreateMessageRequestParams, CreateMessageResult, TextContent
|
|
9
|
+
|
|
10
|
+
from fast_agent.agents.agent_types import AgentConfig
|
|
11
|
+
from fast_agent.agents.llm_agent import LlmAgent
|
|
12
|
+
from fast_agent.core.logging.logger import get_logger
|
|
13
|
+
from fast_agent.interfaces import FastAgentLLMProtocol
|
|
14
|
+
from fast_agent.llm.sampling_converter import SamplingConverter
|
|
15
|
+
from fast_agent.mcp.helpers.server_config_helpers import get_server_config
|
|
16
|
+
from fast_agent.types.llm_stop_reason import LlmStopReason
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from fast_agent.types import PromptMessageExtended
|
|
20
|
+
|
|
21
|
+
logger = get_logger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def create_sampling_llm(
|
|
25
|
+
params: CreateMessageRequestParams, model_string: str, api_key: str | None
|
|
26
|
+
) -> FastAgentLLMProtocol:
|
|
27
|
+
"""
|
|
28
|
+
Create an LLM instance for sampling without tools support.
|
|
29
|
+
This utility function creates a minimal LLM instance based on the model string.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
mcp_ctx: The MCP ClientSession
|
|
33
|
+
model_string: The model to use (e.g. "passthrough", "claude-3-5-sonnet-latest")
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
An initialized LLM instance ready to use
|
|
37
|
+
"""
|
|
38
|
+
from fast_agent.llm.model_factory import ModelFactory
|
|
39
|
+
|
|
40
|
+
app_context = None
|
|
41
|
+
try:
|
|
42
|
+
from fast_agent.context import get_current_context
|
|
43
|
+
|
|
44
|
+
app_context = get_current_context()
|
|
45
|
+
except Exception:
|
|
46
|
+
logger.warning("App context not available for sampling call")
|
|
47
|
+
|
|
48
|
+
agent = LlmAgent(
|
|
49
|
+
config=sampling_agent_config(params),
|
|
50
|
+
context=app_context,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# Create the LLM using the factory
|
|
54
|
+
factory = ModelFactory.create_factory(model_string)
|
|
55
|
+
llm = factory(agent=agent, api_key=api_key)
|
|
56
|
+
|
|
57
|
+
# Attach the LLM to the agent
|
|
58
|
+
agent._llm = llm
|
|
59
|
+
|
|
60
|
+
return llm
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
async def sample(mcp_ctx: ClientSession, params: CreateMessageRequestParams) -> CreateMessageResult:
|
|
64
|
+
"""
|
|
65
|
+
Handle sampling requests from the MCP protocol using SamplingConverter.
|
|
66
|
+
|
|
67
|
+
This function:
|
|
68
|
+
1. Extracts the model from the request
|
|
69
|
+
2. Uses SamplingConverter to convert types
|
|
70
|
+
3. Calls the LLM's generate_prompt method
|
|
71
|
+
4. Returns the result as a CreateMessageResult
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
mcp_ctx: The MCP ClientSession
|
|
75
|
+
params: The sampling request parameters
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
A CreateMessageResult containing the LLM's response
|
|
79
|
+
"""
|
|
80
|
+
# Get server name for notification tracking
|
|
81
|
+
server_name = "unknown"
|
|
82
|
+
if hasattr(mcp_ctx, "session") and hasattr(mcp_ctx.session, "session_server_name"):
|
|
83
|
+
server_name = mcp_ctx.session.session_server_name or "unknown"
|
|
84
|
+
|
|
85
|
+
# Start tracking sampling operation
|
|
86
|
+
try:
|
|
87
|
+
from fast_agent.ui import notification_tracker
|
|
88
|
+
notification_tracker.start_sampling(server_name)
|
|
89
|
+
except Exception:
|
|
90
|
+
# Don't let notification tracking break sampling
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
model: str | None = None
|
|
94
|
+
api_key: str | None = None
|
|
95
|
+
try:
|
|
96
|
+
# Extract model from server config using type-safe helper
|
|
97
|
+
server_config = get_server_config(mcp_ctx)
|
|
98
|
+
|
|
99
|
+
# First priority: explicitly configured sampling model
|
|
100
|
+
if server_config and server_config.sampling:
|
|
101
|
+
model = server_config.sampling.model
|
|
102
|
+
|
|
103
|
+
# Second priority: auto_sampling fallback (if enabled at application level)
|
|
104
|
+
if model is None:
|
|
105
|
+
# Check if auto_sampling is enabled
|
|
106
|
+
auto_sampling_enabled = False
|
|
107
|
+
try:
|
|
108
|
+
from fast_agent.context import get_current_context
|
|
109
|
+
|
|
110
|
+
app_context = get_current_context()
|
|
111
|
+
if app_context and app_context.config:
|
|
112
|
+
auto_sampling_enabled = getattr(app_context.config, "auto_sampling", True)
|
|
113
|
+
except Exception as e:
|
|
114
|
+
logger.debug(f"Could not get application config: {e}")
|
|
115
|
+
auto_sampling_enabled = True # Default to enabled
|
|
116
|
+
|
|
117
|
+
if auto_sampling_enabled:
|
|
118
|
+
# Import here to avoid circular import
|
|
119
|
+
from fast_agent.mcp.mcp_agent_client_session import MCPAgentClientSession
|
|
120
|
+
|
|
121
|
+
# Try agent's model first (from the session)
|
|
122
|
+
if hasattr(mcp_ctx, "session") and isinstance(
|
|
123
|
+
mcp_ctx.session, MCPAgentClientSession
|
|
124
|
+
):
|
|
125
|
+
if mcp_ctx.session.agent_model:
|
|
126
|
+
model = mcp_ctx.session.agent_model
|
|
127
|
+
logger.debug(f"Using agent's model for sampling: {model}")
|
|
128
|
+
if mcp_ctx.session.api_key:
|
|
129
|
+
api_key = mcp_ctx.session.api_key
|
|
130
|
+
logger.debug(f"Using agent's API KEY for sampling: {api_key}")
|
|
131
|
+
|
|
132
|
+
# Fall back to system default model
|
|
133
|
+
if model is None:
|
|
134
|
+
try:
|
|
135
|
+
if app_context and app_context.config and app_context.config.default_model:
|
|
136
|
+
model = app_context.config.default_model
|
|
137
|
+
logger.debug(f"Using system default model for sampling: {model}")
|
|
138
|
+
except Exception as e:
|
|
139
|
+
logger.debug(f"Could not get system default model: {e}")
|
|
140
|
+
|
|
141
|
+
if model is None:
|
|
142
|
+
raise ValueError(
|
|
143
|
+
"No model configured for sampling (server config, agent model, or system default)"
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# Create an LLM instance
|
|
147
|
+
llm = create_sampling_llm(params, model, api_key)
|
|
148
|
+
|
|
149
|
+
# Extract all messages from the request params
|
|
150
|
+
if not params.messages:
|
|
151
|
+
raise ValueError("No messages provided")
|
|
152
|
+
|
|
153
|
+
# Convert all SamplingMessages to PromptMessageExtended objects
|
|
154
|
+
conversation = SamplingConverter.convert_messages(params.messages)
|
|
155
|
+
|
|
156
|
+
# Extract request parameters using our converter
|
|
157
|
+
request_params = SamplingConverter.extract_request_params(params)
|
|
158
|
+
|
|
159
|
+
llm_response: PromptMessageExtended = await llm.generate(conversation, request_params)
|
|
160
|
+
logger.info(f"Complete sampling request : {llm_response.first_text()[:50]}...")
|
|
161
|
+
|
|
162
|
+
return CreateMessageResult(
|
|
163
|
+
role=llm_response.role,
|
|
164
|
+
content=TextContent(type="text", text=llm_response.first_text()),
|
|
165
|
+
model=model,
|
|
166
|
+
stopReason=LlmStopReason.END_TURN.value,
|
|
167
|
+
)
|
|
168
|
+
except Exception as e:
|
|
169
|
+
logger.error(f"Error in sampling: {str(e)}")
|
|
170
|
+
return SamplingConverter.error_result(
|
|
171
|
+
error_message=f"Error in sampling: {str(e)}", model=model
|
|
172
|
+
)
|
|
173
|
+
finally:
|
|
174
|
+
# End tracking sampling operation
|
|
175
|
+
try:
|
|
176
|
+
from fast_agent.ui import notification_tracker
|
|
177
|
+
notification_tracker.end_sampling(server_name)
|
|
178
|
+
except Exception:
|
|
179
|
+
# Don't let notification tracking break sampling
|
|
180
|
+
pass
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def sampling_agent_config(
|
|
184
|
+
params: CreateMessageRequestParams | None = None,
|
|
185
|
+
) -> AgentConfig:
|
|
186
|
+
"""
|
|
187
|
+
Build a sampling AgentConfig based on request parameters.
|
|
188
|
+
|
|
189
|
+
Args:
|
|
190
|
+
params: Optional CreateMessageRequestParams that may contain a system prompt
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
An initialized AgentConfig for use in sampling
|
|
194
|
+
"""
|
|
195
|
+
# Use systemPrompt from params if available, otherwise use default
|
|
196
|
+
instruction = "You are a helpful AI Agent."
|
|
197
|
+
if params and params.systemPrompt is not None:
|
|
198
|
+
instruction = params.systemPrompt
|
|
199
|
+
|
|
200
|
+
return AgentConfig(name="sampling_agent", instruction=instruction)
|