Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(agent): open the prompt editor when editorData is an empty document (#23778)

Innei0f227a58e37d
diff --git a/apps/server/src/services/agent/index.test.ts b/apps/server/src/services/agent/index.test.ts
index efb32b27857..8475b72eadf 100644
--- a/apps/server/src/services/agent/index.test.ts
+++ b/apps/server/src/services/agent/index.test.ts
@@ -1118,5 +1118,43 @@ describe('AgentService', () => {
message: 'Agent not found',
});
});
+
+ const createPromptService = () => {
+ const mockAgentModel = {
+ getAgentConfigById: vi.fn().mockResolvedValue({ id: 'agent-1' }),
+ updateConfig: vi.fn().mockResolvedValue(undefined),
+ };
+ (AgentModel as any).mockImplementation(function () {
+ return mockAgentModel;
+ });
+ (parseAgentConfig as any).mockReturnValue({});
+
+ return { mockAgentModel, service: new AgentService(mockDb, mockUserId) };
+ };
+
+ it('clears stale editorData when only systemRole is written', async () => {
+ const { mockAgentModel, service } = createPromptService();
+
+ await service.updateAgentConfig('agent-1', { systemRole: 'new prompt' });
+
+ expect(mockAgentModel.updateConfig).toHaveBeenCalledWith(
+ 'agent-1',
+ { editorData: null, systemRole: 'new prompt' },
+ undefined,
+ );
+ });
+
+ it('keeps editorData written together with systemRole', async () => {
+ const { mockAgentModel, service } = createPromptService();
+ const editorData = { root: { children: [{ type: 'paragraph' }], type: 'root' } };
+
+ await service.updateAgentConfig('agent-1', { editorData, systemRole: 'new prompt' });
+
+ expect(mockAgentModel.updateConfig).toHaveBeenCalledWith(
+ 'agent-1',
+ { editorData, systemRole: 'new prompt' },
+ undefined,
+ );
+ });
});
});
diff --git a/apps/server/src/services/agent/index.ts b/apps/server/src/services/agent/index.ts
index d32eb960905..5ba2f3c5be4 100644
--- a/apps/server/src/services/agent/index.ts
+++ b/apps/server/src/services/agent/index.ts
@@ -429,6 +429,12 @@ export class AgentService {
value: PartialDeep<AgentItem>,
replaceWorkingDirDeviceIds?: string[],
): Promise<UpdateAgentResult> {
+ // The prompt editor prefers editorData over systemRole, so a systemRole-only
+ // write must drop the old rich document or the editor shows stale content.
+ if ('systemRole' in value && !('editorData' in value)) {
+ value = { ...value, editorData: null } as PartialDeep<AgentItem>;
+ }
+
if (AGENT_SHARE_ALLOWED_PROVIDERS && touchesShareModel(value)) {
const share = await new AgentShareModel(this.db, this.userId, this.workspaceId).getByAgentId(
agentId,
diff --git a/src/routes/(main)/agent/profile/features/EditorCanvas/index.test.tsx b/src/routes/(main)/agent/profile/features/EditorCanvas/index.test.tsx
index 4b221ef8628..894315567ff 100644
--- a/src/routes/(main)/agent/profile/features/EditorCanvas/index.test.tsx
+++ b/src/routes/(main)/agent/profile/features/EditorCanvas/index.test.tsx
@@ -4,6 +4,7 @@
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { EMPTY_EDITOR_STATE } from '@/libs/editor/constants';
import { type SaveStatus } from '@/types/saveState';
import EditorCanvas from './index';
@@ -255,7 +256,7 @@ describe('Agent profile EditorCanvas', () => {
});
it('seeds source mode from the persisted Markdown when editor data also exists', async () => {
- const editorData = { root: { children: ['parsed source'] } };
+ const editorData = { root: { type: 'root', children: ['parsed source'] } };
const persistedMarkdown = '<details>\n\n raw spacing\n\n</details>';
agentStoreState.agentMap = {
'agent-a': {
@@ -276,8 +277,8 @@ describe('Agent profile EditorCanvas', () => {
it('ignores source change callbacks caused by a programmatic value sync', async () => {
permissionState.allowed = true;
- const initialEditorData = { root: { children: ['initial'] } };
- const serverEditorData = { root: { children: ['server'] } };
+ const initialEditorData = { root: { type: 'root', children: ['initial'] } };
+ const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: initialEditorData, systemRole: 'initial prompt' },
};
@@ -390,8 +391,8 @@ describe('Agent profile EditorCanvas', () => {
it('binds a draft save to its original agent and loads the next agent content', async () => {
permissionState.allowed = true;
- const agentAEditorData = { root: { children: ['agent-a'] } };
- const agentBEditorData = { root: { children: ['agent-b'] } };
+ const agentAEditorData = { root: { type: 'root', children: ['agent-a'] } };
+ const agentBEditorData = { root: { type: 'root', children: ['agent-b'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: agentAEditorData, systemRole: 'agent-a prompt' },
'agent-b': { editorData: agentBEditorData, systemRole: 'agent-b prompt' },
@@ -403,7 +404,7 @@ describe('Agent profile EditorCanvas', () => {
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', agentAEditorData));
await act(() => new Promise((resolve) => setTimeout(resolve, 0)));
- editorDocuments.json = { root: { children: ['agent-a edited'] } };
+ editorDocuments.json = { root: { type: 'root', children: ['agent-a edited'] } };
editorDocuments.markdown = 'agent-a edited';
act(() => editorProps.last?.onTextChange(editor));
expect(handleContentChange).toHaveBeenCalledWith('agent-a', expect.any(Function), editor);
@@ -436,8 +437,8 @@ describe('Agent profile EditorCanvas', () => {
it('replaces hydrated editor data with a later server config before local editing starts', async () => {
permissionState.allowed = true;
- const hydratedEditorData = { root: { children: ['hydrated'] } };
- const serverEditorData = { root: { children: ['server'] } };
+ const hydratedEditorData = { root: { type: 'root', children: ['hydrated'] } };
+ const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: hydratedEditorData, systemRole: 'hydrated prompt' },
};
@@ -462,7 +463,7 @@ describe('Agent profile EditorCanvas', () => {
it('ignores the editor delayed callback for a programmatic document sync', async () => {
vi.useFakeTimers();
permissionState.allowed = true;
- const editorData = { root: { children: ['programmatic'] } };
+ const editorData = { root: { type: 'root', children: ['programmatic'] } };
agentStoreState.agentMap = {
'agent-a': { editorData, systemRole: 'programmatic prompt' },
};
@@ -481,8 +482,8 @@ describe('Agent profile EditorCanvas', () => {
it('does not overwrite a local draft with a later editor-data refresh', async () => {
permissionState.allowed = true;
- const initialEditorData = { root: { children: ['initial'] } };
- const serverEditorData = { root: { children: ['server'] } };
+ const initialEditorData = { root: { type: 'root', children: ['initial'] } };
+ const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: initialEditorData, systemRole: 'initial prompt' },
};
@@ -491,7 +492,7 @@ describe('Agent profile EditorCanvas', () => {
act(() => editorProps.last?.onInit());
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', initialEditorData));
await act(() => new Promise((resolve) => setTimeout(resolve, 0)));
- editorDocuments.json = { root: { children: ['local draft'] } };
+ editorDocuments.json = { root: { type: 'root', children: ['local draft'] } };
editorDocuments.markdown = 'local draft';
act(() => editorProps.last?.onTextChange(editor));
editor.setDocument.mockClear();
@@ -550,9 +551,29 @@ describe('Agent profile EditorCanvas', () => {
expect(screen.queryByTestId('prompt-save-status')).not.toBeInTheDocument();
});
+ it('falls back to systemRole when editorData is an empty root document', async () => {
+ const emptyEditorData = {
+ root: { children: [], direction: 'ltr', format: '', indent: 0, type: 'root', version: 1 },
+ };
+ agentStoreState.agentMap = {
+ 'agent-a': { editorData: emptyEditorData, systemRole: '# API written prompt' },
+ };
+
+ render(<EditorCanvas />);
+
+ expect(editorProps.last?.content).toEqual(EMPTY_EDITOR_STATE);
+
+ act(() => editorProps.last?.onInit());
+
+ await waitFor(() =>
+ expect(editor.setDocument).toHaveBeenCalledWith('markdown', '# API written prompt'),
+ );
+ expect(editor.setDocument).not.toHaveBeenCalledWith('json', emptyEditorData);
+ });
+
it('ignores a stream inherited from the previously active agent', async () => {
permissionState.allowed = true;
- const agentBEditorData = { root: { children: ['agent-b'] } };
+ const agentBEditorData = { root: { type: 'root', children: ['agent-b'] } };
agentStoreState.activeAgentId = 'agent-b';
agentStoreState.agentMap = {
'agent-b': { editorData: agentBEditorData, systemRole: 'agent-b prompt' },
diff --git a/src/routes/(main)/agent/profile/features/EditorCanvas/index.tsx b/src/routes/(main)/agent/profile/features/EditorCanvas/index.tsx
index da7cb1dd472..2c35b990fca 100644
--- a/src/routes/(main)/agent/profile/features/EditorCanvas/index.tsx
+++ b/src/routes/(main)/agent/profile/features/EditorCanvas/index.tsx
@@ -18,6 +18,7 @@ import { useResourceAccess } from '@/features/ResourcePermission/useResourceAcce
import { useLocalStorageState } from '@/hooks/useLocalStorageState';
import { usePermission } from '@/hooks/usePermission';
import { EMPTY_EDITOR_STATE } from '@/libs/editor/constants';
+import { isValidEditorData } from '@/libs/editor/isValidEditorData';
import { useAgentStore } from '@/store/agent';
import { useMentionOptions } from '../ProfileEditor/MentionList';
@@ -102,9 +103,7 @@ const AgentEditorCanvas = memo<AgentEditorCanvasProps>(({ agentId, description,
updateConfigById(targetAgentId, payload, { rethrow: true, showErrorMessage: false }),
[updateConfigById],
);
- const [initialLoad] = useState(
- editorData === undefined || editorData?.root === undefined ? EMPTY_EDITOR_STATE : editorData,
- );
+ const [initialLoad] = useState(isValidEditorData(editorData) ? editorData : EMPTY_EDITOR_STATE);
const mentionOptions = useMentionOptions();
const editor = useProfileStore((s) => s.editor);
const handleContentChange = useProfileStore((s) => s.handleContentChange);
@@ -341,7 +340,7 @@ const AgentEditorCanvas = memo<AgentEditorCanvasProps>(({ agentId, description,
// Don't init if streaming is in progress
if (streamingInProgress) return;
try {
- if (editorData && editorData?.root !== undefined) {
+ if (isValidEditorData(editorData)) {
setProgrammaticDocument(editor, 'json', editorData, systemRole);
lastSyncedEditorDataRef.current = structuredClone(editorData);
} else if (systemRole) {
@@ -380,7 +379,7 @@ const AgentEditorCanvas = memo<AgentEditorCanvasProps>(({ agentId, description,
if (!editor || !editorInit || !contentInit) return;
if (streamingInProgress) return;
- const hasEditorData = !!editorData && editorData?.root !== undefined;
+ const hasEditorData = isValidEditorData(editorData);
if (hasEditorData) {
lastSyncedRoleRef.current = undefined;
// A fresh server response is authoritative while this agent has not been