Skip to content

Commit 1d7fc7c

Browse files
committed
Allow CodebuffClient to automatically discover projectFiles!
1 parent cf966d6 commit 1d7fc7c

File tree

7 files changed

+438
-2
lines changed

7 files changed

+438
-2
lines changed

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
"scripts": {
2525
"build": "bun run scripts/build.ts",
2626
"build:types": "tsc -p tsconfig.build.json",
27-
"build:verify": "bun run build && bun run smoke-test:dist && bun run test:cjs && bun run test:esm && bun run test:ripgrep",
27+
"build:verify": "bun run build && bun run smoke-test:dist && bun run test:cjs && bun run test:esm && bun run test:ripgrep && bun run test:tree-sitter-queries",
2828
"test:typecheck-strict": "tsc --noEmit --strict dist/index.d.ts",
2929
"smoke-test:dist": "bun run smoke-test-dist.ts",
3030
"test:cjs": "cd test/cjs-compatibility && npm install --silent && npm run test:all",
3131
"test:esm": "cd test/esm-compatibility && npm install --silent && npm run test:all",
3232
"test:ripgrep": "cd test/ripgrep-bundling && npm install --silent && npm run test:all",
33+
"test:tree-sitter-queries": "cd test/tree-sitter-queries && npm install --silent && npm run test:all",
3334
"clean": "rm -rf dist",
3435
"prepare-dist": "bun run scripts/publish.ts --dry-run",
3536
"publish-sdk": "bun run scripts/publish.ts --public",
@@ -63,6 +64,7 @@
6364
"@vscode/tree-sitter-wasm": "0.1.4",
6465
"ai": "^5.0.0",
6566
"diff": "8.0.2",
67+
"ignore": "7.0.5",
6668
"web-tree-sitter": "0.25.6",
6769
"ws": "^8.18.0",
6870
"zod": "^4.0.0"

sdk/src/run-state.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as os from 'os'
2+
import * as fs from 'fs'
23

34
import { getFileTokenScores } from '@codebuff/code-map/parse'
5+
import {
6+
getProjectFileTree,
7+
getAllFilePaths,
8+
} from '../../common/src/project-file-tree'
49

510
import { type CustomToolDefinition } from './custom-tool'
611
import { getInitialSessionState } from '../../common/src/types/session-state'
@@ -99,6 +104,27 @@ async function computeProjectIndex(
99104
return { fileTree, fileTokenScores, tokenCallers }
100105
}
101106

107+
/**
108+
* Discovers project files using .gitignore patterns when projectFiles is undefined
109+
*/
110+
function discoverProjectFiles(cwd: string): Record<string, string> {
111+
try {
112+
const fileTree = getProjectFileTree(cwd)
113+
const filePaths = getAllFilePaths(fileTree)
114+
115+
// Create projectFiles with empty content - the token scorer will read from disk
116+
return Object.fromEntries(
117+
filePaths.map((filePath) => [
118+
filePath,
119+
fs.readFileSync(filePath, 'utf8'),
120+
]),
121+
)
122+
} catch (error) {
123+
console.warn('Failed to discover project files:', error)
124+
return {}
125+
}
126+
}
127+
102128
/**
103129
* Auto-derives knowledge files from project files if knowledgeFiles is undefined
104130
*/
@@ -128,9 +154,13 @@ export async function initialSessionState(
128154
maxAgentSteps?: number
129155
},
130156
) {
131-
const { projectFiles = {}, agentDefinitions = [] } = options
157+
let { projectFiles, agentDefinitions = [] } = options
132158
let { knowledgeFiles } = options
133159

160+
// Auto-discover project files if not provided
161+
if (projectFiles === undefined) {
162+
projectFiles = discoverProjectFiles(cwd)
163+
}
134164
if (knowledgeFiles === undefined) {
135165
knowledgeFiles = deriveKnowledgeFiles(projectFiles)
136166
}

sdk/test/test-sdk.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getUserCredentials } from '@codebuff/npm-app/credentials'
2+
3+
import { CodebuffClient } from '../src/client'
4+
5+
export async function testSdk() {
6+
const apiKey = getUserCredentials()?.authToken
7+
if (!apiKey) {
8+
throw new Error('Could not load API key from user credentials')
9+
}
10+
11+
const client = new CodebuffClient({
12+
apiKey,
13+
})
14+
15+
const run = await client.run({
16+
agent: 'base',
17+
prompt: 'Create a simple calculator class',
18+
handleEvent: (event) => {
19+
console.log(event)
20+
},
21+
})
22+
23+
console.log(run)
24+
}
25+
26+
testSdk()

sdk/test/tree-sitter-queries/package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@codebuff/sdk-tree-sitter-queries-test",
3+
"version": "0.0.1",
4+
"private": true,
5+
"type": "commonjs",
6+
"scripts": {
7+
"test:all": "node test-query-files.js"
8+
},
9+
"dependencies": {
10+
"@codebuff/sdk": "file:../.."
11+
}
12+
}

0 commit comments

Comments
 (0)