> ## Documentation Index
> Fetch the complete documentation index at: https://docs.versori.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Tool

> The Code Tool gives the ability to write custom Node.js code against the data in an integration. It also offers the ability to write a custom output schema, which can be useful to reshape the data for later on in an integration.

### Incoming Data

* The Code Tool can use data:
  * **directly** - the node prior to the tool
  * **indirectly** - using selectors. {/* Add link to selector documentation */} This gives the ability to take data
    from any point previous in the integration.

### Use cases

* The Code Tool enables the user to write **custom logic** on their data in an integration. This provides the user with
  full control on the operation/operations performed

* Defining a **custom output schema**. In some integrations, the shape of the data from the incoming connector might not
  be suitable for the target connector. The Code Tool can be used as an adapter in these types of scenarios

* **Transforming data** - transforming data from one schema to another

### Writing the code

* Within the Code Tool, there is a code editor. From here, Node.js code can be written

* When creating a Code Tool, the user is provided with some starter code

```
module.exports = async function (ctx) {
    return ctx.data
};
```

* The body of the incoming source node data can be accessed using `ctx.data.body`

* Selector data can be accessed through `ctx.data.selectors.<selector_name>`

### Customising Output schema

The schema coming out of the Code Tool can be customised as shown below:

<Frame caption="Defining Output Schema">
  <img src="https://storage.googleapis.com/versori-assets/apps/user-docs/tools/code_tool/defining_output_schema.jpeg" />
</Frame>

This is useful in situations where the schema needs to be adapted for use later on in the integration.

### Co-Pilot

AI has been built into the Code Tool to help speed up the process of using it to manipulate the data to the desired
outcome. It can be used by going into Co-pilot mode:

<Frame caption="Copilot Mode">
  <img src="https://storage.googleapis.com/versori-assets/apps/user-docs/tools/code_tool/going_into_copilot.jpeg" />
</Frame>

#### Transform

* Similar to the usage of a transfomer {/* Add link to transformer tool */}, Co-Pilot can be used to help write code to
  transform the data from the incoming node to the schema of the target node

<Frame caption="Transform">
  <img src="https://storage.googleapis.com/versori-assets/apps/user-docs/tools/code_tool/transform.jpeg" />
</Frame>

* Note that this expects the output schema to be the target nodes (not customised)

#### Custom Code

* As mentioned previously, the Code Tool can be used for manipulating the output schema. AI can be used to help with
  this. In Co-pilot mode, the user can provide a prompt to what they want to achieve and click `Generate`. The AI will
  then go away, analyse the data it has available to it, understand the user's requirements and then write code to carry
  out the requested task

<Frame caption="Prompting">
  <img src="https://storage.googleapis.com/versori-assets/apps/user-docs/tools/code_tool/generating_code.jpeg" />
</Frame>

### Testing the Code

* **Note** - the board must currently be published to test the code within the Code Tool

* Once the board has been published, the Code Tool can be tested. This can be done using any example data the user may
  have or alternatively using AI to produce dummy test data following the schema of the incoming data

<Frame caption="Testing">
  <img src="https://storage.googleapis.com/versori-assets/apps/user-docs/tools/code_tool/testing_code_tool.jpeg" />
</Frame>
