Skip to content
1 min read · 163 words

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

Group a flat message history (plus its tool calls) into HistoryTurns: 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 ParameterDefault type
M extends RelevanceMessageRelevanceMessage
TC extends RelevanceToolCallRelevanceToolCall

Parameters

ParameterTypeDescription
messagesreadonly M[]The full flat message history, in chronological order.
toolCallsreadonly TC[]The full flat tool-call history, in any order (this function sorts by attribution, not the input order).

Returns

HistoryTurn<M, TC>[]

The grouped turns, in chronological order.