mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-07 09:18:41 +00:00
enable session pruning and allow disabling with OPENCODE_DISABLE_PRUNE
This commit is contained in:
@@ -16,7 +16,6 @@ import { Log } from "../util/log"
|
||||
import { MessageV2 } from "./message-v2"
|
||||
import { Project } from "../project/project"
|
||||
import { Instance } from "../project/instance"
|
||||
import { Token } from "../util/token"
|
||||
import { SessionPrompt } from "./prompt"
|
||||
|
||||
export namespace Session {
|
||||
@@ -293,34 +292,6 @@ export namespace Session {
|
||||
return part
|
||||
}
|
||||
|
||||
// goes backwards through parts until there are 40_000 tokens worth of tool
|
||||
// calls. then erases output of previous tool calls. idea is to throw away old
|
||||
// tool calls that are no longer relevant.
|
||||
export async function prune(input: { sessionID: string }) {
|
||||
const msgs = await messages(input.sessionID)
|
||||
let sum = 0
|
||||
for (let msgIndex = msgs.length - 2; msgIndex >= 0; msgIndex--) {
|
||||
const msg = msgs[msgIndex]
|
||||
if (msg.info.role === "assistant" && msg.info.summary) return
|
||||
for (let partIndex = msg.parts.length - 1; partIndex >= 0; partIndex--) {
|
||||
const part = msg.parts[partIndex]
|
||||
if (part.type === "tool")
|
||||
if (part.state.status === "completed") {
|
||||
if (part.state.time.compacted) return
|
||||
sum += Token.estimate(part.state.output)
|
||||
if (sum > 40_000) {
|
||||
log.info("pruning", {
|
||||
sum,
|
||||
id: part.id,
|
||||
})
|
||||
part.state.time.compacted = Date.now()
|
||||
await updatePart(part)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) {
|
||||
const tokens = {
|
||||
input: usage.inputTokens ?? 0,
|
||||
|
||||
Reference in New Issue
Block a user