All files / frameworks langchain.ts

94.44% Statements 17/18
60% Branches 9/15
80% Functions 4/5
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x     1x   1x         1x 1x                   1x                         954x 954x   954x               954x       954x                                       3x 3x 954x                  
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import { jsonSchemaToModel } from "../utils/shared";
import { DynamicStructuredTool } from "@langchain/core/tools";
import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI";
import type { Optional, Dict, Sequence } from "../sdk/types";
import { WorkspaceConfig } from "../env/config";
import { Workspace } from "../env";
 
export class LangchainToolSet extends BaseComposioToolSet {
    /**
     * Composio toolset for Langchain framework.
     *
     */
    static FRAMEWORK_NAME = "langchain";
    static DEFAULT_ENTITY_ID = "default";
 
    constructor(
        config: {
            apiKey?: Optional<string>,
            baseUrl?: Optional<string>,
            entityId?: string,
            workspaceConfig?: WorkspaceConfig
        }={}
    ) {
        super(
            config.apiKey || null,
            config.baseUrl || COMPOSIO_BASE_URL,
            LangchainToolSet.FRAMEWORK_NAME,
            config.entityId || LangchainToolSet.DEFAULT_ENTITY_ID,
            config.workspaceConfig || Workspace.Host()
        );
    }
 
    private _wrapTool(
        schema: Dict<any>,
        entityId: Optional<string> = null
    ): DynamicStructuredTool {
        const action = schema["name"];
        const description = schema["description"];
 
        const func = async (...kwargs: any[]): Promise<any> => {
            return JSON.stringify(await this.executeAction(
                action,
                kwargs[0],
                entityId || this.entityId
            ));
        };
 
        const parameters = jsonSchemaToModel(schema["parameters"]);
 
        // @TODO: Add escriiption an othjer stuff here
 
        return new DynamicStructuredTool({
            name: action,
            description,
            schema: parameters,
            func: func
        });
    }
 
 
    async getTools(
        filters: {
            actions?: Optional<Array<string>>;
            apps?: Sequence<string>;
            tags?: Optional<Array<string>>;
            useCase?: Optional<string>;
            usecaseLimit?: Optional<number>;
            filterByAvailableApps?: Optional<boolean>;
        },
        entityId: Optional<string> = null
    ): Promise<Sequence<DynamicStructuredTool>> {
        const tools = await this.getToolsSchema(filters, entityId);
        return tools.map((tool) =>
            this._wrapTool(
                tool,
                entityId || this.entityId
            )
        );
    }
 
 
}