MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / readStreamResult

Function readStreamResult

codex_image/codex_image.ts:188–246  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

186 let revisedPrompt: string | null = null;
187 let status: string | null = null;
188 let responseId: string | null = null;
189
190 for await (const chunk of response.data) {
191 buffer += chunk.toString("utf8");
192
193 let delimiterIndex = buffer.indexOf("\n\n");
194 while (delimiterIndex !== -1) {
195 const rawEvent = buffer.slice(0, delimiterIndex);
196 buffer = buffer.slice(delimiterIndex + 2);
197
198 const dataLines = rawEvent
199 .split(/\r?\n/)
200 .filter((line) => line.startsWith("data: "))
201 .map((line) => line.slice(6).trim())
202 .filter(Boolean);
203
204 for (const dataLine of dataLines) {
205 if (dataLine === "[DONE]") continue;
206
207 let payloadObj: any;
208 try {
209 payloadObj = JSON.parse(dataLine);
210 } catch {
211 continue;
212 }
213
214 const eventType = payloadObj?.type;
215 if (eventType === "response.created") {
216 responseId = payloadObj?.response?.id || responseId;
217 status = payloadObj?.response?.status || status;
218 } else if (
219 eventType === "response.image_generation_call.partial_image"
220 ) {
221 imageBase64 = payloadObj?.partial_image_b64 || imageBase64;
222 revisedPrompt = payloadObj?.revised_prompt || revisedPrompt;
223 status = payloadObj?.status || status;
224 } else if (eventType === "response.completed") {
225 status = payloadObj?.response?.status || status;
226 responseId = payloadObj?.response?.id || responseId;
227 }
228 }
229
230 delimiterIndex = buffer.indexOf("\n\n");
231 }
232 }
233
234 return { imageBase64, revisedPrompt, status, responseId };
235 };
236
237 const fetchResponseStatus = async (
238 responseId: string,
239 ): Promise<CodexResponseResult | null> => {
240 try {
241 const response = await axios.get(`${CODEX_URL}/${responseId}`, {
242 timeout: Math.min(60000, Math.max(1000, deadlineAt - Date.now())),
243 headers,
244 });
245 const data = response.data?.response || response.data;

Callers 1

callCodexImageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected