Skip to content

Commit c247bcd

Browse files
authored
add check cli tool (#1178)
1 parent 6fdd37c commit c247bcd

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

examples/slackbot/src/slackbot/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from slackbot.assets import store_user_facts
2323
from slackbot.research_agent import research_prefect_topic
2424
from slackbot.search import (
25+
check_cli_command,
2526
display_callable_signature,
2627
explore_module_offerings,
2728
read_github_issues,
@@ -38,10 +39,9 @@
3839
3940
## Your Mission
4041
Your role is to act as the primary assistant for the user. You will receive raw information from specialized tools. Your job is to synthesize this information into a polished, direct, and complete answer.
41-
If some important aspect of the user's question is unclear, ask them for clarification.
42+
If some important aspect of the user's question is unclear, ASK THEM FOR CLARIFICATION. ADMIT WHEN YOU CANNOT FIND THE ANSWER.
4243
4344
## Key Directives & Rules of Engagement
44-
- **Avoid leaking private details** - _Do not_ mention your internal processes or the tools you used (e.g., avoid phrases like "based on my research" or "the tool returned").
4545
- **Links are Critical:** ALWAYS include relevant links when your tools provide them. This is essential for user trust and allows them to dig deeper. Format them clearly.
4646
- **Assume Prefect 3.x:** Unless the user specifies otherwise, assume the user is using Prefect 3.x. You can mention this assumption IF RELEVANT (e.g., "In Prefect 3.x, you would...").
4747
- **Code is King:** When providing code examples, ensure they are complete and correct. Use your `verify_import_statements` tool's output to guide you.
@@ -63,6 +63,7 @@
6363
2. **For Bugs or Error Reports:** Use `read_github_issues` to find existing discussions or solutions.
6464
3. **For Remembering User Details:** When a user shares information about their goals, environment, or preferences, use `store_facts_about_user` to save these details for future interactions.
6565
4. **For Checking the Work of the Research Agent:** Use `explore_module_offerings` and `display_callable_signature` to verify specific syntax recommendations.
66+
5. **CRITICAL - For CLI Commands:** ALWAYS use `check_cli_command` with --help before suggesting any Prefect CLI command to verify it exists and has the correct syntax. This prevents suggesting non-existent commands.
6667
"""
6768

6869

@@ -197,6 +198,7 @@ def create_agent(
197198
read_github_issues, # For searching GitHub issues
198199
explore_module_offerings, # check the work of the research agent, verify imports, types functions
199200
display_callable_signature, # check the work of the research agent, verify signatures of callable objects
201+
check_cli_command, # verify CLI commands before suggesting them
200202
],
201203
deps_type=UserContext,
202204
)

examples/slackbot/src/slackbot/search.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import subprocess
23

34
import httpx
45
import turbopuffer as tpuf
@@ -225,3 +226,69 @@ def display_callable_signature(import_path: str) -> str:
225226
>>> display_callable_signature('prefect:flow')
226227
"""
227228
return display_signature(import_path)
229+
230+
231+
def check_cli_command(command: str, args: list[str] | None = None) -> str:
232+
"""
233+
Run a CLI command to verify its behavior or check help documentation.
234+
235+
This tool is specifically designed to help verify Prefect CLI commands before suggesting them to users.
236+
Use this to check if a command exists, what its options are, or to verify the correct syntax.
237+
238+
IMPORTANT USAGE GUIDELINES:
239+
- Use this tool BEFORE suggesting any CLI command to a user
240+
- Always check with --help first to verify command structure
241+
- Common commands to verify: prefect deploy, prefect work-pool, prefect worker, etc.
242+
- This helps prevent suggesting non-existent or incorrectly formatted commands
243+
244+
Args:
245+
command: The base command to run (e.g., "prefect", "prefect deploy")
246+
args: Additional arguments to pass (e.g., ["--help"], ["work-pool", "create", "--help"])
247+
248+
Returns:
249+
The output of the command or an error message if it fails
250+
251+
Examples:
252+
# Check if a command exists and see its options
253+
>>> check_cli_command("prefect deploy", ["--help"])
254+
255+
# Verify work pool commands
256+
>>> check_cli_command("prefect work-pool", ["--help"])
257+
258+
# Check specific subcommand help
259+
>>> check_cli_command("prefect", ["worker", "start", "--help"])
260+
"""
261+
if args is None:
262+
args = []
263+
264+
# Construct the full command
265+
full_command = command.split() + args
266+
267+
try:
268+
# Run the command with a timeout
269+
result = subprocess.run(
270+
full_command,
271+
capture_output=True,
272+
text=True,
273+
timeout=10,
274+
check=False, # Don't raise on non-zero exit codes
275+
)
276+
277+
# Combine stdout and stderr for complete output
278+
output = ""
279+
if result.stdout:
280+
output += result.stdout
281+
if result.stderr:
282+
output += f"\n[stderr]: {result.stderr}"
283+
284+
if not output.strip():
285+
output = f"Command ran successfully with exit code {result.returncode} but produced no output"
286+
287+
return output[:2000] # Limit output length to prevent huge responses
288+
289+
except subprocess.TimeoutExpired:
290+
return "Command timed out after 10 seconds"
291+
except FileNotFoundError:
292+
return f"Command '{full_command[0]}' not found. Make sure it's installed and in PATH."
293+
except Exception as e:
294+
return f"Error running command: {str(e)}"

0 commit comments

Comments
 (0)