( items: ConversationItem[], options?: PrepareThreadItemsOptions, )
| 377 | } |
| 378 | |
| 379 | export function prepareThreadItems( |
| 380 | items: ConversationItem[], |
| 381 | options?: PrepareThreadItemsOptions, |
| 382 | ) { |
| 383 | const filtered: ConversationItem[] = []; |
| 384 | for (const item of items) { |
| 385 | const last = filtered[filtered.length - 1]; |
| 386 | if ( |
| 387 | item.kind === "message" && |
| 388 | item.role === "assistant" && |
| 389 | last?.kind === "review" && |
| 390 | last.state === "completed" && |
| 391 | item.text.trim() === last.text.trim() |
| 392 | ) { |
| 393 | continue; |
| 394 | } |
| 395 | filtered.push(item); |
| 396 | } |
| 397 | const normalized = filtered.map((item) => normalizeItem(item)); |
| 398 | const maxItemsPerThreadRaw = options?.maxItemsPerThread; |
| 399 | const maxItemsPerThread = |
| 400 | maxItemsPerThreadRaw === null |
| 401 | ? null |
| 402 | : typeof maxItemsPerThreadRaw === "number" && |
| 403 | Number.isFinite(maxItemsPerThreadRaw) && |
| 404 | maxItemsPerThreadRaw > 0 |
| 405 | ? Math.floor(maxItemsPerThreadRaw) |
| 406 | : DEFAULT_MAX_ITEMS_PER_THREAD; |
| 407 | const limited = |
| 408 | maxItemsPerThread === null |
| 409 | ? normalized |
| 410 | : normalized.length > maxItemsPerThread |
| 411 | ? normalized.slice(-maxItemsPerThread) |
| 412 | : normalized; |
| 413 | const summarized = summarizeExploration(limited); |
| 414 | const cutoff = Math.max(0, summarized.length - TOOL_OUTPUT_RECENT_ITEMS); |
| 415 | return summarized.map((item, index) => { |
| 416 | if (index >= cutoff || item.kind !== "tool") { |
| 417 | return item; |
| 418 | } |
| 419 | const output = item.output ? truncateText(item.output) : item.output; |
| 420 | const changes = item.changes |
| 421 | ? item.changes.map((change) => ({ |
| 422 | ...change, |
| 423 | diff: change.diff ? truncateText(change.diff) : change.diff, |
| 424 | })) |
| 425 | : item.changes; |
| 426 | if (output === item.output && changes === item.changes) { |
| 427 | return item; |
| 428 | } |
| 429 | return { ...item, output, changes }; |
| 430 | }); |
| 431 | } |
no test coverage detected