# Time Window

> Aggregates events into time-based or count-based windows.

Source: https://docs.sommatic.ai/en/nodes/flow.window/

- Slug: `flow.window`
- Family: Flow control
- Version: 1.0.0
- Status: Available
- Execution: async

Aggregates events into time-based or count-based windows.

This operator allows processing of streams of data by grouping them into "windows". It buffers incoming payloads and emits them as a batch when the window conditions are met.

**Window Types:**
- **Tumbling:** Fixed non-overlapping windows (e.g., every 5 minutes).
- **Sliding:** Overlapping windows (e.g., last 5 minutes, updated every minute).
- **Count:** Window of N items.

**Use Cases:**
- Aggregating sensor readings every minute.
- Batching analytics events before sending to a data warehouse.
- Detecting patterns A -> B within a specific timeframe.

## Configuration

- `type` (string, optional): Default: `tumbling`. Allowed values: `tumbling`, `sliding`.
- `interval_seconds` (integer, optional): Default: `60`.
- `max_items` (integer, optional): Force close window if buffer size reaches this limit. Default: `100`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Emits the batch when window closes.
- `error`: Internal failure.

## Input contract

- `payload` (object, optional)

## Output contract

- `window_items` (array<object>, optional)
- `window_meta` (object, optional)
  - `start_time` (string, optional)
  - `end_time` (string, optional)

## In expressions

What a later node can read from this node's output with `$("Time Window").item.json.<path>` (write your node's label instead of `Time Window`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Time Window").item.json.window_items` (array) = `[…] (2)`
- `$("Time Window").item.json.window_meta` (object) = `{…}`
- `$("Time Window").item.json.window_meta.start_time` (string) = `1795705200000`
- `$("Time Window").item.json.window_meta.end_time` (string) = `1795705200150`
- `$("Time Window").item.json.window_meta.count` (number) = `2` (Not suggested by the editor)


## How it works

Buffers the items that reach it during one run and emits them as one batch through `out` when the buffer reaches `max_items` or `interval_seconds` have passed since the first item.

1. The worker wraps the payload as `{ window_items: [payload], window_meta: { start_time, end_time } }` (ISO strings) and sets `meta.window` `{ type, interval, batch_size }` from `type` (default `tumbling`), `interval_seconds` (60) and `max_items` (100).
2. The engine intercepts it and appends that wrapper to an in-memory buffer for this node.
3. When the buffer holds `max_items` entries, or `interval_seconds` have passed since the first entry (checked only when an item arrives), the engine sends `{ window_items, window_meta: { start_time, end_time, count } }` through `out` (times in epoch milliseconds) and empties the buffer.
4. Otherwise nothing continues from the window for that item.

## In a workflow

### Node

```json
{
  "node_id": "node-batch-events",
  "label": "Batch events",
  "operator_slug": "flow.window",
  "operator_version": "1.0.0",
  "config": {
    "type": "tumbling",
    "interval_seconds": 60,
    "max_items": 2
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "window_items": [
      {
        "id": 1
      }
    ],
    "window_meta": {
      "start_time": "2026-11-26T15:00:00.000Z",
      "end_time": "2026-11-26T15:00:00.000Z"
    }
  },
  "meta": {
    "window": {
      "type": "tumbling",
      "interval": 60,
      "batch_size": 2
    }
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The window closed by count or time. Each entry of `window_items` is the worker wrapper, so the original payload is at `window_items[i].window_items[0]`.

```json
{
  "window_items": [
    {
      "window_items": [
        {
          "id": 1
        }
      ],
      "window_meta": {
        "start_time": "2026-11-26T15:00:00.000Z",
        "end_time": "2026-11-26T15:00:00.000Z"
      }
    },
    {
      "window_items": [
        {
          "id": 2
        }
      ],
      "window_meta": {
        "start_time": "2026-11-26T15:00:00.150Z",
        "end_time": "2026-11-26T15:00:00.150Z"
      }
    }
  ],
  "window_meta": {
    "start_time": 1795705200000,
    "end_time": 1795705200150,
    "count": 2
  }
}
```

### `error`

Only when the worker throws unexpectedly.

```json
{
  "error": "<message>"
}
```

## Behaviour in the code

- `type`: Ignored: `sliding` behaves like `tumbling`.
- `interval_seconds`: A value of 0 closes the window on every item.

## Notes and limits

- The buffer lives in memory during one run only. Entries left when the run ends are discarded and never emitted.
- The example envelope is what the worker returns for one item; the engine intercepts it, and only the batch shown on `out` reaches the next node.
- Without a `flow.split` upstream the node sees one item per run, so with the defaults nothing is ever emitted; set `max_items` to the expected number of items, or `interval_seconds` to 0.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "flow.window",
  "name": "Time Window",
  "category": "flow",
  "version": "1.0.0",
  "description": "Aggregates events into time-based or count-based windows.\n\n  This operator allows processing of streams of data by grouping them into \"windows\". It buffers incoming payloads and emits them as a batch when the window conditions are met.\n\n  **Window Types:**\n  - **Tumbling:** Fixed non-overlapping windows (e.g., every 5 minutes).\n  - **Sliding:** Overlapping windows (e.g., last 5 minutes, updated every minute).\n  - **Count:** Window of N items.\n\n  **Use Cases:**\n  - Aggregating sensor readings every minute.\n  - Batching analytics events before sending to a data warehouse.\n  - Detecting patterns A -> B within a specific timeframe.",
  "capabilities": [
    "windowing",
    "aggregation",
    "stream",
    "buffer"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "tumbling",
          "sliding"
        ],
        "default": "tumbling",
        "title": "Window Type"
      },
      "interval_seconds": {
        "type": "integer",
        "default": 60,
        "title": "Interval (Seconds)"
      },
      "max_items": {
        "type": "integer",
        "default": 100,
        "title": "Max Items",
        "description": "Force close window if buffer size reaches this limit."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "description": "Individual event payload.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "Array of buffered payloads.",
    "properties": {
      "window_items": {
        "type": "array",
        "items": {
          "type": "object"
        }
      },
      "window_meta": {
        "type": "object",
        "properties": {
          "start_time": {
            "type": "string"
          },
          "end_time": {
            "type": "string"
          }
        }
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Window",
      "is_default": true,
      "description": "Emits the batch when window closes."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Internal failure."
    }
  ],
  "execution_mode": "async",
  "tags": [
    "stream",
    "window",
    "batch"
  ],
  "is_stateful": true,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "tumbling",
        "sliding"
      ],
      "default": "tumbling",
      "title": "Window Type"
    },
    "interval_seconds": {
      "type": "integer",
      "default": 60,
      "title": "Interval (Seconds)"
    },
    "max_items": {
      "type": "integer",
      "default": 100,
      "title": "Max Items",
      "description": "Force close window if buffer size reaches this limit."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "description": "Individual event payload.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "Array of buffered payloads.",
  "properties": {
    "window_items": {
      "type": "array",
      "items": {
        "type": "object"
      }
    },
    "window_meta": {
      "type": "object",
      "properties": {
        "start_time": {
          "type": "string"
        },
        "end_time": {
          "type": "string"
        }
      }
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Window",
    "is_default": true,
    "description": "Emits the batch when window closes."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Internal failure."
  }
]
```
