@worktango/ai-assistant 0.0.37 → 0.0.39
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.
- package/dist/express.js +22 -18
- package/express.d.ts +12 -3
- package/package.json +1 -1
package/dist/express.js
CHANGED
|
@@ -6485,9 +6485,9 @@ var require_fix_request_body = __commonJS({
|
|
|
6485
6485
|
"../../node_modules/http-proxy-middleware/dist/handlers/fix-request-body.js"(exports) {
|
|
6486
6486
|
"use strict";
|
|
6487
6487
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6488
|
-
exports.fixRequestBody =
|
|
6488
|
+
exports.fixRequestBody = fixRequestBody2;
|
|
6489
6489
|
var querystring = require("querystring");
|
|
6490
|
-
function
|
|
6490
|
+
function fixRequestBody2(proxyReq, req) {
|
|
6491
6491
|
if (req.readableLength !== 0) {
|
|
6492
6492
|
return;
|
|
6493
6493
|
}
|
|
@@ -11191,28 +11191,33 @@ function setupAiAssistantRoutes(args) {
|
|
|
11191
11191
|
const routePath = args.route ?? "/ai-assistant";
|
|
11192
11192
|
const port = args.targetPort ?? 80;
|
|
11193
11193
|
const protocol = "http";
|
|
11194
|
+
args.app.use(routePath, (req, res, next) => {
|
|
11195
|
+
args.getBearerToken(req.headers).then((bearerToken) => {
|
|
11196
|
+
req.headers.Authorization = `Bearer ${bearerToken}`;
|
|
11197
|
+
next();
|
|
11198
|
+
}).catch((error) => {
|
|
11199
|
+
logger.error("Error getting bearer token:", error);
|
|
11200
|
+
next(error);
|
|
11201
|
+
});
|
|
11202
|
+
});
|
|
11194
11203
|
const proxyOptions = {
|
|
11195
11204
|
target: `${protocol}://${args.targetHost}:${port}`,
|
|
11196
11205
|
changeOrigin: true,
|
|
11197
11206
|
pathRewrite: (path, req) => {
|
|
11198
|
-
|
|
11207
|
+
const expressRequest = req;
|
|
11208
|
+
return args.getTargetPath(expressRequest.originalUrl || path);
|
|
11199
11209
|
},
|
|
11200
11210
|
on: {
|
|
11201
|
-
proxyReq:
|
|
11202
|
-
|
|
11203
|
-
|
|
11204
|
-
|
|
11205
|
-
|
|
11206
|
-
|
|
11207
|
-
|
|
11211
|
+
proxyReq: import_http_proxy_middleware.fixRequestBody,
|
|
11212
|
+
proxyRes: (proxyRes, req, res) => {
|
|
11213
|
+
if (req.headers.accept === "text/event-stream") {
|
|
11214
|
+
res.writeHead(res.statusCode, {
|
|
11215
|
+
"Content-Type": "text/event-stream",
|
|
11216
|
+
"Cache-Control": "no-transform",
|
|
11217
|
+
Connection: "keep-alive"
|
|
11218
|
+
});
|
|
11208
11219
|
}
|
|
11209
11220
|
},
|
|
11210
|
-
proxyRes: (proxyRes) => {
|
|
11211
|
-
logger.debug("Proxy response received", {
|
|
11212
|
-
status: proxyRes.statusCode,
|
|
11213
|
-
headers: proxyRes.headers
|
|
11214
|
-
});
|
|
11215
|
-
},
|
|
11216
11221
|
error: (err, req, res) => {
|
|
11217
11222
|
logger.error("Proxy error:", err);
|
|
11218
11223
|
if ("headersSent" in res && !res.headersSent) {
|
|
@@ -11221,8 +11226,7 @@ function setupAiAssistantRoutes(args) {
|
|
|
11221
11226
|
res.end();
|
|
11222
11227
|
}
|
|
11223
11228
|
}
|
|
11224
|
-
}
|
|
11225
|
-
logger
|
|
11229
|
+
}
|
|
11226
11230
|
};
|
|
11227
11231
|
const proxy = (0, import_http_proxy_middleware.createProxyMiddleware)(proxyOptions);
|
|
11228
11232
|
args.app.use(routePath, ...coalesce(args.preflightMiddlewares, []), proxy);
|
package/express.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ export function setupAiAssistantRoutes(args: {
|
|
|
7
7
|
* back-channel request proxying. This should be a JWT token that can be
|
|
8
8
|
* decoded on the `kazoo-web` application to authenticate the request.
|
|
9
9
|
*/
|
|
10
|
-
getBearerToken: (
|
|
10
|
+
getBearerToken: (
|
|
11
|
+
headers: Record<string, undefined | string | string[]>
|
|
12
|
+
) => Promise<string>;
|
|
11
13
|
/**
|
|
12
14
|
* The host of the application to proxy requests to.
|
|
13
15
|
*/
|
|
@@ -31,8 +33,15 @@ export function setupAiAssistantRoutes(args: {
|
|
|
31
33
|
preflightMiddlewares?: RequestHandler[];
|
|
32
34
|
/**
|
|
33
35
|
* A function that should return the target path for the proxy request, based
|
|
34
|
-
* on the incoming
|
|
36
|
+
* on the incoming full path. The path returned will be appended to the
|
|
35
37
|
* target host and port to form the final URL.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* getTargetPath: (fullPath) => {
|
|
42
|
+
* return fullPath;
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
36
45
|
*/
|
|
37
|
-
getTargetPath: (
|
|
46
|
+
getTargetPath: (fullPath: string) => string;
|
|
38
47
|
}): void;
|