Bläddra i källkod

feat(tui): add heap snapshot functionality for TUI and server (#19028)

Dax 3 veckor sedan
förälder
incheckning
15dc33d1a3

+ 15 - 3
.opencode/command/changelog.md

@@ -1,5 +1,17 @@
-go through each PR merged since the last tag
+create UPCOMING_CHANGELOG.md
+
+it should have sections
+
+```
+# TUI
+
+# Desktop
 
 
-for each PR spawn a subagent to summarize what the PR was about. focus on user facing changes. if it was entirely internal or code related you can ignore it. also skip docs updates. each subagent should append its summary to UPCOMING_CHANGELOG.md
+# Core
+
+# Misc
+```
+
+go through each PR merged since the last tag
 
 
-once that is done, read UPCOMING_CHANGELOG.md and group it into sections for better readability. make sure all PR references are preserved
+for each PR spawn a subagent to summarize what the PR was about. focus on user facing changes. if it was entirely internal or code related you can ignore it. also skip docs updates. each subagent should append its summary to UPCOMING_CHANGELOG.md into the appropriate section.

+ 6 - 5
packages/opencode/src/cli/cmd/tui/app.tsx

@@ -110,6 +110,7 @@ export function tui(input: {
   url: string
   url: string
   args: Args
   args: Args
   config: TuiConfig.Info
   config: TuiConfig.Info
+  onSnapshot?: () => Promise<string[]>
   directory?: string
   directory?: string
   fetch?: typeof fetch
   fetch?: typeof fetch
   headers?: RequestInit["headers"]
   headers?: RequestInit["headers"]
@@ -160,7 +161,7 @@ export function tui(input: {
                                         <FrecencyProvider>
                                         <FrecencyProvider>
                                           <PromptHistoryProvider>
                                           <PromptHistoryProvider>
                                             <PromptRefProvider>
                                             <PromptRefProvider>
-                                              <App />
+                                              <App onSnapshot={input.onSnapshot} />
                                             </PromptRefProvider>
                                             </PromptRefProvider>
                                           </PromptHistoryProvider>
                                           </PromptHistoryProvider>
                                         </FrecencyProvider>
                                         </FrecencyProvider>
@@ -201,7 +202,7 @@ export function tui(input: {
   })
   })
 }
 }
 
 
-function App() {
+function App(props: { onSnapshot?: () => Promise<string[]> }) {
   const route = useRoute()
   const route = useRoute()
   const dimensions = useTerminalDimensions()
   const dimensions = useTerminalDimensions()
   const renderer = useRenderer()
   const renderer = useRenderer()
@@ -627,11 +628,11 @@ function App() {
       title: "Write heap snapshot",
       title: "Write heap snapshot",
       category: "System",
       category: "System",
       value: "app.heap_snapshot",
       value: "app.heap_snapshot",
-      onSelect: (dialog) => {
-        const path = writeHeapSnapshot()
+      onSelect: async (dialog) => {
+        const files = await props.onSnapshot?.()
         toast.show({
         toast.show({
           variant: "info",
           variant: "info",
-          message: `Heap snapshot written to ${path}`,
+          message: `Heap snapshot written to ${files?.join(", ")}`,
           duration: 5000,
           duration: 5000,
         })
         })
         dialog.clear()
         dialog.clear()

+ 6 - 0
packages/opencode/src/cli/cmd/tui/thread.ts

@@ -14,6 +14,7 @@ import type { EventSource } from "./context/sdk"
 import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
 import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
 import { TuiConfig } from "@/config/tui"
 import { TuiConfig } from "@/config/tui"
 import { Instance } from "@/project/instance"
 import { Instance } from "@/project/instance"
+import { writeHeapSnapshot } from "v8"
 
 
 declare global {
 declare global {
   const OPENCODE_WORKER_PATH: string
   const OPENCODE_WORKER_PATH: string
@@ -201,6 +202,11 @@ export const TuiThreadCommand = cmd({
       try {
       try {
         await tui({
         await tui({
           url: transport.url,
           url: transport.url,
+          async onSnapshot() {
+            const tui = writeHeapSnapshot("tui.heapsnapshot")
+            const server = await client.call("snapshot", undefined)
+            return [tui, server]
+          },
           config,
           config,
           directory: cwd,
           directory: cwd,
           fetch: transport.fetch,
           fetch: transport.fetch,

+ 5 - 0
packages/opencode/src/cli/cmd/tui/worker.ts

@@ -10,6 +10,7 @@ import { GlobalBus } from "@/bus/global"
 import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
 import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
 import { Flag } from "@/flag/flag"
 import { Flag } from "@/flag/flag"
 import { setTimeout as sleep } from "node:timers/promises"
 import { setTimeout as sleep } from "node:timers/promises"
+import { writeHeapSnapshot } from "node:v8"
 
 
 await Log.init({
 await Log.init({
   print: process.argv.includes("--print-logs"),
   print: process.argv.includes("--print-logs"),
@@ -117,6 +118,10 @@ export const rpc = {
       body,
       body,
     }
     }
   },
   },
+  snapshot() {
+    const result = writeHeapSnapshot("server.heapsnapshot")
+    return result
+  },
   async server(input: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
   async server(input: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
     if (server) await server.stop(true)
     if (server) await server.stop(true)
     server = await Server.listen(input)
     server = await Server.listen(input)