Skip to content

Commit

Permalink
Merge pull request janhq#966 from janhq/cleanTypes
Browse files Browse the repository at this point in the history
refactor: clean types and interfaces
  • Loading branch information
freelerobot authored Dec 13, 2023
2 parents 89d73e7 + db2ccc1 commit 6deb368
Show file tree
Hide file tree
Showing 27 changed files with 590 additions and 488 deletions.
43 changes: 19 additions & 24 deletions core/src/events.ts
Original file line number Diff line number Diff line change
@@ -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',
}

/**
Expand All @@ -28,25 +29,19 @@ 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.
*
* @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.
Expand All @@ -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,
};
}
28 changes: 6 additions & 22 deletions core/src/extensions/assistant.ts
Original file line number Diff line number Diff line change
@@ -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<void>} A promise that resolves when the assistant has been created.
*/
abstract createAssistant(assistant: Assistant): Promise<void>;

/**
* Deletes an existing assistant.
* @param {Assistant} assistant - The assistant object to be deleted.
* @returns {Promise<void>} A promise that resolves when the assistant has been deleted.
*/
abstract deleteAssistant(assistant: Assistant): Promise<void>;

/**
* Retrieves all existing assistants.
* @returns {Promise<Assistant[]>} A promise that resolves to an array of all assistants.
*/
abstract getAssistants(): Promise<Assistant[]>;
export abstract class AssistantExtension extends BaseExtension implements AssistantInterface {
abstract createAssistant(assistant: Assistant): Promise<void>
abstract deleteAssistant(assistant: Assistant): Promise<void>
abstract getAssistants(): Promise<Assistant[]>
}
62 changes: 12 additions & 50 deletions core/src/extensions/conversational.ts
Original file line number Diff line number Diff line change
@@ -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<Thread[]>} A promise that resolves to an array of threads.
*/
abstract getThreads(): Promise<Thread[]>;

/**
* Saves a thread.
* @abstract
* @param {Thread} thread - The thread to save.
* @returns {Promise<void>} A promise that resolves when the thread is saved.
*/
abstract saveThread(thread: Thread): Promise<void>;

/**
* Deletes a thread.
* @abstract
* @param {string} threadId - The ID of the thread to delete.
* @returns {Promise<void>} A promise that resolves when the thread is deleted.
*/
abstract deleteThread(threadId: string): Promise<void>;

/**
* Adds a new message to the thread.
* @param {ThreadMessage} message - The message to be added.
* @returns {Promise<void>} A promise that resolves when the message has been added.
*/
abstract addNewMessage(message: ThreadMessage): Promise<void>;

/**
* 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<void>} A promise that resolves when the messages have been written.
*/
abstract writeMessages(
threadId: string,
messages: ThreadMessage[]
): Promise<void>;

/**
* Retrieves all messages from a specific thread.
* @param {string} threadId - The ID of the thread to retrieve the messages from.
* @returns {Promise<ThreadMessage[]>} A promise that resolves to an array of messages from the thread.
*/
abstract getAllMessages(threadId: string): Promise<ThreadMessage[]>;
export abstract class ConversationalExtension
extends BaseExtension
implements ThreadInterface, MessageInterface
{
abstract getThreads(): Promise<Thread[]>
abstract saveThread(thread: Thread): Promise<void>
abstract deleteThread(threadId: string): Promise<void>
abstract addNewMessage(message: ThreadMessage): Promise<void>
abstract writeMessages(threadId: string, messages: ThreadMessage[]): Promise<void>
abstract getAllMessages(threadId: string): Promise<ThreadMessage[]>
}
13 changes: 4 additions & 9 deletions core/src/extensions/inference.ts
Original file line number Diff line number Diff line change
@@ -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<ThreadMessage>;
export abstract class InferenceExtension extends BaseExtension implements InferenceInterface {
abstract inference(data: MessageRequest): Promise<ThreadMessage>
}
51 changes: 9 additions & 42 deletions core/src/extensions/model.ts
Original file line number Diff line number Diff line change
@@ -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<void>;

/**
* Cancels the download of a specific model.
* @param {string} modelId - The ID of the model to cancel the download for.
* @returns {Promise<void>} A promise that resolves when the download has been cancelled.
*/
abstract cancelModelDownload(modelId: string): Promise<void>;

/**
* 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<void>;

/**
* 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<void>;

/**
* Gets a list of downloaded models.
* @returns A Promise that resolves with an array of downloaded models.
*/
abstract getDownloadedModels(): Promise<Model[]>;

/**
* Gets a list of configured models.
* @returns A Promise that resolves with an array of configured models.
*/
abstract getConfiguredModels(): Promise<Model[]>;
export abstract class ModelExtension extends BaseExtension implements ModelInterface {
abstract downloadModel(model: Model): Promise<void>
abstract cancelModelDownload(modelId: string): Promise<void>
abstract deleteModel(modelId: string): Promise<void>
abstract saveModel(model: Model): Promise<void>
abstract getDownloadedModels(): Promise<Model[]>
abstract getConfiguredModels(): Promise<Model[]>
}
18 changes: 5 additions & 13 deletions core/src/extensions/monitoring.ts
Original file line number Diff line number Diff line change
@@ -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<any>} A promise that resolves with the system resources information.
*/
abstract getResourcesInfo(): Promise<any>;

/**
* Returns the current system load.
* @returns {Promise<any>} A promise that resolves with the current system load.
*/
abstract getCurrentLoad(): Promise<any>;
export abstract class MonitoringExtension extends BaseExtension implements MonitoringInterface {
abstract getResourcesInfo(): Promise<any>
abstract getCurrentLoad(): Promise<any>
}
30 changes: 30 additions & 0 deletions core/src/types/assistant/assistantEntity.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>
}
26 changes: 26 additions & 0 deletions core/src/types/assistant/assistantInterface.ts
Original file line number Diff line number Diff line change
@@ -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<void>} A promise that resolves when the assistant has been created.
*/
createAssistant(assistant: Assistant): Promise<void>

/**
* Deletes an existing assistant.
* @param {Assistant} assistant - The assistant object to be deleted.
* @returns {Promise<void>} A promise that resolves when the assistant has been deleted.
*/
deleteAssistant(assistant: Assistant): Promise<void>

/**
* Retrieves all existing assistants.
* @returns {Promise<Assistant[]>} A promise that resolves to an array of all assistants.
*/
getAssistants(): Promise<Assistant[]>
}
2 changes: 2 additions & 0 deletions core/src/types/assistant/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './assistantEntity'
export * from './assistantInterface'
Loading

0 comments on commit 6deb368

Please sign in to comment.