All files / src/sdk/utils/processor file.ts

90.69% Statements 39/43
66.66% Branches 8/12
100% Functions 5/5
89.18% Lines 33/37

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            6x                 6x   6x       2x       2x             6x         6x   6x 23x   1x 1x           1x 1x     6x     6x         6x     6x       4x   4x 91x   91x                             4x     6x 4783x 4783x 4783x   4783x 18923x   2x         2x           2x       2x     4783x                  
import { Client } from "@hey-api/client-axios";
import {
  TPostProcessor,
  TPreProcessor,
  TSchemaProcessor,
} from "../../../types/base_toolset";
import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from "./fileUtils";
 
type FileBasePropertySchema = {
  type: string;
  title: string;
  description: string;
  file_uploadable?: boolean;
} & Record<string, unknown>;
 
const FILE_SUFFIX = "_schema_parsed_file";
 
const convertFileSchemaProperty = (
  key: string,
  property: FileBasePropertySchema
) => {
  Iif (!property.file_uploadable) {
    return property;
  }
 
  return {
    keyName: `${key}${FILE_SUFFIX}`,
    type: "string",
    description: property.description,
  };
};
 
const processFileUpload = async (
  params: Record<string, unknown>,
  actionName: string,
  client: Client
) => {
  const result = { ...params };
 
  for (const [key, value] of Object.entries(result)) {
    if (!key.endsWith(FILE_SUFFIX)) continue;
 
    const originalKey = key.replace(FILE_SUFFIX, "");
    const fileData = await getFileDataAfterUploadingToS3(
      value as string,
      actionName,
      client
    );
 
    result[originalKey] = fileData;
    delete result[key];
  }
 
  return result;
};
 
export const FILE_INPUT_PROCESSOR: TPreProcessor = async ({
  params,
  actionName,
  client,
}) => {
  return processFileUpload(params, actionName, client);
};
 
export const FILE_DOWNLOADABLE_PROCESSOR: TPostProcessor = async ({
  actionName,
  toolResponse,
}) => {
  const result = JSON.parse(JSON.stringify(toolResponse));
 
  for (const [key, value] of Object.entries(toolResponse.data)) {
    const fileData = value as { s3url?: string; mimetype?: string };
 
    if (!fileData?.s3url) continue;
 
    const downloadedFile = await downloadFileFromS3({
      actionName,
      s3Url: fileData.s3url,
      mimeType: fileData.mimetype || "application/txt",
    });
 
    result.data[key] = {
      uri: downloadedFile.filePath,
      s3url: fileData.s3url,
      mimeType: downloadedFile.mimeType,
    };
  }
 
  return result;
};
 
export const FILE_SCHEMA_PROCESSOR: TSchemaProcessor = ({ toolSchema }) => {
  const { properties, required: requiredProps = [] } = toolSchema.parameters;
  const newProperties = { ...properties };
  const newRequired = [...requiredProps];
 
  for (const [key, property] of Object.entries(newProperties)) {
    if (!property.file_uploadable) continue;
 
    const { type, keyName, description } = convertFileSchemaProperty(
      key,
      property as FileBasePropertySchema
    );
 
    newProperties[keyName as string] = {
      title: property.title,
      type,
      description,
    };
 
    Iif (requiredProps.includes(key)) {
      newRequired[newRequired.indexOf(key)] = keyName as string;
    }
 
    delete newProperties[key];
  }
 
  return {
    ...toolSchema,
    parameters: {
      ...toolSchema.parameters,
      properties: newProperties,
      required: newRequired,
    },
  };
};