mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-07-03 20:41:07 +02:00
Use POST method SSE streaming in agents
This change eliminates an extra API endpoint from agents service. It also removes dependency on redis
This commit is contained in:
parent
ceaef81a5b
commit
8731dc716c
6 changed files with 125 additions and 252 deletions
|
|
@ -1,11 +1,18 @@
|
|||
import { redisClient } from "@/app/lib/redis";
|
||||
|
||||
export async function GET(request: Request, { params }: { params: { streamId: string } }) {
|
||||
// Replace with your actual upstream SSE endpoint.
|
||||
const upstreamUrl = `${process.env.AGENTS_API_URL}/chat_stream/${params.streamId}`;
|
||||
console.log('upstreamUrl', upstreamUrl);
|
||||
|
||||
// get the payload from redis
|
||||
const payload = await redisClient.get(`chat-stream-${params.streamId}`);
|
||||
if (!payload) {
|
||||
return new Response("Stream not found", { status: 404 });
|
||||
}
|
||||
|
||||
// Fetch the upstream SSE stream.
|
||||
const upstreamResponse = await fetch(upstreamUrl, {
|
||||
const upstreamResponse = await fetch(`${process.env.AGENTS_API_URL}/chat_stream`, {
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${process.env.AGENTS_API_KEY || 'test'}`,
|
||||
},
|
||||
cache: 'no-store',
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { z } from "zod";
|
|||
import { generateObject } from "ai";
|
||||
import { ApiMessage } from "./types/types";
|
||||
import { openai } from "@ai-sdk/openai";
|
||||
import { redisClient } from "./redis";
|
||||
|
||||
export async function getAgenticApiResponse(
|
||||
request: z.infer<typeof AgenticAPIChatRequest>,
|
||||
|
|
@ -38,24 +39,20 @@ export async function getAgenticApiResponse(
|
|||
export async function getAgenticResponseStreamId(
|
||||
request: z.infer<typeof AgenticAPIChatRequest>,
|
||||
): Promise<z.infer<typeof AgenticAPIInitStreamResponse>> {
|
||||
// call agentic api
|
||||
console.log(`sending agentic api init stream request`, JSON.stringify(request));
|
||||
const response = await fetch(process.env.AGENTS_API_URL + '/chat_stream_init', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${process.env.AGENTS_API_KEY || 'test'}`,
|
||||
},
|
||||
// serialize the request
|
||||
const payload = JSON.stringify(request);
|
||||
|
||||
// create a uuid for the stream
|
||||
const streamId = crypto.randomUUID();
|
||||
|
||||
// store payload in redis
|
||||
await redisClient.set(`chat-stream-${streamId}`, payload, {
|
||||
EX: 60 * 10, // expire in 10 minutes
|
||||
});
|
||||
if (!response.ok) {
|
||||
console.error('Failed to call agentic init stream api', response);
|
||||
throw new Error(`Failed to call agentic init stream api: ${response.statusText}`);
|
||||
}
|
||||
const responseJson = await response.json();
|
||||
console.log(`received agentic api init stream response`, JSON.stringify(responseJson));
|
||||
const result: z.infer<typeof AgenticAPIInitStreamResponse> = responseJson;
|
||||
return result;
|
||||
|
||||
return {
|
||||
streamId,
|
||||
};
|
||||
}
|
||||
|
||||
// create a PrefixLogger class that wraps console.log with a prefix
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue