Skip to content

Commit

Permalink
[8.x] [AI Assistant] Add scopes telemetry to AI Assistant events (#19…
Browse files Browse the repository at this point in the history
…7983) (#198360)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[AI Assistant] Add scopes telemetry to AI Assistant events
(#197983)](#197983)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Sander
Philipse","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-30T14:04:23Z","message":"[AI
Assistant] Add scopes telemetry to AI Assistant events (#197983)\n\n##
Summary\r\n\r\nThis adds telemetry to the Observability and Search
assistant to judge\r\nwhether it's search or
observability.","sha":"74cf0e4e2bd386ae38a6b8c537545acbd1707022","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Search","Team:Obs
AI
Assistant","ci:project-deploy-observability","v8.16.0","backport:version","v8.17.0"],"title":"[AI
Assistant] Add scopes telemetry to AI Assistant
events","number":197983,"url":"https://github.com/elastic/kibana/pull/197983","mergeCommit":{"message":"[AI
Assistant] Add scopes telemetry to AI Assistant events (#197983)\n\n##
Summary\r\n\r\nThis adds telemetry to the Observability and Search
assistant to judge\r\nwhether it's search or
observability.","sha":"74cf0e4e2bd386ae38a6b8c537545acbd1707022"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197983","number":197983,"mergeCommit":{"message":"[AI
Assistant] Add scopes telemetry to AI Assistant events (#197983)\n\n##
Summary\r\n\r\nThis adds telemetry to the Observability and Search
assistant to judge\r\nwhether it's search or
observability.","sha":"74cf0e4e2bd386ae38a6b8c537545acbd1707022"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sander Philipse <[email protected]>
  • Loading branch information
kibanamachine and sphilipse authored Oct 30, 2024
1 parent 00b51bd commit 50c7456
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useLastUsedPrompts } from '../hooks/use_last_used_prompts';
import { FunctionListPopover } from '../chat/function_list_popover';
import { PromptEditorFunction } from './prompt_editor_function';
import { PromptEditorNaturalLanguage } from './prompt_editor_natural_language';
import { useScopes } from '../hooks/use_scopes';

export interface PromptEditorProps {
disabled: boolean;
Expand All @@ -42,6 +43,7 @@ export function PromptEditor({
onSendTelemetry,
onSubmit,
}: PromptEditorProps) {
const scopes = useScopes();
const containerRef = useRef<HTMLDivElement>(null);

const [mode, setMode] = useState<'prompt' | 'function'>(
Expand Down Expand Up @@ -121,16 +123,15 @@ export function PromptEditor({

setInnerMessage(undefined);
setMode('prompt');

onSendTelemetry({
type: ObservabilityAIAssistantTelemetryEventType.UserSentPromptInChat,
payload: message,
payload: { ...message, scopes },
});
} catch (_) {
setInnerMessage(oldMessage);
setMode(oldMessage.function_call?.name ? 'function' : 'prompt');
}
}, [addLastUsedPrompt, innerMessage, loading, onSendTelemetry, onSubmit]);
}, [addLastUsedPrompt, innerMessage, loading, onSendTelemetry, onSubmit, scopes]);

// Submit on Enter
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { AnalyticsServiceSetup, AnalyticsServiceStart } from '@kbn/core-analytics-browser';
import { AssistantScope } from '@kbn/ai-assistant-common';
import type { Message } from '../../common';
import { chatFeedbackEventSchema, ChatFeedback } from './schemas/chat_feedback';
import { insightFeedbackEventSchema, InsightFeedback } from './schemas/insight_feedback';
Expand All @@ -17,7 +18,10 @@ const schemas = [chatFeedbackEventSchema, insightFeedbackEventSchema, userSentPr
export type TelemetryEventTypeWithPayload =
| { type: ObservabilityAIAssistantTelemetryEventType.ChatFeedback; payload: ChatFeedback }
| { type: ObservabilityAIAssistantTelemetryEventType.InsightFeedback; payload: InsightFeedback }
| { type: ObservabilityAIAssistantTelemetryEventType.UserSentPromptInChat; payload: Message };
| {
type: ObservabilityAIAssistantTelemetryEventType.UserSentPromptInChat;
payload: Message & { scopes: AssistantScope[] };
};

export const registerTelemetryEventTypes = (analytics: AnalyticsServiceSetup) => {
schemas.forEach((schema) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

import type { RootSchema } from '@kbn/core/public';
import { AssistantScope } from '@kbn/ai-assistant-common';
import type { Message } from '../../../common';

export const messageSchema: RootSchema<Message> = {
export const messageSchema: RootSchema<Message & { scopes: AssistantScope[] }> = {
'@timestamp': {
type: 'text',
_meta: {
Expand Down Expand Up @@ -74,4 +75,13 @@ export const messageSchema: RootSchema<Message> = {
},
},
},
scopes: {
type: 'array',
items: {
type: 'text',
_meta: {
description: 'The scopes that were used when generating the message.',
},
},
},
};

0 comments on commit 50c7456

Please sign in to comment.