Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.2.17
bun-version: 1.2.19

- run: bun install

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-vscode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.2.17
bun-version: 1.2.19

- run: git fetch --force --tags
- run: bun install -g @vscode/vsce
Expand Down
6 changes: 6 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "opencode",
"private": true,
"type": "module",
"packageManager": "bun@1.2.14",
"packageManager": "bun@1.2.19",
"scripts": {
"dev": "bun run --conditions=development packages/opencode/src/index.ts",
"typecheck": "bun run --filter='*' typecheck",
Expand Down Expand Up @@ -46,7 +46,10 @@
"trustedDependencies": [
"esbuild",
"protobufjs",
"sharp"
"sharp",
"tree-sitter",
"tree-sitter-bash",
"web-tree-sitter"
],
"patchedDependencies": {}
}
1 change: 1 addition & 0 deletions packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"remeda": "catalog:",
"tree-sitter": "0.22.4",
"tree-sitter-bash": "0.23.3",
"web-tree-sitter": "0.22.6",
"turndown": "7.2.0",
"vscode-jsonrpc": "8.2.1",
"xdg-basedir": "5.1.0",
Expand Down
27 changes: 22 additions & 5 deletions packages/opencode/src/tool/bash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ const MAX_TIMEOUT = 10 * 60 * 1000
const log = Log.create({ service: "bash-tool" })

const parser = lazy(async () => {
const { default: Parser } = await import("tree-sitter")
const Bash = await import("tree-sitter-bash")
const p = new Parser()
p.setLanguage(Bash.language as any)
return p
try {
const { default: Parser } = await import("tree-sitter")
const Bash = await import("tree-sitter-bash")
const p = new Parser()
p.setLanguage(Bash.language as any)
return p
} catch (e) {
const { default: Parser } = await import("web-tree-sitter")
const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, { with: { type: "wasm" } })
await Parser.init({
locateFile() {
return treeWasm
},
})
const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
with: { type: "wasm" },
})
const bashLanguage = await Parser.Language.load(bashWasm)
const p = new Parser()
p.setLanguage(bashLanguage)
return p
}
})

export const BashTool = Tool.define("bash", {
Expand Down
69 changes: 43 additions & 26 deletions packages/opencode/src/tool/test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,70 @@
import Parser from "tree-sitter";
import Bash from "tree-sitter-bash";

const parser = new Parser();
parser.setLanguage(Bash.language as any);
const parser = async () => {
try {
const { default: Parser } = await import("tree-sitter")
const Bash = await import("tree-sitter-bash")
const p = new Parser()
p.setLanguage(Bash.language as any)
return p
} catch (e) {
const { default: Parser } = await import("web-tree-sitter")
const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, { with: { type: "wasm" } })
await Parser.init({
locateFile() {
return treeWasm
},
})
const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
with: { type: "wasm" },
})
const bashLanguage = await Parser.Language.load(bashWasm)
const p = new Parser()
p.setLanguage(bashLanguage)
return p
}
}

const sourceCode = `cd --foo foo/bar && echo "hello" && cd ../baz`;
const sourceCode = `cd --foo foo/bar && echo "hello" && cd ../baz`

const tree = parser.parse(sourceCode);
const tree = await parser().then((p) => p.parse(sourceCode))

// Function to extract commands and arguments
function extractCommands(
node: any,
): Array<{ command: string; args: string[] }> {
const commands: Array<{ command: string; args: string[] }> = [];
function extractCommands(node: any): Array<{ command: string; args: string[] }> {
const commands: Array<{ command: string; args: string[] }> = []

function traverse(node: any) {
if (node.type === "command") {
const commandNode = node.child(0);
const commandNode = node.child(0)
if (commandNode) {
const command = commandNode.text;
const args: string[] = [];
const command = commandNode.text
const args: string[] = []

// Extract arguments
for (let i = 1; i < node.childCount; i++) {
const child = node.child(i);
const child = node.child(i)
if (child && child.type === "word") {
args.push(child.text);
args.push(child.text)
}
}

commands.push({ command, args });
commands.push({ command, args })
}
}

// Traverse children
for (let i = 0; i < node.childCount; i++) {
traverse(node.child(i));
traverse(node.child(i))
}
}

traverse(node);
return commands;
traverse(node)
return commands
}

// Extract and display commands
console.log("Source code: " + sourceCode);
const commands = extractCommands(tree.rootNode);
console.log("Extracted commands:");
console.log("Source code: " + sourceCode)
const commands = extractCommands(tree.rootNode)
console.log("Extracted commands:")
commands.forEach((cmd, index) => {
console.log(`${index + 1}. Command: ${cmd.command}`);
console.log(` Args: [${cmd.args.join(", ")}]`);
});
console.log(`${index + 1}. Command: ${cmd.command}`)
console.log(` Args: [${cmd.args.join(", ")}]`)
})
Loading