f5368c825f
重构了editor agent. 使得它能进行多轮工具调用, 把工具调用的结果嵌入到上下文里。 对于上下文的图片, 使用哈希后的id用来引用,不暴露细节信息to llm。 当前实现, 状态直接维护在json doc里, 需要对整个doc加锁,任务目前只能串行。 把SSE改成一个简单请求( 因为生图工具耗时需要二次确认, 不需要再实时展示给用户进度)。 增加确认/取消生图操作。 把tool的参数和错误情况做了反馈。 TODO: 工具调用的规范/prompt还可以改进。 改用external job来做素材生成, 针对来自editor agent 的生成会把生成资产的引用加入到结果json里, 客户端轮询 external job 确定生成状态, 发现结束了或者失败了就 重新get 会话,后端重新提供会话的时候把 结果插入回会话历史里,用来让 ai 引用 以及显示 --------- Co-authored-by: 段舒康 <kdletters@qq.com> Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/76 Reviewed-by: 段舒康 <kdletters@qq.com> Co-authored-by: 王德宇 <kvtodev@outlook.com> Co-committed-by: 王德宇 <kvtodev@outlook.com>
188 lines
5.4 KiB
TypeScript
188 lines
5.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import {
|
|
cancelEditorAgentToolCall,
|
|
confirmEditorAgentToolCall,
|
|
createEditorAgentConversation,
|
|
deleteEditorAgentConversation,
|
|
getEditorAgentConversation,
|
|
listEditorAgentConversations,
|
|
sendEditorAgentMessage,
|
|
} from './editorAgentClient';
|
|
|
|
const requestJsonMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('../apiClient', () => ({
|
|
requestJson: requestJsonMock,
|
|
}));
|
|
|
|
describe('editorAgentClient', () => {
|
|
afterEach(() => {
|
|
requestJsonMock.mockReset();
|
|
});
|
|
|
|
it('uses the planned editor agent conversation CRUD routes', async () => {
|
|
requestJsonMock
|
|
.mockResolvedValueOnce({
|
|
conversations: [
|
|
{
|
|
conversationId: 'conversation-1',
|
|
projectId: 'project-1',
|
|
title: '角色参考',
|
|
createdAt: '2026-07-03T00:00:00.000Z',
|
|
updatedAt: '2026-07-03T00:00:00.000Z',
|
|
},
|
|
],
|
|
})
|
|
.mockResolvedValueOnce({
|
|
conversation: {
|
|
conversationId: 'conversation-2',
|
|
projectId: 'project-1',
|
|
title: '新对话',
|
|
messages: [],
|
|
createdAt: '2026-07-03T00:00:00.000Z',
|
|
updatedAt: '2026-07-03T00:01:00.000Z',
|
|
},
|
|
})
|
|
.mockResolvedValueOnce({
|
|
conversation: {
|
|
conversationId: 'conversation-1',
|
|
projectId: 'project-1',
|
|
title: '角色参考',
|
|
messages: [],
|
|
createdAt: '2026-07-03T00:00:00.000Z',
|
|
updatedAt: '2026-07-03T00:02:00.000Z',
|
|
},
|
|
})
|
|
.mockResolvedValueOnce({
|
|
conversation: {
|
|
conversationId: 'conversation-1',
|
|
projectId: 'project-1',
|
|
title: '角色参考',
|
|
messages: [],
|
|
createdAt: '2026-07-03T00:00:00.000Z',
|
|
updatedAt: '2026-07-03T00:02:00.000Z',
|
|
},
|
|
});
|
|
|
|
await listEditorAgentConversations('project-1');
|
|
await createEditorAgentConversation('project-1', { title: '新对话' });
|
|
await getEditorAgentConversation('conversation-1');
|
|
await deleteEditorAgentConversation('conversation-1');
|
|
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
1,
|
|
'/api/editor/projects/project-1/agent-conversations',
|
|
{ method: 'GET' },
|
|
'读取画布 Agent 会话列表失败',
|
|
);
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
2,
|
|
'/api/editor/projects/project-1/agent-conversations',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ title: '新对话' }),
|
|
}),
|
|
'创建画布 Agent 会话失败',
|
|
);
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
3,
|
|
'/api/editor/agent-conversations/conversation-1',
|
|
{ method: 'GET' },
|
|
'读取画布 Agent 会话失败',
|
|
);
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
4,
|
|
'/api/editor/agent-conversations/conversation-1',
|
|
{ method: 'DELETE' },
|
|
'删除画布 Agent 会话失败',
|
|
);
|
|
});
|
|
|
|
it('sends an editor agent message and returns delta messages', async () => {
|
|
const responseBody = {
|
|
conversation: {
|
|
conversationId: 'conversation-1',
|
|
projectId: 'project-1',
|
|
title: '帮我把角色改成像素风',
|
|
updatedAt: '2026-07-03T00:00:01.000Z',
|
|
},
|
|
deltaMessages: [
|
|
{
|
|
id: 1,
|
|
role: 'assistant',
|
|
text: '我来处理',
|
|
attachments: [],
|
|
toolCall: null,
|
|
createdAt: '2026-07-03T00:00:00.000Z',
|
|
},
|
|
],
|
|
errorMessage: null,
|
|
};
|
|
requestJsonMock.mockResolvedValueOnce(responseBody);
|
|
|
|
const result = await sendEditorAgentMessage('conversation-1', {
|
|
clientMessageId: 'client-message-1',
|
|
text: '帮我把角色改成像素风',
|
|
attachments: [],
|
|
});
|
|
|
|
expect(result).toEqual(responseBody);
|
|
expect(requestJsonMock).toHaveBeenCalledWith(
|
|
'/api/editor/agent-conversations/conversation-1/messages',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
clientMessageId: 'client-message-1',
|
|
text: '帮我把角色改成像素风',
|
|
attachments: [],
|
|
}),
|
|
}),
|
|
'发送画布 Agent 消息失败',
|
|
expect.objectContaining({
|
|
timeoutMs: 1_200_000,
|
|
authImpact: 'local',
|
|
retry: {
|
|
maxRetries: 1,
|
|
baseDelayMs: 250,
|
|
maxDelayMs: 250,
|
|
retryUnsafeMethods: true,
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('confirms and cancels a pending tool call by numeric message id', async () => {
|
|
requestJsonMock
|
|
.mockResolvedValueOnce({ ok: true })
|
|
.mockResolvedValueOnce({ ok: true });
|
|
|
|
await expect(
|
|
confirmEditorAgentToolCall('conversation/1', 7),
|
|
).resolves.toBeUndefined();
|
|
await expect(
|
|
cancelEditorAgentToolCall('conversation/1', 7),
|
|
).resolves.toBeUndefined();
|
|
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
1,
|
|
'/api/editor/agent-conversations/conversation%2F1/messages/7/confirm',
|
|
{ method: 'POST' },
|
|
'确认画布 Agent 操作失败',
|
|
{
|
|
timeoutMs: 1_200_000,
|
|
authImpact: 'local',
|
|
},
|
|
);
|
|
expect(requestJsonMock).toHaveBeenNthCalledWith(
|
|
2,
|
|
'/api/editor/agent-conversations/conversation%2F1/messages/7/cancel',
|
|
{ method: 'POST' },
|
|
'取消画布 Agent 操作失败',
|
|
{ authImpact: 'local' },
|
|
);
|
|
});
|
|
});
|