|
19 | 19 | import logging |
20 | 20 | import os |
21 | 21 | import queue |
22 | | -import random |
23 | 22 | import threading |
24 | 23 | import time |
25 | 24 | from copy import deepcopy |
26 | 25 | from functools import wraps |
27 | | -from io import BytesIO |
28 | 26 | from typing import Any, Callable, Coroutine, Optional, Type, Union |
29 | 27 |
|
30 | 28 | import requests |
|
33 | 31 | Response, |
34 | 32 | jsonify, |
35 | 33 | make_response, |
36 | | - send_file, |
37 | 34 | ) |
38 | 35 | from flask_login import current_user |
39 | 36 | from flask import ( |
40 | 37 | request as flask_request, |
41 | 38 | ) |
42 | 39 | from peewee import OperationalError |
43 | | -from werkzeug.http import HTTP_STATUS_CODES |
44 | 40 |
|
45 | 41 | from api import settings |
46 | | -from api.constants import REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC |
47 | 42 | from api.db import ActiveEnum |
48 | 43 | from api.db.db_models import APIToken |
49 | | -from api.utils.json_encode import CustomJSONEncoder, json_dumps |
| 44 | +from api.utils.json_encode import CustomJSONEncoder |
50 | 45 | from rag.utils.mcp_tool_call_conn import MCPToolCallSession, close_multiple_mcp_toolcall_sessions |
51 | 46 |
|
52 | 47 | requests.models.complexjson.dumps = functools.partial(json.dumps, cls=CustomJSONEncoder) |
@@ -77,19 +72,6 @@ def serialize_for_json(obj): |
77 | 72 | # Fallback: convert to string representation |
78 | 73 | return str(obj) |
79 | 74 |
|
80 | | - |
81 | | -def get_exponential_backoff_interval(retries, full_jitter=False): |
82 | | - """Calculate the exponential backoff wait time.""" |
83 | | - # Will be zero if factor equals 0 |
84 | | - countdown = min(REQUEST_MAX_WAIT_SEC, REQUEST_WAIT_SEC * (2 ** retries)) |
85 | | - # Full jitter according to |
86 | | - # https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ |
87 | | - if full_jitter: |
88 | | - countdown = random.randrange(countdown + 1) |
89 | | - # Adjust according to maximum wait time and account for negative values. |
90 | | - return max(0, countdown) |
91 | | - |
92 | | - |
93 | 75 | def get_data_error_result(code=settings.RetCode.DATA_ERROR, message="Sorry! Data missing!"): |
94 | 76 | logging.exception(Exception(message)) |
95 | 77 | result_dict = {"code": code, "message": message} |
@@ -124,22 +106,6 @@ def server_error_response(e): |
124 | 106 | return get_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e)) |
125 | 107 |
|
126 | 108 |
|
127 | | -def error_response(response_code, message=None): |
128 | | - if message is None: |
129 | | - message = HTTP_STATUS_CODES.get(response_code, "Unknown Error") |
130 | | - |
131 | | - return Response( |
132 | | - json.dumps( |
133 | | - { |
134 | | - "message": message, |
135 | | - "code": response_code, |
136 | | - } |
137 | | - ), |
138 | | - status=response_code, |
139 | | - mimetype="application/json", |
140 | | - ) |
141 | | - |
142 | | - |
143 | 109 | def validate_request(*args, **kwargs): |
144 | 110 | def wrapper(func): |
145 | 111 | @wraps(func) |
@@ -203,23 +169,6 @@ def wrapper(*args, **kwargs): |
203 | 169 | return wrapper |
204 | 170 |
|
205 | 171 |
|
206 | | -def is_localhost(ip): |
207 | | - return ip in {"127.0.0.1", "::1", "[::1]", "localhost"} |
208 | | - |
209 | | - |
210 | | -def send_file_in_mem(data, filename): |
211 | | - if not isinstance(data, (str, bytes)): |
212 | | - data = json_dumps(data) |
213 | | - if isinstance(data, str): |
214 | | - data = data.encode("utf-8") |
215 | | - |
216 | | - f = BytesIO() |
217 | | - f.write(data) |
218 | | - f.seek(0) |
219 | | - |
220 | | - return send_file(f, as_attachment=True, attachment_filename=filename) |
221 | | - |
222 | | - |
223 | 172 | def get_json_result(code: settings.RetCode = settings.RetCode.SUCCESS, message="success", data=None): |
224 | 173 | response = {"code": code, "message": message, "data": data} |
225 | 174 | return jsonify(response) |
@@ -264,36 +213,12 @@ def construct_response(code=settings.RetCode.SUCCESS, message="success", data=No |
264 | 213 | return response |
265 | 214 |
|
266 | 215 |
|
267 | | -def construct_result(code=settings.RetCode.DATA_ERROR, message="data is missing"): |
268 | | - result_dict = {"code": code, "message": message} |
269 | | - response = {} |
270 | | - for key, value in result_dict.items(): |
271 | | - if value is None and key != "code": |
272 | | - continue |
273 | | - else: |
274 | | - response[key] = value |
275 | | - return jsonify(response) |
276 | | - |
277 | | - |
278 | 216 | def construct_json_result(code: settings.RetCode = settings.RetCode.SUCCESS, message="success", data=None): |
279 | 217 | if data is None: |
280 | 218 | return jsonify({"code": code, "message": message}) |
281 | 219 | else: |
282 | 220 | return jsonify({"code": code, "message": message, "data": data}) |
283 | 221 |
|
284 | | - |
285 | | -def construct_error_response(e): |
286 | | - logging.exception(e) |
287 | | - try: |
288 | | - if e.code == 401: |
289 | | - return construct_json_result(code=settings.RetCode.UNAUTHORIZED, message=repr(e)) |
290 | | - except BaseException: |
291 | | - pass |
292 | | - if len(e.args) > 1: |
293 | | - return construct_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e.args[0]), data=e.args[1]) |
294 | | - return construct_json_result(code=settings.RetCode.EXCEPTION_ERROR, message=repr(e)) |
295 | | - |
296 | | - |
297 | 222 | def token_required(func): |
298 | 223 | @wraps(func) |
299 | 224 | def decorated_function(*args, **kwargs): |
|
0 commit comments