Skip to content

Commit

Permalink
refactor: model plugin to follow new specs (janhq#682)
Browse files Browse the repository at this point in the history
* refactor: model plugin to follow new specs

Signed-off-by: James <[email protected]>

* chore: rebase main

chore: rebase main

---------

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
Co-authored-by: Louis <[email protected]>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent a990fa6 commit 86e693b
Show file tree
Hide file tree
Showing 68 changed files with 2,087 additions and 1,182 deletions.
58 changes: 14 additions & 44 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,7 @@ const executeOnMain: (
method: string,
...args: any[]
) => Promise<any> = (plugin, method, ...args) =>
window.coreAPI?.invokePluginFunc(plugin, method, ...args) ??
window.electronAPI?.invokePluginFunc(plugin, method, ...args);

/**
* @deprecated This object is deprecated and should not be used.
* Use individual functions instead.
*/
const invokePluginFunc: (
plugin: string,
method: string,
...args: any[]
) => Promise<any> = (plugin, method, ...args) =>
window.coreAPI?.invokePluginFunc(plugin, method, ...args) ??
window.electronAPI?.invokePluginFunc(plugin, method, ...args);
window.coreAPI?.invokePluginFunc(plugin, method, ...args)

/**
* Downloads a file from a URL and saves it to the local file system.
Expand All @@ -36,16 +23,7 @@ const invokePluginFunc: (
const downloadFile: (url: string, fileName: string) => Promise<any> = (
url,
fileName
) =>
window.coreAPI?.downloadFile(url, fileName) ??
window.electronAPI?.downloadFile(url, fileName);

/**
* @deprecated This object is deprecated and should not be used.
* Use fs module instead.
*/
const deleteFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
) => window.coreAPI?.downloadFile(url, fileName);

/**
* Aborts the download of a specific file.
Expand All @@ -66,11 +44,18 @@ const appDataPath: () => Promise<any> = () => window.coreAPI?.appDataPath();
* Gets the user space path.
* @returns {Promise<any>} A Promise that resolves with the user space path.
*/
const getUserSpace = (): Promise<string> =>
window.coreAPI?.getUserSpace() ?? window.electronAPI?.getUserSpace();
const getUserSpace = (): Promise<string> => window.coreAPI?.getUserSpace();

/** Register extension point function type definition
*
/**
* Opens the file explorer at a specific path.
* @param {string} path - The path to open in the file explorer.
* @returns {Promise<any>} A promise that resolves when the file explorer is opened.
*/
const openFileExplorer: (path: string) => Promise<any> = (path) =>
window.coreAPI?.openFileExplorer(path);

