From c9eb4aa60ae42312a30e822ec511c5c1d8c80918 Mon Sep 17 00:00:00 2001 From: Alexander Brune Date: Mon, 1 Dec 2025 20:42:12 +0100 Subject: [PATCH] Support reasoning models Reasoning models prepend `...` to the actual answer in the response message. That part should obviously not be included in the command string suggested by please-cli. Signed-off-by: Alexander Brune --- please.sh | 6 ++++++ test/strip_reasoning.bats | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 test/strip_reasoning.bats diff --git a/please.sh b/please.sh index 0fa091e..7fa297c 100755 --- a/please.sh +++ b/please.sh @@ -187,6 +187,11 @@ get_key_from_keychain() { PLEASE_OPENAI_API_KEY="${key}" } +strip_reasoning() { + # Strip think blocks (both inline and multiline) + echo "$1" | sed 's/.*<\/think>//g' | sed '//,/<\/think>/d' +} + get_command() { role="You translate the given input into a Linux command. You may not use natural language, but only a Linux shell command as an answer. Do not use markdown. Do not quote the whole output. If you do not know the answer, answer with \\\"${fail_msg}\\\"." @@ -199,6 +204,7 @@ get_command() { debug "Sending request to OpenAI API: ${payload}" perform_openai_request + message=$(strip_reasoning "$message") command="${message}" } diff --git a/test/strip_reasoning.bats b/test/strip_reasoning.bats new file mode 100644 index 0000000..4218b85 --- /dev/null +++ b/test/strip_reasoning.bats @@ -0,0 +1,19 @@ +#!/usr/bin/env bats + +load $BATS_TEST_DIRNAME/../please.sh + +@test "strip inline reasoning block" { + input="thinking hereecho 'hello world'" + result=$(strip_reasoning "$input") + [ "$result" == "echo 'hello world'" ] +} + +@test "strip multiline reasoning block" { + input=" +thinking here +more thinking + +ls -la" + result=$(strip_reasoning "$input") + [ "$result" == "ls -la" ] +} \ No newline at end of file