/** * In-process fake LLM for credential-free e2e tests. Loaded by `@librechat/api`'s * `createRun` via the `LIBRECHAT_TEST_RUN_HOOK` env var (set by the mock * Playwright config and the `--profile=mock` recorder), it swaps the run's model * for the agents package's own `FakeChatModel` through * `run.Graph.overrideTestModel(...)`. * * This exercises the real `Run.create` -> graph -> tool-node pipeline end to end * without a live provider or a standalone HTTP mock server: responses are decided * from the conversation and the agents' advertised tools. */ const sharp = require('sharp'); const { FakeChatModel } = require('@librechat/agents'); const { ChatGenerationChunk } = require('@langchain/core/outputs'); const { AIMessageChunk } = require('@langchain/core/messages'); const { tryBindReplay } = require('./model-replay'); const { runFileDeliveryResponses } = require('./run-files-model'); const { createRunFileLifecycleResponses } = require('./run-files-lifecycle-model'); const subagentHistory = require('../fixtures/subagent-history.cjs'); const runFileLifecycle = createRunFileLifecycleResponses({ findLastToolMessage, getContentText, messageType, }); const MOCK_REPLY = process.env.MOCK_LLM_REPLY || 'E2E mock reply: pong'; const CHUNK_DELAY_MS = Number(process.env.MOCK_LLM_CHUNK_DELAY_MS) || 10; const CREATE_SKILL_MARKER = 'E2E_CREATE_SKILL:'; const EDIT_SKILL_MARKER = 'E2E_EDIT_SKILL:'; const ASSERT_SKILLS_MARKER = 'E2E_ASSERT_SKILLS:'; const ASSERT_MANUAL_SKILL_MARKER = 'E2E_ASSERT_MANUAL_SKILL:'; const INVOKE_SKILL_MARKER = 'E2E_INVOKE_SKILL:'; const ASSERT_PROVIDER_FILE_MARKER = 'E2E_ASSERT_PROVIDER_FILE:'; const ASSERT_AGENT_CONTEXT_MARKER = 'E2E_ASSERT_AGENT_CONTEXT:'; const REPORT_LLM_CONFIG_MARKER = 'E2E_REPORT_LLM_CONFIG'; const REPORT_CONTEXT_TOKENS_MARKER = 'E2E_REPORT_CONTEXT_TOKENS'; const ASSERT_HISTORY_MARKER = 'E2E_ASSERT_HISTORY:'; const ASSERT_PROJECT_CONTEXT_MARKER = 'E2E_ASSERT_PROJECT_CONTEXT:'; const ASSERT_QUOTE_MARKER = 'E2E_ASSERT_QUOTE:'; const REPLY_MARKER = 'E2E_REPLY:'; const THINK_REPLY_MARKER = 'E2E_THINK_REPLY:'; /** A reasoning part long enough that its settled prefix dwarfs the words still fading in. */ const LONG_THINK_REPLY_MARKER = 'E2E_LONG_THINK_REPLY:'; const LONG_THINK_WORDS = 400; const SLOW_THINK_REPLY_MARKER = 'E2E_SLOW_THINK_REPLY:'; const COUNTED_REPLY_MARKER = 'E2E_COUNTED_REPLY:'; const ORDERED_REPLY_MARKER = 'E2E_ORDERED_REPLY:'; const SLOW_REPLY_MARKER = 'E2E_SLOW_REPLY:'; const EMPTY_SLOW_REPLY_MARKER = 'E2E_EMPTY_SLOW_REPLY:'; const PRE_TOKEN_REPLY_MARKER = 'E2E_PRE_TOKEN_REPLY:'; /** A run that completes having produced no content at all: the shape a * summarizer takes when it returns nothing for a manual compaction. */ const EMPTY_REPLY_MARKER = 'E2E_EMPTY_REPLY:'; const SLOW_COUNTED_REPLY_MARKER = 'E2E_SLOW_COUNTED_REPLY:'; const STEER_TOOL_REPLY_MARKER = 'E2E_STEER_TOOL_REPLY:'; const INTERRUPT_TOOL_REPLY_MARKER = 'E2E_INTERRUPT_TOOL_REPLY:'; const MCP_APP_MARKER = 'E2E_MCP_APP:'; const MCP_APP_PHASE_MARKER = 'E2E_MCP_APP_PHASE:'; const MCP_LINK_APP_MARKER = 'E2E_MCP_LINK_APP:'; const MCP_LEGACY_MARKER = 'E2E_MCP_LEGACY:'; const MCP_LEGACY_MIMELESS_MARKER = 'E2E_MCP_LEGACY_MIMELESS:'; const MCP_LEGACY_ACTION_MARKER = 'E2E_LEGACY_ACTION'; /** `E2E_CACHE_USAGE: 1h` or `5m`: the final usage reports Anthropic-style cache * writes (3000, of which 2000 are 1-hour for `1h`) and 500 cache reads, with * the TTL split on `response_metadata.usage` where the agents SDK leaves it. */ const CACHE_USAGE_MARKER = 'E2E_CACHE_USAGE:'; const CACHE_USAGE_WRITES = 3000; const CACHE_USAGE_READS = 500; function cacheUsageFields(messages, input_tokens, output_tokens) { const ttl = getMarkerValue(getLatestUserText(messages), CACHE_USAGE_MARKER); if (ttl !== '1h' && ttl !== '5m') { return { usage_metadata: { input_tokens, output_tokens, total_tokens: input_tokens + output_tokens }, }; } const oneHour = ttl === '1h' ? 2000 : 0; const cachedInput = input_tokens + CACHE_USAGE_WRITES + CACHE_USAGE_READS; return { usage_metadata: { input_tokens: cachedInput, output_tokens, total_tokens: cachedInput + output_tokens, input_token_details: { cache_creation: CACHE_USAGE_WRITES, cache_read: CACHE_USAGE_READS }, }, response_metadata: { usage: { cache_creation_input_tokens: CACHE_USAGE_WRITES, cache_read_input_tokens: CACHE_USAGE_READS, cache_creation: { ephemeral_5m_input_tokens: CACHE_USAGE_WRITES - oneHour, ephemeral_1h_input_tokens: oneHour, }, }, }, }; } const STEER_SPLIT_REPLY_MARKER = 'E2E_STEER_SPLIT_REPLY:'; const STEER_LATE_REPLY_MARKER = 'E2E_STEER_LATE_REPLY:'; const ACTIVITY_REPLY_MARKER = 'E2E_ACTIVITY_REPLY:'; const ACTIVITY_PHASE_REPLY_MARKER = 'E2E_ACTIVITY_PHASE_REPLY:'; const TOOL_THEN_THINK_REPLY_MARKER = 'E2E_TOOL_THEN_THINK_REPLY:'; const ACTIVITY_FAILED_REPLY_MARKER = 'E2E_ACTIVITY_FAILED_REPLY:'; const ACTIVITY_PROSE_REPLY_MARKER = 'E2E_ACTIVITY_PROSE_REPLY:'; const ASK_USER_QUESTION_MARKER = 'E2E_ASK_USER_QUESTION:'; /** A three-question batch; the LONG variant gives the first question a maximum-size prompt. */ const ASK_USER_QUESTIONS_MARKER = 'E2E_ASK_USER_QUESTIONS:'; const ASK_USER_LONG_QUESTIONS_MARKER = 'E2E_ASK_USER_LONG_QUESTIONS:'; const RESUME_ICON_REPLY_MARKER = 'E2E_RESUME_ICON_REPLY:'; const FORCED_ERROR_MARKER = 'E2E_FORCED_ERROR:'; /** `: