feat(studio): drop 3D Poster Lab and ship Multi-Tenant Multi-Expert Brain OS, Live Playground & n8n Playbooks

This commit is contained in:
Amirul Islam Redwan71
2026-11-26 13:42:25 +06:00
parent 7c5bcd9ac7
commit 6a46c419f2
8 changed files with 1274 additions and 8031 deletions
+61 -7
View File
@@ -43,7 +43,7 @@ function readJson(request, maxBytes) {
});
}
function createApiHandler({ resolveWorkspace, store, scrape, imageHandler, getConnected, publicOrigin, maxBodyBytes = 32768, maxConcurrentImports = 1, maxImportsPerMinute = 5 }) {
function createApiHandler({ resolveWorkspace, store, scrape, imageHandler, getConnected, engineProxy, publicOrigin, maxBodyBytes = 32768, maxConcurrentImports = 1, maxImportsPerMinute = 5 }) {
const configured = new URL(publicOrigin);
if (!['http:', 'https:'].includes(configured.protocol) || configured.pathname !== '/' || configured.search || configured.hash || configured.username || configured.password) {
throw new Error('A public Studio origin without path or credentials is required');
@@ -79,14 +79,31 @@ function createApiHandler({ resolveWorkspace, store, scrape, imageHandler, getCo
try {
const pathname = new URL(request.url, origin).pathname;
const routes = {
'/api/workspace': 'GET', '/api/brand': 'POST', '/api/catalog-image': 'GET',
'/api/get-synced-stores': 'GET', '/api/sync-store': 'POST', '/api/delete-synced-store': 'POST'
'/api/workspace': ['GET'],
'/api/brand': ['POST'],
'/api/catalog-image': ['GET'],
'/api/get-synced-stores': ['GET'],
'/api/sync-store': ['POST'],
'/api/delete-synced-store': ['POST'],
'/api/experts': ['GET', 'POST'],
'/api/experts/simulate': ['POST'],
'/api/experts/generate-content': ['POST'],
'/api/playbooks': ['GET', 'POST'],
'/api/playbooks/test': ['POST']
};
if (!routes[pathname]) return sendJson(response, 404, { success: false, code: 'NOT_FOUND', error: 'API পাওয়া যায়নি।' });
if (request.method !== routes[pathname]) return sendJson(response, 405, { success: false, code: 'METHOD_NOT_ALLOWED', error: 'এই method সমর্থিত নয়।' });
if (request.method === 'POST') writeOrigin(request);
const expertDeleteMatch = !routes[pathname] ? /^\/api\/experts\/([a-zA-Z0-9_-]{1,80})$/.exec(pathname) : null;
const allowedMethods = routes[pathname] || (expertDeleteMatch ? ['DELETE'] : null);
if (!allowedMethods) return sendJson(response, 404, { success: false, code: 'NOT_FOUND', error: 'API পাওয়া যায়নি।' });
if (!allowedMethods.includes(request.method)) return sendJson(response, 405, { success: false, code: 'METHOD_NOT_ALLOWED', error: 'এই method সমর্থিত নয়।' });
if (request.method === 'POST' || request.method === 'DELETE') writeOrigin(request);
const workspace = await resolveWorkspace(request);
const publicWorkspace = { clientId: workspace.clientId, name: workspace.displayName, tenant: workspace.tenant, canWriteProducts: workspace.canWriteProducts };
const publicWorkspace = {
clientId: workspace.clientId,
name: workspace.displayName,
tenant: workspace.tenant,
canWriteProducts: workspace.canWriteProducts,
multiExpertEnabled: Boolean(engineProxy && engineProxy.isConfigured())
};
if (pathname === '/api/workspace') {
publicWorkspace.brand = normalizeBrand(await store.readBrand(workspace) || {}, workspace.displayName);
return sendJson(response, 200, { success: true, workspace: publicWorkspace });
@@ -95,6 +112,43 @@ function createApiHandler({ resolveWorkspace, store, scrape, imageHandler, getCo
if (!imageHandler) throw fail(503, 'IMAGE_UNAVAILABLE', 'Product image এখন পাওয়া যাচ্ছে না।');
return await imageHandler(request, response, workspace);
}
// ── Multi-Tenant Multi-Expert Brain & n8n Playbooks Proxy Routes ───────
if (pathname === '/api/experts' || expertDeleteMatch || pathname.startsWith('/api/experts/') || pathname === '/api/playbooks' || pathname.startsWith('/api/playbooks/')) {
if (!engineProxy || !engineProxy.isConfigured()) {
throw fail(503, 'ENGINE_NOT_CONFIGURED', 'Multi-Expert AI Engine সংযুক্ত নয়।');
}
if (pathname === '/api/experts' && request.method === 'GET') {
return sendJson(response, 200, await engineProxy.listExperts(workspace));
}
if (pathname === '/api/playbooks' && request.method === 'GET') {
return sendJson(response, 200, await engineProxy.listPlaybooks(workspace));
}
if (expertDeleteMatch && request.method === 'DELETE') {
requireProductWrite(workspace);
return sendJson(response, 200, await engineProxy.deleteExpert(workspace, expertDeleteMatch[1]));
}
const body = await readJson(request, maxBodyBytes);
if (pathname === '/api/experts' && request.method === 'POST') {
requireProductWrite(workspace);
return sendJson(response, 200, await engineProxy.saveExpert(workspace, body));
}
if (pathname === '/api/experts/simulate') {
return sendJson(response, 200, await engineProxy.simulate(workspace, body));
}
if (pathname === '/api/experts/generate-content') {
return sendJson(response, 200, await engineProxy.generateContent(workspace, body));
}
if (pathname === '/api/playbooks' && request.method === 'POST') {
requireProductWrite(workspace);
return sendJson(response, 200, await engineProxy.savePlaybook(workspace, body));
}
if (pathname === '/api/playbooks/test') {
if (!body.dry_run) requireProductWrite(workspace);
return sendJson(response, 200, await engineProxy.testPlaybook(workspace, body));
}
}
if (request.method === 'GET') {
const records = await store.read(workspace);
if (getConnected) records.unshift(await getConnected(workspace));