mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-05-22 18:45:19 +02:00
bubble up exceptions in tools
This commit is contained in:
parent
fd457c0692
commit
122ee00fac
1 changed files with 76 additions and 91 deletions
|
|
@ -38,20 +38,9 @@ class NewResponse(BaseModel):
|
||||||
error_msg: Optional[str] = ""
|
error_msg: Optional[str] = ""
|
||||||
|
|
||||||
async def mock_tool(tool_name: str, args: str, description: str, mock_instructions: str) -> str:
|
async def mock_tool(tool_name: str, args: str, description: str, mock_instructions: str) -> str:
|
||||||
"""
|
try:
|
||||||
Handles tool execution by either using mock instructions or generating a response.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
tool_name: The name of the tool
|
|
||||||
args: The arguments passed to the tool
|
|
||||||
tool_config: The configuration of the tool
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The response from the tool
|
|
||||||
"""
|
|
||||||
print(f"Mock tool called for: {tool_name}")
|
print(f"Mock tool called for: {tool_name}")
|
||||||
|
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "system", "content": f"You are simulating the execution of a tool called '{tool_name}'.Here is the description of the tool: {description}. Here are the instructions for the mock tool: {mock_instructions}. Generate a realistic response as if the tool was actually executed with the given parameters."},
|
{"role": "system", "content": f"You are simulating the execution of a tool called '{tool_name}'.Here is the description of the tool: {description}. Here are the instructions for the mock tool: {mock_instructions}. Generate a realistic response as if the tool was actually executed with the given parameters."},
|
||||||
{"role": "user", "content": f"Generate a realistic response for the tool '{tool_name}' with these parameters: {args}. The response should be concise and focused on what the tool would actually return."}
|
{"role": "user", "content": f"Generate a realistic response for the tool '{tool_name}' with these parameters: {args}. The response should be concise and focused on what the tool would actually return."}
|
||||||
|
|
@ -60,18 +49,13 @@ async def mock_tool(tool_name: str, args: str, description: str, mock_instructio
|
||||||
print(f"Generating simulated response for tool: {tool_name}")
|
print(f"Generating simulated response for tool: {tool_name}")
|
||||||
response_content = generate_openai_output(messages, output_type='text', model="gpt-4o")
|
response_content = generate_openai_output(messages, output_type='text', model="gpt-4o")
|
||||||
return response_content
|
return response_content
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in mock_tool: {str(e)}")
|
||||||
|
return f"Error: {str(e)}"
|
||||||
|
|
||||||
async def call_webhook(tool_name: str, args: str, webhook_url: str, signing_secret: str) -> str:
|
async def call_webhook(tool_name: str, args: str, webhook_url: str, signing_secret: str) -> str:
|
||||||
"""
|
try:
|
||||||
Calls the webhook with the given tool name and arguments.
|
print(f"Calling webhook for tool: {tool_name}")
|
||||||
|
|
||||||
Args:
|
|
||||||
tool_name (str): The name of the tool to call.
|
|
||||||
args (str): The arguments for the tool as a JSON string.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The response from the webhook, or an error message if the call fails.
|
|
||||||
"""
|
|
||||||
content_dict = {
|
content_dict = {
|
||||||
"toolCall": {
|
"toolCall": {
|
||||||
"function": {
|
"function": {
|
||||||
|
|
@ -93,7 +77,6 @@ async def call_webhook(tool_name: str, args: str, webhook_url: str, signing_secr
|
||||||
signature_jwt = jwt.encode(payload, signing_secret, algorithm="HS256")
|
signature_jwt = jwt.encode(payload, signing_secret, algorithm="HS256")
|
||||||
headers["X-Signature-Jwt"] = signature_jwt
|
headers["X-Signature-Jwt"] = signature_jwt
|
||||||
|
|
||||||
try:
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(webhook_url, json=request_body, headers=headers) as response:
|
async with session.post(webhook_url, json=request_body, headers=headers) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
|
|
@ -104,14 +87,12 @@ async def call_webhook(tool_name: str, args: str, webhook_url: str, signing_secr
|
||||||
print(f"Webhook error: {error_msg}")
|
print(f"Webhook error: {error_msg}")
|
||||||
return f"Error: {error_msg}"
|
return f"Error: {error_msg}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception in call_webhook: {str(e)}")
|
logger.error(f"Exception in call_webhook: {str(e)}")
|
||||||
return f"Error: Failed to call webhook - {str(e)}"
|
return f"Error: Failed to call webhook - {str(e)}"
|
||||||
|
|
||||||
async def call_mcp(tool_name: str, args: str, mcp_server_url: str) -> str:
|
async def call_mcp(tool_name: str, args: str, mcp_server_url: str) -> str:
|
||||||
"""
|
try:
|
||||||
Calls the MCP with the given tool name and arguments.
|
print(f"MCP tool called for: {tool_name}")
|
||||||
"""
|
|
||||||
|
|
||||||
async with sse_client(url=mcp_server_url) as streams:
|
async with sse_client(url=mcp_server_url) as streams:
|
||||||
async with ClientSession(*streams) as session:
|
async with ClientSession(*streams) as session:
|
||||||
await session.initialize()
|
await session.initialize()
|
||||||
|
|
@ -120,11 +101,12 @@ async def call_mcp(tool_name: str, args: str, mcp_server_url: str) -> str:
|
||||||
json_output = json.dumps([item.__dict__ for item in response.content], indent=2)
|
json_output = json.dumps([item.__dict__ for item in response.content], indent=2)
|
||||||
|
|
||||||
return json_output
|
return json_output
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in call_mcp: {str(e)}")
|
||||||
|
return f"Error: {str(e)}"
|
||||||
|
|
||||||
async def catch_all(ctx: RunContextWrapper[Any], args: str, tool_name: str, tool_config: dict, complete_request: dict) -> str:
|
async def catch_all(ctx: RunContextWrapper[Any], args: str, tool_name: str, tool_config: dict, complete_request: dict) -> str:
|
||||||
"""
|
try:
|
||||||
Handles all tool calls by dispatching to appropriate functions.
|
|
||||||
"""
|
|
||||||
print(f"Catch all called for tool: {tool_name}")
|
print(f"Catch all called for tool: {tool_name}")
|
||||||
print(f"Args: {args}")
|
print(f"Args: {args}")
|
||||||
print(f"Tool config: {tool_config}")
|
print(f"Tool config: {tool_config}")
|
||||||
|
|
@ -156,6 +138,9 @@ async def catch_all(ctx: RunContextWrapper[Any], args: str, tool_name: str, tool
|
||||||
webhook_url = complete_request.get("toolWebhookUrl", "")
|
webhook_url = complete_request.get("toolWebhookUrl", "")
|
||||||
response_content = await call_webhook(tool_name, args, webhook_url, signing_secret)
|
response_content = await call_webhook(tool_name, args, webhook_url, signing_secret)
|
||||||
return response_content
|
return response_content
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in catch_all: {str(e)}")
|
||||||
|
return f"Error: {str(e)}"
|
||||||
|
|
||||||
|
|
||||||
def get_rag_tool(config: dict, complete_request: dict) -> FunctionTool:
|
def get_rag_tool(config: dict, complete_request: dict) -> FunctionTool:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue