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,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"
|
||||
|
||||
Reference in New Issue
Block a user