Skip to content

Build your personal knowledge base with Trilium Notes

refactor(server): add triliumResponseHandled to typings

Elian Doran9ddcaf455270
diff --git a/apps/server/src/express.d.ts b/apps/server/src/express.d.ts
index 5b9673d693..c2708f0ec0 100644
--- a/apps/server/src/express.d.ts
+++ b/apps/server/src/express.d.ts
@@ -17,6 +17,11 @@ export declare module "express-serve-static-core" {
"user-agent"?: string;
};
}
+
+ interface Response {
+ /** Set to true to prevent apiResultHandler from double-handling the response (e.g., for SSE streams) */
+ triliumResponseHandled?: boolean;
+ }
}
export declare module "express-session" {
diff --git a/apps/server/src/routes/api/llm_chat.ts b/apps/server/src/routes/api/llm_chat.ts
index 672df99a4c..fd97d19924 100644
--- a/apps/server/src/routes/api/llm_chat.ts
+++ b/apps/server/src/routes/api/llm_chat.ts
@@ -1,4 +1,5 @@
import type { Request, Response } from "express";
+
import { getProvider, type LlmMessage, type LlmProviderConfig } from "../../services/llm/index.js";
interface ChatRequest {
@@ -34,7 +35,7 @@ async function streamChat(req: Request, res: Response) {
res.flushHeaders();
// Mark response as handled to prevent double-handling by apiResultHandler
- (res as any).triliumResponseHandled = true;
+ res.triliumResponseHandled = true;
// Type assertion for flush method (available when compression is used)
const flushableRes = res as Response & { flush?: () => void };
diff --git a/apps/server/src/routes/route_api.ts b/apps/server/src/routes/route_api.ts
index 444cc260d4..93bdc56f06 100644
--- a/apps/server/src/routes/route_api.ts
+++ b/apps/server/src/routes/route_api.ts
@@ -145,7 +145,7 @@ function internalRoute<P extends ParamsDictionary>(method: HttpMethod, path: str
function handleResponse(resultHandler: ApiResultHandler, req: express.Request, res: express.Response, result: unknown, start: number) {
// Skip result handling if the response has already been handled
- if ((res as any).triliumResponseHandled) {
+ if (res.triliumResponseHandled) {
// Just log the request without additional processing
log.request(req, res, Date.now() - start, 0);
return;
@@ -161,7 +161,7 @@ function handleException(e: unknown | Error, method: HttpMethod, path: string, r
log.error(`${method} ${path} threw exception: '${errMessage}', stack: ${errStack}`);
// Skip sending response if it's already been handled by the route handler
- if ((res as unknown as { triliumResponseHandled?: boolean }).triliumResponseHandled || res.headersSent) {
+ if (res.triliumResponseHandled || res.headersSent) {
return;
}