Skip to content

Commit 15dc33d

Browse files
authored
feat(tui): add heap snapshot functionality for TUI and server (anomalyco#19028)
1 parent 1398674 commit 15dc33d

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

.opencode/command/changelog.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
go through each PR merged since the last tag
1+
create UPCOMING_CHANGELOG.md
2+
3+
it should have sections
4+
5+
```
6+
# TUI
7+
8+
# Desktop
29
3-
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
10+
# Core
11+
12+
# Misc
13+
```
14+
15+
go through each PR merged since the last tag
416

5-
once that is done, read UPCOMING_CHANGELOG.md and group it into sections for better readability. make sure all PR references are preserved
17+
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.

packages/opencode/src/cli/cmd/tui/app.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export function tui(input: {
110110
url: string
111111
args: Args
112112
config: TuiConfig.Info
113+
onSnapshot?: () => Promise<string[]>
113114
directory?: string
114115
fetch?: typeof fetch
115116
headers?: RequestInit["headers"]
@@ -160,7 +161,7 @@ export function tui(input: {
160161
<FrecencyProvider>
161162
<PromptHistoryProvider>
162163
<PromptRefProvider>
163-
<App />
164+
<App onSnapshot={input.onSnapshot} />
164165
</PromptRefProvider>
165166
</PromptHistoryProvider>
166167
</FrecencyProvider>
@@ -201,7 +202,7 @@ export function tui(input: {
201202
})
202203
}
203204

204-
function App() {
205+
function App(props: { onSnapshot?: () => Promise<string[]> }) {
205206
const route = useRoute()
206207
const dimensions = useTerminalDimensions()
207208
const renderer = useRenderer()
@@ -627,11 +628,11 @@ function App() {
627628
title: "Write heap snapshot",
628629
category: "System",
629630
value: "app.heap_snapshot",
630-
onSelect: (dialog) => {
631-
const path = writeHeapSnapshot()
631+
onSelect: async (dialog) => {
632+
const files = await props.onSnapshot?.()
632633
toast.show({
633634
variant: "info",
634-
message: `Heap snapshot written to ${path}`,
635+
message: `Heap snapshot written to ${files?.join(", ")}`,
635636
duration: 5000,
636637
})
637638
dialog.clear()

packages/opencode/src/cli/cmd/tui/thread.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { EventSource } from "./context/sdk"
1414
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
1515
import { TuiConfig } from "@/config/tui"
1616
import { Instance } from "@/project/instance"
17+
import { writeHeapSnapshot } from "v8"
1718

1819
declare global {
1920
const OPENCODE_WORKER_PATH: string
@@ -201,6 +202,11 @@ export const TuiThreadCommand = cmd({
201202
try {
202203
await tui({
203204
url: transport.url,
205+
async onSnapshot() {
206+
const tui = writeHeapSnapshot("tui.heapsnapshot")
207+
const server = await client.call("snapshot", undefined)
208+
return [tui, server]
209+
},
204210
config,
205211
directory: cwd,
206212
fetch: transport.fetch,

packages/opencode/src/cli/cmd/tui/worker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { GlobalBus } from "@/bus/global"
1010
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
1111
import { Flag } from "@/flag/flag"
1212
import { setTimeout as sleep } from "node:timers/promises"
13+
import { writeHeapSnapshot } from "node:v8"
1314

1415
await Log.init({
1516
print: process.argv.includes("--print-logs"),
@@ -117,6 +118,10 @@ export const rpc = {
117118
body,
118119
}
119120
},
121+
snapshot() {
122+
const result = writeHeapSnapshot("server.heapsnapshot")
123+
return result
124+
},
120125
async server(input: { port: number; hostname: string; mdns?: boolean; cors?: string[] }) {
121126
if (server) await server.stop(true)
122127
server = await Server.listen(input)

0 commit comments

Comments
 (0)