|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockCreate, mockExecuteProviderTool } = vi.hoisted(() => ({ |
| 7 | + mockCreate: vi.fn(), |
| 8 | + mockExecuteProviderTool: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('openai', () => ({ |
| 12 | + default: vi.fn().mockImplementation( |
| 13 | + class { |
| 14 | + chat = { completions: { create: mockCreate } } |
| 15 | + } |
| 16 | + ), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@/providers', () => ({ MAX_TOOL_ITERATIONS: 20 })) |
| 20 | + |
| 21 | +vi.mock('@/providers/runtime-context', () => ({ |
| 22 | + executeProviderTool: mockExecuteProviderTool, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/providers/models', () => ({ |
| 26 | + getProviderModels: () => [], |
| 27 | + getProviderDefaultModel: () => '', |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@/providers/attachments', () => ({ |
| 31 | + formatMessagesForProvider: (messages: unknown) => messages, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/providers/trace-enrichment', () => ({ |
| 35 | + enrichLastModelSegmentFromChatCompletions: vi.fn(), |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('@/providers/transport', () => ({ openAICompatTransport: () => ({}) })) |
| 39 | + |
| 40 | +vi.mock('@/providers/tool-schema-adapter', () => ({ |
| 41 | + adaptOpenAIChatToolSchema: (tool: { id: string }) => ({ |
| 42 | + type: 'function', |
| 43 | + function: { name: tool.id, parameters: {} }, |
| 44 | + }), |
| 45 | +})) |
| 46 | + |
| 47 | +vi.mock('@/providers/openai-compat/assistant-history', () => ({ |
| 48 | + createOpenAICompatAssistantHistory: () => ({ role: 'assistant', content: '' }), |
| 49 | +})) |
| 50 | + |
| 51 | +vi.mock('@/providers/openai-compat/stream-events', () => ({ |
| 52 | + createOpenAICompatibleAgentEventStream: () => new ReadableStream({ start: (c) => c.close() }), |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('@/providers/stream-events', () => ({ |
| 56 | + createSettledAgentEventStream: () => new ReadableStream({ start: (c) => c.close() }), |
| 57 | +})) |
| 58 | + |
| 59 | +vi.mock('@/providers/streaming-execution', () => ({ |
| 60 | + createStreamingExecution: vi.fn(() => ({ stream: null, execution: null })), |
| 61 | +})) |
| 62 | + |
| 63 | +vi.mock('@/providers/utils', () => ({ |
| 64 | + isFunctionToolCall: (toolCall: unknown) => |
| 65 | + typeof toolCall === 'object' && |
| 66 | + toolCall !== null && |
| 67 | + 'function' in toolCall && |
| 68 | + (toolCall as { function?: unknown }).function != null, |
| 69 | + calculateCost: vi.fn(() => ({ input: 0, output: 0, total: 0 })), |
| 70 | + sumToolCosts: vi.fn(() => 0), |
| 71 | + prepareToolExecution: vi.fn((_tool, toolArgs) => ({ |
| 72 | + toolParams: toolArgs, |
| 73 | + executionParams: toolArgs, |
| 74 | + })), |
| 75 | + prepareToolsWithUsageControl: vi.fn((tools) => ({ |
| 76 | + tools, |
| 77 | + toolChoice: 'auto', |
| 78 | + forcedTools: [], |
| 79 | + hasFilteredTools: false, |
| 80 | + })), |
| 81 | + checkForForcedToolUsageOpenAI: vi.fn(() => ({ |
| 82 | + hasUsedForcedTool: false, |
| 83 | + usedForcedTools: [], |
| 84 | + })), |
| 85 | +})) |
| 86 | + |
| 87 | +import type { StreamingExecution } from '@/executor/types' |
| 88 | +import type { ProviderRequest, ProviderResponse, ProviderToolConfig } from '@/providers/types' |
| 89 | +import { xAIProvider } from '@/providers/xai' |
| 90 | + |
| 91 | +interface ChatOptions { |
| 92 | + content?: string | null |
| 93 | + toolCalls?: Array<{ id: string; function: { name: string; arguments: string } }> |
| 94 | +} |
| 95 | + |
| 96 | +function chat({ content = null, toolCalls }: ChatOptions = {}) { |
| 97 | + return { |
| 98 | + choices: [ |
| 99 | + { |
| 100 | + message: { content, tool_calls: toolCalls }, |
| 101 | + finish_reason: toolCalls ? 'tool_calls' : 'stop', |
| 102 | + }, |
| 103 | + ], |
| 104 | + usage: { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function tool(name: string): ProviderToolConfig { |
| 109 | + return { |
| 110 | + id: name, |
| 111 | + name, |
| 112 | + description: 'd', |
| 113 | + params: {}, |
| 114 | + parameters: { type: 'object', properties: {}, required: [] }, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function run( |
| 119 | + request: Partial<ProviderRequest> = {} |
| 120 | +): Promise<ProviderResponse | StreamingExecution> { |
| 121 | + return xAIProvider.executeRequest!({ |
| 122 | + model: 'grok-4.6', |
| 123 | + apiKey: 'test-key', |
| 124 | + messages: [{ role: 'user', content: 'Hi' }], |
| 125 | + ...request, |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +const firstPayload = () => mockCreate.mock.calls[0][0] |
| 130 | +const lastPayload = () => mockCreate.mock.calls.at(-1)![0] |
| 131 | + |
| 132 | +describe('xAIProvider.executeRequest', () => { |
| 133 | + beforeEach(() => { |
| 134 | + vi.clearAllMocks() |
| 135 | + mockCreate.mockResolvedValue(chat({ content: 'hello' })) |
| 136 | + mockExecuteProviderTool.mockResolvedValue({ |
| 137 | + rawResponse: { success: true, output: { ok: true } }, |
| 138 | + modelResponse: { success: true, output: { ok: true } }, |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('maps temperature and max_completion_tokens', async () => { |
| 143 | + await run({ temperature: 0.5, maxTokens: 256 }) |
| 144 | + |
| 145 | + const payload = firstPayload() |
| 146 | + expect(payload.model).toBe('grok-4.6') |
| 147 | + expect(payload.temperature).toBe(0.5) |
| 148 | + expect(payload.max_completion_tokens).toBe(256) |
| 149 | + }) |
| 150 | + |
| 151 | + it('forwards reasoning_effort only when set to a non-default value', async () => { |
| 152 | + await run({ reasoningEffort: 'xhigh' }) |
| 153 | + expect(firstPayload().reasoning_effort).toBe('xhigh') |
| 154 | + |
| 155 | + mockCreate.mockClear() |
| 156 | + await run({ reasoningEffort: 'auto' }) |
| 157 | + expect(firstPayload().reasoning_effort).toBeUndefined() |
| 158 | + |
| 159 | + mockCreate.mockClear() |
| 160 | + await run({}) |
| 161 | + expect(firstPayload().reasoning_effort).toBeUndefined() |
| 162 | + }) |
| 163 | + |
| 164 | + it('keeps reasoning_effort on every follow-up call in the tool loop', async () => { |
| 165 | + mockCreate |
| 166 | + .mockResolvedValueOnce( |
| 167 | + chat({ toolCalls: [{ id: 'c1', function: { name: 'known', arguments: '{"q":1}' } }] }) |
| 168 | + ) |
| 169 | + .mockResolvedValueOnce(chat({ content: 'done' })) |
| 170 | + |
| 171 | + await run({ tools: [tool('known')], reasoningEffort: 'high' }) |
| 172 | + |
| 173 | + expect(mockCreate.mock.calls.length).toBeGreaterThan(1) |
| 174 | + for (const [payload] of mockCreate.mock.calls) { |
| 175 | + expect(payload.reasoning_effort).toBe('high') |
| 176 | + } |
| 177 | + }) |
| 178 | + |
| 179 | + it('keeps reasoning_effort on the response_format request', async () => { |
| 180 | + await run({ |
| 181 | + reasoningEffort: 'low', |
| 182 | + responseFormat: { name: 'r', schema: { type: 'object', properties: {} } }, |
| 183 | + }) |
| 184 | + |
| 185 | + const payload = lastPayload() |
| 186 | + expect(payload.response_format.type).toBe('json_schema') |
| 187 | + expect(payload.reasoning_effort).toBe('low') |
| 188 | + }) |
| 189 | + |
| 190 | + it('keeps reasoning_effort on the direct streaming request', async () => { |
| 191 | + await run({ reasoningEffort: 'medium', stream: true }) |
| 192 | + |
| 193 | + const payload = firstPayload() |
| 194 | + expect(payload.stream).toBe(true) |
| 195 | + expect(payload.reasoning_effort).toBe('medium') |
| 196 | + }) |
| 197 | +}) |
0 commit comments