Skip to content

Commit

Permalink
Allow separate directories for each config part, add dropdown for edi…
Browse files Browse the repository at this point in the history
…ting AI presets (#1074)

Adds new functionality on the backend that will merge any file from the
config directory that matches `<partName>.json` or `<partName>/*.json`
into the corresponding config part (presets, termthemes, etc.). This
lets us separate the AI presets into `presets/ai.json` so that we can
add a dropdown in the AI preset selector that will directly open the
file so a user can edit it more easily. Right now, this will create a
preview block in the layout, but in the future we can look into making
this block disconnected from the layout.

If you put AI presets in the regular presets.json file, it will still
work, since all the presets get merged. Same for any other config part.
  • Loading branch information
esimkowitz authored Oct 21, 2024
1 parent 613a583 commit 39fff9e
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 75 deletions.
9 changes: 8 additions & 1 deletion cmd/server/main-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,18 @@ func main() {
log.Printf("error ensuring wave db dir: %v\n", err)
return
}
err = wavebase.EnsureWaveConfigDir()
err = wconfig.EnsureWaveConfigDir()
if err != nil {
log.Printf("error ensuring wave config dir: %v\n", err)
return
}

// TODO: rather than ensure this dir exists, we should let the editor recursively create parent dirs on save
err = wconfig.EnsureWavePresetsDir()
if err != nil {
log.Printf("error ensuring wave presets dir: %v\n", err)
return
}
waveLock, err := wavebase.AcquireWaveLock()
if err != nil {
log.Printf("error acquiring wave lock (another instance of Wave is likely running): %v\n", err)
Expand Down
37 changes: 20 additions & 17 deletions emain/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ const unamePlatform = process.platform;
const unameArch: string = process.arch;
keyutil.setKeyUtilPlatform(unamePlatform);

ipcMain.on("get-is-dev", (event) => {
event.returnValue = isDev;
});
ipcMain.on("get-platform", (event, url) => {
event.returnValue = unamePlatform;
});
ipcMain.on("get-user-name", (event) => {
const userInfo = os.userInfo();
event.returnValue = userInfo.username;
});
ipcMain.on("get-host-name", (event) => {
event.returnValue = os.hostname();
});
ipcMain.on("get-webview-preload", (event) => {
event.returnValue = path.join(getElectronAppBasePath(), "preload", "preload-webview.cjs");
});

// must match golang
function getWaveHomeDir() {
const override = process.env[WaveHomeVarName];
Expand Down Expand Up @@ -72,6 +55,26 @@ function getWaveSrvCwd(): string {
return getWaveHomeDir();
}

ipcMain.on("get-is-dev", (event) => {
event.returnValue = isDev;
});
ipcMain.on("get-platform", (event, url) => {
event.returnValue = unamePlatform;
});
ipcMain.on("get-user-name", (event) => {
const userInfo = os.userInfo();
event.returnValue = userInfo.username;
});
ipcMain.on("get-host-name", (event) => {
event.returnValue = os.hostname();
});
ipcMain.on("get-webview-preload", (event) => {
event.returnValue = path.join(getElectronAppBasePath(), "preload", "preload-webview.cjs");
});
ipcMain.on("get-config-dir", (event) => {
event.returnValue = path.join(getWaveHomeDir(), "config");
});

export {
getElectronAppBasePath,
getElectronAppUnpackedBasePath,
Expand Down
1 change: 1 addition & 0 deletions emain/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contextBridge.exposeInMainWorld("api", {
getCursorPoint: () => ipcRenderer.sendSync("get-cursor-point"),
getUserName: () => ipcRenderer.sendSync("get-user-name"),
getHostName: () => ipcRenderer.sendSync("get-host-name"),
getConfigDir: () => ipcRenderer.sendSync("get-config-dir"),
getAboutModalDetails: () => ipcRenderer.sendSync("get-about-modal-details"),
getDocsiteUrl: () => ipcRenderer.sendSync("get-docsite-url"),
getWebviewPreload: () => ipcRenderer.sendSync("get-webview-preload"),
Expand Down
1 change: 0 additions & 1 deletion frontend/app/store/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function initGlobalAtoms(initOpts: GlobalInitOptions) {
// do nothing
}

const showAboutModalAtom = atom(false) as PrimitiveAtom<boolean>;
try {
getApi().onMenuItemAbout(() => {
modalsModel.pushModal("AboutModal");
Expand Down
49 changes: 32 additions & 17 deletions frontend/app/view/waveai/waveai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Markdown } from "@/app/element/markdown";
import { TypingIndicator } from "@/app/element/typingindicator";
import { RpcApi } from "@/app/store/wshclientapi";
import { TabRpcClient } from "@/app/store/wshrpcutil";
import { atoms, fetchWaveFile, globalStore, WOS } from "@/store/global";
import { atoms, createBlock, fetchWaveFile, getApi, globalStore, WOS } from "@/store/global";
import { BlockService, ObjectService } from "@/store/services";
import { adaptFromReactOrNativeKeyEvent, checkKeyPressed } from "@/util/keyutil";
import { fireAndForget, isBlank, makeIconClass } from "@/util/util";
Expand Down Expand Up @@ -182,26 +182,41 @@ export class WaveAiModel implements ViewModel {
});
}
}

const dropdownItems = Object.entries(presets)
.sort((a, b) => (a[1]["display:order"] > b[1]["display:order"] ? 1 : -1))
.map(
(preset) =>
({
label: preset[1]["display:name"],
onClick: () =>
fireAndForget(async () => {
await ObjectService.UpdateObjectMeta(WOS.makeORef("block", this.blockId), {
...preset[1],
"ai:preset": preset[0],
});
}),
}) as MenuItem
);
dropdownItems.push({
label: "Add AI preset...",
onClick: () => {
fireAndForget(async () => {
const path = `${getApi().getConfigDir()}/presets/ai.json`;
const blockDef: BlockDef = {
meta: {
view: "preview",
file: path,
},
};
await createBlock(blockDef, true);
});
},
});
viewTextChildren.push({
elemtype: "menubutton",
text: presetName,
title: "Select AI Configuration",
items: Object.entries(presets)
.sort((a, b) => (a[1]["display:order"] > b[1]["display:order"] ? 1 : -1))
.map(
(preset) =>
({
label: preset[1]["display:name"],
onClick: () =>
fireAndForget(async () => {
await ObjectService.UpdateObjectMeta(WOS.makeORef("block", this.blockId), {
...preset[1],
"ai:preset": preset[0],
});
}),
}) as MenuItem
),
items: dropdownItems,
});
return viewTextChildren;
});
Expand Down
1 change: 1 addition & 0 deletions frontend/types/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ declare global {
getEnv: (varName: string) => string;
getUserName: () => string;
getHostName: () => string;
getConfigDir: () => string;
getWebviewPreload: () => string;
getAboutModalDetails: () => AboutModalDetails;
getDocsiteUrl: () => string;
Expand Down
4 changes: 0 additions & 4 deletions pkg/wavebase/wavebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ func EnsureWaveDBDir() error {
return CacheEnsureDir(filepath.Join(GetWaveHomeDir(), WaveDBDir), "wavedb", 0700, "wave db directory")
}

func EnsureWaveConfigDir() error {
return CacheEnsureDir(filepath.Join(GetWaveHomeDir(), ConfigDir), "waveconfig", 0700, "wave config directory")
}

func CacheEnsureDir(dirName string, cacheKey string, perm os.FileMode, dirDesc string) error {
baseLock.Lock()
ok := ensureDirCache[cacheKey]
Expand Down
2 changes: 1 addition & 1 deletion pkg/wconfig/defaultconfig/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ package defaultconfig

import "embed"

//go:embed *.json
//go:embed *.json all:*/*.json
var ConfigFS embed.FS
18 changes: 0 additions & 18 deletions pkg/wconfig/defaultconfig/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,5 @@
"bg": "linear-gradient(120deg,hsla(350, 65%, 57%, 1),hsla(30,60%,60%, .75), hsla(208,69%,50%,.15), hsl(230,60%,40%)),radial-gradient(at top right,hsla(300,60%,70%,0.3),transparent),radial-gradient(at top left,hsla(330,100%,70%,.20),transparent),radial-gradient(at top right,hsla(190,100%,40%,.20),transparent),radial-gradient(at bottom left,hsla(323,54%,50%,.5),transparent),radial-gradient(at bottom left,hsla(144,54%,50%,.25),transparent)",
"bg:blendmode": "overlay",
"bg:text": "rgb(200, 200, 200)"
},
"ai@global": {
"display:name": "Global default",
"display:order": -1,
"ai:*": true
},
"ai@wave": {
"display:name": "Wave Proxy - gpt-4o-mini",
"display:order": 0,
"ai:*": true,
"ai:apitype": "",
"ai:baseurl": "",
"ai:apitoken": "",
"ai:name": "",
"ai:orgid": "",
"ai:model": "gpt-4o-mini",
"ai:maxtokens": 2048,
"ai:timeoutms": 60000
}
}
20 changes: 20 additions & 0 deletions pkg/wconfig/defaultconfig/presets/ai.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"ai@global": {
"display:name": "Global default",
"display:order": -1,
"ai:*": true
},
"ai@wave": {
"display:name": "Wave Proxy - gpt-4o-mini",
"display:order": 0,
"ai:*": true,
"ai:apitype": "",
"ai:baseurl": "",
"ai:apitoken": "",
"ai:name": "",
"ai:orgid": "",
"ai:model": "gpt-4o-mini",
"ai:maxtokens": 2048,
"ai:timeoutms": 60000
}
}
11 changes: 10 additions & 1 deletion pkg/wconfig/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@ func GetWatcher() *Watcher {
}
instance = &Watcher{watcher: watcher}
err = instance.watcher.Add(configDirAbsPath)
const failedStr = "failed to add path %s to watcher: %v"
if err != nil {
log.Printf("failed to add path %s to watcher: %v", configDirAbsPath, err)
log.Printf(failedStr, configDirAbsPath, err)
}

subdirs := GetConfigSubdirs()
for _, dir := range subdirs {
err = instance.watcher.Add(dir)
if err != nil {
log.Printf(failedStr, dir, err)
}
}
})
return instance
Expand Down
Loading

0 comments on commit 39fff9e

Please sign in to comment.