All files / frameworks vercel.ts

63.15% Statements 12/19
54.65% Branches 47/86
50% Functions 4/8
63.15% Lines 12/19

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 921x 1x 1x         1x           1x                 953x 953x                                                                   2x                   2x 2x   953x     2x                                    
import { tool } from "ai";
import { ComposioToolSet as BaseComposioToolSet } from "../sdk/base.toolset";
import { jsonSchemaToModel } from "../utils/shared";
 
type Optional<T> = T | null;
type Sequence<T> = Array<T>;
 
export class VercelAIToolSet extends BaseComposioToolSet {
  constructor(config: {
    apiKey?: Optional<string>;
    baseUrl?: Optional<string>;
    entityId?: string;
  }={}) {
    super(
      config.apiKey || null,
      config.baseUrl || null,
      "vercel-ai",
      config.entityId || "default"
    );
  }
 
  generateVercelTool(schema: any) {
    const parameters = jsonSchemaToModel(schema.parameters);
    return tool({
      description: schema.description,
      parameters,
      execute: async (params: Record<string, string>) => {
        return await this.execute_tool_call({
          name: schema.name,
          arguments: JSON.stringify(params)
        }, this.entityId);
      },
    });
  }
 
  async get_actions(filters: { actions: Sequence<string>; }): Promise<{ [key: string]: any }> {
    const actionsList = await this.getActionsSchema(filters);
    const tools = {};
 
    actionsList.forEach(actionSchema => {
      // @ts-ignore
      tools[actionSchema.name!] = this.generateVercelTool(actionSchema);
    });
 
    return tools;
  }
 
  // 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>;
  }): Promise<{ [key: string]: any }> {
 
    const actionsList = await this.client.actions.list({
      ...(filters?.apps && { apps: filters?.apps?.join(",") }),
      ...(filters?.tags && { tags: filters?.tags?.join(",") }),
      ...(filters?.useCase && { useCase: filters?.useCase }),
      ...(filters?.actions && { actions: filters?.actions?.join(",") }),
      ...(filters?.usecaseLimit && { usecaseLimit: filters?.usecaseLimit }),
      filterByAvailableApps: filters?.filterByAvailableApps ?? undefined
    });
 
 
    const tools = {};
    actionsList.items?.forEach(actionSchema => {
      // @ts-ignore
      tools[actionSchema.name!] = this.generateVercelTool(actionSchema);
    });
 
    return tools;
  }
 
 
  async execute_tool_call(
    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
      )
    );
  }
 
}