Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
83f2b19
[AI Assistant] Set scope and rename to Observability and Search
sphilipse Oct 15, 2024
e9da40a
[CI] Auto-commit changed files from 'node scripts/lint_packages --fix'
kibanamachine Oct 15, 2024
8f84195
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Oct 15, 2024
0fed35f
Remove scope UI setting and set default to all
sphilipse Oct 16, 2024
9130251
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Oct 16, 2024
3b090bc
Make scopes multi-sided on both sides
sphilipse Oct 16, 2024
89b15b0
Filter functions based on required screencontexts as well
sphilipse Oct 16, 2024
9cece08
Fix FTRs
sphilipse Oct 16, 2024
34a5ed0
Remove silly test value from code
sphilipse Oct 16, 2024
5565690
Update kbn ai assistant package with scopes
sphilipse Oct 16, 2024
5f99a45
Fix serverless FTRs
sphilipse Oct 16, 2024
7962241
Fix ES function calling FTR
sphilipse Oct 16, 2024
d768314
fix scopes in more places
sphilipse Oct 17, 2024
7d122e7
Merge branch 'main' into search-assistant-set-scope-ui-setting
sphilipse Oct 17, 2024
3603216
Remove scopes from functiondefinition
sphilipse Oct 18, 2024
2e2bdb9
Make special case for system prompts
sphilipse Oct 18, 2024
08e29c1
Remove unused functionsubscription
sphilipse Oct 18, 2024
2e5b00d
Remove unused functionsubscription
sphilipse Oct 18, 2024
d947279
Remove scopes from registering instruction
sphilipse Oct 18, 2024
224004b
Merge branch 'main' into search-assistant-set-scope-ui-setting
sphilipse Oct 21, 2024
acdc9c8
Remove scope from function registration, use it as a parameter instead
sphilipse Oct 21, 2024
fdefbba
Move function scopes to functions themselves
sphilipse Oct 21, 2024
2902679
Fix test type
sphilipse Oct 21, 2024
7f23587
Fix apm functions
sphilipse Oct 21, 2024
b4b4373
Merge branch 'main' into search-assistant-set-scope-ui-setting
sphilipse Oct 22, 2024
ce0d1ba
Merge branch 'main' into search-assistant-set-scope-ui-setting
sphilipse Oct 23, 2024
117e97e
Merge branch 'main' into search-assistant-set-scope-ui-setting
sphilipse Oct 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ Elastic.


|{kib-repo}blob/{branch}/x-pack/plugins/observability_solution/observability_ai_assistant_management/README.md[observabilityAiAssistantManagement]
|The observabilityAiAssistantManagement plugin manages the Ai Assistant for Observability management section.
|The observabilityAiAssistantManagement plugin manages the Ai Assistant for Observability and Search management section.


