Skip to content

圖像生成 API ​

context.imageGeneration 依三層一致性暴露 Luker 結構化圖像生成能力,涵蓋後端能力探測、生圖預設切換、參考圖多協定路由、多角色分區提示詞以及圖像歸屬控制。

三層 API 暴露 ​

js
// Layer 1 —— 核心模組直接 ESM 匯入
import { imageGenerationApi } from '/scripts/image-generation/registry.js';

// Layer 2 —— lukerContext / Luker.getContext() / 擴充 API 註冊表
const imageGen = Luker.getContext().imageGeneration;
const sdApi = Luker.getContext().getExtensionApi('sd');

// Layer 3 —— CardApp 沙箱上下文
const cardImageGen = ctx.imageGeneration;

CardApp 內的 ctx.imageGeneration.generate 預設將 attribution 設為 'active_chat';生成獨立於目前聊天的資產時可顯式傳入 attribution: 'none'。

可用性與後端能力探測 ​

ts
imageGeneration.isAvailable(): boolean
imageGeneration.getCapabilities(): Promise<ImageCapabilities>

ImageCapabilities 描述目前生圖後端與模型支援的結構化能力:

欄位型別說明
sourcestring目前生圖來源識別碼,例如 'google'、'chat_image'、'openai'、'openrouter'、'xai'、'novel'、'comfy'、'auto' 或 'vlad'
modelstring目前生圖模型識別碼
supportsCharacterReferenceboolean是否支援傳入 role: 'character' 角色參考圖
maxCharacterReferencesnumber | null單次請求支援的角色參考圖上限;協定無固定上限時為 null
supportsStyleReferenceboolean是否支援傳入 role: 'style' 畫風參考圖
maxStyleReferencesnumber | null單次請求支援的畫風參考圖上限;協定無固定上限時為 null
supportsSourceImageboolean是否支援傳入 role: 'source' 底圖執行圖像修改
supportsMultiCharacterPromptsboolean是否支援 characterPrompts 多角色分區提示詞與座標
supportsDynamicAspectRatioboolean是否支援按次覆寫 aspectRatio 畫幅比例
availableLoras{ names: string[], includeWeight: boolean }目前 ComfyUI 工作流暴露的動態 LoRA 清單

傳入超出 getCapabilities() 支援範圍的參考圖角色或數量時,generate() 會直接報錯,不靜默丟棄呼叫方引數。

發起圖像生成 ​

ts
imageGeneration.generate(
    requestOrPrompt: string | ImageGenerationRequest,
    options?: Partial<ImageGenerationRequest>,
): Promise<{
    imagePath: string;
    format: string;
    prompt: string;
    negativePrompt: string;
}>

請求參數 ​

欄位型別說明
promptstring必填正向提示詞
negativePromptstring可選負向提示詞
aspectRatio'1:1' | '2:3' | '3:2' | '3:4' | '4:3' | '9:16' | '16:9'單次畫幅比例覆寫,自動映射至目標後端解析度或原生比例參數
width / heightnumber顯式像素寬高覆寫
seednumber隨機種子
lorasArray<{ name: string, weight?: number }>標準 ComfyUI 工作流動態 LoRA 清單
referencesImageReferenceInput[]受管本機參考圖陣列
characterPromptsArray<{ prompt: string, negativePrompt?: string, center?: { x: number, y: number } }>多角色獨立提示詞與歸一化座標,取值範圍 0 至 1
attribution'active_chat' | 'none' | { char_dir: string, chat_name: string, is_group: boolean }控制寫入清理索引的聊天歸屬中繼資料;畫室草稿等跨聊天資產傳 'none'
subdirstring/user/images/ 下的輸出子目錄,例如 '_studio'
includePresetStyleReferencesboolean預設為 true;後端支援畫風參考時自動合併目前生圖預設綁定的畫風參考圖
skipPromptPrefixboolean為 true 時略過全域正向提示詞前綴拼接
signalAbortSignal中止訊號,觸發後立即取消進行中的生成請求

參考圖輸入範例 ​

references 僅接受 /user/images/、/characters/ 或 /User Avatars/ 下的受管相對路徑:

js
const result = await Luker.getContext().imageGeneration.generate({
    prompt: '1girl, rooftop observatory, starlight, brass astrolabe',
    aspectRatio: '3:4',
    references: [
        {
            imagePath: '/user/images/_references/char_alice_a1b2c3d4.png',
            role: 'character',
            name: 'Alice',
            fidelity: 0.8,
        },
        {
            imagePath: '/user/images/_style_references/watercolor.png',
            role: 'style',
            strength: 0.6,
        },
    ],
    attribution: 'active_chat',
});

伺服端媒體載入器直接拒絕外部 URL 與越界相對路徑。

生圖預設與 LoRA 目錄 ​

ts
imageGeneration.listPresets(): Array<{ id: string, name: string, source: string, model: string }>
imageGeneration.getCurrentPreset(): { id: string, name: string, config: object } | null
imageGeneration.switchPreset(idOrName: string): Promise<{ id: string, name: string }>
imageGeneration.getAvailableLoras(): Promise<{ names: string[], includeWeight: boolean }>

生圖預設綁定了連線設定後,若對應連線設定被刪除,generate() 會直接報錯提示重新綁定。

跨外掛圖片搜尋與網頁圖片擷取介面 ​

需要聯網檢索視覺參考圖的擴充功能可透過 getExtensionApi('search-tools') 呼叫搜尋外掛能力:

js
const searchApi = Luker.getContext().getExtensionApi('search-tools');
if (searchApi) {
    const capabilities = searchApi.getActiveProviderCapabilities();
    if (capabilities.supportsImageSearch) {
        const { results } = await searchApi.searchImages({
            query: '角色設定圖',
            max_results: 8,
        });
    }
    const { images } = await searchApi.extractPageImages({
        url: 'https://example.com/character-entry',
    });
}

相關頁面 ​

基於 SillyTavern 建構