|
341 | 341 | abortController.value = null |
342 | 342 | isLoading.value = false |
343 | 343 | isGenerating.value = false |
344 | | - console.log('已手动中止生成') |
| 344 | + processingStatus.value = '' |
345 | 345 | } |
346 | 346 | } |
347 | 347 |
|
348 | 348 | // 服务端模式 |
349 | 349 | const serverMode = ref(false) |
350 | 350 |
|
351 | | - // 生图功能 |
352 | | - const isGeneratingImage = ref(false) |
353 | | -
|
354 | 351 | const sendMessage = async () => { |
355 | 352 | if (!canSendMessage.value || isLoading.value) return |
356 | 353 | generateNewRequestId() |
|
377 | 374 | chatMessages.value.push(userMessage) |
378 | 375 |
|
379 | 376 | // 清空输入并滚动到底部 |
380 | | - userInput.value = '' |
| 377 | + userInput.value = '' |
381 | 378 |
|
382 | 379 | setTimeout(() => { |
383 | 380 | scrollToBottom() |
|
387 | 384 | if (hasImage && !isImageGeneration) { |
388 | 385 | processingStatus.value = '正在编辑图片...' |
389 | 386 | try { |
390 | | - // 调用图片编辑API |
| 387 | + // 创建AbortController用于中止图片编辑请求 |
| 388 | + abortController.value = new AbortController() |
391 | 389 | const imageUrl = await handleImageWithKontextPro( |
392 | 390 | messageText || '请编辑这张图片', |
393 | | - previewImage.value |
| 391 | + previewImage.value, |
| 392 | + abortController.value.signal |
394 | 393 | ) |
395 | 394 |
|
| 395 | + // 如果控制器已中止,不继续处理 |
| 396 | + if (abortController.value?.signal.aborted) { |
| 397 | + isLoading.value = false |
| 398 | + isGenerating.value = false |
| 399 | + processingStatus.value = '' |
| 400 | + return |
| 401 | + } |
396 | 402 | // Image对象预加载图片 |
397 | 403 | const img = new Image() |
398 | 404 | // Promise等待图片加载完成 |
|
404 | 410 | // 图片加载成功后,添加带图片的消息 |
405 | 411 | const assistantMessage = { |
406 | 412 | role: 'assistant' as const, |
407 | | - content: '已为您编辑图片,点击LoadImage节点可以直接应用。', |
| 413 | + content: serverMode.value ? '已为您编辑图片' : '已为您编辑图片,点击LoadImage节点可以直接应用。', |
408 | 414 | time: getCurrentTime(), |
409 | 415 | hasImage: true, |
410 | 416 | image: imageUrl |
|
424 | 430 | } catch (error: any) { |
425 | 431 | isLoading.value = false |
426 | 432 | isGenerating.value = false |
427 | | - processingStatus.value = '' |
| 433 | + processingStatus.value = '' |
428 | 434 | return |
429 | 435 | } |
430 | 436 | } |
431 | | - // 判断是否是图片生成请求 |
432 | | - if (isImageGeneration) { |
433 | | - isGeneratingImage.value = true |
434 | | - processingStatus.value = '正在生成图片...' |
435 | | -
|
436 | | - // 提取提示词 |
437 | | - const prompt = messageText.replace('生成图片:', '').trim() || '一张漂亮的图片' |
438 | | -
|
439 | | - // 调用图像生成API |
440 | | - const imageUrl = await generateImage({ |
441 | | - prompt, |
442 | | - model: 'Kwai-Kolors/Kolors', |
443 | | - loading_callback: loading => { |
444 | | - // 加载状态更新 |
445 | | - if (!loading) { |
446 | | - processingStatus.value = '' |
447 | | - } else { |
448 | | - processingStatus.value = '正在生成图片...' |
449 | | - } |
450 | | - }, |
451 | | - error_callback: error => { |
452 | | - useToaster({ |
453 | | - type: 'error', |
454 | | - message: '生成图片失败: ' + (error.message || '未知错误') |
455 | | - }) |
456 | | - } |
457 | | - }) |
458 | | -
|
459 | | - // 预加载生成的图片 |
460 | | - const img = new Image() |
461 | | - // 使用Promise等待图片加载完成 |
462 | | - await new Promise((resolve, reject) => { |
463 | | - img.onload = () => resolve(true) |
464 | | - img.onerror = () => reject(new Error('图片加载失败')) |
465 | | - img.src = imageUrl |
466 | | - }) |
467 | | -
|
468 | | - // 生成成功后,添加带图片的助手消息 |
469 | | - const assistantMessage = { |
470 | | - role: 'assistant' as const, |
471 | | - // 服务端模式下只展示"已为您生成图片" |
472 | | - content: serverMode.value |
473 | | - ? '已为您生成图片' |
474 | | - : '已为您生成图片(点击LoadImage节点可以应用)', |
475 | | - time: getCurrentTime(), |
476 | | - hasImage: true, |
477 | | - image: imageUrl |
478 | | - } |
479 | | -
|
480 | | - chatMessages.value.push(assistantMessage) |
481 | | -
|
482 | | - // 成功提示 |
483 | | - useToaster({ |
484 | | - type: 'success', |
485 | | - message: '图片生成成功' |
486 | | - }) |
487 | | -
|
488 | | - // 更新状态 |
489 | | - isGeneratingImage.value = false |
490 | | - isLoading.value = false |
491 | | - isGenerating.value = false |
492 | | - processingStatus.value = '' |
493 | | -
|
494 | | - // 滚动到底部 |
495 | | - setTimeout(() => { |
496 | | - scrollToBottom() |
497 | | - }, 0) |
498 | | -
|
499 | | - return |
500 | | - } |
501 | | -
|
502 | 437 | // 创建AbortController用于中止请求 |
503 | 438 | abortController.value = new AbortController() |
504 | 439 |
|
|
609 | 544 | } |
610 | 545 | }, |
611 | 546 | { |
612 | | - model: 'Qwen/Qwen2.5-VL-72B-Instruct', |
| 547 | + model: 'Pro/deepseek-ai/DeepSeek-V3', |
613 | 548 | prompt_id: promptId.value, |
614 | 549 | request_id: requestId.value |
615 | 550 | } |
|
0 commit comments