Skip to content

Commit ff693e4

Browse files
committed
Update RTVI send-file fileformat to be a mime-type
1 parent e0605d9 commit ff693e4

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

client-js/client/client.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import {
1919
LLMContextMessage,
2020
LLMFunctionCallData,
2121
LLMFunctionCallResult,
22+
MimeTypeMapping,
2223
Participant,
2324
PipecatMetricsData,
2425
RTVIEvent,
2526
RTVIEvents,
2627
RTVIFile,
28+
RTVIFileFormat,
2729
RTVIMessage,
2830
RTVIMessageType,
2931
SendTextOptions,
@@ -691,18 +693,54 @@ export class PipecatClient extends RTVIEventEmitter {
691693

692694
@transportReady
693695
public async sendFile(
694-
file: RTVIFile,
696+
file: RTVIFile | File,
695697
content: string,
696698
options: SendTextOptions = {}
697699
) {
698-
file.format = file.format.toLowerCase() as RTVIFile["format"];
699-
await this._sendMessage(
700-
new RTVIMessage(RTVIMessageType.SEND_FILE, {
701-
file,
702-
content,
703-
options,
704-
})
705-
);
700+
let rtvi_file = file instanceof File ? ({} as RTVIFile) : file;
701+
if (file instanceof File) {
702+
return new Promise<void>((resolve) => {
703+
const reader = new FileReader();
704+
reader.onload = async (e) => {
705+
if (!e.target?.result) {
706+
throw new RTVIErrors.RTVIError("Could not read file data");
707+
}
708+
const fileContent = e.target.result as string;
709+
710+
rtvi_file = {
711+
format: file.type,
712+
source: {
713+
type: "bytes",
714+
bytes: fileContent,
715+
},
716+
};
717+
await this._sendMessage(
718+
new RTVIMessage(RTVIMessageType.SEND_FILE, {
719+
file: rtvi_file,
720+
content,
721+
options,
722+
})
723+
);
724+
resolve();
725+
};
726+
727+
reader.readAsDataURL(file);
728+
});
729+
} else {
730+
let format: string = rtvi_file.format.toLowerCase();
731+
if (format in MimeTypeMapping) {
732+
format = MimeTypeMapping[format as RTVIFileFormat];
733+
}
734+
rtvi_file.format = format;
735+
736+
await this._sendMessage(
737+
new RTVIMessage(RTVIMessageType.SEND_FILE, {
738+
file: rtvi_file,
739+
content,
740+
options,
741+
})
742+
);
743+
}
706744
}
707745

708746
/**

client-js/rtvi/messages.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,38 @@ export type RTVIMediaFormat =
228228
| "avi";
229229
export type RTVIFileFormat = RTVIImageFormat | RTVIDocFormat | RTVIMediaFormat;
230230

231+
export const MimeTypeMapping: Record<RTVIFileFormat, string> = {
232+
// Images
233+
png: "image/png",
234+
jpg: "image/jpeg",
235+
jpeg: "image/jpeg",
236+
webp: "image/webp",
237+
gif: "image/gif",
238+
heic: "image/heic",
239+
hief: "image/heif",
240+
// Documents
241+
pdf: "application/pdf",
242+
csv: "text/csv",
243+
txt: "text/plain",
244+
md: "text/markdown",
245+
doc: "application/msword",
246+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
247+
xls: "application/vnd.ms-excel",
248+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
249+
json: "application/json",
250+
html: "text/html",
251+
css: "text/css",
252+
javascript: "application/javascript",
253+
// Media
254+
mp3: "audio/mpeg",
255+
wav: "audio/wav",
256+
ogg: "audio/ogg",
257+
aac: "audio/aac",
258+
mp4: "video/mp4",
259+
webm: "video/webm",
260+
avi: "video/x-msvideo",
261+
};
262+
231263
export type FileSourceType = "bytes" | "url";
232264

233265
export type FileBytes = {
@@ -243,13 +275,16 @@ export type FileUrl = {
243275

244276
export type RTVIFile = {
245277
name?: string;
246-
format: RTVIFileFormat;
278+
// RTVI definition takes the Mime type here, but in client-js, we support
279+
// clients providing shorthands defined above and we map them to Mime types
280+
format: string;
247281
source: FileBytes | FileUrl;
248-
customOpts: { [key: number | string]: Serializable }; // for things like 'detail' in openAI or 'citations' in Bedrock
282+
// for things like 'detail' in openAI or 'citations' in Bedrock
283+
customOpts?: { [key: number | string]: Serializable };
249284
};
250285

251286
export type FileSupport = {
252-
formats: RTVIFileFormat[];
287+
formats: string[];
253288
sources: FileSourceType[];
254289
maxSize: number; // bytes
255290
};

0 commit comments

Comments
 (0)