瀏覽代碼

fix: preserve agent context during compaction (#4556)

Patrick Wolf 3 月之前
父節點
當前提交
416f2964b5

+ 10 - 0
packages/opencode/src/server/server.ts

@@ -813,8 +813,18 @@ export namespace Server {
         async (c) => {
         async (c) => {
           const id = c.req.valid("param").id
           const id = c.req.valid("param").id
           const body = c.req.valid("json")
           const body = c.req.valid("json")
+          const msgs = await Session.messages({ sessionID: id })
+          let currentAgent = "build"
+          for (let i = msgs.length - 1; i >= 0; i--) {
+            const info = msgs[i].info
+            if (info.role === "user") {
+              currentAgent = info.agent || "build"
+              break
+            }
+          }
           await SessionCompaction.create({
           await SessionCompaction.create({
             sessionID: id,
             sessionID: id,
+            agent: currentAgent,
             model: {
             model: {
               providerID: body.providerID,
               providerID: body.providerID,
               modelID: body.modelID,
               modelID: body.modelID,

+ 5 - 3
packages/opencode/src/session/compaction.ts

@@ -92,6 +92,7 @@ export namespace SessionCompaction {
       providerID: string
       providerID: string
       modelID: string
       modelID: string
     }
     }
+    agent: string
     abort: AbortSignal
     abort: AbortSignal
   }) {
   }) {
     const model = await Provider.getModel(input.model.providerID, input.model.modelID)
     const model = await Provider.getModel(input.model.providerID, input.model.modelID)
@@ -101,7 +102,7 @@ export namespace SessionCompaction {
       role: "assistant",
       role: "assistant",
       parentID: input.parentID,
       parentID: input.parentID,
       sessionID: input.sessionID,
       sessionID: input.sessionID,
-      mode: "build",
+      mode: input.agent,
       summary: true,
       summary: true,
       path: {
       path: {
         cwd: Instance.directory,
         cwd: Instance.directory,
@@ -199,7 +200,7 @@ export namespace SessionCompaction {
         time: {
         time: {
           created: Date.now(),
           created: Date.now(),
         },
         },
-        agent: "build",
+        agent: input.agent,
         model: input.model,
         model: input.model,
       })
       })
       await Session.updatePart({
       await Session.updatePart({
@@ -222,6 +223,7 @@ export namespace SessionCompaction {
   export const create = fn(
   export const create = fn(
     z.object({
     z.object({
       sessionID: Identifier.schema("session"),
       sessionID: Identifier.schema("session"),
+      agent: z.string(),
       model: z.object({
       model: z.object({
         providerID: z.string(),
         providerID: z.string(),
         modelID: z.string(),
         modelID: z.string(),
@@ -233,7 +235,7 @@ export namespace SessionCompaction {
         role: "user",
         role: "user",
         model: input.model,
         model: input.model,
         sessionID: input.sessionID,
         sessionID: input.sessionID,
-        agent: "build",
+        agent: input.agent,
         time: {
         time: {
           created: Date.now(),
           created: Date.now(),
         },
         },

+ 2 - 0
packages/opencode/src/session/prompt.ts

@@ -398,6 +398,7 @@ export namespace SessionPrompt {
           messages: msgs,
           messages: msgs,
           parentID: lastUser.id,
           parentID: lastUser.id,
           abort,
           abort,
+          agent: lastUser.agent,
           model: {
           model: {
             providerID: model.providerID,
             providerID: model.providerID,
             modelID: model.modelID,
             modelID: model.modelID,
@@ -416,6 +417,7 @@ export namespace SessionPrompt {
       ) {
       ) {
         await SessionCompaction.create({
         await SessionCompaction.create({
           sessionID,
           sessionID,
+          agent: lastUser.agent,
           model: lastUser.model,
           model: lastUser.model,
         })
         })
         continue
         continue