1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
// const url = "https://urbit.org";
import models, { type AIModelAPI, type LLMChoice } from "../index";
import { memoize } from "../src/cache";
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
async function thingy(arg1: string, arg2?: string): Promise<string> {
const res = `lolololol\n${arg1}\n${arg2};`;
await sleep(3000);
return res;
}
describe("HTTP cache", () => {
test("should cache a function call", async () => {
const ts = Date.now();
// const choice: LLMChoice = { deepseek: "deepseek-chat" };
// const api = models(choice);
//
const cachedCall = memoize(thingy, {
ttlMs: 7 * 24 * 60 * 60 * 1000,
maxEntries: 5000,
persistDir: "./cache/memo",
});
const res = await cachedCall("LOLAZO", "bar");
const elapsed = Date.now() - ts;
console.log(elapsed);
const randomRes = await cachedCall(`${Math.random()}`);
const elapsedRandom = Date.now() - ts;
console.log("uncachable", elapsedRandom);
console.log("cached res", res);
expect(elapsed).toBeLessThan(3000);
});
test("should cache a request", async () => {
const testMessage = "oh hi";
const choice3: LLMChoice = { chatgpt: "gpt-5-nano" };
const choice4: LLMChoice = { deepseek: "deepseek-chat" };
const choice5: LLMChoice = { kimi: "kimi-k2-0905-preview" };
const api3 = models(choice3);
const api4 = models(choice4);
const api5 = models(choice5);
const ts = Date.now();
const r3 = api3.send(testMessage);
const r4 = api4.send(testMessage);
const r5 = api5.send(testMessage);
// Check ChatGPT response
const res3 = await r3;
console.log("elapsed r3", Date.now() - ts);
if ("ok" in res3) {
console.log(`✅ ChatGPT Response: ${res3.ok}`);
expect(res3.ok).toBeString();
} else {
console.log(`❌ ChatGPT Error: ${res3.error}`);
}
// // Check DeepSeek response
const res4 = await r4;
console.log("elapsed r4", Date.now() - ts);
if ("ok" in res4) {
console.log(`✅ DeepSeek Response: ${res4.ok}`);
expect(res4.ok).toBeString();
} else {
console.log(`❌ DeepSeek Error: ${res4.error}`);
}
// // Check Kimi response
const res5 = await r5;
console.log("elapsed r5", Date.now() - ts);
if ("ok" in res5) {
console.log(`✅ Kimi Response: ${res5.ok}`);
expect(res5.ok).toBeString();
} else {
console.log(`❌ Kimi Error: ${res5.error}`);
}
});
});
|