-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathroute.ts
More file actions
209 lines (179 loc) · 6.51 KB
/
route.ts
File metadata and controls
209 lines (179 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* /api/chat — The single API endpoint that drives the chat-based login flow.
*
* Actions:
* start → Creates a BrowserBase session, returns liveViewUrl immediately.
* submit → Acts on the current screen. User actions (form fill, choice click)
* return an SSE stream with two events: action_complete + screen.
* Auto-retries (loading, blocked) return plain JSON.
* close → Tears down the browser session.
*/
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import {
createSession,
getSession,
closeSession,
getPageContext,
waitForPageContent,
} from "@/lib/ai-login/browser";
import { analyzeLoginPage, handleScreen } from "@/lib/ai-login/agent";
import { LoginStateSchema } from "@/lib/ai-login/types";
// ---------------------------------------------------------------------------
// Request validation schemas
// ---------------------------------------------------------------------------
const StartBody = z.object({
action: z.literal("start"),
url: z.string().url().or(z.string().min(1)),
});
const SubmitBody = z.object({
action: z.literal("submit"),
sessionId: z.string().min(1),
screen: LoginStateSchema,
values: z.record(z.string(), z.string()).default({}),
});
const CloseBody = z.object({
action: z.literal("close"),
sessionId: z.string().min(1),
});
// ---------------------------------------------------------------------------
// Response helpers
// ---------------------------------------------------------------------------
function ok(body: Record<string, unknown>) {
return NextResponse.json(body);
}
function err(message: string, status = 500) {
return NextResponse.json({ error: message }, { status });
}
/** Create an SSE response that streams events to the frontend. */
function sseResponse(
handler: (send: (event: string, data: unknown) => void) => Promise<void>,
extraHeaders?: Record<string, string>,
): Response {
const encoder = new TextEncoder();
const stream = new ReadableStream({
async start(controller) {
const send = (event: string, data: unknown) => {
controller.enqueue(
encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`),
);
};
try {
await handler(send);
} catch (error) {
const message =
error instanceof Error ? error.message : "Internal server error";
send("error", { message });
} finally {
controller.close();
}
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
...extraHeaders,
},
});
}
// ---------------------------------------------------------------------------
// POST handler
// ---------------------------------------------------------------------------
export async function POST(request: NextRequest) {
let body: unknown;
try {
body = await request.json();
} catch {
return err("Invalid JSON", 400);
}
const { action } = body as { action?: string };
// Build CORS headers for raw Response objects (SSE).
const origin = request.headers.get("origin") ?? "";
const corsHeaders: Record<string, string> = origin
? { "Access-Control-Allow-Origin": origin }
: {};
try {
// ----------------------------------------------------------------
// ACTION: start — launch browser and return live view immediately
// ----------------------------------------------------------------
if (action === "start") {
const parsed = StartBody.safeParse(body);
if (!parsed.success) return err(parsed.error.issues[0].message, 400);
const session = await createSession();
// Kick off navigation in the background
session.page
.goto(parsed.data.url, {
waitUntil: "domcontentloaded",
timeout: 30000,
})
.catch(() => {});
return ok({
sessionId: session.sessionId,
liveViewUrl: session.liveViewUrl,
screen: { type: "loading_screen" },
screenshot: null,
});
}
// ----------------------------------------------------------------
// ACTION: submit — act on the current screen
// ----------------------------------------------------------------
if (action === "submit") {
const parsed = SubmitBody.safeParse(body);
if (!parsed.success) return err(parsed.error.issues[0].message, 400);
const { sessionId, screen, values } = parsed.data;
const session = await getSession(sessionId);
// Determine if this is a user-initiated action (has actual values)
const hasUserInput = Object.values(values).some((v) => v);
const isUserAction =
hasUserInput &&
[
"credential_login_form",
"choice_screen",
"magic_login_link",
"noop_screen",
].includes(
screen.type,
);
if (isUserAction) {
// SSE: stream action_complete, then analyze and stream screen
return sseResponse(async (send) => {
const { message } = await handleScreen(session, screen, values);
// 1. Signal that the action is done (form fill complete)
send("action_complete", {
action: message.type === "action" ? message.action : "Done",
});
// 2. Wait for page content to settle, then analyze
await waitForPageContent(session.page);
const { screen: analyzed, screenshot } =
await analyzeLoginPage(session);
send("screen", { screen: analyzed, screenshot });
}, corsHeaders);
}
// JSON: auto-retry (loading_screen, blocked_screen) or input_request
const { nextScreen, message } = await handleScreen(
session,
screen,
values,
);
const { screenshot } = await getPageContext(session.page);
return ok({ screen: nextScreen, screenshot, message });
}
// ----------------------------------------------------------------
// ACTION: close — tear down browser session
// ----------------------------------------------------------------
if (action === "close") {
const parsed = CloseBody.safeParse(body);
if (!parsed.success) return err(parsed.error.issues[0].message, 400);
await closeSession(parsed.data.sessionId);
return ok({ success: true });
}
return err("Unknown action", 400);
} catch (error) {
console.error("[/api/chat]", error);
return err(
error instanceof Error ? error.message : "Internal server error",
);
}
}