# הרצת קוד

> מריץ קוד JavaScript קצר שכתבתם על הנתונים עד כה: לעצב אותם מחדש, לחשב, או לקרוא לאחד מחיבורי ה-API שלכם. רץ בסביבה מבודדת.

Source: https://docs.go-lumen.ai/he/reference/steps/run-code

## במבט אחד

| מאפיין | ערך |
| --- | --- |
| **שם השלב** | `run-code` |
| **משאב** | ClusterStep (`lumen.ai/v1alpha1`) |
| **קבוצה וקטגוריה** | control, logic |
| **קרדיטים** | **1** קרדיט בכל פעם שהוא מצליח |
| **השפעה חיצונית** | כן: הוא משנה משהו מחוץ ל-Lumen, ולכן ניסיון חוזר שואל קודם, ובדיקה מראה מה הוא היה עושה במקום לעשות את זה. |
| **ההתקנה צריכה לספק** | כלום |

## קלט

| מאפיין | ערך |
| --- | --- |
| **צריך** | כלום |
| **מפיק** | `fields` |
| **עובד על** | item: מסמך אחד בכל פעם |

״צריך״ ו״מפיק״ הם סוגי נתונים (file, text, fields, email). העורך מציע שלב רק במקום שבו מה שהוא צריך כבר נמצא במסלול. ראו [מסמכים ונתונים](https://docs.go-lumen.ai/he/concepts/how-flows-run.md#documents-and-data).

## פלט

צומת מאוחר יותר מפנה לפלט כ-`{nodeId.field}` בטקסט, או כ-`nodeId.field` בתנאי.

| פלט | סוג |
| --- | --- |
| `result` | string |

More outputs are declared in the setting `outputs`.

## הגדרות

מגדירים תחת `config:` בצומת של התהליך. הגדרות טקסט יכולות להכיל מצייני מקום כמו `{nodeId.field}`.

| הגדרה | סוג | ברירת מחדל | תיאור |
| --- | --- | --- | --- |
| `code` (חובה) | string |  | Code is the body of an async JavaScript function of (input, lumen, console); what it returns is the result. (minLength 1; maxLength 65536) |
| `inputs` | map of string |  | Inputs are the input's properties: a name, and text with `{<nodeId>.<field>}` placeholders. A value that is just one placeholder keeps the field's type. None passes every field so far. |
| `outputs` | list of object |  | Outputs are the fields later steps can pick. (maxItems 50) |
| `outputs[].name` (חובה) | string |  | Name (minLength 1; maxLength 64; pattern \`^\[A-Za-z\]\[A-Za-z0-9\_\]\*$\`) |
| `outputs[].type` (חובה) | string |  | Type (one of string, number, boolean, date, money) |
| `connections` | list of string |  | Connections are the HTTP connections lumen.fetch may call. (maxItems 10) |
| `methods` | list of string |  | Methods are the methods lumen.fetch may use; none is GET and HEAD. |
| `timeoutSeconds` | integer |  | TimeoutSeconds caps one run; zero is 5. (minimum 1; maximum 30) |

ה-JSON Schema המלא של ההגדרות: [run-code.schema.json](https://docs.go-lumen.ai/schemas/steps/run-code.schema.json).

## דוגמה

```yaml title="config"
code: |-
  const lines = JSON.parse(input.lines)
  const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
  console.log('lines', lines.length)
  return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
inputs:
  lines: '{read.lines}'
outputs:
- name: total
  type: number
- name: withVat
  type: number
```

## שימוש בתהליך

צומת שמריץ את `run-code` נכנס תחת `spec.nodes` של [Flow](https://docs.go-lumen.ai/he/concepts/how-flows-run.md#the-flow-as-yaml). תנו לו כל מזהה, ורשמו ב-next את מזהי הצמתים הבאים.

```yaml title="flow.yaml"
apiVersion: lumen.ai/v1alpha1
kind: Flow
metadata:
  name: run-code-example
  namespace: tenant-acme
spec:
  displayName: "Run code"
  triggers:
    - id: start
      ref: {name: manual-run}
      next: [run-code]
  nodes:
    - id: run-code
      stepRef: {name: run-code}
      config:
        code: |-
          const lines = JSON.parse(input.lines)
          const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0)
          console.log('lines', lines.length)
          return { total, withVat: Math.round(total * 1.18 * 100) / 100 }
        inputs:
          lines: '{read.lines}'
        outputs:
        - name: total
          type: number
        - name: withVat
          type: number
```