/**
* Register extension point function type definition
*/
export type RegisterExtensionPoint = (
extensionName: string,
Expand All @@ -79,29 +64,14 @@ export type RegisterExtensionPoint = (
priority?: number
) => void;

/**
* @deprecated This object is deprecated and should not be used.
* Use individual functions instead.
*/
export const core = {
invokePluginFunc,
executeOnMain,
downloadFile,
abortDownload,
deleteFile,
appDataPath,
getUserSpace,
};

/**
* Functions exports
*/
export {
invokePluginFunc,
executeOnMain,
downloadFile,
abortDownload,
deleteFile,
appDataPath,
getUserSpace,
openFileExplorer,
};
33 changes: 25 additions & 8 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,70 @@
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/
const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.coreAPI?.writeFile(path, data) ??
window.electronAPI?.writeFile(path, data);
window.coreAPI?.writeFile(path, data);

/**
* Checks whether the path is a directory.
* @param path - The path to check.
* @returns {boolean} A boolean indicating whether the path is a directory.
*/
const isDirectory = (path: string): Promise<boolean> =>
window.coreAPI?.isDirectory(path) ?? window.electronAPI?.isDirectory(path);
window.coreAPI?.isDirectory(path);

/**
* Reads the contents of a file at the specified path.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.readFile(path) ?? window.electronAPI?.readFile(path);
window.coreAPI?.readFile(path);

/**
* List the directory files
* @param {string} path - The path of the directory to list files.
* @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (path) =>
window.coreAPI?.listFiles(path) ?? window.electronAPI?.listFiles(path);
window.coreAPI?.listFiles(path);

/**
* Creates a directory at the specified path.
* @param {string} path - The path of the directory to create.
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (path) =>
window.coreAPI?.mkdir(path) ?? window.electronAPI?.mkdir(path);
window.coreAPI?.mkdir(path);

/**
* Removes a directory at the specified path.
* @param {string} path - The path of the directory to remove.
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdir: (path: string) => Promise<any> = (path) =>
window.coreAPI?.rmdir(path) ?? window.electronAPI?.rmdir(path);
window.coreAPI?.rmdir(path);
/**
* Deletes a file from the local file system.
* @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/
const deleteFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
window.coreAPI?.deleteFile(path);

/**
* Appends data to a file at the specified path.
* @param path path to the file
* @param data data to append
*/
const appendFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.coreAPI?.appendFile(path, data);

/**
* Reads a file line by line.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A promise that resolves to the lines of the file.
*/
const readLineByLine: (path: string) => Promise<any> = (path) =>
window.coreAPI?.readLineByLine(path);

export const fs = {
isDirectory,
Expand All @@ -63,4 +78,6 @@ export const fs = {
mkdir,
rmdir,
deleteFile,
appendFile,
readLineByLine,
};
22 changes: 4 additions & 18 deletions core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
/**
* @deprecated This object is deprecated and should not be used.
* Use individual functions instead.
*/
export { core, deleteFile, invokePluginFunc } from "./core";

/**
* Core module exports.
* @module
*/
export {
downloadFile,
executeOnMain,
appDataPath,
getUserSpace,
abortDownload,
} from "./core";
export * from "./core";

/**
* Events module exports.
* Events events exports.
* @module
*/
export { events } from "./events";
export * from "./events";

/**
* Events types exports.
* @module
*/
export * from "./events";

export * from "./types/index";

/**
* Filesystem module exports.
* @module
*/
export { fs } from "./fs";
export * from "./fs";

/**
* Plugin base module export.
Expand Down
1 change: 1 addition & 0 deletions core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum PluginType {
Preference = "preference",
SystemMonitoring = "systemMonitoring",
Model = "model",
Assistant = "assistant",
}

export abstract class JanPlugin {
Expand Down
28 changes: 28 additions & 0 deletions core/src/plugins/assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Assistant } from "../index";
import { JanPlugin } from "../plugin";

/**
* Abstract class for assistant plugins.
* @extends JanPlugin
*/
export abstract class AssistantPlugin extends JanPlugin {
/**
* 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[]>;
}
51 changes: 38 additions & 13 deletions core/src/plugins/conversational.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
import { Thread } from "../index";
import { Thread, ThreadMessage } from "../index";
import { JanPlugin } from "../plugin";

/**
* Abstract class for conversational plugins.
* Abstract class for Thread plugins.
* @abstract
* @extends JanPlugin
*/
export abstract class ConversationalPlugin extends JanPlugin {
/**
* Returns a list of conversations.
* Returns a list of thread.
* @abstract
* @returns {Promise<any[]>} A promise that resolves to an array of conversations.
* @returns {Promise<Thread[]>} A promise that resolves to an array of threads.
*/
abstract getConversations(): Promise<any[]>;
abstract getThreads(): Promise<Thread[]>;

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

/**
* Deletes a conversation.
* Deletes a thread.
* @abstract
* @param {string} conversationId - The ID of the conversation to delete.
* @returns {Promise<void>} A promise that resolves when the conversation is deleted.
* @param {string} threadId - The ID of the thread to delete.
* @returns {Promise<void>} A promise that resolves when the thread is deleted.
*/
abstract deleteConversation(conversationId: string): Promise<void>;
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[]>;
}
7 changes: 6 additions & 1 deletion core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export { InferencePlugin } from "./inference";
*/
export { MonitoringPlugin } from "./monitoring";

/**
* Assistant plugin for managing assistants.
*/
export { AssistantPlugin } from "./assistant";

/**
* Model plugin for managing models.
*/
export { ModelPlugin } from "./model";
export { ModelPlugin } from "./model";
6 changes: 3 additions & 3 deletions core/src/plugins/inference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageRequest, ThreadMessage } from "../index";
import { MessageRequest, ModelSettingParams, ThreadMessage } from "../index";
import { JanPlugin } from "../plugin";

/**
Expand All @@ -7,9 +7,9 @@ import { JanPlugin } from "../plugin";
export abstract class InferencePlugin extends JanPlugin {
/**
* Initializes the model for the plugin.
* @param modelFileName - The name of the file containing the model.
* @param modelId - The ID of the model to initialize.
*/
abstract initModel(modelFileName: string): Promise<void>;
abstract initModel(modelId: string, settings?: ModelSettingParams): Promise<void>;

/**
* Stops the model for the plugin.
Expand Down
7 changes: 3 additions & 4 deletions core/src/plugins/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ export abstract class ModelPlugin extends JanPlugin {

/**
* Cancels the download of a specific model.
* @param {string} name - The name of the model to cancel the download for.
* @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(name: string, modelId: string): Promise<void>;
abstract cancelModelDownload(modelId: string): Promise<void>;

/**
* Deletes a model.
* @param filePath - The file path of the model to delete.
* @param modelId - The ID of the model to delete.
* @returns A Promise that resolves when the model has been deleted.
*/
abstract deleteModel(filePath: string): Promise<void>;
abstract deleteModel(modelId: string): Promise<void>;

/**
* Saves a model.
Expand Down
Loading

0 comments on commit 86e693b

Please sign in to comment.