All files / src/image index.ts

56.04% Statements 51/91
81.81% Branches 9/11
75% Functions 3/4
56.04% Lines 51/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                        1x                                                                               1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x       1x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x   5x     5x 5x 5x 5x 2x 2x 2x   5x   5x 5x                                                                                       5x 11x 11x 5x 5x       1x 2x 2x 2x 2x 2x 2x 2x 2x  
/**
 * AgentKits — Image Generation Module
 *
 * Multi-provider image generation with unified interface.
 * Supports: OpenAI (DALL-E), DashScope (通义万象), Zhipu (CogView), custom.
 *
 * Usage:
 *   import { createImageGen } from 'agentkits/image';
 *   const gen = createImageGen({ provider: 'openai', apiKey: '...' });
 *   const images = await gen.generate('A cat sitting on a rainbow');
 */
 
import OpenAI from 'openai';
 
// ── Types ──────────────────────────────────────────────────────────
 
export type ImageProvider = 'openai' | 'dashscope' | 'zhipu' | 'stability' | 'custom';
 
export interface ImageGenConfig {
  provider?: ImageProvider;
  model?: string;
  apiKey?: string;
  baseUrl?: string;
  size?: '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792';
  quality?: 'standard' | 'hd';
  n?: number;
}
 
export interface GeneratedImage {
  url?: string;
  b64_json?: string;
  revised_prompt?: string;
}
 
export interface ImageGenClient {
  /** Generate image(s) from a prompt */
  generate(prompt: string, options?: { size?: string; quality?: string; n?: number; responseFormat?: 'url' | 'b64_json' }): Promise<GeneratedImage[]>;
  /** Current resolved config */
  readonly config: Readonly<ResolvedImageConfig>;
}
 
interface ResolvedImageConfig {
  provider: ImageProvider;
  model: string;
  baseUrl?: string;
  size: string;
  quality: string;
  n: number;
}
 
// ── Provider Defaults ──────────────────────────────────────────────
 
const PROVIDERS: Record<ImageProvider, { model: string; baseUrl?: string }> = {
  openai:    { model: 'dall-e-3' },
  dashscope: { model: 'wanx-v1', baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1' },
  zhipu:     { model: 'cogview-4', baseUrl: 'https://open.bigmodel.cn/api/paas/v4' },
  stability: { model: 'stable-diffusion-xl-1024-v1-0', baseUrl: 'https://api.stability.ai/v1' },
  custom:    { model: 'dall-e-3' },
};
 
const ENV_MAP: Record<ImageProvider, string[]> = {
  openai:    ['OPENAI_API_KEY'],
  dashscope: ['DASHSCOPE_API_KEY'],
  zhipu:     ['ZHIPU_API_KEY'],
  stability: ['STABILITY_API_KEY'],
  custom:    ['AGENTKIT_IMAGE_KEY'],
};
 
// ── Factory ────────────────────────────────────────────────────────
 
export function createImageGen(userConfig: ImageGenConfig = {}): ImageGenClient {
  const provider = userConfig.provider ?? 'openai';
  const defaults = PROVIDERS[provider] ?? PROVIDERS.openai;
 
  const resolved: ResolvedImageConfig = {
    provider,
    model: userConfig.model ?? defaults.model,
    baseUrl: userConfig.baseUrl ?? defaults.baseUrl,
    size: userConfig.size ?? '1024x1024',
    quality: userConfig.quality ?? 'standard',
    n: userConfig.n ?? 1,
  };
 
  const apiKey = userConfig.apiKey
    ?? (ENV_MAP[provider] ?? []).map(k => process.env[k]).find(Boolean);
 
  const clientOpts: Record<string, unknown> = {};
  if (apiKey) clientOpts.apiKey = apiKey;
  if (resolved.baseUrl) clientOpts.baseURL = resolved.baseUrl;
  if (provider !== 'openai') {
    clientOpts.organization = null;
    clientOpts.project = null;
  }
 
  const client = new OpenAI(clientOpts as any);
 
  return {
    async generate(prompt, options = {}) {
      if (provider === 'stability') {
        // Stability AI uses a different REST API
        const url = `${resolved.baseUrl}/generation/${resolved.model}/text-to-image`;
        const res = await fetch(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${apiKey}`,
            'Accept': 'application/json',
          },
          body: JSON.stringify({
            text_prompts: [{ text: prompt }],
            cfg_scale: 7,
            height: parseInt((options.size ?? resolved.size).split('x')[1]) || 1024,
            width: parseInt((options.size ?? resolved.size).split('x')[0]) || 1024,
            samples: options.n ?? resolved.n,
          }),
        });
        if (!res.ok) throw new Error(`Stability API error: ${res.status} ${await res.text()}`);
        const data = await res.json() as { artifacts: Array<{ base64: string }> };
        return (data.artifacts ?? []).map((a: any) => ({
          b64_json: a.base64,
          url: undefined,
          revised_prompt: undefined,
        }));
      }
 
      const response = await client.images.generate({
        model: resolved.model,
        prompt,
        size: (options.size ?? resolved.size) as any,
        quality: (options.quality ?? resolved.quality) as any,
        n: options.n ?? resolved.n,
        response_format: options.responseFormat ?? 'url',
      });
 
      return (response.data ?? []).map(img => ({
        url: img.url ?? undefined,
        b64_json: img.b64_json ?? undefined,
        revised_prompt: img.revised_prompt ?? undefined,
      }));
    },
 
    get config() {
      return resolved;
    },
  };
}
 
// ── Convenience ────────────────────────────────────────────────────
 
export function listImageProviders(): Array<{ id: ImageProvider; model: string; region: string }> {
  return [
    { id: 'openai',    model: 'dall-e-3',   region: 'Global' },
    { id: 'dashscope', model: 'wanx-v1',    region: 'Global (Alibaba)' },
    { id: 'zhipu',     model: 'cogview-4',  region: 'Global (Zhipu)' },
    { id: 'stability', model: 'stable-diffusion-xl', region: 'Global (Stability AI)' },
    { id: 'custom',    model: 'configurable', region: 'Any' },
  ];
}