Skip to content

feat: implement cache on getPrompt and getPromptById methods #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 29, 2024
18 changes: 14 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,14 @@ export class SharedCache {
return SharedCache.instance;
}

public getPromptCacheKey(id: string, name: string, version: number): string {
public getPromptCacheKey(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the cache is not prompt only, let's not leak that business logic in the cache layer.

id?: string,
name?: string,
version?: number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When handling only optional params in js it's recommended to use an object because it's not great ux to do:

instance.getPromptCacheKey(undefined, undefined, version)
// vs
instance.getPromptCacheKey({ version })

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now I understand why auto-complete suggested a object thx

): string {
if (id) {
return id;
} else if (name && version) {
} else if (name && (version || version === 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually a way to do that explicitly:

Suggested change
} else if (name && (version || version === 0)) {
} else if (name && typeof version === "number") {

return `${name}:${version}`;
} else if (name) {
return name;
Expand All @@ -357,8 +361,12 @@ export class SharedCache {

public putPrompt(prompt: Prompt): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

this.put(prompt.id, prompt);
this.put(prompt.name, prompt.id);
this.put(`${prompt.name}-${prompt.version}`, prompt.id);
this.put(prompt.name, prompt);
this.put(`${prompt.name}:${prompt.version}`, prompt);
}

public getCache(): Map<string, any> {
return this.cache;
}

public get(key: string): any {
Expand Down Expand Up @@ -2226,6 +2234,8 @@ export class API {
this.cache.putPrompt(prompt);
return prompt;
} catch (error) {
console.log('key: ', this.cache.getPromptCacheKey(id, name, version));
console.log('cachedPrompt: ', cachedPrompt);
return cachedPrompt;
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios';
import 'dotenv/config';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -697,11 +698,9 @@ is a templated list.`;
it('should fallback to cache when getPrompt DB call fails', async () => {
const prompt = new Prompt(client.api, mockPromptData);
client.api.cache.putPrompt(prompt);
jest
.spyOn(client.api as any, 'makeGqlCall')
.mockRejectedValueOnce(new Error('DB Error'));
jest.spyOn(axios, 'post').mockRejectedValueOnce(new Error('DB Error'));

const result = await client.api.getPrompt(prompt.name, prompt.version);
const result = await client.api.getPrompt(prompt.id);
expect(result).toEqual(prompt);
});

Expand Down
Loading