Skip to content
Open
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
9 changes: 5 additions & 4 deletions packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ export namespace SessionCompaction {
),
}

export const PRUNE_MINIMUM = 20_000
export const PRUNE_PROTECT = 40_000
export const OVERHEAD_BUFFER = 50_000

export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
const config = await Config.get()
if (config.compaction?.auto === false) return false
const context = input.model.limit.context
if (context === 0) return false
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
const usable = context - output
const usable = context - output - OVERHEAD_BUFFER
return count > usable
}

export const PRUNE_MINIMUM = 20_000
export const PRUNE_PROTECT = 40_000

const PRUNE_PROTECTED_TOOLS = ["skill"]

// goes backwards through parts until there are 40_000 tokens worth of tool
Expand Down
9 changes: 7 additions & 2 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,17 @@ export namespace SessionPrompt {
// Add support for other types if needed
}

const text = textParts.join("\n\n")
const output = text.length > 30_000
? text.slice(0, 30_000) + `\n\n[MCP output truncated: exceeded 30000 char limit]`
: text

return {
title: "",
metadata: result.metadata ?? {},
output: textParts.join("\n\n"),
output,
attachments,
content: result.content, // directly return content to preserve ordering when outputting to model
content: result.content,
}
}
item.toModelOutput = (result) => {
Expand Down
54 changes: 10 additions & 44 deletions packages/opencode/src/tool/webfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,53 +87,19 @@ export const WebFetchTool = Tool.define("webfetch", {
const contentType = response.headers.get("content-type") || ""

const title = `${params.url} (${contentType})`
const isHtml = contentType.includes("text/html")

// Handle content based on requested format and actual content type
switch (params.format) {
case "markdown":
if (contentType.includes("text/html")) {
const markdown = convertHTMLToMarkdown(content)
return {
output: markdown,
title,
metadata: {},
}
}
return {
output: content,
title,
metadata: {},
}
const raw = params.format === "markdown" && isHtml
? convertHTMLToMarkdown(content)
: params.format === "text" && isHtml
? await extractTextFromHTML(content)
: content

case "text":
if (contentType.includes("text/html")) {
const text = await extractTextFromHTML(content)
return {
output: text,
title,
metadata: {},
}
}
return {
output: content,
title,
metadata: {},
}
const output = raw.length > 30_000
? raw.slice(0, 30_000) + `\n\n[Output truncated: exceeded 30000 char limit]`
: raw

case "html":
return {
output: content,
title,
metadata: {},
}

default:
return {
output: content,
title,
metadata: {},
}
}
return { output, title, metadata: {} }
},
})

Expand Down