Skip to content

Instantly share code, notes, and snippets.

@rishi23root
Created March 2, 2024 05:48
Show Gist options
  • Save rishi23root/340783a1593795a6ccd0409f09b36901 to your computer and use it in GitHub Desktop.
Save rishi23root/340783a1593795a6ccd0409f09b36901 to your computer and use it in GitHub Desktop.
workaround for the vercel server function execution limits (10sec) for the hobby plan, go edge with pretending stream
import { Inputs } from "@/types/builder";
import { PdfToSchema } from "@/utils/openai/util";
export const runtime = 'edge';
function streamTillPromise(cb: Promise<any>) {
var isResolved = false;
const results = cb.then((data: any) => {
isResolved = true;
console.log('[resolved]');
return data;
}).catch((err: any) => {
isResolved = true;
console.log('[rejected]');
return err;
})
const encoder = new TextEncoder();
return new ReadableStream({
start(controller) {
const timer = setInterval(async () => {
if (isResolved) {
var res: any;
try {
res = (await results);
if (typeof res === 'object') {
res = JSON.stringify(res);
}
controller.enqueue(encoder.encode(res));
} catch (err) {
controller.enqueue(encoder.encode('error: ' + err));
} finally {
controller.close();
clearInterval(timer);
}
} else {
controller.enqueue(encoder.encode('0'));
}
}, 300);
},
});
}
export async function POST(request: Request) {
const { pdfText } = await request.json();
async function heavyTaskWrapper(text: string) {
if (text) {
try {
// convert data to JSON using open ai api function calling
// const text = "data here";
const call = new PdfToSchema(text);
const results = await call.extractSchema();
console.log("[info] Analysing Done");
return {
jsonData: results as Inputs,
error: ""
};
} catch (err) {
console.log(err);
return {
error: "unable to extract text from the format :(, we are redirecting you to the manual form.",
jsonData: {}
};
}
} else {
return {
jsonData: {},
error: "pdf text not found in the request body",
}
}
}
const stream = streamTillPromise(heavyTaskWrapper(pdfText as string));
return new Response(stream, {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
});
}
@Josh9100
Copy link

Josh9100 commented May 5, 2024

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment