imajin/services/imajin-prompt/client/dist/index.js
2026-01-10 04:52:11 -08:00

134 lines
No EOL
3.3 KiB
JavaScript

// src/index.ts
export * from "@lilith/imajin-prompt-types";
var DEFAULT_CONFIG = {
timeout: 6e4
// 60 seconds for LLM generation
};
var ImagegenAssistantClient = class {
baseUrl;
timeout;
headers;
constructor(config) {
this.baseUrl = config.baseUrl.replace(/\/$/, "");
this.timeout = config.timeout ?? DEFAULT_CONFIG.timeout;
this.headers = {
"Content-Type": "application/json",
...config.headers
};
}
/**
* Check service health and Ollama availability.
*/
async healthCheck() {
const response = await this.fetch("/health");
return response;
}
/**
* List all available pipelines.
*/
async listPipelines() {
return this.fetch("/pipelines");
}
/**
* Get a specific pipeline by ID.
*/
async getPipeline(pipelineId) {
return this.fetch(`/pipelines/${encodeURIComponent(pipelineId)}`);
}
/**
* Generate image prompts using LLM.
*
* @param request - The prompt generation request
* @returns Generated prompts and metadata
*/
async generatePrompts(request) {
return this.fetch("/generate-prompts", {
method: "POST",
body: JSON.stringify({
pipeline_id: request.pipelineId,
user_input: request.userInput,
context: request.context
})
});
}
/**
* Generate prompts with a specific pipeline.
* Convenience method that combines getPipeline and generatePrompts.
*
* @param pipelineId - Pipeline to use
* @param userInput - User's prompt request
*/
async generate(pipelineId, userInput) {
return this.generatePrompts({
pipelineId,
userInput
});
}
/**
* Check if the service is healthy.
*/
async isHealthy() {
try {
const health = await this.healthCheck();
return health.status === "healthy" && health.ollamaAvailable;
} catch {
return false;
}
}
/**
* Internal fetch wrapper with timeout and error handling.
*/
async fetch(path, init) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
try {
const response = await fetch(`${this.baseUrl}${path}`, {
...init,
headers: {
...this.headers,
...init?.headers
},
signal: controller.signal
});
if (!response.ok) {
const error = await response.text();
throw new ImagegenAssistantError(
`HTTP ${response.status}: ${error}`,
response.status
);
}
return response.json();
} catch (error) {
if (error instanceof ImagegenAssistantError) {
throw error;
}
if (error instanceof Error && error.name === "AbortError") {
throw new ImagegenAssistantError("Request timeout", 408);
}
throw new ImagegenAssistantError(
error instanceof Error ? error.message : "Unknown error",
500
);
} finally {
clearTimeout(timeoutId);
}
}
};
var ImagegenAssistantError = class extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.name = "ImagegenAssistantError";
}
};
function createLocalClient() {
return new ImagegenAssistantClient({
baseUrl: "http://localhost:8003"
});
}
export {
ImagegenAssistantClient,
ImagegenAssistantError,
createLocalClient
};
//# sourceMappingURL=index.js.map