Building a Multi-Protocol Image Generation Web UI: Design and Practice

    This article explains how to design a web frontend for image generation that works across multiple API protocols. It covers a protocol adaptation layer with a unified internal request model and normalized responses, separate reasoning effort tiers for text and multimodal models, client-side image compression before upload, and batch and randomization features driven by a scheduler. It also outlines a three-section interface layout and how to move scheduling and storage to cloud services for long-running or team use.

    1. Why a Unified Image Generation Frontend Is Needed

    In real image generation work, teams often face several API specifications at the same time: some follow a general chat-style request structure, some use a streaming response structure, and some native multimodal protocols differ significantly in field naming and how content blocks are organized. Maintaining a separate page and script for each specification fragments the codebase quickly, and debugging costs grow linearly with the number of providers integrated.

    Image generation tasks also have a distinct batch nature: the same prompt needs multiple samples to pick the best result, and the same batch of prompts needs to run in one pass for side-by-side comparison. An interface without batch and randomization features leaves engineers spending their time on repetitive clicking.

    A proper frontend therefore needs to solve three things: consolidating protocol differences, keeping reasoning effort controllable, and preprocessing input assets. These three points form the main thread of the following sections.

    2. Protocol Adaptation Layer: Consolidating Differences in One Place

    2.1 Define a Unified Internal Request Model

    Start by defining a vendor-neutral internal data structure that keeps only the semantic fields that actually affect results, such as the prompt, the list of reference images, output dimensions, sample count, random seed, and reasoning effort tier. All external protocols are converted in both directions inside adapters:

    1. Internal request model -> Adapter A (chat-style structure) -> upstream API
    2. -> Adapter B (streaming structure) -> upstream API
    3. -> Adapter C (native multimodal structure) -> upstream API

    The benefit is that page logic is written against a single model, and adding a new protocol requires only a new adapter, with no changes to the interface or task scheduling code.

    2.2 Normalize Responses and Errors

    Differences in response bodies must be smoothed over as well: some protocols put generated results in a content block array, some return them as an incremental event stream, and some place error codes in nested fields. In practice, it helps to normalize everything into three result types: success (with the artifact URL or data), retryable errors (rate limiting, timeouts), and non-retryable errors (invalid parameters, authentication failures). The task scheduler then decides its retry policy based only on these three types, avoiding a separate branch of checks for every protocol.

    2.3 Notes on Relay and Proxy Scenarios

    When requests are forwarded through a relay service, three extra issues need handling: layered timeouts, isolation of authentication information, and the validity period of artifact URLs. A sound approach is to make the relay endpoint a configurable item decoupled from the protocol type, and to re-store or persist the returned artifact URLs so that expiring links do not cause batch task results to be lost.

    3. Reasoning Effort Tiers: Controlled Separately for Text and Multimodal

    3.1 Why Separate Tiers Are Needed

    Text models and multimodal models differ in compute cost and latency characteristics. Raising reasoning effort on the text side usually yields more rigorous logical organization; on the multimodal side, excessive effort tends to slow responses and raise costs, with gains that are not always linear. Splitting the two into independent configuration items is therefore a prerequisite for cost control.

    3.2 Implementing the Parameter Mapping

    Protocols do not express “thinking effort” in a uniform way: some use enumerated tiers, some use a numeric budget, and some enable it through an extra field. A practical approach is to maintain a mapping table in the adapter that translates the low/medium/high tiers in the UI into the actual parameters of each protocol:

    1. {
    2. "text": { "low": "minimal", "medium": "default", "high": "extended" },
    3. "vision": { "low": "default", "medium": "extended", "high": "extended" }
    4. }

    Once the mapping table is decoupled from the interface, changing the strategy requires no frontend code changes, and it is also easier to run canary validation for different task types.

    3.3 Observability

    Tier settings are only meaningful when paired with observability. It helps to record the tier, the actual parameters sent, the elapsed time, and the number of artifacts for each request, forming a comparable table view. Only with data in hand can you judge whether a given tier deserves to be enabled by default.

    4. Image Compression: Necessary Preprocessing Before Upload

    4.1 The Objective of Compression

    Compression before sending an image to a multimodal model is essentially a balance between recognizability and transfer size. A practical approach uses two constraints, a maximum pixel length for the long edge and a quality factor: scale by dimension first, then adjust quality as needed, rather than starting with a very low quality that loses detail.

    4.2 Frontend Implementation

    In the browser, an offscreen canvas can handle scaling and re-encoding into a more size-friendly format. For cases that must preserve an alpha channel, explicitly specify an output type that supports transparency. Example code:

    1. async function compressImage(file, maxEdge = 1024, quality = 0.82) {
    2. const bitmap = await createImageBitmap(file);
    3. const scale = Math.min(1, maxEdge / Math.max(bitmap.width, bitmap.height));
    4. const canvas = new OffscreenCanvas(
    5. Math.round(bitmap.width * scale),
    6. Math.round(bitmap.height * scale)
    7. );
    8. canvas.getContext('2d').drawImage(bitmap, 0, 0, canvas.width, canvas.height);
    9. const blob = await canvas.convertToBlob({ type: 'image/webp', quality });
    10. return blob;
    11. }

    4.3 Working with Batch Tasks

    In batch scenarios, compression should happen before the concurrency queue so that multiple tasks do not repeatedly decode the same original image. A cache keyed by file fingerprint can ensure identical assets are compressed only once; a fallback path should also be set for compression failures, using the original image directly and logging a warning to prevent the whole batch from being interrupted.

    5. Batch and Randomization: Leaving Repetitive Work to the Scheduler

    Batch capability has two dimensions: prompt batching and sample batching. The former is executed serially or with limited concurrency by a task queue; the latter is expressed within a single request through the sample count parameter. Randomization includes random prompt combinations and random seeds: the former is used to explore the prompt space, the latter to make results reproducible.

    From an engineering standpoint, it helps to generate a unique identifier for each task and record its prompt, seed, tier, and artifact references, forming a traceable experiment record. Combined with failure retries and resume-from-checkpoint, the stability of long batch tasks improves noticeably.

    6. Interface and Engineering Recommendations

    The interface can follow a three-section layout of configuration, tasks, and results: the configuration section holds the protocol, relay endpoint, tiers, and compression parameters; the task section shows queue progress and failure reasons; the result section supports thumbnail comparison, enlarged viewing, and batch download. For state management, separate persistent configuration from temporary input so that refreshing the page does not lose key parameters.

    If multi-user collaboration or long-running operation is needed, scheduling and storage can be moved to general cloud capabilities, for example object storage for artifacts, a message queue for peak shaving, a container platform to host the stateless frontend, and logging, monitoring, and alerting services to track failure rates, evolving a single-machine script into a service that can run sustainably.

    7. Conclusion

    Multi-protocol adaptation, tiered reasoning effort, and pre-upload compression may look like three independent features, but they all point to one goal: making the image generation experiment process reusable, comparable, and controllable. Consolidating differences in the adapter layer, turning cost switches into configuration items, and leaving repetitive work to the scheduler, this structure supports both fast individual validation and a smooth transition to team collaboration.