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

73.68% Statements 28/38
75.86% Branches 22/29
100% Functions 4/4
70.58% Lines 24/34

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          6x 6x   6x         4x 4x       4x                               6x 6x   23x                       23x   23x         6x     6x 4783x 4783x   4783x 18917x 18917x   18917x       18917x 2x 2x           2x       4783x                
import {
  TPostProcessor,
  TPreProcessor,
  TSchemaProcessor,
} from "../../../types/base_toolset";
import logger from "../../../utils/logger";
import { saveFile } from "../fileUtils";
 
export const fileResponseProcessor: TPostProcessor = ({
  actionName,
  toolResponse,
}) => {
  const responseData =
    (toolResponse.data.response_data as Record<string, unknown>) || {};
  const fileData = responseData.file as
    | { name: string; content: string }
    | undefined;
 
  if (!fileData) return toolResponse;
 
  const fileNamePrefix = `${actionName}_${Date.now()}`;
  const filePath = saveFile(fileNamePrefix, fileData.content, true);
 
  delete responseData.file;
 
  return {
    ...toolResponse,
    data: {
      ...toolResponse.data,
      file_uri_path: filePath,
    },
  };
};
 
export const fileInputProcessor: TPreProcessor = ({ params, actionName }) => {
  const requestData = Object.entries(params).reduce(
    (acc, [key, value]) => {
      Iif (key === "file_uri_path" && typeof value === "string") {
        try {
          //eslint-disable-next-line @typescript-eslint/no-require-imports
          const fileContent = require("fs").readFileSync(value, "utf-8");
          const fileName =
            value.split("/").pop() || `${actionName}_${Date.now()}`;
          acc["file"] = { name: fileName, content: fileContent };
        } catch (error) {
          logger.error(`Error reading file at ${value}:`, error);
          acc["file"] = { name: value, content: "" }; // Fallback to original value if reading fails
        }
      } else {
        acc[key] = value;
      }
      return acc;
    },
    {} as Record<string, unknown>
  );
 
  return requestData;
};
 
export const fileSchemaProcessor: TSchemaProcessor = ({ toolSchema }) => {
  const { properties } = toolSchema.parameters;
  const clonedProperties = JSON.parse(JSON.stringify(properties));
 
  for (const propertyKey of Object.keys(clonedProperties)) {
    const object = clonedProperties[propertyKey];
    const isObject = typeof object === "object";
    const isFile =
      isObject &&
      object?.required?.includes("name") &&
      object?.required?.includes("content");
 
    if (isFile) {
      const newKey = `${propertyKey}_file_uri_path`;
      clonedProperties[newKey] = {
        type: "string",
        title: "Name",
        description: "Local absolute path to the file or http url to the file",
      };
 
      delete clonedProperties[propertyKey];
    }
  }
 
  return {
    ...toolSchema,
    parameters: {
      ...toolSchema.parameters,
      properties: clonedProperties,
    },
  };
};