All files / frameworks langchain.ts

94.44% Statements 17/18
66.66% Branches 14/21
80% Functions 4/5
94.11% Lines 16/17

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 792x 2x 2x 2x     2x   2x         2x 2x                     2x                         1906x 1906x   1906x                   1906x       1906x                                     4x 1906x      
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;
      runtime?: string;
    } = {}
  ) {
    super(
      config.apiKey || null,
      config.baseUrl || COMPOSIO_BASE_URL,
      config?.runtime || 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,
          params: kwargs[0],
          entityId: entityId || this.entityId,
        })
      );
    };
 
    const parameters = jsonSchemaToModel(schema["parameters"]);
 
    // @TODO: Add escriiption an other 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));
  }
}