-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
256 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@janhq/conversational-json", | ||
"version": "1.0.0", | ||
"description": "Conversational Plugin - Stores jan app conversations as JSON", | ||
"main": "dist/index.js", | ||
"author": "Jan <[email protected]>", | ||
"license": "MIT", | ||
"activationPoints": [ | ||
"init" | ||
], | ||
"scripts": { | ||
"build": "tsc -b . && webpack --config webpack.config.js", | ||
"postinstall": "rimraf *.tgz --glob && npm run build", | ||
"build:publish": "npm pack && cpx *.tgz ../../electron/core/pre-install" | ||
}, | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./main": "./dist/module.js" | ||
}, | ||
"devDependencies": { | ||
"cpx": "^1.5.0", | ||
"rimraf": "^3.0.2", | ||
"webpack": "^5.88.2", | ||
"webpack-cli": "^5.1.4" | ||
}, | ||
"dependencies": { | ||
"@janhq/core": "file:../../core", | ||
"ts-loader": "^9.5.0" | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"files": [ | ||
"dist/*", | ||
"package.json", | ||
"README.md" | ||
], | ||
"bundleDependencies": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { PluginType, fs } from "@janhq/core"; | ||
import { ConversationalPlugin } from "@janhq/core/lib/plugins"; | ||
import { Conversation } from "@janhq/core/lib/types"; | ||
|
||
/** | ||
* JSONConversationalPlugin is a ConversationalPlugin implementation that provides | ||
* functionality for managing conversations. | ||
*/ | ||
export default class JSONConversationalPlugin implements ConversationalPlugin { | ||
/** | ||
* Returns the type of the plugin. | ||
*/ | ||
type(): PluginType { | ||
return PluginType.Conversational; | ||
} | ||
|
||
/** | ||
* Called when the plugin is loaded. | ||
*/ | ||
onLoad() { | ||
fs.mkdir("conversations") | ||
console.debug("JSONConversationalPlugin loaded") | ||
} | ||
|
||
/** | ||
* Called when the plugin is unloaded. | ||
*/ | ||
onUnload() { | ||
console.debug("JSONConversationalPlugin unloaded") | ||
} | ||
|
||
/** | ||
* Returns a Promise that resolves to an array of Conversation objects. | ||
*/ | ||
getConversations(): Promise<Conversation[]> { | ||
return this.getConversationDocs().then((conversationIds) => | ||
Promise.all( | ||
conversationIds.map((conversationId) => | ||
fs | ||
.readFile(`conversations/${conversationId}/${conversationId}.json`) | ||
.then((data) => { | ||
return JSON.parse(data) as Conversation; | ||
}) | ||
) | ||
).then((conversations) => | ||
conversations.sort( | ||
(a, b) => | ||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() | ||
) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Saves a Conversation object to a Markdown file. | ||
* @param conversation The Conversation object to save. | ||
*/ | ||
saveConversation(conversation: Conversation): Promise<void> { | ||
return fs | ||
.mkdir(`conversations/${conversation._id}`) | ||
.then(() => | ||
fs.writeFile( | ||
`conversations/${conversation._id}/${conversation._id}.json`, | ||
JSON.stringify(conversation) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Deletes a conversation with the specified ID. | ||
* @param conversationId The ID of the conversation to delete. | ||
*/ | ||
deleteConversation(conversationId: string): Promise<void> { | ||
return fs.rmdir(`conversations/${conversationId}`); | ||
} | ||
|
||
/** | ||
* Returns a Promise that resolves to an array of conversation IDs. | ||
* The conversation IDs are the names of the Markdown files in the "conversations" directory. | ||
* @private | ||
*/ | ||
private async getConversationDocs(): Promise<string[]> { | ||
return fs.listFiles(`conversations`).then((files: string[]) => { | ||
return Promise.all(files.filter((file) => file.startsWith("jan-"))); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2016", | ||
"module": "ES6", | ||
"moduleResolution": "node", | ||
"outDir": "./dist", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": false, | ||
"skipLibCheck": true, | ||
"rootDir": "./src" | ||
}, | ||
"include": ["./src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const path = require("path"); | ||
const webpack = require("webpack"); | ||
|
||
module.exports = { | ||
experiments: { outputModule: true }, | ||
entry: "./src/index.ts", // Adjust the entry point to match your project's main file | ||
mode: "production", | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: "ts-loader", | ||
exclude: /node_modules/, | ||
}, | ||
], | ||
}, | ||
output: { | ||
filename: "index.js", // Adjust the output file name as needed | ||
path: path.resolve(__dirname, "dist"), | ||
library: { type: "module" }, // Specify ESM output format | ||
}, | ||
plugins: [new webpack.DefinePlugin({})], | ||
resolve: { | ||
extensions: [".ts", ".js"], | ||
}, | ||
// Do not minify the output, otherwise it breaks the class registration | ||
optimization: { | ||
minimize: false, | ||
}, | ||
// Add loaders and other configuration as needed for your project | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ChatMessage } from '@models/ChatMessage' | ||
|
||
/** | ||
* Util function to merge two array of messages and remove duplicates. | ||
* Also preserve the order | ||
* | ||
* @param arr1 Message array 1 | ||
* @param arr2 Message array 2 | ||
* @returns Merged array of messages | ||
*/ | ||
export function mergeAndRemoveDuplicates( | ||
arr1: ChatMessage[], | ||
arr2: ChatMessage[] | ||
): ChatMessage[] { | ||
const mergedArray = arr1.concat(arr2) | ||
const uniqueIdMap = new Map<string, boolean>() | ||
const result: ChatMessage[] = [] | ||
|
||
for (const message of mergedArray) { | ||
if (!uniqueIdMap.has(message.id)) { | ||
uniqueIdMap.set(message.id, true) | ||
result.push(message) | ||
} | ||
} | ||
|
||
return result.reverse() | ||
} | ||
|
||
export const generateMessageId = () => { | ||
return `m-${Date.now()}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.