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
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ x-pack/platform/plugins/private/index_lifecycle_management @elastic/kibana-manag
x-pack/platform/plugins/shared/index_management @elastic/kibana-management
x-pack/platform/packages/shared/index-management/index_management_shared_types @elastic/kibana-management
src/platform/test/plugin_functional/plugins/index_patterns @elastic/kibana-data-discovery
x-pack/platform/packages/shared/kbn-inference-cli @elastic/appex-ai-infra
x-pack/platform/packages/shared/ai-infra/inference-common @elastic/appex-ai-infra
x-pack/platform/plugins/shared/inference_endpoint @elastic/ml-ui
x-pack/platform/packages/shared/kbn-inference-endpoint-ui-common @elastic/response-ops @elastic/appex-ai-infra @elastic/obs-ai-assistant @elastic/security-generative-ai
Expand All @@ -568,12 +569,14 @@ src/platform/packages/private/kbn-jest-serializers @elastic/kibana-operations
src/platform/packages/private/kbn-journeys @elastic/kibana-operations @elastic/appex-qa
packages/kbn-json-ast @elastic/kibana-operations
x-pack/platform/packages/private/ml/json_schemas @elastic/ml-ui
x-pack/solutions/observability/packages/kbn-genai-cli @elastic/obs-knowledge-team
src/platform/test/health_gateway/plugins/status @elastic/kibana-core
src/platform/test/plugin_functional/plugins/kbn_sample_panel_action @elastic/appex-sharedux
src/platform/test/plugin_functional/plugins/kbn_top_nav @elastic/kibana-core
src/platform/test/plugin_functional/plugins/kbn_tp_custom_visualizations @elastic/kibana-visualizations
src/platform/test/interpreter_functional/plugins/kbn_tp_run_pipeline @elastic/kibana-core
x-pack/platform/packages/shared/kbn-key-value-metadata-table @elastic/obs-ux-infra_services-team @elastic/obs-ux-logs-team
x-pack/platform/packages/shared/kbn-kibana-api-cli @elastic/appex-ai-infra
x-pack/test/functional_cors/plugins/kibana_cors_test @elastic/kibana-security
packages/kbn-kibana-manifest-schema @elastic/kibana-operations
src/platform/plugins/private/kibana_overview @elastic/appex-sharedux
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@
"@kbn/kbn-tp-custom-visualizations-plugin": "link:src/platform/test/plugin_functional/plugins/kbn_tp_custom_visualizations",
"@kbn/kbn-tp-run-pipeline-plugin": "link:src/platform/test/interpreter_functional/plugins/kbn_tp_run_pipeline",
"@kbn/key-value-metadata-table": "link:x-pack/platform/packages/shared/kbn-key-value-metadata-table",
"@kbn/kibana-api-cli": "link:x-pack/platform/packages/shared/kbn-kibana-api-cli",
"@kbn/kibana-cors-test-plugin": "link:x-pack/test/functional_cors/plugins/kibana_cors_test",
"@kbn/kibana-overview-plugin": "link:src/platform/plugins/private/kibana_overview",
"@kbn/kibana-react-plugin": "link:src/platform/plugins/shared/kibana_react",
Expand Down Expand Up @@ -1471,10 +1472,12 @@
"@kbn/get-repo-files": "link:src/platform/packages/private/kbn-get-repo-files",
"@kbn/import-locator": "link:packages/kbn-import-locator",
"@kbn/import-resolver": "link:src/platform/packages/private/kbn-import-resolver",
"@kbn/inference-cli": "link:x-pack/platform/packages/shared/kbn-inference-cli",
"@kbn/inventory-e2e": "link:x-pack/solutions/observability/plugins/inventory/e2e",
"@kbn/jest-serializers": "link:src/platform/packages/private/kbn-jest-serializers",
"@kbn/journeys": "link:src/platform/packages/private/kbn-journeys",
"@kbn/json-ast": "link:packages/kbn-json-ast",
"@kbn/kbn-genai-cli": "link:x-pack/solutions/observability/packages/kbn-genai-cli",
"@kbn/kibana-manifest-schema": "link:packages/kbn-kibana-manifest-schema",
"@kbn/lint-packages-cli": "link:packages/kbn-lint-packages-cli",
"@kbn/lint-ts-projects-cli": "link:packages/kbn-lint-ts-projects-cli",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export function supertestToObservable<T = any>(response: supertest.Test): Observ
return new Observable<T>((subscriber) => {
const parser = createParser({
onEvent: (event) => {
subscriber.next(JSON.parse(event.data));
subscriber.next({
type: event.event ?? 'event',
...JSON.parse(event.data),
});
},
});

Expand Down
7 changes: 7 additions & 0 deletions src/platform/packages/shared/kbn-sse-utils/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export class ServerSentEventError<
super(message);
}

public get status() {
if (typeof this.meta === 'object' && this.meta.status) {
return this.meta.status as number;
}
return undefined;
}

toJSON(): ServerSentErrorEvent {
return {
type: ServerSentEventType.error,
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,8 @@
"@kbn/index-management-shared-types/*": ["x-pack/platform/packages/shared/index-management/index_management_shared_types/*"],
"@kbn/index-patterns-test-plugin": ["src/platform/test/plugin_functional/plugins/index_patterns"],
"@kbn/index-patterns-test-plugin/*": ["src/platform/test/plugin_functional/plugins/index_patterns/*"],
"@kbn/inference-cli": ["x-pack/platform/packages/shared/kbn-inference-cli"],
"@kbn/inference-cli/*": ["x-pack/platform/packages/shared/kbn-inference-cli/*"],
"@kbn/inference-common": ["x-pack/platform/packages/shared/ai-infra/inference-common"],
"@kbn/inference-common/*": ["x-pack/platform/packages/shared/ai-infra/inference-common/*"],
"@kbn/inference-endpoint-plugin": ["x-pack/platform/plugins/shared/inference_endpoint"],
Expand Down Expand Up @@ -1130,6 +1132,8 @@
"@kbn/json-ast/*": ["packages/kbn-json-ast/*"],
"@kbn/json-schemas": ["x-pack/platform/packages/private/ml/json_schemas"],
"@kbn/json-schemas/*": ["x-pack/platform/packages/private/ml/json_schemas/*"],
"@kbn/kbn-genai-cli": ["x-pack/solutions/observability/packages/kbn-genai-cli"],
"@kbn/kbn-genai-cli/*": ["x-pack/solutions/observability/packages/kbn-genai-cli/*"],
"@kbn/kbn-health-gateway-status-plugin": ["src/platform/test/health_gateway/plugins/status"],
"@kbn/kbn-health-gateway-status-plugin/*": ["src/platform/test/health_gateway/plugins/status/*"],
"@kbn/kbn-sample-panel-action-plugin": ["src/platform/test/plugin_functional/plugins/kbn_sample_panel_action"],
Expand All @@ -1142,6 +1146,8 @@
"@kbn/kbn-tp-run-pipeline-plugin/*": ["src/platform/test/interpreter_functional/plugins/kbn_tp_run_pipeline/*"],
"@kbn/key-value-metadata-table": ["x-pack/platform/packages/shared/kbn-key-value-metadata-table"],
"@kbn/key-value-metadata-table/*": ["x-pack/platform/packages/shared/kbn-key-value-metadata-table/*"],
"@kbn/kibana-api-cli": ["x-pack/platform/packages/shared/kbn-kibana-api-cli"],
"@kbn/kibana-api-cli/*": ["x-pack/platform/packages/shared/kbn-kibana-api-cli/*"],
"@kbn/kibana-cors-test-plugin": ["x-pack/test/functional_cors/plugins/kibana_cors_test"],
"@kbn/kibana-cors-test-plugin/*": ["x-pack/test/functional_cors/plugins/kibana_cors_test/*"],
"@kbn/kibana-manifest-schema": ["packages/kbn-kibana-manifest-schema"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ export {
InferenceEndpointProvider,
elasticModelIds,
} from './src/inference_endpoints';

export { Tokenizer } from './src/utils/tokenizer';
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ export enum ChatCompletionEventType {
* the whole text content and potential tool calls of the response.
*/
export type ChatCompletionMessageEvent<TToolOptions extends ToolOptions = ToolOptions> =
InferenceTaskEventBase<ChatCompletionEventType.ChatCompletionMessage> & {
/**
* The text content of the LLM response.
*/
content: string;
/**
* The eventual tool calls performed by the LLM.
*/
toolCalls: ToolCallsOf<TToolOptions>['toolCalls'];
};
InferenceTaskEventBase<
ChatCompletionEventType.ChatCompletionMessage,
{
/**
* The text content of the LLM response.
*/
content: string;
/**
* The eventual tool calls performed by the LLM.
*/
toolCalls: ToolCallsOf<TToolOptions>['toolCalls'];
}
>;

/**
* Represent a partial tool call present in a chunk event.
Expand Down Expand Up @@ -64,8 +67,9 @@ export interface ChatCompletionChunkToolCall {
* Chunk event, containing a fragment of the total content,
* and potentially chunks of tool calls.
*/
export type ChatCompletionChunkEvent =
InferenceTaskEventBase<ChatCompletionEventType.ChatCompletionChunk> & {
export type ChatCompletionChunkEvent = InferenceTaskEventBase<
ChatCompletionEventType.ChatCompletionChunk,
{
/**
* The content chunk
*/
Expand All @@ -74,7 +78,8 @@ export type ChatCompletionChunkEvent =
* The tool call chunks
*/
tool_calls: ChatCompletionChunkToolCall[];
};
}
>;

/**
* Token count structure for the chatComplete API.
Expand All @@ -98,13 +103,15 @@ export interface ChatCompletionTokenCount {
* Token count event, send only once, usually (but not necessarily)
* before the message event
*/
export type ChatCompletionTokenCountEvent =
InferenceTaskEventBase<ChatCompletionEventType.ChatCompletionTokenCount> & {
export type ChatCompletionTokenCountEvent = InferenceTaskEventBase<
ChatCompletionEventType.ChatCompletionTokenCount,
{
/**
* The token count structure
*/
tokens: ChatCompletionTokenCount;
};
}
>;

/**
* Events emitted from the {@link ChatCompleteResponse} observable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { ServerSentEventError } from '@kbn/sse-utils';
import { InferenceTaskEventBase, InferenceTaskEventType } from './inference_task';

/**
Expand All @@ -17,44 +18,22 @@ export enum InferenceTaskErrorCode {
abortedError = 'requestAborted',
}

/**
* Base class for all inference API errors.
*/
export class InferenceTaskError<
const InferenceTaskError = ServerSentEventError;
type InferenceTaskError<
TCode extends string,
TMeta extends Record<string, any> | undefined
> extends Error {
constructor(public code: TCode, message: string, public meta: TMeta) {
super(message);
}

public get status() {
if (typeof this.meta === 'object' && this.meta.status) {
return this.meta.status as number;
}
return undefined;
}

toJSON(): InferenceTaskErrorEvent {
return {
type: InferenceTaskEventType.error,
error: {
code: this.code,
message: this.message,
meta: this.meta,
},
> = ServerSentEventError<TCode, TMeta>;

export type InferenceTaskErrorEvent = InferenceTaskEventBase<
InferenceTaskEventType.error,
{
error: {
code: string;
message: string;
meta?: Record<string, any>;
};
}
}

export type InferenceTaskErrorEvent = InferenceTaskEventBase<InferenceTaskEventType.error> & {
error: {
code: string;
message: string;
meta?: Record<string, any>;
};
};

>;
/**
* Inference error thrown when an unexpected internal error occurs while handling the request.
*/
Expand Down Expand Up @@ -162,3 +141,5 @@ export function isInferenceRequestAbortedError(error: unknown): error is Inferen
export function isInferenceProviderError(error: unknown): error is InferenceTaskProviderError {
return isInferenceError(error) && error.code === InferenceTaskErrorCode.providerError;
}

export { InferenceTaskError };
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { ServerSentEventBase } from '@kbn/sse-utils';

/**
* Base interface for all inference events.
*/
export interface InferenceTaskEventBase<TEventType extends string> {
/**
* Unique identifier of the event type.
*/
type: TEventType;
}
export type InferenceTaskEventBase<
TEventType extends string,
TData extends Record<string, any>
> = ServerSentEventBase<TEventType, TData>;

export enum InferenceTaskEventType {
error = 'error',
}

export type InferenceTaskEvent = InferenceTaskEventBase<string>;
export type InferenceTaskEvent = InferenceTaskEventBase<string, Record<string, unknown>>;
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export type Output = Record<string, any> | undefined | unknown;
/**
* Update (chunk) event for the {@link OutputAPI}
*/
export type OutputUpdateEvent<TId extends string = string> =
InferenceTaskEventBase<OutputEventType.OutputUpdate> & {
export type OutputUpdateEvent<TId extends string = string> = InferenceTaskEventBase<
OutputEventType.OutputUpdate,
{
/**
* The id of the operation, as provided as input
*/
Expand All @@ -33,29 +34,33 @@ export type OutputUpdateEvent<TId extends string = string> =
* The text content of the chunk
*/
content: string;
};
}
>;

/**
* Completion (complete message) event for the {@link OutputAPI}
*/
export type OutputCompleteEvent<
TId extends string = string,
TOutput extends Output = Output
> = InferenceTaskEventBase<OutputEventType.OutputComplete> & {
/**
* The id of the operation, as provided as input
*/
id: TId;
/**
* The task output, following the schema specified as input
*/
output: TOutput;
/**
* Potential text content provided by the LLM,
* if it was provided in addition to the tool call
*/
content: string;
};
> = InferenceTaskEventBase<
OutputEventType.OutputComplete,
{
/**
* The id of the operation, as provided as input
*/
id: TId;
/**
* The task output, following the schema specified as input
*/
output: TOutput;
/**
* Potential text content provided by the LLM,
* if it was provided in addition to the tool call
*/
content: string;
}
>;

/**
* Events emitted from the {@link OutputEvent}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export class Tokenizer {
/**
* Approximates the number of tokens in a string,
* assuming 4 characters per token.
*/
static count(input: string): number {
return Math.ceil(input.length / 4);
}

/**
* If the text is longer than the amount of tokens,
* truncate and mark as truncated.
*/
static truncate(
input: string,
maxTokens: number
): { tokens: number; truncated: boolean; text: string } {
const count = Tokenizer.count(input);
if (Tokenizer.count(input) > maxTokens) {
const maxChars = maxTokens * 4;
return { truncated: true, tokens: count, text: input.slice(0, maxChars) + '... <truncated>' };
}
return { truncated: false, tokens: count, text: input };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"target/**/*"
],
"kbn_references": [
"@kbn/sse-utils",
]
}
Loading