|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { modalChatCompletionTool } from '@/tools/modal/chat_completion' |
| 6 | +import type { ModalChatCompletionParams } from '@/tools/modal/types' |
| 7 | +import { MODAL_SHARED_INFERENCE_URL } from '@/tools/modal/utils' |
| 8 | + |
| 9 | +const buildUrl = modalChatCompletionTool.request.url as ( |
| 10 | + params: ModalChatCompletionParams |
| 11 | +) => string |
| 12 | +const buildBody = modalChatCompletionTool.request.body as ( |
| 13 | + params: ModalChatCompletionParams |
| 14 | +) => Record<string, unknown> |
| 15 | +const transform = modalChatCompletionTool.transformResponse! |
| 16 | + |
| 17 | +const baseParams: ModalChatCompletionParams = { |
| 18 | + model: 'my-endpoint.us-west.modal.direct', |
| 19 | + content: 'hello', |
| 20 | + tokenId: 'wk-1', |
| 21 | + tokenSecret: 'ws-2', |
| 22 | +} |
| 23 | + |
| 24 | +describe('modalChatCompletionTool endpoint resolution', () => { |
| 25 | + it('falls back to the shared inference host when no endpoint is given', () => { |
| 26 | + expect(buildUrl(baseParams)).toBe(`${MODAL_SHARED_INFERENCE_URL}/v1/chat/completions`) |
| 27 | + }) |
| 28 | + |
| 29 | + it('treats a blank or whitespace endpoint the same as an omitted one', () => { |
| 30 | + expect(buildUrl({ ...baseParams, endpointUrl: '' })).toBe( |
| 31 | + `${MODAL_SHARED_INFERENCE_URL}/v1/chat/completions` |
| 32 | + ) |
| 33 | + expect(buildUrl({ ...baseParams, endpointUrl: ' ' })).toBe( |
| 34 | + `${MODAL_SHARED_INFERENCE_URL}/v1/chat/completions` |
| 35 | + ) |
| 36 | + }) |
| 37 | + |
| 38 | + it('uses a dedicated endpoint when one is supplied', () => { |
| 39 | + expect(buildUrl({ ...baseParams, endpointUrl: 'https://mine.us-east.modal.direct' })).toBe( |
| 40 | + 'https://mine.us-east.modal.direct/v1/chat/completions' |
| 41 | + ) |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe('modalChatCompletionTool body', () => { |
| 46 | + it('prepends the system prompt as a system message only when one is set', () => { |
| 47 | + expect(buildBody(baseParams).messages).toEqual([{ role: 'user', content: 'hello' }]) |
| 48 | + expect(buildBody({ ...baseParams, systemPrompt: 'be terse' }).messages).toEqual([ |
| 49 | + { role: 'system', content: 'be terse' }, |
| 50 | + { role: 'user', content: 'hello' }, |
| 51 | + ]) |
| 52 | + }) |
| 53 | + |
| 54 | + it('maps the sampling controls onto their OpenAI wire names', () => { |
| 55 | + const body = buildBody({ ...baseParams, maxTokens: 256, temperature: 0, topP: 0.9 }) |
| 56 | + expect(body).toMatchObject({ max_tokens: 256, temperature: 0, top_p: 0.9 }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('omits sampling controls that were never set', () => { |
| 60 | + const body = buildBody(baseParams) |
| 61 | + expect(body).not.toHaveProperty('max_tokens') |
| 62 | + expect(body).not.toHaveProperty('temperature') |
| 63 | + expect(body).not.toHaveProperty('top_p') |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +describe('modalChatCompletionTool transformResponse', () => { |
| 68 | + it('extracts the completion, model, finish reason, and usage', async () => { |
| 69 | + const response = new Response( |
| 70 | + JSON.stringify({ |
| 71 | + model: 'Qwen/Qwen3.5-4B', |
| 72 | + choices: [{ message: { content: 'hi there' }, finish_reason: 'stop' }], |
| 73 | + usage: { prompt_tokens: 3, completion_tokens: 2, total_tokens: 5 }, |
| 74 | + }), |
| 75 | + { status: 200, headers: { 'content-type': 'application/json' } } |
| 76 | + ) |
| 77 | + |
| 78 | + await expect(transform(response, baseParams)).resolves.toMatchObject({ |
| 79 | + success: true, |
| 80 | + output: { |
| 81 | + content: 'hi there', |
| 82 | + model: 'Qwen/Qwen3.5-4B', |
| 83 | + finishReason: 'stop', |
| 84 | + usage: { prompt_tokens: 3, completion_tokens: 2, total_tokens: 5 }, |
| 85 | + }, |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it('nulls usage an engine omitted instead of reporting zeros', async () => { |
| 90 | + const response = new Response(JSON.stringify({ choices: [{ message: { content: 'hi' } }] }), { |
| 91 | + status: 200, |
| 92 | + headers: { 'content-type': 'application/json' }, |
| 93 | + }) |
| 94 | + |
| 95 | + await expect(transform(response, baseParams)).resolves.toMatchObject({ |
| 96 | + output: { |
| 97 | + content: 'hi', |
| 98 | + model: 'my-endpoint.us-west.modal.direct', |
| 99 | + finishReason: null, |
| 100 | + usage: { prompt_tokens: null, completion_tokens: null, total_tokens: null }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('raises the endpoint error on a rejected proxy token', async () => { |
| 106 | + const response = new Response(JSON.stringify({ error: 'invalid proxy auth credentials' }), { |
| 107 | + status: 401, |
| 108 | + headers: { 'content-type': 'application/json' }, |
| 109 | + }) |
| 110 | + await expect(transform(response, baseParams)).rejects.toThrow( |
| 111 | + 'Modal chat completion failed (status 401): invalid proxy auth credentials' |
| 112 | + ) |
| 113 | + }) |
| 114 | +}) |
0 commit comments