Skip to content

Commit

Permalink
refactor: jan extensions (janhq#799)
Browse files Browse the repository at this point in the history
* refactor: rename plugin to extension
  • Loading branch information
louis-jan authored Dec 1, 2023
1 parent e6de39d commit 1143bd3
Show file tree
Hide file tree
Showing 134 changed files with 1,747 additions and 1,860 deletions.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ electron/renderer
package-lock.json

*.log
plugin-core/lib
core/lib/**

# Nitro binary files
plugins/inference-plugin/nitro/*/nitro
plugins/inference-plugin/nitro/*/*.exe
plugins/inference-plugin/nitro/*/*.dll
plugins/inference-plugin/nitro/*/*.metal
extensions/inference-extension/nitro/*/nitro
extensions/inference-extension/nitro/*/*.exe
extensions/inference-extension/nitro/*/*.dll
extensions/inference-extension/nitro/*/*.metal
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ else
cd uikit && yarn install && yarn build
endif

# Installs yarn dependencies and builds core and plugins
# Installs yarn dependencies and builds core and extensions
install-and-build: build-uikit
ifeq ($(OS),Windows_NT)
yarn config set network-timeout 300000
endif
yarn build:core
yarn install
yarn build:plugins
yarn build:extensions

dev: install-and-build
yarn dev
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ Contributions are welcome! Please read the [CONTRIBUTING.md](CONTRIBUTING.md) fi
```
This will start the development server and open the desktop app.
In this step, there are a few notification about installing base plugin, just click `OK` and `Next` to continue.
### For production build
Expand Down
10 changes: 4 additions & 6 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
{
"name": "@janhq/core",
"version": "0.1.10",
"description": "Plugin core lib",
"description": "Jan app core lib",
"keywords": [
"jan",
"plugin",
"core"
],
"homepage": "https://github.com/janhq",
"license": "MIT",
"homepage": "https://jan.ai",
"license": "AGPL-3.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"exports": {
".": "./lib/index.js",
"./plugin": "./lib/plugins/index.js"
".": "./lib/index.js"
},
"files": [
"lib",
Expand Down
8 changes: 1 addition & 7 deletions core/src/@global/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
export {};

declare global {
interface CorePlugin {
store?: any | undefined;
events?: any | undefined;
}
interface Window {
corePlugin?: CorePlugin;
coreAPI?: any | undefined;
electronAPI?: any | undefined;
core?: any;
}
}
20 changes: 10 additions & 10 deletions core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Execute a plugin module function in main process
* Execute a extension module function in main process
*
* @param plugin plugin name to import
* @param extension extension name to import
* @param method function name to execute
* @param args arguments to pass to the function
* @returns Promise<any>
*
*/
const executeOnMain: (
plugin: string,
extension: string,
method: string,
...args: any[]
) => Promise<any> = (plugin, method, ...args) =>
window.coreAPI?.invokePluginFunc(plugin, method, ...args)
) => Promise<any> = (extension, method, ...args) =>
window.core?.api?.invokeExtensionFunc(extension, method, ...args);

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

/**
* Aborts the download of a specific file.
* @param {string} fileName - The name of the file whose download is to be aborted.
* @returns {Promise<any>} A promise that resolves when the download has been aborted.
*/
const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
window.coreAPI?.abortDownload(fileName);
window.core.api?.abortDownload(fileName);

/**
* Retrieves the path to the app data directory using the `coreAPI` object.
* If the `coreAPI` object is not available, the function returns `undefined`.
* @returns A Promise that resolves with the path to the app data directory, or `undefined` if the `coreAPI` object is not available.
*/
const appDataPath: () => Promise<any> = () => window.coreAPI?.appDataPath();
const appDataPath: () => Promise<any> = () => window.core.api?.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();
const getUserSpace = (): Promise<string> => window.core.api?.getUserSpace();

