"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeLambdaMappingTemplate = void 0;
const graphql_transformer_core_1 = require("@aws-amplify/graphql-transformer-core");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const ts_dedent_1 = __importDefault(require("ts-dedent"));
const graphql_transformer_common_1 = require("graphql-transformer-common");
const pluralize_1 = __importDefault(require("pluralize"));
const invokeLambdaMappingTemplate = (config) => {
    const { TOOL_DEFINITIONS_LINE, TOOLS_CONFIGURATION_LINE } = generateToolLines(config);
    const SELECTION_SET = selectionSet;
    const MODEL_CONFIGURATION_LINE = generateModelConfigurationLine(config);
    const RESPONSE_MUTATION_NAME = config.responseMutationName;
    const RESPONSE_MUTATION_INPUT_TYPE_NAME = config.responseMutationInputTypeName;
    const MESSAGE_MODEL_NAME = config.messageModel.messageModel.name.value;
    const GET_QUERY_NAME = `getConversationMessage${(0, graphql_transformer_common_1.toUpper)(config.field.name.value)}`;
    const GET_QUERY_INPUT_TYPE_NAME = 'ID';
    const LIST_QUERY_NAME = `listConversationMessage${(0, graphql_transformer_common_1.toUpper)((0, pluralize_1.default)(config.field.name.value))}`;
    const LIST_QUERY_INPUT_TYPE_NAME = `ModelConversationMessage${(0, graphql_transformer_common_1.toUpper)(config.field.name.value)}FilterInput`;
    const LIST_QUERY_LIMIT = 'undefined';
    const STREAMING_RESPONSE_MUTATION_NAME = config.messageModel.assistantStreamingMutationField.name.value;
    const STREAMING_RESPONSE_MUTATION_INPUT_TYPE_NAME = config.messageModel.assistantStreamingMutationInput.name.value;
    const DISABLE_STREAMING_ENV_VAR = `${config.field.name.value}_DISABLE_STREAMING`;
    const substitutions = {
        TOOL_DEFINITIONS_LINE,
        TOOLS_CONFIGURATION_LINE,
        SELECTION_SET,
        MODEL_CONFIGURATION_LINE,
        RESPONSE_MUTATION_NAME,
        RESPONSE_MUTATION_INPUT_TYPE_NAME,
        MESSAGE_MODEL_NAME,
        GET_QUERY_NAME,
        GET_QUERY_INPUT_TYPE_NAME,
        LIST_QUERY_NAME,
        LIST_QUERY_INPUT_TYPE_NAME,
        LIST_QUERY_LIMIT,
        STREAMING_RESPONSE_MUTATION_NAME,
        STREAMING_RESPONSE_MUTATION_INPUT_TYPE_NAME,
        DISABLE_STREAMING_ENV_VAR,
    };
    let resolver = fs_1.default.readFileSync(path_1.default.join(__dirname, 'invoke-lambda-resolver-fn.template.js'), 'utf8');
    Object.entries(substitutions).forEach(([key, value]) => {
        const replaced = resolver.replace(new RegExp(`\\[\\[${key}\\]\\]`, 'g'), value);
        resolver = replaced;
    });
    const templateName = `Mutation.${config.field.name.value}.invoke-lambda.js`;
    return graphql_transformer_core_1.MappingTemplate.s3MappingFunctionCodeFromString(resolver, templateName);
};
exports.invokeLambdaMappingTemplate = invokeLambdaMappingTemplate;
const generateToolLines = (config) => {
    const toolDefinitions = JSON.stringify(config.toolSpec);
    const TOOL_DEFINITIONS_LINE = toolDefinitions ? `const toolDefinitions = ${toolDefinitions};` : '';
    const TOOLS_CONFIGURATION_LINE = toolDefinitions
        ? (0, ts_dedent_1.default) `const dataTools = toolDefinitions.tools;
     const toolsConfiguration = {
      dataTools,
      clientTools,
    };`
        : (0, ts_dedent_1.default) `const toolsConfiguration = {
      clientTools
    };`;
    return { TOOL_DEFINITIONS_LINE, TOOLS_CONFIGURATION_LINE };
};
const generateModelConfigurationLine = (config) => {
    const { aiModel, systemPrompt } = config;
    return (0, ts_dedent_1.default) `const modelConfiguration = {
    modelId: '${aiModel}',
    systemPrompt: ${JSON.stringify(systemPrompt)},
    ${generateModelInferenceConfigurationLine(config)}
  };`;
};
const generateModelInferenceConfigurationLine = (config) => {
    const { inferenceConfiguration } = config;
    return inferenceConfiguration && Object.keys(inferenceConfiguration).length > 0
        ? (0, ts_dedent_1.default) `inferenceConfiguration: ${JSON.stringify(config.inferenceConfiguration)},`
        : '';
};
const selectionSet = `id conversationId content { image { format source { bytes }} text toolUse { toolUseId name input } toolResult { status toolUseId content { json text image { format source { bytes }} document { format name source { bytes }} }}} role owner createdAt updatedAt`;
//# sourceMappingURL=invoke-lambda-resolver.js.map