All files / src/frameworks anthropic.ts

53.84% Statements 14/26
29.26% Branches 12/41
66.66% Functions 4/6
52% Lines 13/25

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131    1x 1x 1x 1x       1x         1x 1x 1x                   1x                                 956x                             5x           5x 956x                                                                                                                                  
import { Tool } from "@anthropic-ai/sdk/resources/messages/messages.js";
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 { ZToolSchemaFilter } from "../types/base_toolset";
import { Optional } from "../types/util";
 
export class AnthropicToolSet extends BaseComposioToolSet {
  /**
   * Composio toolset for Anthropic framework.
   *
   */
  static FRAMEWORK_NAME = "anthropic";
  static DEFAULT_ENTITY_ID = "default";
  fileName: string = "js/src/frameworks/anthropic.ts";
 
  constructor(
    config: {
      apiKey?: Optional<string>;
      baseUrl?: Optional<string>;
      entityId?: string;
      runtime?: string;
    } = {}
  ) {
    super({
      apiKey: config.apiKey || null,
      baseUrl: config.baseUrl || COMPOSIO_BASE_URL,
      runtime: config?.runtime || null,
      entityId: config.entityId || AnthropicToolSet.DEFAULT_ENTITY_ID,
    });
  }
 
  private _wrapTool(schema: {
    name: string;
    description: string;
    parameters: {
      properties: Record<string, unknown>;
      required: string[];
    };
    appName?: string;
  }): Tool {
    return {
      name: schema.name,
      description: schema.description,
      input_schema: {
        type: "object",
        properties: schema.parameters.properties || {},
        required: schema.parameters.required || [],
      },
    };
  }
 
  async getTools(
    filters: z.infer<typeof ZToolSchemaFilter> = {},
    entityId: Optional<string> = null
  ): Promise<Tool[]> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "getTools",
      file: this.fileName,
      params: { filters, entityId },
    });
 
    const tools = await this.getToolsSchema(filters, entityId);
    return tools.map((tool) => this._wrapTool(tool));
  }
 
  async executeToolCall(
    toolCall: { name: string; arguments: string },
    entityId: Optional<string> = null
  ): Promise<string> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "executeToolCall",
      file: this.fileName,
      params: { toolCall, entityId },
    });
 
    const toolSchema = await this.getToolsSchema({
      actions: [toolCall.name],
    });
    const appName = toolSchema[0]?.appName?.toLowerCase();
    const connectedAccountId = appName && this.connectedAccountIds?.[appName];
 
    return JSON.stringify(
      await this.executeAction({
        action: toolCall.name,
        params: JSON.parse(toolCall.arguments),
        entityId: entityId || this.entityId,
        connectedAccountId: connectedAccountId,
      })
    );
  }
 
  async handleToolCall(
    response: {
      content?: Array<{
        type: string;
        name: string;
        id: string;
        input: Record<string, unknown>;
      }>;
    },
    entityId: Optional<string> = null
  ): Promise<string[]> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "handleToolCall",
      file: this.fileName,
      params: { response, entityId },
    });
 
    const outputs: string[] = [];
    Iif (response.content && Array.isArray(response.content)) {
      for (const content of response.content) {
        Iif (content.type === "tool_use") {
          outputs.push(
            await this.executeToolCall(
              {
                name: content.name,
                arguments: JSON.stringify(content.input),
              },
              entityId
            )
          );
        }
      }
    }
    return outputs;
  }
}