1
1
import { Anthropic } from "@anthropic-ai/sdk"
2
2
import { Content , Part } from "@google/genai"
3
3
4
- // Extended type to support video content blocks that aren't in the standard Anthropic SDK
4
+ /**
5
+ * Extended content block type to support video content that isn't in the standard Anthropic SDK.
6
+ * This interface extends the standard Anthropic content blocks to include video support for Gemini models.
7
+ *
8
+ * @interface VideoContentBlock
9
+ * @property {string } type - Must be "video" to identify this as a video content block
10
+ * @property {Object } source - The video source information
11
+ * @property {string } source.type - Must be "base64" for base64-encoded video data
12
+ * @property {string } source.data - The base64-encoded video data
13
+ * @property {string } source.media_type - The MIME type of the video (e.g., "video/mp4", "video/webm")
14
+ */
5
15
interface VideoContentBlock {
6
16
type : "video"
7
17
source : {
@@ -11,6 +21,10 @@ interface VideoContentBlock {
11
21
}
12
22
}
13
23
24
+ /**
25
+ * Extended content block parameter type that includes both standard Anthropic content blocks
26
+ * and our custom video content block for Gemini model support.
27
+ */
14
28
type ExtendedContentBlockParam = Anthropic . ContentBlockParam | VideoContentBlock
15
29
16
30
export function convertAnthropicContentToGemini ( content : string | ExtendedContentBlockParam [ ] ) : Part [ ] {
@@ -28,11 +42,39 @@ export function convertAnthropicContentToGemini(content: string | ExtendedConten
28
42
}
29
43
30
44
return { inlineData : { data : block . source . data , mimeType : block . source . media_type } }
31
- case "video" :
45
+ case "video" : {
32
46
if ( block . source . type !== "base64" ) {
33
- throw new Error ( "Unsupported video source type" )
47
+ throw new Error ( "Unsupported video source type. Only base64 encoded videos are supported. " )
34
48
}
49
+
50
+ // Validate video MIME type
51
+ const supportedVideoTypes = [ "video/mp4" , "video/webm" , "video/ogg" , "video/quicktime" ]
52
+ if ( ! supportedVideoTypes . includes ( block . source . media_type ) ) {
53
+ throw new Error (
54
+ `Unsupported video format: ${ block . source . media_type } . Supported formats: ${ supportedVideoTypes . join ( ", " ) } ` ,
55
+ )
56
+ }
57
+
58
+ // Check if video data exists
59
+ if ( ! block . source . data || block . source . data . trim ( ) === "" ) {
60
+ throw new Error ( "Video data is empty or missing" )
61
+ }
62
+
63
+ // Validate base64 format
64
+ try {
65
+ // Basic validation - check if it's valid base64
66
+ const base64Regex = / ^ [ A - Z a - z 0 - 9 + / ] * = { 0 , 2 } $ /
67
+ if ( ! base64Regex . test ( block . source . data . replace ( / \s / g, "" ) ) ) {
68
+ throw new Error ( "Invalid base64 format for video data" )
69
+ }
70
+ } catch ( e ) {
71
+ throw new Error (
72
+ `Failed to validate video data: ${ e instanceof Error ? e . message : "Unknown error" } ` ,
73
+ )
74
+ }
75
+
35
76
return { inlineData : { data : block . source . data , mimeType : block . source . media_type } }
77
+ }
36
78
case "tool_use" :
37
79
return {
38
80
functionCall : {
0 commit comments