Open History to see the last commit
1import type { OrchestrationThreadActivity } from "@t3tools/contracts";
2
3import { compareThreadActivityOrder } from "../lib/threadActivity";
4
5export function threadContextReachedLimit(
6 activities: ReadonlyArray<OrchestrationThreadActivity>,
7 contextTokenLimit: number | undefined,
8): boolean {
9 if (contextTokenLimit === undefined) return false;
10 let latest:
11 | { readonly activity: OrchestrationThreadActivity; readonly usedTokens: number }
12 | undefined;
13 for (const activity of activities) {
14 if (activity?.kind !== "context-window.updated") continue;
15 const payload = activity.payload as Record<string, unknown> | undefined;
16 const usedTokens = payload?.usedTokens;
17 if (typeof usedTokens === "number" && Number.isFinite(usedTokens) && usedTokens >= 0) {
18 if (latest === undefined || compareThreadActivityOrder(latest.activity, activity) <= 0) {
19 latest = { activity, usedTokens };
20 }
21 }
22 }
23 return latest !== undefined && latest.usedTokens >= contextTokenLimit;
24}