Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI Assistant] Set scope and rename to Observability and Search #196322

Merged
Merged
Show file tree
Hide file tree
Changes from all 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) => [...scopeFilters, 'all'].includes(scope))
: true;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { euiThemeVars } from '@kbn/ui-theme';
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import type { AssistantScope } from '@kbn/ai-assistant-common';
import { isEqual } from 'lodash';
import { useKibana } from '../hooks/use_kibana';
import { ConversationList, ChatBody, ChatInlineEditingContent } from '../chat';
import { useConversationKey } from '../hooks/use_conversation_key';
Expand All @@ -27,15 +28,15 @@ interface ConversationViewProps {
navigateToConversation: (nextConversationId?: string) => void;
getConversationHref?: (conversationId: string) => string;
newConversationHref?: string;
scope?: AssistantScope;
scopes?: AssistantScope[];
}

export const ConversationView: React.FC<ConversationViewProps> = ({
conversationId,
navigateToConversation,
getConversationHref,
newConversationHref,
scope,
scopes,
}) => {
const { euiTheme } = useEuiTheme();

Expand All @@ -61,10 +62,10 @@ export const ConversationView: React.FC<ConversationViewProps> = ({
);

useEffect(() => {
if (scope) {
service.setScope(scope);
if (scopes && !isEqual(scopes, service.getScopes())) {
service.setScopes(scopes);
}
}, [scope, service]);
}, [scopes, service]);
sphilipse marked this conversation as resolved.
Show resolved Hide resolved

const { key: bodyKey, updateConversationIdInPlace } = useConversationKey(conversationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export function useConversation() {
stop: () => {},
messages: [],
saveTitle: () => {},
scope: 'all',
scopes: ['all'],
};
}
2 changes: 1 addition & 1 deletion x-pack/packages/kbn-ai-assistant/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
export * from './use_ai_assistant_app_service';
export * from './use_ai_assistant_chat_service';
export * from './use_knowledge_base';
export * from './use_scope';
export * from './use_scopes';
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ const mockService: MockedService = {
predefinedConversation$: new Observable(),
},
navigate: jest.fn().mockReturnValue(of()),
scope$: new BehaviorSubject<AssistantScope>('all') as MockedService['scope$'],
setScope: jest.fn(),
getScope: jest.fn(),
scope$: new BehaviorSubject<AssistantScope[]>(['all']) as MockedService['scope$'],
setScopes: jest.fn(),
getScopes: jest.fn(),
};

const mockChatService = createMockChatService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useAIAssistantAppService } from './use_ai_assistant_app_service';
import { useKibana } from './use_kibana';
import { useOnce } from './use_once';
import { useAbortableAsync } from './use_abortable_async';
import { useScope } from './use_scope';
import { useScopes } from './use_scopes';

function createNewConversation({
title = EMPTY_CONVERSATION_TITLE,
Expand Down Expand Up @@ -62,7 +62,7 @@ export function useConversation({
onConversationUpdate,
}: UseConversationProps): UseConversationResult {
const service = useAIAssistantAppService();
const scope = useScope();
const scopes = useScopes();

const {
services: {
Expand Down Expand Up @@ -122,7 +122,7 @@ export function useConversation({
onConversationUpdate?.({ conversation: event.conversation });
},
persist: true,
scope,
scopes,
});

const [displayedConversationId, setDisplayedConversationId] = useState(initialConversationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { useObservable } from 'react-use/lib';
import { useAIAssistantAppService } from './use_ai_assistant_app_service';

export const useScope = () => {
export const useScopes = () => {
const service = useAIAssistantAppService();
const scope = useObservable(service.scope$);
return scope || 'all';
const scopes = useObservable(service.scope$);
return scopes || ['all'];
};
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 @@ -164,7 +164,6 @@ export function registerGetApmDatasetInfoFunction({
`,
},
};
},
['observability']
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export function registerGetApmDownstreamDependenciesFunction({
randomSampler,
}),
};
},
['observability']
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function registerGetApmServicesListFunction({
arguments: args,
}),
};
},
['observability']
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ export function registerGetApmTimeseriesFunction({
content: timeseries.map((series): Omit<ApmTimeseries, 'data'> => omit(series, 'data')),
data: timeseries,
};
},
['observability']
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export function registerAssistantFunctions({
ruleDataClient: IRuleDataClient;
plugins: APMRouteHandlerResources['plugins'];
}): RegistrationCallback {
return async ({ resources, functions: { registerFunction } }) => {
return async ({ resources, functions: { registerFunction }, scopes }) => {
if (!scopes.includes('observability')) {
return;
}
const apmRouteHandlerResources: MinimalAPMRouteHandlerResources = {
context: resources.context,
request: resources.request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
import type { JSONSchema7TypeName } from 'json-schema';
import type { Observable } from 'rxjs';
import type { AssistantScope } from '@kbn/ai-assistant-common';
import { ChatCompletionChunkEvent, MessageAddEvent } from '../conversation_complete';
import { FunctionVisibility } from './function_visibility';
export { FunctionVisibility };
Expand Down Expand Up @@ -42,7 +41,6 @@ export interface FunctionDefinition<TParameters extends CompatibleJSONSchema = a
visibility?: FunctionVisibility;
descriptionForUser?: string;
parameters?: TParameters;
scopes?: AssistantScope[];
}

export type FunctionRegistry = Map<string, FunctionDefinition>;
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
Loading