Skip to content

Commit

Permalink
feat: adding create bot functionality (janhq#368)
Browse files Browse the repository at this point in the history
* feat: adding create bot functionality

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

* update the temperature progress bar

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

* chore: remove tgz

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

* update core dependency

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

* fix e2e test

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

---------

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
  • Loading branch information
namchuai and James authored Oct 23, 2023
1 parent ec9e41e commit 6e2210c
Show file tree
Hide file tree
Showing 93 changed files with 1,904 additions and 579 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ package-lock.json

*.log
plugin-core/lib
core/lib/**
2 changes: 1 addition & 1 deletion electron/tests/explore.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.afterAll(async () => {
});

test("explores models", async () => {
await page.getByRole("button", { name: "Explore Models" }).first().click();
await page.getByTestId("Explore Models").first().click();
const header = await page
.getByRole("heading")
.filter({ hasText: "Explore Models" })
Expand Down
2 changes: 1 addition & 1 deletion electron/tests/my-models.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.afterAll(async () => {
});

test("shows my models", async () => {
await page.getByRole("button", { name: "My Models" }).first().click();
await page.getByTestId("My Models").first().click();
const header = await page
.getByRole("heading")
.filter({ hasText: "My Models" })
Expand Down
15 changes: 5 additions & 10 deletions electron/tests/navigation.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,26 @@ test("renders left navigation panel", async () => {
expect(chatSection).toBe(false);

// Home actions
const newChatBtn = await page
.getByRole("button", { name: "New Chat" })
const createBotBtn = await page
.getByRole("button", { name: "Create bot" })
.first()
.isEnabled();
const exploreBtn = await page
.getByRole("button", { name: "Explore Models" })
.first()
.isEnabled();
const discordBtn = await page
.getByRole("button", { name: "Discord" })
.first()
.isEnabled();
const myModelsBtn = await page
.getByRole("button", { name: "My Models" })
.getByTestId("My Models")
.first()
.isEnabled();
const settingsBtn = await page
.getByRole("button", { name: "Settings" })
.getByTestId("Settings")
.first()
.isEnabled();
expect(
[
newChatBtn,
createBotBtn,
exploreBtn,
discordBtn,
myModelsBtn,
settingsBtn,
].filter((e) => !e).length
Expand Down
2 changes: 1 addition & 1 deletion electron/tests/settings.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.afterAll(async () => {
});

test("shows settings", async () => {
await page.getByRole("button", { name: "Settings" }).first().click();
await page.getByTestId("Settings").first().click();

const pluginList = await page.getByTestId("plugin-item").count();
expect(pluginList).toBe(4);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dev:electron": "yarn workspace jan dev",
"dev:web": "yarn workspace jan-web dev",
"dev": "concurrently --kill-others \"yarn dev:web\" \"wait-on http://localhost:3000 && yarn dev:electron\"",
"test-local": "yarn lint && yarn build && yarn test",
"build:core": "cd core && yarn install && yarn run build",
"build:web": "yarn workspace jan-web build && cpx \"web/out/**\" \"electron/renderer/\"",
"build:electron": "yarn workspace jan build",
Expand Down
179 changes: 161 additions & 18 deletions plugins/data-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { core, store, RegisterExtensionPoint, StoreService, DataService, PluginService } from "@janhq/core";
import {
invokePluginFunc,
store,
RegisterExtensionPoint,
StoreService,
DataService,
PluginService,
} from "@janhq/core";

/**
* Create a collection on data store
Expand All @@ -8,8 +15,14 @@ import { core, store, RegisterExtensionPoint, StoreService, DataService, PluginS
* @returns Promise<void>
*
*/
function createCollection({ name, schema }: { name: string; schema?: { [key: string]: any } }): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "createCollection", name, schema);
function createCollection({
name,
schema,
}: {
name: string;
schema?: { [key: string]: any };
}): Promise<void> {
return invokePluginFunc(MODULE_PATH, "createCollection", name, schema);
}

/**
Expand All @@ -20,7 +33,7 @@ function createCollection({ name, schema }: { name: string; schema?: { [key: str
*
*/
function deleteCollection(name: string): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "deleteCollection", name);
return invokePluginFunc(MODULE_PATH, "deleteCollection", name);
}

