Skip to content

Commit

Permalink
Wait using Promise.all()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rindrics committed Oct 18, 2024
1 parent a2f04a5 commit c90031e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions app/(playground)/p/[agentId]/beta-proto/graph/server-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,29 @@ import type { AgentId } from "../types";
import { elementsToMarkdown } from "../utils/unstructured";
import type { Graph } from "./types";

const flushMetricsAndShutdown = async (lf: Langfuse, metricReader: any) => {
return new Promise<void>((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error("Metric flush timeout after 20 seconds"));
}, 20000);

Promise.all([metricReader.forceFlush(), lf.shutdownAsync()])
.then(() => {
clearTimeout(timeoutId);
resolve();
})
.catch((error) => {
clearTimeout(timeoutId);
reject(error);
});
});
};

type GenerateArtifactStreamParams = {
userPrompt: string;
systemPrompt?: string;
};

export async function generateArtifactStream(
params: GenerateArtifactStreamParams,
) {
Expand Down Expand Up @@ -78,6 +97,19 @@ export async function generateArtifactStream(
stream.done();
})();

waitUntil(
flushMetricsAndShutdown(lf, metricReader).catch((error) => {
if (error.message === "Metric flush timeout after 20 seconds") {
console.error("Metric flush and Langfuse shutdown timed out:", error);
} else {
console.error(
"Error during metric flush and Langfuse shutdown:",
error,
);
}
}),
);

return { object: stream.value };
}

Expand Down

0 comments on commit c90031e

Please sign in to comment.