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
50 changes: 27 additions & 23 deletions apps/daemon/src/routes/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,19 @@ export function registerMediaRoutes(app: Express, ctx: RegisterMediaRoutesDeps)
let task: ReturnType<typeof createMediaTask> | null = null;
try {
const taskId = randomUUID();
const body =
req.body && typeof req.body === 'object' && !Array.isArray(req.body)
? req.body
: {};
task = createMediaTask(taskId, projectId, {
surface: req.body?.surface,
model: req.body?.model,
surface: body?.surface,
model: body?.model,
});
console.error(
`[task ${taskId.slice(0, 8)}] queued model=${req.body?.model} ` +
`surface=${req.body?.surface} ` +
`image=${req.body?.image ? 'yes' : 'no'} ` +
`compositionDir=${req.body?.compositionDir ? 'yes' : 'no'}`,
`[task ${taskId.slice(0, 8)}] queued model=${body?.model} ` +
`surface=${body?.surface} ` +
`image=${body?.image ? 'yes' : 'no'} ` +
`compositionDir=${body?.compositionDir ? 'yes' : 'no'}`,
);

const proxyDispatcher = proxyDispatcherRequestInit(process.env, {
Expand All @@ -146,27 +150,27 @@ export function registerMediaRoutes(app: Express, ctx: RegisterMediaRoutesDeps)
projectRoot: PROJECT_ROOT,
projectsRoot: PROJECTS_DIR,
projectId,
surface: req.body?.surface,
model: req.body?.model,
prompt: req.body?.prompt,
output: req.body?.output,
aspect: req.body?.aspect,
surface: body?.surface,
model: body?.model,
prompt: body?.prompt,
output: body?.output,
aspect: body?.aspect,
length:
typeof req.body?.length === 'number' ? req.body.length : undefined,
typeof body?.length === 'number' ? body.length : undefined,
duration:
typeof req.body?.duration === 'number'
? req.body.duration
typeof body?.duration === 'number'
? body.duration
: undefined,
voice: req.body?.voice,
audioKind: req.body?.audioKind,
language: typeof req.body?.language === 'string' ? req.body.language : undefined,
loop: typeof req.body?.loop === 'boolean' ? req.body.loop : undefined,
promptInfluence: typeof req.body?.promptInfluence === 'number'
? req.body.promptInfluence
voice: body?.voice,
audioKind: body?.audioKind,
language: typeof body?.language === 'string' ? body.language : undefined,
loop: typeof body?.loop === 'boolean' ? body.loop : undefined,
promptInfluence: typeof body?.promptInfluence === 'number'
? body.promptInfluence
: undefined,
compositionDir: req.body?.compositionDir,
image: req.body?.image,
images: Array.isArray(req.body?.images) ? req.body.images : undefined,
compositionDir: body?.compositionDir,
image: body?.image,
images: Array.isArray(body?.images) ? body.images : undefined,
onProgress: (line: any) => appendTaskProgress(task, line),
requestInit: proxyDispatcher.requestInit,
})
Expand Down
11 changes: 10 additions & 1 deletion apps/daemon/src/routes/vela.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ function velaApiProxyBaseUrl(req: Request, getPublicBaseUrl: PublicBaseUrlResolv
return `${getPublicBaseUrl(req)}${AMR_API_PROXY_PREFIX}`;
}

function velaProxyRequestBody(req: Request): Buffer | null {
function isPlainObject(value: unknown): value is Record<string, unknown> {
return Object.prototype.toString.call(value) === '[object Object]';
}

function velaProxyRequestBody(req: Request): Buffer | null | undefined {
if (req.method === 'GET' || req.method === 'HEAD') return null;
if (Buffer.isBuffer(req.body)) return req.body;
if (typeof req.body === 'string') return Buffer.from(req.body);
if (req.body == null) return null;
if (Array.isArray(req.body) || !isPlainObject(req.body)) return undefined;
return Buffer.from(JSON.stringify(req.body));
}

Expand All @@ -79,6 +84,10 @@ function proxyAmrApiRequest(req: Request, res: Response): void {
}
const target = new URL(suffix, AMR_API_UPSTREAM_ORIGIN);
const body = velaProxyRequestBody(req);
if (body === undefined) {
res.status(400).json({ error: 'invalid_request_body_type' });
return;
}
const streamBody = shouldStreamVelaProxyRequest(req, body);
const headers: Record<string, string | string[]> = {};
for (const [key, value] of Object.entries(req.headers)) {
Expand Down
Loading