File size: 5,679 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import { convertToRestPayload } from './apiConversionUtils.js';
import {
  FunctionCallingConfigMode,
  HarmCategory,
  HarmBlockThreshold,
  type GenerateContentParameters,
} from '@google/genai';

describe('apiConversionUtils', () => {
  describe('convertToRestPayload', () => {
    it('handles minimal requests with no config', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
      };

      const result = convertToRestPayload(req);

      expect(result).toStrictEqual({
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
      });
      expect(result['generationConfig']).toBeUndefined();
    });

    it('normalizes string systemInstruction to REST format', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
        config: {
          systemInstruction: 'You are a helpful assistant.',
        },
      };

      const result = convertToRestPayload(req);

      expect(result['systemInstruction']).toStrictEqual({
        parts: [{ text: 'You are a helpful assistant.' }],
      });
      expect(result['generationConfig']).toBeUndefined();
    });

    it('preserves object-based systemInstruction', () => {
      const sysInstruction = { parts: [{ text: 'Object instruction' }] };
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
        config: {
          systemInstruction: sysInstruction,
        },
      };

      const result = convertToRestPayload(req);

      expect(result['systemInstruction']).toStrictEqual(sysInstruction);
    });

    it('hoists capabilities (tools, safety, cachedContent) to the root level', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
        config: {
          tools: [{ functionDeclarations: [{ name: 'myTool' }] }],
          toolConfig: {
            functionCallingConfig: { mode: FunctionCallingConfigMode.ANY },
          },
          safetySettings: [
            {
              category: HarmCategory.HARM_CATEGORY_HARASSMENT,
              threshold: HarmBlockThreshold.BLOCK_NONE,
            },
          ],
          cachedContent: 'cached-content-id',
        },
      };

      const result = convertToRestPayload(req);

      expect(result['tools']).toBeDefined();
      expect(result['toolConfig']).toBeDefined();
      expect(result['safetySettings']).toBeDefined();
      expect(result['cachedContent']).toBe('cached-content-id');
      // generationConfig should be omitted since no pure hyperparameters were passed
      expect(result['generationConfig']).toBeUndefined();
    });

    it('retains pure hyperparameters in generationConfig', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
        config: {
          temperature: 0.7,
          topP: 0.9,
          maxOutputTokens: 100,
        },
      };

      const result = convertToRestPayload(req);

      expect(result['generationConfig']).toStrictEqual({
        temperature: 0.7,
        topP: 0.9,
        maxOutputTokens: 100,
      });
    });

    it('strips JS-specific abortSignal from the final payload', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
        config: {
          temperature: 0.5,
          abortSignal: new AbortController().signal,
        },
      };

      const result = convertToRestPayload(req);

      expect(result['generationConfig']).toStrictEqual({
        temperature: 0.5,
      });
      expect(result['abortSignal']).toBeUndefined();
      // @ts-expect-error Checking that the key doesn't exist inside generationConfig
      expect(result['generationConfig']?.abortSignal).toBeUndefined();
    });

    it('handles a complex kitchen-sink request correctly', () => {
      const req: GenerateContentParameters = {
        model: 'gemini-3-flash',
        contents: [{ role: 'user', parts: [{ text: 'Kitchen sink' }] }],
        config: {
          systemInstruction: 'Be witty.',
          temperature: 0.8,
          tools: [{ functionDeclarations: [{ name: 'test' }] }],
          abortSignal: new AbortController().signal,
          safetySettings: [
            {
              category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
              threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
            },
          ],
          topK: 40,
        },
      };

      const result = convertToRestPayload(req);

      // Root level checks
      expect(result['contents']).toBeDefined();
      expect(result['systemInstruction']).toStrictEqual({
        parts: [{ text: 'Be witty.' }],
      });
      expect(result['tools']).toStrictEqual([
        { functionDeclarations: [{ name: 'test' }] },
      ]);
      expect(result['safetySettings']).toStrictEqual([
        {
          category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
          threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
        },
      ]);
      expect(result['abortSignal']).toBeUndefined();

      // Generation config checks
      expect(result['generationConfig']).toStrictEqual({
        temperature: 0.8,
        topK: 40,
      });
    });
  });
});