/
/
/
1"""Shared AI request and response limit guards for the Music Quiz provider."""
2
3from __future__ import annotations
4
5from music_assistant.providers.music_quiz.constants import (
6 MAX_AI_PROMPT_BYTES,
7 MAX_AI_RESPONSE_BYTES,
8 MAX_AI_RESPONSE_LINES,
9)
10
11
12def ai_prompt_exceeds_limit(prompt: str) -> bool:
13 """
14 Return whether a prompt is too large to submit to an AI engine.
15
16 :param prompt: Prompt the caller intends to submit.
17 """
18 return len(prompt.encode("utf-8")) > MAX_AI_PROMPT_BYTES
19
20
21def validate_ai_response(response: object) -> str:
22 """
23 Return the AI response text, if it is within the shared response limits.
24
25 :param response: Untrusted response returned by an AI provider.
26 :raises TypeError: If the response is not a string.
27 :raises ValueError: If the response exceeds the size or line limit.
28 """
29 if not isinstance(response, str):
30 raise TypeError("response must be a string")
31 if len(response.encode("utf-8")) > MAX_AI_RESPONSE_BYTES:
32 raise ValueError("response exceeds the size limit")
33 if len(response.splitlines()) > MAX_AI_RESPONSE_LINES:
34 raise ValueError("response exceeds the line limit")
35 return response
36