fix(delegate-task): add token counting and truncation to prevent context overflow

Fixes #1815, #1733
This commit is contained in:
YeonGyu-Kim
2026-02-25 14:03:47 +09:00
parent 15519b9580
commit fc1b6e4917
8 changed files with 296 additions and 23 deletions
+32 -18
View File
@@ -1,5 +1,21 @@
import type { BuildSystemContentInput } from "./types"
import { buildPlanAgentSystemPrepend, isPlanAgent } from "./constants"
import { buildSystemContentWithTokenLimit } from "./token-limiter"
const FREE_OR_LOCAL_PROMPT_TOKEN_LIMIT = 24000
function usesFreeOrLocalModel(model: { providerID: string; modelID: string; variant?: string } | undefined): boolean {
if (!model) {
return false
}
const provider = model.providerID.toLowerCase()
const modelId = model.modelID.toLowerCase()
return provider.includes("local")
|| provider === "ollama"
|| provider === "lmstudio"
|| modelId.includes("free")
}
/**
* Build the system content to inject into the agent prompt.
@@ -8,7 +24,11 @@ import { buildPlanAgentSystemPrepend, isPlanAgent } from "./constants"
export function buildSystemContent(input: BuildSystemContentInput): string | undefined {
const {
skillContent,
skillContents,
categoryPromptAppend,
agentsContext,
maxPromptTokens,
model,
agentName,
availableCategories,
availableSkills,
@@ -18,23 +38,17 @@ export function buildSystemContent(input: BuildSystemContentInput): string | und
? buildPlanAgentSystemPrepend(availableCategories, availableSkills)
: ""
if (!skillContent && !categoryPromptAppend && !planAgentPrepend) {
return undefined
}
const effectiveMaxPromptTokens = maxPromptTokens
?? (usesFreeOrLocalModel(model) ? FREE_OR_LOCAL_PROMPT_TOKEN_LIMIT : undefined)
const parts: string[] = []
if (planAgentPrepend) {
parts.push(planAgentPrepend)
}
if (skillContent) {
parts.push(skillContent)
}
if (categoryPromptAppend) {
parts.push(categoryPromptAppend)
}
return parts.join("\n\n") || undefined
return buildSystemContentWithTokenLimit(
{
skillContent,
skillContents,
categoryPromptAppend,
agentsContext: agentsContext ?? planAgentPrepend,
planAgentPrepend,
},
effectiveMaxPromptTokens
)
}