Skip to content

Commit 7d17476

Browse files
committed
first pass making system prompt less fast
1 parent c5eefd1 commit 7d17476

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

packages/opencode/src/external/fzf.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@ export namespace Fzf {
116116
return filepath
117117
}
118118

119-
export async function search(cwd: string, query: string) {
120-
const results = await $`${await filepath()} --filter ${query}`
119+
export async function search(input: {
120+
cwd: string
121+
query: string
122+
limit?: number
123+
}) {
124+
const results = await $`${await filepath()} --filter=${input.query}`
121125
.quiet()
122126
.throws(false)
123-
.cwd(cwd)
127+
.cwd(input.cwd)
124128
.text()
125129
const split = results
126130
.trim()

packages/opencode/src/external/ripgrep.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from "fs/promises"
55
import { z } from "zod"
66
import { NamedError } from "../util/error"
77
import { lazy } from "../util/lazy"
8+
import { $ } from "bun"
89

910
export namespace Ripgrep {
1011
const PLATFORM = {
@@ -111,4 +112,12 @@ export namespace Ripgrep {
111112
const { filepath } = await state()
112113
return filepath
113114
}
115+
116+
export async function files(cwd: string) {
117+
const result =
118+
await $`${await filepath()} --files --hidden --glob '!.git/*'`
119+
.cwd(cwd)
120+
.text()
121+
return result.split("\n").filter(Boolean)
122+
}
114123
}

packages/opencode/src/server/server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ export namespace Server {
457457
async (c) => {
458458
const body = c.req.valid("json")
459459
const app = App.info()
460-
const result = await Fzf.search(app.path.cwd, body.query)
460+
const result = await Fzf.search({
461+
cwd: app.path.cwd,
462+
query: body.query,
463+
limit: 10,
464+
})
461465
return c.json(result)
462466
},
463467
)

packages/opencode/src/session/system.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { App } from "../app/app"
2+
import { Fzf } from "../external/fzf"
3+
import { Ripgrep } from "../external/ripgrep"
24
import { ListTool } from "../tool/ls"
35
import { Filesystem } from "../util/filesystem"
46

@@ -22,8 +24,53 @@ export namespace SystemPrompt {
2224
return result
2325
}
2426

25-
export async function environment(sessionID: string) {
27+
export async function environment() {
2628
const app = App.info()
29+
30+
const tree = async () => {
31+
const files = await Ripgrep.files(app.path.cwd)
32+
type Node = {
33+
children: Record<string, Node>
34+
}
35+
const root: Node = {
36+
children: {},
37+
}
38+
for (const file of files) {
39+
const parts = file.split("/")
40+
let node = root
41+
for (const part of parts) {
42+
const existing = node.children[part]
43+
if (existing) {
44+
node = existing
45+
continue
46+
}
47+
node.children[part] = {
48+
children: {},
49+
}
50+
node = node.children[part]
51+
}
52+
}
53+
54+
function render(path: string[], node: Node): string {
55+
const lines: string[] = []
56+
const entries = Object.entries(node.children).sort(([a], [b]) =>
57+
a.localeCompare(b),
58+
)
59+
60+
for (const [name, child] of entries) {
61+
const currentPath = [...path, name]
62+
const indent = "\t".repeat(path.length)
63+
const hasChildren = Object.keys(child.children).length > 0
64+
lines.push(`${indent}${name}` + (hasChildren ? "/" : ""))
65+
66+
if (hasChildren) lines.push(render(currentPath, child))
67+
}
68+
69+
return lines.join("\n")
70+
}
71+
return render([], root)
72+
}
73+
2774
return [
2875
[
2976
`Here is some useful information about the environment you are running in:`,
@@ -34,7 +81,7 @@ export namespace SystemPrompt {
3481
` Today's date: ${new Date().toDateString()}`,
3582
`</env>`,
3683
`<project>`,
37-
` ${app.git ? await ListTool.execute({ path: app.path.cwd, ignore: [] }, { sessionID: sessionID, messageID: "", abort: AbortSignal.any([]) }).then((x) => x.output) : ""}`,
84+
` ${app.git ? await tree() : ""}`,
3885
`</project>`,
3986
].join("\n"),
4087
]

packages/opencode/src/tool/ls.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { App } from "../app/app"
44
import * as path from "path"
55
import DESCRIPTION from "./ls.txt"
66

7-
const IGNORE_PATTERNS = [
7+
export const IGNORE_PATTERNS = [
88
"node_modules/",
99
"__pycache__/",
1010
".git/",
@@ -18,6 +18,8 @@ const IGNORE_PATTERNS = [
1818
".vscode/",
1919
]
2020

21+
const LIMIT = 100
22+
2123
export const ListTool = Tool.define({
2224
id: "opencode.list",
2325
description: DESCRIPTION,
@@ -45,7 +47,7 @@ export const ListTool = Tool.define({
4547
if (params.ignore?.some((pattern) => new Bun.Glob(pattern).match(file)))
4648
continue
4749
files.push(file)
48-
if (files.length >= 1000) break
50+
if (files.length >= LIMIT) break
4951
}
5052

5153
// Build directory structure
@@ -99,7 +101,7 @@ export const ListTool = Tool.define({
99101
return {
100102
metadata: {
101103
count: files.length,
102-
truncated: files.length >= 1000,
104+
truncated: files.length >= LIMIT,
103105
title: path.relative(app.path.root, searchPath),
104106
},
105107
output,

0 commit comments

Comments
 (0)