|
3 | 3 | #include "log.h"
|
4 | 4 | #include "regex-partial.h"
|
5 | 5 |
|
| 6 | +#include <algorithm> |
| 7 | +#include <cctype> |
6 | 8 | #include <optional>
|
7 | 9 | #include <stdexcept>
|
8 | 10 | #include <string>
|
| 11 | +#include <string_view> |
9 | 12 | #include <vector>
|
10 | 13 |
|
11 | 14 | using json = nlohmann::ordered_json;
|
@@ -181,25 +184,111 @@ bool common_chat_msg_parser::try_parse_reasoning(const std::string & start_think
|
181 | 184 | add_reasoning_content(stripped_reasoning);
|
182 | 185 | }
|
183 | 186 | };
|
184 |
| - if (syntax_.reasoning_format != COMMON_REASONING_FORMAT_NONE) { |
185 |
| - if (syntax_.thinking_forced_open || try_consume_literal(start_think)) { |
186 |
| - if (auto res = try_find_literal(end_think)) { |
187 |
| - handle_reasoning(res->prelude, /* closed */ true); |
188 |
| - consume_spaces(); |
189 |
| - return true; |
190 |
| - } |
191 |
| - auto rest = consume_rest(); |
| 187 | + |
| 188 | + if (syntax_.reasoning_format == COMMON_REASONING_FORMAT_NONE) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + const size_t saved_pos = pos_; |
| 193 | + const size_t saved_content_size = result_.content.size(); |
| 194 | + const size_t saved_reasoning_size = result_.reasoning_content.size(); |
| 195 | + |
| 196 | + auto restore_state = [&]() { |
| 197 | + move_to(saved_pos); |
| 198 | + result_.content.resize(saved_content_size); |
| 199 | + result_.reasoning_content.resize(saved_reasoning_size); |
| 200 | + }; |
| 201 | + |
| 202 | + // Allow leading whitespace to be preserved as content when reasoning is present at the start |
| 203 | + size_t cursor = pos_; |
| 204 | + size_t whitespace_end = cursor; |
| 205 | + while (whitespace_end < input_.size() && std::isspace(static_cast<unsigned char>(input_[whitespace_end]))) { |
| 206 | + ++whitespace_end; |
| 207 | + } |
| 208 | + |
| 209 | + if (whitespace_end >= input_.size()) { |
| 210 | + restore_state(); |
| 211 | + if (syntax_.thinking_forced_open) { |
| 212 | + auto rest = input_.substr(saved_pos); |
192 | 213 | if (!rest.empty()) {
|
193 | 214 | handle_reasoning(rest, /* closed */ !is_partial());
|
194 | 215 | }
|
195 |
| - // Allow unclosed thinking tags, for now (https://github.com/ggml-org/llama.cpp/issues/13812, https://github.com/ggml-org/llama.cpp/issues/13877) |
196 |
| - // if (!syntax_.thinking_forced_open) { |
197 |
| - // throw common_chat_msg_partial_exception(end_think); |
198 |
| - // } |
| 216 | + move_to(input_.size()); |
199 | 217 | return true;
|
200 | 218 | }
|
| 219 | + return false; |
| 220 | + } |
| 221 | + |
| 222 | + cursor = whitespace_end; |
| 223 | + const size_t remaining = input_.size() - cursor; |
| 224 | + const size_t start_prefix = std::min(start_think.size(), remaining); |
| 225 | + const bool has_start_tag = input_.compare(cursor, start_prefix, start_think, 0, start_prefix) == 0; |
| 226 | + |
| 227 | + if (has_start_tag && start_prefix < start_think.size()) { |
| 228 | + move_to(input_.size()); |
| 229 | + return true; |
| 230 | + } |
| 231 | + |
| 232 | + if (has_start_tag) { |
| 233 | + if (whitespace_end > pos_) { |
| 234 | + add_content(input_.substr(pos_, whitespace_end - pos_)); |
| 235 | + } |
| 236 | + cursor += start_think.size(); |
| 237 | + } else if (syntax_.thinking_forced_open) { |
| 238 | + cursor = whitespace_end; |
| 239 | + } else { |
| 240 | + restore_state(); |
| 241 | + return false; |
| 242 | + } |
| 243 | + while (true) { |
| 244 | + if (cursor >= input_.size()) { |
| 245 | + move_to(input_.size()); |
| 246 | + return true; |
| 247 | + } |
| 248 | + |
| 249 | + size_t end_pos = input_.find(end_think, cursor); |
| 250 | + if (end_pos == std::string::npos) { |
| 251 | + std::string_view remaining_view(input_.data() + cursor, input_.size() - cursor); |
| 252 | + size_t partial_off = string_find_partial_stop(remaining_view, end_think); |
| 253 | + size_t reasoning_end = partial_off == std::string::npos ? input_.size() : cursor + partial_off; |
| 254 | + if (reasoning_end > cursor) { |
| 255 | + handle_reasoning(input_.substr(cursor, reasoning_end - cursor), /* closed */ partial_off == std::string::npos && !is_partial()); |
| 256 | + } |
| 257 | + move_to(input_.size()); |
| 258 | + return true; |
| 259 | + } |
| 260 | + |
| 261 | + if (end_pos > cursor) { |
| 262 | + handle_reasoning(input_.substr(cursor, end_pos - cursor), /* closed */ true); |
| 263 | + } else { |
| 264 | + handle_reasoning("", /* closed */ true); |
| 265 | + } |
| 266 | + |
| 267 | + cursor = end_pos + end_think.size(); |
| 268 | + |
| 269 | + while (cursor < input_.size() && std::isspace(static_cast<unsigned char>(input_[cursor]))) { |
| 270 | + ++cursor; |
| 271 | + } |
| 272 | + |
| 273 | + const size_t next_remaining = input_.size() - cursor; |
| 274 | + if (next_remaining == 0) { |
| 275 | + move_to(cursor); |
| 276 | + return true; |
| 277 | + } |
| 278 | + |
| 279 | + const size_t next_prefix = std::min(start_think.size(), next_remaining); |
| 280 | + if (input_.compare(cursor, next_prefix, start_think, 0, next_prefix) == 0) { |
| 281 | + if (next_prefix < start_think.size()) { |
| 282 | + move_to(input_.size()); |
| 283 | + return true; |
| 284 | + } |
| 285 | + cursor += start_think.size(); |
| 286 | + continue; |
| 287 | + } |
| 288 | + |
| 289 | + move_to(cursor); |
| 290 | + return true; |
201 | 291 | }
|
202 |
| - return false; |
203 | 292 | }
|
204 | 293 |
|
205 | 294 | std::string common_chat_msg_parser::consume_rest() {
|
|
0 commit comments