# Code Runner Widget

### Widgets in Workshop

![Code Runner Widget](https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2FIkolB5fg8F4RkKliEssu%2Fjs%20code%20widget.png?alt=media\&token=9e84a463-225f-4901-86ac-f5fabe352aa5)

{% hint style="info" %}
Currently we only provide JavaScript code runner. The supported syntax remains consistent with [#expression](https://docs.myshell.ai/create/basic/common#expression "mention"), including ECMAScript 5.1 and certain 6+ features.\
We don't support third-party libraries or host-environment-specific API like `fetch.`
{% endhint %}

### Config

Fields besides `widget_id` and `output_name`

| Field's Name | JSON Type (Required/Optional) | Description                                                                        | Example                                                                                    |
| ------------ | ----------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| code         | string (Required)             | A stringified code snippet. It should contain one or more function definitions.    | "function main(params) {\n const { a, b } = params;\n const sum = a + b;\n return sum;\n}" |
| function     | string (Optional)             | The name of the function you want to call in the code snippet. Defaults to `main`. | 'main'                                                                                     |
| params       | Object (Optional)             | The parameters you pass to the calling function.                                   | ‘Hello.’                                                                                   |

{% hint style="info" %}
You can use the widget's 'Copy' button to get a stringified code snippet for `code` field.
{% endhint %}

<figure><img src="https://2975681513-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDtmZwLatyyBFLUK6P6Pj%2Fuploads%2FwykXLLRGtNmFngqM4uJ6%2Fcopy%20code.png?alt=media&#x26;token=da27878d-8683-4824-9fd4-3e231efb1545" alt=""><figcaption><p>Copy code</p></figcaption></figure>

### Example

{% code overflow="wrap" %}

```json
{
  "id": "code_widget_template",
  "initial": "home_state",
  "states": {
    "home_state": {
      "tasks": [
        {
          "name": "code_widget_example_task",
          "module_type": "AnyWidgetModule",
          "module_config": {
            "widget_id": "1751859390353202447",
            "params": {
              "a": "{{1}}",
              "b": "{{2}}"
            },
            "code": "function main(params) {\n const { a, b } = params;\n const sum = a + b;\n return sum;\n}",  // the text inputted into prompt widget, you can get it from user input or upper state
            "output_name": "result"
          }
        }
      ],
      "render": {
        "text": "{{result}}", // it's a string produced by prompt widget.
        "buttons": [
          {
            "content": "Run Again",
            "description": "",
            "on_click": "rerun"
          }
        ]
      },
      "transitions": {
        "rerun": "home_state",
        "CHAT": "home_state"
      }
    }
  }
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.myshell.ai/create/pro-config-mode/api-reference/module/anywidget-module/code-runner-widget.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
