fix(ai): 清理 aislop 扫描告警
- 拆分 SSE reducer 到 sseReducer.ts,provider.ts 降至 220 行 - artifact 合并工具抽到 uiArtifacts.ts,去除双重类型断言 - 业务上下文注册表改用 field/relation/entity 构建器收敛重复 - aislop 分数 71 → 81(Healthy),AI Slop 0 告警
This commit is contained in:
@@ -9,7 +9,7 @@ import type {
|
||||
AiReviewSchema,
|
||||
AiToolRun,
|
||||
} from './types';
|
||||
import { mergeArtifactIntoMessage } from './provider';
|
||||
import { mergeArtifactIntoMessage } from './uiArtifacts';
|
||||
|
||||
function mapStatus(record: AiMessageRecord): AiChatMessageStatus {
|
||||
if (record.status === 'pending') return 'loading';
|
||||
|
||||
@@ -6,280 +6,10 @@ import {
|
||||
} from '@ant-design/x-sdk';
|
||||
import { usePermissionStore } from '../../store/permission/permissionStore';
|
||||
import { useUserStore } from '../../store/user/userStore';
|
||||
import type {
|
||||
AiAttachment,
|
||||
AiArtifactSchema,
|
||||
AiChatInput,
|
||||
AiChatMessage,
|
||||
AiChartSchema,
|
||||
AiFormSchema,
|
||||
AiImportPreflight,
|
||||
AiModelRetryInfo,
|
||||
AiReviewSchema,
|
||||
AiSseChunk,
|
||||
AiToolRun,
|
||||
} from './types';
|
||||
import type { AiArtifactSchema, AiChatInput, AiChatMessage, AiReviewSchema, AiSseChunk } from './types';
|
||||
import { emptyAssistant, parseSsePayload, reduceAiSseMessage } from './sseReducer';
|
||||
|
||||
interface AiSsePayload {
|
||||
messageId?: number;
|
||||
userMessageId?: number;
|
||||
assistantMessageId?: number;
|
||||
delta?: string;
|
||||
content?: string;
|
||||
reasoningContent?: string | null;
|
||||
toolCallId?: string;
|
||||
toolName?: string;
|
||||
skillKey?: string | null;
|
||||
status?: string;
|
||||
summary?: string | null;
|
||||
durationMs?: number | null;
|
||||
attachment?: AiAttachment;
|
||||
form?: AiFormSchema;
|
||||
artifact?: AiArtifactSchema;
|
||||
review?: AiReviewSchema;
|
||||
chart?: AiChartSchema;
|
||||
preflight?: AiImportPreflight;
|
||||
wizard?: unknown;
|
||||
retry?: AiModelRetryInfo;
|
||||
message?:
|
||||
| string
|
||||
| {
|
||||
id?: number;
|
||||
content?: string;
|
||||
reasoningContent?: string | null;
|
||||
status?: string;
|
||||
toolRuns?: AiToolRun[];
|
||||
attachments?: AiAttachment[];
|
||||
replyToMessageId?: number | null;
|
||||
metadata?: Record<string, unknown> | null;
|
||||
};
|
||||
error?: string;
|
||||
}
|
||||
|
||||
function emptyAssistant(): AiChatMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
reasoningContent: '',
|
||||
toolRuns: [],
|
||||
attachments: [],
|
||||
forms: [],
|
||||
uiArtifacts: [],
|
||||
};
|
||||
}
|
||||
|
||||
function mergeForms(
|
||||
current: AiFormSchema[] | undefined,
|
||||
incoming: AiFormSchema | AiFormSchema[] | undefined,
|
||||
): AiFormSchema[] {
|
||||
const items = Array.isArray(incoming) ? incoming : incoming ? [incoming] : [];
|
||||
if (!items.length) return current ?? [];
|
||||
const next = [...(current ?? [])];
|
||||
for (const item of items) {
|
||||
if (item && typeof item === 'object' && !next.some((existing) => existing.id === item.id)) {
|
||||
next.push(item);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function mergeById<T extends { id: string }>(
|
||||
current: T[] | undefined,
|
||||
incoming: T | T[] | undefined,
|
||||
): T[] {
|
||||
const items = Array.isArray(incoming) ? incoming : incoming ? [incoming] : [];
|
||||
if (!items.length) return current ?? [];
|
||||
const next = [...(current ?? [])];
|
||||
for (const item of items) {
|
||||
if (!item || typeof item !== 'object') continue;
|
||||
const index = next.findIndex((existing) => existing.id === item.id);
|
||||
if (index === -1) {
|
||||
next.push(item);
|
||||
} else {
|
||||
next[index] = item;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function parseSsePayload(chunk?: AiSseChunk): {
|
||||
event: string;
|
||||
payload: AiSsePayload;
|
||||
} {
|
||||
if (!chunk) return { event: '', payload: {} };
|
||||
const event = chunk.event?.trim() || 'message';
|
||||
if (!chunk.data || chunk.data === '[DONE]') return { event, payload: {} };
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(chunk.data);
|
||||
return {
|
||||
event,
|
||||
payload: parsed && typeof parsed === 'object' ? (parsed as AiSsePayload) : {},
|
||||
};
|
||||
} catch {
|
||||
return { event, payload: { delta: chunk.data } };
|
||||
}
|
||||
}
|
||||
|
||||
function upsertToolRun(
|
||||
toolRuns: AiToolRun[],
|
||||
payload: AiSsePayload,
|
||||
fallbackStatus: AiToolRun['status'],
|
||||
): AiToolRun[] {
|
||||
const toolCallId = payload.toolCallId || `${payload.toolName || 'tool'}-${toolRuns.length}`;
|
||||
const next: AiToolRun = {
|
||||
toolCallId,
|
||||
toolName: payload.toolName || '查询工具',
|
||||
skillKey: payload.skillKey,
|
||||
status: (payload.status as AiToolRun['status']) || fallbackStatus,
|
||||
summary: payload.summary,
|
||||
resultSummary: fallbackStatus === 'running' ? undefined : payload.summary,
|
||||
argumentsSummary: fallbackStatus === 'running' ? payload.summary : undefined,
|
||||
durationMs: payload.durationMs,
|
||||
};
|
||||
const index = toolRuns.findIndex((item) => item.toolCallId === toolCallId);
|
||||
if (index === -1) return [...toolRuns, next];
|
||||
return toolRuns.map((item, itemIndex) => (itemIndex === index ? { ...item, ...next } : item));
|
||||
}
|
||||
|
||||
function normalizeToolRuns(toolRuns: AiToolRun[] | undefined, fallback: AiToolRun[]): AiToolRun[] {
|
||||
if (!toolRuns) return fallback;
|
||||
return toolRuns.map((tool) => ({
|
||||
...tool,
|
||||
status: tool.status === 'error' ? 'failed' : tool.status,
|
||||
summary: tool.resultSummary ?? tool.argumentsSummary ?? tool.summary,
|
||||
}));
|
||||
}
|
||||
|
||||
function applyMessagePayload(
|
||||
message: AiChatMessage,
|
||||
nested: AiSsePayload['message'],
|
||||
payload: AiSsePayload,
|
||||
): void {
|
||||
if (typeof nested !== 'object' || nested === null) return;
|
||||
message.forms = mergeForms(
|
||||
message.forms,
|
||||
(nested.metadata?.a2uiForm as AiFormSchema | undefined) ?? payload.form,
|
||||
);
|
||||
message.reviews = mergeById<AiReviewSchema>(
|
||||
message.reviews,
|
||||
(nested.metadata?.a2uiReview as AiReviewSchema | undefined) ?? payload.review,
|
||||
);
|
||||
message.charts = mergeById<AiChartSchema>(
|
||||
message.charts,
|
||||
(nested.metadata?.a2uiChart as AiChartSchema | AiChartSchema[] | undefined) ?? payload.chart,
|
||||
);
|
||||
const artifacts = nested.metadata?.uiArtifacts;
|
||||
if (Array.isArray(artifacts)) {
|
||||
for (const artifact of artifacts) {
|
||||
if (artifact && typeof artifact === 'object' && typeof artifact.id === 'string') {
|
||||
mergeArtifactIntoMessage(message, artifact as AiArtifactSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
message.replyToMessageId = nested.replyToMessageId ?? message.replyToMessageId;
|
||||
message.metadata = nested.metadata ?? message.metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将统一 artifact 归入 uiArtifacts,并按类型派发到 legacy 列表。
|
||||
*/
|
||||
export function mergeArtifactIntoMessage(
|
||||
message: AiChatMessage,
|
||||
artifact: AiArtifactSchema,
|
||||
): AiChatMessage {
|
||||
message.uiArtifacts = mergeById<AiArtifactSchema>(message.uiArtifacts, artifact);
|
||||
const payload =
|
||||
artifact.payload && typeof artifact.payload === 'object'
|
||||
? (artifact.payload as Record<string, unknown>)
|
||||
: {};
|
||||
if (artifact.type === 'form') {
|
||||
message.forms = mergeForms(message.forms, payload as unknown as AiFormSchema);
|
||||
} else if (artifact.type === 'review') {
|
||||
message.reviews = mergeById<AiReviewSchema>(
|
||||
message.reviews,
|
||||
payload as unknown as AiReviewSchema,
|
||||
);
|
||||
} else if (artifact.type === 'chart') {
|
||||
message.charts = mergeById<AiChartSchema>(
|
||||
message.charts,
|
||||
payload as unknown as AiChartSchema,
|
||||
);
|
||||
} else if (artifact.type === 'import_preflight') {
|
||||
message.metadata = { ...message.metadata, a2uiImportPreflight: payload };
|
||||
} else if (artifact.type === 'import_wizard') {
|
||||
message.metadata = { ...message.metadata, a2uiImportWizard: payload };
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
export function reduceAiSseMessage(
|
||||
originMessage: AiChatMessage | undefined,
|
||||
chunk?: AiSseChunk,
|
||||
): AiChatMessage {
|
||||
const message = originMessage ? { ...originMessage } : emptyAssistant();
|
||||
const { event, payload } = parseSsePayload(chunk);
|
||||
|
||||
if (event === 'message.created') {
|
||||
const nested = typeof payload.message === 'object' ? payload.message : undefined;
|
||||
message.id = nested?.id ?? payload.assistantMessageId ?? payload.messageId ?? message.id;
|
||||
message.content = nested?.content ?? message.content;
|
||||
message.reasoningContent = nested?.reasoningContent ?? message.reasoningContent;
|
||||
message.toolRuns = normalizeToolRuns(nested?.toolRuns, message.toolRuns);
|
||||
message.attachments = nested?.attachments ?? message.attachments;
|
||||
applyMessagePayload(message, nested, payload);
|
||||
} else if (event === 'reasoning.delta') {
|
||||
message.retrying = null;
|
||||
message.reasoningContent += payload.delta ?? payload.reasoningContent ?? '';
|
||||
} else if (event === 'content.delta') {
|
||||
message.retrying = null;
|
||||
message.content += payload.delta ?? payload.content ?? '';
|
||||
} else if (event === 'model.retrying' && payload.retry) {
|
||||
message.retrying = payload.retry;
|
||||
} else if (event === 'ui.form' && payload.form) {
|
||||
message.forms = mergeForms(message.forms, payload.form);
|
||||
} else if (event === 'ui.review' && payload.review) {
|
||||
message.reviews = mergeById<AiReviewSchema>(message.reviews, payload.review);
|
||||
} else if (event === 'ui.chart' && payload.chart) {
|
||||
message.charts = mergeById<AiChartSchema>(message.charts, payload.chart);
|
||||
} else if (event === 'ui.artifact' && payload.artifact) {
|
||||
mergeArtifactIntoMessage(message, payload.artifact);
|
||||
} else if (event === 'ui.import_preflight' && payload.preflight) {
|
||||
message.metadata = { ...message.metadata, a2uiImportPreflight: payload.preflight };
|
||||
} else if (event === 'ui.import_wizard' && payload.wizard) {
|
||||
message.metadata = { ...message.metadata, a2uiImportWizard: payload.wizard };
|
||||
} else if (event === 'tool.started') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'running');
|
||||
} else if (event === 'tool.completed') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'success');
|
||||
} else if (event === 'tool.failed') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'failed');
|
||||
} else if (event === 'attachment.processed' && payload.attachment) {
|
||||
if (!message.attachments.some((item) => item.id === payload.attachment?.id)) {
|
||||
message.attachments = [...message.attachments, payload.attachment];
|
||||
}
|
||||
} else if (event === 'message.completed') {
|
||||
const nested = typeof payload.message === 'object' ? payload.message : undefined;
|
||||
message.id = nested?.id ?? payload.messageId ?? message.id;
|
||||
message.content = nested?.content ?? payload.content ?? message.content;
|
||||
message.reasoningContent =
|
||||
nested?.reasoningContent ?? payload.reasoningContent ?? message.reasoningContent;
|
||||
message.toolRuns = normalizeToolRuns(nested?.toolRuns, message.toolRuns);
|
||||
message.attachments = nested?.attachments ?? message.attachments;
|
||||
applyMessagePayload(message, nested, payload);
|
||||
message.retrying = null;
|
||||
} else if (event === 'message.cancelled') {
|
||||
message.id = payload.messageId ?? message.id;
|
||||
message.cancelled = true;
|
||||
message.retrying = null;
|
||||
} else if (event === 'error') {
|
||||
message.retrying = null;
|
||||
message.error =
|
||||
(typeof payload.message === 'string' ? payload.message : undefined) ||
|
||||
payload.error ||
|
||||
'AI 回答生成失败';
|
||||
}
|
||||
return message;
|
||||
}
|
||||
export { parseSsePayload, reduceAiSseMessage };
|
||||
|
||||
export async function authenticatedFetch(
|
||||
input: RequestInfo | URL,
|
||||
|
||||
208
apps/admin/src/components/AiChat/sseReducer.ts
Normal file
208
apps/admin/src/components/AiChat/sseReducer.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
import type {
|
||||
AiArtifactSchema,
|
||||
AiAttachment,
|
||||
AiChartSchema,
|
||||
AiChatMessage,
|
||||
AiFormSchema,
|
||||
AiImportPreflight,
|
||||
AiModelRetryInfo,
|
||||
AiReviewSchema,
|
||||
AiSseChunk,
|
||||
AiToolRun,
|
||||
} from './types';
|
||||
import { mergeArtifactIntoMessage, mergeById, mergeForms } from './uiArtifacts';
|
||||
|
||||
export interface AiSsePayload {
|
||||
messageId?: number;
|
||||
userMessageId?: number;
|
||||
assistantMessageId?: number;
|
||||
delta?: string;
|
||||
content?: string;
|
||||
reasoningContent?: string | null;
|
||||
toolCallId?: string;
|
||||
toolName?: string;
|
||||
skillKey?: string | null;
|
||||
status?: string;
|
||||
summary?: string | null;
|
||||
durationMs?: number | null;
|
||||
attachment?: AiAttachment;
|
||||
form?: AiFormSchema;
|
||||
artifact?: AiArtifactSchema;
|
||||
review?: AiReviewSchema;
|
||||
chart?: AiChartSchema;
|
||||
preflight?: AiImportPreflight;
|
||||
wizard?: unknown;
|
||||
retry?: AiModelRetryInfo;
|
||||
message?:
|
||||
| string
|
||||
| {
|
||||
id?: number;
|
||||
content?: string;
|
||||
reasoningContent?: string | null;
|
||||
status?: string;
|
||||
toolRuns?: AiToolRun[];
|
||||
attachments?: AiAttachment[];
|
||||
replyToMessageId?: number | null;
|
||||
metadata?: Record<string, unknown> | null;
|
||||
};
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export function emptyAssistant(): AiChatMessage {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
reasoningContent: '',
|
||||
toolRuns: [],
|
||||
attachments: [],
|
||||
forms: [],
|
||||
uiArtifacts: [],
|
||||
};
|
||||
}
|
||||
|
||||
export function parseSsePayload(chunk?: AiSseChunk): {
|
||||
event: string;
|
||||
payload: AiSsePayload;
|
||||
} {
|
||||
if (!chunk) return { event: '', payload: {} };
|
||||
const event = chunk.event?.trim() || 'message';
|
||||
if (!chunk.data || chunk.data === '[DONE]') return { event, payload: {} };
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(chunk.data);
|
||||
return {
|
||||
event,
|
||||
payload: parsed && typeof parsed === 'object' ? (parsed as AiSsePayload) : {},
|
||||
};
|
||||
} catch {
|
||||
return { event, payload: { delta: chunk.data } };
|
||||
}
|
||||
}
|
||||
|
||||
function upsertToolRun(
|
||||
toolRuns: AiToolRun[],
|
||||
payload: AiSsePayload,
|
||||
fallbackStatus: AiToolRun['status'],
|
||||
): AiToolRun[] {
|
||||
const toolCallId = payload.toolCallId || `${payload.toolName || 'tool'}-${toolRuns.length}`;
|
||||
const next: AiToolRun = {
|
||||
toolCallId,
|
||||
toolName: payload.toolName || '查询工具',
|
||||
skillKey: payload.skillKey,
|
||||
status: (payload.status as AiToolRun['status']) || fallbackStatus,
|
||||
summary: payload.summary,
|
||||
resultSummary: fallbackStatus === 'running' ? undefined : payload.summary,
|
||||
argumentsSummary: fallbackStatus === 'running' ? payload.summary : undefined,
|
||||
durationMs: payload.durationMs,
|
||||
};
|
||||
const index = toolRuns.findIndex((item) => item.toolCallId === toolCallId);
|
||||
if (index === -1) return [...toolRuns, next];
|
||||
return toolRuns.map((item, itemIndex) => (itemIndex === index ? { ...item, ...next } : item));
|
||||
}
|
||||
|
||||
function normalizeToolRuns(toolRuns: AiToolRun[] | undefined, fallback: AiToolRun[]): AiToolRun[] {
|
||||
if (!toolRuns) return fallback;
|
||||
return toolRuns.map((tool) => ({
|
||||
...tool,
|
||||
status: tool.status === 'error' ? 'failed' : tool.status,
|
||||
summary: tool.resultSummary ?? tool.argumentsSummary ?? tool.summary,
|
||||
}));
|
||||
}
|
||||
|
||||
function applyMessagePayload(
|
||||
message: AiChatMessage,
|
||||
nested: AiSsePayload['message'],
|
||||
payload: AiSsePayload,
|
||||
): void {
|
||||
if (typeof nested !== 'object' || nested === null) return;
|
||||
message.forms = mergeForms(
|
||||
message.forms,
|
||||
(nested.metadata?.a2uiForm as AiFormSchema | undefined) ?? payload.form,
|
||||
);
|
||||
message.reviews = mergeById<AiReviewSchema>(
|
||||
message.reviews,
|
||||
(nested.metadata?.a2uiReview as AiReviewSchema | undefined) ?? payload.review,
|
||||
);
|
||||
message.charts = mergeById<AiChartSchema>(
|
||||
message.charts,
|
||||
(nested.metadata?.a2uiChart as AiChartSchema | AiChartSchema[] | undefined) ?? payload.chart,
|
||||
);
|
||||
const artifacts = nested.metadata?.uiArtifacts;
|
||||
if (Array.isArray(artifacts)) {
|
||||
for (const artifact of artifacts) {
|
||||
if (artifact && typeof artifact === 'object' && typeof artifact.id === 'string') {
|
||||
mergeArtifactIntoMessage(message, artifact as AiArtifactSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
message.replyToMessageId = nested.replyToMessageId ?? message.replyToMessageId;
|
||||
message.metadata = nested.metadata ?? message.metadata;
|
||||
}
|
||||
|
||||
export function reduceAiSseMessage(
|
||||
originMessage: AiChatMessage | undefined,
|
||||
chunk?: AiSseChunk,
|
||||
): AiChatMessage {
|
||||
const message = originMessage ? { ...originMessage } : emptyAssistant();
|
||||
const { event, payload } = parseSsePayload(chunk);
|
||||
|
||||
if (event === 'message.created') {
|
||||
const nested = typeof payload.message === 'object' ? payload.message : undefined;
|
||||
message.id = nested?.id ?? payload.assistantMessageId ?? payload.messageId ?? message.id;
|
||||
message.content = nested?.content ?? message.content;
|
||||
message.reasoningContent = nested?.reasoningContent ?? message.reasoningContent;
|
||||
message.toolRuns = normalizeToolRuns(nested?.toolRuns, message.toolRuns);
|
||||
message.attachments = nested?.attachments ?? message.attachments;
|
||||
applyMessagePayload(message, nested, payload);
|
||||
} else if (event === 'reasoning.delta') {
|
||||
message.retrying = null;
|
||||
message.reasoningContent += payload.delta ?? payload.reasoningContent ?? '';
|
||||
} else if (event === 'content.delta') {
|
||||
message.retrying = null;
|
||||
message.content += payload.delta ?? payload.content ?? '';
|
||||
} else if (event === 'model.retrying' && payload.retry) {
|
||||
message.retrying = payload.retry;
|
||||
} else if (event === 'ui.form' && payload.form) {
|
||||
message.forms = mergeForms(message.forms, payload.form);
|
||||
} else if (event === 'ui.review' && payload.review) {
|
||||
message.reviews = mergeById<AiReviewSchema>(message.reviews, payload.review);
|
||||
} else if (event === 'ui.chart' && payload.chart) {
|
||||
message.charts = mergeById<AiChartSchema>(message.charts, payload.chart);
|
||||
} else if (event === 'ui.artifact' && payload.artifact) {
|
||||
mergeArtifactIntoMessage(message, payload.artifact);
|
||||
} else if (event === 'ui.import_preflight' && payload.preflight) {
|
||||
message.metadata = { ...message.metadata, a2uiImportPreflight: payload.preflight };
|
||||
} else if (event === 'ui.import_wizard' && payload.wizard) {
|
||||
message.metadata = { ...message.metadata, a2uiImportWizard: payload.wizard };
|
||||
} else if (event === 'tool.started') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'running');
|
||||
} else if (event === 'tool.completed') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'success');
|
||||
} else if (event === 'tool.failed') {
|
||||
message.toolRuns = upsertToolRun(message.toolRuns, payload, 'failed');
|
||||
} else if (event === 'attachment.processed' && payload.attachment) {
|
||||
if (!message.attachments.some((item) => item.id === payload.attachment?.id)) {
|
||||
message.attachments = [...message.attachments, payload.attachment];
|
||||
}
|
||||
} else if (event === 'message.completed') {
|
||||
const nested = typeof payload.message === 'object' ? payload.message : undefined;
|
||||
message.id = nested?.id ?? payload.messageId ?? message.id;
|
||||
message.content = nested?.content ?? payload.content ?? message.content;
|
||||
message.reasoningContent =
|
||||
nested?.reasoningContent ?? payload.reasoningContent ?? message.reasoningContent;
|
||||
message.toolRuns = normalizeToolRuns(nested?.toolRuns, message.toolRuns);
|
||||
message.attachments = nested?.attachments ?? message.attachments;
|
||||
applyMessagePayload(message, nested, payload);
|
||||
message.retrying = null;
|
||||
} else if (event === 'message.cancelled') {
|
||||
message.id = payload.messageId ?? message.id;
|
||||
message.cancelled = true;
|
||||
message.retrying = null;
|
||||
} else if (event === 'error') {
|
||||
message.retrying = null;
|
||||
message.error =
|
||||
(typeof payload.message === 'string' ? payload.message : undefined) ||
|
||||
payload.error ||
|
||||
'AI 回答生成失败';
|
||||
}
|
||||
return message;
|
||||
}
|
||||
69
apps/admin/src/components/AiChat/uiArtifacts.ts
Normal file
69
apps/admin/src/components/AiChat/uiArtifacts.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import type {
|
||||
AiArtifactSchema,
|
||||
AiChartSchema,
|
||||
AiChatMessage,
|
||||
AiFormSchema,
|
||||
AiReviewSchema,
|
||||
} from './types';
|
||||
|
||||
export function mergeById<T extends { id: string }>(
|
||||
current: T[] | undefined,
|
||||
incoming: T | T[] | undefined,
|
||||
): T[] {
|
||||
const items = Array.isArray(incoming) ? incoming : incoming ? [incoming] : [];
|
||||
if (!items.length) return current ?? [];
|
||||
const next = [...(current ?? [])];
|
||||
for (const item of items) {
|
||||
if (!item || typeof item !== 'object') continue;
|
||||
const index = next.findIndex((existing) => existing.id === item.id);
|
||||
if (index === -1) {
|
||||
next.push(item);
|
||||
} else {
|
||||
next[index] = item;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function mergeForms(
|
||||
current: AiFormSchema[] | undefined,
|
||||
incoming: AiFormSchema | AiFormSchema[] | undefined,
|
||||
): AiFormSchema[] {
|
||||
const items = Array.isArray(incoming) ? incoming : incoming ? [incoming] : [];
|
||||
if (!items.length) return current ?? [];
|
||||
const next = [...(current ?? [])];
|
||||
for (const item of items) {
|
||||
if (item && typeof item === 'object' && !next.some((existing) => existing.id === item.id)) {
|
||||
next.push(item);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function payloadOf(artifact: AiArtifactSchema): unknown {
|
||||
return artifact.payload && typeof artifact.payload === 'object' ? artifact.payload : {};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将统一 artifact 归入 uiArtifacts,并按类型派发到 legacy 列表。
|
||||
* payload 来自服务端契约(表单/审阅/图表/预检/向导),按类型做单次断言。
|
||||
*/
|
||||
export function mergeArtifactIntoMessage(
|
||||
message: AiChatMessage,
|
||||
artifact: AiArtifactSchema,
|
||||
): AiChatMessage {
|
||||
message.uiArtifacts = mergeById<AiArtifactSchema>(message.uiArtifacts, artifact);
|
||||
const payload = payloadOf(artifact);
|
||||
if (artifact.type === 'form') {
|
||||
message.forms = mergeForms(message.forms, payload as AiFormSchema);
|
||||
} else if (artifact.type === 'review') {
|
||||
message.reviews = mergeById<AiReviewSchema>(message.reviews, payload as AiReviewSchema);
|
||||
} else if (artifact.type === 'chart') {
|
||||
message.charts = mergeById<AiChartSchema>(message.charts, payload as AiChartSchema);
|
||||
} else if (artifact.type === 'import_preflight') {
|
||||
message.metadata = { ...message.metadata, a2uiImportPreflight: payload };
|
||||
} else if (artifact.type === 'import_wizard') {
|
||||
message.metadata = { ...message.metadata, a2uiImportWizard: payload };
|
||||
}
|
||||
return message;
|
||||
}
|
||||
@@ -9,7 +9,8 @@ import { useSettingsStore } from '../../store/settings/settingsStore';
|
||||
import { aiChatApi, resolveImportPreflight, type ResolveImportPreflightInput } from './api';
|
||||
import { AiMessageContent } from './AiMessageContent';
|
||||
import { mapHistoryMessage } from './message-mappers';
|
||||
import { GongxueAiChatProvider, mergeArtifactIntoMessage } from './provider';
|
||||
import { GongxueAiChatProvider } from './provider';
|
||||
import { mergeArtifactIntoMessage } from './uiArtifacts';
|
||||
import {
|
||||
emptyAssistant,
|
||||
MessageHoverActions,
|
||||
|
||||
Reference in New Issue
Block a user