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.

18.0 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import {
  createFrozenModelParamsProviders,
  readFrozenModelFacts,
  resolveHistoryCount,
  resolveModelParams,
} from './resolveModelParams';
import type { ModelCardFacts, ModelParamsProviders, ModelParamsRequest } from './types';

const cards: ModelCardFacts[] = [
  {
    abilities: { functionCall: true, vision: true },
    displayName: 'GPT-4',
    extendParams: ['reasoningEffort'],
    id: 'gpt-4',
    knowledgeCutoff: '2024-01',
    providerId: 'openai',
  },
  {
    abilities: {},
    displayName: 'DeepSeek V4 Pro',
    extendParams: ['deepseekV4ReasoningEffort'],
    id: 'deepseek-v4-pro',
    providerId: 'deepseek',
  },
  { abilities: { functionCall: false }, displayName: 'Plain', id: 'plain', providerId: 'openai' },
  {
    abilities: { vision: true },
    displayName: 'GPT-4 (hub)',
    extendParams: ['reasoningEffort'],
    id: 'gpt-4',
    knowledgeCutoff: '2024-01',
    providerId: 'lobehub',
  },
];

const providers = (overrides: Partial<ModelParamsProviders> = {}): ModelParamsProviders => ({
  listModelCards: () => cards,
  ...overrides,
});

const request = (overrides: Partial<ModelParamsRequest> = {}): ModelParamsRequest => ({
  agent: { chatConfig: {}, id: 'agt_1' },
  model: 'gpt-4',
  provider: 'openai',
  topicId: 'tpc_1',
  ...overrides,
});

describe('resolveModelParams', () => {
  describe('extend params', () => {
    it('lets the user row win, treats an emptied row as an opt-out and falls back to the cards', async () => {
      const own = await resolveModelParams(
        request(),
        providers({ getUserModelRow: async () => ({ extendParams: ['thinking'] }) }),
      );
      expect(own.modelExtendParams).toEqual(['thinking']);
      expect(own.modelHasReasoningExtendParams).toBe(false);

      const optedOut = await resolveModelParams(
        request(),
        providers({ getUserModelRow: async () => ({ extendParams: [] }) }),
      );
      expect(optedOut.modelExtendParams).toEqual([]);

      const fromCard = await resolveModelParams(request(), providers());
      expect(fromCard.modelExtendParams).toEqual(['reasoningEffort']);
      expect(fromCard.modelHasReasoningExtendParams).toBe(true);
    });

    it('falls back to the canonical card for an aggregation provider', async () => {
      const facts = await resolveModelParams(
        request({ provider: 'lobehub' }),
        providers({
          listModelCards: () => [{ ...cards[3], extendParams: undefined }, cards[0]],
        }),
      );
      expect(facts.modelExtendParams).toEqual(['reasoningEffort']);
      expect(facts.modelKnowledgeCutoff).toBe('2024-01');
    });

    it('applies the effective config to the payload params', async () => {
      const facts = await resolveModelParams(
        request(),
        providers({ getModelReasoningConfig: async () => ({ reasoningEffort: 'high' }) }),
      );
      expect(facts.resolvedExtendParams).toMatchObject({ reasoning_effort: 'high' });
    });
  });

  describe('reasoning config precedence', () => {
    it('lets the topic pin win over the model-instance config', async () => {
      const facts = await resolveModelParams(
        request(),
        providers({
          findTopicReasoningPin: async () => ({
            model: 'gpt-4',
            provider: 'openai',
            reasoningConfig: { reasoningEffort: 'low' },
          }),
          getModelReasoningConfig: async () => ({ reasoningEffort: 'high' }),
        }),
      );
      expect(facts.resolvedExtendParams).toMatchObject({ reasoning_effort: 'low' });
    });

    it('ignores a pin taken for another model or, in a group topic, for another agent', async () => {
      const getModelReasoningConfig = vi.fn(async () => ({ reasoningEffort: 'high' as const }));
      const otherModel = await resolveModelParams(
        request(),
        providers({
          findTopicReasoningPin: async () => ({
            model: 'gpt-4o',
            provider: 'openai',
            reasoningConfig: { reasoningEffort: 'low' },
          }),
          getModelReasoningConfig,
        }),
      );
      expect(otherModel.resolvedExtendParams).toMatchObject({ reasoning_effort: 'high' });

      const otherAgent = await resolveModelParams(
        request(),
        providers({
          findTopicReasoningPin: async () => ({
            agentId: 'agt_2',
            groupId: 'grp_1',
            model: 'gpt-4',
            provider: 'openai',
            reasoningConfig: { reasoningEffort: 'low' },
          }),
          getModelReasoningConfig,
        }),
      );
      expect(otherAgent.resolvedExtendParams).toMatchObject({ reasoning_effort: 'high' });
    });

    it('treats an empty pin as the defaults but a legacy topic without one as unpinned', async () => {
      const getModelReasoningConfig = vi.fn(async () => ({ reasoningEffort: 'low' as const }));
      const pinned = await resolveModelParams(
        request(),
        providers({
          findTopicReasoningPin: async () => ({
            model: 'gpt-4',
            provider: 'openai',
            reasoningConfig: {},
          }),
          getModelReasoningConfig,
        }),
      );
      expect(getModelReasoningConfig).not.toHaveBeenCalled();
      expect(pinned.resolvedExtendParams).toEqual({});

      const legacy = await resolveModelParams(
        request(),
        providers({
          findTopicReasoningPin: async () => ({ model: 'gpt-4', provider: 'openai' }),
          getModelReasoningConfig,
        }),
      );
      expect(legacy.resolvedExtendParams).toMatchObject({ reasoning_effort: 'low' });
    });

    it('does not read either source for a model without reasoning extend params', async () => {
      const findTopicReasoningPin = vi.fn();
      const getModelReasoningConfig = vi.fn();
      await resolveModelParams(
        request({ model: 'plain' }),
        providers({ findTopicReasoningPin, getModelReasoningConfig }),
      );
      expect(findTopicReasoningPin).not.toHaveBeenCalled();
      expect(getModelReasoningConfig).not.toHaveBeenCalled();
    });

    it('lets explicit sub-agent overrides win over the instance config', async () => {
      const facts = await resolveModelParams(
        request({
          agent: { chatConfig: {}, subAgentChatConfigOverride: { reasoningEffort: 'low' } },
        }),
        providers({ getModelReasoningConfig: async () => ({ reasoningEffort: 'high' }) }),
      );
      expect(facts.resolvedExtendParams).toMatchObject({ reasoning_effort: 'low' });
    });
  });

  describe('assistant reasoning replay', () => {
    it('replays only when configured and supported by the card', async () => {
      const off = await resolveModelParams(request(), providers());
      expect(off.shouldReplayAssistantReasoning).toBe(false);
      expect(off.preserveThinkingForPayload).toBeUndefined();

      const withCard = providers({
        listModelCards: () => [{ ...cards[0], extendParams: ['preserveThinking'] }],
      });
      const on = await resolveModelParams(
        request({ agent: { chatConfig: { preserveThinking: true } } }),
        withCard,
      );
      expect(on.shouldReplayAssistantReasoning).toBe(true);
      expect(on.preserveThinkingForPayload).toBe(true);

      const declined = await resolveModelParams(
        request({ agent: { chatConfig: { preserveThinking: false } } }),
        withCard,
      );
      expect(declined.shouldReplayAssistantReasoning).toBe(false);
      expect(declined.preserveThinkingForPayload).toBe(false);
    });

    it('forces replay for DeepSeek thinking models unless V4 thinking is disabled', async () => {
      const forced = await resolveModelParams(
        request({ model: 'deepseek-v4-pro', provider: 'deepseek' }),
        providers(),
      );
      expect(forced.shouldReplayAssistantReasoning).toBe(true);
      expect(forced.preserveThinkingForPayload).toBe(true);

      const disabled = await resolveModelParams(
        request({ model: 'deepseek-v4-pro', provider: 'deepseek' }),
        providers({
          getModelReasoningConfig: async () => ({ deepseekV4ReasoningEffort: 'none' }),
        }),
      );
      expect(disabled.shouldReplayAssistantReasoning).toBe(false);
    });

    it('forces replay for Meta and falls back to the provider for an unknown qwen model', async () => {
      const meta = await resolveModelParams(
        request({ model: 'llama-x', provider: 'meta' }),
        providers(),
      );
      expect(meta.shouldReplayAssistantReasoning).toBe(true);

      const qwen = await resolveModelParams(
        request({
          agent: { chatConfig: { preserveThinking: true } },
          model: 'qwen-unknown',
          provider: 'qwen',
        }),
        providers(),
      );
      expect(qwen.shouldReplayAssistantReasoning).toBe(true);
    });
  });

  describe('capabilities', () => {
    it('prefers the frozen snapshot, then the user row, then the cards', async () => {
      const snapshot = await resolveModelParams(
        request({ mediaCapabilities: { vision: false } }),
        providers({ getUserModelRow: async () => ({ abilities: { vision: true } }) }),
      );
      expect(snapshot.capabilities.isCanUseVision('gpt-4', 'openai')).toBe(false);
      // The snapshot never applies to another model.
      expect(snapshot.capabilities.isCanUseVision('gpt-4', 'lobehub')).toBe(true);

      const row = await resolveModelParams(
        request(),
        providers({ getUserModelRow: async () => ({ abilities: { vision: false } }) }),
      );
      expect(row.capabilities.isCanUseVision('gpt-4', 'openai')).toBe(false);

      const card = await resolveModelParams(request(), providers());
      expect(card.capabilities.isCanUseVision('gpt-4', 'openai')).toBe(true);
      expect(card.capabilities.isCanUseAudio('gpt-4', 'openai')).toBe(false);
    });

    it('assumes function calling for an unknown model and honors an explicit false', async () => {
      const facts = await resolveModelParams(request(), providers());
      expect(facts.capabilities.isCanUseFC('unknown', 'openai')).toBe(true);
      expect(facts.capabilities.isCanUseFC('plain', 'openai')).toBe(false);
    });
  });

  describe('chat config passthrough', () => {
    it('keeps the stored agent mode, reads stream and the history window from the config', async () => {
      const facts = await resolveModelParams(
        request({
          agent: {
            chatConfig: { enableAgentMode: true, enableStreaming: false, historyCount: 0 },
          },
          model: 'plain',
        }),
        providers(),
      );
      // A model without function calling does not demote the agent mode.
      expect(facts.enableAgentMode).toBe(true);
      expect(facts.stream).toBe(false);
      expect(facts.historyCount).toBe(1);

      const defaults = await resolveModelParams(request({ agent: {} }), providers());
      expect(defaults.enableAgentMode).toBeUndefined();
      expect(defaults.stream).toBe(true);
      expect(defaults.historyCount).toBeUndefined();
    });

    it('emits enabledSearch only when the decision enables model search', async () => {
      const on = await resolveModelParams(
        request({ searchDecision: { enabledSearch: true, useModelSearch: true } }),
        providers(),
      );
      expect(on.resolvedExtendParams).toMatchObject({ enabledSearch: true });
      const off = await resolveModelParams(
        request({ searchDecision: { enabledSearch: true, useModelSearch: false } }),
        providers(),
      );
      expect(off.resolvedExtendParams?.enabledSearch).toBeUndefined();
    });
  });
});

describe('resolveHistoryCount', () => {
  it('adds the current turn and leaves unset alone', () => {
    expect(resolveHistoryCount(0)).toBe(1);
    expect(resolveHistoryCount(20)).toBe(21);
    expect(resolveHistoryCount(undefined)).toBeUndefined();
    expect(resolveHistoryCount(null)).toBeUndefined();
  });
});

describe('frozen model facts', () => {
  const liveProviders = () => ({
    findTopicReasoningPin: vi.fn<NonNullable<ModelParamsProviders['findTopicReasoningPin']>>(
      async () => ({
        model: 'gpt-4',
        provider: 'openai',
        reasoningConfig: { reasoningEffort: 'low' },
      }),
    ),
    getModelReasoningConfig: vi.fn<NonNullable<ModelParamsProviders['getModelReasoningConfig']>>(
      async () => ({ reasoningEffort: 'high' }),
    ),
    getUserModelRow: vi.fn<NonNullable<ModelParamsProviders['getUserModelRow']>>(async () => ({
      displayName: 'My GPT-4',
    })),
    listModelCards: vi.fn(() => cards),
  });

  it('resolves the same params off the snapshot without reading a single source again', async () => {
    const live = liveProviders();
    const expected = await resolveModelParams(request(), live);

    const facts = await readFrozenModelFacts(request(), live);
    const reads = liveProviders();
    // The live sources are laid underneath on purpose: the snapshot must answer
    // everything, so none of them may be touched.
    const fromSnapshot = await resolveModelParams(request(), {
      ...reads,
      ...createFrozenModelParamsProviders(facts),
    });

    expect(fromSnapshot.modelDisplayName).toBe(expected.modelDisplayName);
    expect(fromSnapshot.modelKnowledgeCutoff).toBe(expected.modelKnowledgeCutoff);
    expect(fromSnapshot.modelExtendParams).toEqual(expected.modelExtendParams);
    expect(fromSnapshot.resolvedExtendParams).toEqual(expected.resolvedExtendParams);
    expect(fromSnapshot.capabilities.isCanUseFC('gpt-4', 'openai')).toBe(true);
    expect(fromSnapshot.capabilities.isCanUseVision('gpt-4', 'openai')).toBe(true);
    expect(reads.findTopicReasoningPin).not.toHaveBeenCalled();
    expect(reads.getModelReasoningConfig).not.toHaveBeenCalled();
    expect(reads.getUserModelRow).not.toHaveBeenCalled();
    expect(reads.listModelCards).not.toHaveBeenCalled();
  });

  it('survives the state round-trip as plain JSON', async () => {
    const facts = await readFrozenModelFacts(request(), liveProviders());
    // Deliberately JSON, not structuredClone: the state is stored as JSON.
    // eslint-disable-next-line unicorn/prefer-structured-clone
    expect(JSON.parse(JSON.stringify(facts))).toEqual(facts);
    // Only the two cards the rules can match, never the whole bank.
    expect(facts.cards.map((card) => `${card.providerId}/${card.id}`)).toEqual([
      'openai/gpt-4',
      'lobehub/gpt-4',
    ]);
  });

  it('keeps the effort the run started with when the user edits it mid-run', async () => {
    const live = liveProviders();
    const facts = await readFrozenModelFacts(request(), live);
    expect(facts.reasoningConfig).toEqual({ reasoningEffort: 'low' });

    // The user re-pins the topic and changes the model-instance config: the
    // running operation must keep answering with what it froze.
    live.findTopicReasoningPin.mockResolvedValue({
      model: 'gpt-4',
      provider: 'openai',
      reasoningConfig: { reasoningEffort: 'high' },
    });
    const later = await resolveModelParams(request(), createFrozenModelParamsProviders(facts));
    expect(later.resolvedExtendParams).toMatchObject({ reasoning_effort: 'low' });
  });

  it('freezes no reasoning source for a model that cannot consume one', async () => {
    const live = liveProviders();
    const facts = await readFrozenModelFacts(request({ model: 'plain' }), live);
    expect(facts.reasoningConfig).toBeUndefined();
    expect(live.findTopicReasoningPin).not.toHaveBeenCalled();
    expect(live.getModelReasoningConfig).not.toHaveBeenCalled();
  });

  it('answers for the frozen model only, so another model cannot inherit its row', async () => {
    const facts = await readFrozenModelFacts(request(), liveProviders());
    const other = await resolveModelParams(
      request({ model: 'plain' }),
      createFrozenModelParamsProviders(facts),
    );
    expect(other.userModelRow).toBeUndefined();
    expect(other.modelDisplayName).toBeUndefined();
    // No card for the compression model in the snapshot: function calling is
    // assumed and no native media is claimed, as for any unknown card.
    expect(other.capabilities.isCanUseFC('plain', 'openai')).toBe(true);
    expect(other.capabilities.isCanUseVision('plain', 'openai')).toBe(false);
  });

  it('merges the user deployment alias over the bundled card when freezing', async () => {
    // Codex P1 (4256356269): a user-configured alias must win over the bundled
    // card, otherwise deployment-catalog providers inject the catalog default
    // and 404 wherever only the user's endpoint exists.
    const live = liveProviders();
    live.getUserModelRow.mockResolvedValue({ deploymentName: 'prod-endpoint' });

    const facts = await readFrozenModelFacts(
      request({ model: 'deepseek-v4-pro', provider: 'deepseek' }),
      live,
    );
    const frozenCard = facts.cards.find((card) => card.providerId === 'deepseek');
    expect(frozenCard?.deploymentName).toBe('prod-endpoint');
    // The bundled card is preserved apart from the alias.
    expect(frozenCard).toMatchObject({ displayName: 'DeepSeek V4 Pro', id: 'deepseek-v4-pro' });
  });

  it('freezes a minimal deployment card for a custom model the bank has no card for', async () => {
    const live = liveProviders();
    live.getUserModelRow.mockResolvedValue({ deploymentName: 'prod-endpoint' });

    // No bundled card under volcengine: the user row alone still names the
    // deployment the transport must inject.
    const facts = await readFrozenModelFacts(
      request({ model: 'deepseek-v4-pro', provider: 'volcengine' }),
      live,
    );
    expect(facts.cards[0]).toEqual({
      deploymentName: 'prod-endpoint',
      id: 'deepseek-v4-pro',
      providerId: 'volcengine',
    });
    // The canonical card of the same id elsewhere is frozen untouched.
    expect(facts.cards.slice(1)).not.toContainEqual(
      expect.objectContaining({ providerId: 'volcengine' }),
    );
  });

  it('keeps the bundled deployment name when the user row has none', async () => {
    const live = liveProviders();
    live.getUserModelRow.mockResolvedValue({ displayName: 'My model' });

    const facts = await readFrozenModelFacts(
      request({ model: 'deepseek-v4-pro', provider: 'deepseek' }),
      live,
    );
    const frozenCard = facts.cards.find((card) => card.providerId === 'deepseek');
    expect(frozenCard?.deploymentName).toBeUndefined();
  });
});