feat: 完善智能体图片聊天与会话恢复

- 增加私有图片上传、绑定、历史回显与生命周期清理

- 支持输入草稿恢复、图片交互和模型图片能力约束

- 修复旧脏会话幂等删除与前端会话恢复
This commit is contained in:
2026-07-17 19:54:26 +08:00
parent 62d763199f
commit 1e6158be77
62 changed files with 5333 additions and 189 deletions

View File

@@ -0,0 +1,98 @@
// @vitest-environment happy-dom
import { createPinia, setActivePinia } from 'pinia';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useUserStore } from '@easyflow/stores';
import { clearAgentChatBrowserCache } from '#/utils/agent-chat-cache';
import { sendAgentChat } from './api';
import { agentChatRuntimeManager } from './agentChatRuntimeManager';
vi.mock('./api', () => ({
generateAgentSessionId: vi.fn(),
sendAgentChat: vi.fn(),
stopAgentChatStream: vi.fn(),
}));
describe('agentChatRuntimeManager', () => {
beforeEach(() => {
setActivePinia(createPinia());
sessionStorage.clear();
vi.clearAllMocks();
});
it('replaces draft image URLs and isolates snapshots by account', async () => {
let callbacks: any;
vi.mocked(sendAgentChat).mockImplementation((_data, options) => {
callbacks = options;
return Promise.resolve() as any;
});
const userStore = useUserStore();
const firstAccount = {
avatar: '',
id: 'user-1',
loginName: 'admin',
nickname: '管理员',
tenantId: 'tenant-1',
};
userStore.setUserInfo(firstAccount);
await agentChatRuntimeManager.start({
agentId: 'agent-1',
images: [
{
name: 'draft.png',
previewUrl: '/api/v1/agent/media/content?reference=draft%3Aupload-1',
status: 'ready',
uploadId: 'upload-1',
},
],
prompt: '识别图片',
sessionId: '101',
});
callbacks.onMessage({
data: JSON.stringify({
domain: 'SYSTEM',
payload: {
images: [
{
imageRef: 'formal:101:201:0:png',
name: 'draft.png',
previewUrl:
'/api/v1/agent/media/content?reference=formal:101:201:0:png',
},
],
},
type: 'INPUT_ACCEPTED',
}),
});
const accepted = agentChatRuntimeManager.getSnapshot('101');
const userMessage = accepted?.items.find(
(item) => item.type === 'message' && item.role === 'user',
);
expect(
userMessage?.type === 'message' ? userMessage.images?.[0] : null,
).toEqual(
expect.objectContaining({
imageRef: 'formal:101:201:0:png',
previewUrl:
'/api/v1/agent/media/content?reference=formal:101:201:0:png',
}),
);
userStore.setUserInfo({
...firstAccount,
id: 'user-2',
loginName: 'other',
nickname: '其他用户',
});
expect(agentChatRuntimeManager.getSnapshot('101')).toBeUndefined();
clearAgentChatBrowserCache(firstAccount);
userStore.setUserInfo(firstAccount);
expect(agentChatRuntimeManager.getSnapshot('101')).toBeUndefined();
});
});