kiln-server 0.23.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kiln_server/__init__.py +0 -0
- kiln_server/custom_errors.py +101 -0
- kiln_server/document_api.py +2113 -0
- kiln_server/generate_openapi.py +18 -0
- kiln_server/mcp/README.md +66 -0
- kiln_server/mcp/__init__.py +13 -0
- kiln_server/mcp/mcp.py +173 -0
- kiln_server/mcp/mcp_server_tool_utils.py +109 -0
- kiln_server/mcp/runtime.py +88 -0
- kiln_server/mcp/test_mcp_server.py +371 -0
- kiln_server/mcp/tool_selection.py +95 -0
- kiln_server/project_api.py +117 -0
- kiln_server/prompt_api.py +175 -0
- kiln_server/run_api.py +405 -0
- kiln_server/server.py +101 -0
- kiln_server/task_api.py +197 -0
- kiln_server/test_custom_error.py +94 -0
- kiln_server/test_document_api.py +4986 -0
- kiln_server/test_project_api.py +498 -0
- kiln_server/test_prompt_api.py +244 -0
- kiln_server/test_run_api.py +1552 -0
- kiln_server/test_task_api.py +686 -0
- kiln_server-0.23.0.dist-info/METADATA +107 -0
- kiln_server-0.23.0.dist-info/RECORD +27 -0
- kiln_server-0.23.0.dist-info/WHEEL +4 -0
- kiln_server-0.23.0.dist-info/entry_points.txt +3 -0
- kiln_server-0.23.0.dist-info/licenses/LICENSE.txt +12 -0
kiln_server/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
|
|
4
|
+
from fastapi import FastAPI, HTTPException, Request, status
|
|
5
|
+
from fastapi.exceptions import RequestValidationError
|
|
6
|
+
from fastapi.responses import JSONResponse
|
|
7
|
+
from pydantic import ValidationError
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def format_error_loc(loc: tuple):
|
|
13
|
+
if not loc:
|
|
14
|
+
return ""
|
|
15
|
+
formatted = []
|
|
16
|
+
# Skip the first item if it's "body" (case-insensitive)
|
|
17
|
+
start_index = 1 if loc and loc[0].lower() == "body" else 0
|
|
18
|
+
for i, item in enumerate(loc[start_index:]):
|
|
19
|
+
if item is not None and item != "":
|
|
20
|
+
if isinstance(item, str):
|
|
21
|
+
formatted.append(
|
|
22
|
+
item.capitalize() if i == 0 else "." + item.capitalize()
|
|
23
|
+
)
|
|
24
|
+
elif isinstance(item, int):
|
|
25
|
+
formatted.append(f"[{item}]")
|
|
26
|
+
else:
|
|
27
|
+
formatted.append(str(item))
|
|
28
|
+
return "".join(formatted)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def connect_custom_errors(app: FastAPI):
|
|
32
|
+
@app.exception_handler(ValidationError)
|
|
33
|
+
@app.exception_handler(RequestValidationError)
|
|
34
|
+
async def validation_exception_handler(
|
|
35
|
+
request: Request, exc: RequestValidationError | ValidationError
|
|
36
|
+
):
|
|
37
|
+
# Warn level because user data being incorrect isn't a server problem
|
|
38
|
+
# Format errors as JSON string so they're fully captured in dev logs
|
|
39
|
+
if logger.isEnabledFor(logging.WARNING):
|
|
40
|
+
errors = exc.errors()
|
|
41
|
+
errors_json = json.dumps(errors, indent=2, default=str)
|
|
42
|
+
logger.warning(
|
|
43
|
+
f"Validation error on {request.method} {request.url.path} ({len(errors)} error(s)):\n{errors_json}",
|
|
44
|
+
exc_info=exc,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# Write user friendly error messages
|
|
48
|
+
error_messages = []
|
|
49
|
+
for error in exc.errors():
|
|
50
|
+
message = error.get("msg", "Unknown error")
|
|
51
|
+
loc = error.get("loc")
|
|
52
|
+
|
|
53
|
+
# Custom helpers for common errors
|
|
54
|
+
if "String should match pattern '^[A-Za-z0-9 _-]+$'" == message:
|
|
55
|
+
message = "must consist of only letters, numbers, spaces, hyphens, and underscores"
|
|
56
|
+
|
|
57
|
+
error_messages.append(f"{format_error_loc(loc)}: {message}")
|
|
58
|
+
|
|
59
|
+
def serialize_error(error):
|
|
60
|
+
return {
|
|
61
|
+
"type": error.get("type"),
|
|
62
|
+
"loc": [str(loc) for loc in error.get("loc", [])],
|
|
63
|
+
"msg": error.get("msg"),
|
|
64
|
+
"input": str(error.get("input")),
|
|
65
|
+
"ctx": {str(k): str(v) for k, v in error.get("ctx", {}).items()},
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
serialized_errors = [serialize_error(error) for error in exc.errors()]
|
|
69
|
+
|
|
70
|
+
return JSONResponse(
|
|
71
|
+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
72
|
+
headers={"Access-Control-Allow-Origin": "*"},
|
|
73
|
+
content={
|
|
74
|
+
"error_messages": error_messages,
|
|
75
|
+
"message": ".\n".join(error_messages),
|
|
76
|
+
"source_errors": serialized_errors,
|
|
77
|
+
},
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Wrap in a format that the client can understand (message, and error_messages)
|
|
81
|
+
@app.exception_handler(HTTPException)
|
|
82
|
+
async def http_exception_handler(request: Request, exc: HTTPException):
|
|
83
|
+
return JSONResponse(
|
|
84
|
+
status_code=exc.status_code,
|
|
85
|
+
headers={"Access-Control-Allow-Origin": "*"},
|
|
86
|
+
content={"message": exc.detail},
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Fallback error handler for any other exception
|
|
90
|
+
@app.exception_handler(Exception)
|
|
91
|
+
async def fallback_error_handler(request: Request, exc: Exception):
|
|
92
|
+
# Prefer the message/detail from the exception if available, fallback to the exception string
|
|
93
|
+
message = str(exc)
|
|
94
|
+
message = getattr(exc, "detail", message)
|
|
95
|
+
message = getattr(exc, "message", message)
|
|
96
|
+
|
|
97
|
+
return JSONResponse(
|
|
98
|
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
99
|
+
headers={"Access-Control-Allow-Origin": "*"},
|
|
100
|
+
content={"message": message, "raw_error": str(exc)},
|
|
101
|
+
)
|