Skip to content
Merged
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
6 changes: 5 additions & 1 deletion dcp.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"description": "Tool names that should be protected from automatic pruning"
},
"contextLimit": {
"description": "When session tokens exceed this limit, a compress nudge is injected (\"model\" uses the active model's context limit)",
"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)",
"default": 100000,
"oneOf": [
{
Expand All @@ -119,6 +119,10 @@
{
"type": "string",
"enum": ["model"]
},
{
"type": "string",
"pattern": "^\\d+(?:\\.\\d+)?%$"
}
]
}
Expand Down
15 changes: 9 additions & 6 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ToolSettings {
nudgeEnabled: boolean
nudgeFrequency: number
protectedTools: string[]
contextLimit: number | "model"
contextLimit: number | "model" | `${number}%`
}

export interface Tools {
Expand Down Expand Up @@ -290,13 +290,16 @@ function validateConfigTypes(config: Record<string, any>): ValidationError[] {
})
}
if (tools.settings.contextLimit !== undefined) {
if (
typeof tools.settings.contextLimit !== "number" &&
tools.settings.contextLimit !== "model"
) {
const isValidNumber = typeof tools.settings.contextLimit === "number"
const isModelString = tools.settings.contextLimit === "model"
const isPercentString =
typeof tools.settings.contextLimit === "string" &&
tools.settings.contextLimit.endsWith("%")

if (!isValidNumber && !isModelString && !isPercentString) {
errors.push({
key: "tools.settings.contextLimit",
expected: 'number | "model"',
expected: 'number | "model" | "${number}%"',
actual: JSON.stringify(tools.settings.contextLimit),
})
}
Expand Down
31 changes: 29 additions & 2 deletions lib/messages/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ import { getFilePathsFromParameters, isProtected } from "../protected-file-patte
import { getLastUserMessage, isMessageCompacted } from "../shared-utils"
import { getCurrentTokenUsage } from "../strategies/utils"

function parsePercentageString(value: string, total: number): number | undefined {
if (!value.endsWith("%")) return undefined
const percent = parseFloat(value.slice(0, -1))

if (isNaN(percent)) {
return undefined
}

const roundedPercent = Math.round(percent)
const clampedPercent = Math.max(0, Math.min(100, roundedPercent))

return Math.round((clampedPercent / 100) * total)
}

// XML wrappers
export const wrapPrunableTools = (content: string): string => {
return `<prunable-tools>
Expand Down Expand Up @@ -54,9 +68,22 @@ Context management was just performed. Do NOT use the ${toolName} again. A fresh

const resolveContextLimit = (config: PluginConfig, state: SessionState): number | undefined => {
const configLimit = config.tools.settings.contextLimit
if (configLimit === "model") {
return state.modelContextLimit

if (typeof configLimit === "string") {
if (configLimit.endsWith("%")) {
if (state.modelContextLimit === undefined) {
return undefined
}
return parsePercentageString(configLimit, state.modelContextLimit)
}

if (configLimit === "model") {
return state.modelContextLimit
}

return undefined
}

return configLimit
}

Expand Down