Skip to content

Commit

Permalink
fixed account fetching logic (#904)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Enhances `executeAction` in `base.toolset.ts` to fetch
`connectedAccountId` if missing, with formatting improvements and
version update.
> 
>   - **Behavior**:
> - In `ComposioToolSet.executeAction()`, added logic to fetch
`connectedAccountId` if not provided, using
`client.connectedAccounts.list()`.
>   - **Formatting**:
> - Fixed spacing and alignment issues in `base.toolset.ts` for better
readability.
>   - **Versioning**:
> - Updated version in `package.json` from `0.2.9-9` to `0.2.9-10-1`.
>   - **Type Changes**:
> - Renamed `in` to `location` in `Parameter` type in `types.gen.ts`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=ComposioHQ%2Fcomposio&utm_source=github&utm_medium=referral)<sup>
for 76de321. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: Tushar Sadhwani <[email protected]>
  • Loading branch information
plxity and tushar-composio authored Nov 25, 2024
1 parent 6df16db commit 69d99f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "composio-core",
"version": "0.2.9-9",
"version": "0.2.9-10-1",
"description": "",
"main": "lib/src/index.js",
"scripts": {
Expand Down Expand Up @@ -82,4 +82,4 @@
"publishConfig": {
"access": "public"
}
}
}
69 changes: 41 additions & 28 deletions js/src/sdk/base.toolset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { WorkspaceConfig } from "../env/config";
import { Workspace } from "../env";
import logger from "../utils/logger";
import { CEG } from '../sdk/utils/error';
import { ExecuteActionResDTO } from "./client/types.gen";
import { saveFile } from "./utils/fileUtils";
import { ExecuteActionResDTO } from "./client/types.gen";
import { saveFile } from "./utils/fileUtils";
import { convertReqParams, converReqParamForActionExecution } from "./utils";
import { ActionRegistry, CreateActionOptions } from "./actionRegistry";
import { getUserDataJson } from "./utils/config";
Expand All @@ -35,18 +35,18 @@ export class ComposioToolSet {
runtime: string | null = null,
entityId: string = "default",
workspaceConfig: WorkspaceConfig = Workspace.Host()
) {
) {
const clientApiKey: string | undefined = apiKey || getEnvVariable("COMPOSIO_API_KEY") || getUserDataJson().api_key as string;
this.apiKey = clientApiKey;
this.client = new Composio(this.apiKey, baseUrl || undefined, runtime as string );
this.client = new Composio(this.apiKey, baseUrl || undefined, runtime as string);
this.customActionRegistry = new ActionRegistry(this.client);
this.runtime = runtime;
this.entityId = entityId;

if(!workspaceConfig.config.composioBaseURL) {
if (!workspaceConfig.config.composioBaseURL) {
workspaceConfig.config.composioBaseURL = baseUrl
}
if(!workspaceConfig.config.composioAPIKey) {
if (!workspaceConfig.config.composioAPIKey) {
workspaceConfig.config.composioAPIKey = apiKey;
}
this.workspace = new WorkspaceFactory(workspaceConfig.env, workspaceConfig);
Expand All @@ -63,13 +63,13 @@ export class ComposioToolSet {
async getExpectedParamsForUser(
params: { app?: string; integrationId?: string; entityId?: string; authScheme?: "OAUTH2" | "OAUTH1" | "API_KEY" | "BASIC" | "BEARER_TOKEN" | "BASIC_WITH_JWT" } = {},
) {
return this.client.getExpectedParamsForUser(params);
return this.client.getExpectedParamsForUser(params);
}

async setup() {
await this.workspace.new();

if(!this.localActions && this.workspaceEnv !== ExecEnv.HOST) {
if (!this.localActions && this.workspaceEnv !== ExecEnv.HOST) {
this.localActions = await (this.workspace.workspace as RemoteWorkspace).getLocalActionsSchema();
}
}
Expand All @@ -92,7 +92,7 @@ export class ComposioToolSet {
});
const uniqueLocalActions = Array.from(localActionsMap.values());
const _newActions = filters.actions?.map((action: string) => action.toLowerCase());
const toolsWithCustomActions = (await this.customActionRegistry.getActions({ actions: _newActions!})).filter((action: any) => {
const toolsWithCustomActions = (await this.customActionRegistry.getActions({ actions: _newActions! })).filter((action: any) => {
if (_newActions && !_newActions.includes(action.parameters.title.toLowerCase()!)) {
return false;
}
Expand All @@ -102,13 +102,13 @@ export class ComposioToolSet {
});

const toolsActions = [...actions!, ...uniqueLocalActions, ...toolsWithCustomActions];

return toolsActions.map((action: any) => {
return this.modifyActionForLocalExecution(action);
});
}

async getAuthParams(data: {connectedAccountId: string}) {
async getAuthParams(data: { connectedAccountId: string }) {
return this.client.connectedAccounts.getAuthParams({
connectedAccountId: data.connectedAccountId
})
Expand Down Expand Up @@ -138,19 +138,19 @@ export class ComposioToolSet {
): Promise<Sequence<NonNullable<GetListActionsResponse["items"]>[0]>> {
await this.setup();

const apps = await this.client.actions.list({
const apps = await this.client.actions.list({
...(filters?.apps && { apps: filters?.apps?.join(",") }),
...(filters?.tags && { tags: filters?.tags?.join(",") }),
...(filters?.useCase && { useCase: filters?.useCase }),
...(filters?.actions && { actions: filters?.actions?.join(",") }),
...(filters?.useCaseLimit && { usecaseLimit: filters?.useCaseLimit }),
filterByAvailableApps: filters?.filterByAvailableApps ?? undefined
});
});
const localActions = new Map<string, NonNullable<GetListActionsResponse["items"]>[0]>();
if(filters.apps && Array.isArray(filters.apps)) {
if (filters.apps && Array.isArray(filters.apps)) {
for (const appName of filters.apps!) {
const actionData = this.localActions?.filter((a: any) => a.appName === appName);
if(actionData) {
if (actionData) {
for (const action of actionData) {
localActions.set(action.name, action);
}
Expand All @@ -176,11 +176,11 @@ export class ComposioToolSet {
});

const toolsActions = [...apps?.items!, ...uniqueLocalActions, ...toolsWithCustomActions];

return toolsActions.map((action: any) => {
return this.modifyActionForLocalExecution(action);
});

}

modifyActionForLocalExecution(toolSchema: any) {
Expand All @@ -189,7 +189,7 @@ export class ComposioToolSet {
const response = toolSchema.response.properties;

for (const responseKey of Object.keys(response)) {
if(responseKey === "file") {
if (responseKey === "file") {
response["file_uri_path"] = {
type: "string",
title: "Name",
Expand Down Expand Up @@ -219,23 +219,36 @@ export class ComposioToolSet {
connectedAccountId?: string,
): Promise<Record<string, any>> {
// Custom actions are always executed in the host/local environment for JS SDK
if(await this.isCustomAction(action)) {
if (await this.isCustomAction(action)) {
let accountId = connectedAccountId;
if (!accountId) {
// fetch connected account id
const connectedAccounts = await this.client.connectedAccounts.list({
user_uuid: entityId
});
accountId = connectedAccounts?.items[0]?.id;
}

if(!accountId) {
throw new Error("No connected account found for the user");
}

return this.customActionRegistry.executeAction(action, params, {
entityId: entityId,
connectionId: connectedAccountId
connectionId: accountId
});
}
if(this.workspaceEnv && this.workspaceEnv !== ExecEnv.HOST) {
if (this.workspaceEnv && this.workspaceEnv !== ExecEnv.HOST) {
const workspace = await this.workspace.get();
return workspace.executeAction(action, params, {
entityId: this.entityId
});
}
params = await converReqParamForActionExecution(params);
const data = await this.client.getEntity(entityId).execute(action, params, nlaText) as unknown as ExecuteActionResDTO
const data = await this.client.getEntity(entityId).execute(action, params, nlaText) as unknown as ExecuteActionResDTO


return this.processResponse(data ,{
return this.processResponse(data, {
action: action,
entityId: entityId
});
Expand All @@ -251,24 +264,24 @@ export class ComposioToolSet {

// @ts-ignore
const isFile = !!data?.response_data?.file;
if(isFile) {
if (isFile) {
// @ts-ignore
const fileData = data.response_data.file;
const {name, content} = fileData as {name: string, content: string};
const { name, content } = fileData as { name: string, content: string };
const file_name_prefix = `${meta.action}_${meta.entityId}_${Date.now()}`;
const filePath = saveFile(file_name_prefix, content);
const filePath = saveFile(file_name_prefix, content);

// @ts-ignore
delete data.response_data.file

return {
...data,
response_data: {
// @ts-ignore
...data.response_data,
file_uri_path: filePath
}
}
}
}

return data;
Expand Down

0 comments on commit 69d99f8

Please sign in to comment.