forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9cb795
commit 3432888
Showing
6 changed files
with
183 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1550,4 +1550,4 @@ | |
"xmlbuilder": "13.0.2", | ||
"yargs": "^15.4.1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
x-pack/plugins/observability_ai_assistant/public/service/create_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { CoreStart } from '@kbn/core/public'; | ||
import { ReadableStream } from 'stream/web'; | ||
import { ObservabilityAIAssistantService } from '../types'; | ||
import { createService } from './create_service'; | ||
|
||
describe('createService', () => { | ||
describe('chat', () => { | ||
let service: ObservabilityAIAssistantService; | ||
|
||
const httpPostSpy = jest.fn(); | ||
|
||
function respondWithChunks({ chunks, status = 200 }: { status?: number; chunks: string[][] }) { | ||
const response = { | ||
response: { | ||
status, | ||
body: new ReadableStream({ | ||
start(controller) { | ||
chunks.forEach((chunk) => { | ||
controller.enqueue(new TextEncoder().encode(chunk.join('\n'))); | ||
}); | ||
controller.close(); | ||
}, | ||
}), | ||
}, | ||
}; | ||
|
||
httpPostSpy.mockResolvedValueOnce(response); | ||
} | ||
|
||
async function chat(signal: AbortSignal = new AbortController().signal) { | ||
const response = await service.chat({ messages: [], connectorId: '', signal }); | ||
|
||
return response; | ||
} | ||
|
||
beforeEach(() => { | ||
service = createService({ | ||
http: { | ||
post: httpPostSpy, | ||
}, | ||
} as unknown as CoreStart); | ||
}); | ||
|
||
afterEach(() => { | ||
httpPostSpy.mockReset(); | ||
}); | ||
|
||
it('correctly parses a stream of JSON lines', async () => { | ||
const chunk1 = ['data: {}', 'data: {}']; | ||
const chunk2 = ['data: {}', 'data: [DONE]']; | ||
|
||
respondWithChunks({ chunks: [chunk1, chunk2] }); | ||
|
||
const response$ = await chat(); | ||
|
||
const results: any = []; | ||
response$.subscribe({ | ||
next: (data) => results.push(data), | ||
complete: () => { | ||
expect(results).toHaveLength(3); | ||
}, | ||
}); | ||
}); | ||
|
||
it('correctly buffers partial lines', async () => { | ||
const chunk1 = ['data: {}', 'data: {']; | ||
const chunk2 = ['}', 'data: [DONE]']; | ||
|
||
respondWithChunks({ chunks: [chunk1, chunk2] }); | ||
|
||
const response$ = await chat(); | ||
|
||
const results: any = []; | ||
response$.subscribe({ | ||
next: (data) => results.push(data), | ||
complete: () => { | ||
expect(results).toHaveLength(2); | ||
}, | ||
}); | ||
}); | ||
|
||
it('propagates invalid requests as an error', () => { | ||
respondWithChunks({ status: 400, chunks: [] }); | ||
|
||
expect(() => chat()).rejects.toThrowErrorMatchingInlineSnapshot(`"Unexpected error"`); | ||
}); | ||
|
||
it('propagates JSON parsing errors', async () => { | ||
const chunk1 = ['data: {}', 'data: invalid json']; | ||
|
||
respondWithChunks({ chunks: [chunk1] }); | ||
|
||
const response$ = await chat(); | ||
|
||
response$.subscribe({ | ||
error: (err) => { | ||
expect(err).toBeInstanceOf(SyntaxError); | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/observability_ai_assistant/public/service/create_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { CoreStart, HttpResponse } from '@kbn/core/public'; | ||
import { filter, map } from 'rxjs'; | ||
import type { Message } from '../../common'; | ||
import { createCallObservabilityAIAssistantAPI } from '../api'; | ||
import { CreateChatCompletionResponseChunk, ObservabilityAIAssistantService } from '../types'; | ||
import { readableStreamReaderIntoObservable } from '../utils/readable_stream_reader_into_observable'; | ||
|
||
export function createService(coreStart: CoreStart): ObservabilityAIAssistantService { | ||
const client = createCallObservabilityAIAssistantAPI(coreStart); | ||
|
||
return { | ||
isEnabled: () => { | ||
return true; | ||
}, | ||
async chat({ | ||
connectorId, | ||
messages, | ||
signal, | ||
}: { | ||
connectorId: string; | ||
messages: Message[]; | ||
signal: AbortSignal; | ||
}) { | ||
const response = (await client('POST /internal/observability_ai_assistant/chat', { | ||
params: { | ||
body: { | ||
messages, | ||
connectorId, | ||
}, | ||
}, | ||
signal, | ||
asResponse: true, | ||
rawResponse: true, | ||
})) as unknown as HttpResponse; | ||
|
||
const status = response.response?.status; | ||
|
||
if (!status || status >= 400) { | ||
throw new Error(response.response?.statusText || 'Unexpected error'); | ||
} | ||
|
||
const reader = response.response.body?.getReader(); | ||
|
||
if (!reader) { | ||
throw new Error('Could not get reader from response'); | ||
} | ||
|
||
return readableStreamReaderIntoObservable(reader).pipe( | ||
map((line) => line.substring(6)), | ||
filter((line) => !!line && line !== '[DONE]'), | ||
map((line) => JSON.parse(line) as CreateChatCompletionResponseChunk) | ||
); | ||
}, | ||
callApi: client, | ||
}; | ||
} |