feat(kol): add SSE streaming for prompt assist
- Extract assist runtime (prompt building, response parsing, finalize) from the worker into kol_assist_runtime so the sync streaming path and the async task path share the same logic. - Add POST /kol/manage/assist/stream as a Server-Sent Events endpoint emitting start/delta/completed/error events, and wire it to a streamAssist helper on kolManageApi that reports ApiClientError with the server's error envelope. - Stream generated content live in KolPromptEditor and KolGenerateView, switching KolPromptEditArea to a boolean aiBusy flag and refining the generator page layout. - Add KolAssistStreamEvent to shared types. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, h, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ExperimentOutlined, ThunderboltOutlined, LoadingOutlined } from "@ant-design/icons-vue";
|
||||
import { ExperimentOutlined, ThunderboltOutlined } from "@ant-design/icons-vue";
|
||||
import type { KolVariableDefinition, KolVariableType } from "@geo/shared-types";
|
||||
import { Modal } from "ant-design-vue";
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
const props = defineProps<{
|
||||
content: string;
|
||||
variables: KolVariableDefinition[];
|
||||
aiTaskId: string | null;
|
||||
aiBusy: boolean;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -37,6 +37,12 @@ type PointerSelectionState = {
|
||||
startY: number;
|
||||
};
|
||||
|
||||
type OverlaySelectionRange = {
|
||||
start: number;
|
||||
end: number;
|
||||
direction: "forward" | "backward";
|
||||
};
|
||||
|
||||
let pointerSelectionState: PointerSelectionState | null = null;
|
||||
|
||||
const editorPlaceholder = "在这里输入提示词内容,可以使用 {{变量名}} 引用左侧定义的变量";
|
||||
@@ -160,7 +166,18 @@ function handleHighlightPointerUp(event: PointerEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasOverlaySelection(highlight)) {
|
||||
const overlaySelection = getOverlaySelectionRange(highlight);
|
||||
if (overlaySelection) {
|
||||
const scrollTop = textarea.scrollTop;
|
||||
const scrollLeft = textarea.scrollLeft;
|
||||
|
||||
textarea.setSelectionRange(
|
||||
overlaySelection.start,
|
||||
overlaySelection.end,
|
||||
overlaySelection.direction,
|
||||
);
|
||||
focusTextareaWithoutScroll(textarea);
|
||||
restoreTextareaScroll(textarea, scrollTop, scrollLeft);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -210,6 +227,29 @@ function hasOverlaySelection(container: HTMLElement): boolean {
|
||||
return !!anchorNode && !!focusNode && container.contains(anchorNode) && container.contains(focusNode);
|
||||
}
|
||||
|
||||
function getOverlaySelectionRange(container: HTMLElement): OverlaySelectionRange | null {
|
||||
if (!hasOverlaySelection(container)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const selection = window.getSelection();
|
||||
if (!selection) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const anchorIndex = resolveCaretIndex(container, selection.anchorNode, selection.anchorOffset);
|
||||
const focusIndex = resolveCaretIndex(container, selection.focusNode, selection.focusOffset);
|
||||
if (anchorIndex === null || focusIndex === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
start: Math.min(anchorIndex, focusIndex),
|
||||
end: Math.max(anchorIndex, focusIndex),
|
||||
direction: anchorIndex <= focusIndex ? "forward" : "backward",
|
||||
};
|
||||
}
|
||||
|
||||
function getCaretIndexFromPoint(container: HTMLElement, event: PointerEvent): number {
|
||||
const browserIndex = getBrowserCaretIndex(container, event);
|
||||
if (browserIndex !== null) {
|
||||
@@ -433,7 +473,7 @@ function escapeAttribute(value: string): string {
|
||||
v-model:value="aiDescription"
|
||||
class="ai-input"
|
||||
:placeholder="t('kol.manage.editor.aiPlaceholder')"
|
||||
:disabled="!!aiTaskId"
|
||||
:disabled="aiBusy"
|
||||
@press-enter="handleAiGenerate"
|
||||
>
|
||||
<template #prefix>
|
||||
@@ -443,7 +483,7 @@ function escapeAttribute(value: string): string {
|
||||
<a-button
|
||||
type="primary"
|
||||
class="ai-btn"
|
||||
:loading="!!aiTaskId"
|
||||
:loading="aiBusy"
|
||||
@click="handleAiGenerate"
|
||||
>
|
||||
<template #icon><ThunderboltOutlined /></template>
|
||||
@@ -451,20 +491,15 @@ function escapeAttribute(value: string): string {
|
||||
</a-button>
|
||||
<a-button
|
||||
class="ai-btn"
|
||||
:disabled="!content || !!aiTaskId"
|
||||
:disabled="!content || aiBusy"
|
||||
@click="emit('ai-optimize')"
|
||||
>
|
||||
{{ t('kol.manage.editor.aiOptimize') }}
|
||||
</a-button>
|
||||
<span v-if="aiBusy" class="ai-status-text">AI 正在处理中...</span>
|
||||
</div>
|
||||
|
||||
<div class="editor-container">
|
||||
<div v-if="aiTaskId" class="ai-loading-overlay">
|
||||
<div class="loading-content">
|
||||
<LoadingOutlined class="loading-icon" />
|
||||
<span>AI 正在思考中...</span>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
ref="highlightRef"
|
||||
class="prompt-highlight"
|
||||
@@ -512,6 +547,14 @@ function escapeAttribute(value: string): string {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.ai-status-text {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
color: #722ed1;
|
||||
font-size: 13px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.editor-container {
|
||||
flex: 1;
|
||||
position: relative;
|
||||
@@ -576,28 +619,4 @@ function escapeAttribute(value: string): string {
|
||||
color: #bfbfbf;
|
||||
}
|
||||
|
||||
.ai-loading-overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(255, 255, 255, 0.7);
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.loading-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
color: #722ed1;
|
||||
}
|
||||
|
||||
.loading-icon {
|
||||
font-size: 32px;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user