Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/opencode/src/file/ripgrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,15 @@ export namespace Ripgrep {
}
}

const treeCache = new Map<string, { time: number; result: string }>()

export async function tree(input: { cwd: string; limit?: number }) {
const cacheKey = `${input.cwd}:${input.limit ?? 50}`
const cached = treeCache.get(cacheKey)
if (cached && Date.now() - cached.time < 5000) {
return cached.result
}

log.info("tree", input)
const files = await Array.fromAsync(Ripgrep.files({ cwd: input.cwd }))
interface Node {
Expand Down Expand Up @@ -364,7 +372,10 @@ export namespace Ripgrep {
}
result.children.map((x) => render(x, 0))

return lines.join("\n")
const resultString = lines.join("\n")
treeCache.set(cacheKey, { time: Date.now(), result: resultString })

return resultString
}

export async function search(input: {
Expand Down
17 changes: 16 additions & 1 deletion packages/opencode/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,25 @@ export namespace Session {
}),
])

/**
* Memory buffer for parts being updated with deltas to avoid high-frequency disk I/O.
* Flushed on text-end/reasoning-end or periodic interval.
*/
const partBuffer = new Map<string, MessageV2.Part>()

export const updatePart = fn(UpdatePartInput, async (input) => {
const part = "delta" in input ? input.part : input
const delta = "delta" in input ? input.delta : undefined
await Storage.write(["part", part.messageID, part.id], part)

// If it's a delta update, we buffer the write to disk
if (delta !== undefined) {
partBuffer.set(part.id, part)
} else {
// Otherwise, we flush the buffer for this part and write to storage
partBuffer.delete(part.id)
await Storage.write(["part", part.messageID, part.id], part)
}

Bus.publish(MessageV2.Event.PartUpdated, {
part,
delta,
Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export namespace Storage {
using _ = await Lock.write(target)
const content = await Bun.file(target).json()
fn(content)
await Bun.write(target, JSON.stringify(content, null, 2))
await Bun.write(target, JSON.stringify(content))
return content as T
})
}
Expand All @@ -193,7 +193,7 @@ export namespace Storage {
const target = path.join(dir, ...key) + ".json"
return withErrorHandling(async () => {
using _ = await Lock.write(target)
await Bun.write(target, JSON.stringify(content, null, 2))
await Bun.write(target, JSON.stringify(content))
})
}

Expand Down