Skip to content

Commit 49d2289

Browse files
authored
Merge pull request #355 from tobwen/feature/percentage-context-limit
Good idea! I'll probably change the default to a % and add it to the readme default config. Thank you for your help!
2 parents 40592b0 + 41b9afb commit 49d2289

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

dcp.schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"description": "Tool names that should be protected from automatic pruning"
111111
},
112112
"contextLimit": {
113-
"description": "When session tokens exceed this limit, a compress nudge is injected (\"model\" uses the active model's context limit)",
113+
"description": "When session tokens exceed this limit, a compress nudge is injected (\"model\" uses the active model's context limit, \"X%\" uses percentage of the model's context window)",
114114
"default": 100000,
115115
"oneOf": [
116116
{
@@ -119,6 +119,10 @@
119119
{
120120
"type": "string",
121121
"enum": ["model"]
122+
},
123+
{
124+
"type": "string",
125+
"pattern": "^\\d+(?:\\.\\d+)?%$"
122126
}
123127
]
124128
}

lib/config.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface ToolSettings {
2727
nudgeEnabled: boolean
2828
nudgeFrequency: number
2929
protectedTools: string[]
30-
contextLimit: number | "model"
30+
contextLimit: number | "model" | `${number}%`
3131
}
3232

3333
export interface Tools {
@@ -290,13 +290,16 @@ function validateConfigTypes(config: Record<string, any>): ValidationError[] {
290290
})
291291
}
292292
if (tools.settings.contextLimit !== undefined) {
293-
if (
294-
typeof tools.settings.contextLimit !== "number" &&
295-
tools.settings.contextLimit !== "model"
296-
) {
293+
const isValidNumber = typeof tools.settings.contextLimit === "number"
294+
const isModelString = tools.settings.contextLimit === "model"
295+
const isPercentString =
296+
typeof tools.settings.contextLimit === "string" &&
297+
tools.settings.contextLimit.endsWith("%")
298+
299+
if (!isValidNumber && !isModelString && !isPercentString) {
297300
errors.push({
298301
key: "tools.settings.contextLimit",
299-
expected: 'number | "model"',
302+
expected: 'number | "model" | "${number}%"',
300303
actual: JSON.stringify(tools.settings.contextLimit),
301304
})
302305
}

lib/messages/inject.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import { getFilePathsFromParameters, isProtected } from "../protected-file-patte
1313
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"
1414
import { getCurrentTokenUsage } from "../strategies/utils"
1515

16+
function parsePercentageString(value: string, total: number): number | undefined {
17+
if (!value.endsWith("%")) return undefined
18+
const percent = parseFloat(value.slice(0, -1))
19+
20+
if (isNaN(percent)) {
21+
return undefined
22+
}
23+
24+
const roundedPercent = Math.round(percent)
25+
const clampedPercent = Math.max(0, Math.min(100, roundedPercent))
26+
27+
return Math.round((clampedPercent / 100) * total)
28+
}
29+
1630
// XML wrappers
1731
export const wrapPrunableTools = (content: string): string => {
1832
return `<prunable-tools>
@@ -54,9 +68,22 @@ Context management was just performed. Do NOT use the ${toolName} again. A fresh
5468

5569
const resolveContextLimit = (config: PluginConfig, state: SessionState): number | undefined => {
5670
const configLimit = config.tools.settings.contextLimit
57-
if (configLimit === "model") {
58-
return state.modelContextLimit
71+
72+
if (typeof configLimit === "string") {
73+
if (configLimit.endsWith("%")) {
74+
if (state.modelContextLimit === undefined) {
75+
return undefined
76+
}
77+
return parsePercentageString(configLimit, state.modelContextLimit)
78+
}
79+
80+
if (configLimit === "model") {
81+
return state.modelContextLimit
82+
}
83+
84+
return undefined
5985
}
86+
6087
return configLimit
6188
}
6289

0 commit comments

Comments
 (0)