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

Function getRelevantMessages

lib/edge-runtime/dataAnalysis.ts:273–302  ·  view source on GitHub ↗
(
  chatHistory: GPTMessageInclSummary[],
)

Source from the content-addressed store, hash-verified

271}
272
273export function getRelevantMessages(
274 chatHistory: GPTMessageInclSummary[],
275): NonUserMessage[] | null {
276 /** A semi-common error the LLM makes is not including the API calls required for
277 * data analysis in the same output as the data analysis call. So if there are no
278 * API calls we check previous query-response sections in the chat history. **/
279 let lastUserIdx = -1;
280
281 for (let i = 0; i < chatHistory.length; i++) {
282 lastUserIdx = findLastIndex(
283 chatHistory.slice(0, lastUserIdx),
284 (m) => m.role === "user",
285 );
286 const relevantMessages = chatHistory
287 .slice(lastUserIdx)
288 .filter(
289 (m) =>
290 m.role !== "user" &&
291 (m.role !== "function" || m.name !== dataAnalysisActionName),
292 ) as NonUserMessage[];
293 if (relevantMessages.filter((m) => m.role === "function").length > 0) {
294 console.log("Found non-data analysis function call");
295 return relevantMessages;
296 } else if (lastUserIdx === 0) {
297 // Terminate early if you reach the start of the chat history
298 return null;
299 }
300 }
301 return null;
302}

Callers 2

runDataAnalysisFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected