Skip to content

✨ feat: add image generation support for google provider #8503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tjx666
Copy link
Collaborator

@tjx666 tjx666 commented Jul 20, 2025

  • Introduced createImage method in LobeGoogleAI for generating images based on user prompts and aspect ratios.
  • Added error handling for various scenarios in image generation.
  • Updated model configurations to include new image models in google.ts.
  • Enhanced test coverage for image generation functionality in index.test.ts.

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • 📝 docs
  • 🔨 chore

🔀 变更说明 | Description of Change

📝 补充信息 | Additional Information

Summary by Sourcery

Add image generation support for the Google provider by implementing a createImage method in LobeGoogleAI, updating model configurations with new Imagen models, and extending tests to cover image generation scenarios.

New Features:

  • Implement createImage method in LobeGoogleAI for generating images from user prompts with aspect ratio support
  • Add new Imagen image model cards to Google provider configuration

Enhancements:

  • Map and wrap Google API image errors into AgentRuntimeError
  • Include default aspect ratio and numberOfImages in image generation config

Tests:

  • Add comprehensive createImage tests covering success cases, multiple aspect ratios, missing parameters, error handling, and edge cases
  • Translate and update existing test comments to English for consistency

Copy link

vercel bot commented Jul 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lobe-chat-database ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 20, 2025 9:09am
lobe-chat-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 20, 2025 9:09am

Copy link
Contributor

sourcery-ai bot commented Jul 20, 2025

Reviewer's Guide

This PR adds end-to-end support for image generation via Google’s Imagen API by implementing a new createImage method in LobeGoogleAI, enriching model configurations with Imagen model entries, extending runtime types and error utilities for image generation errors, and validating the new feature through extensive tests.

Sequence diagram for createImage flow in LobeGoogleAI

sequenceDiagram
  participant User
  participant LobeGoogleAI
  participant GoogleAIClient
  User->>LobeGoogleAI: createImage(payload)
  LobeGoogleAI->>GoogleAIClient: generateImages({model, prompt, config})
  GoogleAIClient-->>LobeGoogleAI: {generatedImages: [{image: {imageBytes}}]}
  LobeGoogleAI-->>User: {imageUrl}
  Note over LobeGoogleAI: On error, throws AgentRuntimeError.createImage
Loading

Class diagram for AgentRuntimeError utility with image error support

classDiagram
  class AgentRuntimeError {
    +chat(error: ChatCompletionErrorPayload): ChatCompletionErrorPayload
    +init(errorType: ILobeAgentRuntimeErrorType | string | number, error?: any): AgentInitErrorPayload
    +createImage(error: CreateImageErrorPayload): CreateImageErrorPayload
    +textToImage(error: any): any
  }

  class CreateImageErrorPayload {
    +endpoint?: string
    +error: object
    +errorType: ILobeAgentRuntimeErrorType
    +provider: string
  }

  AgentRuntimeError --> CreateImageErrorPayload
Loading

File-Level Changes

Change Details Files
Implement createImage method in LobeGoogleAI
  • Construct and send generateImages request with model, prompt, and aspectRatio
  • Validate response.generatedImages array and imageBytes presence
  • Convert imageBytes to a data URL and wrap errors using parseErrorMessage and AgentRuntimeError.createImage
src/libs/model-runtime/google/index.ts
Add Google Imagen models to configuration
  • Define common parameters schema for Imagen models
  • Add googleImageModels array with new AIImageModelCard entries
  • Merge chat and image models in exported allModels
src/config/aiModels/google.ts
Extend runtime types and error utilities for image errors
  • Introduce CreateImageErrorPayload in model-runtime types
  • Add createImage handler to AgentRuntimeError in error utility
src/libs/model-runtime/types/type.ts
src/libs/model-runtime/utils/createError.ts
Add comprehensive tests for createImage
  • Test successful image generation with various aspect ratios and prompts
  • Cover error mapping for invalid API keys, network failures, and malformed responses
  • Validate edge cases: multiple images and custom future model versions
