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.

Add telemetry support for sdk (#1633)

Musthaq Ahamadd1a958526e0d
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 45b248ce5..8363baa7f 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -32,4 +32,4 @@ jobs:
run: pnpm lint
- name: Run Build
- run: pnpm run build
+ run: pnpm run build:packages
diff --git a/apps/telemetry-proxy/.gitignore b/apps/telemetry-proxy/.gitignore
deleted file mode 100644
index 1d00873fe..000000000
--- a/apps/telemetry-proxy/.gitignore
+++ /dev/null
@@ -1,39 +0,0 @@
-# prod
-dist/
-
-# dev
-.yarn/
-!.yarn/releases
-.vscode/*
-!.vscode/launch.json
-!.vscode/*.code-snippets
-.idea/workspace.xml
-.idea/usage.statistics.xml
-.idea/shelf
-
-# deps
-node_modules/
-.wrangler
-
-# env
-.env
-.env.production
-.dev.vars
-
-# logs
-logs/
-*.log
-npm-debug.log*
-yarn-debug.log*
-yarn-error.log*
-pnpm-debug.log*
-lerna-debug.log*
-
-# misc
-.DS_Store
-
-
-# env
-.dev.vars
-.env
-.wrangler
\ No newline at end of file
diff --git a/apps/telemetry-proxy/README.md b/apps/telemetry-proxy/README.md
deleted file mode 100644
index eba2b1e47..000000000
--- a/apps/telemetry-proxy/README.md
+++ /dev/null
@@ -1,21 +0,0 @@
-```txt
-npm install
-npm run dev
-```
-
-```txt
-npm run deploy
-```
-
-[For generating/synchronizing types based on your Worker configuration run](https://developers.cloudflare.com/workers/wrangler/commands/#types):
-
-```txt
-npm run cf-typegen
-```
-
-Pass the `CloudflareBindings` as generics when instantiation `Hono`:
-
-```ts
-// src/index.ts
-const app = new Hono<{ Bindings: CloudflareBindings }>()
-```
diff --git a/apps/telemetry-proxy/package.json b/apps/telemetry-proxy/package.json
deleted file mode 100644
index 4adf2c4b9..000000000
--- a/apps/telemetry-proxy/package.json
+++ /dev/null
@@ -1,19 +0,0 @@
-{
- "name": "@composio/telemetry-proxy",
- "version": "0.0.1",
- "private": true,
- "type": "module",
- "scripts": {
- "dev": "wrangler dev",
- "deploy": "wrangler deploy --minify",
- "cf-typegen": "wrangler types --env-interface CloudflareBindings"
- },
- "dependencies": {
- "@hono/zod-openapi": "^0.19.8",
- "hono": "^4.7.11",
- "zod": "^3.24.2"
- },
- "devDependencies": {
- "wrangler": "^4.4.0"
- }
-}
diff --git a/apps/telemetry-proxy/src/index.ts b/apps/telemetry-proxy/src/index.ts
deleted file mode 100644
index 4e8fae413..000000000
--- a/apps/telemetry-proxy/src/index.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import telemetry from './routes/v1/telemetry';
-import { AppBindings } from './types/app.types';
-import { OpenAPIHono } from '@hono/zod-openapi';
-
-const app = new OpenAPIHono<{ Bindings: AppBindings }>();
-app.route('/v1', telemetry);
-app.doc('/docs', {
- openapi: '3.0.0',
- info: {
- title: 'Composio Telemetry API',
- version: '1.0.0',
- },
-});
-
-export default app;
diff --git a/apps/telemetry-proxy/src/lib/errors.ts b/apps/telemetry-proxy/src/lib/errors.ts
deleted file mode 100644
index 623379231..000000000
--- a/apps/telemetry-proxy/src/lib/errors.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { ErrorLogPayload, ErrorLogPayloadSchema } from '../types/errors.types';
-
-const getLogTags = (errorData: ErrorLogPayload) => {
- const { source, metadata } = errorData;
- return [
- ...(source?.name ? [`source:${source.name}`] : []),
- ...(source?.service ? [`service:${source.service}`] : []),
- ...(source?.language ? [`language:${source.language}`] : []),
- ...(source?.version ? [`version:${source.version}`] : []),
- ...(source?.platform ? [`platform:${source.platform}`] : []),
- ...(metadata?.projectId ? [`project_id:${metadata.projectId}`] : []),
- ...(metadata?.provider ? [`provider:${metadata.provider}`] : []),
- 'status:error',
- 'level:error',
- ]
- .filter(Boolean)
- .join(',');
-};
-
-const getLogHost = (errorData: ErrorLogPayload) => {
- const { source } = errorData;
- return `composio-${source?.service}-${source?.language}`;
-};
-
-export const createErrorLogPayload = (errorData: ErrorLogPayload) => {
- const logBody = {
- message: errorData.message,
- ddtags: getLogTags(errorData),
- hostname: getLogHost(errorData),
- service: getLogHost(errorData),
- };
- return logBody;
-};
-
-export async function sendErrorLogToDatadog(errorData: ErrorLogPayload, datadogApiKey: string) {
- const parsedPayload = ErrorLogPayloadSchema.safeParse(errorData);
- if (parsedPayload.error) {
- throw parsedPayload.error;
- }
-
- const logBody = createErrorLogPayload(parsedPayload.data);
-
- const res = await fetch('https://api.datadoghq.com/api/v1/series', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'DD-API-KEY': datadogApiKey,
- },
- body: JSON.stringify(logBody),
- });
-
- if (!res.ok) {
- console.error('Failed to send error log to Datadog:', await res.text());
- }
-
- return res.ok;
-}
diff --git a/apps/telemetry-proxy/src/lib/metrics.ts b/apps/telemetry-proxy/src/lib/metrics.ts
deleted file mode 100644
index c7ea0dc2a..000000000
--- a/apps/telemetry-proxy/src/lib/metrics.ts
+++ /dev/null
@@ -1,105 +0,0 @@
-import {
- DDMetricPayload,
- DDMetricPayloadSchema,
- TelemetryMetricPayload,
- TelemetryMetricPayloadSchema,
-} from '../types/metrics.types';
-
-const getMetricName = (
- type: 'function_invocation' | 'function_duration',
- payload: TelemetryMetricPayload
-) => {
- const { functionName, source } = payload;
- if (source?.service === 'sdk') {
- return `composio.${source.service}.${type}`;
- }
- return `composio.sdk.${type}`;
-};
-
-const getMetricTags = (payload: TelemetryMetricPayload) => {
- const { functionName, source, metadata, error } = payload;
- return [
- `function:${functionName}`,
- ...(source?.name ? [`source:${source.name}`] : []),
- ...(source?.service ? [`service:${source.service}`] : []),
- ...(source?.language ? [`language:${source.language}`] : []),
- ...(source?.version ? [`version:${source.version}`] : []),
- ...(source?.platform ? [`platform:${source.platform}`] : []),
- ...(metadata?.projectId ? [`project_id:${metadata.projectId}`] : []),
- ...(metadata?.provider ? [`provider:${metadata.provider}`] : []),
- ...(error ? ['status:error'] : ['status:success']),
- ];
-};
-
-const getMetricHost = (payload: TelemetryMetricPayload) => {
- const { source } = payload;
- return `composio-${source?.service}-${source?.language}`;
-};
-
-const createDDMetricPayload = (
- type: 'count' | 'gauge',
- payload: TelemetryMetricPayload
-): DDMetricPayload => {
- const { durationMs, timestamp } = payload;
- const executionTimestamp = timestamp ?? Math.floor(Date.now() / 1000);
- const executionDuration = durationMs ?? 1;
-
- const parsedPayload = DDMetricPayloadSchema.safeParse({
- host: getMetricHost(payload),
- metric: getMetricName('function_invocation', payload),
- points: [[executionTimestamp, executionDuration]],
- tags: getMetricTags(payload),
- type,
- });
-
- if (parsedPayload.error) {
- throw parsedPayload.error;
- }
-
- return parsedPayload.data;
-};
-
-/**
- * Creates a metric payload for Datadog.
- *
- * @description This function creates a metric payload for Datadog. It returns an array of two
- * objects, one for the count metric and one for the duration metric.
- *
- * @param payload - The payload to create a metric for.
- * @returns An array of two objects, one for the count metric and one for the duration metric.
- */
-export const createMetricPayload = (payload: TelemetryMetricPayload): Array<DDMetricPayload> => {
- const parsedPayload = TelemetryMetricPayloadSchema.safeParse(payload);
- if (parsedPayload.error) {
- throw parsedPayload.error;
- }
-
- const DDMetricCountPayload = createDDMetricPayload('count', payload);
- const DDMetricDurationPayload = createDDMetricPayload('gauge', payload);
-
- return [DDMetricCountPayload, DDMetricDurationPayload];
-};
-
-export const sendMetricToDatadog = async (
- payload: Array<TelemetryMetricPayload>,
- datadogApiKey: string
-) => {
- const parsedPayload = payload.map(item => createMetricPayload(item)).flat();
- console.log(JSON.stringify(parsedPayload, null, 2));
- const res = await fetch('https://api.datadoghq.com/api/v1/series', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'DD-API-KEY': datadogApiKey,
- },
- body: JSON.stringify({
- series: parsedPayload,
- }),
- });
-
- if (!res.ok) {
- console.error('Failed to send metric payload to Datadog:', await res.text());
- }
-
- return res.ok;
-};
diff --git a/apps/telemetry-proxy/src/routes/v1/telemetry.ts b/apps/telemetry-proxy/src/routes/v1/telemetry.ts
deleted file mode 100644
index b6e749445..000000000
--- a/apps/telemetry-proxy/src/routes/v1/telemetry.ts
+++ /dev/null
@@ -1,120 +0,0 @@
-import {
- TelemetryMetricPayload,
- TelemetryMetricPayloadSchema,
- TelemetryMetricRequestSchema,
-} from '../../types/metrics.types';
-import { AppBindings } from '../../types/app.types';
-import { sendMetricToDatadog } from '../../lib/metrics';
-import { sendErrorLogToDatadog } from '../../lib/errors';
-import { ErrorLogPayload, ErrorLogPayloadSchema } from '../../types/errors.types';
-import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
-
-const app = new OpenAPIHono<{ Bindings: AppBindings }>();
-
-const metricsRoute = createRoute({
- method: 'post',
- path: '/metrics/invocations',
- tags: ['telemetry'],
- request: {
- body: {
- content: {
- 'application/json': {
- schema: TelemetryMetricRequestSchema,
- },
- },
- },
- },
- responses: {
- 200: {
- description: 'The success of the request',
- content: {
- 'application/json': {
- schema: z.object({ success: z.boolean() }),
- },
- },
- },
- 400: {
- description: 'Invalid request body',
- content: {
- 'application/json': {
- schema: z.object({
- success: z.boolean(),
- error: z.string(),
- }),
- },
- },
- },
- },
-});
-
-const errorsRoute = createRoute({
- method: 'post',
- path: '/errors',
- tags: ['telemetry'],
- request: {
- body: {
- content: {
- 'application/json': {
- schema: ErrorLogPayloadSchema,
- },
- },
- },
- },
- responses: {
- 200: {
- description: 'The success of the request',
- content: {
- 'application/json': {
- schema: z.object({ success: z.boolean() }),
- },
- },
- },
- 400: {
- description: 'Invalid request body',
- content: {
- 'application/json': {
- schema: z.object({
- success: z.boolean(),
- error: z.string(),
- }),
- },
- },
- },
- },
-});
-
-app.openapi(metricsRoute, async c => {
- try {
- const rawBody = await c.req.json();
- const result = TelemetryMetricRequestSchema.safeParse(rawBody);
-
- if (!result.success) {
- return c.json({ success: false, error: 'Invalid request body' }, 400);
- }
-
- const apiKey = c.env.DATADOG_API_KEY as string;
- const res = await sendMetricToDatadog(result.data, apiKey);
- return c.json({ success: res, error: '' }, res ? 200 : 400);
- } catch (error) {
- return c.json({ success: false, error: 'Failed to process request' }, 400);
- }
-});
-
-app.openapi(errorsRoute, async c => {
- try {
- const rawBody = await c.req.json();
- const result = ErrorLogPayloadSchema.safeParse(rawBody);
-
- if (!result.success) {
- return c.json({ success: false, error: 'Invalid request body' }, 400);
- }
-
- const apiKey = c.env.DATADOG_API_KEY as string;
- const res = await sendErrorLogToDatadog(result.data, apiKey);
- return c.json({ success: res, error: '' }, res ? 200 : 400);
- } catch (error) {
- return c.json({ success: false, error: 'Failed to process request' }, 400);
- }
-});
-
-export default app;
diff --git a/apps/telemetry-proxy/src/types/app.types.ts b/apps/telemetry-proxy/src/types/app.types.ts
deleted file mode 100644
index 1fe7a1e1f..000000000
--- a/apps/telemetry-proxy/src/types/app.types.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export interface AppBindings {
- DATADOG_API_KEY: string;
-}
diff --git a/apps/telemetry-proxy/src/types/errors.types.ts b/apps/telemetry-proxy/src/types/errors.types.ts
deleted file mode 100644
index fe39cadb4..000000000
--- a/apps/telemetry-proxy/src/types/errors.types.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { z } from '@hono/zod-openapi';
-import { TelemetryMetricMetadataSchema, TelemetryMetricSourceSchema } from './telemetry.types';
-
-export const ErrorLogPayloadSchema = z.object({
- functionName: z.string().describe('The name of the function that was invoked'),
- errorName: z.string().describe('The name of the error').optional(),
- message: z.string().describe('The message of the error').optional(),
- stack: z.string().describe('The stack trace of the error').optional(),
- timestamp: z.number().describe('The timestamp of the error in epoch milliseconds').optional(),
- props: z.record(z.any()).optional().describe('The properties of the error'),
- source: TelemetryMetricSourceSchema.optional(),
- metadata: TelemetryMetricMetadataSchema.optional(),
-});
-export type ErrorLogPayload = z.infer<typeof ErrorLogPayloadSchema>;
diff --git a/apps/telemetry-proxy/src/types/metrics.types.ts b/apps/telemetry-proxy/src/types/metrics.types.ts
deleted file mode 100644
index cafbd8409..000000000
--- a/apps/telemetry-proxy/src/types/metrics.types.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import { z } from '@hono/zod-openapi';
-import { TelemetryMetricSourceSchema } from './telemetry.types';
-import { TelemetryMetricMetadataSchema } from './telemetry.types';
-
-export const TelemetryMetricPayloadSchema = z.object({
- functionName: z.string().describe('The name of the function that was invoked'),
- durationMs: z.number().describe('The duration of the function invocation in milliseconds'),
- timestamp: z.number().describe('The timestamp of the function invocation in epoch milliseconds'),
- props: z.record(z.any()).optional().describe('The properties of the function invocation'),
- source: TelemetryMetricSourceSchema.optional(),
- metadata: TelemetryMetricMetadataSchema.optional(),
- error: z
- .object({
- name: z.string().describe('The name of the error'),
- message: z.string().describe('The message of the error'),
- })
- .optional()
- .describe('The error that occurred during the function invocation'),
-});
-
-export type TelemetryMetricSource = z.infer<typeof TelemetryMetricSourceSchema>;
-export type TelemetryMetricMetadata = z.infer<typeof TelemetryMetricMetadataSchema>;
-export type TelemetryMetricPayload = z.infer<typeof TelemetryMetricPayloadSchema>;
-
-export const TelemetryMetricRequestSchema = z.array(TelemetryMetricPayloadSchema);
-export type TelemetryMetricRequest = z.infer<typeof TelemetryMetricRequestSchema>;
-
-// {
-// metric: 'composio.sdk.function_invocation',
-// type: 'count',
-// points: [[Math.floor(Date.now() / 1000), 1]],
-// tags: [
-// `function:${body.functionName}`,
-// ...(body.error ? ['status:error'] : ['status:success']),
-// ],
-// host: 'composio-sdk',
-// },
-export const DDMetricPayloadSchema = z.object({
- metric: z.string().describe('The name of the metric'),
- type: z
- .enum(['count', 'gauge', 'histogram', 'distribution', 'rate'])
- .describe('The type of the metric'),
- points: z.array(z.array(z.number())).describe('The points of the metric'),
- tags: z.array(z.string()).describe('The tags of the metric'),
- host: z.string().describe('The host of the metric'),
-});
-export type DDMetricPayload = z.infer<typeof DDMetricPayloadSchema>;
diff --git a/apps/telemetry-proxy/src/types/telemetry.types.ts b/apps/telemetry-proxy/src/types/telemetry.types.ts
deleted file mode 100644
index ec6ea7e19..000000000
--- a/apps/telemetry-proxy/src/types/telemetry.types.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { z } from '@hono/zod-openapi';
-
-export const TelemetryMetricSourceSchema = z.object({
- name: z.string().describe('The name of the source').optional(),
- service: z
- .enum(['sdk', 'apollo', 'hermes', 'thermos'])
- .describe('The service of the source')
- .optional(),
- language: z
- .enum(['python', 'typescript', 'go', 'rust'])
- .describe('The language of the function that was invoked')
- .optional(),
- version: z.string().describe('The version of the source').optional(),
- platform: z.string().describe('The platform of the source').optional(),
-});
-
-export const TelemetryMetricMetadataSchema = z.object({
- projectId: z.string().describe('The project ID of the source').optional(),
- provider: z.string().describe('The provider used in the source').optional(),
-});
diff --git a/apps/telemetry-proxy/tsconfig.json b/apps/telemetry-proxy/tsconfig.json
deleted file mode 100644
index 23345376b..000000000
--- a/apps/telemetry-proxy/tsconfig.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "compilerOptions": {
- "target": "ESNext",
- "module": "ESNext",
- "moduleResolution": "Bundler",
- "strict": true,
- "skipLibCheck": true,
- "lib": [
- "ESNext"
- ],
- "jsx": "react-jsx",
- "jsxImportSource": "hono/jsx"
- },
-}
\ No newline at end of file
diff --git a/apps/telemetry-proxy/wrangler.jsonc b/apps/telemetry-proxy/wrangler.jsonc
deleted file mode 100644
index d2fba5215..000000000
--- a/apps/telemetry-proxy/wrangler.jsonc
+++ /dev/null
@@ -1,38 +0,0 @@
-{
- "$schema": "node_modules/wrangler/config-schema.json",
- "name": "telemetry-proxy",
- "main": "src/index.ts",
- "compatibility_date": "2025-06-11",
- // "compatibility_flags": [
- // "nodejs_compat"
- // ],
- // "vars": {
- // "MY_VAR": "my-variable"
- // },
- // "kv_namespaces": [
- // {
- // "binding": "MY_KV_NAMESPACE",
- // "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- // }
- // ],
- // "r2_buckets": [
- // {
- // "binding": "MY_BUCKET",
- // "bucket_name": "my-bucket"
- // }
- // ],
- // "d1_databases": [
- // {
- // "binding": "MY_DB",
- // "database_name": "my-database",
- // "database_id": ""
- // }
- // ],
- // "ai": {
- // "binding": "AI"
- // },
- // "observability": {
- // "enabled": true,
- // "head_sampling_rate": 1
- // }
-}
diff --git a/examples/json-schema-to-zod/.env.example b/examples/json-schema-to-zod/.env.example
new file mode 100644
index 000000000..0e728257c
--- /dev/null
+++ b/examples/json-schema-to-zod/.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/examples/json-schema-to-zod/README.md b/examples/json-schema-to-zod/README.md
new file mode 100644
index 000000000..106137a94
--- /dev/null
+++ b/examples/json-schema-to-zod/README.md
@@ -0,0 +1,53 @@
+# Json-schema-to-zod Example
+
+This example demonstrates how to use Composio SDK for json-schema-to-zod.
+
+## 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/examples/json-schema-to-zod/package.json b/examples/json-schema-to-zod/package.json
new file mode 100644
index 000000000..28400c47c
--- /dev/null
+++ b/examples/json-schema-to-zod/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "json-schema-to-zod-example",
+ "private": true,
+ "version": "0.1.0",
+ "description": "Example project for Json-schema-to-zod",
+ "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",
+ "json-schema-to-zod"
+ ],
+ "author": "",
+ "license": "ISC",
+ "packageManager": "[email protected]",
+ "dependencies": {
+ "@composio/core": "workspace:*",
+ "dotenv": "^16.4.1",
+ "zod-to-json-schema": "^3.24.5"
+ },
+ "devDependencies": {
+ "@types/bun": "^1.2.9",
+ "typescript": "^5.8.3"
+ }
+}
diff --git a/examples/json-schema-to-zod/src/index.ts b/examples/json-schema-to-zod/src/index.ts
new file mode 100644
index 000000000..8e8ec0603
--- /dev/null
+++ b/examples/json-schema-to-zod/src/index.ts
@@ -0,0 +1,48 @@
+/**
+ * Json-schema-to-zod Example
+ *
+ * This example demonstrates how to use Composio SDK for json-schema-to-zod.
+ *
+ * Prerequisites:
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
+ * 3. Run the example: pnpm start
+ */
+
+import { Composio, jsonSchemaToZodSchema } from '@composio/core';
+import { zodToJsonSchema } from 'zod-to-json-schema';
+import 'dotenv/config';
+
+/**
+ * Initialize Composio
+ */
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+});
+
+/**
+ * Main function to run the example
+ */
+async function main() {
+ try {
+ console.log('🚀 Starting Json-schema-to-zod Example...');
+
+ // Get available tools
+ const tool = await composio.tools.getRawComposioToolBySlug(
+ 'default',
+ 'GOOGLECALENDAR_PATCH_EVENT'
+ );
+
+ console.log(JSON.stringify(tool.inputParameters, null, 2));
+
+ const zodSchema = jsonSchemaToZodSchema(tool.inputParameters ?? {});
+
+ console.log(JSON.stringify(zodToJsonSchema(zodSchema), null, 2));
+ // TODO: Add your example implementation here
+ console.log('📝 Implement your json-schema-to-zod logic here!');
+ } catch (error) {
+ console.error('❌ Error running example:', error);
+ }
+}
+
+// Run the example
+main().catch(console.error);
diff --git a/examples/json-schema-to-zod/tsconfig.json b/examples/json-schema-to-zod/tsconfig.json
new file mode 100644
index 000000000..158c157c8
--- /dev/null
+++ b/examples/json-schema-to-zod/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/examples/modifiers/src/index.ts b/examples/modifiers/src/index.ts
index af2e858b3..0190043c2 100644
--- a/examples/modifiers/src/index.ts
+++ b/examples/modifiers/src/index.ts
@@ -15,10 +15,13 @@ const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER', {
toolSchema = {
...toolSchema,
inputParameters: {
- ...toolSchema.inputParameters,
- userId: {
- type: 'string',
- description: 'The user ID to get the user for',
+ type: 'object',
+ properties: {
+ ...toolSchema.inputParameters?.properties,
+ userId: {
+ type: 'string',
+ description: 'The user ID to get the user for',
+ },
},
},
};
diff --git a/examples/toolkits/src/auth-fields.ts b/examples/toolkits/src/auth-fields.ts
new file mode 100644
index 000000000..1595d3889
--- /dev/null
+++ b/examples/toolkits/src/auth-fields.ts
@@ -0,0 +1,20 @@
+import { Composio } from '@composio/core';
+
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+});
+
+const toolkitSlug = 'shopify';
+
+console.log('🔄 Getting auth config creation fields for toolkit: ', toolkitSlug);
+const authConfigCreationFields = await composio.toolkits.getAuthConfigCreationFields(toolkitSlug);
+
+console.log('✅ Auth config creation fields:');
+console.log(authConfigCreationFields);
+
+console.log('🔄 Getting connected account initiation fields for toolkit: ', toolkitSlug);
+const connectedAccountInitiationFields =
+ await composio.toolkits.getConnectedAccountInitiationFields(toolkitSlug);
+
+console.log('✅ Connected account initiation fields:');
+console.log(connectedAccountInitiationFields);
diff --git a/packages/core/package.json b/packages/core/package.json
index 94c5fd0f2..2af6c8e5c 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -37,15 +37,15 @@
"vitest": "^3.1.4"
},
"dependencies": {
- "@composio/client": "0.1.0-alpha.17",
+ "@composio/client": "0.1.0-alpha.22",
"@composio/json-schema-to-zod": "0.1.6",
- "zod-to-json-schema": "^3.24.5",
"chalk": "^5.4.1",
"openai": "^4.94.0",
"pusher-js": "^8.4.0",
"semver": "^7.7.2",
"uuid": "^11.1.0",
- "zod": "^3.24.2"
+ "zod": "^3.24.2",
+ "zod-to-json-schema": "^3.24.5"
},
"gitHead": "4fae6e54d5c150fba955cc5fa314281da5a1e064"
}
diff --git a/packages/core/src/composio.ts b/packages/core/src/composio.ts
index 47ed9a183..0d6d48837 100644
--- a/packages/core/src/composio.ts
+++ b/packages/core/src/composio.ts
@@ -6,7 +6,6 @@ import { AuthConfigs } from './models/AuthConfigs';
import { ConnectedAccounts } from './models/ConnectedAccounts';
import { BaseComposioProvider } from './provider/BaseProvider';
import { telemetry } from './telemetry/Telemetry';
-import { BaseTelemetryTransport } from './telemetry/TelemetryTransport';
import { getSDKConfig } from './utils/sdk';
import logger from './utils/logger';
import { IS_DEVELOPMENT_OR_CI } from './utils/constants';
@@ -25,7 +24,6 @@ export type ComposioConfig<
allowTracking?: boolean;
allowTracing?: boolean;
provider?: TProvider;
- telemetryTransport?: BaseTelemetryTransport;
/**
* Request options to be passed to the Composio API client.
* This is useful for passing in a custom fetch implementation.
@@ -82,7 +80,6 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
* @param {boolean} [config.allowTracking=true] - Whether to allow anonymous usage analytics
* @param {boolean} [config.allowTracing=true] - Whether to allow request tracing for debugging
* @param {TProvider} [config.provider] - The provider to use for this Composio instance (defaults to OpenAIProvider)
- * @param {BaseTelemetryTransport} [config.telemetryTransport] - Custom telemetry transport implementation
*
* @example
* ```typescript
@@ -150,22 +147,20 @@ export class Composio<TProvider extends BaseComposioProvider<unknown, unknown> =
* Initialize the client telemetry.
*/
if (this.config.allowTracking) {
- telemetry.setup(
- {
- apiKey: apiKeyParsed ?? '',
- baseUrl: baseURLParsed ?? '',
- frameworkRuntime: this.provider?.name ?? 'unknown',
- isAgentic: this.provider?._isAgentic || false,
- source: 'javascript',
- version: version,
- sdkType: 'Typescript-V3',
- isBrowser: typeof window !== 'undefined',
- // @TODO: Users might want to pass their own session id
- // @TODO: We shouldn't be doing this as people might always have one session id throughout the process in server
- sessionId: this.config.allowTracing ? getRandomUUID() : undefined, // @TODO: get the session id
- },
- config?.telemetryTransport
- );
+ telemetry.setup({
+ apiKey: apiKeyParsed ?? '',
+ baseUrl: baseURLParsed ?? '',
+ frameworkRuntime: this.provider?.name ?? 'unknown',
+ isAgentic: this.provider?._isAgentic || false,
+ source: 'composio-sdk-typescript',
+ version: version,
+ sdkType: 'Typescript-V3',
+ isBrowser: typeof window !== 'undefined',
+ provider: this.provider?.name ?? 'openai',
+ // @TODO: Users might want to pass their own session id
+ // @TODO: We shouldn't be doing this as people might always have one session id throughout the process in server
+ sessionId: this.config.allowTracing ? getRandomUUID() : undefined, // @TODO: get the session id
+ });
}
telemetry.instrument(this);
// instrument the provider since we are not using the provider class directly
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 9696f815f..1963f7947 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -4,7 +4,6 @@ export { ComposioProvider } from './provider/ComposioProvider';
export { BaseAgenticProvider, BaseNonAgenticProvider } from './provider/BaseProvider';
export type { BaseComposioProvider } from './provider/BaseProvider';
export { OpenAIProvider } from './provider/OpenAIProvider';
-export { BaseTelemetryTransport, ConsoleTelemetryTransport } from './telemetry/TelemetryTransport';
export { jsonSchemaToZodSchema } from './utils/jsonSchema';
export { AuthScheme } from './models/AuthScheme';
export * from './types/provider.types';
diff --git a/packages/core/src/models/Toolkits.ts b/packages/core/src/models/Toolkits.ts
index fac708d42..a0999e1fa 100644
--- a/packages/core/src/models/Toolkits.ts
+++ b/packages/core/src/models/Toolkits.ts
@@ -10,6 +10,8 @@ import {
ToolkitRetrieveCategoriesResponse,
ToolkitRetrieveCategoriesResponseSchema,
ToolkitCategorySchema,
+ ToolkitAuthField,
+ ToolkitAuthFieldsResponse,
} from '../types/toolkit.types';
import { ComposioToolkitFetchError, ComposioToolkitNotFoundError } from '../errors';
import { ValidationError } from '../errors/ValidationErrors';
@@ -18,6 +20,8 @@ import { ComposioAuthConfigNotFoundError } from '../errors/AuthConfigErrors';
import { ConnectedAccounts } from './ConnectedAccounts';
import { ConnectionRequest } from './ConnectionRequest';
import { telemetry } from '../telemetry/Telemetry';
+import { AuthSchemeEnum, AuthSchemeType } from '../types/authConfigs.types';
+import logger from '../utils/logger';
/**
* Toolkits class
*
@@ -113,7 +117,7 @@ export class Toolkits {
*
* @private
*/
- private async getToolkitBySlug(slug: string): Promise<ToolkitRetrieveResponse> {
+ protected async getToolkitBySlug(slug: string): Promise<ToolkitRetrieveResponse> {
try {
const result = await this.client.toolkits.retrieve(slug);
const parsedResult = ToolkitRetrieveResponseSchema.safeParse({
@@ -221,6 +225,114 @@ export class Toolkits {
return this.getToolkits(arg);
}
+ private async getAuthConfigFields(
+ toolkitSlug: string,
+ authScheme: AuthSchemeType | null,
+ authConfigType: 'authConfigCreation' | 'connectedAccountInitiation',
+ requiredOnly: boolean
+ ): Promise<ToolkitAuthFieldsResponse> {
+ const toolkit = await this.getToolkitBySlug(toolkitSlug);
+ if (!toolkit.authConfigDetails) {
+ throw new ComposioAuthConfigNotFoundError('No auth config found for toolkit', {
+ meta: {
+ toolkitSlug,
+ },
+ });
+ }
+
+ // if multiple auth configs are found, warn the user and select the first one
+ if (toolkit.authConfigDetails.length > 1 && !authScheme) {
+ logger.warn(
+ `Multiple auth configs found for ${toolkitSlug}, please specify the auth scheme to get details of specific auth scheme. Selecting the first scheme by default.`,
+ {
+ meta: {
+ toolkitSlug,
+ },
+ }
+ );
+ }
+
+ // if authScheme is provided, find the auth config for the given auth scheme
+ // otherwise, use the first auth config
+ const authConfig = authScheme
+ ? toolkit.authConfigDetails.find(authConfig => authConfig.mode === authScheme)
+ : toolkit.authConfigDetails[0];
+
+ if (!authConfig) {
+ throw new ComposioAuthConfigNotFoundError('No auth config found for toolkit', {
+ meta: {
+ toolkitSlug,
+ authScheme,
+ },
+ });
+ }
+
+ const requiredFields = authConfig.fields[authConfigType].required.map(field => ({
+ ...field,
+ required: true,
+ }));
+ if (requiredOnly) {
+ return {
+ authScheme: AuthSchemeEnum.parse(authConfig.mode),
+ fields: requiredFields,
+ };
+ }
+
+ const optionalFields = authConfig.fields[authConfigType].optional.map(field => ({
+ ...field,
+ required: false,
+ }));
+
+ return {
+ authScheme: AuthSchemeEnum.parse(authConfig.mode),
+ fields: [...requiredFields, ...optionalFields],
+ };
+ }
+
+ /**
+ * Retrieves the fields required for creating an auth config for a toolkit.
+ * @param toolkitSlug - The slug of the toolkit to retrieve the fields for
+ * @param authScheme - The auth scheme to retrieve the fields for
+ * @param requiredOnly - Whether to only return the required fields
+ * @returns {Promise<ToolkitAuthFieldsResponse>} The fields required for creating an auth config
+ */
+ async getAuthConfigCreationFields(
+ toolkitSlug: string,
+ {
+ authScheme,
+ requiredOnly = false,
+ }: { authScheme?: AuthSchemeType; requiredOnly?: boolean } = {}
+ ): Promise<ToolkitAuthFieldsResponse> {
+ return this.getAuthConfigFields(
+ toolkitSlug,
+ authScheme ?? null,
+ 'authConfigCreation',
+ requiredOnly
+ );
+ }
+
+ /**
+ * Retrieves the fields required for initiating a connected account for a toolkit.
+ * @param toolkitSlug - The slug of the toolkit to retrieve the fields for
+ * @param authScheme - The auth scheme to retrieve the fields for
+ * @param requiredOnly - Whether to only return the required fields
+ * @returns {Promise<ToolkitAuthFieldsResponse>} The fields required for initiating a connected account
+ */
+ async getConnectedAccountInitiationFields(
+ toolkitSlug: string,
+ {
+ authScheme,
+ requiredOnly = false,
+ }: { authScheme?: AuthSchemeType; requiredOnly?: boolean } = {}
+ ): Promise<ToolkitAuthFieldsResponse> {
+ return this.getAuthConfigFields(
+ toolkitSlug,
+ authScheme ?? null,
+ 'connectedAccountInitiation',
+ requiredOnly
+ );
+ }
+
/**
* Retrieves all toolkit categories available in the Composio SDK.
*
diff --git a/packages/core/src/services/telemetry/TelemetryService.ts b/packages/core/src/services/telemetry/TelemetryService.ts
new file mode 100644
index 000000000..1646aa9d2
--- /dev/null
+++ b/packages/core/src/services/telemetry/TelemetryService.ts
@@ -0,0 +1,35 @@
+import { TelemetryMetricPayloadBody, TelemetryPayload } from './TelemetryService.types';
+
+const TELEMETRY_URL = 'https://telemetry.composio.dev/v1';
+
+export class TelemetryService {
+ /**
+ * Sends a metric to the Telemetry API.
+ * @param payload - The payload to send to the Telemetry API.
+ * @returns The response from the Telemetry API.
+ */
+ static async sendMetric(payload: TelemetryMetricPayloadBody) {
+ return fetch(`${TELEMETRY_URL}/metrics/invocations`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(payload),
+ });
+ }
+
+ /**
+ * Sends an error log to the Telemetry API.
+ * @param payload - The payload to send to the Telemetry API.
+ * @returns The response from the Telemetry API.
+ */
+ static async sendErrorLog(payload: TelemetryPayload) {
+ return fetch(`${TELEMETRY_URL}/errors`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(payload),
+ });
+ }
+}
diff --git a/packages/core/src/services/telemetry/TelemetryService.types.ts b/packages/core/src/services/telemetry/TelemetryService.types.ts
new file mode 100644
index 000000000..36ecec377
--- /dev/null
+++ b/packages/core/src/services/telemetry/TelemetryService.types.ts
@@ -0,0 +1,64 @@
+import { z } from 'zod';
+
+export const TelemetryMetricSourceSchema = z.object({
+ name: z.string().describe('The name of the source').optional(),
+ service: z
+ .enum(['sdk', 'apollo', 'hermes', 'thermos'])
+ .describe('The service of the source')
+ .optional(),
+ language: z
+ .enum(['python', 'typescript', 'go', 'rust'])
+ .describe('The language of the function that was invoked')
+ .optional(),
+ version: z.string().describe('The version of the source').optional(),
+ platform: z.string().describe('The platform of the source').optional(),
+ environment: z
+ .enum(['development', 'production', 'ci', 'staging', 'test'])
+ .describe('The environment of the source, eg: development, production, ci etc')
+ .optional(),
+});
+
+export const TelemetryMetricMetadataSchema = z.object({
+ projectId: z.string().describe('The project ID of the source').optional(),
+ provider: z.string().describe('The provider used in the source').optional(),
+});
+
+export const TelemetryPayloadSchema = z.object({
+ functionName: z.string().describe('The name of the function that was invoked'),
+ durationMs: z
+ .number()
+ .describe('The duration of the function invocation in milliseconds')
+ .optional(),
+ timestamp: z
+ .number()
+ .describe('The timestamp of the function invocation in epoch seconds')
+ .optional(),
+ props: z.record(z.any()).optional().describe('The properties of the function invocation'),
+ source: TelemetryMetricSourceSchema.optional(),
+ metadata: TelemetryMetricMetadataSchema.optional(),
+ error: z
+ .object({
+ name: z.string().describe('The name of the error'),
+ code: z.string().describe('The code of the error').optional(),
+ errorId: z
+ .string()
+ .describe(
+ 'The unique error ID of the error, this is used to track the error across invocations'
+ )
+ .optional(),
+ message: z.string().describe('The message of the error').optional(),
+ stack: z.string().describe('The stack trace of the error').optional(),
+ })
+ .optional()
+ .describe('The error that occurred during the function invocation'),
+});
+
+export const TelemetryMetricPayloadBodySchema = z.array(TelemetryPayloadSchema);
+
+export type TelemetryMetricSource = z.infer<typeof TelemetryMetricSourceSchema>;
+export type TelemetryMetricMetadata = z.infer<typeof TelemetryMetricMetadataSchema>;
+export type TelemetryPayload = z.infer<typeof TelemetryPayloadSchema>;
+export type TelemetryMetricPayloadBody = z.infer<typeof TelemetryMetricPayloadBodySchema>;
+
+export const TelemetryRequestBodySchema = z.array(TelemetryPayloadSchema);
+export type TelemetryRequestBody = z.infer<typeof TelemetryRequestBodySchema>;
diff --git a/packages/core/src/telemetry/BatchProcessor.ts b/packages/core/src/telemetry/BatchProcessor.ts
index 8284f64b3..95119956d 100644
--- a/packages/core/src/telemetry/BatchProcessor.ts
+++ b/packages/core/src/telemetry/BatchProcessor.ts
@@ -1,39 +1,43 @@
+import {
+ TelemetryMetricPayloadBody,
+ TelemetryPayload,
+} from '../services/telemetry/TelemetryService.types';
+
export class BatchProcessor {
- private batch: unknown[] = [];
- private time: number;
- private batchSize: number;
- private processBatchCallback: (data: unknown[]) => void;
- private timer: NodeJS.Timeout | null = null;
-
- constructor(
- time: number = 2000,
- batchSize: number = 100,
- processBatchCallback: (data: unknown[]) => void
- ) {
- this.batch = [];
- this.time = time;
- this.batchSize = batchSize;
- this.processBatchCallback = processBatchCallback;
+ private batch: TelemetryMetricPayloadBody = [];
+ private time: number;
+ private batchSize: number;
+ private processBatchCallback: (data: TelemetryMetricPayloadBody) => void;
+ private timer: NodeJS.Timeout | null = null;
+
+ constructor(
+ time: number = 2000,
+ batchSize: number = 100,
+ processBatchCallback: (data: TelemetryMetricPayloadBody) => void
+ ) {
+ this.batch = [];
+ this.time = time;
+ this.batchSize = batchSize;
+ this.processBatchCallback = processBatchCallback;
+ }
+
+ pushItem(item: TelemetryPayload) {
+ this.batch.push(item);
+ if (this.batch.length >= this.batchSize) {
+ this.processBatch();
+ } else if (!this.timer) {
+ this.timer = setTimeout(() => this.processBatch(), this.time);
}
-
- pushItem(item: unknown) {
- this.batch.push(item);
- if (this.batch.length >= this.batchSize) {
- this.processBatch();
- } else if (!this.timer) {
- this.timer = setTimeout(() => this.processBatch(), this.time);
- }
+ }
+
+ processBatch() {
+ if (this.batch.length > 0) {
+ this.processBatchCallback(this.batch);
+ this.batch = [];
}
-
- processBatch() {
- if (this.batch.length > 0) {
- this.processBatchCallback(this.batch);
- this.batch = [];
- }
- if (this.timer) {
- clearTimeout(this.timer);
- this.timer = null;
- }
+ if (this.timer) {
+ clearTimeout(this.timer);
+ this.timer = null;
}
}
-
\ No newline at end of file
+}
diff --git a/packages/core/src/telemetry/README.md b/packages/core/src/telemetry/README.md
index 0212c63a3..f542dc9c9 100644
--- a/packages/core/src/telemetry/README.md
+++ b/packages/core/src/telemetry/README.md
@@ -1,208 +1,123 @@
-# Telemetry Transports
+# Telemetry System Overview
-This directory contains implementations of different telemetry transport mechanisms for sending telemetry data to various endpoints.
+This document describes how telemetry is implemented in the SDK, including the main components, their responsibilities, and the data flow.
-## Available Transports
+---
-- `BrowserTransport`: Uses XMLHttpRequest for browser environments
-- `ProcessTransport`: Uses Node.js child processes for server-side environments
-- `ConsoleTransport`: Logs telemetry data to console (for development/debugging)
+## 1. Telemetry Architecture Overview
-## Using with Composio
+**Purpose:**
+The telemetry system collects, batches, and sends usage and error data from the SDK to a remote telemetry server, with minimal impact on application performance.
-To use a custom telemetry transport with the Composio instance:
+**Key Components:**
-```typescript
-import { Composio, BaseTelemetryTransport } from '@composio/core';
-
-// Create your custom transport
-class CustomTelemetryTransport implements BaseTelemetryTransport {
- async send(payload: TelemetryTransportParams): Promise<void> {
- // Your custom implementation
- console.log('Sending telemetry:', payload);
- return Promise.resolve();
- }
-}
-
-// Initialize Composio with your custom transport
-const composio = new Composio({
- apiKey: 'your-api-key',
- baseURL: 'https://api.composio.dev',
- allowTracking: true, // Enable telemetry
- telemetryTransport: new CustomTelemetryTransport() // Pass your custom transport
-});
-```
+- `TelemetryTransport`: Main class for instrumentation and telemetry logic.
+- `BatchProcessor`: Buffers and batches telemetry events before sending.
+- `TelemetryService`: Handles HTTP requests to the telemetry API.
+- **Types**: Define the structure of telemetry payloads.
-You can also use the built-in transports:
+---
-```typescript
-import { Composio, BrowserTelemetryTransport, ProcessTelemetryTransport } from '@composio/core';
-
-// For browser environments
-const composio = new Composio({
- apiKey: 'your-api-key',
- telemetryTransport: new BrowserTelemetryTransport()
-});
-
-// For Node.js environments
-const composio = new Composio({
- apiKey: 'your-api-key',
- telemetryTransport: new ProcessTelemetryTransport()
-});
-```
+## 2. BatchProcessor
-Note: The telemetry system will automatically use the appropriate transport based on the environment if you don't specify one, but you can override this behavior by providing your own transport.
+**Role:** Efficiently batches telemetry events to reduce the number of API calls and avoid performance bottlenecks.
-## Creating a New Transport
+**How it works:**
-To create a new telemetry transport, follow these steps:
+- Maintains an internal array (`batch`) to store telemetry events.
+- Configurable batch size and time interval (defaults: 100 items or 2 seconds).
+- When an event is added via `pushItem`:
+ - If the batch size is reached, immediately processes (sends) the batch.
+ - If not, starts a timer (if not already running) to process the batch after the interval.
+- `processBatch`:
+ - Invokes a callback (provided at construction) with the current batch.
+ - Clears the batch and resets the timer.
-1. Create a new file in the `transports` directory (e.g., `YourNewTransport.ts`)
+---
-2. Implement the `BaseTelemetryTransport` interface:
+## 3. TelemetryTransport (Main Telemetry Class)
-```typescript
-import { TelemetryTransportParams } from "../../types/telemetry.types";
-import { BaseTelemetryTransport } from "../TelemetryTransport";
-
-export class YourNewTransport implements BaseTelemetryTransport {
- send(payload: TelemetryTransportParams): Promise<void> {
- return new Promise((resolve) => {
- try {
- // Your transport-specific implementation
- // e.g., using fetch, WebSocket, or any other transport mechanism
-
- // Always resolve the promise, even on error
- resolve();
- } catch (error) {
- // Handle errors silently
- resolve();
- }
- });
- }
-}
-```
+**Initialization:**
-### Required Interface
+- Configured with metadata (SDK version, environment, provider, etc.).
+- Sets up a `BatchProcessor` with a callback to send metrics via `TelemetryService.sendMetric`.
-Your transport must implement the following interface:
+**Instrumentation:**
-```typescript
-interface TelemetryTransportParams {
- url: string; // The endpoint to send data to
- method: string; // HTTP method (e.g., 'POST')
- headers: Record<string, string>; // HTTP headers
- data: AcceptableJSONValue; // The actual telemetry data
-}
-```
+- The `instrument` method wraps all async methods of a given instance (e.g., SDK classes).
+- For each method call:
+ - Creates a telemetry payload with function name, arguments, timestamp, etc.
+ - Pushes the payload to the batch processor.
+ - If the method throws an error, captures error details and sends error telemetry immediately.
-### Implementation Guidelines
+**Sending Telemetry:**
-1. **Error Handling**
- - Always resolve the promise, even when errors occur
- - Don't throw errors that could affect the main application flow
- - Log errors appropriately for debugging
+- **Metrics:** Batched and sent via `TelemetryService.sendMetric` (POST to `/metrics/invocations`).
+- **Errors:** Sent immediately via `TelemetryService.sendErrorLog` (POST to `/errors`).
-2. **Environment Awareness**
- - Add environment-specific checks if your transport is meant for a specific runtime
- - Example:
- ```typescript
- if (typeof window === 'undefined') {
- return Promise.reject(new Error('YourTransport can only be used in specific environment'));
- }
- ```
+**Environment Awareness:**
-3. **Performance**
- - Keep the implementation lightweight
- - Ensure non-blocking operation
- - Consider using background processes or workers for heavy operations
+- Telemetry is disabled in certain environments (e.g., `test`, `ci`, or if `TELEMETRY_DISABLED` is set).
-### Example: WebSocket Transport
+---
-Here's an example of a WebSocket-based transport:
+## 4. TelemetryService
-```typescript
-export class WebSocketTransport implements BaseTelemetryTransport {
- private ws: WebSocket | null = null;
- private queue: TelemetryTransportParams[] = [];
- private isConnecting = false;
-
- constructor(private url: string) {}
-
- private async connect(): Promise<void> {
- if (this.isConnecting) return;
- this.isConnecting = true;
-
- return new Promise((resolve) => {
- this.ws = new WebSocket(this.url);
-
- this.ws.onopen = () => {
- this.isConnecting = false;
- this.processQueue();
- resolve();
- };
-
- this.ws.onerror = () => {
- this.isConnecting = false;
- resolve();
- };
- });
- }
-
- private async processQueue() {
- while (this.queue.length > 0 && this.ws?.readyState === WebSocket.OPEN) {
- const payload = this.queue.shift();
- if (payload) {
- this.ws.send(JSON.stringify(payload));
- }
- }
- }
-
- async send(payload: TelemetryTransportParams): Promise<void> {
- return new Promise((resolve) => {
- try {
- if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
- this.queue.push(payload);
- this.connect().then(resolve);
- } else {
- this.ws.send(JSON.stringify(payload));
- resolve();
- }
- } catch (error) {
- resolve();
- }
- });
- }
-}
-```
+- **sendMetric:** Sends a batch of telemetry events to the `/metrics/invocations` endpoint.
+- **sendErrorLog:** Sends a single error event to the `/errors` endpoint.
+- **Implementation:** Uses `fetch` with JSON payloads.
-### Integration
+---
-To use your new transport:
+## 5. Telemetry Payload Structure
-```typescript
-import { YourNewTransport } from './transports/YourNewTransport';
+**Types:** Defined in `TelemetryService.types.ts` using Zod schemas and TypeScript types.
-// Create an instance
-const transport = new YourNewTransport();
+**Payload Fields:**
-// Use with telemetry system
-telemetrySystem.setTransport(transport);
+- `functionName`: Name of the function/method invoked.
+- `durationMs`: Duration of the invocation (optional).
+- `timestamp`: Epoch time of the event.
+- `props`: Additional properties (e.g., method arguments).
+- `source`: Metadata about the SDK/runtime.
+- `metadata`: Project/provider info.
+- `error`: Error details (if any).
+
+---
+
+## 6. Error Handling
+
+**Error Instrumentation:**
+
+- If an instrumented method throws, the error is captured.
+- Error details (name, message, stack, code) are included in the telemetry payload.
+- Each error is assigned a unique `errorId` if not already present.
+
+---
+
+## 7. Usage Example
+
+```typescript
+const telemetry = new TelemetryTransport();
+telemetry.setup(metadata); // metadata includes SDK version, provider, etc.
+telemetry.instrument(someSdkInstance); // Instruments all async methods for telemetry
```
-## Best Practices
+---
+
+## 8. Summary of Flow
+
+1. SDK initializes telemetry with environment and version info.
+2. SDK classes are instrumented so all async method calls are logged.
+3. Each method call generates a telemetry event, which is batched.
+4. BatchProcessor sends events in bulk (by size or time) to the telemetry server.
+5. Errors are captured and sent immediately.
+6. TelemetryService handles the actual HTTP requests.
-1. **Testing**
- - Write unit tests for your transport
- - Test error scenarios
- - Verify environment-specific behavior
+---
-2. **Documentation**
- - Document any specific requirements or dependencies
- - Include usage examples
- - Note any environment limitations
+## 9. Telemetry Service Hosting & Source
-3. **Maintenance**
- - Keep dependencies up to date
- - Monitor for deprecation warnings
- - Update documentation as needed
+- The telemetry services are hosted at: https://telemetry.composio.io
+- The source code for the telemetry backend is available in the [ComposioHQ/hermes](https://github.com/ComposioHQ/hermes) repository.
+- The relevant backend implementation can be found in the `/apps/telemetry-proxy` directory of that repository.
diff --git a/packages/core/src/telemetry/Telemetry.ts b/packages/core/src/telemetry/Telemetry.ts
index 435071ac2..e90ca66d7 100644
--- a/packages/core/src/telemetry/Telemetry.ts
+++ b/packages/core/src/telemetry/Telemetry.ts
@@ -6,22 +6,18 @@ declare global {
}
import { ComposioError as ComposioClientError } from '@composio/client';
-import {
- TELEMETRY_EVENTS,
- // TelemetryErrorPayload,
- TelemetryErrorPayloadParams,
- TelemetryMetadata,
- TelemetryPayload,
- TelemetryPayloadParams,
-} from '../types/telemetry.types';
-import { TELEMETRY_URL } from '../utils/constants';
+import { TELEMETRY_EVENTS, TelemetryMetadata } from '../types/telemetry.types';
import { getEnvVariable } from '../utils/env';
import { BatchProcessor } from './BatchProcessor';
-import { BaseTelemetryTransport } from './TelemetryTransport';
-import { BrowserTelemetryTransport } from './transports/BrowserTransport';
-import { ProcessTelemetryTransport } from './transports/ProcessTransport';
import { ComposioError } from '../errors/ComposioError';
import { getRandomUUID } from '../utils/uuid';
+import {
+ TelemetryMetricPayloadBody,
+ TelemetryMetricSource,
+ TelemetryPayload,
+} from '../services/telemetry/TelemetryService.types';
+import { TelemetryService } from '../services/telemetry/TelemetryService';
+import logger from '../utils/logger';
/**
* The Telemetry class is used to log the telemetry for any given instance which extends InstrumentedInstance.
*
@@ -39,35 +35,41 @@ import { getRandomUUID } from '../utils/uuid';
* telemetry.instrument(composio.triggers);
*
*/
-export class TelemetryService {
+export class TelemetryTransport {
private telemetryMetadata!: TelemetryMetadata;
- private transport!: BaseTelemetryTransport;
private isTelemetryDisabled: boolean = true;
-
- private batchProcessor = new BatchProcessor(100, 10, async data => {
- await this.sendTelemetry(data as TelemetryPayload[]);
+ private telemetrySource!: TelemetryMetricSource;
+ private readonly telemetrySourceName = 'typescript-sdk';
+ private readonly telemetryServiceName = 'sdk';
+ private readonly telemetryLanguage = 'typescript';
+
+ private batchProcessor = new BatchProcessor(200, 10, async (data: TelemetryMetricPayloadBody) => {
+ logger.debug('Sending batch of telemetry metrics', data);
+ await TelemetryService.sendMetric(data as TelemetryPayload[]);
});
- setup(metadata: TelemetryMetadata, transport?: BaseTelemetryTransport) {
+ setup(metadata: TelemetryMetadata) {
this.telemetryMetadata = metadata;
this.isTelemetryDisabled = false;
-
- const isBrowser = typeof window !== 'undefined';
- if (transport) {
- this.transport = transport;
- } else if (isBrowser) {
- this.transport = new BrowserTelemetryTransport();
- this.telemetryMetadata.transport = 'browser';
- } else {
- this.transport = new ProcessTelemetryTransport();
- this.telemetryMetadata.transport = 'process';
- }
-
+ this.telemetrySource = {
+ name: this.telemetrySourceName,
+ service: this.telemetryServiceName,
+ language: this.telemetryLanguage,
+ version: this.telemetryMetadata?.version,
+ platform: this.telemetryMetadata?.isBrowser ? 'browser' : 'node',
+ };
// send telemetry event for SDK initialization
- this.sendTelemetry([
+ this.sendMetric([
{
- eventName: TELEMETRY_EVENTS.SDK_INITIALIZED,
- data: {},
+ functionName: TELEMETRY_EVENTS.SDK_INITIALIZED,
+ durationMs: 0,
+ timestamp: Date.now() / 1000,
+ props: {},
+ source: this.telemetrySource,
+ metadata: {
+ provider: this.telemetryMetadata?.provider ?? 'openai',
+ },
+ error: undefined,
},
]);
}
@@ -101,13 +103,20 @@ export class TelemetryService {
) => Promise<unknown>;
(instance as unknown as Record<string, Function>)[name] = async (...args: unknown[]) => {
- const telemetryPayload: TelemetryPayloadParams = {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {
+ const telemetryPayload: TelemetryPayload = {
+ functionName: `${instrumentedClassName}.${name}`,
+ durationMs: 0,
+ timestamp: Date.now() / 1000,
+ props: {
fileName: instrumentedClassName,
method: name,
params: args,
},
+ metadata: {
+ provider: this.telemetryMetadata?.provider ?? 'openai',
+ },
+ error: undefined,
+ source: this.telemetrySource,
};
this.batchProcessor.pushItem(telemetryPayload);
@@ -118,7 +127,7 @@ export class TelemetryService {
if (error instanceof Error) {
if (!error.errorId) {
error.errorId = getRandomUUID();
- this.prepareAndSendErrorTelemetry(error, instrumentedClassName, name, args);
+ await this.prepareAndSendErrorTelemetry(error, instrumentedClassName, name, args);
}
}
throw error;
@@ -134,14 +143,12 @@ export class TelemetryService {
* @returns true if the telemetry should be sent, false otherwise
*/
private shouldSendTelemetry() {
- const devEnvironments = ['test', 'development', 'CI'];
+ const telemetryDisabledEnvironments = ['test', 'ci'];
const nodeEnv = (getEnvVariable('NODE_ENV', 'development') || '').toLowerCase();
- const isDevEnvironment = devEnvironments.includes(nodeEnv);
+ const isDisabledEnvironment = telemetryDisabledEnvironments.includes(nodeEnv);
const isTelemetryDisabledByEnv = getEnvVariable('TELEMETRY_DISABLED', 'false') === 'true';
- return (
- this.transport && !this.isTelemetryDisabled && !isTelemetryDisabledByEnv && !isDevEnvironment
- );
+ return !this.isTelemetryDisabled && !isTelemetryDisabledByEnv && !isDisabledEnvironment;
}
/**
@@ -153,45 +160,52 @@ export class TelemetryService {
* @param {string} instrumentedClassName - The class name of the instrumented class.
* @param {string} name - The name of the method that threw the error.
*/
- private prepareAndSendErrorTelemetry(
+ private async prepareAndSendErrorTelemetry(
error: unknown,
instrumentedClassName: string,
name: string,
args: unknown[]
) {
+ const telemetryPayload: TelemetryPayload = {
+ functionName: `${instrumentedClassName}.${name}`,
+ durationMs: 0,
+ timestamp: Date.now() / 1000,
+ props: {
+ fileName: instrumentedClassName,
+ method: name,
+ params: args,
+ },
+ metadata: {
+ provider: this.telemetryMetadata?.provider ?? 'openai',
+ },
+ source: this.telemetrySource,
+ };
// client error, this is likely handled by the API itseld
if (error instanceof ComposioClientError) {
- const telemetryPayload: TelemetryErrorPayloadParams = {
- error_id: error.errorId || '',
- error_message: error.message,
- original_error: error.cause || error,
- current_stack: error.stack ? error.stack.split('\n') : [],
- description: `Original Call : ${this.telemetryMetadata?.source}:${instrumentedClassName}.${name}(${JSON.stringify(args)})`,
+ telemetryPayload.error = {
+ errorId: error.errorId,
+ name: error.name,
+ message: error.message,
+ stack: error.stack,
};
- this.sendErrorTelemetry(telemetryPayload);
} else if (error instanceof ComposioError) {
- // Composio SDK Errors
- const telemetryPayload: TelemetryErrorPayloadParams = {
- error_id: error.errorId || '',
- error_message: error.message,
- original_error: error.cause || error,
- current_stack: error.stack ? error.stack.split('\n') : [],
- error_code: error.code,
- possible_fix: error.possibleFixes?.join('\n'),
- description: `Original Call : ${instrumentedClassName}.${name}(${JSON.stringify(args)})`,
+ telemetryPayload.error = {
+ errorId: error.errorId,
+ name: error.name,
+ code: error.code,
+ message: error.message,
+ stack: error.stack,
};
- this.sendErrorTelemetry(telemetryPayload);
} else if (error instanceof Error) {
- // Unhandled errors
- const telemetryPayload: TelemetryErrorPayloadParams = {
- error_id: error.errorId || '',
- error_message: error instanceof Error ? error.name : 'Unknown error',
- original_error: error,
- current_stack: error.stack ? error.stack.split('\n') : [],
- description: `Original Call : ${this.telemetryMetadata?.source}:${instrumentedClassName}.${name}(${JSON.stringify(args)})`,
+ telemetryPayload.error = {
+ errorId: error.errorId,
+ name: error.name ?? 'Unknown error',
+ message: error.message,
+ stack: error.stack,
};
- this.sendErrorTelemetry(telemetryPayload);
}
+
+ await this.sendErrorTelemetry(telemetryPayload);
}
/**
@@ -199,45 +213,31 @@ export class TelemetryService {
* @param payload - the telemetry payload to send
* @returns
*/
- async sendTelemetry(payload: TelemetryPayloadParams[]) {
+ async sendMetric(payload: TelemetryMetricPayloadBody) {
if (!this.shouldSendTelemetry()) {
+ logger.debug('Telemetry is disabled, skipping metric telemetry', payload);
return;
}
-
- const url = `${TELEMETRY_URL}/api/sdk_metrics/telemetry`;
-
- const reqPayload = {
- data: payload.map(p => ({
- ...p,
- sdk_meta: this.telemetryMetadata,
- })),
- url,
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- };
-
- this.transport.send(reqPayload);
+ try {
+ logger.debug('SDK Metric', payload);
+ await TelemetryService.sendMetric(payload);
+ } catch (error) {
+ logger.error('Error sending metric telemetry', error);
+ }
}
// @TODO: check if this will send the error telemetry to the server
- async sendErrorTelemetry(payload: TelemetryErrorPayloadParams) {
+ async sendErrorTelemetry(payload: TelemetryPayload) {
if (!this.shouldSendTelemetry()) {
+ logger.debug('Telemetry is disabled, skipping metric telemetry', payload);
return;
}
-
- const url = `${TELEMETRY_URL}/api/sdk_metrics/error`;
-
- const reqPayload = {
- data: {
- ...payload,
- sdk_meta: this.telemetryMetadata,
- },
- url,
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- };
-
- this.transport.send(reqPayload);
+ try {
+ logger.debug('SDK Error Telemetry', payload);
+ await TelemetryService.sendErrorLog(payload);
+ } catch (error) {
+ logger.error('Error sending error telemetry', error);
+ }
}
}
-export const telemetry = new TelemetryService();
+export const telemetry = new TelemetryTransport();
diff --git a/packages/core/src/telemetry/TelemetryTransport.ts b/packages/core/src/telemetry/TelemetryTransport.ts
deleted file mode 100644
index 73c964228..000000000
--- a/packages/core/src/telemetry/TelemetryTransport.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import chalk from 'chalk';
-import { TelemetryTransportParams } from '../types/telemetry.types';
-import logger from '../utils/logger';
-
-export abstract class BaseTelemetryTransport {
- /**
- * Send the payload to the telemetry service.
- * @param payload - The payload to send.
- */
- abstract send(payload: TelemetryTransportParams): Promise<void>;
-}
-
-/**
- * A telemetry transport that logs to the console.
- * This is just for testing purposes.
- */
-export class ConsoleTelemetryTransport implements BaseTelemetryTransport {
- send(payload: TelemetryTransportParams): Promise<void> {
- logger.debug(chalk.yellow('telemetry'), JSON.stringify(payload, null, 2));
- return Promise.resolve();
- }
-}
diff --git a/packages/core/src/telemetry/transports/BrowserTransport.ts b/packages/core/src/telemetry/transports/BrowserTransport.ts
deleted file mode 100644
index 9131ed77f..000000000
--- a/packages/core/src/telemetry/transports/BrowserTransport.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import { TelemetryTransportParams } from '../../types/telemetry.types';
-import logger from '../../utils/logger';
-import { BaseTelemetryTransport } from '../TelemetryTransport';
-export class BrowserTelemetryTransport implements BaseTelemetryTransport {
- send(payload: TelemetryTransportParams): Promise<void> {
- if (typeof window === 'undefined') {
- return Promise.reject(
- new Error('BrowserTelemetryTransport can only be used in browser environments')
- );
- }
-
- return new Promise(resolve => {
- try {
- // Create a new XMLHttpRequest object
- const xhr = new XMLHttpRequest();
- // Open a new POST request to the telemetry server
- xhr.open(payload.method, payload.url, true);
- // Set the request header to indicate JSON content
- xhr.setRequestHeader('Content-Type', 'application/json');
- Object.entries(payload.headers || {}).forEach(([key, value]) => {
- xhr.setRequestHeader(key, value);
- });
-
- // Define the onload event handler
- xhr.onload = function () {
- // Log the response if the request was successful
- // if (xhr.status === 200) {
- // logger.debug(xhr.response);
- // }
- resolve();
- };
-
- // Send the reporting payload as a JSON string
- xhr.send(JSON.stringify(payload.data));
- } catch (error) {
- logger.error('Error sending error to telemetry', error);
- // logger.debug("Error sending error to telemetry", error);
- // DO NOTHING
- resolve();
- }
- });
- }
-}
diff --git a/packages/core/src/telemetry/transports/ProcessTransport.ts b/packages/core/src/telemetry/transports/ProcessTransport.ts
deleted file mode 100644
index f5bd64328..000000000
--- a/packages/core/src/telemetry/transports/ProcessTransport.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-import chalk from 'chalk';
-import { TelemetryTransportParams } from '../../types/telemetry.types';
-import logger from '../../utils/logger';
-import { BaseTelemetryTransport } from '../TelemetryTransport';
-export class ProcessTelemetryTransport implements BaseTelemetryTransport {
- send(payload: TelemetryTransportParams): Promise<void> {
- if (typeof window !== 'undefined') {
- return Promise.reject(
- new Error('ProcessTelemetryTransport can only be used in Node.js environments')
- );
- }
-
- return new Promise(resolve => {
- try {
- // use fetch to send the telemetry
-
- const url = new URL(payload.url);
- const protocol = url.protocol === 'https:' ? 'https' : 'http';
- const port = url.port || (url.protocol === 'https:' ? 443 : 80);
-
- const args = [
- '-e',
- `
- const http = require('${protocol}');
- const options = {
- hostname: '${url.hostname}',
- path: '${url.pathname}${url.search}',
- port: ${port},
- method: '${payload.method}',
- headers: ${JSON.stringify(payload.headers)}
- };
-
- const req = http.request(options, (res) => {
- res.on('data', () => {});
- res.on('end', () => {
- process.exit(0);
- });
- });
-
- req.on('error', () => {
- process.exit(0);
- });
-
- req.write(JSON.stringify(${JSON.stringify(payload.data)}));
- req.end();
- `,
- ];
-
- import('child_process')
- .then(({ spawn }) => {
- spawn('node', args, {
- stdio: 'ignore',
- detached: false,
- shell: false,
- }).unref();
- resolve();
- })
- .catch(error => {
- logger.error('Error sending telemetry', error);
- resolve();
- });
- logger.debug(chalk.yellow('Process Telemetry'), JSON.stringify(payload, null, 2));
- } catch (error) {
- logger.error('Error sending telemetry', error);
- resolve();
- }
- });
- }
-}
diff --git a/packages/core/src/types/telemetry.types.ts b/packages/core/src/types/telemetry.types.ts
index 478e055c8..9ee5e0251 100644
--- a/packages/core/src/types/telemetry.types.ts
+++ b/packages/core/src/types/telemetry.types.ts
@@ -17,6 +17,7 @@ export type TelemetryMetadata = {
frameworkRuntime: string;
isAgentic: boolean;
source: string;
+ provider?: string;
transport?: string;
sdkType?: string;
sessionId?: string;
diff --git a/packages/core/src/types/tool.types.ts b/packages/core/src/types/tool.types.ts
index a7d016fb9..222830af2 100644
--- a/packages/core/src/types/tool.types.ts
+++ b/packages/core/src/types/tool.types.ts
@@ -29,6 +29,9 @@ const JSONSchemaProperty: z.ZodType<unknown> = z.object({
type: 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(),
title: z.string().optional(),
default: z.any().optional(),
properties: z.lazy(() => z.record(z.string(), JSONSchemaProperty)).optional(),
@@ -40,6 +43,9 @@ const JSONSchemaProperty: z.ZodType<unknown> = z.object({
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),
required: z.array(z.string()).optional(),
title: z.string().optional(),
diff --git a/packages/core/src/types/toolkit.types.ts b/packages/core/src/types/toolkit.types.ts
index 35ce8aaea..085f63d64 100644
--- a/packages/core/src/types/toolkit.types.ts
+++ b/packages/core/src/types/toolkit.types.ts
@@ -1,4 +1,5 @@
import { z } from 'zod';
+import { AuthSchemeEnum } from './authConfigs.types';
/**
* Toolkit list params
@@ -112,3 +113,14 @@ export type ToolkitCategory = z.infer<typeof ToolkitCategorySchema>;
export type ToolkitRetrieveCategoriesResponse = z.infer<
typeof ToolkitRetrieveCategoriesResponseSchema
>;
+
+export const ToolkitAuthFieldsResponseSchema = z.object({
+ authScheme: AuthSchemeEnum,
+ fields: z.array(
+ ToolkitAuthFieldSchema.extend({
+ required: z.boolean().optional(),
+ })
+ ),
+});
+
+export type ToolkitAuthFieldsResponse = z.infer<typeof ToolkitAuthFieldsResponseSchema>;
diff --git a/packages/core/src/utils/jsonSchema.ts b/packages/core/src/utils/jsonSchema.ts
index fd404fc3e..b6f182987 100644
--- a/packages/core/src/utils/jsonSchema.ts
+++ b/packages/core/src/utils/jsonSchema.ts
@@ -82,7 +82,8 @@ export function jsonSchemaToZodSchema(
);
}
// Convert the JSON schema properties to Zod schema
- return jsonSchemaToZod(schema) as z.ZodType;
+ const zodSchema = jsonSchemaToZod(schema) as z.ZodTypeAny;
+ return zodSchema;
} catch (error) {
throw new JsonSchemaToZodError('Failed to convert JSON Schema to Zod Schema', {
cause: error,
diff --git a/packages/core/test/models/toolkits.test.ts b/packages/core/test/models/toolkits.test.ts
index fc4febc66..f29c7662b 100644
--- a/packages/core/test/models/toolkits.test.ts
+++ b/packages/core/test/models/toolkits.test.ts
@@ -1,11 +1,9 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { Toolkits } from '../../src/models/toolkits';
+import { Toolkits } from '../../src/models/Toolkits';
import ComposioClient from '@composio/client';
-import { ValidationError } from '../../src/errors/ValidationErrors';
-import { ComposioToolkitFetchError, ComposioToolkitNotFoundError } from '../../src/errors';
import { telemetry } from '../../src/telemetry/Telemetry';
-import { ConnectionRequest } from '../../src/models/ConnectionRequest';
import { ComposioAuthConfigNotFoundError } from '../../src/errors/AuthConfigErrors';
+import { AuthSchemeTypes } from '../../src/types/authConfigs.types';
// Mock dependencies
vi.mock('../../src/telemetry/Telemetry', () => ({
@@ -446,4 +444,208 @@ describe('Toolkits', () => {
expect(mockClient.connectedAccounts.initiate).not.toHaveBeenCalled();
});
});
+
+ describe('getAuthConfigCreationFields', () => {
+ const toolkitSlug = 'test-toolkit';
+ const mockToolkit = {
+ authConfigDetails: [
+ {
+ name: 'oauth2',
+ mode: 'OAUTH2',
+ fields: {
+ authConfigCreation: {
+ required: [{ name: 'client_id' }, { name: 'client_secret' }],
+ optional: [{ name: 'redirect_uri' }],
+ },
+ connectedAccountInitiation: {
+ required: [{ name: 'code' }],
+ optional: [{ name: 'state' }],
+ },
+ },
+ },
+ ],
+ };
+
+ beforeEach(() => {
+ vi.spyOn(Toolkits.prototype as any, 'getToolkitBySlug').mockResolvedValue(mockToolkit as any);
+ });
+
+ it('returns all fields when requiredOnly is false', async () => {
+ const result = await toolkits.getAuthConfigCreationFields(toolkitSlug, {
+ requiredOnly: false,
+ });
+ expect(result).toEqual({
+ authScheme: 'OAUTH2',
+ fields: [
+ { name: 'client_id', required: true },
+ { name: 'client_secret', required: true },
+ { name: 'redirect_uri', required: false },
+ ],
+ });
+ });
+
+ it('returns only required fields when requiredOnly is true', async () => {
+ const result = await toolkits.getAuthConfigCreationFields(toolkitSlug, {
+ requiredOnly: true,
+ });
+ expect(result).toEqual({
+ authScheme: 'OAUTH2',
+ fields: [
+ { name: 'client_id', required: true },
+ { name: 'client_secret', required: true },
+ ],
+ });
+ });
+
+ it('selects the correct auth config by authScheme', async () => {
+ const multiToolkit = {
+ authConfigDetails: [
+ {
+ name: 'oauth2',
+ mode: 'OAUTH2',
+ fields: {
+ authConfigCreation: {
+ required: [{ name: 'client_id' }],
+ optional: [],
+ },
+ connectedAccountInitiation: {
+ required: [],
+ optional: [],
+ },
+ },
+ },
+ {
+ name: 'api_key',
+ mode: 'API_KEY',
+ fields: {
+ authConfigCreation: {
+ required: [{ name: 'api_key' }],
+ optional: [],
+ },
+ connectedAccountInitiation: {
+ required: [],
+ optional: [],
+ },
+ },
+ },
+ ],
+ };
+ (Toolkits.prototype as any).getToolkitBySlug = vi.fn().mockResolvedValueOnce(multiToolkit);
+ const result = await toolkits.getAuthConfigCreationFields(toolkitSlug, {
+ authScheme: AuthSchemeTypes.API_KEY,
+ requiredOnly: true,
+ });
+ expect(result).toEqual({
+ authScheme: 'API_KEY',
+ fields: [{ name: 'api_key', required: true }],
+ });
+ });
+
+ it('throws if no authConfigDetails', async () => {
+ (Toolkits.prototype as any).getToolkitBySlug = vi.fn().mockResolvedValueOnce({});
+ await expect(
+ toolkits.getAuthConfigCreationFields(toolkitSlug, { requiredOnly: true })
+ ).rejects.toThrow(ComposioAuthConfigNotFoundError);
+ });
+ });
+
+ describe('getConnectedAccountInitiationFields', () => {
+ const toolkitSlug = 'test-toolkit';
+ const mockToolkit = {
+ authConfigDetails: [
+ {
+ name: 'oauth2',
+ mode: 'OAUTH2',
+ fields: {
+ authConfigCreation: {
+ required: [],
+ optional: [],
+ },
+ connectedAccountInitiation: {
+ required: [{ name: 'code' }],
+ optional: [{ name: 'state' }],
+ },
+ },
+ },
+ ],
+ };
+
+ beforeEach(() => {
+ vi.spyOn(Toolkits.prototype as any, 'getToolkitBySlug').mockResolvedValue(mockToolkit as any);
+ });
+
+ it('returns all fields when requiredOnly is false', async () => {
+ const result = await toolkits.getConnectedAccountInitiationFields(toolkitSlug, {
+ requiredOnly: false,
+ });
+ expect(result).toEqual({
+ authScheme: 'OAUTH2',
+ fields: [
+ { name: 'code', required: true },
+ { name: 'state', required: false },
+ ],
+ });
+ });
+
+ it('returns only required fields when requiredOnly is true', async () => {
+ const result = await toolkits.getConnectedAccountInitiationFields(toolkitSlug, {
+ requiredOnly: true,
+ });
+ expect(result).toEqual({
+ authScheme: 'OAUTH2',
+ fields: [{ name: 'code', required: true }],
+ });
+ });
+
+ it('selects the correct auth config by authScheme', async () => {
+ const multiToolkit = {
+ authConfigDetails: [
+ {
+ name: 'oauth2',
+ mode: 'OAUTH2',
+ fields: {
+ authConfigCreation: {
+ required: [],
+ optional: [],
+ },
+ connectedAccountInitiation: {
+ required: [{ name: 'code' }],
+ optional: [],
+ },
+ },
+ },
+ {
+ name: 'api_key',
+ mode: 'API_KEY',
+ fields: {
+ authConfigCreation: {
+ required: [],
+ optional: [],
+ },
+ connectedAccountInitiation: {
+ required: [{ name: 'api_key_code' }],
+ optional: [],
+ },
+ },
+ },
+ ],
+ };
+ (Toolkits.prototype as any).getToolkitBySlug = vi.fn().mockResolvedValueOnce(multiToolkit);
+ const result = await toolkits.getConnectedAccountInitiationFields(toolkitSlug, {
+ authScheme: AuthSchemeTypes.API_KEY,
+ requiredOnly: true,
+ });
+ expect(result).toEqual({
+ authScheme: 'API_KEY',
+ fields: [{ name: 'api_key_code', required: true }],
+ });
+ });
+
+ it('throws if no authConfigDetails', async () => {
+ (Toolkits.prototype as any).getToolkitBySlug = vi.fn().mockResolvedValueOnce({});
+ await expect(
+ toolkits.getConnectedAccountInitiationFields(toolkitSlug, { requiredOnly: true })
+ ).rejects.toThrow(ComposioAuthConfigNotFoundError);
+ });
+ });
});
diff --git a/packages/core/test/telemetry/BatchProcessor.test.ts b/packages/core/test/telemetry/BatchProcessor.test.ts
deleted file mode 100644
index 012a24c8d..000000000
--- a/packages/core/test/telemetry/BatchProcessor.test.ts
+++ /dev/null
@@ -1,119 +0,0 @@
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { BatchProcessor } from '../../src/telemetry/BatchProcessor';
-
-describe('BatchProcessor', () => {
- // Mock timer functions
- vi.useFakeTimers();
-
- let batchProcessor: BatchProcessor;
- let processBatchCallback: ReturnType<typeof vi.fn>;
-
- beforeEach(() => {
- processBatchCallback = vi.fn().mockResolvedValue(undefined);
- batchProcessor = new BatchProcessor(1000, 3, processBatchCallback);
- });
-
- afterEach(() => {
- vi.clearAllTimers();
- vi.clearAllMocks();
- });
-
- it('should create an instance with default parameters', () => {
- const defaultProcessor = new BatchProcessor(undefined, undefined, processBatchCallback);
- expect(defaultProcessor).toBeInstanceOf(BatchProcessor);
- });
-
- it('should batch items and not process until batch size is reached', () => {
- batchProcessor.pushItem('item1');
- batchProcessor.pushItem('item2');
-
- expect(processBatchCallback).not.toHaveBeenCalled();
- });
-
- it('should process batch when the batch size is reached', () => {
- batchProcessor.pushItem('item1');
- batchProcessor.pushItem('item2');
- batchProcessor.pushItem('item3');
-
- expect(processBatchCallback).toHaveBeenCalledWith(['item1', 'item2', 'item3']);
- expect(processBatchCallback).toHaveBeenCalledTimes(1);
- });
-
- it('should process batch after the specified time', () => {
- batchProcessor.pushItem('item1');
- batchProcessor.pushItem('item2');
-
- expect(processBatchCallback).not.toHaveBeenCalled();
-
- // Fast-forward time by 1 second
- vi.advanceTimersByTime(1000);
-
- expect(processBatchCallback).toHaveBeenCalledWith(['item1', 'item2']);
- expect(processBatchCallback).toHaveBeenCalledTimes(1);
- });
-
- it('should reset the timer after processing batch due to size limit', () => {
- batchProcessor.pushItem('item1');
- batchProcessor.pushItem('item2');
- batchProcessor.pushItem('item3');
-
- expect(processBatchCallback).toHaveBeenCalledTimes(1);
- processBatchCallback.mockClear();
-
- // Add a new item, which should start a new timer
- batchProcessor.pushItem('item4');
-
- // Fast-forward time by 1 second
- vi.advanceTimersByTime(1000);
-
- expect(processBatchCallback).toHaveBeenCalledWith(['item4']);
- expect(processBatchCallback).toHaveBeenCalledTimes(1);
- });
-
- it('should not process an empty batch', () => {
- // No items added
- batchProcessor.processBatch();
-
- expect(processBatchCallback).not.toHaveBeenCalled();
- });
-
- it('should clear timer after manual processBatch call', () => {
- // Spy on setTimeout
- const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
-
- batchProcessor.pushItem('item1');
-
- // A timer should be set
- expect(setTimeoutSpy).toHaveBeenCalled();
-
- // Clear the spy call history
- setTimeoutSpy.mockClear();
-
- // Process the batch manually
- batchProcessor.processBatch();
-
- // Add a new item, which should start a new timer
- batchProcessor.pushItem('item2');
-
- // setTimeout should be called again
- expect(setTimeoutSpy).toHaveBeenCalled();
- });
-
- it('should handle batches efficiently', () => {
- // Create a batch processor with a moderate batch size
- const processor = new BatchProcessor(1000, 20, processBatchCallback);
-
- // Add 19 items
- for (let i = 0; i < 19; i++) {
- processor.pushItem(`item${i}`);
- }
-
- expect(processBatchCallback).not.toHaveBeenCalled();
-
- // Add the 20th item to trigger batch processing
- processor.pushItem('item-last');
-
- expect(processBatchCallback).toHaveBeenCalledTimes(1);
- expect(processBatchCallback.mock.calls[0][0].length).toBe(20);
- });
-});
diff --git a/packages/core/test/telemetry/BrowserTransport.test.ts b/packages/core/test/telemetry/BrowserTransport.test.ts
deleted file mode 100644
index 0023b3748..000000000
--- a/packages/core/test/telemetry/BrowserTransport.test.ts
+++ /dev/null
@@ -1,117 +0,0 @@
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { BrowserTelemetryTransport } from '../../src/telemetry/transports/BrowserTransport';
-import { TelemetryTransportParams } from '../../src/types/telemetry.types';
-
-describe('BrowserTelemetryTransport', () => {
- let transport: BrowserTelemetryTransport;
-
- // Define the mock XHR class
- const mockXHR = {
- open: vi.fn(),
- send: vi.fn(),
- setRequestHeader: vi.fn(),
- onload: null as Function | null,
- };
-
- // Save original globals
- const originalWindow = global.window;
- const originalXMLHttpRequest = global.XMLHttpRequest;
-
- beforeEach(() => {
- // Setup mock window and XMLHttpRequest
- global.window = global.window || ({} as any);
- global.XMLHttpRequest = vi.fn(() => mockXHR) as any;
-
- // Reset mock functions and state
- mockXHR.open.mockReset();
- mockXHR.send.mockReset();
- mockXHR.setRequestHeader.mockReset();
- mockXHR.onload = null;
-
- // Create new transport instance
- transport = new BrowserTelemetryTransport();
- });
-
- afterEach(() => {
- // Restore globals
- global.window = originalWindow;
- global.XMLHttpRequest = originalXMLHttpRequest;
- });
-
- it('should create an instance successfully', () => {
- expect(transport).toBeInstanceOf(BrowserTelemetryTransport);
- });
-
- it('should send telemetry data via XMLHttpRequest', async () => {
- const payload: TelemetryTransportParams = {
- url: 'https://example.com/telemetry',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-Custom-Header': 'test-value',
- },
- data: { key: 'value' },
- };
-
- // Start sending the data
- const promise = transport.send(payload);
-
- // Verify XHR was configured correctly
- expect(mockXHR.open).toHaveBeenCalledWith('POST', 'https://example.com/telemetry', true);
- expect(mockXHR.setRequestHeader).toHaveBeenCalledWith('Content-Type', 'application/json');
- expect(mockXHR.setRequestHeader).toHaveBeenCalledWith('X-Custom-Header', 'test-value');
- expect(mockXHR.send).toHaveBeenCalledWith(JSON.stringify({ key: 'value' }));
-
- // Trigger the onload callback if it was set
- if (mockXHR.onload) {
- (mockXHR.onload as Function)();
- }
-
- // Verify promise resolves
- await expect(promise).resolves.toBeUndefined();
- });
-
- it('should reject if not running in a browser environment', async () => {
- // Temporarily remove window to simulate non-browser environment
- const tempWindow = global.window;
- global.window = undefined as any;
-
- // Expect rejection when sending in non-browser env
- await expect(
- transport.send({
- url: 'https://example.com/telemetry',
- method: 'POST',
- headers: {},
- data: {},
- })
- ).rejects.toThrow('BrowserTelemetryTransport can only be used in browser environments');
-
- // Restore window for other tests
- global.window = tempWindow;
- });
-
- it('should handle errors gracefully', async () => {
- const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
-
- const payload: TelemetryTransportParams = {
- url: 'https://example.com/telemetry',
- method: 'POST',
- headers: {},
- data: {},
- };
-
- // Make the send method throw an error
- mockXHR.send.mockImplementation(() => {
- throw new Error('Network error');
- });
-
- // Should resolve despite the error
- const promise = transport.send(payload);
- await expect(promise).resolves.toBeUndefined();
-
- // Should log the error
- expect(consoleSpy).toHaveBeenCalled();
-
- consoleSpy.mockRestore();
- });
-});
diff --git a/packages/core/test/telemetry/ProcessTransport.test.ts b/packages/core/test/telemetry/ProcessTransport.test.ts
deleted file mode 100644
index fe9cd0a6c..000000000
--- a/packages/core/test/telemetry/ProcessTransport.test.ts
+++ /dev/null
@@ -1,91 +0,0 @@
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { ProcessTelemetryTransport } from '../../src/telemetry/transports/ProcessTransport';
-import { TelemetryTransportParams } from '../../src/types/telemetry.types';
-
-describe('ProcessTelemetryTransport', () => {
- let transport: ProcessTelemetryTransport;
- const originalWindow = global.window;
-
- // Spy on console.error to validate error handling
- const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
-
- beforeEach(() => {
- // Reset window to simulate Node.js environment
- delete (global as any).window;
-
- // Reset mocks and spies
- vi.clearAllMocks();
-
- // Create transport instance
- transport = new ProcessTelemetryTransport();
- });
-
- afterEach(() => {
- // Restore window
- global.window = originalWindow;
- });
-
- it('should create an instance successfully', () => {
- expect(transport).toBeInstanceOf(ProcessTelemetryTransport);
- });
-
- it('should reject if running in a browser environment', async () => {
- // Set window to simulate browser environment
- global.window = {} as any;
-
- await expect(
- transport.send({
- url: 'https://example.com/telemetry',
- method: 'POST',
- headers: {},
- data: {},
- })
- ).rejects.toThrow('ProcessTelemetryTransport can only be used in Node.js environments');
- });
-
- // Since we're running in a test environment and can't easily mock dynamic imports,
- // we'll focus on testing the URL handling and error handling aspects
-
- it('should handle errors gracefully', async () => {
- // Force an error by making URL constructor throw
- const originalURL = global.URL;
- global.URL = vi.fn().mockImplementation(() => {
- throw new Error('Invalid URL');
- }) as any;
-
- const payload: TelemetryTransportParams = {
- url: 'invalid:///url',
- method: 'POST',
- headers: {},
- data: {},
- };
-
- // Test that the promise still resolves despite the error
- await expect(transport.send(payload)).resolves.toBeUndefined();
-
- // Verify error was logged
- expect(consoleErrorSpy).toHaveBeenCalled();
-
- // Restore URL
- global.URL = originalURL;
- });
-
- // For these tests, we can't easily verify the spawn call since it's dynamically imported
- // and would require more complex mocking. Instead, we'll focus on ensuring the method
- // runs without throwing errors.
-
- it('should handle telemetry requests without throwing errors', async () => {
- const payload: TelemetryTransportParams = {
- url: 'https://example.com/telemetry',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-Custom-Header': 'test-value',
- },
- data: { key: 'value' },
- };
-
- // The test passes if no exception is thrown
- await expect(transport.send(payload)).resolves.toBeUndefined();
- });
-});
diff --git a/packages/core/test/telemetry/TelemetryService.test.ts b/packages/core/test/telemetry/TelemetryService.test.ts
index 473ad2ada..a7352fec5 100644
--- a/packages/core/test/telemetry/TelemetryService.test.ts
+++ b/packages/core/test/telemetry/TelemetryService.test.ts
@@ -1,292 +1,155 @@
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { TelemetryService } from '../../src/telemetry/Telemetry';
-import { BaseTelemetryTransport } from '../../src/telemetry/TelemetryTransport';
-import { BrowserTelemetryTransport } from '../../src/telemetry/transports/BrowserTransport';
-import { ProcessTelemetryTransport } from '../../src/telemetry/transports/ProcessTransport';
-import { TELEMETRY_EVENTS, TelemetryMetadata } from '../../src/types/telemetry.types';
-import * as envUtils from '../../src/utils/env';
-
-describe('TelemetryService', () => {
- let telemetryService: TelemetryService;
- let mockTransport: BaseTelemetryTransport;
- let testMetadata: TelemetryMetadata;
+import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
+import { BatchProcessor } from '../../src/telemetry/BatchProcessor';
+import { TelemetryTransport } from '../../src/telemetry/Telemetry';
+import { TelemetryService } from '../../src/services/telemetry/TelemetryService';
+import type {
+ TelemetryPayload,
+ TelemetryMetricPayloadBody,
+ TelemetryMetricMetadata,
+} from '../../src/services/telemetry/TelemetryService.types';
+import { TelemetryMetadata } from '../../src/types/telemetry.types';
+
+// Helper to create a mock telemetry payload
+const createPayload = (overrides: Partial<TelemetryPayload> = {}): TelemetryPayload => ({
+ functionName: 'TestClass.testMethod',
+ durationMs: 123,
+ timestamp: Date.now() / 1000,
+ props: { foo: 'bar' },
+ source: {
+ name: 'test',
+ service: 'sdk',
+ language: 'typescript',
+ version: '1.0.0',
+ platform: 'node',
+ },
+ metadata: { provider: 'test' },
+ ...overrides,
+});
- // Save original window
- const originalWindow = global.window;
+describe('BatchProcessor', () => {
+ let callback: ReturnType<typeof vi.fn>;
+ let processor: BatchProcessor;
beforeEach(() => {
- // Create a new instance for each test
- telemetryService = new TelemetryService();
-
- // Create mock transport
- mockTransport = {
- send: vi.fn().mockResolvedValue(undefined),
- };
-
- // Test metadata
- testMetadata = {
- apiKey: 'test-api-key',
- baseUrl: 'https://api.test.com',
- version: '1.0.0',
- frameworkRuntime: 'test-framework',
- isAgentic: false,
- source: 'test',
- sessionId: 'test-session-id',
- isBrowser: false,
- };
-
- // Reset window to simulate Node.js environment by default
- delete (global as any).window;
-
- // Mock environment variable
- vi.spyOn(envUtils, 'getEnvVariable').mockImplementation((key, defaultValue) => {
- if (key === 'TELEMETRY_DISABLED') return 'false';
- if (key === 'NODE_ENV') return 'production'; // Return 'production' instead of 'test' to allow telemetry
- return defaultValue || '';
- });
+ callback = vi.fn();
+ processor = new BatchProcessor(50, 3, callback); // small batch size and time for test
});
afterEach(() => {
- vi.clearAllMocks();
-
- // Restore window
- if (originalWindow !== undefined) {
- global.window = originalWindow;
- }
+ vi.useRealTimers();
});
- describe('setup', () => {
- it('should set up telemetry metadata', () => {
- telemetryService.setup(testMetadata, mockTransport);
-
- expect(mockTransport.send).toHaveBeenCalledWith(
- expect.objectContaining({
- data: expect.arrayContaining([
- expect.objectContaining({
- eventName: TELEMETRY_EVENTS.SDK_INITIALIZED,
- sdk_meta: testMetadata,
- }),
- ]),
- })
- );
- });
-
- it('should use BrowserTelemetryTransport in browser environment', () => {
- // Mock window to simulate browser environment
- global.window = {} as any;
-
- // Create a proper mock transport with a send method
- const mockBrowserTransport = { send: vi.fn().mockResolvedValue(undefined) };
-
- // Spy on the BrowserTelemetryTransport constructor
- vi.spyOn(BrowserTelemetryTransport.prototype, 'send').mockImplementation(() =>
- Promise.resolve()
- );
-
- // Setup with the mock transport
- telemetryService.setup(testMetadata, mockBrowserTransport);
-
- // Send a test event to verify the transport
- telemetryService['sendTelemetry']([
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {},
- },
- ]);
-
- expect(mockBrowserTransport.send).toHaveBeenCalled();
- });
+ it('should batch items and call callback when batch size is reached', () => {
+ const payloads = [createPayload(), createPayload(), createPayload()];
+ payloads.forEach(p => processor.pushItem(p));
+ expect(callback).toHaveBeenCalledOnce();
+ expect(callback).toHaveBeenCalledWith(payloads);
+ });
- it('should use ProcessTelemetryTransport in Node.js environment', () => {
- // Create a proper mock transport with a send method
- const mockProcessTransport = { send: vi.fn().mockResolvedValue(undefined) };
+ it('should call callback after timeout if batch size not reached', async () => {
+ vi.useFakeTimers();
+ const payloads = [createPayload(), createPayload()];
+ payloads.forEach(p => processor.pushItem(p));
+ expect(callback).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(60);
+ expect(callback).toHaveBeenCalledOnce();
+ expect(callback).toHaveBeenCalledWith(payloads);
+ });
- // Spy on the ProcessTelemetryTransport constructor
- vi.spyOn(ProcessTelemetryTransport.prototype, 'send').mockImplementation(() =>
- Promise.resolve()
- );
+ it('should reset timer and batch after processing', () => {
+ const payloads = [createPayload(), createPayload(), createPayload()];
+ payloads.forEach(p => processor.pushItem(p));
+ // After batch, timer should be null and batch empty
+ // @ts-expect-error: private access for test
+ expect(processor.timer).toBeNull();
+ // @ts-expect-error: private access for test
+ expect(processor.batch).toEqual([]);
+ });
+});
- // Setup with the mock transport
- telemetryService.setup(testMetadata, mockProcessTransport);
+describe('TelemetryTransport', () => {
+ let transport: TelemetryTransport;
+ let sendMetricSpy: any;
+ let sendErrorLogSpy: any;
+ const metadata: TelemetryMetadata = {
+ apiKey: 'test-key',
+ baseUrl: 'http://localhost',
+ version: '1.0.0',
+ frameworkRuntime: 'node',
+ isAgentic: false,
+ source: 'test-source',
+ provider: 'test',
+ };
- // Send a test event to verify the transport
- telemetryService['sendTelemetry']([
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {},
- },
- ]);
+ beforeEach(() => {
+ sendMetricSpy = vi.spyOn(TelemetryService, 'sendMetric').mockResolvedValue({} as any);
+ sendErrorLogSpy = vi.spyOn(TelemetryService, 'sendErrorLog').mockResolvedValue({} as any);
+ transport = new TelemetryTransport();
+ transport.setup(metadata);
+ });
- expect(mockProcessTransport.send).toHaveBeenCalled();
- });
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
- it('should use custom transport when provided', () => {
- telemetryService.setup(testMetadata, mockTransport);
+ it('should send SDK_INITIALIZED metric on setup', () => {
+ expect(sendMetricSpy).toHaveBeenCalled();
+ const call = sendMetricSpy.mock.calls[0][0][0];
+ expect(call.functionName).toBeDefined();
+ expect(call.metadata?.provider).toBe('test');
+ });
- // Send a test event to verify the transport
- telemetryService['sendTelemetry']([
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {},
- },
- ]);
+ it('should batch metrics and send via TelemetryService', async () => {
+ const payload = createPayload();
+ await transport.sendMetric([payload]);
+ expect(sendMetricSpy).toHaveBeenCalledWith([payload]);
+ });
- expect(mockTransport.send).toHaveBeenCalledTimes(2); // Once during setup and once for the test
- });
+ it('should send error telemetry immediately', async () => {
+ const payload = createPayload({ error: { name: 'Error', message: 'fail', stack: 'stack' } });
+ await transport.sendErrorTelemetry(payload);
+ expect(sendErrorLogSpy).toHaveBeenCalledWith(payload);
});
- describe('instrument', () => {
+ it('should instrument async methods and batch telemetry', async () => {
class TestClass {
- constructor() {}
-
- async testMethod(param1: string, param2: number) {
- return { result: `${param1}-${param2}` };
- }
-
- async failingMethod() {
- throw new Error('Test error');
+ async foo(a: number) {
+ return a * 2;
}
+ }
+ const instance = new TestClass();
+ const instrumented = transport.instrument(instance);
+ await instrumented.foo(21);
+ // The batchProcessor will push the payload, but we can check sendMetric is eventually called
+ await transport.sendMetric([]); // flush
+ expect(sendMetricSpy).toHaveBeenCalled();
+ });
- synchronousMethod() {
- return 'sync result';
+ it('should send error telemetry if instrumented method throws', async () => {
+ class TestClass {
+ async fail() {
+ throw new Error('fail!');
}
}
-
- beforeEach(() => {
- telemetryService.setup(testMetadata, mockTransport);
- vi.spyOn(telemetryService as any, 'sendErrorTelemetry');
- });
-
- it('should instrument async methods of an object', async () => {
- const testInstance = new TestClass();
- telemetryService.instrument(testInstance);
-
- // Reset the mock to clear the SDK_INITIALIZED call
- vi.clearAllMocks();
-
- await testInstance.testMethod('test', 123);
-
- // Manually process the batch
- (telemetryService as any).batchProcessor.processBatch();
-
- expect(mockTransport.send).toHaveBeenCalledWith(
- expect.objectContaining({
- data: expect.arrayContaining([
- expect.objectContaining({
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: expect.objectContaining({
- fileName: 'TestClass',
- method: 'testMethod',
- params: ['test', 123],
- }),
- sdk_meta: testMetadata,
- }),
- ]),
- })
- );
- });
- // @TODO: fix this test
- // it('should capture errors from instrumented methods', async () => {
- // const testInstance = new TestClass();
- // telemetryService.instrument(testInstance);
-
- // // Reset the mock to clear the SDK_INITIALIZED call
- // vi.clearAllMocks();
- // // vi.spyOn(telemetryService, 'sendErrorTelemetry').mockImplementation(() => Promise.resolve());
-
- // try {
- // await testInstance.failingMethod();
- // } catch (error) {
- // // Wait for any pending promises to resolve
- // expect(telemetryService.sendErrorTelemetry).toHaveBeenCalledWith(
- // expect.objectContaining({
- // data: expect.arrayContaining([
- // expect.objectContaining({
- // eventName: TELEMETRY_EVENTS.SDK_METHOD_ERROR,
- // data: expect.objectContaining({
- // fileName: 'TestClass',
- // method: 'failingMethod',
- // error: expect.objectContaining({
- // message: 'Test error',
- // }),
- // }),
- // sdk_meta: testMetadata,
- // }),
- // ]),
- // })
- // );
- // }
- // });
-
- it('should use the filename if constructor name is not available', () => {
- const anonymousObject = {
- async testMethod() {
- return 'result';
- },
- };
-
- telemetryService.instrument(anonymousObject, 'custom-filename');
-
- // Verify the instrumentation works with the custom filename
- expect(anonymousObject.testMethod).not.toBe(undefined);
- expect(typeof anonymousObject.testMethod).toBe('function');
- });
+ const instance = new TestClass();
+ const instrumented = transport.instrument(instance);
+ await expect(instrumented.fail()).rejects.toThrow('fail!');
+ expect(sendErrorLogSpy).toHaveBeenCalled();
});
+});
- describe('sendTelemetry', () => {
- beforeEach(() => {
- telemetryService.setup(testMetadata, mockTransport);
- });
-
- it('should not send telemetry if transport is not set', async () => {
- // Create a new instance without setting up transport
- const newTelemetryService = new TelemetryService();
-
- // Reset the mock to clear any previous calls
- vi.clearAllMocks();
-
- await newTelemetryService['sendTelemetry']([
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {},
- },
- ]);
-
- expect(mockTransport.send).not.toHaveBeenCalled();
- });
-
- it('should not send telemetry if telemetry is disabled', async () => {
- // Mock environment variable to disable telemetry
- vi.spyOn(envUtils, 'getEnvVariable').mockImplementation(() => 'true');
-
- await telemetryService['sendTelemetry']([
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: {},
- },
- ]);
-
- // We expect only the SDK_INITIALIZED call during setup
- expect(mockTransport.send).toHaveBeenCalledTimes(1);
- });
-
- it('should send telemetry with correct payload format', async () => {
- const testPayload = [
- {
- eventName: TELEMETRY_EVENTS.SDK_METHOD_INVOKED,
- data: { test: 'data' },
- sdk_meta: testMetadata,
- },
- ];
-
- await telemetryService['sendTelemetry'](testPayload);
+beforeAll(() => {
+ // Save the original NODE_ENV
+ process.env._ORIGINAL_NODE_ENV = process.env.NODE_ENV;
+ process.env.NODE_ENV = 'development';
+});
- expect(mockTransport.send).toHaveBeenLastCalledWith({
- data: testPayload,
- url: expect.stringContaining('/api/sdk_metrics/telemetry'),
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- });
- });
- });
+afterAll(() => {
+ // Restore the original NODE_ENV
+ if (process.env._ORIGINAL_NODE_ENV !== undefined) {
+ process.env.NODE_ENV = process.env._ORIGINAL_NODE_ENV;
+ delete process.env._ORIGINAL_NODE_ENV;
+ } else {
+ delete process.env.NODE_ENV;
+ }
});
diff --git a/packages/json-schema-to-zod/src/parsers/parse-array.ts b/packages/json-schema-to-zod/src/parsers/parse-array.ts
index 8b8b770a6..564e40c07 100644
--- a/packages/json-schema-to-zod/src/parsers/parse-array.ts
+++ b/packages/json-schema-to-zod/src/parsers/parse-array.ts
@@ -4,6 +4,7 @@ import { parseSchema } from './parse-schema';
import type { JsonSchemaObject, Refs, JsonSchema } from '../types';
import { extendSchemaWithMessage } from '../utils/extend-schema';
import { its } from '../utils/its';
+import { JSONSchema7TypeName } from 'json-schema';
type MetadataFields =
| keyof Pick<JsonSchemaObject, 'default' | 'description' | 'title'>
@@ -30,7 +31,7 @@ export const parseArray = (jsonSchema: JsonSchemaObject & { type: 'array' }, ref
// Create new schema with combined types
const newSchema: JsonSchemaObject = {
- type: Array.from(types),
+ type: Array.from(types) as JSONSchema7TypeName[],
...(items && { items }),
};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e95964769..6140bdbb0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -146,6 +146,25 @@ importers:
specifier: ^5.8.3
version: 5.8.3
+ examples/json-schema-to-zod:
+ dependencies:
+ '@composio/core':
+ specifier: workspace:*
+ version: link:../../packages/core
+ dotenv:
+ specifier: ^16.4.1
+ version: 16.5.0
+ zod-to-json-schema:
+ specifier: ^3.24.5
+ version: 3.24.5([email protected])
+ devDependencies:
+ '@types/bun':
+ specifier: ^1.2.9
+ version: 1.2.16
+ typescript:
+ specifier: ^5.8.3
+ version: 5.8.3
+
examples/langchain:
dependencies:
'@composio/core':
@@ -408,8 +427,8 @@ importers:
packages/core:
dependencies:
'@composio/client':
- specifier: 0.1.0-alpha.17
- version: 0.1.0-alpha.17
+ specifier: 0.1.0-alpha.22
+ version: 0.1.0-alpha.22
'@composio/json-schema-to-zod':
specifier: 0.1.6
version: 0.1.6
@@ -854,8 +873,8 @@ packages:
'@cloudflare/[email protected]':
resolution: {integrity: sha512-3VsbEh0nqOWWH+jsJ2W41Ty6qlN1jQ+4R3lBA3gPor0U6LB3e4OA04jg7wyCyJmikBN6KsBcPRp3kj0es/9q2w==}
- '@composio/[email protected]':
- resolution: {integrity: sha512-iNCDSgdwfFVsjbbyZIMknMPy4uO8Xw5+MP2aI+yoQo34VXF17Fi6ZHYirrJPf8RbdoRROZTDJjC/rU18mMzC2Q==}
+ '@composio/[email protected]':
+ resolution: {integrity: sha512-b9KCO5gy0EsCG+nXikOdm17PiZZ85rw3vwu9aNRIse9rEj2FA1SScpP+P9GN4G0lQHl+QPyDuVvGa6U9AATFeg==}
'@composio/[email protected]':
resolution: {integrity: sha512-Eso0rvvdCAxgcjejXXdyJsG4jB3ScUaxkrMaytG4B9Y6uwKJ/uxFrzWqRcwMl5wwQ7nd1vtpJUW/nzr8iRcQMg==}
@@ -5248,7 +5267,7 @@ snapshots:
'@cloudflare/[email protected]': {}
- '@composio/[email protected]': {}
+ '@composio/[email protected]': {}
'@composio/[email protected]':
dependencies:
@@ -7028,7 +7047,7 @@ snapshots:
sirv: 3.0.1
tinyglobby: 0.2.14
tinyrainbow: 2.0.0
'@vitest/[email protected]':
dependencies:
diff --git a/ts/docs/api/toolkits.md b/ts/docs/api/toolkits.md
index b721e738e..22cabeac2 100644
--- a/ts/docs/api/toolkits.md
+++ b/ts/docs/api/toolkits.md
@@ -33,12 +33,12 @@ const allToolkits = await composio.toolkits.get({});
// Get toolkits by category
const devToolkits = await composio.toolkits.get({
- category: 'developer-tools'
+ category: 'developer-tools',
});
// Get local toolkits
const localToolkits = await composio.toolkits.get({
- isLocal: true
+ isLocal: true,
});
```
@@ -79,16 +79,121 @@ console.log(connectionRequest.redirectUrl); // URL to redirect the user for auth
**Returns:** Promise<ConnectionRequest> - The connection request object
+### getAuthConfigCreationFields(toolkitSlug, options)
+
+Retrieves the fields required for creating an auth config for a toolkit.
+
+```typescript
+// Get all fields for creating an auth config
+const fields = await composio.toolkits.getAuthConfigCreationFields('github');
+
+// Get only required fields for a specific auth scheme
+const requiredFields = await composio.toolkits.getAuthConfigCreationFields('github', {
+ authScheme: 'OAUTH2',
+ requiredOnly: true,
+});
+```
+
+Example Response:
+
+```json
+{
+ "authScheme": "OAUTH2",
+ "fields": [
+ {
+ "name": "client_id",
+ "displayName": "Client id",
+ "type": "string",
+ "required": true
+ },
+ {
+ "name": "client_secret",
+ "displayName": "Client secret",
+ "type": "string",
+ "required": true
+ },
+ {
+ "name": "oauth_redirect_uri",
+ "displayName": "Redirect URI",
+ "type": "string",
+ "default": "https://backend.composio.dev/api/v1/auth-apps/add",
+ "required": false
+ },
+ {
+ "name": "scopes",
+ "displayName": "Scopes",
+ "type": "string",
+ "default": "read_products,write_products,read_orders,write_orders",
+ "required": false
+ }
+ ]
+}
+```
+
+**Parameters:**
+
+- `toolkitSlug` (string): The slug of the toolkit to retrieve the fields for
+- `options` (object, optional):
+ - `authScheme` (string, optional): The auth scheme to retrieve the fields for (e.g., 'OAUTH2', 'API_KEY')
+ - `requiredOnly` (boolean, optional): Whether to only return the required fields (default: false)
+
+**Returns:** Promise<ToolkitAuthFieldsResponse> - The authschem and fields required for creating an auth config
+
+**Throws:** ComposioAuthConfigNotFoundError if no auth config is found for the toolkit or the specified auth scheme
+
+### getConnectedAccountInitiationFields(toolkitSlug, options)
+
+Retrieves the fields required for initiating a connected account for a toolkit.
+
+```typescript
+// Get all fields for initiating a connected account
+const fields = await composio.toolkits.getConnectedAccountInitiationFields('github');
+
+// Get only required fields for a specific auth scheme
+const requiredFields = await composio.toolkits.getConnectedAccountInitiationFields('github', {
+ authScheme: 'OAUTH2',
+ requiredOnly: true,
+});
+```
+
+Example Response:
+
+```json
+{
+ "authScheme": "OAUTH2",
+ "fields": [
+ {
+ "name": "shop",
+ "displayName": "Store Subdomain",
+ "type": "string",
+ "default": null,
+ "required": true
+ }
+ ]
+}
+```
+
+**Parameters:**
+
+- `toolkitSlug` (string): The slug of the toolkit to retrieve the fields for
+- `options` (object, optional):
+ - `authScheme` (string, optional): The auth scheme to retrieve the fields for (e.g., 'OAUTH2', 'API_KEY')
+ - `requiredOnly` (boolean, optional): Whether to only return the required fields (default: false)
+
+**Returns:** Promise<ToolkitAuthFieldsResponse> - The auth schemes and fields required for initiating a connected account
+
+**Throws:** ComposioAuthConfigNotFoundError if no auth config is found for the toolkit or the specified auth scheme
+
## Types
### ToolkitListParams
```typescript
interface ToolkitListParams {
- category?: string; // Filter by category
- isLocal?: boolean; // Filter for local toolkits
- managedBy?: string; // Filter by manager
- sortBy?: string; // Sort results by this field
+ category?: string; // Filter by category
+ isLocal?: boolean; // Filter for local toolkits
+ managedBy?: string; // Filter by manager
+ sortBy?: string; // Sort results by this field
}
```
@@ -96,9 +201,9 @@ interface ToolkitListParams {
```typescript
interface ToolKitListResponse {
- items: ToolKitItem[]; // List of toolkits
+ items: ToolKitItem[]; // List of toolkits
nextCursor?: string | null; // Pagination cursor
- totalPages: number; // Total number of pages
+ totalPages: number; // Total number of pages
}
```
@@ -106,18 +211,19 @@ interface ToolKitListResponse {
```typescript
interface ToolKitItem {
- name: string; // Name of the toolkit
- slug: string; // Unique slug identifier
- meta: { // Metadata about the toolkit
- createdAt: string; // When the toolkit was created
- updatedAt: string; // When the toolkit was last updated
- toolsCount: number; // Number of tools in the toolkit
- triggersCount: number; // Number of triggers in the toolkit
+ name: string; // Name of the toolkit
+ slug: string; // Unique slug identifier
+ meta: {
+ // Metadata about the toolkit
+ createdAt: string; // When the toolkit was created
+ updatedAt: string; // When the toolkit was last updated
+ toolsCount: number; // Number of tools in the toolkit
+ triggersCount: number; // Number of triggers in the toolkit
};
- isLocalToolkit: boolean; // Whether this is a local toolkit
- authSchemes: string[]; // Available authentication schemes
+ isLocalToolkit: boolean; // Whether this is a local toolkit
+ authSchemes: string[]; // Available authentication schemes
composioManagedAuthSchemes: string[]; // Managed auth schemes
- noAuth: boolean; // Whether authentication is required
+ noAuth: boolean; // Whether authentication is required
}
```
@@ -125,25 +231,29 @@ interface ToolKitItem {
```typescript
interface ToolkitRetrieveResponse {
- name: string; // Name of the toolkit
- slug: string; // Unique slug identifier
- meta: { // Metadata about the toolkit
- createdAt: string; // When the toolkit was created
- updatedAt: string; // When the toolkit was last updated
- toolsCount: number; // Number of tools in the toolkit
- triggersCount: number; // Number of triggers in the toolkit
+ name: string; // Name of the toolkit
+ slug: string; // Unique slug identifier
+ meta: {
+ // Metadata about the toolkit
+ createdAt: string; // When the toolkit was created
+ updatedAt: string; // When the toolkit was last updated
+ toolsCount: number; // Number of tools in the toolkit
+ triggersCount: number; // Number of triggers in the toolkit
};
- isLocalToolkit: boolean; // Whether this is a local toolkit
+ isLocalToolkit: boolean; // Whether this is a local toolkit
composioManagedAuthSchemes: string[]; // Managed auth schemes
- authConfigDetails: { // Authentication configuration details
- name: string; // Name of the auth configuration
- mode: string; // Auth mode (e.g., "OAUTH2")
- fields: { // Required fields
- authConfigCreation: Record<string, unknown>; // Fields for creating auth config
+ authConfigDetails: {
+ // Authentication configuration details
+ name: string; // Name of the auth configuration
+ mode: string; // Auth mode (e.g., "OAUTH2")
+ fields: {
+ // Required fields
+ authConfigCreation: Record<string, unknown>; // Fields for creating auth config
connectedAccountInitiation: Record<string, unknown>; // Fields for initiating connection
};
- proxy?: { // Proxy configuration (if any)
- baseUrl: string; // Base URL for proxied requests
+ proxy?: {
+ // Proxy configuration (if any)
+ baseUrl: string; // Base URL for proxied requests
};
}[];
}
@@ -155,7 +265,7 @@ interface ToolkitRetrieveResponse {
interface ToolkitRetrieveCategoriesResponse {
items: ToolkitCategory[]; // List of categories
nextCursor?: string | null; // Pagination cursor
- totalPages: number; // Total number of pages
+ totalPages: number; // Total number of pages
}
```
@@ -163,7 +273,7 @@ interface ToolkitRetrieveCategoriesResponse {
```typescript
interface ToolkitCategory {
- id: string; // Category ID
- name: string; // Category name
+ id: string; // Category ID
+ name: string; // Category name
}
-```
\ No newline at end of file
+```