Brings the branch up to the current base (executor settings, project skills, Chinese run messages, cloud IDE removal, wish attachments) and resolves the ten conflicted files, keeping both sides: - wish-refiner: the tenant's executor settings pick the models first, then the Copilot model setting; the tenant credential still goes before the operator's login - worker providers: executor settings apply, and a run with its own credential still gets an empty home - main.ts: Copilot screenshot uploads and wish attachments each keep their own body limit - contracts, http, web types: default budget and skill auto-activation, Copilot and attachment entity kinds, ./text and ./attachments exports - globals.css: Copilot styles and attachment styles, one after the other - tests: the canceled-release reason is Chinese; the refiner's default models have no [1m] suffix, as this branch names them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
104 lines
4.3 KiB
TypeScript
104 lines
4.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import {
|
|
authorizationFor,
|
|
browserMayCall,
|
|
cookieOptions,
|
|
identityHeaders,
|
|
publicOrigin,
|
|
SESSION_COOKIE,
|
|
} from '@/lib/auth';
|
|
export const runtime = 'nodejs';
|
|
export const dynamic = 'force-dynamic';
|
|
type Context = { params: Promise<{ path: string[] }> };
|
|
async function proxy(request: NextRequest, context: Context) {
|
|
const { path } = await context.params;
|
|
if (path.some(part => !part || part === '..' || part === '.' || part.includes('/') || part.includes('\\')))
|
|
return NextResponse.json({ error: { code: 'INVALID_PATH', message: '无效的 API 路径。' } }, { status: 400 });
|
|
// Password checks are the console's own sign-in, never a browser's request.
|
|
if (!browserMayCall(path))
|
|
return NextResponse.json({ error: { code: 'NOT_FOUND', message: '无效的 API 路径。' } }, { status: 404 });
|
|
if (!['GET', 'HEAD'].includes(request.method)) {
|
|
const origin = request.headers.get('origin');
|
|
if ((origin && origin !== publicOrigin(request)) || request.headers.get('sec-fetch-site') === 'cross-site')
|
|
return NextResponse.json({ error: { code: 'ORIGIN_DENIED', message: '请求来源不被允许。' } }, { status: 403 });
|
|
}
|
|
const { token, cookie, localUser } = await authorizationFor(request);
|
|
if (!token)
|
|
return NextResponse.json(
|
|
{
|
|
error: {
|
|
code: 'UNAUTHENTICATED',
|
|
message: '需要有效的工作空间登录会话。',
|
|
},
|
|
},
|
|
{ status: 401 },
|
|
);
|
|
const base = (process.env.WF_API_URL ?? 'http://127.0.0.1:4000').replace(/\/$/, '');
|
|
const url = `${base}/v1/${path.map(encodeURIComponent).join('/')}${request.nextUrl.search}`;
|
|
const headers = new Headers(identityHeaders(token, localUser));
|
|
for (const key of ['content-type', 'accept', 'idempotency-key', 'last-event-id']) {
|
|
const value = request.headers.get(key);
|
|
if (value) headers.set(key, value);
|
|
}
|
|
const stream = path.at(-1) === 'stream';
|
|
// The Copilot answers within 270 s and a pending answer is given up after 300 s; the proxy waits 360 s.
|
|
// Optimizing a wish keeps its own 400 s budget for its model attempts.
|
|
const route = path.at(-1);
|
|
const timeout = route === 'messages' ? 360_000 : route === 'wish-refinements' ? 400_000 : 30_000;
|
|
try {
|
|
const upstream = await fetch(url, {
|
|
method: request.method,
|
|
headers,
|
|
body: ['GET', 'HEAD'].includes(request.method) ? undefined : await request.arrayBuffer(),
|
|
cache: 'no-store',
|
|
redirect: 'manual',
|
|
signal: stream ? request.signal : AbortSignal.any([request.signal, AbortSignal.timeout(timeout)]),
|
|
});
|
|
if (upstream.status >= 300 && upstream.status < 400)
|
|
return NextResponse.json(
|
|
{
|
|
error: {
|
|
code: 'UPSTREAM_REDIRECT',
|
|
message: '控制面返回了意外重定向。',
|
|
},
|
|
},
|
|
{ status: 502 },
|
|
);
|
|
const responseHeaders = new Headers({
|
|
'Cache-Control': 'no-store',
|
|
'Content-Type': upstream.headers.get('content-type') ?? 'application/json',
|
|
});
|
|
const disposition = upstream.headers.get('content-disposition');
|
|
if (disposition) responseHeaders.set('Content-Disposition', disposition);
|
|
// Files people uploaded come back as what they say they are, and never run as a page.
|
|
responseHeaders.set('X-Content-Type-Options', 'nosniff');
|
|
const policy = upstream.headers.get('content-security-policy');
|
|
if (policy) responseHeaders.set('Content-Security-Policy', policy);
|
|
if (stream) {
|
|
responseHeaders.set('X-Accel-Buffering', 'no');
|
|
responseHeaders.set('Connection', 'keep-alive');
|
|
}
|
|
const response = new NextResponse(upstream.body, { status: upstream.status, headers: responseHeaders });
|
|
// A refreshed access token travels back in the encrypted session cookie.
|
|
if (cookie)
|
|
response.cookies.set(SESSION_COOKIE, cookie.value, { ...cookieOptions(request), maxAge: cookie.maxAge });
|
|
return response;
|
|
} catch {
|
|
return NextResponse.json(
|
|
{
|
|
error: {
|
|
code: 'CONTROL_PLANE_UNAVAILABLE',
|
|
message: '无法连接控制面。请检查服务地址与运行状态。',
|
|
},
|
|
},
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
}
|
|
export const GET = proxy;
|
|
export const POST = proxy;
|
|
export const PATCH = proxy;
|
|
export const PUT = proxy;
|
|
export const DELETE = proxy;
|
|
export const HEAD = proxy;
|