diff --git a/src/adapter/image-handlers.ts b/src/adapter/image-handlers.ts new file mode 100644 index 0000000..db3ec78 --- /dev/null +++ b/src/adapter/image-handlers.ts @@ -0,0 +1,322 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import { maskUrl, sanitizeApiBase } from '../lib/http-utils.js' +import type { PipelineContext } from '../proxy/pipeline.js' +import { AdapterError, resolveAdapterRoute } from './router.js' + +const ADAPTER_IMAGE_PATH_RE = /^\/([a-zA-Z0-9_-]+)\/v1\/images\/(generations|edits)(\?.*)?$/ +const HEADER_SEPARATOR = Buffer.from('\r\n\r\n') +const LINE_SEPARATOR = Buffer.from('\r\n') +const MAX_IMAGE_REQUEST_BODY_BYTES = 20 * 1024 * 1024 + +class ImageRequestBodyTooLargeError extends Error { + constructor() { + super(`图片请求体超过 ${MAX_IMAGE_REQUEST_BODY_BYTES} 字节限制`) + this.name = 'ImageRequestBodyTooLargeError' + } +} + +function writeJson(res: ServerResponse, status: number, body: Record): void { + res.writeHead(status, { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + }) + res.end(JSON.stringify(body)) +} + +function authenticateProxyRequest( + ctx: PipelineContext, + req: IncomingMessage, + res: ServerResponse, + logLabel: string +): boolean { + const { config } = ctx.store.getConfig() + if (!config.proxyKey) return true + + const auth = req.headers.authorization ?? req.headers['x-api-key'] ?? '' + const key = String(auth).replace(/^Bearer\s+/i, '').trim() + if (key === config.proxyKey) return true + + ctx.logger.log('request', `${logLabel} auth failed`, { auth: key ? 'sk-***' : '(empty)' }, 'warn') + writeJson(res, 401, { error: { message: '代理 API Key 无效' } }) + return false +} + +async function readRawBody(req: IncomingMessage): Promise { + const declaredLength = Number(req.headers['content-length']) + if (Number.isFinite(declaredLength) && declaredLength > MAX_IMAGE_REQUEST_BODY_BYTES) { + throw new ImageRequestBodyTooLargeError() + } + + const chunks: Buffer[] = [] + let totalBytes = 0 + for await (const chunk of req) { + const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk) + totalBytes += buffer.length + if (totalBytes > MAX_IMAGE_REQUEST_BODY_BYTES) throw new ImageRequestBodyTooLargeError() + chunks.push(buffer) + } + return Buffer.concat(chunks) +} + +function multipartBoundary(contentType: string): string | null { + const match = contentType.match(/boundary=(?:"([^"]+)"|([^;\s]+))/i) + return match?.[1] ?? match?.[2] ?? null +} + +function findMultipartField( + rawBody: Buffer, + boundary: string, + fieldName: string +): { start: number; end: number } | null { + const delimiter = Buffer.from(`--${boundary}`) + const nextDelimiter = Buffer.from(`\r\n--${boundary}`) + let cursor = 0 + + while (cursor < rawBody.length) { + const delimiterStart = rawBody.indexOf(delimiter, cursor) + if (delimiterStart < 0) return null + + let headerStart = delimiterStart + delimiter.length + if (rawBody.subarray(headerStart, headerStart + 2).equals(Buffer.from('--'))) return null + if (rawBody.subarray(headerStart, headerStart + 2).equals(LINE_SEPARATOR)) headerStart += 2 + + const headerEnd = rawBody.indexOf(HEADER_SEPARATOR, headerStart) + if (headerEnd < 0) return null + const valueStart = headerEnd + HEADER_SEPARATOR.length + const valueEnd = rawBody.indexOf(nextDelimiter, valueStart) + if (valueEnd < 0) return null + + const headers = rawBody.subarray(headerStart, headerEnd).toString('latin1') + const disposition = headers.split('\r\n').find((line) => /^content-disposition:/i.test(line)) + if (disposition?.match(new RegExp(`(?:^|;)\\s*name="${fieldName}"(?:;|$)`, 'i'))) { + return { start: valueStart, end: valueEnd } + } + + cursor = valueEnd + LINE_SEPARATOR.length + } + + return null +} + +function readMultipartField(rawBody: Buffer, boundary: string, fieldName: string): string | null { + const range = findMultipartField(rawBody, boundary, fieldName) + return range ? rawBody.subarray(range.start, range.end).toString('utf8') : null +} + +function rewriteMultipartField(rawBody: Buffer, boundary: string, fieldName: string, value: string): Buffer | null { + const range = findMultipartField(rawBody, boundary, fieldName) + if (!range) return null + return Buffer.concat([ + rawBody.subarray(0, range.start), + Buffer.from(value, 'utf8'), + rawBody.subarray(range.end), + ]) +} + +function adapterErrorStatus(error: unknown): number { + if (!(error instanceof AdapterError)) return 502 + return error.code === 'ADAPTER_NOT_FOUND' || error.code === 'MODEL_MAPPING_NOT_FOUND' ? 404 : 502 +} + +function responseHeaders(response: Response): Record { + const headers: Record = { + 'Content-Type': response.headers.get('content-type') ?? 'application/json', + 'Access-Control-Allow-Origin': '*', + } + for (const name of ['x-request-id', 'ah-request-id', 'openai-processing-ms', 'retry-after']) { + const value = response.headers.get(name) + if (value) headers[name] = value + } + return headers +} + +function waitForDrainOrClose(res: ServerResponse): Promise { + return new Promise((resolve) => { + const finish = (): void => { + res.off('drain', finish) + res.off('close', finish) + resolve() + } + res.once('drain', finish) + res.once('close', finish) + }) +} + +async function relayResponse(response: Response, res: ServerResponse): Promise { + res.writeHead(response.status, responseHeaders(response)) + if (!response.body) { + res.end() + return + } + + const reader = response.body.getReader() + try { + while (true) { + const { done, value } = await reader.read() + if (done) break + if (!res.write(Buffer.from(value))) { + await waitForDrainOrClose(res) + if (res.destroyed) return + } + } + res.end() + } finally { + reader.releaseLock() + } +} + +export async function handleAdapterImageRequest( + ctx: PipelineContext, + req: IncomingMessage, + res: ServerResponse +): Promise { + const match = req.url?.match(ADAPTER_IMAGE_PATH_RE) + if (!match) { + writeJson(res, 404, { error: { message: '无效的适配器图片路径' } }) + return + } + + const adapterName = match[1] + const operation = match[2] as 'generations' | 'edits' + const query = match[3] ?? '' + const logLabel = `/${adapterName}/images/${operation}` + if (!authenticateProxyRequest(ctx, req, res, logLabel)) return + + let rawBody: Buffer + try { + rawBody = await readRawBody(req) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + ctx.logger.log('request', `${logLabel} failed to read request body`, { error: message }, 'warn') + if (error instanceof ImageRequestBodyTooLargeError) { + res.setHeader('Connection', 'close') + res.once('finish', () => req.destroy()) + writeJson(res, 413, { error: { message } }) + req.resume() + } else { + writeJson(res, 400, { error: { message: '读取请求体失败' } }) + } + return + } + + let clientModel: string + let upstreamBody: string | Blob + let contentType: string + let boundary: string | null = null + + if (operation === 'generations') { + let body: Record + try { + body = JSON.parse(rawBody.toString('utf8')) as Record + } catch { + writeJson(res, 400, { error: { message: '请求体不是有效 JSON' } }) + return + } + if (typeof body.model !== 'string' || !body.model) { + writeJson(res, 400, { error: { message: '请求缺少 model 字段' } }) + return + } + clientModel = body.model + upstreamBody = JSON.stringify(body) + contentType = 'application/json' + } else { + contentType = String(req.headers['content-type'] ?? '') + boundary = multipartBoundary(contentType) + if (!/^multipart\/form-data;/i.test(contentType) || !boundary) { + writeJson(res, 400, { error: { message: '图片编辑请求必须使用 multipart/form-data' } }) + return + } + const model = readMultipartField(rawBody, boundary, 'model') + if (!model) { + writeJson(res, 400, { error: { message: '请求缺少 model 字段' } }) + return + } + clientModel = model + upstreamBody = new Blob([Uint8Array.from(rawBody)]) + } + + let route + try { + route = resolveAdapterRoute(ctx.store, adapterName, clientModel, 'openai').route + } catch (error) { + const status = adapterErrorStatus(error) + const message = error instanceof Error ? error.message : String(error) + ctx.logger.log('request', `Adapter image route failed: ${adapterName}`, { + adapter: adapterName, + model: clientModel, + operation, + error: message, + }, 'warn') + writeJson(res, status, { error: { message } }) + return + } + + if (operation === 'generations') { + const body = JSON.parse(String(upstreamBody)) as Record + body.model = route.modelId + upstreamBody = JSON.stringify(body) + } else { + const rewritten = rewriteMultipartField(rawBody, boundary!, 'model', route.modelId) + if (!rewritten) { + writeJson(res, 400, { error: { message: '无法解析 multipart model 字段' } }) + return + } + upstreamBody = new Blob([Uint8Array.from(rewritten)]) + } + + const url = `${sanitizeApiBase(route.apiBase)}/v1/images/${operation}${query}` + const startedAt = Date.now() + const abortController = new AbortController() + const abortOnDisconnect = (): void => { + if (!res.writableEnded) abortController.abort() + } + res.once('close', abortOnDisconnect) + + try { + ctx.logger.log('request', `Image upstream request: POST ${maskUrl(url)}`, { + adapter: adapterName, + clientModel, + upstreamModel: route.modelId, + provider: route.providerName, + operation, + bodySize: typeof upstreamBody === 'string' ? Buffer.byteLength(upstreamBody) : upstreamBody.size, + }, 'debug') + + const response = await fetch(url, { + method: 'POST', + headers: { + Authorization: `Bearer ${route.apiKey}`, + Accept: String(req.headers.accept ?? 'application/json'), + 'Content-Type': contentType, + }, + body: upstreamBody, + signal: abortController.signal, + }) + await relayResponse(response, res) + + const latency = Date.now() - startedAt + ctx.tracker.recordRequest(route.providerName, latency, response.ok) + ctx.logger.log('request', `${logLabel} done → ${route.providerName}`, { + adapter: adapterName, + model: clientModel, + modelId: route.modelId, + provider: route.providerName, + operation, + status: response.status, + latency, + }, response.ok ? 'info' : 'warn') + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + ctx.tracker.recordRequest(route.providerName, 0, false) + ctx.logger.log('request', `${logLabel} failed → ${route.providerName}`, { + adapter: adapterName, + model: clientModel, + provider: route.providerName, + operation, + error: message, + }, 'error') + if (!res.headersSent) writeJson(res, 502, { error: { message } }) + } finally { + res.off('close', abortOnDisconnect) + } +} diff --git a/src/api/server.ts b/src/api/server.ts index 73a27bf..3ac6732 100644 --- a/src/api/server.ts +++ b/src/api/server.ts @@ -13,6 +13,7 @@ import { timingSafeEqual } from 'node:crypto' import { AdminHandoffStore, handleCreateAdminHandoff, handleExchangeAdminHandoff, handleGetConfig, handleReload, handleHealth, handleStatus, handleGetLogs, handleGetLogLevel, handleSetLogLevel, handleGetLocale, handleSetLocale, handleGetPort, handleSetPort, handleGetAdapters, handleCreateProvider, handleUpdateProvider, handleDeleteProvider, handleCreateAdapter, handleUpdateAdapter, handleDeleteAdapter, handleTestModel, handleTestAdapter, handleListModels, handlePullModels, handleGetProxyKey, handleSetProxyKey, handleGetTokenStats, handleGetTokenTimeline, handleGetTokenBreakdown, handleGetTokenDbInfo, handlePostTokenCleanup, handleDebugCapturesStatus, handleDebugCaptures, handleDebugCapturesControl, handleDebugCapturesStream, handleGetVision, handleSetVision, handleGetVisionCacheStats, handleClearVisionCache } from './handlers/index.js' import { handleAnthropicMessages, handleOpenAIChat, handleOpenAIResponses } from '../proxy/handlers.js' import { handleAdapterRequest, handleAdapterModels } from '../adapter/handlers.js' +import { handleAdapterImageRequest } from '../adapter/image-handlers.js' export interface ServerContext { store: ConfigStore @@ -121,6 +122,7 @@ const ROUTES: Route[] = [ { method: 'POST', pattern: /^\/v1\/chat\/completions$/, handler: handleOpenAIChat }, { method: 'POST', pattern: /^\/v1\/responses$/, handler: handleOpenAIResponses }, { method: 'POST', pattern: /^\/([a-zA-Z0-9_-]+)\/v1\/(messages|chat\/completions|responses)(\?.*)?$/, handler: handleAdapterRequest }, + { method: 'POST', pattern: /^\/([a-zA-Z0-9_-]+)\/v1\/images\/(generations|edits)(\?.*)?$/, handler: handleAdapterImageRequest }, ] function corsHeaders(res: ServerResponse): void { diff --git a/test/adapter/image-handlers.test.ts b/test/adapter/image-handlers.test.ts new file mode 100644 index 0000000..16d87ea --- /dev/null +++ b/test/adapter/image-handlers.test.ts @@ -0,0 +1,264 @@ +import { after, before, describe, it } from 'node:test' +import assert from 'node:assert' +import { createServer as createHttpServer, request as createHttpRequest, type IncomingMessage, type Server } from 'node:http' +import { createProxyServer } from '../../src/api/server.js' +import { ConfigStore } from '../../src/config/store.js' +import type { Config } from '../../src/config/types.js' +import { Logger } from '../../src/log/logger.js' +import { StatusTracker } from '../../src/status/tracker.js' + +const UPSTREAM_PORT = 19820 +const PROXY_PORT = 19821 + +interface CapturedRequest { + url: string + authorization?: string + contentType?: string + json?: Record + form?: { + model: string | null + prompt: string | null + imageName: string | null + imageType: string | null + imageBytes: number[] + } +} + +const captured: CapturedRequest[] = [] +let upstream: Server +let proxy: Server + +async function readRawBody(req: IncomingMessage): Promise { + const chunks: Buffer[] = [] + for await (const chunk of req) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + } + return Buffer.concat(chunks) +} + +function createConfig(): Config { + return { + proxyKey: 'proxy-secret', + providers: [{ + name: 'images-upstream', + apiKey: 'upstream-secret', + protocols: [ + { type: 'openai-responses', apiBase: 'http://127.0.0.1:1' }, + { type: 'openai', apiBase: `http://127.0.0.1:${UPSTREAM_PORT}` }, + ], + models: [{ id: 'upstream-image-model', protocols: ['openai-responses', 'openai'] }], + }], + adapters: [{ + name: 'amztracker', + models: [{ + sourceModelId: 'client-image-model', + provider: 'images-upstream', + targetModelId: 'upstream-image-model', + }], + }], + } +} + +describe('adapter image handlers', { timeout: 15_000 }, () => { + before(async () => { + upstream = createHttpServer(async (req, res) => { + const raw = await readRawBody(req) + const item: CapturedRequest = { + url: req.url ?? '/', + authorization: req.headers.authorization, + contentType: req.headers['content-type'], + } + + if ((req.url ?? '').startsWith('/v1/images/edits')) { + const request = new Request('http://upstream.test', { + method: 'POST', + headers: { 'content-type': req.headers['content-type'] ?? '' }, + body: raw, + }) + const form = await request.formData() + const image = form.get('image') + item.form = { + model: form.get('model')?.toString() ?? null, + prompt: form.get('prompt')?.toString() ?? null, + imageName: image instanceof File ? image.name : null, + imageType: image instanceof File ? image.type : null, + imageBytes: image instanceof File ? [...new Uint8Array(await image.arrayBuffer())] : [], + } + } else { + item.json = JSON.parse(raw.toString('utf8') || '{}') as Record + } + captured.push(item) + + if (item.json?.prompt === 'rate-limit') { + res.writeHead(429, { 'Content-Type': 'application/json', 'x-request-id': 'upstream-429' }) + res.end(JSON.stringify({ error: { message: 'slow down' } })) + return + } + + res.writeHead(200, { 'Content-Type': 'application/json', 'x-request-id': 'upstream-ok' }) + res.end(JSON.stringify({ created: 123, data: [{ b64_json: 'aW1hZ2U=' }] })) + }) + await new Promise((resolve) => upstream.listen(UPSTREAM_PORT, '127.0.0.1', resolve)) + + proxy = createProxyServer({ + adminHost: '127.0.0.1', + adminPort: PROXY_PORT, + proxyHost: '127.0.0.1', + proxyPort: PROXY_PORT, + store: new ConfigStore('/fake', createConfig()), + tracker: new StatusTracker(), + logger: new Logger(), + }) + await new Promise((resolve) => proxy.listen(PROXY_PORT, '127.0.0.1', resolve)) + }) + + after(async () => { + await Promise.all([ + new Promise((resolve) => proxy.close(() => resolve())), + new Promise((resolve) => upstream.close(() => resolve())), + ]) + }) + + it('generation 选择 OpenAI 协议、重写模型并保留查询参数', async () => { + captured.length = 0 + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/generations?trace=1`, { + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'application/json', + }, + body: JSON.stringify({ model: 'client-image-model', prompt: 'draw a box', size: '1024x1024' }), + }) + + assert.strictEqual(response.status, 200) + assert.strictEqual(response.headers.get('x-request-id'), 'upstream-ok') + assert.deepStrictEqual(await response.json(), { created: 123, data: [{ b64_json: 'aW1hZ2U=' }] }) + assert.strictEqual(captured.length, 1) + assert.strictEqual(captured[0].url, '/v1/images/generations?trace=1') + assert.strictEqual(captured[0].authorization, 'Bearer upstream-secret') + assert.deepStrictEqual(captured[0].json, { + model: 'upstream-image-model', + prompt: 'draw a box', + size: '1024x1024', + }) + }) + + it('edit 保留 multipart 文件并只重写 model 字段', async () => { + captured.length = 0 + const imageBytes = [137, 80, 78, 71, ...Buffer.from('name="model"\r\n\r\nnot-the-form-field')] + const form = new FormData() + form.set('image', new File([Uint8Array.from(imageBytes)], 'input.png', { type: 'image/png' })) + form.set('model', 'client-image-model') + form.set('prompt', 'make it blue') + + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/edits`, { + method: 'POST', + headers: { authorization: 'Bearer proxy-secret' }, + body: form, + }) + + assert.strictEqual(response.status, 200) + assert.strictEqual(captured.length, 1) + assert.strictEqual(captured[0].url, '/v1/images/edits') + assert.strictEqual(captured[0].authorization, 'Bearer upstream-secret') + assert.ok(captured[0].contentType?.startsWith('multipart/form-data; boundary=')) + assert.deepStrictEqual(captured[0].form, { + model: 'upstream-image-model', + prompt: 'make it blue', + imageName: 'input.png', + imageType: 'image/png', + imageBytes, + }) + }) + + it('缺少代理密钥时在访问上游前返回 401', async () => { + captured.length = 0 + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/generations`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ model: 'client-image-model', prompt: 'draw' }), + }) + assert.strictEqual(response.status, 401) + assert.strictEqual(captured.length, 0) + }) + + it('generation 非法 JSON 返回 400', async () => { + captured.length = 0 + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/generations`, { + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'application/json', + }, + body: 'not-json', + }) + assert.strictEqual(response.status, 400) + assert.strictEqual(captured.length, 0) + }) + + it('edit 非 multipart 请求返回 400', async () => { + captured.length = 0 + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/edits`, { + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'application/json', + }, + body: JSON.stringify({ model: 'client-image-model' }), + }) + assert.strictEqual(response.status, 400) + assert.strictEqual(captured.length, 0) + }) + + it('声明超过 20MiB 的图片请求在读取 body 前返回 413', async () => { + captured.length = 0 + const status = await new Promise((resolve, reject) => { + const req = createHttpRequest({ + hostname: '127.0.0.1', + port: PROXY_PORT, + path: '/amztracker/v1/images/edits', + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'multipart/form-data; boundary=test-boundary', + 'content-length': String(20 * 1024 * 1024 + 1), + }, + }, (response) => { + response.resume() + response.once('end', () => resolve(response.statusCode ?? 0)) + }) + req.once('error', reject) + req.end() + }) + assert.strictEqual(status, 413) + assert.strictEqual(captured.length, 0) + }) + + it('未知 adapter 模型返回 404', async () => { + captured.length = 0 + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/generations`, { + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'application/json', + }, + body: JSON.stringify({ model: 'missing-model', prompt: 'draw' }), + }) + assert.strictEqual(response.status, 404) + assert.strictEqual(captured.length, 0) + }) + + it('完整透传上游 4xx 状态、响应体和请求 ID', async () => { + const response = await fetch(`http://127.0.0.1:${PROXY_PORT}/amztracker/v1/images/generations`, { + method: 'POST', + headers: { + authorization: 'Bearer proxy-secret', + 'content-type': 'application/json', + }, + body: JSON.stringify({ model: 'client-image-model', prompt: 'rate-limit' }), + }) + assert.strictEqual(response.status, 429) + assert.strictEqual(response.headers.get('x-request-id'), 'upstream-429') + assert.deepStrictEqual(await response.json(), { error: { message: 'slow down' } }) + }) +})