/**
Expand All @@ -31,8 +44,14 @@ function deleteCollection(name: string): Promise<void> {
* @returns Promise<any>
*
*/
function insertOne({ collectionName, value }: { collectionName: string; value: any }): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "insertOne", collectionName, value);
function insertOne({
collectionName,
value,
}: {
collectionName: string;
value: any;
}): Promise<any> {
return invokePluginFunc(MODULE_PATH, "insertOne", collectionName, value);
}

/**
Expand All @@ -44,8 +63,16 @@ function insertOne({ collectionName, value }: { collectionName: string; value: a
* @returns Promise<void>
*
*/
function updateOne({ collectionName, key, value }: { collectionName: string; key: string; value: any }): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "updateOne", collectionName, key, value);
function updateOne({
collectionName,
key,
value,
}: {
collectionName: string;
key: string;
value: any;
}): Promise<void> {
return invokePluginFunc(MODULE_PATH, "updateOne", collectionName, key, value);
}

/**
Expand All @@ -64,7 +91,13 @@ function updateMany({
value: any;
selector?: { [key: string]: any };
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "updateMany", collectionName, value, selector);
return invokePluginFunc(
MODULE_PATH,
"updateMany",
collectionName,
value,
selector
);
}

/**
Expand All @@ -75,8 +108,14 @@ function updateMany({
* @returns Promise<void>
*
*/
function deleteOne({ collectionName, key }: { collectionName: string; key: string }): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "deleteOne", collectionName, key);
function deleteOne({
collectionName,
key,
}: {
collectionName: string;
key: string;
}): Promise<void> {
return invokePluginFunc(MODULE_PATH, "deleteOne", collectionName, key);
}

/**
Expand All @@ -94,7 +133,7 @@ function deleteMany({
collectionName: string;
selector?: { [key: string]: any };
}): Promise<void> {
return core.invokePluginFunc(MODULE_PATH, "deleteMany", collectionName, selector);
return invokePluginFunc(MODULE_PATH, "deleteMany", collectionName, selector);
}

/**
Expand All @@ -103,8 +142,14 @@ function deleteMany({
* @param {string} key - The key of the record to retrieve.
* @returns {Promise<any>} A promise that resolves when the record is retrieved.
*/
function findOne({ collectionName, key }: { collectionName: string; key: string }): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "findOne", collectionName, key);
function findOne({
collectionName,
key,
}: {
collectionName: string;
key: string;
}): Promise<any> {
return invokePluginFunc(MODULE_PATH, "findOne", collectionName, key);
}

