Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
8 changes: 0 additions & 8 deletions lib/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,6 @@ export const stripHallucinations = (messages: WithParts[], format: IdFormat = "x
if (part.type === "text" && typeof part.text === "string") {
part.text = stripHallucinationsFromString(part.text, format)
}

if (
part.type === "tool" &&
part.state?.status === "completed" &&
typeof part.state.output === "string"
) {
part.state.output = stripHallucinationsFromString(part.state.output, format)
}
}
}
}
7 changes: 6 additions & 1 deletion lib/state/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export function findLastCompactionTimestamp(messages: WithParts[]): number {
if (!isMessageWithInfo(msg)) {
continue
}
if (msg.info.role === "assistant" && msg.info.summary === true) {
if (
msg.info.role === "assistant" &&
msg.info.summary === true &&
msg.info.finish &&
!msg.info.error
) {
return msg.info.time.created
}
}
Expand Down
14 changes: 12 additions & 2 deletions lib/token-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { SessionState, WithParts } from "./state"
import { AssistantMessage, UserMessage } from "@opencode-ai/sdk/v2"
import { Logger } from "./logger"
import * as _anthropicTokenizer from "@anthropic-ai/tokenizer"
const anthropicCountTokens = (_anthropicTokenizer.countTokens ??
(_anthropicTokenizer as any).default?.countTokens) as typeof _anthropicTokenizer.countTokens
const getAnthropicTokenizer = (_anthropicTokenizer.getTokenizer ??
(_anthropicTokenizer as any).default?.getTokenizer) as typeof _anthropicTokenizer.getTokenizer

// The package's countTokens() builds a new tokenizer on every call, which is slow.
// Build one once and reuse it, encoding the same way countTokens() does.
let anthropicTokenizer: ReturnType<typeof getAnthropicTokenizer> | undefined
function anthropicCountTokens(text: string): number {
if (!anthropicTokenizer) {
anthropicTokenizer = getAnthropicTokenizer()
}
return anthropicTokenizer.encode(text.normalize("NFKC"), "all").length
}
import { getLastUserMessage } from "./messages/query"

export function getCurrentTokenUsage(state: SessionState, messages: WithParts[]): number {
Expand Down
49 changes: 35 additions & 14 deletions lib/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,18 @@ export async function setup(ctx: Plugin.Context) {
if (state.isSubAgent && !config.experimental.allowSubAgents)
throw new Error("DCP compression is disabled in subagents")
if (state.compressPermission === "deny") throw new Error("DCP compression is denied")
if (state.compressPermission === "ask")
throw new Error(
"DCP: compress permission 'ask' is not supported by OpenCode V2's public plugin API yet. Compression was not performed.",
)
}

// permission.assert exists from OpenCode 2.0.29; the plugin types used here predate it.
const permission = ctx.permission as typeof ctx.permission & {
assert?: (input: {
sessionID: string
agent?: string
action: string
resources: string[]
save?: string[]
source?: { type: "tool"; messageID: string; id: string }
}) => Promise<void>
}

await ctx.model.transform((editor) => {
Expand All @@ -178,10 +186,10 @@ export async function setup(ctx: Plugin.Context) {
event.agent,
)
if (state.isSubAgent && !config.experimental.allowSubAgents) {
delete event.tools.compress
delete event.tools?.compress
return
}
if (state.compressPermission === "deny") delete event.tools.compress
if (state.compressPermission === "deny") delete event.tools?.compress
state.modelContextLimit = limits.get(`${event.model.providerID}/${event.model.id}`)
state.systemPromptTokens = countTokens(
event.system.map((part) => part.text).join("\n"),
Expand Down Expand Up @@ -267,7 +275,26 @@ export async function setup(ctx: Plugin.Context) {
directory: ctx.location.directory,
worktree: ctx.location.directory,
abort: new AbortController().signal,
ask: async () => allowed(state),
ask: async () => {
allowed(state)
if (state.compressPermission !== "ask") return
if (!permission.assert)
throw new Error(
"DCP: compress permission 'ask' requires OpenCode 2.0.29 or newer. Compression was not performed.",
)
await permission.assert({
sessionID: context.sessionID,
agent: context.agent,
action: "compress",
resources: ["*"],
save: ["*"],
source: {
type: "tool",
messageID: context.messageID,
id: context.id,
},
})
},
metadata: ({ title }: { title?: string }) => {
void context.progress({ title })
},
Expand Down Expand Up @@ -343,14 +370,8 @@ export async function setup(ctx: Plugin.Context) {
return {
manualMode: !!state.manualMode,
canCompress:
state.compressPermission === "allow" &&
state.compressPermission !== "deny" &&
(!state.isSubAgent || config.experimental.allowSubAgents),
...(state.compressPermission === "ask"
? {
blockedReason:
"Permission 'ask' is not supported by the V2 plugin API yet.",
}
: {}),
context: analyzeContextTokens(state, messages),
stats: await buildStatsReport(state, logger),
}
Expand Down
25 changes: 17 additions & 8 deletions lib/v2/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export function history(entries: History, session: Session): WithParts[] {
]
}
if (entry.type === "compaction" && entry.status === "completed") {
return [{ info: info(entry.id, "assistant", session, entry), parts: [] } as WithParts]
const summary = { ...info(entry.id, "assistant", session, entry), finish: "stop" }
return [{ info: summary, parts: [] } as WithParts]
}
return []
})
Expand All @@ -90,15 +91,23 @@ function output(content: ReadonlyArray<{ type: string; text?: string }>): unknow

export function project(native: Message[], entries: History, session: Session) {
const byID = new Map(entries.map((entry) => [entry.id, entry]))
const results = new Map<string, ToolResultPart>()
const owners = new Map<string, string>()
// Providers can reuse tool call IDs across turns, so pair each result with the
// closest preceding call that has the same ID.
const calls = new Map<string, { part: ContentPart; messageId?: string }>()
const results = new Map<ContentPart, ToolResultPart>()
const owners = new Map<ContentPart, string>()
const links = new Map<ContentPart, Part>()
const originals = new Map<string, WithParts>()
const messages: WithParts[] = []
for (const message of native)
for (const part of message.content) {
if (part.type === "tool-result") results.set(part.id, part)
if (part.type === "tool-call" && message.id) owners.set(part.id, message.id)
if (part.type === "tool-call") calls.set(part.id, { part, messageId: message.id })
if (part.type === "tool-result") {
const call = calls.get(part.id)
if (!call) continue
results.set(call.part, part)
if (call.messageId) owners.set(part, call.messageId)
}
}
for (const message of native) {
if (!message.id || !["user", "assistant"].includes(message.role)) continue
Expand All @@ -121,7 +130,7 @@ export function project(native: Message[], entries: History, session: Session) {
url: typeof part.data === "string" ? part.data : "",
}
if (part.type === "tool-call") {
const result = results.get(part.id)
const result = results.get(part)
const entry = byID.get(message.id)
const durable =
entry?.type === "assistant"
Expand Down Expand Up @@ -205,8 +214,8 @@ export function project(native: Message[], entries: History, session: Session) {
if (part.type === "tool-call") content.push(...added.splice(0))
if (
part.type === "tool-result" &&
originals.has(owners.get(part.id)!) &&
!retained.has(owners.get(part.id)!)
originals.has(owners.get(part)!) &&
!retained.has(owners.get(part)!)
)
continue
const edit = links.get(part)
Expand Down
Loading
Loading