mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 06:12:26 +00:00
fix(session): check for context overflow mid-turn in finish-step (#6480)
This commit is contained in:
@@ -13,6 +13,7 @@ import { Plugin } from "@/plugin"
|
||||
import type { Provider } from "@/provider/provider"
|
||||
import { LLM } from "./llm"
|
||||
import { Config } from "@/config/config"
|
||||
import { SessionCompaction } from "./compaction"
|
||||
|
||||
export namespace SessionProcessor {
|
||||
const DOOM_LOOP_THRESHOLD = 3
|
||||
@@ -31,6 +32,7 @@ export namespace SessionProcessor {
|
||||
let snapshot: string | undefined
|
||||
let blocked = false
|
||||
let attempt = 0
|
||||
let needsCompaction = false
|
||||
|
||||
const result = {
|
||||
get message() {
|
||||
@@ -41,6 +43,7 @@ export namespace SessionProcessor {
|
||||
},
|
||||
async process(streamInput: LLM.StreamInput) {
|
||||
log.info("process")
|
||||
needsCompaction = false
|
||||
const shouldBreak = (await Config.get()).experimental?.continue_loop_on_deny !== true
|
||||
while (true) {
|
||||
try {
|
||||
@@ -279,6 +282,9 @@ export namespace SessionProcessor {
|
||||
sessionID: input.sessionID,
|
||||
messageID: input.assistantMessage.parentID,
|
||||
})
|
||||
if (await SessionCompaction.isOverflow({ tokens: usage.tokens, model: input.model })) {
|
||||
needsCompaction = true
|
||||
}
|
||||
break
|
||||
|
||||
case "text-start":
|
||||
@@ -339,6 +345,7 @@ export namespace SessionProcessor {
|
||||
})
|
||||
continue
|
||||
}
|
||||
if (needsCompaction) break
|
||||
}
|
||||
} catch (e: any) {
|
||||
log.error("process", {
|
||||
@@ -398,6 +405,7 @@ export namespace SessionProcessor {
|
||||
}
|
||||
input.assistantMessage.time.completed = Date.now()
|
||||
await Session.updateMessage(input.assistantMessage)
|
||||
if (needsCompaction) return "compact"
|
||||
if (blocked) return "stop"
|
||||
if (input.assistantMessage.error) return "stop"
|
||||
return "continue"
|
||||
|
||||
@@ -549,6 +549,14 @@ export namespace SessionPrompt {
|
||||
model,
|
||||
})
|
||||
if (result === "stop") break
|
||||
if (result === "compact") {
|
||||
await SessionCompaction.create({
|
||||
sessionID,
|
||||
agent: lastUser.agent,
|
||||
model: lastUser.model,
|
||||
auto: true,
|
||||
})
|
||||
}
|
||||
continue
|
||||
}
|
||||
SessionCompaction.prune({ sessionID })
|
||||
|
||||
Reference in New Issue
Block a user