feat: unify creation agent chat fill

This commit is contained in:
2026-04-25 10:50:19 +08:00
parent c06bf84d0a
commit 31f350d499
26 changed files with 540 additions and 153 deletions

View File

@@ -212,6 +212,7 @@ pub async fn submit_puzzle_agent_message(
PuzzleAgentTurnRequest {
llm_client: state.llm_client(),
session: &submitted_session,
quick_fill_requested: payload.quick_fill_requested.unwrap_or(false),
},
|_| {},
)
@@ -277,6 +278,7 @@ pub async fn stream_puzzle_agent_message(
)?;
let owner_user_id = authenticated.claims().user_id().to_string();
let quick_fill_requested = payload.quick_fill_requested.unwrap_or(false);
let session = state
.spacetime_client()
.submit_puzzle_agent_message(PuzzleAgentMessageSubmitRecordInput {
@@ -304,6 +306,7 @@ pub async fn stream_puzzle_agent_message(
PuzzleAgentTurnRequest {
llm_client: state.llm_client(),
session: &session,
quick_fill_requested,
},
move |text| {
let _ = reply_tx.send(text.to_string());
@@ -1505,13 +1508,17 @@ struct GeneratedPuzzleAssetResponse {
asset_id: String,
}
fn require_puzzle_dashscope_settings(state: &AppState) -> Result<PuzzleDashScopeSettings, AppError> {
fn require_puzzle_dashscope_settings(
state: &AppState,
) -> Result<PuzzleDashScopeSettings, AppError> {
let base_url = state.config.dashscope_base_url.trim().trim_end_matches('/');
if base_url.is_empty() {
return Err(AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "dashscope",
"reason": "DASHSCOPE_BASE_URL 未配置",
})));
return Err(
AppError::from_status(StatusCode::SERVICE_UNAVAILABLE).with_details(json!({
"provider": "dashscope",
"reason": "DASHSCOPE_BASE_URL 未配置",
})),
);
}
let api_key = state
@@ -1600,7 +1607,9 @@ async fn create_puzzle_text_to_image_generation(
}))
.send()
.await
.map_err(|error| map_puzzle_dashscope_request_error(format!("创建拼图图片生成任务失败:{error}")))?;
.map_err(|error| {
map_puzzle_dashscope_request_error(format!("创建拼图图片生成任务失败:{error}"))
})?;
let status = response.status();
let response_text = response.text().await.map_err(|error| {
map_puzzle_dashscope_request_error(format!("读取拼图图片生成响应失败:{error}"))
@@ -1642,7 +1651,8 @@ async fn create_puzzle_text_to_image_generation(
"查询拼图图片生成任务失败",
));
}
let poll_payload = parse_puzzle_json_payload(poll_text.as_str(), "解析拼图图片生成任务响应失败")?;
let poll_payload =
parse_puzzle_json_payload(poll_text.as_str(), "解析拼图图片生成任务响应失败")?;
let task_status = find_first_puzzle_string_by_key(&poll_payload, "task_status")
.unwrap_or_default()
.trim()
@@ -1650,13 +1660,18 @@ async fn create_puzzle_text_to_image_generation(
if task_status == "SUCCEEDED" {
let image_urls = extract_puzzle_image_urls(&poll_payload);
if image_urls.is_empty() {
return Err(AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成成功但未返回图片地址",
})));
return Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成成功但未返回图片地址",
})),
);
}
let mut images = Vec::with_capacity(image_urls.len());
for image_url in image_urls.into_iter().take(candidate_count.clamp(1, 2) as usize) {
for image_url in image_urls
.into_iter()
.take(candidate_count.clamp(1, 2) as usize)
{
images.push(download_puzzle_remote_image(http_client, image_url.as_str()).await?);
}
return Ok(PuzzleGeneratedImages { task_id, images });
@@ -1670,10 +1685,12 @@ async fn create_puzzle_text_to_image_generation(
sleep(Duration::from_secs(2)).await;
}
Err(AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成超时或未返回图片地址",
})))
Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "拼图图片生成超时或未返回图片地址",
})),
)
}
async fn download_puzzle_remote_image(
@@ -1694,11 +1711,13 @@ async fn download_puzzle_remote_image(
map_puzzle_dashscope_request_error(format!("读取拼图正式图片内容失败:{error}"))
})?;
if !status.is_success() {
return Err(AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "下载拼图正式图片失败",
"status": status.as_u16(),
})));
return Err(
AppError::from_status(StatusCode::BAD_GATEWAY).with_details(json!({
"provider": "dashscope",
"message": "下载拼图正式图片失败",
"status": status.as_u16(),
})),
);
}
let mime_type = normalize_puzzle_downloaded_image_mime_type(content_type.as_str());
Ok(PuzzleDownloadedImage {