/**
Expand All @@ -123,19 +168,28 @@ function findMany({
selector: { [key: string]: any };
sort?: [{ [key: string]: any }];
}): Promise<any> {
return core.invokePluginFunc(MODULE_PATH, "findMany", collectionName, selector, sort);
return invokePluginFunc(
MODULE_PATH,
"findMany",
collectionName,
selector,
sort
);
}

function onStart() {
createCollection({ name: "conversations", schema: {} });
createCollection({ name: "messages", schema: {} });
createCollection({ name: "bots", schema: {} });
}

// Register all the above functions and objects with the relevant extension points
// prettier-ignore
export function init({ register }: { register: RegisterExtensionPoint }) {
register(PluginService.OnStart, PLUGIN_NAME, onStart);
register(StoreService.CreateCollection, createCollection.name, createCollection);
register(StoreService.DeleteCollection, deleteCollection.name, deleteCollection);

register(StoreService.InsertOne, insertOne.name, insertOne);
register(StoreService.UpdateOne, updateOne.name, updateOne);
register(StoreService.UpdateMany, updateMany.name, updateMany);
Expand All @@ -144,19 +198,34 @@ export function init({ register }: { register: RegisterExtensionPoint }) {
register(StoreService.FindOne, findOne.name, findOne);
register(StoreService.FindMany, findMany.name, findMany);

// for conversations management
register(DataService.GetConversations, getConversations.name, getConversations);
register(DataService.GetConversationById,getConversationById.name,getConversationById);
register(DataService.CreateConversation, createConversation.name, createConversation);
register(DataService.UpdateConversation, updateConversation.name, updateConversation);
register(DataService.UpdateMessage, updateMessage.name, updateMessage);
register(DataService.DeleteConversation, deleteConversation.name, deleteConversation);

// for messages management
register(DataService.UpdateMessage, updateMessage.name, updateMessage);
register(DataService.CreateMessage, createMessage.name, createMessage);
register(DataService.GetConversationMessages, getConversationMessages.name, getConversationMessages);

// for bots management
register(DataService.CreateBot, createBot.name, createBot);
register(DataService.GetBots, getBots.name, getBots);
register(DataService.GetBotById, getBotById.name, getBotById);
register(DataService.DeleteBot, deleteBot.name, deleteBot);
register(DataService.UpdateBot, updateBot.name, updateBot);
}

function getConversations(): Promise<any> {
return store.findMany("conversations", {}, [{ updatedAt: "desc" }]);
}

function getConversationById(id: string): Promise<any> {
return store.findOne("conversations", id);
}

function createConversation(conversation: any): Promise<number | undefined> {
return store.insertOne("conversations", conversation);
}
Expand All @@ -174,9 +243,83 @@ function updateMessage(message: any): Promise<void> {
}

function deleteConversation(id: any) {
return store.deleteOne("conversations", id).then(() => store.deleteMany("messages", { conversationId: id }));
return store
.deleteOne("conversations", id)
.then(() => store.deleteMany("messages", { conversationId: id }));
}

function getConversationMessages(conversationId: any) {
return store.findMany("messages", { conversationId }, [{ createdAt: "desc" }]);
return store.findMany("messages", { conversationId }, [
{ createdAt: "desc" },
]);
}

function createBot(bot: any): Promise<void> {
console.debug("Creating bot", JSON.stringify(bot, null, 2));
return store
.insertOne("bots", bot)
.then(() => {
console.debug("Bot created", JSON.stringify(bot, null, 2));
return Promise.resolve();
})
.catch((err) => {
console.error("Error creating bot", err);
return Promise.reject(err);
});
}

function getBots(): Promise<any> {
console.debug("Getting bots");
return store
.findMany("bots", { name: { $gt: null } })
.then((bots) => {
console.debug("Bots retrieved", JSON.stringify(bots, null, 2));
return Promise.resolve(bots);
})
.catch((err) => {
console.error("Error getting bots", err);
return Promise.reject(err);
});
}

function deleteBot(id: string): Promise<any> {
console.debug("Deleting bot", id);
return store
.deleteOne("bots", id)
.then(() => {
console.debug("Bot deleted", id);
return Promise.resolve();
})
.catch((err) => {
console.error("Error deleting bot", err);
return Promise.reject(err);
});
}

function updateBot(bot: any): Promise<void> {
console.debug("Updating bot", JSON.stringify(bot, null, 2));
return store
.updateOne("bots", bot._id, bot)
.then(() => {
console.debug("Bot updated");
return Promise.resolve();
})
.catch((err) => {
console.error("Error updating bot", err);
return Promise.reject(err);
});
}

function getBotById(botId: string): Promise<any> {
console.debug("Getting bot", botId);
return store
.findOne("bots", botId)
.then((bot) => {
console.debug("Bot retrieved", JSON.stringify(bot, null, 2));
return Promise.resolve(bot);
})
.catch((err) => {
console.error("Error getting bot", err);
return Promise.reject(err);
});
}
10 changes: 7 additions & 3 deletions plugins/data-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"scripts": {
"build": "tsc -b ./config/tsconfig.esm.json && tsc -b ./config/tsconfig.cjs.json && webpack --config webpack.config.js",
"postinstall": "rimraf ./data-plugin*.tgz && npm run build",
"postinstall": "rimraf *.tgz --glob && npm run build",
"build:publish": "npm pack && cpx *.tgz ../../electron/core/pre-install"
},
"exports": {
Expand Down Expand Up @@ -40,8 +40,12 @@
"node_modules"
],
"dependencies": {
"@janhq/core": "^0.1.3",
"@janhq/core": "^0.1.6",
"pouchdb-find": "^8.0.1",
"pouchdb-node": "^8.0.1"
}
},
"bundleDependencies": [
"pouchdb-node",
"pouchdb-find"
]
}
Loading

0 comments on commit 6e2210c

Please sign in to comment.