Skip to content

The open alternative to Salesforce, designed for AI.

1.1 KB
Raw Blame
import { type InputSchema, type FunctionInput } from '@/workflow';
import { type InputJsonSchema } from '@/logic-function';
import { isRecordObjectSchema } from '@/logic-function/is-record-object-schema';
import { isDefined } from '@/utils';

export const getFunctionInputFromInputSchema = (
  inputSchema: InputSchema | InputJsonSchema[],
): FunctionInput => {
  return inputSchema.map((param) => {
    if (isRecordObjectSchema(param)) {
      return null;
    }

    if (param.type === 'records') {
      return [];
    }

    if (
      isDefined(param.type) &&
      ['string', 'number', 'boolean'].includes(param.type)
    ) {
      return param.enum && param.enum.length > 0 ? param.enum[0] : null;
    } else if (param.type === 'object') {
      const result: FunctionInput = {};
      if (isDefined(param.properties)) {
        Object.entries(param.properties).forEach(([key, val]) => {
          result[key] = getFunctionInputFromInputSchema([val])[0];
        });
      }
      return result;
    } else if (param.type === 'array' && isDefined(param.items)) {
      return [];
    }
    return null;
  });
};