feature: core plugin support events & preferences modules #365
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@janhq/plugin-core
Usage
Import the package
Register Plugin Extensions
Every plugin must define an
init
function in its main entry file to initialize the plugin and register its extensions with the Jan platform.You can
register
any function as a plugin extension usingCoreServiceAPI
below. For example, theDataService.GetConversations
entry name can be used to register a function that retrieves conversations.Once the extension is registered, it can be used by other plugins or components in the Jan platform. For example, a UI component might use the DataService.GetConversations extension to retrieve a list of conversations to display to the user.
Interact with Local Data Storage
The Core API allows you to interact with local data storage. Here are a couple of examples of how you can use it:
Insert Data
You can use the store.insertOne function to insert data into a specific collection in the local data store.
Get Data
To retrieve data from a collection in the local data store, you can use the
store.findOne
orstore.findMany
function. It allows you to filter and retrieve documents based on specific criteria.store.getOne(collectionName, key) retrieves a single document that matches the provided key in the specified collection.
store.getMany(collectionName, selector, sort) retrieves multiple documents that match the provided selector in the specified collection.
Update Data
You can update data in the local store using these functions:
store.updateOne(collectionName, key, update) updates a single document that matches the provided key in the specified collection.
store.updateMany(collectionName, selector, update) updates multiple documents that match the provided selector in the specified collection.
Delete Data
You can delete data from the local data store using these functions:
store.deleteOne(collectionName, key) deletes a single document that matches the provided key in the specified collection.
store.deleteMany(collectionName, selector) deletes multiple documents that match the provided selector in the specified collection.
Events
You can subscribe to NewMessageRequest events by defining a function to handle the event and registering it with the events object:
In this example, we're defining a function called handleMessageRequest that takes a NewMessageRequest object as its argument. We're also defining a function called registerListener that registers the handleMessageRequest function as a listener for NewMessageRequest events using the on method of the events object.
Preferences
To register plugin preferences, you can use the preferences object from the @janhq/plugin-core package. Here's an example of how to register and retrieve plugin preferences:
In this example, we're registering preference update handlers and plugin preferences using the preferences object. We're also defining a PluginName constant to use as the name of the plugin.
To retrieve the values of the registered preferences, we're using the get method of the preferences object and passing in the name of the plugin and the name of the preference.
Access Core API
To access the Core API in your plugin, you can follow the code examples and explanations provided below.
Import Core API and Store Module
In your main entry code (e.g.,
index.ts
), start by importing the necessary modules and functions from the@janhq/plugin-core
library.Perform File Operations
The Core API also provides functions to perform file operations. Here are a couple of examples:
Download a File
You can download a file from a specified URL and save it with a given file name using the core.downloadFile function.
Delete a File
To delete a file, you can use the core.deleteFile function, providing the path to the file you want to delete.
Execute plugin module in main process
To execute a plugin module in the main process of your application, you can follow the steps outlined below.
Import the
core
ObjectIn your main process code (e.g.,
index.ts
), start by importing thecore
object from the@janhq/plugin-core
library.Define the Module Path
Specify the path to the plugin module you want to execute. This path should lead to the module file (e.g., module.js) that contains the functions you wish to call.
Define the Function to Execute
Create a function that will execute a function defined in your plugin module. In the example provided, the function
getConversationMessages
is created to invoke thegetConvMessages
function from the plugin module.Define Your Plugin Module
In your plugin module (e.g., module.ts), define the logic for the function you wish to execute. In the example, the function getConvMessages is defined with a placeholder comment indicating where your logic should be implemented.
CoreService API
The
CoreService
type is an exported union type that includes:StoreService
DataService
InferenceService
ModelManagementService
SystemMonitoringService
PreferenceService
StoreService
The
StoreService
enum represents available methods for managing the database store. It includes the following methods:CreateCollection
: Creates a new collection in the data store.DeleteCollection
: Deletes an existing collection from the data store.InsertOne
: Inserts a new value into an existing collection in the data store.UpdateOne
: Updates an existing value in an existing collection in the data store.UpdateMany
: Updates multiple records in a collection in the data store.DeleteOne
: Deletes an existing value from an existing collection in the data store.DeleteMany
: Deletes multiple records in a collection in the data store.FindMany
: Retrieves multiple records from a collection in the data store.FindOne
: Retrieves a single record from a collection in the data store.DataService
The
DataService
enum represents methods related to managing conversations and messages. It includes the following methods:GetConversations
: Gets a list of conversations from the data store.CreateConversation
: Creates a new conversation in the data store.DeleteConversation
: Deletes an existing conversation from the data store.CreateMessage
: Creates a new message in an existing conversation in the data store.UpdateMessage
: Updates an existing message in an existing conversation in the data store.GetConversationMessages
: Gets a list of messages for an existing conversation from the data store.InferenceService
The
InferenceService
enum exports:InitModel
: Initializes a model for inference.StopModel
: Stops a running inference model.ModelManagementService
The
ModelManagementService
enum provides methods for managing models:GetDownloadedModels
: Gets a list of downloaded models.GetAvailableModels
: Gets a list of available models from data store.DeleteModel
: Deletes a downloaded model.DownloadModel
: Downloads a model from the server.SearchModels
: Searches for models on the server.GetConfiguredModels
: Gets configured models from the data store.StoreModel
: Stores a model in the data store.UpdateFinishedDownloadAt
: Updates the finished download time for a model in the data store.GetUnfinishedDownloadModels
: Gets a list of unfinished download models from the data store.GetFinishedDownloadModels
: Gets a list of finished download models from the data store.DeleteDownloadModel
: Deletes a downloaded model from the data store.GetModelById
: Gets a model by its ID from the data store.PreferenceService
The
PreferenceService
enum provides methods for managing plugin preferences:ExperimentComponent
: Represents the UI experiment component for a testing function.SystemMonitoringService
The
SystemMonitoringService
enum includes methods for monitoring system resources:GetResourcesInfo
: Gets information about system resources.GetCurrentLoad
: Gets the current system load.PluginService
The
PluginService
enum includes plugin cycle handlers:OnStart
: Handler for starting. E.g. Create a collection.OnPreferencesUpdate
: Handler for preferences update. E.g. Update instances with new configurations.For more detailed information on each of these components, please refer to the source code.