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

Function removeOldestFunctionCalls

lib/edge-runtime/utils.ts:73–109  ·  view source on GitHub ↗
(
  chatGptPrompt: ChatGPTMessage[],
  model?: "3" | "3-16k" | "4",
  maxTokens?: number,
  maxTokensOut: number = MAX_TOKENS_OUT,
)

Source from the content-addressed store, hash-verified

71}
72
73export function removeOldestFunctionCalls(
74 chatGptPrompt: ChatGPTMessage[],
75 model?: "3" | "3-16k" | "4",
76 maxTokens?: number,
77 maxTokensOut: number = MAX_TOKENS_OUT,
78): ChatGPTMessage[] {
79 /** Remove old function calls if over the context limit **/
80 let tokenCount = getTokenCount(chatGptPrompt);
81 maxTokens =
82 maxTokens ?? (model === "3" ? 4096 : model === "3-16k" ? 16384 : 8192);
83 const originalTokenCount = tokenCount;
84 let numberRemoved = 0;
85 // Keep removing until under the context limit
86 while (tokenCount >= maxTokens - maxTokensOut) {
87 // Removes the oldest function call
88 const oldestFunctionCallIndex = chatGptPrompt.findIndex(
89 // 204 since 4 tokens are added to the prompt for each message
90 (m) => m.role === "function" && getTokenCount([m]) >= 204,
91 );
92 if (oldestFunctionCallIndex === -1) {
93 // No function calls left to remove
94 break;
95 }
96 chatGptPrompt[oldestFunctionCallIndex].content = "Cut for brevity";
97 tokenCount = getTokenCount(chatGptPrompt);
98 numberRemoved += 1;
99 }
100 console.info(
101 "Removed " +
102 numberRemoved +
103 " function calls due to context limit. Original token count: " +
104 originalTokenCount +
105 ", new token count: " +
106 tokenCount,
107 );
108 return chatGptPrompt;
109}
110
111export function getJsonMIMEType<inObj>(
112 inputDict: Record<string, inObj> | undefined | null,

Callers 5

BertieFunction · 0.90
DottieFunction · 0.90
AngelaFunction · 0.90
getMissingParamFunction · 0.90

Calls 1

getTokenCountFunction · 0.90

Tested by

no test coverage detected