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

Function sanitizeMessages

lib/edge-runtime/apiResponseSimplification.ts:159–212  ·  view source on GitHub ↗
(
  messages: ChatGPTMessage[],
  sanitizeUrlsFirst: boolean,
)

Source from the content-addressed store, hash-verified

157}
158
159export function sanitizeMessages(
160 messages: ChatGPTMessage[],
161 sanitizeUrlsFirst: boolean,
162): {
163 cleanedMessages: ChatGPTMessage[];
164 originalToPlaceholderMap: StringMapping;
165} {
166 /** Removes ids and urls from messages, replaces them with variables like ID1, URL4
167 * and returns the cleaned messages and a lookup table of {value: variable} */
168 let idStore: StringMapping = {},
169 urlStore: StringMapping = {};
170 const cleanedMessages = (
171 JSON.parse(JSON.stringify(messages)) as ChatGPTMessage[]
172 ).map((message) => {
173 if (message.role === "function") {
174 try {
175 let cleanedObject = JSON.parse(message.content);
176 if (sanitizeUrlsFirst) {
177 ({ cleanedObject, urlStore } = removeUrlsFromObjs(
178 cleanedObject,
179 urlStore,
180 ));
181 ({ cleanedObject, idStore } = removeIdsFromObjs(
182 cleanedObject,
183 idStore,
184 ));
185 } else {
186 ({ cleanedObject, idStore } = removeIdsFromObjs(
187 cleanedObject,
188 idStore,
189 ));
190 ({ cleanedObject, urlStore } = removeUrlsFromObjs(
191 cleanedObject,
192 urlStore,
193 ));
194 }
195 message.content = JSON.stringify(cleanedObject);
196 } catch {
197 let cleanedMarkdown;
198 ({ cleanedMarkdown, urlStore } = removeUrlsFromMarkdown(
199 message.content,
200 urlStore,
201 ));
202 message.content = cleanedMarkdown;
203 }
204 }
205 return message;
206 });
207
208 return {
209 cleanedMessages,
210 originalToPlaceholderMap: { ...idStore, ...urlStore },
211 };
212}
213
214export function repopulateVariables(
215 obj: Json | Json[],

Callers 4

BertieFunction · 0.90
DottieFunction · 0.90
AngelaFunction · 0.90

Calls 3

removeUrlsFromObjsFunction · 0.85
removeIdsFromObjsFunction · 0.85
removeUrlsFromMarkdownFunction · 0.85

Tested by

no test coverage detected