---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/context/thrift/relevance/functions/groupHistoryIntoTurns.md
---

# Function: groupHistoryIntoTurns()

```ts
function groupHistoryIntoTurns<M, TC>(
  messages: readonly M[],
  toolCalls: readonly TC[],
): HistoryTurn<M, TC>[];
```

Defined in: [src/batteries/context/thrift/relevance.ts:213](https://github.com/NHTIO/ADK/blob/v1.20260713.1/src/src/batteries/context/thrift/relevance.ts#L213)

Group a flat message history (plus its tool calls) into [HistoryTurn](../interfaces/HistoryTurn.md)s: a new turn starts at
each message that begins a fresh exchange and closes at the next `'assistant'`-role message
(inclusive); every message up to the first turn boundary that has no preceding messages also starts
a turn. Tool calls are attributed to the LATEST turn whose first message precedes the call's
timestamp.

## Type Parameters

| Type Parameter                                                           | Default type                                              |
| ------------------------------------------------------------------------ | --------------------------------------------------------- |
| `M` *extends* [`RelevanceMessage`](../interfaces/RelevanceMessage.md)    | [`RelevanceMessage`](../interfaces/RelevanceMessage.md)   |
| `TC` *extends* [`RelevanceToolCall`](../interfaces/RelevanceToolCall.md) | [`RelevanceToolCall`](../interfaces/RelevanceToolCall.md) |

## Parameters

| Parameter   | Type            | Description                                                                                              |
| ----------- | --------------- | -------------------------------------------------------------------------------------------------------- |
| `messages`  | readonly `M`\[]  | The full flat message history, in chronological order.                                                   |
| `toolCalls` | readonly `TC`\[] | The full flat tool-call history, in any order (this function sorts by attribution, not the input order). |

## Returns

[`HistoryTurn`](../interfaces/HistoryTurn.md)<`M`, `TC`>\[]

The grouped turns, in chronological order.
