Skip to content

Commit 956bf95

Browse files
authored
improve prompt caching by computing a deterministic hash (+ other bug fixes) (#2509)
1 parent 8768269 commit 956bf95

5 files changed

Lines changed: 48 additions & 8 deletions

File tree

emain/emain-platform.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ ipcMain.on("get-data-dir", (event) => {
193193
ipcMain.on("get-config-dir", (event) => {
194194
event.returnValue = getWaveConfigDir();
195195
});
196+
ipcMain.on("get-home-dir", (event) => {
197+
event.returnValue = app.getPath("home");
198+
});
196199

197200
/**
198201
* Gets the value of the XDG_CURRENT_DESKTOP environment variable. If ORIGINAL_XDG_CURRENT_DESKTOP is set, it will be returned instead.

emain/preload.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ contextBridge.exposeInMainWorld("api", {
1212
getHostName: () => ipcRenderer.sendSync("get-host-name"),
1313
getDataDir: () => ipcRenderer.sendSync("get-data-dir"),
1414
getConfigDir: () => ipcRenderer.sendSync("get-config-dir"),
15+
getHomeDir: () => ipcRenderer.sendSync("get-home-dir"),
1516
getAboutModalDetails: () => ipcRenderer.sendSync("get-about-modal-details"),
1617
getDocsiteUrl: () => ipcRenderer.sendSync("get-docsite-url"),
1718
getWebviewPreload: () => ipcRenderer.sendSync("get-webview-preload"),

frontend/app/view/codeeditor/schemaendpoints.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,34 @@ type EndpointInfo = {
1010
schema: object;
1111
};
1212

13+
function prependWildcard(path: string): string {
14+
return path.startsWith("/") ? `*${path}` : `*/${path}`;
15+
}
16+
17+
function convertToTildePath(absolutePath: string): string {
18+
const homeDir = getApi().getHomeDir();
19+
if (absolutePath.startsWith(homeDir)) {
20+
return "~" + absolutePath.slice(homeDir.length);
21+
}
22+
return absolutePath;
23+
}
24+
25+
function makeConfigPathMatches(suffix: string): Array<string> {
26+
const configPath = `${getApi().getConfigDir()}${suffix}`;
27+
const tildePath = convertToTildePath(configPath);
28+
const paths = [configPath, prependWildcard(configPath)];
29+
if (tildePath !== configPath) {
30+
paths.push(tildePath);
31+
paths.push(prependWildcard(tildePath));
32+
}
33+
return paths;
34+
}
35+
1336
const allFilepaths: Map<string, Array<string>> = new Map();
14-
allFilepaths.set(`${getWebServerEndpoint()}/schema/settings.json`, [`${getApi().getConfigDir()}/settings.json`]);
15-
allFilepaths.set(`${getWebServerEndpoint()}/schema/connections.json`, [`${getApi().getConfigDir()}/connections.json`]);
16-
allFilepaths.set(`${getWebServerEndpoint()}/schema/aipresets.json`, [`${getApi().getConfigDir()}/presets/ai.json`]);
17-
allFilepaths.set(`${getWebServerEndpoint()}/schema/widgets.json`, [`${getApi().getConfigDir()}/widgets.json`]);
37+
allFilepaths.set(`${getWebServerEndpoint()}/schema/settings.json`, makeConfigPathMatches("/settings.json"));
38+
allFilepaths.set(`${getWebServerEndpoint()}/schema/connections.json`, makeConfigPathMatches("/connections.json"));
39+
allFilepaths.set(`${getWebServerEndpoint()}/schema/aipresets.json`, makeConfigPathMatches("/presets/ai.json"));
40+
allFilepaths.set(`${getWebServerEndpoint()}/schema/widgets.json`, makeConfigPathMatches("/widgets.json"));
1841

1942
async function getSchemaEndpointInfo(endpoint: string): Promise<EndpointInfo> {
2043
let schema: Object;

frontend/types/custom.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ declare global {
8383
getHostName: () => string; // get-host-name
8484
getDataDir: () => string; // get-data-dir
8585
getConfigDir: () => string; // get-config-dir
86+
getHomeDir: () => string; // get-home-dir
8687
getWebviewPreload: () => string; // get-webview-preload
8788
getAboutModalDetails: () => AboutModalDetails; // get-about-modal-details
8889
getDocsiteUrl: () => string; // get-docsite-url

pkg/aiusechat/openai/openai-convertmessage.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package openai
66
import (
77
"bytes"
88
"context"
9+
"crypto/sha256"
910
"encoding/base64"
11+
"encoding/hex"
1012
"encoding/json"
1113
"errors"
1214
"fmt"
@@ -58,6 +60,16 @@ func extractXmlAttribute(tag, attrName string) (string, bool) {
5860
return value, true
5961
}
6062

63+
// generateDeterministicSuffix creates an 8-character hash from input strings
64+
func generateDeterministicSuffix(inputs ...string) string {
65+
hasher := sha256.New()
66+
for _, input := range inputs {
67+
hasher.Write([]byte(input))
68+
}
69+
hash := hasher.Sum(nil)
70+
return hex.EncodeToString(hash)[:8]
71+
}
72+
6173
// ---------- OpenAI Request Types ----------
6274

6375
type StreamOptionsType struct {
@@ -388,8 +400,8 @@ func convertFileAIMessagePart(part uctypes.AIMessagePart) (*OpenAIMessageContent
388400
encodedFileName := strings.ReplaceAll(fileName, `"`, "&quot;")
389401
quotedFileName := strconv.Quote(encodedFileName)
390402

391-
randomSuffix := uuid.New().String()[0:8]
392-
formattedText := fmt.Sprintf("<AttachedTextFile_%s file_name=%s>\n%s\n</AttachedTextFile_%s>", randomSuffix, quotedFileName, textContent, randomSuffix)
403+
deterministicSuffix := generateDeterministicSuffix(textContent, fileName)
404+
formattedText := fmt.Sprintf("<AttachedTextFile_%s file_name=%s>\n%s\n</AttachedTextFile_%s>", deterministicSuffix, quotedFileName, textContent, deterministicSuffix)
393405

394406
return &OpenAIMessageContent{
395407
Type: "input_text",
@@ -412,8 +424,8 @@ func convertFileAIMessagePart(part uctypes.AIMessagePart) (*OpenAIMessageContent
412424
encodedDirName := strings.ReplaceAll(directoryName, `"`, "&quot;")
413425
quotedDirName := strconv.Quote(encodedDirName)
414426

415-
randomSuffix := uuid.New().String()[0:8]
416-
formattedText := fmt.Sprintf("<AttachedDirectoryListing_%s directory_name=%s>\n%s\n</AttachedDirectoryListing_%s>", randomSuffix, quotedDirName, jsonContent, randomSuffix)
427+
deterministicSuffix := generateDeterministicSuffix(jsonContent, directoryName)
428+
formattedText := fmt.Sprintf("<AttachedDirectoryListing_%s directory_name=%s>\n%s\n</AttachedDirectoryListing_%s>", deterministicSuffix, quotedDirName, jsonContent, deterministicSuffix)
417429

418430
return &OpenAIMessageContent{
419431
Type: "input_text",

0 commit comments

Comments
 (0)