瀏覽代碼

type union

Aiden Cline 3 月之前
父節點
當前提交
eb8c34735f

+ 1 - 1
packages/opencode/src/tool/registry.ts

@@ -69,7 +69,7 @@ export namespace ToolRegistry {
           return {
             title: "",
             output: out.truncated ? out.content : result,
-            metadata: { truncated: out.truncated, outputPath: out.outputPath },
+            metadata: { truncated: out.truncated, outputPath: out.truncated ? out.outputPath : undefined },
           }
         },
       }),

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

@@ -77,7 +77,7 @@ export namespace Tool {
             metadata: {
               ...result.metadata,
               truncated: truncated.truncated,
-              outputPath: truncated.outputPath,
+              ...(truncated.truncated && { outputPath: truncated.outputPath }),
             },
           }
         }

+ 1 - 5
packages/opencode/src/tool/truncation.ts

@@ -12,11 +12,7 @@ export namespace Truncate {
   export const DIR = path.join(Global.Path.data, "tool-output")
   const RETENTION_MS = 7 * 24 * 60 * 60 * 1000 // 7 days
 
-  export interface Result {
-    content: string
-    truncated: boolean
-    outputPath?: string
-  }
+  export type Result = { content: string; truncated: false } | { content: string; truncated: true; outputPath: string }
 
   export interface Options {
     maxLines?: number

+ 2 - 2
packages/opencode/test/tool/bash.test.ts

@@ -248,7 +248,7 @@ describe("tool.bash truncation", () => {
         )
         expect((result.metadata as any).truncated).toBe(true)
         expect(result.output).toContain("truncated")
-        expect(result.output).toContain("The tool output was too large")
+        expect(result.output).toContain("The tool call succeeded but the output was truncated")
       },
     })
   })
@@ -268,7 +268,7 @@ describe("tool.bash truncation", () => {
         )
         expect((result.metadata as any).truncated).toBe(true)
         expect(result.output).toContain("truncated")
-        expect(result.output).toContain("The tool output was too large")
+        expect(result.output).toContain("The tool call succeeded but the output was truncated")
       },
     })
   })

+ 7 - 5
packages/opencode/test/tool/truncation.test.ts

@@ -14,7 +14,7 @@ describe("Truncate", () => {
 
       expect(result.truncated).toBe(true)
       expect(result.content).toContain("truncated...")
-      expect(result.outputPath).toBeDefined()
+      if (result.truncated) expect(result.outputPath).toBeDefined()
     })
 
     test("returns content unchanged when under limits", async () => {
@@ -82,12 +82,13 @@ describe("Truncate", () => {
       const result = await Truncate.output(lines, { maxLines: 10 })
 
       expect(result.truncated).toBe(true)
+      expect(result.content).toContain("The tool call succeeded but the output was truncated")
+      expect(result.content).toContain("Grep")
+      if (!result.truncated) throw new Error("expected truncated")
       expect(result.outputPath).toBeDefined()
       expect(result.outputPath).toContain("tool_")
-      expect(result.content).toContain("The tool output was too large")
-      expect(result.content).toContain("Grep")
 
-      const written = await Bun.file(result.outputPath!).text()
+      const written = await Bun.file(result.outputPath).text()
       expect(written).toBe(lines)
     })
 
@@ -116,7 +117,8 @@ describe("Truncate", () => {
       const result = await Truncate.output(content)
 
       expect(result.truncated).toBe(false)
-      expect(result.outputPath).toBeUndefined()
+      if (result.truncated) throw new Error("expected not truncated")
+      expect("outputPath" in result).toBe(false)
     })
   })