Browse Source

feat: pass mode into task tool (#1248)

Yihui Khuu 7 months ago
parent
commit
507c975e92

+ 5 - 2
packages/opencode/src/session/index.ts

@@ -370,6 +370,7 @@ export namespace Session {
     const l = log.clone().tag("session", input.sessionID)
     l.info("chatting")
 
+    const inputMode = input.mode ?? "build"
     const userMsg: MessageV2.Info = {
       id: input.messageID ?? Identifier.ascending("message"),
       role: "user",
@@ -494,7 +495,7 @@ export namespace Session {
         ]
       }),
     ).then((x) => x.flat())
-    if (input.mode === "plan")
+    if (inputMode === "plan")
       userParts.push({
         id: Identifier.ascending("part"),
         messageID: userMsg.id,
@@ -617,7 +618,7 @@ export namespace Session {
         .catch(() => {})
     }
 
-    const mode = await Mode.get(input.mode ?? "build")
+    const mode = await Mode.get(inputMode)
     let system = input.providerID === "anthropic" ? [PROMPT_ANTHROPIC_SPOOF.trim()] : []
     system.push(...(mode.prompt ? [mode.prompt] : SystemPrompt.provider(input.modelID)))
     system.push(...(await SystemPrompt.environment()))
@@ -630,6 +631,7 @@ export namespace Session {
       id: Identifier.ascending("message"),
       role: "assistant",
       system,
+      mode: inputMode,
       path: {
         cwd: app.path.cwd,
         root: app.path.root,
@@ -1122,6 +1124,7 @@ export namespace Session {
       role: "assistant",
       sessionID: input.sessionID,
       system,
+      mode: "build",
       path: {
         cwd: app.path.cwd,
         root: app.path.root,

+ 2 - 0
packages/opencode/src/session/message-v2.ts

@@ -226,6 +226,7 @@ export namespace MessageV2 {
     system: z.string().array(),
     modelID: z.string(),
     providerID: z.string(),
+    mode: z.string(),
     path: z.object({
       cwd: z.string(),
       root: z.string(),
@@ -290,6 +291,7 @@ export namespace MessageV2 {
         modelID: v1.metadata.assistant!.modelID,
         providerID: v1.metadata.assistant!.providerID,
         system: v1.metadata.assistant!.system,
+        mode: "build",
         error: v1.metadata.error,
       }
       const parts = v1.parts.flatMap((part): Part[] => {

+ 16 - 0
packages/opencode/src/storage/storage.ts

@@ -72,6 +72,22 @@ export namespace Storage {
         } catch (e) {}
       }
     },
+    async (dir: string) => {
+      const files = new Bun.Glob("session/message/*/*.json").scanSync({
+        cwd: dir,
+        absolute: true,
+      })
+      for (const file of files) {
+        try {
+          const content = await Bun.file(file).json()
+          if (content.role === "assistant" && !content.mode) {
+            log.info("adding mode field to message", { file })
+            content.mode = "build"
+            await Bun.write(file, JSON.stringify(content, null, 2))
+          }
+        } catch (e) {}
+      }
+    },
   ]
 
   const state = App.state("storage", async () => {

+ 1 - 0
packages/opencode/src/tool/task.ts

@@ -41,6 +41,7 @@ export const TaskTool = Tool.define({
       sessionID: session.id,
       modelID: msg.modelID,
       providerID: msg.providerID,
+      mode: msg.mode,
       tools: {
         todoread: false,
         todowrite: false,