Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Persistent Storage preference #56

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ This should be enough for most users, but similarly you can also create multiple
Let GPT automatically come up with a name for the current chat session after you send the first message. For example, this is similar to what the ChatGPT web UI does.
- **How to enable**: Go to the extension preferences and check the "Enable Smart Chat Naming" box.

### Automatically Check for Updates (Beta)
### Automatically Check for Updates
Let the extension automatically check for updates every day. If a new version is available, you will be notified,
along with the option to update the extension with a single click.

### Persistent Storage
Enable more persistent storage of the extension's data, like AI Chat data or Custom Commands.
This will back up a copy of this data to files on your computer.

---

## FAQs
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,20 @@
},
{
"name": "autoCheckForUpdates",
"label": "Automatically Check for Updates (BETA)",
"label": "Automatically Check for Updates",
"description": "Automatically check for updates from the official GitHub source.",
"required": false,
"type": "checkbox",
"default": false
},
{
"name": "persistentStorage",
"label": "Persistent Storage",
"description": "Enable more persistent storage of the extension's data.",
"required": false,
"type": "checkbox",
"default": false
},
{
"name": "TavilyAPIKeys",
"title": "Tavily API Keys",
Expand Down
27 changes: 17 additions & 10 deletions src/api/storage.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// This is the Storage interface, which combines LocalStorage with more persistent file system storage.
// We generally also keep the two versions in sync, but the sync process happens only periodically
// and thus the file storage should not be relied upon for immediate consistency.
// If the persistent storage preference is enabled, we also keep the two versions in sync,
// but the sync process happens only periodically so the file storage should not be relied upon for immediate consistency.
// Note that persistent storage is not always the best option as file I/O is relatively expensive.
// Thus, only use it when you really need to persist data across sessions.
// Thus, only use it when you really need to persist data across sessions. Otherwise, use local storage.

import { environment, LocalStorage } from "@raycast/api";
import { environment, getPreferenceValues, LocalStorage } from "@raycast/api";
import fs from "fs";

const not_found = (x) => x === undefined || x === null;
const found = (x) => !not_found(x);

export const Storage = {
// whether to enable persistent/combined storage
persistent: getPreferenceValues()["persistentStorage"] || false,

/// Local storage functions - these provide quicker access that is not critical to persist

// check if item exists in local storage
Expand Down Expand Up @@ -105,8 +108,10 @@ export const Storage = {
// first write to local storage function only, and then add key to sync cache to add to file storage later
write: async (key, value) => {
await Storage.localStorage_write(key, value);
await Storage.add_to_sync_cache(key);
await Storage.run_sync();
if (Storage.persistent) {
await Storage.add_to_sync_cache(key);
await Storage.run_sync();
}
},

// combined read function - read from local storage, fallback to file storage.
Expand All @@ -116,17 +121,19 @@ export const Storage = {
if (await Storage.localStorage_has(key)) {
value = await Storage.localStorage_read(key);
// note how we only sync here, as it only makes sense when we have a value in local storage
await Storage.add_to_sync_cache(key);
await Storage.run_sync();
} else if (await Storage.fileStorage_has(key)) {
if (Storage.persistent) {
await Storage.add_to_sync_cache(key);
await Storage.run_sync();
}
} else if (Storage.persistent && (await Storage.fileStorage_has(key))) {
console.log(`Reading key: ${key} from file storage`);
value = await Storage.fileStorage_read(key);
// write to local storage
await Storage.localStorage_write(key, value);
} else {
console.log(`Key: ${key} not found, returning default value`);
value = default_value;
// write to both local and file storage
// write default key to storage
if (value !== undefined) await Storage.write(key, value);
}
return value;
Expand Down
Loading