From 50c7456ded31e37cb72302c498b03291bb218c02 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:48:47 +1100 Subject: [PATCH] [8.x] [AI Assistant] Add scopes telemetry to AI Assistant events (#197983) (#198360) # Backport This will backport the following commits from `main` to `8.x`: - [[AI Assistant] Add scopes telemetry to AI Assistant events (#197983)](https://github.com/elastic/kibana/pull/197983) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Sander Philipse <94373878+sphilipse@users.noreply.github.com> --- .../src/prompt_editor/prompt_editor.tsx | 7 ++++--- .../public/analytics/index.ts | 6 +++++- .../public/analytics/schemas/common.ts | 12 +++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/x-pack/packages/kbn-ai-assistant/src/prompt_editor/prompt_editor.tsx b/x-pack/packages/kbn-ai-assistant/src/prompt_editor/prompt_editor.tsx index cc2fe761d6176..c848935c17a3a 100644 --- a/x-pack/packages/kbn-ai-assistant/src/prompt_editor/prompt_editor.tsx +++ b/x-pack/packages/kbn-ai-assistant/src/prompt_editor/prompt_editor.tsx @@ -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; @@ -42,6 +43,7 @@ export function PromptEditor({ onSendTelemetry, onSubmit, }: PromptEditorProps) { + const scopes = useScopes(); const containerRef = useRef(null); const [mode, setMode] = useState<'prompt' | 'function'>( @@ -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(() => { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/index.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/index.ts index 86b8f14bde9e4..049c5a1e65fb0 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/index.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/index.ts @@ -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'; @@ -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) => { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/schemas/common.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/schemas/common.ts index b01a8e05a4ea5..4a2739ef82c35 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/schemas/common.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/public/analytics/schemas/common.ts @@ -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 = { +export const messageSchema: RootSchema = { '@timestamp': { type: 'text', _meta: { @@ -74,4 +75,13 @@ export const messageSchema: RootSchema = { }, }, }, + scopes: { + type: 'array', + items: { + type: 'text', + _meta: { + description: 'The scopes that were used when generating the message.', + }, + }, + }, };