mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 16:29:20 +08:00
fix: agent 连续改标题时强制落盘,避免 abort 后假成功
This commit is contained in:
@@ -221,8 +221,10 @@ export function streamAgentChat(options: {
|
|||||||
name?: string;
|
name?: string;
|
||||||
label: string;
|
label: string;
|
||||||
}) => void;
|
}) => void;
|
||||||
/** 前端工具:乐观执行改表单 */
|
/** 前端工具:乐观执行改表单(可返回 Promise,流式侧会 await 以串行动画) */
|
||||||
onClientTool?: (call: AgentVO.ClientToolCall) => void;
|
onClientTool?: (
|
||||||
|
call: AgentVO.ClientToolCall,
|
||||||
|
) => void | Promise<void>;
|
||||||
onDone?: () => void;
|
onDone?: () => void;
|
||||||
onError?: (message: string) => void;
|
onError?: (message: string) => void;
|
||||||
}): AbortController {
|
}): AbortController {
|
||||||
@@ -257,7 +259,7 @@ export function streamAgentChat(options: {
|
|||||||
let buffer = '';
|
let buffer = '';
|
||||||
let finished = false;
|
let finished = false;
|
||||||
|
|
||||||
const consumeBlock = (part: string) => {
|
const consumeBlock = async (part: string) => {
|
||||||
const lines = part.split('\n');
|
const lines = part.split('\n');
|
||||||
let event = 'message';
|
let event = 'message';
|
||||||
const dataLines: string[] = [];
|
const dataLines: string[] = [];
|
||||||
@@ -305,10 +307,25 @@ export function streamAgentChat(options: {
|
|||||||
label?: string;
|
label?: string;
|
||||||
};
|
};
|
||||||
if (parsed.name) {
|
if (parsed.name) {
|
||||||
options.onClientTool?.({
|
let toolArgs: Record<string, unknown> = {};
|
||||||
|
const rawArgs = parsed.args;
|
||||||
|
if (rawArgs && typeof rawArgs === 'object') {
|
||||||
|
toolArgs = rawArgs as Record<string, unknown>;
|
||||||
|
} else if (typeof rawArgs === 'string' && rawArgs.trim()) {
|
||||||
|
try {
|
||||||
|
const parsedArgs = JSON.parse(rawArgs) as unknown;
|
||||||
|
if (parsedArgs && typeof parsedArgs === 'object') {
|
||||||
|
toolArgs = parsedArgs as Record<string, unknown>;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 串行等待前端写入(含打字动效),避免连续 ui_* 互相 abort 后未落盘
|
||||||
|
await options.onClientTool?.({
|
||||||
toolCallId: parsed.toolCallId || parsed.name,
|
toolCallId: parsed.toolCallId || parsed.name,
|
||||||
name: parsed.name,
|
name: parsed.name,
|
||||||
args: parsed.args || {},
|
args: toolArgs,
|
||||||
label: parsed.label,
|
label: parsed.label,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -336,10 +353,10 @@ export function streamAgentChat(options: {
|
|||||||
const parts = buffer.split('\n\n');
|
const parts = buffer.split('\n\n');
|
||||||
buffer = parts.pop() || '';
|
buffer = parts.pop() || '';
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (part.trim()) consumeBlock(part);
|
if (part.trim()) await consumeBlock(part);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (buffer.trim()) consumeBlock(buffer);
|
if (buffer.trim()) await consumeBlock(buffer);
|
||||||
if (!finished) options.onDone?.();
|
if (!finished) options.onDone?.();
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e?.name === 'AbortError') return;
|
if (e?.name === 'AbortError') return;
|
||||||
|
|||||||
+6
-2
@@ -299,13 +299,17 @@ function onStreamTool(payload: { phase: string; label: string }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClientTool(call: AgentVO.ClientToolCall) {
|
async function onClientTool(call: AgentVO.ClientToolCall) {
|
||||||
const api = props.editorApi;
|
const api = props.editorApi;
|
||||||
if (!api) return;
|
if (!api) return;
|
||||||
const result = api.applyClientTool(call.name, call.args || {});
|
try {
|
||||||
|
const result = await api.applyClientTool(call.name, call.args || {});
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
message.warning(result.message || '表单写入失败');
|
message.warning(result.message || '表单写入失败');
|
||||||
}
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
message.warning(e?.message || '表单写入失败');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function send(textOverride?: string) {
|
async function send(textOverride?: string) {
|
||||||
|
|||||||
@@ -44,8 +44,9 @@ export type ClientToolResult = {
|
|||||||
/** 编辑器 defineExpose 契约 */
|
/** 编辑器 defineExpose 契约 */
|
||||||
export type PublishEditorAgentApi = {
|
export type PublishEditorAgentApi = {
|
||||||
getFormSnapshot: () => PublishFormSnapshot;
|
getFormSnapshot: () => PublishFormSnapshot;
|
||||||
|
/** 文本类工具会 await 逐字删写动效后再返回 */
|
||||||
applyClientTool: (
|
applyClientTool: (
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
) => ClientToolResult;
|
) => ClientToolResult | Promise<ClientToolResult>;
|
||||||
};
|
};
|
||||||
|
|||||||
+29
-1
@@ -275,6 +275,8 @@ function resetAll() {
|
|||||||
draft.resetBoundDraftId();
|
draft.resetBoundDraftId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { agentTypingField } = agent;
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
getFormSnapshot: agent.getFormSnapshot,
|
getFormSnapshot: agent.getFormSnapshot,
|
||||||
applyClientTool: agent.applyClientTool,
|
applyClientTool: agent.applyClientTool,
|
||||||
@@ -480,9 +482,18 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
|
|||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
|
<div
|
||||||
|
data-agent-field="goodsName"
|
||||||
|
:class="{ 'agent-typing-field': agentTypingField === 'goodsName' }"
|
||||||
|
>
|
||||||
<Form.Item label="商品标题" required>
|
<Form.Item label="商品标题" required>
|
||||||
<Input v-model:value="form.goodsName" :maxlength="60" show-count />
|
<Input v-model:value="form.goodsName" :maxlength="60" show-count />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
data-agent-field="goodsDesc"
|
||||||
|
:class="{ 'agent-typing-field': agentTypingField === 'goodsDesc' }"
|
||||||
|
>
|
||||||
<Form.Item label="商品描述">
|
<Form.Item label="商品描述">
|
||||||
<Input.TextArea
|
<Input.TextArea
|
||||||
v-model:value="form.goodsDesc"
|
v-model:value="form.goodsDesc"
|
||||||
@@ -490,6 +501,7 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
|
|||||||
placeholder="可选,20-500 字"
|
placeholder="可选,20-500 字"
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
|
||||||
<Form.Item label="运费模板" required>
|
<Form.Item label="运费模板" required>
|
||||||
<Select
|
<Select
|
||||||
@@ -719,7 +731,12 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
|
|||||||
</Form.Item>
|
</Form.Item>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="form.catId" class="space-y-4">
|
<div
|
||||||
|
v-if="form.catId"
|
||||||
|
data-agent-field="spec"
|
||||||
|
class="space-y-4"
|
||||||
|
:class="{ 'agent-typing-field': agentTypingField === 'spec' }"
|
||||||
|
>
|
||||||
<SpecAxesCard
|
<SpecAxesCard
|
||||||
:spec-catalog="specCatalog"
|
:spec-catalog="specCatalog"
|
||||||
:spec-axes="specAxes"
|
:spec-axes="specAxes"
|
||||||
@@ -809,3 +826,14 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
|
|||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
/* 智能体逐字改写时的轻量高亮,便于视线跟上 */
|
||||||
|
.agent-typing-field {
|
||||||
|
position: relative;
|
||||||
|
border-radius: 8px;
|
||||||
|
outline: 2px solid color-mix(in srgb, var(--primary) 45%, transparent);
|
||||||
|
outline-offset: 4px;
|
||||||
|
transition: outline-color 0.2s ease;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
+200
-11
@@ -1,4 +1,5 @@
|
|||||||
import type { Ref } from 'vue';
|
import type { Ref } from 'vue';
|
||||||
|
import { nextTick, ref } from 'vue';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
ClientToolResult,
|
ClientToolResult,
|
||||||
@@ -10,9 +11,14 @@ import type {
|
|||||||
SpecCatalogItem,
|
SpecCatalogItem,
|
||||||
SkuRow,
|
SkuRow,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
import { typewriterReplace } from '../utils/typewriter-text';
|
||||||
|
|
||||||
|
/** 智能体正在打字的表单焦点(用于高亮输入区) */
|
||||||
|
export type AgentTypingField = 'goodsName' | 'goodsDesc' | 'spec' | null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 智能体命令面:读表单快照 + 执行 ui_*(含 skuAssignments / 槽位 id 复用)。
|
* 智能体命令面:读表单快照 + 执行 ui_*(含 skuAssignments / 槽位 id 复用)。
|
||||||
|
* 文本类写入走逐字删写动效,便于用户感知变更。
|
||||||
*/
|
*/
|
||||||
export function usePublishAgent(options: {
|
export function usePublishAgent(options: {
|
||||||
form: PublishFormFields;
|
form: PublishFormFields;
|
||||||
@@ -53,6 +59,80 @@ export function usePublishAgent(options: {
|
|||||||
revokeUploadFiles,
|
revokeUploadFiles,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
|
/** 当前打字焦点,编辑器据此加高亮 */
|
||||||
|
const agentTypingField = ref<AgentTypingField>(null);
|
||||||
|
const typingAborts = new Map<string, AbortController>();
|
||||||
|
/** 字段写入世代:仅最新一代落最终值,避免 abort 误报成功却未写入 */
|
||||||
|
const typingGeneration = new Map<string, number>();
|
||||||
|
|
||||||
|
function isAbortError(err: unknown): boolean {
|
||||||
|
return (
|
||||||
|
(err instanceof DOMException && err.name === 'AbortError') ||
|
||||||
|
(err instanceof Error && err.name === 'AbortError')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 滚动到正在改写的输入区 */
|
||||||
|
async function scrollAgentFieldIntoView(field: string) {
|
||||||
|
await nextTick();
|
||||||
|
const el = document.querySelector(
|
||||||
|
`[data-agent-field="${field}"]`,
|
||||||
|
) as HTMLElement | null;
|
||||||
|
el?.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可打断的打字任务。返回本轮世代号;调用方在动画后仅当世代仍是最新时落最终值。
|
||||||
|
*/
|
||||||
|
async function runTypingTask(
|
||||||
|
key: string,
|
||||||
|
field: Exclude<AgentTypingField, null>,
|
||||||
|
task: (signal: AbortSignal) => Promise<void>,
|
||||||
|
): Promise<{ gen: number }> {
|
||||||
|
const gen = (typingGeneration.get(key) ?? 0) + 1;
|
||||||
|
typingGeneration.set(key, gen);
|
||||||
|
typingAborts.get(key)?.abort();
|
||||||
|
const ac = new AbortController();
|
||||||
|
typingAborts.set(key, ac);
|
||||||
|
agentTypingField.value = field;
|
||||||
|
void scrollAgentFieldIntoView(field === 'spec' ? 'spec' : field);
|
||||||
|
try {
|
||||||
|
await task(ac.signal);
|
||||||
|
} catch (err) {
|
||||||
|
if (!isAbortError(err)) throw err;
|
||||||
|
} finally {
|
||||||
|
if (typingAborts.get(key) === ac) {
|
||||||
|
typingAborts.delete(key);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
agentTypingField.value === field &&
|
||||||
|
typingGeneration.get(key) === gen &&
|
||||||
|
typingAborts.size === 0
|
||||||
|
) {
|
||||||
|
agentTypingField.value = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { gen };
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLatestTyping(key: string, gen: number): boolean {
|
||||||
|
return typingGeneration.get(key) === gen;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 兼容 goodsName / goods_name 及偶发字符串化 args */
|
||||||
|
function pickArgString(
|
||||||
|
args: Record<string, unknown>,
|
||||||
|
...keys: string[]
|
||||||
|
): string {
|
||||||
|
for (const key of keys) {
|
||||||
|
const raw = args[key];
|
||||||
|
if (raw == null) continue;
|
||||||
|
const text = String(raw).trim();
|
||||||
|
if (text) return text;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
function getFormSnapshot(): PublishFormSnapshot {
|
function getFormSnapshot(): PublishFormSnapshot {
|
||||||
return {
|
return {
|
||||||
goodsName: form.goodsName,
|
goodsName: form.goodsName,
|
||||||
@@ -85,15 +165,57 @@ export function usePublishAgent(options: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function applySetGoodsName(args: Record<string, unknown>): ClientToolResult {
|
async function applySetGoodsName(
|
||||||
const name = String(args.goodsName ?? '').trim();
|
args: Record<string, unknown>,
|
||||||
|
): Promise<ClientToolResult> {
|
||||||
|
const name = pickArgString(args, 'goodsName', 'goods_name');
|
||||||
if (!name) return { ok: false, message: '标题为空' };
|
if (!name) return { ok: false, message: '标题为空' };
|
||||||
form.goodsName = name.slice(0, 120);
|
// 与输入框 maxlength 对齐,避免受控 Input 截断后看似「没写上」
|
||||||
return { ok: true, message: `已写入标题:${form.goodsName}` };
|
const next = name.slice(0, 60);
|
||||||
|
if (form.goodsName === next) {
|
||||||
|
return { ok: true, message: `标题未变:${next}` };
|
||||||
|
}
|
||||||
|
const { gen } = await runTypingTask('goodsName', 'goodsName', (signal) =>
|
||||||
|
typewriterReplace(
|
||||||
|
() => form.goodsName,
|
||||||
|
(v) => {
|
||||||
|
form.goodsName = v;
|
||||||
|
},
|
||||||
|
next,
|
||||||
|
{ signal },
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// 被更新的写入覆盖时不落旧值;最新一代无论动画是否被打断都强制提交
|
||||||
|
if (!isLatestTyping('goodsName', gen)) {
|
||||||
|
return { ok: true, message: `已写入标题:${next}` };
|
||||||
|
}
|
||||||
|
form.goodsName = next;
|
||||||
|
agentTypingField.value = null;
|
||||||
|
return { ok: true, message: `已写入标题:${next}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
function applySetGoodsDesc(args: Record<string, unknown>): ClientToolResult {
|
async function applySetGoodsDesc(
|
||||||
form.goodsDesc = String(args.goodsDesc ?? '');
|
args: Record<string, unknown>,
|
||||||
|
): Promise<ClientToolResult> {
|
||||||
|
const desc = String(args.goodsDesc ?? args.goods_desc ?? '');
|
||||||
|
if (form.goodsDesc === desc) {
|
||||||
|
return { ok: true, message: '描述未变' };
|
||||||
|
}
|
||||||
|
const { gen } = await runTypingTask('goodsDesc', 'goodsDesc', (signal) =>
|
||||||
|
typewriterReplace(
|
||||||
|
() => form.goodsDesc,
|
||||||
|
(v) => {
|
||||||
|
form.goodsDesc = v;
|
||||||
|
},
|
||||||
|
desc,
|
||||||
|
{ signal, eraseIntervalMs: 10, typeIntervalMs: 18 },
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (!isLatestTyping('goodsDesc', gen)) {
|
||||||
|
return { ok: true, message: '已写入商品描述' };
|
||||||
|
}
|
||||||
|
form.goodsDesc = desc;
|
||||||
|
agentTypingField.value = null;
|
||||||
return { ok: true, message: '已写入商品描述' };
|
return { ok: true, message: '已写入商品描述' };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,8 +235,13 @@ export function usePublishAgent(options: {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 按名称匹配类目规格目录;无目录时建自由轴。同轴按位置复用槽位 id,改文案保留 SKU。 */
|
/**
|
||||||
function applySetSpecAxes(args: Record<string, unknown>): ClientToolResult {
|
* 按名称匹配类目规格目录;无目录时建自由轴。同轴按位置复用槽位 id,改文案保留 SKU。
|
||||||
|
* 文案变更先落轴结构(临时旧 token),逐字打完再 sync SKU。
|
||||||
|
*/
|
||||||
|
async function applySetSpecAxes(
|
||||||
|
args: Record<string, unknown>,
|
||||||
|
): Promise<ClientToolResult> {
|
||||||
const raw = args.axes;
|
const raw = args.axes;
|
||||||
if (!Array.isArray(raw) || raw.length === 0) {
|
if (!Array.isArray(raw) || raw.length === 0) {
|
||||||
return { ok: false, message: '规格轴为空' };
|
return { ok: false, message: '规格轴为空' };
|
||||||
@@ -131,6 +258,13 @@ export function usePublishAgent(options: {
|
|||||||
);
|
);
|
||||||
const prevSkuRows = skuRows.value.map((r) => r);
|
const prevSkuRows = skuRows.value.map((r) => r);
|
||||||
const next: SpecAxis[] = [];
|
const next: SpecAxis[] = [];
|
||||||
|
/** 需要打字动效的槽位:axisIdx / slotIdx / from / to */
|
||||||
|
const animations: Array<{
|
||||||
|
axisIdx: number;
|
||||||
|
slotIdx: number;
|
||||||
|
from: string;
|
||||||
|
to: string;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
for (const item of limited) {
|
for (const item of limited) {
|
||||||
if (!item || typeof item !== 'object') continue;
|
if (!item || typeof item !== 'object') continue;
|
||||||
@@ -168,19 +302,41 @@ export function usePublishAgent(options: {
|
|||||||
? labels.map((t) => resolveSpecValueToken(axis, t))
|
? labels.map((t) => resolveSpecValueToken(axis, t))
|
||||||
: [''];
|
: [''];
|
||||||
axis.selectedValues = mergeSpecValueSlots(prev?.selectedValues, tokens);
|
axis.selectedValues = mergeSpecValueSlots(prev?.selectedValues, tokens);
|
||||||
|
const axisIdx = next.length;
|
||||||
|
axis.selectedValues.forEach((slot, slotIdx) => {
|
||||||
|
const from = prev?.selectedValues[slotIdx]?.token ?? '';
|
||||||
|
const to = slot.token;
|
||||||
|
if (from !== to) {
|
||||||
|
animations.push({ axisIdx, slotIdx, from, to });
|
||||||
|
slot.token = from;
|
||||||
|
}
|
||||||
|
});
|
||||||
next.push(axis);
|
next.push(axis);
|
||||||
} else {
|
} else {
|
||||||
const prev = prevByName.get(axisName);
|
const prev = prevByName.get(axisName);
|
||||||
const rowId = prev?.rowId ?? nextSpecAxisRowId();
|
const rowId = prev?.rowId ?? nextSpecAxisRowId();
|
||||||
const key = prev?.key || `agent_${axisName}_${rowId}`;
|
const key = prev?.key || `agent_${axisName}_${rowId}`;
|
||||||
const tokens = labels.length > 0 ? labels : [''];
|
const tokens = labels.length > 0 ? labels : [''];
|
||||||
|
const selectedValues = mergeSpecValueSlots(
|
||||||
|
prev?.selectedValues,
|
||||||
|
tokens,
|
||||||
|
);
|
||||||
|
const axisIdx = next.length;
|
||||||
|
selectedValues.forEach((slot, slotIdx) => {
|
||||||
|
const from = prev?.selectedValues[slotIdx]?.token ?? '';
|
||||||
|
const to = slot.token;
|
||||||
|
if (from !== to) {
|
||||||
|
animations.push({ axisIdx, slotIdx, from, to });
|
||||||
|
slot.token = from;
|
||||||
|
}
|
||||||
|
});
|
||||||
next.push({
|
next.push({
|
||||||
rowId,
|
rowId,
|
||||||
catalogKey: key,
|
catalogKey: key,
|
||||||
key,
|
key,
|
||||||
name: axisName,
|
name: axisName,
|
||||||
presetOptions: [],
|
presetOptions: [],
|
||||||
selectedValues: mergeSpecValueSlots(prev?.selectedValues, tokens),
|
selectedValues,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,7 +344,39 @@ export function usePublishAgent(options: {
|
|||||||
if (next.length === 0) {
|
if (next.length === 0) {
|
||||||
return { ok: false, message: '未能解析任何规格维' };
|
return { ok: false, message: '未能解析任何规格维' };
|
||||||
}
|
}
|
||||||
|
|
||||||
specAxes.value = next;
|
specAxes.value = next;
|
||||||
|
|
||||||
|
if (animations.length > 0) {
|
||||||
|
const { gen } = await runTypingTask('spec', 'spec', async (signal) => {
|
||||||
|
for (const anim of animations) {
|
||||||
|
if (signal.aborted) break;
|
||||||
|
const axis = specAxes.value[anim.axisIdx];
|
||||||
|
const slot = axis?.selectedValues[anim.slotIdx];
|
||||||
|
if (!slot) continue;
|
||||||
|
slot.token = anim.from;
|
||||||
|
await typewriterReplace(
|
||||||
|
() => slot.token,
|
||||||
|
(v) => {
|
||||||
|
slot.token = v;
|
||||||
|
},
|
||||||
|
anim.to,
|
||||||
|
{ signal, eraseIntervalMs: 14, typeIntervalMs: 22 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 被同字段新写入打断时,由新一轮接管轴与 SKU,避免踩踏
|
||||||
|
if (!isLatestTyping('spec', gen)) {
|
||||||
|
return { ok: true, message: '规格写入已被更新覆盖' };
|
||||||
|
}
|
||||||
|
for (const anim of animations) {
|
||||||
|
const slot =
|
||||||
|
specAxes.value[anim.axisIdx]?.selectedValues[anim.slotIdx];
|
||||||
|
if (slot) slot.token = anim.to;
|
||||||
|
}
|
||||||
|
agentTypingField.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
// 有编码绑定时关闭行序兜底,完全按 agent 的 outSkuSn 对齐
|
// 有编码绑定时关闭行序兜底,完全按 agent 的 outSkuSn 对齐
|
||||||
const deferredOrphans = syncSkuRowsFromSpecs({
|
const deferredOrphans = syncSkuRowsFromSpecs({
|
||||||
orphanByIndex: assignments.length === 0,
|
orphanByIndex: assignments.length === 0,
|
||||||
@@ -212,10 +400,10 @@ export function usePublishAgent(options: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyClientTool(
|
async function applyClientTool(
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
): ClientToolResult {
|
): Promise<ClientToolResult> {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'ui_set_goods_name':
|
case 'ui_set_goods_name':
|
||||||
return applySetGoodsName(args);
|
return applySetGoodsName(args);
|
||||||
@@ -232,6 +420,7 @@ export function usePublishAgent(options: {
|
|||||||
getFormSnapshot,
|
getFormSnapshot,
|
||||||
applyClientTool,
|
applyClientTool,
|
||||||
applySetSpecAxes,
|
applySetSpecAxes,
|
||||||
|
agentTypingField,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ const draftId = computed(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const editorRef = ref<PublishEditorAgentApi | null>(null);
|
const editorRef = ref<PublishEditorAgentApi | null>(null);
|
||||||
|
/** 显式取 .value,避免把组件 ref 本体传给子组件导致偶发取不到命令面 */
|
||||||
|
const editorApi = computed(() => editorRef.value);
|
||||||
const { width: agentWidthPct, collapsed: agentCollapsed } =
|
const { width: agentWidthPct, collapsed: agentCollapsed } =
|
||||||
useColSidebarLayout('pdd-publish-agent');
|
useColSidebarLayout('pdd-publish-agent');
|
||||||
|
|
||||||
@@ -95,7 +97,7 @@ function goBack() {
|
|||||||
class="h-full min-h-0"
|
class="h-full min-h-0"
|
||||||
:draft-id="draftId"
|
:draft-id="draftId"
|
||||||
:collapsed="agentCollapsed"
|
:collapsed="agentCollapsed"
|
||||||
:editor-api="editorRef"
|
:editor-api="editorApi"
|
||||||
@expand="agentCollapsed = false"
|
@expand="agentCollapsed = false"
|
||||||
@collapse="agentCollapsed = true"
|
@collapse="agentCollapsed = true"
|
||||||
/>
|
/>
|
||||||
|
|||||||
+70
@@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* 智能体改写输入框时的逐字删写动效(按 Unicode 码点,避免拆开 emoji)。
|
||||||
|
*/
|
||||||
|
|
||||||
|
function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
reject(new DOMException('Aborted', 'AbortError'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const onAbort = () => {
|
||||||
|
window.clearTimeout(timer);
|
||||||
|
reject(new DOMException('Aborted', 'AbortError'));
|
||||||
|
};
|
||||||
|
const timer = window.setTimeout(() => {
|
||||||
|
signal?.removeEventListener('abort', onAbort);
|
||||||
|
resolve();
|
||||||
|
}, ms);
|
||||||
|
signal?.addEventListener('abort', onAbort, { once: true });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function codePoints(text: string): string[] {
|
||||||
|
return Array.from(text ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TypewriterOptions = {
|
||||||
|
/** 逐字删除间隔 ms,默认 16 */
|
||||||
|
eraseIntervalMs?: number;
|
||||||
|
/** 逐字输入间隔 ms,默认 28 */
|
||||||
|
typeIntervalMs?: number;
|
||||||
|
signal?: AbortSignal;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 先清空当前文案,再逐字写入 next;同字段连续写入时用 AbortSignal 打断上一段。
|
||||||
|
*/
|
||||||
|
export async function typewriterReplace(
|
||||||
|
get: () => string,
|
||||||
|
set: (value: string) => void,
|
||||||
|
next: string,
|
||||||
|
options?: TypewriterOptions,
|
||||||
|
): Promise<void> {
|
||||||
|
const eraseMs = options?.eraseIntervalMs ?? 16;
|
||||||
|
const typeMs = options?.typeIntervalMs ?? 28;
|
||||||
|
const signal = options?.signal;
|
||||||
|
const target = next ?? '';
|
||||||
|
|
||||||
|
const throwIfAborted = () => {
|
||||||
|
if (signal?.aborted) {
|
||||||
|
throw new DOMException('Aborted', 'AbortError');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
throwIfAborted();
|
||||||
|
const from = codePoints(get() ?? '');
|
||||||
|
for (let i = from.length; i > 0; i -= 1) {
|
||||||
|
throwIfAborted();
|
||||||
|
set(from.slice(0, i - 1).join(''));
|
||||||
|
if (eraseMs > 0) await sleep(eraseMs, signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
const to = codePoints(target);
|
||||||
|
for (let i = 0; i < to.length; i += 1) {
|
||||||
|
throwIfAborted();
|
||||||
|
set(to.slice(0, i + 1).join(''));
|
||||||
|
if (typeMs > 0) await sleep(typeMs, signal);
|
||||||
|
}
|
||||||
|
set(target);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user