generated from nhcarrigan/template
feat: add user-selectable aspect ratio and resolution per thread (#18)
## Summary - Adds a two-step thread creation modal — step 1 picks the mode, step 2 configures generation options - Art mode now supports user-selectable aspect ratio (1:1, 4:3, 3:4, 16:9, 9:16, 21:9) - All three modes (Art, Avatar, Replace) now support user-selectable resolution (1K, 2K, 4K) - Mode label in the input area reflects the chosen settings (e.g. `🩷 Art Mode (16:9) · 4K`) - Backend cost calculation now scales with resolution - Regenerated `icon.ico` with clean BMP-only entries to fix Windows local builds ✨ This PR was created with help from Hikari~ 🌸 Reviewed-on: #18 Co-authored-by: Hikari <hikari@nhcarrigan.com> Co-committed-by: Hikari <hikari@nhcarrigan.com>
This commit was merged in pull request #18.
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 364 KiB After Width: | Height: | Size: 298 KiB |
+42
-11
@@ -46,12 +46,19 @@ fn build_safety_settings() -> Value {
|
||||
])
|
||||
}
|
||||
|
||||
fn build_generation_config(mode: &str) -> Value {
|
||||
fn build_generation_config(
|
||||
mode: &str,
|
||||
aspect_ratio: Option<&str>,
|
||||
image_size: &str,
|
||||
) -> Value {
|
||||
let image_config = match mode {
|
||||
"avatar" => json!({ "aspectRatio": "1:1", "imageSize": "4K" }),
|
||||
"art" => json!({ "aspectRatio": "16:9", "imageSize": "4K" }),
|
||||
"avatar" => json!({ "aspectRatio": "1:1", "imageSize": image_size }),
|
||||
"art" => {
|
||||
let ratio = aspect_ratio.unwrap_or("16:9");
|
||||
json!({ "aspectRatio": ratio, "imageSize": image_size })
|
||||
}
|
||||
// replace mode: omit aspectRatio so the model infers it from the source image
|
||||
_ => json!({ "imageSize": "4K" }),
|
||||
_ => json!({ "imageSize": image_size }),
|
||||
};
|
||||
json!({
|
||||
"imageConfig": image_config,
|
||||
@@ -108,16 +115,31 @@ fn build_user_gemini_parts(
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GeminiCallParams {
|
||||
pub mode: String,
|
||||
pub aspect_ratio: Option<String>,
|
||||
pub image_size: String,
|
||||
pub user_text: Option<String>,
|
||||
pub user_image_base64: Option<String>,
|
||||
pub user_image_mime: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn call_gemini(
|
||||
api_key: String,
|
||||
mode: String,
|
||||
history: Vec<ThreadMessage>,
|
||||
user_text: Option<String>,
|
||||
user_image_base64: Option<String>,
|
||||
user_image_mime: Option<String>,
|
||||
params: GeminiCallParams,
|
||||
) -> Result<(Vec<MessagePart>, f64), String> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let GeminiCallParams {
|
||||
mode,
|
||||
aspect_ratio,
|
||||
image_size,
|
||||
user_text,
|
||||
user_image_base64,
|
||||
user_image_mime,
|
||||
} = params;
|
||||
|
||||
let is_first_message = history.is_empty();
|
||||
|
||||
let mut contents: Vec<Value> = history
|
||||
@@ -160,7 +182,11 @@ pub async fn call_gemini(
|
||||
|
||||
contents.push(json!({"role": "user", "parts": user_parts}));
|
||||
|
||||
let generation_config = build_generation_config(mode.as_str());
|
||||
let generation_config = build_generation_config(
|
||||
mode.as_str(),
|
||||
aspect_ratio.as_deref(),
|
||||
image_size.as_str(),
|
||||
);
|
||||
let safety_settings = build_safety_settings();
|
||||
|
||||
let request_body = json!({
|
||||
@@ -252,8 +278,13 @@ pub async fn call_gemini(
|
||||
let candidates_tokens = usage["candidatesTokenCount"].as_u64().unwrap_or(0);
|
||||
let image_part_count = result_parts.iter().filter(|p| p.part_type == "image").count() as u64;
|
||||
|
||||
// Image output tokens (4K = 2000 tokens each) billed at $120/1M tokens
|
||||
let image_output_tokens = image_part_count * 2_000_u64;
|
||||
// Image output tokens per image vary by resolution, billed at $120/1M tokens
|
||||
let tokens_per_image: u64 = match image_size.as_str() {
|
||||
"1K" => 500,
|
||||
"2K" => 1_000,
|
||||
_ => 2_000, // 4K default
|
||||
};
|
||||
let image_output_tokens = image_part_count * tokens_per_image;
|
||||
// Remaining candidates tokens are text/thinking, billed at $12/1M tokens
|
||||
let text_output_tokens = candidates_tokens.saturating_sub(image_output_tokens);
|
||||
|
||||
|
||||
+17
-3
@@ -1,7 +1,7 @@
|
||||
mod gemini;
|
||||
mod storage;
|
||||
|
||||
use gemini::{call_gemini, read_reference_image_base64};
|
||||
use gemini::{call_gemini, read_reference_image_base64, GeminiCallParams};
|
||||
use serde::Serialize;
|
||||
use storage::{
|
||||
delete_thread_from_disk, load_config_from_disk, load_threads_from_disk, save_config_to_disk,
|
||||
@@ -46,16 +46,30 @@ async fn save_config(config: Config) -> Result<(), String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn send_message(
|
||||
api_key: String,
|
||||
mode: String,
|
||||
aspect_ratio: Option<String>,
|
||||
image_size: String,
|
||||
history: Vec<ThreadMessage>,
|
||||
user_text: Option<String>,
|
||||
user_image_base64: Option<String>,
|
||||
user_image_mime: Option<String>,
|
||||
) -> Result<SendMessageResult, String> {
|
||||
let (parts, cost_usd) =
|
||||
call_gemini(api_key, mode, history, user_text, user_image_base64, user_image_mime).await?;
|
||||
let (parts, cost_usd) = call_gemini(
|
||||
api_key,
|
||||
history,
|
||||
GeminiCallParams {
|
||||
mode,
|
||||
aspect_ratio,
|
||||
image_size,
|
||||
user_text,
|
||||
user_image_base64,
|
||||
user_image_mime,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(SendMessageResult { parts, cost_usd })
|
||||
}
|
||||
|
||||
|
||||
@@ -25,11 +25,19 @@ pub struct ThreadMessage {
|
||||
pub cost_usd: Option<f64>,
|
||||
}
|
||||
|
||||
fn default_image_size() -> String {
|
||||
"4K".to_string()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Thread {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub mode: String,
|
||||
#[serde(rename = "aspectRatio", skip_serializing_if = "Option::is_none")]
|
||||
pub aspect_ratio: Option<String>,
|
||||
#[serde(rename = "imageSize", default = "default_image_size")]
|
||||
pub image_size: String,
|
||||
pub messages: Vec<ThreadMessage>,
|
||||
#[serde(rename = "createdAt")]
|
||||
pub created_at: i64,
|
||||
|
||||
Reference in New Issue
Block a user