src/libs/model-runtime/google/index.test.ts

Possibly linked issues

  • #0: The PR implements AI image generation for Google, directly addressing the issue's request for image generation support.
  • ♻️ refactor: refactor with new market url #123: The PR adds image generation support using Google's Imagen models, directly fulfilling the issue's request.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@lobehubbot
Copy link
Member

👍 @tjx666

Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link
Contributor

gru-agent bot commented Jul 20, 2025

TestGru Assignment

Summary

Link CommitId Status Reason
Detail fd86a38 🚫 Skipped No files need to be tested {"src/config/aiModels/google.ts":"File path does not match include patterns.","src/libs/model-runtime/google/index.test.ts":"File path does not match include patterns.","src/libs/model-runtime/google/index.ts":"File path does not match include patterns.","src/libs/model-runtime/types/type.ts":"File path does not match include patterns.","src/libs/model-runtime/utils/createError.ts":"File path does not match include patterns."}

History Assignment

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🌠 Feature Request New feature or request | 特性与建议 labels Jul 20, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @tjx666 - I've reviewed your changes - here's some feedback:

  • Instead of hard-coding numberOfImages to 1 in createImage, consider making it a configurable parameter so users can request multiple images if needed.
  • Rather than using console.error for error logging in createImage, switch to your project’s existing logging utility to keep logs consistent and more easily configurable.
  • createImage assumes a PNG output when building the data URI—consider inferring or allowing configuration of the MIME type in case other formats are supported in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Instead of hard-coding numberOfImages to 1 in createImage, consider making it a configurable parameter so users can request multiple images if needed.
- Rather than using console.error for error logging in createImage, switch to your project’s existing logging utility to keep logs consistent and more easily configurable.
- createImage assumes a PNG output when building the data URI—consider inferring or allowing configuration of the MIME type in case other formats are supported in the future.

## Individual Comments

### Comment 1
<location> `src/libs/model-runtime/google/index.ts:274` </location>
<code_context>
+      }
+
+      const imageBytes = generatedImage.image.imageBytes;
+      const imageUrl = `data:image/png;base64,${imageBytes}`;
+
+      return { imageUrl };
</code_context>

<issue_to_address>
The image MIME type is hardcoded as PNG, which may not always match the actual image format.

If other image formats are possible, make the MIME type dynamic or validate the format. Otherwise, document the PNG-only assumption.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
      const imageBytes = generatedImage.image.imageBytes;
      const imageUrl = `data:image/png;base64,${imageBytes}`;

      return { imageUrl };
