15
MAX_URL_UPLOAD_SIZE_BYTES,
16
} from '@composio/core';
17
import {
18
+ findFileUploadablePaths,
19
+ findLocalPathArguments,
20
schemaHasFileUploadable,
21
ToolFileUploadError,
22
uploadToolInputFiles,
32
},
33
};
34
35
+describe('file argument paths', () => {
36
+ const schema = {
37
+ type: 'object',
38
+ properties: {
39
+ operations: {
40
+ type: 'array',
41
+ items: {
42
+ oneOf: [
43
+ {
44
+ properties: { type: { const: 'add_image' }, image: { file_uploadable: true } },
45
+ required: ['type'],
46
+ },
47
+ {
48
+ properties: { type: { const: 'replace_image' }, image: { file_uploadable: true } },
49
+ required: ['type'],
50
+ },
51
+ {
52
+ properties: { type: { const: 'set_text' }, text: { type: 'string' } },
53
+ required: ['type'],
54
+ },
55
+ ],
56
+ },
57
+ },
58
+ },
59
+ };
60
+
61
+ it('binds the selected union branch to the existing array index', () => {
62
+ expect(
63
+ findFileUploadablePaths(schema, [], {
64
+ operations: [{ type: 'set_text', text: 'keep' }, { type: 'add_image' }],
65
+ })
66
+ ).toEqual([['operations', 1, 'image']]);
67
+ });
68
+
69
+ it('does not invent array entries or choose between multiple image operations', () => {
70
+ expect(findFileUploadablePaths(schema, [], { operations: [] })).toEqual([]);
71
+ expect(findFileUploadablePaths(schema, [], { operations: [{ type: 'set_text' }] })).toEqual([]);
72
+ expect(
73
+ findFileUploadablePaths(schema, [], {
74
+ operations: [{ type: 'add_image' }, { type: 'replace_image' }],
75
+ })
76
+ ).toEqual([
77
+ ['operations', 0, 'image'],
78
+ ['operations', 1, 'image'],
79
+ ]);
80
+ });
81
+
82
+ it('preserves tuple indices, including preceding boolean schemas', () => {
83
+ expect(
84
+ findFileUploadablePaths(
85
+ { type: 'array', items: [true, { file_uploadable: true }] },
86
+ [],
87
+ ['keep', undefined]
88
+ )
89
+ ).toEqual([[1]]);
90
+ });
91
+
92
+ const optionalModeSchema = (required: ReadonlyArray<string>) => ({
93
+ type: 'object',
94
+ allOf: [
95
+ {
96
+ properties: { mode: { const: 'upload' }, file: { file_uploadable: true } },
97
+ required,
98
+ },
99
+ ],
100
+ });
101
+
102
+ it('keeps a branch whose optional const property the caller omitted', () => {
103
+ expect(findFileUploadablePaths(optionalModeSchema(['file']), [], {})).toEqual([['file']]);
104
+ });
105
+
106
+ it('drops a branch whose const property is required and omitted, or set to another value', () => {
107
+ expect(findFileUploadablePaths(optionalModeSchema(['mode', 'file']), [], {})).toEqual([]);
108
+ expect(findFileUploadablePaths(optionalModeSchema(['file']), [], { mode: 'link' })).toEqual([]);
109
+ });
110
+});
111
+
112
+describe('local file MIME metadata', () => {
113
+ afterEach(() => {
114
+ vi.unstubAllGlobals();
115
+ vi.restoreAllMocks();
116
+ });
117
+
118
+ it.effect.each([
119
+ { filename: 'picture.png', expectedMime: 'image/png' },
120
+ { filename: 'picture.JPG', expectedMime: 'image/jpeg' },
121
+ { filename: 'document.pdf', expectedMime: 'application/pdf' },
122
+ {
123
+ filename: 'slides.pptx',
124
+ expectedMime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
125
+ },
126
+ {
127
+ filename: 'document.docx',
128
+ expectedMime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
129
+ },
130
+ { filename: 'unknown.extension-dogfood', expectedMime: 'application/octet-stream' },
131
+ { filename: 'no-extension', expectedMime: 'application/octet-stream' },
132
+ ])(
133
+ 'uses the same MIME for the request, upload, and hydrated $filename',
134
+ ({ filename, expectedMime }) => {
135
+ const root = mkdtempSync(path.join(os.tmpdir(), 'composio-mime-'));
136
+ const file = path.join(root, filename);
137
+ writeFileSync(file, 'fixture bytes');
138
+ const { client, requests } = makeClient();
139
+ const uploadFetch = vi.fn(async () => new Response(null, { status: 200 }));
140
+ vi.stubGlobal('fetch', uploadFetch);
141
+ return Effect.gen(function* () {
142
+ const fsApi = yield* FileSystem.FileSystem;
143
+ const pathApi = yield* Path.Path;
144
+ const result = yield* uploadToolInputFiles({
145
+ fs: fsApi,
146
+ path: pathApi,
147
+ toolSlug: 'GMAIL_SEND_EMAIL',
148
+ arguments_: { attachment: file },
149
+ inputSchema,
150
+ client,
151
+ });
152
+ expect(requests[0]?.body).toMatchObject({ mimetype: expectedMime });
153
+ expect(uploadFetch).toHaveBeenCalledWith(
154
+ 'https://1.1.1.1/put',
155
+ expect.objectContaining({
156
+ headers: expect.objectContaining({ 'Content-Type': expectedMime }),
157
+ })
158
+ );
159
+ expect(result.attachment).toMatchObject({ name: filename, mimetype: expectedMime });
160
+ }).pipe(
161
+ Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)),
162
+ Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true })))
163
+ );
164
+ }
165
+ );
166
+});
167
+
168
// The client over a transport that answers the presigned-URL request with
169
// `presigned`, and records each request it receives.
170
const makeClient = (
289
path: '/api/v3.1/files/upload/request',
290
body: {
291
filename: 'document.pdf',
157
- mimetype: 'application/octet-stream',
292
+ mimetype: 'application/pdf',
293
md5: '5d41402abc4b2a76b9719d911017c592',
294
tool_slug: 'GMAIL_SEND_EMAIL',
295
toolkit_slug: 'gmail',
508
}).pipe(Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)));
509
});
510
});
511
+
512
+// TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD as the API serves it (issue #4965):
513
+// `file` is described as a local file but is not `file_uploadable`.
514
+const trelloAttachmentSchema = {
515
+ type: 'object',
516
+ required: ['idCard'],
517
+ properties: {
518
+ idCard: { type: 'string', description: 'ID or shortLink of the Trello card.' },
519
+ url: {
520
+ type: 'string',
521
+ description:
522
+ "URL of an external resource to attach if not uploading a `file`. Must start with 'http://' or 'https://'.",
523
+ },
524
+ file: {
525
+ type: 'string',
526
+ title: 'File',
527
+ description: 'Path to the local file to attach if not linking a `url`.',
528
+ },
529
+ },
530
+};
531
+
532
+describe('uploadToolInputFiles — local file for an input that is not file_uploadable (issue #4965)', () => {
533
+ const withTempFile = <A, E, R>(
534
+ use: (paths: { readonly root: string; readonly file: string }) => Effect.Effect<A, E, R>
535
+ ) => {
536
+ const root = mkdtempSync(path.join(os.tmpdir(), 'composio-upload-'));
537
+ const file = path.join(root, 'photo.jpg');
538
+ writeFileSync(file, 'jpeg bytes');
539
+ return use({ root, file }).pipe(
540
+ Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)),
541
+ Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true })))
542
+ );
543
+ };
544
+
545
+ const run = (arguments_: Record<string, unknown>, schema: Record<string, unknown>) =>
546
+ Effect.gen(function* () {
547
+ const { client, requests } = makeClient();
548
+ const result = yield* uploadToolInputFiles({
549
+ fs: yield* FileSystem.FileSystem,
550
+ path: yield* Path.Path,
551
+ toolSlug: 'TRELLO_ADD_CARDS_ATTACHMENTS_BY_ID_CARD',
552
+ arguments_,
553
+ inputSchema: schema,
554
+ client,
555
+ }).pipe(Effect.result);
556
+ return { result, requests };
557
+ });
558
+
559
+ it.effect('fails before anything is sent when the argument names a local file', () =>
560
+ withTempFile(({ file }) =>
561
+ Effect.gen(function* () {
562
+ const { result, requests } = yield* run({ idCard: 'card', file }, trelloAttachmentSchema);
563
+
564
+ const failure = yield* Effect.flip(Effect.fromResult(result));
565
+ expect(failure).toBeInstanceOf(ToolFileUploadError);
566
+ expect(failure).toMatchObject({ reason: 'not-uploadable' });
567
+ expect(failure.message).toContain(`"file" (${file})`);
568
+ expect(failure.message).toContain('Nothing was executed');
569
+ expect(requests).toEqual([]);
570
+ })
571
+ )
572
+ );
573
+
574
+ it.effect('names every offending argument, including ones nested in arrays', () =>
575
+ withTempFile(({ file }) =>
576
+ Effect.gen(function* () {
577
+ const { result } = yield* run(
578
+ { files: [{ file_path: file }, { content: 'inline' }] },
579
+ {
580
+ type: 'object',
581
+ properties: {
582
+ files: {
583
+ type: 'array',
584
+ items: {
585
+ type: 'object',
586
+ properties: {
587
+ file_path: {
588
+ type: 'string',
589
+ description: 'Local filesystem path to the file.',
590
+ },
591
+ content: { type: 'string' },
592
+ },
593
+ },
594
+ },
595
+ },
596
+ }
597
+ );
598
+
599
+ const failure = yield* Effect.flip(Effect.fromResult(result));
600
+ expect(failure.message).toContain(`"files.0.file_path" (${file})`);
601
+ })
602
+ )
603
+ );
604
+
605
+ it.effect('passes a path that does not exist here through, as it may be remote', () =>
606
+ withTempFile(({ root }) =>
607
+ Effect.gen(function* () {
608
+ const remote = path.join(root, 'missing', 'report.pdf');
609
+ const { result, requests } = yield* run(
610
+ { idCard: 'card', file: remote },
611
+ trelloAttachmentSchema
612
+ );
613
+
614
+ expect(yield* Effect.fromResult(result)).toEqual({ idCard: 'card', file: remote });
615
+ expect(requests).toEqual([]);
616
+ })
617
+ )
618
+ );
619
+
620
+ it.effect.each([
621
+ { name: 'a directory', value: (root: string) => root },
622
+ { name: 'a URL', value: () => 'https://example.com/photo.jpg' },
623
+ { name: 'a bare word', value: () => 'photo' },
624
+ ])('passes $name through', ({ value }) =>
625
+ withTempFile(({ root }) =>
626
+ Effect.gen(function* () {
627
+ const file = value(root);
628
+ const { result } = yield* run({ idCard: 'card', file }, trelloAttachmentSchema);
629
+ expect(yield* Effect.fromResult(result)).toEqual({ idCard: 'card', file });
630
+ })
631
+ )
632
+ );
633
+
634
+ it.effect('leaves an existing file alone when the input is a repository path', () =>
635
+ withTempFile(({ file }) =>
636
+ Effect.gen(function* () {
637
+ const { result } = yield* run(
638
+ { path: file },
639
+ {
640
+ type: 'object',
641
+ properties: {
642
+ path: {
643
+ type: 'string',
644
+ description: 'The full path to the file in the repository, including the filename.',
645
+ },
646
+ },
647
+ }
648
+ );
649
+
650
+ expect(yield* Effect.fromResult(result)).toEqual({ path: file });
651
+ })
652
+ )
653
+ );
654
+
655
+ it('reads the description of an optional input from its non-null variant', () => {
656
+ expect(
657
+ findLocalPathArguments(
658
+ { file: '/tmp/photo.jpg' },
659
+ {
660
+ type: 'object',
661
+ properties: {
662
+ file: {
663
+ anyOf: [
664
+ { type: 'string', description: 'Local file path of the video to upload.' },
665
+ { type: 'null' },
666
+ ],
667
+ },
668
+ },
669
+ }
670
+ )
671
+ ).toEqual([{ argumentPath: ['file'], value: '/tmp/photo.jpg' }]);
672
+ });
673
+
674
+ it('skips an input that also accepts an upload', () => {
675
+ expect(
676
+ findLocalPathArguments(
677
+ { file: '/tmp/photo.jpg' },
678
+ {
679
+ type: 'object',
680
+ properties: {
681
+ file: {
682
+ description: 'Path to the local file to upload.',
683
+ anyOf: [{ file_uploadable: true }, { type: 'string' }],
684
+ },
685
+ },
686
+ }
687
+ )
688
+ ).toEqual([]);
689
+ });
690
+
691
+ const localPathDescription = { type: 'string', description: 'Path to the local file to attach.' };
692
+
693
+ it.each([
694
+ {
695
+ name: 'allOf splits the description and the upload marker across branches',
696
+ schema: {
697
+ type: 'object',
698
+ allOf: [
699
+ { properties: { file: localPathDescription } },
700
+ { properties: { file: { file_uploadable: true } } },
701
+ ],
702
+ },
703
+ },
704
+ {
705
+ name: 'anyOf offers an uploadable branch beside the path branch',
706
+ schema: {
707
+ type: 'object',
708
+ properties: { file: { anyOf: [localPathDescription, { file_uploadable: true }] } },
709
+ },
710
+ },
711
+ {
712
+ name: 'oneOf nests the uploadable branch one level down',
713
+ schema: {
714
+ type: 'object',
715
+ properties: {
716
+ file: { oneOf: [localPathDescription, { allOf: [{ file_uploadable: true }] }] },
717
+ },
718
+ },
719
+ },
720
+ ])('never flags an input the upload code uploads: $name', ({ schema }) => {
721
+ expect(findLocalPathArguments({ file: '/tmp/photo.jpg' }, schema)).toEqual([]);
722
+ });
723
+
724
+ it.effect('uploads, not refuses, a local file for an allOf-split uploadable input', () =>
725
+ withTempFile(({ file }) =>
726
+ Effect.gen(function* () {
727
+ vi.stubGlobal(
728
+ 'fetch',
729
+ vi.fn(async () => ({ ok: true, statusText: 'OK' }))
730
+ );
731
+ const { result, requests } = yield* run(
732
+ { file },
733
+ {
734
+ type: 'object',
735
+ allOf: [
736
+ { properties: { file: localPathDescription } },
737
+ { properties: { file: { file_uploadable: true } } },
738
+ ],
739
+ }
740
+ );
741
+
742
+ expect(yield* Effect.fromResult(result)).toMatchObject({
743
+ file: { name: 'photo.jpg', s3key: 's3key-123' },
744
+ });
745
+ expect(requests).toHaveLength(1);
746
+ }).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllGlobals())))
747
+ )
748
+ );
749
+
750
+ it.effect('uploads an input once when several branches mark it uploadable', () =>
751
+ withTempFile(({ file }) =>
752
+ Effect.gen(function* () {
753
+ vi.stubGlobal(
754
+ 'fetch',
755
+ vi.fn(async () => ({ ok: true, statusText: 'OK' }))
756
+ );
757
+ const { result, requests } = yield* run(
758
+ { file, note: 'keep' },
759
+ {
760
+ type: 'object',
761
+ anyOf: [
762
+ { properties: { file: { file_uploadable: true } } },
763
+ { properties: { file: { file_uploadable: true }, note: { type: 'string' } } },
764
+ ],
765
+ }
766
+ );
767
+
768
+ expect(yield* Effect.fromResult(result)).toEqual({
769
+ file: { name: 'photo.jpg', mimetype: 'image/jpeg', s3key: 's3key-123' },
770
+ note: 'keep',
771
+ });
772
+ expect(requests).toHaveLength(1);
773
+ }).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllGlobals())))
774
+ )
775
+ );
776
+
777
+ it('flags only the tuple elements the upload code does not upload', () => {
778
+ // `hydrateFileUploads` reads each tuple element against its own `items` schema.
779
+ expect(
780
+ findLocalPathArguments(
781
+ { files: ['/tmp/photo.jpg', '/tmp/other.jpg'] },
782
+ {
783
+ type: 'object',
784
+ properties: {
785
+ files: {
786
+ type: 'array',
787
+ items: [localPathDescription, { ...localPathDescription, file_uploadable: true }],
788
+ },
789
+ },
790
+ }
791
+ )
792
+ ).toEqual([{ argumentPath: ['files', 0], value: '/tmp/photo.jpg' }]);
793
+ });
794
+
795
+ it('reads descriptions in nested composition branches', () => {
796
+ expect(
797
+ findLocalPathArguments(
798
+ { file: '/tmp/photo.jpg' },
799
+ {
800
+ type: 'object',
801
+ properties: {
802
+ file: { anyOf: [{ type: 'null' }, { allOf: [localPathDescription] }] },
803
+ },
804
+ }
805
+ )
806
+ ).toEqual([{ argumentPath: ['file'], value: '/tmp/photo.jpg' }]);
807
+ });
808
+});
809
+
810
+describe('multiple PowerPoint images', () => {
811
+ afterEach(() => {
812
+ vi.unstubAllGlobals();
813
+ vi.restoreAllMocks();
814
+ });
815
+
816
+ it.effect('keeps each nested image reference paired with its uploaded bytes', () => {
817
+ const root = mkdtempSync(path.join(os.tmpdir(), 'composio-multiple-images-'));
818
+ const first = path.join(root, 'first.png');
819
+ const second = path.join(root, 'second.png');
820
+ writeFileSync(first, 'first image bytes');
821
+ writeFileSync(second, 'second image bytes');
822
+ const { client, requests } = stubComposioClient(request => {
823
+ const { filename } = request.body as { filename: string };
824
+ return Response.json({
825
+ new_presigned_url: `https://1.1.1.1/${filename}`,
826
+ key: `uploads/${filename}`,
827
+ });
828
+ });
829
+ const uploaded = new Map<string, string>();
830
+ vi.stubGlobal('fetch', async (url: string, init: RequestInit) => {
831
+ uploaded.set(url, Buffer.from(init.body as Uint8Array).toString());
832
+ return new Response(null, { status: 200 });
833
+ });
834
+ const schema = {
835
+ type: 'object',
836
+ properties: {
837
+ slides: {
838
+ type: 'array',
839
+ items: {
840
+ type: 'object',
841
+ properties: {
842
+ blocks: {
843
+ type: 'array',
844
+ items: {
845
+ oneOf: [
846
+ { properties: { type: { const: 'text' }, text: { type: 'string' } } },
847
+ { properties: { type: { const: 'image' }, image: { file_uploadable: true } } },
848
+ ],
849
+ },
850
+ },
851
+ },
852
+ },
853
+ },
854
+ },
855
+ };
856
+ const arguments_ = {
857
+ filename: 'two-images.pptx',
858
+ slides: [
859
+ {
860
+ blocks: [
861
+ { type: 'text', text: 'Keep this' },
862
+ { type: 'image', image: first },
863
+ ],
864
+ },
865
+ { blocks: [{ type: 'image', image: second }] },
866
+ ],
867
+ };
868
+ return Effect.gen(function* () {
869
+ const fsApi = yield* FileSystem.FileSystem;
870
+ const pathApi = yield* Path.Path;
871
+ const result = yield* uploadToolInputFiles({
872
+ fs: fsApi,
873
+ path: pathApi,
874
+ toolSlug: 'MICROSOFT_POWERPOINT_CREATE_PRESENTATION',
875
+ arguments_,
876
+ inputSchema: schema,
877
+ client,
878
+ });
879
+ expect(requests).toHaveLength(2);
880
+ expect(uploaded).toEqual(
881
+ new Map([
882
+ ['https://1.1.1.1/first.png', 'first image bytes'],
883
+ ['https://1.1.1.1/second.png', 'second image bytes'],
884
+ ])
885
+ );
886
+ expect(result).toEqual({
887
+ filename: 'two-images.pptx',
888
+ slides: [
889
+ {
890
+ blocks: [
891
+ { type: 'text', text: 'Keep this' },
892
+ {
893
+ type: 'image',
894
+ image: {
895
+ name: 'first.png',
896
+ mimetype: 'image/png',
897
+ s3key: 'uploads/first.png',
898
+ },
899
+ },
900
+ ],
901
+ },
902
+ {
903
+ blocks: [
904
+ {
905
+ type: 'image',
906
+ image: {
907
+ name: 'second.png',
908
+ mimetype: 'image/png',
909
+ s3key: 'uploads/second.png',
910
+ },
911
+ },
912
+ ],
913
+ },
914
+ ],
915
+ });
916
+ expect(arguments_.slides[0]?.blocks[1]?.image).toBe(first);
917
+ expect(arguments_.slides[1]?.blocks[0]?.image).toBe(second);
918
+ }).pipe(
919
+ Effect.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer)),
920
+ Effect.ensuring(Effect.sync(() => rmSync(root, { recursive: true, force: true })))
921
+ );
922
+ });
923
+});