Skip to content

Commit 4846476

Browse files
committed
remove more unused logic
1 parent a6f189c commit 4846476

File tree

6 files changed

+37
-54
lines changed

6 files changed

+37
-54
lines changed

src/core/Cline.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ export class Cline {
11671167
// Tools
11681168

11691169
async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> {
1170-
const contextWindow = this.api.getModel().info.contextWindow || 128_000
1170+
const contextWindow = this.api.getModel().info.contextWindow || 64_000 // minimum context (Deepseek)
11711171
const maxAllowedSize = getMaxAllowedSize(contextWindow)
11721172

11731173
const terminalInfo = await this.terminalManager.getOrCreateTerminal(cwd)
@@ -1340,7 +1340,7 @@ export class Cline {
13401340
if (previousRequest && previousRequest.text) {
13411341
const { tokensIn, tokensOut, cacheWrites, cacheReads }: ClineApiReqInfo = JSON.parse(previousRequest.text)
13421342
const totalTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0)
1343-
let contextWindow = this.api.getModel().info.contextWindow || 128_000
1343+
let contextWindow = this.api.getModel().info.contextWindow || 64_000 // minimum context (Deepseek)
13441344
// FIXME: hack to get anyone using openai compatible with deepseek to have the proper context window instead of the default 128k. We need a way for the user to specify the context window for models they input through openai compatible
13451345
if (this.api instanceof OpenAiHandler && this.api.getModel().id.toLowerCase().includes("deepseek")) {
13461346
contextWindow = 64_000
@@ -1986,7 +1986,7 @@ export class Cline {
19861986
}
19871987
}
19881988
// Get context window and used context from API model
1989-
const contextWindow = this.api.getModel().info.contextWindow || 128_000
1989+
const contextWindow = this.api.getModel().info.contextWindow || 64_000 // minimum context (Deepseek)
19901990
const maxAllowedSize = getMaxAllowedSize(contextWindow)
19911991

19921992
// now execute the tool like normal
@@ -3357,7 +3357,7 @@ export class Cline {
33573357
) {
33583358
return {
33593359
...block,
3360-
text: await parseMentions(block.text, cwd, this.urlContentFetcher),
3360+
text: await parseMentions(block.text, cwd, this.urlContentFetcher, this.api),
33613361
}
33623362
}
33633363
}

src/core/mentions/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export function openMention(mention?: string): void {
3838
}
3939
}
4040

41-
export async function parseMentions(text: string, cwd: string, urlContentFetcher: UrlContentFetcher): Promise<string> {
41+
export async function parseMentions(
42+
text: string,
43+
cwd: string,
44+
urlContentFetcher: UrlContentFetcher,
45+
api: { getModel: () => { info: { contextWindow?: number } } },
46+
): Promise<string> {
4247
const mentions: Set<string> = new Set()
4348
let parsedText = text.replace(mentionRegexGlobal, (match, mention) => {
4449
mentions.add(mention)
@@ -90,7 +95,7 @@ export async function parseMentions(text: string, cwd: string, urlContentFetcher
9095
} else if (mention.startsWith("/")) {
9196
const mentionPath = mention.slice(1)
9297
try {
93-
const content = await getFileOrFolderContent(mentionPath, cwd)
98+
const content = await getFileOrFolderContent(mentionPath, cwd, api)
9499
if (mention.endsWith("/")) {
95100
parsedText += `\n\n<folder_content path="${mentionPath}">\n${content}\n</folder_content>`
96101
} else {
@@ -145,7 +150,11 @@ export async function parseMentions(text: string, cwd: string, urlContentFetcher
145150
return parsedText
146151
}
147152

148-
async function getFileOrFolderContent(mentionPath: string, cwd: string): Promise<string> {
153+
async function getFileOrFolderContent(
154+
mentionPath: string,
155+
cwd: string,
156+
api: { getModel: () => { info: { contextWindow?: number } } },
157+
): Promise<string> {
149158
const absPath = path.resolve(cwd, mentionPath)
150159

151160
try {
@@ -156,7 +165,7 @@ async function getFileOrFolderContent(mentionPath: string, cwd: string): Promise
156165
if (isBinary) {
157166
return "(Binary file, unable to display content)"
158167
}
159-
const contextWindow = this.api.getModel().info.contextWindow || 128_000
168+
const contextWindow = api.getModel().info.contextWindow || 64_000 // minimum context (Deepseek)
160169
const content = await extractTextFromFile(absPath, contextWindow)
161170
return content
162171
} else if (stats.isDirectory()) {
@@ -178,7 +187,7 @@ async function getFileOrFolderContent(mentionPath: string, cwd: string): Promise
178187
if (isBinary) {
179188
return undefined
180189
}
181-
const contextWindow = this.api.getModel().info.contextWindow || 128_000
190+
const contextWindow = api.getModel().info.contextWindow || 64_000 // minimum context (Deepseek)
182191
const content = await extractTextFromFile(absoluteFilePath, contextWindow)
183192
return `<file_content path="${filePath.toPosix()}">\n${content}\n</file_content>`
184193
} catch (error) {

src/integrations/misc/extract-text.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import fs from "fs/promises"
44
import path from "path"
55
import os from "os"
66
import { ContentTooLargeError } from "../../shared/errors"
7-
import { calculateMaxAllowedSize } from "../../utils/content-size"
87

9-
const CONTEXT_LIMIT = 1000
8+
const CONTEXT_LIMIT = 1000 // Context limit of 1000 tokens means max allowed size is 500 tokens
109

1110
describe("extract-text", () => {
1211
let tempFilePath: string
@@ -29,13 +28,13 @@ describe("extract-text", () => {
2928
}
3029
})
3130

32-
it("throws ContentTooLargeError when file would exceed half of context limit", async () => {
33-
const halfContextLimit = calculateMaxAllowedSize(CONTEXT_LIMIT) // 500 tokens
34-
const largeContent = "x".repeat(halfContextLimit * 4 + 4) // Just over half context limit in tokens
31+
it("throws ContentTooLargeError when file would exceed max allowed size", async () => {
32+
// Create content that would exceed max allowed size for deepseek (64k - 27k tokens)
33+
const largeContent = "x".repeat(148000) // 37k tokens > (64k - 27k) tokens
3534
await fs.writeFile(tempFilePath, largeContent)
3635

3736
try {
38-
await extractTextFromFile(tempFilePath, CONTEXT_LIMIT)
37+
await extractTextFromFile(tempFilePath, 64_000)
3938
throw new Error("Should have thrown error")
4039
} catch (error) {
4140
expect(error).to.be.instanceOf(ContentTooLargeError)

src/integrations/terminal/TerminalProcess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class TerminalProcess extends EventEmitter<TerminalProcessEvents> {
2525
isHot: boolean = false
2626
private hotTimer: NodeJS.Timeout | null = null
2727
private totalBytes: number = 0
28-
private contextLimit: number = 100000 // Default context window size
28+
private contextLimit: number = 64_000 // minimum context (Deepseek)
2929
private lastCommand: string = ""
3030

3131
// constructor() {

src/utils/content-size.test.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
import { expect } from "chai"
2-
import {
3-
estimateContentSize,
4-
estimateFileSize,
5-
estimateTokens,
6-
calculateMaxAllowedSize,
7-
wouldExceedSizeLimit,
8-
} from "./content-size"
2+
import { estimateContentSize, estimateFileSize, estimateTokens, wouldExceedSizeLimit } from "./content-size"
93
import fs from "fs/promises"
104
import path from "path"
115
import os from "os"
126

13-
const CONTEXT_LIMIT = 1000
7+
const CONTEXT_LIMIT = 1000 // Context limit of 1000 tokens means max allowed size is 500 tokens
148

159
describe("content-size", () => {
16-
describe("calculateMaxAllowedSize", () => {
17-
it("calculates half of the context limit", () => {
18-
expect(calculateMaxAllowedSize(1000)).to.equal(500)
19-
expect(calculateMaxAllowedSize(128000)).to.equal(64000)
20-
})
21-
})
22-
2310
describe("estimateTokens", () => {
2411
it("estimates tokens based on byte count", () => {
2512
expect(estimateTokens(100)).to.equal(25) // 100 bytes / 4 chars per token = 25 tokens
@@ -28,10 +15,10 @@ describe("content-size", () => {
2815
})
2916

3017
describe("wouldExceedSizeLimit", () => {
31-
it("checks if byte count would exceed half of context limit", () => {
32-
expect(wouldExceedSizeLimit(100, 1000)).to.equal(false) // 25 tokens < 500 tokens
33-
expect(wouldExceedSizeLimit(2000, 1000)).to.equal(true) // 500 tokens = 500 tokens (equal is considered exceeding)
34-
expect(wouldExceedSizeLimit(2004, 1000)).to.equal(true) // 501 tokens > 500 tokens
18+
it("checks if byte count would exceed max allowed size", () => {
19+
expect(wouldExceedSizeLimit(100, 64_000)).to.equal(false) // 25 tokens < (64k - 27k) tokens
20+
expect(wouldExceedSizeLimit(148000, 64_000)).to.equal(true) // 37k tokens > (64k - 27k) tokens
21+
expect(wouldExceedSizeLimit(392000, 128_000)).to.equal(true) // 98k tokens > (128k - 30k) tokens
3522
})
3623
})
3724

@@ -54,10 +41,10 @@ describe("content-size", () => {
5441
expect(result.wouldExceedLimit).to.equal(false)
5542
})
5643

57-
it("detects when content would exceed half of context limit", () => {
58-
const halfContextLimit = calculateMaxAllowedSize(CONTEXT_LIMIT) // 500 tokens
59-
const largeContent = "x".repeat(halfContextLimit * 4 + 4) // Just over half context limit in tokens
60-
const result = estimateContentSize(largeContent, CONTEXT_LIMIT)
44+
it("detects when content would exceed max allowed size", () => {
45+
// Create content that would exceed max allowed size for deepseek (64k - 27k tokens)
46+
const largeContent = "x".repeat(148000) // 37k tokens > (64k - 27k) tokens
47+
const result = estimateContentSize(largeContent, 64_000)
6148

6249
expect(result.wouldExceedLimit).to.equal(true)
6350
})

src/utils/content-size.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ export interface SizeEstimate {
99
wouldExceedLimit: boolean
1010
}
1111

12-
/**
13-
* Calculates the maximum allowed size for a single content item (file or terminal output)
14-
* We limit to half the context window to ensure no single item can consume too much context
15-
*/
16-
export function calculateMaxAllowedSize(contextLimit: number): number {
17-
return Math.floor(contextLimit / 2)
18-
}
19-
2012
/**
2113
* Estimates tokens from byte count using a simple character ratio
2214
* This is a rough approximation - actual token count may vary
@@ -31,7 +23,7 @@ export function estimateTokens(bytes: number): number {
3123
*/
3224
export function wouldExceedSizeLimit(byteCount: number, contextLimit: number): boolean {
3325
const estimatedTokenCount = estimateTokens(byteCount)
34-
const maxAllowedSize = calculateMaxAllowedSize(contextLimit)
26+
const maxAllowedSize = getMaxAllowedSize(contextLimit)
3527
return estimatedTokenCount >= maxAllowedSize
3628
}
3729

@@ -41,12 +33,11 @@ export function wouldExceedSizeLimit(byteCount: number, contextLimit: number): b
4133
export function estimateContentSize(content: string | Buffer, contextLimit: number): SizeEstimate {
4234
const bytes = Buffer.isBuffer(content) ? content.length : Buffer.from(content).length
4335
const estimatedTokenCount = estimateTokens(bytes)
44-
const maxAllowedSize = calculateMaxAllowedSize(contextLimit)
4536

4637
return {
4738
bytes,
4839
estimatedTokens: estimatedTokenCount,
49-
wouldExceedLimit: estimatedTokenCount >= maxAllowedSize,
40+
wouldExceedLimit: estimatedTokenCount >= getMaxAllowedSize(contextLimit),
5041
}
5142
}
5243

@@ -57,19 +48,16 @@ export async function estimateFileSize(filePath: string, contextLimit: number):
5748
const stats = await stat(filePath)
5849
const bytes = stats.size
5950
const estimatedTokenCount = estimateTokens(bytes)
60-
const maxAllowedSize = calculateMaxAllowedSize(contextLimit)
6151

6252
return {
6353
bytes,
6454
estimatedTokens: estimatedTokenCount,
65-
wouldExceedLimit: estimatedTokenCount >= maxAllowedSize,
55+
wouldExceedLimit: estimatedTokenCount >= getMaxAllowedSize(contextLimit),
6656
}
6757
}
6858

6959
/**
7060
* Gets the maximum allowed size for the API context window
71-
* This is different from calculateMaxAllowedSize as it's for the entire context window
72-
* rather than a single content item
7361
*/
7462
export function getMaxAllowedSize(contextWindow: number): number {
7563
// Get context window and used context from API model

0 commit comments

Comments
 (0)