jwadow commited on
Commit
9df35c0
·
1 Parent(s): b994a02

feat: implement fake reasoning with extended thinking support (#11)

Browse files
.env.example CHANGED
@@ -89,6 +89,38 @@ PROXY_API_KEY="my-super-secret-password-123"
89
  # Default: 300 seconds (5 minutes) - generous timeout to avoid premature disconnects.
90
  # STREAMING_READ_TIMEOUT="300"
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # ===========================================
93
  # DEBUG (for development only)
94
  # ===========================================
 
89
  # Default: 300 seconds (5 minutes) - generous timeout to avoid premature disconnects.
90
  # STREAMING_READ_TIMEOUT="300"
91
 
92
+ # ===========================================
93
+ # FAKE REASONING (Extended Thinking via Tag Injection)
94
+ # ===========================================
95
+
96
+ # Enable fake reasoning - injects special tags into requests to enable model reasoning.
97
+ # When enabled, the model will include its reasoning process in the response.
98
+ # The response is then parsed and converted to OpenAI-compatible reasoning_content format.
99
+ #
100
+ # WHY "FAKE"? This is NOT native extended thinking API support. Instead, we inject
101
+ # <thinking_mode>enabled</thinking_mode> tags into the prompt, and the model responds
102
+ # with <thinking>...</thinking> blocks that we parse and convert to reasoning_content.
103
+ # It works great, but it's a hack - hence "fake" reasoning.
104
+ #
105
+ # Default: true (ENABLED by default for premium experience out of the box!)
106
+ # To disable, set to false:
107
+ # FAKE_REASONING=false
108
+
109
+ # Maximum thinking length in tokens.
110
+ # This value is injected into the request as <max_thinking_length>{value}</max_thinking_length>
111
+ # Higher values allow for more detailed reasoning but increase response time and token usage.
112
+ # Default: 4000 tokens
113
+ # FAKE_REASONING_MAX_TOKENS=4000
114
+
115
+ # How to handle the thinking block in responses:
116
+ # - "as_reasoning_content": Extract to reasoning_content field (OpenAI-compatible, recommended)
117
+ # - "remove": Remove thinking block completely, return only final answer
118
+ # - "pass": Pass through as-is with original tags in content
119
+ # - "strip_tags": Remove tags but keep thinking content in regular content
120
+ #
121
+ # Default: "as_reasoning_content"
122
+ # FAKE_REASONING_HANDLING=as_reasoning_content
123
+
124
  # ===========================================
125
  # DEBUG (for development only)
126
  # ===========================================
README.md CHANGED
@@ -21,13 +21,13 @@
21
  | Feature | Description |
22
  |---------|-------------|
23
  | 🔌 **OpenAI-compatible API** | Works with any OpenAI client out of the box |
 
24
  | 💬 **Full message history** | Passes complete conversation context |
25
  | 🛠️ **Tool Calling** | Supports function calling in OpenAI format |
26
  | 📡 **Streaming** | Full SSE streaming support |
27
  | 🔄 **Retry Logic** | Automatic retries on errors (403, 429, 5xx) |
28
  | 📋 **Extended model list** | Including versioned models |
29
  | 🔐 **Smart token management** | Automatic refresh before expiration |
30
- | 🧩 **Modular architecture** | Easy to extend with new providers |
31
 
32
  ---
33
 
@@ -330,37 +330,6 @@ print(response.content)
330
 
331
  ---
332
 
333
- ## 📁 Project Structure
334
-
335
- ```
336
- kiro-openai-gateway/
337
- ├── main.py # Entry point, FastAPI app creation
338
- ├── requirements.txt # Python dependencies
339
- ├── .env.example # Environment configuration example
340
- │
341
- ├── kiro_gateway/ # Main package
342
- │ ├── __init__.py # Package exports
343
- │ ├── config.py # Configuration and constants
344
- │ ├── models.py # Pydantic models for OpenAI API
345
- │ ├── auth.py # KiroAuthManager - token management
346
- │ ├── cache.py # ModelInfoCache - model caching
347
- │ ├── utils.py # Helper utilities
348
- │ ├── converters.py # OpenAI <-> Kiro conversion
349
- │ ├── parsers.py # AWS SSE stream parsers
350
- │ ├── streaming.py # Response streaming logic
351
- │ ├── http_client.py # HTTP client with retry logic
352
- │ ├── debug_logger.py # Debug logging (optional)
353
- │ └── routes.py # FastAPI routes
354
- │
355
- ├── tests/ # Tests
356
- │ ├── unit/ # Unit tests
357
- │ └── integration/ # Integration tests
358
- │
359
- └── debug_logs/ # Debug logs (generated when enabled)
360
- ```
361
-
362
- ---
363
-
364
  ## 🔧 Debugging
365
 
366
  Debug logging is **disabled by default**. To enable, add to your `.env`:
@@ -396,34 +365,6 @@ When enabled, requests are logged to the `debug_logs/` folder:
396
 
397
  ---
398
 
399
- ## 🧪 Testing
400
-
401
- ```bash
402
- # Run all tests
403
- pytest
404
-
405
- # Run unit tests only
406
- pytest tests/unit/
407
-
408
- # Run with coverage
409
- pytest --cov=kiro_gateway
410
- ```
411
-
412
- ---
413
-
414
- ## 🔌 Extending with New Providers
415
-
416
- The modular architecture makes it easy to add support for other providers:
417
-
418
- 1. Create a new module `kiro_gateway/providers/new_provider.py`
419
- 2. Implement the required classes:
420
- - `NewProviderAuthManager` — token management
421
- - `NewProviderConverter` — format conversion
422
- - `NewProviderParser` — response parsing
423
- 3. Add routes to `routes.py` or create a separate router
424
-
425
- ---
426
-
427
  ## 📜 License
428
 
429
  This project is licensed under the **GNU Affero General Public License v3.0 (AGPL-3.0)**.
 
21
  | Feature | Description |
22
  |---------|-------------|
23
  | 🔌 **OpenAI-compatible API** | Works with any OpenAI client out of the box |
24
+ | 🧠 **Extended Thinking** | See how the model reasons before answering |
25
  | 💬 **Full message history** | Passes complete conversation context |
26
  | 🛠️ **Tool Calling** | Supports function calling in OpenAI format |
27
  | 📡 **Streaming** | Full SSE streaming support |
28
  | 🔄 **Retry Logic** | Automatic retries on errors (403, 429, 5xx) |
29
  | 📋 **Extended model list** | Including versioned models |
30
  | 🔐 **Smart token management** | Automatic refresh before expiration |
 
31
 
32
  ---
33
 
 
330
 
331
  ---
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  ## 🔧 Debugging
334
 
335
  Debug logging is **disabled by default**. To enable, add to your `.env`:
 
365
 
366
  ---
367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
  ## 📜 License
369
 
370
  This project is licensed under the **GNU Affero General Public License v3.0 (AGPL-3.0)**.
kiro_gateway/config.py CHANGED
@@ -336,11 +336,59 @@ def _warn_timeout_configuration():
336
  """
337
  print(warning_text, file=sys.stderr)
338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339
  # ==================================================================================================
340
  # Application Version
341
  # ==================================================================================================
342
 
343
- APP_VERSION: str = "1.0.8"
344
  APP_TITLE: str = "Kiro API Gateway"
345
  APP_DESCRIPTION: str = "OpenAI-compatible interface for Kiro API (AWS CodeWhisperer). Made by @jwadow"
346
 
 
336
  """
337
  print(warning_text, file=sys.stderr)
338
 
339
+ # ==================================================================================================
340
+ # Fake Reasoning Settings (Extended Thinking via Tag Injection)
341
+ # ==================================================================================================
342
+
343
+ # Enable fake reasoning - injects special tags into requests to enable model reasoning.
344
+ # When enabled, the model will include its reasoning process in the response wrapped in tags.
345
+ # The response is then parsed and converted to OpenAI-compatible reasoning_content format.
346
+ #
347
+ # WHY "FAKE"? This is NOT native extended thinking API support. Instead, we inject
348
+ # <thinking_mode>enabled</thinking_mode> tags into the prompt, and the model responds
349
+ # with <thinking>...</thinking> blocks that we parse and convert to reasoning_content.
350
+ # It works great, but it's a hack - hence "fake" reasoning.
351
+ #
352
+ # Default: true (enabled) - provides premium experience out of the box
353
+ _FAKE_REASONING_RAW: str = os.getenv("FAKE_REASONING", "").lower()
354
+ # Default is True - if env var is not set or empty, enable fake reasoning
355
+ FAKE_REASONING_ENABLED: bool = _FAKE_REASONING_RAW not in ("false", "0", "no", "disabled", "off")
356
+
357
+ # Maximum thinking length in tokens.
358
+ # This value is injected into the request as <max_thinking_length>{value}</max_thinking_length>
359
+ # Higher values allow for more detailed reasoning but increase response time and token usage.
360
+ # Default: 4000 tokens
361
+ FAKE_REASONING_MAX_TOKENS: int = int(os.getenv("FAKE_REASONING_MAX_TOKENS", "4000"))
362
+
363
+ # How to handle the thinking block in responses:
364
+ # - "as_reasoning_content": Extract to reasoning_content field (OpenAI-compatible, recommended)
365
+ # - "remove": Remove thinking block completely, return only final answer
366
+ # - "pass": Pass through as-is with original tags in content
367
+ # - "strip_tags": Remove tags but keep thinking content in regular content
368
+ #
369
+ # Default: "as_reasoning_content"
370
+ _FAKE_REASONING_HANDLING_RAW: str = os.getenv("FAKE_REASONING_HANDLING", "as_reasoning_content").lower()
371
+ if _FAKE_REASONING_HANDLING_RAW in ("as_reasoning_content", "remove", "pass", "strip_tags"):
372
+ FAKE_REASONING_HANDLING: str = _FAKE_REASONING_HANDLING_RAW
373
+ else:
374
+ FAKE_REASONING_HANDLING: str = "as_reasoning_content"
375
+
376
+ # List of opening tags to detect thinking blocks.
377
+ # The parser will look for any of these tags at the start of the response.
378
+ # Order matters - first match wins.
379
+ FAKE_REASONING_OPEN_TAGS: List[str] = ["<thinking>", "<think>", "<reasoning>", "<thought>"]
380
+
381
+ # Maximum size of initial buffer for tag detection (characters).
382
+ # If no thinking tag is found within this limit, content is treated as regular response.
383
+ # Default: 100 characters
384
+ FAKE_REASONING_INITIAL_BUFFER_SIZE: int = 100
385
+
386
+
387
  # ==================================================================================================
388
  # Application Version
389
  # ==================================================================================================
390
 
391
+ APP_VERSION: str = "1.0.9"
392
  APP_TITLE: str = "Kiro API Gateway"
393
  APP_DESCRIPTION: str = "OpenAI-compatible interface for Kiro API (AWS CodeWhisperer). Made by @jwadow"
394
 
kiro_gateway/converters.py CHANGED
@@ -1,6 +1,7 @@
1
  # -*- coding: utf-8 -*-
2
 
3
  # Kiro OpenAI Gateway
 
4
  # Copyright (C) 2025 Jwadow
5
  #
6
  # This program is free software: you can redistribute it and/or modify
@@ -17,13 +18,13 @@
17
  # along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 
19
  """
20
- Конвертеры для преобразования форматов OpenAI <-> Kiro.
21
 
22
- Содержит функции для:
23
- - Извлечения текстового контента из различных форматов
24
- - Объединения соседних сообщений
25
- - Построения истории разговора для Kiro API
26
- - Сборки полного payload для запроса
27
  """
28
 
29
  import json
@@ -31,24 +32,29 @@ from typing import Any, Dict, List, Optional, Tuple
31
 
32
  from loguru import logger
33
 
34
- from kiro_gateway.config import get_internal_model_id, TOOL_DESCRIPTION_MAX_LENGTH
 
 
 
 
 
35
  from kiro_gateway.models import ChatMessage, ChatCompletionRequest, Tool
36
 
37
 
38
  def extract_text_content(content: Any) -> str:
39
  """
40
- Извлекает текстовый контент из различных форматов.
41
 
42
- OpenAI API поддерживает несколько форматов content:
43
- - Строка: "Hello, world!"
44
- - Список: [{"type": "text", "text": "Hello"}]
45
- - None: пустое сообщение
46
 
47
  Args:
48
- content: Контент в любом поддерживаемом формате
49
 
50
  Returns:
51
- Извлечённый текст или пустая строка
52
 
53
  Example:
54
  >>> extract_text_content("Hello")
@@ -76,20 +82,72 @@ def extract_text_content(content: Any) -> str:
76
  return str(content)
77
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
80
  """
81
- Объединяет соседние сообщения с одинаковой ролью и обрабатывает tool messages.
82
 
83
- Kiro API не принимает несколько сообщений подряд от одного role.
84
- Эта функция объединяет такие сообщения в одно.
85
 
86
- Tool messages (role="tool") преобразуются в user messages с tool_results.
87
 
88
  Args:
89
- messages: Список сообщений
90
 
91
  Returns:
92
- Список сообщений с объединёнными соседними сообщениями
93
 
94
  Example:
95
  >>> msgs = [
@@ -105,13 +163,13 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
105
  if not messages:
106
  return []
107
 
108
- # Сначала преобразуем tool messages в user messages с tool_results
109
  processed = []
110
  pending_tool_results = []
111
 
112
  for msg in messages:
113
  if msg.role == "tool":
114
- # Собираем tool results
115
  tool_result = {
116
  "type": "tool_result",
117
  "tool_use_id": msg.tool_call_id or "",
@@ -120,9 +178,9 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
120
  pending_tool_results.append(tool_result)
121
  logger.debug(f"Collected tool result for tool_call_id={msg.tool_call_id}")
122
  else:
123
- # Если есть накопленные tool results, создаём user message с ними
124
  if pending_tool_results:
125
- # Создаём user message с tool_results
126
  tool_results_msg = ChatMessage(
127
  role="user",
128
  content=pending_tool_results.copy()
@@ -133,7 +191,7 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
133
 
134
  processed.append(msg)
135
 
136
- # Если остались tool results в конце
137
  if pending_tool_results:
138
  tool_results_msg = ChatMessage(
139
  role="user",
@@ -142,7 +200,7 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
142
  processed.append(tool_results_msg)
143
  logger.debug(f"Created final user message with {len(pending_tool_results)} tool results")
144
 
145
- # Теперь объединяем соседние сообщения с одинаковой ролью
146
  merged = []
147
  for msg in processed:
148
  if not merged:
@@ -151,8 +209,8 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
151
 
152
  last = merged[-1]
153
  if msg.role == last.role:
154
- # Объединяем контент
155
- # Если оба контента - списки, объединяем списки
156
  if isinstance(last.content, list) and isinstance(msg.content, list):
157
  last.content = last.content + msg.content
158
  elif isinstance(last.content, list):
@@ -164,9 +222,9 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
164
  current_text = extract_text_content(msg.content)
165
  last.content = f"{last_text}\n{current_text}"
166
 
167
- # Объединяем tool_calls для assistant сообщений
168
- # Критично: без этого теряются tool_calls из второго и последующих сообщений,
169
- # что приводит к ошибке 400 от Kiro API (toolResult без соответствующего toolUse)
170
  if msg.role == "assistant" and msg.tool_calls:
171
  if last.tool_calls is None:
172
  last.tool_calls = []
@@ -182,17 +240,17 @@ def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
182
 
183
  def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[str, Any]]:
184
  """
185
- Строит массив history для Kiro API из OpenAI messages.
186
 
187
- Kiro API ожидает чередование userInputMessage и assistantResponseMessage.
188
- Эта функция преобразует OpenAI формат в Kiro формат.
189
 
190
  Args:
191
- messages: Список сообщений в формате OpenAI
192
- model_id: Внутренний ID модели Kiro
193
 
194
  Returns:
195
- Список словарей для поля history в Kiro API
196
 
197
  Example:
198
  >>> msgs = [ChatMessage(role="user", content="Hello")]
@@ -212,7 +270,7 @@ def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[
212
  "origin": "AI_EDITOR",
213
  }
214
 
215
- # Обработка tool_results (ответы на tool calls)
216
  tool_results = _extract_tool_results(msg.content)
217
  if tool_results:
218
  user_input["userInputMessageContext"] = {"toolResults": tool_results}
@@ -224,7 +282,7 @@ def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[
224
 
225
  assistant_response = {"content": content}
226
 
227
- # Обработка tool_calls
228
  tool_uses = _extract_tool_uses(msg)
229
  if tool_uses:
230
  assistant_response["toolUses"] = tool_uses
@@ -232,7 +290,7 @@ def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[
232
  history.append({"assistantResponseMessage": assistant_response})
233
 
234
  elif msg.role == "system":
235
- # System prompt обрабатывается отдельно в build_kiro_payload
236
  pass
237
 
238
  return history
@@ -240,13 +298,13 @@ def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[
240
 
241
  def _extract_tool_results(content: Any) -> List[Dict[str, Any]]:
242
  """
243
- Извлекает tool results из контента сообщения.
244
 
245
  Args:
246
- content: Контент сообщения (может быть списком)
247
 
248
  Returns:
249
- Список tool results в формате Kiro
250
  """
251
  tool_results = []
252
 
@@ -266,19 +324,19 @@ def process_tools_with_long_descriptions(
266
  tools: Optional[List[Tool]]
267
  ) -> Tuple[Optional[List[Tool]], str]:
268
  """
269
- Обрабатывает tools с длинными descriptions.
270
 
271
- Kiro API имеет ограничение на длину description в toolSpecification.
272
- Если description превышает лимит, полное описание переносится в system prompt,
273
- а в tool остаётся ссылка на документацию.
274
 
275
  Args:
276
- tools: Список инструментов из запроса OpenAI
277
 
278
  Returns:
279
- Tuple из:
280
- - Список tools с обработанными descriptions (или None если tools пуст)
281
- - Строка с документацией для добавления в system prompt (пустая если все descriptions короткие)
282
 
283
  Example:
284
  >>> tools = [Tool(type="function", function=ToolFunction(name="bash", description="Very long..."))]
@@ -289,7 +347,7 @@ def process_tools_with_long_descriptions(
289
  if not tools:
290
  return None, ""
291
 
292
- # Если лимит отключен (0), возвращаем tools без изменений
293
  if TOOL_DESCRIPTION_MAX_LENGTH <= 0:
294
  return tools, ""
295
 
@@ -304,10 +362,10 @@ def process_tools_with_long_descriptions(
304
  description = tool.function.description or ""
305
 
306
  if len(description) <= TOOL_DESCRIPTION_MAX_LENGTH:
307
- # Description короткий - оставляем как есть
308
  processed_tools.append(tool)
309
  else:
310
- # Description слишком длинный - переносим в system prompt
311
  tool_name = tool.function.name
312
 
313
  logger.debug(
@@ -315,11 +373,11 @@ def process_tools_with_long_descriptions(
315
  f"moving to system prompt"
316
  )
317
 
318
- # Создаём документацию для system prompt
319
  tool_documentation_parts.append(f"## Tool: {tool_name}\n\n{description}")
320
 
321
- # Создаём копию tool с reference description
322
- # Используем модель Tool для создания новой копии
323
  from kiro_gateway.models import ToolFunction
324
 
325
  reference_description = f"[Full documentation in system prompt under '## Tool: {tool_name}']"
@@ -334,7 +392,7 @@ def process_tools_with_long_descriptions(
334
  )
335
  processed_tools.append(processed_tool)
336
 
337
- # Формируем итоговую документацию
338
  tool_documentation = ""
339
  if tool_documentation_parts:
340
  tool_documentation = (
@@ -349,17 +407,17 @@ def process_tools_with_long_descriptions(
349
 
350
  def _extract_tool_uses(msg: ChatMessage) -> List[Dict[str, Any]]:
351
  """
352
- Извлекает tool uses из сообщения assistant.
353
 
354
  Args:
355
- msg: Сообщение assistant
356
 
357
  Returns:
358
- Список tool uses в формате Kiro
359
  """
360
  tool_uses = []
361
 
362
- # Из поля tool_calls
363
  if msg.tool_calls:
364
  for tc in msg.tool_calls:
365
  if isinstance(tc, dict):
@@ -369,7 +427,7 @@ def _extract_tool_uses(msg: ChatMessage) -> List[Dict[str, Any]]:
369
  "toolUseId": tc.get("id", "")
370
  })
371
 
372
- # Из content (если там есть tool_use)
373
  if isinstance(msg.content, list):
374
  for item in msg.content:
375
  if isinstance(item, dict) and item.get("type") == "tool_use":
@@ -388,34 +446,34 @@ def build_kiro_payload(
388
  profile_arn: str
389
  ) -> dict:
390
  """
391
- Строит полный payload для Kiro API.
392
 
393
- Включает:
394
- - Полную историю сообщений
395
- - System prompt (добавляется к первому user сообщению)
396
- - Tools definitions (с обработкой длинных descriptions)
397
- - Текущее сообщение
398
 
399
- Если tools содержат слишком длинные descriptions, они автоматически
400
- переносятся в system prompt, а в tool остаётся ссылка на документацию.
401
 
402
  Args:
403
- request_data: Запрос в формате OpenAI
404
- conversation_id: Уникальный ID разговора
405
- profile_arn: ARN профиля AWS CodeWhisperer
406
 
407
  Returns:
408
- Словарь payload для POST запроса к Kiro API
409
 
410
  Raises:
411
- ValueError: Если нет сообщений для отправки
412
  """
413
  messages = list(request_data.messages)
414
 
415
- # Обрабатываем tools с длинными descriptions
416
  processed_tools, tool_documentation = process_tools_with_long_descriptions(request_data.tools)
417
 
418
- # Извлекаем system prompt
419
  system_prompt = ""
420
  non_system_messages = []
421
  for msg in messages:
@@ -425,23 +483,23 @@ def build_kiro_payload(
425
  non_system_messages.append(msg)
426
  system_prompt = system_prompt.strip()
427
 
428
- # Добавляем документацию по tools в system prompt если есть
429
  if tool_documentation:
430
  system_prompt = system_prompt + tool_documentation if system_prompt else tool_documentation.strip()
431
 
432
- # Объединяем соседние сообщения с одинаковой ролью
433
  merged_messages = merge_adjacent_messages(non_system_messages)
434
 
435
  if not merged_messages:
436
  raise ValueError("No messages to send")
437
 
438
- # Получаем внутренний ID модели
439
  model_id = get_internal_model_id(request_data.model)
440
 
441
- # Строим историю (все сообщения кроме последнего)
442
  history_messages = merged_messages[:-1] if len(merged_messages) > 1 else []
443
 
444
- # Если есть system prompt, добавляем его к первому user сообщению в истории
445
  if system_prompt and history_messages:
446
  first_msg = history_messages[0]
447
  if first_msg.role == "user":
@@ -450,16 +508,16 @@ def build_kiro_payload(
450
 
451
  history = build_kiro_history(history_messages, model_id)
452
 
453
- # Текущее сообщение (последнее)
454
  current_message = merged_messages[-1]
455
  current_content = extract_text_content(current_message.content)
456
 
457
- # Если system prompt есть, но история пуста - добавляем к текущему сообщению
458
  if system_prompt and not history:
459
  current_content = f"{system_prompt}\n\n{current_content}"
460
 
461
- # Если текущее сообщение - assistant, нужно добавить его в историю
462
- # и создать user сообщение "Continue"
463
  if current_message.role == "assistant":
464
  history.append({
465
  "assistantResponseMessage": {
@@ -468,24 +526,29 @@ def build_kiro_payload(
468
  })
469
  current_content = "Continue"
470
 
471
- # Если контент пустой
472
  if not current_content:
473
  current_content = "Continue"
474
 
475
- # Строим userInputMessage
 
 
 
 
 
476
  user_input_message = {
477
  "content": current_content,
478
  "modelId": model_id,
479
  "origin": "AI_EDITOR",
480
  }
481
 
482
- # Добавляем tools и tool_results если есть
483
- # Используем обработанные tools (с короткими descriptions)
484
  user_input_context = _build_user_input_context(request_data, current_message, processed_tools)
485
  if user_input_context:
486
  user_input_message["userInputMessageContext"] = user_input_context
487
 
488
- # Собираем финальный payload
489
  payload = {
490
  "conversationState": {
491
  "chatTriggerType": "MANUAL",
@@ -496,11 +559,11 @@ def build_kiro_payload(
496
  }
497
  }
498
 
499
- # Добавляем историю только если она не пуста
500
  if history:
501
  payload["conversationState"]["history"] = history
502
 
503
- # Добавляем profileArn
504
  if profile_arn:
505
  payload["profileArn"] = profile_arn
506
 
@@ -509,36 +572,36 @@ def build_kiro_payload(
509
 
510
  def _sanitize_json_schema(schema: Optional[Dict[str, Any]]) -> Dict[str, Any]:
511
  """
512
- Очищает JSON Schema от полей, которые Kiro API не принимает.
513
 
514
- Kiro API возвращает ошибку 400 "Improperly formed request" если:
515
- - required является пустым массивом []
516
- - additionalProperties присутствует в схеме
517
 
518
- Эта функция рекурсивно обрабатывает схему и удаляет проблемные поля.
519
 
520
  Args:
521
- schema: JSON Schema для очистки
522
 
523
  Returns:
524
- Очищенная копия схемы
525
  """
526
  if not schema:
527
  return {}
528
 
529
- # Создаём копию чтобы не мутировать оригинал
530
  result = {}
531
 
532
  for key, value in schema.items():
533
- # Пропускаем пустые required массивы
534
  if key == "required" and isinstance(value, list) and len(value) == 0:
535
  continue
536
 
537
- # Пропускаем additionalProperties - Kiro API его не поддерживает
538
  if key == "additionalProperties":
539
  continue
540
 
541
- # Рекурсивно обрабатываем вложенные объекты
542
  if key == "properties" and isinstance(value, dict):
543
  result[key] = {
544
  prop_name: _sanitize_json_schema(prop_value) if isinstance(prop_value, dict) else prop_value
@@ -547,7 +610,7 @@ def _sanitize_json_schema(schema: Optional[Dict[str, Any]]) -> Dict[str, Any]:
547
  elif isinstance(value, dict):
548
  result[key] = _sanitize_json_schema(value)
549
  elif isinstance(value, list):
550
- # Обрабатываем списки (например, anyOf, oneOf)
551
  result[key] = [
552
  _sanitize_json_schema(item) if isinstance(item, dict) else item
553
  for item in value
@@ -564,34 +627,34 @@ def _build_user_input_context(
564
  processed_tools: Optional[List[Tool]] = None
565
  ) -> Dict[str, Any]:
566
  """
567
- Строит userInputMessageContext для текущего сообщения.
568
 
569
- Включает tools definitions и tool_results.
570
 
571
  Args:
572
- request_data: Запрос с tools
573
- current_message: Текущее сообщение
574
- processed_tools: Обработанные tools с короткими descriptions (опционально).
575
- Если None, используются tools из request_data.
576
 
577
  Returns:
578
- Словарь с контекстом или пустой словарь
579
  """
580
  context = {}
581
 
582
- # Используем обработанные tools если переданы, иначе оригинальные
583
  tools_to_use = processed_tools if processed_tools is not None else request_data.tools
584
 
585
- # Добавляем tools если есть
586
  if tools_to_use:
587
  tools_list = []
588
  for tool in tools_to_use:
589
  if tool.type == "function":
590
- # Очищаем parameters от полей, которые Kiro API не принимает
591
  sanitized_params = _sanitize_json_schema(tool.function.parameters)
592
 
593
- # Kiro API требует непустое description
594
- # Если description пустое или None, используем placeholder
595
  description = tool.function.description
596
  if not description or not description.strip():
597
  description = f"Tool: {tool.function.name}"
@@ -607,7 +670,7 @@ def _build_user_input_context(
607
  if tools_list:
608
  context["tools"] = tools_list
609
 
610
- # Обработка tool_results в текущем сообщении
611
  tool_results = _extract_tool_results(current_message.content)
612
  if tool_results:
613
  context["toolResults"] = tool_results
 
1
  # -*- coding: utf-8 -*-
2
 
3
  # Kiro OpenAI Gateway
4
+ # https://github.com/jwadow/kiro-openai-gateway
5
  # Copyright (C) 2025 Jwadow
6
  #
7
  # This program is free software: you can redistribute it and/or modify
 
18
  # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 
20
  """
21
+ Converters for transforming OpenAI <-> Kiro formats.
22
 
23
+ Contains functions for:
24
+ - Extracting text content from various formats
25
+ - Merging adjacent messages
26
+ - Building conversation history for Kiro API
27
+ - Assembling complete payload for requests
28
  """
29
 
30
  import json
 
32
 
33
  from loguru import logger
34
 
35
+ from kiro_gateway.config import (
36
+ get_internal_model_id,
37
+ TOOL_DESCRIPTION_MAX_LENGTH,
38
+ FAKE_REASONING_ENABLED,
39
+ FAKE_REASONING_MAX_TOKENS,
40
+ )
41
  from kiro_gateway.models import ChatMessage, ChatCompletionRequest, Tool
42
 
43
 
44
  def extract_text_content(content: Any) -> str:
45
  """
46
+ Extracts text content from various formats.
47
 
48
+ OpenAI API supports several content formats:
49
+ - String: "Hello, world!"
50
+ - List: [{"type": "text", "text": "Hello"}]
51
+ - None: empty message
52
 
53
  Args:
54
+ content: Content in any supported format
55
 
56
  Returns:
57
+ Extracted text or empty string
58
 
59
  Example:
60
  >>> extract_text_content("Hello")
 
82
  return str(content)
83
 
84
 
85
+ def inject_thinking_tags(content: str) -> str:
86
+ """
87
+ Inject fake reasoning tags into content.
88
+
89
+ When FAKE_REASONING_ENABLED is True, this function prepends the special
90
+ thinking mode tags to the content. These tags instruct the model to
91
+ include its reasoning process in the response.
92
+
93
+ The injected tags are:
94
+ - <thinking_mode>enabled</thinking_mode>
95
+ - <max_thinking_length>{FAKE_REASONING_MAX_TOKENS}</max_thinking_length>
96
+ - <thinking_instruction>...</thinking_instruction> (quality improvement prompt)
97
+
98
+ Args:
99
+ content: Original content string
100
+
101
+ Returns:
102
+ Content with thinking tags prepended (if enabled) or original content
103
+
104
+ Example:
105
+ >>> # With FAKE_REASONING_ENABLED=True, FAKE_REASONING_MAX_TOKENS=4000
106
+ >>> inject_thinking_tags("Hello")
107
+ '<thinking_mode>enabled</thinking_mode>\\n<max_thinking_length>4000</max_thinking_length>\\n<thinking_instruction>...\\n\\nHello'
108
+ """
109
+ if not FAKE_REASONING_ENABLED:
110
+ return content
111
+
112
+ # Thinking instruction to improve reasoning quality
113
+ # Uses English for better model performance (models are primarily trained on English)
114
+ # Includes key elements: understanding, alternatives, edge cases, verification
115
+ thinking_instruction = (
116
+ "Think in English for better reasoning quality.\n\n"
117
+ "Your thinking process should be thorough and systematic:\n"
118
+ "- First, make sure you fully understand what is being asked\n"
119
+ "- Consider multiple approaches or perspectives when relevant\n"
120
+ "- Think about edge cases, potential issues, and what could go wrong\n"
121
+ "- Challenge your initial assumptions\n"
122
+ "- Verify your reasoning before reaching a conclusion\n\n"
123
+ "Take the time you need. Quality of thought matters more than speed."
124
+ )
125
+
126
+ thinking_prefix = (
127
+ f"<thinking_mode>enabled</thinking_mode>\n"
128
+ f"<max_thinking_length>{FAKE_REASONING_MAX_TOKENS}</max_thinking_length>\n"
129
+ f"<thinking_instruction>{thinking_instruction}</thinking_instruction>\n\n"
130
+ )
131
+
132
+ logger.debug(f"Injecting fake reasoning tags with max_tokens={FAKE_REASONING_MAX_TOKENS}")
133
+
134
+ return thinking_prefix + content
135
+
136
+
137
  def merge_adjacent_messages(messages: List[ChatMessage]) -> List[ChatMessage]:
138
  """
139
+ Merges adjacent messages with the same role and processes tool messages.
140
 
141
+ Kiro API does not accept multiple consecutive messages from the same role.
142
+ This function merges such messages into one.
143
 
144
+ Tool messages (role="tool") are converted to user messages with tool_results.
145
 
146
  Args:
147
+ messages: List of messages
148
 
149
  Returns:
150
+ List of messages with merged adjacent messages
151
 
152
  Example:
153
  >>> msgs = [
 
163
  if not messages:
164
  return []
165
 
166
+ # First, convert tool messages to user messages with tool_results
167
  processed = []
168
  pending_tool_results = []
169
 
170
  for msg in messages:
171
  if msg.role == "tool":
172
+ # Collect tool results
173
  tool_result = {
174
  "type": "tool_result",
175
  "tool_use_id": msg.tool_call_id or "",
 
178
  pending_tool_results.append(tool_result)
179
  logger.debug(f"Collected tool result for tool_call_id={msg.tool_call_id}")
180
  else:
181
+ # If there are accumulated tool results, create user message with them
182
  if pending_tool_results:
183
+ # Create user message with tool_results
184
  tool_results_msg = ChatMessage(
185
  role="user",
186
  content=pending_tool_results.copy()
 
191
 
192
  processed.append(msg)
193
 
194
+ # If tool results remain at the end
195
  if pending_tool_results:
196
  tool_results_msg = ChatMessage(
197
  role="user",
 
200
  processed.append(tool_results_msg)
201
  logger.debug(f"Created final user message with {len(pending_tool_results)} tool results")
202
 
203
+ # Now merge adjacent messages with the same role
204
  merged = []
205
  for msg in processed:
206
  if not merged:
 
209
 
210
  last = merged[-1]
211
  if msg.role == last.role:
212
+ # Merge content
213
+ # If both contents are lists, merge lists
214
  if isinstance(last.content, list) and isinstance(msg.content, list):
215
  last.content = last.content + msg.content
216
  elif isinstance(last.content, list):
 
222
  current_text = extract_text_content(msg.content)
223
  last.content = f"{last_text}\n{current_text}"
224
 
225
+ # Merge tool_calls for assistant messages
226
+ # Critical: without this, tool_calls from second and subsequent messages are lost,
227
+ # leading to 400 error from Kiro API (toolResult without corresponding toolUse)
228
  if msg.role == "assistant" and msg.tool_calls:
229
  if last.tool_calls is None:
230
  last.tool_calls = []
 
240
 
241
  def build_kiro_history(messages: List[ChatMessage], model_id: str) -> List[Dict[str, Any]]:
242
  """
243
+ Builds history array for Kiro API from OpenAI messages.
244
 
245
+ Kiro API expects alternating userInputMessage and assistantResponseMessage.
246
+ This function converts OpenAI format to Kiro format.
247
 
248
  Args:
249
+ messages: List of messages in OpenAI format
250
+ model_id: Internal Kiro model ID
251
 
252
  Returns:
253
+ List of dictionaries for history field in Kiro API
254
 
255
  Example:
256
  >>> msgs = [ChatMessage(role="user", content="Hello")]
 
270
  "origin": "AI_EDITOR",
271
  }
272
 
273
+ # Process tool_results (responses to tool calls)
274
  tool_results = _extract_tool_results(msg.content)
275
  if tool_results:
276
  user_input["userInputMessageContext"] = {"toolResults": tool_results}
 
282
 
283
  assistant_response = {"content": content}
284
 
285
+ # Process tool_calls
286
  tool_uses = _extract_tool_uses(msg)
287
  if tool_uses:
288
  assistant_response["toolUses"] = tool_uses
 
290
  history.append({"assistantResponseMessage": assistant_response})
291
 
292
  elif msg.role == "system":
293
+ # System prompt is handled separately in build_kiro_payload
294
  pass
295
 
296
  return history
 
298
 
299
  def _extract_tool_results(content: Any) -> List[Dict[str, Any]]:
300
  """
301
+ Extracts tool results from message content.
302
 
303
  Args:
304
+ content: Message content (can be a list)
305
 
306
  Returns:
307
+ List of tool results in Kiro format
308
  """
309
  tool_results = []
310
 
 
324
  tools: Optional[List[Tool]]
325
  ) -> Tuple[Optional[List[Tool]], str]:
326
  """
327
+ Processes tools with long descriptions.
328
 
329
+ Kiro API has a limit on description length in toolSpecification.
330
+ If description exceeds the limit, full description is moved to system prompt,
331
+ and a reference to documentation remains in the tool.
332
 
333
  Args:
334
+ tools: List of tools from OpenAI request
335
 
336
  Returns:
337
+ Tuple of:
338
+ - List of tools with processed descriptions (or None if tools is empty)
339
+ - String with documentation to add to system prompt (empty if all descriptions are short)
340
 
341
  Example:
342
  >>> tools = [Tool(type="function", function=ToolFunction(name="bash", description="Very long..."))]
 
347
  if not tools:
348
  return None, ""
349
 
350
+ # If limit is disabled (0), return tools unchanged
351
  if TOOL_DESCRIPTION_MAX_LENGTH <= 0:
352
  return tools, ""
353
 
 
362
  description = tool.function.description or ""
363
 
364
  if len(description) <= TOOL_DESCRIPTION_MAX_LENGTH:
365
+ # Description is short - leave as is
366
  processed_tools.append(tool)
367
  else:
368
+ # Description is too long - move to system prompt
369
  tool_name = tool.function.name
370
 
371
  logger.debug(
 
373
  f"moving to system prompt"
374
  )
375
 
376
+ # Create documentation for system prompt
377
  tool_documentation_parts.append(f"## Tool: {tool_name}\n\n{description}")
378
 
379
+ # Create copy of tool with reference description
380
+ # Use Tool model to create new copy
381
  from kiro_gateway.models import ToolFunction
382
 
383
  reference_description = f"[Full documentation in system prompt under '## Tool: {tool_name}']"
 
392
  )
393
  processed_tools.append(processed_tool)
394
 
395
+ # Form final documentation
396
  tool_documentation = ""
397
  if tool_documentation_parts:
398
  tool_documentation = (
 
407
 
408
  def _extract_tool_uses(msg: ChatMessage) -> List[Dict[str, Any]]:
409
  """
410
+ Extracts tool uses from assistant message.
411
 
412
  Args:
413
+ msg: Assistant message
414
 
415
  Returns:
416
+ List of tool uses in Kiro format
417
  """
418
  tool_uses = []
419
 
420
+ # From tool_calls field
421
  if msg.tool_calls:
422
  for tc in msg.tool_calls:
423
  if isinstance(tc, dict):
 
427
  "toolUseId": tc.get("id", "")
428
  })
429
 
430
+ # From content (if it contains tool_use)
431
  if isinstance(msg.content, list):
432
  for item in msg.content:
433
  if isinstance(item, dict) and item.get("type") == "tool_use":
 
446
  profile_arn: str
447
  ) -> dict:
448
  """
449
+ Builds complete payload for Kiro API.
450
 
451
+ Includes:
452
+ - Full message history
453
+ - System prompt (added to first user message)
454
+ - Tools definitions (with long description handling)
455
+ - Current message
456
 
457
+ If tools contain descriptions that are too long, they are automatically
458
+ moved to system prompt, and a reference to documentation remains in the tool.
459
 
460
  Args:
461
+ request_data: Request in OpenAI format
462
+ conversation_id: Unique conversation ID
463
+ profile_arn: AWS CodeWhisperer profile ARN
464
 
465
  Returns:
466
+ Payload dictionary for POST request to Kiro API
467
 
468
  Raises:
469
+ ValueError: If there are no messages to send
470
  """
471
  messages = list(request_data.messages)
472
 
473
+ # Process tools with long descriptions
474
  processed_tools, tool_documentation = process_tools_with_long_descriptions(request_data.tools)
475
 
476
+ # Extract system prompt
477
  system_prompt = ""
478
  non_system_messages = []
479
  for msg in messages:
 
483
  non_system_messages.append(msg)
484
  system_prompt = system_prompt.strip()
485
 
486
+ # Add tool documentation to system prompt if present
487
  if tool_documentation:
488
  system_prompt = system_prompt + tool_documentation if system_prompt else tool_documentation.strip()
489
 
490
+ # Merge adjacent messages with the same role
491
  merged_messages = merge_adjacent_messages(non_system_messages)
492
 
493
  if not merged_messages:
494
  raise ValueError("No messages to send")
495
 
496
+ # Get internal model ID
497
  model_id = get_internal_model_id(request_data.model)
498
 
499
+ # Build history (all messages except the last one)
500
  history_messages = merged_messages[:-1] if len(merged_messages) > 1 else []
501
 
502
+ # If there's a system prompt, add it to the first user message in history
503
  if system_prompt and history_messages:
504
  first_msg = history_messages[0]
505
  if first_msg.role == "user":
 
508
 
509
  history = build_kiro_history(history_messages, model_id)
510
 
511
+ # Current message (the last one)
512
  current_message = merged_messages[-1]
513
  current_content = extract_text_content(current_message.content)
514
 
515
+ # If system prompt exists but history is empty - add to current message
516
  if system_prompt and not history:
517
  current_content = f"{system_prompt}\n\n{current_content}"
518
 
519
+ # If current message is assistant, need to add it to history
520
+ # and create user message "Continue"
521
  if current_message.role == "assistant":
522
  history.append({
523
  "assistantResponseMessage": {
 
526
  })
527
  current_content = "Continue"
528
 
529
+ # If content is empty - use "Continue"
530
  if not current_content:
531
  current_content = "Continue"
532
 
533
+ # Inject thinking tags if enabled (only for the current/last user message)
534
+ # Must be AFTER empty content check to avoid injecting tags into "Continue"
535
+ if current_message.role == "user":
536
+ current_content = inject_thinking_tags(current_content)
537
+
538
+ # Build userInputMessage
539
  user_input_message = {
540
  "content": current_content,
541
  "modelId": model_id,
542
  "origin": "AI_EDITOR",
543
  }
544
 
545
+ # Add tools and tool_results if present
546
+ # Use processed tools (with short descriptions)
547
  user_input_context = _build_user_input_context(request_data, current_message, processed_tools)
548
  if user_input_context:
549
  user_input_message["userInputMessageContext"] = user_input_context
550
 
551
+ # Assemble final payload
552
  payload = {
553
  "conversationState": {
554
  "chatTriggerType": "MANUAL",
 
559
  }
560
  }
561
 
562
+ # Add history only if not empty
563
  if history:
564
  payload["conversationState"]["history"] = history
565
 
566
+ # Add profileArn
567
  if profile_arn:
568
  payload["profileArn"] = profile_arn
569
 
 
572
 
573
  def _sanitize_json_schema(schema: Optional[Dict[str, Any]]) -> Dict[str, Any]:
574
  """
575
+ Sanitizes JSON Schema from fields that Kiro API doesn't accept.
576
 
577
+ Kiro API returns 400 "Improperly formed request" error if:
578
+ - required is an empty array []
579
+ - additionalProperties is present in schema
580
 
581
+ This function recursively processes the schema and removes problematic fields.
582
 
583
  Args:
584
+ schema: JSON Schema to sanitize
585
 
586
  Returns:
587
+ Sanitized copy of schema
588
  """
589
  if not schema:
590
  return {}
591
 
592
+ # Create copy to avoid mutating original
593
  result = {}
594
 
595
  for key, value in schema.items():
596
+ # Skip empty required arrays
597
  if key == "required" and isinstance(value, list) and len(value) == 0:
598
  continue
599
 
600
+ # Skip additionalProperties - Kiro API doesn't support it
601
  if key == "additionalProperties":
602
  continue
603
 
604
+ # Recursively process nested objects
605
  if key == "properties" and isinstance(value, dict):
606
  result[key] = {
607
  prop_name: _sanitize_json_schema(prop_value) if isinstance(prop_value, dict) else prop_value
 
610
  elif isinstance(value, dict):
611
  result[key] = _sanitize_json_schema(value)
612
  elif isinstance(value, list):
613
+ # Process lists (e.g., anyOf, oneOf)
614
  result[key] = [
615
  _sanitize_json_schema(item) if isinstance(item, dict) else item
616
  for item in value
 
627
  processed_tools: Optional[List[Tool]] = None
628
  ) -> Dict[str, Any]:
629
  """
630
+ Builds userInputMessageContext for current message.
631
 
632
+ Includes tools definitions and tool_results.
633
 
634
  Args:
635
+ request_data: Request with tools
636
+ current_message: Current message
637
+ processed_tools: Processed tools with short descriptions (optional).
638
+ If None, tools from request_data are used.
639
 
640
  Returns:
641
+ Dictionary with context or empty dictionary
642
  """
643
  context = {}
644
 
645
+ # Use processed tools if provided, otherwise original
646
  tools_to_use = processed_tools if processed_tools is not None else request_data.tools
647
 
648
+ # Add tools if present
649
  if tools_to_use:
650
  tools_list = []
651
  for tool in tools_to_use:
652
  if tool.type == "function":
653
+ # Sanitize parameters from fields that Kiro API doesn't accept
654
  sanitized_params = _sanitize_json_schema(tool.function.parameters)
655
 
656
+ # Kiro API requires non-empty description
657
+ # If description is empty or None, use placeholder
658
  description = tool.function.description
659
  if not description or not description.strip():
660
  description = f"Tool: {tool.function.name}"
 
670
  if tools_list:
671
  context["tools"] = tools_list
672
 
673
+ # Process tool_results in current message
674
  tool_results = _extract_tool_results(current_message.content)
675
  if tool_results:
676
  context["toolResults"] = tool_results
kiro_gateway/streaming.py CHANGED
@@ -37,8 +37,14 @@ from loguru import logger
37
 
38
  from kiro_gateway.parsers import AwsEventStreamParser, parse_bracket_tool_calls, deduplicate_tool_calls
39
  from kiro_gateway.utils import generate_completion_id
40
- from kiro_gateway.config import FIRST_TOKEN_TIMEOUT, FIRST_TOKEN_MAX_RETRIES
 
 
 
 
 
41
  from kiro_gateway.tokenizer import count_tokens, count_message_tokens, count_tools_tokens
 
42
 
43
  if TYPE_CHECKING:
44
  from kiro_gateway.auth import KiroAuthManager
@@ -107,9 +113,16 @@ async def stream_kiro_to_openai_internal(
107
  metering_data = None
108
  context_usage_percentage = None
109
  full_content = ""
 
110
 
111
  streaming_error_occurred = False
112
 
 
 
 
 
 
 
113
  try:
114
  # Create iterator for reading bytes
115
  byte_iterator = response.aiter_bytes()
@@ -144,25 +157,86 @@ async def stream_kiro_to_openai_internal(
144
  content = event["data"]
145
  full_content += content
146
 
147
- delta = {"content": content}
148
- if first_chunk:
149
- delta["role"] = "assistant"
150
- first_chunk = False
151
-
152
- openai_chunk = {
153
- "id": completion_id,
154
- "object": "chat.completion.chunk",
155
- "created": created_time,
156
- "model": model,
157
- "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
158
- }
159
-
160
- chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
161
-
162
- if debug_logger:
163
- debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
164
-
165
- yield chunk_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  elif event["type"] == "usage":
168
  metering_data = event["data"]
@@ -183,13 +257,114 @@ async def stream_kiro_to_openai_internal(
183
  content = event["data"]
184
  full_content += content
185
 
186
- # Form delta
187
- delta = {"content": content}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  if first_chunk:
189
  delta["role"] = "assistant"
190
  first_chunk = False
191
 
192
- # Form OpenAI chunk
193
  openai_chunk = {
194
  "id": completion_id,
195
  "object": "chat.completion.chunk",
@@ -200,17 +375,34 @@ async def stream_kiro_to_openai_internal(
200
 
201
  chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
202
 
203
- # Log modified chunk
204
  if debug_logger:
205
  debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
206
 
207
  yield chunk_text
 
 
 
 
 
 
208
 
209
- elif event["type"] == "usage":
210
- metering_data = event["data"]
 
 
 
 
 
211
 
212
- elif event["type"] == "context_usage":
213
- context_usage_percentage = event["data"]
 
 
 
 
 
 
 
214
 
215
  # Check bracket-style tool calls in full content
216
  bracket_tool_calls = parse_bracket_tool_calls(full_content)
@@ -541,6 +733,7 @@ async def collect_stream_response(
541
  Dictionary with full response in OpenAI chat.completion format
542
  """
543
  full_content = ""
 
544
  final_usage = None
545
  tool_calls = []
546
  completion_id = generate_completion_id()
@@ -568,6 +761,8 @@ async def collect_stream_response(
568
  delta = chunk_data.get("choices", [{}])[0].get("delta", {})
569
  if "content" in delta:
570
  full_content += delta["content"]
 
 
571
  if "tool_calls" in delta:
572
  tool_calls.extend(delta["tool_calls"])
573
 
@@ -580,6 +775,8 @@ async def collect_stream_response(
580
 
581
  # Form final response
582
  message = {"role": "assistant", "content": full_content}
 
 
583
  if tool_calls:
584
  # For non-streaming response remove index field from tool_calls,
585
  # as it's only required for streaming chunks
 
37
 
38
  from kiro_gateway.parsers import AwsEventStreamParser, parse_bracket_tool_calls, deduplicate_tool_calls
39
  from kiro_gateway.utils import generate_completion_id
40
+ from kiro_gateway.config import (
41
+ FIRST_TOKEN_TIMEOUT,
42
+ FIRST_TOKEN_MAX_RETRIES,
43
+ FAKE_REASONING_ENABLED,
44
+ FAKE_REASONING_HANDLING,
45
+ )
46
  from kiro_gateway.tokenizer import count_tokens, count_message_tokens, count_tools_tokens
47
+ from kiro_gateway.thinking_parser import ThinkingParser
48
 
49
  if TYPE_CHECKING:
50
  from kiro_gateway.auth import KiroAuthManager
 
113
  metering_data = None
114
  context_usage_percentage = None
115
  full_content = ""
116
+ full_thinking_content = "" # Accumulated thinking content for non-streaming
117
 
118
  streaming_error_occurred = False
119
 
120
+ # Initialize thinking parser if fake reasoning is enabled
121
+ thinking_parser: Optional[ThinkingParser] = None
122
+ if FAKE_REASONING_ENABLED:
123
+ thinking_parser = ThinkingParser(handling_mode=FAKE_REASONING_HANDLING)
124
+ logger.debug(f"Thinking parser initialized with mode: {FAKE_REASONING_HANDLING}")
125
+
126
  try:
127
  # Create iterator for reading bytes
128
  byte_iterator = response.aiter_bytes()
 
157
  content = event["data"]
158
  full_content += content
159
 
160
+ # Process through thinking parser if enabled
161
+ if thinking_parser:
162
+ parse_result = thinking_parser.feed(content)
163
+
164
+ # Yield thinking content if any
165
+ if parse_result.thinking_content:
166
+ full_thinking_content += parse_result.thinking_content
167
+ processed_thinking = thinking_parser.process_for_output(
168
+ parse_result.thinking_content,
169
+ parse_result.is_first_thinking_chunk,
170
+ parse_result.is_last_thinking_chunk,
171
+ )
172
+ if processed_thinking:
173
+ # Send as reasoning_content or content based on mode
174
+ if FAKE_REASONING_HANDLING == "as_reasoning_content":
175
+ delta = {"reasoning_content": processed_thinking}
176
+ else:
177
+ delta = {"content": processed_thinking}
178
+
179
+ if first_chunk:
180
+ delta["role"] = "assistant"
181
+ first_chunk = False
182
+
183
+ openai_chunk = {
184
+ "id": completion_id,
185
+ "object": "chat.completion.chunk",
186
+ "created": created_time,
187
+ "model": model,
188
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
189
+ }
190
+
191
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
192
+
193
+ if debug_logger:
194
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
195
+
196
+ yield chunk_text
197
+
198
+ # Yield regular content if any
199
+ if parse_result.regular_content:
200
+ delta = {"content": parse_result.regular_content}
201
+ if first_chunk:
202
+ delta["role"] = "assistant"
203
+ first_chunk = False
204
+
205
+ openai_chunk = {
206
+ "id": completion_id,
207
+ "object": "chat.completion.chunk",
208
+ "created": created_time,
209
+ "model": model,
210
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
211
+ }
212
+
213
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
214
+
215
+ if debug_logger:
216
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
217
+
218
+ yield chunk_text
219
+ else:
220
+ # No thinking parser - pass through as-is
221
+ delta = {"content": content}
222
+ if first_chunk:
223
+ delta["role"] = "assistant"
224
+ first_chunk = False
225
+
226
+ openai_chunk = {
227
+ "id": completion_id,
228
+ "object": "chat.completion.chunk",
229
+ "created": created_time,
230
+ "model": model,
231
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
232
+ }
233
+
234
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
235
+
236
+ if debug_logger:
237
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
238
+
239
+ yield chunk_text
240
 
241
  elif event["type"] == "usage":
242
  metering_data = event["data"]
 
257
  content = event["data"]
258
  full_content += content
259
 
260
+ # Process through thinking parser if enabled
261
+ if thinking_parser:
262
+ parse_result = thinking_parser.feed(content)
263
+
264
+ # Yield thinking content if any
265
+ if parse_result.thinking_content:
266
+ full_thinking_content += parse_result.thinking_content
267
+ processed_thinking = thinking_parser.process_for_output(
268
+ parse_result.thinking_content,
269
+ parse_result.is_first_thinking_chunk,
270
+ parse_result.is_last_thinking_chunk,
271
+ )
272
+ if processed_thinking:
273
+ # Send as reasoning_content or content based on mode
274
+ if FAKE_REASONING_HANDLING == "as_reasoning_content":
275
+ delta = {"reasoning_content": processed_thinking}
276
+ else:
277
+ delta = {"content": processed_thinking}
278
+
279
+ if first_chunk:
280
+ delta["role"] = "assistant"
281
+ first_chunk = False
282
+
283
+ openai_chunk = {
284
+ "id": completion_id,
285
+ "object": "chat.completion.chunk",
286
+ "created": created_time,
287
+ "model": model,
288
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
289
+ }
290
+
291
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
292
+
293
+ if debug_logger:
294
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
295
+
296
+ yield chunk_text
297
+
298
+ # Yield regular content if any
299
+ if parse_result.regular_content:
300
+ delta = {"content": parse_result.regular_content}
301
+ if first_chunk:
302
+ delta["role"] = "assistant"
303
+ first_chunk = False
304
+
305
+ openai_chunk = {
306
+ "id": completion_id,
307
+ "object": "chat.completion.chunk",
308
+ "created": created_time,
309
+ "model": model,
310
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
311
+ }
312
+
313
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
314
+
315
+ if debug_logger:
316
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
317
+
318
+ yield chunk_text
319
+ else:
320
+ # No thinking parser - pass through as-is
321
+ delta = {"content": content}
322
+ if first_chunk:
323
+ delta["role"] = "assistant"
324
+ first_chunk = False
325
+
326
+ openai_chunk = {
327
+ "id": completion_id,
328
+ "object": "chat.completion.chunk",
329
+ "created": created_time,
330
+ "model": model,
331
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
332
+ }
333
+
334
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
335
+
336
+ if debug_logger:
337
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
338
+
339
+ yield chunk_text
340
+
341
+ elif event["type"] == "usage":
342
+ metering_data = event["data"]
343
+
344
+ elif event["type"] == "context_usage":
345
+ context_usage_percentage = event["data"]
346
+
347
+ # Finalize thinking parser and yield any remaining content
348
+ if thinking_parser:
349
+ final_result = thinking_parser.finalize()
350
+
351
+ if final_result.thinking_content:
352
+ full_thinking_content += final_result.thinking_content
353
+ processed_thinking = thinking_parser.process_for_output(
354
+ final_result.thinking_content,
355
+ final_result.is_first_thinking_chunk,
356
+ final_result.is_last_thinking_chunk,
357
+ )
358
+ if processed_thinking:
359
+ if FAKE_REASONING_HANDLING == "as_reasoning_content":
360
+ delta = {"reasoning_content": processed_thinking}
361
+ else:
362
+ delta = {"content": processed_thinking}
363
+
364
  if first_chunk:
365
  delta["role"] = "assistant"
366
  first_chunk = False
367
 
 
368
  openai_chunk = {
369
  "id": completion_id,
370
  "object": "chat.completion.chunk",
 
375
 
376
  chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
377
 
 
378
  if debug_logger:
379
  debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
380
 
381
  yield chunk_text
382
+
383
+ if final_result.regular_content:
384
+ delta = {"content": final_result.regular_content}
385
+ if first_chunk:
386
+ delta["role"] = "assistant"
387
+ first_chunk = False
388
 
389
+ openai_chunk = {
390
+ "id": completion_id,
391
+ "object": "chat.completion.chunk",
392
+ "created": created_time,
393
+ "model": model,
394
+ "choices": [{"index": 0, "delta": delta, "finish_reason": None}]
395
+ }
396
 
397
+ chunk_text = f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"
398
+
399
+ if debug_logger:
400
+ debug_logger.log_modified_chunk(chunk_text.encode('utf-8'))
401
+
402
+ yield chunk_text
403
+
404
+ if thinking_parser.found_thinking_block:
405
+ logger.debug(f"Thinking block processed: {len(full_thinking_content)} chars")
406
 
407
  # Check bracket-style tool calls in full content
408
  bracket_tool_calls = parse_bracket_tool_calls(full_content)
 
733
  Dictionary with full response in OpenAI chat.completion format
734
  """
735
  full_content = ""
736
+ full_reasoning_content = ""
737
  final_usage = None
738
  tool_calls = []
739
  completion_id = generate_completion_id()
 
761
  delta = chunk_data.get("choices", [{}])[0].get("delta", {})
762
  if "content" in delta:
763
  full_content += delta["content"]
764
+ if "reasoning_content" in delta:
765
+ full_reasoning_content += delta["reasoning_content"]
766
  if "tool_calls" in delta:
767
  tool_calls.extend(delta["tool_calls"])
768
 
 
775
 
776
  # Form final response
777
  message = {"role": "assistant", "content": full_content}
778
+ if full_reasoning_content:
779
+ message["reasoning_content"] = full_reasoning_content
780
  if tool_calls:
781
  # For non-streaming response remove index field from tool_calls,
782
  # as it's only required for streaming chunks
kiro_gateway/thinking_parser.py ADDED
@@ -0,0 +1,385 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Kiro OpenAI Gateway
4
+ # https://github.com/jwadow/kiro-openai-gateway
5
+ # Copyright (C) 2025 Jwadow
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Affero General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Affero General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Affero General Public License
18
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+
20
+ """
21
+ Thinking block parser for streaming responses.
22
+
23
+ Implements a finite state machine (FSM) for reliable parsing of thinking blocks
24
+ (<thinking>, <think>, <reasoning>, etc.) that may be split across multiple
25
+ network chunks.
26
+
27
+ Key features:
28
+ - Tag detection ONLY at the start of response
29
+ - "Cautious" sending - buffers potential tag fragments to avoid splitting tags
30
+ - After closing tag - all content is treated as regular content
31
+ - Support for multiple tag formats
32
+ """
33
+
34
+ from enum import IntEnum
35
+ from typing import Optional, List
36
+ from dataclasses import dataclass, field
37
+
38
+ from loguru import logger
39
+
40
+ from kiro_gateway.config import (
41
+ FAKE_REASONING_HANDLING,
42
+ FAKE_REASONING_OPEN_TAGS,
43
+ FAKE_REASONING_INITIAL_BUFFER_SIZE,
44
+ )
45
+
46
+
47
+ class ParserState(IntEnum):
48
+ """
49
+ States of the thinking block parser FSM.
50
+
51
+ PRE_CONTENT: Initial state, buffering to detect opening tag
52
+ IN_THINKING: Inside thinking block, buffering until closing tag
53
+ STREAMING: Regular streaming, no more thinking block detection
54
+ """
55
+ PRE_CONTENT = 0
56
+ IN_THINKING = 1
57
+ STREAMING = 2
58
+
59
+
60
+ @dataclass
61
+ class ThinkingParseResult:
62
+ """
63
+ Result of processing a content chunk through the parser.
64
+
65
+ Attributes:
66
+ thinking_content: Content to be sent as reasoning_content (or processed per mode)
67
+ regular_content: Regular content to be sent as delta.content
68
+ is_first_thinking_chunk: True if this is the first chunk of thinking content
69
+ is_last_thinking_chunk: True if thinking block just closed
70
+ state_changed: True if parser state changed during this feed
71
+ """
72
+ thinking_content: Optional[str] = None
73
+ regular_content: Optional[str] = None
74
+ is_first_thinking_chunk: bool = False
75
+ is_last_thinking_chunk: bool = False
76
+ state_changed: bool = False
77
+
78
+
79
+ class ThinkingParser:
80
+ """
81
+ Finite state machine parser for thinking blocks in streaming responses.
82
+
83
+ The parser detects thinking tags ONLY at the start of the response.
84
+ Once a thinking block is found and closed, all subsequent content
85
+ is treated as regular content (even if it contains thinking tags).
86
+
87
+ This implements "cautious" buffering to handle tags split across chunks:
88
+ - In PRE_CONTENT: buffer until tag found or buffer exceeds limit
89
+ - In IN_THINKING: buffer last MAX_TAG_LENGTH chars to avoid splitting closing tag
90
+
91
+ Example:
92
+ >>> parser = ThinkingParser()
93
+ >>> result = parser.feed("<think")
94
+ >>> result.thinking_content # None - still buffering
95
+ >>> result = parser.feed("ing>Hello")
96
+ >>> result.thinking_content # "Hello" (or None if buffering)
97
+ >>> result = parser.feed("</thinking>World")
98
+ >>> result.thinking_content # remaining thinking content
99
+ >>> result.regular_content # "World"
100
+ """
101
+
102
+ def __init__(
103
+ self,
104
+ handling_mode: Optional[str] = None,
105
+ open_tags: Optional[List[str]] = None,
106
+ initial_buffer_size: int = FAKE_REASONING_INITIAL_BUFFER_SIZE,
107
+ ):
108
+ """
109
+ Initialize the thinking parser.
110
+
111
+ Args:
112
+ handling_mode: How to handle thinking blocks. One of:
113
+ - "as_reasoning_content": Extract to reasoning_content field
114
+ - "remove": Remove thinking block completely
115
+ - "pass": Pass through with original tags
116
+ - "strip_tags": Remove tags but keep content
117
+ If None, uses FAKE_REASONING_HANDLING from config.
118
+ open_tags: List of opening tags to detect. If None, uses config.
119
+ initial_buffer_size: Max chars to buffer while looking for opening tag.
120
+ """
121
+ self.handling_mode = handling_mode or FAKE_REASONING_HANDLING
122
+ self.open_tags = open_tags or FAKE_REASONING_OPEN_TAGS
123
+ self.initial_buffer_size = initial_buffer_size
124
+
125
+ # Calculate max tag length for cautious buffering
126
+ # We need to buffer enough to not split a closing tag
127
+ self.max_tag_length = max(len(tag) for tag in self.open_tags) * 2
128
+
129
+ # State
130
+ self.state = ParserState.PRE_CONTENT
131
+ self.initial_buffer = ""
132
+ self.thinking_buffer = ""
133
+ self.open_tag: Optional[str] = None
134
+ self.close_tag: Optional[str] = None
135
+ self.is_first_thinking_chunk = True
136
+ self._thinking_block_found = False
137
+
138
+ def feed(self, content: str) -> ThinkingParseResult:
139
+ """
140
+ Process a chunk of content through the parser.
141
+
142
+ Args:
143
+ content: New content from delta.content
144
+
145
+ Returns:
146
+ ThinkingParseResult with processed content
147
+ """
148
+ result = ThinkingParseResult()
149
+
150
+ if not content:
151
+ return result
152
+
153
+ # Handle based on current state
154
+ if self.state == ParserState.PRE_CONTENT:
155
+ result = self._handle_pre_content(content)
156
+
157
+ # If state changed to IN_THINKING, process remaining content
158
+ if self.state == ParserState.IN_THINKING and result.state_changed:
159
+ # Content after tag is already in thinking_buffer from _handle_pre_content
160
+ pass
161
+ elif self.state == ParserState.IN_THINKING and not result.state_changed:
162
+ result = self._handle_in_thinking(content)
163
+
164
+ # If state changed to STREAMING, regular_content is already set
165
+ if self.state == ParserState.STREAMING and not result.state_changed:
166
+ result.regular_content = content
167
+
168
+ return result
169
+
170
+ def _handle_pre_content(self, content: str) -> ThinkingParseResult:
171
+ """
172
+ Handle content in PRE_CONTENT state.
173
+
174
+ Buffers content and looks for opening tag at the start.
175
+ """
176
+ result = ThinkingParseResult()
177
+ self.initial_buffer += content
178
+
179
+ # Strip leading whitespace for tag detection
180
+ stripped = self.initial_buffer.lstrip()
181
+
182
+ # Check if buffer starts with any of the opening tags
183
+ for tag in self.open_tags:
184
+ if stripped.startswith(tag):
185
+ # Tag found! Transition to IN_THINKING
186
+ self.state = ParserState.IN_THINKING
187
+ self.open_tag = tag
188
+ self.close_tag = f"</{tag[1:]}" # <thinking> -> </thinking>
189
+ self._thinking_block_found = True
190
+ result.state_changed = True
191
+
192
+ logger.debug(f"Thinking tag '{tag}' detected. Transitioning to IN_THINKING.")
193
+
194
+ # Content after the tag goes to thinking buffer
195
+ content_after_tag = stripped[len(tag):]
196
+ self.thinking_buffer = content_after_tag
197
+ self.initial_buffer = ""
198
+
199
+ # Now process the thinking buffer for potential closing tag
200
+ thinking_result = self._process_thinking_buffer()
201
+ if thinking_result.thinking_content:
202
+ result.thinking_content = thinking_result.thinking_content
203
+ result.is_first_thinking_chunk = thinking_result.is_first_thinking_chunk
204
+ if thinking_result.is_last_thinking_chunk:
205
+ result.is_last_thinking_chunk = True
206
+ if thinking_result.regular_content:
207
+ result.regular_content = thinking_result.regular_content
208
+
209
+ return result
210
+
211
+ # Check if we might still be receiving the tag
212
+ # (buffer is shorter than longest tag and could be a prefix)
213
+ for tag in self.open_tags:
214
+ if tag.startswith(stripped) and len(stripped) < len(tag):
215
+ # Could still be receiving the tag, keep buffering
216
+ return result
217
+
218
+ # No tag found and buffer is either:
219
+ # 1. Too long (exceeds initial_buffer_size)
220
+ # 2. Doesn't match any tag prefix
221
+ if len(self.initial_buffer) > self.initial_buffer_size or not self._could_be_tag_prefix(stripped):
222
+ # No thinking block, transition to STREAMING
223
+ self.state = ParserState.STREAMING
224
+ result.state_changed = True
225
+ result.regular_content = self.initial_buffer
226
+ self.initial_buffer = ""
227
+
228
+ logger.debug("No thinking tag detected. Transitioning to STREAMING.")
229
+
230
+ return result
231
+
232
+ def _could_be_tag_prefix(self, text: str) -> bool:
233
+ """Check if text could be the start of any opening tag."""
234
+ if not text:
235
+ return True # Empty could be anything
236
+
237
+ for tag in self.open_tags:
238
+ if tag.startswith(text):
239
+ return True
240
+ return False
241
+
242
+ def _handle_in_thinking(self, content: str) -> ThinkingParseResult:
243
+ """
244
+ Handle content in IN_THINKING state.
245
+
246
+ Buffers content and looks for closing tag.
247
+ Uses "cautious" sending to avoid splitting the closing tag.
248
+ """
249
+ self.thinking_buffer += content
250
+ return self._process_thinking_buffer()
251
+
252
+ def _process_thinking_buffer(self) -> ThinkingParseResult:
253
+ """
254
+ Process the thinking buffer, looking for closing tag.
255
+
256
+ Implements "cautious" sending - keeps last max_tag_length chars
257
+ in buffer to avoid splitting the closing tag across chunks.
258
+ """
259
+ result = ThinkingParseResult()
260
+
261
+ if not self.close_tag:
262
+ return result
263
+
264
+ # Check for closing tag
265
+ if self.close_tag in self.thinking_buffer:
266
+ # Found closing tag!
267
+ idx = self.thinking_buffer.find(self.close_tag)
268
+ thinking_content = self.thinking_buffer[:idx]
269
+ after_tag = self.thinking_buffer[idx + len(self.close_tag):]
270
+
271
+ # Send all thinking content
272
+ if thinking_content:
273
+ result.thinking_content = thinking_content
274
+ result.is_first_thinking_chunk = self.is_first_thinking_chunk
275
+ self.is_first_thinking_chunk = False
276
+
277
+ result.is_last_thinking_chunk = True
278
+
279
+ # Transition to STREAMING
280
+ self.state = ParserState.STREAMING
281
+ result.state_changed = True
282
+ self.thinking_buffer = ""
283
+
284
+ logger.debug(f"Closing tag '{self.close_tag}' found. Transitioning to STREAMING.")
285
+
286
+ # Content after closing tag is regular content
287
+ # Strip leading whitespace/newlines that often follow the closing tag
288
+ if after_tag:
289
+ stripped_after = after_tag.lstrip()
290
+ if stripped_after:
291
+ result.regular_content = stripped_after
292
+
293
+ return result
294
+
295
+ # No closing tag yet - use "cautious" sending
296
+ # Keep last max_tag_length chars in buffer to avoid splitting tag
297
+ if len(self.thinking_buffer) > self.max_tag_length:
298
+ send_part = self.thinking_buffer[:-self.max_tag_length]
299
+ self.thinking_buffer = self.thinking_buffer[-self.max_tag_length:]
300
+
301
+ result.thinking_content = send_part
302
+ result.is_first_thinking_chunk = self.is_first_thinking_chunk
303
+ self.is_first_thinking_chunk = False
304
+
305
+ return result
306
+
307
+ def finalize(self) -> ThinkingParseResult:
308
+ """
309
+ Finalize parsing when stream ends.
310
+
311
+ Flushes any remaining buffered content.
312
+
313
+ Returns:
314
+ ThinkingParseResult with any remaining content
315
+ """
316
+ result = ThinkingParseResult()
317
+
318
+ # Flush thinking buffer if we're still in thinking state
319
+ if self.thinking_buffer:
320
+ if self.state == ParserState.IN_THINKING:
321
+ result.thinking_content = self.thinking_buffer
322
+ result.is_first_thinking_chunk = self.is_first_thinking_chunk
323
+ result.is_last_thinking_chunk = True
324
+ logger.warning("Stream ended while still in thinking block. Flushing remaining content.")
325
+ else:
326
+ result.regular_content = self.thinking_buffer
327
+ self.thinking_buffer = ""
328
+
329
+ # Flush initial buffer if we never found a tag
330
+ if self.initial_buffer:
331
+ result.regular_content = (result.regular_content or "") + self.initial_buffer
332
+ self.initial_buffer = ""
333
+
334
+ return result
335
+
336
+ def reset(self) -> None:
337
+ """Reset parser to initial state."""
338
+ self.state = ParserState.PRE_CONTENT
339
+ self.initial_buffer = ""
340
+ self.thinking_buffer = ""
341
+ self.open_tag = None
342
+ self.close_tag = None
343
+ self.is_first_thinking_chunk = True
344
+ self._thinking_block_found = False
345
+
346
+ @property
347
+ def found_thinking_block(self) -> bool:
348
+ """Returns True if a thinking block was detected in this response."""
349
+ return self._thinking_block_found
350
+
351
+ def process_for_output(
352
+ self,
353
+ thinking_content: Optional[str],
354
+ is_first: bool,
355
+ is_last: bool,
356
+ ) -> Optional[str]:
357
+ """
358
+ Process thinking content according to handling mode.
359
+
360
+ Args:
361
+ thinking_content: Raw thinking content
362
+ is_first: True if this is the first thinking chunk
363
+ is_last: True if this is the last thinking chunk
364
+
365
+ Returns:
366
+ Processed content string or None (for "remove" mode)
367
+ """
368
+ if not thinking_content:
369
+ return None
370
+
371
+ if self.handling_mode == "remove":
372
+ return None
373
+
374
+ if self.handling_mode == "pass":
375
+ # Add tags back
376
+ prefix = self.open_tag if is_first and self.open_tag else ""
377
+ suffix = self.close_tag if is_last and self.close_tag else ""
378
+ return f"{prefix}{thinking_content}{suffix}"
379
+
380
+ if self.handling_mode == "strip_tags":
381
+ # Return content without tags
382
+ return thinking_content
383
+
384
+ # "as_reasoning_content" - return as-is, caller will put in reasoning_content field
385
+ return thinking_content
tests/README.md CHANGED
@@ -79,6 +79,7 @@ tests/
79
  │ ├── test_debug_logger.py # DebugLogger tests (off/errors/all modes)
80
  │ ├── test_parsers.py # AwsEventStreamParser tests
81
  │ ├── test_streaming.py # Streaming function tests
 
82
  │ ├── test_tokenizer.py # Tokenizer tests (tiktoken)
83
  │ ├── test_http_client.py # KiroHttpClient tests
84
  │ └── test_routes.py # API endpoint tests
@@ -732,7 +733,7 @@ Tests for application log capture (app_logs.txt).
732
 
733
  ### `tests/unit/test_converters.py`
734
 
735
- Unit tests for **OpenAI <-> Kiro** converters. **68 tests.**
736
 
737
  #### `TestExtractTextContent`
738
 
@@ -945,6 +946,74 @@ Tests for `_sanitize_json_schema` function that cleans JSON Schema from fields n
945
  - **What it does**: Verifies handling of mixed tools list
946
  - **Purpose**: Ensure empty descriptions are replaced while normal ones are preserved (real scenario from Cline)
947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
948
  #### `TestBuildKiroPayload`
949
 
950
  - **`test_builds_simple_payload()`**: Verifies simple payload building
@@ -1081,6 +1150,121 @@ Unit tests for **AwsEventStreamParser** and helper parsing functions. **52 tests
1081
 
1082
  ---
1083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1084
  ### `tests/unit/test_tokenizer.py`
1085
 
1086
  Unit tests for **tokenizer module** (token counting with tiktoken). **32 tests.**
 
79
  │ ├── test_debug_logger.py # DebugLogger tests (off/errors/all modes)
80
  │ ├── test_parsers.py # AwsEventStreamParser tests
81
  │ ├── test_streaming.py # Streaming function tests
82
+ │ ├── test_thinking_parser.py # ThinkingParser tests (FSM for thinking blocks)
83
  │ ├── test_tokenizer.py # Tokenizer tests (tiktoken)
84
  │ ├── test_http_client.py # KiroHttpClient tests
85
  │ └── test_routes.py # API endpoint tests
 
733
 
734
  ### `tests/unit/test_converters.py`
735
 
736
+ Unit tests for **OpenAI <-> Kiro** converters. **84 tests.**
737
 
738
  #### `TestExtractTextContent`
739
 
 
946
  - **What it does**: Verifies handling of mixed tools list
947
  - **Purpose**: Ensure empty descriptions are replaced while normal ones are preserved (real scenario from Cline)
948
 
949
+ #### `TestInjectThinkingTags`
950
+
951
+ Tests for inject_thinking_tags function with thinking instruction.
952
+
953
+ - **`test_returns_original_content_when_disabled()`**:
954
+ - **What it does**: Verifies that content is returned unchanged when fake reasoning is disabled
955
+ - **Purpose**: Ensure no modification occurs when FAKE_REASONING_ENABLED=False
956
+
957
+ - **`test_injects_tags_when_enabled()`**:
958
+ - **What it does**: Verifies that thinking tags are injected when enabled
959
+ - **Purpose**: Ensure tags are prepended to content when FAKE_REASONING_ENABLED=True
960
+
961
+ - **`test_injects_thinking_instruction_tag()`**:
962
+ - **What it does**: Verifies that thinking_instruction tag is injected
963
+ - **Purpose**: Ensure the quality improvement prompt is included
964
+
965
+ - **`test_thinking_instruction_contains_english_directive()`**:
966
+ - **What it does**: Verifies that thinking instruction includes English language directive
967
+ - **Purpose**: Ensure model is instructed to think in English for better reasoning quality
968
+
969
+ - **`test_thinking_instruction_contains_systematic_approach()`**:
970
+ - **What it does**: Verifies that thinking instruction includes systematic approach guidance
971
+ - **Purpose**: Ensure model is instructed to think systematically
972
+
973
+ - **`test_thinking_instruction_contains_understanding_step()`**:
974
+ - **What it does**: Verifies that thinking instruction includes understanding step
975
+ - **Purpose**: Ensure model is instructed to understand the problem first
976
+
977
+ - **`test_thinking_instruction_contains_alternatives_consideration()`**:
978
+ - **What it does**: Verifies that thinking instruction includes alternatives consideration
979
+ - **Purpose**: Ensure model is instructed to consider multiple approaches
980
+
981
+ - **`test_thinking_instruction_contains_edge_cases()`**:
982
+ - **What it does**: Verifies that thinking instruction includes edge cases consideration
983
+ - **Purpose**: Ensure model is instructed to think about edge cases and potential issues
984
+
985
+ - **`test_thinking_instruction_contains_verification_step()`**:
986
+ - **What it does**: Verifies that thinking instruction includes verification step
987
+ - **Purpose**: Ensure model is instructed to verify reasoning before concluding
988
+
989
+ - **`test_thinking_instruction_contains_assumptions_challenge()`**:
990
+ - **What it does**: Verifies that thinking instruction includes assumptions challenge
991
+ - **Purpose**: Ensure model is instructed to challenge initial assumptions
992
+
993
+ - **`test_thinking_instruction_contains_quality_over_speed()`**:
994
+ - **What it does**: Verifies that thinking instruction emphasizes quality over speed
995
+ - **Purpose**: Ensure model is instructed to prioritize quality of thought
996
+
997
+ - **`test_uses_configured_max_tokens()`**:
998
+ - **What it does**: Verifies that FAKE_REASONING_MAX_TOKENS config value is used
999
+ - **Purpose**: Ensure the configured max tokens value is injected into the tag
1000
+
1001
+ - **`test_preserves_empty_content()`**:
1002
+ - **What it does**: Verifies that empty content is handled correctly
1003
+ - **Purpose**: Ensure empty string doesn't cause issues
1004
+
1005
+ - **`test_preserves_multiline_content()`**:
1006
+ - **What it does**: Verifies that multiline content is preserved correctly
1007
+ - **Purpose**: Ensure newlines in original content are not corrupted
1008
+
1009
+ - **`test_preserves_special_characters()`**:
1010
+ - **What it does**: Verifies that special characters in content are preserved
1011
+ - **Purpose**: Ensure XML-like content in user message doesn't break injection
1012
+
1013
+ - **`test_tag_order_is_correct()`**:
1014
+ - **What it does**: Verifies that tags are in the correct order
1015
+ - **Purpose**: Ensure thinking_mode comes first, then max_thinking_length, then instruction, then content
1016
+
1017
  #### `TestBuildKiroPayload`
1018
 
1019
  - **`test_builds_simple_payload()`**: Verifies simple payload building
 
1150
 
1151
  ---
1152
 
1153
+ ### `tests/unit/test_thinking_parser.py`
1154
+
1155
+ Unit tests for **ThinkingParser** (FSM-based parser for thinking blocks in streaming responses). **63 tests.**
1156
+
1157
+ #### `TestParserStateEnum`
1158
+
1159
+ - **`test_pre_content_value()`**: Verifies PRE_CONTENT enum value is 0
1160
+ - **`test_in_thinking_value()`**: Verifies IN_THINKING enum value is 1
1161
+ - **`test_streaming_value()`**: Verifies STREAMING enum value is 2
1162
+
1163
+ #### `TestThinkingParseResult`
1164
+
1165
+ - **`test_default_values()`**: Verifies default values of ThinkingParseResult dataclass
1166
+ - **`test_custom_values()`**: Verifies custom values can be set in ThinkingParseResult
1167
+
1168
+ #### `TestThinkingParserInitialization`
1169
+
1170
+ - **`test_default_initialization()`**: Verifies parser starts in PRE_CONTENT state with empty buffers
1171
+ - **`test_custom_handling_mode()`**: Verifies handling_mode can be overridden
1172
+ - **`test_custom_open_tags()`**: Verifies open_tags can be overridden
1173
+ - **`test_custom_initial_buffer_size()`**: Verifies initial_buffer_size can be overridden
1174
+ - **`test_max_tag_length_calculated()`**: Verifies max_tag_length is calculated from open_tags
1175
+
1176
+ #### `TestThinkingParserFeedPreContent`
1177
+
1178
+ - **`test_empty_content_returns_empty_result()`**: Verifies empty content doesn't change state
1179
+ - **`test_detects_thinking_tag()`**: Verifies `<thinking>` tag detection and state transition
1180
+ - **`test_detects_think_tag()`**: Verifies `<think>` tag detection
1181
+ - **`test_detects_reasoning_tag()`**: Verifies `<reasoning>` tag detection
1182
+ - **`test_detects_thought_tag()`**: Verifies `<thought>` tag detection
1183
+ - **`test_strips_leading_whitespace_for_tag_detection()`**: Verifies leading whitespace is stripped
1184
+ - **`test_buffers_partial_tag()`**: Verifies partial tag is buffered
1185
+ - **`test_completes_partial_tag()`**: Verifies partial tag is completed across chunks
1186
+ - **`test_no_tag_transitions_to_streaming()`**: Verifies transition to STREAMING when no tag found
1187
+ - **`test_buffer_exceeds_limit_transitions_to_streaming()`**: Verifies transition when buffer exceeds limit
1188
+
1189
+ #### `TestThinkingParserFeedInThinking`
1190
+
1191
+ - **`test_accumulates_thinking_content()`**: Verifies thinking content is accumulated
1192
+ - **`test_detects_closing_tag()`**: Verifies closing tag detection and state transition
1193
+ - **`test_regular_content_after_closing_tag()`**: Verifies content after closing tag is regular_content
1194
+ - **`test_strips_whitespace_after_closing_tag()`**: Verifies whitespace is stripped after closing tag
1195
+ - **`test_cautious_buffering()`**: Verifies cautious buffering keeps last max_tag_length chars
1196
+ - **`test_split_closing_tag()`**: Verifies split closing tag is handled
1197
+
1198
+ #### `TestThinkingParserFeedStreaming`
1199
+
1200
+ - **`test_passes_content_through()`**: Verifies content is passed through in STREAMING state
1201
+ - **`test_ignores_thinking_tags_in_streaming()`**: Verifies thinking tags are ignored in STREAMING state
1202
+
1203
+ #### `TestThinkingParserFinalize`
1204
+
1205
+ - **`test_flushes_thinking_buffer()`**: Verifies thinking buffer is flushed on finalize
1206
+ - **`test_flushes_initial_buffer()`**: Verifies initial buffer is flushed on finalize
1207
+ - **`test_clears_buffers_after_finalize()`**: Verifies buffers are cleared after finalize
1208
+
1209
+ #### `TestThinkingParserReset`
1210
+
1211
+ - **`test_resets_to_initial_state()`**: Verifies reset returns parser to initial state
1212
+
1213
+ #### `TestThinkingParserFoundThinkingBlock`
1214
+
1215
+ - **`test_false_initially()`**: Verifies found_thinking_block is False initially
1216
+ - **`test_true_after_tag_detection()`**: Verifies found_thinking_block is True after tag detection
1217
+ - **`test_false_when_no_tag()`**: Verifies found_thinking_block is False when no tag found
1218
+
1219
+ #### `TestThinkingParserProcessForOutput`
1220
+
1221
+ - **`test_as_reasoning_content_mode()`**: Verifies as_reasoning_content mode returns content as-is
1222
+ - **`test_remove_mode()`**: Verifies remove mode returns None
1223
+ - **`test_pass_mode_first_chunk()`**: Verifies pass mode adds opening tag to first chunk
1224
+ - **`test_pass_mode_last_chunk()`**: Verifies pass mode adds closing tag to last chunk
1225
+ - **`test_pass_mode_first_and_last_chunk()`**: Verifies pass mode adds both tags when first and last
1226
+ - **`test_pass_mode_middle_chunk()`**: Verifies pass mode returns content as-is for middle chunk
1227
+ - **`test_strip_tags_mode()`**: Verifies strip_tags mode returns content without tags
1228
+ - **`test_none_content_returns_none()`**: Verifies None content returns None
1229
+ - **`test_empty_content_returns_none()`**: Verifies empty content returns None
1230
+
1231
+ #### `TestThinkingParserFullFlow`
1232
+
1233
+ Integration tests for full parsing flow.
1234
+
1235
+ - **`test_complete_thinking_block()`**: Verifies complete thinking block parsing
1236
+ - **`test_multi_chunk_thinking_block()`**: Verifies thinking block split across multiple chunks
1237
+ - **`test_no_thinking_block()`**: Verifies handling of content without thinking block
1238
+ - **`test_thinking_block_with_newlines()`**: Verifies thinking block with newlines after closing tag
1239
+ - **`test_empty_thinking_block()`**: Verifies empty thinking block handling
1240
+ - **`test_thinking_block_only_whitespace_after()`**: Verifies thinking block with only whitespace after
1241
+
1242
+ #### `TestThinkingParserEdgeCases`
1243
+
1244
+ - **`test_nested_tags_not_supported()`**: Verifies nested tags are not specially handled
1245
+ - **`test_tag_in_middle_of_content()`**: Verifies tag in middle of content is not detected
1246
+ - **`test_malformed_closing_tag()`**: Verifies malformed closing tag is not detected
1247
+ - **`test_unicode_content()`**: Verifies Unicode content is handled correctly
1248
+ - **`test_very_long_thinking_content()`**: Verifies very long thinking content is handled
1249
+ - **`test_special_characters_in_content()`**: Verifies special characters are handled
1250
+ - **`test_multiple_feeds_after_streaming()`**: Verifies multiple feeds in STREAMING state
1251
+
1252
+ #### `TestThinkingParserConfigIntegration`
1253
+
1254
+ - **`test_uses_config_handling_mode()`**: Verifies parser uses FAKE_REASONING_HANDLING from config
1255
+ - **`test_uses_config_open_tags()`**: Verifies parser uses FAKE_REASONING_OPEN_TAGS from config
1256
+ - **`test_default_initial_buffer_size_from_config()`**: Verifies parser uses default initial_buffer_size from config
1257
+
1258
+ #### `TestInjectThinkingTags`
1259
+
1260
+ Tests for inject_thinking_tags function in converters.
1261
+
1262
+ - **`test_injects_tags_when_enabled()`**: Verifies tags are injected when FAKE_REASONING_ENABLED is True
1263
+ - **`test_no_injection_when_disabled()`**: Verifies tags are not injected when FAKE_REASONING_ENABLED is False
1264
+ - **`test_injection_preserves_content()`**: Verifies original content is preserved after injection
1265
+
1266
+ ---
1267
+
1268
  ### `tests/unit/test_tokenizer.py`
1269
 
1270
  Unit tests for **tokenizer module** (token counting with tiktoken). **32 tests.**
tests/unit/test_converters.py CHANGED
The diff for this file is too large to render. See raw diff
 
tests/unit/test_thinking_parser.py ADDED
@@ -0,0 +1,992 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ Unit tests for ThinkingParser - FSM-based parser for thinking blocks in streaming responses.
5
+
6
+ Tests cover:
7
+ - Parser state transitions (PRE_CONTENT -> IN_THINKING -> STREAMING)
8
+ - Tag detection at response start
9
+ - "Cautious" buffering for split tags
10
+ - Different handling modes (as_reasoning_content, remove, pass, strip_tags)
11
+ - Edge cases and error handling
12
+ """
13
+
14
+ import pytest
15
+ from unittest.mock import patch
16
+
17
+ from kiro_gateway.thinking_parser import (
18
+ ThinkingParser,
19
+ ThinkingParseResult,
20
+ ParserState,
21
+ )
22
+
23
+
24
+ class TestParserStateEnum:
25
+ """Tests for ParserState enum."""
26
+
27
+ def test_pre_content_value(self):
28
+ """
29
+ What it does: Verifies PRE_CONTENT enum value.
30
+ Purpose: Ensure PRE_CONTENT is 0 (initial state).
31
+ """
32
+ print("Checking PRE_CONTENT enum value...")
33
+ assert ParserState.PRE_CONTENT == 0
34
+
35
+ def test_in_thinking_value(self):
36
+ """
37
+ What it does: Verifies IN_THINKING enum value.
38
+ Purpose: Ensure IN_THINKING is 1.
39
+ """
40
+ print("Checking IN_THINKING enum value...")
41
+ assert ParserState.IN_THINKING == 1
42
+
43
+ def test_streaming_value(self):
44
+ """
45
+ What it does: Verifies STREAMING enum value.
46
+ Purpose: Ensure STREAMING is 2.
47
+ """
48
+ print("Checking STREAMING enum value...")
49
+ assert ParserState.STREAMING == 2
50
+
51
+
52
+ class TestThinkingParseResult:
53
+ """Tests for ThinkingParseResult dataclass."""
54
+
55
+ def test_default_values(self):
56
+ """
57
+ What it does: Verifies default values of ThinkingParseResult.
58
+ Purpose: Ensure all fields have correct defaults.
59
+ """
60
+ print("Creating ThinkingParseResult with defaults...")
61
+ result = ThinkingParseResult()
62
+
63
+ print(f"Comparing: Expected None, Got {result.thinking_content}")
64
+ assert result.thinking_content is None
65
+ assert result.regular_content is None
66
+ assert result.is_first_thinking_chunk is False
67
+ assert result.is_last_thinking_chunk is False
68
+ assert result.state_changed is False
69
+
70
+ def test_custom_values(self):
71
+ """
72
+ What it does: Verifies custom values in ThinkingParseResult.
73
+ Purpose: Ensure all fields can be set.
74
+ """
75
+ print("Creating ThinkingParseResult with custom values...")
76
+ result = ThinkingParseResult(
77
+ thinking_content="thinking",
78
+ regular_content="regular",
79
+ is_first_thinking_chunk=True,
80
+ is_last_thinking_chunk=True,
81
+ state_changed=True
82
+ )
83
+
84
+ print(f"Comparing thinking_content: Expected 'thinking', Got '{result.thinking_content}'")
85
+ assert result.thinking_content == "thinking"
86
+ assert result.regular_content == "regular"
87
+ assert result.is_first_thinking_chunk is True
88
+ assert result.is_last_thinking_chunk is True
89
+ assert result.state_changed is True
90
+
91
+
92
+ class TestThinkingParserInitialization:
93
+ """Tests for ThinkingParser initialization."""
94
+
95
+ def test_default_initialization(self):
96
+ """
97
+ What it does: Verifies default initialization of ThinkingParser.
98
+ Purpose: Ensure parser starts in PRE_CONTENT state with empty buffers.
99
+ """
100
+ print("Creating ThinkingParser with defaults...")
101
+ parser = ThinkingParser()
102
+
103
+ print(f"Comparing state: Expected PRE_CONTENT, Got {parser.state}")
104
+ assert parser.state == ParserState.PRE_CONTENT
105
+ assert parser.initial_buffer == ""
106
+ assert parser.thinking_buffer == ""
107
+ assert parser.open_tag is None
108
+ assert parser.close_tag is None
109
+ assert parser.is_first_thinking_chunk is True
110
+ assert parser._thinking_block_found is False
111
+
112
+ def test_custom_handling_mode(self):
113
+ """
114
+ What it does: Verifies custom handling_mode parameter.
115
+ Purpose: Ensure handling_mode can be overridden.
116
+ """
117
+ print("Creating ThinkingParser with custom handling_mode...")
118
+ parser = ThinkingParser(handling_mode="remove")
119
+
120
+ print(f"Comparing handling_mode: Expected 'remove', Got '{parser.handling_mode}'")
121
+ assert parser.handling_mode == "remove"
122
+
123
+ def test_custom_open_tags(self):
124
+ """
125
+ What it does: Verifies custom open_tags parameter.
126
+ Purpose: Ensure open_tags can be overridden.
127
+ """
128
+ print("Creating ThinkingParser with custom open_tags...")
129
+ custom_tags = ["<custom>", "<test>"]
130
+ parser = ThinkingParser(open_tags=custom_tags)
131
+
132
+ print(f"Comparing open_tags: Expected {custom_tags}, Got {parser.open_tags}")
133
+ assert parser.open_tags == custom_tags
134
+
135
+ def test_custom_initial_buffer_size(self):
136
+ """
137
+ What it does: Verifies custom initial_buffer_size parameter.
138
+ Purpose: Ensure initial_buffer_size can be overridden.
139
+ """
140
+ print("Creating ThinkingParser with custom initial_buffer_size...")
141
+ parser = ThinkingParser(initial_buffer_size=50)
142
+
143
+ print(f"Comparing initial_buffer_size: Expected 50, Got {parser.initial_buffer_size}")
144
+ assert parser.initial_buffer_size == 50
145
+
146
+ def test_max_tag_length_calculated(self):
147
+ """
148
+ What it does: Verifies max_tag_length is calculated from open_tags.
149
+ Purpose: Ensure cautious buffering uses correct buffer size.
150
+ """
151
+ print("Creating ThinkingParser and checking max_tag_length...")
152
+ parser = ThinkingParser(open_tags=["<thinking>", "<think>"])
153
+
154
+ # max_tag_length = max(len(tag) for tag in open_tags) * 2
155
+ # len("<thinking>") = 10, so max_tag_length = 20
156
+ expected = 20
157
+ print(f"Comparing max_tag_length: Expected {expected}, Got {parser.max_tag_length}")
158
+ assert parser.max_tag_length == expected
159
+
160
+
161
+ class TestThinkingParserFeedPreContent:
162
+ """Tests for ThinkingParser.feed() in PRE_CONTENT state."""
163
+
164
+ def test_empty_content_returns_empty_result(self):
165
+ """
166
+ What it does: Verifies empty content returns empty result.
167
+ Purpose: Ensure empty string doesn't change state.
168
+ """
169
+ print("Feeding empty content...")
170
+ parser = ThinkingParser()
171
+ result = parser.feed("")
172
+
173
+ print(f"Comparing result: Expected empty result")
174
+ assert result.thinking_content is None
175
+ assert result.regular_content is None
176
+ assert result.state_changed is False
177
+ assert parser.state == ParserState.PRE_CONTENT
178
+
179
+ def test_detects_thinking_tag(self):
180
+ """
181
+ What it does: Verifies <thinking> tag detection.
182
+ Purpose: Ensure parser transitions to IN_THINKING on tag detection.
183
+ """
184
+ print("Feeding content with <thinking> tag...")
185
+ parser = ThinkingParser()
186
+ result = parser.feed("<thinking>Hello")
187
+
188
+ print(f"Comparing state: Expected IN_THINKING, Got {parser.state}")
189
+ assert parser.state == ParserState.IN_THINKING
190
+ assert parser.open_tag == "<thinking>"
191
+ assert parser.close_tag == "</thinking>"
192
+ assert result.state_changed is True
193
+ assert parser._thinking_block_found is True
194
+
195
+ def test_detects_think_tag(self):
196
+ """
197
+ What it does: Verifies <think> tag detection.
198
+ Purpose: Ensure parser detects alternative tag format.
199
+ """
200
+ print("Feeding content with <think> tag...")
201
+ parser = ThinkingParser()
202
+ result = parser.feed("<think>Hello")
203
+
204
+ print(f"Comparing open_tag: Expected '<think>', Got '{parser.open_tag}'")
205
+ assert parser.state == ParserState.IN_THINKING
206
+ assert parser.open_tag == "<think>"
207
+ assert parser.close_tag == "</think>"
208
+
209
+ def test_detects_reasoning_tag(self):
210
+ """
211
+ What it does: Verifies <reasoning> tag detection.
212
+ Purpose: Ensure parser detects reasoning tag format.
213
+ """
214
+ print("Feeding content with <reasoning> tag...")
215
+ parser = ThinkingParser()
216
+ result = parser.feed("<reasoning>Hello")
217
+
218
+ print(f"Comparing open_tag: Expected '<reasoning>', Got '{parser.open_tag}'")
219
+ assert parser.state == ParserState.IN_THINKING
220
+ assert parser.open_tag == "<reasoning>"
221
+ assert parser.close_tag == "</reasoning>"
222
+
223
+ def test_detects_thought_tag(self):
224
+ """
225
+ What it does: Verifies <thought> tag detection.
226
+ Purpose: Ensure parser detects thought tag format.
227
+ """
228
+ print("Feeding content with <thought> tag...")
229
+ parser = ThinkingParser()
230
+ result = parser.feed("<thought>Hello")
231
+
232
+ print(f"Comparing open_tag: Expected '<thought>', Got '{parser.open_tag}'")
233
+ assert parser.state == ParserState.IN_THINKING
234
+ assert parser.open_tag == "<thought>"
235
+ assert parser.close_tag == "</thought>"
236
+
237
+ def test_strips_leading_whitespace_for_tag_detection(self):
238
+ """
239
+ What it does: Verifies leading whitespace is stripped for tag detection.
240
+ Purpose: Ensure tags with leading whitespace are detected.
241
+ """
242
+ print("Feeding content with leading whitespace...")
243
+ parser = ThinkingParser()
244
+ result = parser.feed(" \n\n<thinking>Hello")
245
+
246
+ print(f"Comparing state: Expected IN_THINKING, Got {parser.state}")
247
+ assert parser.state == ParserState.IN_THINKING
248
+ assert parser.open_tag == "<thinking>"
249
+
250
+ def test_buffers_partial_tag(self):
251
+ """
252
+ What it does: Verifies partial tag is buffered.
253
+ Purpose: Ensure parser waits for complete tag.
254
+ """
255
+ print("Feeding partial tag...")
256
+ parser = ThinkingParser()
257
+ result = parser.feed("<think")
258
+
259
+ print(f"Comparing state: Expected PRE_CONTENT, Got {parser.state}")
260
+ assert parser.state == ParserState.PRE_CONTENT
261
+ assert parser.initial_buffer == "<think"
262
+ assert result.state_changed is False
263
+
264
+ def test_completes_partial_tag(self):
265
+ """
266
+ What it does: Verifies partial tag is completed across chunks.
267
+ Purpose: Ensure split tags are handled correctly.
268
+ """
269
+ print("Feeding partial tag in two chunks...")
270
+ parser = ThinkingParser()
271
+
272
+ result1 = parser.feed("<think")
273
+ print(f"After first chunk: state={parser.state}, buffer='{parser.initial_buffer}'")
274
+ assert parser.state == ParserState.PRE_CONTENT
275
+
276
+ result2 = parser.feed("ing>Hello")
277
+ print(f"After second chunk: state={parser.state}")
278
+ assert parser.state == ParserState.IN_THINKING
279
+ assert parser.open_tag == "<thinking>"
280
+
281
+ def test_no_tag_transitions_to_streaming(self):
282
+ """
283
+ What it does: Verifies transition to STREAMING when no tag found.
284
+ Purpose: Ensure regular content is passed through.
285
+ """
286
+ print("Feeding content without thinking tag...")
287
+ parser = ThinkingParser()
288
+ result = parser.feed("Hello, this is regular content without any thinking tags.")
289
+
290
+ print(f"Comparing state: Expected STREAMING, Got {parser.state}")
291
+ assert parser.state == ParserState.STREAMING
292
+ assert result.state_changed is True
293
+ assert result.regular_content == "Hello, this is regular content without any thinking tags."
294
+
295
+ def test_buffer_exceeds_limit_transitions_to_streaming(self):
296
+ """
297
+ What it does: Verifies transition to STREAMING when buffer exceeds limit.
298
+ Purpose: Ensure parser doesn't buffer indefinitely.
299
+ """
300
+ print("Feeding content that exceeds buffer limit...")
301
+ parser = ThinkingParser(initial_buffer_size=10)
302
+ result = parser.feed("This is a long content that exceeds the buffer limit")
303
+
304
+ print(f"Comparing state: Expected STREAMING, Got {parser.state}")
305
+ assert parser.state == ParserState.STREAMING
306
+ assert result.state_changed is True
307
+
308
+
309
+ class TestThinkingParserFeedInThinking:
310
+ """Tests for ThinkingParser.feed() in IN_THINKING state."""
311
+
312
+ def test_accumulates_thinking_content(self):
313
+ """
314
+ What it does: Verifies thinking content is accumulated.
315
+ Purpose: Ensure content inside thinking block is captured.
316
+ """
317
+ print("Feeding thinking content...")
318
+ parser = ThinkingParser()
319
+ parser.feed("<thinking>")
320
+
321
+ # Feed more content
322
+ result = parser.feed("This is thinking content")
323
+
324
+ print(f"Comparing thinking_buffer: Got '{parser.thinking_buffer}'")
325
+ # Content is in buffer due to cautious sending
326
+ assert "This is thinking content" in parser.thinking_buffer or result.thinking_content
327
+
328
+ def test_detects_closing_tag(self):
329
+ """
330
+ What it does: Verifies closing tag detection.
331
+ Purpose: Ensure parser transitions to STREAMING on closing tag.
332
+ """
333
+ print("Feeding content with closing tag...")
334
+ parser = ThinkingParser()
335
+ parser.feed("<thinking>Hello")
336
+ result = parser.feed("</thinking>World")
337
+
338
+ print(f"Comparing state: Expected STREAMING, Got {parser.state}")
339
+ assert parser.state == ParserState.STREAMING
340
+ assert result.is_last_thinking_chunk is True
341
+ assert result.state_changed is True
342
+
343
+ def test_regular_content_after_closing_tag(self):
344
+ """
345
+ What it does: Verifies regular content after closing tag.
346
+ Purpose: Ensure content after closing tag is returned as regular_content.
347
+ """
348
+ print("Feeding content with closing tag and regular content...")
349
+ parser = ThinkingParser()
350
+ parser.feed("<thinking>Thinking")
351
+ result = parser.feed("</thinking>Regular content")
352
+
353
+ print(f"Comparing regular_content: Got '{result.regular_content}'")
354
+ assert result.regular_content == "Regular content"
355
+
356
+ def test_strips_whitespace_after_closing_tag(self):
357
+ """
358
+ What it does: Verifies whitespace is stripped after closing tag.
359
+ Purpose: Ensure leading newlines after closing tag are removed.
360
+ """
361
+ print("Feeding content with whitespace after closing tag...")
362
+ parser = ThinkingParser()
363
+ parser.feed("<thinking>Thinking")
364
+ result = parser.feed("</thinking>\n\nRegular content")
365
+
366
+ print(f"Comparing regular_content: Got '{result.regular_content}'")
367
+ assert result.regular_content == "Regular content"
368
+
369
+ def test_cautious_buffering(self):
370
+ """
371
+ What it does: Verifies cautious buffering keeps last max_tag_length chars.
372
+ Purpose: Ensure closing tag is not split across chunks.
373
+ """
374
+ print("Testing cautious buffering...")
375
+ parser = ThinkingParser(open_tags=["<t>"]) # Short tag for easier testing
376
+ parser.feed("<t>")
377
+
378
+ # Feed content longer than max_tag_length
379
+ long_content = "A" * 50
380
+ result = parser.feed(long_content)
381
+
382
+ print(f"Comparing thinking_buffer length: Got {len(parser.thinking_buffer)}")
383
+ # Buffer should keep last max_tag_length chars
384
+ assert len(parser.thinking_buffer) <= parser.max_tag_length
385
+
386
+ def test_split_closing_tag(self):
387
+ """
388
+ What it does: Verifies split closing tag is handled.
389
+ Purpose: Ensure closing tag split across chunks is detected.
390
+ """
391
+ print("Feeding split closing tag...")
392
+ parser = ThinkingParser()
393
+ parser.feed("<thinking>Hello")
394
+ parser.feed("</think")
395
+ result = parser.feed("ing>World")
396
+
397
+ print(f"Comparing state: Expected STREAMING, Got {parser.state}")
398
+ assert parser.state == ParserState.STREAMING
399
+
400
+
401
+ class TestThinkingParserFeedStreaming:
402
+ """Tests for ThinkingParser.feed() in STREAMING state."""
403
+
404
+ def test_passes_content_through(self):
405
+ """
406
+ What it does: Verifies content is passed through in STREAMING state.
407
+ Purpose: Ensure regular content is returned as-is.
408
+ """
409
+ print("Feeding content in STREAMING state...")
410
+ parser = ThinkingParser()
411
+ # Transition to STREAMING by feeding non-tag content
412
+ parser.feed("Regular content")
413
+
414
+ result = parser.feed("More content")
415
+
416
+ print(f"Comparing regular_content: Expected 'More content', Got '{result.regular_content}'")
417
+ assert result.regular_content == "More content"
418
+ assert result.thinking_content is None
419
+
420
+ def test_ignores_thinking_tags_in_streaming(self):
421
+ """
422
+ What it does: Verifies thinking tags are ignored in STREAMING state.
423
+ Purpose: Ensure tags after initial detection are passed through.
424
+ """
425
+ print("Feeding thinking tag in STREAMING state...")
426
+ parser = ThinkingParser()
427
+ parser.feed("Regular content") # Transition to STREAMING
428
+
429
+ result = parser.feed("<thinking>This should be regular</thinking>")
430
+
431
+ print(f"Comparing regular_content: Got '{result.regular_content}'")
432
+ assert result.regular_content == "<thinking>This should be regular</thinking>"
433
+ assert result.thinking_content is None
434
+
435
+
436
+ class TestThinkingParserFinalize:
437
+ """Tests for ThinkingParser.finalize()."""
438
+
439
+ def test_flushes_thinking_buffer(self):
440
+ """
441
+ What it does: Verifies thinking buffer is flushed on finalize.
442
+ Purpose: Ensure remaining thinking content is returned.
443
+ """
444
+ print("Finalizing parser with thinking buffer...")
445
+ parser = ThinkingParser()
446
+ parser.feed("<thinking>Incomplete thinking")
447
+
448
+ result = parser.finalize()
449
+
450
+ print(f"Comparing thinking_content: Got '{result.thinking_content}'")
451
+ assert result.thinking_content is not None
452
+ assert result.is_last_thinking_chunk is True
453
+
454
+ def test_flushes_initial_buffer(self):
455
+ """
456
+ What it does: Verifies initial buffer is flushed on finalize.
457
+ Purpose: Ensure buffered content is returned when no tag found.
458
+ """
459
+ print("Finalizing parser with initial buffer...")
460
+ parser = ThinkingParser()
461
+ parser.feed("<thi") # Partial tag, stays in initial_buffer
462
+
463
+ result = parser.finalize()
464
+
465
+ print(f"Comparing regular_content: Got '{result.regular_content}'")
466
+ assert result.regular_content == "<thi"
467
+
468
+ def test_clears_buffers_after_finalize(self):
469
+ """
470
+ What it does: Verifies buffers are cleared after finalize.
471
+ Purpose: Ensure buffers are empty after finalize.
472
+ """
473
+ print("Checking buffers after finalize...")
474
+ parser = ThinkingParser()
475
+ parser.feed("<thinking>Content")
476
+ parser.finalize()
477
+
478
+ print(f"Comparing buffers: thinking_buffer='{parser.thinking_buffer}', initial_buffer='{parser.initial_buffer}'")
479
+ assert parser.thinking_buffer == ""
480
+ assert parser.initial_buffer == ""
481
+
482
+
483
+ class TestThinkingParserReset:
484
+ """Tests for ThinkingParser.reset()."""
485
+
486
+ def test_resets_to_initial_state(self):
487
+ """
488
+ What it does: Verifies reset returns parser to initial state.
489
+ Purpose: Ensure parser can be reused.
490
+ """
491
+ print("Resetting parser after use...")
492
+ parser = ThinkingParser()
493
+ parser.feed("<thinking>Content</thinking>Regular")
494
+
495
+ parser.reset()
496
+
497
+ print(f"Comparing state: Expected PRE_CONTENT, Got {parser.state}")
498
+ assert parser.state == ParserState.PRE_CONTENT
499
+ assert parser.initial_buffer == ""
500
+ assert parser.thinking_buffer == ""
501
+ assert parser.open_tag is None
502
+ assert parser.close_tag is None
503
+ assert parser.is_first_thinking_chunk is True
504
+ assert parser._thinking_block_found is False
505
+
506
+
507
+ class TestThinkingParserFoundThinkingBlock:
508
+ """Tests for ThinkingParser.found_thinking_block property."""
509
+
510
+ def test_false_initially(self):
511
+ """
512
+ What it does: Verifies found_thinking_block is False initially.
513
+ Purpose: Ensure property starts as False.
514
+ """
515
+ print("Checking found_thinking_block initially...")
516
+ parser = ThinkingParser()
517
+
518
+ print(f"Comparing: Expected False, Got {parser.found_thinking_block}")
519
+ assert parser.found_thinking_block is False
520
+
521
+ def test_true_after_tag_detection(self):
522
+ """
523
+ What it does: Verifies found_thinking_block is True after tag detection.
524
+ Purpose: Ensure property is set when thinking block is found.
525
+ """
526
+ print("Checking found_thinking_block after tag detection...")
527
+ parser = ThinkingParser()
528
+ parser.feed("<thinking>Content")
529
+
530
+ print(f"Comparing: Expected True, Got {parser.found_thinking_block}")
531
+ assert parser.found_thinking_block is True
532
+
533
+ def test_false_when_no_tag(self):
534
+ """
535
+ What it does: Verifies found_thinking_block is False when no tag found.
536
+ Purpose: Ensure property stays False for regular content.
537
+ """
538
+ print("Checking found_thinking_block with no tag...")
539
+ parser = ThinkingParser()
540
+ parser.feed("Regular content without thinking tags")
541
+
542
+ print(f"Comparing: Expected False, Got {parser.found_thinking_block}")
543
+ assert parser.found_thinking_block is False
544
+
545
+
546
+ class TestThinkingParserProcessForOutput:
547
+ """Tests for ThinkingParser.process_for_output()."""
548
+
549
+ def test_as_reasoning_content_mode(self):
550
+ """
551
+ What it does: Verifies as_reasoning_content mode returns content as-is.
552
+ Purpose: Ensure content is returned unchanged for reasoning_content field.
553
+ """
554
+ print("Testing as_reasoning_content mode...")
555
+ parser = ThinkingParser(handling_mode="as_reasoning_content")
556
+ parser.open_tag = "<thinking>"
557
+ parser.close_tag = "</thinking>"
558
+
559
+ result = parser.process_for_output("Thinking content", is_first=True, is_last=True)
560
+
561
+ print(f"Comparing: Expected 'Thinking content', Got '{result}'")
562
+ assert result == "Thinking content"
563
+
564
+ def test_remove_mode(self):
565
+ """
566
+ What it does: Verifies remove mode returns None.
567
+ Purpose: Ensure thinking content is removed.
568
+ """
569
+ print("Testing remove mode...")
570
+ parser = ThinkingParser(handling_mode="remove")
571
+
572
+ result = parser.process_for_output("Thinking content", is_first=True, is_last=True)
573
+
574
+ print(f"Comparing: Expected None, Got {result}")
575
+ assert result is None
576
+
577
+ def test_pass_mode_first_chunk(self):
578
+ """
579
+ What it does: Verifies pass mode adds opening tag to first chunk.
580
+ Purpose: Ensure tags are preserved in pass mode.
581
+ """
582
+ print("Testing pass mode with first chunk...")
583
+ parser = ThinkingParser(handling_mode="pass")
584
+ parser.open_tag = "<thinking>"
585
+ parser.close_tag = "</thinking>"
586
+
587
+ result = parser.process_for_output("Content", is_first=True, is_last=False)
588
+
589
+ print(f"Comparing: Expected '<thinking>Content', Got '{result}'")
590
+ assert result == "<thinking>Content"
591
+
592
+ def test_pass_mode_last_chunk(self):
593
+ """
594
+ What it does: Verifies pass mode adds closing tag to last chunk.
595
+ Purpose: Ensure closing tag is added in pass mode.
596
+ """
597
+ print("Testing pass mode with last chunk...")
598
+ parser = ThinkingParser(handling_mode="pass")
599
+ parser.open_tag = "<thinking>"
600
+ parser.close_tag = "</thinking>"
601
+
602
+ result = parser.process_for_output("Content", is_first=False, is_last=True)
603
+
604
+ print(f"Comparing: Expected 'Content</thinking>', Got '{result}'")
605
+ assert result == "Content</thinking>"
606
+
607
+ def test_pass_mode_first_and_last_chunk(self):
608
+ """
609
+ What it does: Verifies pass mode adds both tags when first and last.
610
+ Purpose: Ensure both tags are added for single chunk.
611
+ """
612
+ print("Testing pass mode with first and last chunk...")
613
+ parser = ThinkingParser(handling_mode="pass")
614
+ parser.open_tag = "<thinking>"
615
+ parser.close_tag = "</thinking>"
616
+
617
+ result = parser.process_for_output("Content", is_first=True, is_last=True)
618
+
619
+ print(f"Comparing: Expected '<thinking>Content</thinking>', Got '{result}'")
620
+ assert result == "<thinking>Content</thinking>"
621
+
622
+ def test_pass_mode_middle_chunk(self):
623
+ """
624
+ What it does: Verifies pass mode returns content as-is for middle chunk.
625
+ Purpose: Ensure no tags are added for middle chunks.
626
+ """
627
+ print("Testing pass mode with middle chunk...")
628
+ parser = ThinkingParser(handling_mode="pass")
629
+ parser.open_tag = "<thinking>"
630
+ parser.close_tag = "</thinking>"
631
+
632
+ result = parser.process_for_output("Content", is_first=False, is_last=False)
633
+
634
+ print(f"Comparing: Expected 'Content', Got '{result}'")
635
+ assert result == "Content"
636
+
637
+ def test_strip_tags_mode(self):
638
+ """
639
+ What it does: Verifies strip_tags mode returns content without tags.
640
+ Purpose: Ensure content is returned without tags.
641
+ """
642
+ print("Testing strip_tags mode...")
643
+ parser = ThinkingParser(handling_mode="strip_tags")
644
+
645
+ result = parser.process_for_output("Thinking content", is_first=True, is_last=True)
646
+
647
+ print(f"Comparing: Expected 'Thinking content', Got '{result}'")
648
+ assert result == "Thinking content"
649
+
650
+ def test_none_content_returns_none(self):
651
+ """
652
+ What it does: Verifies None content returns None.
653
+ Purpose: Ensure None is handled correctly.
654
+ """
655
+ print("Testing None content...")
656
+ parser = ThinkingParser()
657
+
658
+ result = parser.process_for_output(None, is_first=True, is_last=True)
659
+
660
+ print(f"Comparing: Expected None, Got {result}")
661
+ assert result is None
662
+
663
+ def test_empty_content_returns_none(self):
664
+ """
665
+ What it does: Verifies empty content returns None.
666
+ Purpose: Ensure empty string is handled correctly.
667
+ """
668
+ print("Testing empty content...")
669
+ parser = ThinkingParser()
670
+
671
+ result = parser.process_for_output("", is_first=True, is_last=True)
672
+
673
+ print(f"Comparing: Expected None, Got {result}")
674
+ assert result is None
675
+
676
+
677
+ class TestThinkingParserFullFlow:
678
+ """Integration tests for full parsing flow."""
679
+
680
+ def test_complete_thinking_block(self):
681
+ """
682
+ What it does: Verifies complete thinking block parsing.
683
+ Purpose: Ensure full flow works correctly.
684
+ """
685
+ print("Testing complete thinking block flow...")
686
+ parser = ThinkingParser()
687
+
688
+ # Feed complete thinking block
689
+ result1 = parser.feed("<thinking>This is my reasoning process.</thinking>Here is the answer.")
690
+
691
+ print(f"State: {parser.state}")
692
+ print(f"Thinking content: {result1.thinking_content}")
693
+ print(f"Regular content: {result1.regular_content}")
694
+
695
+ assert parser.state == ParserState.STREAMING
696
+ assert parser.found_thinking_block is True
697
+ assert result1.regular_content == "Here is the answer."
698
+
699
+ def test_multi_chunk_thinking_block(self):
700
+ """
701
+ What it does: Verifies thinking block split across multiple chunks.
702
+ Purpose: Ensure chunked content is handled correctly.
703
+ """
704
+ print("Testing multi-chunk thinking block...")
705
+ parser = ThinkingParser()
706
+
707
+ # Feed in multiple chunks
708
+ result1 = parser.feed("<think")
709
+ print(f"After chunk 1: state={parser.state}")
710
+ assert parser.state == ParserState.PRE_CONTENT
711
+
712
+ result2 = parser.feed("ing>Let me think")
713
+ print(f"After chunk 2: state={parser.state}")
714
+ assert parser.state == ParserState.IN_THINKING
715
+
716
+ result3 = parser.feed(" about this...</think")
717
+ print(f"After chunk 3: state={parser.state}")
718
+ assert parser.state == ParserState.IN_THINKING
719
+
720
+ result4 = parser.feed("ing>The answer is 42.")
721
+ print(f"After chunk 4: state={parser.state}")
722
+ assert parser.state == ParserState.STREAMING
723
+ assert result4.regular_content == "The answer is 42."
724
+
725
+ def test_no_thinking_block(self):
726
+ """
727
+ What it does: Verifies handling of content without thinking block.
728
+ Purpose: Ensure regular content passes through unchanged.
729
+ """
730
+ print("Testing content without thinking block...")
731
+ parser = ThinkingParser()
732
+
733
+ result = parser.feed("This is just regular content without any thinking tags.")
734
+
735
+ print(f"State: {parser.state}")
736
+ print(f"Regular content: {result.regular_content}")
737
+
738
+ assert parser.state == ParserState.STREAMING
739
+ assert parser.found_thinking_block is False
740
+ assert result.regular_content == "This is just regular content without any thinking tags."
741
+
742
+ def test_thinking_block_with_newlines(self):
743
+ """
744
+ What it does: Verifies thinking block with newlines after closing tag.
745
+ Purpose: Ensure newlines are stripped from regular content.
746
+ """
747
+ print("Testing thinking block with newlines...")
748
+ parser = ThinkingParser()
749
+
750
+ result = parser.feed("<thinking>Reasoning</thinking>\n\n\nAnswer here")
751
+
752
+ print(f"Regular content: '{result.regular_content}'")
753
+ assert result.regular_content == "Answer here"
754
+
755
+ def test_empty_thinking_block(self):
756
+ """
757
+ What it does: Verifies empty thinking block handling.
758
+ Purpose: Ensure empty thinking block doesn't break parser.
759
+ """
760
+ print("Testing empty thinking block...")
761
+ parser = ThinkingParser()
762
+
763
+ result = parser.feed("<thinking></thinking>Answer")
764
+
765
+ print(f"State: {parser.state}")
766
+ print(f"Regular content: '{result.regular_content}'")
767
+ assert parser.state == ParserState.STREAMING
768
+ assert result.regular_content == "Answer"
769
+
770
+ def test_thinking_block_only_whitespace_after(self):
771
+ """
772
+ What it does: Verifies thinking block with only whitespace after closing tag.
773
+ Purpose: Ensure whitespace-only content after tag returns None.
774
+ """
775
+ print("Testing thinking block with only whitespace after...")
776
+ parser = ThinkingParser()
777
+
778
+ result = parser.feed("<thinking>Reasoning</thinking> \n\n ")
779
+
780
+ print(f"Regular content: {result.regular_content}")
781
+ # Whitespace-only content should be stripped to None
782
+ assert result.regular_content is None or result.regular_content == ""
783
+
784
+
785
+ class TestThinkingParserEdgeCases:
786
+ """Edge case tests for ThinkingParser."""
787
+
788
+ def test_nested_tags_not_supported(self):
789
+ """
790
+ What it does: Verifies nested tags are not specially handled.
791
+ Purpose: Ensure nested tags are treated as content.
792
+ """
793
+ print("Testing nested tags...")
794
+ parser = ThinkingParser()
795
+
796
+ result = parser.feed("<thinking>Outer<thinking>Inner</thinking>Still outer</thinking>Answer")
797
+
798
+ print(f"State: {parser.state}")
799
+ # First </thinking> closes the block
800
+ assert parser.state == ParserState.STREAMING
801
+
802
+ def test_tag_in_middle_of_content(self):
803
+ """
804
+ What it does: Verifies tag in middle of content is not detected.
805
+ Purpose: Ensure tags are only detected at start.
806
+ """
807
+ print("Testing tag in middle of content...")
808
+ parser = ThinkingParser()
809
+
810
+ result = parser.feed("Some text <thinking>This is not a thinking block</thinking>")
811
+
812
+ print(f"State: {parser.state}")
813
+ print(f"Regular content: '{result.regular_content}'")
814
+ assert parser.state == ParserState.STREAMING
815
+ assert parser.found_thinking_block is False
816
+ assert "<thinking>" in result.regular_content
817
+
818
+ def test_malformed_closing_tag(self):
819
+ """
820
+ What it does: Verifies malformed closing tag is not detected.
821
+ Purpose: Ensure only exact closing tag is matched.
822
+ """
823
+ print("Testing malformed closing tag...")
824
+ parser = ThinkingParser()
825
+
826
+ parser.feed("<thinking>Content")
827
+ result = parser.feed("</THINKING>More content") # Wrong case
828
+
829
+ print(f"State: {parser.state}")
830
+ # Should still be in thinking state
831
+ assert parser.state == ParserState.IN_THINKING
832
+
833
+ def test_unicode_content(self):
834
+ """
835
+ What it does: Verifies Unicode content is handled correctly.
836
+ Purpose: Ensure non-ASCII characters work.
837
+ """
838
+ print("Testing Unicode content...")
839
+ parser = ThinkingParser()
840
+
841
+ result = parser.feed("<thinking>Думаю о проблеме 🤔</thinking>Ответ: 42")
842
+
843
+ print(f"Regular content: '{result.regular_content}'")
844
+ assert parser.state == ParserState.STREAMING
845
+ assert result.regular_content == "Ответ: 42"
846
+
847
+ def test_very_long_thinking_content(self):
848
+ """
849
+ What it does: Verifies very long thinking content is handled.
850
+ Purpose: Ensure large content doesn't break parser.
851
+ """
852
+ print("Testing very long thinking content...")
853
+ parser = ThinkingParser()
854
+
855
+ long_content = "A" * 10000
856
+ result = parser.feed(f"<thinking>{long_content}</thinking>Done")
857
+
858
+ print(f"State: {parser.state}")
859
+ assert parser.state == ParserState.STREAMING
860
+ assert result.regular_content == "Done"
861
+
862
+ def test_special_characters_in_content(self):
863
+ """
864
+ What it does: Verifies special characters are handled.
865
+ Purpose: Ensure HTML-like content doesn't break parser.
866
+ """
867
+ print("Testing special characters...")
868
+ parser = ThinkingParser()
869
+
870
+ result = parser.feed("<thinking>Content with <b>bold</b> and &amp; entities</thinking>Answer")
871
+
872
+ print(f"State: {parser.state}")
873
+ assert parser.state == ParserState.STREAMING
874
+ assert result.regular_content == "Answer"
875
+
876
+ def test_multiple_feeds_after_streaming(self):
877
+ """
878
+ What it does: Verifies multiple feeds in STREAMING state.
879
+ Purpose: Ensure parser continues to work after transition.
880
+ """
881
+ print("Testing multiple feeds in STREAMING state...")
882
+ parser = ThinkingParser()
883
+
884
+ parser.feed("<thinking>Thinking</thinking>First")
885
+ result2 = parser.feed(" Second")
886
+ result3 = parser.feed(" Third")
887
+
888
+ print(f"Result 2: '{result2.regular_content}'")
889
+ print(f"Result 3: '{result3.regular_content}'")
890
+ assert result2.regular_content == " Second"
891
+ assert result3.regular_content == " Third"
892
+
893
+
894
+ class TestThinkingParserConfigIntegration:
895
+ """Tests for ThinkingParser integration with config."""
896
+
897
+ def test_uses_config_handling_mode(self):
898
+ """
899
+ What it does: Verifies parser uses FAKE_REASONING_HANDLING from config.
900
+ Purpose: Ensure config integration works.
901
+ """
902
+ print("Testing config handling mode...")
903
+ with patch('kiro_gateway.thinking_parser.FAKE_REASONING_HANDLING', 'remove'):
904
+ parser = ThinkingParser()
905
+
906
+ print(f"Handling mode: {parser.handling_mode}")
907
+ assert parser.handling_mode == "remove"
908
+
909
+ def test_uses_config_open_tags(self):
910
+ """
911
+ What it does: Verifies parser uses FAKE_REASONING_OPEN_TAGS from config.
912
+ Purpose: Ensure config integration works.
913
+ """
914
+ print("Testing config open tags...")
915
+ custom_tags = ["<custom>"]
916
+ with patch('kiro_gateway.thinking_parser.FAKE_REASONING_OPEN_TAGS', custom_tags):
917
+ parser = ThinkingParser()
918
+
919
+ print(f"Open tags: {parser.open_tags}")
920
+ assert parser.open_tags == custom_tags
921
+
922
+ def test_default_initial_buffer_size_from_config(self):
923
+ """
924
+ What it does: Verifies parser uses default initial_buffer_size from config.
925
+ Purpose: Ensure config value is used when not overridden.
926
+
927
+ Note: We can't easily patch the config value after import, so we just
928
+ verify the default is used. Custom values are tested in
929
+ TestThinkingParserInitialization.test_custom_initial_buffer_size.
930
+ """
931
+ print("Testing default initial buffer size from config...")
932
+ from kiro_gateway.config import FAKE_REASONING_INITIAL_BUFFER_SIZE
933
+
934
+ parser = ThinkingParser()
935
+
936
+ print(f"Initial buffer size: {parser.initial_buffer_size}")
937
+ print(f"Config value: {FAKE_REASONING_INITIAL_BUFFER_SIZE}")
938
+ assert parser.initial_buffer_size == FAKE_REASONING_INITIAL_BUFFER_SIZE
939
+
940
+
941
+ class TestInjectThinkingTags:
942
+ """Tests for inject_thinking_tags function in converters."""
943
+
944
+ def test_injects_tags_when_enabled(self):
945
+ """
946
+ What it does: Verifies tags are injected when FAKE_REASONING_ENABLED is True.
947
+ Purpose: Ensure tags are added to content.
948
+ """
949
+ print("Testing tag injection when enabled...")
950
+ from kiro_gateway.converters import inject_thinking_tags
951
+
952
+ with patch('kiro_gateway.converters.FAKE_REASONING_ENABLED', True):
953
+ with patch('kiro_gateway.converters.FAKE_REASONING_MAX_TOKENS', 4000):
954
+ result = inject_thinking_tags("Hello")
955
+
956
+ print(f"Result: '{result}'")
957
+ assert "<thinking_mode>enabled</thinking_mode>" in result
958
+ assert "<max_thinking_length>4000</max_thinking_length>" in result
959
+ assert "Hello" in result
960
+
961
+ def test_no_injection_when_disabled(self):
962
+ """
963
+ What it does: Verifies tags are not injected when FAKE_REASONING_ENABLED is False.
964
+ Purpose: Ensure tags are not added when disabled.
965
+ """
966
+ print("Testing no tag injection when disabled...")
967
+ from kiro_gateway.converters import inject_thinking_tags
968
+
969
+ with patch('kiro_gateway.converters.FAKE_REASONING_ENABLED', False):
970
+ result = inject_thinking_tags("Hello")
971
+
972
+ print(f"Result: '{result}'")
973
+ assert result == "Hello"
974
+ assert "<thinking_mode>" not in result
975
+
976
+ def test_injection_preserves_content(self):
977
+ """
978
+ What it does: Verifies original content is preserved after injection.
979
+ Purpose: Ensure content is not modified.
980
+ """
981
+ print("Testing content preservation...")
982
+ from kiro_gateway.converters import inject_thinking_tags
983
+
984
+ original = "This is my original content with special chars: <>&"
985
+
986
+ with patch('kiro_gateway.converters.FAKE_REASONING_ENABLED', True):
987
+ with patch('kiro_gateway.converters.FAKE_REASONING_MAX_TOKENS', 4000):
988
+ result = inject_thinking_tags(original)
989
+
990
+ print(f"Result ends with original: {result.endswith(original)}")
991
+ assert result.endswith(original)
992
+