Skip to content

Commit

Permalink
[Obs AI Assistant] Fall back to request.url for kibana fn (#173717)
Browse files Browse the repository at this point in the history
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Justin Kambic <[email protected]>
  • Loading branch information
3 people authored Dec 22, 2023
1 parent b5cd85c commit 31b7380
Showing 1 changed file with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import axios from 'axios';
import { format } from 'url';
import { format, parse } from 'url';
import { castArray, first, pick, pickBy } from 'lodash';
import type { FunctionRegistrationParameters } from '.';

export function registerKibanaFunction({
Expand Down Expand Up @@ -51,28 +52,45 @@ export function registerKibanaFunction({
({ arguments: { method, pathname, body, query } }, signal) => {
const { request } = resources;

const {
protocol,
host,
username,
password,
pathname: pathnameFromRequest,
} = request.rewrittenUrl!;
const { protocol, host, pathname: pathnameFromRequest } = request.rewrittenUrl || request.url;

const origin = first(castArray(request.headers.origin));

const nextUrl = {
host,
protocol,
username,
password,
...(origin ? pick(parse(origin), 'host', 'protocol') : {}),
pathname: pathnameFromRequest.replace(
'/internal/observability_ai_assistant/chat/complete',
pathname
),
query,
};

const copiedHeaderNames = [
'accept-encoding',
'accept-language',
'accept',
'content-type',
'cookie',
'kbn-build-number',
'kbn-version',
'origin',
'referer',
'user-agent',
'x-elastic-internal-origin',
'x-kbn-context',
];

const headers = pickBy(request.headers, (value, key) => {
return (
copiedHeaderNames.includes(key.toLowerCase()) || key.toLowerCase().startsWith('sec-')
);
});

return axios({
method,
headers: request.headers,
headers,
url: format(nextUrl),
data: body ? JSON.stringify(body) : undefined,
signal,
Expand Down

0 comments on commit 31b7380

Please sign in to comment.