Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/opencode/src/provider/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export namespace ModelsDev {
limit: z.object({
context: z.number(),
output: z.number(),
input: z.number().optional(),
}),
modalities: z
.object({
Expand Down
2 changes: 2 additions & 0 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export namespace Provider {
limit: z.object({
context: z.number(),
output: z.number(),
input: z.number().optional(),
}),
status: z.enum(["alpha", "beta", "deprecated", "active"]),
options: z.record(z.string(), z.any()),
Expand Down Expand Up @@ -527,6 +528,7 @@ export namespace Provider {
limit: {
context: model.limit.context,
output: model.limit.output,
input: model.limit.input,
},
capabilities: {
temperature: model.temperature,
Expand Down
4 changes: 3 additions & 1 deletion packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export namespace SessionCompaction {
),
}

const NEXT_INPUT_TOKEN_MAX = 20_000

export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
const config = await Config.get()
if (config.compaction?.auto === false) return false
const context = input.model.limit.context
if (context === 0) return false
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
const usable = context - output
const usable = input.model.limit.input ? input.model.limit.input - NEXT_INPUT_TOKEN_MAX : context - output
return count > usable
}

Expand Down
15 changes: 14 additions & 1 deletion packages/opencode/test/session/compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import type { Provider } from "../../src/provider/provider"

Log.init({ print: false })

function createModel(opts: { context: number; output: number; cost?: Provider.Model["cost"] }): Provider.Model {
function createModel(opts: { context: number; output: number; inputLimit?: number; cost?: Provider.Model["cost"] }): Provider.Model {
return {
id: "test-model",
providerID: "test",
name: "Test",
limit: {
context: opts.context,
output: opts.output,
input: opts.inputLimit,
},
cost: opts.cost ?? { input: 0, output: 0, cache: { read: 0, write: 0 } },
capabilities: {
Expand Down Expand Up @@ -102,6 +103,18 @@ describe("session.compaction.isOverflow", () => {
},
})
})

test("returns true when input limit is reached", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const model = createModel({ context: 100_000, output: 32_000, inputLimit: 50_000 })
const tokens = { input: 40_000, output: 5_000, reasoning: 0, cache: { read: 0, write: 0 } }
expect(await SessionCompaction.isOverflow({ tokens, model })).toBe(true)
},
})
})
})

describe("util.token.estimate", () => {
Expand Down