-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jucasoliveira/changes_on_terminal
add_changes_and_new_lib
- Loading branch information
Showing
24 changed files
with
1,047 additions
and
482 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,4 +110,6 @@ dist | |
*.txt | ||
*.jsonl | ||
.yarn | ||
lib | ||
lib | ||
|
||
credentials.json |
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 |
---|---|---|
@@ -1,70 +1,71 @@ | ||
import * as crypto from "../src/encrypt"; | ||
|
||
import fs from "fs"; | ||
|
||
import { expect } from "chai"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import { | ||
encrypt, | ||
decrypt, | ||
getCredentials, | ||
saveCredentials, | ||
deleteCredentials, | ||
} from "../src/creds"; | ||
|
||
import {expect} from "chai"; | ||
describe("Encryption and Decryption", () => { | ||
it("should return a string in the format 'IV:encrypted_text'", () => { | ||
const result = encrypt("test"); | ||
expect(result).to.be.a("string"); | ||
expect(result).to.include(":"); | ||
}); | ||
|
||
describe("encrypt()", () => { | ||
it("should return a string in the format 'IV:encrypted_text'", () => { | ||
const result = crypto.encrypt("test"); | ||
expect(result).to.match(/^[a-f0-9]{32}:([a-f0-9]{2})*$/); | ||
}); | ||
it("should return a different result each time it is called", () => { | ||
const result1 = encrypt("test"); | ||
const result2 = encrypt("test"); | ||
expect(result1).to.not.equal(result2); | ||
}); | ||
|
||
it("should return a different result each time it is called", () => { | ||
const result1 = crypto.encrypt("test"); | ||
const result2 = crypto.encrypt("test"); | ||
expect(result1).not.equal(result2); | ||
}); | ||
}); | ||
it("should return the original input text when given a valid encrypted string", () => { | ||
const encrypted = encrypt("test"); | ||
const decrypted = decrypt(encrypted); | ||
expect(decrypted).to.equal("test"); | ||
}); | ||
|
||
describe("decrypt()", () => { | ||
it("should return the original input text when given a valid encrypted string", () => { | ||
const encrypted = crypto.encrypt("test"); | ||
const result = crypto.decrypt(encrypted); | ||
expect(result).to.equal("test"); | ||
}); | ||
|
||
it("should throw an error when given an invalid encrypted string", () => { | ||
expect(() => crypto.decrypt("invalid")).to.throw(); | ||
}); | ||
it("should throw an error when given an invalid encrypted string", () => { | ||
expect(() => decrypt("invalid:string")).to.throw(); | ||
}); | ||
}); | ||
|
||
describe("Api Key ", () => { | ||
// the path to the apiKey.txt file should point to ../src/apiKey.txt | ||
const apiKeyPath = path.resolve(__dirname, "../src/apiKey.txt"); | ||
describe("Credentials Management", () => { | ||
const credentialsPath = path.resolve(__dirname, "../src/credentials.json"); | ||
|
||
const removeLink = () => { | ||
try { | ||
fs.unlinkSync(apiKeyPath) | ||
} catch { /* empty */ | ||
} | ||
} | ||
beforeEach(() => { | ||
deleteCredentials(); | ||
}); | ||
|
||
beforeEach(removeLink) | ||
afterEach(removeLink) | ||
afterEach(() => { | ||
deleteCredentials(); | ||
}); | ||
|
||
it("should return null if the API key has not been saved", () => { | ||
const result = crypto.getApiKey(); | ||
expect(result).to.equal(null); | ||
it("should return null values if credentials have not been saved", () => { | ||
const result = getCredentials(); | ||
expect(result).to.deep.equal({ | ||
apiKey: null, | ||
engine: null, | ||
tavilyApiKey: null, | ||
}); | ||
}); | ||
|
||
it("should return the API key if it has been saved", () => { | ||
const encrypted = crypto.encrypt("test"); | ||
crypto.saveApiKey(encrypted) | ||
const result = crypto.getApiKey(); | ||
expect(result).to.equal("test"); | ||
}); | ||
it("should save and retrieve credentials correctly", () => { | ||
saveCredentials("testApiKey", "testEngine", "testTavilyApiKey"); | ||
const result = getCredentials(); | ||
expect(result.apiKey).to.equal("testApiKey"); | ||
expect(result.engine).to.equal("testEngine"); | ||
expect(result.tavilyApiKey).to.equal("testTavilyApiKey"); | ||
}); | ||
|
||
it("should save the given API key to a file", () => { | ||
if (!fs.existsSync(apiKeyPath)) { | ||
fs.writeFileSync(apiKeyPath, ""); | ||
} | ||
const encryptedText = crypto.encrypt("test"); | ||
crypto.saveApiKey(encryptedText); | ||
const result = crypto.getApiKey()// fs.readFileSync(apiKeyPath, "utf8"); | ||
|
||
expect(result).to.equal("test"); | ||
}); | ||
it("should encrypt the API key when saving", () => { | ||
saveCredentials("testApiKey", "testEngine", "testTavilyApiKey"); | ||
const rawData = fs.readFileSync(credentialsPath, "utf-8"); | ||
const savedCredentials = JSON.parse(rawData); | ||
expect(savedCredentials.apiKey).to.not.equal("testApiKey"); | ||
expect(decrypt(savedCredentials.apiKey)).to.equal("testApiKey"); | ||
}); | ||
}); |
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
Empty file.
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,5 @@ | ||
const clearFunc = () => { | ||
process.stdout.write("\x1Bc"); | ||
}; | ||
|
||
export default clearFunc; |
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,8 @@ | ||
import chalk from "chalk"; | ||
|
||
const exitFunc = () => { | ||
console.log(chalk.yellow("Goodbye!")); | ||
process.exit(0); | ||
}; | ||
|
||
export default exitFunc; |
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,22 @@ | ||
import chalk from "chalk"; | ||
import { handleFileReference } from "../handlers/fileHandler"; // Assuming this function exists | ||
import { apiKeyPrompt, promptResponse } from "../utils"; // Assuming this function exists | ||
|
||
const fileFunc = async (userInput: string) => { | ||
const creds = await apiKeyPrompt(); | ||
// we need to call file handler here | ||
const [, filePath, ...promptParts] = userInput.split(" "); | ||
const promptText = promptParts.join(" "); | ||
if (filePath) { | ||
await handleFileReference(filePath, promptText); | ||
if (creds.apiKey != null) { | ||
await promptResponse(creds.engine, creds.apiKey, userInput, {}); | ||
} | ||
} else { | ||
console.log( | ||
chalk.yellow("Please provide a file path. Usage: @file <path> [prompt]") | ||
); | ||
} | ||
}; | ||
|
||
export default fileFunc; |
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,43 @@ | ||
import exitFunc from "./exit"; | ||
import clearFunc from "./clear"; | ||
import fileFunc from "./file"; | ||
import webFunc from "./web"; | ||
|
||
export interface Plugin { | ||
keyword: string; | ||
execute: (userInput: string) => Promise<void> | void; | ||
} | ||
|
||
const plugins: Plugin[] = []; | ||
|
||
const registerPlugin = ( | ||
keyword: string, | ||
execute: (userInput: string) => Promise<void> | void | ||
) => { | ||
plugins.push({ keyword, execute }); | ||
}; | ||
|
||
export const mapPlugins = (userInput: string): Plugin | undefined => { | ||
return plugins.find((plugin) => userInput.startsWith(plugin.keyword)); | ||
}; | ||
|
||
export const initializePlugins = () => { | ||
// Register your plugins here | ||
registerPlugin("exit", exitFunc); | ||
registerPlugin("clear", clearFunc); | ||
registerPlugin("@file", fileFunc); | ||
registerPlugin("@web", webFunc); | ||
}; | ||
|
||
export const executeCommand = (userInput: string): boolean => { | ||
const command = plugins.find((plugin) => | ||
userInput.startsWith(plugin.keyword) | ||
); | ||
if (command) { | ||
command.execute(userInput); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
export default executeCommand; |
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,20 @@ | ||
import chalk from "chalk"; | ||
import { handleWebResearch } from "../handlers/webHandler"; | ||
import { promptResponse, apiKeyPrompt } from "../utils"; | ||
|
||
const webFunc = async (userInput: string) => { | ||
const creds = await apiKeyPrompt(); | ||
const query = userInput.slice(5).trim(); | ||
if (query) { | ||
await handleWebResearch(query, userInput); | ||
if (creds.apiKey != null) { | ||
await promptResponse(creds.engine, creds.apiKey, userInput, {}); | ||
} | ||
} else { | ||
console.log( | ||
chalk.yellow("Please provide a search query. Usage: @web <query>") | ||
); | ||
} | ||
}; | ||
|
||
export default webFunc; |
Oops, something went wrong.