diff --git a/core/src/events.ts b/core/src/events.ts index 1acbef918..7d3877046 100644 --- a/core/src/events.ts +++ b/core/src/events.ts @@ -1,25 +1,26 @@ +// TODO: refactor EventName to use the events defined in /types /** * The `EventName` enumeration contains the names of all the available events in the Jan platform. */ export enum EventName { /** The `OnMessageSent` event is emitted when a message is sent. */ - OnMessageSent = "OnMessageSent", + OnMessageSent = 'OnMessageSent', /** The `OnMessageResponse` event is emitted when a message is received. */ - OnMessageResponse = "OnMessageResponse", + OnMessageResponse = 'OnMessageResponse', /** The `OnMessageUpdate` event is emitted when a message is updated. */ - OnMessageUpdate = "OnMessageUpdate", + OnMessageUpdate = 'OnMessageUpdate', /** The `OnModelInit` event is emitted when a model inits. */ - OnModelInit = "OnModelInit", + OnModelInit = 'OnModelInit', /** The `OnModelReady` event is emitted when a model ready. */ - OnModelReady = "OnModelReady", + OnModelReady = 'OnModelReady', /** The `OnModelFail` event is emitted when a model fails loading. */ - OnModelFail = "OnModelFail", + OnModelFail = 'OnModelFail', /** The `OnModelStop` event is emitted when a model start to stop. */ - OnModelStop = "OnModelStop", + OnModelStop = 'OnModelStop', /** The `OnModelStopped` event is emitted when a model stopped ok. */ - OnModelStopped = "OnModelStopped", + OnModelStopped = 'OnModelStopped', /** The `OnInferenceStopped` event is emitted when a inference is stopped. */ - OnInferenceStopped = "OnInferenceStopped", + OnInferenceStopped = 'OnInferenceStopped', } /** @@ -28,12 +29,9 @@ export enum EventName { * @param eventName The name of the event to observe. * @param handler The handler function to call when the event is observed. */ -const on: (eventName: string, handler: Function) => void = ( - eventName, - handler -) => { - global.core?.events?.on(eventName, handler); -}; +const on: (eventName: string, handler: Function) => void = (eventName, handler) => { + global.core?.events?.on(eventName, handler) +} /** * Removes an observer for an event. @@ -41,12 +39,9 @@ const on: (eventName: string, handler: Function) => void = ( * @param eventName The name of the event to stop observing. * @param handler The handler function to call when the event is observed. */ -const off: (eventName: string, handler: Function) => void = ( - eventName, - handler -) => { - global.core?.events?.off(eventName, handler); -}; +const off: (eventName: string, handler: Function) => void = (eventName, handler) => { + global.core?.events?.off(eventName, handler) +} /** * Emits an event. @@ -55,11 +50,11 @@ const off: (eventName: string, handler: Function) => void = ( * @param object The object to pass to the event callback. */ const emit: (eventName: string, object: any) => void = (eventName, object) => { - global.core?.events?.emit(eventName, object); -}; + global.core?.events?.emit(eventName, object) +} export const events = { on, off, emit, -}; +} diff --git a/core/src/extensions/assistant.ts b/core/src/extensions/assistant.ts index 56c1f27b7..96646e374 100644 --- a/core/src/extensions/assistant.ts +++ b/core/src/extensions/assistant.ts @@ -1,28 +1,12 @@ -import { Assistant } from "../index"; -import { BaseExtension } from "../extension"; +import { Assistant, AssistantInterface } from '../index' +import { BaseExtension } from '../extension' /** * Assistant extension for managing assistants. * @extends BaseExtension */ -export abstract class AssistantExtension extends BaseExtension { - /** - * Creates a new assistant. - * @param {Assistant} assistant - The assistant object to be created. - * @returns {Promise} A promise that resolves when the assistant has been created. - */ - abstract createAssistant(assistant: Assistant): Promise; - - /** - * Deletes an existing assistant. - * @param {Assistant} assistant - The assistant object to be deleted. - * @returns {Promise} A promise that resolves when the assistant has been deleted. - */ - abstract deleteAssistant(assistant: Assistant): Promise; - - /** - * Retrieves all existing assistants. - * @returns {Promise} A promise that resolves to an array of all assistants. - */ - abstract getAssistants(): Promise; +export abstract class AssistantExtension extends BaseExtension implements AssistantInterface { + abstract createAssistant(assistant: Assistant): Promise + abstract deleteAssistant(assistant: Assistant): Promise + abstract getAssistants(): Promise } diff --git a/core/src/extensions/conversational.ts b/core/src/extensions/conversational.ts index 291346531..673ea9c11 100644 --- a/core/src/extensions/conversational.ts +++ b/core/src/extensions/conversational.ts @@ -1,57 +1,19 @@ -import { Thread, ThreadMessage } from "../index"; -import { BaseExtension } from "../extension"; +import { Thread, ThreadInterface, ThreadMessage, MessageInterface } from '../index' +import { BaseExtension } from '../extension' /** * Conversational extension. Persists and retrieves conversations. * @abstract * @extends BaseExtension */ -export abstract class ConversationalExtension extends BaseExtension { - /** - * Returns a list of thread. - * @abstract - * @returns {Promise} A promise that resolves to an array of threads. - */ - abstract getThreads(): Promise; - - /** - * Saves a thread. - * @abstract - * @param {Thread} thread - The thread to save. - * @returns {Promise} A promise that resolves when the thread is saved. - */ - abstract saveThread(thread: Thread): Promise; - - /** - * Deletes a thread. - * @abstract - * @param {string} threadId - The ID of the thread to delete. - * @returns {Promise} A promise that resolves when the thread is deleted. - */ - abstract deleteThread(threadId: string): Promise; - - /** - * Adds a new message to the thread. - * @param {ThreadMessage} message - The message to be added. - * @returns {Promise} A promise that resolves when the message has been added. - */ - abstract addNewMessage(message: ThreadMessage): Promise; - - /** - * Writes an array of messages to a specific thread. - * @param {string} threadId - The ID of the thread to write the messages to. - * @param {ThreadMessage[]} messages - The array of messages to be written. - * @returns {Promise} A promise that resolves when the messages have been written. - */ - abstract writeMessages( - threadId: string, - messages: ThreadMessage[] - ): Promise; - - /** - * Retrieves all messages from a specific thread. - * @param {string} threadId - The ID of the thread to retrieve the messages from. - * @returns {Promise} A promise that resolves to an array of messages from the thread. - */ - abstract getAllMessages(threadId: string): Promise; +export abstract class ConversationalExtension + extends BaseExtension + implements ThreadInterface, MessageInterface +{ + abstract getThreads(): Promise + abstract saveThread(thread: Thread): Promise + abstract deleteThread(threadId: string): Promise + abstract addNewMessage(message: ThreadMessage): Promise + abstract writeMessages(threadId: string, messages: ThreadMessage[]): Promise + abstract getAllMessages(threadId: string): Promise } diff --git a/core/src/extensions/inference.ts b/core/src/extensions/inference.ts index 9453a06d5..f3654934a 100644 --- a/core/src/extensions/inference.ts +++ b/core/src/extensions/inference.ts @@ -1,14 +1,9 @@ -import { MessageRequest, ModelSettingParams, ThreadMessage } from "../index"; -import { BaseExtension } from "../extension"; +import { InferenceInterface, MessageRequest, ThreadMessage } from '../index' +import { BaseExtension } from '../extension' /** * Inference extension. Start, stop and inference models. */ -export abstract class InferenceExtension extends BaseExtension { - /** - * Processes an inference request. - * @param data - The data for the inference request. - * @returns The result of the inference request. - */ - abstract inference(data: MessageRequest): Promise; +export abstract class InferenceExtension extends BaseExtension implements InferenceInterface { + abstract inference(data: MessageRequest): Promise } diff --git a/core/src/extensions/model.ts b/core/src/extensions/model.ts index 276d15dcc..cac9d9d89 100644 --- a/core/src/extensions/model.ts +++ b/core/src/extensions/model.ts @@ -1,47 +1,14 @@ -import { BaseExtension } from "../extension"; -import { Model } from "../types/index"; +import { BaseExtension } from '../extension' +import { Model, ModelInterface } from '../index' /** * Model extension for managing models. */ -export abstract class ModelExtension extends BaseExtension { - /** - * Downloads a model. - * @param model - The model to download. - * @returns A Promise that resolves when the model has been downloaded. - */ - abstract downloadModel(model: Model): Promise; - - /** - * Cancels the download of a specific model. - * @param {string} modelId - The ID of the model to cancel the download for. - * @returns {Promise} A promise that resolves when the download has been cancelled. - */ - abstract cancelModelDownload(modelId: string): Promise; - - /** - * Deletes a model. - * @param modelId - The ID of the model to delete. - * @returns A Promise that resolves when the model has been deleted. - */ - abstract deleteModel(modelId: string): Promise; - - /** - * Saves a model. - * @param model - The model to save. - * @returns A Promise that resolves when the model has been saved. - */ - abstract saveModel(model: Model): Promise; - - /** - * Gets a list of downloaded models. - * @returns A Promise that resolves with an array of downloaded models. - */ - abstract getDownloadedModels(): Promise; - - /** - * Gets a list of configured models. - * @returns A Promise that resolves with an array of configured models. - */ - abstract getConfiguredModels(): Promise; +export abstract class ModelExtension extends BaseExtension implements ModelInterface { + abstract downloadModel(model: Model): Promise + abstract cancelModelDownload(modelId: string): Promise + abstract deleteModel(modelId: string): Promise + abstract saveModel(model: Model): Promise + abstract getDownloadedModels(): Promise + abstract getConfiguredModels(): Promise } diff --git a/core/src/extensions/monitoring.ts b/core/src/extensions/monitoring.ts index f3d66e658..94f437f86 100644 --- a/core/src/extensions/monitoring.ts +++ b/core/src/extensions/monitoring.ts @@ -1,19 +1,11 @@ -import { BaseExtension } from "../extension"; +import { BaseExtension } from '../extension' +import { MonitoringInterface } from '../index' /** * Monitoring extension for system monitoring. * @extends BaseExtension */ -export abstract class MonitoringExtension extends BaseExtension { - /** - * Returns information about the system resources. - * @returns {Promise} A promise that resolves with the system resources information. - */ - abstract getResourcesInfo(): Promise; - - /** - * Returns the current system load. - * @returns {Promise} A promise that resolves with the current system load. - */ - abstract getCurrentLoad(): Promise; +export abstract class MonitoringExtension extends BaseExtension implements MonitoringInterface { + abstract getResourcesInfo(): Promise + abstract getCurrentLoad(): Promise } diff --git a/core/src/types/assistant/assistantEntity.ts b/core/src/types/assistant/assistantEntity.ts new file mode 100644 index 000000000..91bb2bb22 --- /dev/null +++ b/core/src/types/assistant/assistantEntity.ts @@ -0,0 +1,30 @@ +/** + * Assistant type defines the shape of an assistant object. + * @stored + */ +export type Assistant = { + /** Represents the avatar of the user. */ + avatar: string + /** Represents the location of the thread. */ + thread_location: string | undefined + /** Represents the unique identifier of the object. */ + id: string + /** Represents the object. */ + object: string + /** Represents the creation timestamp of the object. */ + created_at: number + /** Represents the name of the object. */ + name: string + /** Represents the description of the object. */ + description?: string + /** Represents the model of the object. */ + model: string + /** Represents the instructions for the object. */ + instructions?: string + /** Represents the tools associated with the object. */ + tools?: any + /** Represents the file identifiers associated with the object. */ + file_ids: string[] + /** Represents the metadata of the object. */ + metadata?: Record +} diff --git a/core/src/types/assistant/assistantInterface.ts b/core/src/types/assistant/assistantInterface.ts new file mode 100644 index 000000000..3c10bbb7f --- /dev/null +++ b/core/src/types/assistant/assistantInterface.ts @@ -0,0 +1,26 @@ +import { Assistant } from './assistantEntity' +/** + * Assistant extension for managing assistants. + * @extends BaseExtension + */ +export interface AssistantInterface { + /** + * Creates a new assistant. + * @param {Assistant} assistant - The assistant object to be created. + * @returns {Promise} A promise that resolves when the assistant has been created. + */ + createAssistant(assistant: Assistant): Promise + + /** + * Deletes an existing assistant. + * @param {Assistant} assistant - The assistant object to be deleted. + * @returns {Promise} A promise that resolves when the assistant has been deleted. + */ + deleteAssistant(assistant: Assistant): Promise + + /** + * Retrieves all existing assistants. + * @returns {Promise} A promise that resolves to an array of all assistants. + */ + getAssistants(): Promise +} diff --git a/core/src/types/assistant/index.ts b/core/src/types/assistant/index.ts new file mode 100644 index 000000000..83ea73f85 --- /dev/null +++ b/core/src/types/assistant/index.ts @@ -0,0 +1,2 @@ +export * from './assistantEntity' +export * from './assistantInterface' diff --git a/core/src/types/index.ts b/core/src/types/index.ts index 2e19f61d8..a4722ba78 100644 --- a/core/src/types/index.ts +++ b/core/src/types/index.ts @@ -1,328 +1,6 @@ -/** - * Message Request and Response - * ============================ - * */ - -/** - * The role of the author of this message. - */ -export enum ChatCompletionRole { - System = "system", - Assistant = "assistant", - User = "user", -} - -/** - * The `MessageRequest` type defines the shape of a new message request object. - * @data_transfer_object - */ -export type ChatCompletionMessage = { - /** The contents of the message. **/ - content?: string; - /** The role of the author of this message. **/ - role: ChatCompletionRole; -}; - -/** - * The `MessageRequest` type defines the shape of a new message request object. - * @data_transfer_object - */ -export type MessageRequest = { - id?: string; - - /** The thread id of the message request. **/ - threadId: string; - - /** - * The assistant id of the message request. - */ - assistantId?: string; - - /** Messages for constructing a chat completion request **/ - messages?: ChatCompletionMessage[]; - - /** Settings for constructing a chat completion request **/ - model?: ModelInfo; -}; - -/** - * Thread and Message - * ======================== - * */ - -/** - * The status of the message. - * @data_transfer_object - */ -export enum MessageStatus { - /** Message is fully loaded. **/ - Ready = "ready", - /** Message is not fully loaded. **/ - Pending = "pending", -} -/** - * The `ThreadMessage` type defines the shape of a thread's message object. - * @stored - */ -export type ThreadMessage = { - /** Unique identifier for the message, generated by default using the ULID method. **/ - id: string; - /** Object name **/ - object: string; - /** Thread id, default is a ulid. **/ - thread_id: string; - /** The assistant id of this thread. **/ - assistant_id?: string; - /** The role of the author of this message. **/ - role: ChatCompletionRole; - /** The content of this message. **/ - content: ThreadContent[]; - /** The status of this message. **/ - status: MessageStatus; - /** The timestamp indicating when this message was created. Represented in Unix time. **/ - created: number; - /** The timestamp indicating when this message was updated. Represented in Unix time. **/ - updated: number; - /** The additional metadata of this message. **/ - metadata?: Record; -}; - -/** - * The content type of the message. - */ -export enum ContentType { - Text = "text", - Image = "image", -} -/** - * The `ThreadContent` type defines the shape of a message's content object - * @data_transfer_object - */ -export type ThreadContent = { - type: ContentType; - text: ContentValue; -}; - -/** - * The `ContentValue` type defines the shape of a content value object - * @data_transfer_object - */ -export type ContentValue = { - value: string; - annotations: string[]; -}; - -/** - * The `Thread` type defines the shape of a thread object. - * @stored - */ -export interface Thread { - /** Unique identifier for the thread, generated by default using the ULID method. **/ - id: string; - /** Object name **/ - object: string; - /** The title of this thread. **/ - title: string; - /** Assistants in this thread. **/ - assistants: ThreadAssistantInfo[]; - /** The timestamp indicating when this thread was created, represented in ISO 8601 format. **/ - created: number; - /** The timestamp indicating when this thread was updated, represented in ISO 8601 format. **/ - updated: number; - /** The additional metadata of this thread. **/ - metadata?: Record; -} - -/** - * Represents the information about an assistant in a thread. - * @stored - */ -export type ThreadAssistantInfo = { - assistant_id: string; - assistant_name: string; - model: ModelInfo; - instructions?: string; -}; - -/** - * Represents the information about a model. - * @stored - */ -export type ModelInfo = { - id: string; - settings: ModelSettingParams; - parameters: ModelRuntimeParams; - engine?: InferenceEngine; -}; - -/** - * Represents the state of a thread. - * @stored - */ -export type ThreadState = { - hasMore: boolean; - waitingForResponse: boolean; - error?: Error; - lastMessage?: string; - isFinishInit?: boolean; -}; -/** - * Represents the inference engine. - * @stored - */ - -enum InferenceEngine { - nitro = "nitro", - openai = "openai", - triton_trtllm = "triton_trtllm", - hf_endpoint = "hf_endpoint", -} - -/** - * Model type defines the shape of a model object. - * @stored - */ -export interface Model { - /** - * The type of the object. - * Default: "model" - */ - object: string; - - /** - * The version of the model. - */ - version: number; - - /** - * The format of the model. - */ - format: string; - - /** - * The model download source. It can be an external url or a local filepath. - */ - source_url: string; - - /** - * The model identifier, which can be referenced in the API endpoints. - */ - id: string; - - /** - * Human-readable name that is used for UI. - */ - name: string; - - /** - * The Unix timestamp (in seconds) for when the model was created - */ - created: number; - - /** - * Default: "A cool model from Huggingface" - */ - description: string; - - /** - * The model state. - * Default: "to_download" - * Enum: "to_download" "downloading" "ready" "running" - */ - state?: ModelState; - - /** - * The model settings. - */ - settings: ModelSettingParams; - - /** - * The model runtime parameters. - */ - parameters: ModelRuntimeParams; - - /** - * Metadata of the model. - */ - metadata: ModelMetadata; - /** - * The model engine. - */ - engine: InferenceEngine; -} - -export type ModelMetadata = { - author: string; - tags: string[]; - size: number; - cover?: string; -}; - -/** - * The Model transition states. - */ -export enum ModelState { - Downloading = "downloading", - Ready = "ready", - Running = "running", -} - -/** - * The available model settings. - */ -export type ModelSettingParams = { - ctx_len?: number; - ngl?: number; - embedding?: boolean; - n_parallel?: number; - cpu_threads?: number; - system_prompt?: string; - user_prompt?: string; - ai_prompt?: string; -}; - -/** - * The available model runtime parameters. - */ -export type ModelRuntimeParams = { - temperature?: number; - token_limit?: number; - top_k?: number; - top_p?: number; - stream?: boolean; - max_tokens?: number; - stop?: string[]; - frequency_penalty?: number; - presence_penalty?: number; -}; - -/** - * Assistant type defines the shape of an assistant object. - * @stored - */ -export type Assistant = { - /** Represents the avatar of the user. */ - avatar: string; - /** Represents the location of the thread. */ - thread_location: string | undefined; - /** Represents the unique identifier of the object. */ - id: string; - /** Represents the object. */ - object: string; - /** Represents the creation timestamp of the object. */ - created_at: number; - /** Represents the name of the object. */ - name: string; - /** Represents the description of the object. */ - description?: string; - /** Represents the model of the object. */ - model: string; - /** Represents the instructions for the object. */ - instructions?: string; - /** Represents the tools associated with the object. */ - tools?: any; - /** Represents the file identifiers associated with the object. */ - file_ids: string[]; - /** Represents the metadata of the object. */ - metadata?: Record; -}; +export * from './assistant' +export * from './model' +export * from './thread' +export * from './message' +export * from './inference' +export * from './monitoring' diff --git a/core/src/types/inference/index.ts b/core/src/types/inference/index.ts new file mode 100644 index 000000000..a0a71f142 --- /dev/null +++ b/core/src/types/inference/index.ts @@ -0,0 +1,3 @@ +export * from './inferenceEntity' +export * from './inferenceInterface' +export * from './inferenceEvent' diff --git a/core/src/types/inference/inferenceEntity.ts b/core/src/types/inference/inferenceEntity.ts new file mode 100644 index 000000000..58b838ae7 --- /dev/null +++ b/core/src/types/inference/inferenceEntity.ts @@ -0,0 +1,19 @@ +/** + * The role of the author of this message. + */ +export enum ChatCompletionRole { + System = 'system', + Assistant = 'assistant', + User = 'user', +} + +/** + * The `MessageRequest` type defines the shape of a new message request object. + * @data_transfer_object + */ +export type ChatCompletionMessage = { + /** The contents of the message. **/ + content?: string + /** The role of the author of this message. **/ + role: ChatCompletionRole +} diff --git a/core/src/types/inference/inferenceEvent.ts b/core/src/types/inference/inferenceEvent.ts new file mode 100644 index 000000000..f685a54b3 --- /dev/null +++ b/core/src/types/inference/inferenceEvent.ts @@ -0,0 +1,7 @@ +/** + * The `EventName` enumeration contains the names of all the available events in the Jan platform. + */ +export enum InferenceEvent { + /** The `OnInferenceStopped` event is emitted when a inference is stopped. */ + OnInferenceStopped = 'OnInferenceStopped', +} diff --git a/core/src/types/inference/inferenceInterface.ts b/core/src/types/inference/inferenceInterface.ts new file mode 100644 index 000000000..21e327e45 --- /dev/null +++ b/core/src/types/inference/inferenceInterface.ts @@ -0,0 +1,13 @@ +import { MessageRequest, ThreadMessage } from '../message' + +/** + * Inference extension. Start, stop and inference models. + */ +export interface InferenceInterface { + /** + * Processes an inference request. + * @param data - The data for the inference request. + * @returns The result of the inference request. + */ + inference(data: MessageRequest): Promise +} diff --git a/core/src/types/message/index.ts b/core/src/types/message/index.ts new file mode 100644 index 000000000..e8d78deda --- /dev/null +++ b/core/src/types/message/index.ts @@ -0,0 +1,3 @@ +export * from './messageEntity' +export * from './messageInterface' +export * from './messageEvent' diff --git a/core/src/types/message/messageEntity.ts b/core/src/types/message/messageEntity.ts new file mode 100644 index 000000000..ba3399b24 --- /dev/null +++ b/core/src/types/message/messageEntity.ts @@ -0,0 +1,88 @@ +import { ChatCompletionMessage, ChatCompletionRole } from '../inference' +import { ModelInfo } from '../model' + +/** + * The `ThreadMessage` type defines the shape of a thread's message object. + * @stored + */ +export type ThreadMessage = { + /** Unique identifier for the message, generated by default using the ULID method. **/ + id: string + /** Object name **/ + object: string + /** Thread id, default is a ulid. **/ + thread_id: string + /** The assistant id of this thread. **/ + assistant_id?: string + /** The role of the author of this message. **/ + role: ChatCompletionRole + /** The content of this message. **/ + content: ThreadContent[] + /** The status of this message. **/ + status: MessageStatus + /** The timestamp indicating when this message was created. Represented in Unix time. **/ + created: number + /** The timestamp indicating when this message was updated. Represented in Unix time. **/ + updated: number + /** The additional metadata of this message. **/ + metadata?: Record +} + +/** + * The `MessageRequest` type defines the shape of a new message request object. + * @data_transfer_object + */ +export type MessageRequest = { + id?: string + + /** The thread id of the message request. **/ + threadId: string + + /** + * The assistant id of the message request. + */ + assistantId?: string + + /** Messages for constructing a chat completion request **/ + messages?: ChatCompletionMessage[] + + /** Settings for constructing a chat completion request **/ + model?: ModelInfo +} + +/** + * The status of the message. + * @data_transfer_object + */ +export enum MessageStatus { + /** Message is fully loaded. **/ + Ready = 'ready', + /** Message is not fully loaded. **/ + Pending = 'pending', +} + +/** + * The content type of the message. + */ +export enum ContentType { + Text = 'text', + Image = 'image', +} + +/** + * The `ContentValue` type defines the shape of a content value object + * @data_transfer_object + */ +export type ContentValue = { + value: string + annotations: string[] +} + +/** + * The `ThreadContent` type defines the shape of a message's content object + * @data_transfer_object + */ +export type ThreadContent = { + type: ContentType + text: ContentValue +} diff --git a/core/src/types/message/messageEvent.ts b/core/src/types/message/messageEvent.ts new file mode 100644 index 000000000..40fd84c30 --- /dev/null +++ b/core/src/types/message/messageEvent.ts @@ -0,0 +1,8 @@ +export enum MessageEvent { + /** The `OnMessageSent` event is emitted when a message is sent. */ + OnMessageSent = 'OnMessageSent', + /** The `OnMessageResponse` event is emitted when a message is received. */ + OnMessageResponse = 'OnMessageResponse', + /** The `OnMessageUpdate` event is emitted when a message is updated. */ + OnMessageUpdate = 'OnMessageUpdate', +} diff --git a/core/src/types/message/messageInterface.ts b/core/src/types/message/messageInterface.ts new file mode 100644 index 000000000..f6579da88 --- /dev/null +++ b/core/src/types/message/messageInterface.ts @@ -0,0 +1,30 @@ +import { ThreadMessage } from './messageEntity' + +/** + * Conversational extension. Persists and retrieves conversations. + * @abstract + * @extends BaseExtension + */ +export interface MessageInterface { + /** + * Adds a new message to the thread. + * @param {ThreadMessage} message - The message to be added. + * @returns {Promise} A promise that resolves when the message has been added. + */ + addNewMessage(message: ThreadMessage): Promise + + /** + * Writes an array of messages to a specific thread. + * @param {string} threadId - The ID of the thread to write the messages to. + * @param {ThreadMessage[]} messages - The array of messages to be written. + * @returns {Promise} A promise that resolves when the messages have been written. + */ + writeMessages(threadId: string, messages: ThreadMessage[]): Promise + + /** + * Retrieves all messages from a specific thread. + * @param {string} threadId - The ID of the thread to retrieve the messages from. + * @returns {Promise} A promise that resolves to an array of messages from the thread. + */ + getAllMessages(threadId: string): Promise +} diff --git a/core/src/types/model/index.ts b/core/src/types/model/index.ts new file mode 100644 index 000000000..cba06ea95 --- /dev/null +++ b/core/src/types/model/index.ts @@ -0,0 +1,3 @@ +export * from './modelEntity' +export * from './modelInterface' +export * from './modelEvent' diff --git a/core/src/types/model/modelEntity.ts b/core/src/types/model/modelEntity.ts new file mode 100644 index 000000000..6f045588c --- /dev/null +++ b/core/src/types/model/modelEntity.ts @@ -0,0 +1,140 @@ +/** + * Represents the information about a model. + * @stored + */ +export type ModelInfo = { + id: string + settings: ModelSettingParams + parameters: ModelRuntimeParams + engine?: InferenceEngine +} + +/** + * Represents the inference engine. + * @stored + */ + +enum InferenceEngine { + nitro = 'nitro', + openai = 'openai', + triton_trtllm = 'triton_trtllm', + hf_endpoint = 'hf_endpoint', +} + +/** + * Model type defines the shape of a model object. + * @stored + */ +export type Model = { + /** + * The type of the object. + * Default: "model" + */ + object: string + + /** + * The version of the model. + */ + version: number + + /** + * The format of the model. + */ + format: string + + /** + * The model download source. It can be an external url or a local filepath. + */ + source_url: string + + /** + * The model identifier, which can be referenced in the API endpoints. + */ + id: string + + /** + * Human-readable name that is used for UI. + */ + name: string + + /** + * The Unix timestamp (in seconds) for when the model was created + */ + created: number + + /** + * Default: "A cool model from Huggingface" + */ + description: string + + /** + * The model state. + * Default: "to_download" + * Enum: "to_download" "downloading" "ready" "running" + */ + state?: ModelState + + /** + * The model settings. + */ + settings: ModelSettingParams + + /** + * The model runtime parameters. + */ + parameters: ModelRuntimeParams + + /** + * Metadata of the model. + */ + metadata: ModelMetadata + /** + * The model engine. + */ + engine: InferenceEngine +} + +export type ModelMetadata = { + author: string + tags: string[] + size: number + cover?: string +} + +/** + * The Model transition states. + */ +export enum ModelState { + Downloading = 'downloading', + Ready = 'ready', + Running = 'running', +} + +/** + * The available model settings. + */ +export type ModelSettingParams = { + ctx_len?: number + ngl?: number + embedding?: boolean + n_parallel?: number + cpu_threads?: number + system_prompt?: string + user_prompt?: string + ai_prompt?: string +} + +/** + * The available model runtime parameters. + */ +export type ModelRuntimeParams = { + temperature?: number + token_limit?: number + top_k?: number + top_p?: number + stream?: boolean + max_tokens?: number + stop?: string[] + frequency_penalty?: number + presence_penalty?: number +} diff --git a/core/src/types/model/modelEvent.ts b/core/src/types/model/modelEvent.ts new file mode 100644 index 000000000..978a48724 --- /dev/null +++ b/core/src/types/model/modelEvent.ts @@ -0,0 +1,15 @@ +/** + * The `EventName` enumeration contains the names of all the available events in the Jan platform. + */ +export enum ModelEvent { + /** The `OnModelInit` event is emitted when a model inits. */ + OnModelInit = 'OnModelInit', + /** The `OnModelReady` event is emitted when a model ready. */ + OnModelReady = 'OnModelReady', + /** The `OnModelFail` event is emitted when a model fails loading. */ + OnModelFail = 'OnModelFail', + /** The `OnModelStop` event is emitted when a model start to stop. */ + OnModelStop = 'OnModelStop', + /** The `OnModelStopped` event is emitted when a model stopped ok. */ + OnModelStopped = 'OnModelStopped', +} diff --git a/core/src/types/model/modelInterface.ts b/core/src/types/model/modelInterface.ts new file mode 100644 index 000000000..19b5e6051 --- /dev/null +++ b/core/src/types/model/modelInterface.ts @@ -0,0 +1,46 @@ +import { Model } from './modelEntity' + +/** + * Model extension for managing models. + */ +export interface ModelInterface { + /** + * Downloads a model. + * @param model - The model to download. + * @returns A Promise that resolves when the model has been downloaded. + */ + downloadModel(model: Model): Promise + + /** + * Cancels the download of a specific model. + * @param {string} modelId - The ID of the model to cancel the download for. + * @returns {Promise} A promise that resolves when the download has been cancelled. + */ + cancelModelDownload(modelId: string): Promise + + /** + * Deletes a model. + * @param modelId - The ID of the model to delete. + * @returns A Promise that resolves when the model has been deleted. + */ + deleteModel(modelId: string): Promise + + /** + * Saves a model. + * @param model - The model to save. + * @returns A Promise that resolves when the model has been saved. + */ + saveModel(model: Model): Promise + + /** + * Gets a list of downloaded models. + * @returns A Promise that resolves with an array of downloaded models. + */ + getDownloadedModels(): Promise + + /** + * Gets a list of configured models. + * @returns A Promise that resolves with an array of configured models. + */ + getConfiguredModels(): Promise +} diff --git a/core/src/types/monitoring/index.ts b/core/src/types/monitoring/index.ts new file mode 100644 index 000000000..5828dae8b --- /dev/null +++ b/core/src/types/monitoring/index.ts @@ -0,0 +1 @@ +export * from './monitoringInterface' diff --git a/core/src/types/monitoring/monitoringInterface.ts b/core/src/types/monitoring/monitoringInterface.ts new file mode 100644 index 000000000..ffdbebcc1 --- /dev/null +++ b/core/src/types/monitoring/monitoringInterface.ts @@ -0,0 +1,17 @@ +/** + * Monitoring extension for system monitoring. + * @extends BaseExtension + */ +export interface MonitoringInterface { + /** + * Returns information about the system resources. + * @returns {Promise} A promise that resolves with the system resources information. + */ + getResourcesInfo(): Promise + + /** + * Returns the current system load. + * @returns {Promise} A promise that resolves with the current system load. + */ + getCurrentLoad(): Promise +} diff --git a/core/src/types/thread/index.ts b/core/src/types/thread/index.ts new file mode 100644 index 000000000..c6ff6204a --- /dev/null +++ b/core/src/types/thread/index.ts @@ -0,0 +1,2 @@ +export * from './threadEntity' +export * from './threadInterface' diff --git a/core/src/types/thread/threadEntity.ts b/core/src/types/thread/threadEntity.ts new file mode 100644 index 000000000..4ff3aa1fc --- /dev/null +++ b/core/src/types/thread/threadEntity.ts @@ -0,0 +1,45 @@ +import { ModelInfo } from '../model' + +/** + * The `Thread` type defines the shape of a thread object. + * @stored + */ +export type Thread = { + /** Unique identifier for the thread, generated by default using the ULID method. **/ + id: string + /** Object name **/ + object: string + /** The title of this thread. **/ + title: string + /** Assistants in this thread. **/ + assistants: ThreadAssistantInfo[] + /** The timestamp indicating when this thread was created, represented in ISO 8601 format. **/ + created: number + /** The timestamp indicating when this thread was updated, represented in ISO 8601 format. **/ + updated: number + /** The additional metadata of this thread. **/ + metadata?: Record +} + +/** + * Represents the information about an assistant in a thread. + * @stored + */ +export type ThreadAssistantInfo = { + assistant_id: string + assistant_name: string + model: ModelInfo + instructions?: string +} + +/** + * Represents the state of a thread. + * @stored + */ +export type ThreadState = { + hasMore: boolean + waitingForResponse: boolean + error?: Error + lastMessage?: string + isFinishInit?: boolean +} diff --git a/core/src/types/thread/threadInterface.ts b/core/src/types/thread/threadInterface.ts new file mode 100644 index 000000000..792c8c8a5 --- /dev/null +++ b/core/src/types/thread/threadInterface.ts @@ -0,0 +1,31 @@ +import { Thread } from './threadEntity' + +/** + * Conversational extension. Persists and retrieves conversations. + * @abstract + * @extends BaseExtension + */ +export interface ThreadInterface { + /** + * Returns a list of thread. + * @abstract + * @returns {Promise} A promise that resolves to an array of threads. + */ + getThreads(): Promise + + /** + * Saves a thread. + * @abstract + * @param {Thread} thread - The thread to save. + * @returns {Promise} A promise that resolves when the thread is saved. + */ + saveThread(thread: Thread): Promise + + /** + * Deletes a thread. + * @abstract + * @param {string} threadId - The ID of the thread to delete. + * @returns {Promise} A promise that resolves when the thread is deleted. + */ + deleteThread(threadId: string): Promise +}