Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions flight-booking-app/app/api/chat/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { chatMessageHook } from "@/workflows/chat/hooks/chat-message";

export async function POST(
req: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { message } = await req.json();
const { id: threadId } = await params;

console.log("Resuming hook for thread:", threadId, "with message:", message);

try {
await chatMessageHook.resume(`thread:${threadId}`, { message });
return Response.json({ success: true });
} catch (error) {
console.error("Error resuming hook for thread:", threadId, error);
return Response.json(
{
error: `Failed to resume hook: ${
error instanceof Error ? error.message : "Unknown error"
}`,
},
{ status: 500 }
);
}
}
26 changes: 14 additions & 12 deletions flight-booking-app/app/api/chat/[id]/stream/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import { getRun } from "workflow/api";
//export const maxDuration = 5;

export async function GET(
request: Request,
{ params }: { params: Promise<{ id: string }> },
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const { searchParams } = new URL(request.url);
const startIndexParam = searchParams.get("startIndex");
const startIndex =
startIndexParam !== null ? parseInt(startIndexParam, 10) : undefined;
const run = getRun(id);
const stream = run.getReadable({ startIndex });
const { id } = await params;
const { searchParams } = new URL(request.url);

return createUIMessageStreamResponse({
stream,
});
const startIndexParam = searchParams.get("startIndex");
const startIndex =
startIndexParam !== null ? parseInt(startIndexParam, 10) : undefined;

const run = getRun(id);
const stream = run.getReadable({ startIndex });

return createUIMessageStreamResponse({
stream,
});
}
38 changes: 27 additions & 11 deletions flight-booking-app/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@ import { chat } from "@/workflows/chat";
//export const maxDuration = 8;

export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const body = await req.json();

const run = await start(chat, [messages]);
const workflowStream = run.readable;
// Extract threadId from body or generate one if not provided
const threadId: string =
body.threadId ||
body.messages?.[0]?.metadata?.threadId ||
crypto.randomUUID();
const messages: UIMessage[] = body.messages || [];

return createUIMessageStreamResponse({
stream: workflowStream,
headers: {
// The workflow run ID is stored into `localStorage` on the client side,
// which influences the `resume` flag in the `useChat` hook.
"x-workflow-run-id": run.runId,
},
});
console.log(
"Starting chat workflow for thread:",
threadId,
"with",
messages.length,
"messages"
);

const run = await start(chat, [threadId, messages]);
const workflowStream = run.readable;

return createUIMessageStreamResponse({
stream: workflowStream,
headers: {
// The workflow run ID is stored on the client side for reconnection
"x-workflow-run-id": run.runId,
// The thread ID is used for sending follow-up messages via hooks
"x-thread-id": threadId,
},
});
}
2 changes: 1 addition & 1 deletion flight-booking-app/app/api/hooks/approval/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bookingApprovalHook } from '@/workflows/chat/hooks/approval';
import { bookingApprovalHook } from "@/workflows/chat/hooks/approval";

export async function POST(request: Request) {
const { toolCallId, approved, comment } = await request.json();
Expand Down
Loading