Skip to content

Commit 809092b

Browse files
thdxrnipuna-perera
authored andcommitted
message rendering performance improvements
1 parent c4b2acb commit 809092b

File tree

5 files changed

+329
-259
lines changed

5 files changed

+329
-259
lines changed

packages/opencode/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import { DebugCommand } from "./cli/cmd/debug"
1717
import { StatsCommand } from "./cli/cmd/stats"
1818
import { McpCommand } from "./cli/cmd/mcp"
1919
import { InstallGithubCommand } from "./cli/cmd/install-github"
20+
import { Trace } from "./trace"
21+
22+
Trace.init()
2023

2124
const cancel = new AbortController()
2225

@@ -42,7 +45,7 @@ const cli = yargs(hideBin(process.argv))
4245
type: "boolean",
4346
})
4447
.middleware(async () => {
45-
await Log.init({ print: process.argv.includes("--print-logs") })
48+
await Log.init({ print: process.argv.includes("--print-logs"), dev: Installation.isDev() })
4649

4750
try {
4851
const { Config } = await import("./config/config")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Global } from "../global"
2+
import { Installation } from "../installation"
3+
import path from "path"
4+
5+
export namespace Trace {
6+
export function init() {
7+
if (!Installation.isDev()) return
8+
const writer = Bun.file(path.join(Global.Path.data, "log", "fetch.log")).writer()
9+
10+
const originalFetch = globalThis.fetch
11+
// @ts-expect-error
12+
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
13+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url
14+
const method = init?.method || "GET"
15+
16+
const urlObj = new URL(url)
17+
18+
writer.write(`\n${method} ${urlObj.pathname}${urlObj.search} HTTP/1.1\n`)
19+
writer.write(`Host: ${urlObj.host}\n`)
20+
21+
if (init?.headers) {
22+
if (init.headers instanceof Headers) {
23+
init.headers.forEach((value, key) => {
24+
writer.write(`${key}: ${value}\n`)
25+
})
26+
} else {
27+
for (const [key, value] of Object.entries(init.headers)) {
28+
writer.write(`${key}: ${value}\n`)
29+
}
30+
}
31+
}
32+
33+
if (init?.body) {
34+
writer.write(`\n${init.body}`)
35+
}
36+
writer.flush()
37+
const response = await originalFetch(input, init)
38+
const clonedResponse = response.clone()
39+
writer.write(`\nHTTP/1.1 ${response.status} ${response.statusText}\n`)
40+
response.headers.forEach((value, key) => {
41+
writer.write(`${key}: ${value}\n`)
42+
})
43+
if (clonedResponse.body) {
44+
clonedResponse.text().then(async (x) => {
45+
writer.write(`\n${x}\n`)
46+
})
47+
}
48+
writer.flush()
49+
50+
return response
51+
}
52+
}
53+
}

packages/opencode/src/util/log.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export namespace Log {
5050

5151
export interface Options {
5252
print: boolean
53+
dev?: boolean
5354
level?: Level
5455
}
5556

@@ -63,7 +64,10 @@ export namespace Log {
6364
await fs.mkdir(dir, { recursive: true })
6465
cleanup(dir)
6566
if (options.print) return
66-
logpath = path.join(dir, new Date().toISOString().split(".")[0].replace(/:/g, "") + ".log")
67+
logpath = path.join(
68+
dir,
69+
options.dev ? "dev.log" : new Date().toISOString().split(".")[0].replace(/:/g, "") + ".log",
70+
)
6771
const logfile = Bun.file(logpath)
6872
await fs.truncate(logpath).catch(() => {})
6973
const writer = logfile.writer()
@@ -75,15 +79,16 @@ export namespace Log {
7579
}
7680

7781
async function cleanup(dir: string) {
78-
const entries = await fs.readdir(dir, { withFileTypes: true })
79-
const files = entries
80-
.filter((entry) => entry.isFile() && entry.name.endsWith(".log"))
81-
.map((entry) => path.join(dir, entry.name))
82-
82+
const glob = new Bun.Glob("????-??-??T??????.log")
83+
const files = await Array.fromAsync(
84+
glob.scan({
85+
cwd: dir,
86+
absolute: true,
87+
}),
88+
)
8389
if (files.length <= 5) return
8490

8591
const filesToDelete = files.slice(0, -10)
86-
8792
await Promise.all(filesToDelete.map((file) => fs.unlink(file).catch(() => {})))
8893
}
8994

packages/tui/internal/app/app.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ type SendMsg struct {
6868
type SetEditorContentMsg struct {
6969
Text string
7070
}
71-
type OptimisticMessageAddedMsg struct {
72-
Message opencode.MessageUnion
73-
}
7471
type FileRenderedMsg struct {
7572
FilePath string
7673
}
@@ -508,7 +505,6 @@ func (a *App) SendChatMessage(
508505
}
509506

510507
a.Messages = append(a.Messages, Message{Info: message, Parts: parts})
511-
cmds = append(cmds, util.CmdHandler(OptimisticMessageAddedMsg{Message: message}))
512508

513509
cmds = append(cmds, func() tea.Msg {
514510
partsParam := []opencode.SessionChatParamsPartUnion{}

0 commit comments

Comments
 (0)