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

Function hideLongGraphOutputs

lib/v2/edge-runtime/ai.ts:89–136  ·  view source on GitHub ↗
(
  chatGptPrompt: ChatGPTMessage[],
  fnNames?: string[],
)

Source from the content-addressed store, hash-verified

87};
88
89export function hideLongGraphOutputs(
90 chatGptPrompt: ChatGPTMessage[],
91 fnNames?: string[],
92): {
93 chatGptPrompt: ChatGPTMessage[];
94 graphDataHidden: boolean;
95} {
96 const names = fnNames ?? [dataAnalysisActionName];
97 let graphDataHidden = false;
98 const out = chatGptPrompt.map((m) => {
99 if (m.role === "function" && names.includes(m.name)) {
100 if (
101 // No newlines in our minified JSONs
102 !m.content.includes("\n") &&
103 // Below are to check it's a JSON
104 ["{", "["].includes(m.content[0]) &&
105 ["}", "]"].includes(m.content[m.content.length - 1]) &&
106 // Got to be long otherwise no point
107 getTokenCount([m]) > 500
108 ) {
109 try {
110 const graphData = JSON.parse(m.content);
111 const arrayLength = graphData.data.length;
112 const graphOrTable = graphData.type === "table" ? "table" : "graph";
113 let dataStr = "";
114 if (getTokenCount(JSON.stringify(graphData.data.slice(0, 3))) > 300) {
115 dataStr = `[${JSON.stringify(graphData.data[0])},`;
116 } else {
117 dataStr = `[${graphData.data
118 .slice(0, 3)
119 .map(JSON.stringify)
120 .join(",")},`;
121 }
122 delete graphData.data;
123 m.content = JSON.stringify(graphData);
124 dataStr += `<further elements cut for brevity (total length: ${arrayLength}) - DO NOT pretend to know the data, instead tell the user to look at this ${graphOrTable}>]`;
125 m.content = m.content.slice(0, -1) + ',"data":' + dataStr + "}";
126 graphDataHidden = true;
127 } catch (e) {}
128 } else if (getTokenCount([m]) > 500) {
129 // Hiding logs
130 m.content = "<cut for brevity - DO NOT pretend to know the logs>";
131 }
132 }
133 return m;
134 });
135 return { chatGptPrompt: out, graphDataHidden };
136}
137
138export async function Bertie( // Bertie will eat you for breakfast
139 controller: ReadableStreamDefaultController,

Callers 4

CassiusFunction · 0.90
executeMessagesFunction · 0.90
ai.test.tsFile · 0.90
BertieFunction · 0.85

Calls 1

getTokenCountFunction · 0.90

Tested by

no test coverage detected