File size: 5,684 Bytes
e43a4a9
 
 
 
 
 
 
 
 
 
 
 
 
740e55f
 
 
e43a4a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740e55f
8b5482f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740e55f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b5482f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import test from "node:test";
import assert from "node:assert/strict";
import { createRequestNormalizationService } from "../src/services/requestNormalizationService.js";

test("normalizes audio URLs to mp3 base64 and raw image base64 to data URLs", async () => {
  const service = createRequestNormalizationService({
    audioConversionService: {
      async downloadAndConvertToMp3Base64(url) {
        assert.equal(url, "https://example.com/sample.wav");
        return {
          data: "ZmFrZS1tcDM=",
          format: "mp3"
        };
      },
      async normalizeBase64Audio(audio) {
        return audio;
      }
    }
  });

  const { normalizedBody, responseContext } = await service.normalize({
    messages: [
      {
        role: "user",
        content: [
          {
            type: "image_url",
            image_url: {
              url: "iVBORw0KGgoAAAANSUhEUgAAAAUA"
            },
            mime_type: "image/png"
          },
          {
            type: "input_audio",
            input_audio: {
              url: "https://example.com/sample.wav"
            }
          }
        ]
      }
    ],
    audio: {
      format: "wav"
    }
  });

  assert.equal(normalizedBody.messages[0].content[0].image_url.url.startsWith("data:image/png;base64,"), true);
  assert.deepEqual(normalizedBody.messages[0].content[1], {
    type: "input_audio",
    input_audio: {
      data: "ZmFrZS1tcDM=",
      format: "mp3"
    }
  });
  assert.equal(responseContext.audioFormat, "wav");
});

test("downloads remote audio when the URL is provided in input_audio.data", async () => {
  const service = createRequestNormalizationService({
    audioConversionService: {
      async downloadAndConvertToMp3Base64(url) {
        assert.equal(url, "https://example.com/sample.mp4");
        return {
          data: "ZG93bmxvYWRlZC1hbmQtY29udmVydGVk",
          format: "mp3"
        };
      },
      async normalizeBase64Audio() {
        throw new Error("unexpected base64 conversion");
      }
    }
  });

  const { normalizedBody } = await service.normalize({
    messages: [
      {
        role: "user",
        content: [
          {
            type: "input_audio",
            input_audio: {
              data: "https://example.com/sample.mp4",
              format: "m4a"
            }
          }
        ]
      }
    ]
  });

  assert.deepEqual(normalizedBody.messages[0].content[0], {
    type: "input_audio",
    input_audio: {
      data: "ZG93bmxvYWRlZC1hbmQtY29udmVydGVk",
      format: "mp3"
    }
  });
});

test("normalizes uploaded wav audio before forwarding upstream", async () => {
  const service = createRequestNormalizationService({
    audioConversionService: {
      async downloadAndConvertToMp3Base64() {
        throw new Error("unexpected url conversion");
      },
      async normalizeBase64Audio(audio) {
        assert.deepEqual(audio, {
          data: "UklGRl8AAABXQVZFZm10",
          format: "wav"
        });

        return {
          data: "bm9ybWFsaXplZC13YXY=",
          format: "wav"
        };
      }
    }
  });

  const { normalizedBody } = await service.normalize({
    messages: [
      {
        role: "user",
        content: [
          {
            type: "input_audio",
            input_audio: {
              data: "UklGRl8AAABXQVZFZm10",
              format: "wav"
            }
          }
        ]
      }
    ]
  });

  assert.deepEqual(normalizedBody.messages[0].content[0], {
    type: "input_audio",
    input_audio: {
      data: "bm9ybWFsaXplZC13YXY=",
      format: "wav"
    }
  });
});

test("preserves uploaded m4a format for conversion and forwards mp3 output", async () => {
  const service = createRequestNormalizationService({
    audioConversionService: {
      async downloadAndConvertToMp3Base64() {
        throw new Error("unexpected url conversion");
      },
      async normalizeBase64Audio(audio) {
        assert.deepEqual(audio, {
          data: "AAAAGGZ0eXBNNEEg",
          format: "m4a"
        });

        return {
          data: "Y29udmVydGVkLW1wMw==",
          format: "mp3"
        };
      }
    }
  });

  const { normalizedBody } = await service.normalize({
    messages: [
      {
        role: "user",
        content: [
          {
            type: "input_audio",
            input_audio: {
              data: "AAAAGGZ0eXBNNEEg",
              format: "m4a"
            }
          }
        ]
      }
    ]
  });

  assert.deepEqual(normalizedBody.messages[0].content[0], {
    type: "input_audio",
    input_audio: {
      data: "Y29udmVydGVkLW1wMw==",
      format: "mp3"
    }
  });
});

test("parses m4a data urls with codec metadata before conversion", async () => {
  const service = createRequestNormalizationService({
    audioConversionService: {
      async downloadAndConvertToMp3Base64() {
        throw new Error("unexpected url conversion");
      },
      async normalizeBase64Audio(audio) {
        assert.deepEqual(audio, {
          data: "AAAAGGZ0eXBNNEEg",
          format: "m4a"
        });

        return {
          data: "Y29udmVydGVkLW1wMw==",
          format: "mp3"
        };
      }
    }
  });

  const { normalizedBody } = await service.normalize({
    messages: [
      {
        role: "user",
        content: [
          {
            type: "input_audio",
            input_audio: {
              data: "data:audio/mp4;codecs=mp4a.40.2;base64,AAAAGGZ0eXBNNEEg",
              format: "m4a"
            }
          }
        ]
      }
    ]
  });

  assert.deepEqual(normalizedBody.messages[0].content[0], {
    type: "input_audio",
    input_audio: {
      data: "Y29udmVydGVkLW1wMw==",
      format: "mp3"
    }
  });
});