@tuturuuu/ai 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tuturuuu/ai",
3
3
  "license": "MIT",
4
- "version": "0.8.1",
4
+ "version": "0.8.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/tutur3u/platform",
@@ -76,9 +76,9 @@
76
76
  "@streamdown/math": "^1.0.2",
77
77
  "@streamdown/mermaid": "^1.0.2",
78
78
  "@tuturuuu/google": "0.1.0",
79
- "@tuturuuu/internal-api": "0.29.0",
79
+ "@tuturuuu/internal-api": "0.29.1",
80
80
  "@tuturuuu/supabase": "0.5.0",
81
- "@tuturuuu/utils": "0.23.0",
81
+ "@tuturuuu/utils": "0.24.2",
82
82
  "@vercel/sandbox": "^2.9.2",
83
83
  "@zernio/chat-sdk-adapter": "^0.5.0",
84
84
  "ai": "^7.0.56",
@@ -0,0 +1,60 @@
1
+ export const MAX_INLINE_TRANSCRIPTION_AUDIO_BYTES = 18 * 1024 * 1024;
2
+ export const TRANSCRIPTION_MULTIPART_HEADROOM_BYTES = 1024 * 1024;
3
+ export const MAX_TRANSCRIPTION_MULTIPART_REQUEST_BYTES =
4
+ MAX_INLINE_TRANSCRIPTION_AUDIO_BYTES + TRANSCRIPTION_MULTIPART_HEADROOM_BYTES;
5
+
6
+ const TRANSCRIPTION_AUDIO_MEDIA_TYPES = [
7
+ 'audio/webm',
8
+ 'audio/mp4',
9
+ 'audio/ogg',
10
+ 'audio/mpeg',
11
+ ] as const;
12
+
13
+ export type TranscriptionAudioMediaType =
14
+ (typeof TRANSCRIPTION_AUDIO_MEDIA_TYPES)[number];
15
+
16
+ type TranscriptionAudioValidation =
17
+ | { mediaType: TranscriptionAudioMediaType; ok: true }
18
+ | { message: string; ok: false; status: 400 | 413 | 415 };
19
+
20
+ export function validateTranscriptionAudioFile(
21
+ audio: Pick<File, 'size' | 'type'>
22
+ ): TranscriptionAudioValidation {
23
+ if (audio.size <= 0) {
24
+ return { message: 'The recording is empty.', ok: false, status: 400 };
25
+ }
26
+
27
+ if (audio.size > MAX_INLINE_TRANSCRIPTION_AUDIO_BYTES) {
28
+ return {
29
+ message: 'The recording exceeds the inline transcription limit.',
30
+ ok: false,
31
+ status: 413,
32
+ };
33
+ }
34
+
35
+ const baseMediaType = audio.type.split(';', 1)[0]?.trim().toLowerCase();
36
+ const mediaType = TRANSCRIPTION_AUDIO_MEDIA_TYPES.find(
37
+ (allowedMediaType) => allowedMediaType === baseMediaType
38
+ );
39
+ if (!mediaType) {
40
+ return {
41
+ message:
42
+ 'The recording format is not supported for inline transcription.',
43
+ ok: false,
44
+ status: 415,
45
+ };
46
+ }
47
+
48
+ return { mediaType, ok: true };
49
+ }
50
+
51
+ export function hasOversizedDeclaredContentLength(req: Request) {
52
+ const contentLength = req.headers.get('content-length')?.trim();
53
+ if (!contentLength || !/^\d+$/u.test(contentLength)) {
54
+ return false;
55
+ }
56
+
57
+ return (
58
+ BigInt(contentLength) > BigInt(MAX_TRANSCRIPTION_MULTIPART_REQUEST_BYTES)
59
+ );
60
+ }
@@ -3,6 +3,10 @@ import { createClient } from '@tuturuuu/supabase/next/server';
3
3
  import { generateObject } from 'ai';
4
4
  import { z } from 'zod';
5
5
  import { withAiMemory } from '../../memory';
6
+ import {
7
+ hasOversizedDeclaredContentLength,
8
+ validateTranscriptionAudioFile,
9
+ } from './input';
6
10
 
7
11
  const DEFAULT_MODEL_NAME = 'gemini-3.1-flash-lite';
8
12
 
@@ -42,14 +46,23 @@ export function createPOST() {
42
46
  return new Response('Unauthorized', { status: 401 });
43
47
  }
44
48
 
49
+ if (hasOversizedDeclaredContentLength(req)) {
50
+ return new Response('Audio file too large', { status: 413 });
51
+ }
52
+
45
53
  const formData = await req.formData();
46
- const audioFile = formData.get('audio') as File;
54
+ const audioFile = formData.get('audio');
47
55
  const wsId = formData.get('wsId') as string | null;
48
56
 
49
- if (!audioFile) {
57
+ if (!(audioFile instanceof File)) {
50
58
  return new Response('No audio file provided', { status: 400 });
51
59
  }
52
60
 
61
+ const validation = validateTranscriptionAudioFile(audioFile);
62
+ if (!validation.ok) {
63
+ return new Response(validation.message, { status: validation.status });
64
+ }
65
+
53
66
  // Convert file to buffer
54
67
  const audioBuffer = await audioFile.arrayBuffer();
55
68
  const audioUint8Array = new Uint8Array(audioBuffer);
@@ -73,7 +86,7 @@ export function createPOST() {
73
86
  content: [
74
87
  {
75
88
  type: 'file',
76
- mediaType: 'audio/mpeg',
89
+ mediaType: validation.mediaType,
77
90
  data: audioUint8Array,
78
91
  },
79
92
  ],