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>
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createApiClient } from "@geo/http-client";
|
||||
import { ApiClientError, createApiClient } from "@geo/http-client";
|
||||
import type {
|
||||
ApiEnvelope,
|
||||
ArticleDetail,
|
||||
@@ -29,6 +29,7 @@ import type {
|
||||
KnowledgeTextItemRequest,
|
||||
KnowledgeURLItemRequest,
|
||||
KolAssistRequest,
|
||||
KolAssistStreamEvent,
|
||||
KolAssistSubmitResponse,
|
||||
KolAssistTask,
|
||||
KolPackageSummary,
|
||||
@@ -384,6 +385,27 @@ export const kolManageApi = {
|
||||
body,
|
||||
);
|
||||
},
|
||||
streamAssist(
|
||||
body: KolAssistRequest,
|
||||
handlers: {
|
||||
onEvent: (event: KolAssistStreamEvent) => void;
|
||||
onClose?: () => void;
|
||||
},
|
||||
signal: AbortSignal,
|
||||
) {
|
||||
return subscribeSSE<KolAssistStreamEvent>(
|
||||
"/api/tenant/kol/manage/assist/stream",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
handlers,
|
||||
signal,
|
||||
);
|
||||
},
|
||||
getAssist(id: string) {
|
||||
return apiClient.get<KolAssistTask>(`/api/tenant/kol/manage/assist/${id}`);
|
||||
},
|
||||
@@ -701,7 +723,7 @@ async function subscribeSSE<TEvent extends SSEEventShape>(
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`stream request failed with status ${response.status}`);
|
||||
throw await buildSSERequestError(response);
|
||||
}
|
||||
if (!response.body) {
|
||||
throw new Error("stream response body is empty");
|
||||
@@ -730,6 +752,44 @@ async function subscribeSSE<TEvent extends SSEEventShape>(
|
||||
}
|
||||
}
|
||||
|
||||
type SSEErrorEnvelope = {
|
||||
code?: number;
|
||||
message?: string;
|
||||
detail?: string;
|
||||
request_id?: string;
|
||||
};
|
||||
|
||||
async function buildSSERequestError(response: Response): Promise<ApiClientError> {
|
||||
try {
|
||||
const payload = (await response.clone().json()) as SSEErrorEnvelope | null;
|
||||
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
||||
return new ApiClientError({
|
||||
message: payload.message ?? "unknown_error",
|
||||
code: payload.code,
|
||||
status: response.status,
|
||||
detail: payload.detail,
|
||||
requestId: payload.request_id,
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Ignore JSON parse errors and fall back to a generic error below.
|
||||
}
|
||||
|
||||
let detail: string | undefined;
|
||||
try {
|
||||
const text = (await response.text()).trim();
|
||||
detail = text || undefined;
|
||||
} catch {
|
||||
detail = undefined;
|
||||
}
|
||||
|
||||
return new ApiClientError({
|
||||
message: "unknown_error",
|
||||
status: response.status,
|
||||
detail: detail ?? `stream request failed with status ${response.status}`,
|
||||
});
|
||||
}
|
||||
|
||||
export const brandsApi = {
|
||||
list() {
|
||||
return apiClient.get<Brand[]>("/api/tenant/brands");
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useRoute, useRouter } from "vue-router";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useMutation, useQuery } from "@tanstack/vue-query";
|
||||
import { message } from "ant-design-vue";
|
||||
import { ArrowLeftOutlined, ThunderboltOutlined } from "@ant-design/icons-vue";
|
||||
import { LeftOutlined } from "@ant-design/icons-vue";
|
||||
import { kolGenerateApi } from "@/lib/api";
|
||||
import KolDynamicForm from "@/components/kol/KolDynamicForm.vue";
|
||||
import KnowledgeGroupSelect from "@/components/KnowledgeGroupSelect.vue";
|
||||
@@ -78,11 +78,11 @@ function handleBack() {
|
||||
}
|
||||
|
||||
function handleSubmit() {
|
||||
const schema = schemaQuery.data.value;
|
||||
if (!schema?.schema_json?.variables) return;
|
||||
const schemaValue = schemaQuery.data.value;
|
||||
if (!schemaValue?.schema_json?.variables) return;
|
||||
|
||||
const missingLabels: string[] = [];
|
||||
schema.schema_json.variables.forEach((v) => {
|
||||
schemaValue.schema_json.variables.forEach((v) => {
|
||||
const normalizedVariable = normalizeKolVariableDefinition(v);
|
||||
if (normalizedVariable.required && isKolVariableValueEmpty(normalizedVariable, values.value[fieldKey(v.key, v.id)])) {
|
||||
missingLabels.push(v.label);
|
||||
@@ -99,36 +99,39 @@ function handleSubmit() {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="kol-generate-view">
|
||||
<div class="view-header">
|
||||
<a-button type="link" @click="handleBack" class="back-btn">
|
||||
<template #icon><ArrowLeftOutlined /></template>
|
||||
{{ t("common.back") }}
|
||||
</a-button>
|
||||
</div>
|
||||
|
||||
<div class="wizard-page">
|
||||
<div v-if="schemaQuery.isPending.value" class="loading-state">
|
||||
<a-spin size="large" />
|
||||
</div>
|
||||
|
||||
<div v-else-if="schema" class="view-content">
|
||||
<a-row :gutter="24" justify="center">
|
||||
<a-col :xs="24" :lg="16" :xl="12">
|
||||
<a-card class="generate-card">
|
||||
<template #title>
|
||||
<div class="card-header">
|
||||
<span class="package-name">{{ schema.package_name }}</span>
|
||||
<span class="divider">/</span>
|
||||
<span class="prompt-name">{{ schema.prompt_name }}</span>
|
||||
<a-tag v-if="schema.platform_hint" color="cyan" class="platform-badge">
|
||||
{{ schema.platform_hint }}
|
||||
</a-tag>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else-if="schema">
|
||||
<header class="wizard-page__header">
|
||||
<div class="header-left">
|
||||
<a-button type="text" shape="circle" @click="handleBack">
|
||||
<template #icon><LeftOutlined /></template>
|
||||
</a-button>
|
||||
<div class="header-title-box">
|
||||
<h2 class="header-title">
|
||||
{{ schema.package_name }}
|
||||
<span class="divider">/</span>
|
||||
{{ schema.prompt_name }}
|
||||
<a-tag v-if="schema.platform_hint" color="cyan" class="platform-badge">
|
||||
{{ schema.platform_hint }}
|
||||
</a-tag>
|
||||
</h2>
|
||||
<p class="header-eyebrow">{{ t("kol.generate.fillVariables") }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="hint-text">
|
||||
<ThunderboltOutlined class="hint-icon" />
|
||||
{{ t("kol.generate.fillVariables") }}
|
||||
<main class="wizard-page__main">
|
||||
<section class="wizard-section">
|
||||
<div class="wizard-card">
|
||||
<div class="wizard-card__header" style="margin-bottom: 24px;">
|
||||
<div>
|
||||
<h3>{{ t("kol.generate.fillVariables") }}</h3>
|
||||
<p>根据设定提供相关信息</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="allowWebSearch || allowUserKnowledge" class="capability-card">
|
||||
@@ -150,7 +153,6 @@ function handleSubmit() {
|
||||
:disabled="generateMutation.isPending.value"
|
||||
placeholder="可选,选择你自己的知识库分组辅助生成"
|
||||
/>
|
||||
<div class="capability-hint">只会使用你当前租户下可访问的知识库分组。</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -159,37 +161,32 @@ function handleSubmit() {
|
||||
:variables="schema.schema_json.variables"
|
||||
class="form-body"
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<div class="form-actions">
|
||||
<a-button
|
||||
type="primary"
|
||||
size="large"
|
||||
block
|
||||
:loading="generateMutation.isPending.value"
|
||||
@click="handleSubmit"
|
||||
>
|
||||
{{ t("kol.generate.submit") }}
|
||||
</a-button>
|
||||
</div>
|
||||
</a-card>
|
||||
</a-col>
|
||||
</a-row>
|
||||
</div>
|
||||
<footer class="wizard-page__footer">
|
||||
<div></div>
|
||||
<div class="footer-actions">
|
||||
<a-button
|
||||
type="primary"
|
||||
:loading="generateMutation.isPending.value"
|
||||
@click="handleSubmit"
|
||||
>
|
||||
{{ t("kol.generate.submit") }}
|
||||
</a-button>
|
||||
</div>
|
||||
</footer>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.kol-generate-view {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.view-header {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.back-btn {
|
||||
padding: 0;
|
||||
color: #595959;
|
||||
.wizard-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: calc(100vh - 120px);
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.loading-state {
|
||||
@@ -198,60 +195,107 @@ function handleSubmit() {
|
||||
padding: 100px;
|
||||
}
|
||||
|
||||
.generate-card {
|
||||
border-radius: 12px;
|
||||
border: 1px solid #e6edf5;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
|
||||
}
|
||||
|
||||
.card-header {
|
||||
.wizard-page__header {
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
justify-content: space-between;
|
||||
padding: 16px 32px;
|
||||
border-bottom: 1px solid #edf2f7;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.package-name {
|
||||
color: #8c8c8c;
|
||||
font-weight: 400;
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.header-title-box {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.header-title {
|
||||
margin: 0;
|
||||
color: #111827;
|
||||
font-size: 20px;
|
||||
font-weight: 800;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.divider {
|
||||
margin: 0 8px;
|
||||
color: #d9d9d9;
|
||||
}
|
||||
|
||||
.prompt-name {
|
||||
color: #1a1a1a;
|
||||
font-weight: 600;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.platform-badge {
|
||||
margin-left: 4px;
|
||||
margin-left: 8px;
|
||||
}
|
||||
|
||||
.hint-text {
|
||||
margin-bottom: 24px;
|
||||
padding: 12px 16px;
|
||||
background: #f0f7ff;
|
||||
border-radius: 8px;
|
||||
color: #003a8c;
|
||||
font-size: 14px;
|
||||
.header-eyebrow {
|
||||
margin: 0;
|
||||
color: #6d7c94;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.wizard-page__main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin: 0 auto;
|
||||
width: 100%;
|
||||
max-width: 860px;
|
||||
padding: 24px 32px 64px;
|
||||
}
|
||||
|
||||
.wizard-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.wizard-card {
|
||||
padding: 32px 0;
|
||||
}
|
||||
|
||||
.wizard-card__header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.wizard-card__header h3 {
|
||||
margin: 0;
|
||||
color: #111827;
|
||||
font-size: 16px;
|
||||
font-weight: 800;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.hint-icon {
|
||||
color: #1890ff;
|
||||
.wizard-card__header h3::before {
|
||||
content: "";
|
||||
display: inline-block;
|
||||
width: 4px;
|
||||
height: 16px;
|
||||
border-radius: 2px;
|
||||
background: #111827;
|
||||
margin-right: 12px;
|
||||
}
|
||||
|
||||
.form-body {
|
||||
margin-bottom: 24px;
|
||||
.wizard-card__header p {
|
||||
margin: 6px 0 0 16px;
|
||||
color: #667085;
|
||||
font-size: 13px;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
.capability-card {
|
||||
margin-bottom: 24px;
|
||||
padding: 16px;
|
||||
margin-bottom: 32px;
|
||||
padding: 20px;
|
||||
border: 1px solid #e6edf5;
|
||||
border-radius: 12px;
|
||||
background: #fafcff;
|
||||
@@ -265,7 +309,7 @@ function handleSubmit() {
|
||||
}
|
||||
|
||||
.capability-knowledge {
|
||||
margin-top: 16px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.capability-copy {
|
||||
@@ -286,7 +330,43 @@ function handleSubmit() {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
margin-top: 32px;
|
||||
.wizard-page__footer {
|
||||
position: sticky;
|
||||
bottom: 0;
|
||||
z-index: 100;
|
||||
margin-top: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 16px 24px;
|
||||
border-top: 1px solid rgba(20, 44, 88, 0.08);
|
||||
backdrop-filter: blur(14px);
|
||||
background: rgba(255, 255, 255, 0.88);
|
||||
}
|
||||
|
||||
.footer-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.wizard-page__main {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.wizard-card {
|
||||
padding: 18px 0;
|
||||
}
|
||||
|
||||
.wizard-page__footer {
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.footer-actions {
|
||||
width: 100%;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -2545,7 +2545,7 @@ function onStructureDragEnd(): void {
|
||||
.wizard-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: calc(100vh - 64px);
|
||||
min-height: calc(100vh - 120px);
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ import { articlesApi, templatesApi, workspaceApi } from "@/lib/api";
|
||||
import { buildArticleClipboardContent, resolveArticleActionState } from "@/lib/article-list-actions";
|
||||
import { formatError } from "@/lib/errors";
|
||||
import {
|
||||
formatDateTime,
|
||||
getGenerateStatusMeta,
|
||||
getPublishStatusMeta,
|
||||
getTemplateMeta,
|
||||
@@ -689,6 +690,9 @@ function refreshRecords(): void {
|
||||
<h4 class="templates-view__kol-title" :title="card.prompt_name">{{ card.prompt_name }}</h4>
|
||||
<div class="templates-view__kol-meta-row">
|
||||
<span class="templates-view__kol-prompt" :title="card.package_name">{{ card.package_name }}</span>
|
||||
<span class="templates-view__kol-updated-at">
|
||||
{{ t("common.updatedAt") }}: {{ formatDateTime(card.updated_at) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="templates-view__kol-footer">
|
||||
@@ -1171,9 +1175,9 @@ function refreshRecords(): void {
|
||||
|
||||
.templates-view__kol-meta-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
gap: 6px;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
@@ -1188,6 +1192,12 @@ function refreshRecords(): void {
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.templates-view__kol-updated-at {
|
||||
font-size: 12px;
|
||||
color: #9ca3af;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.templates-view__kol-footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
|
||||
@@ -27,8 +27,8 @@ import ArticleSourceMeta from "@/components/ArticleSourceMeta.vue";
|
||||
import ArticleDetailDrawer from "@/components/ArticleDetailDrawer.vue";
|
||||
import PublishArticleModal from "@/components/PublishArticleModal.vue";
|
||||
import { articlesApi, workspaceApi } from "@/lib/api";
|
||||
import { getTemplateMeta } from "@/lib/display";
|
||||
import { buildArticleClipboardContent, resolveArticleActionState } from "@/lib/article-list-actions";
|
||||
import { formatDateTime, getTemplateMeta } from "@/lib/display";
|
||||
import { formatError } from "@/lib/errors";
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
@@ -303,6 +303,9 @@ function refreshDashboard(): void {
|
||||
<h4 class="kol-card-title" :title="card.prompt_name">{{ card.prompt_name }}</h4>
|
||||
<div class="kol-card-meta-row">
|
||||
<span class="kol-card-prompt" :title="card.package_name">{{ card.package_name }}</span>
|
||||
<span class="kol-card-updated-at">
|
||||
{{ t("common.updatedAt") }}: {{ formatDateTime(card.updated_at) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="kol-card-footer">
|
||||
@@ -747,9 +750,9 @@ function refreshDashboard(): void {
|
||||
|
||||
.kol-card-meta-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
gap: 6px;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
@@ -764,6 +767,12 @@ function refreshDashboard(): void {
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.kol-card-updated-at {
|
||||
font-size: 12px;
|
||||
color: #9ca3af;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.kol-card-footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
|
||||
@@ -1331,6 +1331,18 @@ export interface KolAssistTask {
|
||||
error_message?: string;
|
||||
}
|
||||
|
||||
export interface KolAssistStreamEvent {
|
||||
type: "start" | "delta" | "completed" | "error";
|
||||
mode?: "generate" | "optimize";
|
||||
status?: "generating" | "completed" | "failed";
|
||||
delta?: string;
|
||||
content?: string;
|
||||
schema?: KolSchemaJson;
|
||||
error?: string;
|
||||
model?: string;
|
||||
done?: boolean;
|
||||
}
|
||||
|
||||
export interface KolDashboardOverview {
|
||||
total_subscriber_tenants: number;
|
||||
month_new_subscriptions: number;
|
||||
|
||||
@@ -0,0 +1,423 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/shared/auth"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/llm"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/response"
|
||||
)
|
||||
|
||||
const kolAssistSystemPrompt = "You are a prompt designer for marketing content. Respond ONLY with JSON. Output the `content` field first. Do not wrap in markdown fences."
|
||||
|
||||
var kolAssistResponseSchema = []byte(`{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["content"],
|
||||
"properties": {
|
||||
"content": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
func (s *KolAssistService) Run(
|
||||
ctx context.Context,
|
||||
actor auth.Actor,
|
||||
req AssistRequest,
|
||||
onDelta func(string),
|
||||
) (*KolAssistResult, string, error) {
|
||||
if _, err := s.profileSvc.RequireActiveKol(ctx, actor); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if err := s.llm.Validate(); err != nil {
|
||||
return nil, "", response.ErrServiceUnavailable(50341, "llm_unavailable", err.Error())
|
||||
}
|
||||
|
||||
req, _, err := normalizeAssistRequest(req)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
generateReq, err := BuildKolAssistGenerateRequest(req)
|
||||
if err != nil {
|
||||
return nil, "", response.ErrInternal(50087, "kol_assist_prompt_build_failed", err.Error())
|
||||
}
|
||||
|
||||
streamAccumulator := newKolAssistContentStream(onDelta)
|
||||
generated, err := s.llm.Generate(ctx, generateReq, streamAccumulator.ConsumeRawDelta)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
result, err := ParseKolAssistResponse(generated.Content)
|
||||
if err != nil {
|
||||
return nil, "", response.ErrInternal(50086, "kol_assist_result_invalid", err.Error())
|
||||
}
|
||||
result, err = FinalizeKolAssistResult(req, result)
|
||||
if err != nil {
|
||||
return nil, "", response.ErrInternal(50086, "kol_assist_result_invalid", err.Error())
|
||||
}
|
||||
|
||||
return result, generated.Model, nil
|
||||
}
|
||||
|
||||
func BuildKolAssistGenerateRequest(req AssistRequest) (llm.GenerateRequest, error) {
|
||||
userPrompt, err := BuildKolAssistUserPrompt(req)
|
||||
if err != nil {
|
||||
return llm.GenerateRequest{}, err
|
||||
}
|
||||
|
||||
return llm.GenerateRequest{
|
||||
Prompt: fmt.Sprintf("SYSTEM:\n%s\n\nUSER:\n%s", kolAssistSystemPrompt, userPrompt),
|
||||
ResponseFormat: &llm.ResponseFormat{
|
||||
Type: llm.ResponseFormatTypeJSONSchema,
|
||||
Name: "kol_assist_response",
|
||||
Description: "JSON object with content:string",
|
||||
SchemaJSON: kolAssistResponseSchema,
|
||||
Strict: true,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func BuildKolAssistUserPrompt(req AssistRequest) (string, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(req.Mode)) {
|
||||
case kolAssistModeGenerate:
|
||||
return req.Description, nil
|
||||
case kolAssistModeOptimize:
|
||||
schemaJSON := "null"
|
||||
if req.Schema != nil {
|
||||
payload, err := json.Marshal(req.Schema)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("encode schema: %w", err)
|
||||
}
|
||||
schemaJSON = string(payload)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"Optimize the following prompt. Keep variable placeholders intact. Current schema: %s\n\nPROMPT:\n%s",
|
||||
schemaJSON,
|
||||
req.CurrentContent,
|
||||
), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported assist mode %q", req.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
func ParseKolAssistResponse(content string) (*KolAssistResult, error) {
|
||||
type rawPayload struct {
|
||||
Content string `json:"content"`
|
||||
Schema json.RawMessage `json:"schema"`
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
for _, candidate := range extractJSONCandidates(content) {
|
||||
var payload rawPayload
|
||||
if err := json.Unmarshal([]byte(candidate), &payload); err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
|
||||
result := &KolAssistResult{
|
||||
Content: strings.TrimSpace(payload.Content),
|
||||
Schema: KolSchemaJSON{Variables: []KolVariableDefinition{}},
|
||||
}
|
||||
if result.Content == "" {
|
||||
lastErr = fmt.Errorf("kol assist result content is empty")
|
||||
continue
|
||||
}
|
||||
|
||||
schema, err := decodeKolAssistSchema(payload.Schema)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
result.Schema = schema
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if lastErr == nil {
|
||||
lastErr = fmt.Errorf("empty content")
|
||||
}
|
||||
return nil, fmt.Errorf("decode kol assist result: %w", lastErr)
|
||||
}
|
||||
|
||||
func FinalizeKolAssistResult(req AssistRequest, result *KolAssistResult) (*KolAssistResult, error) {
|
||||
if result == nil {
|
||||
return nil, fmt.Errorf("kol assist result is nil")
|
||||
}
|
||||
|
||||
baseSchema := result.Schema
|
||||
if req.Mode == kolAssistModeOptimize && req.Schema != nil {
|
||||
merged := make([]KolVariableDefinition, 0, len(req.Schema.Variables)+len(result.Schema.Variables))
|
||||
merged = append(merged, req.Schema.Variables...)
|
||||
merged = append(merged, result.Schema.Variables...)
|
||||
baseSchema = KolSchemaJSON{Variables: merged}
|
||||
}
|
||||
|
||||
result.Schema = SyncKolSchemaWithContent(result.Content, baseSchema)
|
||||
if err := ValidateSchema(result.Schema); err != nil {
|
||||
return nil, fmt.Errorf("invalid kol assist schema: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func SyncKolSchemaWithContent(content string, schema KolSchemaJSON) KolSchemaJSON {
|
||||
keys := extractKolPlaceholderKeys(content)
|
||||
if len(keys) == 0 {
|
||||
return KolSchemaJSON{Variables: []KolVariableDefinition{}}
|
||||
}
|
||||
|
||||
schema = normalizeKolSchema(schema)
|
||||
|
||||
byKey := make(map[string][]KolVariableDefinition, len(schema.Variables))
|
||||
for _, variable := range schema.Variables {
|
||||
key := strings.TrimSpace(variable.Key)
|
||||
if key == "" {
|
||||
key = strings.TrimSpace(variable.Label)
|
||||
}
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
byKey[key] = append(byKey[key], variable)
|
||||
}
|
||||
|
||||
usedIDs := make(map[string]struct{}, len(keys))
|
||||
variables := make([]KolVariableDefinition, 0, len(keys))
|
||||
|
||||
for _, key := range keys {
|
||||
var variable KolVariableDefinition
|
||||
if candidates := byKey[key]; len(candidates) > 0 {
|
||||
variable = candidates[0]
|
||||
byKey[key] = candidates[1:]
|
||||
} else {
|
||||
variable = KolVariableDefinition{
|
||||
Key: key,
|
||||
Label: key,
|
||||
Type: KolVariableTypeInput,
|
||||
Required: true,
|
||||
}
|
||||
}
|
||||
|
||||
if strings.TrimSpace(variable.Key) == "" {
|
||||
variable.Key = key
|
||||
}
|
||||
if strings.TrimSpace(variable.Label) == "" {
|
||||
variable.Label = key
|
||||
}
|
||||
|
||||
variable = normalizeKolVariableDefinition(variable)
|
||||
id := strings.TrimSpace(variable.ID)
|
||||
if id == "" || !strings.HasPrefix(id, "var_") {
|
||||
id = newKolVariableID()
|
||||
}
|
||||
if _, exists := usedIDs[id]; exists {
|
||||
id = newKolVariableID()
|
||||
}
|
||||
variable.ID = id
|
||||
usedIDs[id] = struct{}{}
|
||||
variables = append(variables, variable)
|
||||
}
|
||||
|
||||
return normalizeKolSchema(KolSchemaJSON{Variables: variables})
|
||||
}
|
||||
|
||||
func extractKolPlaceholderKeys(content string) []string {
|
||||
text := strings.TrimSpace(content)
|
||||
if text == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{})
|
||||
keys := make([]string, 0)
|
||||
matches := kolPlaceholderRE.FindAllStringSubmatch(text, -1)
|
||||
for _, match := range matches {
|
||||
if len(match) < 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(match[1])
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func newKolVariableID() string {
|
||||
return "var_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:8]
|
||||
}
|
||||
|
||||
type kolAssistContentStream struct {
|
||||
raw strings.Builder
|
||||
content string
|
||||
onDelta func(string)
|
||||
}
|
||||
|
||||
func newKolAssistContentStream(onDelta func(string)) *kolAssistContentStream {
|
||||
return &kolAssistContentStream{onDelta: onDelta}
|
||||
}
|
||||
|
||||
func (s *kolAssistContentStream) ConsumeRawDelta(chunk string) {
|
||||
if s == nil || s.onDelta == nil || chunk == "" {
|
||||
return
|
||||
}
|
||||
|
||||
s.raw.WriteString(chunk)
|
||||
current := extractKolAssistContentFromPartialJSON(s.raw.String())
|
||||
if current == "" || current == s.content {
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(current, s.content) {
|
||||
delta := current[len(s.content):]
|
||||
s.content = current
|
||||
if delta != "" {
|
||||
s.onDelta(delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extractKolAssistContentFromPartialJSON(raw string) string {
|
||||
keyIndex := strings.Index(raw, `"content"`)
|
||||
if keyIndex < 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
index := keyIndex + len(`"content"`)
|
||||
for index < len(raw) && isKolAssistJSONWhitespace(raw[index]) {
|
||||
index++
|
||||
}
|
||||
if index >= len(raw) || raw[index] != ':' {
|
||||
return ""
|
||||
}
|
||||
index++
|
||||
for index < len(raw) && isKolAssistJSONWhitespace(raw[index]) {
|
||||
index++
|
||||
}
|
||||
if index >= len(raw) || raw[index] != '"' {
|
||||
return ""
|
||||
}
|
||||
index++
|
||||
|
||||
var builder strings.Builder
|
||||
for index < len(raw) {
|
||||
ch := raw[index]
|
||||
if ch == '"' {
|
||||
return builder.String()
|
||||
}
|
||||
if ch != '\\' {
|
||||
builder.WriteByte(ch)
|
||||
index++
|
||||
continue
|
||||
}
|
||||
|
||||
if index+1 >= len(raw) {
|
||||
return builder.String()
|
||||
}
|
||||
escaped := raw[index+1]
|
||||
switch escaped {
|
||||
case '"', '\\', '/':
|
||||
builder.WriteByte(escaped)
|
||||
index += 2
|
||||
case 'b':
|
||||
builder.WriteByte('\b')
|
||||
index += 2
|
||||
case 'f':
|
||||
builder.WriteByte('\f')
|
||||
index += 2
|
||||
case 'n':
|
||||
builder.WriteByte('\n')
|
||||
index += 2
|
||||
case 'r':
|
||||
builder.WriteByte('\r')
|
||||
index += 2
|
||||
case 't':
|
||||
builder.WriteByte('\t')
|
||||
index += 2
|
||||
case 'u':
|
||||
if index+6 > len(raw) {
|
||||
return builder.String()
|
||||
}
|
||||
var codePoint rune
|
||||
for _, hexChar := range raw[index+2 : index+6] {
|
||||
codePoint <<= 4
|
||||
switch {
|
||||
case hexChar >= '0' && hexChar <= '9':
|
||||
codePoint += rune(hexChar - '0')
|
||||
case hexChar >= 'a' && hexChar <= 'f':
|
||||
codePoint += rune(hexChar-'a') + 10
|
||||
case hexChar >= 'A' && hexChar <= 'F':
|
||||
codePoint += rune(hexChar-'A') + 10
|
||||
default:
|
||||
return builder.String()
|
||||
}
|
||||
}
|
||||
builder.WriteRune(codePoint)
|
||||
index += 6
|
||||
default:
|
||||
return builder.String()
|
||||
}
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func isKolAssistJSONWhitespace(value byte) bool {
|
||||
return value == ' ' || value == '\n' || value == '\r' || value == '\t'
|
||||
}
|
||||
|
||||
func decodeKolAssistSchema(raw json.RawMessage) (KolSchemaJSON, error) {
|
||||
cleaned := strings.TrimSpace(string(raw))
|
||||
if cleaned == "" || cleaned == "null" {
|
||||
return KolSchemaJSON{Variables: []KolVariableDefinition{}}, nil
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
Variables []json.RawMessage `json:"variables"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(cleaned), &payload); err != nil {
|
||||
return KolSchemaJSON{}, fmt.Errorf("decode kol assist schema: %w", err)
|
||||
}
|
||||
|
||||
variables := make([]KolVariableDefinition, 0, len(payload.Variables))
|
||||
for _, item := range payload.Variables {
|
||||
if len(item) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var variable KolVariableDefinition
|
||||
if err := json.Unmarshal(item, &variable); err == nil {
|
||||
variables = append(variables, variable)
|
||||
continue
|
||||
}
|
||||
|
||||
var key string
|
||||
if err := json.Unmarshal(item, &key); err == nil {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
variables = append(variables, KolVariableDefinition{
|
||||
Key: key,
|
||||
Label: key,
|
||||
Type: KolVariableTypeInput,
|
||||
Required: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
return KolSchemaJSON{}, fmt.Errorf("decode kol assist schema variable: unsupported item %s", string(item))
|
||||
}
|
||||
|
||||
return normalizeKolSchema(KolSchemaJSON{Variables: variables}), nil
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFinalizeKolAssistResultRepairsDuplicateVariableIDs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := FinalizeKolAssistResult(
|
||||
AssistRequest{Mode: kolAssistModeGenerate},
|
||||
&KolAssistResult{
|
||||
Content: "围绕 {{城市}} 和 {{目标词}} 输出内容",
|
||||
Schema: KolSchemaJSON{
|
||||
Variables: []KolVariableDefinition{
|
||||
{ID: "var_dup", Key: "城市", Label: "城市", Type: KolVariableTypeInput, Required: true},
|
||||
{ID: "var_dup", Key: "目标词", Label: "目标词", Type: KolVariableTypeInput, Required: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Schema.Variables, 2)
|
||||
require.NotEqual(t, result.Schema.Variables[0].ID, result.Schema.Variables[1].ID)
|
||||
require.Equal(t, "城市", result.Schema.Variables[0].Key)
|
||||
require.Equal(t, "目标词", result.Schema.Variables[1].Key)
|
||||
}
|
||||
|
||||
func TestFinalizeKolAssistResultOptimizePreservesExistingSchemaByKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
originalMaxLength := 80
|
||||
|
||||
result, err := FinalizeKolAssistResult(
|
||||
AssistRequest{
|
||||
Mode: kolAssistModeOptimize,
|
||||
Schema: &KolSchemaJSON{
|
||||
Variables: []KolVariableDefinition{
|
||||
{ID: "var_city", Key: "城市", Label: "城市", Type: KolVariableTypeInput, Required: true, MaxLength: intPtr(originalMaxLength)},
|
||||
},
|
||||
},
|
||||
},
|
||||
&KolAssistResult{
|
||||
Content: "围绕 {{城市}} 优化提示词",
|
||||
Schema: KolSchemaJSON{
|
||||
Variables: []KolVariableDefinition{
|
||||
{ID: "var_dup", Key: "城市", Label: "城市", Type: KolVariableTypeTextarea, Required: false},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Schema.Variables, 1)
|
||||
require.Equal(t, "var_city", result.Schema.Variables[0].ID)
|
||||
require.Equal(t, KolVariableTypeInput, result.Schema.Variables[0].Type)
|
||||
require.NotNil(t, result.Schema.Variables[0].MaxLength)
|
||||
require.Equal(t, originalMaxLength, *result.Schema.Variables[0].MaxLength)
|
||||
}
|
||||
|
||||
func TestParseKolAssistResponseSupportsStringVariables(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := ParseKolAssistResponse(`{"content":"围绕 {{城市}} 写内容","schema":{"variables":["城市"]}}`)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "围绕 {{城市}} 写内容", result.Content)
|
||||
require.Len(t, result.Schema.Variables, 1)
|
||||
require.Equal(t, "城市", result.Schema.Variables[0].Key)
|
||||
}
|
||||
|
||||
func TestExtractKolAssistContentFromPartialJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
content := extractKolAssistContentFromPartialJSON("{\"content\":\"第一段\\n第二")
|
||||
|
||||
require.Equal(t, "第一段\n第二", content)
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -21,6 +24,17 @@ type KolManageHandler struct {
|
||||
assets *AssetHandler
|
||||
}
|
||||
|
||||
type kolAssistStreamEvent struct {
|
||||
Mode string `json:"mode,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Delta string `json:"delta,omitempty"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Schema *app.KolSchemaJSON `json:"schema,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Done bool `json:"done"`
|
||||
}
|
||||
|
||||
func NewKolManageHandler(a *bootstrap.App, assets *AssetHandler) *KolManageHandler {
|
||||
profileSvc := app.NewKolProfileService(a.KolProfiles).WithObjectStorage(a.ObjectStorage)
|
||||
|
||||
@@ -460,6 +474,92 @@ func (h *KolManageHandler) AssistSubmit(c *gin.Context) {
|
||||
response.Success(c, app.KolAssistSubmitResponse{TaskID: taskID})
|
||||
}
|
||||
|
||||
func (h *KolManageHandler) AssistStream(c *gin.Context) {
|
||||
var req app.AssistRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
actor, ok := actorFromRequest(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
c.Header("Cache-Control", "no-cache")
|
||||
c.Header("Connection", "keep-alive")
|
||||
c.Header("X-Accel-Buffering", "no")
|
||||
|
||||
flusher, ok := c.Writer.(http.Flusher)
|
||||
if !ok {
|
||||
response.Error(c, response.ErrInternal(50011, "stream_unsupported", "response writer does not support streaming"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := writeSSE(c, "start", kolAssistStreamEvent{
|
||||
Mode: req.Mode,
|
||||
Status: "generating",
|
||||
Done: false,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
flusher.Flush()
|
||||
|
||||
var streamed strings.Builder
|
||||
var streamWriteErr error
|
||||
|
||||
result, model, err := h.assistSvc.Run(c.Request.Context(), actor, req, func(delta string) {
|
||||
if delta == "" || streamWriteErr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
streamed.WriteString(delta)
|
||||
if writeErr := writeSSE(c, "delta", kolAssistStreamEvent{
|
||||
Mode: req.Mode,
|
||||
Status: "generating",
|
||||
Delta: delta,
|
||||
Content: streamed.String(),
|
||||
Done: false,
|
||||
}); writeErr != nil {
|
||||
streamWriteErr = writeErr
|
||||
return
|
||||
}
|
||||
flusher.Flush()
|
||||
})
|
||||
if streamWriteErr != nil {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(c.Request.Context().Err(), context.Canceled) || errors.Is(c.Request.Context().Err(), context.DeadlineExceeded) {
|
||||
return
|
||||
}
|
||||
|
||||
appErr := response.Normalize(err)
|
||||
_ = writeSSE(c, "error", kolAssistStreamEvent{
|
||||
Mode: req.Mode,
|
||||
Status: "failed",
|
||||
Content: strings.TrimSpace(streamed.String()),
|
||||
Error: resolveKolAssistStreamErrorMessage(appErr),
|
||||
Done: true,
|
||||
})
|
||||
flusher.Flush()
|
||||
return
|
||||
}
|
||||
|
||||
if err := writeSSE(c, "completed", kolAssistStreamEvent{
|
||||
Mode: req.Mode,
|
||||
Status: "completed",
|
||||
Content: result.Content,
|
||||
Schema: &result.Schema,
|
||||
Model: model,
|
||||
Done: true,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
func (h *KolManageHandler) AssistGet(c *gin.Context) {
|
||||
taskID := c.Param("id")
|
||||
if taskID == "" {
|
||||
@@ -504,3 +604,16 @@ func parseInt64Param(c *gin.Context, key, label string) (int64, bool) {
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func resolveKolAssistStreamErrorMessage(appErr *response.AppError) string {
|
||||
if appErr == nil {
|
||||
return "AI 处理失败,请稍后重试"
|
||||
}
|
||||
if detail := strings.TrimSpace(appErr.Detail); detail != "" {
|
||||
return detail
|
||||
}
|
||||
if message := strings.TrimSpace(appErr.Message); message != "" {
|
||||
return message
|
||||
}
|
||||
return "AI 处理失败,请稍后重试"
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
kolManage.PUT("/prompts/:id/activate", kolManageHandler.ActivatePrompt)
|
||||
kolManage.PUT("/prompts/:id/archive", kolManageHandler.ArchivePrompt)
|
||||
kolManage.POST("/assist", kolManageHandler.AssistSubmit)
|
||||
kolManage.POST("/assist/stream", kolManageHandler.AssistStream)
|
||||
kolManage.GET("/assist/:id", kolManageHandler.AssistGet)
|
||||
|
||||
kolDash := protected.Group("/tenant/kol/dashboard")
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
@@ -18,8 +17,6 @@ import (
|
||||
"github.com/geo-platform/tenant-api/internal/tenant/repository"
|
||||
)
|
||||
|
||||
const kolAssistSystemPrompt = "You are a prompt designer for marketing content. Respond ONLY with JSON: {content: string, schema: {variables: [...]}}"
|
||||
|
||||
var errKolAssistConsumerClosed = errors.New("kol assist consumer channel closed")
|
||||
|
||||
type KolAssistWorker struct {
|
||||
@@ -191,24 +188,21 @@ func (w *KolAssistWorker) processTask(ctx context.Context, job tenantapp.KolAssi
|
||||
return fmt.Errorf("decode kol assist request: %w", err)
|
||||
}
|
||||
|
||||
userPrompt, err := buildKolAssistUserPrompt(req)
|
||||
generateReq, err := tenantapp.BuildKolAssistGenerateRequest(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result, err := w.llm.Generate(ctx, llm.GenerateRequest{
|
||||
Prompt: fmt.Sprintf("SYSTEM:\n%s\n\nUSER:\n%s", kolAssistSystemPrompt, userPrompt),
|
||||
ResponseFormat: &llm.ResponseFormat{
|
||||
Type: llm.ResponseFormatTypeJSONObject,
|
||||
Name: "kol_assist_response",
|
||||
Description: "JSON object with content:string and schema:{variables:[]}",
|
||||
},
|
||||
}, nil)
|
||||
result, err := w.llm.Generate(ctx, generateReq, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate kol assist content: %w", err)
|
||||
}
|
||||
|
||||
parsed, err := parseKolAssistResponse(result.Content)
|
||||
parsed, err := tenantapp.ParseKolAssistResponse(result.Content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parsed, err = tenantapp.FinalizeKolAssistResult(req, parsed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -223,44 +217,3 @@ func (w *KolAssistWorker) processTask(ctx context.Context, job tenantapp.KolAssi
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildKolAssistUserPrompt(req tenantapp.AssistRequest) (string, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(req.Mode)) {
|
||||
case "generate":
|
||||
return req.Description, nil
|
||||
case "optimize":
|
||||
schemaJSON := "null"
|
||||
if req.Schema != nil {
|
||||
payload, err := json.Marshal(req.Schema)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("encode schema: %w", err)
|
||||
}
|
||||
schemaJSON = string(payload)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"Optimize the following prompt. Keep variable placeholders intact. Current schema: %s\n\nPROMPT:\n%s",
|
||||
schemaJSON,
|
||||
req.CurrentContent,
|
||||
), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported assist mode %q", req.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
func parseKolAssistResponse(content string) (*tenantapp.KolAssistResult, error) {
|
||||
var result tenantapp.KolAssistResult
|
||||
if err := json.Unmarshal([]byte(content), &result); err != nil {
|
||||
return nil, fmt.Errorf("decode kol assist result: %w", err)
|
||||
}
|
||||
result.Content = strings.TrimSpace(result.Content)
|
||||
if result.Content == "" {
|
||||
return nil, fmt.Errorf("kol assist result content is empty")
|
||||
}
|
||||
if result.Schema.Variables == nil {
|
||||
result.Schema.Variables = []tenantapp.KolVariableDefinition{}
|
||||
}
|
||||
if err := tenantapp.ValidateSchema(result.Schema); err != nil {
|
||||
return nil, fmt.Errorf("invalid kol assist schema: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user