Skip to content

Commit 7bb2959

Browse files
committed
feat(compaction): Handle when models have input limit
1 parent c545fa2 commit 7bb2959

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

packages/opencode/src/provider/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export namespace ModelsDev {
4848
limit: z.object({
4949
context: z.number(),
5050
output: z.number(),
51+
input: z.number().optional(),
5152
}),
5253
modalities: z
5354
.object({

packages/opencode/src/provider/provider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ export namespace Provider {
465465
limit: z.object({
466466
context: z.number(),
467467
output: z.number(),
468+
input: z.number().optional(),
468469
}),
469470
status: z.enum(["alpha", "beta", "deprecated", "active"]),
470471
options: z.record(z.string(), z.any()),
@@ -527,6 +528,7 @@ export namespace Provider {
527528
limit: {
528529
context: model.limit.context,
529530
output: model.limit.output,
531+
input: model.limit.input,
530532
},
531533
capabilities: {
532534
temperature: model.temperature,

packages/opencode/src/session/compaction.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { Provider } from "../provider/provider"
77
import { MessageV2 } from "./message-v2"
88
import z from "zod"
99
import { SessionPrompt } from "./prompt"
10+
import { Flag } from "../flag/flag"
1011
import { Token } from "../util/token"
1112
import { Log } from "../util/log"
1213
import { SessionProcessor } from "./processor"
1314
import { fn } from "@/util/fn"
1415
import { Agent } from "@/agent/agent"
1516
import { Plugin } from "@/plugin"
16-
import { Config } from "@/config/config"
1717

1818
export namespace SessionCompaction {
1919
const log = Log.create({ service: "session.compaction" })
@@ -27,14 +27,15 @@ export namespace SessionCompaction {
2727
),
2828
}
2929

30-
export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
31-
const config = await Config.get()
32-
if (config.compaction?.auto === false) return false
30+
export const NEXT_INPUT_TOKEN_MAX = 20_000
31+
32+
export function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
33+
if (Flag.OPENCODE_DISABLE_AUTOCOMPACT) return false
3334
const context = input.model.limit.context
3435
if (context === 0) return false
3536
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
3637
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
37-
const usable = context - output
38+
const usable = input.model.limit.input ? input.model.limit.input - NEXT_INPUT_TOKEN_MAX : context - output
3839
return count > usable
3940
}
4041

@@ -47,8 +48,7 @@ export namespace SessionCompaction {
4748
// calls. then erases output of previous tool calls. idea is to throw away old
4849
// tool calls that are no longer relevant.
4950
export async function prune(input: { sessionID: string }) {
50-
const config = await Config.get()
51-
if (config.compaction?.prune === false) return
51+
if (Flag.OPENCODE_DISABLE_PRUNE) return
5252
log.info("pruning")
5353
const msgs = await Session.messages({ sessionID: input.sessionID })
5454
let total = 0

0 commit comments

Comments
 (0)