Skip to content

Commit b045f69

Browse files
committed
feat(lsp): add result limits to prevent token overflow
- Add DEFAULT_MAX_REFERENCES, DEFAULT_MAX_SYMBOLS, DEFAULT_MAX_DIAGNOSTICS (200 each) - Apply limits to lsp_find_references, lsp_document_symbols, lsp_workspace_symbols, lsp_diagnostics - Show truncation warning when results exceed limits
1 parent 725ec9b commit b045f69

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/tools/lsp/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export const SEVERITY_MAP: Record<number, string> = {
3636
4: "hint",
3737
}
3838

39+
export const DEFAULT_MAX_REFERENCES = 200
40+
export const DEFAULT_MAX_SYMBOLS = 200
41+
export const DEFAULT_MAX_DIAGNOSTICS = 200
42+
3943
export const BUILTIN_SERVERS: Record<string, Omit<LSPServerConfig, "id">> = {
4044
typescript: {
4145
command: ["typescript-language-server", "--stdio"],

src/tools/lsp/tools.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { tool } from "@opencode-ai/plugin/tool"
22
import { getAllServers } from "./config"
3+
import {
4+
DEFAULT_MAX_REFERENCES,
5+
DEFAULT_MAX_SYMBOLS,
6+
DEFAULT_MAX_DIAGNOSTICS,
7+
} from "./constants"
38
import {
49
withLspClient,
510
formatHoverResult,
@@ -112,7 +117,14 @@ export const lsp_find_references = tool({
112117
return output
113118
}
114119

115-
const output = result.map(formatLocation).join("\n")
120+
const total = result.length
121+
const truncated = total > DEFAULT_MAX_REFERENCES
122+
const limited = truncated ? result.slice(0, DEFAULT_MAX_REFERENCES) : result
123+
const lines = limited.map(formatLocation)
124+
if (truncated) {
125+
lines.unshift(`Found ${total} references (showing first ${DEFAULT_MAX_REFERENCES}):`)
126+
}
127+
const output = lines.join("\n")
116128
return output
117129
} catch (e) {
118130
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
@@ -138,13 +150,21 @@ export const lsp_document_symbols = tool({
138150
return output
139151
}
140152

141-
let output: string
142-
if ("range" in result[0]) {
143-
output = (result as DocumentSymbol[]).map((s) => formatDocumentSymbol(s)).join("\n")
153+
const total = result.length
154+
const truncated = total > DEFAULT_MAX_SYMBOLS
155+
const limited = truncated ? result.slice(0, DEFAULT_MAX_SYMBOLS) : result
156+
157+
const lines: string[] = []
158+
if (truncated) {
159+
lines.push(`Found ${total} symbols (showing first ${DEFAULT_MAX_SYMBOLS}):`)
160+
}
161+
162+
if ("range" in limited[0]) {
163+
lines.push(...(limited as DocumentSymbol[]).map((s) => formatDocumentSymbol(s)))
144164
} else {
145-
output = (result as SymbolInfo[]).map(formatSymbolInfo).join("\n")
165+
lines.push(...(limited as SymbolInfo[]).map(formatSymbolInfo))
146166
}
147-
return output
167+
return lines.join("\n")
148168
} catch (e) {
149169
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
150170
return output
@@ -171,8 +191,15 @@ export const lsp_workspace_symbols = tool({
171191
return output
172192
}
173193

174-
const limited = args.limit ? result.slice(0, args.limit) : result
175-
const output = limited.map(formatSymbolInfo).join("\n")
194+
const total = result.length
195+
const limit = Math.min(args.limit ?? DEFAULT_MAX_SYMBOLS, DEFAULT_MAX_SYMBOLS)
196+
const truncated = total > limit
197+
const limited = result.slice(0, limit)
198+
const lines = limited.map(formatSymbolInfo)
199+
if (truncated) {
200+
lines.unshift(`Found ${total} symbols (showing first ${limit}):`)
201+
}
202+
const output = lines.join("\n")
176203
return output
177204
} catch (e) {
178205
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
@@ -213,7 +240,14 @@ export const lsp_diagnostics = tool({
213240
return output
214241
}
215242

216-
const output = diagnostics.map(formatDiagnostic).join("\n")
243+
const total = diagnostics.length
244+
const truncated = total > DEFAULT_MAX_DIAGNOSTICS
245+
const limited = truncated ? diagnostics.slice(0, DEFAULT_MAX_DIAGNOSTICS) : diagnostics
246+
const lines = limited.map(formatDiagnostic)
247+
if (truncated) {
248+
lines.unshift(`Found ${total} diagnostics (showing first ${DEFAULT_MAX_DIAGNOSTICS}):`)
249+
}
250+
const output = lines.join("\n")
217251
return output
218252
} catch (e) {
219253
const output = `Error: ${e instanceof Error ? e.message : String(e)}`

0 commit comments

Comments
 (0)