2026-01-10 04:52:11 -08:00
|
|
|
# Client Libraries
|
|
|
|
|
|
2026-01-16 17:01:10 -08:00
|
|
|
TypeScript HTTP clients for consuming @imajin services.
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
## Available Clients
|
|
|
|
|
|
|
|
|
|
| Package | Service | Port |
|
|
|
|
|
|---------|---------|------|
|
2026-01-10 05:04:48 -08:00
|
|
|
| `@lilith/imajin-diffusion-client` | imajin-diffusion | 8002 |
|
|
|
|
|
| `@lilith/imajin-prompt-client` | imajin-prompt | 8003 |
|
|
|
|
|
| `@lilith/imajin-processing-client` | imajin-processing | 8004 |
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-01-10 05:04:48 -08:00
|
|
|
npm install @lilith/imajin-diffusion-client
|
|
|
|
|
npm install @lilith/imajin-prompt-client
|
|
|
|
|
npm install @lilith/imajin-processing-client
|
2026-01-10 04:52:11 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Peer dependency**: `zod` (for runtime validation)
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
npm install zod
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## ImageGenerationClient
|
|
|
|
|
|
|
|
|
|
### Setup
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-01-10 05:04:48 -08:00
|
|
|
import { ImageGenerationClient } from '@lilith/imajin-diffusion-client';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
const client = new ImageGenerationClient({
|
|
|
|
|
baseUrl: 'http://localhost:8002',
|
|
|
|
|
// Optional: custom fetch, timeout, etc.
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Health Check
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const health = await client.healthCheck();
|
|
|
|
|
console.log(health.gpuAvailable); // true
|
|
|
|
|
console.log(health.models); // ['photorealistic', 'anime']
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Synchronous Generation
|
|
|
|
|
|
|
|
|
|
Best for small images (< 1024x1024):
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.generate({
|
|
|
|
|
prompt: 'A professional product photo',
|
|
|
|
|
model: 'photorealistic',
|
|
|
|
|
layout: 'product_square',
|
|
|
|
|
negativePrompt: 'blurry, low quality',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
|
const imageData = result.result.imageData; // base64
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Async Generation
|
|
|
|
|
|
|
|
|
|
Best for large images:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Start job
|
|
|
|
|
const { jobId } = await client.generateAsync({
|
|
|
|
|
prompt: 'Wide banner image',
|
|
|
|
|
layout: 'hero',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Poll for completion
|
|
|
|
|
const result = await client.pollJobStatus(jobId, {
|
|
|
|
|
interval: 1000, // Poll every 1s
|
|
|
|
|
timeout: 60000, // Max 60s
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Smart Generation
|
|
|
|
|
|
|
|
|
|
Automatically selects sync/async based on image size:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.generateSmart({
|
|
|
|
|
prompt: 'Any image',
|
|
|
|
|
layout: 'widescreen',
|
|
|
|
|
});
|
|
|
|
|
// Uses async for widescreen, sync for smaller layouts
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Batch Generation
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const results = await client.generateBatch([
|
|
|
|
|
{ prompt: 'Image 1', layout: 'square' },
|
|
|
|
|
{ prompt: 'Image 2', layout: 'square' },
|
|
|
|
|
{ prompt: 'Image 3', layout: 'square' },
|
|
|
|
|
]);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## ImagegenAssistantClient
|
|
|
|
|
|
|
|
|
|
### Setup
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-01-10 05:04:48 -08:00
|
|
|
import { ImagegenAssistantClient } from '@lilith/imajin-prompt-client';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
const client = new ImagegenAssistantClient({
|
|
|
|
|
baseUrl: 'http://localhost:8003',
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### List Pipelines
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const pipelines = await client.listPipelines();
|
|
|
|
|
// [{ id: 'skeleton-anime-girls', name: '...' }, ...]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Generate Prompts
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.generatePrompts({
|
|
|
|
|
pipelineId: 'skeleton-anime-girls',
|
|
|
|
|
userInput: 'Generate 5 hologram style skeletons',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(result.prompts);
|
|
|
|
|
// [{ name: 'hologram_1', prompt: '...', negativePrompt: '...' }, ...]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Analyze Context
|
|
|
|
|
|
|
|
|
|
Two-stage analysis with cultural classification:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const config = await client.analyzeContext({
|
|
|
|
|
category: 'escorts',
|
|
|
|
|
city: 'Tokyo',
|
|
|
|
|
filters: ['kawaii', 'femboy'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(config.model); // 'anime'
|
|
|
|
|
console.log(config.maturityLevel); // 'suggestive'
|
|
|
|
|
console.log(config.prompts); // Image prompts
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## ImageProcessingClient
|
|
|
|
|
|
|
|
|
|
### Setup
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-01-10 05:04:48 -08:00
|
|
|
import { ImageProcessingClient } from '@lilith/imajin-processing-client';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
const client = new ImageProcessingClient({
|
|
|
|
|
baseUrl: 'http://localhost:8004',
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Sanitize
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.sanitize({
|
|
|
|
|
imageData: base64Image,
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Resize
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.resize({
|
|
|
|
|
imageData: base64Image,
|
|
|
|
|
width: 800,
|
|
|
|
|
height: 600,
|
|
|
|
|
fit: 'cover',
|
|
|
|
|
quality: 85,
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Generate Derivatives
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.derivatives({
|
|
|
|
|
imageData: base64Image,
|
|
|
|
|
variants: [
|
|
|
|
|
{ name: 'large', width: 1200 },
|
|
|
|
|
{ name: 'medium', width: 800 },
|
|
|
|
|
{ name: 'thumb', width: 150 },
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// result.variants = { large: '...', medium: '...', thumb: '...' }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Convert Format
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
const result = await client.convert({
|
|
|
|
|
imageData: base64Image,
|
|
|
|
|
format: 'webp',
|
|
|
|
|
quality: 85,
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Error Handling
|
|
|
|
|
|
|
|
|
|
All clients throw typed errors:
|
|
|
|
|
|
|
|
|
|
```typescript
|
2026-01-10 05:04:48 -08:00
|
|
|
import { ImageGenerationError } from '@lilith/imajin-diffusion-client';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await client.generate({ ... });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof ImageGenerationError) {
|
|
|
|
|
console.log(error.code); // 'GPU_OOM'
|
|
|
|
|
console.log(error.message); // 'GPU out of memory'
|
|
|
|
|
console.log(error.details); // { requested: '8GB', available: '4GB' }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## TypeScript Types
|
|
|
|
|
|
|
|
|
|
Types are exported from `-types` packages:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import type {
|
|
|
|
|
GenerateRequest,
|
|
|
|
|
GenerateResponse,
|
|
|
|
|
LayoutType,
|
|
|
|
|
ModelType,
|
2026-01-10 05:04:48 -08:00
|
|
|
} from '@lilith/imajin-diffusion-types';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
AnalyzeContextRequest,
|
|
|
|
|
GenerationConfig,
|
|
|
|
|
ParsedPrompt,
|
2026-01-10 05:04:48 -08:00
|
|
|
} from '@lilith/imajin-prompt-types';
|
2026-01-10 04:52:11 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## React Integration
|
|
|
|
|
|
|
|
|
|
Use with TanStack Query:
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import { useQuery, useMutation } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
|
|
const client = new ImageGenerationClient({ baseUrl: '...' });
|
|
|
|
|
|
|
|
|
|
// Query
|
|
|
|
|
const { data: health } = useQuery({
|
|
|
|
|
queryKey: ['health'],
|
|
|
|
|
queryFn: () => client.healthCheck(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Mutation
|
|
|
|
|
const generateMutation = useMutation({
|
|
|
|
|
mutationFn: (request) => client.generate(request),
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-10 05:04:48 -08:00
|
|
|
Or use the built-in hooks from `@lilith/imajin-react`:
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
```typescript
|
2026-01-10 05:04:48 -08:00
|
|
|
import { useImagegenAssistant } from '@lilith/imajin-react';
|
2026-01-10 04:52:11 -08:00
|
|
|
|
|
|
|
|
const { generatePrompts, isLoading } = useImagegenAssistant({
|
|
|
|
|
baseUrl: 'http://localhost:8003',
|
|
|
|
|
});
|
|
|
|
|
```
|