Skip to content

Commit fd71a9d

Browse files
committed
make sure only single command starts with "ros2".
Signed-off-by: Tomoya Fujita <[email protected]>
1 parent e9f0b2a commit fd71a9d

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

ros2ai/api/constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# This is only required when actually accessing api.openai.com
1616
ROS_OPENAI_API_KEY_ENV_VAR = 'OPENAI_API_KEY'
1717

18-
ROS_OPENAI_DEFAULT_MODEL = 'gpt-4o'
18+
ROS_OPENAI_DEFAULT_MODEL = 'gpt-5'
1919
ROS_OLLAMA_DEFAULT_MODEL = 'llama3.1'
2020

2121
ROS_OPENAI_MODEL_NAME_ENV_VAR = 'OPENAI_MODEL_NAME'
@@ -35,8 +35,8 @@
3535
'You are a Robot Operating System version 2 (as known as ROS2) {} distribution ' \
3636
'professional assistant who can provide helpful answers against any questions.'
3737
ROLE_SYSTEM_EXEC_DEFAULT = \
38-
'You are a Robot Operating System 2 (as known as ROS2) {} distribution command line executor, ' \
39-
'provides executable command string only without any comments or code blocks or backticks.'
38+
'You are a Robot Operating System 2 (as known as ROS 2) {} distribution command line executor, ' \
39+
'provides single executable command string only without any comments or code blocks or backticks.'
4040
ROLE_SYSTEM_ENV_VAR = 'OPENAI_ROLE_SYSTEM'
4141

4242
# Temperature controls the consistency for behavior. (range 0.0 - 2.0)

ros2ai/api/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import signal
1616
import subprocess
1717
import os
18+
import re
1819

1920
def run_command(*, command, argv = None, prefix = None):
2021
"""
@@ -118,3 +119,37 @@ def remove_backticks(string) -> str:
118119
:return: The string without backticks.
119120
"""
120121
return string.replace('`', '')
122+
123+
def ros2_single_command(command_string) -> str:
124+
"""
125+
Check if the command string contains only a single command (no command separators).
126+
If multiple commands are found, extract the first 'ros2' command and warn the user.
127+
128+
:command_string: The command string to validate.
129+
:return: The validated single command string, or the first 'ros2' command if multiple found.
130+
"""
131+
# Common command separators in shell
132+
separators = [';', '&&', '||', '&', '|']
133+
134+
# Strip whitespace and check for separators
135+
cleaned_command = command_string.strip()
136+
137+
# Check if command contains separators
138+
has_separators = any(separator in cleaned_command for separator in separators)
139+
140+
if has_separators:
141+
print(f"Warning: Multiple commands detected in \"{command_string}\". Only the first 'ros2' command will be executed.")
142+
143+
# Split by all separators and find first ros2 command
144+
pattern = '|'.join(re.escape(sep) for sep in separators)
145+
commands = re.split(pattern, cleaned_command)
146+
147+
for cmd in commands:
148+
cmd = cmd.strip()
149+
if cmd.startswith('ros2'):
150+
return cmd
151+
152+
# If no ros2 command found, return the first command
153+
return commands[0].strip() if commands else ""
154+
155+
return cleaned_command

ros2ai/verb/exec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ros2ai.api.config import get_role_system
1717
from ros2ai.api.constants import ROLE_SYSTEM_EXEC_DEFAULT
1818
from ros2ai.api.openai import ChatCompletionClient, ChatCompletionParameters
19-
from ros2ai.api.utils import get_ros_distro, run_executable, truncate_before_substring, remove_backticks
19+
from ros2ai.api.utils import get_ros_distro, run_executable, truncate_before_substring, remove_backticks, ros2_single_command
2020
from ros2ai.verb import VerbExtension
2121

2222

@@ -71,9 +71,11 @@ def main(self, *, args):
7171
if (args.debug is True):
7272
client.print_all()
7373
print(f"System role:\n{system_role}")
74+
# Extract command from the response
7475
command_str = truncate_before_substring(
7576
original = client.get_result(), substring = 'ros2')
7677
command_str = remove_backticks(command_str)
78+
command_str = ros2_single_command(command_str)
7779
if not args.dry_run:
7880
return_code = run_executable(command = command_str)
7981
return return_code

scripts/verification.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ command_list=(
2929
"ros2 ai query -h"
3030
"ros2 ai query \"say hello\""
3131
"ros2 ai query \"say hello\" -nv"
32-
"ros2 ai query \"say hello\" -m gpt-5 -t 100"
33-
"ros2 ai exec \"give me all topics\""
32+
"ros2 ai query \"say hello\" -m gpt-5 -t 1000"
33+
"ros2 ai exec -m o3 \"give me all topics\""
34+
"ros2 ai exec -m o4-mini \"give me all topics\""
35+
"ros2 ai exec -m gpt-5 \"give me all topics\""
36+
"ros2 ai exec -m gemini-2.5-pro \"give me all topics\""
3437
"ros2 ai exec \"give me all topics\" --dry-run"
3538
"ros2 ai exec \"give me all topics\" -d"
3639
)

0 commit comments

Comments
 (0)