2025-05-18 01:37:54 +05:30
|
|
|
import { getCustomerIdForProject, logUsage } from "@/app/lib/billing";
|
|
|
|
|
import { USE_BILLING } from "@/app/lib/feature_flags";
|
2025-04-09 14:50:14 +05:30
|
|
|
import { redisClient } from "@/app/lib/redis";
|
2025-06-23 08:38:26 +05:30
|
|
|
import { streamResponse } from "@/app/lib/agents";
|
2025-07-18 01:18:47 +05:30
|
|
|
import { ZStreamAgentResponsePayload } from "@/app/lib/types/types";
|
2025-04-09 14:50:14 +05:30
|
|
|
|
2025-06-24 12:07:30 +05:30
|
|
|
export async function GET(request: Request, props: { params: Promise<{ streamId: string }> }) {
|
|
|
|
|
const params = await props.params;
|
2025-04-09 14:50:14 +05:30
|
|
|
// get the payload from redis
|
|
|
|
|
const payload = await redisClient.get(`chat-stream-${params.streamId}`);
|
|
|
|
|
if (!payload) {
|
|
|
|
|
return new Response("Stream not found", { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-18 01:37:54 +05:30
|
|
|
// parse the payload
|
2025-07-23 15:37:49 +05:30
|
|
|
const { projectId, workflow, messages } = ZStreamAgentResponsePayload.parse(JSON.parse(payload));
|
2025-06-23 08:38:26 +05:30
|
|
|
console.log('payload', payload);
|
2025-05-18 01:37:54 +05:30
|
|
|
|
|
|
|
|
// fetch billing customer id
|
|
|
|
|
let billingCustomerId: string | null = null;
|
|
|
|
|
if (USE_BILLING) {
|
2025-07-18 01:18:47 +05:30
|
|
|
billingCustomerId = await getCustomerIdForProject(projectId);
|
2025-03-25 01:42:22 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-18 01:37:54 +05:30
|
|
|
const encoder = new TextEncoder();
|
2025-06-23 08:38:26 +05:30
|
|
|
let messageCount = 0;
|
2025-03-25 01:42:22 +05:30
|
|
|
|
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
|
async start(controller) {
|
2025-06-23 08:38:26 +05:30
|
|
|
try {
|
|
|
|
|
// Iterate over the generator
|
2025-07-23 15:37:49 +05:30
|
|
|
for await (const event of streamResponse(projectId, workflow, messages)) {
|
2025-06-23 08:38:26 +05:30
|
|
|
// Check if this is a message event (has role property)
|
|
|
|
|
if ('role' in event) {
|
|
|
|
|
if (event.role === 'assistant') {
|
|
|
|
|
messageCount++;
|
|
|
|
|
}
|
|
|
|
|
controller.enqueue(encoder.encode(`event: message\ndata: ${JSON.stringify(event)}\n\n`));
|
|
|
|
|
} else {
|
|
|
|
|
controller.enqueue(encoder.encode(`event: done\ndata: ${JSON.stringify(event)}\n\n`));
|
2025-05-18 01:37:54 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 01:42:22 +05:30
|
|
|
controller.close();
|
2025-05-18 01:37:54 +05:30
|
|
|
|
2025-06-23 08:38:26 +05:30
|
|
|
// Log billing usage
|
2025-05-18 01:37:54 +05:30
|
|
|
if (USE_BILLING && billingCustomerId) {
|
|
|
|
|
await logUsage(billingCustomerId, {
|
|
|
|
|
type: "agent_messages",
|
|
|
|
|
amount: messageCount,
|
2025-06-23 08:38:26 +05:30
|
|
|
});
|
2025-05-18 01:37:54 +05:30
|
|
|
}
|
2025-03-25 01:42:22 +05:30
|
|
|
} catch (error) {
|
2025-05-18 01:37:54 +05:30
|
|
|
console.error('Error processing stream:', error);
|
2025-03-25 01:42:22 +05:30
|
|
|
controller.error(error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new Response(stream, {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "text/event-stream",
|
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|