@@ -4,6 +4,7 @@ import type { PostHog } from 'posthog-node'
44import type { CachedPrompt , GetPromptOptions , PromptApiResponse , PromptVariables , PromptsDirectOptions } from './types'
55
66const DEFAULT_CACHE_TTL_SECONDS = 300 // 5 minutes
7+ type PromptVersionCache = Map < number | undefined , CachedPrompt >
78
89function isPromptApiResponse ( data : unknown ) : data is PromptApiResponse {
910 return (
@@ -63,7 +64,7 @@ export class Prompts {
6364 private projectApiKey : string
6465 private host : string
6566 private defaultCacheTtlSeconds : number
66- private cache : Map < string , CachedPrompt > = new Map ( )
67+ private cache : Map < string , PromptVersionCache > = new Map ( )
6768
6869 constructor ( options : PromptsOptions ) {
6970 this . defaultCacheTtlSeconds = options . defaultCacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS
@@ -80,8 +81,19 @@ export class Prompts {
8081 }
8182 }
8283
83- private getCacheKey ( name : string , version ?: number ) : string {
84- return version === undefined ? `${ name } ::latest` : `${ name } ::version:${ version } `
84+ private getPromptCache ( name : string ) : PromptVersionCache | undefined {
85+ return this . cache . get ( name )
86+ }
87+
88+ private getOrCreatePromptCache ( name : string ) : PromptVersionCache {
89+ const cachedPromptVersions = this . cache . get ( name )
90+ if ( cachedPromptVersions ) {
91+ return cachedPromptVersions
92+ }
93+
94+ const promptVersions : PromptVersionCache = new Map ( )
95+ this . cache . set ( name , promptVersions )
96+ return promptVersions
8597 }
8698
8799 private getPromptLabel ( name : string , version ?: number ) : string {
@@ -100,11 +112,10 @@ export class Prompts {
100112 const cacheTtlSeconds = options ?. cacheTtlSeconds ?? this . defaultCacheTtlSeconds
101113 const fallback = options ?. fallback
102114 const version = options ?. version
103- const cacheKey = this . getCacheKey ( name , version )
104115 const promptLabel = this . getPromptLabel ( name , version )
105116
106117 // Check cache first
107- const cached = this . cache . get ( cacheKey )
118+ const cached = this . getPromptCache ( name ) ? .get ( version )
108119 const now = Date . now ( )
109120
110121 if ( cached ) {
@@ -121,7 +132,7 @@ export class Prompts {
121132 const fetchedAt = Date . now ( )
122133
123134 // Update cache
124- this . cache . set ( cacheKey , {
135+ this . getOrCreatePromptCache ( name ) . set ( version , {
125136 prompt,
126137 fetchedAt,
127138 } )
@@ -169,24 +180,29 @@ export class Prompts {
169180 /**
170181 * Clear the cache for a specific prompt or all prompts
171182 *
172- * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt.
183+ * @param name - Optional prompt name to clear. If provided, clears all cached versions for that prompt unless a version is also provided.
184+ * @param version - Optional prompt version to clear. Requires a prompt name.
173185 */
174- clearCache ( name ?: string ) : void {
175- if ( name !== undefined ) {
176- const latestKey = this . getCacheKey ( name )
177- const versionPrefix = `${ name } ::version:`
178- for ( const key of this . cache . keys ( ) ) {
179- if ( key === latestKey ) {
180- this . cache . delete ( key )
181- continue
182- }
183-
184- if ( key . startsWith ( versionPrefix ) && / ^ \d + $ / . test ( key . slice ( versionPrefix . length ) ) ) {
185- this . cache . delete ( key )
186- }
187- }
188- } else {
186+ clearCache ( name ?: string , version ?: number ) : void {
187+ if ( version !== undefined && name === undefined ) {
188+ throw new Error ( "'version' requires 'name' to be provided" )
189+ }
190+
191+ if ( name === undefined ) {
189192 this . cache . clear ( )
193+ return
194+ }
195+
196+ if ( version === undefined ) {
197+ this . cache . delete ( name )
198+ return
199+ }
200+
201+ const promptVersions = this . getPromptCache ( name )
202+ promptVersions ?. delete ( version )
203+
204+ if ( promptVersions ?. size === 0 ) {
205+ this . cache . delete ( name )
190206 }
191207 }
192208
0 commit comments