diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/evaluation.ts b/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/evaluation.ts index 98fe6903ba620a..17e6217f99fc98 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/evaluation.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/evaluation.ts @@ -77,6 +77,8 @@ function runEvaluations() { node: serviceUrls.esUrl, }); + await kibanaClient.createSpaceIfNeeded(); + const connectors = await kibanaClient.getConnectors(); if (!connectors.length) { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/kibana_client.ts b/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/kibana_client.ts index 0de3d3cebabe31..f491dbd182f702 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/kibana_client.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_app/scripts/evaluation/kibana_client.ts @@ -95,7 +95,7 @@ export class KibanaClient { }); } - private getUrl(props: { query?: UrlObject['query']; pathname: string }) { + private getUrl(props: { query?: UrlObject['query']; pathname: string; ignoreSpaceId?: boolean }) { const parsed = parse(this.url); const baseUrl = parsed.pathname?.replaceAll('/', '') ?? ''; @@ -104,7 +104,7 @@ export class KibanaClient { ...parsed, pathname: `/${[ baseUrl, - ...(this.spaceId ? ['s', this.spaceId] : []), + ...(props.ignoreSpaceId || !this.spaceId ? [] : ['s', this.spaceId]), props.pathname.startsWith('/') ? props.pathname.substring(1) : props.pathname, ].join('/')}`, query: props.query, @@ -115,7 +115,7 @@ export class KibanaClient { callKibana( method: string, - props: { query?: UrlObject['query']; pathname: string }, + props: { query?: UrlObject['query']; pathname: string; ignoreSpaceId?: boolean }, data?: any ) { const url = this.getUrl(props); @@ -162,6 +162,58 @@ export class KibanaClient { this.log.info('Knowledge base installed'); } + async createSpaceIfNeeded() { + if (!this.spaceId) { + return; + } + + this.log.debug(`Checking if space ${this.spaceId} exists`); + + const spaceExistsResponse = await this.callKibana<{ + id?: string; + }>('GET', { + pathname: `/api/spaces/space/${this.spaceId}`, + ignoreSpaceId: true, + }).catch((error) => { + if (isAxiosError(error) && error.response?.status === 404) { + return { + status: 404, + data: { + id: undefined, + }, + }; + } + throw error; + }); + + if (spaceExistsResponse.data.id) { + this.log.debug(`Space id ${this.spaceId} found`); + return; + } + + this.log.info(`Creating space ${this.spaceId}`); + + const spaceCreatedResponse = await this.callKibana<{ id: string }>( + 'POST', + { + pathname: '/api/spaces/space', + ignoreSpaceId: true, + }, + { + id: this.spaceId, + name: this.spaceId, + } + ); + + if (spaceCreatedResponse.status === 200) { + this.log.info(`Created space ${this.spaceId}`); + } else { + throw new Error( + `Error creating space: ${spaceCreatedResponse.status} - ${spaceCreatedResponse.data}` + ); + } + } + createChatClient({ connectorId, evaluationConnectorId,