Skip to content

Commit 4ab1d2b

Browse files
committed
cleanup caches
1 parent 6f67b70 commit 4ab1d2b

18 files changed

Lines changed: 402 additions & 345 deletions

packages/core/src/agent.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MemoryCache } from "./cache"
1+
import { createCache } from "./cache"
22
import {
33
AGENT_MEMORY_CACHE_NAME,
44
AGENT_MEMORY_FLEX_TOKENS,
@@ -8,7 +8,7 @@ import { errorMessage } from "./error"
88
import { GenerationOptions } from "./generation"
99
import { HTMLEscape } from "./html"
1010
import { prettifyMarkdown } from "./markdown"
11-
import { MarkdownTrace, TraceOptions } from "./trace"
11+
import { TraceOptions } from "./trace"
1212
import { logVerbose } from "./util"
1313

1414
/**
@@ -79,7 +79,7 @@ export async function agentAddMemory(
7979
options: Pick<GenerationOptions, "userState"> & Required<TraceOptions>
8080
) {
8181
const { trace } = options || {}
82-
const cache = MemoryCache.byName<
82+
const cache = createCache<
8383
{ agent: string; query: string },
8484
{
8585
agent: string
@@ -101,7 +101,7 @@ export async function agentAddMemory(
101101
}
102102

103103
async function loadMemories(options: Pick<GenerationOptions, "userState">) {
104-
const cache = MemoryCache.byName<
104+
const cache = createCache<
105105
{ agent: string; query: string },
106106
{
107107
agent: string
@@ -151,7 +151,7 @@ export async function traceAgentMemory(
151151
}
152152

153153
async function defMemory(ctx: ChatTurnGenerationContext) {
154-
const cache = MemoryCache.byName<
154+
const cache = createCache<
155155
{ agent: string; query: string },
156156
{
157157
agent: string

packages/core/src/azurecontentsafety.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
import { runtimeHost } from "./host"
99
import { CancellationOptions } from "./cancellation"
1010
import { YAMLStringify } from "./yaml"
11-
import { DirectoryCache } from "./directorycache"
1211
import { AzureCredentialsType } from "./server/messages"
1312
import { trimTrailingSlash } from "./cleaners"
1413
import { chunkString } from "./chunkers"
14+
import { CacheOptions, createCache } from "./cache"
1515

1616
interface AzureContentSafetyRequest {
1717
userPrompt?: string
@@ -28,12 +28,14 @@ interface AzureContentSafetyResponse {
2828
}
2929

3030
class AzureContentSafetyClient implements ContentSafety {
31-
private readonly cache: DirectoryCache<
31+
private readonly cache: WorkspaceFileCache<
3232
{ route: string; body: object; options: object },
3333
object
3434
>
35-
constructor(readonly options?: TraceOptions & CancellationOptions) {
36-
this.cache = DirectoryCache.byName("azurecontentsafety")
35+
constructor(
36+
readonly options?: CacheOptions & TraceOptions & CancellationOptions
37+
) {
38+
this.cache = createCache("azurecontentsafety", options)
3739
}
3840

3941
async detectHarmfulContent(

packages/core/src/cache.test.ts

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
1-
import { JSONLineCache, CacheEntry } from "./cache"
21
import { describe, test, beforeEach } from "node:test"
32
import assert from "node:assert/strict"
43
import * as fs from "node:fs/promises"
54
import * as path from "node:path"
65
import { TestHost } from "./testhost"
6+
import { JSONLineCache } from "./jsonlinecache"
7+
import { createCache } from "./cache"
78

89
const tempDir = path.join(".genaiscript", "temp")
910

10-
describe("Cache", () => {
11-
beforeEach(async () => {
12-
TestHost.install()
13-
await fs.mkdir(tempDir, { recursive: true })
14-
})
15-
test("JSONLineCache instance creation with byName", async () => {
16-
const cache = JSONLineCache.byName<string, number>("testCache")
17-
assert.ok(cache instanceof JSONLineCache)
18-
})
19-
test("JSONLineCache set key-value pair", async () => {
20-
const cache = JSONLineCache.byName<string, number>("testCache")
21-
await cache.set("anotherKey", 99)
22-
const value = await cache.get("anotherKey")
23-
assert.strictEqual(value, 99)
24-
})
11+
for (const type of ["memory", "jsonl", "fs"]) {
12+
describe(`cache.${type}`, () => {
13+
beforeEach(async () => {
14+
TestHost.install()
15+
await fs.mkdir(tempDir, { recursive: true })
16+
})
17+
test("instance creation with byName", async () => {
18+
const cache = createCache<string, number>("testCache", {
19+
type: type as any,
20+
})
21+
assert.ok(cache instanceof JSONLineCache)
22+
})
23+
test("set key-value pair", async () => {
24+
const cache = createCache<string, number>("testCache", {
25+
type: type as any,
26+
})
27+
await cache.set("anotherKey", 99)
28+
const value = await cache.get("anotherKey")
29+
assert.strictEqual(value, 99)
30+
})
2531

26-
test("JSONLineCache getKeySHA computation", async () => {
27-
const cache = JSONLineCache.byName<string, number>("testCache")
28-
const sha = await cache.getKeySHA("testKey")
29-
assert.ok(sha)
30-
assert.strictEqual(typeof sha, "string")
31-
})
32+
test("getSha computation", async () => {
33+
const cache = createCache<string, number>("testCache", {
34+
type: type as any,
35+
})
36+
const sha = await cache.getSha("testKey")
37+
assert.ok(sha)
38+
assert.strictEqual(typeof sha, "string")
39+
})
3240

33-
test("keySHA generates SHA256 hash from a key", async () => {
34-
const cache = JSONLineCache.byName<string, number>("testCache")
35-
const sha = await cache.getKeySHA("testKey")
36-
assert.ok(sha)
37-
assert.strictEqual(typeof sha, "string")
38-
})
39-
test("JSONLineCache getOrUpdate retrieves existing value", async () => {
40-
const cache = JSONLineCache.byName<string, number>("testCache")
41-
await cache.set("existingKey", 42)
42-
const value = await cache.getOrUpdate(
43-
"existingKey",
44-
async () => 99,
45-
() => true
46-
)
47-
assert.strictEqual(value.value, 42)
48-
})
41+
test("keySHA generates SHA256 hash from a key", async () => {
42+
const cache = createCache<string, number>("testCache", {
43+
type: type as any,
44+
})
45+
const sha = await cache.getSha("testKey")
46+
assert.ok(sha)
47+
assert.strictEqual(typeof sha, "string")
48+
})
49+
test("getOrUpdate retrieves existing value", async () => {
50+
const cache = createCache<string, number>("testCache", {
51+
type: type as any,
52+
})
53+
await cache.set("existingKey", 42)
54+
const value = await cache.getOrUpdate(
55+
"existingKey",
56+
async () => 99,
57+
() => true
58+
)
59+
assert.strictEqual(value.value, 42)
60+
})
4961

50-
test("JSONLineCache getOrUpdate updates with new value if key does not exist", async () => {
51-
const cache = JSONLineCache.byName<string, number>("testCache")
52-
const value = await cache.getOrUpdate(
53-
"newKey",
54-
async () => 99,
55-
() => true
56-
)
57-
assert.strictEqual(value.value, 99)
58-
const cachedValue = await cache.get("newKey")
59-
assert.strictEqual(cachedValue, 99)
62+
test("getOrUpdate updates with new value if key does not exist", async () => {
63+
const cache = createCache<string, number>("testCache", {
64+
type: type as any,
65+
})
66+
const value = await cache.getOrUpdate(
67+
"newKey",
68+
async () => 99,
69+
() => true
70+
)
71+
assert.strictEqual(value.value, 99)
72+
const cachedValue = await cache.get("newKey")
73+
assert.strictEqual(cachedValue, 99)
74+
})
6075
})
61-
})
76+
}

0 commit comments

Comments
 (0)