Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

feat: Add file upload / download modifiers (#1670)

Musthaq Ahamad84d43812e22d
diff --git a/package.json b/package.json
index 20b0e58a2..d50fbd492 100644
--- a/package.json
+++ b/package.json
@@ -20,10 +20,10 @@
"lint:fix:packages": "eslint ts/packages --ext .ts,.tsx --fix",
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\"",
"prepare": "husky",
- "create:provider": "bash scripts/create-provider.sh",
- "create:example": "bash scripts/create-example.sh",
- "check:peer-deps": "tsx scripts/check-peer-deps.ts",
- "update:peer-deps": "tsx scripts/update-peer-deps.ts",
+ "create:provider": "bash ts/scripts/create-provider.sh",
+ "create:example": "bash ts/scripts/create-example.sh",
+ "check:peer-deps": "tsx ts/scripts/check-peer-deps.ts",
+ "update:peer-deps": "tsx ts/scripts/update-peer-deps.ts",
"prepublish": "pnpm run build:packages",
"changeset": "changeset",
"changeset:version": "changeset version",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2a625cb34..58d6d4a19 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -413,6 +413,22 @@ importers:
specifier: ^5.8.3
version: 5.8.3
+ ts/examples/tools:
+ dependencies:
+ '@composio/core':
+ specifier: workspace:*
+ version: link:../../packages/core
+ dotenv:
+ specifier: ^16.4.1
+ version: 16.5.0
+ devDependencies:
+ '@types/bun':
+ specifier: ^1.2.9
+ version: 1.2.16
+ typescript:
+ specifier: ^5.8.3
+ version: 5.8.3
+
ts/examples/triggers:
dependencies:
'@composio/core':
@@ -532,6 +548,9 @@ importers:
'@composio/json-schema-to-zod':
specifier: 0.1.9
version: 0.1.9
+ '@types/json-schema':
+ specifier: ^7.0.15
+ version: 7.0.15
chalk:
specifier: ^5.4.1
version: 5.4.1
@@ -8738,14 +8757,6 @@ snapshots:
chai: 5.2.0
tinyrainbow: 2.0.0
- dependencies:
- '@vitest/spy': 3.2.3
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
dependencies:
'@vitest/spy': 3.2.3
@@ -8754,22 +8765,6 @@ snapshots:
optionalDependencies:
- dependencies:
- '@vitest/spy': 3.2.3
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
- dependencies:
- '@vitest/spy': 3.2.3
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
dependencies:
'@vitest/spy': 3.2.4
@@ -8827,7 +8822,7 @@ snapshots:
sirv: 3.0.1
tinyglobby: 0.2.14
tinyrainbow: 2.0.0
'@vitest/[email protected]':
dependencies:
@@ -11658,7 +11653,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.3
'@vitest/pretty-format': 3.2.3
'@vitest/runner': 3.2.3
'@vitest/snapshot': 3.2.3
@@ -11742,7 +11737,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.3
'@vitest/pretty-format': 3.2.3
'@vitest/runner': 3.2.3
'@vitest/snapshot': 3.2.3
@@ -11784,7 +11779,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.3
'@vitest/pretty-format': 3.2.3
'@vitest/runner': 3.2.3
'@vitest/snapshot': 3.2.3
diff --git a/ts/examples/tools/.env.example b/ts/examples/tools/.env.example
new file mode 100644
index 000000000..0e728257c
--- /dev/null
+++ b/ts/examples/tools/.env.example
@@ -0,0 +1,6 @@
+# Composio API Key - Get it from https://app.composio.dev
+COMPOSIO_API_KEY=your_composio_api_key_here
+
+# Add other environment variables as needed for your example
+# OPENAI_API_KEY=your_openai_api_key_here
+# ANTHROPIC_API_KEY=your_anthropic_api_key_here
diff --git a/ts/examples/tools/README.md b/ts/examples/tools/README.md
new file mode 100644
index 000000000..f3d715815
--- /dev/null
+++ b/ts/examples/tools/README.md
@@ -0,0 +1,53 @@
+# Tools Example
+
+This example demonstrates how to use Composio SDK for tools.
+
+## Setup
+
+1. **Install dependencies:**
+ ```bash
+ pnpm install
+ ```
+
+2. **Configure environment:**
+ ```bash
+ cp .env.example .env
+ ```
+
+ Then edit `.env` and add your API keys:
+ - `COMPOSIO_API_KEY`: Get it from [Composio Dashboard](https://app.composio.dev)
+
+## Running the Example
+
+```bash
+# Run the example
+pnpm start
+
+# Run in development mode (with file watching)
+pnpm dev
+```
+
+## What This Example Does
+
+- Initializes Composio SDK
+- Fetches available tools
+- Demonstrates basic usage patterns
+
+## Customization
+
+Edit `src/index.ts` to:
+- Add specific apps you want to integrate with
+- Implement your business logic
+- Add error handling and logging
+
+## Related Examples
+
+- [OpenAI Example](../openai) - Shows integration with OpenAI
+- [LangChain Example](../langchain) - Shows integration with LangChain
+- [More Examples](../) - Browse all available examples
+
+## Support
+
+- [Documentation](https://docs.composio.dev)
+- [Discord Community](https://discord.gg/composio)
+- [GitHub Issues](https://github.com/composio/composio/issues)
diff --git a/ts/examples/tools/package.json b/ts/examples/tools/package.json
new file mode 100644
index 000000000..ad11dcaf5
--- /dev/null
+++ b/ts/examples/tools/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "tools-example",
+ "private": true,
+ "version": "0.1.0",
+ "description": "Example project for Tools",
+ "main": "src/index.ts",
+ "scripts": {
+ "start": "bun src/index.ts",
+ "dev": "bun --watch src/index.ts",
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "keywords": ["composio", "example", "tools"],
+ "author": "",
+ "license": "ISC",
+ "packageManager": "[email protected]",
+ "dependencies": {
+ "@composio/core": "workspace:*",
+ "dotenv": "^16.4.1"
+ },
+ "devDependencies": {
+ "@types/bun": "^1.2.9",
+ "typescript": "^5.8.3"
+ }
+}
diff --git a/ts/examples/tools/src/image.png b/ts/examples/tools/src/image.png
new file mode 100644
index 000000000..301f2a3dd
Binary files /dev/null and b/ts/examples/tools/src/image.png differ
diff --git a/ts/examples/tools/src/index.ts b/ts/examples/tools/src/index.ts
new file mode 100644
index 000000000..1a4bcf02c
--- /dev/null
+++ b/ts/examples/tools/src/index.ts
@@ -0,0 +1,51 @@
+/**
+ * Tools Example
+ *
+ * This example demonstrates how to use Composio SDK for tools.
+ *
+ * Prerequisites:
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
+ * 3. Run the example: pnpm start
+ */
+
+import { Composio } from '@composio/core';
+import 'dotenv/config';
+import path from 'path';
+
+/**
+ * Initialize Composio
+ */
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+ allowTracking: false,
+ allowTracing: false,
+});
+
+/**
+ * Main function to run the example
+ */
+async function main() {
+ try {
+ console.log('🚀 Starting Tools Example...');
+
+ const tools = await composio.tools.getRawComposioTools({
+ tools: ['GOOGLEDRIVE_UPLOAD_FILE'],
+ });
+
+ console.log(JSON.stringify(tools, null, 2));
+
+ const result = await composio.tools.execute('GOOGLEDRIVE_UPLOAD_FILE', {
+ arguments: {
+ file_to_upload: path.join(__dirname, 'image.png'),
+ },
+ userId: 'default',
+ });
+
+ console.log(JSON.stringify(result, null, 2));
+ } catch (error) {
+ console.error('❌ Error running example:', error);
+ }
+}
+
+// Run the example
+main().catch(console.error);
diff --git a/ts/examples/tools/tsconfig.json b/ts/examples/tools/tsconfig.json
new file mode 100644
index 000000000..158c157c8
--- /dev/null
+++ b/ts/examples/tools/tsconfig.json
@@ -0,0 +1,16 @@
+{
+ "compilerOptions": {
+ "target": "ESNext",
+ "module": "ESNext",
+ "declaration": true,
+ "declarationDir": "./dist",
+ "outDir": "./dist",
+ "rootDir": "./src",
+ "strict": true,
+ "esModuleInterop": true,
+ "moduleResolution": "node",
+ "skipLibCheck": true,
+ "resolveJsonModule": true
+ },
+ "include": ["src"]
+}
diff --git a/ts/packages/core/package.json b/ts/packages/core/package.json
index 8e45338bb..a2992bfa0 100644
--- a/ts/packages/core/package.json
+++ b/ts/packages/core/package.json
@@ -39,6 +39,7 @@
"dependencies": {
"@composio/client": "0.1.0-alpha.24",
"@composio/json-schema-to-zod": "0.1.9",
+ "@types/json-schema": "^7.0.15",
"chalk": "^5.4.1",
"openai": "^4.94.0",
"pusher-js": "^8.4.0",
diff --git a/ts/packages/core/src/errors/FileModifierErrors.ts b/ts/packages/core/src/errors/FileModifierErrors.ts
new file mode 100644
index 000000000..59642e0d4
--- /dev/null
+++ b/ts/packages/core/src/errors/FileModifierErrors.ts
@@ -0,0 +1,19 @@
+import { ComposioError, ComposioErrorOptions } from './ComposioError';
+
+export const FileModifierErrorCodes = {
+ FILE_UPLOAD_FAILED: 'FILE_UPLOAD_FAILED',
+} as const;
+
+export class ComposioFileUploadError extends ComposioError {
+ constructor(
+ message: string = 'Failed to upload file',
+ options: Omit<ComposioErrorOptions, 'code'> = {}
+ ) {
+ super(message, {
+ ...options,
+ code: FileModifierErrorCodes.FILE_UPLOAD_FAILED,
+ possibleFixes: options.possibleFixes || ['Check if the file exists in the location provided'],
+ });
+ this.name = 'ComposioFileUploadError';
+ }
+}
diff --git a/ts/packages/core/src/models/Tools.ts b/ts/packages/core/src/models/Tools.ts
index 74d9948bf..5e9a6eb2f 100644
--- a/ts/packages/core/src/models/Tools.ts
+++ b/ts/packages/core/src/models/Tools.ts
@@ -25,6 +25,8 @@ import {
import { CustomTools } from './CustomTools';
import { CustomToolInputParameter, CustomToolOptions } from '../types/customTool.types';
import {
+ afterExecuteModifier,
+ beforeExecuteModifier,
ExecuteToolModifiers,
ProviderOptions,
TransformToolSchemaModifier,
@@ -41,6 +43,7 @@ import {
} from '../errors/ToolErrors';
import { ValidationError } from '../errors/ValidationErrors';
import { telemetry } from '../telemetry/Telemetry';
+import { FileToolModifier } from '../utils/modifiers/FileToolModifier';
/**
* This class is used to manage tools in the Composio SDK.
* It provides methods to list, get, and execute tools.
@@ -218,6 +221,96 @@ export class Tools<
return null;
}
+ /**
+ * Applies the default schema modifiers to the tools
+ * @param tools - The tools to apply the default schema modifiers to
+ * @returns The tools with the default schema modifiers applied
+ */
+ private async applyDefaultSchemaModifiers(tools: Tool[]): Promise<Tool[]> {
+ const fileToolModifier = new FileToolModifier(this.client);
+ return await Promise.all(
+ tools.map(tool =>
+ fileToolModifier.modifyToolSchema(tool.slug, tool.toolkit?.slug ?? 'unknown', tool)
+ )
+ );
+ }
+
+ /**
+ * Applies the before execute modifiers to the tool execution params
+ * @param options.toolSlug - The slug of the tool
+ * @param options.toolkitSlug - The slug of the toolkit
+ * @param options.params - The params of the tool execution
+ * @param modifier - The modifier to apply
+ * @returns The modified params
+ */
+ private async applyBeforeExecuteModifiers(
+ tool: Tool,
+ {
+ toolSlug,
+ toolkitSlug,
+ params,
+ }: {
+ toolSlug: string;
+ toolkitSlug: string;
+ params: ToolExecuteParams;
+ },
+ modifier?: beforeExecuteModifier
+ ): Promise<ToolExecuteParams> {
+ const fileToolModifier = new FileToolModifier(this.client);
+ let modifiedParams = params;
+ if (modifier) {
+ if (typeof modifier === 'function') {
+ modifiedParams = await modifier(toolSlug, toolkitSlug, modifiedParams);
+ } else {
+ throw new ComposioInvalidModifierError('Invalid beforeExecute modifier. Not a function.');
+ }
+ }
+ modifiedParams = await fileToolModifier.fileUploadModifier(tool, {
+ toolSlug,
+ toolkitSlug,
+ params: modifiedParams,
+ });
+ return modifiedParams;
+ }
+
+ /**
+ * Applies the after execute modifiers to the tool execution result
+ * @param options.toolSlug - The slug of the tool
+ * @param options.toolkitSlug - The slug of the toolkit
+ * @param options.result - The result of the tool execution
+ * @param modifier - The modifier to apply
+ * @returns The modified result
+ */
+ private async applyAfterExecuteModifiers(
+ tool: Tool,
+ {
+ toolSlug,
+ toolkitSlug,
+ result,
+ }: {
+ toolSlug: string;
+ toolkitSlug: string;
+ result: ToolExecuteResponse;
+ },
+ modifier?: afterExecuteModifier
+ ): Promise<ToolExecuteResponse> {
+ const fileToolModifier = new FileToolModifier(this.client);
+ let modifiedResult = result;
+ if (modifier) {
+ if (typeof modifier === 'function') {
+ modifiedResult = await modifier(toolSlug, toolkitSlug, modifiedResult);
+ } else {
+ throw new ComposioInvalidModifierError('Invalid afterExecute modifier. Not a function.');
+ }
+ }
+ modifiedResult = await fileToolModifier.fileDownloadModifier(tool, {
+ toolSlug,
+ toolkitSlug,
+ result: modifiedResult,
+ });
+ return modifiedResult;
+ }
+
/**
* Lists all tools available in the Composio SDK including custom tools.
*
@@ -307,7 +400,10 @@ export class Tools<
toolSlugs: 'tools' in queryParams.data ? queryParams.data.tools : undefined,
});
- let modifiedTools = [...caseTransformedTools, ...customTools];
+ let modifiedTools = await this.applyDefaultSchemaModifiers([
+ ...caseTransformedTools,
+ ...customTools,
+ ]);
// apply local modifiers if they are provided
if (modifier) {
@@ -357,8 +453,8 @@ export class Tools<
});
}
- // change the case of the tool to camel case
- let modifiedTool = this.transformToolCases(tool);
+ // change the case of the tool to camel case and apply default modifiers
+ let [modifiedTool] = await this.applyDefaultSchemaModifiers([this.transformToolCases(tool)]);
// apply local modifiers if they are provided
if (modifier) {
if (typeof modifier === 'function') {
@@ -538,13 +634,17 @@ export class Tools<
body: ToolExecuteParams,
modifiers?: ExecuteToolModifiers
): Promise<ToolExecuteResponse> {
- if (modifiers?.beforeExecute) {
- if (typeof modifiers.beforeExecute === 'function') {
- body = await modifiers.beforeExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', body);
- } else {
- throw new ComposioInvalidModifierError('Invalid beforeExecute modifier. Not a function.');
- }
- }
+ // apply before execute modifiers
+ body = await this.applyBeforeExecuteModifiers(
+ tool,
+ {
+ toolSlug: tool.slug,
+ toolkitSlug: tool.toolkit?.slug ?? 'unknown',
+ params: body,
+ },
+ modifiers?.beforeExecute
+ );
+
// fetch connected accounts if doesn't exist
let connectedAccountId = body.connectedAccountId;
if (!connectedAccountId) {
@@ -564,14 +664,15 @@ export class Tools<
// apply transformations to the response
result = this.transformToolExecuteResponse(result);
- // apply local modifiers if they are provided
- if (modifiers?.afterExecute) {
- if (typeof modifiers.afterExecute === 'function') {
- result = await modifiers.afterExecute(tool.slug, tool.toolkit?.slug ?? 'unknown', result);
- } else {
- throw new ComposioInvalidModifierError('Invalid afterExecute modifier. Not a function.');
- }
- }
+ result = await this.applyAfterExecuteModifiers(
+ tool,
+ {
+ toolSlug: tool.slug,
+ toolkitSlug: tool.toolkit?.slug ?? 'unknown',
+ result,
+ },
+ modifiers?.afterExecute
+ );
return result;
}
diff --git a/ts/packages/core/src/types/tool.types.ts b/ts/packages/core/src/types/tool.types.ts
index cd7162990..eb69ba349 100644
--- a/ts/packages/core/src/types/tool.types.ts
+++ b/ts/packages/core/src/types/tool.types.ts
@@ -25,19 +25,24 @@ const JSONSchemaType = z.enum([
]);
// JSON Schema property definition
-const JSONSchemaProperty: z.ZodType<unknown> = z.object({
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const JSONSchemaPropertySchema: z.ZodType<any> = z.object({
type: z.union([JSONSchemaType, z.array(JSONSchemaType)]).optional(),
description: z.string().optional(),
- anyOf: z.lazy(() => z.array(JSONSchemaProperty)).optional(),
- oneOf: z.lazy(() => z.array(JSONSchemaProperty)).optional(),
- allOf: z.lazy(() => z.array(JSONSchemaProperty)).optional(),
- not: z.lazy(() => JSONSchemaProperty).optional(),
+ anyOf: z.lazy(() => z.array(JSONSchemaPropertySchema)).optional(),
+ oneOf: z.lazy(() => z.array(JSONSchemaPropertySchema)).optional(),
+ allOf: z.lazy(() => z.array(JSONSchemaPropertySchema)).optional(),
+ not: z.lazy(() => JSONSchemaPropertySchema).optional(),
title: z.string().optional(),
default: z.any().optional(),
nullable: z.boolean().optional(),
- properties: z.lazy(() => z.record(z.string(), JSONSchemaProperty)).optional(),
+ properties: z.lazy(() => z.record(z.string(), JSONSchemaPropertySchema)).optional(),
required: z.array(z.string()).optional(),
- items: z.lazy(() => z.union([JSONSchemaProperty, z.array(JSONSchemaProperty)])).optional(),
+ file_uploadable: z.boolean().optional(),
+ file_downloadable: z.boolean().optional(),
+ items: z
+ .lazy(() => z.union([JSONSchemaPropertySchema, z.array(JSONSchemaPropertySchema)]))
+ .optional(),
enum: z.array(z.any()).optional(),
const: z.any().optional(),
minimum: z.number().optional(),
@@ -54,37 +59,38 @@ const JSONSchemaProperty: z.ZodType<unknown> = z.object({
uniqueItems: z.boolean().optional(),
minProperties: z.number().optional(),
maxProperties: z.number().optional(),
- patternProperties: z.lazy(() => z.record(z.string(), JSONSchemaProperty)).optional(),
- additionalProperties: z.union([z.boolean(), z.lazy(() => JSONSchemaProperty)]).optional(),
+ patternProperties: z.lazy(() => z.record(z.string(), JSONSchemaPropertySchema)).optional(),
+ additionalProperties: z.union([z.boolean(), z.lazy(() => JSONSchemaPropertySchema)]).optional(),
examples: z.array(z.any()).optional(),
readOnly: z.boolean().optional(),
writeOnly: z.boolean().optional(),
- if: z.lazy(() => JSONSchemaProperty).optional(),
- then: z.lazy(() => JSONSchemaProperty).optional(),
- else: z.lazy(() => JSONSchemaProperty).optional(),
+ if: z.lazy(() => JSONSchemaPropertySchema).optional(),
+ then: z.lazy(() => JSONSchemaPropertySchema).optional(),
+ else: z.lazy(() => JSONSchemaPropertySchema).optional(),
$ref: z.string().optional(),
definitions: z
.record(
z.string(),
- z.lazy(() => JSONSchemaProperty)
+ z.lazy(() => JSONSchemaPropertySchema)
)
.optional(),
$defs: z
.record(
z.string(),
- z.lazy(() => JSONSchemaProperty)
+ z.lazy(() => JSONSchemaPropertySchema)
)
.optional(),
});
+export type JSONSchemaProperty = z.infer<typeof JSONSchemaPropertySchema>;
// Schema for parameters (input/output)
const ParametersSchema = z.object({
type: z.literal('object'),
- anyOf: z.array(JSONSchemaProperty).optional(),
- oneOf: z.array(JSONSchemaProperty).optional(),
- allOf: z.array(JSONSchemaProperty).optional(),
- not: JSONSchemaProperty.optional(),
- properties: z.record(z.string(), JSONSchemaProperty),
+ anyOf: z.array(JSONSchemaPropertySchema).optional(),
+ oneOf: z.array(JSONSchemaPropertySchema).optional(),
+ allOf: z.array(JSONSchemaPropertySchema).optional(),
+ not: JSONSchemaPropertySchema.optional(),
+ properties: z.record(z.string(), JSONSchemaPropertySchema),
required: z.array(z.string()).optional(),
title: z.string().optional(),
default: z.any().optional(),
diff --git a/ts/packages/core/src/utils/fileUtils.ts b/ts/packages/core/src/utils/fileUtils.ts
new file mode 100644
index 000000000..9ef8102eb
--- /dev/null
+++ b/ts/packages/core/src/utils/fileUtils.ts
@@ -0,0 +1,209 @@
+import crypto from 'crypto';
+import pathModule from 'path';
+import ComposioClient from '@composio/client';
+import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
+import logger from './logger';
+
+const readFileContent = async (path: string): Promise<{ content: string; mimeType: string }> => {
+ try {
+ const content = require('fs').readFileSync(path);
+ return {
+ content: content.toString('base64'),
+ mimeType: 'application/octet-stream',
+ };
+ } catch (error) {
+ throw new Error(`Error reading file at ${path}: ${error}`);
+ }
+};
+
+const readFileContentFromURL = async (
+ path: string
+): Promise<{ content: string; mimeType: string }> => {
+ const response = await fetch(path);
+ if (!response.ok) {
+ throw new Error(`Failed to fetch file: ${response.statusText}`);
+ }
+ const arrayBuffer = await response.arrayBuffer();
+ const content = Buffer.from(arrayBuffer);
+ const mimeType = response.headers.get('content-type') || 'application/octet-stream';
+ return {
+ content: content.toString('base64'),
+ mimeType,
+ };
+};
+
+const uploadFileToS3 = async (
+ fileName: string,
+ content: string,
+ toolSlug: string,
+ toolkitSlug: string,
+ mimeType: string,
+ client: ComposioClient
+): Promise<string> => {
+ const response = await client.files.createPresignedURL({
+ filename: fileName,
+ mimetype: mimeType,
+ md5: crypto.createHash('md5').update(Buffer.from(content, 'base64')).digest('hex'),
+ tool_slug: toolSlug,
+ toolkit_slug: toolkitSlug,
+ });
+
+ const { key, type } = response;
+
+ if (type === 'new' || type === 'update') {
+ logger.debug(`Uploading ${key} file to S3: ${key}`);
+ const buffer = Buffer.from(content, 'base64');
+ const signedURL =
+ response.type === 'update' ? response.update_presigned_url : response.new_presigned_url;
+
+ // Upload the file using the presigned URL
+ const uploadResponse = await fetch(signedURL, {
+ method: 'PUT',
+ body: buffer,
+ headers: {
+ 'Content-Type': mimeType,
+ 'Content-Length': buffer.length.toString(),
+ },
+ });
+
+ if (!uploadResponse.ok) {
+ throw new Error(`Failed to upload file to S3: ${uploadResponse.statusText}`);
+ }
+ } else {
+ logger.debug(`File already exists in S3: ${key}`);
+ }
+
+ return key;
+};
+
+export const getFileDataAfterUploadingToS3 = async (
+ path: string,
+ toolSlug: string,
+ toolkitSlug: string,
+ client: ComposioClient
+): Promise<{
+ name: string;
+ mimetype: string;
+ s3key: string;
+}> => {
+ const isURL = path.startsWith('http');
+ const fileData = isURL ? await readFileContentFromURL(path) : await readFileContent(path);
+ logger.debug(`Uploading file to S3: ${path}`);
+ const s3key = await uploadFileToS3(
+ pathModule.basename(path) || `${toolSlug}_${Date.now()}`,
+ fileData.content,
+ toolSlug,
+ toolkitSlug,
+ fileData.mimeType,
+ client
+ );
+
+ logger.debug(`Done! File uploaded to S3: ${s3key}`, JSON.stringify(fileData, null, 2));
+ return {
+ name:
+ pathModule.basename(path) || `${toolSlug}_${Date.now()}.${fileData.mimeType.split('/')[1]}`,
+ mimetype: fileData.mimeType,
+ s3key: s3key,
+ };
+};
+
+export const downloadFileFromS3 = async ({
+ toolSlug,
+ s3Url,
+ mimeType,
+}: {
+ toolSlug: string;
+ s3Url: string;
+ mimeType: string;
+}) => {
+ const response = await fetch(s3Url);
+ if (!response.ok) {
+ throw new Error(`Failed to download file: ${response.statusText}`);
+ }
+ const data = await response.arrayBuffer();
+
+ const extension = mimeType.split('/')[1] || 'txt';
+ const fileName = `${toolSlug}_${Date.now()}.${extension}`;
+ const filePath = saveFile(fileName, Buffer.from(data), true);
+ return {
+ name: fileName,
+ mimeType: mimeType,
+ s3Key: s3Url,
+ filePath: filePath,
+ };
+};
+
+/**
+ * Gets the Composio directory.
+ * @param createDirIfNotExists - Whether to create the directory if it doesn't exist.
+ * @returns The path to the Composio directory.
+ */
+export const getComposioDir = (createDirIfNotExists: boolean = false) => {
+ try {
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const os = require('os');
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const path = require('path');
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const fs = require('fs');
+ const composioDir = path.join(os.homedir(), COMPOSIO_DIR);
+ if (createDirIfNotExists && !fs.existsSync(composioDir)) {
+ fs.mkdirSync(composioDir, { recursive: true });
+ }
+ return composioDir;
+ } catch (_error) {
+ return null;
+ }
+};
+
+/**
+ * Gets the Composio temporary files directory.
+ * @param createDirIfNotExists - Whether to create the directory if it doesn't exist.
+ * @returns The path to the Composio temporary files directory.
+ */
+export const getComposioTempFilesDir = (createDirIfNotExists: boolean = false) => {
+ try {
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const os = require('os');
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const path = require('path');
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const fs = require('fs');
+ const composioFilesDir = path.join(os.homedir(), COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME);
+ if (createDirIfNotExists && !fs.existsSync(composioFilesDir)) {
+ fs.mkdirSync(composioFilesDir, { recursive: true });
+ }
+ return composioFilesDir;
+ } catch (_error) {
+ return null;
+ }
+};
+
+/**
+ * Saves a file to the Composio directory.
+ * @param file - The name of the file to save.
+ * @param content - The content of the file to save. Can be a string or Buffer.
+ * @param isTempFile - Whether the file is a temporary file.
+ * @returns The path to the saved file.
+ */
+export const saveFile = (file: string, content: string | Buffer, isTempFile: boolean = false) => {
+ try {
+ const path = require('path');
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
+ const fs = require('fs');
+ const composioFilesDir = isTempFile ? getComposioTempFilesDir(true) : getComposioDir(true);
+ const filePath = path.join(composioFilesDir, path.basename(file));
+
+ logger.info(`Saving file to: ${filePath}`);
+
+ if (Buffer.isBuffer(content)) {
+ fs.writeFileSync(filePath, content);
+ } else {
+ fs.writeFileSync(filePath, content, 'utf8');
+ }
+
+ return filePath;
+ } catch (_error) {
+ return null;
+ }
+};
diff --git a/ts/packages/core/src/utils/modifiers/FileToolModifier.ts b/ts/packages/core/src/utils/modifiers/FileToolModifier.ts
new file mode 100644
index 000000000..77f1c6711
--- /dev/null
+++ b/ts/packages/core/src/utils/modifiers/FileToolModifier.ts
@@ -0,0 +1,168 @@
+import {
+ JSONSchemaProperty,
+ Tool,
+ ToolExecuteParams,
+ ToolExecuteResponse,
+} from '../../types/tool.types';
+import ComposioClient from '@composio/client';
+import logger from '../logger';
+import { ComposioFileUploadError } from '../../errors/FileModifierErrors';
+import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils';
+
+/**
+ * Transforms the properties of the tool schema to include the file upload URL.
+ *
+ * Attaches the format: 'path' to the properties that are file uploadable for agents.
+ *
+ * @param properties - The properties of the tool schema.
+ * @returns The transformed properties.
+ */
+const transformProperties = (properties: JSONSchemaProperty): JSONSchemaProperty => {
+ const newProperties: JSONSchemaProperty = {};
+
+ for (const [key, property] of Object.entries(properties) as [string, JSONSchemaProperty][]) {
+ if (!property.file_uploadable) {
+ newProperties[key] = property;
+ continue;
+ }
+ newProperties[key] = {
+ ...property,
+ format: 'path',
+ };
+ }
+
+ return properties;
+};
+
+export class FileToolModifier {
+ private client: ComposioClient;
+
+ constructor(client: ComposioClient) {
+ this.client = client;
+ }
+
+ /**
+ * Modifies the tool schema to include the file upload URL.
+ *
+ * @description This modifier is used to upload a file to the Composio platform and replace the file path with the file upload URL.
+ *
+ * @param _toolSlug - The slug of the tool that is being executed.
+ * @param _toolkitSlug - The slug of the toolkit that is being executed.
+ * @param schema - The schema of the tool.
+ * @returns The schema with the file upload URL included.
+ */
+ async modifyToolSchema(toolSlug: string, toolkitSlug: string, schema: Tool): Promise<Tool> {
+ if (!schema.inputParameters?.properties) {
+ return schema;
+ }
+
+ const properties = transformProperties(schema.inputParameters.properties);
+
+ return {
+ ...schema,
+ inputParameters: {
+ ...schema.inputParameters,
+ properties,
+ },
+ };
+ }
+
+ /**
+ * Modifies the input parameters to include the file upload URL.
+ *
+ * @description This modifier is used to upload a file to the Composio platform and replace the file path with the file upload URL.
+ *
+ * @param toolSlug - The slug of the tool that is being executed.
+ * @param toolkitSlug - The slug of the toolkit that is being executed.
+ *
+ */
+ async fileUploadModifier(
+ tool: Tool,
+ options: { toolSlug: string; toolkitSlug: string; params: ToolExecuteParams }
+ ): Promise<ToolExecuteParams> {
+ const { arguments: args } = options.params;
+ if (!args) {
+ return options.params;
+ }
+ // Check if the arguments is an object
+ for (const key of Object.keys(args)) {
+ // Check if the key ends with _schema_parsed_file and is a string
+ const toolProperty = tool.inputParameters?.properties?.[key];
+
+ if (toolProperty?.file_uploadable) {
+ logger.debug(`Processing file upload for: ${key}`);
+ try {
+ const fileData = await getFileDataAfterUploadingToS3(
+ args[key] as string,
+ options.toolSlug,
+ options.toolkitSlug ?? 'unknown',
+ this.client
+ );
+ args[key] = fileData;
+ } catch (error) {
+ throw new ComposioFileUploadError(`Failed to upload file: ${key}`, {
+ cause: error,
+ });
+ }
+ }
+ }
+ return options.params;
+ }
+
+ /**
+ * Modifies the result to include the file download URL.
+ *
+ * @description This modifier is used to download a file and
+ *
+ * @param toolSlug - The slug of the tool that is being executed.
+ * @param toolkitSlug - The slug of the toolkit that is being executed.
+ * @param result - The result of the tool execution.
+ * @returns The result with the file download URL included.
+ */
+ async fileDownloadModifier(
+ tool: Tool,
+ options: {
+ toolSlug: string;
+ toolkitSlug: string;
+ result: ToolExecuteResponse;
+ }
+ ): Promise<ToolExecuteResponse> {
+ console.log(JSON.stringify(options.result, null, 2));
+ console.log(JSON.stringify(tool.outputParameters, null, 2));
+ for (const [key, value] of Object.entries(options.result?.data)) {
+ const fileData = value as { s3url?: string; mimetype?: string };
+
+ if (!fileData?.s3url) continue;
+
+ logger.debug(`Downloading file from S3: ${fileData.s3url}`);
+
+ try {
+ const downloadedFile = await downloadFileFromS3({
+ toolSlug: options.toolSlug,
+ s3Url: fileData.s3url,
+ mimeType: fileData.mimetype || 'application/txt',
+ });
+
+ logger.debug(`Downloaded file from S3: ${downloadedFile.filePath}`);
+
+ options.result.data[key] = {
+ uri: downloadedFile.filePath,
+ file_downloaded: true,
+ s3url: fileData.s3url,
+ mimeType: downloadedFile.mimeType,
+ };
+ } catch (error) {
+ logger.error(`Failed to download file from S3: ${fileData.s3url}`, {
+ cause: error,
+ });
+ options.result.data[key] = {
+ uri: '',
+ file_downloaded: false,
+ s3url: fileData.s3url,
+ mimeType: fileData.mimetype || 'application/txt',
+ };
+ }
+ }
+ return options.result;
+ }
+}
diff --git a/ts/scripts/create-example.sh b/ts/scripts/create-example.sh
index 44ab301eb..6389792d9 100755
--- a/ts/scripts/create-example.sh
+++ b/ts/scripts/create-example.sh
@@ -8,7 +8,7 @@ if [ -z "$1" ]; then
fi
EXAMPLE_NAME=$1
-EXAMPLE_PATH="examples/$EXAMPLE_NAME"
+EXAMPLE_PATH="ts/examples/$EXAMPLE_NAME"
CAPITAL_EXAMPLE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${EXAMPLE_NAME:0:1})${EXAMPLE_NAME:1}"
# Check if example directory already exists
diff --git a/ts/scripts/create-provider.sh b/ts/scripts/create-provider.sh
index 8a1d164a2..e90fc7209 100644
--- a/ts/scripts/create-provider.sh
+++ b/ts/scripts/create-provider.sh
@@ -8,7 +8,7 @@ if [ -z "$1" ]; then
fi
TOOLSET_NAME=$1
-TOOLSET_PATH="packages/providers/$TOOLSET_NAME"
+TOOLSET_PATH="ts/packages/providers/$TOOLSET_NAME"
CAPITAL_TOOLSET_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${TOOLSET_NAME:0:1})${TOOLSET_NAME:1}"
# Check if provider should be agentic
diff --git a/ts/scripts/update-peer-deps.ts b/ts/scripts/update-peer-deps.ts
index 9758afbcb..233bc9a59 100644
--- a/ts/scripts/update-peer-deps.ts
+++ b/ts/scripts/update-peer-deps.ts
@@ -6,7 +6,7 @@
import fs from 'fs';
import path from 'path';
-const corePkgPackageJson = path.resolve(__dirname, '../packages/core', 'package.json');
+const corePkgPackageJson = path.resolve(__dirname, '../ts/packages/core', 'package.json');
const corePkg = JSON.parse(fs.readFileSync(corePkgPackageJson, 'utf-8'));
const coreVersion = corePkg.version;