MCPcopy Create free account
hub / github.com/Superflows-AI/superflows / LlmResponseCache

Class LlmResponseCache

lib/edge-runtime/llmResponseCache.ts:10–368  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import _ from "lodash";
9
10export class LlmResponseCache {
11 private matchConvId: number | null;
12 private messages: GPTMessageInclSummary[];
13 private analyticsMessages:
14 | { instruction_message: string; output: string }[]
15 | null;
16 private orgId: number;
17
18 constructor() {
19 this.matchConvId = null;
20 this.messages = [];
21 this.analyticsMessages = null;
22 this.orgId = -1;
23 }
24
25 async initialize(
26 userMessage: string,
27 orgId: number,
28 conversationIndex: number,
29 supabase: SupabaseClient<Database>,
30 ): Promise<void> {
31 this.orgId = orgId;
32 const { data: matchingConvData, error: matchConvError } = await supabase
33 .from("chat_messages")
34 .select("conversation_id")
35 .match({
36 role: "user",
37 org_id: orgId,
38 content: userMessage,
39 conversation_index: conversationIndex,
40 fresh: true,
41 })
42 // Take the most recent matching conversation that isn't the current one
43 .order("conversation_id", { ascending: false })
44 // This will return the current convo & optionally the most recent matching 4 convos
45 .limit(5);
46 if (matchConvError) console.error(matchConvError.message);
47
48 if (matchingConvData && matchingConvData?.length > 1) {
49 console.log("Main hit", matchingConvData.slice(1));
50 // Get chat messages for all matching conversations
51 const { data: matchingChatData, error: chatError } = await supabase
52 .from("chat_messages")
53 .select(
54 "role,content,name,summary,conversation_index,conversation_id,chosen_actions,chosen_route,chat_summary",
55 )
56 .eq("org_id", orgId)
57 .in(
58 "conversation_id",
59 matchingConvData.slice(1).map((c) => c.conversation_id),
60 )
61 .order("conversation_id", { ascending: false });
62 if (chatError) console.error(chatError.message);
63 if (matchingChatData && matchingChatData.length > 1) {
64 // Iterate through the matching conversations to find if there's a valid one
65 for (const convId of matchingConvData
66 .slice(1)
67 .map((c) => c.conversation_id)) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected