tryCompactOnExit runs compaction when the chatloop is about to exit early (e.g. via ErrDynamicToolCall). The normal inline and post-run compaction paths are unreachable in early-exit scenarios, so this ensures the context window doesn't grow unbounded.
( ctx context.Context, opts RunOptions, usage fantasy.Usage, metadata fantasy.ProviderMetadata, )
| 1801 | // early-exit scenarios, so this ensures the context window |
| 1802 | // doesn't grow unbounded. |
| 1803 | func tryCompactOnExit( |
| 1804 | ctx context.Context, |
| 1805 | opts RunOptions, |
| 1806 | usage fantasy.Usage, |
| 1807 | metadata fantasy.ProviderMetadata, |
| 1808 | ) { |
| 1809 | if opts.Compaction == nil || opts.ReloadMessages == nil { |
| 1810 | return |
| 1811 | } |
| 1812 | reloaded, err := opts.ReloadMessages(ctx) |
| 1813 | if err != nil { |
| 1814 | return |
| 1815 | } |
| 1816 | did, compactErr := tryCompact( |
| 1817 | ctx, |
| 1818 | opts.Model, |
| 1819 | opts.Compaction, |
| 1820 | opts.ContextLimitFallback, |
| 1821 | usage, |
| 1822 | metadata, |
| 1823 | reloaded, |
| 1824 | ) |
| 1825 | opts.Metrics.RecordCompaction(opts.Model.Provider(), opts.Model.Model(), did, compactErr) |
| 1826 | if compactErr != nil && opts.Compaction.OnError != nil { |
| 1827 | opts.Compaction.OnError(compactErr) |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | func isToolActive(name string, activeTools []string) bool { |
| 1832 | return len(activeTools) == 0 || slices.Contains(activeTools, name) |
no test coverage detected