mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +00:00
Add cached Langfuse PromptService
This commit is contained in:
10
src/core/langfuse/PromptGateway.ts
Normal file
10
src/core/langfuse/PromptGateway.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface RawPrompt {
|
||||
name: string;
|
||||
version: number;
|
||||
config: Record<string, unknown>;
|
||||
compile(variables: Record<string, unknown>): { role: 'system' | 'user' | 'assistant'; content: string }[];
|
||||
}
|
||||
|
||||
export interface PromptFetcher {
|
||||
fetch(promptName: string): Promise<RawPrompt>;
|
||||
}
|
||||
46
src/core/langfuse/PromptService.ts
Normal file
46
src/core/langfuse/PromptService.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { PromptFetcher, RawPrompt } from './PromptGateway';
|
||||
import type { CompiledPrompt } from './types';
|
||||
|
||||
interface CacheEntry {
|
||||
raw: RawPrompt;
|
||||
fetchedAt: number;
|
||||
}
|
||||
|
||||
export class PromptService {
|
||||
private readonly cache = new Map<string, CacheEntry>();
|
||||
|
||||
constructor(
|
||||
private readonly fetcher: PromptFetcher,
|
||||
private readonly cacheEnabled: boolean
|
||||
) {}
|
||||
|
||||
async getCompiled(promptName: string, variables: Record<string, unknown>): Promise<CompiledPrompt> {
|
||||
const raw = await this.getRaw(promptName);
|
||||
return {
|
||||
name: raw.name,
|
||||
version: raw.version,
|
||||
config: raw.config,
|
||||
messages: raw.compile(variables),
|
||||
};
|
||||
}
|
||||
|
||||
invalidate(promptName?: string): void {
|
||||
if (promptName) {
|
||||
this.cache.delete(promptName);
|
||||
} else {
|
||||
this.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private async getRaw(promptName: string): Promise<RawPrompt> {
|
||||
if (this.cacheEnabled) {
|
||||
const cached = this.cache.get(promptName);
|
||||
if (cached) return cached.raw;
|
||||
}
|
||||
const raw = await this.fetcher.fetch(promptName);
|
||||
if (this.cacheEnabled) {
|
||||
this.cache.set(promptName, { raw, fetchedAt: Date.now() });
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
6
src/core/langfuse/types.ts
Normal file
6
src/core/langfuse/types.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface CompiledPrompt {
|
||||
name: string;
|
||||
version: number;
|
||||
config: Record<string, unknown>;
|
||||
messages: { role: 'system' | 'user' | 'assistant'; content: string }[];
|
||||
}
|
||||
Reference in New Issue
Block a user