/**
* 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);
window.core.api?.openFileExplorer(path);

/**
* Register extension point function type definition
Expand Down
20 changes: 9 additions & 11 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
* The `EventName` enumeration contains the names of all the available events in the Jan platform.
*/
export enum EventName {
OnNewConversation = "onNewConversation",
OnNewMessageRequest = "onNewMessageRequest",
OnNewMessageResponse = "onNewMessageResponse",
OnMessageResponseUpdate = "onMessageResponseUpdate",
OnMessageResponseFinished = "onMessageResponseFinished",
OnDownloadUpdate = "onDownloadUpdate",
OnDownloadSuccess = "onDownloadSuccess",
OnDownloadError = "onDownloadError",
/** 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",
}

/**
Expand All @@ -22,7 +20,7 @@ const on: (eventName: string, handler: Function) => void = (
eventName,
handler
) => {
window.corePlugin?.events?.on(eventName, handler);
window.core?.events?.on(eventName, handler);
};

/**
Expand All @@ -35,7 +33,7 @@ const off: (eventName: string, handler: Function) => void = (
eventName,
handler
) => {
window.corePlugin?.events?.off(eventName, handler);
window.core?.events?.off(eventName, handler);
};

/**
Expand All @@ -45,7 +43,7 @@ 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) => {
window.corePlugin?.events?.emit(eventName, object);
window.core?.events?.emit(eventName, object);
};

export const events = {
Expand Down
30 changes: 30 additions & 0 deletions core/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export enum ExtensionType {
Assistant = "assistant",
Conversational = "conversational",
Inference = "inference",
Model = "model",
SystemMonitoring = "systemMonitoring",
}

/**
* Represents a base extension.
* This class should be extended by any class that represents an extension.
*/
export abstract class BaseExtension {
/**
* Returns the type of the extension.
* @returns {ExtensionType} The type of the extension
* Undefined means its not extending any known extension by the application.
*/
abstract type(): ExtensionType | undefined;
/**
* Called when the extension is loaded.
* Any initialization logic for the extension should be put here.
*/
abstract onLoad(): void;
/**
* Called when the extension is unloaded.
* Any cleanup logic for the extension should be put here.
*/
abstract onUnload(): void;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Assistant } from "../index";
import { JanPlugin } from "../plugin";
import { BaseExtension } from "../extension";

/**
* Abstract class for assistant plugins.
* @extends JanPlugin
* Assistant extension for managing assistants.
* @extends BaseExtension
*/
export abstract class AssistantPlugin extends JanPlugin {
export abstract class AssistantExtension extends BaseExtension {
/**
* Creates a new assistant.
* @param {Assistant} assistant - The assistant object to be created.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Thread, ThreadMessage } from "../index";
import { JanPlugin } from "../plugin";
import { BaseExtension } from "../extension";

/**
* Abstract class for Thread plugins.
* Conversational extension. Persists and retrieves conversations.
* @abstract
* @extends JanPlugin
* @extends BaseExtension
*/
export abstract class ConversationalPlugin extends JanPlugin {
export abstract class ConversationalExtension extends BaseExtension {
/**
* Returns a list of thread.
* @abstract
Expand Down
25 changes: 25 additions & 0 deletions core/src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Conversational extension. Persists and retrieves conversations.
* @module
*/
export { ConversationalExtension } from "./conversational";

/**
* Inference extension. Start, stop and inference models.
*/
export { InferenceExtension } from "./inference";

/**
* Monitoring extension for system monitoring.
*/
export { MonitoringExtension } from "./monitoring";

/**
* Assistant extension for managing assistants.
*/
export { AssistantExtension } from "./assistant";

/**
* Model extension for managing models.
*/
export { ModelExtension } from "./model";
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { MessageRequest, ModelSettingParams, ThreadMessage } from "../index";
import { JanPlugin } from "../plugin";
import { BaseExtension } from "../extension";

/**
* An abstract class representing an Inference Plugin for Jan.
* Inference extension. Start, stop and inference models.
*/
export abstract class InferencePlugin extends JanPlugin {
export abstract class InferenceExtension extends BaseExtension {
/**
* Initializes the model for the plugin.
* Initializes the model for the extension.
* @param modelId - The ID of the model to initialize.
*/
abstract initModel(modelId: string, settings?: ModelSettingParams): Promise<void>;

/**
* Stops the model for the plugin.
* Stops the model for the extension.
*/
abstract stopModel(): Promise<void>;

Expand Down
10 changes: 3 additions & 7 deletions core/src/plugins/model.ts → core/src/extensions/model.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
/**
* Represents a plugin for managing machine learning models.
* @abstract
*/
import { JanPlugin } from "../plugin";
import { BaseExtension } from "../extension";
import { Model, ModelCatalog } from "../types/index";

/**
* An abstract class representing a plugin for managing machine learning models.
* Model extension for managing models.
*/
export abstract class ModelPlugin extends JanPlugin {
export abstract class ModelExtension extends BaseExtension {
/**
* Downloads a model.
* @param model - The model to download.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JanPlugin } from "../plugin";
import { BaseExtension } from "../extension";

/**
* Abstract class for monitoring plugins.
* @extends JanPlugin
* Monitoring extension for system monitoring.
* @extends BaseExtension
*/
export abstract class MonitoringPlugin extends JanPlugin {
export abstract class MonitoringExtension extends BaseExtension {
/**
* Returns information about the system resources.
* @returns {Promise<any>} A promise that resolves with the system resources information.
Expand Down
Loading

0 comments on commit 1143bd3

Please sign in to comment.