7f571d36ea
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import type { Config } from "@/config/config"
|
|
import { SessionLegacy } from "@opencode-ai/core/session/legacy"
|
|
import type { Provider } from "@/provider/provider"
|
|
import { ProviderTransform } from "@/provider/transform"
|
|
import type { MessageV2 } from "./message-v2"
|
|
|
|
const COMPACTION_BUFFER = 20_000
|
|
|
|
export function usable(input: { cfg: Config.Info; model: Provider.Model; outputTokenMax?: number }) {
|
|
const context = input.model.limit.context
|
|
if (context === 0) return 0
|
|
|
|
const reserved =
|
|
input.cfg.compaction?.reserved ??
|
|
Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax))
|
|
return input.model.limit.input
|
|
? Math.max(0, input.model.limit.input - reserved)
|
|
: Math.max(0, context - ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax))
|
|
}
|
|
|
|
export function isOverflow(input: {
|
|
cfg: Config.Info
|
|
tokens: SessionLegacy.Assistant["tokens"]
|
|
model: Provider.Model
|
|
outputTokenMax?: number
|
|
}) {
|
|
if (input.cfg.compaction?.auto === false) return false
|
|
if (input.model.limit.context === 0) return false
|
|
|
|
const count =
|
|
input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write
|
|
return count >= usable(input)
|
|
}
|