| 108 | const DEFAULT_HEAD_LIMIT = 250 |
| 109 | |
| 110 | function applyHeadLimit<T>( |
| 111 | items: T[], |
| 112 | limit: number | undefined, |
| 113 | offset: number = 0, |
| 114 | ): { items: T[]; appliedLimit: number | undefined } { |
| 115 | // Explicit 0 = unlimited escape hatch |
| 116 | if (limit === 0) { |
| 117 | return { items: items.slice(offset), appliedLimit: undefined } |
| 118 | } |
| 119 | const effectiveLimit = limit ?? DEFAULT_HEAD_LIMIT |
| 120 | const sliced = items.slice(offset, offset + effectiveLimit) |
| 121 | // Only report appliedLimit when truncation actually occurred, so the model |
| 122 | // knows there may be more results and can paginate with offset. |
| 123 | const wasTruncated = items.length - offset > effectiveLimit |
| 124 | return { |
| 125 | items: sliced, |
| 126 | appliedLimit: wasTruncated ? effectiveLimit : undefined, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Format limit/offset information for display in tool results. |
| 131 | // appliedLimit is only set when truncation actually occurred (see applyHeadLimit), |