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:
2026-04-18 16:17:11 +08:00
parent 79c65c1da7
commit 4bbce5f083
13 changed files with 1062 additions and 234 deletions
@@ -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>
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, nextTick, reactive, ref, watch } from "vue";
import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
import { message } from "ant-design-vue";
@@ -10,7 +10,7 @@ import KolVariableConfig from "./KolVariableConfig.vue";
import KolPromptEditArea from "./KolPromptEditArea.vue";
import KolDynamicForm from "./KolDynamicForm.vue";
import { kolManageApi } from "@/lib/api";
import type { KolCardConfig, KolVariableDefinition } from "@geo/shared-types";
import type { KolAssistRequest, KolAssistStreamEvent, KolCardConfig, KolVariableDefinition } from "@geo/shared-types";
import { formatError } from "@/lib/errors";
import { buildKolPlatformOptions } from "@/lib/kol-platform-options";
import { mergeKolCardConfig, parseKolCardConfig } from "@/lib/kol-card-config";
@@ -35,7 +35,7 @@ const content = ref("");
const variables = ref<KolVariableDefinition[]>([]);
const previewOpen = ref(false);
const previewValues = ref<Record<string, any>>({});
const aiTaskId = ref<string | null>(null);
const aiBusy = ref(false);
const cardConfig = ref<KolCardConfig>({});
const variableConfigRef = ref<{
focusVariable: (key: string, options?: { focusInput?: boolean }) => void;
@@ -106,57 +106,120 @@ const saveMutation = useMutation({
},
});
const { data: assistTask } = useQuery({
queryKey: computed(() => ["kol", "assist", aiTaskId.value]),
queryFn: () => kolManageApi.getAssist(aiTaskId.value!),
enabled: computed(() => !!aiTaskId.value),
refetchInterval: (query) => {
const status = query.state.data?.status;
return status === "completed" || status === "failed" ? false : 2000;
},
});
let aiAssistAbortController: AbortController | null = null;
watch(
() => assistTask.value,
(task) => {
if (task?.status === "completed" && task.result) {
content.value = task.result.content;
variables.value = syncKolVariablesWithContent(task.result.content, task.result.schema.variables);
aiTaskId.value = null;
message.success("AI 处理完成");
} else if (task?.status === "failed") {
aiTaskId.value = null;
message.error(task.error_message || "AI 处理失败");
}
function applyAssistResult(event: KolAssistStreamEvent) {
const nextContent = (event.content ?? "").trim();
if (!nextContent) {
return;
}
);
async function handleAiGenerate(description: string) {
content.value = nextContent;
variables.value = syncKolVariablesWithContent(
nextContent,
event.schema?.variables ?? variables.value,
);
}
function handleAssistStreamEvent(event: KolAssistStreamEvent): "completed" | "error" | null {
if (event.type === "start") {
aiBusy.value = true;
return null;
}
if (event.type === "delta") {
aiBusy.value = true;
if (typeof event.content === "string") {
content.value = event.content;
} else if (typeof event.delta === "string" && event.delta) {
content.value += event.delta;
}
return null;
}
if (event.type === "completed") {
aiBusy.value = false;
applyAssistResult(event);
message.success("AI 处理完成");
return "completed";
}
if (event.type === "error") {
aiBusy.value = false;
if (typeof event.content === "string" && event.content.trim()) {
content.value = event.content;
}
message.error(event.error || "AI 处理失败");
return "error";
}
return null;
}
function stopAiAssistStream() {
aiAssistAbortController?.abort();
aiAssistAbortController = null;
aiBusy.value = false;
}
async function runAiAssistStream(payload: KolAssistRequest) {
stopAiAssistStream();
const controller = new AbortController();
aiAssistAbortController = controller;
aiBusy.value = true;
let terminalState: "completed" | "error" | null = null;
try {
const res = await kolManageApi.submitAssist({
mode: "generate",
description,
prompt_id: props.promptId,
});
aiTaskId.value = res.task_id;
await kolManageApi.streamAssist(
payload,
{
onEvent(event) {
terminalState = handleAssistStreamEvent(event);
},
onClose() {
if (aiAssistAbortController === controller && terminalState === null) {
aiBusy.value = false;
}
},
},
controller.signal,
);
if (!controller.signal.aborted && terminalState === null) {
aiBusy.value = false;
message.error("AI 处理失败");
}
} catch (error) {
if (controller.signal.aborted) {
return;
}
aiBusy.value = false;
message.error(formatError(error));
} finally {
if (aiAssistAbortController === controller) {
aiAssistAbortController = null;
}
}
}
async function handleAiGenerate(description: string) {
await runAiAssistStream({
mode: "generate",
description,
prompt_id: props.promptId,
});
}
async function handleAiOptimize() {
try {
const syncedVariables = syncVariablesFromContent();
const res = await kolManageApi.submitAssist({
mode: "optimize",
current_content: content.value,
schema: { variables: syncedVariables },
prompt_id: props.promptId,
});
aiTaskId.value = res.task_id;
} catch (error) {
message.error(formatError(error));
}
const syncedVariables = syncVariablesFromContent();
await runAiAssistStream({
mode: "optimize",
current_content: content.value,
schema: { variables: syncedVariables },
prompt_id: props.promptId,
});
}
function buildPromptPayload(nextVariables: KolVariableDefinition[] = variables.value) {
@@ -253,6 +316,10 @@ function statusLabel(status?: string): string {
if (status === "archived") return t("kol.manage.status.archived");
return t("kol.manage.status.draft");
}
onBeforeUnmount(() => {
stopAiAssistStream();
});
</script>
<template>
@@ -331,7 +398,7 @@ function statusLabel(status?: string): string {
<KolPromptEditArea
v-model:content="content"
v-model:variables="variables"
:ai-task-id="aiTaskId"
:ai-busy="aiBusy"
@ai-generate="handleAiGenerate"
@ai-optimize="handleAiOptimize"
@focus-variable="handleFocusVariable"