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.

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

import type {
  AgentRuntimeHost,
  ContextBuilder,
  ContextBuildOutput,
  LLMAttemptOutput,
  LLMRetryPolicy,
  LLMTrace,
  LLMTransport,
  MessageTransport,
  OperationStore,
  StreamSink,
} from '../transport';
import type { AgentInstructionCallLlm, AgentState } from '../types';
import { callLlm } from './callLlm';

const createState = (): AgentState => ({
  cost: {
    calculatedAt: new Date().toISOString(),
    currency: 'USD',
    llm: { byModel: [], currency: 'USD', total: 0 },
    tools: { byTool: [], currency: 'USD', total: 0 },
    total: 0,
  },
  createdAt: new Date().toISOString(),
  lastModified: new Date().toISOString(),
  messages: [],
  origin: {
    agentId: 'agent-1',
    threadId: 'thread-1',
    topicId: 'topic-1',
  },
  operationId: 'op-1',
  status: 'running',
  stepCount: 0,
  toolManifestMap: {},
  usage: {
    humanInteraction: {
      approvalRequests: 0,
      promptRequests: 0,
      selectRequests: 0,
      totalWaitingTimeMs: 0,
    },
    llm: {
      apiCalls: 0,
      processingTimeMs: 0,
      tokens: { input: 0, output: 0, total: 0 },
    },
    tools: {
      byTool: [],
      totalCalls: 0,
      totalTimeMs: 0,
    },
  },
});

const instruction: AgentInstructionCallLlm = {
  payload: {
    messages: [{ content: 'hello', role: 'user' }],
    model: 'gpt-4',
    provider: 'openai',
    tools: [],
  },
  type: 'call_llm',
};

const createMessageTransport = (): MessageTransport => ({
  createAssistantMessage: vi.fn().mockResolvedValue({ id: 'assistant-1' }),
  createToolMessage: vi.fn(),
  deleteMessage: vi.fn(),
  findById: vi.fn().mockResolvedValue({ id: 'parent-1' }),
  findToolMessageIdByToolCallId: vi.fn(),
  getToolMessage: vi.fn(),
  query: vi.fn(),
  update: vi.fn(),
  updatePluginState: vi.fn(),
  updateToolIntervention: vi.fn(),
  updateToolMessage: vi.fn(),
});

const createStreamSink = (): StreamSink => ({
  publishChunk: vi.fn(),
  publishError: vi.fn(),
  publishEvent: vi.fn(),
});

const contextOutput: ContextBuildOutput = {
  messages: [{ content: 'prepared hello', role: 'user' }],
  replayAssistantReasoning: false,
  resolvedTools: {
    enabledToolIds: [],
    manifestMap: {},
    promptManifestMap: {},
    sourceMap: {},
    tools: [],
  },
};

const createContextBuilder = (): ContextBuilder => ({
  build: vi.fn().mockResolvedValue(contextOutput),
});

const createAttemptOutput = (content = 'answer'): LLMAttemptOutput => ({
  answerSalvagedFromReasoning: false,
  content,
  contentParts: [],
  grounding: null,
  hasContentImages: false,
  hasReasoningImages: false,
  imageList: [],
  reasoningParts: [],
  thinkingContent: '',
  toolCalls: [],
  toolsCalling: [],
});

const createRetryPolicy = (overrides: Partial<LLMRetryPolicy> = {}): LLMRetryPolicy => ({
  classifyError: vi.fn().mockReturnValue({ kind: 'stop', message: 'failed' }),
  maxAttempts: vi.fn().mockReturnValue(3),
  onError: vi.fn().mockResolvedValue(undefined),
  resolveRetryBudget: vi.fn().mockReturnValue(2),
  waitForRetry: vi.fn().mockResolvedValue(undefined),
  ...overrides,
});

const createTrace = (overrides: Partial<LLMTrace> = {}): LLMTrace => ({
  close: vi.fn(),
  onFirstChunk: vi.fn(),
  recordResult: vi.fn(),
  run: vi.fn((task) => task()),
  ...overrides,
});

const createCallTransport = ({
  policy = createRetryPolicy(),
  runAttempt = vi.fn().mockResolvedValue({ ok: true, output: createAttemptOutput() }),
  trace = createTrace(),
}: {
  policy?: LLMRetryPolicy;
  runAttempt?: LLMTransport['runAttempt'];
  trace?: LLMTrace;
} = {}) => {
  const createTraceScope = vi.fn().mockReturnValue(trace);
  const llm: LLMTransport = {
    createTrace: createTraceScope,
    retryPolicy: policy,
    runAttempt,
    stream: vi.fn(),
  };

  return { createTrace: createTraceScope, llm, policy, runAttempt, trace };
};

const createHost = (
  llm: LLMTransport,
  messages = createMessageTransport(),
  stream = createStreamSink(),
  context = createContextBuilder(),
  operationStore?: OperationStore,
): AgentRuntimeHost => ({
  operation: { operationId: 'op-1', stepIndex: 0 },
  transports: {
    context,
    llm,
    messages,
    operationStore,
    stream,
  },
});

describe('callLlm executor', () => {
  it('ships a joined steer parent on stream_start so the client can show it before the step ends', async () => {
    const transport = createCallTransport();
    const messages = createMessageTransport();
    vi.mocked(messages.findById).mockResolvedValue({
      content: 'why not the Feishu CLI',
      id: 'steer-1',
      metadata: { steer: true, trigger: 'chat' },
      parentId: 'assistant-0',
      role: 'user',
    });
    vi.mocked(messages.createAssistantMessage).mockResolvedValue({
      id: 'assistant-1',
      parentId: 'steer-1',
      role: 'assistant',
    });
    const stream = createStreamSink();
    const host = createHost(transport.llm, messages, stream);

    await callLlm(host)(
      { payload: { ...instruction.payload, parentMessageId: 'steer-1' }, type: 'call_llm' },
      createState(),
      { phase: 'user_input' },
    );

    expect(stream.publishEvent).toHaveBeenCalledWith(
      expect.objectContaining({
        data: expect.objectContaining({
          parentMessage: {
            content: 'why not the Feishu CLI',
            id: 'steer-1',
            metadata: { steer: true, trigger: 'chat' },
            parentId: 'assistant-0',
            role: 'user',
          },
        }),
        type: 'stream_start',
      }),
    );
  });

  it('ships the stored creation times on stream_start so the client orders by server time', async () => {
    const transport = createCallTransport();
    const messages = createMessageTransport();
    vi.mocked(messages.findById).mockResolvedValue({
      content: 'x',
      createdAt: new Date('2026-11-11T15:22:48.000Z'),
      id: 'steer-1',
      metadata: { steer: true },
      role: 'user',
    });
    vi.mocked(messages.createAssistantMessage).mockResolvedValue({
      createdAt: new Date('2026-11-11T15:22:48.043Z'),
      id: 'assistant-1',
      parentId: 'steer-1',
      role: 'assistant',
    });
    const stream = createStreamSink();
    const host = createHost(transport.llm, messages, stream);

    await callLlm(host)(
      { payload: { ...instruction.payload, parentMessageId: 'steer-1' }, type: 'call_llm' },
      createState(),
      { phase: 'user_input' },
    );

    const streamStart = vi
      .mocked(stream.publishEvent)
      .mock.calls.find(([event]) => event.type === 'stream_start');
    expect(streamStart?.[0].data).toMatchObject({
      assistantMessage: { createdAt: Date.parse('2026-11-11T15:22:48.043Z'), id: 'assistant-1' },
      parentMessage: { createdAt: Date.parse('2026-11-11T15:22:48.000Z'), id: 'steer-1' },
    });
  });

  it('leaves a non-steer parent off stream_start: the client already has it', async () => {
    const transport = createCallTransport();
    const messages = createMessageTransport();
    vi.mocked(messages.findById).mockResolvedValue({
      content: 'hello',
      id: 'user-1',
      metadata: {},
      role: 'user',
    });
    const stream = createStreamSink();
    const host = createHost(transport.llm, messages, stream);

    await callLlm(host)(
      { payload: { ...instruction.payload, parentId: 'user-1' }, type: 'call_llm' },
      createState(),
      { phase: 'user_input' },
    );

    const streamStart = vi
      .mocked(stream.publishEvent)
      .mock.calls.find(([event]) => event.type === 'stream_start');
    expect(streamStart?.[0].data).not.toHaveProperty('parentMessage');
  });

  it('prepares the assistant message and delegates call_llm execution to the LLM transport', async () => {
    const state = createState();
    const transport = createCallTransport();
    const messages = createMessageTransport();
    const stream = createStreamSink();
    const context = createContextBuilder();
    const host = createHost(transport.llm, messages, stream, context);
    const instructionWithParent: AgentInstructionCallLlm = {
      payload: {
        ...instruction.payload,
        parentId: 'parent-1',
      },
      type: 'call_llm',
    };

    const result = await callLlm(host)(instructionWithParent, state, {
      instructionIndex: 1,
      phase: 'user_input',
    });
    expect(messages.findById).toHaveBeenCalledWith('parent-1');
    expect(messages.createAssistantMessage).toHaveBeenCalledWith(
      expect.objectContaining({
        agentId: 'agent-1',
        content: '',
        // Creation provenance stamp — must reference the running operation so
        // the id stays resolvable after client reloads.
        metadata: { operationId: 'op-1' },
        model: 'gpt-4',
        parentId: 'parent-1',
        provider: 'openai',
        role: 'assistant',
        threadId: 'thread-1',
        topicId: 'topic-1',
      }),
      {
        idempotencyKey: 'agent-runtime:op-1:step:0:instruction:1:assistant',
      },
    );
    expect(stream.publishEvent).toHaveBeenCalledWith({
      data: {
        assistantMessage: { id: 'assistant-1' },
        model: 'gpt-4',
        provider: 'openai',
      },
      stepIndex: 0,
      type: 'stream_start',
    });
    expect(context.build).toHaveBeenCalledWith({
      model: 'gpt-4',
      payload: instructionWithParent.payload,
      provider: 'openai',
      state,
    });
    expect(transport.createTrace).toHaveBeenCalledWith({
      assistantMessageId: 'assistant-1',
      // The built context rides along so the trace records what the request carries.
      context: expect.objectContaining({ messages: [{ content: 'prepared hello', role: 'user' }] }),
      conversationId: 'topic-1',
      model: 'gpt-4',
      provider: 'openai',
    });
    expect(transport.runAttempt).toHaveBeenCalledWith(
      expect.objectContaining({
        attempt: 1,
        context: contextOutput,
        events: [expect.objectContaining({ type: 'llm_result' })],
        maxAttempts: 3,
        model: 'gpt-4',
        provider: 'openai',
        state,
      }),
    );
    expect(messages.update).toHaveBeenCalledWith(
      'assistant-1',
      expect.objectContaining({ content: 'answer', search: null }),
    );
    expect(result.newState.messages.at(-1)).toMatchObject({
      content: 'answer',
      id: 'assistant-1',
      role: 'assistant',
    });
    expect(transport.trace.recordResult).toHaveBeenCalledWith(createAttemptOutput());
    expect(transport.trace.close).toHaveBeenCalledWith(undefined);
  });

  it('persists the context builder request accounting on the assistant message', async () => {
    const contextAccounting = {
      bySource: {
        content: 3,
        fileContext: 0,
        reasoning: 0,
        thoughtSignature: 0,
        toolCallId: 0,
        toolCalls: 0,
        toolDefinition: 0,
      },
      estimator: 'tokenx' as const,
      messageCount: 1,
      system: 0,
      total: 3,
    };
    const transport = createCallTransport();
    const messages = createMessageTransport();
    const context: ContextBuilder = {
      build: vi.fn().mockResolvedValue({ ...contextOutput, contextAccounting }),
    };
    const host = createHost(transport.llm, messages, createStreamSink(), context);

    await callLlm(host)(instruction, createState(), {
      instructionIndex: 0,
      phase: 'user_input',
    });

    expect(messages.update).toHaveBeenCalledWith(
      'assistant-1',
      expect.objectContaining({ metadata: expect.objectContaining({ contextAccounting }) }),
    );
  });

  it('reuses an existing assistant message without creating a new one', async () => {
    const state = createState();
    const transport = createCallTransport();
    const messages = createMessageTransport();
    const host = createHost(transport.llm, messages);
    const reuseInstruction: AgentInstructionCallLlm = {
      payload: {
        ...instruction.payload,
        assistantMessageId: 'assistant-existing',
      },
      type: 'call_llm',
    };

    await expect(callLlm(host)(reuseInstruction, state)).resolves.toMatchObject({
      newState: { messages: [expect.objectContaining({ id: 'assistant-existing' })] },
    });
    expect(messages.createAssistantMessage).not.toHaveBeenCalled();
    // Pre-created placeholders exist before the operation does — the executor
    // must merge the provenance stamp onto them.
    expect(messages.update).toHaveBeenCalledWith('assistant-existing', {
      metadata: { operationId: 'op-1' },
    });
    expect(transport.createTrace).toHaveBeenCalledWith(
      expect.objectContaining({
        assistantMessageId: 'assistant-existing',
      }),
    );
  });

  it('keeps the run alive when the provenance stamp write fails', async () => {
    const state = createState();
    const transport = createCallTransport();
    const messages = createMessageTransport();
    vi.mocked(messages.update).mockRejectedValueOnce(new Error('db down'));
    const host = createHost(transport.llm, messages);
    const reuseInstruction: AgentInstructionCallLlm = {
      payload: {
        ...instruction.payload,
        assistantMessageId: 'assistant-existing',
      },
      type: 'call_llm',
    };

    // The stamp is a tracing aid — its failure must not become an LLM error.
    await expect(callLlm(host)(reuseInstruction, state)).resolves.toMatchObject({
      newState: { messages: [expect.objectContaining({ id: 'assistant-existing' })] },
    });
  });

  it('throws when the LLM transport does not provide runAttempt', async () => {
    const host = createHost({
      retryPolicy: createRetryPolicy(),
      stream: vi.fn(),
    });

    await expect(callLlm(host)(instruction, createState())).rejects.toThrow(
      'LLMTransport.runAttempt is required for call_llm executor',
    );
  });

  it('throws when the LLM transport does not provide retryPolicy', async () => {
    const host = createHost({ runAttempt: vi.fn(), stream: vi.fn() });

    await expect(callLlm(host)(instruction, createState())).rejects.toThrow(
      'LLMTransport.retryPolicy is required for call_llm executor',
    );
  });

  it('executes without an optional tracing adapter', async () => {
    const transport = createCallTransport();
    delete transport.llm.createTrace;

    await expect(
      callLlm(createHost(transport.llm))(instruction, createState()),
    ).resolves.toMatchObject({
      newState: { messages: [expect.objectContaining({ content: 'answer' })] },
    });
    expect(transport.runAttempt).toHaveBeenCalledTimes(1);
  });

  it('publishes context build failures without executing the model turn', async () => {
    const error = new Error('context failed');
    const context: ContextBuilder = { build: vi.fn().mockRejectedValue(error) };
    const transport = createCallTransport();
    const stream = createStreamSink();
    const host = createHost(transport.llm, createMessageTransport(), stream, context);

    await expect(callLlm(host)(instruction, createState())).rejects.toBe(error);
    expect(stream.publishError).toHaveBeenCalledWith({
      error,
      phase: 'llm_execution',
      stepIndex: 0,
    });
    expect(transport.runAttempt).not.toHaveBeenCalled();
  });

  it('rejects prepared context without resolved tools before creating a trace', async () => {
    const transport = createCallTransport();
    const context: ContextBuilder = {
      build: vi.fn().mockResolvedValue({
        ...contextOutput,
        resolvedTools: undefined,
      }),
    };

    await expect(
      callLlm(createHost(transport.llm, undefined, undefined, context))(instruction, createState()),
    ).rejects.toThrow('Resolved tools are required for call_llm');
    expect(transport.createTrace).not.toHaveBeenCalled();
    expect(transport.runAttempt).not.toHaveBeenCalled();
  });

  it('rejects prepared context containing only system messages', async () => {
    const transport = createCallTransport();
    const context: ContextBuilder = {
      build: vi.fn().mockResolvedValue({
        ...contextOutput,
        messages: [{ content: 'system only', role: 'system' }],
      }),
    };

    await expect(
      callLlm(createHost(transport.llm, undefined, undefined, context))(instruction, createState()),
    ).rejects.toThrow('call_llm produced no non-system messages for openai/gpt-4');
    expect(transport.createTrace).not.toHaveBeenCalled();
    expect(transport.runAttempt).not.toHaveBeenCalled();
  });

  it('fails before creating an assistant message when the parent message is missing', async () => {
    const messages = createMessageTransport();
    vi.mocked(messages.findById).mockResolvedValue(undefined);
    const stream = createStreamSink();
    const transport = createCallTransport();
    const host = createHost(transport.llm, messages, stream);
    const instructionWithParent: AgentInstructionCallLlm = {
      payload: {
        ...instruction.payload,
        parentMessageId: 'missing-parent',
      },
      type: 'call_llm',
    };

    await expect(callLlm(host)(instructionWithParent, createState())).rejects.toMatchObject({
      errorType: 'ConversationParentMissing',
      parentId: 'missing-parent',
    });
    expect(messages.createAssistantMessage).not.toHaveBeenCalled();
    expect(stream.publishEvent).toHaveBeenCalledWith({
      data: {
        error:
          'Conversation parent message missing-parent no longer exists. It was likely deleted while the operation was running.',
        errorType: 'ConversationParentMissing',
        phase: 'parent_message_preflight',
      },
      stepIndex: 0,
      type: 'error',
    });
  });

  it('owns retries and publishes stream_retry before finalizing the successful attempt', async () => {
    const state = createState();
    const error = new Error('temporary outage');
    const firstOutput = createAttemptOutput('partial');
    const finalOutput = createAttemptOutput('complete');
    finalOutput.usage = { totalTokens: 10 };
    const runAttempt = vi
      .fn()
      .mockResolvedValueOnce({ error, ok: false, output: firstOutput })
      .mockResolvedValueOnce({ ok: true, output: finalOutput });
    const onRetry = vi.fn();
    const policy = createRetryPolicy({
      classifyError: vi.fn().mockReturnValue({
        code: 'UPSTREAM_TIMEOUT',
        kind: 'retry',
        message: error.message,
      }),
      onRetry,
    });
    const transport = createCallTransport({ policy, runAttempt });
    const stream = createStreamSink();
    const operationStore: OperationStore = {
      clearRunningMark: vi.fn(),
      loadState: vi.fn().mockResolvedValue(null),
    };
    const host = createHost(
      transport.llm,
      createMessageTransport(),
      stream,
      createContextBuilder(),
      operationStore,
    );

    const result = await callLlm(host)(instruction, state);

    const retryEvent = {
      data: {
        attempt: 2,
        delayMs: 1000,
        errorType: 'UPSTREAM_TIMEOUT',
        kind: 'retry',
        maxAttempts: 3,
      },
      type: 'stream_retry' as const,
    };
    expect(runAttempt).toHaveBeenNthCalledWith(
      1,
      expect.objectContaining({
        attempt: 1,
        events: [retryEvent, expect.objectContaining({ type: 'llm_result' })],
        maxAttempts: 3,
      }),
    );
    expect(runAttempt).toHaveBeenNthCalledWith(
      2,
      expect.objectContaining({
        attempt: 2,
        events: [retryEvent, expect.objectContaining({ type: 'llm_result' })],
        maxAttempts: 3,
      }),
    );
    expect(onRetry).toHaveBeenCalledWith({
      attempt: 1,
      delayMs: 1000,
      error: {
        code: 'UPSTREAM_TIMEOUT',
        kind: 'retry',
        message: error.message,
      },
      maxAttempts: 3,
    });
    expect(stream.publishEvent).toHaveBeenCalledWith({
      data: retryEvent.data,
      stepIndex: 0,
      type: 'stream_retry',
    });
    expect(policy.waitForRetry).toHaveBeenCalledWith(1000);
    // Both provider attempts count toward the user-facing LLM-call metric.
    expect(result.newState.usage?.llm.apiCalls).toBe(2);
    expect(result.newState.messages.at(-1)).toMatchObject({ content: 'complete' });
    expect(transport.trace.recordResult).toHaveBeenCalledWith(finalOutput);
    expect(policy.onError).not.toHaveBeenCalled();
    expect(transport.trace.close).toHaveBeenCalledWith(undefined);
  });

  it('stops retrying and persists the partial attempt when the operation is interrupted', async () => {
    const state = createState();
    const error = new Error('stream aborted');
    const output = createAttemptOutput('partial');
    const policy = createRetryPolicy({
      classifyError: vi.fn().mockReturnValue({ kind: 'retry', message: error.message }),
    });
    const runAttempt = vi.fn().mockResolvedValue({ error, ok: false, output });
    const transport = createCallTransport({ policy, runAttempt });
    const operationStore: OperationStore = {
      clearRunningMark: vi.fn(),
      loadState: vi.fn().mockResolvedValue({ ...state, status: 'interrupted' }),
    };
    const stream = createStreamSink();
    const messages = createMessageTransport();
    const host = createHost(
      transport.llm,
      messages,
      stream,
      createContextBuilder(),
      operationStore,
    );

    await expect(callLlm(host)(instruction, state)).rejects.toBe(error);

    expect(runAttempt).toHaveBeenCalledTimes(1);
    expect(policy.waitForRetry).not.toHaveBeenCalled();
    expect(messages.update).toHaveBeenCalledWith(
      'assistant-1',
      expect.objectContaining({
        content: 'partial',
        metadata: expect.objectContaining({ interruptedMidStream: true }),
      }),
    );
    expect(policy.onError).toHaveBeenCalledWith({
      error,
      events: [],
      interrupted: true,
      output,
      retryBudget: 2,
    });
    expect(transport.trace.close).toHaveBeenCalledWith(error);
    expect(stream.publishError).toHaveBeenCalledWith({
      error,
      phase: 'llm_execution',
      stepIndex: 0,
    });
  });
});