Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/api/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,34 @@ def _create_fallback_chat_response(
)


def _create_error_chat_response(
user_message: str,
error_message: str,
error_type: str,
session_id: str,
confidence: float,
) -> ChatResponse:
"""Create a standardized ChatResponse for handled chat errors."""
suggestions = [
"Try rephrasing your question",
"Try again in a moment",
"Contact support if the issue persists",
]
return ChatResponse(
reply=user_message,
route="error",
intent="error",
session_id=session_id,
context={
"error": error_message,
"error_type": error_type,
"suggestions": suggestions,
},
confidence=confidence,
recommendations=suggestions,
)


def _create_safety_violation_response(
violations: List[str],
confidence: float,
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_chat_router_private_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Regression tests for chat router private helper wiring."""

import ast
import unittest
from pathlib import Path


CHAT_ROUTER_PATH = Path(__file__).resolve().parents[2] / "src" / "api" / "routers" / "chat.py"


class ChatRouterPrivateHelperTests(unittest.TestCase):
def test_called_create_helpers_are_defined(self) -> None:
"""Ensure refactors do not leave runtime-only missing helper errors behind."""
tree = ast.parse(CHAT_ROUTER_PATH.read_text())
defined_helpers = set()
called_helpers = set()

for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name.startswith("_create_"):
defined_helpers.add(node.name)
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id.startswith("_create_"):
called_helpers.add(node.func.id)

self.assertLessEqual(called_helpers, defined_helpers)


if __name__ == "__main__":
unittest.main()
Loading