{"version":3,"file":"aws_sfn.cjs","names":["Tool","Client","Invoker","Describer","TaskSuccessSender"],"sources":["../../src/tools/aws_sfn.ts"],"sourcesContent":["import {\n  SFNClient as Client,\n  StartExecutionCommand as Invoker,\n  DescribeExecutionCommand as Describer,\n  SendTaskSuccessCommand as TaskSuccessSender,\n} from \"@aws-sdk/client-sfn\";\n\nimport { Tool, ToolParams } from \"@langchain/core/tools\";\n\n/**\n * Interface for AWS Step Functions configuration.\n */\nexport interface SfnConfig {\n  stateMachineArn: string;\n  region?: string;\n  accessKeyId?: string;\n  secretAccessKey?: string;\n}\n\n/**\n * Interface for AWS Step Functions client constructor arguments.\n */\ninterface SfnClientConstructorArgs {\n  region?: string;\n  credentials?: {\n    accessKeyId: string;\n    secretAccessKey: string;\n  };\n}\n\n/**\n * Class for starting the execution of an AWS Step Function.\n */\nexport class StartExecutionAWSSfnTool extends Tool {\n  static lc_name() {\n    return \"StartExecutionAWSSfnTool\";\n  }\n\n  private sfnConfig: SfnConfig;\n\n  public name: string;\n\n  public description: string;\n\n  constructor({\n    name,\n    description,\n    ...rest\n  }: SfnConfig & { name: string; description: string }) {\n    super();\n    this.name = name;\n    this.description = description;\n    this.sfnConfig = rest;\n  }\n\n  /**\n   * Generates a formatted description for the StartExecutionAWSSfnTool.\n   * @param name Name of the state machine.\n   * @param description Description of the state machine.\n   * @returns A formatted description string.\n   */\n  static formatDescription(name: string, description: string): string {\n    return `Use to start executing the ${name} state machine. Use to run ${name} workflows. Whenever you need to start (or execute) an asynchronous workflow (or state machine) about ${description} you should ALWAYS use this. Input should be a valid JSON string.`;\n  }\n\n  /** @ignore */\n  async _call(input: string): Promise<string> {\n    const clientConstructorArgs: SfnClientConstructorArgs =\n      getClientConstructorArgs(this.sfnConfig);\n    const sfnClient = new Client(clientConstructorArgs);\n\n    return new Promise((resolve) => {\n      let payload;\n      try {\n        payload = JSON.parse(input);\n      } catch (e) {\n        console.error(\"Error starting state machine execution:\", e);\n        resolve(\"failed to complete request\");\n      }\n\n      const command = new Invoker({\n        stateMachineArn: this.sfnConfig.stateMachineArn,\n        input: JSON.stringify(payload),\n      });\n\n      sfnClient\n        .send(command)\n        .then((response) =>\n          resolve(\n            response.executionArn ? response.executionArn : \"request completed.\"\n          )\n        )\n        .catch((error: Error) => {\n          console.error(\"Error starting state machine execution:\", error);\n          resolve(\"failed to complete request\");\n        });\n    });\n  }\n}\n\n/**\n * Class for checking the status of an AWS Step Function execution.\n */\nexport class DescribeExecutionAWSSfnTool extends Tool {\n  static lc_name() {\n    return \"DescribeExecutionAWSSfnTool\";\n  }\n\n  name = \"describe-execution-aws-sfn\";\n\n  description =\n    \"This tool should ALWAYS be used for checking the status of any AWS Step Function execution (aka. state machine execution). Input to this tool is a properly formatted AWS Step Function Execution ARN (executionArn). The output is a stringified JSON object containing the executionArn, name, status, startDate, stopDate, input, output, error, and cause of the execution.\";\n\n  sfnConfig: Omit<SfnConfig, \"stateMachineArn\">;\n\n  constructor(config: Omit<SfnConfig, \"stateMachineArn\"> & ToolParams) {\n    super(config);\n    this.sfnConfig = config;\n  }\n\n  /** @ignore */\n  async _call(input: string) {\n    const clientConstructorArgs: SfnClientConstructorArgs =\n      getClientConstructorArgs(this.sfnConfig);\n    const sfnClient = new Client(clientConstructorArgs);\n\n    const command = new Describer({\n      executionArn: input,\n    });\n    return await sfnClient\n      .send(command)\n      .then((response) =>\n        response.executionArn\n          ? JSON.stringify({\n              executionArn: response.executionArn,\n              name: response.name,\n              status: response.status,\n              startDate: response.startDate,\n              stopDate: response.stopDate,\n              input: response.input,\n              output: response.output,\n              error: response.error,\n              cause: response.cause,\n            })\n          : \"{}\"\n      )\n      .catch((error: Error) => {\n        console.error(\"Error describing state machine execution:\", error);\n        return \"failed to complete request\";\n      });\n  }\n}\n\n/**\n * Class for sending a task success signal to an AWS Step Function\n * execution.\n */\nexport class SendTaskSuccessAWSSfnTool extends Tool {\n  static lc_name() {\n    return \"SendTaskSuccessAWSSfnTool\";\n  }\n\n  name = \"send-task-success-aws-sfn\";\n\n  description =\n    \"This tool should ALWAYS be used for sending task success to an AWS Step Function execution (aka. statemachine exeuction). Input to this tool is a stringify JSON object containing the taskToken and output.\";\n\n  sfnConfig: Omit<SfnConfig, \"stateMachineArn\">;\n\n  constructor(config: Omit<SfnConfig, \"stateMachineArn\"> & ToolParams) {\n    super(config);\n    this.sfnConfig = config;\n  }\n\n  /** @ignore */\n  async _call(input: string) {\n    const clientConstructorArgs: SfnClientConstructorArgs =\n      getClientConstructorArgs(this.sfnConfig);\n    const sfnClient = new Client(clientConstructorArgs);\n\n    let payload;\n    try {\n      payload = JSON.parse(input);\n    } catch (e) {\n      console.error(\"Error starting state machine execution:\", e);\n      return \"failed to complete request\";\n    }\n\n    const command = new TaskSuccessSender({\n      taskToken: payload.taskToken,\n      output: JSON.stringify(payload.output),\n    });\n\n    return await sfnClient\n      .send(command)\n      .then(() => \"request completed.\")\n      .catch((error: Error) => {\n        console.error(\n          \"Error sending task success to state machine execution:\",\n          error\n        );\n        return \"failed to complete request\";\n      });\n  }\n}\n\n/**\n * Helper function to construct the AWS SFN client.\n */\nfunction getClientConstructorArgs(config: Partial<SfnConfig>) {\n  const clientConstructorArgs: SfnClientConstructorArgs = {};\n\n  if (config.region) {\n    clientConstructorArgs.region = config.region;\n  }\n\n  if (config.accessKeyId && config.secretAccessKey) {\n    clientConstructorArgs.credentials = {\n      accessKeyId: config.accessKeyId,\n      secretAccessKey: config.secretAccessKey,\n    };\n  }\n\n  return clientConstructorArgs;\n}\n"],"mappings":";;;;;;;;;;;;;AAiCA,IAAa,2BAAb,cAA8CA,sBAAAA,KAAK;CACjD,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA;CAEA;CAEA,YAAY,EACV,MACA,aACA,GAAG,QACiD;AACpD,SAAO;AACP,OAAK,OAAO;AACZ,OAAK,cAAc;AACnB,OAAK,YAAY;;;;;;;;CASnB,OAAO,kBAAkB,MAAc,aAA6B;AAClE,SAAO,8BAA8B,KAAK,6BAA6B,KAAK,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