Skip to content

Commit a4b1f86

Browse files
authored
added cache where we add promises and then we can check if those prom… (#5458)
* added cache where we add promises and then we can check if those promises are there and then we await them and we start them when the obj is made * no console * rm console
1 parent b74947d commit a4b1f86

File tree

4 files changed

+71
-48
lines changed

4 files changed

+71
-48
lines changed

worker/src/lib/ai-gateway/AttemptBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class AttemptBuilder {
123123

124124
const attemptArrays = await Promise.all(
125125
providerData.map(async (data) => {
126-
const byokAttempts = await this.buildByokAttempts(
126+
const byokAttempts = this.buildByokAttempts(
127127
modelSpec,
128128
data,
129129
orgId,
@@ -132,13 +132,13 @@ export class AttemptBuilder {
132132
);
133133

134134
// Always build PTB attempts (feature flag removed)
135-
const ptbAttempts = await this.buildPtbAttempts(
135+
const ptbAttempts = this.buildPtbAttempts(
136136
modelSpec,
137137
data,
138138
bodyMapping,
139139
plugins
140140
);
141-
return [...byokAttempts, ...ptbAttempts];
141+
return [...(await byokAttempts), ...(await ptbAttempts)];
142142
})
143143
);
144144

worker/src/lib/ai-gateway/SimpleAIGateway.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export class SimpleAIGateway {
8181

8282
const providerKeysManager = new ProviderKeysManager(
8383
new ProviderKeysStore(this.supabaseClient),
84-
env
84+
env,
85+
this.orgId
8586
);
8687

8788
// Create SecureCacheProvider for distributed caching

worker/src/lib/managers/ProviderKeysManager.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import { ModelProviderName } from "@helicone-package/cost/models/providers";
22
import { ProviderKey, ProviderKeysStore } from "../db/ProviderKeysStore";
3-
import {
4-
getFromKVCacheOnly,
5-
InMemoryCache,
6-
removeFromCache,
7-
storeInCache,
8-
} from "../util/cache/secureCache";
3+
import { getFromKVCacheOnly, storeInCache } from "../util/cache/secureCache";
94

105
export class ProviderKeysManager {
6+
providerKeysFromCache: Map<string, Promise<ProviderKey[] | null>> = new Map();
117
constructor(
128
private store: ProviderKeysStore,
13-
private env: Env
14-
) {}
9+
private env: Env,
10+
orgId?: string
11+
) {
12+
if (orgId) {
13+
this.providerKeysFromCache.set(
14+
orgId,
15+
this.getProviderKeysFromCache(`provider_keys_${orgId}`)
16+
);
17+
}
18+
19+
this.providerKeysFromCache.set(
20+
env.HELICONE_ORG_ID,
21+
this.getProviderKeysFromCache(`provider_keys_${env.HELICONE_ORG_ID}`)
22+
);
23+
}
1524

1625
async setProviderKeys() {
1726
const providerKeys = await this.store.getProviderKeys();
@@ -76,28 +85,34 @@ export class ProviderKeysManager {
7685
return filteredKeys[0];
7786
}
7887

79-
async getProviderKeys(orgId: string): Promise<ProviderKey[] | null> {
80-
const kvCacheKey = `provider_keys_${orgId}`;
81-
const memoryCacheKey = `provider_keys_in_memory_${orgId}`;
82-
83-
// Check in-memory cache first (fastest)
84-
const cachedKeys =
85-
InMemoryCache.getInstance<ProviderKey[]>().get(memoryCacheKey);
86-
if (cachedKeys) {
87-
return cachedKeys;
88-
}
89-
90-
// Fall back to KV cache
88+
private async getProviderKeysFromCache(
89+
kvCacheKey: string
90+
): Promise<ProviderKey[] | null> {
9191
const kvKeys = await getFromKVCacheOnly(kvCacheKey, this.env, 43200);
9292
if (!kvKeys) {
9393
return null;
9494
}
95+
return JSON.parse(kvKeys) as ProviderKey[];
96+
}
97+
98+
async getProviderKeys(orgId: string): Promise<ProviderKey[] | null> {
99+
if (this.providerKeysFromCache.has(orgId)) {
100+
const promiseVal = this.providerKeysFromCache.get(orgId);
95101

96-
// Parse once and store in memory cache for subsequent requests
97-
const parsedKeys = JSON.parse(kvKeys) as ProviderKey[];
98-
InMemoryCache.getInstance<ProviderKey[]>().set(memoryCacheKey, parsedKeys);
102+
if (promiseVal !== undefined) {
103+
return await promiseVal;
104+
}
105+
}
99106

100-
return parsedKeys;
107+
const result = await this.getProviderKeysFromCache(
108+
`provider_keys_${orgId}`
109+
);
110+
if (result) {
111+
this.providerKeysFromCache.set(orgId, Promise.resolve(result));
112+
return result;
113+
}
114+
115+
return null;
101116
}
102117

103118
async getProviderKeyWithFetch(
@@ -107,6 +122,7 @@ export class ProviderKeysManager {
107122
keyCuid?: string
108123
): Promise<ProviderKey | null> {
109124
const keys = await this.getProviderKeys(orgId);
125+
110126
const validKey = this.chooseProviderKey(
111127
keys ?? [],
112128
provider,

worker/src/routers/api/apiRouter.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ function getAPIRouterV1(
101101
}
102102

103103
try {
104-
const { removeFromCache } = await import("../../lib/util/cache/secureCache");
104+
const { removeFromCache } = await import(
105+
"../../lib/util/cache/secureCache"
106+
);
105107
const cacheKeysToDelete: string[] = [];
106-
108+
107109
if (data.versionId) {
108110
const promptBodyCacheKey = `prompt_body_${data.promptId}_${data.versionId}_${orgId}`;
109111
cacheKeysToDelete.push(promptBodyCacheKey);
@@ -118,30 +120,34 @@ function getAPIRouterV1(
118120
}
119121

120122
await Promise.all(
121-
cacheKeysToDelete.map(key =>
122-
removeFromCache(key, env)
123-
)
123+
cacheKeysToDelete.map((key) => removeFromCache(key, env))
124124
);
125125

126-
return new Response(JSON.stringify({
127-
success: true,
128-
deletedKeys: cacheKeysToDelete
129-
}), {
130-
status: 200,
131-
headers: { "Content-Type": "application/json" }
132-
});
126+
return new Response(
127+
JSON.stringify({
128+
success: true,
129+
deletedKeys: cacheKeysToDelete,
130+
}),
131+
{
132+
status: 200,
133+
headers: { "Content-Type": "application/json" },
134+
}
135+
);
133136
} catch (error) {
134137
console.error("Error resetting prompt cache:", error);
135-
return new Response(JSON.stringify({
136-
success: false,
137-
error: error instanceof Error ? error.message : "Unknown error"
138-
}), {
139-
status: 500,
140-
headers: { "Content-Type": "application/json" }
141-
});
138+
return new Response(
139+
JSON.stringify({
140+
success: false,
141+
error: error instanceof Error ? error.message : "Unknown error",
142+
}),
143+
{
144+
status: 500,
145+
headers: { "Content-Type": "application/json" },
146+
}
147+
);
142148
}
143149
}
144-
)
150+
);
145151

146152
router.post(
147153
"/mock-set-provider-keys/:orgId",

0 commit comments

Comments
 (0)