Browse Source

fix: reduce overhead of task tool metadata

Aiden Cline 2 months ago
parent
commit
3e36069f41

+ 1 - 1
packages/opencode/src/cli/cmd/tui/routes/session/index.tsx

@@ -1522,7 +1522,7 @@ ToolRegistry.register<typeof TaskTool>({
           <box>
             <For each={props.metadata.summary ?? []}>
               {(task) => (
-                <text style={{ fg: theme.textMuted }}>
+                <text style={{ fg: task.state.status === "error" ? theme.error : theme.textMuted }}>
                   ∟ {Locale.titlecase(task.tool)} {task.state.status === "completed" ? task.state.title : ""}
                 </text>
               )}

+ 24 - 8
packages/opencode/src/tool/task.ts

@@ -52,16 +52,24 @@ export const TaskTool = Tool.define("task", async () => {
       })
 
       const messageID = Identifier.ascending("message")
-      const parts: Record<string, MessageV2.ToolPart> = {}
+      const parts: Record<string, { id: string; tool: string; state: { status: string; title?: string } }> = {}
       const unsub = Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
         if (evt.properties.part.sessionID !== session.id) return
         if (evt.properties.part.messageID === messageID) return
         if (evt.properties.part.type !== "tool") return
-        parts[evt.properties.part.id] = evt.properties.part
+        const part = evt.properties.part
+        parts[part.id] = {
+          id: part.id,
+          tool: part.tool,
+          state: {
+            status: part.state.status,
+            title: part.state.status === "completed" ? part.state.title : undefined,
+          },
+        }
         ctx.metadata({
           title: params.description,
           metadata: {
-            summary: Object.values(parts).sort((a, b) => a.id?.localeCompare(b.id)),
+            summary: Object.values(parts).sort((a, b) => a.id.localeCompare(b.id)),
             sessionId: session.id,
           },
         })
@@ -98,10 +106,18 @@ export const TaskTool = Tool.define("task", async () => {
         parts: promptParts,
       })
       unsub()
-      let all
-      all = await Session.messages({ sessionID: session.id })
-      all = all.filter((x) => x.info.role === "assistant")
-      all = all.flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
+      const messages = await Session.messages({ sessionID: session.id })
+      const summary = messages
+        .filter((x) => x.info.role === "assistant")
+        .flatMap((msg) => msg.parts.filter((x: any) => x.type === "tool") as MessageV2.ToolPart[])
+        .map((part) => ({
+          id: part.id,
+          tool: part.tool,
+          state: {
+            status: part.state.status,
+            title: part.state.status === "completed" ? part.state.title : undefined,
+          },
+        }))
       const text = result.parts.findLast((x) => x.type === "text")?.text ?? ""
 
       const output = text + "\n\n" + ["<task_metadata>", `session_id: ${session.id}`, "</task_metadata>"].join("\n")
@@ -109,7 +125,7 @@ export const TaskTool = Tool.define("task", async () => {
       return {
         title: params.description,
         metadata: {
-          summary: all,
+          summary,
           sessionId: session.id,
         },
         output,