dadacat-lambda-pipeline
Version:
JavaScript client for the dadacat 3-lambda image generation pipeline
113 lines (94 loc) • 3.75 kB
JavaScript
/**
* Client for dadacat-agent-x86 lambda function
* @module DadacatClient
*/
import fetch from 'cross-fetch';
/**
* Client for interacting with dadacat-agent-x86 lambda
*/
export class DadacatClient {
/**
* Initialize with function URL
* @param {string} [functionUrl] - The lambda function URL
*/
constructor(functionUrl) {
this.functionUrl = functionUrl;
this.timeout = 30000; // 30 second timeout
}
/**
* Generate agent response for given prompt
* @param {string} prompt - Input prompt string (will be sent as 'message' field to lambda)
* @returns {Promise<Object>} Response object with status and response
*/
async generateResponse(prompt) {
if (!this.functionUrl) {
throw new Error('dadacat-agent-x86 function URL not configured');
}
console.log(`[DadacatClient] Input prompt type: ${typeof prompt}`);
console.log(`[DadacatClient] Input prompt value: "${prompt}"`);
console.log(`[DadacatClient] Input prompt length: ${prompt ? prompt.length : 'null/undefined'}`);
const payload = { message: prompt };
const jsonPayload = JSON.stringify(payload);
console.log(`[DadacatClient] Payload object:`, payload);
console.log(`[DadacatClient] JSON stringified payload: ${jsonPayload}`);
console.log(`[DadacatClient] JSON payload length: ${jsonPayload.length} bytes`);
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.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 response = await fetch(this.functionUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: jsonPayload,
signal: controller.signal
});
clearTimeout(timeoutId);
console.log(`[DadacatClient] Response status: ${response.status}`);
console.log(`[DadacatClient] Response headers:`, Object.fromEntries(response.headers.entries()));
if (!response.ok) {
const errorText = await response.text();
console.error(`[DadacatClient] Error response body: ${errorText}`);
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
}
const responseText = await response.text();
console.log(`[DadacatClient] Raw response text: ${responseText}`);
let result;
try {
result = JSON.parse(responseText);
} catch (parseError) {
console.error(`[DadacatClient] Failed to parse response as JSON:`, parseError);
throw new Error(`Invalid JSON response: ${responseText}`);
}
console.log(`[DadacatClient] Parsed response:`, result);
console.log(`[DadacatClient] Response status: ${result.status}`);
return result;
} catch (error) {
if (error.name === 'AbortError') {
console.error('dadacat-agent-x86 request timed out');
return { status: 'error', error: 'Request timed out' };
}
console.error(`dadacat-agent-x86 request failed: ${error.message}`);
return { status: 'error', error: error.message };
}
}
/**
* Test if the lambda function is accessible
* @returns {Promise<boolean>} True if accessible, false otherwise
*/
async testConnection() {
try {
const result = await this.generateResponse('test');
return result.status === 'success';
} catch (error) {
console.error(`Connection test failed: ${error.message}`);
return false;
}
}
}