---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/context/compact/summarizer/functions/assembleCompactedTurns.md
---

# Function: assembleCompactedTurns()

```ts
function assembleCompactedTurns<M, TC>(
  turns: readonly HistoryTurn<M, TC>[],
  options: AssembleCompactedTurnsOptions,
): Promise<AssembleCompactedTurnsResult<M, TC>>;
```

Defined in: [src/batteries/context/compact/summarizer.ts:260](https://github.com/NHTIO/ADK/blob/v1.20260713.1/src/src/batteries/context/compact/summarizer.ts#L260)

The Compact assembly step: keep the newest `keepVerbatim` turns in full; fold everything OLDER into
a running structured summary, (re)generated via [summariseTurns](summariseTurns.md) when the older region grows
past `summariseAtTokens` AND new turns have aged into it since the last summarization. Returns
`[syntheticSummaryTurn, ...recentVerbatimTurns]`.

## Type Parameters

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

## Parameters

| Parameter | Type                                                                                         | Description                                                                                                                                                                              |
| --------- | -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `turns`   | readonly [`HistoryTurn`](../../../thrift/relevance/interfaces/HistoryTurn.md)<`M`, `TC`>\[] | The full grouped history, chronological (e.g. from [@nhtio/adk/batteries/context/thrift/relevance!groupHistoryIntoTurns](../../../thrift/relevance/functions/groupHistoryIntoTurns.md)). |
| `options` | [`AssembleCompactedTurnsOptions`](../interfaces/AssembleCompactedTurnsOptions.md)            | See [AssembleCompactedTurnsOptions](../interfaces/AssembleCompactedTurnsOptions.md).                                                                                                     |

## Returns

`Promise`<[`AssembleCompactedTurnsResult`](../interfaces/AssembleCompactedTurnsResult.md)<`M`, `TC`>>

See [AssembleCompactedTurnsResult](../interfaces/AssembleCompactedTurnsResult.md).

## Remarks

**Faithfulness to the original** (`#assembleCompactedTurns`): the threshold/keep-verbatim/rolling
logic is a line-for-line port — same re-summarize condition (`older.length > coveredOlder &&
(summary === null || olderTokens > summariseAtTokens)`), same synthetic turn shape (`qa` = the
summary text, `createdAt` = the epoch-zero sentinel that sorts before every real turn, one message
with `id: summaryMessageId`, content prefixed `[Earlier conversation, compacted summary]`, no tool
calls), same "nothing to compact yet" early return when there's no older region at all. The ONE
deviation: state that lived on private class fields (`#compactSummary`, `#compactCoveredOlder`) in
the original is now explicit input/output (`options.priorState` in, `{ summary, coveredOlder }` out)
— a pure battery function cannot own mutable instance state, so the caller threads it through. This
is a mechanical decoupling change, not a behavioral one: a caller who persists and re-supplies
`priorState` exactly as returned reproduces the original's stateful behavior turn-for-turn.

Unlike Token Thrift, this function CANNOT resurface the exact older detail a later turn needs — it
only has the summary's blurred prose. See the module barrel for the honest, both-ways evaluation of
that fidelity trade-off against thrift's subtractive approach.

## Throws

[@nhtio/adk/batteries/context/exceptions!E\_CONTEXT\_RESOLVER\_MISSING](../../../exceptions/variables/E_CONTEXT_RESOLVER_MISSING.md) When
`options.summarize` or `options.estimateTokens` is not a function.
