UNPKG

dadacat-lambda-pipeline

Version:
3 lines (2 loc) 21.2 kB
"use strict";var e=require("cross-fetch");class t{constructor(e){this.functionUrl=e,this.timeout=3e4}async generateResponse(t){if(!this.functionUrl)throw new Error("dadacat-agent-x86 function URL not configured");console.log("[DadacatClient] Input prompt type: "+typeof t),console.log(`[DadacatClient] Input prompt value: "${t}"`),console.log(`[DadacatClient] Input prompt length: ${t?t.length:"null/undefined"}`);const o={message:t},a=JSON.stringify(o);console.log("[DadacatClient] Payload object:",o),console.log(`[DadacatClient] JSON stringified payload: ${a}`),console.log(`[DadacatClient] JSON payload length: ${a.length} bytes`);try{const t=new AbortController,o=setTimeout(()=>t.abort(),this.timeout);console.log(`[DadacatClient] Sending POST request to: ${this.functionUrl}`),console.log("[DadacatClient] Request headers:",{"Content-Type":"application/json",Accept:"application/json"});const n=await e(this.functionUrl,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:a,signal:t.signal});if(clearTimeout(o),console.log(`[DadacatClient] Response status: ${n.status}`),console.log("[DadacatClient] Response headers:",Object.fromEntries(n.headers.entries())),!n.ok){const e=await n.text();throw console.error(`[DadacatClient] Error response body: ${e}`),new Error(`HTTP error! status: ${n.status}, body: ${e}`)}const s=await n.text();let r;console.log(`[DadacatClient] Raw response text: ${s}`);try{r=JSON.parse(s)}catch(e){throw console.error("[DadacatClient] Failed to parse response as JSON:",e),new Error(`Invalid JSON response: ${s}`)}return console.log("[DadacatClient] Parsed response:",r),console.log(`[DadacatClient] Response status: ${r.status}`),r}catch(e){return"AbortError"===e.name?(console.error("dadacat-agent-x86 request timed out"),{status:"error",error:"Request timed out"}):(console.error(`dadacat-agent-x86 request failed: ${e.message}`),{status:"error",error:e.message})}}async testConnection(){try{return"success"===(await this.generateResponse("test")).status}catch(e){return console.error(`Connection test failed: ${e.message}`),!1}}}class o{constructor(e){this.baseUrl=e,this.timeout=3e4}async generateImage(t){if(!this.baseUrl)throw new Error("ImageGenerationProcessor base URL not configured");const o=`${this.baseUrl}/generate`;console.log("Calling ImageGenerationProcessor generate endpoint"),console.debug("Request data:",t);try{const a=new AbortController,n=setTimeout(()=>a.abort(),this.timeout),s=await e(o,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t),signal:a.signal});if(clearTimeout(n),!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const r=await s.json(),i=r.data?.job_id;return console.log(`Image generation queued: job_id=${i}`),r}catch(e){return"AbortError"===e.name?(console.error("ImageGenerationProcessor request timed out"),{success:!1,error:"Request timed out"}):(console.error(`ImageGenerationProcessor request failed: ${e.message}`),{success:!1,error:e.message})}}async checkStatus(t){if(!this.baseUrl)throw new Error("ImageGenerationProcessor base URL not configured");const o=`${this.baseUrl}/status/${t}`;console.debug(`Checking status for job_id: ${t}`);try{const a=new AbortController,n=setTimeout(()=>a.abort(),this.timeout),s=await e(o,{method:"GET",headers:{Accept:"application/json"},signal:a.signal});if(clearTimeout(n),!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const r=await s.json(),i=r.data?.status||"unknown";return console.debug(`Job ${t} status: ${i}`),r}catch(e){return"AbortError"===e.name?(console.error(`Status check timed out for job ${t}`),{success:!1,error:"Status check timed out"}):(console.error(`Status check failed for job ${t}: ${e.message}`),{success:!1,error:e.message})}}async pollForCompletion(e,t=300,o=5){console.log(`Polling for completion of job ${e} (max wait: ${t}s)`);const a=Date.now(),n=1e3*t,s=1e3*o;for(;Date.now()-a<n;){const t=await this.checkStatus(e);if(!t.success)return console.error(`Status check failed: ${JSON.stringify(t)}`),t;const a=t.data?.status||"unknown";if("completed"===a)return console.log(`Job ${e} completed successfully`),t;if("failed"===a)return console.error(`Job ${e} failed`),t;["queued","processing"].includes(a)?(console.log(`Job ${e} still ${a}, waiting ${o}s...`),await this._sleep(s)):(console.warn(`Job ${e} has unknown status: ${a}`),await this._sleep(s))}return console.error(`Job ${e} polling timed out after ${t}s`),{success:!1,error:`Polling timed out after ${t}s`}}async testConnection(){try{const e={prompt:"test image for client validation",options:{model:"dall-e-3",size:"1024x1024"}};return(await this.generateImage(e)).success||!1}catch(e){return console.error(`Connection test failed: ${e.message}`),!1}}_sleep(e){return new Promise(t=>setTimeout(t,e))}}class a{constructor(e){this.baseUrl=e,this.timeout=6e4}async uploadImage(t){if(!this.baseUrl)throw new Error("ImageB2Uploader API Gateway base URL not configured");const o=`${this.baseUrl}/upload-b2`;console.log("Calling ImageB2Uploader via API Gateway"),console.debug("Request data:",t);const a=["job_id","run_id","image_url"];for(const e of a)if(!(e in t))throw new Error(`Missing required field: ${e}`);try{const a=new AbortController,n=setTimeout(()=>a.abort(),this.timeout),s=await e(o,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(t),signal:a.signal});if(clearTimeout(n),!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const r=await s.json();if(r.success){const e=r.data?.b2_url;console.log(`Image uploaded successfully: ${e}`)}else console.error(`Upload failed: ${JSON.stringify(r)}`);return r}catch(e){return"AbortError"===e.name?(console.error("ImageB2Uploader request timed out"),{success:!1,error:"Request timed out"}):(console.error(`ImageB2Uploader request failed: ${e.message}`),{success:!1,error:e.message})}}async testConnection(){try{return await this.uploadImage({test:"connection"}),!1}catch(e){const t=(e.message||"").includes("Missing required field");return t?console.log("✅ ImageB2Uploader connection test passed (validation error expected)"):console.error(`Connection test failed: ${e.message}`),t}}}class n{static OPTIONS={model:{name:"Model",description:"The model to use for image generation",options:{"gpt-image-1":"GPT Image 1","dall-e-3":"DALL-E 3","dall-e-2":"DALL-E 2"},default:"dall-e-3"},size:{name:"Image Size",description:"The size of the generated images",options:{"dall-e-2":[{value:"256x256",display:"Small (256x256)"},{value:"512x512",display:"Medium (512x512)"},{value:"1024x1024",display:"Large (1024x1024)"}],"dall-e-3":[{value:"1024x1024",display:"Square (1024x1024)"},{value:"1792x1024",display:"Landscape (1792x1024)"},{value:"1024x1792",display:"Portrait (1024x1792)"}],"gpt-image-1":[{value:"1024x1024",display:"Square (1024x1024)"},{value:"1536x1024",display:"Landscape (1536x1024)"},{value:"1024x1536",display:"Portrait (1024x1536)"}]},default:{"dall-e-2":"1024x1024","dall-e-3":"1024x1024","gpt-image-1":"1024x1024"}},n:{name:"Number of Images",description:"The number of images to generate",options:{"dall-e-2":Array.from({length:10},(e,t)=>t+1),"dall-e-3":[1],"gpt-image-1":Array.from({length:10},(e,t)=>t+1)},default:1},quality:{name:"Image Quality",description:"The quality of the image that will be generated",options:{"dall-e-2":{standard:"Standard"},"dall-e-3":{standard:"Standard",hd:"HD"},"gpt-image-1":{auto:"Auto",high:"High",medium:"Medium",low:"Low"}},default:{"dall-e-2":"standard","dall-e-3":"standard","gpt-image-1":"auto"}},style:{name:"Image Style",description:"The style of the generated images (DALL-E 3 only)",options:{"dall-e-2":null,"dall-e-3":{vivid:"Vivid (hyper-real and dramatic)",natural:"Natural (more natural looking)"},"gpt-image-1":null},default:{"dall-e-3":"vivid"}},background:{name:"Background",description:"Sets transparency for the background (GPT Image 1 only)",options:{"dall-e-2":null,"dall-e-3":null,"gpt-image-1":{auto:"Auto (model decides)",transparent:"Transparent",opaque:"Opaque"}},default:{"gpt-image-1":"auto"}},moderation:{name:"Content Moderation",description:"Content moderation level for images (GPT Image 1 only)",options:{"dall-e-2":null,"dall-e-3":null,"gpt-image-1":{auto:"Auto (standard filtering)",low:"Low (less restrictive filtering)"}},default:{"gpt-image-1":"auto"}},output_format:{name:"Output Format",description:"Format of generated images (GPT Image 1 only)",options:{"dall-e-2":null,"dall-e-3":null,"gpt-image-1":{png:"PNG",jpeg:"JPEG",webp:"WebP"}},default:{"gpt-image-1":"png"}},output_compression:{name:"Output Compression",description:"Compression level for WebP/JPEG (GPT Image 1 only)",options:{"dall-e-2":null,"dall-e-3":null,"gpt-image-1":{values:Array.from({length:100},(e,t)=>t+1),applicable_formats:["webp","jpeg"]}},default:{"gpt-image-1":100}},response_format:{name:"Response Format",description:"Format in which images are returned (DALL-E models only)",options:{"dall-e-2":{url:"URL",b64_json:"Base64 JSON"},"dall-e-3":{url:"URL",b64_json:"Base64 JSON"},"gpt-image-1":null},default:{"dall-e-2":"url","dall-e-3":"url"}}};static validateOptions(e={},t="dall-e-3"){const o={validatedOptions:{},errors:[],warnings:[]};if(!this.OPTIONS.model.options[t])return o.errors.push(`Invalid model: ${t}. Valid models: ${Object.keys(this.OPTIONS.model.options).join(", ")}`),o;o.validatedOptions=this.getDefaultOptions(t);for(const[a,n]of Object.entries(e)){const e=this.OPTIONS[a];if(!e){o.warnings.push(`Unknown option: ${a}`);continue}const s=e.options[t];if(null===s){o.warnings.push(`Option '${a}' is not supported by model '${t}'`);continue}const r=this._validateOptionValue(a,n,s,t);r?o.errors.push(r):o.validatedOptions[a]=n}return o}static getDefaultOptions(e="dall-e-3"){const t={};for(const[o,a]of Object.entries(this.OPTIONS))"model"!==o&&null!==a.options[e]&&("object"==typeof a.default&&void 0!==a.default[e]?t[o]=a.default[e]:"object"!=typeof a.default&&(t[o]=a.default));return t.model=e,t}static getSupportedOptions(e="dall-e-3"){const t={};for(const[o,a]of Object.entries(this.OPTIONS))null!==a.options[e]&&(t[o]={name:a.name,description:a.description,options:a.options[e],default:"object"==typeof a.default?a.default[e]:a.default});return t}static _validateOptionValue(e,t,o,a){switch(e){case"size":const n=o.map(e=>e.value);if(!n.includes(t))return`Invalid size '${t}' for model '${a}'. Valid sizes: ${n.join(", ")}`;break;case"n":if(!o.includes(t))return`Invalid number of images '${t}' for model '${a}'. Valid values: ${o.join(", ")}`;break;case"quality":case"style":case"background":case"moderation":case"output_format":case"response_format":if("object"==typeof o&&!o[t])return`Invalid ${e} '${t}' for model '${a}'. Valid values: ${Object.keys(o).join(", ")}`;break;case"output_compression":if(o.values&&!o.values.includes(t))return`Invalid output_compression '${t}' for model '${a}'. Valid range: 1-100`}return null}static cleanOptionsForModel(e={},t="dall-e-3"){const o={};for(const[a,n]of Object.entries(e)){const e=this.OPTIONS[a];e&&null!==e.options[t]&&(o[a]=n)}return o}}class s{constructor(e){this.config={maxRetries:3,retryDelay:5,timeout:300,pollingInterval:5,...e},this.dadacatClient=new t(this.config.dadacatUrl),this.imageGenClient=new o(this.config.imageGenUrl),this.b2UploadClient=new a(this.config.b2UploadUrl)}async runSequentialPipeline(e){const t=`sequential_${Date.now()}`,o=Date.now();console.log(`[PipelineOrchestrator] Starting sequential pipeline ${t}`),console.log("[PipelineOrchestrator] Input prompt type: "+typeof e),console.log(`[PipelineOrchestrator] Input prompt value: "${e}"`),console.log(`[PipelineOrchestrator] Input prompt length: ${e?e.length:"null/undefined"}`);const a=[];let n=null;try{const n=Date.now();console.log("[PipelineOrchestrator] Step 1: Calling dadacat-agent-x86"),console.log(`[PipelineOrchestrator] Passing prompt to dadacat: "${e}"`);const s=await this.dadacatClient.generateResponse(e),r=(Date.now()-n)/1e3;if(a.push({step:1,lambda:"dadacat-agent-x86",input:{prompt:e},output:s,duration:r,success:"success"===s.status}),"success"!==s.status)throw new Error(`Step 1 failed: ${JSON.stringify(s)}`);const i=s.response||"";console.log(`Step 1 completed in ${r.toFixed(2)}s: ${i.substring(0,100)}...`);const l=Date.now();console.log("Step 2: Calling ImageGenerationProcessor");const c={prompt:i,options:{model:"dall-e-3",size:"1024x1024",quality:"standard"}},d=await this.imageGenClient.generateImage(c);if(!d.success)throw new Error(`Step 2 failed to queue: ${JSON.stringify(d)}`);const u=d.data?.job_id,p=d.data?.run_id;console.log(`Step 2 queued: job_id=${u}, run_id=${p}`),console.log("Polling for image generation completion...");const g=await this.imageGenClient.pollForCompletion(u,this.config.timeout,this.config.pollingInterval),m=(Date.now()-l)/1e3;if(a.push({step:2,lambda:"ImageGenerationProcessor",input:c,output:g,duration:m,success:g.success&&"completed"===g.data?.status}),!g.success||"completed"!==g.data?.status)throw new Error(`Step 2 failed: ${JSON.stringify(g)}`);const f=g.data?.url;console.log(`Step 2 completed in ${m.toFixed(2)}s: ${f}`);const h=Date.now();console.log("Step 3: Calling ImageB2Uploader");const w={job_id:u,run_id:p,image_url:f,metadata:{original_prompt:e,agent_response:i,generation_timestamp:(new Date).toISOString()}},b=await this.b2UploadClient.uploadImage(w),y=(Date.now()-h)/1e3;if(a.push({step:3,lambda:"ImageB2Uploader",input:w,output:b,duration:y,success:b.success}),!b.success)throw new Error(`Step 3 failed: ${JSON.stringify(b)}`);const $=b.data?.b2_url;console.log(`Step 3 completed in ${y.toFixed(2)}s: ${$}`);const S=(Date.now()-o)/1e3;return{success:!0,testId:t,timestamp:new Date(o).toISOString(),inputData:{prompt:e},steps:a,finalResult:{agentResponse:i,imageUrl:f,b2Url:$,jobId:u,runId:p},totalDuration:S}}catch(s){n=s.message,console.error(`Pipeline failed: ${n}`);const r=(Date.now()-o)/1e3;return{success:!1,testId:t,timestamp:new Date(o).toISOString(),inputData:{prompt:e},steps:a,error:n,totalDuration:r}}}async runMetadataPipeline(e){const t=`metadata_${Date.now()}`,o=Date.now();console.log(`Starting metadata pipeline ${t}`),console.log("Input data:",JSON.stringify(e,null,2));const a=[];let n=null;try{const{original_prompt:n,batch_id:s,user_context:r}=e,i=Date.now();console.log("Step 1: Calling dadacat-agent-x86 with metadata");const l=await this.dadacatClient.generateResponse(n),c=(Date.now()-i)/1e3;if("success"!==l.status)throw new Error(`Step 1 failed: ${JSON.stringify(l)}`);const d=l.response||"",u=Date.now();console.log("Step 2: Calling ImageGenerationProcessor with metadata");const p={prompt:d,batch_id:s,options:{model:"dall-e-3",size:"1024x1024",quality:"standard"}},g=await this.imageGenClient.generateImage(p);if(!g.success)throw new Error(`Step 2 failed to queue: ${JSON.stringify(g)}`);const m=g.data?.job_id,f=g.data?.run_id,h=await this.imageGenClient.pollForCompletion(m,this.config.timeout,this.config.pollingInterval),w=(Date.now()-u)/1e3;if(!h.success||"completed"!==h.data?.status)throw new Error(`Step 2 failed: ${JSON.stringify(h)}`);const b=h.data?.url,y=Date.now();console.log("Step 3: Calling ImageB2Uploader with metadata");const $={job_id:m,run_id:f,image_url:b,metadata:{original_prompt:n,agent_response:d,batch_id:s,user_context:r,generation_timestamp:(new Date).toISOString()}},S=await this.b2UploadClient.uploadImage($),I=(Date.now()-y)/1e3;if(!S.success)throw new Error(`Step 3 failed: ${JSON.stringify(S)}`);const O=S.data?.b2_url,_=(Date.now()-o)/1e3;return a.push({step:1,lambda:"dadacat-agent-x86",input:{prompt:n},output:l,duration:c,success:!0},{step:2,lambda:"ImageGenerationProcessor",input:p,output:h,duration:w,success:!0},{step:3,lambda:"ImageB2Uploader",input:$,output:S,duration:I,success:!0}),{success:!0,testId:t,timestamp:new Date(o).toISOString(),inputData:e,steps:a,finalResult:{agentResponse:d,imageUrl:b,b2Url:O,jobId:m,runId:f,metadata:{batch_id:s,user_context:r,original_prompt:n}},totalDuration:_}}catch(s){n=s.message,console.error(`Metadata pipeline failed: ${n}`);const r=(Date.now()-o)/1e3;return{success:!1,testId:t,timestamp:new Date(o).toISOString(),inputData:e,steps:a,error:n,totalDuration:r}}}async runConfigurablePipeline(e){const t=`configurable_${Date.now()}`,o=Date.now();console.log(`Starting configurable pipeline ${t}`),console.log("Input data:",JSON.stringify(e,null,2));const a=[];let s=null;try{const{human_prompt:s,batch_id:r,additional_prompt:i,options:l={},bucket:c,folder:d,artproject:u,on_website:p="yes",custom_metadata:g={}}=e;if(!s)throw new Error("human_prompt is required");const m=l.model||"dall-e-3",f=n.validateOptions(l,m);if(f.errors.length>0)throw new Error(`Invalid options: ${f.errors.join(", ")}`);f.warnings.length>0&&console.warn("Option warnings:",f.warnings.join(", "));const h=f.validatedOptions,w=Date.now();console.log("[PipelineOrchestrator] Step 1: Calling dadacat-agent-x86"),console.log("[PipelineOrchestrator] human_prompt type: "+typeof s),console.log(`[PipelineOrchestrator] human_prompt value: "${s}"`),console.log(`[PipelineOrchestrator] human_prompt length: ${s?s.length:"null/undefined"}`);const b=await this.dadacatClient.generateResponse(s),y=(Date.now()-w)/1e3;if(a.push({step:1,lambda:"dadacat-agent-x86",input:{prompt:s},output:b,duration:y,success:"success"===b.status}),"success"!==b.status)throw new Error(`Step 1 failed: ${JSON.stringify(b)}`);const $=b.response||"";console.log(`Step 1 completed in ${y.toFixed(2)}s`);const S=Date.now();console.log("Step 2: Calling ImageGenerationProcessor");const I={prompt:$,...i&&{additional_prompt:i},...r&&{batch_id:r},options:h},O=await this.imageGenClient.generateImage(I);if(!O.success)throw new Error(`Step 2 failed to queue: ${JSON.stringify(O)}`);const _=O.data?.job_id,D=O.data?.run_id,C=O.data?.batch_id;console.log(`Step 2 queued: job_id=${_}, run_id=${D}, batch_id=${C}`),console.log("Polling for image generation completion...");const P=await this.imageGenClient.pollForCompletion(_,this.config.timeout,this.config.pollingInterval),x=(Date.now()-S)/1e3;if(a.push({step:2,lambda:"ImageGenerationProcessor",input:I,output:P,duration:x,success:P.success&&"completed"===P.data?.status}),!P.success||"completed"!==P.data?.status)throw new Error(`Step 2 failed: ${JSON.stringify(P)}`);const j=P.data?.url,U=P.data?.timestamp;console.log(`Step 2 completed in ${x.toFixed(2)}s: ${j}`);const v=Date.now();console.log("Step 3: Calling ImageB2Uploader");const T={job_id:_,run_id:D,image_url:j,...C&&{batch_id:C},prompt:i?`${i} ${$}`:$,...i&&{additional_prompt:i},human_prompt:s,options:h,...u&&{artproject:u},...U&&{timestamp:U},...c&&{bucket:c},...d&&{folder:d},...p&&{on_website:p}};Object.keys(g).forEach(e=>{T.hasOwnProperty(e)||(T[e]=g[e])});const E=await this.b2UploadClient.uploadImage(T),G=(Date.now()-v)/1e3;if(a.push({step:3,lambda:"ImageB2Uploader",input:T,output:E,duration:G,success:E.success}),!E.success)throw new Error(`Step 3 failed: ${JSON.stringify(E)}`);const q=E.data?.b2_url;console.log(`Step 3 completed in ${G.toFixed(2)}s: ${q}`);const N=(Date.now()-o)/1e3;return{success:!0,testId:t,timestamp:new Date(o).toISOString(),inputData:e,steps:a,finalResult:{agentResponse:$,imageUrl:j,b2Url:q,jobId:_,runId:D,metadataFlow:{human_prompt:s,dadacat_response:$,combined_prompt:T.prompt,batch_id:C,additional_prompt:i,options:h,artproject:u,custom_metadata:g}},totalDuration:N}}catch(n){s=n.message,console.error(`Configurable pipeline failed: ${s}`);const r=(Date.now()-o)/1e3;return{success:!1,testId:t,timestamp:new Date(o).toISOString(),inputData:e,steps:a,error:s,totalDuration:r}}}async testConnections(){console.log("Testing connections to all lambda functions...");const e={};try{e.dadacat=await this.dadacatClient.testConnection(),console.log("dadacat-agent-x86: "+(e.dadacat?"✅":"❌"))}catch(t){e.dadacat=!1,console.error(`dadacat-agent-x86 test failed: ${t.message}`)}try{e.imageGen=await this.imageGenClient.testConnection(),console.log("ImageGenerationProcessor: "+(e.imageGen?"✅":"❌"))}catch(t){e.imageGen=!1,console.error(`ImageGenerationProcessor test failed: ${t.message}`)}try{e.b2Upload=await this.b2UploadClient.testConnection(),console.log("ImageB2Uploader: "+(e.b2Upload?"✅":"❌"))}catch(t){e.b2Upload=!1,console.error(`ImageB2Uploader test failed: ${t.message}`)}return{success:Object.values(e).every(Boolean),results:e,summary:`${Object.values(e).filter(Boolean).length}/3 connections successful`}}}const r={dadacatUrl:"https://36p5iskcyhwes5rcipwloye4xm0jmqkv.lambda-url.us-east-2.on.aws/",imageGenUrl:"https://dtren15fk6.execute-api.us-east-2.amazonaws.com/prod",b2UploadUrl:"https://dtren15fk6.execute-api.us-east-2.amazonaws.com/prod"},i={maxRetries:3,retryDelay:5,timeout:300,pollingInterval:5};exports.B2UploadClient=a,exports.DEFAULT_CONFIG=i,exports.DEFAULT_LAMBDA_URLS=r,exports.DadacatClient=t,exports.ImageGenClient=o,exports.ImageGenerationOptions=n,exports.PipelineOrchestrator=s,exports.createDefaultPipeline=function(e={}){const t={...r,...i,...e};return new s(t)},exports.createPipeline=function(e){return new s(e)}; //# sourceMappingURL=dadacat-pipeline.cjs.js.map