Skip to content

Commit 6f67b70

Browse files
committed
twoards multi-cache support
1 parent c21efc5 commit 6f67b70

9 files changed

Lines changed: 24 additions & 14 deletions

File tree

packages/core/src/azurecontentsafety.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { runtimeHost } from "./host"
99
import { CancellationOptions } from "./cancellation"
1010
import { YAMLStringify } from "./yaml"
11-
import { JSONLineCache } from "./cache"
11+
import { DirectoryCache } from "./directorycache"
1212
import { AzureCredentialsType } from "./server/messages"
1313
import { trimTrailingSlash } from "./cleaners"
1414
import { chunkString } from "./chunkers"
@@ -28,12 +28,12 @@ interface AzureContentSafetyResponse {
2828
}
2929

3030
class AzureContentSafetyClient implements ContentSafety {
31-
private readonly cache: JSONLineCache<
31+
private readonly cache: DirectoryCache<
3232
{ route: string; body: object; options: object },
3333
object
3434
>
3535
constructor(readonly options?: TraceOptions & CancellationOptions) {
36-
this.cache = JSONLineCache.byName("azurecontentsafety")
36+
this.cache = DirectoryCache.byName("azurecontentsafety")
3737
}
3838

3939
async detectHarmfulContent(

packages/core/src/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class MemoryCache<K, V>
151151
* @param key - The key to compute SHA for
152152
* @returns A promise resolving to the SHA string
153153
*/
154-
async getKeySHA(key: K) {
154+
async getKeyHash(key: K) {
155155
const sha = await MemoryCache.keySHA(key)
156156
return sha
157157
}

packages/core/src/chatcache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JSONLineCache } from "./cache"
1+
import { DirectoryCache } from "./directorycache"
22
import {
33
ChatCompletionResponse,
44
CreateChatCompletionRequest,
@@ -13,7 +13,7 @@ export type ChatCompletionRequestCacheKey = CreateChatCompletionRequest &
1313

1414
// Define a JSON line cache type that maps cache keys to cache values.
1515
// This cache stores chat completion requests and their associated responses.
16-
export type ChatCompletationRequestCache = JSONLineCache<
16+
export type ChatCompletationRequestCache = DirectoryCache<
1717
ChatCompletionRequestCacheKey,
1818
ChatCompletionResponse
1919
>
@@ -24,7 +24,7 @@ export type ChatCompletationRequestCache = JSONLineCache<
2424
export function getChatCompletionCache(
2525
name?: string
2626
): ChatCompletationRequestCache {
27-
return JSONLineCache.byName<
27+
return DirectoryCache.byName<
2828
ChatCompletionRequestCacheKey,
2929
ChatCompletionResponse
3030
>(name || CHAT_CACHE)

packages/core/src/directorycache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ export class DirectoryCache<K, V> implements WorkspaceFileCache<any, any> {
7676
private folder() {
7777
return dotGenaiscriptPath("cache", this.name)
7878
}
79+
80+
async getKeyHash(key: K): Promise<string> {
81+
return await MemoryCache.keySHA(key)
82+
}
7983
}

packages/core/src/runpromptcontext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import { agentAddMemory, agentQueryMemory } from "./agent"
9191
import { YAMLStringify } from "./yaml"
9292
import { Project } from "./server/messages"
9393
import { mergeEnvVarsWithSystem, parametersToVars } from "./vars"
94-
import { JSONLineCache } from "./cache"
94+
import { JSONLineCache } from "./jsonlinecache"
9595
import { FFmepgClient } from "./ffmpeg"
9696
import { BufferToBlob } from "./bufferlike"
9797
import { host } from "./host"

packages/core/src/types/prompt_template.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,12 @@ interface WorkspaceFileCache<K, V> {
10881088
* List the values in the cache.
10891089
*/
10901090
values(): Promise<V[]>
1091+
1092+
/**
1093+
* Gets the sha of the key
1094+
* @param key
1095+
*/
1096+
getKeyHash(key: K): Promise<string>
10911097
}
10921098

10931099
interface WorkspaceGrepOptions extends FilterGitFilesOptions {

packages/core/src/vectorsearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { resolveModelConnectionInfo } from "./models"
1313
import { EMBEDDINGS_MODEL_ID } from "./constants"
1414
import { runtimeHost } from "./host"
1515
import { resolveLanguageModel } from "./lm"
16-
import { JSONLineCache } from "./cache"
16+
import { JSONLineCache } from "./jsonlinecache"
1717
import { EmbeddingsResponse } from "vectra"
1818
import { assert } from "./util"
1919

packages/core/src/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import debug from "debug"
22
const dbg = debug("genaiscript:workspace")
33

44
import { copyFile, mkdir, writeFile } from "fs/promises"
5-
import { JSONLineCache } from "./cache"
5+
import { JSONLineCache } from "./jsonlinecache"
66
import { DOT_ENV_REGEX } from "./constants"
77
import { CSVTryParse } from "./csv"
88
import { dataTryParse } from "./data"

packages/vscode/src/state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Utils } from "vscode-uri"
77
import { listFiles, saveAllTextDocuments } from "./fs"
88
import { parseAnnotations } from "../../core/src/annotations"
99
import { Project, PromptScriptRunOptions } from "../../core/src/server/messages"
10-
import { JSONLineCache } from "../../core/src/jsonlinecache"
10+
import { DirectoryCache } from "../../core/src/directorycache"
1111
import { ChatCompletionsProgressReport } from "../../core/src/chattypes"
1212
import { fixCustomPrompts, fixPromptDefinitions } from "../../core/src/scripts"
1313
import { logMeasure } from "../../core/src/perf"
@@ -97,7 +97,7 @@ export class ExtensionState extends EventTarget {
9797
private _project: Project = undefined
9898
private _aiRequest: AIRequest = undefined
9999
private _diagColl: vscode.DiagnosticCollection
100-
private _aiRequestCache: JSONLineCache<
100+
private _aiRequestCache: DirectoryCache<
101101
AIRequestSnapshotKey,
102102
AIRequestSnapshot
103103
> = undefined
@@ -122,7 +122,7 @@ export class ExtensionState extends EventTarget {
122122
this._diagColl = vscode.languages.createDiagnosticCollection(TOOL_NAME)
123123
subscriptions.push(this._diagColl)
124124

125-
this._aiRequestCache = JSONLineCache.byName<
125+
this._aiRequestCache = DirectoryCache.byName<
126126
AIRequestSnapshotKey,
127127
AIRequestSnapshot
128128
>(AI_REQUESTS_CACHE)
@@ -241,7 +241,7 @@ export class ExtensionState extends EventTarget {
241241
fragment: options.fragment,
242242
version: CORE_VERSION,
243243
}
244-
return { key, sha: await this._aiRequestCache.getKeySHA(key) }
244+
return { key, sha: await this._aiRequestCache.getKeyHash(key) }
245245
}
246246

247247
dispatchAIRequestChange() {

0 commit comments

Comments
 (0)