All files / src/frameworks vercel.ts

73.91% Statements 17/23
21.87% Branches 7/32
66.66% Functions 4/6
73.91% Lines 17/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 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 1451x 1x 1x 1x 1x         1x                         1x 1x                   1x                           954x                                                         3x                         3x   3x                         3x 3x 954x           3x                                                                
import { CoreTool, jsonSchema, tool } from "ai";
import { z } from "zod";
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import { TELEMETRY_LOGGER } from "../sdk/utils/telemetry";
import { TELEMETRY_EVENTS } from "../sdk/utils/telemetry/events";
import { RawActionData } from "../types/base_toolset";
 
type Optional<T> = T | null;
 
const ZExecuteToolCallParams = z.object({
  actions: z.array(z.string()).optional(),
  apps: z.array(z.string()).optional(),
  params: z.record(z.any()).optional(),
  entityId: z.string().optional(),
  useCase: z.string().optional(),
  usecaseLimit: z.number().optional(),
  connectedAccountId: z.string().optional(),
  tags: z.array(z.string()).optional(),
 
  filterByAvailableApps: z.boolean().optional().default(false),
});
 
export class VercelAIToolSet extends BaseComposioToolSet {
  fileName: string = "js/src/frameworks/vercel.ts";
  constructor(
    config: {
      apiKey?: Optional<string>;
      baseUrl?: Optional<string>;
      entityId?: string;
      connectedAccountIds?: Record<string, string>;
      allowTracing?: boolean;
    } = {}
  ) {
    super({
      apiKey: config.apiKey || null,
      baseUrl: config.baseUrl || null,
      runtime: "vercel-ai",
      entityId: config.entityId || "default",
      connectedAccountIds: config.connectedAccountIds,
      allowTracing: config.allowTracing || false,
    });
  }
 
  private generateVercelTool(
    schema: RawActionData,
    entityId: Optional<string> = null
  ) {
    return tool({
      description: schema.description,
      // @ts-ignore the type are JSONSchemV7. Internally it's resolved
      parameters: jsonSchema(schema.parameters as unknown),
      execute: async (params) => {
        return await this.executeToolCall(
          {
            name: schema.name,
            arguments: JSON.stringify(params),
          },
          entityId || this.entityId
        );
      },
    });
  }
 
  // change this implementation
  async getTools(
    filters: {
      actions?: Array<string>;
      apps?: Array<string>;
      tags?: Optional<Array<string>>;
      useCase?: Optional<string>;
      usecaseLimit?: Optional<number>;
      filterByAvailableApps?: Optional<boolean>;
      integrationId?: Optional<string>;
    },
    entityId: Optional<string> = null
  ): Promise<{ [key: string]: CoreTool }> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "getTools",
      file: this.fileName,
      params: filters,
    });
 
    const {
      apps,
      tags,
      useCase,
      usecaseLimit,
      filterByAvailableApps,
      actions,
    } = ZExecuteToolCallParams.parse(filters);
 
    const actionsList = await this.getToolsSchema(
      {
        apps,
        actions,
        tags,
        useCase,
        useCaseLimit: usecaseLimit,
        filterByAvailableApps,
      },
      entityId,
      filters.integrationId
    );
 
    const tools: { [key: string]: CoreTool } = {};
    actionsList.forEach((actionSchema) => {
      tools[actionSchema.name!] = this.generateVercelTool(
        actionSchema,
        entityId
      );
    });
 
    return tools;
  }
 
  async executeToolCall(
    tool: { name: string; arguments: unknown },
    entityId: Optional<string> = null
  ): Promise<string> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "executeToolCall",
      file: this.fileName,
      params: { tool, entityId },
    });
 
    const toolSchema = await this.getToolsSchema({
      actions: [tool.name],
    });
    const appName = toolSchema[0]?.appName?.toLowerCase();
    const connectedAccountId = appName && this.connectedAccountIds?.[appName];
 
    return JSON.stringify(
      await this.executeAction({
        action: tool.name,
        params:
          typeof tool.arguments === "string"
            ? JSON.parse(tool.arguments)
            : tool.arguments,
        entityId: entityId || this.entityId,
        connectedAccountId: connectedAccountId,
      })
    );
  }
}