All files / frameworks cloudflare.ts

35.48% Statements 11/31
29.16% Branches 7/24
27.27% Functions 3/11
35.48% Lines 11/31

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731x         1x   1x 1x           1x                             1x                       1x 1x 1x                           1x       1x                                                                                                                                                                                                                              
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import {
  AiTextGenerationOutput,
  AiTextGenerationToolInput,
} from "@cloudflare/workers-types";
import { COMPOSIO_BASE_URL } from "../sdk/client/core/OpenAPI";
import { WorkspaceConfig } from "../env/config";
import { Workspace } from "../env";
import logger from "../utils/logger";
import {  ActionsControllerV1ListActionsResponse, ActionsListResponseDTO } from "../sdk/client";
 
type Optional<T> = T | null;
type Sequence<T> = Array<T>;
 
export class CloudflareToolSet extends BaseComposioToolSet {
  /**
   * Composio toolset for Cloudflare framework.
   *
   * Example:
   * ```typescript
   *
   * ```
   */
  constructor(config: {
    apiKey?: Optional<string>;
    baseUrl?: Optional<string>;
    entityId?: string;
    workspaceConfig?: WorkspaceConfig
  }) {
    super(
      config.apiKey || null,
      config.baseUrl || COMPOSIO_BASE_URL,
      "cloudflare",
      config.entityId || "default",
      config.workspaceConfig || Workspace.Host()
    );
  }
 
  async getActions(filters: {
    actions: Sequence<string>;
  }): Promise<Sequence<AiTextGenerationToolInput>> {
    const actions = await this.getActionsSchema(filters);
    return actions.map((action: NonNullable<ActionsListResponseDTO["items"]>[0]) => {
          const formattedSchema: AiTextGenerationToolInput["function"] = {
            name: action.name!,
            description: action.description!,
            parameters: action.parameters as unknown as {
              type: "object";
              properties: {
                [key: string]: {
                  type: string;
                  description?: string;
                };
              };
              required: string[];
            },
          };
          const tool: AiTextGenerationToolInput = {
            type: "function",
            function: formattedSchema,
          };
          return tool;
        }) || [];
  }
 
  /**
   * @deprecated Use getActions instead.
   */
  async get_actions(filters: {
    actions: Sequence<string>;
  }): Promise<Sequence<AiTextGenerationToolInput>> {
    logger.warn("get_actions is deprecated, use getActions instead");
    return this.getActions(filters);
  }
 
  async getTools(filters: {
    apps: Sequence<string>;
    tags?: Optional<Array<string>>;
    useCase?: Optional<string>;
  }): Promise<Sequence<AiTextGenerationToolInput>> {
    const actions = await this.getToolsSchema(filters);
    return actions.map((action: NonNullable<ActionsControllerV1ListActionsResponse["items"]>[0]) => {
        const formattedSchema: AiTextGenerationToolInput["function"] = {
          name: action.name!,
          description: action.description!,
          parameters: action.parameters as unknown as {
            type: "object";
            properties: {
              [key: string]: {
                type: string;
                description?: string;
              };
            };
            required: string[];
          },
        };
        const tool: AiTextGenerationToolInput = {
          type: "function",
          function: formattedSchema,
        };
        return tool;
      }) || [];
  }
 
  /**
   * @deprecated Use getTools instead.
   */
  async get_tools(filters: {
    apps: Sequence<string>;
    tags?: Optional<Array<string>>;
    useCase?: Optional<string>;
  }): Promise<Sequence<AiTextGenerationToolInput>> {
    logger.warn("get_tools is deprecated, use getTools instead");
    return this.getTools(filters);
  }
 
  async executeToolCall(
    tool: {
      name: string;
      arguments: unknown;
    },
    entityId: Optional<string> = null
  ): Promise<string> {
    return JSON.stringify(
      await this.executeAction(
        tool.name,
        typeof tool.arguments === "string" ? JSON.parse(tool.arguments) : tool.arguments,
        entityId || this.entityId
      )
    );
  }
 
  /**
   * @deprecated Use executeToolCall instead.
   */
  async execute_tool_call(
    tool: {
      name: string;
      arguments: unknown;
    },
    entityId: Optional<string> = null
  ): Promise<string> {
    logger.warn("execute_tool_call is deprecated, use executeToolCall instead");
    return this.executeToolCall(tool, entityId);
  }
 
  async handleToolCall(
    result: AiTextGenerationOutput,
    entityId: Optional<string> = null
  ): Promise<Sequence<string>> {
    const outputs = [];
    Iif ("tool_calls" in result && Array.isArray(result.tool_calls)) {
      for (const tool_call of result.tool_calls) {
        Iif (tool_call.name) {
          outputs.push(await this.executeToolCall(tool_call, entityId));
        }
      }
    }
    return outputs;
  }
 
  /**
   * @deprecated Use handleToolCall instead.
   */
  async handle_tool_call(
    result: AiTextGenerationOutput,
    entityId: Optional<string> = null
  ): Promise<Sequence<string>> {
    logger.warn("handle_tool_call is deprecated, use handleToolCall instead");
    return this.handleToolCall(result, entityId);
  }
}