All files / src/frameworks langchain.ts

91.3% Statements 21/23
56.25% Branches 18/32
80% Functions 4/5
91.3% Lines 21/23

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 84 85 86 87 88 89 90 91 922x   2x 2x 2x 2x     2x   2x         2x 2x 2x                       2x                           1907x 1907x 1907x   1907x                       1907x       1907x                       5x           5x         5x 1907x        
import { DynamicStructuredTool } from "@langchain/core/tools";
import { z } from "zod";
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";
import { RawActionData, ZToolSchemaFilter } from "../types/base_toolset";
import type { Optional, Sequence } from "../types/util";
import { jsonSchemaToModel } from "../utils/shared";
 
export class LangchainToolSet extends BaseComposioToolSet {
  /**
   * Composio toolset for Langchain framework.
   *
   */
  static FRAMEWORK_NAME = "langchain";
  static DEFAULT_ENTITY_ID = "default";
  fileName: string = "js/src/frameworks/langchain.ts";
 
  constructor(
    config: {
      apiKey?: Optional<string>;
      baseUrl?: Optional<string>;
      entityId?: string;
      runtime?: string;
      connectedAccountIds?: Record<string, string>;
      allowTracing?: boolean;
    } = {}
  ) {
    super({
      apiKey: config.apiKey || null,
      baseUrl: config.baseUrl || COMPOSIO_BASE_URL,
      runtime: config?.runtime || LangchainToolSet.FRAMEWORK_NAME,
      entityId: config.entityId || LangchainToolSet.DEFAULT_ENTITY_ID,
      connectedAccountIds: config.connectedAccountIds,
      allowTracing: config.allowTracing || false,
    });
  }
 
  private _wrapTool(
    schema: RawActionData,
    entityId: Optional<string> = null
  ): DynamicStructuredTool {
    const action = schema["name"];
    const description = schema["description"];
    const appName = schema["appName"]?.toLowerCase();
 
    const func = async (...kwargs: unknown[]): Promise<unknown> => {
      const connectedAccountId = appName && this.connectedAccountIds?.[appName];
      return JSON.stringify(
        await this.executeAction({
          action,
          params: kwargs[0] as Record<string, unknown>,
          entityId: entityId || this.entityId,
          connectedAccountId: connectedAccountId,
        })
      );
    };
 
    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: z.infer<typeof ZToolSchemaFilter> = {},
    entityId: Optional<string> = null
  ): Promise<Sequence<DynamicStructuredTool>> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "getTools",
      file: this.fileName,
      params: { filters, entityId },
    });
 
    const tools = await this.getToolsSchema(
      filters,
      entityId,
      filters.integrationId
    );
    return tools.map((tool) =>
      this._wrapTool(tool as RawActionData, entityId || this.entityId)
    );
  }
}