Spaces:
Sleeping
Sleeping
wuyiqunLu commited on
feat: add timeout and remove png from code result (#80)
Browse filesthe error message could not be surfaced somehow and instead got network
error, but it timeout as expected
10 unmodified lines11121314151674 unmodified lines9192939495969750 unmodified lines1481491501511521534 unmodified lines1581591601611621632 unmodified lines16616716816917017117217317410 unmodified lines// export const runtime = 'edge';export const dynamic = 'force-dynamic';export const maxDuration = 300; // This function can run for a maximum of 5 minutesconst uploadBase64 = async (base64: string,74 unmodified linesconst fetchResponse = await fetch(`https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&self_reflection=false`,// `http://localhost:5001/v1/agent/chat?agent_class=vision_agent&self_reflection=${enableSelfReflection}`,{method: 'POST',headers: {50 unmodified linesconst decoder = new TextDecoder('utf-8');let maxChunkSize = 0;let buffer = '';const stream = new ReadableStream({async start(controller) {// const parser = createParser(streamParser);4 unmodified linesconst lines = buffer.split('\n').filter(line => line.trim().length > 0);buffer = lines.pop() ?? ''; // Save the last incomplete line back to the bufferlet done = false;const parseLine = async (2 unmodified lines) => {try {const msg = JSON.parse(line);if (msg.type !== 'final_code') {return line;}const result = JSON.parse(msg.payload.result) as ResultPayload;for (let index = 0; index < result.results.length; index++) {const png = result.results[index].png ?? '';10 unmodified lines1112131415161774 unmodified lines9293949596979850 unmodified lines1491501511521531541554 unmodified lines1601611621631641651661671681691701711721731741751761771781791801811822 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021110 unmodified lines// export const runtime = 'edge';export const dynamic = 'force-dynamic';export const maxDuration = 300; // This function can run for a maximum of 5 minutesconst TIMEOUT_MILI_SECONDS = 5 * 60 * 1000;const uploadBase64 = async (base64: string,74 unmodified linesconst fetchResponse = await fetch(`https://api.dev.landing.ai/v1/agent/chat?agent_class=vision_agent&self_reflection=false`,// `http://localhost:5001/v1/agent/chat?agent_class=vision_agent&self_reflection=false`,{method: 'POST',headers: {50 unmodified linesconst decoder = new TextDecoder('utf-8');let maxChunkSize = 0;let buffer = '';let time = Date.now();const stream = new ReadableStream({async start(controller) {// const parser = createParser(streamParser);4 unmodified linesconst lines = buffer.split('\n').filter(line => line.trim().length > 0);if (lines.length === 0) {if (Date.now() - time > TIMEOUT_MILI_SECONDS) {logger.info(session,{message: 'Agent timed out',},request,'__Agent_timeout__',);controller.error(`Haven't received any response in last ${TIMEOUT_MILI_SECONDS / 60000} minutes, agent timed out.`,);}} else {time = Date.now();}buffer = lines.pop() ?? ''; // Save the last incomplete line back to the bufferlet done = false;const parseLine = async (2 unmodified lines) => {try {const msg = JSON.parse(line);if (msg.type !== 'final_code' &&(msg.type !== 'code' ||msg.status === 'started' ||msg.status === 'running')) {return line;}if (msg.type === 'code') {msg.payload.result = JSON.stringify({...msg.payload.result,results: msg.payload.result.results.map((_result: any) => {return {..._result,png: undefined,mp4: undefined,};}),});return JSON.stringify(msg);}const result = JSON.parse(msg.payload.result) as ResultPayload;for (let index = 0; index < result.results.length; index++) {const png = result.results[index].png ?? '';
app/api/vision-agent/route.ts+39-2