Skip to content

Commit 1e382ac

Browse files
committed
more cleanup
1 parent a580121 commit 1e382ac

5 files changed

Lines changed: 10 additions & 65 deletions

File tree

packages/core/src/cache.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { MemoryCache } from "./memcache"
44
import { host } from "./host"
55
import { NotSupportedError } from "./error"
66
import { CancellationOptions } from "./cancellation"
7+
import debug from "debug"
8+
const dbg = debug("genaiscript:cache")
79

810
/**
911
* Represents a cache entry with a hashed identifier (`sha`), `key`, and `val`.
@@ -30,14 +32,18 @@ export function createCache<K, V>(
3032
options?: CacheOptions & CancellationOptions
3133
): WorkspaceFileCache<K, V> {
3234
name = cacheNormalizeName(name) // Sanitize name
33-
if (!name) throw new NotSupportedError("missing cache name")
35+
if (!name) {
36+
dbg(`empty cache name`)
37+
throw new NotSupportedError("missing cache name")
38+
}
3439

3540
const type = options?.type || "fs"
3641
const key = `cache:${type}:${name}`
3742
const userState = options?.userState || host.userState
3843
if (userState[key]) return userState[key] // Return if exists
3944
if (options?.lookupOnly) return undefined
4045

46+
dbg(`creating ${name} ${type}`)
4147
let r: WorkspaceFileCache<K, V>
4248
switch (type) {
4349
case "memory":

packages/core/src/fscache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class FsCache<K, V> implements WorkspaceFileCache<any, any> {
3434
}
3535

3636
async get(key: any): Promise<any> {
37+
if (key === undefined) return undefined // Handle undefined key
3738
const sha = await this.getSha(key)
3839
const fn = this.cacheFilename(sha)
3940
const res = await tryReadJSON(fn)

packages/core/src/memcache.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,6 @@ export class MemoryCache<K, V>
4242
return Object.values(this._entries).map((kv) => kv.val)
4343
}
4444

45-
/**
46-
* Retrieve all entries from the cache.
47-
* @returns A promise resolving to an array of cache entries
48-
*/
49-
async entries(): Promise<CacheEntry<V>[]> {
50-
await this.initialize()
51-
return Object.values(this._entries).map((e) => ({ ...e }))
52-
}
53-
54-
/**
55-
* Retrieve a specific entry by its SHA identifier.
56-
* @param sha - The SHA identifier of the entry
57-
* @returns A promise resolving to the cache entry
58-
*/
59-
async getEntryBySha(sha: string) {
60-
await this.initialize()
61-
return this._entries[sha]
62-
}
63-
6445
/**
6546
* Get the value associated with a specific key.
6647
* @param key - The key of the entry

packages/core/src/workspace.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ export function createWorkspaceFileSystem(): Omit<
146146
return data
147147
},
148148
cache: async (name: string) => {
149-
if (!name) {
150-
dbg(`cache name is missing`)
151-
throw new NotSupportedError("missing cache name")
152-
}
153-
dbg(`cache name: ${name}`)
154149
const res = createCache<any, any>(name)
155150
return res
156151
},

packages/vscode/src/state.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ export class ExtensionState extends EventTarget {
9797
private _project: Project = undefined
9898
private _aiRequest: AIRequest = undefined
9999
private _diagColl: vscode.DiagnosticCollection
100-
private _aiRequestCache: WorkspaceFileCache<
101-
AIRequestSnapshotKey,
102-
AIRequestSnapshot
103-
> = undefined
104100
readonly output: vscode.LogOutputChannel
105101
readonly sessionApiKey: string
106102
private panel: vscode.WebviewPanel
@@ -122,11 +118,6 @@ export class ExtensionState extends EventTarget {
122118
this._diagColl = vscode.languages.createDiagnosticCollection(TOOL_NAME)
123119
subscriptions.push(this._diagColl)
124120

125-
this._aiRequestCache = createCache<
126-
AIRequestSnapshotKey,
127-
AIRequestSnapshot
128-
>(AI_REQUESTS_CACHE)
129-
130121
// clear errors when file edited (remove me?)
131122
subscriptions.push(
132123
vscode.workspace.onDidChangeTextDocument(
@@ -169,10 +160,6 @@ export class ExtensionState extends EventTarget {
169160
return res
170161
}
171162

172-
aiRequestCache() {
173-
return this._aiRequestCache
174-
}
175-
176163
async applyEdits() {
177164
const req = this.aiRequest
178165
if (!req) return
@@ -193,7 +180,7 @@ export class ExtensionState extends EventTarget {
193180

194181
async requestAI(
195182
options: AIRequestOptions
196-
): Promise<Partial<GenerationResult> & { requestSha: string }> {
183+
): Promise<Partial<GenerationResult>> {
197184
try {
198185
const req = await this.startAIRequest(options)
199186
if (!req) {
@@ -208,42 +195,17 @@ export class ExtensionState extends EventTarget {
208195
else if (text) this.showWebview({ reveal: false })
209196
}
210197

211-
const { key, sha } = await this.snapshotAIRequestKey(req)
212-
const snapshot = snapshotAIRequest(req)
213-
await this._aiRequestCache.set(key, snapshot)
214198
this.setDiagnostics()
215199
this.dispatchChange()
216200

217201
if (edits?.length && options.mode != "notebook") this.applyEdits()
218-
return { ...res, requestSha: sha }
202+
return res
219203
} catch (e) {
220204
if (isCancelError(e)) return undefined
221205
throw e
222206
}
223207
}
224208

225-
private async snapshotAIRequestKey(r: AIRequest) {
226-
const { options } = r
227-
const key: AIRequestSnapshotKey = {
228-
template: {
229-
id: options.template.id,
230-
title: options.template.title,
231-
hash: await hash(
232-
{
233-
template: options.template,
234-
parameters: options.parameters,
235-
mode: options.mode,
236-
runOptions: options.runOptions,
237-
},
238-
{ version: true }
239-
),
240-
},
241-
fragment: options.fragment,
242-
version: CORE_VERSION,
243-
}
244-
return { key, sha: await this._aiRequestCache.getSha(key) }
245-
}
246-
247209
dispatchAIRequestChange() {
248210
this.dispatchEvent(new Event(AI_REQUEST_CHANGE))
249211
}

0 commit comments

Comments
 (0)