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.

34.4 KB
Raw Blame
import { GeneralChatAgent, GraphAgent } from '@lobechat/agent-runtime';
import { PageAgentIdentifier } from '@lobechat/builtin-tool-page-agent';
import { SELF_FEEDBACK_INTENT_IDENTIFIER } from '@lobechat/builtin-tool-self-iteration';
import { promptAssistantName } from '@lobechat/const';
import { RequestTrigger } from '@lobechat/types';
import type * as ModelBankModule from 'model-bank';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { createServerAgentToolsEngine } from '@/server/modules/Mecha';
import { AgentRuntimeService } from '@/server/services/agentRuntime';
import { enqueueAgentSignalSourceEvent } from '@/server/services/agentSignal';

import { AiAgentService } from '../index';
import { toolSetsFrom } from './helpers/connectorToolSets';

const runtimeAgentConfig = (chatConfig: Record<string, unknown>, id: string, slug: string) => ({
  chatConfig,
  id,
  model: 'gpt-4',
  plugins: [],
  provider: 'openai',
  slug,
  systemRole: '',
});

const inboxRunInput = () => ({
  agentId: 'agent-inbox',
  prompt: 'Hello',
});

const {
  mockCreateOperation,
  mockGetAgentConfig,
  mockGetBuiltinAgent,
  mockGetInfoForAIGeneration,
  mockGetModelMetadata,
  mockGetUserPreference,
  mockIsAgentSignalEnabledForUser,
  mockIsSandboxAvailableForUser,
  mockMessageCreate,
  mockMessageQuery,
  mockResolveTask,
  mockToolState,
  mockToolsEnv,
} = vi.hoisted(() => ({
  mockCreateOperation: vi.fn(),
  mockGetAgentConfig: vi.fn(),
  mockGetBuiltinAgent: vi.fn(),
  mockGetInfoForAIGeneration: vi.fn(),
  mockGetModelMetadata: vi.fn(),
  mockGetUserPreference: vi.fn(),
  mockIsAgentSignalEnabledForUser: vi.fn(),
  mockIsSandboxAvailableForUser: vi.fn(),
  mockMessageCreate: vi.fn(),
  mockMessageQuery: vi.fn(),
  mockResolveTask: vi.fn(),
  mockToolState: { enabledToolIds: [] as string[] },
  mockToolsEnv: {
    MULTIMODAL_UNDERSTANDING_MODEL: undefined as string | undefined,
    MULTIMODAL_UNDERSTANDING_PROVIDER: undefined as string | undefined,
  },
}));

vi.mock('@/server/services/sandbox/availability', () => ({
  isSandboxAvailableForUser: mockIsSandboxAvailableForUser,
}));

vi.mock('@/envs/tools', () => ({
  toolsEnv: mockToolsEnv,
}));

vi.mock('@/libs/trusted-client', () => ({
  generateTrustedClientToken: vi.fn().mockReturnValue(undefined),
  getTrustedClientTokenForSession: vi.fn().mockResolvedValue(undefined),
  isTrustedClientEnabled: vi.fn().mockReturnValue(false),
}));

vi.mock('@/database/models/message', () => ({
  MessageModel: vi.fn().mockImplementation(function () {
    return {
      create: mockMessageCreate,
      getLatestNonToolMessageId: vi.fn().mockResolvedValue(undefined),
      getLatestSpineMessageId: vi.fn().mockResolvedValue(undefined),
      query: mockMessageQuery,
      update: vi.fn().mockResolvedValue({}),
    };
  }),
}));

vi.mock('@/database/models/aiModel', () => ({
  AiModelModel: vi.fn().mockImplementation(function () {
    return {
      findByIdAndProvider: mockGetModelMetadata,
    };
  }),
}));

