Skip to content

Commit

Permalink
Switch to using Store API
Browse files Browse the repository at this point in the history
  • Loading branch information
JinayJain committed Mar 9, 2023
1 parent 377849e commit 1ba21d6
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 53 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"react-syntax-highlighter": "^15.5.0",
"rehype-katex": "^6.0.2",
"remark-breaks": "^3.0.2",
"remark-math": "^5.1.1"
"remark-math": "^5.1.1",
"tauri-plugin-store-api": "https://github.com/tauri-apps/tauri-plugin-store"
},
"devDependencies": {
"@tauri-apps/cli": "^1.2.2",
Expand Down
13 changes: 13 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ edition = "2021"
tauri-build = { version = "1.2", features = [] }

[dependencies]
tauri = { version = "1.2", features = ["clipboard-all", "fs-exists", "fs-read-file", "fs-write-file", "global-shortcut-all", "shell-open", "system-tray", "window-close", "window-hide", "window-maximize", "window-minimize", "window-set-always-on-top", "window-set-focus", "window-show", "window-start-dragging", "window-unmaximize", "window-unminimize"] }
tauri = { version = "1.2", features = ["clipboard-all", "global-shortcut-all", "shell-open", "system-tray", "window-close", "window-hide", "window-maximize", "window-minimize", "window-set-always-on-top", "window-set-focus", "window-show", "window-start-dragging", "window-unmaximize", "window-unminimize"] }
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn main() {
let sys_tray = build_system_tray();

tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::default().build())
.system_tray(sys_tray)
.setup(|app| {
// get window
Expand Down
15 changes: 1 addition & 14 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
},
"allowlist": {
"all": false,
"fs": {
"all": false,
"copyFile": false,
"createDir": false,
"exists": true,
"readDir": false,
"readFile": true,
"removeDir": false,
"removeFile": false,
"renameFile": false,
"writeFile": true,
"scope": ["$APPDATA/*"]
},
"clipboard": {
"all": true
},
Expand Down Expand Up @@ -63,7 +50,7 @@
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "dev.jinay.gpt-anywhere",
"identifier": "dev.jinay.slash-gpt",
"targets": "all"
},
"security": {
Expand Down
5 changes: 2 additions & 3 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Search from "../components/Search";
import { Box, Heading } from "@chakra-ui/react";
import { Box } from "@chakra-ui/react";
import ResponseBox from "../components/ResponseBox";
import { useEffect, useState } from "react";
import { useState } from "react";
import { fillerMarkdown } from "../util/consts";
import { chatComplete } from "../util/openai";
import { listen, TauriEvent } from "@tauri-apps/api/event";

const CLEAR_TEXT = "";
// const CLEAR_TEXT = fillerMarkdown;
Expand Down
24 changes: 8 additions & 16 deletions src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,20 @@ import {
useToast,
} from "@chakra-ui/react";
import { useEffect, useState } from "react";
import {
BaseDirectory,
exists,
readTextFile,
writeTextFile,
} from "@tauri-apps/api/fs";
import { API_KEY_FILE } from "../util/consts";
import { STORE_KEY } from "../util/consts";
import store from "../util/store";

function Settings() {
const toast = useToast();
const [apiKey, setApiKey] = useState<string | null>(null);

useEffect(() => {
const populateApiKey = async () => {
if (!exists(API_KEY_FILE, { dir: BaseDirectory.AppData })) return;

const key = await readTextFile(API_KEY_FILE, {
dir: BaseDirectory.AppData,
});
const key: string | null = await store.get(STORE_KEY.API_KEY);

setApiKey(key);
if (key) {
setApiKey(key);
}
};

populateApiKey();
Expand All @@ -41,9 +34,8 @@ function Settings() {
const handleSave = async () => {
if (!apiKey) return;

await writeTextFile("api-key.txt", apiKey, {
dir: BaseDirectory.AppData,
});
await store.set(STORE_KEY.API_KEY, apiKey);
await store.save();

toast({
title: "Saved",
Expand Down
16 changes: 8 additions & 8 deletions src/util/consts.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const PROMPTS = {
export const PROMPTS = {
base: "You are a friendly AI assistant. Answer only the question being asked in a concise manner. Use Markdown, KaTeX, escape sequences, and emojis when appropriate.",
linuxTerminal:
"Act as a hypothetical Linux terminal. Only provide the output of commands I give you. Do not include anything else. Use Markdown code blocks to format output.",
python:
"Act as a hypothetical Python interpreter. Do not ask for more information. Assume all files/libraries are present. Only provide the output of code I give you. Do not include anything else. Use Markdown code blocks to format output.",
};
} as const;

const SYSTEM_PROMPT = PROMPTS.base;
const MAX_TOKENS = 300;
export const SYSTEM_PROMPT = PROMPTS.base;
export const MAX_TOKENS = 300;

const API_KEY_FILE = "api-key.txt";
export const STORE_KEY = {
API_KEY: "api_key",
};

const fillerMarkdown = `
export const fillerMarkdown = `
# Welcome to the React Markdown Previewer!
## This is a sub-heading...
Expand Down Expand Up @@ -62,5 +64,3 @@ And here. | Okay. | I think we get it.
![React Logo w/ Text](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png)
`;

export { fillerMarkdown, SYSTEM_PROMPT, MAX_TOKENS, API_KEY_FILE };
15 changes: 5 additions & 10 deletions src/util/openai.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { BaseDirectory, readTextFile } from "@tauri-apps/api/fs";
import { CreateChatCompletionRequest } from "openai";
import { SYSTEM_PROMPT, MAX_TOKENS, API_KEY_FILE } from "./consts";
import { SYSTEM_PROMPT, MAX_TOKENS, STORE_KEY } from "./consts";
import store from "./store";

type ApiParams = Omit<
CreateChatCompletionRequest,
"model" | "messages" | "stream"
>;

async function processLine(line: string) {
const sliced = line.replace(/^data: /, "");
const sliced = line.slice(6).trim();

if (sliced === "[DONE]") {
return {
done: true,
};
}

const data = JSON.parse(line.slice(6));
const data = JSON.parse(sliced);
const msg: string = data?.choices?.[0]?.delta?.content;

if (msg) {
Expand All @@ -33,13 +34,7 @@ async function processLine(line: string) {

async function sendApiRequest(prompt: string, apiParams?: ApiParams) {
// const OPENAI_API_KEY = import.meta.env.VITE_OPENAI_API_KEY;
console.log("API_KEY_FILE", API_KEY_FILE);

const OPENAI_API_KEY = await readTextFile(API_KEY_FILE, {
dir: BaseDirectory.AppData,
});

console.log("OPENAI_API_KEY", OPENAI_API_KEY);
const OPENAI_API_KEY = await store.get(STORE_KEY.API_KEY);

return await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
Expand Down
5 changes: 5 additions & 0 deletions src/util/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Store } from "tauri-plugin-store-api";

const store = new Store("store.dat");

export default store;
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,12 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

"tauri-plugin-store-api@https://github.com/tauri-apps/tauri-plugin-store":
version "0.0.0"
resolved "https://github.com/tauri-apps/tauri-plugin-store#201639d72bc8c4dec6f40ff7c02162c7926bfa3d"
dependencies:
"@tauri-apps/api" "^1.2.0"

tiny-invariant@^1.0.6:
version "1.3.1"
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.3.1.tgz#8560808c916ef02ecfd55e66090df23a4b7aa642"
Expand Down

0 comments on commit 1ba21d6

Please sign in to comment.