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
4 changes: 4 additions & 0 deletions packages/opencode/src/flag/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export namespace Flag {
export const OPENCODE_EXPERIMENTAL_MARKDOWN = truthy("OPENCODE_EXPERIMENTAL_MARKDOWN")
export const OPENCODE_MODELS_URL = process.env["OPENCODE_MODELS_URL"]
export const OPENCODE_MODELS_PATH = process.env["OPENCODE_MODELS_PATH"]
export const OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX = truthy(
"OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX",
)
export const OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT = process.env["OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT"]

function number(key: string) {
const value = process.env[key]
Expand Down
34 changes: 29 additions & 5 deletions packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { fn } from "@/util/fn"
import { Agent } from "@/agent/agent"
import { Plugin } from "@/plugin"
import { Config } from "@/config/config"
import { Flag } from "../flag/flag"
import { SystemPrompt } from "./system"
import { InstructionPrompt } from "./instruction"

export namespace SessionCompaction {
const log = Log.create({ service: "session.compaction" })
Expand Down Expand Up @@ -97,7 +100,8 @@ export namespace SessionCompaction {
auto: boolean
}) {
const userMessage = input.messages.findLast((m) => m.info.id === input.parentID)!.info as MessageV2.User
const agent = await Agent.get("compaction")
const usePrefixCache = Flag.OPENCODE_EXPERIMENTAL_COMPACTION_PRESERVE_PREFIX
const agent = usePrefixCache ? await Agent.get(userMessage.agent) : await Agent.get("compaction")
const model = agent.model
? await Provider.getModel(agent.model.providerID, agent.model.modelID)
: await Provider.getModel(userMessage.model.providerID, userMessage.model.modelID)
Expand Down Expand Up @@ -132,22 +136,38 @@ export namespace SessionCompaction {
model,
abort: input.abort,
})
const session = usePrefixCache ? await Session.get(input.sessionID) : undefined
let tools = {}
let system: string[] = []
if (usePrefixCache && session) {
tools = await SessionPrompt.resolveTools({
agent,
model,
session,
tools: userMessage.tools,
processor,
bypassAgentCheck: false,
messages: input.messages,
})
system = [...(await SystemPrompt.environment(model)), ...(await InstructionPrompt.system())]
}
// Allow plugins to inject context or replace compaction prompt
const compacting = await Plugin.trigger(
"experimental.session.compacting",
{ sessionID: input.sessionID },
{ context: [], prompt: undefined },
)
const defaultPrompt =
const defaultPrompt = Flag.OPENCODE_EXPERIMENTAL_COMPACTION_PROMPT ??
"Provide a detailed prompt for continuing our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next considering new session will not have access to our conversation."

const promptText = compacting.prompt ?? [defaultPrompt, ...compacting.context].join("\n\n")
const result = await processor.process({
let result = await processor.process({
user: userMessage,
agent,
abort: input.abort,
sessionID: input.sessionID,
tools: {},
system: [],
tools,
system,
messages: [
...MessageV2.toModelMessages(input.messages, model),
{
Expand All @@ -163,6 +183,10 @@ export namespace SessionCompaction {
model,
})

// ignore compact-flag for usePrefixCache
// needsCompact is currently set before context recalc and we don't utilize remove-tools-trick
result = usePrefixCache && result == "compact" ? "continue" : result

if (result === "continue" && input.auto) {
const continueMsg = await Session.updateMessage({
id: Identifier.ascending("message"),
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ export namespace SessionPrompt {
return Provider.defaultModel()
}

async function resolveTools(input: {
export async function resolveTools(input: {
agent: Agent.Info
model: Provider.Model
session: Session.Info
Expand Down
Loading