Skip to content

Commit 711bbc6

Browse files
committed
add content type
1 parent 25590f4 commit 711bbc6

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

packages/client-twitter/src/utils.ts

+17-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { stringToUuid } from "@ai16z/eliza";
55
import { ClientBase } from "./base";
66
import { elizaLogger } from "@ai16z/eliza";
77
import { DEFAULT_MAX_TWEET_LENGTH } from "./environment";
8+
import { Media } from "@ai16z/eliza";
89
import fs from "fs";
910
import path from "path";
1011

@@ -164,27 +165,15 @@ export async function buildConversationThread(
164165
return thread;
165166
}
166167

167-
export function getMediaType(filePath: string) {
168-
const extension = filePath.split('.').pop().toLowerCase();
169-
switch (extension) {
170-
case 'png':
171-
case 'jpg':
172-
case 'jpeg':
173-
return 'image';
174-
case 'mp4':
175-
return 'video';
176-
default:
177-
throw new Error(`Unsupported media type: ${extension}`);
168+
export function getMediaType(attachment: Media) {
169+
if (attachment.contentType?.startsWith("video")) {
170+
return "video";
171+
} else if (attachment.contentType?.startsWith("image")) {
172+
return "image";
173+
} else {
174+
throw new Error(`Unsupported media type`);
178175
}
179176
}
180-
type Attachment = {
181-
id: string;
182-
url: string; // Path to the file
183-
title?: string;
184-
source?: string;
185-
description?: string;
186-
text?: string;
187-
};
188177

189178
export async function sendTweet(
190179
client: ClientBase,
@@ -206,22 +195,26 @@ export async function sendTweet(
206195

207196
if (content.attachments && content.attachments.length > 0) {
208197
mediaData = await Promise.all(
209-
content.attachments.map(async (attachment:Attachment) => {
198+
content.attachments.map(async (attachment: Media) => {
210199
if (/^(http|https):\/\//.test(attachment.url)) {
211200
// Handle HTTP URLs
212201
const response = await fetch(attachment.url);
213202
if (!response.ok) {
214-
throw new Error(`Failed to fetch file: ${attachment.url}`);
203+
throw new Error(
204+
`Failed to fetch file: ${attachment.url}`
205+
);
215206
}
216-
const mediaBuffer = Buffer.from(await response.arrayBuffer());
217-
const mediaType = getMediaType(attachment.url);
207+
const mediaBuffer = Buffer.from(
208+
await response.arrayBuffer()
209+
);
210+
const mediaType = getMediaType(attachment);
218211
return { data: mediaBuffer, mediaType };
219212
} else if (fs.existsSync(attachment.url)) {
220213
// Handle local file paths
221214
const mediaBuffer = await fs.promises.readFile(
222215
path.resolve(attachment.url)
223216
);
224-
const mediaType = getMediaType(attachment.url);
217+
const mediaType = getMediaType(attachment);
225218
return { data: mediaBuffer, mediaType };
226219
} else {
227220
throw new Error(

packages/core/src/types.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ export type Media = {
561561

562562
/** Text content */
563563
text: string;
564+
565+
/** Content type */
566+
contentType?: string;
564567
};
565568

566569
/**
@@ -1128,12 +1131,16 @@ export interface IPdfService extends Service {
11281131
}
11291132

11301133
export interface IAwsS3Service extends Service {
1131-
uploadFile(imagePath: string, useSignedUrl: boolean, expiresIn: number ): Promise<{
1134+
uploadFile(
1135+
imagePath: string,
1136+
useSignedUrl: boolean,
1137+
expiresIn: number
1138+
): Promise<{
11321139
success: boolean;
11331140
url?: string;
11341141
error?: string;
11351142
}>;
1136-
generateSignedUrl(fileName: string, expiresIn: number): Promise<string>
1143+
generateSignedUrl(fileName: string, expiresIn: number): Promise<string>;
11371144
}
11381145

11391146
export type SearchResult = {

packages/plugin-image-generation/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const imageGeneration: Action = {
205205
source: "imageGeneration",
206206
description: "...", //caption.title,
207207
text: "...", //caption.description,
208+
contentType: "image",
208209
},
209210
],
210211
},

0 commit comments

Comments
 (0)