mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-04 21:04:37 +08:00
pr review changes
This commit is contained in:
parent
a50240c2f9
commit
e066cac7fd
@ -75,13 +75,15 @@ export const POST = createSmartRouteHandler({
|
||||
body: result.toUIMessageStreamResponse(),
|
||||
};
|
||||
} else {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 120_000);
|
||||
const result = await generateText({
|
||||
model,
|
||||
system: systemPrompt,
|
||||
messages,
|
||||
tools: toolsArg,
|
||||
stopWhen: stepCountIs(stepLimit),
|
||||
});
|
||||
}).finally(() => clearTimeout(timeoutId));
|
||||
|
||||
const contentBlocks: Array<
|
||||
| { type: "text", text: string }
|
||||
|
||||
@ -47,7 +47,23 @@ function sanitizeGeneratedCode(code: string): string {
|
||||
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const payload = await req.json() as {
|
||||
const rawPayload: unknown = await req.json();
|
||||
|
||||
if (
|
||||
typeof rawPayload !== "object" || rawPayload == null ||
|
||||
typeof (rawPayload as Record<string, unknown>).projectId !== "string" ||
|
||||
typeof (rawPayload as Record<string, unknown>).systemPrompt !== "string" ||
|
||||
!Array.isArray((rawPayload as Record<string, unknown>).tools) ||
|
||||
!Array.isArray((rawPayload as Record<string, unknown>).messages)
|
||||
) {
|
||||
return new Response(JSON.stringify({ error: "Invalid request payload: projectId, systemPrompt, tools, and messages are required" }), {
|
||||
status: 400,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
// Safe to destructure after runtime validation above
|
||||
const { projectId, systemPrompt, tools, messages, quality = "smartest", speed = "fast" } = rawPayload as {
|
||||
projectId: string,
|
||||
systemPrompt: string,
|
||||
tools: string[],
|
||||
@ -56,15 +72,13 @@ export async function POST(req: Request) {
|
||||
speed?: string,
|
||||
};
|
||||
|
||||
const { projectId, systemPrompt, tools, messages, quality = "smartest", speed = "fast" } = payload;
|
||||
|
||||
const user = await stackServerApp.getUser({ or: "redirect" });
|
||||
const accessToken = await user.getAccessToken();
|
||||
|
||||
const projects = await user.listOwnedProjects();
|
||||
const hasProjectAccess = projects.some((p) => p.id === projectId);
|
||||
|
||||
if (!hasProjectAccess || !accessToken) {
|
||||
if (!hasProjectAccess || accessToken == null) {
|
||||
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
||||
status: 403,
|
||||
headers: { "content-type": "application/json" },
|
||||
@ -98,9 +112,11 @@ export async function POST(req: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
const result = await backendResponse.json() as { content: Array<{ type: string, args?: { content?: string, [key: string]: unknown }, [key: string]: unknown }> };
|
||||
const result: unknown = await backendResponse.json();
|
||||
const contentArr: Array<{ type: string, args?: { content?: string, [key: string]: unknown }, [key: string]: unknown }> =
|
||||
Array.isArray((result as Record<string, unknown> | null)?.content) ? (result as Record<string, unknown>).content as typeof contentArr : [];
|
||||
const sanitized = {
|
||||
content: result.content.map((item) => {
|
||||
content: contentArr.map((item) => {
|
||||
if (item.type === "tool-call" && typeof item.args?.content === "string") {
|
||||
return { ...item, args: { ...item.args, content: sanitizeGeneratedCode(item.args.content) } };
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ai-sdk/react": "^3.0.0",
|
||||
"@openrouter/ai-sdk-provider": "0.7.5",
|
||||
"@modelcontextprotocol/sdk": "^1.17.2",
|
||||
"@phosphor-icons/react": "^2.1.10",
|
||||
"@radix-ui/react-collapsible": "^1.1.11",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user