vi.mock('@/database/models/agent', () => ({
  AgentModel: vi.fn().mockImplementation(function () {
    return {
      getAgentConfig: vi.fn(),
      getBuiltinAgent: mockGetBuiltinAgent,
      queryAgents: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/agent', () => ({
  AgentService: vi.fn().mockImplementation(function () {
    return {
      getAgentConfig: mockGetAgentConfig,
    };
  }),
}));

// Keep the real per-agent opt-in rule so these tests exercise the same Lobe AI
// default-on / opt-out logic production uses; only the user rollout is mocked.
vi.mock('@/server/services/agentSignal/featureGate', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  isAgentSignalEnabledForUser: mockIsAgentSignalEnabledForUser,
}));

vi.mock('@/server/services/agentSignal', () => ({
  enqueueAgentSignalSourceEvent: vi.fn().mockResolvedValue(undefined),
}));

vi.mock('@/database/models/plugin', () => ({
  PluginModel: vi.fn().mockImplementation(function () {
    return {
      query: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/database/models/connector', () => ({
  ConnectorModel: vi.fn().mockImplementation(function () {
    return {
      queryByIdentifiers: vi.fn().mockResolvedValue([]),
      resolveByIdentifiers: vi.fn().mockResolvedValue([]),
      resolveToolSets: toolSetsFrom(vi.fn().mockResolvedValue([])),
    };
  }),
}));

vi.mock('@/database/models/connectorTool', () => ({
  ConnectorToolModel: vi.fn().mockImplementation(function () {
    return {
      queryByConnector: vi.fn().mockResolvedValue([]),
      queryByConnectorIds: vi.fn().mockResolvedValue([]),
      queryAllByConnectorIds: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/database/models/topic', () => ({
  TopicModel: vi.fn().mockImplementation(function () {
    return {
      releaseTaskCallbackReservation: vi.fn().mockResolvedValue(undefined),
      tryReserveTaskCallback: vi.fn().mockResolvedValue(true),
      create: vi.fn().mockResolvedValue({ id: 'topic-1' }),
      findById: vi.fn().mockResolvedValue(null),
    };
  }),
}));

vi.mock('@/database/models/thread', () => ({
  ThreadModel: vi.fn().mockImplementation(function () {
    return {
      create: vi.fn(),
      findById: vi.fn(),
      update: vi.fn(),
    };
  }),
}));

vi.mock('@/database/models/user', () => ({
  UserModel: class {
    static getInfoForAIGeneration = mockGetInfoForAIGeneration;
    getUserPreference = mockGetUserPreference;
    getUserSettings = vi.fn().mockResolvedValue(undefined);
  },
}));

vi.mock('@/database/models/task', () => ({
  TaskModel: vi.fn().mockImplementation(function () {
    return {
      resolve: mockResolveTask,
    };
  }),
}));

// The run's project: coordinated by `agent-coordinator`, one member, no knowledge.
vi.mock('@/database/models/project', () => ({
  ProjectModel: vi.fn().mockImplementation(function () {
    const project = {
      coordinatorAgentId: 'agent-coordinator',
      id: 'project-1',
      identifier: 'APO',
      name: 'Apollo',
      status: 'active',
    };
    return {
      findById: vi.fn().mockResolvedValue(project),
      getEnabledKnowledgeBaseIds: vi.fn().mockResolvedValue([]),
      listAgents: vi.fn().mockResolvedValue([
        {
          agent: { id: 'agent-coordinator', title: 'Coordinator' },
          binding: { enabled: true, role: 'coordinator' },
        },
      ]),
      listKnowledgeBases: vi.fn().mockResolvedValue([]),
      listTasks: vi.fn().mockResolvedValue([]),
      resolveCoordinatorAgents: vi.fn(async (items: unknown[]) => items),
    };
  }),
}));

vi.mock('@/server/services/agentRuntime', () => ({
  AgentRuntimeService: vi.fn().mockImplementation(function () {
    return {
      createOperation: mockCreateOperation,
    };
  }),
}));

vi.mock('@/server/services/market', () => ({
  MarketService: vi.fn().mockImplementation(function () {
    return {
      getLobehubSkillManifests: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/composio', () => ({
  ComposioService: vi.fn().mockImplementation(function () {
    return {
      getComposioManifests: vi.fn().mockResolvedValue([]),
    };
  }),
}));

vi.mock('@/server/services/file', () => ({
  FileService: vi.fn().mockImplementation(function () {
    return {
      getFullFileUrl: (path: string | null) => Promise.resolve(path || ''),
      uploadFromUrl: vi.fn(),
    };
  }),
}));

vi.mock('@/server/modules/Mecha', () => ({
  createServerAgentToolsEngine: vi.fn().mockReturnValue({
    generateToolsDetailed: vi.fn().mockImplementation(function () {
      return { enabledToolIds: [...mockToolState.enabledToolIds], tools: [] };
    }),
    getEnabledPluginManifests: vi.fn().mockReturnValue(new Map()),
  }),
  serverMessagesEngine: vi.fn().mockResolvedValue([{ content: 'test', role: 'user' }]),
}));

vi.mock('@/server/services/deviceGateway', () => ({
  deviceGateway: {
    isConfigured: false,
    queryDeviceList: vi.fn().mockResolvedValue([]),
  },
}));

vi.mock('model-bank', async (importOriginal) => {
  const actual = await importOriginal<typeof ModelBankModule>();
  return {
    ...actual,
    LOBE_DEFAULT_MODEL_LIST: [
      {
        abilities: { audio: false, functionCall: true, video: false, vision: true },
        id: 'gpt-4',
        providerId: 'openai',
      },
      {
        abilities: { audio: false, functionCall: true, video: false, vision: false },
        id: 'text-only',
        providerId: 'openai',
      },
      {
        abilities: { audio: true, functionCall: true, video: true, vision: true },
        id: 'gemini-3.1-flash-lite-preview',
        providerId: 'google',
      },
    ],
  };
});

describe('AiAgentService.execAgent - builtin agent runtime config', () => {
  let service: AiAgentService;
  const mockDb = {} as any;
  const userId = 'test-user-id';
  const minimalGraph = {
    edges: [{ from: '__root__', instruction: 'Answer with the graph runtime.', to: 'answer' }],
    fields: {},
    name: 'answer-graph',
    nodes: { answer: { type: 'llm' } },
    terminal: 'answer',
  };

  beforeEach(() => {
    vi.clearAllMocks();
    mockMessageCreate.mockResolvedValue({ id: 'msg-1' });
    mockMessageQuery.mockResolvedValue([]);
    mockIsAgentSignalEnabledForUser.mockResolvedValue(true);
    mockIsSandboxAvailableForUser.mockResolvedValue(true);
    mockResolveTask.mockResolvedValue(null);
    mockToolState.enabledToolIds = [];
    mockGetInfoForAIGeneration.mockResolvedValue({
      preferredReplyLanguage: 'en-US',
      userName: 'Test User',
    });
    mockGetModelMetadata.mockResolvedValue(undefined);
    mockToolsEnv.MULTIMODAL_UNDERSTANDING_MODEL = 'vision-model';
    mockToolsEnv.MULTIMODAL_UNDERSTANDING_PROVIDER = 'test-provider';
    mockCreateOperation.mockResolvedValue({
      autoStarted: true,
      messageId: 'queue-msg-1',
      operationId: 'op-123',
      success: true,
    });
    mockGetBuiltinAgent.mockResolvedValue(null);
    service = new AiAgentService(mockDb, userId);
  });

  describe('graph runtime factory', () => {
    const getLatestAgentFactory = () => {
      const options = vi.mocked(AgentRuntimeService).mock.calls.at(-1)?.[2] as any;
      const agentFactory = options?.agentFactory;

      expect(agentFactory).toEqual(expect.any(Function));

      return agentFactory as (config: any) => unknown;
    };

    it('creates GraphAgent when graph mode is enabled with a valid graph snapshot', () => {
      service = new AiAgentService(mockDb, userId);

      const agent = getLatestAgentFactory()({
        agentConfig: {
          agencyConfig: {
            enableGraphMode: true,
            graph: minimalGraph,
          },
        },
        operationId: 'op-graph',
      });

      expect(agent).toBeInstanceOf(GraphAgent);
    });

    it('falls back to GeneralChatAgent when the graph snapshot is invalid', () => {
      service = new AiAgentService(mockDb, userId);

      const agent = getLatestAgentFactory()({
        agentConfig: {
          agencyConfig: {
            enableGraphMode: true,
            graph: { ...minimalGraph, edges: [] },
          },
        },
        operationId: 'op-invalid-graph',
      });

      expect(agent).toBeInstanceOf(GeneralChatAgent);
    });

    it('falls back to a legacy chatConfig graph snapshot', () => {
      service = new AiAgentService(mockDb, userId);

      const agent = getLatestAgentFactory()({
        agentConfig: {
          chatConfig: {
            enableGraphMode: true,
            graph: minimalGraph,
          },
        },
        operationId: 'op-legacy-graph',
      });

      expect(agent).toBeInstanceOf(GraphAgent);
    });

    it('keeps an upstream runtime agent factory authoritative', () => {
      const upstreamAgent = { runner: vi.fn() };
      const upstreamFactory = vi.fn(function () {
        return upstreamAgent;
      });
      service = new AiAgentService(mockDb, userId, {
        runtimeOptions: {
          agentFactory: upstreamFactory,
        },
      } as any);

      const config = {
        agentConfig: {
          chatConfig: {
            enableGraphMode: true,
            graph: minimalGraph,
          },
        },
        operationId: 'op-upstream',
      };
      const agent = getLatestAgentFactory()(config);

      expect(agent).toBe(upstreamAgent);
      expect(upstreamFactory).toHaveBeenCalledWith(config);
    });
  });

  it('materializes a builtin agent addressed by slug when no row exists yet', async () => {
    // Background self-iteration runs dispatch via execAgent({ slug }) before any
    // persisted row exists. The first resolve (by slug) misses; execAgent must
    // lazily materialize the virtual builtin row (getBuiltinAgent) and re-resolve
    // — without it the run throws `Agent not found: self-reflection`.
    mockGetAgentConfig.mockResolvedValueOnce(null).mockResolvedValueOnce({
      chatConfig: {},
      id: 'agent-self-reflection',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'self-reflection',
      systemRole: '',
    });
    mockGetBuiltinAgent.mockResolvedValueOnce({
      id: 'agent-self-reflection',
      slug: 'self-reflection',
    });

    await service.execAgent({ prompt: 'reflect', slug: 'self-reflection' });

    expect(mockGetBuiltinAgent).toHaveBeenCalledWith('self-reflection');
    expect(mockCreateOperation).toHaveBeenCalledTimes(1);
    expect(mockCreateOperation.mock.calls[0][0].agentConfig.slug).toBe('self-reflection');
  });

  it('throws for an unknown non-builtin identifier without materializing a row', async () => {
    mockGetAgentConfig.mockResolvedValue(null);

    await expect(service.execAgent({ agentId: 'does-not-exist', prompt: 'hi' })).rejects.toThrow(
      'Agent not found: does-not-exist',
    );
    expect(mockGetBuiltinAgent).not.toHaveBeenCalled();
  });

  it('should merge runtime systemRole for inbox agent when DB systemRole is empty', async () => {
    // Inbox agent with no user-customized systemRole in DB
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-inbox',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'inbox',
      systemRole: '', // empty in DB
    });

    await service.execAgent(inboxRunInput());

    // Verify createOperation was called with agentConfig containing the runtime systemRole
    expect(mockCreateOperation).toHaveBeenCalledTimes(1);
    const callArgs = mockCreateOperation.mock.calls[0][0];
    // Derived, not a literal: the assistant's name comes from the branding slot,
    // so a hardcoded 'Lobe' here only passes on the upstream default.
    expect(callArgs.agentConfig.systemRole).toContain(`You are ${promptAssistantName('Lobe')}`);
    // Model identity is injected by ModelInfoProvider now, not the `{{model}}`
    // template placeholder; `{{date}}` still proves the runtime template merged.
    expect(callArgs.agentConfig.systemRole).toContain('{{date}}');
  });

  it('should pass user response language into web onboarding runtime systemRole', async () => {
    mockGetInfoForAIGeneration.mockResolvedValue({
      preferredReplyLanguage: 'zh-CN',
      userName: 'Test User',
    });
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-web-onboarding',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'web-onboarding',
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-web-onboarding',
      prompt: '你好',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.agentConfig.agencyConfig?.executionTarget).toBe('none');
    expect(callArgs.agentConfig.systemRole).toContain('Preferred reply language: zh-CN');
    expect(callArgs.agentConfig.systemRole).toContain(
      'Every visible reply, question, and visible choice label must be entirely in zh-CN',
    );
  });

  it('should NOT override user-customized systemRole for inbox agent', async () => {
    const customSystemRole = 'You are a custom assistant.';
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-inbox',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'inbox',
      systemRole: customSystemRole, // user has customized
    });

    await service.execAgent(inboxRunInput());

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.agentConfig.systemRole).toBe(`${customSystemRole}\n\n${REPLY_LANGUAGE_ONLY}`);
  });

  // Regular agents get no builtin runtime prompt. They do get the user's reply
  // language appended — the same rule the client runtime applies, now shared
  // through `@lobechat/mecha`.
  const REPLY_LANGUAGE_ONLY =
    'Preferred reply language: en-US. Use this language unless the user explicitly asks to switch.';

  it('should not apply runtime config for non-builtin agents', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'my-custom-slug', // not a builtin slug
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-custom',
      prompt: 'Hello',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    // No runtime prompt applied — only the reply-language instruction.
    expect(callArgs.agentConfig.systemRole).toBe(REPLY_LANGUAGE_ONLY);
  });

  it('should not apply runtime config for agents without slug', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-no-slug',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-no-slug',
      prompt: 'Hello',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.agentConfig.systemRole).toBe(REPLY_LANGUAGE_ONLY);
  });

  it('should persist request trigger metadata on the created user message', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { topicId: 'topic-1' },
      prompt: 'Hello',
      trigger: RequestTrigger.Onboarding,
    });

    expect(mockMessageCreate).toHaveBeenCalledWith(
      expect.objectContaining({
        content: 'Hello',
        metadata: { trigger: RequestTrigger.Onboarding },
        role: 'user',
      }),
      undefined,
    );
  });

  it('should inject self-feedback intent tool for Lobe AI when user gate is enabled', async () => {
    mockGetAgentConfig.mockResolvedValue(runtimeAgentConfig({}, 'agent-inbox', 'inbox'));

    await service.execAgent(inboxRunInput());

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeDefined();
    expect(callArgs.toolSet.sourceMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBe('builtin');
  });

  /**
   * The Labs → Self-evolving switch is the user gate. The run already reads `users.preference`
   * for expertise injection, so tool exposure must reuse that row instead of reading it again.
   */
  it('decides the self-feedback intent tool from the preference the run already loaded', async () => {
    mockGetUserPreference.mockResolvedValue({ lab: { enableSelfLearning: true } });
    mockGetAgentConfig.mockResolvedValue(runtimeAgentConfig({}, 'agent-inbox', 'inbox'));

    await service.execAgent({ agentId: 'agent-inbox', prompt: 'Hello' });

    expect(mockIsAgentSignalEnabledForUser).toHaveBeenCalledWith(expect.anything(), userId, {
      preference: { lab: { enableSelfLearning: true } },
    });
    expect(mockGetUserPreference).toHaveBeenCalledOnce();
  });

  // No per-agent switch: Labs → Self-evolving covers every agent.
  it('should inject self-feedback intent tool for custom agents without any per-agent setting', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'custom-agent',
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-custom',
      prompt: 'Hello',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeDefined();
  });

  it('should not inject self-feedback intent tool for any agent when Labs Self-evolving is off', async () => {
    mockIsAgentSignalEnabledForUser.mockResolvedValue(false);
    mockGetAgentConfig.mockResolvedValue(
      runtimeAgentConfig({ selfIteration: { enabled: true } }, 'agent-custom', 'custom-agent'),
    );

    await service.execAgent({ agentId: 'agent-custom', prompt: 'Hello' });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).not.toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeUndefined();
  });

  // `getAgentConfig` merges DEFAULT_AGENT_CHAT_CONFIG, so a real Lobe AI run sees
  // `selfIteration.enabled: false` even though the user never chose it.
  it('should inject self-feedback intent tool for Lobe AI when the merged config reports enabled: false', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: { selfIteration: { enabled: false } },
      id: 'agent-inbox',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'inbox',
      systemRole: '',
    });

    await service.execAgent(inboxRunInput());

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeDefined();
  });

  it('should ignore a legacy per-agent opt-out on Lobe AI', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: { selfIteration: { enabled: false, optedOut: true } },
      id: 'agent-inbox',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      slug: 'inbox',
      systemRole: '',
    });

    await service.execAgent(inboxRunInput());

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeDefined();
  });

  it('should inject self-feedback intent tool for custom agents with agent self-iteration', async () => {
    mockGetAgentConfig.mockResolvedValue(
      runtimeAgentConfig({ selfIteration: { enabled: true } }, 'agent-custom', 'custom-agent'),
    );

    await service.execAgent({
      agentId: 'agent-custom',
      prompt: 'Hello',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.toolSet.enabledToolIds).toContain(SELF_FEEDBACK_INTENT_IDENTIFIER);
    expect(callArgs.toolSet.manifestMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBeDefined();
    expect(callArgs.toolSet.sourceMap[SELF_FEEDBACK_INTENT_IDENTIFIER]).toBe('builtin');
  });

  describe('agent.user.message signal gate', () => {
    const runWithAgent = async (agent: { chatConfig: Record<string, unknown>; slug: string }) => {
      mockGetAgentConfig.mockResolvedValue({
        ...agent,
        id: 'agent-gate',
        model: 'gpt-4',
        plugins: [],
        provider: 'openai',
        systemRole: '',
      });

      await service.execAgent({ agentId: 'agent-gate', prompt: 'Hello' });

      return vi
        .mocked(enqueueAgentSignalSourceEvent)
        .mock.calls.filter(([input]) => input.sourceType === 'agent.user.message');
    };

    // No per-agent switch: every agent feeds the channel; the Labs switch is checked downstream.
    it('feeds the signal channel for custom agents without any per-agent setting', async () => {
      const calls = await runWithAgent({ chatConfig: {}, slug: 'custom-agent' });

      expect(calls).toHaveLength(1);
    });

    it('feeds the signal channel for custom agents with self-iteration on', async () => {
      const calls = await runWithAgent({
        chatConfig: { selfIteration: { enabled: true } },
        slug: 'custom-agent',
      });

      expect(calls).toHaveLength(1);
    });

    it('feeds the signal channel for Lobe AI', async () => {
      const calls = await runWithAgent({ chatConfig: {}, slug: 'inbox' });

      expect(calls).toHaveLength(1);
    });

    it('marks the first message of a conversation as the conversation root', async () => {
      const calls = await runWithAgent({ chatConfig: {}, slug: 'inbox' });

      expect(calls[0][0].payload).toMatchObject({ isConversationRoot: true });
    });

    it('does not mark a message that follows an earlier turn as the conversation root', async () => {
      mockMessageCreate.mockResolvedValue({ id: 'msg-1', parentId: 'msg-assistant-prev' });

      const calls = await runWithAgent({ chatConfig: {}, slug: 'inbox' });

      expect(calls[0][0].payload).not.toHaveProperty('isConversationRoot');
    });

    it('feeds the signal channel for Lobe AI when the merged config reports enabled: false', async () => {
      const calls = await runWithAgent({
        chatConfig: { selfIteration: { enabled: false } },
        slug: 'inbox',
      });

      expect(calls).toHaveLength(1);
    });

    it('ignores a legacy per-agent opt-out on Lobe AI', async () => {
      const calls = await runWithAgent({
        chatConfig: { selfIteration: { enabled: false, optedOut: true } },
        slug: 'inbox',
      });

      expect(calls).toHaveLength(1);
    });
  });

  it('should leave the page agent out in page scope when no sandbox is available', async () => {
    mockIsSandboxAvailableForUser.mockResolvedValue(false);
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: ['lobe-agent-documents'],
      provider: 'openai',
      systemRole: 'Custom role.',
    });

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { documentId: 'docs-1', scope: 'page', topicId: 'topic-1' },
      prompt: 'Rewrite this page',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    // No page agent without a sandbox; the saved documents pin loads on demand.
    expect(callArgs.agentConfig.plugins).toEqual([]);
  });

  it('should inject page-agent runtime for regular agents in page scope', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: { enableHistoryCount: true },
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: ['lobe-agent-documents'],
      provider: 'openai',
      systemRole: 'Custom role.',
    });

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: {
        documentId: 'docs-1',
        scope: 'page',
        topicId: 'topic-1',
      },
      prompt: 'Rewrite this page',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(callArgs.appContext).toMatchObject({
      documentId: 'docs-1',
      scope: 'page',
    });
    // The page agent is a runtime addition; the saved documents pin loads on demand.
    expect(callArgs.agentConfig.plugins).toEqual([PageAgentIdentifier]);
    expect(callArgs.agentConfig.chatConfig.enableHistoryCount).toBe(false);
    expect(callArgs.agentConfig.systemRole).toContain('Custom role.');
    expect(callArgs.agentConfig.systemRole).toContain(
      'You are a helpful document (page) editing assistant',
    );

    expect(createServerAgentToolsEngine).toHaveBeenCalledWith(
      expect.anything(),
      expect.objectContaining({
        agentConfig: expect.objectContaining({
          plugins: [PageAgentIdentifier],
        }),
      }),
    );
    // …and stays discoverable rather than cut as an unselected builtin.
    const engineArgs = vi.mocked(createServerAgentToolsEngine).mock.calls[0][1];
    expect(engineArgs.disabledPluginIds).not.toContain('lobe-agent-documents');
  });

  it('should normalize task identifier from appContext before creating runtime operation', async () => {
    mockResolveTask.mockResolvedValue({ id: 'task-row-1', identifier: 'T-1' });
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-task',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      systemRole: '',
    });

    await service.execAgent({
      agentId: 'agent-task',
      appContext: {
        defaultTaskAssigneeAgentId: 'agt_inbox',
        scope: 'task',
        taskId: 'T-1',
        topicId: 'topic-1',
      },
      prompt: 'Show current task',
    });

    const callArgs = mockCreateOperation.mock.calls[0][0];
    expect(mockResolveTask).toHaveBeenCalledWith('T-1');
    expect(callArgs.appContext).toMatchObject({
      defaultTaskAssigneeAgentId: 'agt_inbox',
      scope: 'task',
      taskId: 'task-row-1',
      topicId: 'topic-1',
    });
    expect(callArgs.initialContext.initialContext.taskManager.contextPrompt).toContain(
      'Default Lobe AI agent id: agt_inbox',
    );
  });

  describe('project context', () => {
    const runCoordinator = async () => {
      mockGetAgentConfig.mockResolvedValue({
        chatConfig: {},
        id: 'agent-coordinator',
        model: 'gpt-4',
        plugins: [],
        provider: 'openai',
        systemRole: '',
      });
      await service.execAgent({
        agentId: 'agent-coordinator',
        appContext: { projectId: 'project-1' },
        prompt: 'Split the launch into tasks',
      });
      return mockCreateOperation.mock.calls[0][0].initialContext.initialContext.projectContext
        .contextPrompt as string;
    };

    it('gives the coordinator its delegation rules when the task tool is in its tool set', async () => {
      mockToolState.enabledToolIds = ['lobe-task'];

      const prompt = await runCoordinator();

      expect(prompt).toContain('Apollo');
      expect(prompt).toContain('<coordination_rules>');
    });

    it('leaves the delegation rules out when the final tool set has no task tool', async () => {
      // e.g. an exclusive continuation: the rules would name createTask / runTask,
      // which the model cannot call.
      const prompt = await runCoordinator();

      expect(prompt).toContain('Apollo');
      expect(prompt).not.toContain('<coordination_rules>');
    });
  });

  it('should inject lobe-agent when history has audio and model lacks native audio support', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'text-only',
      plugins: [],
      provider: 'openai',
      systemRole: '',
    });
    mockMessageQuery.mockResolvedValue([
      {
        audioList: [{ alt: 'audio.mp3', id: 'file-audio', url: 'https://example.com/audio.mp3' }],
        id: 'history-audio',
        role: 'user',
      },
    ]);

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { topicId: 'topic-1' },
      prompt: 'What is said in the previous audio?',
    });

    expect(createServerAgentToolsEngine).toHaveBeenCalledWith(
      expect.anything(),
      expect.objectContaining({
        agentConfig: expect.objectContaining({
          plugins: expect.arrayContaining(['lobe-agent']),
        }),
      }),
    );
  });

  it('should not inject lobe-agent when the LobeHub routed model supports audio natively', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gemini-3.1-flash-lite-preview',
      plugins: [],
      provider: 'lobehub',
      systemRole: '',
    });
    mockMessageQuery.mockResolvedValue([
      {
        audioList: [{ id: 'file-audio', url: 'https://example.com/audio.mp3' }],
        id: 'history-audio',
        role: 'user',
      },
    ]);

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { topicId: 'topic-1' },
      prompt: 'What is said in the previous audio?',
    });

    const callArgs = vi.mocked(createServerAgentToolsEngine).mock.calls[0][1];
    expect(callArgs.agentConfig.plugins).not.toContain('lobe-agent');
  });

  it('should not inject lobe-agent when user model abilities support images natively', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'custom-vision-model',
      plugins: [],
      provider: 'custom-provider',
      systemRole: '',
    });
    mockGetModelMetadata.mockResolvedValue({ abilities: { vision: true } });
    mockMessageQuery.mockResolvedValue([
      {
        id: 'history-image',
        imageList: [{ alt: 'image.png', id: 'file-image', url: 'https://example.com/image.png' }],
        role: 'user',
      },
    ]);

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { topicId: 'topic-1' },
      prompt: 'What is shown in the previous image?',
    });

    const callArgs = vi.mocked(createServerAgentToolsEngine).mock.calls[0][1];
    expect(callArgs.agentConfig.plugins).not.toContain('lobe-agent');
  });

  it('should inject lobe-agent when user model abilities disable builtin image support', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gpt-4',
      plugins: [],
      provider: 'openai',
      systemRole: '',
    });
    mockGetModelMetadata.mockResolvedValue({ abilities: { vision: false } });
    mockMessageQuery.mockResolvedValue([
      {
        id: 'history-image',
        imageList: [{ alt: 'image.png', id: 'file-image', url: 'https://example.com/image.png' }],
        role: 'user',
      },
    ]);

    await service.execAgent({
      agentId: 'agent-custom',
      appContext: { topicId: 'topic-1' },
      prompt: 'What is shown in the previous image?',
    });

    expect(createServerAgentToolsEngine).toHaveBeenCalledWith(
      expect.anything(),
      expect.objectContaining({
        agentConfig: expect.objectContaining({
          plugins: expect.arrayContaining(['lobe-agent']),
        }),
      }),
    );
  });

  it('should preserve builtin image output support when user abilities override vision', async () => {
    mockGetAgentConfig.mockResolvedValue({
      chatConfig: {},
      id: 'agent-custom',
      model: 'gemini-3.1-flash-image',
      plugins: [],
      provider: 'google',
      systemRole: '',
    });
    mockGetModelMetadata.mockResolvedValue({ abilities: { vision: false } });

    await service.execAgent({
      agentId: 'agent-custom',
      prompt: 'Generate an image',
    });

    const callArgs = vi.mocked(createServerAgentToolsEngine).mock.calls[0][1];
    expect(callArgs.modelAbilities).toMatchObject({ imageOutput: true });
  });
});