|{kib-repo}blob/{branch}/x-pack/plugins/observability_solution/observability_logs_explorer/README.md[observabilityLogsExplorer]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function AiAssistantSelectionPage() {
isDisabled={!observabilityAIAssistantEnabled}
title={i18n.translate(
'aiAssistantManagementSelection.aiAssistantSelectionPage.observabilityLabel',
{ defaultMessage: 'Elastic AI Assistant for Observability' }
{ defaultMessage: 'Elastic AI Assistant for Observability and Search' }
)}
titleSize="xs"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export class AIAssistantManagementSelectionPlugin
core.uiSettings.register({
[PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY]: {
name: i18n.translate('aiAssistantManagementSelection.preferredAIAssistantTypeSettingName', {
defaultMessage: 'Observability AI Assistant scope',
defaultMessage: 'AI Assistant for Observability and Search visibility',
}),
category: [DEFAULT_APP_CATEGORIES.observability.id],
value: this.config.preferredAIAssistantType,
description: i18n.translate(
'aiAssistantManagementSelection.preferredAIAssistantTypeSettingDescription',
{
defaultMessage:
'<em>[technical preview]</em> Whether to show the Observability AI Assistant menu item in Observability, everywhere, or nowhere.',
'<em>[technical preview]</em> Whether to show the AI Assistant menu item in Observability and Search, everywhere, or nowhere.',
values: {
em: (chunks) => `<em>${chunks}</em>`,
},
Expand All @@ -77,7 +77,7 @@ export class AIAssistantManagementSelectionPlugin
optionLabels: {
[AIAssistantType.Default]: i18n.translate(
'aiAssistantManagementSelection.preferredAIAssistantTypeSettingValueDefault',
{ defaultMessage: 'Observability only (default)' }
{ defaultMessage: 'Observability and Search only (default)' }
),
[AIAssistantType.Observability]: i18n.translate(
'aiAssistantManagementSelection.preferredAIAssistantTypeSettingValueObservability',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

import type { AssistantScope } from '../types';

export function filterScopes<T extends { scopes?: AssistantScope[] }>(scope?: AssistantScope) {
export function filterScopes<T extends { scopes?: AssistantScope[] }>(
scopeFilters?: AssistantScope[]
) {
return function (value: T): boolean {
if (!scope || !value) {
if (!scopeFilters || !value) {
return true;
}
return value?.scopes ? value.scopes.includes(scope) || value.scopes.includes('all') : true;
return value?.scopes
? value.scopes.some((scope) => scope === 'all' || scopeFilters.includes(scope))
: true;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const createMockChatService = (): MockedChatService => {
content: '',
},
}),
getScope: jest.fn(),
getScopes: jest.fn(),
};
return mockChatService;
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function ChatContent({
}) {
const service = useObservabilityAIAssistant();
const chatService = useObservabilityAIAssistantChatService();
const scope = chatService.getScope();
const scopes = chatService.getScopes();

const initialMessagesRef = useRef(initialMessages);

Expand All @@ -69,7 +69,7 @@ function ChatContent({
initialMessages,
persist: false,
disableFunctions: true,
scope,
scopes,
});

const lastAssistantResponse = getLastMessageOfType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const mockChatService: MockedChatService = {
role: MessageRole.System,
},
}),
getScope: jest.fn(),
getScopes: jest.fn(),
};

const addErrorMock = jest.fn();
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('useChat', () => {
service: {
getScreenContexts: () => [],
} as unknown as ObservabilityAIAssistantService,
scope: 'observability',
scopes: ['observability'],
} as UseChatProps,
});
});
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('useChat', () => {
service: {
getScreenContexts: () => [],
} as unknown as ObservabilityAIAssistantService,
scope: 'observability',
scopes: ['observability'],
} as UseChatProps,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface UseChatPropsWithoutContext {
disableFunctions?: boolean;
onConversationUpdate?: (event: ConversationCreateEvent | ConversationUpdateEvent) => void;
onChatComplete?: (messages: Message[]) => void;
scope: AssistantScope;
scopes: AssistantScope[];
}

export type UseChatProps = Omit<UseChatPropsWithoutContext, 'notifications'>;
Expand All @@ -72,7 +72,7 @@ function useChatWithoutContext({
onChatComplete,
persist,
disableFunctions,
scope,
scopes,
}: UseChatPropsWithoutContext): UseChatResult {
const [chatState, setChatState] = useState(ChatState.Ready);
const systemMessage = useMemo(() => {
Expand Down Expand Up @@ -165,7 +165,7 @@ function useChatWithoutContext({
disableFunctions: disableFunctions ?? false,
signal: abortControllerRef.current.signal,
conversationId,
scope,
scopes,
});

function getPendingMessages() {
Expand Down Expand Up @@ -264,7 +264,7 @@ function useChatWithoutContext({
disableFunctions,
service,
systemMessage,
scope,
scopes,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const mockChatService: ObservabilityAIAssistantChatService = {
content: 'System',
},
}),
getScope: jest.fn(),
getScopes: jest.fn(),
};

export const mockService: ObservabilityAIAssistantService = {
Expand All @@ -64,9 +64,9 @@ export const mockService: ObservabilityAIAssistantService = {
predefinedConversation$: new Observable(),
},
navigate: async () => of(),
setScope: jest.fn(),
getScope: jest.fn(),
scope$: new BehaviorSubject<AssistantScope>('all'),
setScopes: jest.fn(),
getScopes: jest.fn(),
scope$: new BehaviorSubject<AssistantScope[]>(['all']),
};

function createSetupContract(): ObservabilityAIAssistantPublicSetup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ObservabilityAIAssistantPlugin
coreStart.application.capabilities.observabilityAIAssistant[
aiAssistantCapabilities.show
] === true,
scope: this.scopeFromConfig || 'observability',
scopes: this.scopeFromConfig ? [this.scopeFromConfig] : ['all'],
scopeIsMutable: !!this.scopeFromConfig,
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('complete', () => {
disableFunctions: false,
signal: new AbortController().signal,
...params,
scope: 'all',
scopes: ['all'],
},
requestCallback
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function complete(
disableFunctions,
signal,
instructions,
scope,
scopes,
}: {
client: Pick<ObservabilityAIAssistantChatService, 'chat' | 'complete'>;
getScreenContexts: () => ObservabilityAIAssistantScreenContext[];
Expand All @@ -66,7 +66,7 @@ export function complete(
screenContexts,
conversationId,
instructions,
scope,
scopes,
},
},
}).pipe(shareReplay());
Expand Down Expand Up @@ -133,7 +133,7 @@ export function complete(
persist,
disableFunctions,
instructions,
scope,
scopes,
},
requestCallback
).subscribe(subscriber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { concatenateChatCompletionChunks } from '../../common/utils/concatenate_
import type { ObservabilityAIAssistantChatService } from '../types';
import { createChatService } from './create_chat_service';
import { AssistantScope } from '@kbn/ai-assistant-common';
import { ObservabilityAIAssistantScreenContext } from '../../common/types';

async function getConcatenatedMessage(
response$: Observable<StreamingChatResponseEventWithoutError>
Expand Down Expand Up @@ -71,7 +72,8 @@ describe('createChatService', () => {
apiClient: clientSpy,
registrations: [],
signal: new AbortController().signal,
scope$: new BehaviorSubject<AssistantScope>('observability'),
scope$: new BehaviorSubject<AssistantScope[]>(['observability']),
screenContexts$: new BehaviorSubject<ObservabilityAIAssistantScreenContext[]>([]),
});
});

Expand All @@ -85,7 +87,7 @@ describe('createChatService', () => {
signal,
messages: [],
connectorId: '',
scope: 'observability',
scopes: ['observability'],
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from 'rxjs';
import { BehaviorSubject } from 'rxjs';
import type { AssistantScope } from '@kbn/ai-assistant-common';
import { ObservabilityAIAssistantScreenContext } from '../../common/types';
import { ChatCompletionChunkEvent, Message, MessageRole } from '../../common';
import {
StreamingChatResponseEventType,
Expand Down Expand Up @@ -144,24 +145,27 @@ class ChatService {
private renderFunctionRegistry: Map<string, RenderFunction<unknown, FunctionResponse>>;
private abortSignal: AbortSignal;
private apiClient: ObservabilityAIAssistantAPIClient;
public scope$: BehaviorSubject<AssistantScope>;
public scope$: BehaviorSubject<AssistantScope[]>;
private analytics: AnalyticsServiceStart;
private registrations: ChatRegistrationRenderFunction[];
private systemMessage: string;
public functions$: BehaviorSubject<FunctionDefinition[]>;
private screenContexts$: BehaviorSubject<ObservabilityAIAssistantScreenContext[]>;

constructor({
abortSignal,
apiClient,
scope$,
analytics,
registrations,
screenContexts$,
}: {
abortSignal: AbortSignal;
apiClient: ObservabilityAIAssistantAPIClient;
scope$: BehaviorSubject<AssistantScope>;
scope$: BehaviorSubject<AssistantScope[]>;
analytics: AnalyticsServiceStart;
registrations: ChatRegistrationRenderFunction[];
screenContexts$: BehaviorSubject<ObservabilityAIAssistantScreenContext[]>;
}) {
this.functionRegistry = new Map();
this.renderFunctionRegistry = new Map();
Expand All @@ -171,6 +175,7 @@ class ChatService {
this.analytics = analytics;
this.registrations = registrations;
this.systemMessage = '';
this.screenContexts$ = screenContexts$;
this.functions$ = new BehaviorSubject([] as FunctionDefinition[]);
scope$.subscribe(() => {
this.initialize();
Expand All @@ -186,15 +191,25 @@ class ChatService {

async initialize() {
this.functionRegistry = new Map();
const [{ functionDefinitions, systemMessage }] = await Promise.all([
this.apiClient('GET /internal/observability_ai_assistant/{scope}/functions', {
signal: this.abortSignal,
params: {
path: {
scope: this.getScope(),
},
const systemMessages: string[] = [];
const requiredFunctions = this.screenContexts$.value.flatMap(
(context) => context.actions?.flatMap((action) => action.parameters?.required || []) || []
);
const scopePromise = this.apiClient('GET /internal/observability_ai_assistant/functions', {
signal: this.abortSignal,
params: {
query: {
scopes: this.getScopes(),
requiredFunctions: ['query', 'beer'],
},
}),
},
}).then(({ functionDefinitions, systemMessage }) => {
functionDefinitions.forEach((fn) => this.functionRegistry.set(fn.name, fn));
systemMessages.push(systemMessage);
});

await Promise.all([
scopePromise,
...this.registrations.map((registration) => {
return registration({
registerRenderFunction: (name, renderFn) => {
Expand All @@ -204,10 +219,7 @@ class ChatService {
}),
]);

functionDefinitions.forEach((fn) => {
this.functionRegistry.set(fn.name, fn);
});
this.systemMessage = systemMessage;
this.systemMessage = systemMessages.join('\n');

this.functions$.next(this.getFunctions());
}
Expand Down Expand Up @@ -251,7 +263,7 @@ class ChatService {
definitions: Array.from(this.functionRegistry.values()),
}).filter((value) => {
return value.scopes
? value.scopes?.includes(this.getScope()) || value.scopes?.includes('all')
? value.scopes?.some((scope) => scope === 'all' || this.getScopes().includes(scope))
: true;
});
};
Expand Down Expand Up @@ -301,7 +313,7 @@ class ChatService {
connectorId,
functionCall,
functions: functions ?? [],
scope: this.getScope(),
scopes: this.getScopes(),
},
},
signal,
Expand Down Expand Up @@ -334,7 +346,7 @@ class ChatService {
signal,
client: this.getClient(),
instructions,
scope: this.getScope(),
scopes: this.getScopes(),
},
({ params }) => {
return this.callStreamingApi('POST /internal/observability_ai_assistant/chat/complete', {
Expand All @@ -345,7 +357,7 @@ class ChatService {
);
};

public getScope() {
public getScopes() {
return this.scope$.value;
}
}
Expand All @@ -356,18 +368,21 @@ export async function createChatService({
registrations,
apiClient,
scope$,
screenContexts$,
}: {
analytics: AnalyticsServiceStart;
signal: AbortSignal;
registrations: ChatRegistrationRenderFunction[];
apiClient: ObservabilityAIAssistantAPIClient;
scope$: BehaviorSubject<AssistantScope>;
scope$: BehaviorSubject<AssistantScope[]>;
screenContexts$: BehaviorSubject<ObservabilityAIAssistantScreenContext[]>;
}): Promise<ObservabilityAIAssistantChatService> {
return new ChatService({
analytics,
apiClient,
scope$,
registrations,
abortSignal: setupAbortSignal,
screenContexts$,
});
}
Loading