Groups a flat message list into turns that each start at a user message, and buckets the activities of each turn by what they belong to.
Messages before the first user message form a turn without a user. An
activity goes to activitiesByToolCall when its links.toolCallId matches
a client or server tool call of an assistant in the same turn. Otherwise it
goes to activitiesByAssistant when its links.assistantMessageId matches
an assistant in the turn. Anything else is unlinkedActivities.
The function is pure: it does not mutate its input and keeps the input order inside every bucket.
API
export declare function groupTurns<
M extends AnyMessage ,
>(messages: readonly M[]): readonly Turn <M>[];Examples
for (const turn of groupTurns(chat.messages())) {
for (const assistant of turn.assistants) {
const plans = turn.activitiesByAssistant.get(assistant.id) ?? [];
}
}