Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ describe('Observability AI Assistant client', () => {
getInstructions: jest.fn(),
} as any;

const getConnectorByIdMock = jest.fn().mockResolvedValue({
connectorId: 'test-connector-id',
name: 'Test Connector',
type: 'openai',
});

const analyticsMock = { reportEvent: jest.fn() } as unknown as AnalyticsServiceStart;

let llmSimulator: LlmSimulator;
Expand Down Expand Up @@ -194,6 +200,12 @@ describe('Observability AI Assistant client', () => {
isSystemAction: false,
} as any);

getConnectorByIdMock.mockResolvedValue({
connectorId: 'test-connector-id',
name: 'Test Connector',
type: 'openai',
});

return new ObservabilityAIAssistantClient({
config: {} as ObservabilityAIAssistantConfig,
core: {} as CoreSetup<ObservabilityAIAssistantPluginStartDependencies>,
Expand All @@ -203,6 +215,7 @@ describe('Observability AI Assistant client', () => {
asInternalUser: internalUserEsClientMock,
asCurrentUser: currentUserEsClientMock,
},
getConnectorById: getConnectorByIdMock,
inferenceClient: inferenceClientMock,
knowledgeBaseService: knowledgeBaseServiceMock,
logger: loggerMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
ChatCompleteResponse,
FunctionCallingMode,
InferenceClient,
InferenceConnector,
} from '@kbn/inference-common';
import { ToolChoiceType } from '@kbn/inference-common';
import type { AnalyticsServiceStart } from '@kbn/core/server';
Expand Down Expand Up @@ -99,6 +100,7 @@ export class ObservabilityAIAssistantClient {
asInternalUser: ElasticsearchClient;
asCurrentUser: ElasticsearchClient;
};
getConnectorById: (connectorId: string) => Promise<InferenceConnector>;
inferenceClient: InferenceClient;
logger: Logger;
user?: {
Expand Down Expand Up @@ -281,12 +283,7 @@ export class ObservabilityAIAssistantClient {
);

const connector$ = defer(() =>
from(
this.dependencies.actionsClient.get({
id: connectorId,
throwIfSystemAction: true,
})
).pipe(
from(this.dependencies.getConnectorById(connectorId)).pipe(
catchError((error) => {
this.dependencies.logger.debug(
`Failed to fetch connector for analytics: ${error.message}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { firstValueFrom } from 'rxjs';
import type { AnalyticsServiceStart } from '@kbn/core/server';
import type { Connector } from '@kbn/actions-plugin/server';
import type { Logger } from '@kbn/logging';
import type { InferenceConnector } from '@kbn/inference-common';
import { InferenceConnectorType } from '@kbn/inference-common';
import { MessageRole, type Message } from '../../../../common';
import { toolCallEventType } from '../../../analytics/tool_call';
import { executeFunctionAndCatchError } from './continue_conversation';
import { createMockConnector } from '@kbn/actions-plugin/server/application/connector/mocks';

const testMessages: Message[] = [
{
Expand All @@ -21,12 +21,15 @@ const testMessages: Message[] = [
},
];

const mockConnector: Connector = createMockConnector({
id: 'test-connector-id',
const mockConnector: InferenceConnector = {
connectorId: 'test-connector-id',
name: 'Test GenAI Connector',
actionTypeId: '.gen-ai',
type: InferenceConnectorType.OpenAI,
config: { apiProvider: 'OpenAI' },
});
capabilities: {},
isInferenceEndpoint: false,
isPreconfigured: false,
};

describe('executeFunctionAndCatchError', () => {
const mockLogger = { error: jest.fn(), debug: jest.fn(), trace: jest.fn() } as unknown as Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import {
import { withExecuteToolSpan } from '@kbn/inference-tracing';
import { createToolNotFoundError } from '@kbn/inference-plugin/common/chat_complete/errors';
import type { AnalyticsServiceStart } from '@kbn/core/server';
import type { Connector } from '@kbn/actions-plugin/server';
import type { AssistantScope } from '@kbn/ai-assistant-common';
import { isToolValidationError } from '@kbn/inference-common';
import { type InferenceConnector, isToolValidationError } from '@kbn/inference-common';
import { getInferenceConnectorInfo } from '../../../../common/utils/get_inference_connector';
import type { ToolCallEvent } from '../../../analytics/tool_call';
import { toolCallEventType } from '../../../analytics/tool_call';
Expand Down Expand Up @@ -76,7 +75,7 @@ export function executeFunctionAndCatchError({
connectorId: string;
simulateFunctionCalling: boolean;
analytics: AnalyticsServiceStart;
connector?: Connector;
connector?: InferenceConnector;
scopes: AssistantScope[];
}): Observable<MessageOrChatEvent> {
return withExecuteToolSpan(name, { tool: { input: args } }, (span) => {
Expand All @@ -103,9 +102,10 @@ export function executeFunctionAndCatchError({

return executeFunctionResponse$.pipe(
tap(() => {
const connectorInfo = getInferenceConnectorInfo(connector);
analytics.reportEvent<ToolCallEvent>(toolCallEventType, {
toolName: name,
connector: getInferenceConnectorInfo(connector),
connector: connectorInfo,
scopes,
});
}),
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Expand Down Expand Up @@ -240,7 +240,7 @@ export function continueConversation({
connectorId: string;
simulateFunctionCalling: boolean;
analytics: AnalyticsServiceStart;
connector?: Connector;
connector?: InferenceConnector;
scopes: AssistantScope[];
}): Observable<MessageOrChatEvent> {
let nextFunctionCallsLeft = functionCallsLeft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,20 @@ export class ObservabilityAIAssistantService {
productDoc: plugins.productDocBase.management,
});

const actionsClient = await plugins.actions.getActionsClientWithRequest(request);

return new ObservabilityAIAssistantClient({
core: this.core,
config: this.config,
actionsClient: await plugins.actions.getActionsClientWithRequest(request),
actionsClient,
uiSettingsClient,
namespace: spaceId,
esClient: {
asInternalUser,
asCurrentUser,
},
getConnectorById: (id: string) =>
plugins.inference.getConnectorByIdWithoutClientRequest(id, actionsClient, asInternalUser),
inferenceClient,
logger: this.logger,
user: user
Expand Down
Loading