=======
      const imageBytes = generatedImage.image.imageBytes;
      // Determine MIME type dynamically if available, otherwise assume PNG
      let mimeType = 'image/png';
      if (generatedImage.image.mimeType) {
        mimeType = generatedImage.image.mimeType;
      } else {
        // If only PNG is supported, document this assumption
        // Assumes generated images are always PNG. Update this if other formats are supported.
      }
      const imageUrl = `data:${mimeType};base64,${imageBytes}`;

      return { imageUrl };
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/config/aiModels/google.ts:501` </location>
<code_context>

-export const allModels = [...googleChatModels];
+// Common parameters for Imagen models
+const imagenBaseParameters: ModelParamsSchema = {
+  aspectRatio: {
+    default: '1:1',
+    enum: ['1:1', '16:9', '9:16', '3:4', '4:3'],
+  },
+  prompt: { default: '' },
+};
+
</code_context>

<issue_to_address>
The 'prompt' parameter in imagenBaseParameters lacks validation or description.

Add a description and validation, such as minLength, to the 'prompt' parameter for better schema clarity and validation.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
  prompt: { default: '' },
=======
  prompt: {
    default: '',
    description: 'The text prompt describing the image to generate.',
    minLength: 1,
  },
>>>>>>> REPLACE

</suggested_fix>

### Comment 3
<location> `src/libs/model-runtime/types/type.ts:19` </location>
<code_context>
   provider: string;
 }

+export interface CreateImageErrorPayload {
+  [key: string]: any;
+  endpoint?: string;
+  error: object;
+  errorType: ILobeAgentRuntimeErrorType;
+  provider: string;
+}
+
</code_context>

<issue_to_address>
The CreateImageErrorPayload interface allows arbitrary keys, which may reduce type safety.

Consider limiting the interface to specific fields or clearly documenting its structure to improve maintainability and type safety.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
export interface CreateImageErrorPayload {
  [key: string]: any;
  endpoint?: string;
  error: object;
  errorType: ILobeAgentRuntimeErrorType;
  provider: string;
}
=======
export interface CreateImageErrorPayload {
  endpoint?: string;
  error: object;
  errorType: ILobeAgentRuntimeErrorType;
  provider: string;
}
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 273 to 279
const imageBytes = generatedImage.image.imageBytes;
const imageUrl = `data:image/png;base64,${imageBytes}`;

return { imageUrl };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The image MIME type is hardcoded as PNG, which may not always match the actual image format.

If other image formats are possible, make the MIME type dynamic or validate the format. Otherwise, document the PNG-only assumption.

Suggested change
const imageBytes = generatedImage.image.imageBytes;
const imageUrl = `data:image/png;base64,${imageBytes}`;
return { imageUrl };
const imageBytes = generatedImage.image.imageBytes;
// Determine MIME type dynamically if available, otherwise assume PNG
let mimeType = 'image/png';
if (generatedImage.image.mimeType) {
mimeType = generatedImage.image.mimeType;
} else {
// If only PNG is supported, document this assumption
// Assumes generated images are always PNG. Update this if other formats are supported.
}
const imageUrl = `data:${mimeType};base64,${imageBytes}`;
return { imageUrl };

default: '1:1',
enum: ['1:1', '16:9', '9:16', '3:4', '4:3'],
},
prompt: { default: '' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The 'prompt' parameter in imagenBaseParameters lacks validation or description.

Add a description and validation, such as minLength, to the 'prompt' parameter for better schema clarity and validation.

Suggested change
prompt: { default: '' },
prompt: {
default: '',
description: 'The text prompt describing the image to generate.',
minLength: 1,
},

Comment on lines 19 to 23
export interface CreateImageErrorPayload {
[key: string]: any;
endpoint?: string;
error: object;
errorType: ILobeAgentRuntimeErrorType;
provider: string;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The CreateImageErrorPayload interface allows arbitrary keys, which may reduce type safety.

Consider limiting the interface to specific fields or clearly documenting its structure to improve maintainability and type safety.

Suggested change
export interface CreateImageErrorPayload {
[key: string]: any;
endpoint?: string;
error: object;
errorType: ILobeAgentRuntimeErrorType;
provider: string;
}
export interface CreateImageErrorPayload {
endpoint?: string;
error: object;
errorType: ILobeAgentRuntimeErrorType;
provider: string;
}

Copy link

codecov bot commented Jul 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.33%. Comparing base (0b1557d) to head (723bb79).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8503      +/-   ##
==========================================
+ Coverage   85.31%   85.33%   +0.01%     
==========================================
  Files         908      908              
  Lines       68547    68609      +62     
  Branches     4434     4672     +238     
==========================================
+ Hits        58483    58545      +62     
  Misses      10064    10064              
Flag Coverage Δ
app 85.33% <100.00%> (+0.01%) ⬆️
server 96.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- Introduced `createImage` method in `LobeGoogleAI` for generating images based on user prompts and aspect ratios.
- Added error handling for various scenarios in image generation.
- Updated model configurations to include new image models in `google.ts`.
- Enhanced test coverage for image generation functionality in `index.test.ts`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌠 Feature Request New feature or request | 特性与建议 size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants